| 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 | |
| 27 | #include <stdio.h> |
| 28 | |
| 29 | using namespace android; |
| 30 | using namespace testing; |
| 31 | |
| 32 | namespace android { |
| 33 | namespace os { |
| 34 | namespace statsd { |
| 35 | |
| Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 36 | using android::util::ProtoOutputStream; |
| 37 | |
| David Chen | d9269e2 | 2017-12-05 13:43:51 -0800 | [diff] [blame] | 38 | #ifdef __ANDROID__ |
| 39 | |
| 40 | /** |
| 41 | * Mock MetricsManager (ByteSize() is called). |
| 42 | */ |
| 43 | class MockMetricsManager : public MetricsManager { |
| 44 | public: |
| Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 45 | 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 Chen | d9269e2 | 2017-12-05 13:43:51 -0800 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | MOCK_METHOD0(byteSize, size_t()); |
| Yao Chen | 06dba5d | 2018-01-26 13:38:16 -0800 | [diff] [blame] | 55 | |
| 56 | MOCK_METHOD1(dropData, void(const uint64_t dropTimeNs)); |
| David Chen | d9269e2 | 2017-12-05 13:43:51 -0800 | [diff] [blame] | 57 | }; |
| 58 | |
| 59 | TEST(StatsLogProcessorTest, TestRateLimitByteSize) { |
| 60 | sp<UidMap> m = new UidMap(); |
| Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 61 | sp<AlarmMonitor> anomalyAlarmMonitor; |
| 62 | sp<AlarmMonitor> periodicAlarmMonitor; |
| David Chen | d9269e2 | 2017-12-05 13:43:51 -0800 | [diff] [blame] | 63 | // Construct the processor with a dummy sendBroadcast function that does nothing. |
| Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 64 | StatsLogProcessor p(m, anomalyAlarmMonitor, periodicAlarmMonitor, 0, |
| 65 | [](const ConfigKey& key) {}); |
| David Chen | d9269e2 | 2017-12-05 13:43:51 -0800 | [diff] [blame] | 66 | |
| 67 | MockMetricsManager mockMetricsManager; |
| 68 | |
| Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 69 | ConfigKey key(100, 12345); |
| David Chen | d9269e2 | 2017-12-05 13:43:51 -0800 | [diff] [blame] | 70 | // 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-mac | b0d0628 | 2018-01-05 15:44:07 -0800 | [diff] [blame] | 73 | p.flushIfNecessaryLocked(99, key, mockMetricsManager); |
| 74 | p.flushIfNecessaryLocked(100, key, mockMetricsManager); |
| 75 | p.flushIfNecessaryLocked(101, key, mockMetricsManager); |
| David Chen | d9269e2 | 2017-12-05 13:43:51 -0800 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | TEST(StatsLogProcessorTest, TestRateLimitBroadcast) { |
| 79 | sp<UidMap> m = new UidMap(); |
| Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 80 | sp<AlarmMonitor> anomalyAlarmMonitor; |
| 81 | sp<AlarmMonitor> subscriberAlarmMonitor; |
| David Chen | d9269e2 | 2017-12-05 13:43:51 -0800 | [diff] [blame] | 82 | int broadcastCount = 0; |
| Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 83 | StatsLogProcessor p(m, anomalyAlarmMonitor, subscriberAlarmMonitor, 0, |
| 84 | [&broadcastCount](const ConfigKey& key) { broadcastCount++; }); |
| David Chen | d9269e2 | 2017-12-05 13:43:51 -0800 | [diff] [blame] | 85 | |
| 86 | MockMetricsManager mockMetricsManager; |
| 87 | |
| Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 88 | ConfigKey key(100, 12345); |
| David Chen | d9269e2 | 2017-12-05 13:43:51 -0800 | [diff] [blame] | 89 | EXPECT_CALL(mockMetricsManager, byteSize()) |
| Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 90 | .Times(1) |
| David Chen | d9269e2 | 2017-12-05 13:43:51 -0800 | [diff] [blame] | 91 | .WillRepeatedly(Return(int(StatsdStats::kMaxMetricsBytesPerConfig * .95))); |
| 92 | |
| 93 | // 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] | 94 | p.flushIfNecessaryLocked(1, key, mockMetricsManager); |
| David Chen | d9269e2 | 2017-12-05 13:43:51 -0800 | [diff] [blame] | 95 | EXPECT_EQ(1, broadcastCount); |
| 96 | |
| Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 97 | // b/73089712 |
| David Chen | d9269e2 | 2017-12-05 13:43:51 -0800 | [diff] [blame] | 98 | // This next call to flush should not trigger a broadcast. |
| Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 99 | // p.mLastByteSizeTimes.clear(); // Force another check for byte size. |
| 100 | // p.flushIfNecessaryLocked(2, key, mockMetricsManager); |
| 101 | // EXPECT_EQ(1, broadcastCount); |
| David Chen | d9269e2 | 2017-12-05 13:43:51 -0800 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | TEST(StatsLogProcessorTest, TestDropWhenByteSizeTooLarge) { |
| 105 | sp<UidMap> m = new UidMap(); |
| Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 106 | sp<AlarmMonitor> anomalyAlarmMonitor; |
| 107 | sp<AlarmMonitor> subscriberAlarmMonitor; |
| David Chen | d9269e2 | 2017-12-05 13:43:51 -0800 | [diff] [blame] | 108 | int broadcastCount = 0; |
| Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 109 | StatsLogProcessor p(m, anomalyAlarmMonitor, subscriberAlarmMonitor, 0, |
| David Chen | d9269e2 | 2017-12-05 13:43:51 -0800 | [diff] [blame] | 110 | [&broadcastCount](const ConfigKey& key) { broadcastCount++; }); |
| 111 | |
| 112 | MockMetricsManager mockMetricsManager; |
| 113 | |
| Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 114 | ConfigKey key(100, 12345); |
| David Chen | d9269e2 | 2017-12-05 13:43:51 -0800 | [diff] [blame] | 115 | EXPECT_CALL(mockMetricsManager, byteSize()) |
| 116 | .Times(1) |
| 117 | .WillRepeatedly(Return(int(StatsdStats::kMaxMetricsBytesPerConfig * 1.2))); |
| 118 | |
| Yao Chen | 06dba5d | 2018-01-26 13:38:16 -0800 | [diff] [blame] | 119 | EXPECT_CALL(mockMetricsManager, dropData(_)).Times(1); |
| David Chen | d9269e2 | 2017-12-05 13:43:51 -0800 | [diff] [blame] | 120 | |
| 121 | // Expect to call the onDumpReport and skip the broadcast. |
| Yangster-mac | b0d0628 | 2018-01-05 15:44:07 -0800 | [diff] [blame] | 122 | p.flushIfNecessaryLocked(1, key, mockMetricsManager); |
| David Chen | d9269e2 | 2017-12-05 13:43:51 -0800 | [diff] [blame] | 123 | EXPECT_EQ(0, broadcastCount); |
| 124 | } |
| 125 | |
| David Chen | 35045cb | 2018-03-23 22:21:47 -0700 | [diff] [blame^] | 126 | TEST(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 Chen | d9269e2 | 2017-12-05 13:43:51 -0800 | [diff] [blame] | 152 | #else |
| 153 | GTEST_LOG_(INFO) << "This test does nothing.\n"; |
| 154 | #endif |
| 155 | |
| 156 | } // namespace statsd |
| 157 | } // namespace os |
| Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 158 | } // namespace android |