Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [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 | #define LOG_TAG "statsd_test" |
| 16 | |
| 17 | #include <gtest/gtest.h> |
| 18 | #include "../src/condition/ConditionTracker.h" |
| 19 | #include "../src/matchers/LogMatchingTracker.h" |
| 20 | #include "../src/metrics/CountMetricProducer.h" |
| 21 | #include "../src/metrics/MetricProducer.h" |
| 22 | #include "../src/metrics/metrics_manager_util.h" |
| 23 | |
| 24 | #include "frameworks/base/cmds/statsd/src/statsd_config.pb.h" |
| 25 | |
| 26 | #include <stdio.h> |
| 27 | #include <set> |
| 28 | #include <unordered_map> |
| 29 | #include <vector> |
| 30 | |
| 31 | using namespace android::os::statsd; |
| 32 | using android::sp; |
| 33 | using std::set; |
| 34 | using std::unordered_map; |
| 35 | using std::vector; |
| 36 | |
| 37 | #ifdef __ANDROID__ |
| 38 | |
| 39 | // TODO: ADD MORE TEST CASES. |
| 40 | |
| 41 | StatsdConfig buildGoodConfig() { |
| 42 | StatsdConfig config; |
| 43 | config.set_config_id(12345L); |
| 44 | |
| 45 | LogEntryMatcher* eventMatcher = config.add_log_entry_matcher(); |
| 46 | eventMatcher->set_name("SCREEN_IS_ON"); |
| 47 | |
| 48 | SimpleLogEntryMatcher* simpleLogEntryMatcher = eventMatcher->mutable_simple_log_entry_matcher(); |
| 49 | simpleLogEntryMatcher->add_tag(2 /*SCREEN_STATE_CHANGE*/); |
| 50 | simpleLogEntryMatcher->add_key_value_matcher()->mutable_key_matcher()->set_key( |
| 51 | 1 /*SCREEN_STATE_CHANGE__DISPLAY_STATE*/); |
| 52 | simpleLogEntryMatcher->mutable_key_value_matcher(0)->set_eq_int( |
| 53 | 2 /*SCREEN_STATE_CHANGE__DISPLAY_STATE__STATE_ON*/); |
| 54 | |
| 55 | eventMatcher = config.add_log_entry_matcher(); |
| 56 | eventMatcher->set_name("SCREEN_IS_OFF"); |
| 57 | |
| 58 | simpleLogEntryMatcher = eventMatcher->mutable_simple_log_entry_matcher(); |
| 59 | simpleLogEntryMatcher->add_tag(2 /*SCREEN_STATE_CHANGE*/); |
| 60 | simpleLogEntryMatcher->add_key_value_matcher()->mutable_key_matcher()->set_key( |
| 61 | 1 /*SCREEN_STATE_CHANGE__DISPLAY_STATE*/); |
| 62 | simpleLogEntryMatcher->mutable_key_value_matcher(0)->set_eq_int( |
| 63 | 1 /*SCREEN_STATE_CHANGE__DISPLAY_STATE__STATE_OFF*/); |
| 64 | |
| 65 | eventMatcher = config.add_log_entry_matcher(); |
| 66 | eventMatcher->set_name("SCREEN_ON_OR_OFF"); |
| 67 | |
| 68 | LogEntryMatcher_Combination* combination = eventMatcher->mutable_combination(); |
| 69 | combination->set_operation(LogicalOperation::OR); |
| 70 | combination->add_matcher("SCREEN_IS_ON"); |
| 71 | combination->add_matcher("SCREEN_IS_OFF"); |
| 72 | |
| 73 | return config; |
| 74 | } |
| 75 | |
| 76 | StatsdConfig buildCircleMatchers() { |
| 77 | StatsdConfig config; |
| 78 | config.set_config_id(12345L); |
| 79 | |
| 80 | LogEntryMatcher* eventMatcher = config.add_log_entry_matcher(); |
| 81 | eventMatcher->set_name("SCREEN_IS_ON"); |
| 82 | |
| 83 | SimpleLogEntryMatcher* simpleLogEntryMatcher = eventMatcher->mutable_simple_log_entry_matcher(); |
| 84 | simpleLogEntryMatcher->add_tag(2 /*SCREEN_STATE_CHANGE*/); |
| 85 | simpleLogEntryMatcher->add_key_value_matcher()->mutable_key_matcher()->set_key( |
| 86 | 1 /*SCREEN_STATE_CHANGE__DISPLAY_STATE*/); |
| 87 | simpleLogEntryMatcher->mutable_key_value_matcher(0)->set_eq_int( |
| 88 | 2 /*SCREEN_STATE_CHANGE__DISPLAY_STATE__STATE_ON*/); |
| 89 | |
| 90 | eventMatcher = config.add_log_entry_matcher(); |
| 91 | eventMatcher->set_name("SCREEN_ON_OR_OFF"); |
| 92 | |
| 93 | LogEntryMatcher_Combination* combination = eventMatcher->mutable_combination(); |
| 94 | combination->set_operation(LogicalOperation::OR); |
| 95 | combination->add_matcher("SCREEN_IS_ON"); |
| 96 | // Circle dependency |
| 97 | combination->add_matcher("SCREEN_ON_OR_OFF"); |
| 98 | |
| 99 | return config; |
| 100 | } |
| 101 | |
| 102 | StatsdConfig buildMissingMatchers() { |
| 103 | StatsdConfig config; |
| 104 | config.set_config_id(12345L); |
| 105 | |
| 106 | LogEntryMatcher* eventMatcher = config.add_log_entry_matcher(); |
| 107 | eventMatcher->set_name("SCREEN_IS_ON"); |
| 108 | |
| 109 | SimpleLogEntryMatcher* simpleLogEntryMatcher = eventMatcher->mutable_simple_log_entry_matcher(); |
| 110 | simpleLogEntryMatcher->add_tag(2 /*SCREEN_STATE_CHANGE*/); |
| 111 | simpleLogEntryMatcher->add_key_value_matcher()->mutable_key_matcher()->set_key( |
| 112 | 1 /*SCREEN_STATE_CHANGE__DISPLAY_STATE*/); |
| 113 | simpleLogEntryMatcher->mutable_key_value_matcher(0)->set_eq_int( |
| 114 | 2 /*SCREEN_STATE_CHANGE__DISPLAY_STATE__STATE_ON*/); |
| 115 | |
| 116 | eventMatcher = config.add_log_entry_matcher(); |
| 117 | eventMatcher->set_name("SCREEN_ON_OR_OFF"); |
| 118 | |
| 119 | LogEntryMatcher_Combination* combination = eventMatcher->mutable_combination(); |
| 120 | combination->set_operation(LogicalOperation::OR); |
| 121 | combination->add_matcher("SCREEN_IS_ON"); |
| 122 | // undefined matcher |
| 123 | combination->add_matcher("ABC"); |
| 124 | |
| 125 | return config; |
| 126 | } |
| 127 | |
| 128 | StatsdConfig buildCircleConditions() { |
| 129 | StatsdConfig config; |
| 130 | config.set_config_id(12345L); |
| 131 | |
| 132 | LogEntryMatcher* eventMatcher = config.add_log_entry_matcher(); |
| 133 | eventMatcher->set_name("SCREEN_IS_ON"); |
| 134 | |
| 135 | SimpleLogEntryMatcher* simpleLogEntryMatcher = eventMatcher->mutable_simple_log_entry_matcher(); |
| 136 | simpleLogEntryMatcher->add_tag(2 /*SCREEN_STATE_CHANGE*/); |
| 137 | simpleLogEntryMatcher->add_key_value_matcher()->mutable_key_matcher()->set_key( |
| 138 | 1 /*SCREEN_STATE_CHANGE__DISPLAY_STATE*/); |
| 139 | simpleLogEntryMatcher->mutable_key_value_matcher(0)->set_eq_int( |
| 140 | 2 /*SCREEN_STATE_CHANGE__DISPLAY_STATE__STATE_ON*/); |
| 141 | |
| 142 | eventMatcher = config.add_log_entry_matcher(); |
| 143 | eventMatcher->set_name("SCREEN_IS_OFF"); |
| 144 | |
| 145 | simpleLogEntryMatcher = eventMatcher->mutable_simple_log_entry_matcher(); |
| 146 | simpleLogEntryMatcher->add_tag(2 /*SCREEN_STATE_CHANGE*/); |
| 147 | simpleLogEntryMatcher->add_key_value_matcher()->mutable_key_matcher()->set_key( |
| 148 | 1 /*SCREEN_STATE_CHANGE__DISPLAY_STATE*/); |
| 149 | simpleLogEntryMatcher->mutable_key_value_matcher(0)->set_eq_int( |
| 150 | 1 /*SCREEN_STATE_CHANGE__DISPLAY_STATE__STATE_OFF*/); |
| 151 | |
| 152 | Condition* condition = config.add_condition(); |
| 153 | condition->set_name("SCREEN_IS_ON"); |
| 154 | SimpleCondition* simpleCondition = condition->mutable_simple_condition(); |
| 155 | simpleCondition->set_start("SCREEN_IS_ON"); |
| 156 | simpleCondition->set_stop("SCREEN_IS_OFF"); |
| 157 | |
| 158 | condition = config.add_condition(); |
| 159 | condition->set_name("SCREEN_IS_EITHER_ON_OFF"); |
| 160 | |
| 161 | Condition_Combination* combination = condition->mutable_combination(); |
| 162 | combination->set_operation(LogicalOperation::OR); |
| 163 | combination->add_condition("SCREEN_IS_ON"); |
| 164 | combination->add_condition("SCREEN_IS_EITHER_ON_OFF"); |
| 165 | |
| 166 | return config; |
| 167 | } |
| 168 | |
| 169 | TEST(MetricsManagerTest, TestGoodConfig) { |
| 170 | StatsdConfig config = buildGoodConfig(); |
| 171 | set<int> allTagIds; |
| 172 | vector<sp<LogMatchingTracker>> allLogEntryMatchers; |
| 173 | vector<sp<ConditionTracker>> allConditionTrackers; |
| 174 | vector<sp<MetricProducer>> allMetricProducers; |
| 175 | unordered_map<int, std::vector<int>> conditionToMetricMap; |
| 176 | unordered_map<int, std::vector<int>> trackerToMetricMap; |
| 177 | unordered_map<int, std::vector<int>> trackerToConditionMap; |
| 178 | |
| 179 | EXPECT_TRUE(initStatsdConfig(config, allTagIds, allLogEntryMatchers, allConditionTrackers, |
| 180 | allMetricProducers, conditionToMetricMap, trackerToMetricMap, |
| 181 | trackerToConditionMap)); |
| 182 | } |
| 183 | |
| 184 | TEST(MetricsManagerTest, TestCircleLogMatcherDependency) { |
| 185 | StatsdConfig config = buildCircleMatchers(); |
| 186 | set<int> allTagIds; |
| 187 | vector<sp<LogMatchingTracker>> allLogEntryMatchers; |
| 188 | vector<sp<ConditionTracker>> allConditionTrackers; |
| 189 | vector<sp<MetricProducer>> allMetricProducers; |
| 190 | unordered_map<int, std::vector<int>> conditionToMetricMap; |
| 191 | unordered_map<int, std::vector<int>> trackerToMetricMap; |
| 192 | unordered_map<int, std::vector<int>> trackerToConditionMap; |
| 193 | |
| 194 | EXPECT_FALSE(initStatsdConfig(config, allTagIds, allLogEntryMatchers, allConditionTrackers, |
| 195 | allMetricProducers, conditionToMetricMap, trackerToMetricMap, |
| 196 | trackerToConditionMap)); |
| 197 | } |
| 198 | |
| 199 | TEST(MetricsManagerTest, TestMissingMatchers) { |
| 200 | StatsdConfig config = buildMissingMatchers(); |
| 201 | set<int> allTagIds; |
| 202 | vector<sp<LogMatchingTracker>> allLogEntryMatchers; |
| 203 | vector<sp<ConditionTracker>> allConditionTrackers; |
| 204 | vector<sp<MetricProducer>> allMetricProducers; |
| 205 | unordered_map<int, std::vector<int>> conditionToMetricMap; |
| 206 | unordered_map<int, std::vector<int>> trackerToMetricMap; |
| 207 | unordered_map<int, std::vector<int>> trackerToConditionMap; |
| 208 | |
| 209 | EXPECT_FALSE(initStatsdConfig(config, allTagIds, allLogEntryMatchers, allConditionTrackers, |
| 210 | allMetricProducers, conditionToMetricMap, trackerToMetricMap, |
| 211 | trackerToConditionMap)); |
| 212 | } |
| 213 | |
| 214 | TEST(MetricsManagerTest, TestCircleConditionDependency) { |
| 215 | StatsdConfig config = buildCircleConditions(); |
| 216 | set<int> allTagIds; |
| 217 | vector<sp<LogMatchingTracker>> allLogEntryMatchers; |
| 218 | vector<sp<ConditionTracker>> allConditionTrackers; |
| 219 | vector<sp<MetricProducer>> allMetricProducers; |
| 220 | unordered_map<int, std::vector<int>> conditionToMetricMap; |
| 221 | unordered_map<int, std::vector<int>> trackerToMetricMap; |
| 222 | unordered_map<int, std::vector<int>> trackerToConditionMap; |
| 223 | |
| 224 | EXPECT_FALSE(initStatsdConfig(config, allTagIds, allLogEntryMatchers, allConditionTrackers, |
| 225 | allMetricProducers, conditionToMetricMap, trackerToMetricMap, |
| 226 | trackerToConditionMap)); |
| 227 | } |
| 228 | |
| 229 | #else |
| 230 | GTEST_LOG_(INFO) << "This test does nothing.\n"; |
| 231 | #endif |