blob: 293e8ed1c44ce804a9ff59a2aaa43c87ec579153 [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"
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
tsaichristinea3d2ed82020-03-19 20:53:36 -070042TEST(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(
49 m, pullerManager, anomalyAlarmMonitor, subscriberAlarmMonitor, 0,
50 [](const ConfigKey& key) { return true; },
51 [](const int&, const vector<int64_t>&) { return true; });
52
53 std::unique_ptr<LogEvent> addEvent = CreateIsolatedUidChangedEvent(
54 1 /*timestamp*/, 100 /*hostUid*/, 101 /*isolatedUid*/, 1 /*is_create*/);
55 EXPECT_EQ(101, m->getHostUidOrSelf(101));
56 p.OnLogEvent(addEvent.get());
57 EXPECT_EQ(100, m->getHostUidOrSelf(101));
58
59 std::unique_ptr<LogEvent> removeEvent = CreateIsolatedUidChangedEvent(
60 1 /*timestamp*/, 100 /*hostUid*/, 101 /*isolatedUid*/, 0 /*is_create*/);
61 p.OnLogEvent(removeEvent.get());
62 EXPECT_EQ(101, m->getHostUidOrSelf(101));
63}
David Chen21582962017-11-01 17:32:46 -070064
David Chende701692017-10-05 13:16:02 -070065TEST(UidMapTest, TestMatching) {
66 UidMap m;
67 vector<int32_t> uids;
Dianne Hackborn3accca02013-09-20 09:32:11 -070068 vector<int64_t> versions;
David Chende701692017-10-05 13:16:02 -070069 vector<String16> apps;
dwchen730403e2018-10-29 11:41:56 -070070 vector<String16> versionStrings;
71 vector<String16> installers;
David Chende701692017-10-05 13:16:02 -070072
73 uids.push_back(1000);
74 uids.push_back(1000);
dwchen730403e2018-10-29 11:41:56 -070075 versionStrings.push_back(String16("v1"));
76 versionStrings.push_back(String16("v1"));
77 installers.push_back(String16(""));
78 installers.push_back(String16(""));
David Chende701692017-10-05 13:16:02 -070079 apps.push_back(String16(kApp1.c_str()));
80 apps.push_back(String16(kApp2.c_str()));
81 versions.push_back(4);
82 versions.push_back(5);
dwchen730403e2018-10-29 11:41:56 -070083 m.updateMap(1, uids, versions, versionStrings, apps, installers);
David Chende701692017-10-05 13:16:02 -070084 EXPECT_TRUE(m.hasApp(1000, kApp1));
85 EXPECT_TRUE(m.hasApp(1000, kApp2));
86 EXPECT_FALSE(m.hasApp(1000, "not.app"));
Yangster9df9a7f2017-12-18 13:33:05 -080087
88 std::set<string> name_set = m.getAppNamesFromUid(1000u, true /* returnNormalized */);
Muhammad Qureshidff78d62020-05-11 13:37:43 -070089 ASSERT_EQ(name_set.size(), 2u);
Yangster9df9a7f2017-12-18 13:33:05 -080090 EXPECT_TRUE(name_set.find(kApp1) != name_set.end());
91 EXPECT_TRUE(name_set.find(kApp2) != name_set.end());
92
93 name_set = m.getAppNamesFromUid(12345, true /* returnNormalized */);
94 EXPECT_TRUE(name_set.empty());
David Chende701692017-10-05 13:16:02 -070095}
96
97TEST(UidMapTest, TestAddAndRemove) {
98 UidMap m;
99 vector<int32_t> uids;
Dianne Hackborn3accca02013-09-20 09:32:11 -0700100 vector<int64_t> versions;
David Chende701692017-10-05 13:16:02 -0700101 vector<String16> apps;
dwchen730403e2018-10-29 11:41:56 -0700102 vector<String16> versionStrings;
103 vector<String16> installers;
David Chende701692017-10-05 13:16:02 -0700104
105 uids.push_back(1000);
106 uids.push_back(1000);
dwchen730403e2018-10-29 11:41:56 -0700107 versionStrings.push_back(String16("v1"));
108 versionStrings.push_back(String16("v1"));
109 installers.push_back(String16(""));
110 installers.push_back(String16(""));
David Chende701692017-10-05 13:16:02 -0700111 apps.push_back(String16(kApp1.c_str()));
112 apps.push_back(String16(kApp2.c_str()));
113 versions.push_back(4);
114 versions.push_back(5);
dwchen730403e2018-10-29 11:41:56 -0700115 m.updateMap(1, uids, versions, versionStrings, apps, installers);
David Chende701692017-10-05 13:16:02 -0700116
Yangster9df9a7f2017-12-18 13:33:05 -0800117 std::set<string> name_set = m.getAppNamesFromUid(1000, true /* returnNormalized */);
Muhammad Qureshidff78d62020-05-11 13:37:43 -0700118 ASSERT_EQ(name_set.size(), 2u);
Yangster9df9a7f2017-12-18 13:33:05 -0800119 EXPECT_TRUE(name_set.find(kApp1) != name_set.end());
120 EXPECT_TRUE(name_set.find(kApp2) != name_set.end());
121
122 // Update the app1 version.
dwchen730403e2018-10-29 11:41:56 -0700123 m.updateApp(2, String16(kApp1.c_str()), 1000, 40, String16("v40"), String16(""));
David Chende701692017-10-05 13:16:02 -0700124 EXPECT_EQ(40, m.getAppVersion(1000, kApp1));
125
Yangster9df9a7f2017-12-18 13:33:05 -0800126 name_set = m.getAppNamesFromUid(1000, true /* returnNormalized */);
Muhammad Qureshidff78d62020-05-11 13:37:43 -0700127 ASSERT_EQ(name_set.size(), 2u);
Yangster9df9a7f2017-12-18 13:33:05 -0800128 EXPECT_TRUE(name_set.find(kApp1) != name_set.end());
129 EXPECT_TRUE(name_set.find(kApp2) != name_set.end());
130
David Chenbd125272018-04-04 19:02:50 -0700131 m.removeApp(3, String16(kApp1.c_str()), 1000);
David Chende701692017-10-05 13:16:02 -0700132 EXPECT_FALSE(m.hasApp(1000, kApp1));
133 EXPECT_TRUE(m.hasApp(1000, kApp2));
Yangster9df9a7f2017-12-18 13:33:05 -0800134 name_set = m.getAppNamesFromUid(1000, true /* returnNormalized */);
Muhammad Qureshidff78d62020-05-11 13:37:43 -0700135 ASSERT_EQ(name_set.size(), 1u);
Yangster9df9a7f2017-12-18 13:33:05 -0800136 EXPECT_TRUE(name_set.find(kApp1) == name_set.end());
137 EXPECT_TRUE(name_set.find(kApp2) != name_set.end());
138
139 // Remove app2.
David Chenbd125272018-04-04 19:02:50 -0700140 m.removeApp(4, String16(kApp2.c_str()), 1000);
Yangster9df9a7f2017-12-18 13:33:05 -0800141 EXPECT_FALSE(m.hasApp(1000, kApp1));
142 EXPECT_FALSE(m.hasApp(1000, kApp2));
143 name_set = m.getAppNamesFromUid(1000, true /* returnNormalized */);
144 EXPECT_TRUE(name_set.empty());
145}
146
147TEST(UidMapTest, TestUpdateApp) {
148 UidMap m;
dwchen730403e2018-10-29 11:41:56 -0700149 m.updateMap(1, {1000, 1000}, {4, 5}, {String16("v4"), String16("v5")},
150 {String16(kApp1.c_str()), String16(kApp2.c_str())}, {String16(""), String16("")});
Yangster9df9a7f2017-12-18 13:33:05 -0800151 std::set<string> name_set = m.getAppNamesFromUid(1000, true /* returnNormalized */);
Muhammad Qureshidff78d62020-05-11 13:37:43 -0700152 ASSERT_EQ(name_set.size(), 2u);
Yangster9df9a7f2017-12-18 13:33:05 -0800153 EXPECT_TRUE(name_set.find(kApp1) != name_set.end());
154 EXPECT_TRUE(name_set.find(kApp2) != name_set.end());
155
156 // Adds a new name for uid 1000.
dwchen730403e2018-10-29 11:41:56 -0700157 m.updateApp(2, String16("NeW_aPP1_NAmE"), 1000, 40, String16("v40"), String16(""));
Yangster9df9a7f2017-12-18 13:33:05 -0800158 name_set = m.getAppNamesFromUid(1000, true /* returnNormalized */);
Muhammad Qureshidff78d62020-05-11 13:37:43 -0700159 ASSERT_EQ(name_set.size(), 3u);
Yangster9df9a7f2017-12-18 13:33:05 -0800160 EXPECT_TRUE(name_set.find(kApp1) != name_set.end());
161 EXPECT_TRUE(name_set.find(kApp2) != name_set.end());
162 EXPECT_TRUE(name_set.find("NeW_aPP1_NAmE") == name_set.end());
163 EXPECT_TRUE(name_set.find("new_app1_name") != name_set.end());
164
165 // This name is also reused by another uid 2000.
dwchen730403e2018-10-29 11:41:56 -0700166 m.updateApp(3, String16("NeW_aPP1_NAmE"), 2000, 1, String16("v1"), String16(""));
Yangster9df9a7f2017-12-18 13:33:05 -0800167 name_set = m.getAppNamesFromUid(2000, true /* returnNormalized */);
Muhammad Qureshidff78d62020-05-11 13:37:43 -0700168 ASSERT_EQ(name_set.size(), 1u);
Yangster9df9a7f2017-12-18 13:33:05 -0800169 EXPECT_TRUE(name_set.find("NeW_aPP1_NAmE") == name_set.end());
170 EXPECT_TRUE(name_set.find("new_app1_name") != name_set.end());
David Chende701692017-10-05 13:16:02 -0700171}
David Chend6896892017-10-25 11:49:03 -0700172
yro4beccbe2018-03-15 19:42:05 -0700173static void protoOutputStreamToUidMapping(ProtoOutputStream* proto, UidMapping* results) {
174 vector<uint8_t> bytes;
175 bytes.resize(proto->size());
176 size_t pos = 0;
Joe Onorato99598ee2019-02-11 15:55:13 +0000177 sp<ProtoReader> reader = proto->data();
178 while (reader->readBuffer() != NULL) {
179 size_t toRead = reader->currentToRead();
180 std::memcpy(&((bytes)[pos]), reader->readBuffer(), toRead);
yro4beccbe2018-03-15 19:42:05 -0700181 pos += toRead;
Joe Onorato99598ee2019-02-11 15:55:13 +0000182 reader->move(toRead);
yro4beccbe2018-03-15 19:42:05 -0700183 }
184 results->ParseFromArray(bytes.data(), bytes.size());
185}
186
David Chen35045cb2018-03-23 22:21:47 -0700187// Test that uid map returns at least one snapshot even if we already obtained
188// this snapshot from a previous call to getData.
189TEST(UidMapTest, TestOutputIncludesAtLeastOneSnapshot) {
190 UidMap m;
191 // Initialize single config key.
192 ConfigKey config1(1, StringToId("config1"));
193 m.OnConfigUpdated(config1);
194 vector<int32_t> uids;
195 vector<int64_t> versions;
196 vector<String16> apps;
dwchen730403e2018-10-29 11:41:56 -0700197 vector<String16> versionStrings;
198 vector<String16> installers;
David Chen35045cb2018-03-23 22:21:47 -0700199 uids.push_back(1000);
200 apps.push_back(String16(kApp2.c_str()));
dwchen730403e2018-10-29 11:41:56 -0700201 versionStrings.push_back(String16("v1"));
202 installers.push_back(String16(""));
David Chen35045cb2018-03-23 22:21:47 -0700203 versions.push_back(5);
dwchen730403e2018-10-29 11:41:56 -0700204 m.updateMap(1, uids, versions, versionStrings, apps, installers);
David Chen35045cb2018-03-23 22:21:47 -0700205
206 // Set the last timestamp for this config key to be newer.
207 m.mLastUpdatePerConfigKey[config1] = 2;
208
209 ProtoOutputStream proto;
dwchen730403e2018-10-29 11:41:56 -0700210 m.appendUidMap(3, config1, nullptr, true, true, &proto);
David Chen35045cb2018-03-23 22:21:47 -0700211
212 // Check there's still a uidmap attached this one.
213 UidMapping results;
214 protoOutputStreamToUidMapping(&proto, &results);
Muhammad Qureshidff78d62020-05-11 13:37:43 -0700215 ASSERT_EQ(1, results.snapshots_size());
dwchen730403e2018-10-29 11:41:56 -0700216 EXPECT_EQ("v1", results.snapshots(0).package_info(0).version_string());
David Chen35045cb2018-03-23 22:21:47 -0700217}
218
David Chenbd125272018-04-04 19:02:50 -0700219TEST(UidMapTest, TestRemovedAppRetained) {
220 UidMap m;
221 // Initialize single config key.
222 ConfigKey config1(1, StringToId("config1"));
223 m.OnConfigUpdated(config1);
224 vector<int32_t> uids;
225 vector<int64_t> versions;
dwchen730403e2018-10-29 11:41:56 -0700226 vector<String16> versionStrings;
227 vector<String16> installers;
David Chenbd125272018-04-04 19:02:50 -0700228 vector<String16> apps;
229 uids.push_back(1000);
230 apps.push_back(String16(kApp2.c_str()));
231 versions.push_back(5);
dwchen730403e2018-10-29 11:41:56 -0700232 versionStrings.push_back(String16("v5"));
233 installers.push_back(String16(""));
234 m.updateMap(1, uids, versions, versionStrings, apps, installers);
David Chenbd125272018-04-04 19:02:50 -0700235 m.removeApp(2, String16(kApp2.c_str()), 1000);
236
237 ProtoOutputStream proto;
dwchen730403e2018-10-29 11:41:56 -0700238 m.appendUidMap(3, config1, nullptr, true, true, &proto);
David Chenbd125272018-04-04 19:02:50 -0700239
240 // Snapshot should still contain this item as deleted.
241 UidMapping results;
242 protoOutputStreamToUidMapping(&proto, &results);
Muhammad Qureshidff78d62020-05-11 13:37:43 -0700243 ASSERT_EQ(1, results.snapshots(0).package_info_size());
David Chenbd125272018-04-04 19:02:50 -0700244 EXPECT_EQ(true, results.snapshots(0).package_info(0).deleted());
245}
246
247TEST(UidMapTest, TestRemovedAppOverGuardrail) {
248 UidMap m;
249 // Initialize single config key.
250 ConfigKey config1(1, StringToId("config1"));
251 m.OnConfigUpdated(config1);
252 vector<int32_t> uids;
253 vector<int64_t> versions;
dwchen730403e2018-10-29 11:41:56 -0700254 vector<String16> versionStrings;
255 vector<String16> installers;
David Chenbd125272018-04-04 19:02:50 -0700256 vector<String16> apps;
257 const int maxDeletedApps = StatsdStats::kMaxDeletedAppsInUidMap;
258 for (int j = 0; j < maxDeletedApps + 10; j++) {
259 uids.push_back(j);
260 apps.push_back(String16(kApp1.c_str()));
261 versions.push_back(j);
dwchen730403e2018-10-29 11:41:56 -0700262 versionStrings.push_back(String16("v"));
263 installers.push_back(String16(""));
David Chenbd125272018-04-04 19:02:50 -0700264 }
dwchen730403e2018-10-29 11:41:56 -0700265 m.updateMap(1, uids, versions, versionStrings, apps, installers);
David Chenbd125272018-04-04 19:02:50 -0700266
267 // First, verify that we have the expected number of items.
268 UidMapping results;
269 ProtoOutputStream proto;
dwchen730403e2018-10-29 11:41:56 -0700270 m.appendUidMap(3, config1, nullptr, true, true, &proto);
David Chenbd125272018-04-04 19:02:50 -0700271 protoOutputStreamToUidMapping(&proto, &results);
Muhammad Qureshidff78d62020-05-11 13:37:43 -0700272 ASSERT_EQ(maxDeletedApps + 10, results.snapshots(0).package_info_size());
David Chenbd125272018-04-04 19:02:50 -0700273
274 // Now remove all the apps.
dwchen730403e2018-10-29 11:41:56 -0700275 m.updateMap(1, uids, versions, versionStrings, apps, installers);
David Chenbd125272018-04-04 19:02:50 -0700276 for (int j = 0; j < maxDeletedApps + 10; j++) {
277 m.removeApp(4, String16(kApp1.c_str()), j);
278 }
279
280 proto.clear();
dwchen730403e2018-10-29 11:41:56 -0700281 m.appendUidMap(5, config1, nullptr, true, true, &proto);
David Chenbd125272018-04-04 19:02:50 -0700282 // Snapshot drops the first nine items.
283 protoOutputStreamToUidMapping(&proto, &results);
Muhammad Qureshidff78d62020-05-11 13:37:43 -0700284 ASSERT_EQ(maxDeletedApps, results.snapshots(0).package_info_size());
David Chenbd125272018-04-04 19:02:50 -0700285}
286
David Chend6896892017-10-25 11:49:03 -0700287TEST(UidMapTest, TestClearingOutput) {
288 UidMap m;
289
Yangster-mac94e197c2018-01-02 16:03:03 -0800290 ConfigKey config1(1, StringToId("config1"));
291 ConfigKey config2(1, StringToId("config2"));
David Chend6896892017-10-25 11:49:03 -0700292
293 m.OnConfigUpdated(config1);
294
295 vector<int32_t> uids;
Dianne Hackborn3accca02013-09-20 09:32:11 -0700296 vector<int64_t> versions;
dwchen730403e2018-10-29 11:41:56 -0700297 vector<String16> versionStrings;
298 vector<String16> installers;
David Chend6896892017-10-25 11:49:03 -0700299 vector<String16> apps;
300 uids.push_back(1000);
301 uids.push_back(1000);
302 apps.push_back(String16(kApp1.c_str()));
303 apps.push_back(String16(kApp2.c_str()));
304 versions.push_back(4);
305 versions.push_back(5);
dwchen730403e2018-10-29 11:41:56 -0700306 versionStrings.push_back(String16("v4"));
307 versionStrings.push_back(String16("v5"));
308 installers.push_back(String16(""));
309 installers.push_back(String16(""));
310 m.updateMap(1, uids, versions, versionStrings, apps, installers);
David Chend6896892017-10-25 11:49:03 -0700311
yro4beccbe2018-03-15 19:42:05 -0700312 ProtoOutputStream proto;
dwchen730403e2018-10-29 11:41:56 -0700313 m.appendUidMap(2, config1, nullptr, true, true, &proto);
David Chenf384b902018-03-14 18:36:45 -0700314 UidMapping results;
yro4beccbe2018-03-15 19:42:05 -0700315 protoOutputStreamToUidMapping(&proto, &results);
Muhammad Qureshidff78d62020-05-11 13:37:43 -0700316 ASSERT_EQ(1, results.snapshots_size());
David Chend6896892017-10-25 11:49:03 -0700317
David Chen35045cb2018-03-23 22:21:47 -0700318 // We have to keep at least one snapshot in memory at all times.
yro4beccbe2018-03-15 19:42:05 -0700319 proto.clear();
dwchen730403e2018-10-29 11:41:56 -0700320 m.appendUidMap(2, config1, nullptr, true, true, &proto);
yro4beccbe2018-03-15 19:42:05 -0700321 protoOutputStreamToUidMapping(&proto, &results);
Muhammad Qureshidff78d62020-05-11 13:37:43 -0700322 ASSERT_EQ(1, results.snapshots_size());
David Chend6896892017-10-25 11:49:03 -0700323
324 // Now add another configuration.
325 m.OnConfigUpdated(config2);
dwchen730403e2018-10-29 11:41:56 -0700326 m.updateApp(5, String16(kApp1.c_str()), 1000, 40, String16("v40"), String16(""));
Muhammad Qureshidff78d62020-05-11 13:37:43 -0700327 ASSERT_EQ(1U, m.mChanges.size());
yro4beccbe2018-03-15 19:42:05 -0700328 proto.clear();
dwchen730403e2018-10-29 11:41:56 -0700329 m.appendUidMap(6, config1, nullptr, true, true, &proto);
yro4beccbe2018-03-15 19:42:05 -0700330 protoOutputStreamToUidMapping(&proto, &results);
Muhammad Qureshidff78d62020-05-11 13:37:43 -0700331 ASSERT_EQ(1, results.snapshots_size());
332 ASSERT_EQ(1, results.changes_size());
333 ASSERT_EQ(1U, m.mChanges.size());
David Chend6896892017-10-25 11:49:03 -0700334
David Chenc136f452017-11-27 11:52:26 -0800335 // Add another delta update.
dwchen730403e2018-10-29 11:41:56 -0700336 m.updateApp(7, String16(kApp2.c_str()), 1001, 41, String16("v41"), String16(""));
Muhammad Qureshidff78d62020-05-11 13:37:43 -0700337 ASSERT_EQ(2U, m.mChanges.size());
David Chenc136f452017-11-27 11:52:26 -0800338
339 // We still can't remove anything.
yro4beccbe2018-03-15 19:42:05 -0700340 proto.clear();
dwchen730403e2018-10-29 11:41:56 -0700341 m.appendUidMap(8, config1, nullptr, true, true, &proto);
yro4beccbe2018-03-15 19:42:05 -0700342 protoOutputStreamToUidMapping(&proto, &results);
Muhammad Qureshidff78d62020-05-11 13:37:43 -0700343 ASSERT_EQ(1, results.snapshots_size());
344 ASSERT_EQ(1, results.changes_size());
345 ASSERT_EQ(2U, m.mChanges.size());
David Chend6896892017-10-25 11:49:03 -0700346
yro4beccbe2018-03-15 19:42:05 -0700347 proto.clear();
dwchen730403e2018-10-29 11:41:56 -0700348 m.appendUidMap(9, config2, nullptr, true, true, &proto);
yro4beccbe2018-03-15 19:42:05 -0700349 protoOutputStreamToUidMapping(&proto, &results);
Muhammad Qureshidff78d62020-05-11 13:37:43 -0700350 ASSERT_EQ(1, results.snapshots_size());
351 ASSERT_EQ(2, results.changes_size());
David Chend6896892017-10-25 11:49:03 -0700352 // At this point both should be cleared.
Muhammad Qureshidff78d62020-05-11 13:37:43 -0700353 ASSERT_EQ(0U, m.mChanges.size());
David Chend6896892017-10-25 11:49:03 -0700354}
David Chenc136f452017-11-27 11:52:26 -0800355
356TEST(UidMapTest, TestMemoryComputed) {
357 UidMap m;
358
Yangster-mac94e197c2018-01-02 16:03:03 -0800359 ConfigKey config1(1, StringToId("config1"));
David Chenc136f452017-11-27 11:52:26 -0800360 m.OnConfigUpdated(config1);
361
362 size_t startBytes = m.mBytesUsed;
363 vector<int32_t> uids;
Dianne Hackborn3accca02013-09-20 09:32:11 -0700364 vector<int64_t> versions;
David Chenc136f452017-11-27 11:52:26 -0800365 vector<String16> apps;
dwchen730403e2018-10-29 11:41:56 -0700366 vector<String16> versionStrings;
367 vector<String16> installers;
David Chenc136f452017-11-27 11:52:26 -0800368 uids.push_back(1000);
369 apps.push_back(String16(kApp1.c_str()));
370 versions.push_back(1);
dwchen730403e2018-10-29 11:41:56 -0700371 versionStrings.push_back(String16("v1"));
372 installers.push_back(String16(""));
373 m.updateMap(1, uids, versions, versionStrings, apps, installers);
David Chenc136f452017-11-27 11:52:26 -0800374
dwchen730403e2018-10-29 11:41:56 -0700375 m.updateApp(3, String16(kApp1.c_str()), 1000, 40, String16("v40"), String16(""));
David Chenc136f452017-11-27 11:52:26 -0800376
yro4beccbe2018-03-15 19:42:05 -0700377 ProtoOutputStream proto;
David Chenf384b902018-03-14 18:36:45 -0700378 vector<uint8_t> bytes;
dwchen730403e2018-10-29 11:41:56 -0700379 m.appendUidMap(2, config1, nullptr, true, true, &proto);
David Chenc136f452017-11-27 11:52:26 -0800380 size_t prevBytes = m.mBytesUsed;
381
dwchen730403e2018-10-29 11:41:56 -0700382 m.appendUidMap(4, config1, nullptr, true, true, &proto);
David Chenc136f452017-11-27 11:52:26 -0800383 EXPECT_TRUE(m.mBytesUsed < prevBytes);
384}
385
386TEST(UidMapTest, TestMemoryGuardrail) {
387 UidMap m;
388 string buf;
389
Yangster-mac94e197c2018-01-02 16:03:03 -0800390 ConfigKey config1(1, StringToId("config1"));
David Chenc136f452017-11-27 11:52:26 -0800391 m.OnConfigUpdated(config1);
392
393 size_t startBytes = m.mBytesUsed;
394 vector<int32_t> uids;
Dianne Hackborn3accca02013-09-20 09:32:11 -0700395 vector<int64_t> versions;
dwchen730403e2018-10-29 11:41:56 -0700396 vector<String16> versionStrings;
397 vector<String16> installers;
David Chenc136f452017-11-27 11:52:26 -0800398 vector<String16> apps;
399 for (int i = 0; i < 100; i++) {
400 uids.push_back(1);
401 buf = "EXTREMELY_LONG_STRING_FOR_APP_TO_WASTE_MEMORY." + to_string(i);
402 apps.push_back(String16(buf.c_str()));
403 versions.push_back(1);
dwchen730403e2018-10-29 11:41:56 -0700404 versionStrings.push_back(String16("v1"));
405 installers.push_back(String16(""));
David Chenc136f452017-11-27 11:52:26 -0800406 }
dwchen730403e2018-10-29 11:41:56 -0700407 m.updateMap(1, uids, versions, versionStrings, apps, installers);
David Chenc136f452017-11-27 11:52:26 -0800408
dwchen730403e2018-10-29 11:41:56 -0700409 m.updateApp(3, String16("EXTREMELY_LONG_STRING_FOR_APP_TO_WASTE_MEMORY.0"), 1000, 2,
410 String16("v2"), String16(""));
Muhammad Qureshidff78d62020-05-11 13:37:43 -0700411 ASSERT_EQ(1U, m.mChanges.size());
David Chenc136f452017-11-27 11:52:26 -0800412
413 // Now force deletion by limiting the memory to hold one delta change.
dwchen730403e2018-10-29 11:41:56 -0700414 m.maxBytesOverride = 120; // Since the app string alone requires >45 characters.
415 m.updateApp(5, String16("EXTREMELY_LONG_STRING_FOR_APP_TO_WASTE_MEMORY.0"), 1000, 4,
416 String16("v4"), String16(""));
Muhammad Qureshidff78d62020-05-11 13:37:43 -0700417 ASSERT_EQ(1U, m.mChanges.size());
David Chenc136f452017-11-27 11:52:26 -0800418}
Yangster-mac9def8e32018-04-17 13:55:51 -0700419
David Chende701692017-10-05 13:16:02 -0700420#else
421GTEST_LOG_(INFO) << "This test does nothing.\n";
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700422#endif
David Chend6896892017-10-25 11:49:03 -0700423
424} // namespace statsd
425} // namespace os
yro4beccbe2018-03-15 19:42:05 -0700426} // namespace android