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> |
Yao Chen | 9c1debe | 2018-02-19 14:39:19 -0800 | [diff] [blame] | 17 | #include "frameworks/base/cmds/statsd/src/stats_log.pb.h" |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 18 | #include "frameworks/base/cmds/statsd/src/statsd_config.pb.h" |
| 19 | #include "matchers/matcher_util.h" |
Yao Chen | 9c1debe | 2018-02-19 14:39:19 -0800 | [diff] [blame] | 20 | #include "src/logd/LogEvent.h" |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 21 | #include "stats_log_util.h" |
| 22 | #include "stats_util.h" |
| 23 | #include "subscriber/SubscriberReporter.h" |
| 24 | |
| 25 | #ifdef __ANDROID__ |
| 26 | |
| 27 | namespace android { |
| 28 | namespace os { |
| 29 | namespace statsd { |
| 30 | |
| 31 | TEST(AtomMatcherTest, TestFieldTranslation) { |
| 32 | FieldMatcher matcher1; |
| 33 | matcher1.set_field(10); |
| 34 | FieldMatcher* child = matcher1.add_child(); |
| 35 | child->set_field(1); |
| 36 | child->set_position(Position::ANY); |
| 37 | |
| 38 | child = child->add_child(); |
| 39 | child->set_field(1); |
| 40 | |
| 41 | vector<Matcher> output; |
| 42 | translateFieldMatcher(matcher1, &output); |
| 43 | |
| 44 | EXPECT_EQ((size_t)1, output.size()); |
| 45 | |
| 46 | const auto& matcher12 = output[0]; |
| 47 | EXPECT_EQ((int32_t)10, matcher12.mMatcher.getTag()); |
Yangster-mac | e06cfd7 | 2018-03-10 23:22:59 -0800 | [diff] [blame^] | 48 | EXPECT_EQ((int32_t)0x02010001, matcher12.mMatcher.getField()); |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 49 | EXPECT_EQ((int32_t)0xff7f007f, matcher12.mMask); |
| 50 | } |
| 51 | |
Yangster-mac | e06cfd7 | 2018-03-10 23:22:59 -0800 | [diff] [blame^] | 52 | TEST(AtomMatcherTest, TestFieldTranslation_ALL) { |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 53 | FieldMatcher matcher1; |
| 54 | matcher1.set_field(10); |
| 55 | FieldMatcher* child = matcher1.add_child(); |
| 56 | child->set_field(1); |
Yangster-mac | e06cfd7 | 2018-03-10 23:22:59 -0800 | [diff] [blame^] | 57 | child->set_position(Position::ALL); |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 58 | |
| 59 | child = child->add_child(); |
| 60 | child->set_field(1); |
| 61 | |
Yangster-mac | e06cfd7 | 2018-03-10 23:22:59 -0800 | [diff] [blame^] | 62 | vector<Matcher> output; |
| 63 | translateFieldMatcher(matcher1, &output); |
| 64 | |
| 65 | EXPECT_EQ((size_t)1, output.size()); |
| 66 | |
| 67 | const auto& matcher12 = output[0]; |
| 68 | EXPECT_EQ((int32_t)10, matcher12.mMatcher.getTag()); |
| 69 | EXPECT_EQ((int32_t)0x02010001, matcher12.mMatcher.getField()); |
| 70 | EXPECT_EQ((int32_t)0xff7f7f7f, matcher12.mMask); |
| 71 | } |
| 72 | |
| 73 | TEST(AtomMatcherTest, TestFilter_ALL) { |
| 74 | FieldMatcher matcher1; |
| 75 | matcher1.set_field(10); |
| 76 | FieldMatcher* child = matcher1.add_child(); |
| 77 | child->set_field(1); |
| 78 | child->set_position(Position::ALL); |
| 79 | |
| 80 | child->add_child()->set_field(1); |
| 81 | child->add_child()->set_field(2); |
| 82 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 83 | child = matcher1.add_child(); |
| 84 | child->set_field(2); |
| 85 | |
| 86 | vector<Matcher> matchers; |
| 87 | translateFieldMatcher(matcher1, &matchers); |
| 88 | |
Yao Chen | 9c1debe | 2018-02-19 14:39:19 -0800 | [diff] [blame] | 89 | AttributionNodeInternal attribution_node1; |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 90 | attribution_node1.set_uid(1111); |
| 91 | attribution_node1.set_tag("location1"); |
| 92 | |
Yao Chen | 9c1debe | 2018-02-19 14:39:19 -0800 | [diff] [blame] | 93 | AttributionNodeInternal attribution_node2; |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 94 | attribution_node2.set_uid(2222); |
| 95 | attribution_node2.set_tag("location2"); |
| 96 | |
Yao Chen | 9c1debe | 2018-02-19 14:39:19 -0800 | [diff] [blame] | 97 | AttributionNodeInternal attribution_node3; |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 98 | attribution_node3.set_uid(3333); |
| 99 | attribution_node3.set_tag("location3"); |
Yao Chen | 9c1debe | 2018-02-19 14:39:19 -0800 | [diff] [blame] | 100 | std::vector<AttributionNodeInternal> attribution_nodes = {attribution_node1, attribution_node2, |
| 101 | attribution_node3}; |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 102 | |
| 103 | // Set up the event |
| 104 | LogEvent event(10, 12345); |
| 105 | event.write(attribution_nodes); |
| 106 | event.write("some value"); |
| 107 | // Convert to a LogEvent |
| 108 | event.init(); |
Yangster-mac | e06cfd7 | 2018-03-10 23:22:59 -0800 | [diff] [blame^] | 109 | HashableDimensionKey output; |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 110 | |
| 111 | filterValues(matchers, event.getValues(), &output); |
| 112 | |
Yangster-mac | e06cfd7 | 2018-03-10 23:22:59 -0800 | [diff] [blame^] | 113 | EXPECT_EQ((size_t)7, output.getValues().size()); |
| 114 | EXPECT_EQ((int32_t)0x02010101, output.getValues()[0].mField.getField()); |
| 115 | EXPECT_EQ((int32_t)1111, output.getValues()[0].mValue.int_value); |
| 116 | EXPECT_EQ((int32_t)0x02010102, output.getValues()[1].mField.getField()); |
| 117 | EXPECT_EQ("location1", output.getValues()[1].mValue.str_value); |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 118 | |
Yangster-mac | e06cfd7 | 2018-03-10 23:22:59 -0800 | [diff] [blame^] | 119 | EXPECT_EQ((int32_t)0x02010201, output.getValues()[2].mField.getField()); |
| 120 | EXPECT_EQ((int32_t)2222, output.getValues()[2].mValue.int_value); |
| 121 | EXPECT_EQ((int32_t)0x02010202, output.getValues()[3].mField.getField()); |
| 122 | EXPECT_EQ("location2", output.getValues()[3].mValue.str_value); |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 123 | |
Yangster-mac | e06cfd7 | 2018-03-10 23:22:59 -0800 | [diff] [blame^] | 124 | EXPECT_EQ((int32_t)0x02010301, output.getValues()[4].mField.getField()); |
| 125 | EXPECT_EQ((int32_t)3333, output.getValues()[4].mValue.int_value); |
| 126 | EXPECT_EQ((int32_t)0x02010302, output.getValues()[5].mField.getField()); |
| 127 | EXPECT_EQ("location3", output.getValues()[5].mValue.str_value); |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 128 | |
Yangster-mac | e06cfd7 | 2018-03-10 23:22:59 -0800 | [diff] [blame^] | 129 | EXPECT_EQ((int32_t)0x00020000, output.getValues()[6].mField.getField()); |
| 130 | EXPECT_EQ("some value", output.getValues()[6].mValue.str_value); |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | TEST(AtomMatcherTest, TestSubDimension) { |
| 134 | HashableDimensionKey dim; |
| 135 | |
| 136 | int pos1[] = {1, 1, 1}; |
| 137 | int pos2[] = {1, 1, 2}; |
| 138 | int pos3[] = {1, 1, 3}; |
| 139 | int pos4[] = {2, 0, 0}; |
| 140 | Field field1(10, pos1, 2); |
| 141 | Field field2(10, pos2, 2); |
| 142 | |
| 143 | Field field3(10, pos3, 2); |
| 144 | Field field4(10, pos4, 0); |
| 145 | |
| 146 | Value value1((int32_t)10025); |
| 147 | Value value2("tag"); |
| 148 | |
| 149 | Value value11((int32_t)10026); |
| 150 | Value value22("tag2"); |
| 151 | |
| 152 | dim.addValue(FieldValue(field1, value1)); |
| 153 | dim.addValue(FieldValue(field2, value2)); |
| 154 | |
| 155 | HashableDimensionKey subDim1; |
| 156 | subDim1.addValue(FieldValue(field1, value1)); |
| 157 | |
| 158 | HashableDimensionKey subDim2; |
| 159 | subDim1.addValue(FieldValue(field2, value2)); |
| 160 | |
| 161 | EXPECT_TRUE(dim.contains(dim)); |
| 162 | EXPECT_TRUE(dim.contains(subDim1)); |
| 163 | EXPECT_TRUE(dim.contains(subDim2)); |
| 164 | |
| 165 | HashableDimensionKey subDim3; |
| 166 | subDim3.addValue(FieldValue(field1, value11)); |
| 167 | EXPECT_FALSE(dim.contains(subDim3)); |
| 168 | |
| 169 | HashableDimensionKey subDim4; |
| 170 | // Empty dimension is always a sub dimension of other dimensions |
| 171 | EXPECT_TRUE(dim.contains(subDim4)); |
| 172 | } |
| 173 | |
| 174 | TEST(AtomMatcherTest, TestMetric2ConditionLink) { |
Yao Chen | 9c1debe | 2018-02-19 14:39:19 -0800 | [diff] [blame] | 175 | AttributionNodeInternal attribution_node1; |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 176 | attribution_node1.set_uid(1111); |
| 177 | attribution_node1.set_tag("location1"); |
| 178 | |
Yao Chen | 9c1debe | 2018-02-19 14:39:19 -0800 | [diff] [blame] | 179 | AttributionNodeInternal attribution_node2; |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 180 | attribution_node2.set_uid(2222); |
| 181 | attribution_node2.set_tag("location2"); |
| 182 | |
Yao Chen | 9c1debe | 2018-02-19 14:39:19 -0800 | [diff] [blame] | 183 | AttributionNodeInternal attribution_node3; |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 184 | attribution_node3.set_uid(3333); |
| 185 | attribution_node3.set_tag("location3"); |
Yao Chen | 9c1debe | 2018-02-19 14:39:19 -0800 | [diff] [blame] | 186 | std::vector<AttributionNodeInternal> attribution_nodes = {attribution_node1, attribution_node2, |
| 187 | attribution_node3}; |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 188 | |
| 189 | // Set up the event |
| 190 | LogEvent event(10, 12345); |
| 191 | event.write(attribution_nodes); |
| 192 | event.write("some value"); |
| 193 | // Convert to a LogEvent |
| 194 | event.init(); |
| 195 | |
| 196 | FieldMatcher whatMatcher; |
| 197 | whatMatcher.set_field(10); |
| 198 | FieldMatcher* child11 = whatMatcher.add_child(); |
| 199 | child11->set_field(1); |
| 200 | child11->set_position(Position::ANY); |
| 201 | child11 = child11->add_child(); |
| 202 | child11->set_field(1); |
| 203 | |
| 204 | FieldMatcher conditionMatcher; |
| 205 | conditionMatcher.set_field(27); |
| 206 | FieldMatcher* child2 = conditionMatcher.add_child(); |
| 207 | child2->set_field(2); |
| 208 | child2->set_position(Position::LAST); |
| 209 | |
| 210 | child2 = child2->add_child(); |
| 211 | child2->set_field(2); |
| 212 | |
| 213 | Metric2Condition link; |
| 214 | |
| 215 | translateFieldMatcher(whatMatcher, &link.metricFields); |
| 216 | translateFieldMatcher(conditionMatcher, &link.conditionFields); |
| 217 | |
| 218 | EXPECT_EQ((size_t)1, link.metricFields.size()); |
| 219 | EXPECT_EQ((int32_t)0x02010001, link.metricFields[0].mMatcher.getField()); |
| 220 | EXPECT_EQ((int32_t)0xff7f007f, link.metricFields[0].mMask); |
| 221 | EXPECT_EQ((int32_t)10, link.metricFields[0].mMatcher.getTag()); |
| 222 | |
| 223 | EXPECT_EQ((size_t)1, link.conditionFields.size()); |
| 224 | EXPECT_EQ((int32_t)0x02028002, link.conditionFields[0].mMatcher.getField()); |
| 225 | EXPECT_EQ((int32_t)0xff7f807f, link.conditionFields[0].mMask); |
| 226 | EXPECT_EQ((int32_t)27, link.conditionFields[0].mMatcher.getTag()); |
| 227 | } |
| 228 | |
| 229 | TEST(AtomMatcherTest, TestSubscriberDimensionWrite) { |
| 230 | HashableDimensionKey dim; |
| 231 | |
| 232 | int pos1[] = {1, 1, 1}; |
| 233 | int pos2[] = {1, 1, 2}; |
| 234 | int pos3[] = {1, 1, 3}; |
| 235 | int pos4[] = {2, 0, 0}; |
| 236 | |
| 237 | Field field1(10, pos1, 2); |
| 238 | Field field2(10, pos2, 2); |
| 239 | Field field3(10, pos3, 2); |
| 240 | Field field4(10, pos4, 0); |
| 241 | |
| 242 | Value value1((int32_t)10025); |
| 243 | Value value2("tag"); |
| 244 | Value value3((int32_t)987654); |
| 245 | Value value4((int32_t)99999); |
| 246 | |
| 247 | dim.addValue(FieldValue(field1, value1)); |
| 248 | dim.addValue(FieldValue(field2, value2)); |
| 249 | dim.addValue(FieldValue(field3, value3)); |
| 250 | dim.addValue(FieldValue(field4, value4)); |
| 251 | |
| 252 | SubscriberReporter::getStatsDimensionsValue(dim); |
| 253 | // TODO: can't test anything here because SubscriberReport class doesn't have any read api. |
| 254 | } |
| 255 | |
| 256 | TEST(AtomMatcherTest, TestWriteDimensionToProto) { |
| 257 | HashableDimensionKey dim; |
| 258 | int pos1[] = {1, 1, 1}; |
| 259 | int pos2[] = {1, 1, 2}; |
| 260 | int pos3[] = {1, 1, 3}; |
| 261 | int pos4[] = {2, 0, 0}; |
| 262 | Field field1(10, pos1, 2); |
| 263 | Field field2(10, pos2, 2); |
| 264 | Field field3(10, pos3, 2); |
| 265 | Field field4(10, pos4, 0); |
| 266 | |
| 267 | Value value1((int32_t)10025); |
| 268 | Value value2("tag"); |
| 269 | Value value3((int32_t)987654); |
| 270 | Value value4((int32_t)99999); |
| 271 | |
| 272 | dim.addValue(FieldValue(field1, value1)); |
| 273 | dim.addValue(FieldValue(field2, value2)); |
| 274 | dim.addValue(FieldValue(field3, value3)); |
| 275 | dim.addValue(FieldValue(field4, value4)); |
| 276 | |
| 277 | android::util::ProtoOutputStream protoOut; |
| 278 | writeDimensionToProto(dim, &protoOut); |
| 279 | |
| 280 | vector<uint8_t> outData; |
| 281 | outData.resize(protoOut.size()); |
| 282 | size_t pos = 0; |
| 283 | auto iter = protoOut.data(); |
| 284 | while (iter.readBuffer() != NULL) { |
| 285 | size_t toRead = iter.currentToRead(); |
| 286 | std::memcpy(&(outData[pos]), iter.readBuffer(), toRead); |
| 287 | pos += toRead; |
| 288 | iter.rp()->move(toRead); |
| 289 | } |
| 290 | |
| 291 | DimensionsValue result; |
| 292 | EXPECT_EQ(true, result.ParseFromArray(&outData[0], outData.size())); |
| 293 | EXPECT_EQ(10, result.field()); |
| 294 | EXPECT_EQ(DimensionsValue::ValueCase::kValueTuple, result.value_case()); |
| 295 | EXPECT_EQ(2, result.value_tuple().dimensions_value_size()); |
| 296 | |
| 297 | const auto& dim1 = result.value_tuple().dimensions_value(0); |
| 298 | EXPECT_EQ(DimensionsValue::ValueCase::kValueTuple, dim1.value_case()); |
| 299 | EXPECT_EQ(3, dim1.value_tuple().dimensions_value_size()); |
| 300 | |
| 301 | const auto& dim11 = dim1.value_tuple().dimensions_value(0); |
| 302 | EXPECT_EQ(DimensionsValue::ValueCase::kValueInt, dim11.value_case()); |
| 303 | EXPECT_EQ(10025, dim11.value_int()); |
| 304 | |
| 305 | const auto& dim12 = dim1.value_tuple().dimensions_value(1); |
| 306 | EXPECT_EQ(DimensionsValue::ValueCase::kValueStr, dim12.value_case()); |
| 307 | EXPECT_EQ("tag", dim12.value_str()); |
| 308 | |
| 309 | const auto& dim13 = dim1.value_tuple().dimensions_value(2); |
| 310 | EXPECT_EQ(DimensionsValue::ValueCase::kValueInt, dim13.value_case()); |
| 311 | EXPECT_EQ(987654, dim13.value_int()); |
| 312 | |
| 313 | const auto& dim2 = result.value_tuple().dimensions_value(1); |
| 314 | EXPECT_EQ(DimensionsValue::ValueCase::kValueInt, dim2.value_case()); |
| 315 | EXPECT_EQ(99999, dim2.value_int()); |
| 316 | } |
| 317 | |
| 318 | TEST(AtomMatcherTest, TestWriteAtomToProto) { |
Yao Chen | 9c1debe | 2018-02-19 14:39:19 -0800 | [diff] [blame] | 319 | AttributionNodeInternal attribution_node1; |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 320 | attribution_node1.set_uid(1111); |
| 321 | attribution_node1.set_tag("location1"); |
| 322 | |
Yao Chen | 9c1debe | 2018-02-19 14:39:19 -0800 | [diff] [blame] | 323 | AttributionNodeInternal attribution_node2; |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 324 | attribution_node2.set_uid(2222); |
| 325 | attribution_node2.set_tag("location2"); |
| 326 | |
Yao Chen | 9c1debe | 2018-02-19 14:39:19 -0800 | [diff] [blame] | 327 | std::vector<AttributionNodeInternal> attribution_nodes = {attribution_node1, attribution_node2}; |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 328 | |
| 329 | // Set up the event |
| 330 | LogEvent event(4, 12345); |
| 331 | event.write(attribution_nodes); |
| 332 | event.write((int32_t)999); |
| 333 | // Convert to a LogEvent |
| 334 | event.init(); |
| 335 | |
| 336 | android::util::ProtoOutputStream protoOutput; |
| 337 | writeFieldValueTreeToStream(event.GetTagId(), event.getValues(), &protoOutput); |
| 338 | |
| 339 | vector<uint8_t> outData; |
| 340 | outData.resize(protoOutput.size()); |
| 341 | size_t pos = 0; |
| 342 | auto iter = protoOutput.data(); |
| 343 | while (iter.readBuffer() != NULL) { |
| 344 | size_t toRead = iter.currentToRead(); |
| 345 | std::memcpy(&(outData[pos]), iter.readBuffer(), toRead); |
| 346 | pos += toRead; |
| 347 | iter.rp()->move(toRead); |
| 348 | } |
| 349 | |
| 350 | Atom result; |
| 351 | EXPECT_EQ(true, result.ParseFromArray(&outData[0], outData.size())); |
| 352 | EXPECT_EQ(Atom::PushedCase::kBleScanResultReceived, result.pushed_case()); |
| 353 | const auto& atom = result.ble_scan_result_received(); |
| 354 | EXPECT_EQ(2, atom.attribution_node_size()); |
| 355 | EXPECT_EQ(1111, atom.attribution_node(0).uid()); |
| 356 | EXPECT_EQ("location1", atom.attribution_node(0).tag()); |
| 357 | EXPECT_EQ(2222, atom.attribution_node(1).uid()); |
| 358 | EXPECT_EQ("location2", atom.attribution_node(1).tag()); |
| 359 | EXPECT_EQ(999, atom.num_of_results()); |
| 360 | } |
| 361 | |
| 362 | |
| 363 | } // namespace statsd |
| 364 | } // namespace os |
| 365 | } // namespace android |
| 366 | #else |
| 367 | GTEST_LOG_(INFO) << "This test does nothing.\n"; |
| 368 | #endif |