diff options
author | Xin Li <delphij@google.com> | 2020-09-10 17:22:01 +0000 |
---|---|---|
committer | Gerrit Code Review <noreply-gerritcodereview@google.com> | 2020-09-10 17:22:01 +0000 |
commit | 8ac6741e47c76bde065f868ea64d2f04541487b9 (patch) | |
tree | 1a679458fdbd8d370692d56791e2bf83acee35b5 /cmds/statsd/tests/LogEntryMatcher_test.cpp | |
parent | 3de940cc40b1e3fdf8224e18a8308a16768cbfa8 (diff) | |
parent | c64112eb974e9aa7638aead998f07a868acfb5a7 (diff) |
Merge "Merge Android R"
Diffstat (limited to 'cmds/statsd/tests/LogEntryMatcher_test.cpp')
-rw-r--r-- | cmds/statsd/tests/LogEntryMatcher_test.cpp | 497 |
1 files changed, 272 insertions, 225 deletions
diff --git a/cmds/statsd/tests/LogEntryMatcher_test.cpp b/cmds/statsd/tests/LogEntryMatcher_test.cpp index 2cbcf28ef7d6..6264c075426a 100644 --- a/cmds/statsd/tests/LogEntryMatcher_test.cpp +++ b/cmds/statsd/tests/LogEntryMatcher_test.cpp @@ -12,20 +12,23 @@ // See the License for the specific language governing permissions and // limitations under the License. +#include <gtest/gtest.h> +#include <stdio.h> + +#include "annotations.h" #include "frameworks/base/cmds/statsd/src/statsd_config.pb.h" #include "matchers/matcher_util.h" +#include "stats_event.h" #include "stats_log_util.h" #include "stats_util.h" - -#include <gtest/gtest.h> - -#include <stdio.h> +#include "statsd_test_util.h" using namespace android::os::statsd; using std::unordered_map; using std::vector; const int32_t TAG_ID = 123; +const int32_t TAG_ID_2 = 28; // hardcoded tag of atom with uid field const int FIELD_ID_1 = 1; const int FIELD_ID_2 = 2; const int FIELD_ID_3 = 2; @@ -35,6 +38,77 @@ const int ATTRIBUTION_TAG_FIELD_ID = 2; #ifdef __ANDROID__ + +namespace { + +void makeIntLogEvent(LogEvent* logEvent, const int32_t atomId, const int64_t timestamp, + const int32_t value) { + AStatsEvent* statsEvent = AStatsEvent_obtain(); + AStatsEvent_setAtomId(statsEvent, atomId); + AStatsEvent_overwriteTimestamp(statsEvent, timestamp); + AStatsEvent_writeInt32(statsEvent, value); + + parseStatsEventToLogEvent(statsEvent, logEvent); +} + +void makeFloatLogEvent(LogEvent* logEvent, const int32_t atomId, const int64_t timestamp, + const float floatValue) { + AStatsEvent* statsEvent = AStatsEvent_obtain(); + AStatsEvent_setAtomId(statsEvent, atomId); + AStatsEvent_overwriteTimestamp(statsEvent, timestamp); + AStatsEvent_writeFloat(statsEvent, floatValue); + + parseStatsEventToLogEvent(statsEvent, logEvent); +} + +void makeStringLogEvent(LogEvent* logEvent, const int32_t atomId, const int64_t timestamp, + const string& name) { + AStatsEvent* statsEvent = AStatsEvent_obtain(); + AStatsEvent_setAtomId(statsEvent, atomId); + AStatsEvent_overwriteTimestamp(statsEvent, timestamp); + AStatsEvent_writeString(statsEvent, name.c_str()); + + parseStatsEventToLogEvent(statsEvent, logEvent); +} + +void makeIntWithBoolAnnotationLogEvent(LogEvent* logEvent, const int32_t atomId, + const int32_t field, const uint8_t annotationId, + const bool annotationValue) { + AStatsEvent* statsEvent = AStatsEvent_obtain(); + AStatsEvent_setAtomId(statsEvent, atomId); + AStatsEvent_writeInt32(statsEvent, field); + AStatsEvent_addBoolAnnotation(statsEvent, annotationId, annotationValue); + + parseStatsEventToLogEvent(statsEvent, logEvent); +} + +void makeAttributionLogEvent(LogEvent* logEvent, const int32_t atomId, const int64_t timestamp, + const vector<int>& attributionUids, + const vector<string>& attributionTags, const string& name) { + AStatsEvent* statsEvent = AStatsEvent_obtain(); + AStatsEvent_setAtomId(statsEvent, atomId); + AStatsEvent_overwriteTimestamp(statsEvent, timestamp); + + writeAttribution(statsEvent, attributionUids, attributionTags); + AStatsEvent_writeString(statsEvent, name.c_str()); + + parseStatsEventToLogEvent(statsEvent, logEvent); +} + +void makeBoolLogEvent(LogEvent* logEvent, const int32_t atomId, const int64_t timestamp, + const bool bool1, const bool bool2) { + AStatsEvent* statsEvent = AStatsEvent_obtain(); + AStatsEvent_setAtomId(statsEvent, atomId); + AStatsEvent_overwriteTimestamp(statsEvent, timestamp); + + AStatsEvent_writeBool(statsEvent, bool1); + AStatsEvent_writeBool(statsEvent, bool2); + + parseStatsEventToLogEvent(statsEvent, logEvent); +} + +} // anonymous namespace + TEST(AtomMatcherTest, TestSimpleMatcher) { UidMap uidMap; @@ -43,9 +117,8 @@ TEST(AtomMatcherTest, TestSimpleMatcher) { auto simpleMatcher = matcher.mutable_simple_atom_matcher(); simpleMatcher->set_atom_id(TAG_ID); - LogEvent event(TAG_ID, 0); - EXPECT_TRUE(event.write(11)); - event.init(); + LogEvent event(/*uid=*/0, /*pid=*/0); + makeIntLogEvent(&event, TAG_ID, 0, 11); // Test EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event)); @@ -57,26 +130,12 @@ TEST(AtomMatcherTest, TestSimpleMatcher) { TEST(AtomMatcherTest, TestAttributionMatcher) { UidMap uidMap; - AttributionNodeInternal attribution_node1; - attribution_node1.set_uid(1111); - attribution_node1.set_tag("location1"); - - AttributionNodeInternal attribution_node2; - attribution_node2.set_uid(2222); - attribution_node2.set_tag("location2"); + std::vector<int> attributionUids = {1111, 2222, 3333}; + std::vector<string> attributionTags = {"location1", "location2", "location3"}; - AttributionNodeInternal attribution_node3; - attribution_node3.set_uid(3333); - attribution_node3.set_tag("location3"); - std::vector<AttributionNodeInternal> attribution_nodes = {attribution_node1, attribution_node2, - attribution_node3}; - - // Set up the event - LogEvent event(TAG_ID, 0); - event.write(attribution_nodes); - event.write("some value"); - // Convert to a LogEvent - event.init(); + // Set up the log event. + LogEvent event(/*uid=*/0, /*pid=*/0); + makeAttributionLogEvent(&event, TAG_ID, 0, attributionUids, attributionTags, "some value"); // Set up the matcher AtomMatcher matcher; @@ -88,8 +147,9 @@ TEST(AtomMatcherTest, TestAttributionMatcher) { attributionMatcher->set_field(FIELD_ID_1); attributionMatcher->set_position(Position::FIRST); attributionMatcher->mutable_matches_tuple()->add_field_value_matcher()->set_field( - ATTRIBUTION_TAG_FIELD_ID); - attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string("tag"); + ATTRIBUTION_TAG_FIELD_ID); + attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string( + "tag"); auto fieldMatcher = simpleMatcher->add_field_value_matcher(); fieldMatcher->set_field(FIELD_ID_2); @@ -97,40 +157,40 @@ TEST(AtomMatcherTest, TestAttributionMatcher) { // Tag not matched. EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event)); - attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0) - ->set_eq_string("location3"); + attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string( + "location3"); EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event)); - attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0) - ->set_eq_string("location1"); + attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string( + "location1"); EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event)); // Match last node. attributionMatcher->set_position(Position::LAST); EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event)); - attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0) - ->set_eq_string("location3"); + attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string( + "location3"); EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event)); // Match any node. attributionMatcher->set_position(Position::ANY); EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event)); - attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0) - ->set_eq_string("location1"); + attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string( + "location1"); EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event)); - attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0) - ->set_eq_string("location2"); + attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string( + "location2"); EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event)); - attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0) - ->set_eq_string("location3"); + attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string( + "location3"); EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event)); - attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0) - ->set_eq_string("location4"); + attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string( + "location4"); EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event)); // Attribution match but primitive field not match. attributionMatcher->set_position(Position::ANY); - attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0) - ->set_eq_string("location2"); + attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string( + "location2"); fieldMatcher->set_eq_string("wrong value"); EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event)); @@ -139,8 +199,9 @@ TEST(AtomMatcherTest, TestAttributionMatcher) { // Uid match. attributionMatcher->set_position(Position::ANY); attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_field( - ATTRIBUTION_UID_FIELD_ID); - attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string("pkg0"); + ATTRIBUTION_UID_FIELD_ID); + attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string( + "pkg0"); EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event)); uidMap.updateMap( @@ -153,148 +214,148 @@ TEST(AtomMatcherTest, TestAttributionMatcher) { android::String16(""), android::String16("")}); EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event)); - attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0) - ->set_eq_string("pkg3"); + attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string( + "pkg3"); EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event)); - attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0) - ->set_eq_string("pkg2"); + attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string( + "pkg2"); EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event)); - attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0) - ->set_eq_string("pkg1"); + attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string( + "pkg1"); EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event)); - attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0) - ->set_eq_string("pkg0"); + attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string( + "pkg0"); EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event)); attributionMatcher->set_position(Position::FIRST); - attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0) - ->set_eq_string("pkg0"); + attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string( + "pkg0"); EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event)); - attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0) - ->set_eq_string("pkg3"); + attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string( + "pkg3"); EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event)); - attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0) - ->set_eq_string("pkg2"); + attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string( + "pkg2"); EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event)); - attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0) - ->set_eq_string("pkg1"); + attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string( + "pkg1"); EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event)); attributionMatcher->set_position(Position::LAST); - attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0) - ->set_eq_string("pkg0"); + attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string( + "pkg0"); EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event)); - attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0) - ->set_eq_string("pkg3"); + attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string( + "pkg3"); EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event)); - attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0) - ->set_eq_string("pkg2"); + attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string( + "pkg2"); EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event)); - attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0) - ->set_eq_string("pkg1"); + attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string( + "pkg1"); EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event)); // Uid + tag. attributionMatcher->set_position(Position::ANY); attributionMatcher->mutable_matches_tuple()->add_field_value_matcher()->set_field( - ATTRIBUTION_TAG_FIELD_ID); - attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0) - ->set_eq_string("pkg0"); - attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(1) - ->set_eq_string("location1"); - EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event)); - attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0) - ->set_eq_string("pkg1"); - attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(1) - ->set_eq_string("location1"); - EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event)); - attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0) - ->set_eq_string("pkg1"); - attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(1) - ->set_eq_string("location2"); - EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event)); - attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0) - ->set_eq_string("pkg2"); - attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(1) - ->set_eq_string("location3"); - EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event)); - attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0) - ->set_eq_string("pkg3"); - attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(1) - ->set_eq_string("location3"); - EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event)); - attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0) - ->set_eq_string("pkg3"); - attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(1) - ->set_eq_string("location1"); + ATTRIBUTION_TAG_FIELD_ID); + attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string( + "pkg0"); + attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(1)->set_eq_string( + "location1"); + EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event)); + attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string( + "pkg1"); + attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(1)->set_eq_string( + "location1"); + EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event)); + attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string( + "pkg1"); + attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(1)->set_eq_string( + "location2"); + EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event)); + attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string( + "pkg2"); + attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(1)->set_eq_string( + "location3"); + EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event)); + attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string( + "pkg3"); + attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(1)->set_eq_string( + "location3"); + EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event)); + attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string( + "pkg3"); + attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(1)->set_eq_string( + "location1"); EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event)); attributionMatcher->set_position(Position::FIRST); - attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0) - ->set_eq_string("pkg0"); - attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(1) - ->set_eq_string("location1"); + attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string( + "pkg0"); + attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(1)->set_eq_string( + "location1"); EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event)); - attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0) - ->set_eq_string("pkg1"); - attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(1) - ->set_eq_string("location1"); + attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string( + "pkg1"); + attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(1)->set_eq_string( + "location1"); EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event)); - attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0) - ->set_eq_string("pkg1"); - attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(1) - ->set_eq_string("location2"); + attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string( + "pkg1"); + attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(1)->set_eq_string( + "location2"); EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event)); - attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0) - ->set_eq_string("pkg2"); - attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(1) - ->set_eq_string("location3"); + attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string( + "pkg2"); + attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(1)->set_eq_string( + "location3"); EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event)); - attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0) - ->set_eq_string("pkg3"); - attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(1) - ->set_eq_string("location3"); + attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string( + "pkg3"); + attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(1)->set_eq_string( + "location3"); EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event)); - attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0) - ->set_eq_string("pkg3"); - attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(1) - ->set_eq_string("location1"); + attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string( + "pkg3"); + attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(1)->set_eq_string( + "location1"); EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event)); attributionMatcher->set_position(Position::LAST); - attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0) - ->set_eq_string("pkg0"); - attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(1) - ->set_eq_string("location1"); - EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event)); - attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0) - ->set_eq_string("pkg1"); - attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(1) - ->set_eq_string("location1"); - EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event)); - attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0) - ->set_eq_string("pkg1"); - attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(1) - ->set_eq_string("location2"); - EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event)); - attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0) - ->set_eq_string("pkg2"); - attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(1) - ->set_eq_string("location3"); - EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event)); - attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0) - ->set_eq_string("pkg3"); - attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(1) - ->set_eq_string("location3"); - EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event)); - attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0) - ->set_eq_string("pkg3"); - attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(1) - ->set_eq_string("location1"); + attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string( + "pkg0"); + attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(1)->set_eq_string( + "location1"); + EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event)); + attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string( + "pkg1"); + attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(1)->set_eq_string( + "location1"); + EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event)); + attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string( + "pkg1"); + attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(1)->set_eq_string( + "location2"); + EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event)); + attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string( + "pkg2"); + attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(1)->set_eq_string( + "location3"); + EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event)); + attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string( + "pkg3"); + attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(1)->set_eq_string( + "location3"); + EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event)); + attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(0)->set_eq_string( + "pkg3"); + attributionMatcher->mutable_matches_tuple()->mutable_field_value_matcher(1)->set_eq_string( + "location1"); EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event)); } -TEST(AtomMatcherTest, TestNeqAnyStringMatcher) { +TEST(AtomMatcherTest, TestUidFieldMatcher) { UidMap uidMap; uidMap.updateMap( 1, {1111, 1111, 2222, 3333, 3333} /* uid list */, {1, 1, 2, 1, 2} /* version list */, @@ -305,30 +366,48 @@ TEST(AtomMatcherTest, TestNeqAnyStringMatcher) { {android::String16(""), android::String16(""), android::String16(""), android::String16(""), android::String16("")}); - AttributionNodeInternal attribution_node1; - attribution_node1.set_uid(1111); - attribution_node1.set_tag("location1"); + // Set up matcher + AtomMatcher matcher; + auto simpleMatcher = matcher.mutable_simple_atom_matcher(); + simpleMatcher->set_atom_id(TAG_ID); + simpleMatcher->add_field_value_matcher()->set_field(1); + simpleMatcher->mutable_field_value_matcher(0)->set_eq_string("pkg0"); - AttributionNodeInternal attribution_node2; - attribution_node2.set_uid(2222); - attribution_node2.set_tag("location2"); + // Make event without is_uid annotation. + LogEvent event1(/*uid=*/0, /*pid=*/0); + makeIntLogEvent(&event1, TAG_ID, 0, 1111); + EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event1)); - AttributionNodeInternal attribution_node3; - attribution_node3.set_uid(3333); - attribution_node3.set_tag("location3"); + // Make event with is_uid annotation. + LogEvent event2(/*uid=*/0, /*pid=*/0); + makeIntWithBoolAnnotationLogEvent(&event2, TAG_ID_2, 1111, ANNOTATION_ID_IS_UID, true); - AttributionNodeInternal attribution_node4; - attribution_node4.set_uid(1066); - attribution_node4.set_tag("location3"); - std::vector<AttributionNodeInternal> attribution_nodes = {attribution_node1, attribution_node2, - attribution_node3, attribution_node4}; + // Event has is_uid annotation, so mapping from uid to package name occurs. + simpleMatcher->set_atom_id(TAG_ID_2); + EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event2)); + + // Event has is_uid annotation, but uid maps to different package name. + simpleMatcher->mutable_field_value_matcher(0)->set_eq_string("Pkg2"); + EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event2)); +} + +TEST(AtomMatcherTest, TestNeqAnyStringMatcher) { + UidMap uidMap; + uidMap.updateMap( + 1, {1111, 1111, 2222, 3333, 3333} /* uid list */, {1, 1, 2, 1, 2} /* version list */, + {android::String16("v1"), android::String16("v1"), android::String16("v2"), + android::String16("v1"), android::String16("v2")}, + {android::String16("pkg0"), android::String16("pkg1"), android::String16("pkg1"), + android::String16("Pkg2"), android::String16("PkG3")} /* package name list */, + {android::String16(""), android::String16(""), android::String16(""), + android::String16(""), android::String16("")}); + + std::vector<int> attributionUids = {1111, 2222, 3333, 1066}; + std::vector<string> attributionTags = {"location1", "location2", "location3", "location3"}; // Set up the event - LogEvent event(TAG_ID, 0); - event.write(attribution_nodes); - event.write("some value"); - // Convert to a LogEvent - event.init(); + LogEvent event(/*uid=*/0, /*pid=*/0); + makeAttributionLogEvent(&event, TAG_ID, 0, attributionUids, attributionTags, "some value"); // Set up the matcher AtomMatcher matcher; @@ -384,30 +463,12 @@ TEST(AtomMatcherTest, TestEqAnyStringMatcher) { {android::String16(""), android::String16(""), android::String16(""), android::String16(""), android::String16("")}); - AttributionNodeInternal attribution_node1; - attribution_node1.set_uid(1067); - attribution_node1.set_tag("location1"); - - AttributionNodeInternal attribution_node2; - attribution_node2.set_uid(2222); - attribution_node2.set_tag("location2"); - - AttributionNodeInternal attribution_node3; - attribution_node3.set_uid(3333); - attribution_node3.set_tag("location3"); - - AttributionNodeInternal attribution_node4; - attribution_node4.set_uid(1066); - attribution_node4.set_tag("location3"); - std::vector<AttributionNodeInternal> attribution_nodes = {attribution_node1, attribution_node2, - attribution_node3, attribution_node4}; + std::vector<int> attributionUids = {1067, 2222, 3333, 1066}; + std::vector<string> attributionTags = {"location1", "location2", "location3", "location3"}; // Set up the event - LogEvent event(TAG_ID, 0); - event.write(attribution_nodes); - event.write("some value"); - // Convert to a LogEvent - event.init(); + LogEvent event(/*uid=*/0, /*pid=*/0); + makeAttributionLogEvent(&event, TAG_ID, 0, attributionUids, attributionTags, "some value"); // Set up the matcher AtomMatcher matcher; @@ -467,11 +528,8 @@ TEST(AtomMatcherTest, TestBoolMatcher) { keyValue2->set_field(FIELD_ID_2); // Set up the event - LogEvent event(TAG_ID, 0); - EXPECT_TRUE(event.write(true)); - EXPECT_TRUE(event.write(false)); - // Convert to a LogEvent - event.init(); + LogEvent event(/*uid=*/0, /*pid=*/0); + makeBoolLogEvent(&event, TAG_ID, 0, true, false); // Test keyValue1->set_eq_bool(true); @@ -502,10 +560,8 @@ TEST(AtomMatcherTest, TestStringMatcher) { keyValue->set_eq_string("some value"); // Set up the event - LogEvent event(TAG_ID, 0); - event.write("some value"); - // Convert to a LogEvent - event.init(); + LogEvent event(/*uid=*/0, /*pid=*/0); + makeStringLogEvent(&event, TAG_ID, 0, "some value"); // Test EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event)); @@ -523,12 +579,8 @@ TEST(AtomMatcherTest, TestMultiFieldsMatcher) { keyValue2->set_field(FIELD_ID_2); // Set up the event - LogEvent event(TAG_ID, 0); - event.write(2); - event.write(3); - - // Convert to a LogEvent - event.init(); + LogEvent event(/*uid=*/0, /*pid=*/0); + CreateTwoValueLogEvent(&event, TAG_ID, 0, 2, 3); // Test keyValue1->set_eq_int(2); @@ -555,9 +607,8 @@ TEST(AtomMatcherTest, TestIntComparisonMatcher) { keyValue->set_field(FIELD_ID_1); // Set up the event - LogEvent event(TAG_ID, 0); - event.write(11); - event.init(); + LogEvent event(/*uid=*/0, /*pid=*/0); + makeIntLogEvent(&event, TAG_ID, 0, 11); // Test @@ -612,26 +663,22 @@ TEST(AtomMatcherTest, TestFloatComparisonMatcher) { auto keyValue = simpleMatcher->add_field_value_matcher(); keyValue->set_field(FIELD_ID_1); - LogEvent event1(TAG_ID, 0); + LogEvent event1(/*uid=*/0, /*pid=*/0); + makeFloatLogEvent(&event1, TAG_ID, 0, 10.1f); keyValue->set_lt_float(10.0); - event1.write(10.1f); - event1.init(); EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event1)); - LogEvent event2(TAG_ID, 0); - event2.write(9.9f); - event2.init(); + LogEvent event2(/*uid=*/0, /*pid=*/0); + makeFloatLogEvent(&event2, TAG_ID, 0, 9.9f); EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event2)); - LogEvent event3(TAG_ID, 0); - event3.write(10.1f); - event3.init(); + LogEvent event3(/*uid=*/0, /*pid=*/0); + makeFloatLogEvent(&event3, TAG_ID, 0, 10.1f); keyValue->set_gt_float(10.0); EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event3)); - LogEvent event4(TAG_ID, 0); - event4.write(9.9f); - event4.init(); + LogEvent event4(/*uid=*/0, /*pid=*/0); + makeFloatLogEvent(&event4, TAG_ID, 0, 9.9f); EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event4)); } |