(Part 3) Use new socket schema with statsd tests
Update last set of statsd tests to use new socket schema
Test: bit statsd_test:*
Bug: 149590301
Change-Id: I0fe2c219ad75813db54ff0cfbad50f55e29cb626
diff --git a/cmds/statsd/tests/FieldValue_test.cpp b/cmds/statsd/tests/FieldValue_test.cpp
index a5ff067..0bf24f1 100644
--- a/cmds/statsd/tests/FieldValue_test.cpp
+++ b/cmds/statsd/tests/FieldValue_test.cpp
@@ -14,13 +14,16 @@
* limitations under the License.
*/
#include <gtest/gtest.h>
+
#include "frameworks/base/cmds/statsd/src/stats_log.pb.h"
#include "frameworks/base/cmds/statsd/src/statsd_config.pb.h"
#include "matchers/matcher_util.h"
#include "src/logd/LogEvent.h"
+#include "stats_event.h"
#include "stats_log_util.h"
#include "stats_util.h"
#include "subscriber/SubscriberReporter.h"
+#include "tests/statsd_test_util.h"
#ifdef __ANDROID__
@@ -30,6 +33,58 @@
namespace os {
namespace statsd {
+namespace {
+void makeLogEvent(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);
+
+ vector<const char*> cTags(attributionTags.size());
+ for (int i = 0; i < cTags.size(); i++) {
+ cTags[i] = attributionTags[i].c_str();
+ }
+
+ AStatsEvent_writeAttributionChain(statsEvent,
+ reinterpret_cast<const uint32_t*>(attributionUids.data()),
+ cTags.data(), attributionUids.size());
+ AStatsEvent_writeString(statsEvent, name.c_str());
+ AStatsEvent_build(statsEvent);
+
+ size_t size;
+ uint8_t* buf = AStatsEvent_getBuffer(statsEvent, &size);
+ logEvent->parseBuffer(buf, size);
+
+ AStatsEvent_release(statsEvent);
+}
+
+void makeLogEvent(LogEvent* logEvent, const int32_t atomId, const int64_t timestamp,
+ const vector<int>& attributionUids, const vector<string>& attributionTags,
+ const int32_t value) {
+ AStatsEvent* statsEvent = AStatsEvent_obtain();
+ AStatsEvent_setAtomId(statsEvent, atomId);
+ AStatsEvent_overwriteTimestamp(statsEvent, timestamp);
+
+ vector<const char*> cTags(attributionTags.size());
+ for (int i = 0; i < cTags.size(); i++) {
+ cTags[i] = attributionTags[i].c_str();
+ }
+
+ AStatsEvent_writeAttributionChain(statsEvent,
+ reinterpret_cast<const uint32_t*>(attributionUids.data()),
+ cTags.data(), attributionUids.size());
+ AStatsEvent_writeInt32(statsEvent, value);
+ AStatsEvent_build(statsEvent);
+
+ size_t size;
+ uint8_t* buf = AStatsEvent_getBuffer(statsEvent, &size);
+ logEvent->parseBuffer(buf, size);
+
+ AStatsEvent_release(statsEvent);
+}
+} // anonymous namespace
+
TEST(AtomMatcherTest, TestFieldTranslation) {
FieldMatcher matcher1;
matcher1.set_field(10);
@@ -72,66 +127,50 @@
EXPECT_EQ((int32_t)0xff7f7f7f, matcher12.mMask);
}
-// TODO(b/149590301): Update this test to use new socket schema.
-//TEST(AtomMatcherTest, TestFilter_ALL) {
-// FieldMatcher matcher1;
-// matcher1.set_field(10);
-// FieldMatcher* child = matcher1.add_child();
-// child->set_field(1);
-// child->set_position(Position::ALL);
-//
-// child->add_child()->set_field(1);
-// child->add_child()->set_field(2);
-//
-// child = matcher1.add_child();
-// child->set_field(2);
-//
-// vector<Matcher> matchers;
-// translateFieldMatcher(matcher1, &matchers);
-//
-// 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");
-//
-// 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(10, 12345);
-// event.write(attribution_nodes);
-// event.write("some value");
-// // Convert to a LogEvent
-// event.init();
-// HashableDimensionKey output;
-//
-// filterValues(matchers, event.getValues(), &output);
-//
-// EXPECT_EQ((size_t)7, output.getValues().size());
-// EXPECT_EQ((int32_t)0x02010101, output.getValues()[0].mField.getField());
-// EXPECT_EQ((int32_t)1111, output.getValues()[0].mValue.int_value);
-// EXPECT_EQ((int32_t)0x02010102, output.getValues()[1].mField.getField());
-// EXPECT_EQ("location1", output.getValues()[1].mValue.str_value);
-//
-// EXPECT_EQ((int32_t)0x02010201, output.getValues()[2].mField.getField());
-// EXPECT_EQ((int32_t)2222, output.getValues()[2].mValue.int_value);
-// EXPECT_EQ((int32_t)0x02010202, output.getValues()[3].mField.getField());
-// EXPECT_EQ("location2", output.getValues()[3].mValue.str_value);
-//
-// EXPECT_EQ((int32_t)0x02010301, output.getValues()[4].mField.getField());
-// EXPECT_EQ((int32_t)3333, output.getValues()[4].mValue.int_value);
-// EXPECT_EQ((int32_t)0x02010302, output.getValues()[5].mField.getField());
-// EXPECT_EQ("location3", output.getValues()[5].mValue.str_value);
-//
-// EXPECT_EQ((int32_t)0x00020000, output.getValues()[6].mField.getField());
-// EXPECT_EQ("some value", output.getValues()[6].mValue.str_value);
-//}
+TEST(AtomMatcherTest, TestFilter_ALL) {
+ FieldMatcher matcher1;
+ matcher1.set_field(10);
+ FieldMatcher* child = matcher1.add_child();
+ child->set_field(1);
+ child->set_position(Position::ALL);
+
+ child->add_child()->set_field(1);
+ child->add_child()->set_field(2);
+
+ child = matcher1.add_child();
+ child->set_field(2);
+
+ vector<Matcher> matchers;
+ translateFieldMatcher(matcher1, &matchers);
+
+ std::vector<int> attributionUids = {1111, 2222, 3333};
+ std::vector<string> attributionTags = {"location1", "location2", "location3"};
+
+ LogEvent event(/*uid=*/0, /*pid=*/0);
+ makeLogEvent(&event, 10 /*atomId*/, 1012345, attributionUids, attributionTags, "some value");
+ HashableDimensionKey output;
+
+ filterValues(matchers, event.getValues(), &output);
+
+ EXPECT_EQ((size_t)7, output.getValues().size());
+ EXPECT_EQ((int32_t)0x02010101, output.getValues()[0].mField.getField());
+ EXPECT_EQ((int32_t)1111, output.getValues()[0].mValue.int_value);
+ EXPECT_EQ((int32_t)0x02010102, output.getValues()[1].mField.getField());
+ EXPECT_EQ("location1", output.getValues()[1].mValue.str_value);
+
+ EXPECT_EQ((int32_t)0x02010201, output.getValues()[2].mField.getField());
+ EXPECT_EQ((int32_t)2222, output.getValues()[2].mValue.int_value);
+ EXPECT_EQ((int32_t)0x02010202, output.getValues()[3].mField.getField());
+ EXPECT_EQ("location2", output.getValues()[3].mValue.str_value);
+
+ EXPECT_EQ((int32_t)0x02010301, output.getValues()[4].mField.getField());
+ EXPECT_EQ((int32_t)3333, output.getValues()[4].mValue.int_value);
+ EXPECT_EQ((int32_t)0x02010302, output.getValues()[5].mField.getField());
+ EXPECT_EQ("location3", output.getValues()[5].mValue.str_value);
+
+ EXPECT_EQ((int32_t)0x00020000, output.getValues()[6].mField.getField());
+ EXPECT_EQ("some value", output.getValues()[6].mValue.str_value);
+}
TEST(AtomMatcherTest, TestSubDimension) {
HashableDimensionKey dim;
@@ -174,61 +213,45 @@
EXPECT_TRUE(dim.contains(subDim4));
}
-// TODO(b/149590301): Update this test to use new socket schema.
-//TEST(AtomMatcherTest, TestMetric2ConditionLink) {
-// 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");
-//
-// 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(10, 12345);
-// event.write(attribution_nodes);
-// event.write("some value");
-// // Convert to a LogEvent
-// event.init();
-//
-// FieldMatcher whatMatcher;
-// whatMatcher.set_field(10);
-// FieldMatcher* child11 = whatMatcher.add_child();
-// child11->set_field(1);
-// child11->set_position(Position::ANY);
-// child11 = child11->add_child();
-// child11->set_field(1);
-//
-// FieldMatcher conditionMatcher;
-// conditionMatcher.set_field(27);
-// FieldMatcher* child2 = conditionMatcher.add_child();
-// child2->set_field(2);
-// child2->set_position(Position::LAST);
-//
-// child2 = child2->add_child();
-// child2->set_field(2);
-//
-// Metric2Condition link;
-//
-// translateFieldMatcher(whatMatcher, &link.metricFields);
-// translateFieldMatcher(conditionMatcher, &link.conditionFields);
-//
-// EXPECT_EQ((size_t)1, link.metricFields.size());
-// EXPECT_EQ((int32_t)0x02010001, link.metricFields[0].mMatcher.getField());
-// EXPECT_EQ((int32_t)0xff7f007f, link.metricFields[0].mMask);
-// EXPECT_EQ((int32_t)10, link.metricFields[0].mMatcher.getTag());
-//
-// EXPECT_EQ((size_t)1, link.conditionFields.size());
-// EXPECT_EQ((int32_t)0x02028002, link.conditionFields[0].mMatcher.getField());
-// EXPECT_EQ((int32_t)0xff7f807f, link.conditionFields[0].mMask);
-// EXPECT_EQ((int32_t)27, link.conditionFields[0].mMatcher.getTag());
-//}
+TEST(AtomMatcherTest, TestMetric2ConditionLink) {
+ std::vector<int> attributionUids = {1111, 2222, 3333};
+ std::vector<string> attributionTags = {"location1", "location2", "location3"};
+
+ LogEvent event(/*uid=*/0, /*pid=*/0);
+ makeLogEvent(&event, 10 /*atomId*/, 12345, attributionUids, attributionTags, "some value");
+
+ FieldMatcher whatMatcher;
+ whatMatcher.set_field(10);
+ FieldMatcher* child11 = whatMatcher.add_child();
+ child11->set_field(1);
+ child11->set_position(Position::ANY);
+ child11 = child11->add_child();
+ child11->set_field(1);
+
+ FieldMatcher conditionMatcher;
+ conditionMatcher.set_field(27);
+ FieldMatcher* child2 = conditionMatcher.add_child();
+ child2->set_field(2);
+ child2->set_position(Position::LAST);
+
+ child2 = child2->add_child();
+ child2->set_field(2);
+
+ Metric2Condition link;
+
+ translateFieldMatcher(whatMatcher, &link.metricFields);
+ translateFieldMatcher(conditionMatcher, &link.conditionFields);
+
+ EXPECT_EQ((size_t)1, link.metricFields.size());
+ EXPECT_EQ((int32_t)0x02010001, link.metricFields[0].mMatcher.getField());
+ EXPECT_EQ((int32_t)0xff7f007f, link.metricFields[0].mMask);
+ EXPECT_EQ((int32_t)10, link.metricFields[0].mMatcher.getTag());
+
+ EXPECT_EQ((size_t)1, link.conditionFields.size());
+ EXPECT_EQ((int32_t)0x02028002, link.conditionFields[0].mMatcher.getField());
+ EXPECT_EQ((int32_t)0xff7f807f, link.conditionFields[0].mMask);
+ EXPECT_EQ((int32_t)27, link.conditionFields[0].mMatcher.getTag());
+}
TEST(AtomMatcherTest, TestWriteDimensionPath) {
for (auto position : {Position::ANY, Position::ALL, Position::FIRST, Position::LAST}) {
@@ -439,50 +462,38 @@
EXPECT_EQ(99999, dim4.value_long());
}
-// TODO(b/149590301): Update this test to use new socket schema.
-//TEST(AtomMatcherTest, TestWriteAtomToProto) {
-// 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<AttributionNodeInternal> attribution_nodes = {attribution_node1, attribution_node2};
-//
-// // Set up the event
-// LogEvent event(4, 12345);
-// event.write(attribution_nodes);
-// event.write((int32_t)999);
-// // Convert to a LogEvent
-// event.init();
-//
-// android::util::ProtoOutputStream protoOutput;
-// writeFieldValueTreeToStream(event.GetTagId(), event.getValues(), &protoOutput);
-//
-// vector<uint8_t> outData;
-// outData.resize(protoOutput.size());
-// size_t pos = 0;
-// sp<ProtoReader> reader = protoOutput.data();
-// while (reader->readBuffer() != NULL) {
-// size_t toRead = reader->currentToRead();
-// std::memcpy(&(outData[pos]), reader->readBuffer(), toRead);
-// pos += toRead;
-// reader->move(toRead);
-// }
-//
-// Atom result;
-// EXPECT_EQ(true, result.ParseFromArray(&outData[0], outData.size()));
-// EXPECT_EQ(Atom::PushedCase::kBleScanResultReceived, result.pushed_case());
-// const auto& atom = result.ble_scan_result_received();
-// EXPECT_EQ(2, atom.attribution_node_size());
-// EXPECT_EQ(1111, atom.attribution_node(0).uid());
-// EXPECT_EQ("location1", atom.attribution_node(0).tag());
-// EXPECT_EQ(2222, atom.attribution_node(1).uid());
-// EXPECT_EQ("location2", atom.attribution_node(1).tag());
-// EXPECT_EQ(999, atom.num_results());
-//}
+TEST(AtomMatcherTest, TestWriteAtomToProto) {
+ std::vector<int> attributionUids = {1111, 2222};
+ std::vector<string> attributionTags = {"location1", "location2"};
+
+ LogEvent event(/*uid=*/0, /*pid=*/0);
+ makeLogEvent(&event, 4 /*atomId*/, 12345, attributionUids, attributionTags, 999);
+
+ android::util::ProtoOutputStream protoOutput;
+ writeFieldValueTreeToStream(event.GetTagId(), event.getValues(), &protoOutput);
+
+ vector<uint8_t> outData;
+ outData.resize(protoOutput.size());
+ size_t pos = 0;
+ sp<ProtoReader> reader = protoOutput.data();
+ while (reader->readBuffer() != NULL) {
+ size_t toRead = reader->currentToRead();
+ std::memcpy(&(outData[pos]), reader->readBuffer(), toRead);
+ pos += toRead;
+ reader->move(toRead);
+ }
+
+ Atom result;
+ EXPECT_EQ(true, result.ParseFromArray(&outData[0], outData.size()));
+ EXPECT_EQ(Atom::PushedCase::kBleScanResultReceived, result.pushed_case());
+ const auto& atom = result.ble_scan_result_received();
+ EXPECT_EQ(2, atom.attribution_node_size());
+ EXPECT_EQ(1111, atom.attribution_node(0).uid());
+ EXPECT_EQ("location1", atom.attribution_node(0).tag());
+ EXPECT_EQ(2222, atom.attribution_node(1).uid());
+ EXPECT_EQ("location2", atom.attribution_node(1).tag());
+ EXPECT_EQ(999, atom.num_results());
+}
/*
* Test two Matchers is not a subset of one Matcher.