blob: d9fa4e99d54d85b855683b33d47960c703a06f98 [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;
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
David Chen21582962017-11-01 17:32:46 -070042TEST(UidMapTest, TestIsolatedUID) {
43 sp<UidMap> m = new UidMap();
Chenjie Yue2219202018-06-08 10:07:51 -070044 sp<StatsPullerManager> pullerManager = new StatsPullerManager();
Yangster-mac932ecec2018-02-01 10:23:52 -080045 sp<AlarmMonitor> anomalyAlarmMonitor;
46 sp<AlarmMonitor> subscriberAlarmMonitor;
David Chenc136f45a2017-11-27 11:52:26 -080047 // Construct the processor with a dummy sendBroadcast function that does nothing.
Chenjie Yue2219202018-06-08 10:07:51 -070048 StatsLogProcessor p(m, pullerManager, anomalyAlarmMonitor, subscriberAlarmMonitor, 0,
Tej Singh6ede28b2019-01-29 17:06:54 -080049 [](const ConfigKey& key) { return true; },
50 [](const int&, const vector<int64_t>&) {return true;});
David Chen21582962017-11-01 17:32:46 -070051 LogEvent addEvent(android::util::ISOLATED_UID_CHANGED, 1);
Yao Chen80235402017-11-13 20:42:25 -080052 addEvent.write(100); // parent UID
53 addEvent.write(101); // isolated UID
54 addEvent.write(1); // Indicates creation.
David Chen21582962017-11-01 17:32:46 -070055 addEvent.init();
56
Yangster-macd40053e2018-01-09 16:29:22 -080057 EXPECT_EQ(101, m->getHostUidOrSelf(101));
David Chen21582962017-11-01 17:32:46 -070058
Yangster-macd40053e2018-01-09 16:29:22 -080059 p.OnLogEvent(&addEvent);
60 EXPECT_EQ(100, m->getHostUidOrSelf(101));
David Chen21582962017-11-01 17:32:46 -070061
62 LogEvent removeEvent(android::util::ISOLATED_UID_CHANGED, 1);
Yao Chen80235402017-11-13 20:42:25 -080063 removeEvent.write(100); // parent UID
64 removeEvent.write(101); // isolated UID
65 removeEvent.write(0); // Indicates removal.
David Chen21582962017-11-01 17:32:46 -070066 removeEvent.init();
Yangster-macd40053e2018-01-09 16:29:22 -080067 p.OnLogEvent(&removeEvent);
68 EXPECT_EQ(101, m->getHostUidOrSelf(101));
David Chen21582962017-11-01 17:32:46 -070069}
70
David Chende701692017-10-05 13:16:02 -070071TEST(UidMapTest, TestMatching) {
72 UidMap m;
73 vector<int32_t> uids;
Dianne Hackborn3accca02013-09-20 09:32:11 -070074 vector<int64_t> versions;
David Chende701692017-10-05 13:16:02 -070075 vector<String16> apps;
dwchen730403e2018-10-29 11:41:56 -070076 vector<String16> versionStrings;
77 vector<String16> installers;
David Chende701692017-10-05 13:16:02 -070078
79 uids.push_back(1000);
80 uids.push_back(1000);
dwchen730403e2018-10-29 11:41:56 -070081 versionStrings.push_back(String16("v1"));
82 versionStrings.push_back(String16("v1"));
83 installers.push_back(String16(""));
84 installers.push_back(String16(""));
David Chende701692017-10-05 13:16:02 -070085 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);
dwchen730403e2018-10-29 11:41:56 -070089 m.updateMap(1, uids, versions, versionStrings, apps, installers);
David Chende701692017-10-05 13:16:02 -070090 EXPECT_TRUE(m.hasApp(1000, kApp1));
91 EXPECT_TRUE(m.hasApp(1000, kApp2));
92 EXPECT_FALSE(m.hasApp(1000, "not.app"));
Yangster9df9a7f2017-12-18 13:33:05 -080093
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());
David Chende701692017-10-05 13:16:02 -0700101}
102
103TEST(UidMapTest, TestAddAndRemove) {
104 UidMap m;
105 vector<int32_t> uids;
Dianne Hackborn3accca02013-09-20 09:32:11 -0700106 vector<int64_t> versions;
David Chende701692017-10-05 13:16:02 -0700107 vector<String16> apps;
dwchen730403e2018-10-29 11:41:56 -0700108 vector<String16> versionStrings;
109 vector<String16> installers;
David Chende701692017-10-05 13:16:02 -0700110
111 uids.push_back(1000);
112 uids.push_back(1000);
dwchen730403e2018-10-29 11:41:56 -0700113 versionStrings.push_back(String16("v1"));
114 versionStrings.push_back(String16("v1"));
115 installers.push_back(String16(""));
116 installers.push_back(String16(""));
David Chende701692017-10-05 13:16:02 -0700117 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);
dwchen730403e2018-10-29 11:41:56 -0700121 m.updateMap(1, uids, versions, versionStrings, apps, installers);
David Chende701692017-10-05 13:16:02 -0700122
Yangster9df9a7f2017-12-18 13:33:05 -0800123 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.
dwchen730403e2018-10-29 11:41:56 -0700129 m.updateApp(2, String16(kApp1.c_str()), 1000, 40, String16("v40"), String16(""));
David Chende701692017-10-05 13:16:02 -0700130 EXPECT_EQ(40, m.getAppVersion(1000, kApp1));
131
Yangster9df9a7f2017-12-18 13:33:05 -0800132 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
David Chenbd125272018-04-04 19:02:50 -0700137 m.removeApp(3, String16(kApp1.c_str()), 1000);
David Chende701692017-10-05 13:16:02 -0700138 EXPECT_FALSE(m.hasApp(1000, kApp1));
139 EXPECT_TRUE(m.hasApp(1000, kApp2));
Yangster9df9a7f2017-12-18 13:33:05 -0800140 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.
David Chenbd125272018-04-04 19:02:50 -0700146 m.removeApp(4, String16(kApp2.c_str()), 1000);
Yangster9df9a7f2017-12-18 13:33:05 -0800147 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
153TEST(UidMapTest, TestUpdateApp) {
154 UidMap m;
dwchen730403e2018-10-29 11:41:56 -0700155 m.updateMap(1, {1000, 1000}, {4, 5}, {String16("v4"), String16("v5")},
156 {String16(kApp1.c_str()), String16(kApp2.c_str())}, {String16(""), String16("")});
Yangster9df9a7f2017-12-18 13:33:05 -0800157 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.
dwchen730403e2018-10-29 11:41:56 -0700163 m.updateApp(2, String16("NeW_aPP1_NAmE"), 1000, 40, String16("v40"), String16(""));
Yangster9df9a7f2017-12-18 13:33:05 -0800164 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.
dwchen730403e2018-10-29 11:41:56 -0700172 m.updateApp(3, String16("NeW_aPP1_NAmE"), 2000, 1, String16("v1"), String16(""));
Yangster9df9a7f2017-12-18 13:33:05 -0800173 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());
David Chende701692017-10-05 13:16:02 -0700177}
David Chend6896892017-10-25 11:49:03 -0700178
yro4beccbe2018-03-15 19:42:05 -0700179static void protoOutputStreamToUidMapping(ProtoOutputStream* proto, UidMapping* results) {
180 vector<uint8_t> bytes;
181 bytes.resize(proto->size());
182 size_t pos = 0;
Joe Onorato99598ee2019-02-11 15:55:13 +0000183 sp<ProtoReader> reader = proto->data();
184 while (reader->readBuffer() != NULL) {
185 size_t toRead = reader->currentToRead();
186 std::memcpy(&((bytes)[pos]), reader->readBuffer(), toRead);
yro4beccbe2018-03-15 19:42:05 -0700187 pos += toRead;
Joe Onorato99598ee2019-02-11 15:55:13 +0000188 reader->move(toRead);
yro4beccbe2018-03-15 19:42:05 -0700189 }
190 results->ParseFromArray(bytes.data(), bytes.size());
191}
192
David Chen35045cb2018-03-23 22:21:47 -0700193// Test that uid map returns at least one snapshot even if we already obtained
194// this snapshot from a previous call to getData.
195TEST(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;
dwchen730403e2018-10-29 11:41:56 -0700203 vector<String16> versionStrings;
204 vector<String16> installers;
David Chen35045cb2018-03-23 22:21:47 -0700205 uids.push_back(1000);
206 apps.push_back(String16(kApp2.c_str()));
dwchen730403e2018-10-29 11:41:56 -0700207 versionStrings.push_back(String16("v1"));
208 installers.push_back(String16(""));
David Chen35045cb2018-03-23 22:21:47 -0700209 versions.push_back(5);
dwchen730403e2018-10-29 11:41:56 -0700210 m.updateMap(1, uids, versions, versionStrings, apps, installers);
David Chen35045cb2018-03-23 22:21:47 -0700211
212 // Set the last timestamp for this config key to be newer.
213 m.mLastUpdatePerConfigKey[config1] = 2;
214
215 ProtoOutputStream proto;
dwchen730403e2018-10-29 11:41:56 -0700216 m.appendUidMap(3, config1, nullptr, true, true, &proto);
David Chen35045cb2018-03-23 22:21:47 -0700217
218 // Check there's still a uidmap attached this one.
219 UidMapping results;
220 protoOutputStreamToUidMapping(&proto, &results);
221 EXPECT_EQ(1, results.snapshots_size());
dwchen730403e2018-10-29 11:41:56 -0700222 EXPECT_EQ("v1", results.snapshots(0).package_info(0).version_string());
David Chen35045cb2018-03-23 22:21:47 -0700223}
224
David Chenbd125272018-04-04 19:02:50 -0700225TEST(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;
dwchen730403e2018-10-29 11:41:56 -0700232 vector<String16> versionStrings;
233 vector<String16> installers;
David Chenbd125272018-04-04 19:02:50 -0700234 vector<String16> apps;
235 uids.push_back(1000);
236 apps.push_back(String16(kApp2.c_str()));
237 versions.push_back(5);
dwchen730403e2018-10-29 11:41:56 -0700238 versionStrings.push_back(String16("v5"));
239 installers.push_back(String16(""));
240 m.updateMap(1, uids, versions, versionStrings, apps, installers);
David Chenbd125272018-04-04 19:02:50 -0700241 m.removeApp(2, String16(kApp2.c_str()), 1000);
242
243 ProtoOutputStream proto;
dwchen730403e2018-10-29 11:41:56 -0700244 m.appendUidMap(3, config1, nullptr, true, true, &proto);
David Chenbd125272018-04-04 19:02:50 -0700245
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
253TEST(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;
dwchen730403e2018-10-29 11:41:56 -0700260 vector<String16> versionStrings;
261 vector<String16> installers;
David Chenbd125272018-04-04 19:02:50 -0700262 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);
dwchen730403e2018-10-29 11:41:56 -0700268 versionStrings.push_back(String16("v"));
269 installers.push_back(String16(""));
David Chenbd125272018-04-04 19:02:50 -0700270 }
dwchen730403e2018-10-29 11:41:56 -0700271 m.updateMap(1, uids, versions, versionStrings, apps, installers);
David Chenbd125272018-04-04 19:02:50 -0700272
273 // First, verify that we have the expected number of items.
274 UidMapping results;
275 ProtoOutputStream proto;
dwchen730403e2018-10-29 11:41:56 -0700276 m.appendUidMap(3, config1, nullptr, true, true, &proto);
David Chenbd125272018-04-04 19:02:50 -0700277 protoOutputStreamToUidMapping(&proto, &results);
278 EXPECT_EQ(maxDeletedApps + 10, results.snapshots(0).package_info_size());
279
280 // Now remove all the apps.
dwchen730403e2018-10-29 11:41:56 -0700281 m.updateMap(1, uids, versions, versionStrings, apps, installers);
David Chenbd125272018-04-04 19:02:50 -0700282 for (int j = 0; j < maxDeletedApps + 10; j++) {
283 m.removeApp(4, String16(kApp1.c_str()), j);
284 }
285
286 proto.clear();
dwchen730403e2018-10-29 11:41:56 -0700287 m.appendUidMap(5, config1, nullptr, true, true, &proto);
David Chenbd125272018-04-04 19:02:50 -0700288 // Snapshot drops the first nine items.
289 protoOutputStreamToUidMapping(&proto, &results);
290 EXPECT_EQ(maxDeletedApps, results.snapshots(0).package_info_size());
291}
292
David Chend6896892017-10-25 11:49:03 -0700293TEST(UidMapTest, TestClearingOutput) {
294 UidMap m;
295
Yangster-mac94e197c2018-01-02 16:03:03 -0800296 ConfigKey config1(1, StringToId("config1"));
297 ConfigKey config2(1, StringToId("config2"));
David Chend6896892017-10-25 11:49:03 -0700298
299 m.OnConfigUpdated(config1);
300
301 vector<int32_t> uids;
Dianne Hackborn3accca02013-09-20 09:32:11 -0700302 vector<int64_t> versions;
dwchen730403e2018-10-29 11:41:56 -0700303 vector<String16> versionStrings;
304 vector<String16> installers;
David Chend6896892017-10-25 11:49:03 -0700305 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);
dwchen730403e2018-10-29 11:41:56 -0700312 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);
David Chend6896892017-10-25 11:49:03 -0700317
yro4beccbe2018-03-15 19:42:05 -0700318 ProtoOutputStream proto;
dwchen730403e2018-10-29 11:41:56 -0700319 m.appendUidMap(2, config1, nullptr, true, true, &proto);
David Chenf384b902018-03-14 18:36:45 -0700320 UidMapping results;
yro4beccbe2018-03-15 19:42:05 -0700321 protoOutputStreamToUidMapping(&proto, &results);
David Chend6896892017-10-25 11:49:03 -0700322 EXPECT_EQ(1, results.snapshots_size());
323
David Chen35045cb2018-03-23 22:21:47 -0700324 // We have to keep at least one snapshot in memory at all times.
yro4beccbe2018-03-15 19:42:05 -0700325 proto.clear();
dwchen730403e2018-10-29 11:41:56 -0700326 m.appendUidMap(2, config1, nullptr, true, true, &proto);
yro4beccbe2018-03-15 19:42:05 -0700327 protoOutputStreamToUidMapping(&proto, &results);
David Chencfc311d2018-01-23 17:55:54 -0800328 EXPECT_EQ(1, results.snapshots_size());
David Chend6896892017-10-25 11:49:03 -0700329
330 // Now add another configuration.
331 m.OnConfigUpdated(config2);
dwchen730403e2018-10-29 11:41:56 -0700332 m.updateApp(5, String16(kApp1.c_str()), 1000, 40, String16("v40"), String16(""));
David Chenf384b902018-03-14 18:36:45 -0700333 EXPECT_EQ(1U, m.mChanges.size());
yro4beccbe2018-03-15 19:42:05 -0700334 proto.clear();
dwchen730403e2018-10-29 11:41:56 -0700335 m.appendUidMap(6, config1, nullptr, true, true, &proto);
yro4beccbe2018-03-15 19:42:05 -0700336 protoOutputStreamToUidMapping(&proto, &results);
David Chencfc311d2018-01-23 17:55:54 -0800337 EXPECT_EQ(1, results.snapshots_size());
David Chend6896892017-10-25 11:49:03 -0700338 EXPECT_EQ(1, results.changes_size());
David Chenf384b902018-03-14 18:36:45 -0700339 EXPECT_EQ(1U, m.mChanges.size());
David Chend6896892017-10-25 11:49:03 -0700340
David Chenc136f45a2017-11-27 11:52:26 -0800341 // Add another delta update.
dwchen730403e2018-10-29 11:41:56 -0700342 m.updateApp(7, String16(kApp2.c_str()), 1001, 41, String16("v41"), String16(""));
David Chenf384b902018-03-14 18:36:45 -0700343 EXPECT_EQ(2U, m.mChanges.size());
David Chenc136f45a2017-11-27 11:52:26 -0800344
345 // We still can't remove anything.
yro4beccbe2018-03-15 19:42:05 -0700346 proto.clear();
dwchen730403e2018-10-29 11:41:56 -0700347 m.appendUidMap(8, config1, nullptr, true, true, &proto);
yro4beccbe2018-03-15 19:42:05 -0700348 protoOutputStreamToUidMapping(&proto, &results);
David Chencfc311d2018-01-23 17:55:54 -0800349 EXPECT_EQ(1, results.snapshots_size());
David Chenf384b902018-03-14 18:36:45 -0700350 EXPECT_EQ(1, results.changes_size());
351 EXPECT_EQ(2U, m.mChanges.size());
David Chend6896892017-10-25 11:49:03 -0700352
yro4beccbe2018-03-15 19:42:05 -0700353 proto.clear();
dwchen730403e2018-10-29 11:41:56 -0700354 m.appendUidMap(9, config2, nullptr, true, true, &proto);
yro4beccbe2018-03-15 19:42:05 -0700355 protoOutputStreamToUidMapping(&proto, &results);
David Chencfc311d2018-01-23 17:55:54 -0800356 EXPECT_EQ(1, results.snapshots_size());
David Chend6896892017-10-25 11:49:03 -0700357 EXPECT_EQ(2, results.changes_size());
358 // At this point both should be cleared.
David Chenf384b902018-03-14 18:36:45 -0700359 EXPECT_EQ(0U, m.mChanges.size());
David Chend6896892017-10-25 11:49:03 -0700360}
David Chenc136f45a2017-11-27 11:52:26 -0800361
362TEST(UidMapTest, TestMemoryComputed) {
363 UidMap m;
364
Yangster-mac94e197c2018-01-02 16:03:03 -0800365 ConfigKey config1(1, StringToId("config1"));
David Chenc136f45a2017-11-27 11:52:26 -0800366 m.OnConfigUpdated(config1);
367
368 size_t startBytes = m.mBytesUsed;
369 vector<int32_t> uids;
Dianne Hackborn3accca02013-09-20 09:32:11 -0700370 vector<int64_t> versions;
David Chenc136f45a2017-11-27 11:52:26 -0800371 vector<String16> apps;
dwchen730403e2018-10-29 11:41:56 -0700372 vector<String16> versionStrings;
373 vector<String16> installers;
David Chenc136f45a2017-11-27 11:52:26 -0800374 uids.push_back(1000);
375 apps.push_back(String16(kApp1.c_str()));
376 versions.push_back(1);
dwchen730403e2018-10-29 11:41:56 -0700377 versionStrings.push_back(String16("v1"));
378 installers.push_back(String16(""));
379 m.updateMap(1, uids, versions, versionStrings, apps, installers);
David Chenc136f45a2017-11-27 11:52:26 -0800380
dwchen730403e2018-10-29 11:41:56 -0700381 m.updateApp(3, String16(kApp1.c_str()), 1000, 40, String16("v40"), String16(""));
David Chenc136f45a2017-11-27 11:52:26 -0800382
yro4beccbe2018-03-15 19:42:05 -0700383 ProtoOutputStream proto;
David Chenf384b902018-03-14 18:36:45 -0700384 vector<uint8_t> bytes;
dwchen730403e2018-10-29 11:41:56 -0700385 m.appendUidMap(2, config1, nullptr, true, true, &proto);
David Chenc136f45a2017-11-27 11:52:26 -0800386 size_t prevBytes = m.mBytesUsed;
387
dwchen730403e2018-10-29 11:41:56 -0700388 m.appendUidMap(4, config1, nullptr, true, true, &proto);
David Chenc136f45a2017-11-27 11:52:26 -0800389 EXPECT_TRUE(m.mBytesUsed < prevBytes);
390}
391
392TEST(UidMapTest, TestMemoryGuardrail) {
393 UidMap m;
394 string buf;
395
Yangster-mac94e197c2018-01-02 16:03:03 -0800396 ConfigKey config1(1, StringToId("config1"));
David Chenc136f45a2017-11-27 11:52:26 -0800397 m.OnConfigUpdated(config1);
398
399 size_t startBytes = m.mBytesUsed;
400 vector<int32_t> uids;
Dianne Hackborn3accca02013-09-20 09:32:11 -0700401 vector<int64_t> versions;
dwchen730403e2018-10-29 11:41:56 -0700402 vector<String16> versionStrings;
403 vector<String16> installers;
David Chenc136f45a2017-11-27 11:52:26 -0800404 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);
dwchen730403e2018-10-29 11:41:56 -0700410 versionStrings.push_back(String16("v1"));
411 installers.push_back(String16(""));
David Chenc136f45a2017-11-27 11:52:26 -0800412 }
dwchen730403e2018-10-29 11:41:56 -0700413 m.updateMap(1, uids, versions, versionStrings, apps, installers);
David Chenc136f45a2017-11-27 11:52:26 -0800414
dwchen730403e2018-10-29 11:41:56 -0700415 m.updateApp(3, String16("EXTREMELY_LONG_STRING_FOR_APP_TO_WASTE_MEMORY.0"), 1000, 2,
416 String16("v2"), String16(""));
David Chenf384b902018-03-14 18:36:45 -0700417 EXPECT_EQ(1U, m.mChanges.size());
David Chenc136f45a2017-11-27 11:52:26 -0800418
419 // Now force deletion by limiting the memory to hold one delta change.
dwchen730403e2018-10-29 11:41:56 -0700420 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(""));
David Chenf384b902018-03-14 18:36:45 -0700423 EXPECT_EQ(1U, m.mChanges.size());
David Chenc136f45a2017-11-27 11:52:26 -0800424}
Yangster-mac9def8e32018-04-17 13:55:51 -0700425
David Chende701692017-10-05 13:16:02 -0700426#else
427GTEST_LOG_(INFO) << "This test does nothing.\n";
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700428#endif
David Chend6896892017-10-25 11:49:03 -0700429
430} // namespace statsd
431} // namespace os
yro4beccbe2018-03-15 19:42:05 -0700432} // namespace android