blob: c04a40cfebd94ad643e2765f8b67fbab94c2608e [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 Chenc136f452017-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 Chenc136f452017-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,
Tej Singh6ede28b2019-01-29 17:06:54 -080048 [](const ConfigKey& key) { return true; },
49 [](const int&, const vector<int64_t>&) {return true;});
David Chen21582962017-11-01 17:32:46 -070050 LogEvent addEvent(android::util::ISOLATED_UID_CHANGED, 1);
Yao Chen80235402017-11-13 20:42:25 -080051 addEvent.write(100); // parent UID
52 addEvent.write(101); // isolated UID
53 addEvent.write(1); // Indicates creation.
David Chen21582962017-11-01 17:32:46 -070054 addEvent.init();
55
Yangster-macd40053e2018-01-09 16:29:22 -080056 EXPECT_EQ(101, m->getHostUidOrSelf(101));
David Chen21582962017-11-01 17:32:46 -070057
Yangster-macd40053e2018-01-09 16:29:22 -080058 p.OnLogEvent(&addEvent);
59 EXPECT_EQ(100, m->getHostUidOrSelf(101));
David Chen21582962017-11-01 17:32:46 -070060
61 LogEvent removeEvent(android::util::ISOLATED_UID_CHANGED, 1);
Yao Chen80235402017-11-13 20:42:25 -080062 removeEvent.write(100); // parent UID
63 removeEvent.write(101); // isolated UID
64 removeEvent.write(0); // Indicates removal.
David Chen21582962017-11-01 17:32:46 -070065 removeEvent.init();
Yangster-macd40053e2018-01-09 16:29:22 -080066 p.OnLogEvent(&removeEvent);
67 EXPECT_EQ(101, m->getHostUidOrSelf(101));
David Chen21582962017-11-01 17:32:46 -070068}
69
David Chende701692017-10-05 13:16:02 -070070TEST(UidMapTest, TestMatching) {
71 UidMap m;
72 vector<int32_t> uids;
Dianne Hackborn3accca02013-09-20 09:32:11 -070073 vector<int64_t> versions;
David Chende701692017-10-05 13:16:02 -070074 vector<String16> apps;
dwchen730403e2018-10-29 11:41:56 -070075 vector<String16> versionStrings;
76 vector<String16> installers;
David Chende701692017-10-05 13:16:02 -070077
78 uids.push_back(1000);
79 uids.push_back(1000);
dwchen730403e2018-10-29 11:41:56 -070080 versionStrings.push_back(String16("v1"));
81 versionStrings.push_back(String16("v1"));
82 installers.push_back(String16(""));
83 installers.push_back(String16(""));
David Chende701692017-10-05 13:16:02 -070084 apps.push_back(String16(kApp1.c_str()));
85 apps.push_back(String16(kApp2.c_str()));
86 versions.push_back(4);
87 versions.push_back(5);
dwchen730403e2018-10-29 11:41:56 -070088 m.updateMap(1, uids, versions, versionStrings, apps, installers);
David Chende701692017-10-05 13:16:02 -070089 EXPECT_TRUE(m.hasApp(1000, kApp1));
90 EXPECT_TRUE(m.hasApp(1000, kApp2));
91 EXPECT_FALSE(m.hasApp(1000, "not.app"));
Yangster9df9a7f2017-12-18 13:33:05 -080092
93 std::set<string> name_set = m.getAppNamesFromUid(1000u, true /* returnNormalized */);
94 EXPECT_EQ(name_set.size(), 2u);
95 EXPECT_TRUE(name_set.find(kApp1) != name_set.end());
96 EXPECT_TRUE(name_set.find(kApp2) != name_set.end());
97
98 name_set = m.getAppNamesFromUid(12345, true /* returnNormalized */);
99 EXPECT_TRUE(name_set.empty());
David Chende701692017-10-05 13:16:02 -0700100}
101
102TEST(UidMapTest, TestAddAndRemove) {
103 UidMap m;
104 vector<int32_t> uids;
Dianne Hackborn3accca02013-09-20 09:32:11 -0700105 vector<int64_t> versions;
David Chende701692017-10-05 13:16:02 -0700106 vector<String16> apps;
dwchen730403e2018-10-29 11:41:56 -0700107 vector<String16> versionStrings;
108 vector<String16> installers;
David Chende701692017-10-05 13:16:02 -0700109
110 uids.push_back(1000);
111 uids.push_back(1000);
dwchen730403e2018-10-29 11:41:56 -0700112 versionStrings.push_back(String16("v1"));
113 versionStrings.push_back(String16("v1"));
114 installers.push_back(String16(""));
115 installers.push_back(String16(""));
David Chende701692017-10-05 13:16:02 -0700116 apps.push_back(String16(kApp1.c_str()));
117 apps.push_back(String16(kApp2.c_str()));
118 versions.push_back(4);
119 versions.push_back(5);
dwchen730403e2018-10-29 11:41:56 -0700120 m.updateMap(1, uids, versions, versionStrings, apps, installers);
David Chende701692017-10-05 13:16:02 -0700121
Yangster9df9a7f2017-12-18 13:33:05 -0800122 std::set<string> name_set = m.getAppNamesFromUid(1000, true /* returnNormalized */);
123 EXPECT_EQ(name_set.size(), 2u);
124 EXPECT_TRUE(name_set.find(kApp1) != name_set.end());
125 EXPECT_TRUE(name_set.find(kApp2) != name_set.end());
126
127 // Update the app1 version.
dwchen730403e2018-10-29 11:41:56 -0700128 m.updateApp(2, String16(kApp1.c_str()), 1000, 40, String16("v40"), String16(""));
David Chende701692017-10-05 13:16:02 -0700129 EXPECT_EQ(40, m.getAppVersion(1000, kApp1));
130
Yangster9df9a7f2017-12-18 13:33:05 -0800131 name_set = m.getAppNamesFromUid(1000, true /* returnNormalized */);
132 EXPECT_EQ(name_set.size(), 2u);
133 EXPECT_TRUE(name_set.find(kApp1) != name_set.end());
134 EXPECT_TRUE(name_set.find(kApp2) != name_set.end());
135
David Chenbd125272018-04-04 19:02:50 -0700136 m.removeApp(3, String16(kApp1.c_str()), 1000);
David Chende701692017-10-05 13:16:02 -0700137 EXPECT_FALSE(m.hasApp(1000, kApp1));
138 EXPECT_TRUE(m.hasApp(1000, kApp2));
Yangster9df9a7f2017-12-18 13:33:05 -0800139 name_set = m.getAppNamesFromUid(1000, true /* returnNormalized */);
140 EXPECT_EQ(name_set.size(), 1u);
141 EXPECT_TRUE(name_set.find(kApp1) == name_set.end());
142 EXPECT_TRUE(name_set.find(kApp2) != name_set.end());
143
144 // Remove app2.
David Chenbd125272018-04-04 19:02:50 -0700145 m.removeApp(4, String16(kApp2.c_str()), 1000);
Yangster9df9a7f2017-12-18 13:33:05 -0800146 EXPECT_FALSE(m.hasApp(1000, kApp1));
147 EXPECT_FALSE(m.hasApp(1000, kApp2));
148 name_set = m.getAppNamesFromUid(1000, true /* returnNormalized */);
149 EXPECT_TRUE(name_set.empty());
150}
151
152TEST(UidMapTest, TestUpdateApp) {
153 UidMap m;
dwchen730403e2018-10-29 11:41:56 -0700154 m.updateMap(1, {1000, 1000}, {4, 5}, {String16("v4"), String16("v5")},
155 {String16(kApp1.c_str()), String16(kApp2.c_str())}, {String16(""), String16("")});
Yangster9df9a7f2017-12-18 13:33:05 -0800156 std::set<string> name_set = m.getAppNamesFromUid(1000, true /* returnNormalized */);
157 EXPECT_EQ(name_set.size(), 2u);
158 EXPECT_TRUE(name_set.find(kApp1) != name_set.end());
159 EXPECT_TRUE(name_set.find(kApp2) != name_set.end());
160
161 // Adds a new name for uid 1000.
dwchen730403e2018-10-29 11:41:56 -0700162 m.updateApp(2, String16("NeW_aPP1_NAmE"), 1000, 40, String16("v40"), String16(""));
Yangster9df9a7f2017-12-18 13:33:05 -0800163 name_set = m.getAppNamesFromUid(1000, true /* returnNormalized */);
164 EXPECT_EQ(name_set.size(), 3u);
165 EXPECT_TRUE(name_set.find(kApp1) != name_set.end());
166 EXPECT_TRUE(name_set.find(kApp2) != name_set.end());
167 EXPECT_TRUE(name_set.find("NeW_aPP1_NAmE") == name_set.end());
168 EXPECT_TRUE(name_set.find("new_app1_name") != name_set.end());
169
170 // This name is also reused by another uid 2000.
dwchen730403e2018-10-29 11:41:56 -0700171 m.updateApp(3, String16("NeW_aPP1_NAmE"), 2000, 1, String16("v1"), String16(""));
Yangster9df9a7f2017-12-18 13:33:05 -0800172 name_set = m.getAppNamesFromUid(2000, true /* returnNormalized */);
173 EXPECT_EQ(name_set.size(), 1u);
174 EXPECT_TRUE(name_set.find("NeW_aPP1_NAmE") == name_set.end());
175 EXPECT_TRUE(name_set.find("new_app1_name") != name_set.end());
David Chende701692017-10-05 13:16:02 -0700176}
David Chend6896892017-10-25 11:49:03 -0700177
yro4beccbe2018-03-15 19:42:05 -0700178static void protoOutputStreamToUidMapping(ProtoOutputStream* proto, UidMapping* results) {
179 vector<uint8_t> bytes;
180 bytes.resize(proto->size());
181 size_t pos = 0;
182 auto iter = proto->data();
183 while (iter.readBuffer() != NULL) {
184 size_t toRead = iter.currentToRead();
185 std::memcpy(&((bytes)[pos]), iter.readBuffer(), toRead);
186 pos += toRead;
187 iter.rp()->move(toRead);
188 }
189 results->ParseFromArray(bytes.data(), bytes.size());
190}
191
David Chen35045cb2018-03-23 22:21:47 -0700192// Test that uid map returns at least one snapshot even if we already obtained
193// this snapshot from a previous call to getData.
194TEST(UidMapTest, TestOutputIncludesAtLeastOneSnapshot) {
195 UidMap m;
196 // Initialize single config key.
197 ConfigKey config1(1, StringToId("config1"));
198 m.OnConfigUpdated(config1);
199 vector<int32_t> uids;
200 vector<int64_t> versions;
201 vector<String16> apps;
dwchen730403e2018-10-29 11:41:56 -0700202 vector<String16> versionStrings;
203 vector<String16> installers;
David Chen35045cb2018-03-23 22:21:47 -0700204 uids.push_back(1000);
205 apps.push_back(String16(kApp2.c_str()));
dwchen730403e2018-10-29 11:41:56 -0700206 versionStrings.push_back(String16("v1"));
207 installers.push_back(String16(""));
David Chen35045cb2018-03-23 22:21:47 -0700208 versions.push_back(5);
dwchen730403e2018-10-29 11:41:56 -0700209 m.updateMap(1, uids, versions, versionStrings, apps, installers);
David Chen35045cb2018-03-23 22:21:47 -0700210
211 // Set the last timestamp for this config key to be newer.
212 m.mLastUpdatePerConfigKey[config1] = 2;
213
214 ProtoOutputStream proto;
dwchen730403e2018-10-29 11:41:56 -0700215 m.appendUidMap(3, config1, nullptr, true, true, &proto);
David Chen35045cb2018-03-23 22:21:47 -0700216
217 // Check there's still a uidmap attached this one.
218 UidMapping results;
219 protoOutputStreamToUidMapping(&proto, &results);
220 EXPECT_EQ(1, results.snapshots_size());
dwchen730403e2018-10-29 11:41:56 -0700221 EXPECT_EQ("v1", results.snapshots(0).package_info(0).version_string());
David Chen35045cb2018-03-23 22:21:47 -0700222}
223
David Chenbd125272018-04-04 19:02:50 -0700224TEST(UidMapTest, TestRemovedAppRetained) {
225 UidMap m;
226 // Initialize single config key.
227 ConfigKey config1(1, StringToId("config1"));
228 m.OnConfigUpdated(config1);
229 vector<int32_t> uids;
230 vector<int64_t> versions;
dwchen730403e2018-10-29 11:41:56 -0700231 vector<String16> versionStrings;
232 vector<String16> installers;
David Chenbd125272018-04-04 19:02:50 -0700233 vector<String16> apps;
234 uids.push_back(1000);
235 apps.push_back(String16(kApp2.c_str()));
236 versions.push_back(5);
dwchen730403e2018-10-29 11:41:56 -0700237 versionStrings.push_back(String16("v5"));
238 installers.push_back(String16(""));
239 m.updateMap(1, uids, versions, versionStrings, apps, installers);
David Chenbd125272018-04-04 19:02:50 -0700240 m.removeApp(2, String16(kApp2.c_str()), 1000);
241
242 ProtoOutputStream proto;
dwchen730403e2018-10-29 11:41:56 -0700243 m.appendUidMap(3, config1, nullptr, true, true, &proto);
David Chenbd125272018-04-04 19:02:50 -0700244
245 // Snapshot should still contain this item as deleted.
246 UidMapping results;
247 protoOutputStreamToUidMapping(&proto, &results);
248 EXPECT_EQ(1, results.snapshots(0).package_info_size());
249 EXPECT_EQ(true, results.snapshots(0).package_info(0).deleted());
250}
251
252TEST(UidMapTest, TestRemovedAppOverGuardrail) {
253 UidMap m;
254 // Initialize single config key.
255 ConfigKey config1(1, StringToId("config1"));
256 m.OnConfigUpdated(config1);
257 vector<int32_t> uids;
258 vector<int64_t> versions;
dwchen730403e2018-10-29 11:41:56 -0700259 vector<String16> versionStrings;
260 vector<String16> installers;
David Chenbd125272018-04-04 19:02:50 -0700261 vector<String16> apps;
262 const int maxDeletedApps = StatsdStats::kMaxDeletedAppsInUidMap;
263 for (int j = 0; j < maxDeletedApps + 10; j++) {
264 uids.push_back(j);
265 apps.push_back(String16(kApp1.c_str()));
266 versions.push_back(j);
dwchen730403e2018-10-29 11:41:56 -0700267 versionStrings.push_back(String16("v"));
268 installers.push_back(String16(""));
David Chenbd125272018-04-04 19:02:50 -0700269 }
dwchen730403e2018-10-29 11:41:56 -0700270 m.updateMap(1, uids, versions, versionStrings, apps, installers);
David Chenbd125272018-04-04 19:02:50 -0700271
272 // First, verify that we have the expected number of items.
273 UidMapping results;
274 ProtoOutputStream proto;
dwchen730403e2018-10-29 11:41:56 -0700275 m.appendUidMap(3, config1, nullptr, true, true, &proto);
David Chenbd125272018-04-04 19:02:50 -0700276 protoOutputStreamToUidMapping(&proto, &results);
277 EXPECT_EQ(maxDeletedApps + 10, results.snapshots(0).package_info_size());
278
279 // Now remove all the apps.
dwchen730403e2018-10-29 11:41:56 -0700280 m.updateMap(1, uids, versions, versionStrings, apps, installers);
David Chenbd125272018-04-04 19:02:50 -0700281 for (int j = 0; j < maxDeletedApps + 10; j++) {
282 m.removeApp(4, String16(kApp1.c_str()), j);
283 }
284
285 proto.clear();
dwchen730403e2018-10-29 11:41:56 -0700286 m.appendUidMap(5, config1, nullptr, true, true, &proto);
David Chenbd125272018-04-04 19:02:50 -0700287 // Snapshot drops the first nine items.
288 protoOutputStreamToUidMapping(&proto, &results);
289 EXPECT_EQ(maxDeletedApps, results.snapshots(0).package_info_size());
290}
291
David Chend6896892017-10-25 11:49:03 -0700292TEST(UidMapTest, TestClearingOutput) {
293 UidMap m;
294
Yangster-mac94e197c2018-01-02 16:03:03 -0800295 ConfigKey config1(1, StringToId("config1"));
296 ConfigKey config2(1, StringToId("config2"));
David Chend6896892017-10-25 11:49:03 -0700297
298 m.OnConfigUpdated(config1);
299
300 vector<int32_t> uids;
Dianne Hackborn3accca02013-09-20 09:32:11 -0700301 vector<int64_t> versions;
dwchen730403e2018-10-29 11:41:56 -0700302 vector<String16> versionStrings;
303 vector<String16> installers;
David Chend6896892017-10-25 11:49:03 -0700304 vector<String16> apps;
305 uids.push_back(1000);
306 uids.push_back(1000);
307 apps.push_back(String16(kApp1.c_str()));
308 apps.push_back(String16(kApp2.c_str()));
309 versions.push_back(4);
310 versions.push_back(5);
dwchen730403e2018-10-29 11:41:56 -0700311 versionStrings.push_back(String16("v4"));
312 versionStrings.push_back(String16("v5"));
313 installers.push_back(String16(""));
314 installers.push_back(String16(""));
315 m.updateMap(1, uids, versions, versionStrings, apps, installers);
David Chend6896892017-10-25 11:49:03 -0700316
yro4beccbe2018-03-15 19:42:05 -0700317 ProtoOutputStream proto;
dwchen730403e2018-10-29 11:41:56 -0700318 m.appendUidMap(2, config1, nullptr, true, true, &proto);
David Chenf384b902018-03-14 18:36:45 -0700319 UidMapping results;
yro4beccbe2018-03-15 19:42:05 -0700320 protoOutputStreamToUidMapping(&proto, &results);
David Chend6896892017-10-25 11:49:03 -0700321 EXPECT_EQ(1, results.snapshots_size());
322
David Chen35045cb2018-03-23 22:21:47 -0700323 // We have to keep at least one snapshot in memory at all times.
yro4beccbe2018-03-15 19:42:05 -0700324 proto.clear();
dwchen730403e2018-10-29 11:41:56 -0700325 m.appendUidMap(2, config1, nullptr, true, true, &proto);
yro4beccbe2018-03-15 19:42:05 -0700326 protoOutputStreamToUidMapping(&proto, &results);
David Chencfc311d2018-01-23 17:55:54 -0800327 EXPECT_EQ(1, results.snapshots_size());
David Chend6896892017-10-25 11:49:03 -0700328
329 // Now add another configuration.
330 m.OnConfigUpdated(config2);
dwchen730403e2018-10-29 11:41:56 -0700331 m.updateApp(5, String16(kApp1.c_str()), 1000, 40, String16("v40"), String16(""));
David Chenf384b902018-03-14 18:36:45 -0700332 EXPECT_EQ(1U, m.mChanges.size());
yro4beccbe2018-03-15 19:42:05 -0700333 proto.clear();
dwchen730403e2018-10-29 11:41:56 -0700334 m.appendUidMap(6, config1, nullptr, true, true, &proto);
yro4beccbe2018-03-15 19:42:05 -0700335 protoOutputStreamToUidMapping(&proto, &results);
David Chencfc311d2018-01-23 17:55:54 -0800336 EXPECT_EQ(1, results.snapshots_size());
David Chend6896892017-10-25 11:49:03 -0700337 EXPECT_EQ(1, results.changes_size());
David Chenf384b902018-03-14 18:36:45 -0700338 EXPECT_EQ(1U, m.mChanges.size());
David Chend6896892017-10-25 11:49:03 -0700339
David Chenc136f452017-11-27 11:52:26 -0800340 // Add another delta update.
dwchen730403e2018-10-29 11:41:56 -0700341 m.updateApp(7, String16(kApp2.c_str()), 1001, 41, String16("v41"), String16(""));
David Chenf384b902018-03-14 18:36:45 -0700342 EXPECT_EQ(2U, m.mChanges.size());
David Chenc136f452017-11-27 11:52:26 -0800343
344 // We still can't remove anything.
yro4beccbe2018-03-15 19:42:05 -0700345 proto.clear();
dwchen730403e2018-10-29 11:41:56 -0700346 m.appendUidMap(8, config1, nullptr, true, true, &proto);
yro4beccbe2018-03-15 19:42:05 -0700347 protoOutputStreamToUidMapping(&proto, &results);
David Chencfc311d2018-01-23 17:55:54 -0800348 EXPECT_EQ(1, results.snapshots_size());
David Chenf384b902018-03-14 18:36:45 -0700349 EXPECT_EQ(1, results.changes_size());
350 EXPECT_EQ(2U, m.mChanges.size());
David Chend6896892017-10-25 11:49:03 -0700351
yro4beccbe2018-03-15 19:42:05 -0700352 proto.clear();
dwchen730403e2018-10-29 11:41:56 -0700353 m.appendUidMap(9, config2, nullptr, true, true, &proto);
yro4beccbe2018-03-15 19:42:05 -0700354 protoOutputStreamToUidMapping(&proto, &results);
David Chencfc311d2018-01-23 17:55:54 -0800355 EXPECT_EQ(1, results.snapshots_size());
David Chend6896892017-10-25 11:49:03 -0700356 EXPECT_EQ(2, results.changes_size());
357 // At this point both should be cleared.
David Chenf384b902018-03-14 18:36:45 -0700358 EXPECT_EQ(0U, m.mChanges.size());
David Chend6896892017-10-25 11:49:03 -0700359}
David Chenc136f452017-11-27 11:52:26 -0800360
361TEST(UidMapTest, TestMemoryComputed) {
362 UidMap m;
363
Yangster-mac94e197c2018-01-02 16:03:03 -0800364 ConfigKey config1(1, StringToId("config1"));
David Chenc136f452017-11-27 11:52:26 -0800365 m.OnConfigUpdated(config1);
366
367 size_t startBytes = m.mBytesUsed;
368 vector<int32_t> uids;
Dianne Hackborn3accca02013-09-20 09:32:11 -0700369 vector<int64_t> versions;
David Chenc136f452017-11-27 11:52:26 -0800370 vector<String16> apps;
dwchen730403e2018-10-29 11:41:56 -0700371 vector<String16> versionStrings;
372 vector<String16> installers;
David Chenc136f452017-11-27 11:52:26 -0800373 uids.push_back(1000);
374 apps.push_back(String16(kApp1.c_str()));
375 versions.push_back(1);
dwchen730403e2018-10-29 11:41:56 -0700376 versionStrings.push_back(String16("v1"));
377 installers.push_back(String16(""));
378 m.updateMap(1, uids, versions, versionStrings, apps, installers);
David Chenc136f452017-11-27 11:52:26 -0800379
dwchen730403e2018-10-29 11:41:56 -0700380 m.updateApp(3, String16(kApp1.c_str()), 1000, 40, String16("v40"), String16(""));
David Chenc136f452017-11-27 11:52:26 -0800381
yro4beccbe2018-03-15 19:42:05 -0700382 ProtoOutputStream proto;
David Chenf384b902018-03-14 18:36:45 -0700383 vector<uint8_t> bytes;
dwchen730403e2018-10-29 11:41:56 -0700384 m.appendUidMap(2, config1, nullptr, true, true, &proto);
David Chenc136f452017-11-27 11:52:26 -0800385 size_t prevBytes = m.mBytesUsed;
386
dwchen730403e2018-10-29 11:41:56 -0700387 m.appendUidMap(4, config1, nullptr, true, true, &proto);
David Chenc136f452017-11-27 11:52:26 -0800388 EXPECT_TRUE(m.mBytesUsed < prevBytes);
389}
390
391TEST(UidMapTest, TestMemoryGuardrail) {
392 UidMap m;
393 string buf;
394
Yangster-mac94e197c2018-01-02 16:03:03 -0800395 ConfigKey config1(1, StringToId("config1"));
David Chenc136f452017-11-27 11:52:26 -0800396 m.OnConfigUpdated(config1);
397
398 size_t startBytes = m.mBytesUsed;
399 vector<int32_t> uids;
Dianne Hackborn3accca02013-09-20 09:32:11 -0700400 vector<int64_t> versions;
dwchen730403e2018-10-29 11:41:56 -0700401 vector<String16> versionStrings;
402 vector<String16> installers;
David Chenc136f452017-11-27 11:52:26 -0800403 vector<String16> apps;
404 for (int i = 0; i < 100; i++) {
405 uids.push_back(1);
406 buf = "EXTREMELY_LONG_STRING_FOR_APP_TO_WASTE_MEMORY." + to_string(i);
407 apps.push_back(String16(buf.c_str()));
408 versions.push_back(1);
dwchen730403e2018-10-29 11:41:56 -0700409 versionStrings.push_back(String16("v1"));
410 installers.push_back(String16(""));
David Chenc136f452017-11-27 11:52:26 -0800411 }
dwchen730403e2018-10-29 11:41:56 -0700412 m.updateMap(1, uids, versions, versionStrings, apps, installers);
David Chenc136f452017-11-27 11:52:26 -0800413
dwchen730403e2018-10-29 11:41:56 -0700414 m.updateApp(3, String16("EXTREMELY_LONG_STRING_FOR_APP_TO_WASTE_MEMORY.0"), 1000, 2,
415 String16("v2"), String16(""));
David Chenf384b902018-03-14 18:36:45 -0700416 EXPECT_EQ(1U, m.mChanges.size());
David Chenc136f452017-11-27 11:52:26 -0800417
418 // Now force deletion by limiting the memory to hold one delta change.
dwchen730403e2018-10-29 11:41:56 -0700419 m.maxBytesOverride = 120; // Since the app string alone requires >45 characters.
420 m.updateApp(5, String16("EXTREMELY_LONG_STRING_FOR_APP_TO_WASTE_MEMORY.0"), 1000, 4,
421 String16("v4"), String16(""));
David Chenf384b902018-03-14 18:36:45 -0700422 EXPECT_EQ(1U, m.mChanges.size());
David Chenc136f452017-11-27 11:52:26 -0800423}
Yangster-mac9def8e32018-04-17 13:55:51 -0700424
David Chende701692017-10-05 13:16:02 -0700425#else
426GTEST_LOG_(INFO) << "This test does nothing.\n";
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700427#endif
David Chend6896892017-10-25 11:49:03 -0700428
429} // namespace statsd
430} // namespace os
yro4beccbe2018-03-15 19:42:05 -0700431} // namespace android