| David Chen | d9269e2 | 2017-12-05 13:43:51 -0800 | [diff] [blame] | 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 "StatsLogProcessor.h" |
| 16 | #include "config/ConfigKey.h" |
| David Chen | 35045cb | 2018-03-23 22:21:47 -0700 | [diff] [blame] | 17 | #include "frameworks/base/cmds/statsd/src/stats_log.pb.h" |
| David Chen | d9269e2 | 2017-12-05 13:43:51 -0800 | [diff] [blame] | 18 | #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 | |
| David Chen | 9e6dbbd | 2018-05-07 17:52:29 -0700 | [diff] [blame] | 27 | #include "tests/statsd_test_util.h" |
| 28 | |
| David Chen | d9269e2 | 2017-12-05 13:43:51 -0800 | [diff] [blame] | 29 | #include <stdio.h> |
| 30 | |
| 31 | using namespace android; |
| 32 | using namespace testing; |
| 33 | |
| 34 | namespace android { |
| 35 | namespace os { |
| 36 | namespace statsd { |
| 37 | |
| Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 38 | using android::util::ProtoOutputStream; |
| 39 | |
| David Chen | d9269e2 | 2017-12-05 13:43:51 -0800 | [diff] [blame] | 40 | #ifdef __ANDROID__ |
| 41 | |
| 42 | /** |
| 43 | * Mock MetricsManager (ByteSize() is called). |
| 44 | */ |
| 45 | class MockMetricsManager : public MetricsManager { |
| 46 | public: |
| Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 47 | MockMetricsManager() |
| 48 | : MetricsManager(ConfigKey(1, 12345), StatsdConfig(), 1000, 1000, new UidMap(), |
| 49 | new StatsPullerManager(), |
| 50 | new AlarmMonitor(10, [](const sp<IStatsCompanionService>&, int64_t) {}, |
| 51 | [](const sp<IStatsCompanionService>&) {}), |
| 52 | new AlarmMonitor(10, [](const sp<IStatsCompanionService>&, int64_t) {}, |
| 53 | [](const sp<IStatsCompanionService>&) {})) { |
| David Chen | d9269e2 | 2017-12-05 13:43:51 -0800 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | MOCK_METHOD0(byteSize, size_t()); |
| Yao Chen | 06dba5d | 2018-01-26 13:38:16 -0800 | [diff] [blame] | 57 | |
| Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 58 | MOCK_METHOD1(dropData, void(const int64_t dropTimeNs)); |
| David Chen | d9269e2 | 2017-12-05 13:43:51 -0800 | [diff] [blame] | 59 | }; |
| 60 | |
| 61 | TEST(StatsLogProcessorTest, TestRateLimitByteSize) { |
| 62 | sp<UidMap> m = new UidMap(); |
| Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 63 | sp<StatsPullerManager> pullerManager = new StatsPullerManager(); |
| Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 64 | sp<AlarmMonitor> anomalyAlarmMonitor; |
| 65 | sp<AlarmMonitor> periodicAlarmMonitor; |
| David Chen | d9269e2 | 2017-12-05 13:43:51 -0800 | [diff] [blame] | 66 | // Construct the processor with a dummy sendBroadcast function that does nothing. |
| Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 67 | StatsLogProcessor p(m, pullerManager, anomalyAlarmMonitor, periodicAlarmMonitor, 0, |
| 68 | [](const ConfigKey& key) { return true; }); |
| David Chen | d9269e2 | 2017-12-05 13:43:51 -0800 | [diff] [blame] | 69 | |
| 70 | MockMetricsManager mockMetricsManager; |
| 71 | |
| Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 72 | ConfigKey key(100, 12345); |
| David Chen | d9269e2 | 2017-12-05 13:43:51 -0800 | [diff] [blame] | 73 | // Expect only the first flush to trigger a check for byte size since the last two are |
| 74 | // rate-limited. |
| 75 | EXPECT_CALL(mockMetricsManager, byteSize()).Times(1); |
| Yangster-mac | b0d0628 | 2018-01-05 15:44:07 -0800 | [diff] [blame] | 76 | p.flushIfNecessaryLocked(99, key, mockMetricsManager); |
| 77 | p.flushIfNecessaryLocked(100, key, mockMetricsManager); |
| 78 | p.flushIfNecessaryLocked(101, key, mockMetricsManager); |
| David Chen | d9269e2 | 2017-12-05 13:43:51 -0800 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | TEST(StatsLogProcessorTest, TestRateLimitBroadcast) { |
| 82 | sp<UidMap> m = new UidMap(); |
| Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 83 | sp<StatsPullerManager> pullerManager = new StatsPullerManager(); |
| Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 84 | sp<AlarmMonitor> anomalyAlarmMonitor; |
| 85 | sp<AlarmMonitor> subscriberAlarmMonitor; |
| David Chen | d9269e2 | 2017-12-05 13:43:51 -0800 | [diff] [blame] | 86 | int broadcastCount = 0; |
| Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 87 | StatsLogProcessor p(m, pullerManager, anomalyAlarmMonitor, subscriberAlarmMonitor, 0, |
| 88 | [&broadcastCount](const ConfigKey& key) { |
| 89 | broadcastCount++; |
| 90 | return true; |
| 91 | }); |
| David Chen | d9269e2 | 2017-12-05 13:43:51 -0800 | [diff] [blame] | 92 | |
| 93 | MockMetricsManager mockMetricsManager; |
| 94 | |
| Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 95 | ConfigKey key(100, 12345); |
| David Chen | d9269e2 | 2017-12-05 13:43:51 -0800 | [diff] [blame] | 96 | EXPECT_CALL(mockMetricsManager, byteSize()) |
| Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 97 | .Times(1) |
| David Chen | d9269e2 | 2017-12-05 13:43:51 -0800 | [diff] [blame] | 98 | .WillRepeatedly(Return(int(StatsdStats::kMaxMetricsBytesPerConfig * .95))); |
| 99 | |
| 100 | // Expect only one broadcast despite always returning a size that should trigger broadcast. |
| Yangster-mac | b0d0628 | 2018-01-05 15:44:07 -0800 | [diff] [blame] | 101 | p.flushIfNecessaryLocked(1, key, mockMetricsManager); |
| David Chen | d9269e2 | 2017-12-05 13:43:51 -0800 | [diff] [blame] | 102 | EXPECT_EQ(1, broadcastCount); |
| 103 | |
| Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 104 | // b/73089712 |
| David Chen | d9269e2 | 2017-12-05 13:43:51 -0800 | [diff] [blame] | 105 | // This next call to flush should not trigger a broadcast. |
| Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 106 | // p.mLastByteSizeTimes.clear(); // Force another check for byte size. |
| 107 | // p.flushIfNecessaryLocked(2, key, mockMetricsManager); |
| 108 | // EXPECT_EQ(1, broadcastCount); |
| David Chen | d9269e2 | 2017-12-05 13:43:51 -0800 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | TEST(StatsLogProcessorTest, TestDropWhenByteSizeTooLarge) { |
| 112 | sp<UidMap> m = new UidMap(); |
| Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 113 | sp<StatsPullerManager> pullerManager = new StatsPullerManager(); |
| Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 114 | sp<AlarmMonitor> anomalyAlarmMonitor; |
| 115 | sp<AlarmMonitor> subscriberAlarmMonitor; |
| David Chen | d9269e2 | 2017-12-05 13:43:51 -0800 | [diff] [blame] | 116 | int broadcastCount = 0; |
| Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 117 | StatsLogProcessor p(m, pullerManager, anomalyAlarmMonitor, subscriberAlarmMonitor, 0, |
| 118 | [&broadcastCount](const ConfigKey& key) { |
| 119 | broadcastCount++; |
| 120 | return true; |
| 121 | }); |
| David Chen | d9269e2 | 2017-12-05 13:43:51 -0800 | [diff] [blame] | 122 | |
| 123 | MockMetricsManager mockMetricsManager; |
| 124 | |
| Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 125 | ConfigKey key(100, 12345); |
| David Chen | d9269e2 | 2017-12-05 13:43:51 -0800 | [diff] [blame] | 126 | EXPECT_CALL(mockMetricsManager, byteSize()) |
| 127 | .Times(1) |
| 128 | .WillRepeatedly(Return(int(StatsdStats::kMaxMetricsBytesPerConfig * 1.2))); |
| 129 | |
| Yao Chen | 06dba5d | 2018-01-26 13:38:16 -0800 | [diff] [blame] | 130 | EXPECT_CALL(mockMetricsManager, dropData(_)).Times(1); |
| David Chen | d9269e2 | 2017-12-05 13:43:51 -0800 | [diff] [blame] | 131 | |
| 132 | // Expect to call the onDumpReport and skip the broadcast. |
| Yangster-mac | b0d0628 | 2018-01-05 15:44:07 -0800 | [diff] [blame] | 133 | p.flushIfNecessaryLocked(1, key, mockMetricsManager); |
| David Chen | d9269e2 | 2017-12-05 13:43:51 -0800 | [diff] [blame] | 134 | EXPECT_EQ(0, broadcastCount); |
| 135 | } |
| 136 | |
| David Chen | 9e6dbbd | 2018-05-07 17:52:29 -0700 | [diff] [blame] | 137 | StatsdConfig MakeConfig(bool includeMetric) { |
| 138 | StatsdConfig config; |
| 139 | config.add_allowed_log_source("AID_ROOT"); // LogEvent defaults to UID of root. |
| 140 | |
| 141 | if (includeMetric) { |
| 142 | auto appCrashMatcher = CreateProcessCrashAtomMatcher(); |
| 143 | *config.add_atom_matcher() = appCrashMatcher; |
| 144 | auto countMetric = config.add_count_metric(); |
| 145 | countMetric->set_id(StringToId("AppCrashes")); |
| 146 | countMetric->set_what(appCrashMatcher.id()); |
| 147 | countMetric->set_bucket(FIVE_MINUTES); |
| 148 | } |
| 149 | return config; |
| 150 | } |
| 151 | |
| David Chen | 35045cb | 2018-03-23 22:21:47 -0700 | [diff] [blame] | 152 | TEST(StatsLogProcessorTest, TestUidMapHasSnapshot) { |
| 153 | // Setup simple config key corresponding to empty config. |
| 154 | sp<UidMap> m = new UidMap(); |
| Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 155 | sp<StatsPullerManager> pullerManager = new StatsPullerManager(); |
| David Chen | bd12527 | 2018-04-04 19:02:50 -0700 | [diff] [blame] | 156 | m->updateMap(1, {1, 2}, {1, 2}, {String16("p1"), String16("p2")}); |
| David Chen | 35045cb | 2018-03-23 22:21:47 -0700 | [diff] [blame] | 157 | sp<AlarmMonitor> anomalyAlarmMonitor; |
| 158 | sp<AlarmMonitor> subscriberAlarmMonitor; |
| 159 | int broadcastCount = 0; |
| Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 160 | StatsLogProcessor p(m, pullerManager, anomalyAlarmMonitor, subscriberAlarmMonitor, 0, |
| 161 | [&broadcastCount](const ConfigKey& key) { |
| 162 | broadcastCount++; |
| 163 | return true; |
| 164 | }); |
| David Chen | 35045cb | 2018-03-23 22:21:47 -0700 | [diff] [blame] | 165 | ConfigKey key(3, 4); |
| David Chen | 9e6dbbd | 2018-05-07 17:52:29 -0700 | [diff] [blame] | 166 | StatsdConfig config = MakeConfig(true); |
| Yangster-mac | c04feba | 2018-04-02 14:37:33 -0700 | [diff] [blame] | 167 | p.OnConfigUpdated(0, key, config); |
| David Chen | 35045cb | 2018-03-23 22:21:47 -0700 | [diff] [blame] | 168 | |
| 169 | // Expect to get no metrics, but snapshot specified above in uidmap. |
| 170 | vector<uint8_t> bytes; |
| Bookatz | ff71cad | 2018-09-20 17:17:49 -0700 | [diff] [blame^] | 171 | p.onDumpReport(key, 1, false, true, ADB_DUMP, &bytes); |
| David Chen | 35045cb | 2018-03-23 22:21:47 -0700 | [diff] [blame] | 172 | |
| 173 | ConfigMetricsReportList output; |
| 174 | output.ParseFromArray(bytes.data(), bytes.size()); |
| 175 | EXPECT_TRUE(output.reports_size() > 0); |
| 176 | auto uidmap = output.reports(0).uid_map(); |
| 177 | EXPECT_TRUE(uidmap.snapshots_size() > 0); |
| 178 | EXPECT_EQ(2, uidmap.snapshots(0).package_info_size()); |
| 179 | } |
| 180 | |
| David Chen | 9e6dbbd | 2018-05-07 17:52:29 -0700 | [diff] [blame] | 181 | TEST(StatsLogProcessorTest, TestEmptyConfigHasNoUidMap) { |
| 182 | // Setup simple config key corresponding to empty config. |
| 183 | sp<UidMap> m = new UidMap(); |
| Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 184 | sp<StatsPullerManager> pullerManager = new StatsPullerManager(); |
| David Chen | 9e6dbbd | 2018-05-07 17:52:29 -0700 | [diff] [blame] | 185 | m->updateMap(1, {1, 2}, {1, 2}, {String16("p1"), String16("p2")}); |
| 186 | sp<AlarmMonitor> anomalyAlarmMonitor; |
| 187 | sp<AlarmMonitor> subscriberAlarmMonitor; |
| 188 | int broadcastCount = 0; |
| Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 189 | StatsLogProcessor p(m, pullerManager, anomalyAlarmMonitor, subscriberAlarmMonitor, 0, |
| 190 | [&broadcastCount](const ConfigKey& key) { |
| 191 | broadcastCount++; |
| 192 | return true; |
| 193 | }); |
| David Chen | 9e6dbbd | 2018-05-07 17:52:29 -0700 | [diff] [blame] | 194 | ConfigKey key(3, 4); |
| 195 | StatsdConfig config = MakeConfig(false); |
| 196 | p.OnConfigUpdated(0, key, config); |
| 197 | |
| 198 | // Expect to get no metrics, but snapshot specified above in uidmap. |
| 199 | vector<uint8_t> bytes; |
| Bookatz | ff71cad | 2018-09-20 17:17:49 -0700 | [diff] [blame^] | 200 | p.onDumpReport(key, 1, false, true, ADB_DUMP, &bytes); |
| David Chen | 9e6dbbd | 2018-05-07 17:52:29 -0700 | [diff] [blame] | 201 | |
| 202 | ConfigMetricsReportList output; |
| 203 | output.ParseFromArray(bytes.data(), bytes.size()); |
| 204 | EXPECT_TRUE(output.reports_size() > 0); |
| 205 | EXPECT_FALSE(output.reports(0).has_uid_map()); |
| 206 | } |
| 207 | |
| David Chen | faa1af5 | 2018-03-30 15:14:04 -0700 | [diff] [blame] | 208 | TEST(StatsLogProcessorTest, TestReportIncludesSubConfig) { |
| 209 | // Setup simple config key corresponding to empty config. |
| 210 | sp<UidMap> m = new UidMap(); |
| Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 211 | sp<StatsPullerManager> pullerManager = new StatsPullerManager(); |
| David Chen | faa1af5 | 2018-03-30 15:14:04 -0700 | [diff] [blame] | 212 | sp<AlarmMonitor> anomalyAlarmMonitor; |
| 213 | sp<AlarmMonitor> subscriberAlarmMonitor; |
| 214 | int broadcastCount = 0; |
| Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 215 | StatsLogProcessor p(m, pullerManager, anomalyAlarmMonitor, subscriberAlarmMonitor, 0, |
| 216 | [&broadcastCount](const ConfigKey& key) { |
| 217 | broadcastCount++; |
| 218 | return true; |
| 219 | }); |
| David Chen | faa1af5 | 2018-03-30 15:14:04 -0700 | [diff] [blame] | 220 | ConfigKey key(3, 4); |
| 221 | StatsdConfig config; |
| 222 | auto annotation = config.add_annotation(); |
| 223 | annotation->set_field_int64(1); |
| 224 | annotation->set_field_int32(2); |
| 225 | config.add_allowed_log_source("AID_ROOT"); |
| 226 | p.OnConfigUpdated(1, key, config); |
| 227 | |
| 228 | // Expect to get no metrics, but snapshot specified above in uidmap. |
| 229 | vector<uint8_t> bytes; |
| Bookatz | ff71cad | 2018-09-20 17:17:49 -0700 | [diff] [blame^] | 230 | p.onDumpReport(key, 1, false, true, ADB_DUMP, &bytes); |
| David Chen | faa1af5 | 2018-03-30 15:14:04 -0700 | [diff] [blame] | 231 | |
| 232 | ConfigMetricsReportList output; |
| 233 | output.ParseFromArray(bytes.data(), bytes.size()); |
| 234 | EXPECT_TRUE(output.reports_size() > 0); |
| 235 | auto report = output.reports(0); |
| 236 | EXPECT_EQ(1, report.annotation_size()); |
| 237 | EXPECT_EQ(1, report.annotation(0).field_int64()); |
| 238 | EXPECT_EQ(2, report.annotation(0).field_int32()); |
| 239 | } |
| 240 | |
| David Chen | d9269e2 | 2017-12-05 13:43:51 -0800 | [diff] [blame] | 241 | #else |
| 242 | GTEST_LOG_(INFO) << "This test does nothing.\n"; |
| 243 | #endif |
| 244 | |
| 245 | } // namespace statsd |
| 246 | } // namespace os |
| Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 247 | } // namespace android |