blob: a49c18fb98579b6067b8327d17686138f87b05fb [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"
Jeffrey Huang3eb84d42020-03-17 10:31:22 -070021#include "statslog_statsdtest.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;
Joe Onorato99598ee2019-02-11 15:55:13 +000036using android::util::ProtoReader;
yro4beccbe2018-03-15 19:42:05 -070037
David Chende701692017-10-05 13:16:02 -070038#ifdef __ANDROID__
39const string kApp1 = "app1.sharing.1";
40const string kApp2 = "app2.sharing.1";
41
Jeffrey Huang1e4368a2020-02-18 12:28:52 -080042// TODO(b/149590301): Update this test to use new socket schema.
43//TEST(UidMapTest, TestIsolatedUID) {
44// sp<UidMap> m = new UidMap();
45// sp<StatsPullerManager> pullerManager = new StatsPullerManager();
46// sp<AlarmMonitor> anomalyAlarmMonitor;
47// sp<AlarmMonitor> subscriberAlarmMonitor;
48// // Construct the processor with a dummy sendBroadcast function that does nothing.
49// StatsLogProcessor p(m, pullerManager, anomalyAlarmMonitor, subscriberAlarmMonitor, 0,
50// [](const ConfigKey& key) { return true; },
51// [](const int&, const vector<int64_t>&) {return true;});
Jeffrey Huang3eb84d42020-03-17 10:31:22 -070052// LogEvent addEvent(util::ISOLATED_UID_CHANGED, 1);
Jeffrey Huang1e4368a2020-02-18 12:28:52 -080053// addEvent.write(100); // parent UID
54// addEvent.write(101); // isolated UID
55// addEvent.write(1); // Indicates creation.
56// addEvent.init();
57//
58// EXPECT_EQ(101, m->getHostUidOrSelf(101));
59//
60// p.OnLogEvent(&addEvent);
61// EXPECT_EQ(100, m->getHostUidOrSelf(101));
62//
Jeffrey Huang3eb84d42020-03-17 10:31:22 -070063// LogEvent removeEvent(util::ISOLATED_UID_CHANGED, 1);
Jeffrey Huang1e4368a2020-02-18 12:28:52 -080064// removeEvent.write(100); // parent UID
65// removeEvent.write(101); // isolated UID
66// removeEvent.write(0); // Indicates removal.
67// removeEvent.init();
68// p.OnLogEvent(&removeEvent);
69// EXPECT_EQ(101, m->getHostUidOrSelf(101));
70//}
David Chen21582962017-11-01 17:32:46 -070071
David Chende701692017-10-05 13:16:02 -070072TEST(UidMapTest, TestMatching) {
73 UidMap m;
74 vector<int32_t> uids;
Dianne Hackborn3accca02013-09-20 09:32:11 -070075 vector<int64_t> versions;
David Chende701692017-10-05 13:16:02 -070076 vector<String16> apps;
dwchen730403e2018-10-29 11:41:56 -070077 vector<String16> versionStrings;
78 vector<String16> installers;
David Chende701692017-10-05 13:16:02 -070079
80 uids.push_back(1000);
81 uids.push_back(1000);
dwchen730403e2018-10-29 11:41:56 -070082 versionStrings.push_back(String16("v1"));
83 versionStrings.push_back(String16("v1"));
84 installers.push_back(String16(""));
85 installers.push_back(String16(""));
David Chende701692017-10-05 13:16:02 -070086 apps.push_back(String16(kApp1.c_str()));
87 apps.push_back(String16(kApp2.c_str()));
88 versions.push_back(4);
89 versions.push_back(5);
dwchen730403e2018-10-29 11:41:56 -070090 m.updateMap(1, uids, versions, versionStrings, apps, installers);
David Chende701692017-10-05 13:16:02 -070091 EXPECT_TRUE(m.hasApp(1000, kApp1));
92 EXPECT_TRUE(m.hasApp(1000, kApp2));
93 EXPECT_FALSE(m.hasApp(1000, "not.app"));
Yangster9df9a7f2017-12-18 13:33:05 -080094
95 std::set<string> name_set = m.getAppNamesFromUid(1000u, true /* returnNormalized */);
96 EXPECT_EQ(name_set.size(), 2u);
97 EXPECT_TRUE(name_set.find(kApp1) != name_set.end());
98 EXPECT_TRUE(name_set.find(kApp2) != name_set.end());
99
100 name_set = m.getAppNamesFromUid(12345, true /* returnNormalized */);
101 EXPECT_TRUE(name_set.empty());
David Chende701692017-10-05 13:16:02 -0700102}
103
104TEST(UidMapTest, TestAddAndRemove) {
105 UidMap m;
106 vector<int32_t> uids;
Dianne Hackborn3accca02013-09-20 09:32:11 -0700107 vector<int64_t> versions;
David Chende701692017-10-05 13:16:02 -0700108 vector<String16> apps;
dwchen730403e2018-10-29 11:41:56 -0700109 vector<String16> versionStrings;
110 vector<String16> installers;
David Chende701692017-10-05 13:16:02 -0700111
112 uids.push_back(1000);
113 uids.push_back(1000);
dwchen730403e2018-10-29 11:41:56 -0700114 versionStrings.push_back(String16("v1"));
115 versionStrings.push_back(String16("v1"));
116 installers.push_back(String16(""));
117 installers.push_back(String16(""));
David Chende701692017-10-05 13:16:02 -0700118 apps.push_back(String16(kApp1.c_str()));
119 apps.push_back(String16(kApp2.c_str()));
120 versions.push_back(4);
121 versions.push_back(5);
dwchen730403e2018-10-29 11:41:56 -0700122 m.updateMap(1, uids, versions, versionStrings, apps, installers);
David Chende701692017-10-05 13:16:02 -0700123
Yangster9df9a7f2017-12-18 13:33:05 -0800124 std::set<string> name_set = m.getAppNamesFromUid(1000, true /* returnNormalized */);
125 EXPECT_EQ(name_set.size(), 2u);
126 EXPECT_TRUE(name_set.find(kApp1) != name_set.end());
127 EXPECT_TRUE(name_set.find(kApp2) != name_set.end());
128
129 // Update the app1 version.
dwchen730403e2018-10-29 11:41:56 -0700130 m.updateApp(2, String16(kApp1.c_str()), 1000, 40, String16("v40"), String16(""));
David Chende701692017-10-05 13:16:02 -0700131 EXPECT_EQ(40, m.getAppVersion(1000, kApp1));
132
Yangster9df9a7f2017-12-18 13:33:05 -0800133 name_set = m.getAppNamesFromUid(1000, true /* returnNormalized */);
134 EXPECT_EQ(name_set.size(), 2u);
135 EXPECT_TRUE(name_set.find(kApp1) != name_set.end());
136 EXPECT_TRUE(name_set.find(kApp2) != name_set.end());
137
David Chenbd125272018-04-04 19:02:50 -0700138 m.removeApp(3, String16(kApp1.c_str()), 1000);
David Chende701692017-10-05 13:16:02 -0700139 EXPECT_FALSE(m.hasApp(1000, kApp1));
140 EXPECT_TRUE(m.hasApp(1000, kApp2));
Yangster9df9a7f2017-12-18 13:33:05 -0800141 name_set = m.getAppNamesFromUid(1000, true /* returnNormalized */);
142 EXPECT_EQ(name_set.size(), 1u);
143 EXPECT_TRUE(name_set.find(kApp1) == name_set.end());
144 EXPECT_TRUE(name_set.find(kApp2) != name_set.end());
145
146 // Remove app2.
David Chenbd125272018-04-04 19:02:50 -0700147 m.removeApp(4, String16(kApp2.c_str()), 1000);
Yangster9df9a7f2017-12-18 13:33:05 -0800148 EXPECT_FALSE(m.hasApp(1000, kApp1));
149 EXPECT_FALSE(m.hasApp(1000, kApp2));
150 name_set = m.getAppNamesFromUid(1000, true /* returnNormalized */);
151 EXPECT_TRUE(name_set.empty());
152}
153
154TEST(UidMapTest, TestUpdateApp) {
155 UidMap m;
dwchen730403e2018-10-29 11:41:56 -0700156 m.updateMap(1, {1000, 1000}, {4, 5}, {String16("v4"), String16("v5")},
157 {String16(kApp1.c_str()), String16(kApp2.c_str())}, {String16(""), String16("")});
Yangster9df9a7f2017-12-18 13:33:05 -0800158 std::set<string> name_set = m.getAppNamesFromUid(1000, true /* returnNormalized */);
159 EXPECT_EQ(name_set.size(), 2u);
160 EXPECT_TRUE(name_set.find(kApp1) != name_set.end());
161 EXPECT_TRUE(name_set.find(kApp2) != name_set.end());
162
163 // Adds a new name for uid 1000.
dwchen730403e2018-10-29 11:41:56 -0700164 m.updateApp(2, String16("NeW_aPP1_NAmE"), 1000, 40, String16("v40"), String16(""));
Yangster9df9a7f2017-12-18 13:33:05 -0800165 name_set = m.getAppNamesFromUid(1000, true /* returnNormalized */);
166 EXPECT_EQ(name_set.size(), 3u);
167 EXPECT_TRUE(name_set.find(kApp1) != name_set.end());
168 EXPECT_TRUE(name_set.find(kApp2) != name_set.end());
169 EXPECT_TRUE(name_set.find("NeW_aPP1_NAmE") == name_set.end());
170 EXPECT_TRUE(name_set.find("new_app1_name") != name_set.end());
171
172 // This name is also reused by another uid 2000.
dwchen730403e2018-10-29 11:41:56 -0700173 m.updateApp(3, String16("NeW_aPP1_NAmE"), 2000, 1, String16("v1"), String16(""));
Yangster9df9a7f2017-12-18 13:33:05 -0800174 name_set = m.getAppNamesFromUid(2000, true /* returnNormalized */);
175 EXPECT_EQ(name_set.size(), 1u);
176 EXPECT_TRUE(name_set.find("NeW_aPP1_NAmE") == name_set.end());
177 EXPECT_TRUE(name_set.find("new_app1_name") != name_set.end());
David Chende701692017-10-05 13:16:02 -0700178}
David Chend6896892017-10-25 11:49:03 -0700179
yro4beccbe2018-03-15 19:42:05 -0700180static void protoOutputStreamToUidMapping(ProtoOutputStream* proto, UidMapping* results) {
181 vector<uint8_t> bytes;
182 bytes.resize(proto->size());
183 size_t pos = 0;
Joe Onorato99598ee2019-02-11 15:55:13 +0000184 sp<ProtoReader> reader = proto->data();
185 while (reader->readBuffer() != NULL) {
186 size_t toRead = reader->currentToRead();
187 std::memcpy(&((bytes)[pos]), reader->readBuffer(), toRead);
yro4beccbe2018-03-15 19:42:05 -0700188 pos += toRead;
Joe Onorato99598ee2019-02-11 15:55:13 +0000189 reader->move(toRead);
yro4beccbe2018-03-15 19:42:05 -0700190 }
191 results->ParseFromArray(bytes.data(), bytes.size());
192}
193
David Chen35045cb2018-03-23 22:21:47 -0700194// Test that uid map returns at least one snapshot even if we already obtained
195// this snapshot from a previous call to getData.
196TEST(UidMapTest, TestOutputIncludesAtLeastOneSnapshot) {
197 UidMap m;
198 // Initialize single config key.
199 ConfigKey config1(1, StringToId("config1"));
200 m.OnConfigUpdated(config1);
201 vector<int32_t> uids;
202 vector<int64_t> versions;
203 vector<String16> apps;
dwchen730403e2018-10-29 11:41:56 -0700204 vector<String16> versionStrings;
205 vector<String16> installers;
David Chen35045cb2018-03-23 22:21:47 -0700206 uids.push_back(1000);
207 apps.push_back(String16(kApp2.c_str()));
dwchen730403e2018-10-29 11:41:56 -0700208 versionStrings.push_back(String16("v1"));
209 installers.push_back(String16(""));
David Chen35045cb2018-03-23 22:21:47 -0700210 versions.push_back(5);
dwchen730403e2018-10-29 11:41:56 -0700211 m.updateMap(1, uids, versions, versionStrings, apps, installers);
David Chen35045cb2018-03-23 22:21:47 -0700212
213 // Set the last timestamp for this config key to be newer.
214 m.mLastUpdatePerConfigKey[config1] = 2;
215
216 ProtoOutputStream proto;
dwchen730403e2018-10-29 11:41:56 -0700217 m.appendUidMap(3, config1, nullptr, true, true, &proto);
David Chen35045cb2018-03-23 22:21:47 -0700218
219 // Check there's still a uidmap attached this one.
220 UidMapping results;
221 protoOutputStreamToUidMapping(&proto, &results);
222 EXPECT_EQ(1, results.snapshots_size());
dwchen730403e2018-10-29 11:41:56 -0700223 EXPECT_EQ("v1", results.snapshots(0).package_info(0).version_string());
David Chen35045cb2018-03-23 22:21:47 -0700224}
225
David Chenbd125272018-04-04 19:02:50 -0700226TEST(UidMapTest, TestRemovedAppRetained) {
227 UidMap m;
228 // Initialize single config key.
229 ConfigKey config1(1, StringToId("config1"));
230 m.OnConfigUpdated(config1);
231 vector<int32_t> uids;
232 vector<int64_t> versions;
dwchen730403e2018-10-29 11:41:56 -0700233 vector<String16> versionStrings;
234 vector<String16> installers;
David Chenbd125272018-04-04 19:02:50 -0700235 vector<String16> apps;
236 uids.push_back(1000);
237 apps.push_back(String16(kApp2.c_str()));
238 versions.push_back(5);
dwchen730403e2018-10-29 11:41:56 -0700239 versionStrings.push_back(String16("v5"));
240 installers.push_back(String16(""));
241 m.updateMap(1, uids, versions, versionStrings, apps, installers);
David Chenbd125272018-04-04 19:02:50 -0700242 m.removeApp(2, String16(kApp2.c_str()), 1000);
243
244 ProtoOutputStream proto;
dwchen730403e2018-10-29 11:41:56 -0700245 m.appendUidMap(3, config1, nullptr, true, true, &proto);
David Chenbd125272018-04-04 19:02:50 -0700246
247 // Snapshot should still contain this item as deleted.
248 UidMapping results;
249 protoOutputStreamToUidMapping(&proto, &results);
250 EXPECT_EQ(1, results.snapshots(0).package_info_size());
251 EXPECT_EQ(true, results.snapshots(0).package_info(0).deleted());
252}
253
254TEST(UidMapTest, TestRemovedAppOverGuardrail) {
255 UidMap m;
256 // Initialize single config key.
257 ConfigKey config1(1, StringToId("config1"));
258 m.OnConfigUpdated(config1);
259 vector<int32_t> uids;
260 vector<int64_t> versions;
dwchen730403e2018-10-29 11:41:56 -0700261 vector<String16> versionStrings;
262 vector<String16> installers;
David Chenbd125272018-04-04 19:02:50 -0700263 vector<String16> apps;
264 const int maxDeletedApps = StatsdStats::kMaxDeletedAppsInUidMap;
265 for (int j = 0; j < maxDeletedApps + 10; j++) {
266 uids.push_back(j);
267 apps.push_back(String16(kApp1.c_str()));
268 versions.push_back(j);
dwchen730403e2018-10-29 11:41:56 -0700269 versionStrings.push_back(String16("v"));
270 installers.push_back(String16(""));
David Chenbd125272018-04-04 19:02:50 -0700271 }
dwchen730403e2018-10-29 11:41:56 -0700272 m.updateMap(1, uids, versions, versionStrings, apps, installers);
David Chenbd125272018-04-04 19:02:50 -0700273
274 // First, verify that we have the expected number of items.
275 UidMapping results;
276 ProtoOutputStream proto;
dwchen730403e2018-10-29 11:41:56 -0700277 m.appendUidMap(3, config1, nullptr, true, true, &proto);
David Chenbd125272018-04-04 19:02:50 -0700278 protoOutputStreamToUidMapping(&proto, &results);
279 EXPECT_EQ(maxDeletedApps + 10, results.snapshots(0).package_info_size());
280
281 // Now remove all the apps.
dwchen730403e2018-10-29 11:41:56 -0700282 m.updateMap(1, uids, versions, versionStrings, apps, installers);
David Chenbd125272018-04-04 19:02:50 -0700283 for (int j = 0; j < maxDeletedApps + 10; j++) {
284 m.removeApp(4, String16(kApp1.c_str()), j);
285 }
286
287 proto.clear();
dwchen730403e2018-10-29 11:41:56 -0700288 m.appendUidMap(5, config1, nullptr, true, true, &proto);
David Chenbd125272018-04-04 19:02:50 -0700289 // Snapshot drops the first nine items.
290 protoOutputStreamToUidMapping(&proto, &results);
291 EXPECT_EQ(maxDeletedApps, results.snapshots(0).package_info_size());
292}
293
David Chend6896892017-10-25 11:49:03 -0700294TEST(UidMapTest, TestClearingOutput) {
295 UidMap m;
296
Yangster-mac94e197c2018-01-02 16:03:03 -0800297 ConfigKey config1(1, StringToId("config1"));
298 ConfigKey config2(1, StringToId("config2"));
David Chend6896892017-10-25 11:49:03 -0700299
300 m.OnConfigUpdated(config1);
301
302 vector<int32_t> uids;
Dianne Hackborn3accca02013-09-20 09:32:11 -0700303 vector<int64_t> versions;
dwchen730403e2018-10-29 11:41:56 -0700304 vector<String16> versionStrings;
305 vector<String16> installers;
David Chend6896892017-10-25 11:49:03 -0700306 vector<String16> apps;
307 uids.push_back(1000);
308 uids.push_back(1000);
309 apps.push_back(String16(kApp1.c_str()));
310 apps.push_back(String16(kApp2.c_str()));
311 versions.push_back(4);
312 versions.push_back(5);
dwchen730403e2018-10-29 11:41:56 -0700313 versionStrings.push_back(String16("v4"));
314 versionStrings.push_back(String16("v5"));
315 installers.push_back(String16(""));
316 installers.push_back(String16(""));
317 m.updateMap(1, uids, versions, versionStrings, apps, installers);
David Chend6896892017-10-25 11:49:03 -0700318
yro4beccbe2018-03-15 19:42:05 -0700319 ProtoOutputStream proto;
dwchen730403e2018-10-29 11:41:56 -0700320 m.appendUidMap(2, config1, nullptr, true, true, &proto);
David Chenf384b902018-03-14 18:36:45 -0700321 UidMapping results;
yro4beccbe2018-03-15 19:42:05 -0700322 protoOutputStreamToUidMapping(&proto, &results);
David Chend6896892017-10-25 11:49:03 -0700323 EXPECT_EQ(1, results.snapshots_size());
324
David Chen35045cb2018-03-23 22:21:47 -0700325 // We have to keep at least one snapshot in memory at all times.
yro4beccbe2018-03-15 19:42:05 -0700326 proto.clear();
dwchen730403e2018-10-29 11:41:56 -0700327 m.appendUidMap(2, config1, nullptr, true, true, &proto);
yro4beccbe2018-03-15 19:42:05 -0700328 protoOutputStreamToUidMapping(&proto, &results);
David Chencfc311d2018-01-23 17:55:54 -0800329 EXPECT_EQ(1, results.snapshots_size());
David Chend6896892017-10-25 11:49:03 -0700330
331 // Now add another configuration.
332 m.OnConfigUpdated(config2);
dwchen730403e2018-10-29 11:41:56 -0700333 m.updateApp(5, String16(kApp1.c_str()), 1000, 40, String16("v40"), String16(""));
David Chenf384b902018-03-14 18:36:45 -0700334 EXPECT_EQ(1U, m.mChanges.size());
yro4beccbe2018-03-15 19:42:05 -0700335 proto.clear();
dwchen730403e2018-10-29 11:41:56 -0700336 m.appendUidMap(6, config1, nullptr, true, true, &proto);
yro4beccbe2018-03-15 19:42:05 -0700337 protoOutputStreamToUidMapping(&proto, &results);
David Chencfc311d2018-01-23 17:55:54 -0800338 EXPECT_EQ(1, results.snapshots_size());
David Chend6896892017-10-25 11:49:03 -0700339 EXPECT_EQ(1, results.changes_size());
David Chenf384b902018-03-14 18:36:45 -0700340 EXPECT_EQ(1U, m.mChanges.size());
David Chend6896892017-10-25 11:49:03 -0700341
David Chenc136f45a2017-11-27 11:52:26 -0800342 // Add another delta update.
dwchen730403e2018-10-29 11:41:56 -0700343 m.updateApp(7, String16(kApp2.c_str()), 1001, 41, String16("v41"), String16(""));
David Chenf384b902018-03-14 18:36:45 -0700344 EXPECT_EQ(2U, m.mChanges.size());
David Chenc136f45a2017-11-27 11:52:26 -0800345
346 // We still can't remove anything.
yro4beccbe2018-03-15 19:42:05 -0700347 proto.clear();
dwchen730403e2018-10-29 11:41:56 -0700348 m.appendUidMap(8, config1, nullptr, true, true, &proto);
yro4beccbe2018-03-15 19:42:05 -0700349 protoOutputStreamToUidMapping(&proto, &results);
David Chencfc311d2018-01-23 17:55:54 -0800350 EXPECT_EQ(1, results.snapshots_size());
David Chenf384b902018-03-14 18:36:45 -0700351 EXPECT_EQ(1, results.changes_size());
352 EXPECT_EQ(2U, m.mChanges.size());
David Chend6896892017-10-25 11:49:03 -0700353
yro4beccbe2018-03-15 19:42:05 -0700354 proto.clear();
dwchen730403e2018-10-29 11:41:56 -0700355 m.appendUidMap(9, config2, nullptr, true, true, &proto);
yro4beccbe2018-03-15 19:42:05 -0700356 protoOutputStreamToUidMapping(&proto, &results);
David Chencfc311d2018-01-23 17:55:54 -0800357 EXPECT_EQ(1, results.snapshots_size());
David Chend6896892017-10-25 11:49:03 -0700358 EXPECT_EQ(2, results.changes_size());
359 // At this point both should be cleared.
David Chenf384b902018-03-14 18:36:45 -0700360 EXPECT_EQ(0U, m.mChanges.size());
David Chend6896892017-10-25 11:49:03 -0700361}
David Chenc136f45a2017-11-27 11:52:26 -0800362
363TEST(UidMapTest, TestMemoryComputed) {
364 UidMap m;
365
Yangster-mac94e197c2018-01-02 16:03:03 -0800366 ConfigKey config1(1, StringToId("config1"));
David Chenc136f45a2017-11-27 11:52:26 -0800367 m.OnConfigUpdated(config1);
368
369 size_t startBytes = m.mBytesUsed;
370 vector<int32_t> uids;
Dianne Hackborn3accca02013-09-20 09:32:11 -0700371 vector<int64_t> versions;
David Chenc136f45a2017-11-27 11:52:26 -0800372 vector<String16> apps;
dwchen730403e2018-10-29 11:41:56 -0700373 vector<String16> versionStrings;
374 vector<String16> installers;
David Chenc136f45a2017-11-27 11:52:26 -0800375 uids.push_back(1000);
376 apps.push_back(String16(kApp1.c_str()));
377 versions.push_back(1);
dwchen730403e2018-10-29 11:41:56 -0700378 versionStrings.push_back(String16("v1"));
379 installers.push_back(String16(""));
380 m.updateMap(1, uids, versions, versionStrings, apps, installers);
David Chenc136f45a2017-11-27 11:52:26 -0800381
dwchen730403e2018-10-29 11:41:56 -0700382 m.updateApp(3, String16(kApp1.c_str()), 1000, 40, String16("v40"), String16(""));
David Chenc136f45a2017-11-27 11:52:26 -0800383
yro4beccbe2018-03-15 19:42:05 -0700384 ProtoOutputStream proto;
David Chenf384b902018-03-14 18:36:45 -0700385 vector<uint8_t> bytes;
dwchen730403e2018-10-29 11:41:56 -0700386 m.appendUidMap(2, config1, nullptr, true, true, &proto);
David Chenc136f45a2017-11-27 11:52:26 -0800387 size_t prevBytes = m.mBytesUsed;
388
dwchen730403e2018-10-29 11:41:56 -0700389 m.appendUidMap(4, config1, nullptr, true, true, &proto);
David Chenc136f45a2017-11-27 11:52:26 -0800390 EXPECT_TRUE(m.mBytesUsed < prevBytes);
391}
392
393TEST(UidMapTest, TestMemoryGuardrail) {
394 UidMap m;
395 string buf;
396
Yangster-mac94e197c2018-01-02 16:03:03 -0800397 ConfigKey config1(1, StringToId("config1"));
David Chenc136f45a2017-11-27 11:52:26 -0800398 m.OnConfigUpdated(config1);
399
400 size_t startBytes = m.mBytesUsed;
401 vector<int32_t> uids;
Dianne Hackborn3accca02013-09-20 09:32:11 -0700402 vector<int64_t> versions;
dwchen730403e2018-10-29 11:41:56 -0700403 vector<String16> versionStrings;
404 vector<String16> installers;
David Chenc136f45a2017-11-27 11:52:26 -0800405 vector<String16> apps;
406 for (int i = 0; i < 100; i++) {
407 uids.push_back(1);
408 buf = "EXTREMELY_LONG_STRING_FOR_APP_TO_WASTE_MEMORY." + to_string(i);
409 apps.push_back(String16(buf.c_str()));
410 versions.push_back(1);
dwchen730403e2018-10-29 11:41:56 -0700411 versionStrings.push_back(String16("v1"));
412 installers.push_back(String16(""));
David Chenc136f45a2017-11-27 11:52:26 -0800413 }
dwchen730403e2018-10-29 11:41:56 -0700414 m.updateMap(1, uids, versions, versionStrings, apps, installers);
David Chenc136f45a2017-11-27 11:52:26 -0800415
dwchen730403e2018-10-29 11:41:56 -0700416 m.updateApp(3, String16("EXTREMELY_LONG_STRING_FOR_APP_TO_WASTE_MEMORY.0"), 1000, 2,
417 String16("v2"), String16(""));
David Chenf384b902018-03-14 18:36:45 -0700418 EXPECT_EQ(1U, m.mChanges.size());
David Chenc136f45a2017-11-27 11:52:26 -0800419
420 // Now force deletion by limiting the memory to hold one delta change.
dwchen730403e2018-10-29 11:41:56 -0700421 m.maxBytesOverride = 120; // Since the app string alone requires >45 characters.
422 m.updateApp(5, String16("EXTREMELY_LONG_STRING_FOR_APP_TO_WASTE_MEMORY.0"), 1000, 4,
423 String16("v4"), String16(""));
David Chenf384b902018-03-14 18:36:45 -0700424 EXPECT_EQ(1U, m.mChanges.size());
David Chenc136f45a2017-11-27 11:52:26 -0800425}
Yangster-mac9def8e32018-04-17 13:55:51 -0700426
David Chende701692017-10-05 13:16:02 -0700427#else
428GTEST_LOG_(INFO) << "This test does nothing.\n";
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700429#endif
David Chend6896892017-10-25 11:49:03 -0700430
431} // namespace statsd
432} // namespace os
yro4beccbe2018-03-15 19:42:05 -0700433} // namespace android