David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 1 | // Copyright (C) 2017 The Android Open Source Project |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 15 | #include "packages/UidMap.h" |
David Chen | 2158296 | 2017-11-01 17:32:46 -0700 | [diff] [blame] | 16 | #include "StatsLogProcessor.h" |
David Chen | d689689 | 2017-10-25 11:49:03 -0700 | [diff] [blame] | 17 | #include "config/ConfigKey.h" |
David Chen | c136f45 | 2017-11-27 11:52:26 -0800 | [diff] [blame] | 18 | #include "guardrail/StatsdStats.h" |
David Chen | 2158296 | 2017-11-01 17:32:46 -0700 | [diff] [blame] | 19 | #include "logd/LogEvent.h" |
Yangster-mac | 9def8e3 | 2018-04-17 13:55:51 -0700 | [diff] [blame] | 20 | #include "hash.h" |
Jeffrey Huang | 3eb84d4 | 2020-03-17 10:31:22 -0700 | [diff] [blame] | 21 | #include "statslog_statsdtest.h" |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 22 | #include "statsd_test_util.h" |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 23 | |
yro | 4beccbe | 2018-03-15 19:42:05 -0700 | [diff] [blame] | 24 | #include <android/util/ProtoOutputStream.h> |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 25 | #include <gtest/gtest.h> |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 26 | |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 27 | #include <stdio.h> |
| 28 | |
| 29 | using namespace android; |
David Chen | d689689 | 2017-10-25 11:49:03 -0700 | [diff] [blame] | 30 | |
| 31 | namespace android { |
| 32 | namespace os { |
| 33 | namespace statsd { |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 34 | |
yro | 4beccbe | 2018-03-15 19:42:05 -0700 | [diff] [blame] | 35 | using android::util::ProtoOutputStream; |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 36 | using android::util::ProtoReader; |
yro | 4beccbe | 2018-03-15 19:42:05 -0700 | [diff] [blame] | 37 | |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 38 | #ifdef __ANDROID__ |
| 39 | const string kApp1 = "app1.sharing.1"; |
| 40 | const string kApp2 = "app2.sharing.1"; |
| 41 | |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 42 | TEST(UidMapTest, TestIsolatedUID) { |
| 43 | sp<UidMap> m = new UidMap(); |
| 44 | sp<StatsPullerManager> pullerManager = new StatsPullerManager(); |
| 45 | sp<AlarmMonitor> anomalyAlarmMonitor; |
| 46 | sp<AlarmMonitor> subscriberAlarmMonitor; |
| 47 | // Construct the processor with a dummy sendBroadcast function that does nothing. |
| 48 | StatsLogProcessor p( |
| 49 | m, pullerManager, anomalyAlarmMonitor, subscriberAlarmMonitor, 0, |
| 50 | [](const ConfigKey& key) { return true; }, |
| 51 | [](const int&, const vector<int64_t>&) { return true; }); |
| 52 | |
| 53 | std::unique_ptr<LogEvent> addEvent = CreateIsolatedUidChangedEvent( |
| 54 | 1 /*timestamp*/, 100 /*hostUid*/, 101 /*isolatedUid*/, 1 /*is_create*/); |
| 55 | EXPECT_EQ(101, m->getHostUidOrSelf(101)); |
| 56 | p.OnLogEvent(addEvent.get()); |
| 57 | EXPECT_EQ(100, m->getHostUidOrSelf(101)); |
| 58 | |
| 59 | std::unique_ptr<LogEvent> removeEvent = CreateIsolatedUidChangedEvent( |
| 60 | 1 /*timestamp*/, 100 /*hostUid*/, 101 /*isolatedUid*/, 0 /*is_create*/); |
| 61 | p.OnLogEvent(removeEvent.get()); |
| 62 | EXPECT_EQ(101, m->getHostUidOrSelf(101)); |
| 63 | } |
David Chen | 2158296 | 2017-11-01 17:32:46 -0700 | [diff] [blame] | 64 | |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 65 | TEST(UidMapTest, TestMatching) { |
| 66 | UidMap m; |
| 67 | vector<int32_t> uids; |
Dianne Hackborn | 3accca0 | 2013-09-20 09:32:11 -0700 | [diff] [blame] | 68 | vector<int64_t> versions; |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 69 | vector<String16> apps; |
dwchen | 730403e | 2018-10-29 11:41:56 -0700 | [diff] [blame] | 70 | vector<String16> versionStrings; |
| 71 | vector<String16> installers; |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 72 | |
| 73 | uids.push_back(1000); |
| 74 | uids.push_back(1000); |
dwchen | 730403e | 2018-10-29 11:41:56 -0700 | [diff] [blame] | 75 | versionStrings.push_back(String16("v1")); |
| 76 | versionStrings.push_back(String16("v1")); |
| 77 | installers.push_back(String16("")); |
| 78 | installers.push_back(String16("")); |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 79 | apps.push_back(String16(kApp1.c_str())); |
| 80 | apps.push_back(String16(kApp2.c_str())); |
| 81 | versions.push_back(4); |
| 82 | versions.push_back(5); |
dwchen | 730403e | 2018-10-29 11:41:56 -0700 | [diff] [blame] | 83 | m.updateMap(1, uids, versions, versionStrings, apps, installers); |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 84 | EXPECT_TRUE(m.hasApp(1000, kApp1)); |
| 85 | EXPECT_TRUE(m.hasApp(1000, kApp2)); |
| 86 | EXPECT_FALSE(m.hasApp(1000, "not.app")); |
Yangster | 9df9a7f | 2017-12-18 13:33:05 -0800 | [diff] [blame] | 87 | |
| 88 | std::set<string> name_set = m.getAppNamesFromUid(1000u, true /* returnNormalized */); |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 89 | ASSERT_EQ(name_set.size(), 2u); |
Yangster | 9df9a7f | 2017-12-18 13:33:05 -0800 | [diff] [blame] | 90 | EXPECT_TRUE(name_set.find(kApp1) != name_set.end()); |
| 91 | EXPECT_TRUE(name_set.find(kApp2) != name_set.end()); |
| 92 | |
| 93 | name_set = m.getAppNamesFromUid(12345, true /* returnNormalized */); |
| 94 | EXPECT_TRUE(name_set.empty()); |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | TEST(UidMapTest, TestAddAndRemove) { |
| 98 | UidMap m; |
| 99 | vector<int32_t> uids; |
Dianne Hackborn | 3accca0 | 2013-09-20 09:32:11 -0700 | [diff] [blame] | 100 | vector<int64_t> versions; |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 101 | vector<String16> apps; |
dwchen | 730403e | 2018-10-29 11:41:56 -0700 | [diff] [blame] | 102 | vector<String16> versionStrings; |
| 103 | vector<String16> installers; |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 104 | |
| 105 | uids.push_back(1000); |
| 106 | uids.push_back(1000); |
dwchen | 730403e | 2018-10-29 11:41:56 -0700 | [diff] [blame] | 107 | versionStrings.push_back(String16("v1")); |
| 108 | versionStrings.push_back(String16("v1")); |
| 109 | installers.push_back(String16("")); |
| 110 | installers.push_back(String16("")); |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 111 | apps.push_back(String16(kApp1.c_str())); |
| 112 | apps.push_back(String16(kApp2.c_str())); |
| 113 | versions.push_back(4); |
| 114 | versions.push_back(5); |
dwchen | 730403e | 2018-10-29 11:41:56 -0700 | [diff] [blame] | 115 | m.updateMap(1, uids, versions, versionStrings, apps, installers); |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 116 | |
Yangster | 9df9a7f | 2017-12-18 13:33:05 -0800 | [diff] [blame] | 117 | std::set<string> name_set = m.getAppNamesFromUid(1000, true /* returnNormalized */); |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 118 | ASSERT_EQ(name_set.size(), 2u); |
Yangster | 9df9a7f | 2017-12-18 13:33:05 -0800 | [diff] [blame] | 119 | EXPECT_TRUE(name_set.find(kApp1) != name_set.end()); |
| 120 | EXPECT_TRUE(name_set.find(kApp2) != name_set.end()); |
| 121 | |
| 122 | // Update the app1 version. |
dwchen | 730403e | 2018-10-29 11:41:56 -0700 | [diff] [blame] | 123 | m.updateApp(2, String16(kApp1.c_str()), 1000, 40, String16("v40"), String16("")); |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 124 | EXPECT_EQ(40, m.getAppVersion(1000, kApp1)); |
| 125 | |
Yangster | 9df9a7f | 2017-12-18 13:33:05 -0800 | [diff] [blame] | 126 | name_set = m.getAppNamesFromUid(1000, true /* returnNormalized */); |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 127 | ASSERT_EQ(name_set.size(), 2u); |
Yangster | 9df9a7f | 2017-12-18 13:33:05 -0800 | [diff] [blame] | 128 | EXPECT_TRUE(name_set.find(kApp1) != name_set.end()); |
| 129 | EXPECT_TRUE(name_set.find(kApp2) != name_set.end()); |
| 130 | |
David Chen | bd12527 | 2018-04-04 19:02:50 -0700 | [diff] [blame] | 131 | m.removeApp(3, String16(kApp1.c_str()), 1000); |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 132 | EXPECT_FALSE(m.hasApp(1000, kApp1)); |
| 133 | EXPECT_TRUE(m.hasApp(1000, kApp2)); |
Yangster | 9df9a7f | 2017-12-18 13:33:05 -0800 | [diff] [blame] | 134 | name_set = m.getAppNamesFromUid(1000, true /* returnNormalized */); |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 135 | ASSERT_EQ(name_set.size(), 1u); |
Yangster | 9df9a7f | 2017-12-18 13:33:05 -0800 | [diff] [blame] | 136 | EXPECT_TRUE(name_set.find(kApp1) == name_set.end()); |
| 137 | EXPECT_TRUE(name_set.find(kApp2) != name_set.end()); |
| 138 | |
| 139 | // Remove app2. |
David Chen | bd12527 | 2018-04-04 19:02:50 -0700 | [diff] [blame] | 140 | m.removeApp(4, String16(kApp2.c_str()), 1000); |
Yangster | 9df9a7f | 2017-12-18 13:33:05 -0800 | [diff] [blame] | 141 | EXPECT_FALSE(m.hasApp(1000, kApp1)); |
| 142 | EXPECT_FALSE(m.hasApp(1000, kApp2)); |
| 143 | name_set = m.getAppNamesFromUid(1000, true /* returnNormalized */); |
| 144 | EXPECT_TRUE(name_set.empty()); |
| 145 | } |
| 146 | |
| 147 | TEST(UidMapTest, TestUpdateApp) { |
| 148 | UidMap m; |
dwchen | 730403e | 2018-10-29 11:41:56 -0700 | [diff] [blame] | 149 | m.updateMap(1, {1000, 1000}, {4, 5}, {String16("v4"), String16("v5")}, |
| 150 | {String16(kApp1.c_str()), String16(kApp2.c_str())}, {String16(""), String16("")}); |
Yangster | 9df9a7f | 2017-12-18 13:33:05 -0800 | [diff] [blame] | 151 | std::set<string> name_set = m.getAppNamesFromUid(1000, true /* returnNormalized */); |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 152 | ASSERT_EQ(name_set.size(), 2u); |
Yangster | 9df9a7f | 2017-12-18 13:33:05 -0800 | [diff] [blame] | 153 | EXPECT_TRUE(name_set.find(kApp1) != name_set.end()); |
| 154 | EXPECT_TRUE(name_set.find(kApp2) != name_set.end()); |
| 155 | |
| 156 | // Adds a new name for uid 1000. |
dwchen | 730403e | 2018-10-29 11:41:56 -0700 | [diff] [blame] | 157 | m.updateApp(2, String16("NeW_aPP1_NAmE"), 1000, 40, String16("v40"), String16("")); |
Yangster | 9df9a7f | 2017-12-18 13:33:05 -0800 | [diff] [blame] | 158 | name_set = m.getAppNamesFromUid(1000, true /* returnNormalized */); |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 159 | ASSERT_EQ(name_set.size(), 3u); |
Yangster | 9df9a7f | 2017-12-18 13:33:05 -0800 | [diff] [blame] | 160 | EXPECT_TRUE(name_set.find(kApp1) != name_set.end()); |
| 161 | EXPECT_TRUE(name_set.find(kApp2) != name_set.end()); |
| 162 | EXPECT_TRUE(name_set.find("NeW_aPP1_NAmE") == name_set.end()); |
| 163 | EXPECT_TRUE(name_set.find("new_app1_name") != name_set.end()); |
| 164 | |
| 165 | // This name is also reused by another uid 2000. |
dwchen | 730403e | 2018-10-29 11:41:56 -0700 | [diff] [blame] | 166 | m.updateApp(3, String16("NeW_aPP1_NAmE"), 2000, 1, String16("v1"), String16("")); |
Yangster | 9df9a7f | 2017-12-18 13:33:05 -0800 | [diff] [blame] | 167 | name_set = m.getAppNamesFromUid(2000, true /* returnNormalized */); |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 168 | ASSERT_EQ(name_set.size(), 1u); |
Yangster | 9df9a7f | 2017-12-18 13:33:05 -0800 | [diff] [blame] | 169 | EXPECT_TRUE(name_set.find("NeW_aPP1_NAmE") == name_set.end()); |
| 170 | EXPECT_TRUE(name_set.find("new_app1_name") != name_set.end()); |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 171 | } |
David Chen | d689689 | 2017-10-25 11:49:03 -0700 | [diff] [blame] | 172 | |
yro | 4beccbe | 2018-03-15 19:42:05 -0700 | [diff] [blame] | 173 | static void protoOutputStreamToUidMapping(ProtoOutputStream* proto, UidMapping* results) { |
| 174 | vector<uint8_t> bytes; |
| 175 | bytes.resize(proto->size()); |
| 176 | size_t pos = 0; |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 177 | sp<ProtoReader> reader = proto->data(); |
| 178 | while (reader->readBuffer() != NULL) { |
| 179 | size_t toRead = reader->currentToRead(); |
| 180 | std::memcpy(&((bytes)[pos]), reader->readBuffer(), toRead); |
yro | 4beccbe | 2018-03-15 19:42:05 -0700 | [diff] [blame] | 181 | pos += toRead; |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 182 | reader->move(toRead); |
yro | 4beccbe | 2018-03-15 19:42:05 -0700 | [diff] [blame] | 183 | } |
| 184 | results->ParseFromArray(bytes.data(), bytes.size()); |
| 185 | } |
| 186 | |
David Chen | 35045cb | 2018-03-23 22:21:47 -0700 | [diff] [blame] | 187 | // Test that uid map returns at least one snapshot even if we already obtained |
| 188 | // this snapshot from a previous call to getData. |
| 189 | TEST(UidMapTest, TestOutputIncludesAtLeastOneSnapshot) { |
| 190 | UidMap m; |
| 191 | // Initialize single config key. |
| 192 | ConfigKey config1(1, StringToId("config1")); |
| 193 | m.OnConfigUpdated(config1); |
| 194 | vector<int32_t> uids; |
| 195 | vector<int64_t> versions; |
| 196 | vector<String16> apps; |
dwchen | 730403e | 2018-10-29 11:41:56 -0700 | [diff] [blame] | 197 | vector<String16> versionStrings; |
| 198 | vector<String16> installers; |
David Chen | 35045cb | 2018-03-23 22:21:47 -0700 | [diff] [blame] | 199 | uids.push_back(1000); |
| 200 | apps.push_back(String16(kApp2.c_str())); |
dwchen | 730403e | 2018-10-29 11:41:56 -0700 | [diff] [blame] | 201 | versionStrings.push_back(String16("v1")); |
| 202 | installers.push_back(String16("")); |
David Chen | 35045cb | 2018-03-23 22:21:47 -0700 | [diff] [blame] | 203 | versions.push_back(5); |
dwchen | 730403e | 2018-10-29 11:41:56 -0700 | [diff] [blame] | 204 | m.updateMap(1, uids, versions, versionStrings, apps, installers); |
David Chen | 35045cb | 2018-03-23 22:21:47 -0700 | [diff] [blame] | 205 | |
| 206 | // Set the last timestamp for this config key to be newer. |
| 207 | m.mLastUpdatePerConfigKey[config1] = 2; |
| 208 | |
| 209 | ProtoOutputStream proto; |
dwchen | 730403e | 2018-10-29 11:41:56 -0700 | [diff] [blame] | 210 | m.appendUidMap(3, config1, nullptr, true, true, &proto); |
David Chen | 35045cb | 2018-03-23 22:21:47 -0700 | [diff] [blame] | 211 | |
| 212 | // Check there's still a uidmap attached this one. |
| 213 | UidMapping results; |
| 214 | protoOutputStreamToUidMapping(&proto, &results); |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 215 | ASSERT_EQ(1, results.snapshots_size()); |
dwchen | 730403e | 2018-10-29 11:41:56 -0700 | [diff] [blame] | 216 | EXPECT_EQ("v1", results.snapshots(0).package_info(0).version_string()); |
David Chen | 35045cb | 2018-03-23 22:21:47 -0700 | [diff] [blame] | 217 | } |
| 218 | |
David Chen | bd12527 | 2018-04-04 19:02:50 -0700 | [diff] [blame] | 219 | TEST(UidMapTest, TestRemovedAppRetained) { |
| 220 | UidMap m; |
| 221 | // Initialize single config key. |
| 222 | ConfigKey config1(1, StringToId("config1")); |
| 223 | m.OnConfigUpdated(config1); |
| 224 | vector<int32_t> uids; |
| 225 | vector<int64_t> versions; |
dwchen | 730403e | 2018-10-29 11:41:56 -0700 | [diff] [blame] | 226 | vector<String16> versionStrings; |
| 227 | vector<String16> installers; |
David Chen | bd12527 | 2018-04-04 19:02:50 -0700 | [diff] [blame] | 228 | vector<String16> apps; |
| 229 | uids.push_back(1000); |
| 230 | apps.push_back(String16(kApp2.c_str())); |
| 231 | versions.push_back(5); |
dwchen | 730403e | 2018-10-29 11:41:56 -0700 | [diff] [blame] | 232 | versionStrings.push_back(String16("v5")); |
| 233 | installers.push_back(String16("")); |
| 234 | m.updateMap(1, uids, versions, versionStrings, apps, installers); |
David Chen | bd12527 | 2018-04-04 19:02:50 -0700 | [diff] [blame] | 235 | m.removeApp(2, String16(kApp2.c_str()), 1000); |
| 236 | |
| 237 | ProtoOutputStream proto; |
dwchen | 730403e | 2018-10-29 11:41:56 -0700 | [diff] [blame] | 238 | m.appendUidMap(3, config1, nullptr, true, true, &proto); |
David Chen | bd12527 | 2018-04-04 19:02:50 -0700 | [diff] [blame] | 239 | |
| 240 | // Snapshot should still contain this item as deleted. |
| 241 | UidMapping results; |
| 242 | protoOutputStreamToUidMapping(&proto, &results); |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 243 | ASSERT_EQ(1, results.snapshots(0).package_info_size()); |
David Chen | bd12527 | 2018-04-04 19:02:50 -0700 | [diff] [blame] | 244 | EXPECT_EQ(true, results.snapshots(0).package_info(0).deleted()); |
| 245 | } |
| 246 | |
| 247 | TEST(UidMapTest, TestRemovedAppOverGuardrail) { |
| 248 | UidMap m; |
| 249 | // Initialize single config key. |
| 250 | ConfigKey config1(1, StringToId("config1")); |
| 251 | m.OnConfigUpdated(config1); |
| 252 | vector<int32_t> uids; |
| 253 | vector<int64_t> versions; |
dwchen | 730403e | 2018-10-29 11:41:56 -0700 | [diff] [blame] | 254 | vector<String16> versionStrings; |
| 255 | vector<String16> installers; |
David Chen | bd12527 | 2018-04-04 19:02:50 -0700 | [diff] [blame] | 256 | vector<String16> apps; |
| 257 | const int maxDeletedApps = StatsdStats::kMaxDeletedAppsInUidMap; |
| 258 | for (int j = 0; j < maxDeletedApps + 10; j++) { |
| 259 | uids.push_back(j); |
| 260 | apps.push_back(String16(kApp1.c_str())); |
| 261 | versions.push_back(j); |
dwchen | 730403e | 2018-10-29 11:41:56 -0700 | [diff] [blame] | 262 | versionStrings.push_back(String16("v")); |
| 263 | installers.push_back(String16("")); |
David Chen | bd12527 | 2018-04-04 19:02:50 -0700 | [diff] [blame] | 264 | } |
dwchen | 730403e | 2018-10-29 11:41:56 -0700 | [diff] [blame] | 265 | m.updateMap(1, uids, versions, versionStrings, apps, installers); |
David Chen | bd12527 | 2018-04-04 19:02:50 -0700 | [diff] [blame] | 266 | |
| 267 | // First, verify that we have the expected number of items. |
| 268 | UidMapping results; |
| 269 | ProtoOutputStream proto; |
dwchen | 730403e | 2018-10-29 11:41:56 -0700 | [diff] [blame] | 270 | m.appendUidMap(3, config1, nullptr, true, true, &proto); |
David Chen | bd12527 | 2018-04-04 19:02:50 -0700 | [diff] [blame] | 271 | protoOutputStreamToUidMapping(&proto, &results); |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 272 | ASSERT_EQ(maxDeletedApps + 10, results.snapshots(0).package_info_size()); |
David Chen | bd12527 | 2018-04-04 19:02:50 -0700 | [diff] [blame] | 273 | |
| 274 | // Now remove all the apps. |
dwchen | 730403e | 2018-10-29 11:41:56 -0700 | [diff] [blame] | 275 | m.updateMap(1, uids, versions, versionStrings, apps, installers); |
David Chen | bd12527 | 2018-04-04 19:02:50 -0700 | [diff] [blame] | 276 | for (int j = 0; j < maxDeletedApps + 10; j++) { |
| 277 | m.removeApp(4, String16(kApp1.c_str()), j); |
| 278 | } |
| 279 | |
| 280 | proto.clear(); |
dwchen | 730403e | 2018-10-29 11:41:56 -0700 | [diff] [blame] | 281 | m.appendUidMap(5, config1, nullptr, true, true, &proto); |
David Chen | bd12527 | 2018-04-04 19:02:50 -0700 | [diff] [blame] | 282 | // Snapshot drops the first nine items. |
| 283 | protoOutputStreamToUidMapping(&proto, &results); |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 284 | ASSERT_EQ(maxDeletedApps, results.snapshots(0).package_info_size()); |
David Chen | bd12527 | 2018-04-04 19:02:50 -0700 | [diff] [blame] | 285 | } |
| 286 | |
David Chen | d689689 | 2017-10-25 11:49:03 -0700 | [diff] [blame] | 287 | TEST(UidMapTest, TestClearingOutput) { |
| 288 | UidMap m; |
| 289 | |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 290 | ConfigKey config1(1, StringToId("config1")); |
| 291 | ConfigKey config2(1, StringToId("config2")); |
David Chen | d689689 | 2017-10-25 11:49:03 -0700 | [diff] [blame] | 292 | |
| 293 | m.OnConfigUpdated(config1); |
| 294 | |
| 295 | vector<int32_t> uids; |
Dianne Hackborn | 3accca0 | 2013-09-20 09:32:11 -0700 | [diff] [blame] | 296 | vector<int64_t> versions; |
dwchen | 730403e | 2018-10-29 11:41:56 -0700 | [diff] [blame] | 297 | vector<String16> versionStrings; |
| 298 | vector<String16> installers; |
David Chen | d689689 | 2017-10-25 11:49:03 -0700 | [diff] [blame] | 299 | vector<String16> apps; |
| 300 | uids.push_back(1000); |
| 301 | uids.push_back(1000); |
| 302 | apps.push_back(String16(kApp1.c_str())); |
| 303 | apps.push_back(String16(kApp2.c_str())); |
| 304 | versions.push_back(4); |
| 305 | versions.push_back(5); |
dwchen | 730403e | 2018-10-29 11:41:56 -0700 | [diff] [blame] | 306 | versionStrings.push_back(String16("v4")); |
| 307 | versionStrings.push_back(String16("v5")); |
| 308 | installers.push_back(String16("")); |
| 309 | installers.push_back(String16("")); |
| 310 | m.updateMap(1, uids, versions, versionStrings, apps, installers); |
David Chen | d689689 | 2017-10-25 11:49:03 -0700 | [diff] [blame] | 311 | |
yro | 4beccbe | 2018-03-15 19:42:05 -0700 | [diff] [blame] | 312 | ProtoOutputStream proto; |
dwchen | 730403e | 2018-10-29 11:41:56 -0700 | [diff] [blame] | 313 | m.appendUidMap(2, config1, nullptr, true, true, &proto); |
David Chen | f384b90 | 2018-03-14 18:36:45 -0700 | [diff] [blame] | 314 | UidMapping results; |
yro | 4beccbe | 2018-03-15 19:42:05 -0700 | [diff] [blame] | 315 | protoOutputStreamToUidMapping(&proto, &results); |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 316 | ASSERT_EQ(1, results.snapshots_size()); |
David Chen | d689689 | 2017-10-25 11:49:03 -0700 | [diff] [blame] | 317 | |
David Chen | 35045cb | 2018-03-23 22:21:47 -0700 | [diff] [blame] | 318 | // We have to keep at least one snapshot in memory at all times. |
yro | 4beccbe | 2018-03-15 19:42:05 -0700 | [diff] [blame] | 319 | proto.clear(); |
dwchen | 730403e | 2018-10-29 11:41:56 -0700 | [diff] [blame] | 320 | m.appendUidMap(2, config1, nullptr, true, true, &proto); |
yro | 4beccbe | 2018-03-15 19:42:05 -0700 | [diff] [blame] | 321 | protoOutputStreamToUidMapping(&proto, &results); |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 322 | ASSERT_EQ(1, results.snapshots_size()); |
David Chen | d689689 | 2017-10-25 11:49:03 -0700 | [diff] [blame] | 323 | |
| 324 | // Now add another configuration. |
| 325 | m.OnConfigUpdated(config2); |
dwchen | 730403e | 2018-10-29 11:41:56 -0700 | [diff] [blame] | 326 | m.updateApp(5, String16(kApp1.c_str()), 1000, 40, String16("v40"), String16("")); |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 327 | ASSERT_EQ(1U, m.mChanges.size()); |
yro | 4beccbe | 2018-03-15 19:42:05 -0700 | [diff] [blame] | 328 | proto.clear(); |
dwchen | 730403e | 2018-10-29 11:41:56 -0700 | [diff] [blame] | 329 | m.appendUidMap(6, config1, nullptr, true, true, &proto); |
yro | 4beccbe | 2018-03-15 19:42:05 -0700 | [diff] [blame] | 330 | protoOutputStreamToUidMapping(&proto, &results); |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 331 | ASSERT_EQ(1, results.snapshots_size()); |
| 332 | ASSERT_EQ(1, results.changes_size()); |
| 333 | ASSERT_EQ(1U, m.mChanges.size()); |
David Chen | d689689 | 2017-10-25 11:49:03 -0700 | [diff] [blame] | 334 | |
David Chen | c136f45 | 2017-11-27 11:52:26 -0800 | [diff] [blame] | 335 | // Add another delta update. |
dwchen | 730403e | 2018-10-29 11:41:56 -0700 | [diff] [blame] | 336 | m.updateApp(7, String16(kApp2.c_str()), 1001, 41, String16("v41"), String16("")); |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 337 | ASSERT_EQ(2U, m.mChanges.size()); |
David Chen | c136f45 | 2017-11-27 11:52:26 -0800 | [diff] [blame] | 338 | |
| 339 | // We still can't remove anything. |
yro | 4beccbe | 2018-03-15 19:42:05 -0700 | [diff] [blame] | 340 | proto.clear(); |
dwchen | 730403e | 2018-10-29 11:41:56 -0700 | [diff] [blame] | 341 | m.appendUidMap(8, config1, nullptr, true, true, &proto); |
yro | 4beccbe | 2018-03-15 19:42:05 -0700 | [diff] [blame] | 342 | protoOutputStreamToUidMapping(&proto, &results); |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 343 | ASSERT_EQ(1, results.snapshots_size()); |
| 344 | ASSERT_EQ(1, results.changes_size()); |
| 345 | ASSERT_EQ(2U, m.mChanges.size()); |
David Chen | d689689 | 2017-10-25 11:49:03 -0700 | [diff] [blame] | 346 | |
yro | 4beccbe | 2018-03-15 19:42:05 -0700 | [diff] [blame] | 347 | proto.clear(); |
dwchen | 730403e | 2018-10-29 11:41:56 -0700 | [diff] [blame] | 348 | m.appendUidMap(9, config2, nullptr, true, true, &proto); |
yro | 4beccbe | 2018-03-15 19:42:05 -0700 | [diff] [blame] | 349 | protoOutputStreamToUidMapping(&proto, &results); |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 350 | ASSERT_EQ(1, results.snapshots_size()); |
| 351 | ASSERT_EQ(2, results.changes_size()); |
David Chen | d689689 | 2017-10-25 11:49:03 -0700 | [diff] [blame] | 352 | // At this point both should be cleared. |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 353 | ASSERT_EQ(0U, m.mChanges.size()); |
David Chen | d689689 | 2017-10-25 11:49:03 -0700 | [diff] [blame] | 354 | } |
David Chen | c136f45 | 2017-11-27 11:52:26 -0800 | [diff] [blame] | 355 | |
| 356 | TEST(UidMapTest, TestMemoryComputed) { |
| 357 | UidMap m; |
| 358 | |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 359 | ConfigKey config1(1, StringToId("config1")); |
David Chen | c136f45 | 2017-11-27 11:52:26 -0800 | [diff] [blame] | 360 | m.OnConfigUpdated(config1); |
| 361 | |
| 362 | size_t startBytes = m.mBytesUsed; |
| 363 | vector<int32_t> uids; |
Dianne Hackborn | 3accca0 | 2013-09-20 09:32:11 -0700 | [diff] [blame] | 364 | vector<int64_t> versions; |
David Chen | c136f45 | 2017-11-27 11:52:26 -0800 | [diff] [blame] | 365 | vector<String16> apps; |
dwchen | 730403e | 2018-10-29 11:41:56 -0700 | [diff] [blame] | 366 | vector<String16> versionStrings; |
| 367 | vector<String16> installers; |
David Chen | c136f45 | 2017-11-27 11:52:26 -0800 | [diff] [blame] | 368 | uids.push_back(1000); |
| 369 | apps.push_back(String16(kApp1.c_str())); |
| 370 | versions.push_back(1); |
dwchen | 730403e | 2018-10-29 11:41:56 -0700 | [diff] [blame] | 371 | versionStrings.push_back(String16("v1")); |
| 372 | installers.push_back(String16("")); |
| 373 | m.updateMap(1, uids, versions, versionStrings, apps, installers); |
David Chen | c136f45 | 2017-11-27 11:52:26 -0800 | [diff] [blame] | 374 | |
dwchen | 730403e | 2018-10-29 11:41:56 -0700 | [diff] [blame] | 375 | m.updateApp(3, String16(kApp1.c_str()), 1000, 40, String16("v40"), String16("")); |
David Chen | c136f45 | 2017-11-27 11:52:26 -0800 | [diff] [blame] | 376 | |
yro | 4beccbe | 2018-03-15 19:42:05 -0700 | [diff] [blame] | 377 | ProtoOutputStream proto; |
David Chen | f384b90 | 2018-03-14 18:36:45 -0700 | [diff] [blame] | 378 | vector<uint8_t> bytes; |
dwchen | 730403e | 2018-10-29 11:41:56 -0700 | [diff] [blame] | 379 | m.appendUidMap(2, config1, nullptr, true, true, &proto); |
David Chen | c136f45 | 2017-11-27 11:52:26 -0800 | [diff] [blame] | 380 | size_t prevBytes = m.mBytesUsed; |
| 381 | |
dwchen | 730403e | 2018-10-29 11:41:56 -0700 | [diff] [blame] | 382 | m.appendUidMap(4, config1, nullptr, true, true, &proto); |
David Chen | c136f45 | 2017-11-27 11:52:26 -0800 | [diff] [blame] | 383 | EXPECT_TRUE(m.mBytesUsed < prevBytes); |
| 384 | } |
| 385 | |
| 386 | TEST(UidMapTest, TestMemoryGuardrail) { |
| 387 | UidMap m; |
| 388 | string buf; |
| 389 | |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 390 | ConfigKey config1(1, StringToId("config1")); |
David Chen | c136f45 | 2017-11-27 11:52:26 -0800 | [diff] [blame] | 391 | m.OnConfigUpdated(config1); |
| 392 | |
| 393 | size_t startBytes = m.mBytesUsed; |
| 394 | vector<int32_t> uids; |
Dianne Hackborn | 3accca0 | 2013-09-20 09:32:11 -0700 | [diff] [blame] | 395 | vector<int64_t> versions; |
dwchen | 730403e | 2018-10-29 11:41:56 -0700 | [diff] [blame] | 396 | vector<String16> versionStrings; |
| 397 | vector<String16> installers; |
David Chen | c136f45 | 2017-11-27 11:52:26 -0800 | [diff] [blame] | 398 | vector<String16> apps; |
| 399 | for (int i = 0; i < 100; i++) { |
| 400 | uids.push_back(1); |
| 401 | buf = "EXTREMELY_LONG_STRING_FOR_APP_TO_WASTE_MEMORY." + to_string(i); |
| 402 | apps.push_back(String16(buf.c_str())); |
| 403 | versions.push_back(1); |
dwchen | 730403e | 2018-10-29 11:41:56 -0700 | [diff] [blame] | 404 | versionStrings.push_back(String16("v1")); |
| 405 | installers.push_back(String16("")); |
David Chen | c136f45 | 2017-11-27 11:52:26 -0800 | [diff] [blame] | 406 | } |
dwchen | 730403e | 2018-10-29 11:41:56 -0700 | [diff] [blame] | 407 | m.updateMap(1, uids, versions, versionStrings, apps, installers); |
David Chen | c136f45 | 2017-11-27 11:52:26 -0800 | [diff] [blame] | 408 | |
dwchen | 730403e | 2018-10-29 11:41:56 -0700 | [diff] [blame] | 409 | m.updateApp(3, String16("EXTREMELY_LONG_STRING_FOR_APP_TO_WASTE_MEMORY.0"), 1000, 2, |
| 410 | String16("v2"), String16("")); |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 411 | ASSERT_EQ(1U, m.mChanges.size()); |
David Chen | c136f45 | 2017-11-27 11:52:26 -0800 | [diff] [blame] | 412 | |
| 413 | // Now force deletion by limiting the memory to hold one delta change. |
dwchen | 730403e | 2018-10-29 11:41:56 -0700 | [diff] [blame] | 414 | m.maxBytesOverride = 120; // Since the app string alone requires >45 characters. |
| 415 | m.updateApp(5, String16("EXTREMELY_LONG_STRING_FOR_APP_TO_WASTE_MEMORY.0"), 1000, 4, |
| 416 | String16("v4"), String16("")); |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 417 | ASSERT_EQ(1U, m.mChanges.size()); |
David Chen | c136f45 | 2017-11-27 11:52:26 -0800 | [diff] [blame] | 418 | } |
Yangster-mac | 9def8e3 | 2018-04-17 13:55:51 -0700 | [diff] [blame] | 419 | |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 420 | #else |
| 421 | GTEST_LOG_(INFO) << "This test does nothing.\n"; |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 422 | #endif |
David Chen | d689689 | 2017-10-25 11:49:03 -0700 | [diff] [blame] | 423 | |
| 424 | } // namespace statsd |
| 425 | } // namespace os |
yro | 4beccbe | 2018-03-15 19:42:05 -0700 | [diff] [blame] | 426 | } // namespace android |