Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | #include <gtest/gtest.h> |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 17 | |
Yao Chen | 9c1debe | 2018-02-19 14:39:19 -0800 | [diff] [blame] | 18 | #include "frameworks/base/cmds/statsd/src/stats_log.pb.h" |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 19 | #include "frameworks/base/cmds/statsd/src/statsd_config.pb.h" |
| 20 | #include "matchers/matcher_util.h" |
Yao Chen | 9c1debe | 2018-02-19 14:39:19 -0800 | [diff] [blame] | 21 | #include "src/logd/LogEvent.h" |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 22 | #include "stats_event.h" |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 23 | #include "stats_log_util.h" |
| 24 | #include "stats_util.h" |
| 25 | #include "subscriber/SubscriberReporter.h" |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 26 | #include "tests/statsd_test_util.h" |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 27 | |
| 28 | #ifdef __ANDROID__ |
| 29 | |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 30 | using android::util::ProtoReader; |
| 31 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 32 | namespace android { |
| 33 | namespace os { |
| 34 | namespace statsd { |
| 35 | |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 36 | namespace { |
| 37 | void makeLogEvent(LogEvent* logEvent, const int32_t atomId, const int64_t timestamp, |
| 38 | const vector<int>& attributionUids, const vector<string>& attributionTags, |
| 39 | const string& name) { |
| 40 | AStatsEvent* statsEvent = AStatsEvent_obtain(); |
| 41 | AStatsEvent_setAtomId(statsEvent, atomId); |
| 42 | AStatsEvent_overwriteTimestamp(statsEvent, timestamp); |
| 43 | |
tsaichristine | 8dca82e | 2020-04-07 09:40:03 -0700 | [diff] [blame] | 44 | writeAttribution(statsEvent, attributionUids, attributionTags); |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 45 | AStatsEvent_writeString(statsEvent, name.c_str()); |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 46 | |
tsaichristine | 8dca82e | 2020-04-07 09:40:03 -0700 | [diff] [blame] | 47 | parseStatsEventToLogEvent(statsEvent, logEvent); |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | void makeLogEvent(LogEvent* logEvent, const int32_t atomId, const int64_t timestamp, |
| 51 | const vector<int>& attributionUids, const vector<string>& attributionTags, |
| 52 | const int32_t value) { |
| 53 | AStatsEvent* statsEvent = AStatsEvent_obtain(); |
| 54 | AStatsEvent_setAtomId(statsEvent, atomId); |
| 55 | AStatsEvent_overwriteTimestamp(statsEvent, timestamp); |
| 56 | |
tsaichristine | 8dca82e | 2020-04-07 09:40:03 -0700 | [diff] [blame] | 57 | writeAttribution(statsEvent, attributionUids, attributionTags); |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 58 | AStatsEvent_writeInt32(statsEvent, value); |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 59 | |
tsaichristine | 8dca82e | 2020-04-07 09:40:03 -0700 | [diff] [blame] | 60 | parseStatsEventToLogEvent(statsEvent, logEvent); |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 61 | } |
| 62 | } // anonymous namespace |
| 63 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 64 | TEST(AtomMatcherTest, TestFieldTranslation) { |
| 65 | FieldMatcher matcher1; |
| 66 | matcher1.set_field(10); |
| 67 | FieldMatcher* child = matcher1.add_child(); |
| 68 | child->set_field(1); |
| 69 | child->set_position(Position::ANY); |
| 70 | |
| 71 | child = child->add_child(); |
| 72 | child->set_field(1); |
| 73 | |
| 74 | vector<Matcher> output; |
| 75 | translateFieldMatcher(matcher1, &output); |
| 76 | |
| 77 | EXPECT_EQ((size_t)1, output.size()); |
| 78 | |
| 79 | const auto& matcher12 = output[0]; |
| 80 | EXPECT_EQ((int32_t)10, matcher12.mMatcher.getTag()); |
Yangster-mac | e06cfd7 | 2018-03-10 23:22:59 -0800 | [diff] [blame] | 81 | EXPECT_EQ((int32_t)0x02010001, matcher12.mMatcher.getField()); |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 82 | EXPECT_EQ((int32_t)0xff7f007f, matcher12.mMask); |
| 83 | } |
| 84 | |
Yangster-mac | e06cfd7 | 2018-03-10 23:22:59 -0800 | [diff] [blame] | 85 | TEST(AtomMatcherTest, TestFieldTranslation_ALL) { |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 86 | FieldMatcher matcher1; |
| 87 | matcher1.set_field(10); |
| 88 | FieldMatcher* child = matcher1.add_child(); |
| 89 | child->set_field(1); |
Yangster-mac | e06cfd7 | 2018-03-10 23:22:59 -0800 | [diff] [blame] | 90 | child->set_position(Position::ALL); |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 91 | |
| 92 | child = child->add_child(); |
| 93 | child->set_field(1); |
| 94 | |
Yangster-mac | e06cfd7 | 2018-03-10 23:22:59 -0800 | [diff] [blame] | 95 | vector<Matcher> output; |
| 96 | translateFieldMatcher(matcher1, &output); |
| 97 | |
| 98 | EXPECT_EQ((size_t)1, output.size()); |
| 99 | |
| 100 | const auto& matcher12 = output[0]; |
| 101 | EXPECT_EQ((int32_t)10, matcher12.mMatcher.getTag()); |
| 102 | EXPECT_EQ((int32_t)0x02010001, matcher12.mMatcher.getField()); |
| 103 | EXPECT_EQ((int32_t)0xff7f7f7f, matcher12.mMask); |
| 104 | } |
| 105 | |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 106 | TEST(AtomMatcherTest, TestFilter_ALL) { |
| 107 | FieldMatcher matcher1; |
| 108 | matcher1.set_field(10); |
| 109 | FieldMatcher* child = matcher1.add_child(); |
| 110 | child->set_field(1); |
| 111 | child->set_position(Position::ALL); |
| 112 | |
| 113 | child->add_child()->set_field(1); |
| 114 | child->add_child()->set_field(2); |
| 115 | |
| 116 | child = matcher1.add_child(); |
| 117 | child->set_field(2); |
| 118 | |
| 119 | vector<Matcher> matchers; |
| 120 | translateFieldMatcher(matcher1, &matchers); |
| 121 | |
| 122 | std::vector<int> attributionUids = {1111, 2222, 3333}; |
| 123 | std::vector<string> attributionTags = {"location1", "location2", "location3"}; |
| 124 | |
| 125 | LogEvent event(/*uid=*/0, /*pid=*/0); |
| 126 | makeLogEvent(&event, 10 /*atomId*/, 1012345, attributionUids, attributionTags, "some value"); |
| 127 | HashableDimensionKey output; |
| 128 | |
| 129 | filterValues(matchers, event.getValues(), &output); |
| 130 | |
| 131 | EXPECT_EQ((size_t)7, output.getValues().size()); |
| 132 | EXPECT_EQ((int32_t)0x02010101, output.getValues()[0].mField.getField()); |
| 133 | EXPECT_EQ((int32_t)1111, output.getValues()[0].mValue.int_value); |
| 134 | EXPECT_EQ((int32_t)0x02010102, output.getValues()[1].mField.getField()); |
| 135 | EXPECT_EQ("location1", output.getValues()[1].mValue.str_value); |
| 136 | |
| 137 | EXPECT_EQ((int32_t)0x02010201, output.getValues()[2].mField.getField()); |
| 138 | EXPECT_EQ((int32_t)2222, output.getValues()[2].mValue.int_value); |
| 139 | EXPECT_EQ((int32_t)0x02010202, output.getValues()[3].mField.getField()); |
| 140 | EXPECT_EQ("location2", output.getValues()[3].mValue.str_value); |
| 141 | |
| 142 | EXPECT_EQ((int32_t)0x02010301, output.getValues()[4].mField.getField()); |
| 143 | EXPECT_EQ((int32_t)3333, output.getValues()[4].mValue.int_value); |
| 144 | EXPECT_EQ((int32_t)0x02010302, output.getValues()[5].mField.getField()); |
| 145 | EXPECT_EQ("location3", output.getValues()[5].mValue.str_value); |
| 146 | |
| 147 | EXPECT_EQ((int32_t)0x00020000, output.getValues()[6].mField.getField()); |
| 148 | EXPECT_EQ("some value", output.getValues()[6].mValue.str_value); |
| 149 | } |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 150 | |
| 151 | TEST(AtomMatcherTest, TestSubDimension) { |
| 152 | HashableDimensionKey dim; |
| 153 | |
| 154 | int pos1[] = {1, 1, 1}; |
| 155 | int pos2[] = {1, 1, 2}; |
| 156 | int pos3[] = {1, 1, 3}; |
| 157 | int pos4[] = {2, 0, 0}; |
| 158 | Field field1(10, pos1, 2); |
| 159 | Field field2(10, pos2, 2); |
| 160 | |
| 161 | Field field3(10, pos3, 2); |
| 162 | Field field4(10, pos4, 0); |
| 163 | |
| 164 | Value value1((int32_t)10025); |
| 165 | Value value2("tag"); |
| 166 | |
| 167 | Value value11((int32_t)10026); |
| 168 | Value value22("tag2"); |
| 169 | |
| 170 | dim.addValue(FieldValue(field1, value1)); |
| 171 | dim.addValue(FieldValue(field2, value2)); |
| 172 | |
| 173 | HashableDimensionKey subDim1; |
| 174 | subDim1.addValue(FieldValue(field1, value1)); |
| 175 | |
| 176 | HashableDimensionKey subDim2; |
| 177 | subDim1.addValue(FieldValue(field2, value2)); |
| 178 | |
| 179 | EXPECT_TRUE(dim.contains(dim)); |
| 180 | EXPECT_TRUE(dim.contains(subDim1)); |
| 181 | EXPECT_TRUE(dim.contains(subDim2)); |
| 182 | |
| 183 | HashableDimensionKey subDim3; |
| 184 | subDim3.addValue(FieldValue(field1, value11)); |
| 185 | EXPECT_FALSE(dim.contains(subDim3)); |
| 186 | |
| 187 | HashableDimensionKey subDim4; |
| 188 | // Empty dimension is always a sub dimension of other dimensions |
| 189 | EXPECT_TRUE(dim.contains(subDim4)); |
| 190 | } |
| 191 | |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 192 | TEST(AtomMatcherTest, TestMetric2ConditionLink) { |
| 193 | std::vector<int> attributionUids = {1111, 2222, 3333}; |
| 194 | std::vector<string> attributionTags = {"location1", "location2", "location3"}; |
| 195 | |
| 196 | LogEvent event(/*uid=*/0, /*pid=*/0); |
| 197 | makeLogEvent(&event, 10 /*atomId*/, 12345, attributionUids, attributionTags, "some value"); |
| 198 | |
| 199 | FieldMatcher whatMatcher; |
| 200 | whatMatcher.set_field(10); |
| 201 | FieldMatcher* child11 = whatMatcher.add_child(); |
| 202 | child11->set_field(1); |
| 203 | child11->set_position(Position::ANY); |
| 204 | child11 = child11->add_child(); |
| 205 | child11->set_field(1); |
| 206 | |
| 207 | FieldMatcher conditionMatcher; |
| 208 | conditionMatcher.set_field(27); |
| 209 | FieldMatcher* child2 = conditionMatcher.add_child(); |
| 210 | child2->set_field(2); |
| 211 | child2->set_position(Position::LAST); |
| 212 | |
| 213 | child2 = child2->add_child(); |
| 214 | child2->set_field(2); |
| 215 | |
| 216 | Metric2Condition link; |
| 217 | |
| 218 | translateFieldMatcher(whatMatcher, &link.metricFields); |
| 219 | translateFieldMatcher(conditionMatcher, &link.conditionFields); |
| 220 | |
| 221 | EXPECT_EQ((size_t)1, link.metricFields.size()); |
| 222 | EXPECT_EQ((int32_t)0x02010001, link.metricFields[0].mMatcher.getField()); |
| 223 | EXPECT_EQ((int32_t)0xff7f007f, link.metricFields[0].mMask); |
| 224 | EXPECT_EQ((int32_t)10, link.metricFields[0].mMatcher.getTag()); |
| 225 | |
| 226 | EXPECT_EQ((size_t)1, link.conditionFields.size()); |
| 227 | EXPECT_EQ((int32_t)0x02028002, link.conditionFields[0].mMatcher.getField()); |
| 228 | EXPECT_EQ((int32_t)0xff7f807f, link.conditionFields[0].mMask); |
| 229 | EXPECT_EQ((int32_t)27, link.conditionFields[0].mMatcher.getTag()); |
| 230 | } |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 231 | |
Yangster-mac | 9def8e3 | 2018-04-17 13:55:51 -0700 | [diff] [blame] | 232 | TEST(AtomMatcherTest, TestWriteDimensionPath) { |
| 233 | for (auto position : {Position::ANY, Position::ALL, Position::FIRST, Position::LAST}) { |
| 234 | FieldMatcher matcher1; |
| 235 | matcher1.set_field(10); |
| 236 | FieldMatcher* child = matcher1.add_child(); |
| 237 | child->set_field(2); |
| 238 | child->set_position(position); |
| 239 | child->add_child()->set_field(1); |
| 240 | child->add_child()->set_field(3); |
| 241 | |
| 242 | child = matcher1.add_child(); |
| 243 | child->set_field(4); |
| 244 | |
| 245 | child = matcher1.add_child(); |
| 246 | child->set_field(6); |
| 247 | child->add_child()->set_field(2); |
| 248 | |
| 249 | vector<Matcher> matchers; |
| 250 | translateFieldMatcher(matcher1, &matchers); |
| 251 | |
| 252 | android::util::ProtoOutputStream protoOut; |
| 253 | writeDimensionPathToProto(matchers, &protoOut); |
| 254 | |
| 255 | vector<uint8_t> outData; |
| 256 | outData.resize(protoOut.size()); |
| 257 | size_t pos = 0; |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 258 | sp<ProtoReader> reader = protoOut.data(); |
| 259 | while (reader->readBuffer() != NULL) { |
| 260 | size_t toRead = reader->currentToRead(); |
| 261 | std::memcpy(&(outData[pos]), reader->readBuffer(), toRead); |
Yangster-mac | 9def8e3 | 2018-04-17 13:55:51 -0700 | [diff] [blame] | 262 | pos += toRead; |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 263 | reader->move(toRead); |
Yangster-mac | 9def8e3 | 2018-04-17 13:55:51 -0700 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | DimensionsValue result; |
| 267 | EXPECT_EQ(true, result.ParseFromArray(&outData[0], outData.size())); |
| 268 | |
| 269 | EXPECT_EQ(10, result.field()); |
| 270 | EXPECT_EQ(DimensionsValue::ValueCase::kValueTuple, result.value_case()); |
| 271 | EXPECT_EQ(3, result.value_tuple().dimensions_value_size()); |
| 272 | |
| 273 | const auto& dim1 = result.value_tuple().dimensions_value(0); |
| 274 | EXPECT_EQ(2, dim1.field()); |
| 275 | EXPECT_EQ(2, dim1.value_tuple().dimensions_value_size()); |
| 276 | |
| 277 | const auto& dim11 = dim1.value_tuple().dimensions_value(0); |
| 278 | EXPECT_EQ(1, dim11.field()); |
| 279 | |
| 280 | const auto& dim12 = dim1.value_tuple().dimensions_value(1); |
| 281 | EXPECT_EQ(3, dim12.field()); |
| 282 | |
| 283 | const auto& dim2 = result.value_tuple().dimensions_value(1); |
| 284 | EXPECT_EQ(4, dim2.field()); |
| 285 | |
| 286 | const auto& dim3 = result.value_tuple().dimensions_value(2); |
| 287 | EXPECT_EQ(6, dim3.field()); |
| 288 | EXPECT_EQ(1, dim3.value_tuple().dimensions_value_size()); |
| 289 | const auto& dim31 = dim3.value_tuple().dimensions_value(0); |
| 290 | EXPECT_EQ(2, dim31.field()); |
| 291 | } |
| 292 | } |
| 293 | |
Ruchir Rastogi | 56da4c3 | 2020-02-03 18:53:24 -0800 | [diff] [blame] | 294 | //TODO(b/149050405) Update this test for StatsDimensionValueParcel |
| 295 | //TEST(AtomMatcherTest, TestSubscriberDimensionWrite) { |
| 296 | // HashableDimensionKey dim; |
| 297 | // |
| 298 | // int pos1[] = {1, 1, 1}; |
| 299 | // int pos2[] = {1, 1, 2}; |
| 300 | // int pos3[] = {1, 1, 3}; |
| 301 | // int pos4[] = {2, 0, 0}; |
| 302 | // |
| 303 | // Field field1(10, pos1, 2); |
| 304 | // Field field2(10, pos2, 2); |
| 305 | // Field field3(10, pos3, 2); |
| 306 | // Field field4(10, pos4, 0); |
| 307 | // |
| 308 | // Value value1((int32_t)10025); |
| 309 | // Value value2("tag"); |
| 310 | // Value value3((int32_t)987654); |
| 311 | // Value value4((int32_t)99999); |
| 312 | // |
| 313 | // dim.addValue(FieldValue(field1, value1)); |
| 314 | // dim.addValue(FieldValue(field2, value2)); |
| 315 | // dim.addValue(FieldValue(field3, value3)); |
| 316 | // dim.addValue(FieldValue(field4, value4)); |
| 317 | // |
| 318 | // SubscriberReporter::getStatsDimensionsValue(dim); |
| 319 | // // TODO(b/110562792): can't test anything here because StatsDimensionsValue class doesn't |
| 320 | // // have any read api. |
| 321 | //} |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 322 | |
| 323 | TEST(AtomMatcherTest, TestWriteDimensionToProto) { |
| 324 | HashableDimensionKey dim; |
| 325 | int pos1[] = {1, 1, 1}; |
| 326 | int pos2[] = {1, 1, 2}; |
| 327 | int pos3[] = {1, 1, 3}; |
| 328 | int pos4[] = {2, 0, 0}; |
| 329 | Field field1(10, pos1, 2); |
| 330 | Field field2(10, pos2, 2); |
| 331 | Field field3(10, pos3, 2); |
| 332 | Field field4(10, pos4, 0); |
| 333 | |
| 334 | Value value1((int32_t)10025); |
| 335 | Value value2("tag"); |
| 336 | Value value3((int32_t)987654); |
| 337 | Value value4((int32_t)99999); |
| 338 | |
| 339 | dim.addValue(FieldValue(field1, value1)); |
| 340 | dim.addValue(FieldValue(field2, value2)); |
| 341 | dim.addValue(FieldValue(field3, value3)); |
| 342 | dim.addValue(FieldValue(field4, value4)); |
| 343 | |
| 344 | android::util::ProtoOutputStream protoOut; |
Yangster-mac | 9def8e3 | 2018-04-17 13:55:51 -0700 | [diff] [blame] | 345 | writeDimensionToProto(dim, nullptr /* include strings */, &protoOut); |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 346 | |
| 347 | vector<uint8_t> outData; |
| 348 | outData.resize(protoOut.size()); |
| 349 | size_t pos = 0; |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 350 | sp<ProtoReader> reader = protoOut.data(); |
| 351 | while (reader->readBuffer() != NULL) { |
| 352 | size_t toRead = reader->currentToRead(); |
| 353 | std::memcpy(&(outData[pos]), reader->readBuffer(), toRead); |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 354 | pos += toRead; |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 355 | reader->move(toRead); |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | DimensionsValue result; |
| 359 | EXPECT_EQ(true, result.ParseFromArray(&outData[0], outData.size())); |
| 360 | EXPECT_EQ(10, result.field()); |
| 361 | EXPECT_EQ(DimensionsValue::ValueCase::kValueTuple, result.value_case()); |
| 362 | EXPECT_EQ(2, result.value_tuple().dimensions_value_size()); |
| 363 | |
| 364 | const auto& dim1 = result.value_tuple().dimensions_value(0); |
| 365 | EXPECT_EQ(DimensionsValue::ValueCase::kValueTuple, dim1.value_case()); |
| 366 | EXPECT_EQ(3, dim1.value_tuple().dimensions_value_size()); |
| 367 | |
| 368 | const auto& dim11 = dim1.value_tuple().dimensions_value(0); |
| 369 | EXPECT_EQ(DimensionsValue::ValueCase::kValueInt, dim11.value_case()); |
| 370 | EXPECT_EQ(10025, dim11.value_int()); |
| 371 | |
| 372 | const auto& dim12 = dim1.value_tuple().dimensions_value(1); |
| 373 | EXPECT_EQ(DimensionsValue::ValueCase::kValueStr, dim12.value_case()); |
| 374 | EXPECT_EQ("tag", dim12.value_str()); |
| 375 | |
| 376 | const auto& dim13 = dim1.value_tuple().dimensions_value(2); |
| 377 | EXPECT_EQ(DimensionsValue::ValueCase::kValueInt, dim13.value_case()); |
| 378 | EXPECT_EQ(987654, dim13.value_int()); |
| 379 | |
| 380 | const auto& dim2 = result.value_tuple().dimensions_value(1); |
| 381 | EXPECT_EQ(DimensionsValue::ValueCase::kValueInt, dim2.value_case()); |
| 382 | EXPECT_EQ(99999, dim2.value_int()); |
| 383 | } |
| 384 | |
Yangster-mac | 9def8e3 | 2018-04-17 13:55:51 -0700 | [diff] [blame] | 385 | TEST(AtomMatcherTest, TestWriteDimensionLeafNodesToProto) { |
| 386 | HashableDimensionKey dim; |
| 387 | int pos1[] = {1, 1, 1}; |
| 388 | int pos2[] = {1, 1, 2}; |
| 389 | int pos3[] = {1, 1, 3}; |
| 390 | int pos4[] = {2, 0, 0}; |
| 391 | Field field1(10, pos1, 2); |
| 392 | Field field2(10, pos2, 2); |
| 393 | Field field3(10, pos3, 2); |
| 394 | Field field4(10, pos4, 0); |
| 395 | |
| 396 | Value value1((int32_t)10025); |
| 397 | Value value2("tag"); |
| 398 | Value value3((int32_t)987654); |
| 399 | Value value4((int64_t)99999); |
| 400 | |
| 401 | dim.addValue(FieldValue(field1, value1)); |
| 402 | dim.addValue(FieldValue(field2, value2)); |
| 403 | dim.addValue(FieldValue(field3, value3)); |
| 404 | dim.addValue(FieldValue(field4, value4)); |
| 405 | |
| 406 | android::util::ProtoOutputStream protoOut; |
| 407 | writeDimensionLeafNodesToProto(dim, 1, nullptr /* include strings */, &protoOut); |
| 408 | |
| 409 | vector<uint8_t> outData; |
| 410 | outData.resize(protoOut.size()); |
| 411 | size_t pos = 0; |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 412 | sp<ProtoReader> reader = protoOut.data(); |
| 413 | while (reader->readBuffer() != NULL) { |
| 414 | size_t toRead = reader->currentToRead(); |
| 415 | std::memcpy(&(outData[pos]), reader->readBuffer(), toRead); |
Yangster-mac | 9def8e3 | 2018-04-17 13:55:51 -0700 | [diff] [blame] | 416 | pos += toRead; |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 417 | reader->move(toRead); |
Yangster-mac | 9def8e3 | 2018-04-17 13:55:51 -0700 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | DimensionsValueTuple result; |
| 421 | EXPECT_EQ(true, result.ParseFromArray(&outData[0], outData.size())); |
| 422 | EXPECT_EQ(4, result.dimensions_value_size()); |
| 423 | |
| 424 | const auto& dim1 = result.dimensions_value(0); |
| 425 | EXPECT_EQ(DimensionsValue::ValueCase::kValueInt, dim1.value_case()); |
| 426 | EXPECT_EQ(10025, dim1.value_int()); |
| 427 | |
| 428 | const auto& dim2 = result.dimensions_value(1); |
| 429 | EXPECT_EQ(DimensionsValue::ValueCase::kValueStr, dim2.value_case()); |
| 430 | EXPECT_EQ("tag", dim2.value_str()); |
| 431 | |
| 432 | const auto& dim3 = result.dimensions_value(2); |
| 433 | EXPECT_EQ(DimensionsValue::ValueCase::kValueInt, dim3.value_case()); |
| 434 | EXPECT_EQ(987654, dim3.value_int()); |
| 435 | |
| 436 | const auto& dim4 = result.dimensions_value(3); |
| 437 | EXPECT_EQ(DimensionsValue::ValueCase::kValueLong, dim4.value_case()); |
| 438 | EXPECT_EQ(99999, dim4.value_long()); |
| 439 | } |
| 440 | |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 441 | TEST(AtomMatcherTest, TestWriteAtomToProto) { |
| 442 | std::vector<int> attributionUids = {1111, 2222}; |
| 443 | std::vector<string> attributionTags = {"location1", "location2"}; |
| 444 | |
| 445 | LogEvent event(/*uid=*/0, /*pid=*/0); |
| 446 | makeLogEvent(&event, 4 /*atomId*/, 12345, attributionUids, attributionTags, 999); |
| 447 | |
| 448 | android::util::ProtoOutputStream protoOutput; |
| 449 | writeFieldValueTreeToStream(event.GetTagId(), event.getValues(), &protoOutput); |
| 450 | |
| 451 | vector<uint8_t> outData; |
| 452 | outData.resize(protoOutput.size()); |
| 453 | size_t pos = 0; |
| 454 | sp<ProtoReader> reader = protoOutput.data(); |
| 455 | while (reader->readBuffer() != NULL) { |
| 456 | size_t toRead = reader->currentToRead(); |
| 457 | std::memcpy(&(outData[pos]), reader->readBuffer(), toRead); |
| 458 | pos += toRead; |
| 459 | reader->move(toRead); |
| 460 | } |
| 461 | |
| 462 | Atom result; |
| 463 | EXPECT_EQ(true, result.ParseFromArray(&outData[0], outData.size())); |
| 464 | EXPECT_EQ(Atom::PushedCase::kBleScanResultReceived, result.pushed_case()); |
| 465 | const auto& atom = result.ble_scan_result_received(); |
| 466 | EXPECT_EQ(2, atom.attribution_node_size()); |
| 467 | EXPECT_EQ(1111, atom.attribution_node(0).uid()); |
| 468 | EXPECT_EQ("location1", atom.attribution_node(0).tag()); |
| 469 | EXPECT_EQ(2222, atom.attribution_node(1).uid()); |
| 470 | EXPECT_EQ("location2", atom.attribution_node(1).tag()); |
| 471 | EXPECT_EQ(999, atom.num_results()); |
| 472 | } |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 473 | |
tsaichristine | 3a83e81 | 2019-12-13 16:46:11 -0800 | [diff] [blame] | 474 | /* |
| 475 | * Test two Matchers is not a subset of one Matcher. |
| 476 | * Test one Matcher is subset of two Matchers. |
| 477 | */ |
| 478 | TEST(AtomMatcherTest, TestSubsetDimensions1) { |
| 479 | // Initialize first set of matchers |
| 480 | FieldMatcher matcher1; |
| 481 | matcher1.set_field(10); |
| 482 | |
| 483 | FieldMatcher* child = matcher1.add_child(); |
| 484 | child->set_field(1); |
| 485 | child->set_position(Position::ALL); |
| 486 | child->add_child()->set_field(1); |
| 487 | child->add_child()->set_field(2); |
| 488 | |
| 489 | vector<Matcher> matchers1; |
| 490 | translateFieldMatcher(matcher1, &matchers1); |
| 491 | EXPECT_EQ(2, matchers1.size()); |
| 492 | |
| 493 | // Initialize second set of matchers |
| 494 | FieldMatcher matcher2; |
| 495 | matcher2.set_field(10); |
| 496 | |
| 497 | child = matcher2.add_child(); |
| 498 | child->set_field(1); |
| 499 | child->set_position(Position::ALL); |
| 500 | child->add_child()->set_field(1); |
| 501 | |
| 502 | vector<Matcher> matchers2; |
| 503 | translateFieldMatcher(matcher2, &matchers2); |
| 504 | EXPECT_EQ(1, matchers2.size()); |
| 505 | |
| 506 | EXPECT_FALSE(subsetDimensions(matchers1, matchers2)); |
| 507 | EXPECT_TRUE(subsetDimensions(matchers2, matchers1)); |
| 508 | } |
| 509 | /* |
| 510 | * Test not a subset with one matching Matcher, one non-matching Matcher. |
| 511 | */ |
| 512 | TEST(AtomMatcherTest, TestSubsetDimensions2) { |
| 513 | // Initialize first set of matchers |
| 514 | FieldMatcher matcher1; |
| 515 | matcher1.set_field(10); |
| 516 | |
| 517 | FieldMatcher* child = matcher1.add_child(); |
| 518 | child->set_field(1); |
| 519 | |
| 520 | child = matcher1.add_child(); |
| 521 | child->set_field(2); |
| 522 | |
| 523 | vector<Matcher> matchers1; |
| 524 | translateFieldMatcher(matcher1, &matchers1); |
| 525 | |
| 526 | // Initialize second set of matchers |
| 527 | FieldMatcher matcher2; |
| 528 | matcher2.set_field(10); |
| 529 | |
| 530 | child = matcher2.add_child(); |
| 531 | child->set_field(1); |
| 532 | |
| 533 | child = matcher2.add_child(); |
| 534 | child->set_field(3); |
| 535 | |
| 536 | vector<Matcher> matchers2; |
| 537 | translateFieldMatcher(matcher2, &matchers2); |
| 538 | |
| 539 | EXPECT_FALSE(subsetDimensions(matchers1, matchers2)); |
| 540 | } |
| 541 | |
| 542 | /* |
| 543 | * Test not a subset if parent field is not equal. |
| 544 | */ |
| 545 | TEST(AtomMatcherTest, TestSubsetDimensions3) { |
| 546 | // Initialize first set of matchers |
| 547 | FieldMatcher matcher1; |
| 548 | matcher1.set_field(10); |
| 549 | |
| 550 | FieldMatcher* child = matcher1.add_child(); |
| 551 | child->set_field(1); |
| 552 | |
| 553 | vector<Matcher> matchers1; |
| 554 | translateFieldMatcher(matcher1, &matchers1); |
| 555 | |
| 556 | // Initialize second set of matchers |
| 557 | FieldMatcher matcher2; |
| 558 | matcher2.set_field(5); |
| 559 | |
| 560 | child = matcher2.add_child(); |
| 561 | child->set_field(1); |
| 562 | |
| 563 | vector<Matcher> matchers2; |
| 564 | translateFieldMatcher(matcher2, &matchers2); |
| 565 | |
| 566 | EXPECT_FALSE(subsetDimensions(matchers1, matchers2)); |
| 567 | } |
| 568 | |
| 569 | /* |
| 570 | * Test is subset with two matching Matchers. |
| 571 | */ |
| 572 | TEST(AtomMatcherTest, TestSubsetDimensions4) { |
| 573 | // Initialize first set of matchers |
| 574 | FieldMatcher matcher1; |
| 575 | matcher1.set_field(10); |
| 576 | |
| 577 | FieldMatcher* child = matcher1.add_child(); |
| 578 | child->set_field(1); |
| 579 | |
| 580 | child = matcher1.add_child(); |
| 581 | child->set_field(2); |
| 582 | |
| 583 | vector<Matcher> matchers1; |
| 584 | translateFieldMatcher(matcher1, &matchers1); |
| 585 | |
| 586 | // Initialize second set of matchers |
| 587 | FieldMatcher matcher2; |
| 588 | matcher2.set_field(10); |
| 589 | |
| 590 | child = matcher2.add_child(); |
| 591 | child->set_field(1); |
| 592 | |
| 593 | child = matcher2.add_child(); |
| 594 | child->set_field(2); |
| 595 | |
| 596 | child = matcher2.add_child(); |
| 597 | child->set_field(3); |
| 598 | |
| 599 | vector<Matcher> matchers2; |
| 600 | translateFieldMatcher(matcher2, &matchers2); |
| 601 | |
| 602 | EXPECT_TRUE(subsetDimensions(matchers1, matchers2)); |
| 603 | EXPECT_FALSE(subsetDimensions(matchers2, matchers1)); |
| 604 | } |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 605 | |
| 606 | } // namespace statsd |
| 607 | } // namespace os |
| 608 | } // namespace android |
| 609 | #else |
| 610 | GTEST_LOG_(INFO) << "This test does nothing.\n"; |
Yao Chen | 5bfffb5 | 2018-06-21 16:58:51 -0700 | [diff] [blame] | 611 | #endif |