diff options
| author | 2019-06-24 18:25:38 -0700 | |
|---|---|---|
| committer | 2019-06-26 09:46:56 -0700 | |
| commit | 7a57b8e49865b5ebcd00a78cb562a298cc6643a6 (patch) | |
| tree | b413a47f4bef320d1344dfa2be6bd2e9a72b387e | |
| parent | 3b77412d513ef8b6b548e91449b94b4c50f44f1e (diff) | |
Matching regular uid field with pkg name in statsd
Bug: 124377157
Test: bit statsd_test:*
Change-Id: I6844ea1a9eade4bfcd3698dfbc2bf8475c8e2bec
| -rw-r--r-- | cmds/statsd/src/FieldValue.cpp | 14 | ||||
| -rw-r--r-- | cmds/statsd/src/FieldValue.h | 1 | ||||
| -rw-r--r-- | cmds/statsd/src/matchers/matcher_util.cpp | 2 | ||||
| -rw-r--r-- | cmds/statsd/tests/LogEntryMatcher_test.cpp | 40 |
4 files changed, 55 insertions, 2 deletions
diff --git a/cmds/statsd/src/FieldValue.cpp b/cmds/statsd/src/FieldValue.cpp index 13f5c8ae5fd8..1185127ab805 100644 --- a/cmds/statsd/src/FieldValue.cpp +++ b/cmds/statsd/src/FieldValue.cpp @@ -149,6 +149,18 @@ bool isAttributionUidField(const Field& field, const Value& value) { return false; } +bool isUidField(const Field& field, const Value& value) { + auto it = android::util::AtomsInfo::kAtomsWithUidField.find(field.getTag()); + + if (it != android::util::AtomsInfo::kAtomsWithUidField.end()) { + int uidField = it->second; + return field.getDepth() == 0 && field.getPosAtDepth(0) == uidField && + value.getType() == INT; + } + + return false; +} + Value::Value(const Value& from) { type = from.getType(); switch (type) { @@ -464,4 +476,4 @@ bool HasPositionALL(const FieldMatcher& matcher) { } // namespace statsd } // namespace os -} // namespace android
\ No newline at end of file +} // namespace android diff --git a/cmds/statsd/src/FieldValue.h b/cmds/statsd/src/FieldValue.h index 6729e052b5ee..0e033e06c203 100644 --- a/cmds/statsd/src/FieldValue.h +++ b/cmds/statsd/src/FieldValue.h @@ -392,6 +392,7 @@ int getUidIfExists(const FieldValue& value); void translateFieldMatcher(const FieldMatcher& matcher, std::vector<Matcher>* output); bool isAttributionUidField(const Field& field, const Value& value); +bool isUidField(const Field& field, const Value& value); bool equalDimensions(const std::vector<Matcher>& dimension_a, const std::vector<Matcher>& dimension_b); diff --git a/cmds/statsd/src/matchers/matcher_util.cpp b/cmds/statsd/src/matchers/matcher_util.cpp index 8dc5cef988b0..10ac4a182f87 100644 --- a/cmds/statsd/src/matchers/matcher_util.cpp +++ b/cmds/statsd/src/matchers/matcher_util.cpp @@ -84,7 +84,7 @@ bool combinationMatch(const vector<int>& children, const LogicalOperation& opera bool tryMatchString(const UidMap& uidMap, const Field& field, const Value& value, const string& str_match) { - if (isAttributionUidField(field, value)) { + if (isAttributionUidField(field, value) || isUidField(field, value)) { int uid = value.int_value; auto aidIt = UidMap::sAidToUidMapping.find(str_match); if (aidIt != UidMap::sAidToUidMapping.end()) { diff --git a/cmds/statsd/tests/LogEntryMatcher_test.cpp b/cmds/statsd/tests/LogEntryMatcher_test.cpp index 2b9528f7d1de..70f0f6f75a59 100644 --- a/cmds/statsd/tests/LogEntryMatcher_test.cpp +++ b/cmds/statsd/tests/LogEntryMatcher_test.cpp @@ -29,6 +29,7 @@ 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; @@ -297,6 +298,45 @@ TEST(AtomMatcherTest, TestAttributionMatcher) { EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event)); } +TEST(AtomMatcherTest, TestUidFieldMatcher) { + 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("")}); + + // 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"); + + // Set up the event + LogEvent event(TAG_ID, 0); + event.write(1111); + event.init(); + + LogEvent event2(TAG_ID_2, 0); + event2.write(1111); + event2.write("some value"); + event2.init(); + + // Tag not in kAtomsWithUidField + EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event)); + + // Tag found in kAtomsWithUidField and has matching uid + EXPECT_TRUE(matchesSimple(uidMap, *simpleMatcher, event2)); + + // Tag found in kAtomsWithUidField but has non-matching uid + simpleMatcher->mutable_field_value_matcher(0)->set_eq_string("Pkg2"); + EXPECT_FALSE(matchesSimple(uidMap, *simpleMatcher, event2)); +} + TEST(AtomMatcherTest, TestNeqAnyStringMatcher) { UidMap uidMap; uidMap.updateMap( |