blob: dde50c2597da755356a8f26dd161d45de7306c0c [file] [log] [blame]
David Chende701692017-10-05 13:16:02 -07001// 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 Onorato9fc9edf2017-10-15 20:08:52 -070015#include "packages/UidMap.h"
David Chen21582962017-11-01 17:32:46 -070016#include "StatsLogProcessor.h"
David Chend6896892017-10-25 11:49:03 -070017#include "config/ConfigKey.h"
David Chenc136f45a2017-11-27 11:52:26 -080018#include "guardrail/StatsdStats.h"
David Chen21582962017-11-01 17:32:46 -070019#include "logd/LogEvent.h"
Yangster-mac9def8e32018-04-17 13:55:51 -070020#include "hash.h"
David Chen21582962017-11-01 17:32:46 -070021#include "statslog.h"
Yangster-mac94e197c2018-01-02 16:03:03 -080022#include "statsd_test_util.h"
David Chende701692017-10-05 13:16:02 -070023
yro4beccbe2018-03-15 19:42:05 -070024#include <android/util/ProtoOutputStream.h>
David Chende701692017-10-05 13:16:02 -070025#include <gtest/gtest.h>
Joe Onorato9fc9edf2017-10-15 20:08:52 -070026
David Chende701692017-10-05 13:16:02 -070027#include <stdio.h>
28
29using namespace android;
David Chend6896892017-10-25 11:49:03 -070030
31namespace android {
32namespace os {
33namespace statsd {
David Chende701692017-10-05 13:16:02 -070034
yro4beccbe2018-03-15 19:42:05 -070035using android::util::ProtoOutputStream;
36
David Chende701692017-10-05 13:16:02 -070037#ifdef __ANDROID__
38const string kApp1 = "app1.sharing.1";
39const string kApp2 = "app2.sharing.1";
40
David Chen21582962017-11-01 17:32:46 -070041TEST(UidMapTest, TestIsolatedUID) {
42 sp<UidMap> m = new UidMap();
Yangster-mac932ecec2018-02-01 10:23:52 -080043 sp<AlarmMonitor> anomalyAlarmMonitor;
44 sp<AlarmMonitor> subscriberAlarmMonitor;
David Chenc136f45a2017-11-27 11:52:26 -080045 // Construct the processor with a dummy sendBroadcast function that does nothing.
Yangster-mac932ecec2018-02-01 10:23:52 -080046 StatsLogProcessor p(m, anomalyAlarmMonitor, subscriberAlarmMonitor, 0,
47 [](const ConfigKey& key) {});
David Chen21582962017-11-01 17:32:46 -070048 LogEvent addEvent(android::util::ISOLATED_UID_CHANGED, 1);
Yao Chen80235402017-11-13 20:42:25 -080049 addEvent.write(100); // parent UID
50 addEvent.write(101); // isolated UID
51 addEvent.write(1); // Indicates creation.
David Chen21582962017-11-01 17:32:46 -070052 addEvent.init();
53
Yangster-macd40053e2018-01-09 16:29:22 -080054 EXPECT_EQ(101, m->getHostUidOrSelf(101));
David Chen21582962017-11-01 17:32:46 -070055
Yangster-macd40053e2018-01-09 16:29:22 -080056 p.OnLogEvent(&addEvent);
57 EXPECT_EQ(100, m->getHostUidOrSelf(101));
David Chen21582962017-11-01 17:32:46 -070058
59 LogEvent removeEvent(android::util::ISOLATED_UID_CHANGED, 1);
Yao Chen80235402017-11-13 20:42:25 -080060 removeEvent.write(100); // parent UID
61 removeEvent.write(101); // isolated UID
62 removeEvent.write(0); // Indicates removal.
David Chen21582962017-11-01 17:32:46 -070063 removeEvent.init();
Yangster-macd40053e2018-01-09 16:29:22 -080064 p.OnLogEvent(&removeEvent);
65 EXPECT_EQ(101, m->getHostUidOrSelf(101));
David Chen21582962017-11-01 17:32:46 -070066}
67
David Chende701692017-10-05 13:16:02 -070068TEST(UidMapTest, TestMatching) {
69 UidMap m;
70 vector<int32_t> uids;
Dianne Hackborn3accca02013-09-20 09:32:11 -070071 vector<int64_t> versions;
David Chende701692017-10-05 13:16:02 -070072 vector<String16> apps;
73
74 uids.push_back(1000);
75 uids.push_back(1000);
76 apps.push_back(String16(kApp1.c_str()));
77 apps.push_back(String16(kApp2.c_str()));
78 versions.push_back(4);
79 versions.push_back(5);
David Chenbd125272018-04-04 19:02:50 -070080 m.updateMap(1, uids, versions, apps);
David Chende701692017-10-05 13:16:02 -070081 EXPECT_TRUE(m.hasApp(1000, kApp1));
82 EXPECT_TRUE(m.hasApp(1000, kApp2));
83 EXPECT_FALSE(m.hasApp(1000, "not.app"));
Yangster9df9a7f2017-12-18 13:33:05 -080084
85 std::set<string> name_set = m.getAppNamesFromUid(1000u, true /* returnNormalized */);
86 EXPECT_EQ(name_set.size(), 2u);
87 EXPECT_TRUE(name_set.find(kApp1) != name_set.end());
88 EXPECT_TRUE(name_set.find(kApp2) != name_set.end());
89
90 name_set = m.getAppNamesFromUid(12345, true /* returnNormalized */);
91 EXPECT_TRUE(name_set.empty());
David Chende701692017-10-05 13:16:02 -070092}
93
94TEST(UidMapTest, TestAddAndRemove) {
95 UidMap m;
96 vector<int32_t> uids;
Dianne Hackborn3accca02013-09-20 09:32:11 -070097 vector<int64_t> versions;
David Chende701692017-10-05 13:16:02 -070098 vector<String16> apps;
99
100 uids.push_back(1000);
101 uids.push_back(1000);
102 apps.push_back(String16(kApp1.c_str()));
103 apps.push_back(String16(kApp2.c_str()));
104 versions.push_back(4);
105 versions.push_back(5);
David Chenbd125272018-04-04 19:02:50 -0700106 m.updateMap(1, uids, versions, apps);
David Chende701692017-10-05 13:16:02 -0700107
Yangster9df9a7f2017-12-18 13:33:05 -0800108 std::set<string> name_set = m.getAppNamesFromUid(1000, true /* returnNormalized */);
109 EXPECT_EQ(name_set.size(), 2u);
110 EXPECT_TRUE(name_set.find(kApp1) != name_set.end());
111 EXPECT_TRUE(name_set.find(kApp2) != name_set.end());
112
113 // Update the app1 version.
David Chenbd125272018-04-04 19:02:50 -0700114 m.updateApp(2, String16(kApp1.c_str()), 1000, 40);
David Chende701692017-10-05 13:16:02 -0700115 EXPECT_EQ(40, m.getAppVersion(1000, kApp1));
116
Yangster9df9a7f2017-12-18 13:33:05 -0800117 name_set = m.getAppNamesFromUid(1000, true /* returnNormalized */);
118 EXPECT_EQ(name_set.size(), 2u);
119 EXPECT_TRUE(name_set.find(kApp1) != name_set.end());
120 EXPECT_TRUE(name_set.find(kApp2) != name_set.end());
121
David Chenbd125272018-04-04 19:02:50 -0700122 m.removeApp(3, String16(kApp1.c_str()), 1000);
David Chende701692017-10-05 13:16:02 -0700123 EXPECT_FALSE(m.hasApp(1000, kApp1));
124 EXPECT_TRUE(m.hasApp(1000, kApp2));
Yangster9df9a7f2017-12-18 13:33:05 -0800125 name_set = m.getAppNamesFromUid(1000, true /* returnNormalized */);
126 EXPECT_EQ(name_set.size(), 1u);
127 EXPECT_TRUE(name_set.find(kApp1) == name_set.end());
128 EXPECT_TRUE(name_set.find(kApp2) != name_set.end());
129
130 // Remove app2.
David Chenbd125272018-04-04 19:02:50 -0700131 m.removeApp(4, String16(kApp2.c_str()), 1000);
Yangster9df9a7f2017-12-18 13:33:05 -0800132 EXPECT_FALSE(m.hasApp(1000, kApp1));
133 EXPECT_FALSE(m.hasApp(1000, kApp2));
134 name_set = m.getAppNamesFromUid(1000, true /* returnNormalized */);
135 EXPECT_TRUE(name_set.empty());
136}
137
138TEST(UidMapTest, TestUpdateApp) {
139 UidMap m;
David Chenbd125272018-04-04 19:02:50 -0700140 m.updateMap(1, {1000, 1000}, {4, 5}, {String16(kApp1.c_str()), String16(kApp2.c_str())});
Yangster9df9a7f2017-12-18 13:33:05 -0800141 std::set<string> name_set = m.getAppNamesFromUid(1000, true /* returnNormalized */);
142 EXPECT_EQ(name_set.size(), 2u);
143 EXPECT_TRUE(name_set.find(kApp1) != name_set.end());
144 EXPECT_TRUE(name_set.find(kApp2) != name_set.end());
145
146 // Adds a new name for uid 1000.
David Chenbd125272018-04-04 19:02:50 -0700147 m.updateApp(2, String16("NeW_aPP1_NAmE"), 1000, 40);
Yangster9df9a7f2017-12-18 13:33:05 -0800148 name_set = m.getAppNamesFromUid(1000, true /* returnNormalized */);
149 EXPECT_EQ(name_set.size(), 3u);
150 EXPECT_TRUE(name_set.find(kApp1) != name_set.end());
151 EXPECT_TRUE(name_set.find(kApp2) != name_set.end());
152 EXPECT_TRUE(name_set.find("NeW_aPP1_NAmE") == name_set.end());
153 EXPECT_TRUE(name_set.find("new_app1_name") != name_set.end());
154
155 // This name is also reused by another uid 2000.
David Chenbd125272018-04-04 19:02:50 -0700156 m.updateApp(3, String16("NeW_aPP1_NAmE"), 2000, 1);
Yangster9df9a7f2017-12-18 13:33:05 -0800157 name_set = m.getAppNamesFromUid(2000, true /* returnNormalized */);
158 EXPECT_EQ(name_set.size(), 1u);
159 EXPECT_TRUE(name_set.find("NeW_aPP1_NAmE") == name_set.end());
160 EXPECT_TRUE(name_set.find("new_app1_name") != name_set.end());
David Chende701692017-10-05 13:16:02 -0700161}
David Chend6896892017-10-25 11:49:03 -0700162
yro4beccbe2018-03-15 19:42:05 -0700163static void protoOutputStreamToUidMapping(ProtoOutputStream* proto, UidMapping* results) {
164 vector<uint8_t> bytes;
165 bytes.resize(proto->size());
166 size_t pos = 0;
167 auto iter = proto->data();
168 while (iter.readBuffer() != NULL) {
169 size_t toRead = iter.currentToRead();
170 std::memcpy(&((bytes)[pos]), iter.readBuffer(), toRead);
171 pos += toRead;
172 iter.rp()->move(toRead);
173 }
174 results->ParseFromArray(bytes.data(), bytes.size());
175}
176
David Chen35045cb2018-03-23 22:21:47 -0700177// Test that uid map returns at least one snapshot even if we already obtained
178// this snapshot from a previous call to getData.
179TEST(UidMapTest, TestOutputIncludesAtLeastOneSnapshot) {
180 UidMap m;
181 // Initialize single config key.
182 ConfigKey config1(1, StringToId("config1"));
183 m.OnConfigUpdated(config1);
184 vector<int32_t> uids;
185 vector<int64_t> versions;
186 vector<String16> apps;
187 uids.push_back(1000);
188 apps.push_back(String16(kApp2.c_str()));
189 versions.push_back(5);
190 m.updateMap(1, uids, versions, apps);
191
192 // Set the last timestamp for this config key to be newer.
193 m.mLastUpdatePerConfigKey[config1] = 2;
194
195 ProtoOutputStream proto;
Yangster-mac9def8e32018-04-17 13:55:51 -0700196 m.appendUidMap(3, config1, nullptr, &proto);
David Chen35045cb2018-03-23 22:21:47 -0700197
198 // Check there's still a uidmap attached this one.
199 UidMapping results;
200 protoOutputStreamToUidMapping(&proto, &results);
201 EXPECT_EQ(1, results.snapshots_size());
202}
203
David Chenbd125272018-04-04 19:02:50 -0700204TEST(UidMapTest, TestRemovedAppRetained) {
205 UidMap m;
206 // Initialize single config key.
207 ConfigKey config1(1, StringToId("config1"));
208 m.OnConfigUpdated(config1);
209 vector<int32_t> uids;
210 vector<int64_t> versions;
211 vector<String16> apps;
212 uids.push_back(1000);
213 apps.push_back(String16(kApp2.c_str()));
214 versions.push_back(5);
215 m.updateMap(1, uids, versions, apps);
216 m.removeApp(2, String16(kApp2.c_str()), 1000);
217
218 ProtoOutputStream proto;
Yangster-mac9def8e32018-04-17 13:55:51 -0700219 m.appendUidMap(3, config1, nullptr, &proto);
David Chenbd125272018-04-04 19:02:50 -0700220
221 // Snapshot should still contain this item as deleted.
222 UidMapping results;
223 protoOutputStreamToUidMapping(&proto, &results);
224 EXPECT_EQ(1, results.snapshots(0).package_info_size());
225 EXPECT_EQ(true, results.snapshots(0).package_info(0).deleted());
226}
227
228TEST(UidMapTest, TestRemovedAppOverGuardrail) {
229 UidMap m;
230 // Initialize single config key.
231 ConfigKey config1(1, StringToId("config1"));
232 m.OnConfigUpdated(config1);
233 vector<int32_t> uids;
234 vector<int64_t> versions;
235 vector<String16> apps;
236 const int maxDeletedApps = StatsdStats::kMaxDeletedAppsInUidMap;
237 for (int j = 0; j < maxDeletedApps + 10; j++) {
238 uids.push_back(j);
239 apps.push_back(String16(kApp1.c_str()));
240 versions.push_back(j);
241 }
242 m.updateMap(1, uids, versions, apps);
243
244 // First, verify that we have the expected number of items.
245 UidMapping results;
246 ProtoOutputStream proto;
Yangster-mac9def8e32018-04-17 13:55:51 -0700247 m.appendUidMap(3, config1, nullptr, &proto);
David Chenbd125272018-04-04 19:02:50 -0700248 protoOutputStreamToUidMapping(&proto, &results);
249 EXPECT_EQ(maxDeletedApps + 10, results.snapshots(0).package_info_size());
250
251 // Now remove all the apps.
252 m.updateMap(1, uids, versions, apps);
253 for (int j = 0; j < maxDeletedApps + 10; j++) {
254 m.removeApp(4, String16(kApp1.c_str()), j);
255 }
256
257 proto.clear();
Yangster-mac9def8e32018-04-17 13:55:51 -0700258 m.appendUidMap(5, config1, nullptr, &proto);
David Chenbd125272018-04-04 19:02:50 -0700259 // Snapshot drops the first nine items.
260 protoOutputStreamToUidMapping(&proto, &results);
261 EXPECT_EQ(maxDeletedApps, results.snapshots(0).package_info_size());
262}
263
David Chend6896892017-10-25 11:49:03 -0700264TEST(UidMapTest, TestClearingOutput) {
265 UidMap m;
266
Yangster-mac94e197c2018-01-02 16:03:03 -0800267 ConfigKey config1(1, StringToId("config1"));
268 ConfigKey config2(1, StringToId("config2"));
David Chend6896892017-10-25 11:49:03 -0700269
270 m.OnConfigUpdated(config1);
271
272 vector<int32_t> uids;
Dianne Hackborn3accca02013-09-20 09:32:11 -0700273 vector<int64_t> versions;
David Chend6896892017-10-25 11:49:03 -0700274 vector<String16> apps;
275 uids.push_back(1000);
276 uids.push_back(1000);
277 apps.push_back(String16(kApp1.c_str()));
278 apps.push_back(String16(kApp2.c_str()));
279 versions.push_back(4);
280 versions.push_back(5);
281 m.updateMap(1, uids, versions, apps);
282
yro4beccbe2018-03-15 19:42:05 -0700283 ProtoOutputStream proto;
Yangster-mac9def8e32018-04-17 13:55:51 -0700284 m.appendUidMap(2, config1, nullptr, &proto);
David Chenf384b902018-03-14 18:36:45 -0700285 UidMapping results;
yro4beccbe2018-03-15 19:42:05 -0700286 protoOutputStreamToUidMapping(&proto, &results);
David Chend6896892017-10-25 11:49:03 -0700287 EXPECT_EQ(1, results.snapshots_size());
288
David Chen35045cb2018-03-23 22:21:47 -0700289 // We have to keep at least one snapshot in memory at all times.
yro4beccbe2018-03-15 19:42:05 -0700290 proto.clear();
Yangster-mac9def8e32018-04-17 13:55:51 -0700291 m.appendUidMap(2, config1, nullptr, &proto);
yro4beccbe2018-03-15 19:42:05 -0700292 protoOutputStreamToUidMapping(&proto, &results);
David Chencfc311d2018-01-23 17:55:54 -0800293 EXPECT_EQ(1, results.snapshots_size());
David Chend6896892017-10-25 11:49:03 -0700294
295 // Now add another configuration.
296 m.OnConfigUpdated(config2);
297 m.updateApp(5, String16(kApp1.c_str()), 1000, 40);
David Chenf384b902018-03-14 18:36:45 -0700298 EXPECT_EQ(1U, m.mChanges.size());
yro4beccbe2018-03-15 19:42:05 -0700299 proto.clear();
Yangster-mac9def8e32018-04-17 13:55:51 -0700300 m.appendUidMap(6, config1, nullptr, &proto);
yro4beccbe2018-03-15 19:42:05 -0700301 protoOutputStreamToUidMapping(&proto, &results);
David Chencfc311d2018-01-23 17:55:54 -0800302 EXPECT_EQ(1, results.snapshots_size());
David Chend6896892017-10-25 11:49:03 -0700303 EXPECT_EQ(1, results.changes_size());
David Chenf384b902018-03-14 18:36:45 -0700304 EXPECT_EQ(1U, m.mChanges.size());
David Chend6896892017-10-25 11:49:03 -0700305
David Chenc136f45a2017-11-27 11:52:26 -0800306 // Add another delta update.
David Chend6896892017-10-25 11:49:03 -0700307 m.updateApp(7, String16(kApp2.c_str()), 1001, 41);
David Chenf384b902018-03-14 18:36:45 -0700308 EXPECT_EQ(2U, m.mChanges.size());
David Chenc136f45a2017-11-27 11:52:26 -0800309
310 // We still can't remove anything.
yro4beccbe2018-03-15 19:42:05 -0700311 proto.clear();
Yangster-mac9def8e32018-04-17 13:55:51 -0700312 m.appendUidMap(8, config1, nullptr, &proto);
yro4beccbe2018-03-15 19:42:05 -0700313 protoOutputStreamToUidMapping(&proto, &results);
David Chencfc311d2018-01-23 17:55:54 -0800314 EXPECT_EQ(1, results.snapshots_size());
David Chenf384b902018-03-14 18:36:45 -0700315 EXPECT_EQ(1, results.changes_size());
316 EXPECT_EQ(2U, m.mChanges.size());
David Chend6896892017-10-25 11:49:03 -0700317
yro4beccbe2018-03-15 19:42:05 -0700318 proto.clear();
Yangster-mac9def8e32018-04-17 13:55:51 -0700319 m.appendUidMap(9, config2, nullptr, &proto);
yro4beccbe2018-03-15 19:42:05 -0700320 protoOutputStreamToUidMapping(&proto, &results);
David Chencfc311d2018-01-23 17:55:54 -0800321 EXPECT_EQ(1, results.snapshots_size());
David Chend6896892017-10-25 11:49:03 -0700322 EXPECT_EQ(2, results.changes_size());
323 // At this point both should be cleared.
David Chenf384b902018-03-14 18:36:45 -0700324 EXPECT_EQ(0U, m.mChanges.size());
David Chend6896892017-10-25 11:49:03 -0700325}
David Chenc136f45a2017-11-27 11:52:26 -0800326
327TEST(UidMapTest, TestMemoryComputed) {
328 UidMap m;
329
Yangster-mac94e197c2018-01-02 16:03:03 -0800330 ConfigKey config1(1, StringToId("config1"));
David Chenc136f45a2017-11-27 11:52:26 -0800331 m.OnConfigUpdated(config1);
332
333 size_t startBytes = m.mBytesUsed;
334 vector<int32_t> uids;
Dianne Hackborn3accca02013-09-20 09:32:11 -0700335 vector<int64_t> versions;
David Chenc136f45a2017-11-27 11:52:26 -0800336 vector<String16> apps;
337 uids.push_back(1000);
338 apps.push_back(String16(kApp1.c_str()));
339 versions.push_back(1);
340 m.updateMap(1, uids, versions, apps);
David Chenc136f45a2017-11-27 11:52:26 -0800341
342 m.updateApp(3, String16(kApp1.c_str()), 1000, 40);
David Chenc136f45a2017-11-27 11:52:26 -0800343
yro4beccbe2018-03-15 19:42:05 -0700344 ProtoOutputStream proto;
David Chenf384b902018-03-14 18:36:45 -0700345 vector<uint8_t> bytes;
Yangster-mac9def8e32018-04-17 13:55:51 -0700346 m.appendUidMap(2, config1, nullptr, &proto);
David Chenc136f45a2017-11-27 11:52:26 -0800347 size_t prevBytes = m.mBytesUsed;
348
Yangster-mac9def8e32018-04-17 13:55:51 -0700349 m.appendUidMap(4, config1, nullptr, &proto);
David Chenc136f45a2017-11-27 11:52:26 -0800350 EXPECT_TRUE(m.mBytesUsed < prevBytes);
351}
352
353TEST(UidMapTest, TestMemoryGuardrail) {
354 UidMap m;
355 string buf;
356
Yangster-mac94e197c2018-01-02 16:03:03 -0800357 ConfigKey config1(1, StringToId("config1"));
David Chenc136f45a2017-11-27 11:52:26 -0800358 m.OnConfigUpdated(config1);
359
360 size_t startBytes = m.mBytesUsed;
361 vector<int32_t> uids;
Dianne Hackborn3accca02013-09-20 09:32:11 -0700362 vector<int64_t> versions;
David Chenc136f45a2017-11-27 11:52:26 -0800363 vector<String16> apps;
364 for (int i = 0; i < 100; i++) {
365 uids.push_back(1);
366 buf = "EXTREMELY_LONG_STRING_FOR_APP_TO_WASTE_MEMORY." + to_string(i);
367 apps.push_back(String16(buf.c_str()));
368 versions.push_back(1);
369 }
370 m.updateMap(1, uids, versions, apps);
David Chenc136f45a2017-11-27 11:52:26 -0800371
372 m.updateApp(3, String16("EXTREMELY_LONG_STRING_FOR_APP_TO_WASTE_MEMORY.0"), 1000, 2);
David Chenf384b902018-03-14 18:36:45 -0700373 EXPECT_EQ(1U, m.mChanges.size());
David Chenc136f45a2017-11-27 11:52:26 -0800374
375 // Now force deletion by limiting the memory to hold one delta change.
376 m.maxBytesOverride = 80; // Since the app string alone requires >45 characters.
377 m.updateApp(5, String16("EXTREMELY_LONG_STRING_FOR_APP_TO_WASTE_MEMORY.0"), 1000, 4);
David Chenf384b902018-03-14 18:36:45 -0700378 EXPECT_EQ(1U, m.mChanges.size());
David Chenc136f45a2017-11-27 11:52:26 -0800379}
Yangster-mac9def8e32018-04-17 13:55:51 -0700380
David Chende701692017-10-05 13:16:02 -0700381#else
382GTEST_LOG_(INFO) << "This test does nothing.\n";
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700383#endif
David Chend6896892017-10-25 11:49:03 -0700384
385} // namespace statsd
386} // namespace os
yro4beccbe2018-03-15 19:42:05 -0700387} // namespace android