blob: 4b9a87d78817a04485041f4a694afb3d670f2169 [file] [log] [blame]
David Chend9269e22017-12-05 13:43:51 -08001// 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 "StatsLogProcessor.h"
16#include "config/ConfigKey.h"
David Chen35045cb2018-03-23 22:21:47 -070017#include "frameworks/base/cmds/statsd/src/stats_log.pb.h"
David Chend9269e22017-12-05 13:43:51 -080018#include "frameworks/base/cmds/statsd/src/statsd_config.pb.h"
19#include "guardrail/StatsdStats.h"
20#include "logd/LogEvent.h"
21#include "packages/UidMap.h"
22#include "statslog.h"
23
24#include <gmock/gmock.h>
25#include <gtest/gtest.h>
26
27#include <stdio.h>
28
29using namespace android;
30using namespace testing;
31
32namespace android {
33namespace os {
34namespace statsd {
35
Yao Chen288c6002017-12-12 13:43:18 -080036using android::util::ProtoOutputStream;
37
David Chend9269e22017-12-05 13:43:51 -080038#ifdef __ANDROID__
39
40/**
41 * Mock MetricsManager (ByteSize() is called).
42 */
43class MockMetricsManager : public MetricsManager {
44public:
Yangster-mac932ecec2018-02-01 10:23:52 -080045 MockMetricsManager() : MetricsManager(
46 ConfigKey(1, 12345), StatsdConfig(), 1000,
47 new UidMap(),
48 new AlarmMonitor(10, [](const sp<IStatsCompanionService>&, int64_t){},
49 [](const sp<IStatsCompanionService>&){}),
50 new AlarmMonitor(10, [](const sp<IStatsCompanionService>&, int64_t){},
51 [](const sp<IStatsCompanionService>&){})) {
David Chend9269e22017-12-05 13:43:51 -080052 }
53
54 MOCK_METHOD0(byteSize, size_t());
Yao Chen06dba5d2018-01-26 13:38:16 -080055
56 MOCK_METHOD1(dropData, void(const uint64_t dropTimeNs));
David Chend9269e22017-12-05 13:43:51 -080057};
58
59TEST(StatsLogProcessorTest, TestRateLimitByteSize) {
60 sp<UidMap> m = new UidMap();
Yangster-mac932ecec2018-02-01 10:23:52 -080061 sp<AlarmMonitor> anomalyAlarmMonitor;
62 sp<AlarmMonitor> periodicAlarmMonitor;
David Chend9269e22017-12-05 13:43:51 -080063 // Construct the processor with a dummy sendBroadcast function that does nothing.
Yangster-mac932ecec2018-02-01 10:23:52 -080064 StatsLogProcessor p(m, anomalyAlarmMonitor, periodicAlarmMonitor, 0,
65 [](const ConfigKey& key) {});
David Chend9269e22017-12-05 13:43:51 -080066
67 MockMetricsManager mockMetricsManager;
68
Yangster-mac94e197c2018-01-02 16:03:03 -080069 ConfigKey key(100, 12345);
David Chend9269e22017-12-05 13:43:51 -080070 // Expect only the first flush to trigger a check for byte size since the last two are
71 // rate-limited.
72 EXPECT_CALL(mockMetricsManager, byteSize()).Times(1);
Yangster-macb0d06282018-01-05 15:44:07 -080073 p.flushIfNecessaryLocked(99, key, mockMetricsManager);
74 p.flushIfNecessaryLocked(100, key, mockMetricsManager);
75 p.flushIfNecessaryLocked(101, key, mockMetricsManager);
David Chend9269e22017-12-05 13:43:51 -080076}
77
78TEST(StatsLogProcessorTest, TestRateLimitBroadcast) {
79 sp<UidMap> m = new UidMap();
Yangster-mac932ecec2018-02-01 10:23:52 -080080 sp<AlarmMonitor> anomalyAlarmMonitor;
81 sp<AlarmMonitor> subscriberAlarmMonitor;
David Chend9269e22017-12-05 13:43:51 -080082 int broadcastCount = 0;
Yangster-mac932ecec2018-02-01 10:23:52 -080083 StatsLogProcessor p(m, anomalyAlarmMonitor, subscriberAlarmMonitor, 0,
84 [&broadcastCount](const ConfigKey& key) { broadcastCount++; });
David Chend9269e22017-12-05 13:43:51 -080085
86 MockMetricsManager mockMetricsManager;
87
Yangster-mac94e197c2018-01-02 16:03:03 -080088 ConfigKey key(100, 12345);
David Chend9269e22017-12-05 13:43:51 -080089 EXPECT_CALL(mockMetricsManager, byteSize())
Yao Chen8a8d16c2018-02-08 14:50:40 -080090 .Times(1)
David Chend9269e22017-12-05 13:43:51 -080091 .WillRepeatedly(Return(int(StatsdStats::kMaxMetricsBytesPerConfig * .95)));
92
93 // Expect only one broadcast despite always returning a size that should trigger broadcast.
Yangster-macb0d06282018-01-05 15:44:07 -080094 p.flushIfNecessaryLocked(1, key, mockMetricsManager);
David Chend9269e22017-12-05 13:43:51 -080095 EXPECT_EQ(1, broadcastCount);
96
Yao Chen8a8d16c2018-02-08 14:50:40 -080097 // b/73089712
David Chend9269e22017-12-05 13:43:51 -080098 // This next call to flush should not trigger a broadcast.
Yao Chen8a8d16c2018-02-08 14:50:40 -080099 // p.mLastByteSizeTimes.clear(); // Force another check for byte size.
100 // p.flushIfNecessaryLocked(2, key, mockMetricsManager);
101 // EXPECT_EQ(1, broadcastCount);
David Chend9269e22017-12-05 13:43:51 -0800102}
103
104TEST(StatsLogProcessorTest, TestDropWhenByteSizeTooLarge) {
105 sp<UidMap> m = new UidMap();
Yangster-mac932ecec2018-02-01 10:23:52 -0800106 sp<AlarmMonitor> anomalyAlarmMonitor;
107 sp<AlarmMonitor> subscriberAlarmMonitor;
David Chend9269e22017-12-05 13:43:51 -0800108 int broadcastCount = 0;
Yangster-mac932ecec2018-02-01 10:23:52 -0800109 StatsLogProcessor p(m, anomalyAlarmMonitor, subscriberAlarmMonitor, 0,
David Chend9269e22017-12-05 13:43:51 -0800110 [&broadcastCount](const ConfigKey& key) { broadcastCount++; });
111
112 MockMetricsManager mockMetricsManager;
113
Yangster-mac94e197c2018-01-02 16:03:03 -0800114 ConfigKey key(100, 12345);
David Chend9269e22017-12-05 13:43:51 -0800115 EXPECT_CALL(mockMetricsManager, byteSize())
116 .Times(1)
117 .WillRepeatedly(Return(int(StatsdStats::kMaxMetricsBytesPerConfig * 1.2)));
118
Yao Chen06dba5d2018-01-26 13:38:16 -0800119 EXPECT_CALL(mockMetricsManager, dropData(_)).Times(1);
David Chend9269e22017-12-05 13:43:51 -0800120
121 // Expect to call the onDumpReport and skip the broadcast.
Yangster-macb0d06282018-01-05 15:44:07 -0800122 p.flushIfNecessaryLocked(1, key, mockMetricsManager);
David Chend9269e22017-12-05 13:43:51 -0800123 EXPECT_EQ(0, broadcastCount);
124}
125
David Chen35045cb2018-03-23 22:21:47 -0700126TEST(StatsLogProcessorTest, TestUidMapHasSnapshot) {
127 // Setup simple config key corresponding to empty config.
128 sp<UidMap> m = new UidMap();
129 m->updateMap({1, 2}, {1, 2}, {String16("p1"), String16("p2")});
130 sp<AlarmMonitor> anomalyAlarmMonitor;
131 sp<AlarmMonitor> subscriberAlarmMonitor;
132 int broadcastCount = 0;
133 StatsLogProcessor p(m, anomalyAlarmMonitor, subscriberAlarmMonitor, 0,
134 [&broadcastCount](const ConfigKey& key) { broadcastCount++; });
135 ConfigKey key(3, 4);
136 StatsdConfig config;
137 config.add_allowed_log_source("AID_ROOT");
138 p.OnConfigUpdated(key, config);
139
140 // Expect to get no metrics, but snapshot specified above in uidmap.
141 vector<uint8_t> bytes;
142 p.onDumpReport(key, 1, &bytes);
143
144 ConfigMetricsReportList output;
145 output.ParseFromArray(bytes.data(), bytes.size());
146 EXPECT_TRUE(output.reports_size() > 0);
147 auto uidmap = output.reports(0).uid_map();
148 EXPECT_TRUE(uidmap.snapshots_size() > 0);
149 EXPECT_EQ(2, uidmap.snapshots(0).package_info_size());
150}
151
David Chend9269e22017-12-05 13:43:51 -0800152#else
153GTEST_LOG_(INFO) << "This test does nothing.\n";
154#endif
155
156} // namespace statsd
157} // namespace os
Yao Chen288c6002017-12-12 13:43:18 -0800158} // namespace android