Merge "Matching regular uid field with pkg name in statsd"

This commit is contained in:
Christine Tsai
2019-06-26 18:43:34 +00:00
committed by Android (Google) Code Review
4 changed files with 55 additions and 2 deletions

View File

@@ -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
} // namespace android

View File

@@ -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);

View File

@@ -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()) {

View File

@@ -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(