blob: 2de6377149472f24500242449786f43ee0ebcf9e [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
Joe Onorato9fc9edf2017-10-15 20:08:52 -070015#include "frameworks/base/cmds/statsd/src/statsd_config.pb.h"
16#include "matchers/matcher_util.h"
Yao Chen8a8d16c2018-02-08 14:50:40 -080017#include "stats_log_util.h"
Joe Onorato9fc9edf2017-10-15 20:08:52 -070018#include "stats_util.h"
David Chendd896942017-09-26 11:44:40 -070019
Yao Chen44cf27c2017-09-14 22:32:50 -070020#include <gtest/gtest.h>
21#include <log/log_event_list.h>
22#include <log/log_read.h>
23#include <log/logprint.h>
David Chendd896942017-09-26 11:44:40 -070024
25#include <stdio.h>
26
27using namespace android::os::statsd;
28using std::unordered_map;
Yao Chencaf339d2017-10-06 16:01:10 -070029using std::vector;
David Chendd896942017-09-26 11:44:40 -070030
Yao Chen80235402017-11-13 20:42:25 -080031const int32_t TAG_ID = 123;
tsaichristine7a57b8e2019-06-24 18:25:38 -070032const int32_t TAG_ID_2 = 28; // hardcoded tag of atom with uid field
Joe Onoratoc4dfae52017-10-17 23:38:21 -070033const int FIELD_ID_1 = 1;
34const int FIELD_ID_2 = 2;
35const int FIELD_ID_3 = 2;
36
Yangster-mac20877162017-12-22 17:19:39 -080037const int ATTRIBUTION_UID_FIELD_ID = 1;
38const int ATTRIBUTION_TAG_FIELD_ID = 2;
39
Stefan Lafoncdb1a0e2017-09-27 20:24:15 -070040
David Chendd896942017-09-26 11:44:40 -070041#ifdef __ANDROID__
Jeffrey Huang1e4368a2020-02-18 12:28:52 -080042// TODO(b/149590301): Update these tests to use new socket schema.
43//TEST(AtomMatcherTest, TestSimpleMatcher) {
44// UidMap uidMap;
45//
46// // Set up the matcher
47// AtomMatcher matcher;
48// auto simpleMatcher = matcher.mutable_simple_atom_matcher();
49// simpleMatcher->set_atom_id(TAG_ID);
50//
51// LogEvent event(TAG_ID, 0);
52// EXPECT_TRUE(event.write(11));
53// event.init();
54//
55// // Test
56// EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event));
57//
58// // Wrong tag id.
59// simpleMatcher->set_atom_id(TAG_ID + 1);
60// EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event));
61//}
62//
63//TEST(AtomMatcherTest, TestAttributionMatcher) {
64// UidMap uidMap;
65// AttributionNodeInternal attribution_node1;
66// attribution_node1.set_uid(1111);
67// attribution_node1.set_tag("location1");
68//
69// AttributionNodeInternal attribution_node2;
70// attribution_node2.set_uid(2222);
71// attribution_node2.set_tag("location2");
72//
73// AttributionNodeInternal attribution_node3;
74// attribution_node3.set_uid(3333);
75// attribution_node3.set_tag("location3");
76// std::vector<AttributionNodeInternal> attribution_nodes = {attribution_node1, attribution_node2,
77// attribution_node3};
78//
79// // Set up the event
80// LogEvent event(TAG_ID, 0);
81// event.write(attribution_nodes);
82// event.write("some value");
83// // Convert to a LogEvent
84// event.init();
85//
86// // Set up the matcher
87// AtomMatcher matcher;
88// auto simpleMatcher = matcher.mutable_simple_atom_matcher();
89// simpleMatcher->set_atom_id(TAG_ID);
90//
91// // Match first node.
92// auto attributionMatcher = simpleMatcher->add_field_value_matcher();
93// attributionMatcher->set_field(FIELD_ID_1);
94// attributionMatcher->set_position(Position::FIRST);
95// attributionMatcher->mutable_matches_tuple()->add_field_value_matcher()->set_field(
96// ATTRIBUTION_TAG_FIELD_ID);
97// attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string("tag");
98//
99// auto fieldMatcher = simpleMatcher->add_field_value_matcher();
100// fieldMatcher->set_field(FIELD_ID_2);
101// fieldMatcher->set_eq_string("some value");
102//
103// // Tag not matched.
104// EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event));
105// attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)
106// ->set_eq_string("location3");
107// EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event));
108// attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)
109// ->set_eq_string("location1");
110// EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event));
111//
112// // Match last node.
113// attributionMatcher->set_position(Position::LAST);
114// EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event));
115// attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)
116// ->set_eq_string("location3");
117// EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event));
118//
119// // Match any node.
120// attributionMatcher->set_position(Position::ANY);
121// EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event));
122// attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)
123// ->set_eq_string("location1");
124// EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event));
125// attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)
126// ->set_eq_string("location2");
127// EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event));
128// attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)
129// ->set_eq_string("location3");
130// EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event));
131// attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)
132// ->set_eq_string("location4");
133// EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event));
134//
135// // Attribution match but primitive field not match.
136// attributionMatcher->set_position(Position::ANY);
137// attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)
138// ->set_eq_string("location2");
139// fieldMatcher->set_eq_string("wrong value");
140// EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event));
141//
142// fieldMatcher->set_eq_string("some value");
143//
144// // Uid match.
145// attributionMatcher->set_position(Position::ANY);
146// attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_field(
147// ATTRIBUTION_UID_FIELD_ID);
148// attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string("pkg0");
149// EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event));
150//
151// uidMap.updateMap(
152// 1, {1111, 1111, 2222, 3333, 3333} /* uid list */, {1, 1, 2, 1, 2} /* version list */,
153// {android::String16("v1"), android::String16("v1"), android::String16("v2"),
154// android::String16("v1"), android::String16("v2")},
155// {android::String16("pkg0"), android::String16("pkg1"), android::String16("pkg1"),
156// android::String16("Pkg2"), android::String16("PkG3")} /* package name list */,
157// {android::String16(""), android::String16(""), android::String16(""),
158// android::String16(""), android::String16("")});
159//
160// EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event));
161// attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)
162// ->set_eq_string("pkg3");
163// EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event));
164// attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)
165// ->set_eq_string("pkg2");
166// EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event));
167// attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)
168// ->set_eq_string("pkg1");
169// EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event));
170// attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)
171// ->set_eq_string("pkg0");
172// EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event));
173//
174// attributionMatcher->set_position(Position::FIRST);
175// attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)
176// ->set_eq_string("pkg0");
177// EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event));
178// attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)
179// ->set_eq_string("pkg3");
180// EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event));
181// attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)
182// ->set_eq_string("pkg2");
183// EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event));
184// attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)
185// ->set_eq_string("pkg1");
186// EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event));
187//
188// attributionMatcher->set_position(Position::LAST);
189// attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)
190// ->set_eq_string("pkg0");
191// EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event));
192// attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)
193// ->set_eq_string("pkg3");
194// EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event));
195// attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)
196// ->set_eq_string("pkg2");
197// EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event));
198// attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)
199// ->set_eq_string("pkg1");
200// EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event));
201//
202// // Uid + tag.
203// attributionMatcher->set_position(Position::ANY);
204// attributionMatcher->mutable_matches_tuple()->add_field_value_matcher()->set_field(
205// ATTRIBUTION_TAG_FIELD_ID);
206// attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)
207// ->set_eq_string("pkg0");
208// attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(1)
209// ->set_eq_string("location1");
210// EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event));
211// attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)
212// ->set_eq_string("pkg1");
213// attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(1)
214// ->set_eq_string("location1");
215// EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event));
216// attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)
217// ->set_eq_string("pkg1");
218// attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(1)
219// ->set_eq_string("location2");
220// EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event));
221// attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)
222// ->set_eq_string("pkg2");
223// attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(1)
224// ->set_eq_string("location3");
225// EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event));
226// attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)
227// ->set_eq_string("pkg3");
228// attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(1)
229// ->set_eq_string("location3");
230// EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event));
231// attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)
232// ->set_eq_string("pkg3");
233// attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(1)
234// ->set_eq_string("location1");
235// EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event));
236//
237// attributionMatcher->set_position(Position::FIRST);
238// attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)
239// ->set_eq_string("pkg0");
240// attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(1)
241// ->set_eq_string("location1");
242// EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event));
243// attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)
244// ->set_eq_string("pkg1");
245// attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(1)
246// ->set_eq_string("location1");
247// EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event));
248// attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)
249// ->set_eq_string("pkg1");
250// attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(1)
251// ->set_eq_string("location2");
252// EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event));
253// attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)
254// ->set_eq_string("pkg2");
255// attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(1)
256// ->set_eq_string("location3");
257// EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event));
258// attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)
259// ->set_eq_string("pkg3");
260// attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(1)
261// ->set_eq_string("location3");
262// EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event));
263// attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)
264// ->set_eq_string("pkg3");
265// attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(1)
266// ->set_eq_string("location1");
267// EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event));
268//
269// attributionMatcher->set_position(Position::LAST);
270// attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)
271// ->set_eq_string("pkg0");
272// attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(1)
273// ->set_eq_string("location1");
274// EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event));
275// attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)
276// ->set_eq_string("pkg1");
277// attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(1)
278// ->set_eq_string("location1");
279// EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event));
280// attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)
281// ->set_eq_string("pkg1");
282// attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(1)
283// ->set_eq_string("location2");
284// EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event));
285// attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)
286// ->set_eq_string("pkg2");
287// attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(1)
288// ->set_eq_string("location3");
289// EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event));
290// attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)
291// ->set_eq_string("pkg3");
292// attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(1)
293// ->set_eq_string("location3");
294// EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event));
295// attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)
296// ->set_eq_string("pkg3");
297// attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(1)
298// ->set_eq_string("location1");
299// EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event));
300//}
301//
302//TEST(AtomMatcherTest, TestUidFieldMatcher) {
303// UidMap uidMap;
304// uidMap.updateMap(
305// 1, {1111, 1111, 2222, 3333, 3333} /* uid list */, {1, 1, 2, 1, 2} /* version list */,
306// {android::String16("v1"), android::String16("v1"), android::String16("v2"),
307// android::String16("v1"), android::String16("v2")},
308// {android::String16("pkg0"), android::String16("pkg1"), android::String16("pkg1"),
309// android::String16("Pkg2"), android::String16("PkG3")} /* package name list */,
310// {android::String16(""), android::String16(""), android::String16(""),
311// android::String16(""), android::String16("")});
312//
313// // Set up matcher
314// AtomMatcher matcher;
315// auto simpleMatcher = matcher.mutable_simple_atom_matcher();
316// simpleMatcher->set_atom_id(TAG_ID);
317// simpleMatcher->add_field_value_matcher()->set_field(1);
318// simpleMatcher->mutable_field_value_matcher(0)->set_eq_string("pkg0");
319//
320// // Set up the event
321// LogEvent event(TAG_ID, 0);
322// event.write(1111);
323// event.init();
324//
325// LogEvent event2(TAG_ID_2, 0);
326// event2.write(1111);
327// event2.write("some value");
328// event2.init();
329//
330// // Tag not in kAtomsWithUidField
331// EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event));
332//
333// // Tag found in kAtomsWithUidField and has matching uid
334// simpleMatcher->set_atom_id(TAG_ID_2);
335// EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event2));
336//
337// // Tag found in kAtomsWithUidField but has non-matching uid
338// simpleMatcher->mutable_field_value_matcher(0)->set_eq_string("Pkg2");
339// EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event2));
340//}
341//
342//TEST(AtomMatcherTest, TestNeqAnyStringMatcher) {
343// UidMap uidMap;
344// uidMap.updateMap(
345// 1, {1111, 1111, 2222, 3333, 3333} /* uid list */, {1, 1, 2, 1, 2} /* version list */,
346// {android::String16("v1"), android::String16("v1"), android::String16("v2"),
347// android::String16("v1"), android::String16("v2")},
348// {android::String16("pkg0"), android::String16("pkg1"), android::String16("pkg1"),
349// android::String16("Pkg2"), android::String16("PkG3")} /* package name list */,
350// {android::String16(""), android::String16(""), android::String16(""),
351// android::String16(""), android::String16("")});
352//
353// AttributionNodeInternal attribution_node1;
354// attribution_node1.set_uid(1111);
355// attribution_node1.set_tag("location1");
356//
357// AttributionNodeInternal attribution_node2;
358// attribution_node2.set_uid(2222);
359// attribution_node2.set_tag("location2");
360//
361// AttributionNodeInternal attribution_node3;
362// attribution_node3.set_uid(3333);
363// attribution_node3.set_tag("location3");
364//
365// AttributionNodeInternal attribution_node4;
366// attribution_node4.set_uid(1066);
367// attribution_node4.set_tag("location3");
368// std::vector<AttributionNodeInternal> attribution_nodes = {attribution_node1, attribution_node2,
369// attribution_node3, attribution_node4};
370//
371// // Set up the event
372// LogEvent event(TAG_ID, 0);
373// event.write(attribution_nodes);
374// event.write("some value");
375// // Convert to a LogEvent
376// event.init();
377//
378// // Set up the matcher
379// AtomMatcher matcher;
380// auto simpleMatcher = matcher.mutable_simple_atom_matcher();
381// simpleMatcher->set_atom_id(TAG_ID);
382//
383// // Match first node.
384// auto attributionMatcher = simpleMatcher->add_field_value_matcher();
385// attributionMatcher->set_field(FIELD_ID_1);
386// attributionMatcher->set_position(Position::FIRST);
387// attributionMatcher->mutable_matches_tuple()->add_field_value_matcher()->set_field(
388// ATTRIBUTION_UID_FIELD_ID);
389// auto neqStringList = attributionMatcher->mutable_matches_tuple()
390// ->mutable_field_value_matcher(0)
391// ->mutable_neq_any_string();
392// neqStringList->add_str_value("pkg2");
393// neqStringList->add_str_value("pkg3");
394//
395// auto fieldMatcher = simpleMatcher->add_field_value_matcher();
396// fieldMatcher->set_field(FIELD_ID_2);
397// fieldMatcher->set_eq_string("some value");
398//
399// EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event));
400//
401// neqStringList->Clear();
402// neqStringList->add_str_value("pkg1");
403// neqStringList->add_str_value("pkg3");
404// EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event));
405//
406// attributionMatcher->set_position(Position::ANY);
407// neqStringList->Clear();
408// neqStringList->add_str_value("maps.com");
409// EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event));
410//
411// neqStringList->Clear();
412// neqStringList->add_str_value("PkG3");
413// EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event));
414//
415// attributionMatcher->set_position(Position::LAST);
416// neqStringList->Clear();
417// neqStringList->add_str_value("AID_STATSD");
418// EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event));
419//}
420//
421//TEST(AtomMatcherTest, TestEqAnyStringMatcher) {
422// UidMap uidMap;
423// uidMap.updateMap(
424// 1, {1111, 1111, 2222, 3333, 3333} /* uid list */, {1, 1, 2, 1, 2} /* version list */,
425// {android::String16("v1"), android::String16("v1"), android::String16("v2"),
426// android::String16("v1"), android::String16("v2")},
427// {android::String16("pkg0"), android::String16("pkg1"), android::String16("pkg1"),
428// android::String16("Pkg2"), android::String16("PkG3")} /* package name list */,
429// {android::String16(""), android::String16(""), android::String16(""),
430// android::String16(""), android::String16("")});
431//
432// AttributionNodeInternal attribution_node1;
433// attribution_node1.set_uid(1067);
434// attribution_node1.set_tag("location1");
435//
436// AttributionNodeInternal attribution_node2;
437// attribution_node2.set_uid(2222);
438// attribution_node2.set_tag("location2");
439//
440// AttributionNodeInternal attribution_node3;
441// attribution_node3.set_uid(3333);
442// attribution_node3.set_tag("location3");
443//
444// AttributionNodeInternal attribution_node4;
445// attribution_node4.set_uid(1066);
446// attribution_node4.set_tag("location3");
447// std::vector<AttributionNodeInternal> attribution_nodes = {attribution_node1, attribution_node2,
448// attribution_node3, attribution_node4};
449//
450// // Set up the event
451// LogEvent event(TAG_ID, 0);
452// event.write(attribution_nodes);
453// event.write("some value");
454// // Convert to a LogEvent
455// event.init();
456//
457// // Set up the matcher
458// AtomMatcher matcher;
459// auto simpleMatcher = matcher.mutable_simple_atom_matcher();
460// simpleMatcher->set_atom_id(TAG_ID);
461//
462// // Match first node.
463// auto attributionMatcher = simpleMatcher->add_field_value_matcher();
464// attributionMatcher->set_field(FIELD_ID_1);
465// attributionMatcher->set_position(Position::FIRST);
466// attributionMatcher->mutable_matches_tuple()->add_field_value_matcher()->set_field(
467// ATTRIBUTION_UID_FIELD_ID);
468// auto eqStringList = attributionMatcher->mutable_matches_tuple()
469// ->mutable_field_value_matcher(0)
470// ->mutable_eq_any_string();
471// eqStringList->add_str_value("AID_ROOT");
472// eqStringList->add_str_value("AID_INCIDENTD");
473//
474// auto fieldMatcher = simpleMatcher->add_field_value_matcher();
475// fieldMatcher->set_field(FIELD_ID_2);
476// fieldMatcher->set_eq_string("some value");
477//
478// EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event));
479//
480// attributionMatcher->set_position(Position::ANY);
481// eqStringList->Clear();
482// eqStringList->add_str_value("AID_STATSD");
483// EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event));
484//
485// eqStringList->Clear();
486// eqStringList->add_str_value("pkg1");
487// EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event));
488//
489// auto normalStringField = fieldMatcher->mutable_eq_any_string();
490// normalStringField->add_str_value("some value123");
491// normalStringField->add_str_value("some value");
492// EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event));
493//
494// normalStringField->Clear();
495// normalStringField->add_str_value("AID_STATSD");
496// EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event));
497//
498// eqStringList->Clear();
499// eqStringList->add_str_value("maps.com");
500// EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event));
501//}
502//
503//TEST(AtomMatcherTest, TestBoolMatcher) {
504// UidMap uidMap;
505// // Set up the matcher
506// AtomMatcher matcher;
507// auto simpleMatcher = matcher.mutable_simple_atom_matcher();
508// simpleMatcher->set_atom_id(TAG_ID);
509// auto keyValue1 = simpleMatcher->add_field_value_matcher();
510// keyValue1->set_field(FIELD_ID_1);
511// auto keyValue2 = simpleMatcher->add_field_value_matcher();
512// keyValue2->set_field(FIELD_ID_2);
513//
514// // Set up the event
515// LogEvent event(TAG_ID, 0);
516// EXPECT_TRUE(event.write(true));
517// EXPECT_TRUE(event.write(false));
518// // Convert to a LogEvent
519// event.init();
520//
521// // Test
522// keyValue1->set_eq_bool(true);
523// keyValue2->set_eq_bool(false);
524// EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event));
525//
526// keyValue1->set_eq_bool(false);
527// keyValue2->set_eq_bool(false);
528// EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event));
529//
530// keyValue1->set_eq_bool(false);
531// keyValue2->set_eq_bool(true);
532// EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event));
533//
534// keyValue1->set_eq_bool(true);
535// keyValue2->set_eq_bool(true);
536// EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event));
537//}
538//
539//TEST(AtomMatcherTest, TestStringMatcher) {
540// UidMap uidMap;
541// // Set up the matcher
542// AtomMatcher matcher;
543// auto simpleMatcher = matcher.mutable_simple_atom_matcher();
544// simpleMatcher->set_atom_id(TAG_ID);
545// auto keyValue = simpleMatcher->add_field_value_matcher();
546// keyValue->set_field(FIELD_ID_1);
547// keyValue->set_eq_string("some value");
548//
549// // Set up the event
550// LogEvent event(TAG_ID, 0);
551// event.write("some value");
552// // Convert to a LogEvent
553// event.init();
554//
555// // Test
556// EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event));
557//}
558//
559//TEST(AtomMatcherTest, TestMultiFieldsMatcher) {
560// UidMap uidMap;
561// // Set up the matcher
562// AtomMatcher matcher;
563// auto simpleMatcher = matcher.mutable_simple_atom_matcher();
564// simpleMatcher->set_atom_id(TAG_ID);
565// auto keyValue1 = simpleMatcher->add_field_value_matcher();
566// keyValue1->set_field(FIELD_ID_1);
567// auto keyValue2 = simpleMatcher->add_field_value_matcher();
568// keyValue2->set_field(FIELD_ID_2);
569//
570// // Set up the event
571// LogEvent event(TAG_ID, 0);
572// event.write(2);
573// event.write(3);
574//
575// // Convert to a LogEvent
576// event.init();
577//
578// // Test
579// keyValue1->set_eq_int(2);
580// keyValue2->set_eq_int(3);
581// EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event));
582//
583// keyValue1->set_eq_int(2);
584// keyValue2->set_eq_int(4);
585// EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event));
586//
587// keyValue1->set_eq_int(4);
588// keyValue2->set_eq_int(3);
589// EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event));
590//}
591//
592//TEST(AtomMatcherTest, TestIntComparisonMatcher) {
593// UidMap uidMap;
594// // Set up the matcher
595// AtomMatcher matcher;
596// auto simpleMatcher = matcher.mutable_simple_atom_matcher();
597//
598// simpleMatcher->set_atom_id(TAG_ID);
599// auto keyValue = simpleMatcher->add_field_value_matcher();
600// keyValue->set_field(FIELD_ID_1);
601//
602// // Set up the event
603// LogEvent event(TAG_ID, 0);
604// event.write(11);
605// event.init();
606//
607// // Test
608//
609// // eq_int
610// keyValue->set_eq_int(10);
611// EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event));
612// keyValue->set_eq_int(11);
613// EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event));
614// keyValue->set_eq_int(12);
615// EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event));
616//
617// // lt_int
618// keyValue->set_lt_int(10);
619// EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event));
620// keyValue->set_lt_int(11);
621// EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event));
622// keyValue->set_lt_int(12);
623// EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event));
624//
625// // lte_int
626// keyValue->set_lte_int(10);
627// EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event));
628// keyValue->set_lte_int(11);
629// EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event));
630// keyValue->set_lte_int(12);
631// EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event));
632//
633// // gt_int
634// keyValue->set_gt_int(10);
635// EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event));
636// keyValue->set_gt_int(11);
637// EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event));
638// keyValue->set_gt_int(12);
639// EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event));
640//
641// // gte_int
642// keyValue->set_gte_int(10);
643// EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event));
644// keyValue->set_gte_int(11);
645// EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event));
646// keyValue->set_gte_int(12);
647// EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event));
648//}
649//
650//TEST(AtomMatcherTest, TestFloatComparisonMatcher) {
651// UidMap uidMap;
652// // Set up the matcher
653// AtomMatcher matcher;
654// auto simpleMatcher = matcher.mutable_simple_atom_matcher();
655// simpleMatcher->set_atom_id(TAG_ID);
656//
657// auto keyValue = simpleMatcher->add_field_value_matcher();
658// keyValue->set_field(FIELD_ID_1);
659//
660// LogEvent event1(TAG_ID, 0);
661// keyValue->set_lt_float(10.0);
662// event1.write(10.1f);
663// event1.init();
664// EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event1));
665//
666// LogEvent event2(TAG_ID, 0);
667// event2.write(9.9f);
668// event2.init();
669// EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event2));
670//
671// LogEvent event3(TAG_ID, 0);
672// event3.write(10.1f);
673// event3.init();
674// keyValue->set_gt_float(10.0);
675// EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event3));
676//
677// LogEvent event4(TAG_ID, 0);
678// event4.write(9.9f);
679// event4.init();
680// EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event4));
681//}
David Chendd896942017-09-26 11:44:40 -0700682
683// Helper for the composite matchers.
Stefan Lafonb8c9aa82017-12-03 14:27:25 -0800684void addSimpleMatcher(SimpleAtomMatcher* simpleMatcher, int tag, int key, int val) {
Yangster-mac20877162017-12-22 17:19:39 -0800685 simpleMatcher->set_atom_id(tag);
686 auto keyValue = simpleMatcher->add_field_value_matcher();
687 keyValue->set_field(key);
David Chendd896942017-09-26 11:44:40 -0700688 keyValue->set_eq_int(val);
689}
690
Stefan Lafonb8c9aa82017-12-03 14:27:25 -0800691TEST(AtomMatcherTest, TestAndMatcher) {
David Chendd896942017-09-26 11:44:40 -0700692 // Set up the matcher
Yao Chencaf339d2017-10-06 16:01:10 -0700693 LogicalOperation operation = LogicalOperation::AND;
David Chendd896942017-09-26 11:44:40 -0700694
Yao Chencaf339d2017-10-06 16:01:10 -0700695 vector<int> children;
696 children.push_back(0);
697 children.push_back(1);
698 children.push_back(2);
David Chendd896942017-09-26 11:44:40 -0700699
Yao Chencaf339d2017-10-06 16:01:10 -0700700 vector<MatchingState> matcherResults;
701 matcherResults.push_back(MatchingState::kMatched);
702 matcherResults.push_back(MatchingState::kNotMatched);
703 matcherResults.push_back(MatchingState::kMatched);
David Chendd896942017-09-26 11:44:40 -0700704
Yao Chencaf339d2017-10-06 16:01:10 -0700705 EXPECT_FALSE(combinationMatch(children, operation, matcherResults));
706
707 matcherResults.clear();
708 matcherResults.push_back(MatchingState::kMatched);
709 matcherResults.push_back(MatchingState::kMatched);
710 matcherResults.push_back(MatchingState::kMatched);
711
712 EXPECT_TRUE(combinationMatch(children, operation, matcherResults));
David Chendd896942017-09-26 11:44:40 -0700713}
714
Stefan Lafonb8c9aa82017-12-03 14:27:25 -0800715TEST(AtomMatcherTest, TestOrMatcher) {
David Chendd896942017-09-26 11:44:40 -0700716 // Set up the matcher
Yao Chencaf339d2017-10-06 16:01:10 -0700717 LogicalOperation operation = LogicalOperation::OR;
David Chendd896942017-09-26 11:44:40 -0700718
Yao Chencaf339d2017-10-06 16:01:10 -0700719 vector<int> children;
720 children.push_back(0);
721 children.push_back(1);
722 children.push_back(2);
David Chendd896942017-09-26 11:44:40 -0700723
Yao Chencaf339d2017-10-06 16:01:10 -0700724 vector<MatchingState> matcherResults;
725 matcherResults.push_back(MatchingState::kMatched);
726 matcherResults.push_back(MatchingState::kNotMatched);
727 matcherResults.push_back(MatchingState::kMatched);
David Chendd896942017-09-26 11:44:40 -0700728
Yao Chencaf339d2017-10-06 16:01:10 -0700729 EXPECT_TRUE(combinationMatch(children, operation, matcherResults));
730
731 matcherResults.clear();
732 matcherResults.push_back(MatchingState::kNotMatched);
733 matcherResults.push_back(MatchingState::kNotMatched);
734 matcherResults.push_back(MatchingState::kNotMatched);
735
736 EXPECT_FALSE(combinationMatch(children, operation, matcherResults));
David Chendd896942017-09-26 11:44:40 -0700737}
738
Stefan Lafonb8c9aa82017-12-03 14:27:25 -0800739TEST(AtomMatcherTest, TestNotMatcher) {
David Chendd896942017-09-26 11:44:40 -0700740 // Set up the matcher
Yao Chencaf339d2017-10-06 16:01:10 -0700741 LogicalOperation operation = LogicalOperation::NOT;
David Chendd896942017-09-26 11:44:40 -0700742
Yao Chencaf339d2017-10-06 16:01:10 -0700743 vector<int> children;
744 children.push_back(0);
David Chendd896942017-09-26 11:44:40 -0700745
Yao Chencaf339d2017-10-06 16:01:10 -0700746 vector<MatchingState> matcherResults;
747 matcherResults.push_back(MatchingState::kMatched);
David Chendd896942017-09-26 11:44:40 -0700748
Yao Chencaf339d2017-10-06 16:01:10 -0700749 EXPECT_FALSE(combinationMatch(children, operation, matcherResults));
750
751 matcherResults.clear();
752 matcherResults.push_back(MatchingState::kNotMatched);
753 EXPECT_TRUE(combinationMatch(children, operation, matcherResults));
David Chendd896942017-09-26 11:44:40 -0700754}
755
Stefan Lafonb8c9aa82017-12-03 14:27:25 -0800756TEST(AtomMatcherTest, TestNandMatcher) {
David Chendd896942017-09-26 11:44:40 -0700757 // Set up the matcher
Yao Chencaf339d2017-10-06 16:01:10 -0700758 LogicalOperation operation = LogicalOperation::NAND;
David Chendd896942017-09-26 11:44:40 -0700759
Yao Chencaf339d2017-10-06 16:01:10 -0700760 vector<int> children;
761 children.push_back(0);
762 children.push_back(1);
David Chendd896942017-09-26 11:44:40 -0700763
Yao Chencaf339d2017-10-06 16:01:10 -0700764 vector<MatchingState> matcherResults;
765 matcherResults.push_back(MatchingState::kMatched);
766 matcherResults.push_back(MatchingState::kNotMatched);
David Chendd896942017-09-26 11:44:40 -0700767
Yao Chencaf339d2017-10-06 16:01:10 -0700768 EXPECT_TRUE(combinationMatch(children, operation, matcherResults));
769
770 matcherResults.clear();
771 matcherResults.push_back(MatchingState::kNotMatched);
772 matcherResults.push_back(MatchingState::kNotMatched);
773 EXPECT_TRUE(combinationMatch(children, operation, matcherResults));
774
775 matcherResults.clear();
776 matcherResults.push_back(MatchingState::kMatched);
777 matcherResults.push_back(MatchingState::kMatched);
778 EXPECT_FALSE(combinationMatch(children, operation, matcherResults));
David Chendd896942017-09-26 11:44:40 -0700779}
780
Stefan Lafonb8c9aa82017-12-03 14:27:25 -0800781TEST(AtomMatcherTest, TestNorMatcher) {
David Chendd896942017-09-26 11:44:40 -0700782 // Set up the matcher
Yao Chencaf339d2017-10-06 16:01:10 -0700783 LogicalOperation operation = LogicalOperation::NOR;
David Chendd896942017-09-26 11:44:40 -0700784
Yao Chencaf339d2017-10-06 16:01:10 -0700785 vector<int> children;
786 children.push_back(0);
787 children.push_back(1);
David Chendd896942017-09-26 11:44:40 -0700788
Yao Chencaf339d2017-10-06 16:01:10 -0700789 vector<MatchingState> matcherResults;
790 matcherResults.push_back(MatchingState::kMatched);
791 matcherResults.push_back(MatchingState::kNotMatched);
David Chendd896942017-09-26 11:44:40 -0700792
Yao Chencaf339d2017-10-06 16:01:10 -0700793 EXPECT_FALSE(combinationMatch(children, operation, matcherResults));
David Chendd896942017-09-26 11:44:40 -0700794
Yao Chencaf339d2017-10-06 16:01:10 -0700795 matcherResults.clear();
796 matcherResults.push_back(MatchingState::kNotMatched);
797 matcherResults.push_back(MatchingState::kNotMatched);
798 EXPECT_TRUE(combinationMatch(children, operation, matcherResults));
David Chendd896942017-09-26 11:44:40 -0700799
Yao Chencaf339d2017-10-06 16:01:10 -0700800 matcherResults.clear();
801 matcherResults.push_back(MatchingState::kMatched);
802 matcherResults.push_back(MatchingState::kMatched);
803 EXPECT_FALSE(combinationMatch(children, operation, matcherResults));
David Chendd896942017-09-26 11:44:40 -0700804}
David Chendd896942017-09-26 11:44:40 -0700805#else
Yao Chen44cf27c2017-09-14 22:32:50 -0700806GTEST_LOG_(INFO) << "This test does nothing.\n";
David Chendd896942017-09-26 11:44:40 -0700807#endif