blob: 3bc98624ff7569d1cceb6056e2053bee513705c9 [file] [log] [blame]
David Chendd896942017-09-26 11:44:40 -07001// 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
Joe Onorato9fc9edf2017-10-15 20:08:52 -070015#include "frameworks/base/cmds/statsd/src/statsd_config.pb.h"
16#include "matchers/matcher_util.h"
17#include "stats_util.h"
David Chendd896942017-09-26 11:44:40 -070018
Yao Chen44cf27c2017-09-14 22:32:50 -070019#include <gtest/gtest.h>
20#include <log/log_event_list.h>
21#include <log/log_read.h>
22#include <log/logprint.h>
David Chendd896942017-09-26 11:44:40 -070023
24#include <stdio.h>
25
26using namespace android::os::statsd;
27using std::unordered_map;
Yao Chencaf339d2017-10-06 16:01:10 -070028using std::vector;
David Chendd896942017-09-26 11:44:40 -070029
Stefan Lafoncdb1a0e2017-09-27 20:24:15 -070030const int kTagIdWakelock = 123;
31const int kKeyIdState = 45;
32const int kKeyIdPackageVersion = 67;
33
David Chendd896942017-09-26 11:44:40 -070034#ifdef __ANDROID__
35TEST(LogEntryMatcherTest, TestSimpleMatcher) {
36 // Set up the matcher
37 LogEntryMatcher matcher;
38 auto simpleMatcher = matcher.mutable_simple_log_entry_matcher();
Stefan Lafoncdb1a0e2017-09-27 20:24:15 -070039 simpleMatcher->add_tag(kTagIdWakelock);
David Chendd896942017-09-26 11:44:40 -070040
Yao Chen44cf27c2017-09-14 22:32:50 -070041 LogEventWrapper wrapper;
42 wrapper.tagId = kTagIdWakelock;
David Chendd896942017-09-26 11:44:40 -070043
Yao Chencaf339d2017-10-06 16:01:10 -070044 EXPECT_TRUE(matchesSimple(*simpleMatcher, wrapper));
David Chendd896942017-09-26 11:44:40 -070045}
46
47TEST(LogEntryMatcherTest, TestBoolMatcher) {
48 // Set up the matcher
49 LogEntryMatcher matcher;
50 auto simpleMatcher = matcher.mutable_simple_log_entry_matcher();
Stefan Lafoncdb1a0e2017-09-27 20:24:15 -070051 simpleMatcher->add_tag(kTagIdWakelock);
David Chendd896942017-09-26 11:44:40 -070052 auto keyValue = simpleMatcher->add_key_value_matcher();
Stefan Lafoncdb1a0e2017-09-27 20:24:15 -070053 keyValue->mutable_key_matcher()->set_key(kKeyIdState);
David Chendd896942017-09-26 11:44:40 -070054
Yao Chen44cf27c2017-09-14 22:32:50 -070055 LogEventWrapper wrapper;
56 wrapper.tagId = kTagIdWakelock;
David Chendd896942017-09-26 11:44:40 -070057
58 keyValue->set_eq_bool(true);
Yao Chen44cf27c2017-09-14 22:32:50 -070059 wrapper.boolMap[kKeyIdState] = true;
Yao Chencaf339d2017-10-06 16:01:10 -070060 EXPECT_TRUE(matchesSimple(*simpleMatcher, wrapper));
David Chendd896942017-09-26 11:44:40 -070061
62 keyValue->set_eq_bool(false);
Yao Chencaf339d2017-10-06 16:01:10 -070063 EXPECT_FALSE(matchesSimple(*simpleMatcher, wrapper));
David Chendd896942017-09-26 11:44:40 -070064
Yao Chencaf339d2017-10-06 16:01:10 -070065 wrapper.boolMap[kKeyIdState] = false;
66 EXPECT_TRUE(matchesSimple(*simpleMatcher, wrapper));
David Chendd896942017-09-26 11:44:40 -070067}
68
69TEST(LogEntryMatcherTest, TestStringMatcher) {
70 // Set up the matcher
71 LogEntryMatcher matcher;
72 auto simpleMatcher = matcher.mutable_simple_log_entry_matcher();
Stefan Lafoncdb1a0e2017-09-27 20:24:15 -070073 simpleMatcher->add_tag(kTagIdWakelock);
David Chendd896942017-09-26 11:44:40 -070074 auto keyValue = simpleMatcher->add_key_value_matcher();
Stefan Lafoncdb1a0e2017-09-27 20:24:15 -070075 keyValue->mutable_key_matcher()->set_key(kKeyIdState);
David Chendd896942017-09-26 11:44:40 -070076 keyValue->set_eq_string("wakelock_name");
77
Yao Chen44cf27c2017-09-14 22:32:50 -070078 LogEventWrapper wrapper;
79 wrapper.tagId = kTagIdWakelock;
David Chendd896942017-09-26 11:44:40 -070080
Yao Chen44cf27c2017-09-14 22:32:50 -070081 wrapper.strMap[kKeyIdState] = "wakelock_name";
David Chendd896942017-09-26 11:44:40 -070082
Yao Chencaf339d2017-10-06 16:01:10 -070083 EXPECT_TRUE(matchesSimple(*simpleMatcher, wrapper));
David Chendd896942017-09-26 11:44:40 -070084}
85
86TEST(LogEntryMatcherTest, TestIntComparisonMatcher) {
87 // Set up the matcher
88 LogEntryMatcher matcher;
89 auto simpleMatcher = matcher.mutable_simple_log_entry_matcher();
Stefan Lafoncdb1a0e2017-09-27 20:24:15 -070090 simpleMatcher->add_tag(kTagIdWakelock);
David Chendd896942017-09-26 11:44:40 -070091 auto keyValue = simpleMatcher->add_key_value_matcher();
Stefan Lafoncdb1a0e2017-09-27 20:24:15 -070092 keyValue->mutable_key_matcher()->set_key(kKeyIdState);
David Chendd896942017-09-26 11:44:40 -070093
Yao Chen44cf27c2017-09-14 22:32:50 -070094 LogEventWrapper wrapper;
95 wrapper.tagId = kTagIdWakelock;
David Chendd896942017-09-26 11:44:40 -070096
97 keyValue->set_lt_int(10);
Yao Chen44cf27c2017-09-14 22:32:50 -070098 wrapper.intMap[kKeyIdState] = 11;
Yao Chencaf339d2017-10-06 16:01:10 -070099 EXPECT_FALSE(matchesSimple(*simpleMatcher, wrapper));
Yao Chen44cf27c2017-09-14 22:32:50 -0700100 wrapper.intMap[kKeyIdState] = 10;
Yao Chencaf339d2017-10-06 16:01:10 -0700101 EXPECT_FALSE(matchesSimple(*simpleMatcher, wrapper));
Yao Chen44cf27c2017-09-14 22:32:50 -0700102 wrapper.intMap[kKeyIdState] = 9;
Yao Chencaf339d2017-10-06 16:01:10 -0700103 EXPECT_TRUE(matchesSimple(*simpleMatcher, wrapper));
David Chendd896942017-09-26 11:44:40 -0700104
105 keyValue->set_gt_int(10);
Yao Chen44cf27c2017-09-14 22:32:50 -0700106 wrapper.intMap[kKeyIdState] = 11;
Yao Chencaf339d2017-10-06 16:01:10 -0700107 EXPECT_TRUE(matchesSimple(*simpleMatcher, wrapper));
Yao Chen44cf27c2017-09-14 22:32:50 -0700108 wrapper.intMap[kKeyIdState] = 10;
Yao Chencaf339d2017-10-06 16:01:10 -0700109 EXPECT_FALSE(matchesSimple(*simpleMatcher, wrapper));
Yao Chen44cf27c2017-09-14 22:32:50 -0700110 wrapper.intMap[kKeyIdState] = 9;
Yao Chencaf339d2017-10-06 16:01:10 -0700111 EXPECT_FALSE(matchesSimple(*simpleMatcher, wrapper));
David Chendd896942017-09-26 11:44:40 -0700112}
113
114TEST(LogEntryMatcherTest, TestIntWithEqualityComparisonMatcher) {
115 // Set up the matcher
116 LogEntryMatcher matcher;
117 auto simpleMatcher = matcher.mutable_simple_log_entry_matcher();
Stefan Lafoncdb1a0e2017-09-27 20:24:15 -0700118 simpleMatcher->add_tag(kTagIdWakelock);
David Chendd896942017-09-26 11:44:40 -0700119 auto keyValue = simpleMatcher->add_key_value_matcher();
Stefan Lafoncdb1a0e2017-09-27 20:24:15 -0700120 keyValue->mutable_key_matcher()->set_key(kKeyIdState);
David Chendd896942017-09-26 11:44:40 -0700121
Yao Chen44cf27c2017-09-14 22:32:50 -0700122 LogEventWrapper wrapper;
123 wrapper.tagId = kTagIdWakelock;
David Chendd896942017-09-26 11:44:40 -0700124
125 keyValue->set_lte_int(10);
Yao Chen44cf27c2017-09-14 22:32:50 -0700126 wrapper.intMap[kKeyIdState] = 11;
Yao Chencaf339d2017-10-06 16:01:10 -0700127 EXPECT_FALSE(matchesSimple(*simpleMatcher, wrapper));
Yao Chen44cf27c2017-09-14 22:32:50 -0700128 wrapper.intMap[kKeyIdState] = 10;
Yao Chencaf339d2017-10-06 16:01:10 -0700129 EXPECT_TRUE(matchesSimple(*simpleMatcher, wrapper));
Yao Chen44cf27c2017-09-14 22:32:50 -0700130 wrapper.intMap[kKeyIdState] = 9;
Yao Chencaf339d2017-10-06 16:01:10 -0700131 EXPECT_TRUE(matchesSimple(*simpleMatcher, wrapper));
David Chendd896942017-09-26 11:44:40 -0700132
133 keyValue->set_gte_int(10);
Yao Chen44cf27c2017-09-14 22:32:50 -0700134 wrapper.intMap[kKeyIdState] = 11;
Yao Chencaf339d2017-10-06 16:01:10 -0700135 EXPECT_TRUE(matchesSimple(*simpleMatcher, wrapper));
Yao Chen44cf27c2017-09-14 22:32:50 -0700136 wrapper.intMap[kKeyIdState] = 10;
Yao Chencaf339d2017-10-06 16:01:10 -0700137 EXPECT_TRUE(matchesSimple(*simpleMatcher, wrapper));
Yao Chen44cf27c2017-09-14 22:32:50 -0700138 wrapper.intMap[kKeyIdState] = 9;
Yao Chencaf339d2017-10-06 16:01:10 -0700139 EXPECT_FALSE(matchesSimple(*simpleMatcher, wrapper));
David Chendd896942017-09-26 11:44:40 -0700140}
141
142TEST(LogEntryMatcherTest, TestFloatComparisonMatcher) {
143 // Set up the matcher
144 LogEntryMatcher matcher;
145 auto simpleMatcher = matcher.mutable_simple_log_entry_matcher();
Stefan Lafoncdb1a0e2017-09-27 20:24:15 -0700146 simpleMatcher->add_tag(kTagIdWakelock);
David Chendd896942017-09-26 11:44:40 -0700147 auto keyValue = simpleMatcher->add_key_value_matcher();
Stefan Lafoncdb1a0e2017-09-27 20:24:15 -0700148 keyValue->mutable_key_matcher()->set_key(kKeyIdState);
David Chendd896942017-09-26 11:44:40 -0700149
Yao Chen44cf27c2017-09-14 22:32:50 -0700150 LogEventWrapper wrapper;
151 wrapper.tagId = kTagIdWakelock;
David Chendd896942017-09-26 11:44:40 -0700152
153 keyValue->set_lt_float(10.0);
Yao Chen44cf27c2017-09-14 22:32:50 -0700154 wrapper.floatMap[kKeyIdState] = 10.1;
Yao Chencaf339d2017-10-06 16:01:10 -0700155 EXPECT_FALSE(matchesSimple(*simpleMatcher, wrapper));
Yao Chen44cf27c2017-09-14 22:32:50 -0700156 wrapper.floatMap[kKeyIdState] = 9.9;
Yao Chencaf339d2017-10-06 16:01:10 -0700157 EXPECT_TRUE(matchesSimple(*simpleMatcher, wrapper));
David Chendd896942017-09-26 11:44:40 -0700158
159 keyValue->set_gt_float(10.0);
Yao Chen44cf27c2017-09-14 22:32:50 -0700160 wrapper.floatMap[kKeyIdState] = 10.1;
Yao Chencaf339d2017-10-06 16:01:10 -0700161 EXPECT_TRUE(matchesSimple(*simpleMatcher, wrapper));
Yao Chen44cf27c2017-09-14 22:32:50 -0700162 wrapper.floatMap[kKeyIdState] = 9.9;
Yao Chencaf339d2017-10-06 16:01:10 -0700163 EXPECT_FALSE(matchesSimple(*simpleMatcher, wrapper));
David Chendd896942017-09-26 11:44:40 -0700164}
165
166// Helper for the composite matchers.
Stefan Lafoncdb1a0e2017-09-27 20:24:15 -0700167void addSimpleMatcher(SimpleLogEntryMatcher* simpleMatcher, int tag, int key, int val) {
David Chendd896942017-09-26 11:44:40 -0700168 simpleMatcher->add_tag(tag);
169 auto keyValue = simpleMatcher->add_key_value_matcher();
170 keyValue->mutable_key_matcher()->set_key(key);
171 keyValue->set_eq_int(val);
172}
173
174TEST(LogEntryMatcherTest, TestAndMatcher) {
175 // Set up the matcher
Yao Chencaf339d2017-10-06 16:01:10 -0700176 LogicalOperation operation = LogicalOperation::AND;
David Chendd896942017-09-26 11:44:40 -0700177
Yao Chencaf339d2017-10-06 16:01:10 -0700178 vector<int> children;
179 children.push_back(0);
180 children.push_back(1);
181 children.push_back(2);
David Chendd896942017-09-26 11:44:40 -0700182
Yao Chencaf339d2017-10-06 16:01:10 -0700183 vector<MatchingState> matcherResults;
184 matcherResults.push_back(MatchingState::kMatched);
185 matcherResults.push_back(MatchingState::kNotMatched);
186 matcherResults.push_back(MatchingState::kMatched);
David Chendd896942017-09-26 11:44:40 -0700187
Yao Chencaf339d2017-10-06 16:01:10 -0700188 EXPECT_FALSE(combinationMatch(children, operation, matcherResults));
189
190 matcherResults.clear();
191 matcherResults.push_back(MatchingState::kMatched);
192 matcherResults.push_back(MatchingState::kMatched);
193 matcherResults.push_back(MatchingState::kMatched);
194
195 EXPECT_TRUE(combinationMatch(children, operation, matcherResults));
David Chendd896942017-09-26 11:44:40 -0700196}
197
198TEST(LogEntryMatcherTest, TestOrMatcher) {
199 // Set up the matcher
Yao Chencaf339d2017-10-06 16:01:10 -0700200 LogicalOperation operation = LogicalOperation::OR;
David Chendd896942017-09-26 11:44:40 -0700201
Yao Chencaf339d2017-10-06 16:01:10 -0700202 vector<int> children;
203 children.push_back(0);
204 children.push_back(1);
205 children.push_back(2);
David Chendd896942017-09-26 11:44:40 -0700206
Yao Chencaf339d2017-10-06 16:01:10 -0700207 vector<MatchingState> matcherResults;
208 matcherResults.push_back(MatchingState::kMatched);
209 matcherResults.push_back(MatchingState::kNotMatched);
210 matcherResults.push_back(MatchingState::kMatched);
David Chendd896942017-09-26 11:44:40 -0700211
Yao Chencaf339d2017-10-06 16:01:10 -0700212 EXPECT_TRUE(combinationMatch(children, operation, matcherResults));
213
214 matcherResults.clear();
215 matcherResults.push_back(MatchingState::kNotMatched);
216 matcherResults.push_back(MatchingState::kNotMatched);
217 matcherResults.push_back(MatchingState::kNotMatched);
218
219 EXPECT_FALSE(combinationMatch(children, operation, matcherResults));
David Chendd896942017-09-26 11:44:40 -0700220}
221
222TEST(LogEntryMatcherTest, TestNotMatcher) {
223 // Set up the matcher
Yao Chencaf339d2017-10-06 16:01:10 -0700224 LogicalOperation operation = LogicalOperation::NOT;
David Chendd896942017-09-26 11:44:40 -0700225
Yao Chencaf339d2017-10-06 16:01:10 -0700226 vector<int> children;
227 children.push_back(0);
David Chendd896942017-09-26 11:44:40 -0700228
Yao Chencaf339d2017-10-06 16:01:10 -0700229 vector<MatchingState> matcherResults;
230 matcherResults.push_back(MatchingState::kMatched);
David Chendd896942017-09-26 11:44:40 -0700231
Yao Chencaf339d2017-10-06 16:01:10 -0700232 EXPECT_FALSE(combinationMatch(children, operation, matcherResults));
233
234 matcherResults.clear();
235 matcherResults.push_back(MatchingState::kNotMatched);
236 EXPECT_TRUE(combinationMatch(children, operation, matcherResults));
David Chendd896942017-09-26 11:44:40 -0700237}
238
Yao Chencaf339d2017-10-06 16:01:10 -0700239TEST(LogEntryMatcherTest, TestNandMatcher) {
David Chendd896942017-09-26 11:44:40 -0700240 // Set up the matcher
Yao Chencaf339d2017-10-06 16:01:10 -0700241 LogicalOperation operation = LogicalOperation::NAND;
David Chendd896942017-09-26 11:44:40 -0700242
Yao Chencaf339d2017-10-06 16:01:10 -0700243 vector<int> children;
244 children.push_back(0);
245 children.push_back(1);
David Chendd896942017-09-26 11:44:40 -0700246
Yao Chencaf339d2017-10-06 16:01:10 -0700247 vector<MatchingState> matcherResults;
248 matcherResults.push_back(MatchingState::kMatched);
249 matcherResults.push_back(MatchingState::kNotMatched);
David Chendd896942017-09-26 11:44:40 -0700250
Yao Chencaf339d2017-10-06 16:01:10 -0700251 EXPECT_TRUE(combinationMatch(children, operation, matcherResults));
252
253 matcherResults.clear();
254 matcherResults.push_back(MatchingState::kNotMatched);
255 matcherResults.push_back(MatchingState::kNotMatched);
256 EXPECT_TRUE(combinationMatch(children, operation, matcherResults));
257
258 matcherResults.clear();
259 matcherResults.push_back(MatchingState::kMatched);
260 matcherResults.push_back(MatchingState::kMatched);
261 EXPECT_FALSE(combinationMatch(children, operation, matcherResults));
David Chendd896942017-09-26 11:44:40 -0700262}
263
Yao Chencaf339d2017-10-06 16:01:10 -0700264TEST(LogEntryMatcherTest, TestNorMatcher) {
David Chendd896942017-09-26 11:44:40 -0700265 // Set up the matcher
Yao Chencaf339d2017-10-06 16:01:10 -0700266 LogicalOperation operation = LogicalOperation::NOR;
David Chendd896942017-09-26 11:44:40 -0700267
Yao Chencaf339d2017-10-06 16:01:10 -0700268 vector<int> children;
269 children.push_back(0);
270 children.push_back(1);
David Chendd896942017-09-26 11:44:40 -0700271
Yao Chencaf339d2017-10-06 16:01:10 -0700272 vector<MatchingState> matcherResults;
273 matcherResults.push_back(MatchingState::kMatched);
274 matcherResults.push_back(MatchingState::kNotMatched);
David Chendd896942017-09-26 11:44:40 -0700275
Yao Chencaf339d2017-10-06 16:01:10 -0700276 EXPECT_FALSE(combinationMatch(children, operation, matcherResults));
David Chendd896942017-09-26 11:44:40 -0700277
Yao Chencaf339d2017-10-06 16:01:10 -0700278 matcherResults.clear();
279 matcherResults.push_back(MatchingState::kNotMatched);
280 matcherResults.push_back(MatchingState::kNotMatched);
281 EXPECT_TRUE(combinationMatch(children, operation, matcherResults));
David Chendd896942017-09-26 11:44:40 -0700282
Yao Chencaf339d2017-10-06 16:01:10 -0700283 matcherResults.clear();
284 matcherResults.push_back(MatchingState::kMatched);
285 matcherResults.push_back(MatchingState::kMatched);
286 EXPECT_FALSE(combinationMatch(children, operation, matcherResults));
David Chendd896942017-09-26 11:44:40 -0700287}
Stefan Lafoncdb1a0e2017-09-27 20:24:15 -0700288
David Chendd896942017-09-26 11:44:40 -0700289#else
Yao Chen44cf27c2017-09-14 22:32:50 -0700290GTEST_LOG_(INFO) << "This test does nothing.\n";
David Chendd896942017-09-26 11:44:40 -0700291#endif