1 // Copyright (C) 2017 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include <gmock/gmock.h>
16 #include <gtest/gtest.h>
17 #include <private/android_filesystem_config.h>
18 #include <stdio.h>
19
20 #include <set>
21 #include <unordered_map>
22 #include <vector>
23
24 #include "metrics/metrics_test_helper.h"
25 #include "src/condition/ConditionTracker.h"
26 #include "src/matchers/AtomMatchingTracker.h"
27 #include "src/metrics/CountMetricProducer.h"
28 #include "src/metrics/GaugeMetricProducer.h"
29 #include "src/metrics/MetricProducer.h"
30 #include "src/metrics/NumericValueMetricProducer.h"
31 #include "src/metrics/parsing_utils/metrics_manager_util.h"
32 #include "src/state/StateManager.h"
33 #include "src/statsd_config.pb.h"
34 #include "statsd_test_util.h"
35 #include "statslog_statsdtest.h"
36
37 using namespace testing;
38 using android::sp;
39 using android::os::statsd::Predicate;
40 using std::map;
41 using std::set;
42 using std::unordered_map;
43 using std::vector;
44
45 #ifdef __ANDROID__
46
47 namespace android {
48 namespace os {
49 namespace statsd {
50
51 namespace {
52 const int kConfigId = 12345;
53 const ConfigKey kConfigKey(0, kConfigId);
54
55 const long timeBaseSec = 1000;
56
buildEventConfig(bool isRestricted)57 StatsdConfig buildEventConfig(bool isRestricted) {
58 StatsdConfig config;
59 config.set_id(kConfigId);
60 if (isRestricted) {
61 config.set_restricted_metrics_delegate_package_name("delegate");
62 }
63
64 AtomMatcher* eventMatcher = config.add_atom_matcher();
65 eventMatcher->set_id(StringToId("SCREEN_IS_ON"));
66 SimpleAtomMatcher* simpleAtomMatcher = eventMatcher->mutable_simple_atom_matcher();
67 simpleAtomMatcher->set_atom_id(2 /*SCREEN_STATE_CHANGE*/);
68
69 EventMetric* metric = config.add_event_metric();
70 metric->set_id(3);
71 metric->set_what(StringToId("SCREEN_IS_ON"));
72 return config;
73 }
74
buildGoodRestrictedConfig()75 StatsdConfig buildGoodRestrictedConfig() {
76 return buildEventConfig(/*isRestricted*/ true);
77 }
78
buildGoodEventConfig()79 StatsdConfig buildGoodEventConfig() {
80 return buildEventConfig(/*isRestricted*/ false);
81 }
82
unionSet(const vector<set<int32_t>> sets)83 set<int32_t> unionSet(const vector<set<int32_t>> sets) {
84 set<int32_t> toRet;
85 for (const set<int32_t>& s : sets) {
86 toRet.insert(s.begin(), s.end());
87 }
88 return toRet;
89 }
90
91 } // anonymous namespace
92
TEST(MetricsManagerTest,TestLogSources)93 TEST(MetricsManagerTest, TestLogSources) {
94 string app1 = "app1";
95 set<int32_t> app1Uids = {1111, 11111};
96 string app2 = "app2";
97 set<int32_t> app2Uids = {2222};
98 string app3 = "app3";
99 set<int32_t> app3Uids = {3333, 1111};
100
101 map<string, set<int32_t>> pkgToUids;
102 pkgToUids[app1] = app1Uids;
103 pkgToUids[app2] = app2Uids;
104 pkgToUids[app3] = app3Uids;
105
106 int32_t atom1 = 10, atom2 = 20, atom3 = 30;
107 sp<MockUidMap> uidMap = new StrictMock<MockUidMap>();
108 EXPECT_CALL(*uidMap, getAppUid(_))
109 .Times(4)
110 .WillRepeatedly(Invoke([&pkgToUids](const string& pkg) {
111 const auto& it = pkgToUids.find(pkg);
112 if (it != pkgToUids.end()) {
113 return it->second;
114 }
115 return set<int32_t>();
116 }));
117 sp<MockStatsPullerManager> pullerManager = new StrictMock<MockStatsPullerManager>();
118 EXPECT_CALL(*pullerManager, RegisterPullUidProvider(kConfigKey, _)).Times(1);
119 EXPECT_CALL(*pullerManager, UnregisterPullUidProvider(kConfigKey, _)).Times(1);
120
121 sp<AlarmMonitor> anomalyAlarmMonitor;
122 sp<AlarmMonitor> periodicAlarmMonitor;
123
124 StatsdConfig config;
125 config.add_allowed_log_source("AID_SYSTEM");
126 config.add_allowed_log_source(app1);
127 config.add_default_pull_packages("AID_SYSTEM");
128 config.add_default_pull_packages("AID_ROOT");
129
130 const set<int32_t> defaultPullUids = {AID_SYSTEM, AID_ROOT};
131
132 PullAtomPackages* pullAtomPackages = config.add_pull_atom_packages();
133 pullAtomPackages->set_atom_id(atom1);
134 pullAtomPackages->add_packages(app1);
135 pullAtomPackages->add_packages(app3);
136
137 pullAtomPackages = config.add_pull_atom_packages();
138 pullAtomPackages->set_atom_id(atom2);
139 pullAtomPackages->add_packages(app2);
140 pullAtomPackages->add_packages("AID_STATSD");
141
142 MetricsManager metricsManager(kConfigKey, config, timeBaseSec, timeBaseSec, uidMap,
143 pullerManager, anomalyAlarmMonitor, periodicAlarmMonitor);
144 EXPECT_TRUE(metricsManager.isConfigValid());
145
146 EXPECT_THAT(metricsManager.mAllowedUid, ElementsAre(AID_SYSTEM));
147 EXPECT_THAT(metricsManager.mAllowedPkg, ElementsAre(app1));
148 EXPECT_THAT(metricsManager.mAllowedLogSources,
149 ContainerEq(unionSet(vector<set<int32_t>>({app1Uids, {AID_SYSTEM}}))));
150 EXPECT_THAT(metricsManager.mDefaultPullUids, ContainerEq(defaultPullUids));
151
152 vector<int32_t> atom1Uids = metricsManager.getPullAtomUids(atom1);
153 EXPECT_THAT(atom1Uids,
154 UnorderedElementsAreArray(unionSet({defaultPullUids, app1Uids, app3Uids})));
155
156 vector<int32_t> atom2Uids = metricsManager.getPullAtomUids(atom2);
157 EXPECT_THAT(atom2Uids,
158 UnorderedElementsAreArray(unionSet({defaultPullUids, app2Uids, {AID_STATSD}})));
159
160 vector<int32_t> atom3Uids = metricsManager.getPullAtomUids(atom3);
161 EXPECT_THAT(atom3Uids, UnorderedElementsAreArray(defaultPullUids));
162 }
163
TEST(MetricsManagerTest,TestLogSourcesOnConfigUpdate)164 TEST(MetricsManagerTest, TestLogSourcesOnConfigUpdate) {
165 string app1 = "app1";
166 set<int32_t> app1Uids = {11110, 11111};
167 string app2 = "app2";
168 set<int32_t> app2Uids = {22220};
169 string app3 = "app3";
170 set<int32_t> app3Uids = {33330, 11110};
171
172 map<string, set<int32_t>> pkgToUids;
173 pkgToUids[app1] = app1Uids;
174 pkgToUids[app2] = app2Uids;
175 pkgToUids[app3] = app3Uids;
176
177 int32_t atom1 = 10, atom2 = 20, atom3 = 30;
178 sp<MockUidMap> uidMap = new StrictMock<MockUidMap>();
179 EXPECT_CALL(*uidMap, getAppUid(_))
180 .Times(8)
181 .WillRepeatedly(Invoke([&pkgToUids](const string& pkg) {
182 const auto& it = pkgToUids.find(pkg);
183 if (it != pkgToUids.end()) {
184 return it->second;
185 }
186 return set<int32_t>();
187 }));
188 sp<MockStatsPullerManager> pullerManager = new StrictMock<MockStatsPullerManager>();
189 EXPECT_CALL(*pullerManager, RegisterPullUidProvider(kConfigKey, _)).Times(1);
190 EXPECT_CALL(*pullerManager, UnregisterPullUidProvider(kConfigKey, _)).Times(1);
191
192 sp<AlarmMonitor> anomalyAlarmMonitor;
193 sp<AlarmMonitor> periodicAlarmMonitor;
194
195 StatsdConfig config;
196 config.add_allowed_log_source(app1);
197 config.add_default_pull_packages("AID_SYSTEM");
198 config.add_default_pull_packages("AID_ROOT");
199
200 PullAtomPackages* pullAtomPackages = config.add_pull_atom_packages();
201 pullAtomPackages->set_atom_id(atom1);
202 pullAtomPackages->add_packages(app1);
203 pullAtomPackages->add_packages(app3);
204
205 pullAtomPackages = config.add_pull_atom_packages();
206 pullAtomPackages->set_atom_id(atom2);
207 pullAtomPackages->add_packages(app2);
208 pullAtomPackages->add_packages("AID_STATSD");
209
210 MetricsManager metricsManager(kConfigKey, config, timeBaseSec, timeBaseSec, uidMap,
211 pullerManager, anomalyAlarmMonitor, periodicAlarmMonitor);
212 EXPECT_TRUE(metricsManager.isConfigValid());
213
214 // Update with new allowed log sources.
215 StatsdConfig newConfig;
216 newConfig.add_allowed_log_source(app2);
217 newConfig.add_default_pull_packages("AID_SYSTEM");
218 newConfig.add_default_pull_packages("AID_STATSD");
219
220 pullAtomPackages = newConfig.add_pull_atom_packages();
221 pullAtomPackages->set_atom_id(atom2);
222 pullAtomPackages->add_packages(app1);
223 pullAtomPackages->add_packages(app3);
224
225 pullAtomPackages = newConfig.add_pull_atom_packages();
226 pullAtomPackages->set_atom_id(atom3);
227 pullAtomPackages->add_packages(app2);
228 pullAtomPackages->add_packages("AID_ADB");
229
230 metricsManager.updateConfig(newConfig, timeBaseSec, timeBaseSec, anomalyAlarmMonitor,
231 periodicAlarmMonitor);
232 EXPECT_TRUE(metricsManager.isConfigValid());
233
234 EXPECT_THAT(metricsManager.mAllowedPkg, ElementsAre(app2));
235 EXPECT_THAT(metricsManager.mAllowedLogSources,
236 ContainerEq(unionSet(vector<set<int32_t>>({app2Uids}))));
237 const set<int32_t> defaultPullUids = {AID_SYSTEM, AID_STATSD};
238 EXPECT_THAT(metricsManager.mDefaultPullUids, ContainerEq(defaultPullUids));
239
240 vector<int32_t> atom1Uids = metricsManager.getPullAtomUids(atom1);
241 EXPECT_THAT(atom1Uids, UnorderedElementsAreArray(defaultPullUids));
242
243 vector<int32_t> atom2Uids = metricsManager.getPullAtomUids(atom2);
244 EXPECT_THAT(atom2Uids,
245 UnorderedElementsAreArray(unionSet({defaultPullUids, app1Uids, app3Uids})));
246
247 vector<int32_t> atom3Uids = metricsManager.getPullAtomUids(atom3);
248 EXPECT_THAT(atom3Uids,
249 UnorderedElementsAreArray(unionSet({defaultPullUids, app2Uids, {AID_ADB}})));
250 }
251
252 struct MetricsManagerServerFlagParam {
253 string flagValue;
254 string label;
255 };
256
257 class MetricsManagerTest_SPlus
258 : public ::testing::Test,
259 public ::testing::WithParamInterface<MetricsManagerServerFlagParam> {
260 protected:
SetUp()261 void SetUp() override {
262 if (shouldSkipTest()) {
263 GTEST_SKIP() << skipReason();
264 }
265 }
266
shouldSkipTest() const267 bool shouldSkipTest() const {
268 return !isAtLeastS();
269 }
270
skipReason() const271 string skipReason() const {
272 return "Skipping MetricsManagerTest_SPlus because device is not S+";
273 }
274
275 std::optional<string> originalFlagValue;
276 };
277
278 INSTANTIATE_TEST_SUITE_P(
279 MetricsManagerTest_SPlus, MetricsManagerTest_SPlus,
280 testing::ValuesIn<MetricsManagerServerFlagParam>({
281 // Server flag values
282 {FLAG_TRUE, "ServerFlagTrue"},
283 {FLAG_FALSE, "ServerFlagFalse"},
284 }),
__anon6e19653d0402(const testing::TestParamInfo<MetricsManagerTest_SPlus::ParamType>& info) 285 [](const testing::TestParamInfo<MetricsManagerTest_SPlus::ParamType>& info) {
286 return info.param.label;
287 });
288
TEST(MetricsManagerTest,TestCheckLogCredentialsWhitelistedAtom)289 TEST(MetricsManagerTest, TestCheckLogCredentialsWhitelistedAtom) {
290 sp<UidMap> uidMap;
291 sp<StatsPullerManager> pullerManager = new StatsPullerManager();
292 sp<AlarmMonitor> anomalyAlarmMonitor;
293 sp<AlarmMonitor> periodicAlarmMonitor;
294
295 StatsdConfig config;
296 config.add_whitelisted_atom_ids(3);
297 config.add_whitelisted_atom_ids(4);
298
299 MetricsManager metricsManager(kConfigKey, config, timeBaseSec, timeBaseSec, uidMap,
300 pullerManager, anomalyAlarmMonitor, periodicAlarmMonitor);
301
302 const int32_t customAppUid = AID_APP_START + 1;
303 LogEvent event(customAppUid /* uid */, 0 /* pid */);
304 CreateNoValuesLogEvent(&event, 10 /* atom id */, 0 /* timestamp */);
305 EXPECT_FALSE(metricsManager.checkLogCredentials(event));
306
307 CreateNoValuesLogEvent(&event, 3 /* atom id */, 0 /* timestamp */);
308 EXPECT_TRUE(metricsManager.checkLogCredentials(event));
309
310 CreateNoValuesLogEvent(&event, 4 /* atom id */, 0 /* timestamp */);
311 EXPECT_TRUE(metricsManager.checkLogCredentials(event));
312 }
313
TEST(MetricsManagerTest,TestWhitelistedAtomStateTracker)314 TEST(MetricsManagerTest, TestWhitelistedAtomStateTracker) {
315 sp<UidMap> uidMap;
316 sp<StatsPullerManager> pullerManager = new StatsPullerManager();
317 sp<AlarmMonitor> anomalyAlarmMonitor;
318 sp<AlarmMonitor> periodicAlarmMonitor;
319
320 StatsdConfig config = buildGoodConfig(kConfigId);
321 config.add_allowed_log_source("AID_SYSTEM");
322 config.add_whitelisted_atom_ids(3);
323 config.add_whitelisted_atom_ids(4);
324
325 State state;
326 state.set_id(1);
327 state.set_atom_id(3);
328
329 *config.add_state() = state;
330
331 config.mutable_count_metric(0)->add_slice_by_state(state.id());
332
333 StateManager::getInstance().clear();
334
335 MetricsManager metricsManager(kConfigKey, config, timeBaseSec, timeBaseSec, uidMap,
336 pullerManager, anomalyAlarmMonitor, periodicAlarmMonitor);
337
338 EXPECT_EQ(0, StateManager::getInstance().getStateTrackersCount());
339 EXPECT_FALSE(metricsManager.isConfigValid());
340 }
341
TEST_P(MetricsManagerTest_SPlus,TestRestrictedMetricsConfig)342 TEST_P(MetricsManagerTest_SPlus, TestRestrictedMetricsConfig) {
343 sp<UidMap> uidMap;
344 sp<StatsPullerManager> pullerManager = new StatsPullerManager();
345 sp<AlarmMonitor> anomalyAlarmMonitor;
346 sp<AlarmMonitor> periodicAlarmMonitor;
347
348 StatsdConfig config = buildGoodRestrictedConfig();
349 config.add_allowed_log_source("AID_SYSTEM");
350 config.set_restricted_metrics_delegate_package_name("rm");
351
352 MetricsManager metricsManager(kConfigKey, config, timeBaseSec, timeBaseSec, uidMap,
353 pullerManager, anomalyAlarmMonitor, periodicAlarmMonitor);
354
355 if (isAtLeastU()) {
356 EXPECT_TRUE(metricsManager.isConfigValid());
357 } else {
358 EXPECT_EQ(metricsManager.mInvalidConfigReason,
359 INVALID_CONFIG_REASON_RESTRICTED_METRIC_NOT_ENABLED);
360 ASSERT_FALSE(metricsManager.isConfigValid());
361 }
362 }
363
TEST_P(MetricsManagerTest_SPlus,TestRestrictedMetricsConfigUpdate)364 TEST_P(MetricsManagerTest_SPlus, TestRestrictedMetricsConfigUpdate) {
365 sp<UidMap> uidMap;
366 sp<StatsPullerManager> pullerManager = new StatsPullerManager();
367 sp<AlarmMonitor> anomalyAlarmMonitor;
368 sp<AlarmMonitor> periodicAlarmMonitor;
369
370 StatsdConfig config = buildGoodRestrictedConfig();
371 config.add_allowed_log_source("AID_SYSTEM");
372 config.set_restricted_metrics_delegate_package_name("rm");
373
374 MetricsManager metricsManager(kConfigKey, config, timeBaseSec, timeBaseSec, uidMap,
375 pullerManager, anomalyAlarmMonitor, periodicAlarmMonitor);
376
377 StatsdConfig config2 = buildGoodRestrictedConfig();
378 metricsManager.updateConfig(config, timeBaseSec, timeBaseSec, anomalyAlarmMonitor,
379 periodicAlarmMonitor);
380
381 if (isAtLeastU()) {
382 EXPECT_TRUE(metricsManager.isConfigValid());
383 } else {
384 EXPECT_EQ(metricsManager.mInvalidConfigReason,
385 INVALID_CONFIG_REASON_RESTRICTED_METRIC_NOT_ENABLED);
386 ASSERT_FALSE(metricsManager.isConfigValid());
387 }
388 }
389
TEST(MetricsManagerTest,TestMaxMetricsMemoryKb)390 TEST(MetricsManagerTest, TestMaxMetricsMemoryKb) {
391 sp<UidMap> uidMap;
392 sp<StatsPullerManager> pullerManager = new StatsPullerManager();
393 sp<AlarmMonitor> anomalyAlarmMonitor;
394 sp<AlarmMonitor> periodicAlarmMonitor;
395 size_t memoryLimitKb = 8 * 1024;
396
397 StatsdConfig config = buildGoodConfig(kConfigId);
398 config.add_allowed_log_source("AID_SYSTEM");
399 config.set_max_metrics_memory_kb(memoryLimitKb);
400
401 MetricsManager metricsManager(kConfigKey, config, timeBaseSec, timeBaseSec, uidMap,
402 pullerManager, anomalyAlarmMonitor, periodicAlarmMonitor);
403
404 EXPECT_EQ(memoryLimitKb, metricsManager.getMaxMetricsBytes() / 1024);
405 EXPECT_TRUE(metricsManager.isConfigValid());
406 }
407
TEST(MetricsManagerTest,TestMaxMetricsMemoryKbOnConfigUpdate)408 TEST(MetricsManagerTest, TestMaxMetricsMemoryKbOnConfigUpdate) {
409 sp<UidMap> uidMap;
410 sp<StatsPullerManager> pullerManager = new StatsPullerManager();
411 sp<AlarmMonitor> anomalyAlarmMonitor;
412 sp<AlarmMonitor> periodicAlarmMonitor;
413 size_t memoryLimitKb = 8 * 1024;
414
415 StatsdConfig config = buildGoodConfig(kConfigId);
416 config.add_allowed_log_source("AID_SYSTEM");
417 config.set_max_metrics_memory_kb(memoryLimitKb);
418
419 MetricsManager metricsManager(kConfigKey, config, timeBaseSec, timeBaseSec, uidMap,
420 pullerManager, anomalyAlarmMonitor, periodicAlarmMonitor);
421
422 EXPECT_EQ(memoryLimitKb, metricsManager.getMaxMetricsBytes() / 1024);
423 EXPECT_TRUE(metricsManager.isConfigValid());
424
425 // Update with new memory limit
426 size_t newMemoryLimitKb = 10 * 1024;
427 StatsdConfig newConfig;
428 newConfig.add_allowed_log_source("AID_SYSTEM");
429 newConfig.set_max_metrics_memory_kb(newMemoryLimitKb);
430
431 metricsManager.updateConfig(newConfig, timeBaseSec, timeBaseSec, anomalyAlarmMonitor,
432 periodicAlarmMonitor);
433 EXPECT_EQ(newMemoryLimitKb, metricsManager.getMaxMetricsBytes() / 1024);
434 EXPECT_TRUE(metricsManager.isConfigValid());
435 }
436
TEST(MetricsManagerTest,TestMaxMetricsMemoryKbInvalid)437 TEST(MetricsManagerTest, TestMaxMetricsMemoryKbInvalid) {
438 sp<UidMap> uidMap;
439 sp<StatsPullerManager> pullerManager = new StatsPullerManager();
440 sp<AlarmMonitor> anomalyAlarmMonitor;
441 sp<AlarmMonitor> periodicAlarmMonitor;
442 size_t memoryLimitKb = (StatsdStats::kHardMaxMetricsBytesPerConfig / 1024) + 1;
443 size_t defaultMemoryLimit = StatsdStats::kDefaultMaxMetricsBytesPerConfig;
444
445 StatsdConfig config = buildGoodConfig(kConfigId);
446 config.add_allowed_log_source("AID_SYSTEM");
447 config.set_max_metrics_memory_kb(memoryLimitKb);
448
449 MetricsManager metricsManager(kConfigKey, config, timeBaseSec, timeBaseSec, uidMap,
450 pullerManager, anomalyAlarmMonitor, periodicAlarmMonitor);
451
452 // Since 20MB + 1B is invalid for the memory limit, we default back to 2MB
453 EXPECT_EQ(defaultMemoryLimit, metricsManager.getMaxMetricsBytes());
454 EXPECT_TRUE(metricsManager.isConfigValid());
455 }
456
TEST(MetricsManagerTest,TestGetTriggerMemoryKb)457 TEST(MetricsManagerTest, TestGetTriggerMemoryKb) {
458 sp<UidMap> uidMap;
459 sp<StatsPullerManager> pullerManager = new StatsPullerManager();
460 sp<AlarmMonitor> anomalyAlarmMonitor;
461 sp<AlarmMonitor> periodicAlarmMonitor;
462 size_t memoryLimitKb = 8 * 1024;
463
464 StatsdConfig config = buildGoodConfig(kConfigId);
465 config.add_allowed_log_source("AID_SYSTEM");
466 config.set_soft_metrics_memory_kb(memoryLimitKb);
467
468 MetricsManager metricsManager(kConfigKey, config, timeBaseSec, timeBaseSec, uidMap,
469 pullerManager, anomalyAlarmMonitor, periodicAlarmMonitor);
470
471 EXPECT_EQ(memoryLimitKb, metricsManager.getTriggerGetDataBytes() / 1024);
472 EXPECT_TRUE(metricsManager.isConfigValid());
473 }
474
TEST(MetricsManagerTest,TestGetTriggerMemoryKbOnConfigUpdate)475 TEST(MetricsManagerTest, TestGetTriggerMemoryKbOnConfigUpdate) {
476 sp<UidMap> uidMap;
477 sp<StatsPullerManager> pullerManager = new StatsPullerManager();
478 sp<AlarmMonitor> anomalyAlarmMonitor;
479 sp<AlarmMonitor> periodicAlarmMonitor;
480 size_t memoryLimitKb = 8 * 1024;
481
482 StatsdConfig config = buildGoodConfig(kConfigId);
483 config.add_allowed_log_source("AID_SYSTEM");
484 config.set_soft_metrics_memory_kb(memoryLimitKb);
485
486 MetricsManager metricsManager(kConfigKey, config, timeBaseSec, timeBaseSec, uidMap,
487 pullerManager, anomalyAlarmMonitor, periodicAlarmMonitor);
488
489 EXPECT_EQ(memoryLimitKb, metricsManager.getTriggerGetDataBytes() / 1024);
490 EXPECT_TRUE(metricsManager.isConfigValid());
491
492 // Update with new memory limit
493 size_t newMemoryLimitKb = 9 * 1024;
494 StatsdConfig newConfig;
495 newConfig.add_allowed_log_source("AID_SYSTEM");
496 newConfig.set_soft_metrics_memory_kb(newMemoryLimitKb);
497
498 metricsManager.updateConfig(newConfig, timeBaseSec, timeBaseSec, anomalyAlarmMonitor,
499 periodicAlarmMonitor);
500 EXPECT_EQ(newMemoryLimitKb, metricsManager.getTriggerGetDataBytes() / 1024);
501 EXPECT_TRUE(metricsManager.isConfigValid());
502 }
503
TEST(MetricsManagerTest,TestGetTriggerMemoryKbInvalid)504 TEST(MetricsManagerTest, TestGetTriggerMemoryKbInvalid) {
505 sp<UidMap> uidMap;
506 sp<StatsPullerManager> pullerManager = new StatsPullerManager();
507 sp<AlarmMonitor> anomalyAlarmMonitor;
508 sp<AlarmMonitor> periodicAlarmMonitor;
509 size_t memoryLimitKb = (StatsdStats::kHardMaxTriggerGetDataBytes / 1024) + 1;
510 size_t defaultMemoryLimit = StatsdStats::kDefaultBytesPerConfigTriggerGetData;
511
512 StatsdConfig config = buildGoodConfig(kConfigId);
513 config.add_allowed_log_source("AID_SYSTEM");
514 config.set_soft_metrics_memory_kb(memoryLimitKb);
515
516 MetricsManager metricsManager(kConfigKey, config, timeBaseSec, timeBaseSec, uidMap,
517 pullerManager, anomalyAlarmMonitor, periodicAlarmMonitor);
518
519 // Since 10MB + 1B is invalid for the memory limit, we default back to 192KB
520 EXPECT_EQ(defaultMemoryLimit, metricsManager.getTriggerGetDataBytes());
521 EXPECT_TRUE(metricsManager.isConfigValid());
522 }
523
TEST(MetricsManagerTest,TestGetTriggerMemoryKbUnset)524 TEST(MetricsManagerTest, TestGetTriggerMemoryKbUnset) {
525 sp<UidMap> uidMap;
526 sp<StatsPullerManager> pullerManager = new StatsPullerManager();
527 sp<AlarmMonitor> anomalyAlarmMonitor;
528 sp<AlarmMonitor> periodicAlarmMonitor;
529 size_t defaultMemoryLimit = StatsdStats::kDefaultBytesPerConfigTriggerGetData;
530
531 StatsdConfig config = buildGoodConfig(kConfigId);
532 config.add_allowed_log_source("AID_SYSTEM");
533
534 MetricsManager metricsManager(kConfigKey, config, timeBaseSec, timeBaseSec, uidMap,
535 pullerManager, anomalyAlarmMonitor, periodicAlarmMonitor);
536
537 // Since the memory limit is unset, we default back to 192KB
538 EXPECT_EQ(defaultMemoryLimit, metricsManager.getTriggerGetDataBytes());
539 EXPECT_TRUE(metricsManager.isConfigValid());
540 }
541
542 } // namespace statsd
543 } // namespace os
544 } // namespace android
545
546 #else
547 GTEST_LOG_(INFO) << "This test does nothing.\n";
548 #endif
549