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 | |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 15 | #include <gtest/gtest.h> |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 16 | #include <stdio.h> |
| 17 | |
Ruchir Rastogi | ffa34f05 | 2020-04-04 15:30:11 -0700 | [diff] [blame] | 18 | #include "annotations.h" |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 19 | #include "frameworks/base/cmds/statsd/src/statsd_config.pb.h" |
| 20 | #include "matchers/matcher_util.h" |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 21 | #include "stats_event.h" |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 22 | #include "stats_log_util.h" |
| 23 | #include "stats_util.h" |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 24 | #include "statsd_test_util.h" |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 25 | |
| 26 | using namespace android::os::statsd; |
| 27 | using std::unordered_map; |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 28 | using std::vector; |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 29 | |
Yao Chen | 8023540 | 2017-11-13 20:42:25 -0800 | [diff] [blame] | 30 | const int32_t TAG_ID = 123; |
tsaichristine | 7a57b8e | 2019-06-24 18:25:38 -0700 | [diff] [blame] | 31 | const int32_t TAG_ID_2 = 28; // hardcoded tag of atom with uid field |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 32 | const int FIELD_ID_1 = 1; |
| 33 | const int FIELD_ID_2 = 2; |
| 34 | const int FIELD_ID_3 = 2; |
| 35 | |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 36 | const int ATTRIBUTION_UID_FIELD_ID = 1; |
| 37 | const int ATTRIBUTION_TAG_FIELD_ID = 2; |
| 38 | |
Stefan Lafon | cdb1a0e | 2017-09-27 20:24:15 -0700 | [diff] [blame] | 39 | |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 40 | #ifdef __ANDROID__ |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 41 | |
| 42 | namespace { |
| 43 | |
| 44 | void makeIntLogEvent(LogEvent* logEvent, const int32_t atomId, const int64_t timestamp, |
| 45 | const int32_t value) { |
| 46 | AStatsEvent* statsEvent = AStatsEvent_obtain(); |
| 47 | AStatsEvent_setAtomId(statsEvent, atomId); |
| 48 | AStatsEvent_overwriteTimestamp(statsEvent, timestamp); |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 49 | AStatsEvent_writeInt32(statsEvent, value); |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 50 | |
tsaichristine | 8dca82e | 2020-04-07 09:40:03 -0700 | [diff] [blame] | 51 | parseStatsEventToLogEvent(statsEvent, logEvent); |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | void makeFloatLogEvent(LogEvent* logEvent, const int32_t atomId, const int64_t timestamp, |
| 55 | const float floatValue) { |
| 56 | AStatsEvent* statsEvent = AStatsEvent_obtain(); |
| 57 | AStatsEvent_setAtomId(statsEvent, atomId); |
| 58 | AStatsEvent_overwriteTimestamp(statsEvent, timestamp); |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 59 | AStatsEvent_writeFloat(statsEvent, floatValue); |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 60 | |
tsaichristine | 8dca82e | 2020-04-07 09:40:03 -0700 | [diff] [blame] | 61 | parseStatsEventToLogEvent(statsEvent, logEvent); |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | void makeStringLogEvent(LogEvent* logEvent, const int32_t atomId, const int64_t timestamp, |
| 65 | const string& name) { |
| 66 | AStatsEvent* statsEvent = AStatsEvent_obtain(); |
| 67 | AStatsEvent_setAtomId(statsEvent, atomId); |
| 68 | AStatsEvent_overwriteTimestamp(statsEvent, timestamp); |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 69 | AStatsEvent_writeString(statsEvent, name.c_str()); |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 70 | |
tsaichristine | 8dca82e | 2020-04-07 09:40:03 -0700 | [diff] [blame] | 71 | parseStatsEventToLogEvent(statsEvent, logEvent); |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 72 | } |
| 73 | |
Ruchir Rastogi | ffa34f05 | 2020-04-04 15:30:11 -0700 | [diff] [blame] | 74 | void makeIntWithBoolAnnotationLogEvent(LogEvent* logEvent, const int32_t atomId, |
| 75 | const int32_t field, const uint8_t annotationId, |
| 76 | const bool annotationValue) { |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 77 | AStatsEvent* statsEvent = AStatsEvent_obtain(); |
| 78 | AStatsEvent_setAtomId(statsEvent, atomId); |
Ruchir Rastogi | ffa34f05 | 2020-04-04 15:30:11 -0700 | [diff] [blame] | 79 | AStatsEvent_writeInt32(statsEvent, field); |
| 80 | AStatsEvent_addBoolAnnotation(statsEvent, annotationId, annotationValue); |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 81 | |
tsaichristine | 8dca82e | 2020-04-07 09:40:03 -0700 | [diff] [blame] | 82 | parseStatsEventToLogEvent(statsEvent, logEvent); |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | void makeAttributionLogEvent(LogEvent* logEvent, const int32_t atomId, const int64_t timestamp, |
| 86 | const vector<int>& attributionUids, |
| 87 | const vector<string>& attributionTags, const string& name) { |
| 88 | AStatsEvent* statsEvent = AStatsEvent_obtain(); |
| 89 | AStatsEvent_setAtomId(statsEvent, atomId); |
| 90 | AStatsEvent_overwriteTimestamp(statsEvent, timestamp); |
| 91 | |
tsaichristine | 8dca82e | 2020-04-07 09:40:03 -0700 | [diff] [blame] | 92 | writeAttribution(statsEvent, attributionUids, attributionTags); |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 93 | AStatsEvent_writeString(statsEvent, name.c_str()); |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 94 | |
tsaichristine | 8dca82e | 2020-04-07 09:40:03 -0700 | [diff] [blame] | 95 | parseStatsEventToLogEvent(statsEvent, logEvent); |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | void makeBoolLogEvent(LogEvent* logEvent, const int32_t atomId, const int64_t timestamp, |
| 99 | const bool bool1, const bool bool2) { |
| 100 | AStatsEvent* statsEvent = AStatsEvent_obtain(); |
| 101 | AStatsEvent_setAtomId(statsEvent, atomId); |
| 102 | AStatsEvent_overwriteTimestamp(statsEvent, timestamp); |
| 103 | |
| 104 | AStatsEvent_writeBool(statsEvent, bool1); |
| 105 | AStatsEvent_writeBool(statsEvent, bool2); |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 106 | |
tsaichristine | 8dca82e | 2020-04-07 09:40:03 -0700 | [diff] [blame] | 107 | parseStatsEventToLogEvent(statsEvent, logEvent); |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | } // anonymous namespace |
| 111 | |
Stefan Lafon | b8c9aa8 | 2017-12-03 14:27:25 -0800 | [diff] [blame] | 112 | TEST(AtomMatcherTest, TestSimpleMatcher) { |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 113 | UidMap uidMap; |
| 114 | |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 115 | // Set up the matcher |
Stefan Lafon | b8c9aa8 | 2017-12-03 14:27:25 -0800 | [diff] [blame] | 116 | AtomMatcher matcher; |
| 117 | auto simpleMatcher = matcher.mutable_simple_atom_matcher(); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 118 | simpleMatcher->set_atom_id(TAG_ID); |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 119 | |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 120 | LogEvent event(/*uid=*/0, /*pid=*/0); |
| 121 | makeIntLogEvent(&event, TAG_ID, 0, 11); |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 122 | |
| 123 | // Test |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 124 | EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event)); |
| 125 | |
| 126 | // Wrong tag id. |
| 127 | simpleMatcher->set_atom_id(TAG_ID + 1); |
| 128 | EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event)); |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 129 | } |
| 130 | |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 131 | TEST(AtomMatcherTest, TestAttributionMatcher) { |
| 132 | UidMap uidMap; |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 133 | std::vector<int> attributionUids = {1111, 2222, 3333}; |
| 134 | std::vector<string> attributionTags = {"location1", "location2", "location3"}; |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 135 | |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 136 | // Set up the log event. |
| 137 | LogEvent event(/*uid=*/0, /*pid=*/0); |
| 138 | makeAttributionLogEvent(&event, TAG_ID, 0, attributionUids, attributionTags, "some value"); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 139 | |
| 140 | // Set up the matcher |
| 141 | AtomMatcher matcher; |
| 142 | auto simpleMatcher = matcher.mutable_simple_atom_matcher(); |
| 143 | simpleMatcher->set_atom_id(TAG_ID); |
| 144 | |
| 145 | // Match first node. |
| 146 | auto attributionMatcher = simpleMatcher->add_field_value_matcher(); |
| 147 | attributionMatcher->set_field(FIELD_ID_1); |
| 148 | attributionMatcher->set_position(Position::FIRST); |
| 149 | attributionMatcher->mutable_matches_tuple()->add_field_value_matcher()->set_field( |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 150 | ATTRIBUTION_TAG_FIELD_ID); |
| 151 | attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string( |
| 152 | "tag"); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 153 | |
| 154 | auto fieldMatcher = simpleMatcher->add_field_value_matcher(); |
| 155 | fieldMatcher->set_field(FIELD_ID_2); |
| 156 | fieldMatcher->set_eq_string("some value"); |
| 157 | |
| 158 | // Tag not matched. |
| 159 | EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event)); |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 160 | attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string( |
| 161 | "location3"); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 162 | EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event)); |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 163 | attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string( |
| 164 | "location1"); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 165 | EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event)); |
| 166 | |
| 167 | // Match last node. |
| 168 | attributionMatcher->set_position(Position::LAST); |
| 169 | EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event)); |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 170 | attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string( |
| 171 | "location3"); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 172 | EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event)); |
| 173 | |
| 174 | // Match any node. |
| 175 | attributionMatcher->set_position(Position::ANY); |
| 176 | EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event)); |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 177 | attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string( |
| 178 | "location1"); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 179 | EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event)); |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 180 | attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string( |
| 181 | "location2"); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 182 | EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event)); |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 183 | attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string( |
| 184 | "location3"); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 185 | EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event)); |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 186 | attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string( |
| 187 | "location4"); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 188 | EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event)); |
| 189 | |
| 190 | // Attribution match but primitive field not match. |
| 191 | attributionMatcher->set_position(Position::ANY); |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 192 | attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string( |
| 193 | "location2"); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 194 | fieldMatcher->set_eq_string("wrong value"); |
| 195 | EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event)); |
| 196 | |
| 197 | fieldMatcher->set_eq_string("some value"); |
| 198 | |
| 199 | // Uid match. |
| 200 | attributionMatcher->set_position(Position::ANY); |
| 201 | attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_field( |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 202 | ATTRIBUTION_UID_FIELD_ID); |
| 203 | attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string( |
| 204 | "pkg0"); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 205 | EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event)); |
| 206 | |
David Chen | bd12527 | 2018-04-04 19:02:50 -0700 | [diff] [blame] | 207 | uidMap.updateMap( |
| 208 | 1, {1111, 1111, 2222, 3333, 3333} /* uid list */, {1, 1, 2, 1, 2} /* version list */, |
dwchen | 730403e | 2018-10-29 11:41:56 -0700 | [diff] [blame] | 209 | {android::String16("v1"), android::String16("v1"), android::String16("v2"), |
| 210 | android::String16("v1"), android::String16("v2")}, |
David Chen | bd12527 | 2018-04-04 19:02:50 -0700 | [diff] [blame] | 211 | {android::String16("pkg0"), android::String16("pkg1"), android::String16("pkg1"), |
dwchen | 730403e | 2018-10-29 11:41:56 -0700 | [diff] [blame] | 212 | android::String16("Pkg2"), android::String16("PkG3")} /* package name list */, |
| 213 | {android::String16(""), android::String16(""), android::String16(""), |
| 214 | android::String16(""), android::String16("")}); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 215 | |
| 216 | EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event)); |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 217 | attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string( |
| 218 | "pkg3"); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 219 | EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event)); |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 220 | attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string( |
| 221 | "pkg2"); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 222 | EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event)); |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 223 | attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string( |
| 224 | "pkg1"); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 225 | EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event)); |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 226 | attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string( |
| 227 | "pkg0"); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 228 | EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event)); |
| 229 | |
| 230 | attributionMatcher->set_position(Position::FIRST); |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 231 | attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string( |
| 232 | "pkg0"); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 233 | EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event)); |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 234 | attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string( |
| 235 | "pkg3"); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 236 | EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event)); |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 237 | attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string( |
| 238 | "pkg2"); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 239 | EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event)); |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 240 | attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string( |
| 241 | "pkg1"); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 242 | EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event)); |
| 243 | |
| 244 | attributionMatcher->set_position(Position::LAST); |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 245 | attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string( |
| 246 | "pkg0"); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 247 | EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event)); |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 248 | attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string( |
| 249 | "pkg3"); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 250 | EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event)); |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 251 | attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string( |
| 252 | "pkg2"); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 253 | EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event)); |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 254 | attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string( |
| 255 | "pkg1"); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 256 | EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event)); |
| 257 | |
| 258 | // Uid + tag. |
| 259 | attributionMatcher->set_position(Position::ANY); |
| 260 | attributionMatcher->mutable_matches_tuple()->add_field_value_matcher()->set_field( |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 261 | ATTRIBUTION_TAG_FIELD_ID); |
| 262 | attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string( |
| 263 | "pkg0"); |
| 264 | attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(1)->set_eq_string( |
| 265 | "location1"); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 266 | EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event)); |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 267 | attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string( |
| 268 | "pkg1"); |
| 269 | attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(1)->set_eq_string( |
| 270 | "location1"); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 271 | EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event)); |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 272 | attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string( |
| 273 | "pkg1"); |
| 274 | attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(1)->set_eq_string( |
| 275 | "location2"); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 276 | EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event)); |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 277 | attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string( |
| 278 | "pkg2"); |
| 279 | attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(1)->set_eq_string( |
| 280 | "location3"); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 281 | EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event)); |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 282 | attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string( |
| 283 | "pkg3"); |
| 284 | attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(1)->set_eq_string( |
| 285 | "location3"); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 286 | EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event)); |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 287 | attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string( |
| 288 | "pkg3"); |
| 289 | attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(1)->set_eq_string( |
| 290 | "location1"); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 291 | EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event)); |
| 292 | |
| 293 | attributionMatcher->set_position(Position::FIRST); |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 294 | attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string( |
| 295 | "pkg0"); |
| 296 | attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(1)->set_eq_string( |
| 297 | "location1"); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 298 | EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event)); |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 299 | attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string( |
| 300 | "pkg1"); |
| 301 | attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(1)->set_eq_string( |
| 302 | "location1"); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 303 | EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event)); |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 304 | attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string( |
| 305 | "pkg1"); |
| 306 | attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(1)->set_eq_string( |
| 307 | "location2"); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 308 | EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event)); |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 309 | attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string( |
| 310 | "pkg2"); |
| 311 | attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(1)->set_eq_string( |
| 312 | "location3"); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 313 | EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event)); |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 314 | attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string( |
| 315 | "pkg3"); |
| 316 | attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(1)->set_eq_string( |
| 317 | "location3"); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 318 | EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event)); |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 319 | attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string( |
| 320 | "pkg3"); |
| 321 | attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(1)->set_eq_string( |
| 322 | "location1"); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 323 | EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event)); |
| 324 | |
| 325 | attributionMatcher->set_position(Position::LAST); |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 326 | attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string( |
| 327 | "pkg0"); |
| 328 | attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(1)->set_eq_string( |
| 329 | "location1"); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 330 | EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event)); |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 331 | attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string( |
| 332 | "pkg1"); |
| 333 | attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(1)->set_eq_string( |
| 334 | "location1"); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 335 | EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event)); |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 336 | attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string( |
| 337 | "pkg1"); |
| 338 | attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(1)->set_eq_string( |
| 339 | "location2"); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 340 | EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event)); |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 341 | attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string( |
| 342 | "pkg2"); |
| 343 | attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(1)->set_eq_string( |
| 344 | "location3"); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 345 | EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event)); |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 346 | attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string( |
| 347 | "pkg3"); |
| 348 | attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(1)->set_eq_string( |
| 349 | "location3"); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 350 | EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event)); |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 351 | attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string( |
| 352 | "pkg3"); |
| 353 | attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(1)->set_eq_string( |
| 354 | "location1"); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 355 | EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event)); |
| 356 | } |
| 357 | |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 358 | TEST(AtomMatcherTest, TestUidFieldMatcher) { |
| 359 | UidMap uidMap; |
| 360 | uidMap.updateMap( |
| 361 | 1, {1111, 1111, 2222, 3333, 3333} /* uid list */, {1, 1, 2, 1, 2} /* version list */, |
| 362 | {android::String16("v1"), android::String16("v1"), android::String16("v2"), |
| 363 | android::String16("v1"), android::String16("v2")}, |
| 364 | {android::String16("pkg0"), android::String16("pkg1"), android::String16("pkg1"), |
| 365 | android::String16("Pkg2"), android::String16("PkG3")} /* package name list */, |
| 366 | {android::String16(""), android::String16(""), android::String16(""), |
| 367 | android::String16(""), android::String16("")}); |
| 368 | |
| 369 | // Set up matcher |
| 370 | AtomMatcher matcher; |
| 371 | auto simpleMatcher = matcher.mutable_simple_atom_matcher(); |
| 372 | simpleMatcher->set_atom_id(TAG_ID); |
| 373 | simpleMatcher->add_field_value_matcher()->set_field(1); |
| 374 | simpleMatcher->mutable_field_value_matcher(0)->set_eq_string("pkg0"); |
| 375 | |
Ruchir Rastogi | ffa34f05 | 2020-04-04 15:30:11 -0700 | [diff] [blame] | 376 | // Make event without is_uid annotation. |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 377 | LogEvent event1(/*uid=*/0, /*pid=*/0); |
| 378 | makeIntLogEvent(&event1, TAG_ID, 0, 1111); |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 379 | EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event1)); |
| 380 | |
Ruchir Rastogi | ffa34f05 | 2020-04-04 15:30:11 -0700 | [diff] [blame] | 381 | // Make event with is_uid annotation. |
| 382 | LogEvent event2(/*uid=*/0, /*pid=*/0); |
| 383 | makeIntWithBoolAnnotationLogEvent(&event2, TAG_ID_2, 1111, ANNOTATION_ID_IS_UID, true); |
| 384 | |
| 385 | // Event has is_uid annotation, so mapping from uid to package name occurs. |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 386 | simpleMatcher->set_atom_id(TAG_ID_2); |
| 387 | EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event2)); |
| 388 | |
Ruchir Rastogi | ffa34f05 | 2020-04-04 15:30:11 -0700 | [diff] [blame] | 389 | // Event has is_uid annotation, but uid maps to different package name. |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 390 | simpleMatcher->mutable_field_value_matcher(0)->set_eq_string("Pkg2"); |
| 391 | EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event2)); |
| 392 | } |
| 393 | |
Yao Chen | 9b1140e | 2018-02-27 10:55:54 -0800 | [diff] [blame] | 394 | TEST(AtomMatcherTest, TestNeqAnyStringMatcher) { |
| 395 | UidMap uidMap; |
| 396 | uidMap.updateMap( |
David Chen | bd12527 | 2018-04-04 19:02:50 -0700 | [diff] [blame] | 397 | 1, {1111, 1111, 2222, 3333, 3333} /* uid list */, {1, 1, 2, 1, 2} /* version list */, |
dwchen | 730403e | 2018-10-29 11:41:56 -0700 | [diff] [blame] | 398 | {android::String16("v1"), android::String16("v1"), android::String16("v2"), |
| 399 | android::String16("v1"), android::String16("v2")}, |
Yao Chen | 9b1140e | 2018-02-27 10:55:54 -0800 | [diff] [blame] | 400 | {android::String16("pkg0"), android::String16("pkg1"), android::String16("pkg1"), |
dwchen | 730403e | 2018-10-29 11:41:56 -0700 | [diff] [blame] | 401 | android::String16("Pkg2"), android::String16("PkG3")} /* package name list */, |
| 402 | {android::String16(""), android::String16(""), android::String16(""), |
| 403 | android::String16(""), android::String16("")}); |
Yao Chen | 9b1140e | 2018-02-27 10:55:54 -0800 | [diff] [blame] | 404 | |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 405 | std::vector<int> attributionUids = {1111, 2222, 3333, 1066}; |
| 406 | std::vector<string> attributionTags = {"location1", "location2", "location3", "location3"}; |
Yao Chen | 9b1140e | 2018-02-27 10:55:54 -0800 | [diff] [blame] | 407 | |
| 408 | // Set up the event |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 409 | LogEvent event(/*uid=*/0, /*pid=*/0); |
| 410 | makeAttributionLogEvent(&event, TAG_ID, 0, attributionUids, attributionTags, "some value"); |
Yao Chen | 9b1140e | 2018-02-27 10:55:54 -0800 | [diff] [blame] | 411 | |
| 412 | // Set up the matcher |
| 413 | AtomMatcher matcher; |
| 414 | auto simpleMatcher = matcher.mutable_simple_atom_matcher(); |
| 415 | simpleMatcher->set_atom_id(TAG_ID); |
| 416 | |
| 417 | // Match first node. |
| 418 | auto attributionMatcher = simpleMatcher->add_field_value_matcher(); |
| 419 | attributionMatcher->set_field(FIELD_ID_1); |
| 420 | attributionMatcher->set_position(Position::FIRST); |
| 421 | attributionMatcher->mutable_matches_tuple()->add_field_value_matcher()->set_field( |
| 422 | ATTRIBUTION_UID_FIELD_ID); |
| 423 | auto neqStringList = attributionMatcher->mutable_matches_tuple() |
| 424 | ->mutable_field_value_matcher(0) |
Yao Chen | d50f2ae | 2018-03-23 11:10:13 -0700 | [diff] [blame] | 425 | ->mutable_neq_any_string(); |
Yao Chen | 9b1140e | 2018-02-27 10:55:54 -0800 | [diff] [blame] | 426 | neqStringList->add_str_value("pkg2"); |
| 427 | neqStringList->add_str_value("pkg3"); |
| 428 | |
| 429 | auto fieldMatcher = simpleMatcher->add_field_value_matcher(); |
| 430 | fieldMatcher->set_field(FIELD_ID_2); |
| 431 | fieldMatcher->set_eq_string("some value"); |
| 432 | |
| 433 | EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event)); |
| 434 | |
| 435 | neqStringList->Clear(); |
| 436 | neqStringList->add_str_value("pkg1"); |
| 437 | neqStringList->add_str_value("pkg3"); |
| 438 | EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event)); |
| 439 | |
| 440 | attributionMatcher->set_position(Position::ANY); |
| 441 | neqStringList->Clear(); |
| 442 | neqStringList->add_str_value("maps.com"); |
| 443 | EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event)); |
| 444 | |
| 445 | neqStringList->Clear(); |
| 446 | neqStringList->add_str_value("PkG3"); |
| 447 | EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event)); |
| 448 | |
| 449 | attributionMatcher->set_position(Position::LAST); |
| 450 | neqStringList->Clear(); |
| 451 | neqStringList->add_str_value("AID_STATSD"); |
| 452 | EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event)); |
| 453 | } |
| 454 | |
| 455 | TEST(AtomMatcherTest, TestEqAnyStringMatcher) { |
| 456 | UidMap uidMap; |
| 457 | uidMap.updateMap( |
David Chen | bd12527 | 2018-04-04 19:02:50 -0700 | [diff] [blame] | 458 | 1, {1111, 1111, 2222, 3333, 3333} /* uid list */, {1, 1, 2, 1, 2} /* version list */, |
dwchen | 730403e | 2018-10-29 11:41:56 -0700 | [diff] [blame] | 459 | {android::String16("v1"), android::String16("v1"), android::String16("v2"), |
| 460 | android::String16("v1"), android::String16("v2")}, |
Yao Chen | 9b1140e | 2018-02-27 10:55:54 -0800 | [diff] [blame] | 461 | {android::String16("pkg0"), android::String16("pkg1"), android::String16("pkg1"), |
dwchen | 730403e | 2018-10-29 11:41:56 -0700 | [diff] [blame] | 462 | android::String16("Pkg2"), android::String16("PkG3")} /* package name list */, |
| 463 | {android::String16(""), android::String16(""), android::String16(""), |
| 464 | android::String16(""), android::String16("")}); |
Yao Chen | 9b1140e | 2018-02-27 10:55:54 -0800 | [diff] [blame] | 465 | |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 466 | std::vector<int> attributionUids = {1067, 2222, 3333, 1066}; |
| 467 | std::vector<string> attributionTags = {"location1", "location2", "location3", "location3"}; |
Yao Chen | 9b1140e | 2018-02-27 10:55:54 -0800 | [diff] [blame] | 468 | |
| 469 | // Set up the event |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 470 | LogEvent event(/*uid=*/0, /*pid=*/0); |
| 471 | makeAttributionLogEvent(&event, TAG_ID, 0, attributionUids, attributionTags, "some value"); |
Yao Chen | 9b1140e | 2018-02-27 10:55:54 -0800 | [diff] [blame] | 472 | |
| 473 | // Set up the matcher |
| 474 | AtomMatcher matcher; |
| 475 | auto simpleMatcher = matcher.mutable_simple_atom_matcher(); |
| 476 | simpleMatcher->set_atom_id(TAG_ID); |
| 477 | |
| 478 | // Match first node. |
| 479 | auto attributionMatcher = simpleMatcher->add_field_value_matcher(); |
| 480 | attributionMatcher->set_field(FIELD_ID_1); |
| 481 | attributionMatcher->set_position(Position::FIRST); |
| 482 | attributionMatcher->mutable_matches_tuple()->add_field_value_matcher()->set_field( |
| 483 | ATTRIBUTION_UID_FIELD_ID); |
| 484 | auto eqStringList = attributionMatcher->mutable_matches_tuple() |
| 485 | ->mutable_field_value_matcher(0) |
| 486 | ->mutable_eq_any_string(); |
| 487 | eqStringList->add_str_value("AID_ROOT"); |
| 488 | eqStringList->add_str_value("AID_INCIDENTD"); |
| 489 | |
| 490 | auto fieldMatcher = simpleMatcher->add_field_value_matcher(); |
| 491 | fieldMatcher->set_field(FIELD_ID_2); |
| 492 | fieldMatcher->set_eq_string("some value"); |
| 493 | |
| 494 | EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event)); |
| 495 | |
| 496 | attributionMatcher->set_position(Position::ANY); |
| 497 | eqStringList->Clear(); |
| 498 | eqStringList->add_str_value("AID_STATSD"); |
| 499 | EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event)); |
| 500 | |
| 501 | eqStringList->Clear(); |
| 502 | eqStringList->add_str_value("pkg1"); |
| 503 | EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event)); |
| 504 | |
| 505 | auto normalStringField = fieldMatcher->mutable_eq_any_string(); |
| 506 | normalStringField->add_str_value("some value123"); |
| 507 | normalStringField->add_str_value("some value"); |
| 508 | EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event)); |
| 509 | |
| 510 | normalStringField->Clear(); |
| 511 | normalStringField->add_str_value("AID_STATSD"); |
| 512 | EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event)); |
| 513 | |
| 514 | eqStringList->Clear(); |
| 515 | eqStringList->add_str_value("maps.com"); |
| 516 | EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event)); |
| 517 | } |
| 518 | |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 519 | TEST(AtomMatcherTest, TestBoolMatcher) { |
| 520 | UidMap uidMap; |
| 521 | // Set up the matcher |
| 522 | AtomMatcher matcher; |
| 523 | auto simpleMatcher = matcher.mutable_simple_atom_matcher(); |
| 524 | simpleMatcher->set_atom_id(TAG_ID); |
| 525 | auto keyValue1 = simpleMatcher->add_field_value_matcher(); |
| 526 | keyValue1->set_field(FIELD_ID_1); |
| 527 | auto keyValue2 = simpleMatcher->add_field_value_matcher(); |
| 528 | keyValue2->set_field(FIELD_ID_2); |
| 529 | |
| 530 | // Set up the event |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 531 | LogEvent event(/*uid=*/0, /*pid=*/0); |
| 532 | makeBoolLogEvent(&event, TAG_ID, 0, true, false); |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 533 | |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 534 | // Test |
| 535 | keyValue1->set_eq_bool(true); |
| 536 | keyValue2->set_eq_bool(false); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 537 | EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event)); |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 538 | |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 539 | keyValue1->set_eq_bool(false); |
| 540 | keyValue2->set_eq_bool(false); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 541 | EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event)); |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 542 | |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 543 | keyValue1->set_eq_bool(false); |
| 544 | keyValue2->set_eq_bool(true); |
| 545 | EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event)); |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 546 | |
| 547 | keyValue1->set_eq_bool(true); |
| 548 | keyValue2->set_eq_bool(true); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 549 | EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event)); |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 550 | } |
| 551 | |
Stefan Lafon | b8c9aa8 | 2017-12-03 14:27:25 -0800 | [diff] [blame] | 552 | TEST(AtomMatcherTest, TestStringMatcher) { |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 553 | UidMap uidMap; |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 554 | // Set up the matcher |
Stefan Lafon | b8c9aa8 | 2017-12-03 14:27:25 -0800 | [diff] [blame] | 555 | AtomMatcher matcher; |
| 556 | auto simpleMatcher = matcher.mutable_simple_atom_matcher(); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 557 | simpleMatcher->set_atom_id(TAG_ID); |
| 558 | auto keyValue = simpleMatcher->add_field_value_matcher(); |
| 559 | keyValue->set_field(FIELD_ID_1); |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 560 | keyValue->set_eq_string("some value"); |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 561 | |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 562 | // Set up the event |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 563 | LogEvent event(/*uid=*/0, /*pid=*/0); |
| 564 | makeStringLogEvent(&event, TAG_ID, 0, "some value"); |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 565 | |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 566 | // Test |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 567 | EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event)); |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 568 | } |
| 569 | |
Stefan Lafon | b8c9aa8 | 2017-12-03 14:27:25 -0800 | [diff] [blame] | 570 | TEST(AtomMatcherTest, TestMultiFieldsMatcher) { |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 571 | UidMap uidMap; |
Yao Chen | d41c422 | 2017-11-15 19:26:14 -0800 | [diff] [blame] | 572 | // Set up the matcher |
Stefan Lafon | b8c9aa8 | 2017-12-03 14:27:25 -0800 | [diff] [blame] | 573 | AtomMatcher matcher; |
| 574 | auto simpleMatcher = matcher.mutable_simple_atom_matcher(); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 575 | simpleMatcher->set_atom_id(TAG_ID); |
| 576 | auto keyValue1 = simpleMatcher->add_field_value_matcher(); |
| 577 | keyValue1->set_field(FIELD_ID_1); |
| 578 | auto keyValue2 = simpleMatcher->add_field_value_matcher(); |
| 579 | keyValue2->set_field(FIELD_ID_2); |
Yao Chen | d41c422 | 2017-11-15 19:26:14 -0800 | [diff] [blame] | 580 | |
| 581 | // Set up the event |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 582 | LogEvent event(/*uid=*/0, /*pid=*/0); |
| 583 | CreateTwoValueLogEvent(&event, TAG_ID, 0, 2, 3); |
Yao Chen | d41c422 | 2017-11-15 19:26:14 -0800 | [diff] [blame] | 584 | |
| 585 | // Test |
| 586 | keyValue1->set_eq_int(2); |
| 587 | keyValue2->set_eq_int(3); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 588 | EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event)); |
Yao Chen | d41c422 | 2017-11-15 19:26:14 -0800 | [diff] [blame] | 589 | |
| 590 | keyValue1->set_eq_int(2); |
| 591 | keyValue2->set_eq_int(4); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 592 | EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event)); |
Yao Chen | d41c422 | 2017-11-15 19:26:14 -0800 | [diff] [blame] | 593 | |
| 594 | keyValue1->set_eq_int(4); |
| 595 | keyValue2->set_eq_int(3); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 596 | EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event)); |
Yao Chen | d41c422 | 2017-11-15 19:26:14 -0800 | [diff] [blame] | 597 | } |
| 598 | |
Stefan Lafon | b8c9aa8 | 2017-12-03 14:27:25 -0800 | [diff] [blame] | 599 | TEST(AtomMatcherTest, TestIntComparisonMatcher) { |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 600 | UidMap uidMap; |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 601 | // Set up the matcher |
Stefan Lafon | b8c9aa8 | 2017-12-03 14:27:25 -0800 | [diff] [blame] | 602 | AtomMatcher matcher; |
| 603 | auto simpleMatcher = matcher.mutable_simple_atom_matcher(); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 604 | |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 605 | simpleMatcher->set_atom_id(TAG_ID); |
| 606 | auto keyValue = simpleMatcher->add_field_value_matcher(); |
| 607 | keyValue->set_field(FIELD_ID_1); |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 608 | |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 609 | // Set up the event |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 610 | LogEvent event(/*uid=*/0, /*pid=*/0); |
| 611 | makeIntLogEvent(&event, TAG_ID, 0, 11); |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 612 | |
| 613 | // Test |
| 614 | |
| 615 | // eq_int |
| 616 | keyValue->set_eq_int(10); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 617 | EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event)); |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 618 | keyValue->set_eq_int(11); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 619 | EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event)); |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 620 | keyValue->set_eq_int(12); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 621 | EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event)); |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 622 | |
| 623 | // lt_int |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 624 | keyValue->set_lt_int(10); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 625 | EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event)); |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 626 | keyValue->set_lt_int(11); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 627 | EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event)); |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 628 | keyValue->set_lt_int(12); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 629 | EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event)); |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 630 | |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 631 | // lte_int |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 632 | keyValue->set_lte_int(10); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 633 | EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event)); |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 634 | keyValue->set_lte_int(11); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 635 | EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event)); |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 636 | keyValue->set_lte_int(12); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 637 | EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event)); |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 638 | |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 639 | // gt_int |
| 640 | keyValue->set_gt_int(10); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 641 | EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event)); |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 642 | keyValue->set_gt_int(11); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 643 | EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event)); |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 644 | keyValue->set_gt_int(12); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 645 | EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event)); |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 646 | |
| 647 | // gte_int |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 648 | keyValue->set_gte_int(10); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 649 | EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event)); |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 650 | keyValue->set_gte_int(11); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 651 | EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event)); |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 652 | keyValue->set_gte_int(12); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 653 | EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event)); |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 654 | } |
| 655 | |
Stefan Lafon | b8c9aa8 | 2017-12-03 14:27:25 -0800 | [diff] [blame] | 656 | TEST(AtomMatcherTest, TestFloatComparisonMatcher) { |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 657 | UidMap uidMap; |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 658 | // Set up the matcher |
Stefan Lafon | b8c9aa8 | 2017-12-03 14:27:25 -0800 | [diff] [blame] | 659 | AtomMatcher matcher; |
| 660 | auto simpleMatcher = matcher.mutable_simple_atom_matcher(); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 661 | simpleMatcher->set_atom_id(TAG_ID); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 662 | |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 663 | auto keyValue = simpleMatcher->add_field_value_matcher(); |
| 664 | keyValue->set_field(FIELD_ID_1); |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 665 | |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 666 | LogEvent event1(/*uid=*/0, /*pid=*/0); |
| 667 | makeFloatLogEvent(&event1, TAG_ID, 0, 10.1f); |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 668 | keyValue->set_lt_float(10.0); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 669 | EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event1)); |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 670 | |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 671 | LogEvent event2(/*uid=*/0, /*pid=*/0); |
| 672 | makeFloatLogEvent(&event2, TAG_ID, 0, 9.9f); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 673 | EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event2)); |
Yao Chen | 8023540 | 2017-11-13 20:42:25 -0800 | [diff] [blame] | 674 | |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 675 | LogEvent event3(/*uid=*/0, /*pid=*/0); |
| 676 | makeFloatLogEvent(&event3, TAG_ID, 0, 10.1f); |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 677 | keyValue->set_gt_float(10.0); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 678 | EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event3)); |
Yao Chen | 8023540 | 2017-11-13 20:42:25 -0800 | [diff] [blame] | 679 | |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 680 | LogEvent event4(/*uid=*/0, /*pid=*/0); |
| 681 | makeFloatLogEvent(&event4, TAG_ID, 0, 9.9f); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 682 | EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event4)); |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 683 | } |
| 684 | |
| 685 | // Helper for the composite matchers. |
Stefan Lafon | b8c9aa8 | 2017-12-03 14:27:25 -0800 | [diff] [blame] | 686 | void addSimpleMatcher(SimpleAtomMatcher* simpleMatcher, int tag, int key, int val) { |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 687 | simpleMatcher->set_atom_id(tag); |
| 688 | auto keyValue = simpleMatcher->add_field_value_matcher(); |
| 689 | keyValue->set_field(key); |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 690 | keyValue->set_eq_int(val); |
| 691 | } |
| 692 | |
Stefan Lafon | b8c9aa8 | 2017-12-03 14:27:25 -0800 | [diff] [blame] | 693 | TEST(AtomMatcherTest, TestAndMatcher) { |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 694 | // Set up the matcher |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 695 | LogicalOperation operation = LogicalOperation::AND; |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 696 | |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 697 | vector<int> children; |
| 698 | children.push_back(0); |
| 699 | children.push_back(1); |
| 700 | children.push_back(2); |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 701 | |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 702 | vector<MatchingState> matcherResults; |
| 703 | matcherResults.push_back(MatchingState::kMatched); |
| 704 | matcherResults.push_back(MatchingState::kNotMatched); |
| 705 | matcherResults.push_back(MatchingState::kMatched); |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 706 | |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 707 | EXPECT_FALSE(combinationMatch(children, operation, matcherResults)); |
| 708 | |
| 709 | matcherResults.clear(); |
| 710 | matcherResults.push_back(MatchingState::kMatched); |
| 711 | matcherResults.push_back(MatchingState::kMatched); |
| 712 | matcherResults.push_back(MatchingState::kMatched); |
| 713 | |
| 714 | EXPECT_TRUE(combinationMatch(children, operation, matcherResults)); |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 715 | } |
| 716 | |
Stefan Lafon | b8c9aa8 | 2017-12-03 14:27:25 -0800 | [diff] [blame] | 717 | TEST(AtomMatcherTest, TestOrMatcher) { |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 718 | // Set up the matcher |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 719 | LogicalOperation operation = LogicalOperation::OR; |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 720 | |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 721 | vector<int> children; |
| 722 | children.push_back(0); |
| 723 | children.push_back(1); |
| 724 | children.push_back(2); |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 725 | |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 726 | vector<MatchingState> matcherResults; |
| 727 | matcherResults.push_back(MatchingState::kMatched); |
| 728 | matcherResults.push_back(MatchingState::kNotMatched); |
| 729 | matcherResults.push_back(MatchingState::kMatched); |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 730 | |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 731 | EXPECT_TRUE(combinationMatch(children, operation, matcherResults)); |
| 732 | |
| 733 | matcherResults.clear(); |
| 734 | matcherResults.push_back(MatchingState::kNotMatched); |
| 735 | matcherResults.push_back(MatchingState::kNotMatched); |
| 736 | matcherResults.push_back(MatchingState::kNotMatched); |
| 737 | |
| 738 | EXPECT_FALSE(combinationMatch(children, operation, matcherResults)); |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 739 | } |
| 740 | |
Stefan Lafon | b8c9aa8 | 2017-12-03 14:27:25 -0800 | [diff] [blame] | 741 | TEST(AtomMatcherTest, TestNotMatcher) { |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 742 | // Set up the matcher |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 743 | LogicalOperation operation = LogicalOperation::NOT; |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 744 | |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 745 | vector<int> children; |
| 746 | children.push_back(0); |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 747 | |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 748 | vector<MatchingState> matcherResults; |
| 749 | matcherResults.push_back(MatchingState::kMatched); |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 750 | |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 751 | EXPECT_FALSE(combinationMatch(children, operation, matcherResults)); |
| 752 | |
| 753 | matcherResults.clear(); |
| 754 | matcherResults.push_back(MatchingState::kNotMatched); |
| 755 | EXPECT_TRUE(combinationMatch(children, operation, matcherResults)); |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 756 | } |
| 757 | |
Stefan Lafon | b8c9aa8 | 2017-12-03 14:27:25 -0800 | [diff] [blame] | 758 | TEST(AtomMatcherTest, TestNandMatcher) { |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 759 | // Set up the matcher |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 760 | LogicalOperation operation = LogicalOperation::NAND; |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 761 | |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 762 | vector<int> children; |
| 763 | children.push_back(0); |
| 764 | children.push_back(1); |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 765 | |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 766 | vector<MatchingState> matcherResults; |
| 767 | matcherResults.push_back(MatchingState::kMatched); |
| 768 | matcherResults.push_back(MatchingState::kNotMatched); |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 769 | |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 770 | EXPECT_TRUE(combinationMatch(children, operation, matcherResults)); |
| 771 | |
| 772 | matcherResults.clear(); |
| 773 | matcherResults.push_back(MatchingState::kNotMatched); |
| 774 | matcherResults.push_back(MatchingState::kNotMatched); |
| 775 | EXPECT_TRUE(combinationMatch(children, operation, matcherResults)); |
| 776 | |
| 777 | matcherResults.clear(); |
| 778 | matcherResults.push_back(MatchingState::kMatched); |
| 779 | matcherResults.push_back(MatchingState::kMatched); |
| 780 | EXPECT_FALSE(combinationMatch(children, operation, matcherResults)); |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 781 | } |
| 782 | |
Stefan Lafon | b8c9aa8 | 2017-12-03 14:27:25 -0800 | [diff] [blame] | 783 | TEST(AtomMatcherTest, TestNorMatcher) { |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 784 | // Set up the matcher |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 785 | LogicalOperation operation = LogicalOperation::NOR; |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 786 | |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 787 | vector<int> children; |
| 788 | children.push_back(0); |
| 789 | children.push_back(1); |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 790 | |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 791 | vector<MatchingState> matcherResults; |
| 792 | matcherResults.push_back(MatchingState::kMatched); |
| 793 | matcherResults.push_back(MatchingState::kNotMatched); |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 794 | |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 795 | EXPECT_FALSE(combinationMatch(children, operation, matcherResults)); |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 796 | |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 797 | matcherResults.clear(); |
| 798 | matcherResults.push_back(MatchingState::kNotMatched); |
| 799 | matcherResults.push_back(MatchingState::kNotMatched); |
| 800 | EXPECT_TRUE(combinationMatch(children, operation, matcherResults)); |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 801 | |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 802 | matcherResults.clear(); |
| 803 | matcherResults.push_back(MatchingState::kMatched); |
| 804 | matcherResults.push_back(MatchingState::kMatched); |
| 805 | EXPECT_FALSE(combinationMatch(children, operation, matcherResults)); |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 806 | } |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 807 | #else |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 808 | GTEST_LOG_(INFO) << "This test does nothing.\n"; |
David Chen | dd89694 | 2017-09-26 11:44:40 -0700 | [diff] [blame] | 809 | #endif |