blob: 6264c075426a2e7e6140ff9c4fbf20869718190c [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
Yao Chen44cf27c2017-09-14 22:32:50 -070015#include <gtest/gtest.h>
David Chendd896942017-09-26 11:44:40 -070016#include <stdio.h>
17
Ruchir Rastogiffa34f052020-04-04 15:30:11 -070018#include "annotations.h"
David Chendd896942017-09-26 11:44:40 -070019#include "frameworks/base/cmds/statsd/src/statsd_config.pb.h"
20#include "matchers/matcher_util.h"
tsaichristinea3d2ed82020-03-19 20:53:36 -070021#include "stats_event.h"
David Chendd896942017-09-26 11:44:40 -070022#include "stats_log_util.h"
23#include "stats_util.h"
tsaichristinea3d2ed82020-03-19 20:53:36 -070024#include "statsd_test_util.h"
David Chendd896942017-09-26 11:44:40 -070025
26using namespace android::os::statsd;
27using std::unordered_map;
Yao Chencaf339d2017-10-06 16:01:10 -070028using std::vector;
David Chendd896942017-09-26 11:44:40 -070029
Yao Chen80235402017-11-13 20:42:25 -080030const int32_t TAG_ID = 123;
tsaichristine7a57b8e2019-06-24 18:25:38 -070031const int32_t TAG_ID_2 = 28; // hardcoded tag of atom with uid field
Joe Onoratoc4dfae52017-10-17 23:38:21 -070032const int FIELD_ID_1 = 1;
33const int FIELD_ID_2 = 2;
34const int FIELD_ID_3 = 2;
35
Yangster-mac20877162017-12-22 17:19:39 -080036const int ATTRIBUTION_UID_FIELD_ID = 1;
37const int ATTRIBUTION_TAG_FIELD_ID = 2;
38
Stefan Lafoncdb1a0e2017-09-27 20:24:15 -070039
David Chendd896942017-09-26 11:44:40 -070040#ifdef __ANDROID__
tsaichristinea3d2ed82020-03-19 20:53:36 -070041
42namespace {
43
44void 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);
tsaichristinea3d2ed82020-03-19 20:53:36 -070049 AStatsEvent_writeInt32(statsEvent, value);
tsaichristinea3d2ed82020-03-19 20:53:36 -070050
tsaichristine8dca82e2020-04-07 09:40:03 -070051 parseStatsEventToLogEvent(statsEvent, logEvent);
tsaichristinea3d2ed82020-03-19 20:53:36 -070052}
53
54void 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);
tsaichristinea3d2ed82020-03-19 20:53:36 -070059 AStatsEvent_writeFloat(statsEvent, floatValue);
tsaichristinea3d2ed82020-03-19 20:53:36 -070060
tsaichristine8dca82e2020-04-07 09:40:03 -070061 parseStatsEventToLogEvent(statsEvent, logEvent);
tsaichristinea3d2ed82020-03-19 20:53:36 -070062}
63
64void 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);
tsaichristinea3d2ed82020-03-19 20:53:36 -070069 AStatsEvent_writeString(statsEvent, name.c_str());
tsaichristinea3d2ed82020-03-19 20:53:36 -070070
tsaichristine8dca82e2020-04-07 09:40:03 -070071 parseStatsEventToLogEvent(statsEvent, logEvent);
tsaichristinea3d2ed82020-03-19 20:53:36 -070072}
73
Ruchir Rastogiffa34f052020-04-04 15:30:11 -070074void makeIntWithBoolAnnotationLogEvent(LogEvent* logEvent, const int32_t atomId,
75 const int32_t field, const uint8_t annotationId,
76 const bool annotationValue) {
tsaichristinea3d2ed82020-03-19 20:53:36 -070077 AStatsEvent* statsEvent = AStatsEvent_obtain();
78 AStatsEvent_setAtomId(statsEvent, atomId);
Ruchir Rastogiffa34f052020-04-04 15:30:11 -070079 AStatsEvent_writeInt32(statsEvent, field);
80 AStatsEvent_addBoolAnnotation(statsEvent, annotationId, annotationValue);
tsaichristinea3d2ed82020-03-19 20:53:36 -070081
tsaichristine8dca82e2020-04-07 09:40:03 -070082 parseStatsEventToLogEvent(statsEvent, logEvent);
tsaichristinea3d2ed82020-03-19 20:53:36 -070083}
84
85void 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
tsaichristine8dca82e2020-04-07 09:40:03 -070092 writeAttribution(statsEvent, attributionUids, attributionTags);
tsaichristinea3d2ed82020-03-19 20:53:36 -070093 AStatsEvent_writeString(statsEvent, name.c_str());
tsaichristinea3d2ed82020-03-19 20:53:36 -070094
tsaichristine8dca82e2020-04-07 09:40:03 -070095 parseStatsEventToLogEvent(statsEvent, logEvent);
tsaichristinea3d2ed82020-03-19 20:53:36 -070096}
97
98void 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);
tsaichristinea3d2ed82020-03-19 20:53:36 -0700106
tsaichristine8dca82e2020-04-07 09:40:03 -0700107 parseStatsEventToLogEvent(statsEvent, logEvent);
tsaichristinea3d2ed82020-03-19 20:53:36 -0700108}
109
110} // anonymous namespace
111
Stefan Lafonb8c9aa82017-12-03 14:27:25 -0800112TEST(AtomMatcherTest, TestSimpleMatcher) {
Yangster-mac20877162017-12-22 17:19:39 -0800113 UidMap uidMap;
114
David Chendd896942017-09-26 11:44:40 -0700115 // Set up the matcher
Stefan Lafonb8c9aa82017-12-03 14:27:25 -0800116 AtomMatcher matcher;
117 auto simpleMatcher = matcher.mutable_simple_atom_matcher();
Yangster-mac20877162017-12-22 17:19:39 -0800118 simpleMatcher->set_atom_id(TAG_ID);
David Chendd896942017-09-26 11:44:40 -0700119
tsaichristinea3d2ed82020-03-19 20:53:36 -0700120 LogEvent event(/*uid=*/0, /*pid=*/0);
121 makeIntLogEvent(&event, TAG_ID, 0, 11);
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700122
123 // Test
Yangster-mac20877162017-12-22 17:19:39 -0800124 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 Chendd896942017-09-26 11:44:40 -0700129}
130
Yangster-mac20877162017-12-22 17:19:39 -0800131TEST(AtomMatcherTest, TestAttributionMatcher) {
132 UidMap uidMap;
tsaichristinea3d2ed82020-03-19 20:53:36 -0700133 std::vector<int> attributionUids = {1111, 2222, 3333};
134 std::vector<string> attributionTags = {"location1", "location2", "location3"};
Yangster-mac20877162017-12-22 17:19:39 -0800135
tsaichristinea3d2ed82020-03-19 20:53:36 -0700136 // Set up the log event.
137 LogEvent event(/*uid=*/0, /*pid=*/0);
138 makeAttributionLogEvent(&event, TAG_ID, 0, attributionUids, attributionTags, "some value");
Yangster-mac20877162017-12-22 17:19:39 -0800139
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(
tsaichristinea3d2ed82020-03-19 20:53:36 -0700150 ATTRIBUTION_TAG_FIELD_ID);
151 attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string(
152 "tag");
Yangster-mac20877162017-12-22 17:19:39 -0800153
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));
tsaichristinea3d2ed82020-03-19 20:53:36 -0700160 attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string(
161 "location3");
Yangster-mac20877162017-12-22 17:19:39 -0800162 EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event));
tsaichristinea3d2ed82020-03-19 20:53:36 -0700163 attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string(
164 "location1");
Yangster-mac20877162017-12-22 17:19:39 -0800165 EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event));
166
167 // Match last node.
168 attributionMatcher->set_position(Position::LAST);
169 EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event));
tsaichristinea3d2ed82020-03-19 20:53:36 -0700170 attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string(
171 "location3");
Yangster-mac20877162017-12-22 17:19:39 -0800172 EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event));
173
174 // Match any node.
175 attributionMatcher->set_position(Position::ANY);
176 EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event));
tsaichristinea3d2ed82020-03-19 20:53:36 -0700177 attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string(
178 "location1");
Yangster-mac20877162017-12-22 17:19:39 -0800179 EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event));
tsaichristinea3d2ed82020-03-19 20:53:36 -0700180 attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string(
181 "location2");
Yangster-mac20877162017-12-22 17:19:39 -0800182 EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event));
tsaichristinea3d2ed82020-03-19 20:53:36 -0700183 attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string(
184 "location3");
Yangster-mac20877162017-12-22 17:19:39 -0800185 EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event));
tsaichristinea3d2ed82020-03-19 20:53:36 -0700186 attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string(
187 "location4");
Yangster-mac20877162017-12-22 17:19:39 -0800188 EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event));
189
190 // Attribution match but primitive field not match.
191 attributionMatcher->set_position(Position::ANY);
tsaichristinea3d2ed82020-03-19 20:53:36 -0700192 attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string(
193 "location2");
Yangster-mac20877162017-12-22 17:19:39 -0800194 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(
tsaichristinea3d2ed82020-03-19 20:53:36 -0700202 ATTRIBUTION_UID_FIELD_ID);
203 attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string(
204 "pkg0");
Yangster-mac20877162017-12-22 17:19:39 -0800205 EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event));
206
David Chenbd125272018-04-04 19:02:50 -0700207 uidMap.updateMap(
208 1, {1111, 1111, 2222, 3333, 3333} /* uid list */, {1, 1, 2, 1, 2} /* version list */,
dwchen730403e2018-10-29 11:41:56 -0700209 {android::String16("v1"), android::String16("v1"), android::String16("v2"),
210 android::String16("v1"), android::String16("v2")},
David Chenbd125272018-04-04 19:02:50 -0700211 {android::String16("pkg0"), android::String16("pkg1"), android::String16("pkg1"),
dwchen730403e2018-10-29 11:41:56 -0700212 android::String16("Pkg2"), android::String16("PkG3")} /* package name list */,
213 {android::String16(""), android::String16(""), android::String16(""),
214 android::String16(""), android::String16("")});
Yangster-mac20877162017-12-22 17:19:39 -0800215
216 EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event));
tsaichristinea3d2ed82020-03-19 20:53:36 -0700217 attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string(
218 "pkg3");
Yangster-mac20877162017-12-22 17:19:39 -0800219 EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event));
tsaichristinea3d2ed82020-03-19 20:53:36 -0700220 attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string(
221 "pkg2");
Yangster-mac20877162017-12-22 17:19:39 -0800222 EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event));
tsaichristinea3d2ed82020-03-19 20:53:36 -0700223 attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string(
224 "pkg1");
Yangster-mac20877162017-12-22 17:19:39 -0800225 EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event));
tsaichristinea3d2ed82020-03-19 20:53:36 -0700226 attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string(
227 "pkg0");
Yangster-mac20877162017-12-22 17:19:39 -0800228 EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event));
229
230 attributionMatcher->set_position(Position::FIRST);
tsaichristinea3d2ed82020-03-19 20:53:36 -0700231 attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string(
232 "pkg0");
Yangster-mac20877162017-12-22 17:19:39 -0800233 EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event));
tsaichristinea3d2ed82020-03-19 20:53:36 -0700234 attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string(
235 "pkg3");
Yangster-mac20877162017-12-22 17:19:39 -0800236 EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event));
tsaichristinea3d2ed82020-03-19 20:53:36 -0700237 attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string(
238 "pkg2");
Yangster-mac20877162017-12-22 17:19:39 -0800239 EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event));
tsaichristinea3d2ed82020-03-19 20:53:36 -0700240 attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string(
241 "pkg1");
Yangster-mac20877162017-12-22 17:19:39 -0800242 EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event));
243
244 attributionMatcher->set_position(Position::LAST);
tsaichristinea3d2ed82020-03-19 20:53:36 -0700245 attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string(
246 "pkg0");
Yangster-mac20877162017-12-22 17:19:39 -0800247 EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event));
tsaichristinea3d2ed82020-03-19 20:53:36 -0700248 attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string(
249 "pkg3");
Yangster-mac20877162017-12-22 17:19:39 -0800250 EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event));
tsaichristinea3d2ed82020-03-19 20:53:36 -0700251 attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string(
252 "pkg2");
Yangster-mac20877162017-12-22 17:19:39 -0800253 EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event));
tsaichristinea3d2ed82020-03-19 20:53:36 -0700254 attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string(
255 "pkg1");
Yangster-mac20877162017-12-22 17:19:39 -0800256 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(
tsaichristinea3d2ed82020-03-19 20:53:36 -0700261 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-mac20877162017-12-22 17:19:39 -0800266 EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event));
tsaichristinea3d2ed82020-03-19 20:53:36 -0700267 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-mac20877162017-12-22 17:19:39 -0800271 EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event));
tsaichristinea3d2ed82020-03-19 20:53:36 -0700272 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-mac20877162017-12-22 17:19:39 -0800276 EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event));
tsaichristinea3d2ed82020-03-19 20:53:36 -0700277 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-mac20877162017-12-22 17:19:39 -0800281 EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event));
tsaichristinea3d2ed82020-03-19 20:53:36 -0700282 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-mac20877162017-12-22 17:19:39 -0800286 EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event));
tsaichristinea3d2ed82020-03-19 20:53:36 -0700287 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-mac20877162017-12-22 17:19:39 -0800291 EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event));
292
293 attributionMatcher->set_position(Position::FIRST);
tsaichristinea3d2ed82020-03-19 20:53:36 -0700294 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-mac20877162017-12-22 17:19:39 -0800298 EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event));
tsaichristinea3d2ed82020-03-19 20:53:36 -0700299 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-mac20877162017-12-22 17:19:39 -0800303 EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event));
tsaichristinea3d2ed82020-03-19 20:53:36 -0700304 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-mac20877162017-12-22 17:19:39 -0800308 EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event));
tsaichristinea3d2ed82020-03-19 20:53:36 -0700309 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-mac20877162017-12-22 17:19:39 -0800313 EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event));
tsaichristinea3d2ed82020-03-19 20:53:36 -0700314 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-mac20877162017-12-22 17:19:39 -0800318 EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event));
tsaichristinea3d2ed82020-03-19 20:53:36 -0700319 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-mac20877162017-12-22 17:19:39 -0800323 EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event));
324
325 attributionMatcher->set_position(Position::LAST);
tsaichristinea3d2ed82020-03-19 20:53:36 -0700326 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-mac20877162017-12-22 17:19:39 -0800330 EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event));
tsaichristinea3d2ed82020-03-19 20:53:36 -0700331 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-mac20877162017-12-22 17:19:39 -0800335 EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event));
tsaichristinea3d2ed82020-03-19 20:53:36 -0700336 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-mac20877162017-12-22 17:19:39 -0800340 EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event));
tsaichristinea3d2ed82020-03-19 20:53:36 -0700341 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-mac20877162017-12-22 17:19:39 -0800345 EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event));
tsaichristinea3d2ed82020-03-19 20:53:36 -0700346 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-mac20877162017-12-22 17:19:39 -0800350 EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event));
tsaichristinea3d2ed82020-03-19 20:53:36 -0700351 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-mac20877162017-12-22 17:19:39 -0800355 EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event));
356}
357
tsaichristinea3d2ed82020-03-19 20:53:36 -0700358TEST(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 Rastogiffa34f052020-04-04 15:30:11 -0700376 // Make event without is_uid annotation.
tsaichristinea3d2ed82020-03-19 20:53:36 -0700377 LogEvent event1(/*uid=*/0, /*pid=*/0);
378 makeIntLogEvent(&event1, TAG_ID, 0, 1111);
tsaichristinea3d2ed82020-03-19 20:53:36 -0700379 EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event1));
380
Ruchir Rastogiffa34f052020-04-04 15:30:11 -0700381 // 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.
tsaichristinea3d2ed82020-03-19 20:53:36 -0700386 simpleMatcher->set_atom_id(TAG_ID_2);
387 EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event2));
388
Ruchir Rastogiffa34f052020-04-04 15:30:11 -0700389 // Event has is_uid annotation, but uid maps to different package name.
tsaichristinea3d2ed82020-03-19 20:53:36 -0700390 simpleMatcher->mutable_field_value_matcher(0)->set_eq_string("Pkg2");
391 EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event2));
392}
393
Yao Chen9b1140e2018-02-27 10:55:54 -0800394TEST(AtomMatcherTest, TestNeqAnyStringMatcher) {
395 UidMap uidMap;
396 uidMap.updateMap(
David Chenbd125272018-04-04 19:02:50 -0700397 1, {1111, 1111, 2222, 3333, 3333} /* uid list */, {1, 1, 2, 1, 2} /* version list */,
dwchen730403e2018-10-29 11:41:56 -0700398 {android::String16("v1"), android::String16("v1"), android::String16("v2"),
399 android::String16("v1"), android::String16("v2")},
Yao Chen9b1140e2018-02-27 10:55:54 -0800400 {android::String16("pkg0"), android::String16("pkg1"), android::String16("pkg1"),
dwchen730403e2018-10-29 11:41:56 -0700401 android::String16("Pkg2"), android::String16("PkG3")} /* package name list */,
402 {android::String16(""), android::String16(""), android::String16(""),
403 android::String16(""), android::String16("")});
Yao Chen9b1140e2018-02-27 10:55:54 -0800404
tsaichristinea3d2ed82020-03-19 20:53:36 -0700405 std::vector<int> attributionUids = {1111, 2222, 3333, 1066};
406 std::vector<string> attributionTags = {"location1", "location2", "location3", "location3"};
Yao Chen9b1140e2018-02-27 10:55:54 -0800407
408 // Set up the event
tsaichristinea3d2ed82020-03-19 20:53:36 -0700409 LogEvent event(/*uid=*/0, /*pid=*/0);
410 makeAttributionLogEvent(&event, TAG_ID, 0, attributionUids, attributionTags, "some value");
Yao Chen9b1140e2018-02-27 10:55:54 -0800411
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 Chend50f2ae2018-03-23 11:10:13 -0700425 ->mutable_neq_any_string();
Yao Chen9b1140e2018-02-27 10:55:54 -0800426 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
455TEST(AtomMatcherTest, TestEqAnyStringMatcher) {
456 UidMap uidMap;
457 uidMap.updateMap(
David Chenbd125272018-04-04 19:02:50 -0700458 1, {1111, 1111, 2222, 3333, 3333} /* uid list */, {1, 1, 2, 1, 2} /* version list */,
dwchen730403e2018-10-29 11:41:56 -0700459 {android::String16("v1"), android::String16("v1"), android::String16("v2"),
460 android::String16("v1"), android::String16("v2")},
Yao Chen9b1140e2018-02-27 10:55:54 -0800461 {android::String16("pkg0"), android::String16("pkg1"), android::String16("pkg1"),
dwchen730403e2018-10-29 11:41:56 -0700462 android::String16("Pkg2"), android::String16("PkG3")} /* package name list */,
463 {android::String16(""), android::String16(""), android::String16(""),
464 android::String16(""), android::String16("")});
Yao Chen9b1140e2018-02-27 10:55:54 -0800465
tsaichristinea3d2ed82020-03-19 20:53:36 -0700466 std::vector<int> attributionUids = {1067, 2222, 3333, 1066};
467 std::vector<string> attributionTags = {"location1", "location2", "location3", "location3"};
Yao Chen9b1140e2018-02-27 10:55:54 -0800468
469 // Set up the event
tsaichristinea3d2ed82020-03-19 20:53:36 -0700470 LogEvent event(/*uid=*/0, /*pid=*/0);
471 makeAttributionLogEvent(&event, TAG_ID, 0, attributionUids, attributionTags, "some value");
Yao Chen9b1140e2018-02-27 10:55:54 -0800472
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-mac20877162017-12-22 17:19:39 -0800519TEST(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
tsaichristinea3d2ed82020-03-19 20:53:36 -0700531 LogEvent event(/*uid=*/0, /*pid=*/0);
532 makeBoolLogEvent(&event, TAG_ID, 0, true, false);
David Chendd896942017-09-26 11:44:40 -0700533
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700534 // Test
535 keyValue1->set_eq_bool(true);
536 keyValue2->set_eq_bool(false);
Yangster-mac20877162017-12-22 17:19:39 -0800537 EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event));
David Chendd896942017-09-26 11:44:40 -0700538
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700539 keyValue1->set_eq_bool(false);
540 keyValue2->set_eq_bool(false);
Yangster-mac20877162017-12-22 17:19:39 -0800541 EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event));
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700542
Yangster-mac20877162017-12-22 17:19:39 -0800543 keyValue1->set_eq_bool(false);
544 keyValue2->set_eq_bool(true);
545 EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event));
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700546
547 keyValue1->set_eq_bool(true);
548 keyValue2->set_eq_bool(true);
Yangster-mac20877162017-12-22 17:19:39 -0800549 EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event));
David Chendd896942017-09-26 11:44:40 -0700550}
551
Stefan Lafonb8c9aa82017-12-03 14:27:25 -0800552TEST(AtomMatcherTest, TestStringMatcher) {
Yangster-mac20877162017-12-22 17:19:39 -0800553 UidMap uidMap;
David Chendd896942017-09-26 11:44:40 -0700554 // Set up the matcher
Stefan Lafonb8c9aa82017-12-03 14:27:25 -0800555 AtomMatcher matcher;
556 auto simpleMatcher = matcher.mutable_simple_atom_matcher();
Yangster-mac20877162017-12-22 17:19:39 -0800557 simpleMatcher->set_atom_id(TAG_ID);
558 auto keyValue = simpleMatcher->add_field_value_matcher();
559 keyValue->set_field(FIELD_ID_1);
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700560 keyValue->set_eq_string("some value");
David Chendd896942017-09-26 11:44:40 -0700561
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700562 // Set up the event
tsaichristinea3d2ed82020-03-19 20:53:36 -0700563 LogEvent event(/*uid=*/0, /*pid=*/0);
564 makeStringLogEvent(&event, TAG_ID, 0, "some value");
David Chendd896942017-09-26 11:44:40 -0700565
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700566 // Test
Yangster-mac20877162017-12-22 17:19:39 -0800567 EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event));
David Chendd896942017-09-26 11:44:40 -0700568}
569
Stefan Lafonb8c9aa82017-12-03 14:27:25 -0800570TEST(AtomMatcherTest, TestMultiFieldsMatcher) {
Yangster-mac20877162017-12-22 17:19:39 -0800571 UidMap uidMap;
Yao Chend41c4222017-11-15 19:26:14 -0800572 // Set up the matcher
Stefan Lafonb8c9aa82017-12-03 14:27:25 -0800573 AtomMatcher matcher;
574 auto simpleMatcher = matcher.mutable_simple_atom_matcher();
Yangster-mac20877162017-12-22 17:19:39 -0800575 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 Chend41c4222017-11-15 19:26:14 -0800580
581 // Set up the event
tsaichristinea3d2ed82020-03-19 20:53:36 -0700582 LogEvent event(/*uid=*/0, /*pid=*/0);
583 CreateTwoValueLogEvent(&event, TAG_ID, 0, 2, 3);
Yao Chend41c4222017-11-15 19:26:14 -0800584
585 // Test
586 keyValue1->set_eq_int(2);
587 keyValue2->set_eq_int(3);
Yangster-mac20877162017-12-22 17:19:39 -0800588 EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event));
Yao Chend41c4222017-11-15 19:26:14 -0800589
590 keyValue1->set_eq_int(2);
591 keyValue2->set_eq_int(4);
Yangster-mac20877162017-12-22 17:19:39 -0800592 EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event));
Yao Chend41c4222017-11-15 19:26:14 -0800593
594 keyValue1->set_eq_int(4);
595 keyValue2->set_eq_int(3);
Yangster-mac20877162017-12-22 17:19:39 -0800596 EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event));
Yao Chend41c4222017-11-15 19:26:14 -0800597}
598
Stefan Lafonb8c9aa82017-12-03 14:27:25 -0800599TEST(AtomMatcherTest, TestIntComparisonMatcher) {
Yangster-mac20877162017-12-22 17:19:39 -0800600 UidMap uidMap;
David Chendd896942017-09-26 11:44:40 -0700601 // Set up the matcher
Stefan Lafonb8c9aa82017-12-03 14:27:25 -0800602 AtomMatcher matcher;
603 auto simpleMatcher = matcher.mutable_simple_atom_matcher();
Yao Chen729093d2017-10-16 10:33:26 -0700604
Yangster-mac20877162017-12-22 17:19:39 -0800605 simpleMatcher->set_atom_id(TAG_ID);
606 auto keyValue = simpleMatcher->add_field_value_matcher();
607 keyValue->set_field(FIELD_ID_1);
David Chendd896942017-09-26 11:44:40 -0700608
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700609 // Set up the event
tsaichristinea3d2ed82020-03-19 20:53:36 -0700610 LogEvent event(/*uid=*/0, /*pid=*/0);
611 makeIntLogEvent(&event, TAG_ID, 0, 11);
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700612
613 // Test
614
615 // eq_int
616 keyValue->set_eq_int(10);
Yangster-mac20877162017-12-22 17:19:39 -0800617 EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event));
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700618 keyValue->set_eq_int(11);
Yangster-mac20877162017-12-22 17:19:39 -0800619 EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event));
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700620 keyValue->set_eq_int(12);
Yangster-mac20877162017-12-22 17:19:39 -0800621 EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event));
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700622
623 // lt_int
David Chendd896942017-09-26 11:44:40 -0700624 keyValue->set_lt_int(10);
Yangster-mac20877162017-12-22 17:19:39 -0800625 EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event));
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700626 keyValue->set_lt_int(11);
Yangster-mac20877162017-12-22 17:19:39 -0800627 EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event));
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700628 keyValue->set_lt_int(12);
Yangster-mac20877162017-12-22 17:19:39 -0800629 EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event));
David Chendd896942017-09-26 11:44:40 -0700630
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700631 // lte_int
David Chendd896942017-09-26 11:44:40 -0700632 keyValue->set_lte_int(10);
Yangster-mac20877162017-12-22 17:19:39 -0800633 EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event));
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700634 keyValue->set_lte_int(11);
Yangster-mac20877162017-12-22 17:19:39 -0800635 EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event));
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700636 keyValue->set_lte_int(12);
Yangster-mac20877162017-12-22 17:19:39 -0800637 EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event));
David Chendd896942017-09-26 11:44:40 -0700638
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700639 // gt_int
640 keyValue->set_gt_int(10);
Yangster-mac20877162017-12-22 17:19:39 -0800641 EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event));
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700642 keyValue->set_gt_int(11);
Yangster-mac20877162017-12-22 17:19:39 -0800643 EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event));
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700644 keyValue->set_gt_int(12);
Yangster-mac20877162017-12-22 17:19:39 -0800645 EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event));
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700646
647 // gte_int
David Chendd896942017-09-26 11:44:40 -0700648 keyValue->set_gte_int(10);
Yangster-mac20877162017-12-22 17:19:39 -0800649 EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event));
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700650 keyValue->set_gte_int(11);
Yangster-mac20877162017-12-22 17:19:39 -0800651 EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event));
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700652 keyValue->set_gte_int(12);
Yangster-mac20877162017-12-22 17:19:39 -0800653 EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event));
David Chendd896942017-09-26 11:44:40 -0700654}
655
Stefan Lafonb8c9aa82017-12-03 14:27:25 -0800656TEST(AtomMatcherTest, TestFloatComparisonMatcher) {
Yangster-mac20877162017-12-22 17:19:39 -0800657 UidMap uidMap;
David Chendd896942017-09-26 11:44:40 -0700658 // Set up the matcher
Stefan Lafonb8c9aa82017-12-03 14:27:25 -0800659 AtomMatcher matcher;
660 auto simpleMatcher = matcher.mutable_simple_atom_matcher();
Yangster-mac20877162017-12-22 17:19:39 -0800661 simpleMatcher->set_atom_id(TAG_ID);
Yao Chen729093d2017-10-16 10:33:26 -0700662
Yangster-mac20877162017-12-22 17:19:39 -0800663 auto keyValue = simpleMatcher->add_field_value_matcher();
664 keyValue->set_field(FIELD_ID_1);
David Chendd896942017-09-26 11:44:40 -0700665
tsaichristinea3d2ed82020-03-19 20:53:36 -0700666 LogEvent event1(/*uid=*/0, /*pid=*/0);
667 makeFloatLogEvent(&event1, TAG_ID, 0, 10.1f);
David Chendd896942017-09-26 11:44:40 -0700668 keyValue->set_lt_float(10.0);
Yangster-mac20877162017-12-22 17:19:39 -0800669 EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event1));
David Chendd896942017-09-26 11:44:40 -0700670
tsaichristinea3d2ed82020-03-19 20:53:36 -0700671 LogEvent event2(/*uid=*/0, /*pid=*/0);
672 makeFloatLogEvent(&event2, TAG_ID, 0, 9.9f);
Yangster-mac20877162017-12-22 17:19:39 -0800673 EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event2));
Yao Chen80235402017-11-13 20:42:25 -0800674
tsaichristinea3d2ed82020-03-19 20:53:36 -0700675 LogEvent event3(/*uid=*/0, /*pid=*/0);
676 makeFloatLogEvent(&event3, TAG_ID, 0, 10.1f);
David Chendd896942017-09-26 11:44:40 -0700677 keyValue->set_gt_float(10.0);
Yangster-mac20877162017-12-22 17:19:39 -0800678 EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event3));
Yao Chen80235402017-11-13 20:42:25 -0800679
tsaichristinea3d2ed82020-03-19 20:53:36 -0700680 LogEvent event4(/*uid=*/0, /*pid=*/0);
681 makeFloatLogEvent(&event4, TAG_ID, 0, 9.9f);
Yangster-mac20877162017-12-22 17:19:39 -0800682 EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event4));
David Chendd896942017-09-26 11:44:40 -0700683}
684
685// Helper for the composite matchers.
Stefan Lafonb8c9aa82017-12-03 14:27:25 -0800686void addSimpleMatcher(SimpleAtomMatcher* simpleMatcher, int tag, int key, int val) {
Yangster-mac20877162017-12-22 17:19:39 -0800687 simpleMatcher->set_atom_id(tag);
688 auto keyValue = simpleMatcher->add_field_value_matcher();
689 keyValue->set_field(key);
David Chendd896942017-09-26 11:44:40 -0700690 keyValue->set_eq_int(val);
691}
692
Stefan Lafonb8c9aa82017-12-03 14:27:25 -0800693TEST(AtomMatcherTest, TestAndMatcher) {
David Chendd896942017-09-26 11:44:40 -0700694 // Set up the matcher
Yao Chencaf339d2017-10-06 16:01:10 -0700695 LogicalOperation operation = LogicalOperation::AND;
David Chendd896942017-09-26 11:44:40 -0700696
Yao Chencaf339d2017-10-06 16:01:10 -0700697 vector<int> children;
698 children.push_back(0);
699 children.push_back(1);
700 children.push_back(2);
David Chendd896942017-09-26 11:44:40 -0700701
Yao Chencaf339d2017-10-06 16:01:10 -0700702 vector<MatchingState> matcherResults;
703 matcherResults.push_back(MatchingState::kMatched);
704 matcherResults.push_back(MatchingState::kNotMatched);
705 matcherResults.push_back(MatchingState::kMatched);
David Chendd896942017-09-26 11:44:40 -0700706
Yao Chencaf339d2017-10-06 16:01:10 -0700707 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 Chendd896942017-09-26 11:44:40 -0700715}
716
Stefan Lafonb8c9aa82017-12-03 14:27:25 -0800717TEST(AtomMatcherTest, TestOrMatcher) {
David Chendd896942017-09-26 11:44:40 -0700718 // Set up the matcher
Yao Chencaf339d2017-10-06 16:01:10 -0700719 LogicalOperation operation = LogicalOperation::OR;
David Chendd896942017-09-26 11:44:40 -0700720
Yao Chencaf339d2017-10-06 16:01:10 -0700721 vector<int> children;
722 children.push_back(0);
723 children.push_back(1);
724 children.push_back(2);
David Chendd896942017-09-26 11:44:40 -0700725
Yao Chencaf339d2017-10-06 16:01:10 -0700726 vector<MatchingState> matcherResults;
727 matcherResults.push_back(MatchingState::kMatched);
728 matcherResults.push_back(MatchingState::kNotMatched);
729 matcherResults.push_back(MatchingState::kMatched);
David Chendd896942017-09-26 11:44:40 -0700730
Yao Chencaf339d2017-10-06 16:01:10 -0700731 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 Chendd896942017-09-26 11:44:40 -0700739}
740
Stefan Lafonb8c9aa82017-12-03 14:27:25 -0800741TEST(AtomMatcherTest, TestNotMatcher) {
David Chendd896942017-09-26 11:44:40 -0700742 // Set up the matcher
Yao Chencaf339d2017-10-06 16:01:10 -0700743 LogicalOperation operation = LogicalOperation::NOT;
David Chendd896942017-09-26 11:44:40 -0700744
Yao Chencaf339d2017-10-06 16:01:10 -0700745 vector<int> children;
746 children.push_back(0);
David Chendd896942017-09-26 11:44:40 -0700747
Yao Chencaf339d2017-10-06 16:01:10 -0700748 vector<MatchingState> matcherResults;
749 matcherResults.push_back(MatchingState::kMatched);
David Chendd896942017-09-26 11:44:40 -0700750
Yao Chencaf339d2017-10-06 16:01:10 -0700751 EXPECT_FALSE(combinationMatch(children, operation, matcherResults));
752
753 matcherResults.clear();
754 matcherResults.push_back(MatchingState::kNotMatched);
755 EXPECT_TRUE(combinationMatch(children, operation, matcherResults));
David Chendd896942017-09-26 11:44:40 -0700756}
757
Stefan Lafonb8c9aa82017-12-03 14:27:25 -0800758TEST(AtomMatcherTest, TestNandMatcher) {
David Chendd896942017-09-26 11:44:40 -0700759 // Set up the matcher
Yao Chencaf339d2017-10-06 16:01:10 -0700760 LogicalOperation operation = LogicalOperation::NAND;
David Chendd896942017-09-26 11:44:40 -0700761
Yao Chencaf339d2017-10-06 16:01:10 -0700762 vector<int> children;
763 children.push_back(0);
764 children.push_back(1);
David Chendd896942017-09-26 11:44:40 -0700765
Yao Chencaf339d2017-10-06 16:01:10 -0700766 vector<MatchingState> matcherResults;
767 matcherResults.push_back(MatchingState::kMatched);
768 matcherResults.push_back(MatchingState::kNotMatched);
David Chendd896942017-09-26 11:44:40 -0700769
Yao Chencaf339d2017-10-06 16:01:10 -0700770 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 Chendd896942017-09-26 11:44:40 -0700781}
782
Stefan Lafonb8c9aa82017-12-03 14:27:25 -0800783TEST(AtomMatcherTest, TestNorMatcher) {
David Chendd896942017-09-26 11:44:40 -0700784 // Set up the matcher
Yao Chencaf339d2017-10-06 16:01:10 -0700785 LogicalOperation operation = LogicalOperation::NOR;
David Chendd896942017-09-26 11:44:40 -0700786
Yao Chencaf339d2017-10-06 16:01:10 -0700787 vector<int> children;
788 children.push_back(0);
789 children.push_back(1);
David Chendd896942017-09-26 11:44:40 -0700790
Yao Chencaf339d2017-10-06 16:01:10 -0700791 vector<MatchingState> matcherResults;
792 matcherResults.push_back(MatchingState::kMatched);
793 matcherResults.push_back(MatchingState::kNotMatched);
David Chendd896942017-09-26 11:44:40 -0700794
Yao Chencaf339d2017-10-06 16:01:10 -0700795 EXPECT_FALSE(combinationMatch(children, operation, matcherResults));
David Chendd896942017-09-26 11:44:40 -0700796
Yao Chencaf339d2017-10-06 16:01:10 -0700797 matcherResults.clear();
798 matcherResults.push_back(MatchingState::kNotMatched);
799 matcherResults.push_back(MatchingState::kNotMatched);
800 EXPECT_TRUE(combinationMatch(children, operation, matcherResults));
David Chendd896942017-09-26 11:44:40 -0700801
Yao Chencaf339d2017-10-06 16:01:10 -0700802 matcherResults.clear();
803 matcherResults.push_back(MatchingState::kMatched);
804 matcherResults.push_back(MatchingState::kMatched);
805 EXPECT_FALSE(combinationMatch(children, operation, matcherResults));
David Chendd896942017-09-26 11:44:40 -0700806}
David Chendd896942017-09-26 11:44:40 -0700807#else
Yao Chen44cf27c2017-09-14 22:32:50 -0700808GTEST_LOG_(INFO) << "This test does nothing.\n";
David Chendd896942017-09-26 11:44:40 -0700809#endif