blob: 606980178e2a683f481caa062823b86ec9b7a764 [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
15#define LOG_TAG "statsd_test"
16
Yao Chen44cf27c2017-09-14 22:32:50 -070017#include <gtest/gtest.h>
18#include <log/log_event_list.h>
19#include <log/log_read.h>
20#include <log/logprint.h>
Yao Chencaf339d2017-10-06 16:01:10 -070021#include "../src/matchers/matcher_util.h"
22#include "../src/stats_util.h"
Yao Chen44cf27c2017-09-14 22:32:50 -070023#include "frameworks/base/cmds/statsd/src/statsd_config.pb.h"
David Chendd896942017-09-26 11:44:40 -070024
25#include <stdio.h>
26
27using namespace android::os::statsd;
28using std::unordered_map;
Yao Chencaf339d2017-10-06 16:01:10 -070029using std::vector;
David Chendd896942017-09-26 11:44:40 -070030
Stefan Lafoncdb1a0e2017-09-27 20:24:15 -070031const int kTagIdWakelock = 123;
32const int kKeyIdState = 45;
33const int kKeyIdPackageVersion = 67;
34
David Chendd896942017-09-26 11:44:40 -070035#ifdef __ANDROID__
36TEST(LogEntryMatcherTest, TestSimpleMatcher) {
37 // Set up the matcher
38 LogEntryMatcher matcher;
39 auto simpleMatcher = matcher.mutable_simple_log_entry_matcher();
Stefan Lafoncdb1a0e2017-09-27 20:24:15 -070040 simpleMatcher->add_tag(kTagIdWakelock);
David Chendd896942017-09-26 11:44:40 -070041
Yao Chen44cf27c2017-09-14 22:32:50 -070042 LogEventWrapper wrapper;
43 wrapper.tagId = kTagIdWakelock;
David Chendd896942017-09-26 11:44:40 -070044
Yao Chencaf339d2017-10-06 16:01:10 -070045 EXPECT_TRUE(matchesSimple(*simpleMatcher, wrapper));
David Chendd896942017-09-26 11:44:40 -070046}
47
48TEST(LogEntryMatcherTest, TestBoolMatcher) {
49 // Set up the matcher
50 LogEntryMatcher matcher;
51 auto simpleMatcher = matcher.mutable_simple_log_entry_matcher();
Stefan Lafoncdb1a0e2017-09-27 20:24:15 -070052 simpleMatcher->add_tag(kTagIdWakelock);
David Chendd896942017-09-26 11:44:40 -070053 auto keyValue = simpleMatcher->add_key_value_matcher();
Stefan Lafoncdb1a0e2017-09-27 20:24:15 -070054 keyValue->mutable_key_matcher()->set_key(kKeyIdState);
David Chendd896942017-09-26 11:44:40 -070055
Yao Chen44cf27c2017-09-14 22:32:50 -070056 LogEventWrapper wrapper;
57 wrapper.tagId = kTagIdWakelock;
David Chendd896942017-09-26 11:44:40 -070058
59 keyValue->set_eq_bool(true);
Yao Chen44cf27c2017-09-14 22:32:50 -070060 wrapper.boolMap[kKeyIdState] = true;
Yao Chencaf339d2017-10-06 16:01:10 -070061 EXPECT_TRUE(matchesSimple(*simpleMatcher, wrapper));
David Chendd896942017-09-26 11:44:40 -070062
63 keyValue->set_eq_bool(false);
Yao Chencaf339d2017-10-06 16:01:10 -070064 EXPECT_FALSE(matchesSimple(*simpleMatcher, wrapper));
David Chendd896942017-09-26 11:44:40 -070065
Yao Chencaf339d2017-10-06 16:01:10 -070066 wrapper.boolMap[kKeyIdState] = false;
67 EXPECT_TRUE(matchesSimple(*simpleMatcher, wrapper));
David Chendd896942017-09-26 11:44:40 -070068}
69
70TEST(LogEntryMatcherTest, TestStringMatcher) {
71 // Set up the matcher
72 LogEntryMatcher matcher;
73 auto simpleMatcher = matcher.mutable_simple_log_entry_matcher();
Stefan Lafoncdb1a0e2017-09-27 20:24:15 -070074 simpleMatcher->add_tag(kTagIdWakelock);
David Chendd896942017-09-26 11:44:40 -070075 auto keyValue = simpleMatcher->add_key_value_matcher();
Stefan Lafoncdb1a0e2017-09-27 20:24:15 -070076 keyValue->mutable_key_matcher()->set_key(kKeyIdState);
David Chendd896942017-09-26 11:44:40 -070077 keyValue->set_eq_string("wakelock_name");
78
Yao Chen44cf27c2017-09-14 22:32:50 -070079 LogEventWrapper wrapper;
80 wrapper.tagId = kTagIdWakelock;
David Chendd896942017-09-26 11:44:40 -070081
Yao Chen44cf27c2017-09-14 22:32:50 -070082 wrapper.strMap[kKeyIdState] = "wakelock_name";
David Chendd896942017-09-26 11:44:40 -070083
Yao Chencaf339d2017-10-06 16:01:10 -070084 EXPECT_TRUE(matchesSimple(*simpleMatcher, wrapper));
David Chendd896942017-09-26 11:44:40 -070085}
86
87TEST(LogEntryMatcherTest, TestIntComparisonMatcher) {
88 // Set up the matcher
89 LogEntryMatcher matcher;
90 auto simpleMatcher = matcher.mutable_simple_log_entry_matcher();
Stefan Lafoncdb1a0e2017-09-27 20:24:15 -070091 simpleMatcher->add_tag(kTagIdWakelock);
David Chendd896942017-09-26 11:44:40 -070092 auto keyValue = simpleMatcher->add_key_value_matcher();
Stefan Lafoncdb1a0e2017-09-27 20:24:15 -070093 keyValue->mutable_key_matcher()->set_key(kKeyIdState);
David Chendd896942017-09-26 11:44:40 -070094
Yao Chen44cf27c2017-09-14 22:32:50 -070095 LogEventWrapper wrapper;
96 wrapper.tagId = kTagIdWakelock;
David Chendd896942017-09-26 11:44:40 -070097
98 keyValue->set_lt_int(10);
Yao Chen44cf27c2017-09-14 22:32:50 -070099 wrapper.intMap[kKeyIdState] = 11;
Yao Chencaf339d2017-10-06 16:01:10 -0700100 EXPECT_FALSE(matchesSimple(*simpleMatcher, wrapper));
Yao Chen44cf27c2017-09-14 22:32:50 -0700101 wrapper.intMap[kKeyIdState] = 10;
Yao Chencaf339d2017-10-06 16:01:10 -0700102 EXPECT_FALSE(matchesSimple(*simpleMatcher, wrapper));
Yao Chen44cf27c2017-09-14 22:32:50 -0700103 wrapper.intMap[kKeyIdState] = 9;
Yao Chencaf339d2017-10-06 16:01:10 -0700104 EXPECT_TRUE(matchesSimple(*simpleMatcher, wrapper));
David Chendd896942017-09-26 11:44:40 -0700105
106 keyValue->set_gt_int(10);
Yao Chen44cf27c2017-09-14 22:32:50 -0700107 wrapper.intMap[kKeyIdState] = 11;
Yao Chencaf339d2017-10-06 16:01:10 -0700108 EXPECT_TRUE(matchesSimple(*simpleMatcher, wrapper));
Yao Chen44cf27c2017-09-14 22:32:50 -0700109 wrapper.intMap[kKeyIdState] = 10;
Yao Chencaf339d2017-10-06 16:01:10 -0700110 EXPECT_FALSE(matchesSimple(*simpleMatcher, wrapper));
Yao Chen44cf27c2017-09-14 22:32:50 -0700111 wrapper.intMap[kKeyIdState] = 9;
Yao Chencaf339d2017-10-06 16:01:10 -0700112 EXPECT_FALSE(matchesSimple(*simpleMatcher, wrapper));
David Chendd896942017-09-26 11:44:40 -0700113}
114
115TEST(LogEntryMatcherTest, TestIntWithEqualityComparisonMatcher) {
116 // Set up the matcher
117 LogEntryMatcher matcher;
118 auto simpleMatcher = matcher.mutable_simple_log_entry_matcher();
Stefan Lafoncdb1a0e2017-09-27 20:24:15 -0700119 simpleMatcher->add_tag(kTagIdWakelock);
David Chendd896942017-09-26 11:44:40 -0700120 auto keyValue = simpleMatcher->add_key_value_matcher();
Stefan Lafoncdb1a0e2017-09-27 20:24:15 -0700121 keyValue->mutable_key_matcher()->set_key(kKeyIdState);
David Chendd896942017-09-26 11:44:40 -0700122
Yao Chen44cf27c2017-09-14 22:32:50 -0700123 LogEventWrapper wrapper;
124 wrapper.tagId = kTagIdWakelock;
David Chendd896942017-09-26 11:44:40 -0700125
126 keyValue->set_lte_int(10);
Yao Chen44cf27c2017-09-14 22:32:50 -0700127 wrapper.intMap[kKeyIdState] = 11;
Yao Chencaf339d2017-10-06 16:01:10 -0700128 EXPECT_FALSE(matchesSimple(*simpleMatcher, wrapper));
Yao Chen44cf27c2017-09-14 22:32:50 -0700129 wrapper.intMap[kKeyIdState] = 10;
Yao Chencaf339d2017-10-06 16:01:10 -0700130 EXPECT_TRUE(matchesSimple(*simpleMatcher, wrapper));
Yao Chen44cf27c2017-09-14 22:32:50 -0700131 wrapper.intMap[kKeyIdState] = 9;
Yao Chencaf339d2017-10-06 16:01:10 -0700132 EXPECT_TRUE(matchesSimple(*simpleMatcher, wrapper));
David Chendd896942017-09-26 11:44:40 -0700133
134 keyValue->set_gte_int(10);
Yao Chen44cf27c2017-09-14 22:32:50 -0700135 wrapper.intMap[kKeyIdState] = 11;
Yao Chencaf339d2017-10-06 16:01:10 -0700136 EXPECT_TRUE(matchesSimple(*simpleMatcher, wrapper));
Yao Chen44cf27c2017-09-14 22:32:50 -0700137 wrapper.intMap[kKeyIdState] = 10;
Yao Chencaf339d2017-10-06 16:01:10 -0700138 EXPECT_TRUE(matchesSimple(*simpleMatcher, wrapper));
Yao Chen44cf27c2017-09-14 22:32:50 -0700139 wrapper.intMap[kKeyIdState] = 9;
Yao Chencaf339d2017-10-06 16:01:10 -0700140 EXPECT_FALSE(matchesSimple(*simpleMatcher, wrapper));
David Chendd896942017-09-26 11:44:40 -0700141}
142
143TEST(LogEntryMatcherTest, TestFloatComparisonMatcher) {
144 // Set up the matcher
145 LogEntryMatcher matcher;
146 auto simpleMatcher = matcher.mutable_simple_log_entry_matcher();
Stefan Lafoncdb1a0e2017-09-27 20:24:15 -0700147 simpleMatcher->add_tag(kTagIdWakelock);
David Chendd896942017-09-26 11:44:40 -0700148 auto keyValue = simpleMatcher->add_key_value_matcher();
Stefan Lafoncdb1a0e2017-09-27 20:24:15 -0700149 keyValue->mutable_key_matcher()->set_key(kKeyIdState);
David Chendd896942017-09-26 11:44:40 -0700150
Yao Chen44cf27c2017-09-14 22:32:50 -0700151 LogEventWrapper wrapper;
152 wrapper.tagId = kTagIdWakelock;
David Chendd896942017-09-26 11:44:40 -0700153
154 keyValue->set_lt_float(10.0);
Yao Chen44cf27c2017-09-14 22:32:50 -0700155 wrapper.floatMap[kKeyIdState] = 10.1;
Yao Chencaf339d2017-10-06 16:01:10 -0700156 EXPECT_FALSE(matchesSimple(*simpleMatcher, wrapper));
Yao Chen44cf27c2017-09-14 22:32:50 -0700157 wrapper.floatMap[kKeyIdState] = 9.9;
Yao Chencaf339d2017-10-06 16:01:10 -0700158 EXPECT_TRUE(matchesSimple(*simpleMatcher, wrapper));
David Chendd896942017-09-26 11:44:40 -0700159
160 keyValue->set_gt_float(10.0);
Yao Chen44cf27c2017-09-14 22:32:50 -0700161 wrapper.floatMap[kKeyIdState] = 10.1;
Yao Chencaf339d2017-10-06 16:01:10 -0700162 EXPECT_TRUE(matchesSimple(*simpleMatcher, wrapper));
Yao Chen44cf27c2017-09-14 22:32:50 -0700163 wrapper.floatMap[kKeyIdState] = 9.9;
Yao Chencaf339d2017-10-06 16:01:10 -0700164 EXPECT_FALSE(matchesSimple(*simpleMatcher, wrapper));
David Chendd896942017-09-26 11:44:40 -0700165}
166
167// Helper for the composite matchers.
Stefan Lafoncdb1a0e2017-09-27 20:24:15 -0700168void addSimpleMatcher(SimpleLogEntryMatcher* simpleMatcher, int tag, int key, int val) {
David Chendd896942017-09-26 11:44:40 -0700169 simpleMatcher->add_tag(tag);
170 auto keyValue = simpleMatcher->add_key_value_matcher();
171 keyValue->mutable_key_matcher()->set_key(key);
172 keyValue->set_eq_int(val);
173}
174
175TEST(LogEntryMatcherTest, TestAndMatcher) {
176 // Set up the matcher
Yao Chencaf339d2017-10-06 16:01:10 -0700177 LogicalOperation operation = LogicalOperation::AND;
David Chendd896942017-09-26 11:44:40 -0700178
Yao Chencaf339d2017-10-06 16:01:10 -0700179 vector<int> children;
180 children.push_back(0);
181 children.push_back(1);
182 children.push_back(2);
David Chendd896942017-09-26 11:44:40 -0700183
Yao Chencaf339d2017-10-06 16:01:10 -0700184 vector<MatchingState> matcherResults;
185 matcherResults.push_back(MatchingState::kMatched);
186 matcherResults.push_back(MatchingState::kNotMatched);
187 matcherResults.push_back(MatchingState::kMatched);
David Chendd896942017-09-26 11:44:40 -0700188
Yao Chencaf339d2017-10-06 16:01:10 -0700189 EXPECT_FALSE(combinationMatch(children, operation, matcherResults));
190
191 matcherResults.clear();
192 matcherResults.push_back(MatchingState::kMatched);
193 matcherResults.push_back(MatchingState::kMatched);
194 matcherResults.push_back(MatchingState::kMatched);
195
196 EXPECT_TRUE(combinationMatch(children, operation, matcherResults));
David Chendd896942017-09-26 11:44:40 -0700197}
198
199TEST(LogEntryMatcherTest, TestOrMatcher) {
200 // Set up the matcher
Yao Chencaf339d2017-10-06 16:01:10 -0700201 LogicalOperation operation = LogicalOperation::OR;
David Chendd896942017-09-26 11:44:40 -0700202
Yao Chencaf339d2017-10-06 16:01:10 -0700203 vector<int> children;
204 children.push_back(0);
205 children.push_back(1);
206 children.push_back(2);
David Chendd896942017-09-26 11:44:40 -0700207
Yao Chencaf339d2017-10-06 16:01:10 -0700208 vector<MatchingState> matcherResults;
209 matcherResults.push_back(MatchingState::kMatched);
210 matcherResults.push_back(MatchingState::kNotMatched);
211 matcherResults.push_back(MatchingState::kMatched);
David Chendd896942017-09-26 11:44:40 -0700212
Yao Chencaf339d2017-10-06 16:01:10 -0700213 EXPECT_TRUE(combinationMatch(children, operation, matcherResults));
214
215 matcherResults.clear();
216 matcherResults.push_back(MatchingState::kNotMatched);
217 matcherResults.push_back(MatchingState::kNotMatched);
218 matcherResults.push_back(MatchingState::kNotMatched);
219
220 EXPECT_FALSE(combinationMatch(children, operation, matcherResults));
David Chendd896942017-09-26 11:44:40 -0700221}
222
223TEST(LogEntryMatcherTest, TestNotMatcher) {
224 // Set up the matcher
Yao Chencaf339d2017-10-06 16:01:10 -0700225 LogicalOperation operation = LogicalOperation::NOT;
David Chendd896942017-09-26 11:44:40 -0700226
Yao Chencaf339d2017-10-06 16:01:10 -0700227 vector<int> children;
228 children.push_back(0);
David Chendd896942017-09-26 11:44:40 -0700229
Yao Chencaf339d2017-10-06 16:01:10 -0700230 vector<MatchingState> matcherResults;
231 matcherResults.push_back(MatchingState::kMatched);
David Chendd896942017-09-26 11:44:40 -0700232
Yao Chencaf339d2017-10-06 16:01:10 -0700233 EXPECT_FALSE(combinationMatch(children, operation, matcherResults));
234
235 matcherResults.clear();
236 matcherResults.push_back(MatchingState::kNotMatched);
237 EXPECT_TRUE(combinationMatch(children, operation, matcherResults));
David Chendd896942017-09-26 11:44:40 -0700238}
239
Yao Chencaf339d2017-10-06 16:01:10 -0700240TEST(LogEntryMatcherTest, TestNandMatcher) {
David Chendd896942017-09-26 11:44:40 -0700241 // Set up the matcher
Yao Chencaf339d2017-10-06 16:01:10 -0700242 LogicalOperation operation = LogicalOperation::NAND;
David Chendd896942017-09-26 11:44:40 -0700243
Yao Chencaf339d2017-10-06 16:01:10 -0700244 vector<int> children;
245 children.push_back(0);
246 children.push_back(1);
David Chendd896942017-09-26 11:44:40 -0700247
Yao Chencaf339d2017-10-06 16:01:10 -0700248 vector<MatchingState> matcherResults;
249 matcherResults.push_back(MatchingState::kMatched);
250 matcherResults.push_back(MatchingState::kNotMatched);
David Chendd896942017-09-26 11:44:40 -0700251
Yao Chencaf339d2017-10-06 16:01:10 -0700252 EXPECT_TRUE(combinationMatch(children, operation, matcherResults));
253
254 matcherResults.clear();
255 matcherResults.push_back(MatchingState::kNotMatched);
256 matcherResults.push_back(MatchingState::kNotMatched);
257 EXPECT_TRUE(combinationMatch(children, operation, matcherResults));
258
259 matcherResults.clear();
260 matcherResults.push_back(MatchingState::kMatched);
261 matcherResults.push_back(MatchingState::kMatched);
262 EXPECT_FALSE(combinationMatch(children, operation, matcherResults));
David Chendd896942017-09-26 11:44:40 -0700263}
264
Yao Chencaf339d2017-10-06 16:01:10 -0700265TEST(LogEntryMatcherTest, TestNorMatcher) {
David Chendd896942017-09-26 11:44:40 -0700266 // Set up the matcher
Yao Chencaf339d2017-10-06 16:01:10 -0700267 LogicalOperation operation = LogicalOperation::NOR;
David Chendd896942017-09-26 11:44:40 -0700268
Yao Chencaf339d2017-10-06 16:01:10 -0700269 vector<int> children;
270 children.push_back(0);
271 children.push_back(1);
David Chendd896942017-09-26 11:44:40 -0700272
Yao Chencaf339d2017-10-06 16:01:10 -0700273 vector<MatchingState> matcherResults;
274 matcherResults.push_back(MatchingState::kMatched);
275 matcherResults.push_back(MatchingState::kNotMatched);
David Chendd896942017-09-26 11:44:40 -0700276
Yao Chencaf339d2017-10-06 16:01:10 -0700277 EXPECT_FALSE(combinationMatch(children, operation, matcherResults));
David Chendd896942017-09-26 11:44:40 -0700278
Yao Chencaf339d2017-10-06 16:01:10 -0700279 matcherResults.clear();
280 matcherResults.push_back(MatchingState::kNotMatched);
281 matcherResults.push_back(MatchingState::kNotMatched);
282 EXPECT_TRUE(combinationMatch(children, operation, matcherResults));
David Chendd896942017-09-26 11:44:40 -0700283
Yao Chencaf339d2017-10-06 16:01:10 -0700284 matcherResults.clear();
285 matcherResults.push_back(MatchingState::kMatched);
286 matcherResults.push_back(MatchingState::kMatched);
287 EXPECT_FALSE(combinationMatch(children, operation, matcherResults));
David Chendd896942017-09-26 11:44:40 -0700288}
Stefan Lafoncdb1a0e2017-09-27 20:24:15 -0700289
David Chendd896942017-09-26 11:44:40 -0700290#else
Yao Chen44cf27c2017-09-14 22:32:50 -0700291GTEST_LOG_(INFO) << "This test does nothing.\n";
David Chendd896942017-09-26 11:44:40 -0700292#endif