blob: 9cdf5827d1f8342d2f1f2836dbb5e178a26c2790 [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>
16#include <log/log_event_list.h>
17#include <log/log_read.h>
18#include <log/logprint.h>
David Chendd896942017-09-26 11:44:40 -070019#include <stdio.h>
20
tsaichristinea3d2ed82020-03-19 20:53:36 -070021#include "frameworks/base/cmds/statsd/src/statsd_config.pb.h"
22#include "matchers/matcher_util.h"
23#include "stats_event.h"
24#include "stats_log_util.h"
25#include "stats_util.h"
26#include "statsd_test_util.h"
27
David Chendd896942017-09-26 11:44:40 -070028using namespace android::os::statsd;
29using std::unordered_map;
Yao Chencaf339d2017-10-06 16:01:10 -070030using std::vector;
David Chendd896942017-09-26 11:44:40 -070031
Yao Chen80235402017-11-13 20:42:25 -080032const int32_t TAG_ID = 123;
tsaichristine7a57b8e2019-06-24 18:25:38 -070033const int32_t TAG_ID_2 = 28; // hardcoded tag of atom with uid field
Joe Onoratoc4dfae52017-10-17 23:38:21 -070034const int FIELD_ID_1 = 1;
35const int FIELD_ID_2 = 2;
36const int FIELD_ID_3 = 2;
37
Yangster-mac20877162017-12-22 17:19:39 -080038const int ATTRIBUTION_UID_FIELD_ID = 1;
39const int ATTRIBUTION_TAG_FIELD_ID = 2;
40
Stefan Lafoncdb1a0e2017-09-27 20:24:15 -070041
David Chendd896942017-09-26 11:44:40 -070042#ifdef __ANDROID__
tsaichristinea3d2ed82020-03-19 20:53:36 -070043
44namespace {
45
46void makeIntLogEvent(LogEvent* logEvent, const int32_t atomId, const int64_t timestamp,
47 const int32_t value) {
48 AStatsEvent* statsEvent = AStatsEvent_obtain();
49 AStatsEvent_setAtomId(statsEvent, atomId);
50 AStatsEvent_overwriteTimestamp(statsEvent, timestamp);
tsaichristinea3d2ed82020-03-19 20:53:36 -070051 AStatsEvent_writeInt32(statsEvent, value);
tsaichristinea3d2ed82020-03-19 20:53:36 -070052
tsaichristine8dca82e2020-04-07 09:40:03 -070053 parseStatsEventToLogEvent(statsEvent, logEvent);
tsaichristinea3d2ed82020-03-19 20:53:36 -070054}
55
56void makeFloatLogEvent(LogEvent* logEvent, const int32_t atomId, const int64_t timestamp,
57 const float floatValue) {
58 AStatsEvent* statsEvent = AStatsEvent_obtain();
59 AStatsEvent_setAtomId(statsEvent, atomId);
60 AStatsEvent_overwriteTimestamp(statsEvent, timestamp);
tsaichristinea3d2ed82020-03-19 20:53:36 -070061 AStatsEvent_writeFloat(statsEvent, floatValue);
tsaichristinea3d2ed82020-03-19 20:53:36 -070062
tsaichristine8dca82e2020-04-07 09:40:03 -070063 parseStatsEventToLogEvent(statsEvent, logEvent);
tsaichristinea3d2ed82020-03-19 20:53:36 -070064}
65
66void makeStringLogEvent(LogEvent* logEvent, const int32_t atomId, const int64_t timestamp,
67 const string& name) {
68 AStatsEvent* statsEvent = AStatsEvent_obtain();
69 AStatsEvent_setAtomId(statsEvent, atomId);
70 AStatsEvent_overwriteTimestamp(statsEvent, timestamp);
tsaichristinea3d2ed82020-03-19 20:53:36 -070071 AStatsEvent_writeString(statsEvent, name.c_str());
tsaichristinea3d2ed82020-03-19 20:53:36 -070072
tsaichristine8dca82e2020-04-07 09:40:03 -070073 parseStatsEventToLogEvent(statsEvent, logEvent);
tsaichristinea3d2ed82020-03-19 20:53:36 -070074}
75
76void makeIntStringLogEvent(LogEvent* logEvent, const int32_t atomId, const int64_t timestamp,
77 const int32_t value, const string& name) {
78 AStatsEvent* statsEvent = AStatsEvent_obtain();
79 AStatsEvent_setAtomId(statsEvent, atomId);
80 AStatsEvent_overwriteTimestamp(statsEvent, timestamp);
81
82 AStatsEvent_writeInt32(statsEvent, value);
83 AStatsEvent_writeString(statsEvent, name.c_str());
tsaichristinea3d2ed82020-03-19 20:53:36 -070084
tsaichristine8dca82e2020-04-07 09:40:03 -070085 parseStatsEventToLogEvent(statsEvent, logEvent);
tsaichristinea3d2ed82020-03-19 20:53:36 -070086}
87
88void makeAttributionLogEvent(LogEvent* logEvent, const int32_t atomId, const int64_t timestamp,
89 const vector<int>& attributionUids,
90 const vector<string>& attributionTags, const string& name) {
91 AStatsEvent* statsEvent = AStatsEvent_obtain();
92 AStatsEvent_setAtomId(statsEvent, atomId);
93 AStatsEvent_overwriteTimestamp(statsEvent, timestamp);
94
tsaichristine8dca82e2020-04-07 09:40:03 -070095 writeAttribution(statsEvent, attributionUids, attributionTags);
tsaichristinea3d2ed82020-03-19 20:53:36 -070096 AStatsEvent_writeString(statsEvent, name.c_str());
tsaichristinea3d2ed82020-03-19 20:53:36 -070097
tsaichristine8dca82e2020-04-07 09:40:03 -070098 parseStatsEventToLogEvent(statsEvent, logEvent);
tsaichristinea3d2ed82020-03-19 20:53:36 -070099}
100
101void makeBoolLogEvent(LogEvent* logEvent, const int32_t atomId, const int64_t timestamp,
102 const bool bool1, const bool bool2) {
103 AStatsEvent* statsEvent = AStatsEvent_obtain();
104 AStatsEvent_setAtomId(statsEvent, atomId);
105 AStatsEvent_overwriteTimestamp(statsEvent, timestamp);
106
107 AStatsEvent_writeBool(statsEvent, bool1);
108 AStatsEvent_writeBool(statsEvent, bool2);
tsaichristinea3d2ed82020-03-19 20:53:36 -0700109
tsaichristine8dca82e2020-04-07 09:40:03 -0700110 parseStatsEventToLogEvent(statsEvent, logEvent);
tsaichristinea3d2ed82020-03-19 20:53:36 -0700111}
112
113} // anonymous namespace
114
115TEST(AtomMatcherTest, TestSimpleMatcher) {
116 UidMap uidMap;
117
118 // Set up the matcher
119 AtomMatcher matcher;
120 auto simpleMatcher = matcher.mutable_simple_atom_matcher();
121 simpleMatcher->set_atom_id(TAG_ID);
122
123 LogEvent event(/*uid=*/0, /*pid=*/0);
124 makeIntLogEvent(&event, TAG_ID, 0, 11);
125
126 // Test
127 EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event));
128
129 // Wrong tag id.
130 simpleMatcher->set_atom_id(TAG_ID + 1);
131 EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event));
132}
133
134TEST(AtomMatcherTest, TestAttributionMatcher) {
135 UidMap uidMap;
136 std::vector<int> attributionUids = {1111, 2222, 3333};
137 std::vector<string> attributionTags = {"location1", "location2", "location3"};
138
139 // Set up the log event.
140 LogEvent event(/*uid=*/0, /*pid=*/0);
141 makeAttributionLogEvent(&event, TAG_ID, 0, attributionUids, attributionTags, "some value");
142
143 // Set up the matcher
144 AtomMatcher matcher;
145 auto simpleMatcher = matcher.mutable_simple_atom_matcher();
146 simpleMatcher->set_atom_id(TAG_ID);
147
148 // Match first node.
149 auto attributionMatcher = simpleMatcher->add_field_value_matcher();
150 attributionMatcher->set_field(FIELD_ID_1);
151 attributionMatcher->set_position(Position::FIRST);
152 attributionMatcher->mutable_matches_tuple()->add_field_value_matcher()->set_field(
153 ATTRIBUTION_TAG_FIELD_ID);
154 attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string(
155 "tag");
156
157 auto fieldMatcher = simpleMatcher->add_field_value_matcher();
158 fieldMatcher->set_field(FIELD_ID_2);
159 fieldMatcher->set_eq_string("some value");
160
161 // Tag not matched.
162 EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event));
163 attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string(
164 "location3");
165 EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event));
166 attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string(
167 "location1");
168 EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event));
169
170 // Match last node.
171 attributionMatcher->set_position(Position::LAST);
172 EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event));
173 attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string(
174 "location3");
175 EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event));
176
177 // Match any node.
178 attributionMatcher->set_position(Position::ANY);
179 EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event));
180 attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string(
181 "location1");
182 EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event));
183 attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string(
184 "location2");
185 EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event));
186 attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string(
187 "location3");
188 EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event));
189 attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string(
190 "location4");
191 EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event));
192
193 // Attribution match but primitive field not match.
194 attributionMatcher->set_position(Position::ANY);
195 attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string(
196 "location2");
197 fieldMatcher->set_eq_string("wrong value");
198 EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event));
199
200 fieldMatcher->set_eq_string("some value");
201
202 // Uid match.
203 attributionMatcher->set_position(Position::ANY);
204 attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_field(
205 ATTRIBUTION_UID_FIELD_ID);
206 attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string(
207 "pkg0");
208 EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event));
209
210 uidMap.updateMap(
211 1, {1111, 1111, 2222, 3333, 3333} /* uid list */, {1, 1, 2, 1, 2} /* version list */,
212 {android::String16("v1"), android::String16("v1"), android::String16("v2"),
213 android::String16("v1"), android::String16("v2")},
214 {android::String16("pkg0"), android::String16("pkg1"), android::String16("pkg1"),
215 android::String16("Pkg2"), android::String16("PkG3")} /* package name list */,
216 {android::String16(""), android::String16(""), android::String16(""),
217 android::String16(""), android::String16("")});
218
219 EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event));
220 attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string(
221 "pkg3");
222 EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event));
223 attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string(
224 "pkg2");
225 EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event));
226 attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string(
227 "pkg1");
228 EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event));
229 attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string(
230 "pkg0");
231 EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event));
232
233 attributionMatcher->set_position(Position::FIRST);
234 attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string(
235 "pkg0");
236 EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event));
237 attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string(
238 "pkg3");
239 EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event));
240 attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string(
241 "pkg2");
242 EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event));
243 attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string(
244 "pkg1");
245 EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event));
246
247 attributionMatcher->set_position(Position::LAST);
248 attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string(
249 "pkg0");
250 EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event));
251 attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string(
252 "pkg3");
253 EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event));
254 attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string(
255 "pkg2");
256 EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event));
257 attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string(
258 "pkg1");
259 EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event));
260
261 // Uid + tag.
262 attributionMatcher->set_position(Position::ANY);
263 attributionMatcher->mutable_matches_tuple()->add_field_value_matcher()->set_field(
264 ATTRIBUTION_TAG_FIELD_ID);
265 attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string(
266 "pkg0");
267 attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(1)->set_eq_string(
268 "location1");
269 EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event));
270 attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string(
271 "pkg1");
272 attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(1)->set_eq_string(
273 "location1");
274 EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event));
275 attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string(
276 "pkg1");
277 attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(1)->set_eq_string(
278 "location2");
279 EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event));
280 attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string(
281 "pkg2");
282 attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(1)->set_eq_string(
283 "location3");
284 EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event));
285 attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string(
286 "pkg3");
287 attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(1)->set_eq_string(
288 "location3");
289 EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event));
290 attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string(
291 "pkg3");
292 attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(1)->set_eq_string(
293 "location1");
294 EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event));
295
296 attributionMatcher->set_position(Position::FIRST);
297 attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string(
298 "pkg0");
299 attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(1)->set_eq_string(
300 "location1");
301 EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event));
302 attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string(
303 "pkg1");
304 attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(1)->set_eq_string(
305 "location1");
306 EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event));
307 attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string(
308 "pkg1");
309 attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(1)->set_eq_string(
310 "location2");
311 EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event));
312 attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string(
313 "pkg2");
314 attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(1)->set_eq_string(
315 "location3");
316 EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event));
317 attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string(
318 "pkg3");
319 attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(1)->set_eq_string(
320 "location3");
321 EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event));
322 attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string(
323 "pkg3");
324 attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(1)->set_eq_string(
325 "location1");
326 EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event));
327
328 attributionMatcher->set_position(Position::LAST);
329 attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string(
330 "pkg0");
331 attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(1)->set_eq_string(
332 "location1");
333 EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event));
334 attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string(
335 "pkg1");
336 attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(1)->set_eq_string(
337 "location1");
338 EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event));
339 attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string(
340 "pkg1");
341 attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(1)->set_eq_string(
342 "location2");
343 EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event));
344 attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string(
345 "pkg2");
346 attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(1)->set_eq_string(
347 "location3");
348 EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event));
349 attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string(
350 "pkg3");
351 attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(1)->set_eq_string(
352 "location3");
353 EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event));
354 attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string(
355 "pkg3");
356 attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(1)->set_eq_string(
357 "location1");
358 EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event));
359}
360
361TEST(AtomMatcherTest, TestUidFieldMatcher) {
362 UidMap uidMap;
363 uidMap.updateMap(
364 1, {1111, 1111, 2222, 3333, 3333} /* uid list */, {1, 1, 2, 1, 2} /* version list */,
365 {android::String16("v1"), android::String16("v1"), android::String16("v2"),
366 android::String16("v1"), android::String16("v2")},
367 {android::String16("pkg0"), android::String16("pkg1"), android::String16("pkg1"),
368 android::String16("Pkg2"), android::String16("PkG3")} /* package name list */,
369 {android::String16(""), android::String16(""), android::String16(""),
370 android::String16(""), android::String16("")});
371
372 // Set up matcher
373 AtomMatcher matcher;
374 auto simpleMatcher = matcher.mutable_simple_atom_matcher();
375 simpleMatcher->set_atom_id(TAG_ID);
376 simpleMatcher->add_field_value_matcher()->set_field(1);
377 simpleMatcher->mutable_field_value_matcher(0)->set_eq_string("pkg0");
378
379 // Set up the event
380 LogEvent event1(/*uid=*/0, /*pid=*/0);
381 makeIntLogEvent(&event1, TAG_ID, 0, 1111);
382
383 LogEvent event2(/*uid=*/0, /*pid=*/0);
384 makeIntStringLogEvent(&event2, TAG_ID_2, 0, 1111, "some value");
385
386 // Tag not in kAtomsWithUidField
387 EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event1));
388
389 // Tag found in kAtomsWithUidField and has matching uid
390 simpleMatcher->set_atom_id(TAG_ID_2);
391 EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event2));
392
393 // Tag found in kAtomsWithUidField but has non-matching uid
394 simpleMatcher->mutable_field_value_matcher(0)->set_eq_string("Pkg2");
395 EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event2));
396}
397
398TEST(AtomMatcherTest, TestNeqAnyStringMatcher) {
399 UidMap uidMap;
400 uidMap.updateMap(
401 1, {1111, 1111, 2222, 3333, 3333} /* uid list */, {1, 1, 2, 1, 2} /* version list */,
402 {android::String16("v1"), android::String16("v1"), android::String16("v2"),
403 android::String16("v1"), android::String16("v2")},
404 {android::String16("pkg0"), android::String16("pkg1"), android::String16("pkg1"),
405 android::String16("Pkg2"), android::String16("PkG3")} /* package name list */,
406 {android::String16(""), android::String16(""), android::String16(""),
407 android::String16(""), android::String16("")});
408
409 std::vector<int> attributionUids = {1111, 2222, 3333, 1066};
410 std::vector<string> attributionTags = {"location1", "location2", "location3", "location3"};
411
412 // Set up the event
413 LogEvent event(/*uid=*/0, /*pid=*/0);
414 makeAttributionLogEvent(&event, TAG_ID, 0, attributionUids, attributionTags, "some value");
415
416 // Set up the matcher
417 AtomMatcher matcher;
418 auto simpleMatcher = matcher.mutable_simple_atom_matcher();
419 simpleMatcher->set_atom_id(TAG_ID);
420
421 // Match first node.
422 auto attributionMatcher = simpleMatcher->add_field_value_matcher();
423 attributionMatcher->set_field(FIELD_ID_1);
424 attributionMatcher->set_position(Position::FIRST);
425 attributionMatcher->mutable_matches_tuple()->add_field_value_matcher()->set_field(
426 ATTRIBUTION_UID_FIELD_ID);
427 auto neqStringList = attributionMatcher->mutable_matches_tuple()
428 ->mutable_field_value_matcher(0)
429 ->mutable_neq_any_string();
430 neqStringList->add_str_value("pkg2");
431 neqStringList->add_str_value("pkg3");
432
433 auto fieldMatcher = simpleMatcher->add_field_value_matcher();
434 fieldMatcher->set_field(FIELD_ID_2);
435 fieldMatcher->set_eq_string("some value");
436
437 EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event));
438
439 neqStringList->Clear();
440 neqStringList->add_str_value("pkg1");
441 neqStringList->add_str_value("pkg3");
442 EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event));
443
444 attributionMatcher->set_position(Position::ANY);
445 neqStringList->Clear();
446 neqStringList->add_str_value("maps.com");
447 EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event));
448
449 neqStringList->Clear();
450 neqStringList->add_str_value("PkG3");
451 EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event));
452
453 attributionMatcher->set_position(Position::LAST);
454 neqStringList->Clear();
455 neqStringList->add_str_value("AID_STATSD");
456 EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event));
457}
458
459TEST(AtomMatcherTest, TestEqAnyStringMatcher) {
460 UidMap uidMap;
461 uidMap.updateMap(
462 1, {1111, 1111, 2222, 3333, 3333} /* uid list */, {1, 1, 2, 1, 2} /* version list */,
463 {android::String16("v1"), android::String16("v1"), android::String16("v2"),
464 android::String16("v1"), android::String16("v2")},
465 {android::String16("pkg0"), android::String16("pkg1"), android::String16("pkg1"),
466 android::String16("Pkg2"), android::String16("PkG3")} /* package name list */,
467 {android::String16(""), android::String16(""), android::String16(""),
468 android::String16(""), android::String16("")});
469
470 std::vector<int> attributionUids = {1067, 2222, 3333, 1066};
471 std::vector<string> attributionTags = {"location1", "location2", "location3", "location3"};
472
473 // Set up the event
474 LogEvent event(/*uid=*/0, /*pid=*/0);
475 makeAttributionLogEvent(&event, TAG_ID, 0, attributionUids, attributionTags, "some value");
476
477 // Set up the matcher
478 AtomMatcher matcher;
479 auto simpleMatcher = matcher.mutable_simple_atom_matcher();
480 simpleMatcher->set_atom_id(TAG_ID);
481
482 // Match first node.
483 auto attributionMatcher = simpleMatcher->add_field_value_matcher();
484 attributionMatcher->set_field(FIELD_ID_1);
485 attributionMatcher->set_position(Position::FIRST);
486 attributionMatcher->mutable_matches_tuple()->add_field_value_matcher()->set_field(
487 ATTRIBUTION_UID_FIELD_ID);
488 auto eqStringList = attributionMatcher->mutable_matches_tuple()
489 ->mutable_field_value_matcher(0)
490 ->mutable_eq_any_string();
491 eqStringList->add_str_value("AID_ROOT");
492 eqStringList->add_str_value("AID_INCIDENTD");
493
494 auto fieldMatcher = simpleMatcher->add_field_value_matcher();
495 fieldMatcher->set_field(FIELD_ID_2);
496 fieldMatcher->set_eq_string("some value");
497
498 EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event));
499
500 attributionMatcher->set_position(Position::ANY);
501 eqStringList->Clear();
502 eqStringList->add_str_value("AID_STATSD");
503 EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event));
504
505 eqStringList->Clear();
506 eqStringList->add_str_value("pkg1");
507 EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event));
508
509 auto normalStringField = fieldMatcher->mutable_eq_any_string();
510 normalStringField->add_str_value("some value123");
511 normalStringField->add_str_value("some value");
512 EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event));
513
514 normalStringField->Clear();
515 normalStringField->add_str_value("AID_STATSD");
516 EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event));
517
518 eqStringList->Clear();
519 eqStringList->add_str_value("maps.com");
520 EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event));
521}
522
523TEST(AtomMatcherTest, TestBoolMatcher) {
524 UidMap uidMap;
525 // Set up the matcher
526 AtomMatcher matcher;
527 auto simpleMatcher = matcher.mutable_simple_atom_matcher();
528 simpleMatcher->set_atom_id(TAG_ID);
529 auto keyValue1 = simpleMatcher->add_field_value_matcher();
530 keyValue1->set_field(FIELD_ID_1);
531 auto keyValue2 = simpleMatcher->add_field_value_matcher();
532 keyValue2->set_field(FIELD_ID_2);
533
534 // Set up the event
535 LogEvent event(/*uid=*/0, /*pid=*/0);
536 makeBoolLogEvent(&event, TAG_ID, 0, true, false);
537
538 // Test
539 keyValue1->set_eq_bool(true);
540 keyValue2->set_eq_bool(false);
541 EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event));
542
543 keyValue1->set_eq_bool(false);
544 keyValue2->set_eq_bool(false);
545 EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event));
546
547 keyValue1->set_eq_bool(false);
548 keyValue2->set_eq_bool(true);
549 EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event));
550
551 keyValue1->set_eq_bool(true);
552 keyValue2->set_eq_bool(true);
553 EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event));
554}
555
556TEST(AtomMatcherTest, TestStringMatcher) {
557 UidMap uidMap;
558 // Set up the matcher
559 AtomMatcher matcher;
560 auto simpleMatcher = matcher.mutable_simple_atom_matcher();
561 simpleMatcher->set_atom_id(TAG_ID);
562 auto keyValue = simpleMatcher->add_field_value_matcher();
563 keyValue->set_field(FIELD_ID_1);
564 keyValue->set_eq_string("some value");
565
566 // Set up the event
567 LogEvent event(/*uid=*/0, /*pid=*/0);
568 makeStringLogEvent(&event, TAG_ID, 0, "some value");
569
570 // Test
571 EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event));
572}
573
574TEST(AtomMatcherTest, TestMultiFieldsMatcher) {
575 UidMap uidMap;
576 // Set up the matcher
577 AtomMatcher matcher;
578 auto simpleMatcher = matcher.mutable_simple_atom_matcher();
579 simpleMatcher->set_atom_id(TAG_ID);
580 auto keyValue1 = simpleMatcher->add_field_value_matcher();
581 keyValue1->set_field(FIELD_ID_1);
582 auto keyValue2 = simpleMatcher->add_field_value_matcher();
583 keyValue2->set_field(FIELD_ID_2);
584
585 // Set up the event
586 LogEvent event(/*uid=*/0, /*pid=*/0);
587 CreateTwoValueLogEvent(&event, TAG_ID, 0, 2, 3);
588
589 // Test
590 keyValue1->set_eq_int(2);
591 keyValue2->set_eq_int(3);
592 EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event));
593
594 keyValue1->set_eq_int(2);
595 keyValue2->set_eq_int(4);
596 EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event));
597
598 keyValue1->set_eq_int(4);
599 keyValue2->set_eq_int(3);
600 EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event));
601}
602
603TEST(AtomMatcherTest, TestIntComparisonMatcher) {
604 UidMap uidMap;
605 // Set up the matcher
606 AtomMatcher matcher;
607 auto simpleMatcher = matcher.mutable_simple_atom_matcher();
608
609 simpleMatcher->set_atom_id(TAG_ID);
610 auto keyValue = simpleMatcher->add_field_value_matcher();
611 keyValue->set_field(FIELD_ID_1);
612
613 // Set up the event
614 LogEvent event(/*uid=*/0, /*pid=*/0);
615 makeIntLogEvent(&event, TAG_ID, 0, 11);
616
617 // Test
618
619 // eq_int
620 keyValue->set_eq_int(10);
621 EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event));
622 keyValue->set_eq_int(11);
623 EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event));
624 keyValue->set_eq_int(12);
625 EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event));
626
627 // lt_int
628 keyValue->set_lt_int(10);
629 EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event));
630 keyValue->set_lt_int(11);
631 EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event));
632 keyValue->set_lt_int(12);
633 EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event));
634
635 // lte_int
636 keyValue->set_lte_int(10);
637 EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event));
638 keyValue->set_lte_int(11);
639 EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event));
640 keyValue->set_lte_int(12);
641 EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event));
642
643 // gt_int
644 keyValue->set_gt_int(10);
645 EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event));
646 keyValue->set_gt_int(11);
647 EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event));
648 keyValue->set_gt_int(12);
649 EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event));
650
651 // gte_int
652 keyValue->set_gte_int(10);
653 EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event));
654 keyValue->set_gte_int(11);
655 EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event));
656 keyValue->set_gte_int(12);
657 EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event));
658}
659
660TEST(AtomMatcherTest, TestFloatComparisonMatcher) {
661 UidMap uidMap;
662 // Set up the matcher
663 AtomMatcher matcher;
664 auto simpleMatcher = matcher.mutable_simple_atom_matcher();
665 simpleMatcher->set_atom_id(TAG_ID);
666
667 auto keyValue = simpleMatcher->add_field_value_matcher();
668 keyValue->set_field(FIELD_ID_1);
669
670 LogEvent event1(/*uid=*/0, /*pid=*/0);
671 makeFloatLogEvent(&event1, TAG_ID, 0, 10.1f);
672 keyValue->set_lt_float(10.0);
673 EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event1));
674
675 LogEvent event2(/*uid=*/0, /*pid=*/0);
676 makeFloatLogEvent(&event2, TAG_ID, 0, 9.9f);
677 EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event2));
678
679 LogEvent event3(/*uid=*/0, /*pid=*/0);
680 makeFloatLogEvent(&event3, TAG_ID, 0, 10.1f);
681 keyValue->set_gt_float(10.0);
682 EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event3));
683
684 LogEvent event4(/*uid=*/0, /*pid=*/0);
685 makeFloatLogEvent(&event4, TAG_ID, 0, 9.9f);
686 EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event4));
687}
David Chendd896942017-09-26 11:44:40 -0700688
689// Helper for the composite matchers.
Stefan Lafonb8c9aa82017-12-03 14:27:25 -0800690void addSimpleMatcher(SimpleAtomMatcher* simpleMatcher, int tag, int key, int val) {
Yangster-mac20877162017-12-22 17:19:39 -0800691 simpleMatcher->set_atom_id(tag);
692 auto keyValue = simpleMatcher->add_field_value_matcher();
693 keyValue->set_field(key);
David Chendd896942017-09-26 11:44:40 -0700694 keyValue->set_eq_int(val);
695}
696
Stefan Lafonb8c9aa82017-12-03 14:27:25 -0800697TEST(AtomMatcherTest, TestAndMatcher) {
David Chendd896942017-09-26 11:44:40 -0700698 // Set up the matcher
Yao Chencaf339d2017-10-06 16:01:10 -0700699 LogicalOperation operation = LogicalOperation::AND;
David Chendd896942017-09-26 11:44:40 -0700700
Yao Chencaf339d2017-10-06 16:01:10 -0700701 vector<int> children;
702 children.push_back(0);
703 children.push_back(1);
704 children.push_back(2);
David Chendd896942017-09-26 11:44:40 -0700705
Yao Chencaf339d2017-10-06 16:01:10 -0700706 vector<MatchingState> matcherResults;
707 matcherResults.push_back(MatchingState::kMatched);
708 matcherResults.push_back(MatchingState::kNotMatched);
709 matcherResults.push_back(MatchingState::kMatched);
David Chendd896942017-09-26 11:44:40 -0700710
Yao Chencaf339d2017-10-06 16:01:10 -0700711 EXPECT_FALSE(combinationMatch(children, operation, matcherResults));
712
713 matcherResults.clear();
714 matcherResults.push_back(MatchingState::kMatched);
715 matcherResults.push_back(MatchingState::kMatched);
716 matcherResults.push_back(MatchingState::kMatched);
717
718 EXPECT_TRUE(combinationMatch(children, operation, matcherResults));
David Chendd896942017-09-26 11:44:40 -0700719}
720
Stefan Lafonb8c9aa82017-12-03 14:27:25 -0800721TEST(AtomMatcherTest, TestOrMatcher) {
David Chendd896942017-09-26 11:44:40 -0700722 // Set up the matcher
Yao Chencaf339d2017-10-06 16:01:10 -0700723 LogicalOperation operation = LogicalOperation::OR;
David Chendd896942017-09-26 11:44:40 -0700724
Yao Chencaf339d2017-10-06 16:01:10 -0700725 vector<int> children;
726 children.push_back(0);
727 children.push_back(1);
728 children.push_back(2);
David Chendd896942017-09-26 11:44:40 -0700729
Yao Chencaf339d2017-10-06 16:01:10 -0700730 vector<MatchingState> matcherResults;
731 matcherResults.push_back(MatchingState::kMatched);
732 matcherResults.push_back(MatchingState::kNotMatched);
733 matcherResults.push_back(MatchingState::kMatched);
David Chendd896942017-09-26 11:44:40 -0700734
Yao Chencaf339d2017-10-06 16:01:10 -0700735 EXPECT_TRUE(combinationMatch(children, operation, matcherResults));
736
737 matcherResults.clear();
738 matcherResults.push_back(MatchingState::kNotMatched);
739 matcherResults.push_back(MatchingState::kNotMatched);
740 matcherResults.push_back(MatchingState::kNotMatched);
741
742 EXPECT_FALSE(combinationMatch(children, operation, matcherResults));
David Chendd896942017-09-26 11:44:40 -0700743}
744
Stefan Lafonb8c9aa82017-12-03 14:27:25 -0800745TEST(AtomMatcherTest, TestNotMatcher) {
David Chendd896942017-09-26 11:44:40 -0700746 // Set up the matcher
Yao Chencaf339d2017-10-06 16:01:10 -0700747 LogicalOperation operation = LogicalOperation::NOT;
David Chendd896942017-09-26 11:44:40 -0700748
Yao Chencaf339d2017-10-06 16:01:10 -0700749 vector<int> children;
750 children.push_back(0);
David Chendd896942017-09-26 11:44:40 -0700751
Yao Chencaf339d2017-10-06 16:01:10 -0700752 vector<MatchingState> matcherResults;
753 matcherResults.push_back(MatchingState::kMatched);
David Chendd896942017-09-26 11:44:40 -0700754
Yao Chencaf339d2017-10-06 16:01:10 -0700755 EXPECT_FALSE(combinationMatch(children, operation, matcherResults));
756
757 matcherResults.clear();
758 matcherResults.push_back(MatchingState::kNotMatched);
759 EXPECT_TRUE(combinationMatch(children, operation, matcherResults));
David Chendd896942017-09-26 11:44:40 -0700760}
761
Stefan Lafonb8c9aa82017-12-03 14:27:25 -0800762TEST(AtomMatcherTest, TestNandMatcher) {
David Chendd896942017-09-26 11:44:40 -0700763 // Set up the matcher
Yao Chencaf339d2017-10-06 16:01:10 -0700764 LogicalOperation operation = LogicalOperation::NAND;
David Chendd896942017-09-26 11:44:40 -0700765
Yao Chencaf339d2017-10-06 16:01:10 -0700766 vector<int> children;
767 children.push_back(0);
768 children.push_back(1);
David Chendd896942017-09-26 11:44:40 -0700769
Yao Chencaf339d2017-10-06 16:01:10 -0700770 vector<MatchingState> matcherResults;
771 matcherResults.push_back(MatchingState::kMatched);
772 matcherResults.push_back(MatchingState::kNotMatched);
David Chendd896942017-09-26 11:44:40 -0700773
Yao Chencaf339d2017-10-06 16:01:10 -0700774 EXPECT_TRUE(combinationMatch(children, operation, matcherResults));
775
776 matcherResults.clear();
777 matcherResults.push_back(MatchingState::kNotMatched);
778 matcherResults.push_back(MatchingState::kNotMatched);
779 EXPECT_TRUE(combinationMatch(children, operation, matcherResults));
780
781 matcherResults.clear();
782 matcherResults.push_back(MatchingState::kMatched);
783 matcherResults.push_back(MatchingState::kMatched);
784 EXPECT_FALSE(combinationMatch(children, operation, matcherResults));
David Chendd896942017-09-26 11:44:40 -0700785}
786
Stefan Lafonb8c9aa82017-12-03 14:27:25 -0800787TEST(AtomMatcherTest, TestNorMatcher) {
David Chendd896942017-09-26 11:44:40 -0700788 // Set up the matcher
Yao Chencaf339d2017-10-06 16:01:10 -0700789 LogicalOperation operation = LogicalOperation::NOR;
David Chendd896942017-09-26 11:44:40 -0700790
Yao Chencaf339d2017-10-06 16:01:10 -0700791 vector<int> children;
792 children.push_back(0);
793 children.push_back(1);
David Chendd896942017-09-26 11:44:40 -0700794
Yao Chencaf339d2017-10-06 16:01:10 -0700795 vector<MatchingState> matcherResults;
796 matcherResults.push_back(MatchingState::kMatched);
797 matcherResults.push_back(MatchingState::kNotMatched);
David Chendd896942017-09-26 11:44:40 -0700798
Yao Chencaf339d2017-10-06 16:01:10 -0700799 EXPECT_FALSE(combinationMatch(children, operation, matcherResults));
David Chendd896942017-09-26 11:44:40 -0700800
Yao Chencaf339d2017-10-06 16:01:10 -0700801 matcherResults.clear();
802 matcherResults.push_back(MatchingState::kNotMatched);
803 matcherResults.push_back(MatchingState::kNotMatched);
804 EXPECT_TRUE(combinationMatch(children, operation, matcherResults));
David Chendd896942017-09-26 11:44:40 -0700805
Yao Chencaf339d2017-10-06 16:01:10 -0700806 matcherResults.clear();
807 matcherResults.push_back(MatchingState::kMatched);
808 matcherResults.push_back(MatchingState::kMatched);
809 EXPECT_FALSE(combinationMatch(children, operation, matcherResults));
David Chendd896942017-09-26 11:44:40 -0700810}
David Chendd896942017-09-26 11:44:40 -0700811#else
Yao Chen44cf27c2017-09-14 22:32:50 -0700812GTEST_LOG_(INFO) << "This test does nothing.\n";
David Chendd896942017-09-26 11:44:40 -0700813#endif