David Chen | dd89694 | 2017-09-26 11:44:40 -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 "frameworks/base/cmds/statsd/src/statsd_config.pb.h" |
| 18 | #include "../src/matchers/LogEntryMatcherManager.h" |
| 19 | #include "../src/parse_util.h" |
| 20 | #include <log/logprint.h> |
| 21 | #include <log/log_read.h> |
| 22 | #include <log/log_event_list.h> |
| 23 | #include <gtest/gtest.h> |
| 24 | |
| 25 | #include <stdio.h> |
| 26 | |
| 27 | using namespace android::os::statsd; |
| 28 | using std::unordered_map; |
| 29 | |
Stefan Lafon | cdb1a0e | 2017-09-27 20:24:15 -0700 | [diff] [blame^] | 30 | const int kTagIdWakelock = 123; |
| 31 | const int kKeyIdState = 45; |
| 32 | const int kKeyIdPackageVersion = 67; |
| 33 | |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 34 | #ifdef __ANDROID__ |
| 35 | TEST(LogEntryMatcherTest, TestSimpleMatcher) { |
| 36 | // Set up the matcher |
| 37 | LogEntryMatcher matcher; |
| 38 | auto simpleMatcher = matcher.mutable_simple_log_entry_matcher(); |
Stefan Lafon | cdb1a0e | 2017-09-27 20:24:15 -0700 | [diff] [blame^] | 39 | simpleMatcher->add_tag(kTagIdWakelock); |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 40 | |
| 41 | unordered_map<int, long> intMap; |
| 42 | unordered_map<int, string> strMap; |
| 43 | unordered_map<int, float> floatMap; |
| 44 | unordered_map<int, bool> boolMap; |
| 45 | |
Stefan Lafon | cdb1a0e | 2017-09-27 20:24:15 -0700 | [diff] [blame^] | 46 | EXPECT_TRUE(LogEntryMatcherManager::matches(matcher, kTagIdWakelock, intMap, strMap, floatMap, boolMap)); |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | TEST(LogEntryMatcherTest, TestBoolMatcher) { |
| 50 | // Set up the matcher |
| 51 | LogEntryMatcher matcher; |
| 52 | auto simpleMatcher = matcher.mutable_simple_log_entry_matcher(); |
Stefan Lafon | cdb1a0e | 2017-09-27 20:24:15 -0700 | [diff] [blame^] | 53 | simpleMatcher->add_tag(kTagIdWakelock); |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 54 | auto keyValue = simpleMatcher->add_key_value_matcher(); |
Stefan Lafon | cdb1a0e | 2017-09-27 20:24:15 -0700 | [diff] [blame^] | 55 | keyValue->mutable_key_matcher()->set_key(kKeyIdState); |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 56 | |
| 57 | |
| 58 | unordered_map<int, long> intMap; |
| 59 | unordered_map<int, string> strMap; |
| 60 | unordered_map<int, float> floatMap; |
| 61 | unordered_map<int, bool> boolMap; |
| 62 | |
| 63 | keyValue->set_eq_bool(true); |
Stefan Lafon | cdb1a0e | 2017-09-27 20:24:15 -0700 | [diff] [blame^] | 64 | boolMap[kKeyIdState] = true; |
| 65 | EXPECT_TRUE(LogEntryMatcherManager::matches(matcher, kTagIdWakelock, intMap, strMap, floatMap, |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 66 | boolMap)); |
| 67 | |
| 68 | keyValue->set_eq_bool(false); |
Stefan Lafon | cdb1a0e | 2017-09-27 20:24:15 -0700 | [diff] [blame^] | 69 | EXPECT_FALSE(LogEntryMatcherManager::matches(matcher, kTagIdWakelock, intMap, strMap, |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 70 | floatMap, boolMap)); |
| 71 | |
Stefan Lafon | cdb1a0e | 2017-09-27 20:24:15 -0700 | [diff] [blame^] | 72 | boolMap[kKeyIdState] = false; |
| 73 | EXPECT_TRUE(LogEntryMatcherManager::matches(matcher, kTagIdWakelock, intMap, strMap, floatMap, |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 74 | boolMap)); |
| 75 | } |
| 76 | |
| 77 | TEST(LogEntryMatcherTest, TestStringMatcher) { |
| 78 | // Set up the matcher |
| 79 | LogEntryMatcher matcher; |
| 80 | auto simpleMatcher = matcher.mutable_simple_log_entry_matcher(); |
Stefan Lafon | cdb1a0e | 2017-09-27 20:24:15 -0700 | [diff] [blame^] | 81 | simpleMatcher->add_tag(kTagIdWakelock); |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 82 | auto keyValue = simpleMatcher->add_key_value_matcher(); |
Stefan Lafon | cdb1a0e | 2017-09-27 20:24:15 -0700 | [diff] [blame^] | 83 | keyValue->mutable_key_matcher()->set_key(kKeyIdState); |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 84 | keyValue->set_eq_string("wakelock_name"); |
| 85 | |
| 86 | unordered_map<int, long> intMap; |
| 87 | unordered_map<int, string> strMap; |
| 88 | unordered_map<int, float> floatMap; |
| 89 | unordered_map<int, bool> boolMap; |
| 90 | |
Stefan Lafon | cdb1a0e | 2017-09-27 20:24:15 -0700 | [diff] [blame^] | 91 | strMap[kKeyIdState] = "wakelock_name"; |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 92 | |
Stefan Lafon | cdb1a0e | 2017-09-27 20:24:15 -0700 | [diff] [blame^] | 93 | EXPECT_TRUE(LogEntryMatcherManager::matches(matcher, kTagIdWakelock, intMap, strMap, floatMap, boolMap)); |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | TEST(LogEntryMatcherTest, TestIntComparisonMatcher) { |
| 97 | // Set up the matcher |
| 98 | LogEntryMatcher matcher; |
| 99 | auto simpleMatcher = matcher.mutable_simple_log_entry_matcher(); |
Stefan Lafon | cdb1a0e | 2017-09-27 20:24:15 -0700 | [diff] [blame^] | 100 | simpleMatcher->add_tag(kTagIdWakelock); |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 101 | auto keyValue = simpleMatcher->add_key_value_matcher(); |
Stefan Lafon | cdb1a0e | 2017-09-27 20:24:15 -0700 | [diff] [blame^] | 102 | keyValue->mutable_key_matcher()->set_key(kKeyIdState); |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 103 | |
| 104 | unordered_map<int, long> intMap; |
| 105 | unordered_map<int, string> strMap; |
| 106 | unordered_map<int, float> floatMap; |
| 107 | unordered_map<int, bool> boolMap; |
| 108 | |
| 109 | keyValue->set_lt_int(10); |
Stefan Lafon | cdb1a0e | 2017-09-27 20:24:15 -0700 | [diff] [blame^] | 110 | intMap[kKeyIdState] = 11; |
| 111 | EXPECT_FALSE(LogEntryMatcherManager::matches(matcher, kTagIdWakelock, intMap, strMap, |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 112 | floatMap, boolMap)); |
Stefan Lafon | cdb1a0e | 2017-09-27 20:24:15 -0700 | [diff] [blame^] | 113 | intMap[kKeyIdState] = 10; |
| 114 | EXPECT_FALSE(LogEntryMatcherManager::matches(matcher, kTagIdWakelock, intMap, strMap, |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 115 | floatMap, boolMap)); |
Stefan Lafon | cdb1a0e | 2017-09-27 20:24:15 -0700 | [diff] [blame^] | 116 | intMap[kKeyIdState] = 9; |
| 117 | EXPECT_TRUE(LogEntryMatcherManager::matches(matcher, kTagIdWakelock, intMap, strMap, |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 118 | floatMap, boolMap)); |
| 119 | |
| 120 | keyValue->set_gt_int(10); |
Stefan Lafon | cdb1a0e | 2017-09-27 20:24:15 -0700 | [diff] [blame^] | 121 | intMap[kKeyIdState] = 11; |
| 122 | EXPECT_TRUE(LogEntryMatcherManager::matches(matcher, kTagIdWakelock, intMap, strMap, |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 123 | floatMap, boolMap)); |
Stefan Lafon | cdb1a0e | 2017-09-27 20:24:15 -0700 | [diff] [blame^] | 124 | intMap[kKeyIdState] = 10; |
| 125 | EXPECT_FALSE(LogEntryMatcherManager::matches(matcher, kTagIdWakelock, intMap, strMap, |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 126 | floatMap, boolMap)); |
Stefan Lafon | cdb1a0e | 2017-09-27 20:24:15 -0700 | [diff] [blame^] | 127 | intMap[kKeyIdState] = 9; |
| 128 | EXPECT_FALSE(LogEntryMatcherManager::matches(matcher, kTagIdWakelock, intMap, strMap, |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 129 | floatMap, boolMap)); |
| 130 | } |
| 131 | |
| 132 | TEST(LogEntryMatcherTest, TestIntWithEqualityComparisonMatcher) { |
| 133 | // Set up the matcher |
| 134 | LogEntryMatcher matcher; |
| 135 | auto simpleMatcher = matcher.mutable_simple_log_entry_matcher(); |
Stefan Lafon | cdb1a0e | 2017-09-27 20:24:15 -0700 | [diff] [blame^] | 136 | simpleMatcher->add_tag(kTagIdWakelock); |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 137 | auto keyValue = simpleMatcher->add_key_value_matcher(); |
Stefan Lafon | cdb1a0e | 2017-09-27 20:24:15 -0700 | [diff] [blame^] | 138 | keyValue->mutable_key_matcher()->set_key(kKeyIdState); |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 139 | |
| 140 | unordered_map<int, long> intMap; |
| 141 | unordered_map<int, string> strMap; |
| 142 | unordered_map<int, float> floatMap; |
| 143 | unordered_map<int, bool> boolMap; |
| 144 | |
| 145 | keyValue->set_lte_int(10); |
Stefan Lafon | cdb1a0e | 2017-09-27 20:24:15 -0700 | [diff] [blame^] | 146 | intMap[kKeyIdState] = 11; |
| 147 | EXPECT_FALSE(LogEntryMatcherManager::matches(matcher, kTagIdWakelock, intMap, strMap, |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 148 | floatMap, boolMap)); |
Stefan Lafon | cdb1a0e | 2017-09-27 20:24:15 -0700 | [diff] [blame^] | 149 | intMap[kKeyIdState] = 10; |
| 150 | EXPECT_TRUE(LogEntryMatcherManager::matches(matcher, kTagIdWakelock, intMap, strMap, |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 151 | floatMap, boolMap)); |
Stefan Lafon | cdb1a0e | 2017-09-27 20:24:15 -0700 | [diff] [blame^] | 152 | intMap[kKeyIdState] = 9; |
| 153 | EXPECT_TRUE(LogEntryMatcherManager::matches(matcher, kTagIdWakelock, intMap, strMap, |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 154 | floatMap, boolMap)); |
| 155 | |
| 156 | keyValue->set_gte_int(10); |
Stefan Lafon | cdb1a0e | 2017-09-27 20:24:15 -0700 | [diff] [blame^] | 157 | intMap[kKeyIdState] = 11; |
| 158 | EXPECT_TRUE(LogEntryMatcherManager::matches(matcher, kTagIdWakelock, intMap, strMap, |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 159 | floatMap, boolMap)); |
Stefan Lafon | cdb1a0e | 2017-09-27 20:24:15 -0700 | [diff] [blame^] | 160 | intMap[kKeyIdState] = 10; |
| 161 | EXPECT_TRUE(LogEntryMatcherManager::matches(matcher, kTagIdWakelock, intMap, strMap, |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 162 | floatMap, boolMap)); |
Stefan Lafon | cdb1a0e | 2017-09-27 20:24:15 -0700 | [diff] [blame^] | 163 | intMap[kKeyIdState] = 9; |
| 164 | EXPECT_FALSE(LogEntryMatcherManager::matches(matcher, kTagIdWakelock, intMap, strMap, |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 165 | floatMap, boolMap)); |
| 166 | } |
| 167 | |
| 168 | TEST(LogEntryMatcherTest, TestFloatComparisonMatcher) { |
| 169 | // Set up the matcher |
| 170 | LogEntryMatcher matcher; |
| 171 | auto simpleMatcher = matcher.mutable_simple_log_entry_matcher(); |
Stefan Lafon | cdb1a0e | 2017-09-27 20:24:15 -0700 | [diff] [blame^] | 172 | simpleMatcher->add_tag(kTagIdWakelock); |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 173 | auto keyValue = simpleMatcher->add_key_value_matcher(); |
Stefan Lafon | cdb1a0e | 2017-09-27 20:24:15 -0700 | [diff] [blame^] | 174 | keyValue->mutable_key_matcher()->set_key(kKeyIdState); |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 175 | |
| 176 | unordered_map<int, long> intMap; |
| 177 | unordered_map<int, string> strMap; |
| 178 | unordered_map<int, float> floatMap; |
| 179 | unordered_map<int, bool> boolMap; |
| 180 | |
| 181 | keyValue->set_lt_float(10.0); |
Stefan Lafon | cdb1a0e | 2017-09-27 20:24:15 -0700 | [diff] [blame^] | 182 | floatMap[kKeyIdState] = 10.1; |
| 183 | EXPECT_FALSE(LogEntryMatcherManager::matches(matcher, kTagIdWakelock, intMap, strMap, |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 184 | floatMap, boolMap)); |
Stefan Lafon | cdb1a0e | 2017-09-27 20:24:15 -0700 | [diff] [blame^] | 185 | floatMap[kKeyIdState] = 9.9; |
| 186 | EXPECT_TRUE(LogEntryMatcherManager::matches(matcher, kTagIdWakelock, intMap, strMap, |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 187 | floatMap, boolMap)); |
| 188 | |
| 189 | keyValue->set_gt_float(10.0); |
Stefan Lafon | cdb1a0e | 2017-09-27 20:24:15 -0700 | [diff] [blame^] | 190 | floatMap[kKeyIdState] = 10.1; |
| 191 | EXPECT_TRUE(LogEntryMatcherManager::matches(matcher, kTagIdWakelock, intMap, strMap, |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 192 | floatMap, boolMap)); |
Stefan Lafon | cdb1a0e | 2017-09-27 20:24:15 -0700 | [diff] [blame^] | 193 | floatMap[kKeyIdState] = 9.9; |
| 194 | EXPECT_FALSE(LogEntryMatcherManager::matches(matcher, kTagIdWakelock, intMap, strMap, |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 195 | floatMap, boolMap)); |
| 196 | } |
| 197 | |
| 198 | // Helper for the composite matchers. |
Stefan Lafon | cdb1a0e | 2017-09-27 20:24:15 -0700 | [diff] [blame^] | 199 | void addSimpleMatcher(SimpleLogEntryMatcher* simpleMatcher, int tag, int key, int val) { |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 200 | simpleMatcher->add_tag(tag); |
| 201 | auto keyValue = simpleMatcher->add_key_value_matcher(); |
| 202 | keyValue->mutable_key_matcher()->set_key(key); |
| 203 | keyValue->set_eq_int(val); |
| 204 | } |
| 205 | |
| 206 | TEST(LogEntryMatcherTest, TestAndMatcher) { |
| 207 | // Set up the matcher |
| 208 | LogEntryMatcher matcher; |
| 209 | auto combination = matcher.mutable_combination(); |
| 210 | combination->set_operation(LogicalOperation::AND); |
| 211 | |
Stefan Lafon | cdb1a0e | 2017-09-27 20:24:15 -0700 | [diff] [blame^] | 212 | addSimpleMatcher(combination->add_matcher()->mutable_simple_log_entry_matcher(), kTagIdWakelock, kKeyIdState, 3); |
| 213 | addSimpleMatcher(combination->add_matcher()->mutable_simple_log_entry_matcher(), kTagIdWakelock, kKeyIdPackageVersion, 4); |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 214 | |
| 215 | unordered_map<int, long> intMap; |
| 216 | unordered_map<int, string> strMap; |
| 217 | unordered_map<int, float> floatMap; |
| 218 | unordered_map<int, bool> boolMap; |
| 219 | |
Stefan Lafon | cdb1a0e | 2017-09-27 20:24:15 -0700 | [diff] [blame^] | 220 | intMap[kKeyIdPackageVersion] = 4; |
| 221 | EXPECT_FALSE(LogEntryMatcherManager::matches(matcher, kTagIdWakelock, intMap, strMap, floatMap, boolMap)); |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 222 | intMap.clear(); |
Stefan Lafon | cdb1a0e | 2017-09-27 20:24:15 -0700 | [diff] [blame^] | 223 | intMap[kKeyIdState] = 3; |
| 224 | EXPECT_FALSE(LogEntryMatcherManager::matches(matcher, kTagIdWakelock, intMap, strMap, floatMap, boolMap)); |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 225 | intMap.clear(); |
Stefan Lafon | cdb1a0e | 2017-09-27 20:24:15 -0700 | [diff] [blame^] | 226 | intMap[kKeyIdState] = 3; |
| 227 | intMap[kKeyIdPackageVersion] = 4; |
| 228 | EXPECT_TRUE(LogEntryMatcherManager::matches(matcher, kTagIdWakelock, intMap, strMap, floatMap, boolMap)); |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | TEST(LogEntryMatcherTest, TestOrMatcher) { |
| 232 | // Set up the matcher |
| 233 | LogEntryMatcher matcher; |
| 234 | auto combination = matcher.mutable_combination(); |
| 235 | combination->set_operation(LogicalOperation::OR); |
| 236 | |
| 237 | addSimpleMatcher(combination->add_matcher()->mutable_simple_log_entry_matcher(), |
Stefan Lafon | cdb1a0e | 2017-09-27 20:24:15 -0700 | [diff] [blame^] | 238 | kTagIdWakelock, kKeyIdState, 3); |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 239 | addSimpleMatcher(combination->add_matcher()->mutable_simple_log_entry_matcher(), |
Stefan Lafon | cdb1a0e | 2017-09-27 20:24:15 -0700 | [diff] [blame^] | 240 | kTagIdWakelock, kKeyIdPackageVersion, 4); |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 241 | |
| 242 | unordered_map<int, long> intMap; |
| 243 | unordered_map<int, string> strMap; |
| 244 | unordered_map<int, float> floatMap; |
| 245 | unordered_map<int, bool> boolMap; |
| 246 | |
| 247 | // Don't set any key-value pairs. |
Stefan Lafon | cdb1a0e | 2017-09-27 20:24:15 -0700 | [diff] [blame^] | 248 | EXPECT_FALSE(LogEntryMatcherManager::matches(matcher, kTagIdWakelock, intMap, strMap, floatMap, |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 249 | boolMap)); |
Stefan Lafon | cdb1a0e | 2017-09-27 20:24:15 -0700 | [diff] [blame^] | 250 | intMap[kKeyIdPackageVersion] = 4; |
| 251 | EXPECT_TRUE(LogEntryMatcherManager::matches(matcher, kTagIdWakelock, intMap, strMap, floatMap, |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 252 | boolMap)); |
| 253 | intMap.clear(); |
Stefan Lafon | cdb1a0e | 2017-09-27 20:24:15 -0700 | [diff] [blame^] | 254 | intMap[kKeyIdState] = 3; |
| 255 | EXPECT_TRUE(LogEntryMatcherManager::matches(matcher, kTagIdWakelock, intMap, strMap, floatMap, |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 256 | boolMap)); |
| 257 | intMap.clear(); |
Stefan Lafon | cdb1a0e | 2017-09-27 20:24:15 -0700 | [diff] [blame^] | 258 | intMap[kKeyIdState] = 3; |
| 259 | intMap[kKeyIdPackageVersion] = 4; |
| 260 | EXPECT_TRUE(LogEntryMatcherManager::matches(matcher, kTagIdWakelock, intMap, strMap, floatMap, |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 261 | boolMap)); |
| 262 | } |
| 263 | |
| 264 | TEST(LogEntryMatcherTest, TestNotMatcher) { |
| 265 | // Set up the matcher |
| 266 | LogEntryMatcher matcher; |
| 267 | auto combination = matcher.mutable_combination(); |
| 268 | combination->set_operation(LogicalOperation::NOT); |
| 269 | |
| 270 | // Define first simpleMatcher |
| 271 | addSimpleMatcher(combination->add_matcher()->mutable_simple_log_entry_matcher(), |
Stefan Lafon | cdb1a0e | 2017-09-27 20:24:15 -0700 | [diff] [blame^] | 272 | kTagIdWakelock, kKeyIdState, 3); |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 273 | |
| 274 | unordered_map<int, long> intMap; |
| 275 | unordered_map<int, string> strMap; |
| 276 | unordered_map<int, float> floatMap; |
| 277 | unordered_map<int, bool> boolMap; |
| 278 | |
| 279 | // Don't set any key-value pairs. |
Stefan Lafon | cdb1a0e | 2017-09-27 20:24:15 -0700 | [diff] [blame^] | 280 | intMap[kKeyIdState] = 3; |
| 281 | EXPECT_FALSE(LogEntryMatcherManager::matches(matcher, kTagIdWakelock, intMap, strMap, floatMap, |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 282 | boolMap)); |
| 283 | } |
| 284 | |
| 285 | TEST(LogEntryMatcherTest, TestNANDMatcher) { |
| 286 | // Set up the matcher |
| 287 | LogEntryMatcher matcher; |
| 288 | auto combination = matcher.mutable_combination(); |
| 289 | combination->set_operation(LogicalOperation::NAND); |
| 290 | |
| 291 | addSimpleMatcher(combination->add_matcher()->mutable_simple_log_entry_matcher(), |
Stefan Lafon | cdb1a0e | 2017-09-27 20:24:15 -0700 | [diff] [blame^] | 292 | kTagIdWakelock, kKeyIdState, 3); |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 293 | addSimpleMatcher(combination->add_matcher()->mutable_simple_log_entry_matcher(), |
Stefan Lafon | cdb1a0e | 2017-09-27 20:24:15 -0700 | [diff] [blame^] | 294 | kTagIdWakelock, kKeyIdPackageVersion, 4); |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 295 | |
| 296 | unordered_map<int, long> intMap; |
| 297 | unordered_map<int, string> strMap; |
| 298 | unordered_map<int, float> floatMap; |
| 299 | unordered_map<int, bool> boolMap; |
| 300 | |
| 301 | // Don't set any key-value pairs. |
Stefan Lafon | cdb1a0e | 2017-09-27 20:24:15 -0700 | [diff] [blame^] | 302 | EXPECT_TRUE(LogEntryMatcherManager::matches(matcher, kTagIdWakelock, intMap, strMap, floatMap, |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 303 | boolMap)); |
Stefan Lafon | cdb1a0e | 2017-09-27 20:24:15 -0700 | [diff] [blame^] | 304 | intMap[kKeyIdState] = 3; |
| 305 | EXPECT_TRUE(LogEntryMatcherManager::matches(matcher, kTagIdWakelock, intMap, strMap, floatMap, |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 306 | boolMap)); |
Stefan Lafon | cdb1a0e | 2017-09-27 20:24:15 -0700 | [diff] [blame^] | 307 | intMap[kKeyIdPackageVersion] = 4; |
| 308 | EXPECT_FALSE(LogEntryMatcherManager::matches(matcher, kTagIdWakelock, intMap, strMap, floatMap, |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 309 | boolMap)); |
| 310 | } |
| 311 | |
| 312 | TEST(LogEntryMatcherTest, TestNORMatcher) { |
| 313 | // Set up the matcher |
| 314 | LogEntryMatcher matcher; |
| 315 | auto combination = matcher.mutable_combination(); |
| 316 | combination->set_operation(LogicalOperation::NOR); |
| 317 | |
| 318 | addSimpleMatcher(combination->add_matcher()->mutable_simple_log_entry_matcher(), |
Stefan Lafon | cdb1a0e | 2017-09-27 20:24:15 -0700 | [diff] [blame^] | 319 | kTagIdWakelock, kKeyIdState, 3); |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 320 | addSimpleMatcher(combination->add_matcher()->mutable_simple_log_entry_matcher(), |
Stefan Lafon | cdb1a0e | 2017-09-27 20:24:15 -0700 | [diff] [blame^] | 321 | kTagIdWakelock, kKeyIdPackageVersion, 4); |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 322 | |
| 323 | unordered_map<int, long> intMap; |
| 324 | unordered_map<int, string> strMap; |
| 325 | unordered_map<int, float> floatMap; |
| 326 | unordered_map<int, bool> boolMap; |
| 327 | |
| 328 | // Don't set any key-value pairs. |
Stefan Lafon | cdb1a0e | 2017-09-27 20:24:15 -0700 | [diff] [blame^] | 329 | EXPECT_TRUE(LogEntryMatcherManager::matches(matcher, kTagIdWakelock, intMap, strMap, floatMap, |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 330 | boolMap)); |
Stefan Lafon | cdb1a0e | 2017-09-27 20:24:15 -0700 | [diff] [blame^] | 331 | intMap[kKeyIdState] = 3; |
| 332 | EXPECT_FALSE(LogEntryMatcherManager::matches(matcher, kTagIdWakelock, intMap, strMap, floatMap, |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 333 | boolMap)); |
Stefan Lafon | cdb1a0e | 2017-09-27 20:24:15 -0700 | [diff] [blame^] | 334 | intMap[kKeyIdPackageVersion] = 4; |
| 335 | EXPECT_FALSE(LogEntryMatcherManager::matches(matcher, kTagIdWakelock, intMap, strMap, floatMap, |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 336 | boolMap)); |
| 337 | } |
| 338 | |
| 339 | // Tests that a NOT on top of AND is the same as NAND |
| 340 | TEST(LogEntryMatcherTest, TestMultipleLayerMatcher) { |
| 341 | LogEntryMatcher matcher; |
| 342 | auto not_combination = matcher.mutable_combination(); |
| 343 | not_combination->set_operation(LogicalOperation::NOT); |
| 344 | |
| 345 | // Now add the AND |
| 346 | auto combination = not_combination->add_matcher()->mutable_combination(); |
| 347 | combination->set_operation(LogicalOperation::AND); |
| 348 | addSimpleMatcher(combination->add_matcher()->mutable_simple_log_entry_matcher(), |
Stefan Lafon | cdb1a0e | 2017-09-27 20:24:15 -0700 | [diff] [blame^] | 349 | kTagIdWakelock, kKeyIdState, 3); |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 350 | addSimpleMatcher(combination->add_matcher()->mutable_simple_log_entry_matcher(), |
Stefan Lafon | cdb1a0e | 2017-09-27 20:24:15 -0700 | [diff] [blame^] | 351 | kTagIdWakelock, kKeyIdPackageVersion, 4); |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 352 | |
| 353 | unordered_map<int, long> intMap; |
| 354 | unordered_map<int, string> strMap; |
| 355 | unordered_map<int, float> floatMap; |
| 356 | unordered_map<int, bool> boolMap; |
| 357 | |
| 358 | // Don't set any key-value pairs. |
Stefan Lafon | cdb1a0e | 2017-09-27 20:24:15 -0700 | [diff] [blame^] | 359 | EXPECT_TRUE(LogEntryMatcherManager::matches(matcher, kTagIdWakelock, intMap, strMap, floatMap, |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 360 | boolMap)); |
Stefan Lafon | cdb1a0e | 2017-09-27 20:24:15 -0700 | [diff] [blame^] | 361 | intMap[kKeyIdState] = 3; |
| 362 | EXPECT_TRUE(LogEntryMatcherManager::matches(matcher, kTagIdWakelock, intMap, strMap, floatMap, |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 363 | boolMap)); |
Stefan Lafon | cdb1a0e | 2017-09-27 20:24:15 -0700 | [diff] [blame^] | 364 | intMap[kKeyIdPackageVersion] = 4; |
| 365 | EXPECT_FALSE(LogEntryMatcherManager::matches(matcher, kTagIdWakelock, intMap, strMap, floatMap, |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 366 | boolMap)); |
| 367 | } |
Stefan Lafon | cdb1a0e | 2017-09-27 20:24:15 -0700 | [diff] [blame^] | 368 | |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 369 | #else |
| 370 | GTEST_LOG_(INFO) << "This test does nothing.\n"; |
| 371 | #endif |