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