blob: 99082cc647f6643494a62f43db1bf00bd5a11bb4 [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();
Chenjie Yue2219202018-06-08 10:07:51 -070043 sp<StatsPullerManager> pullerManager = new StatsPullerManager();
Yangster-mac932ecec2018-02-01 10:23:52 -080044 sp<AlarmMonitor> anomalyAlarmMonitor;
45 sp<AlarmMonitor> subscriberAlarmMonitor;
David Chenc136f45a2017-11-27 11:52:26 -080046 // Construct the processor with a dummy sendBroadcast function that does nothing.
Chenjie Yue2219202018-06-08 10:07:51 -070047 StatsLogProcessor p(m, pullerManager, anomalyAlarmMonitor, subscriberAlarmMonitor, 0,
48 [](const ConfigKey& key) { return true; });
David Chen21582962017-11-01 17:32:46 -070049 LogEvent addEvent(android::util::ISOLATED_UID_CHANGED, 1);
Yao Chen80235402017-11-13 20:42:25 -080050 addEvent.write(100); // parent UID
51 addEvent.write(101); // isolated UID
52 addEvent.write(1); // Indicates creation.
David Chen21582962017-11-01 17:32:46 -070053 addEvent.init();
54
Yangster-macd40053e2018-01-09 16:29:22 -080055 EXPECT_EQ(101, m->getHostUidOrSelf(101));
David Chen21582962017-11-01 17:32:46 -070056
Yangster-macd40053e2018-01-09 16:29:22 -080057 p.OnLogEvent(&addEvent);
58 EXPECT_EQ(100, m->getHostUidOrSelf(101));
David Chen21582962017-11-01 17:32:46 -070059
60 LogEvent removeEvent(android::util::ISOLATED_UID_CHANGED, 1);
Yao Chen80235402017-11-13 20:42:25 -080061 removeEvent.write(100); // parent UID
62 removeEvent.write(101); // isolated UID
63 removeEvent.write(0); // Indicates removal.
David Chen21582962017-11-01 17:32:46 -070064 removeEvent.init();
Yangster-macd40053e2018-01-09 16:29:22 -080065 p.OnLogEvent(&removeEvent);
66 EXPECT_EQ(101, m->getHostUidOrSelf(101));
David Chen21582962017-11-01 17:32:46 -070067}
68
David Chende701692017-10-05 13:16:02 -070069TEST(UidMapTest, TestMatching) {
70 UidMap m;
71 vector<int32_t> uids;
Dianne Hackborn3accca02013-09-20 09:32:11 -070072 vector<int64_t> versions;
David Chende701692017-10-05 13:16:02 -070073 vector<String16> apps;
74
75 uids.push_back(1000);
76 uids.push_back(1000);
77 apps.push_back(String16(kApp1.c_str()));
78 apps.push_back(String16(kApp2.c_str()));
79 versions.push_back(4);
80 versions.push_back(5);
David Chenbd125272018-04-04 19:02:50 -070081 m.updateMap(1, uids, versions, apps);
David Chende701692017-10-05 13:16:02 -070082 EXPECT_TRUE(m.hasApp(1000, kApp1));
83 EXPECT_TRUE(m.hasApp(1000, kApp2));
84 EXPECT_FALSE(m.hasApp(1000, "not.app"));
Yangster9df9a7f2017-12-18 13:33:05 -080085
86 std::set<string> name_set = m.getAppNamesFromUid(1000u, true /* returnNormalized */);
87 EXPECT_EQ(name_set.size(), 2u);
88 EXPECT_TRUE(name_set.find(kApp1) != name_set.end());
89 EXPECT_TRUE(name_set.find(kApp2) != name_set.end());
90
91 name_set = m.getAppNamesFromUid(12345, true /* returnNormalized */);
92 EXPECT_TRUE(name_set.empty());
David Chende701692017-10-05 13:16:02 -070093}
94
95TEST(UidMapTest, TestAddAndRemove) {
96 UidMap m;
97 vector<int32_t> uids;
Dianne Hackborn3accca02013-09-20 09:32:11 -070098 vector<int64_t> versions;
David Chende701692017-10-05 13:16:02 -070099 vector<String16> apps;
100
101 uids.push_back(1000);
102 uids.push_back(1000);
103 apps.push_back(String16(kApp1.c_str()));
104 apps.push_back(String16(kApp2.c_str()));
105 versions.push_back(4);
106 versions.push_back(5);
David Chenbd125272018-04-04 19:02:50 -0700107 m.updateMap(1, uids, versions, apps);
David Chende701692017-10-05 13:16:02 -0700108
Yangster9df9a7f2017-12-18 13:33:05 -0800109 std::set<string> name_set = m.getAppNamesFromUid(1000, true /* returnNormalized */);
110 EXPECT_EQ(name_set.size(), 2u);
111 EXPECT_TRUE(name_set.find(kApp1) != name_set.end());
112 EXPECT_TRUE(name_set.find(kApp2) != name_set.end());
113
114 // Update the app1 version.
David Chenbd125272018-04-04 19:02:50 -0700115 m.updateApp(2, String16(kApp1.c_str()), 1000, 40);
David Chende701692017-10-05 13:16:02 -0700116 EXPECT_EQ(40, m.getAppVersion(1000, kApp1));
117
Yangster9df9a7f2017-12-18 13:33:05 -0800118 name_set = m.getAppNamesFromUid(1000, true /* returnNormalized */);
119 EXPECT_EQ(name_set.size(), 2u);
120 EXPECT_TRUE(name_set.find(kApp1) != name_set.end());
121 EXPECT_TRUE(name_set.find(kApp2) != name_set.end());
122
David Chenbd125272018-04-04 19:02:50 -0700123 m.removeApp(3, String16(kApp1.c_str()), 1000);
David Chende701692017-10-05 13:16:02 -0700124 EXPECT_FALSE(m.hasApp(1000, kApp1));
125 EXPECT_TRUE(m.hasApp(1000, kApp2));
Yangster9df9a7f2017-12-18 13:33:05 -0800126 name_set = m.getAppNamesFromUid(1000, true /* returnNormalized */);
127 EXPECT_EQ(name_set.size(), 1u);
128 EXPECT_TRUE(name_set.find(kApp1) == name_set.end());
129 EXPECT_TRUE(name_set.find(kApp2) != name_set.end());
130
131 // Remove app2.
David Chenbd125272018-04-04 19:02:50 -0700132 m.removeApp(4, String16(kApp2.c_str()), 1000);
Yangster9df9a7f2017-12-18 13:33:05 -0800133 EXPECT_FALSE(m.hasApp(1000, kApp1));
134 EXPECT_FALSE(m.hasApp(1000, kApp2));
135 name_set = m.getAppNamesFromUid(1000, true /* returnNormalized */);
136 EXPECT_TRUE(name_set.empty());
137}
138
139TEST(UidMapTest, TestUpdateApp) {
140 UidMap m;
David Chenbd125272018-04-04 19:02:50 -0700141 m.updateMap(1, {1000, 1000}, {4, 5}, {String16(kApp1.c_str()), String16(kApp2.c_str())});
Yangster9df9a7f2017-12-18 13:33:05 -0800142 std::set<string> name_set = m.getAppNamesFromUid(1000, true /* returnNormalized */);
143 EXPECT_EQ(name_set.size(), 2u);
144 EXPECT_TRUE(name_set.find(kApp1) != name_set.end());
145 EXPECT_TRUE(name_set.find(kApp2) != name_set.end());
146
147 // Adds a new name for uid 1000.
David Chenbd125272018-04-04 19:02:50 -0700148 m.updateApp(2, String16("NeW_aPP1_NAmE"), 1000, 40);
Yangster9df9a7f2017-12-18 13:33:05 -0800149 name_set = m.getAppNamesFromUid(1000, true /* returnNormalized */);
150 EXPECT_EQ(name_set.size(), 3u);
151 EXPECT_TRUE(name_set.find(kApp1) != name_set.end());
152 EXPECT_TRUE(name_set.find(kApp2) != name_set.end());
153 EXPECT_TRUE(name_set.find("NeW_aPP1_NAmE") == name_set.end());
154 EXPECT_TRUE(name_set.find("new_app1_name") != name_set.end());
155
156 // This name is also reused by another uid 2000.
David Chenbd125272018-04-04 19:02:50 -0700157 m.updateApp(3, String16("NeW_aPP1_NAmE"), 2000, 1);
Yangster9df9a7f2017-12-18 13:33:05 -0800158 name_set = m.getAppNamesFromUid(2000, true /* returnNormalized */);
159 EXPECT_EQ(name_set.size(), 1u);
160 EXPECT_TRUE(name_set.find("NeW_aPP1_NAmE") == name_set.end());
161 EXPECT_TRUE(name_set.find("new_app1_name") != name_set.end());
David Chende701692017-10-05 13:16:02 -0700162}
David Chend6896892017-10-25 11:49:03 -0700163
yro4beccbe2018-03-15 19:42:05 -0700164static void protoOutputStreamToUidMapping(ProtoOutputStream* proto, UidMapping* results) {
165 vector<uint8_t> bytes;
166 bytes.resize(proto->size());
167 size_t pos = 0;
168 auto iter = proto->data();
169 while (iter.readBuffer() != NULL) {
170 size_t toRead = iter.currentToRead();
171 std::memcpy(&((bytes)[pos]), iter.readBuffer(), toRead);
172 pos += toRead;
173 iter.rp()->move(toRead);
174 }
175 results->ParseFromArray(bytes.data(), bytes.size());
176}
177
David Chen35045cb2018-03-23 22:21:47 -0700178// Test that uid map returns at least one snapshot even if we already obtained
179// this snapshot from a previous call to getData.
180TEST(UidMapTest, TestOutputIncludesAtLeastOneSnapshot) {
181 UidMap m;
182 // Initialize single config key.
183 ConfigKey config1(1, StringToId("config1"));
184 m.OnConfigUpdated(config1);
185 vector<int32_t> uids;
186 vector<int64_t> versions;
187 vector<String16> apps;
188 uids.push_back(1000);
189 apps.push_back(String16(kApp2.c_str()));
190 versions.push_back(5);
191 m.updateMap(1, uids, versions, apps);
192
193 // Set the last timestamp for this config key to be newer.
194 m.mLastUpdatePerConfigKey[config1] = 2;
195
196 ProtoOutputStream proto;
Yangster-mac9def8e32018-04-17 13:55:51 -0700197 m.appendUidMap(3, config1, nullptr, &proto);
David Chen35045cb2018-03-23 22:21:47 -0700198
199 // Check there's still a uidmap attached this one.
200 UidMapping results;
201 protoOutputStreamToUidMapping(&proto, &results);
202 EXPECT_EQ(1, results.snapshots_size());
203}
204
David Chenbd125272018-04-04 19:02:50 -0700205TEST(UidMapTest, TestRemovedAppRetained) {
206 UidMap m;
207 // Initialize single config key.
208 ConfigKey config1(1, StringToId("config1"));
209 m.OnConfigUpdated(config1);
210 vector<int32_t> uids;
211 vector<int64_t> versions;
212 vector<String16> apps;
213 uids.push_back(1000);
214 apps.push_back(String16(kApp2.c_str()));
215 versions.push_back(5);
216 m.updateMap(1, uids, versions, apps);
217 m.removeApp(2, String16(kApp2.c_str()), 1000);
218
219 ProtoOutputStream proto;
Yangster-mac9def8e32018-04-17 13:55:51 -0700220 m.appendUidMap(3, config1, nullptr, &proto);
David Chenbd125272018-04-04 19:02:50 -0700221
222 // Snapshot should still contain this item as deleted.
223 UidMapping results;
224 protoOutputStreamToUidMapping(&proto, &results);
225 EXPECT_EQ(1, results.snapshots(0).package_info_size());
226 EXPECT_EQ(true, results.snapshots(0).package_info(0).deleted());
227}
228
229TEST(UidMapTest, TestRemovedAppOverGuardrail) {
230 UidMap m;
231 // Initialize single config key.
232 ConfigKey config1(1, StringToId("config1"));
233 m.OnConfigUpdated(config1);
234 vector<int32_t> uids;
235 vector<int64_t> versions;
236 vector<String16> apps;
237 const int maxDeletedApps = StatsdStats::kMaxDeletedAppsInUidMap;
238 for (int j = 0; j < maxDeletedApps + 10; j++) {
239 uids.push_back(j);
240 apps.push_back(String16(kApp1.c_str()));
241 versions.push_back(j);
242 }
243 m.updateMap(1, uids, versions, apps);
244
245 // First, verify that we have the expected number of items.
246 UidMapping results;
247 ProtoOutputStream proto;
Yangster-mac9def8e32018-04-17 13:55:51 -0700248 m.appendUidMap(3, config1, nullptr, &proto);
David Chenbd125272018-04-04 19:02:50 -0700249 protoOutputStreamToUidMapping(&proto, &results);
250 EXPECT_EQ(maxDeletedApps + 10, results.snapshots(0).package_info_size());
251
252 // Now remove all the apps.
253 m.updateMap(1, uids, versions, apps);
254 for (int j = 0; j < maxDeletedApps + 10; j++) {
255 m.removeApp(4, String16(kApp1.c_str()), j);
256 }
257
258 proto.clear();
Yangster-mac9def8e32018-04-17 13:55:51 -0700259 m.appendUidMap(5, config1, nullptr, &proto);
David Chenbd125272018-04-04 19:02:50 -0700260 // Snapshot drops the first nine items.
261 protoOutputStreamToUidMapping(&proto, &results);
262 EXPECT_EQ(maxDeletedApps, results.snapshots(0).package_info_size());
263}
264
David Chend6896892017-10-25 11:49:03 -0700265TEST(UidMapTest, TestClearingOutput) {
266 UidMap m;
267
Yangster-mac94e197c2018-01-02 16:03:03 -0800268 ConfigKey config1(1, StringToId("config1"));
269 ConfigKey config2(1, StringToId("config2"));
David Chend6896892017-10-25 11:49:03 -0700270
271 m.OnConfigUpdated(config1);
272
273 vector<int32_t> uids;
Dianne Hackborn3accca02013-09-20 09:32:11 -0700274 vector<int64_t> versions;
David Chend6896892017-10-25 11:49:03 -0700275 vector<String16> apps;
276 uids.push_back(1000);
277 uids.push_back(1000);
278 apps.push_back(String16(kApp1.c_str()));
279 apps.push_back(String16(kApp2.c_str()));
280 versions.push_back(4);
281 versions.push_back(5);
282 m.updateMap(1, uids, versions, apps);
283
yro4beccbe2018-03-15 19:42:05 -0700284 ProtoOutputStream proto;
Yangster-mac9def8e32018-04-17 13:55:51 -0700285 m.appendUidMap(2, config1, nullptr, &proto);
David Chenf384b902018-03-14 18:36:45 -0700286 UidMapping results;
yro4beccbe2018-03-15 19:42:05 -0700287 protoOutputStreamToUidMapping(&proto, &results);
David Chend6896892017-10-25 11:49:03 -0700288 EXPECT_EQ(1, results.snapshots_size());
289
David Chen35045cb2018-03-23 22:21:47 -0700290 // We have to keep at least one snapshot in memory at all times.
yro4beccbe2018-03-15 19:42:05 -0700291 proto.clear();
Yangster-mac9def8e32018-04-17 13:55:51 -0700292 m.appendUidMap(2, config1, nullptr, &proto);
yro4beccbe2018-03-15 19:42:05 -0700293 protoOutputStreamToUidMapping(&proto, &results);
David Chencfc311d2018-01-23 17:55:54 -0800294 EXPECT_EQ(1, results.snapshots_size());
David Chend6896892017-10-25 11:49:03 -0700295
296 // Now add another configuration.
297 m.OnConfigUpdated(config2);
298 m.updateApp(5, String16(kApp1.c_str()), 1000, 40);
David Chenf384b902018-03-14 18:36:45 -0700299 EXPECT_EQ(1U, m.mChanges.size());
yro4beccbe2018-03-15 19:42:05 -0700300 proto.clear();
Yangster-mac9def8e32018-04-17 13:55:51 -0700301 m.appendUidMap(6, config1, nullptr, &proto);
yro4beccbe2018-03-15 19:42:05 -0700302 protoOutputStreamToUidMapping(&proto, &results);
David Chencfc311d2018-01-23 17:55:54 -0800303 EXPECT_EQ(1, results.snapshots_size());
David Chend6896892017-10-25 11:49:03 -0700304 EXPECT_EQ(1, results.changes_size());
David Chenf384b902018-03-14 18:36:45 -0700305 EXPECT_EQ(1U, m.mChanges.size());
David Chend6896892017-10-25 11:49:03 -0700306
David Chenc136f45a2017-11-27 11:52:26 -0800307 // Add another delta update.
David Chend6896892017-10-25 11:49:03 -0700308 m.updateApp(7, String16(kApp2.c_str()), 1001, 41);
David Chenf384b902018-03-14 18:36:45 -0700309 EXPECT_EQ(2U, m.mChanges.size());
David Chenc136f45a2017-11-27 11:52:26 -0800310
311 // We still can't remove anything.
yro4beccbe2018-03-15 19:42:05 -0700312 proto.clear();
Yangster-mac9def8e32018-04-17 13:55:51 -0700313 m.appendUidMap(8, config1, nullptr, &proto);
yro4beccbe2018-03-15 19:42:05 -0700314 protoOutputStreamToUidMapping(&proto, &results);
David Chencfc311d2018-01-23 17:55:54 -0800315 EXPECT_EQ(1, results.snapshots_size());
David Chenf384b902018-03-14 18:36:45 -0700316 EXPECT_EQ(1, results.changes_size());
317 EXPECT_EQ(2U, m.mChanges.size());
David Chend6896892017-10-25 11:49:03 -0700318
yro4beccbe2018-03-15 19:42:05 -0700319 proto.clear();
Yangster-mac9def8e32018-04-17 13:55:51 -0700320 m.appendUidMap(9, config2, nullptr, &proto);
yro4beccbe2018-03-15 19:42:05 -0700321 protoOutputStreamToUidMapping(&proto, &results);
David Chencfc311d2018-01-23 17:55:54 -0800322 EXPECT_EQ(1, results.snapshots_size());
David Chend6896892017-10-25 11:49:03 -0700323 EXPECT_EQ(2, results.changes_size());
324 // At this point both should be cleared.
David Chenf384b902018-03-14 18:36:45 -0700325 EXPECT_EQ(0U, m.mChanges.size());
David Chend6896892017-10-25 11:49:03 -0700326}
David Chenc136f45a2017-11-27 11:52:26 -0800327
328TEST(UidMapTest, TestMemoryComputed) {
329 UidMap m;
330
Yangster-mac94e197c2018-01-02 16:03:03 -0800331 ConfigKey config1(1, StringToId("config1"));
David Chenc136f45a2017-11-27 11:52:26 -0800332 m.OnConfigUpdated(config1);
333
334 size_t startBytes = m.mBytesUsed;
335 vector<int32_t> uids;
Dianne Hackborn3accca02013-09-20 09:32:11 -0700336 vector<int64_t> versions;
David Chenc136f45a2017-11-27 11:52:26 -0800337 vector<String16> apps;
338 uids.push_back(1000);
339 apps.push_back(String16(kApp1.c_str()));
340 versions.push_back(1);
341 m.updateMap(1, uids, versions, apps);
David Chenc136f45a2017-11-27 11:52:26 -0800342
343 m.updateApp(3, String16(kApp1.c_str()), 1000, 40);
David Chenc136f45a2017-11-27 11:52:26 -0800344
yro4beccbe2018-03-15 19:42:05 -0700345 ProtoOutputStream proto;
David Chenf384b902018-03-14 18:36:45 -0700346 vector<uint8_t> bytes;
Yangster-mac9def8e32018-04-17 13:55:51 -0700347 m.appendUidMap(2, config1, nullptr, &proto);
David Chenc136f45a2017-11-27 11:52:26 -0800348 size_t prevBytes = m.mBytesUsed;
349
Yangster-mac9def8e32018-04-17 13:55:51 -0700350 m.appendUidMap(4, config1, nullptr, &proto);
David Chenc136f45a2017-11-27 11:52:26 -0800351 EXPECT_TRUE(m.mBytesUsed < prevBytes);
352}
353
354TEST(UidMapTest, TestMemoryGuardrail) {
355 UidMap m;
356 string buf;
357
Yangster-mac94e197c2018-01-02 16:03:03 -0800358 ConfigKey config1(1, StringToId("config1"));
David Chenc136f45a2017-11-27 11:52:26 -0800359 m.OnConfigUpdated(config1);
360
361 size_t startBytes = m.mBytesUsed;
362 vector<int32_t> uids;
Dianne Hackborn3accca02013-09-20 09:32:11 -0700363 vector<int64_t> versions;
David Chenc136f45a2017-11-27 11:52:26 -0800364 vector<String16> apps;
365 for (int i = 0; i < 100; i++) {
366 uids.push_back(1);
367 buf = "EXTREMELY_LONG_STRING_FOR_APP_TO_WASTE_MEMORY." + to_string(i);
368 apps.push_back(String16(buf.c_str()));
369 versions.push_back(1);
370 }
371 m.updateMap(1, uids, versions, apps);
David Chenc136f45a2017-11-27 11:52:26 -0800372
373 m.updateApp(3, String16("EXTREMELY_LONG_STRING_FOR_APP_TO_WASTE_MEMORY.0"), 1000, 2);
David Chenf384b902018-03-14 18:36:45 -0700374 EXPECT_EQ(1U, m.mChanges.size());
David Chenc136f45a2017-11-27 11:52:26 -0800375
376 // Now force deletion by limiting the memory to hold one delta change.
377 m.maxBytesOverride = 80; // Since the app string alone requires >45 characters.
378 m.updateApp(5, String16("EXTREMELY_LONG_STRING_FOR_APP_TO_WASTE_MEMORY.0"), 1000, 4);
David Chenf384b902018-03-14 18:36:45 -0700379 EXPECT_EQ(1U, m.mChanges.size());
David Chenc136f45a2017-11-27 11:52:26 -0800380}
Yangster-mac9def8e32018-04-17 13:55:51 -0700381
David Chende701692017-10-05 13:16:02 -0700382#else
383GTEST_LOG_(INFO) << "This test does nothing.\n";
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700384#endif
David Chend6896892017-10-25 11:49:03 -0700385
386} // namespace statsd
387} // namespace os
yro4beccbe2018-03-15 19:42:05 -0700388} // namespace android