Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 4aa7af0a authored by Christine Tsai's avatar Christine Tsai Committed by Android (Google) Code Review
Browse files

Merge "(Part 3) Use new socket schema with statsd tests" into rvc-dev

parents 294823f0 a3d2ed8c
Loading
Loading
Loading
Loading
+3 −2
Original line number Diff line number Diff line
@@ -78,7 +78,8 @@ message Atom {
    // Pushed atoms start at 2.
    oneof pushed {
        // For StatsLog reasons, 1 is illegal and will not work. Must start at 2.
        BleScanStateChanged ble_scan_state_changed = 2 [(module) = "bluetooth"];
        BleScanStateChanged ble_scan_state_changed = 2
                [(module) = "bluetooth", (module) = "statsdtest"];
        ProcessStateChanged process_state_changed = 3 [(module) = "framework"];
        BleScanResultReceived ble_scan_result_received = 4 [(module) = "bluetooth"];
        SensorStateChanged sensor_state_changed =
@@ -434,7 +435,7 @@ message Atom {
        ProcessMemoryState process_memory_state = 10013 [(module) = "framework"];
        SystemElapsedRealtime system_elapsed_realtime = 10014 [(module) = "framework"];
        SystemUptime system_uptime = 10015 [(module) = "framework", (module) = "statsdtest"];
        CpuActiveTime cpu_active_time = 10016 [(module) = "framework"];
        CpuActiveTime cpu_active_time = 10016 [(module) = "framework", (module) = "statsdtest"];
        CpuClusterTime cpu_cluster_time = 10017 [(module) = "framework", (module) = "statsdtest"];
        DiskSpace disk_space = 10018 [deprecated=true, (module) = "statsdtest"];
        RemainingBatteryCapacity remaining_battery_capacity = 10019 [(module) = "framework"];
+170 −159
Original line number Diff line number Diff line
@@ -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 android {
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 @@ TEST(AtomMatcherTest, TestFieldTranslation_ALL) {
    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 @@ TEST(AtomMatcherTest, TestSubDimension) {
    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 @@ TEST(AtomMatcherTest, TestWriteDimensionLeafNodesToProto) {
    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.
+692 −646

File changed.

Preview size limit exceeded, changes collapsed.

+1414 −1410

File changed.

Preview size limit exceeded, changes collapsed.

+22 −29
Original line number Diff line number Diff line
@@ -39,35 +39,28 @@ using android::util::ProtoReader;
const string kApp1 = "app1.sharing.1";
const string kApp2 = "app2.sharing.1";

// TODO(b/149590301): Update this test to use new socket schema.
//TEST(UidMapTest, TestIsolatedUID) {
//    sp<UidMap> m = new UidMap();
//    sp<StatsPullerManager> pullerManager = new StatsPullerManager();
//    sp<AlarmMonitor> anomalyAlarmMonitor;
//    sp<AlarmMonitor> subscriberAlarmMonitor;
//    // Construct the processor with a dummy sendBroadcast function that does nothing.
//    StatsLogProcessor p(m, pullerManager, anomalyAlarmMonitor, subscriberAlarmMonitor, 0,
//                        [](const ConfigKey& key) { return true; },
//                        [](const int&, const vector<int64_t>&) {return true;});
//    LogEvent addEvent(util::ISOLATED_UID_CHANGED, 1);
//    addEvent.write(100);  // parent UID
//    addEvent.write(101);  // isolated UID
//    addEvent.write(1);    // Indicates creation.
//    addEvent.init();
//
//    EXPECT_EQ(101, m->getHostUidOrSelf(101));
//
//    p.OnLogEvent(&addEvent);
//    EXPECT_EQ(100, m->getHostUidOrSelf(101));
//
//    LogEvent removeEvent(util::ISOLATED_UID_CHANGED, 1);
//    removeEvent.write(100);  // parent UID
//    removeEvent.write(101);  // isolated UID
//    removeEvent.write(0);    // Indicates removal.
//    removeEvent.init();
//    p.OnLogEvent(&removeEvent);
//    EXPECT_EQ(101, m->getHostUidOrSelf(101));
//}
TEST(UidMapTest, TestIsolatedUID) {
    sp<UidMap> m = new UidMap();
    sp<StatsPullerManager> pullerManager = new StatsPullerManager();
    sp<AlarmMonitor> anomalyAlarmMonitor;
    sp<AlarmMonitor> subscriberAlarmMonitor;
    // Construct the processor with a dummy sendBroadcast function that does nothing.
    StatsLogProcessor p(
            m, pullerManager, anomalyAlarmMonitor, subscriberAlarmMonitor, 0,
            [](const ConfigKey& key) { return true; },
            [](const int&, const vector<int64_t>&) { return true; });

    std::unique_ptr<LogEvent> addEvent = CreateIsolatedUidChangedEvent(
            1 /*timestamp*/, 100 /*hostUid*/, 101 /*isolatedUid*/, 1 /*is_create*/);
    EXPECT_EQ(101, m->getHostUidOrSelf(101));
    p.OnLogEvent(addEvent.get());
    EXPECT_EQ(100, m->getHostUidOrSelf(101));

    std::unique_ptr<LogEvent> removeEvent = CreateIsolatedUidChangedEvent(
            1 /*timestamp*/, 100 /*hostUid*/, 101 /*isolatedUid*/, 0 /*is_create*/);
    p.OnLogEvent(removeEvent.get());
    EXPECT_EQ(101, m->getHostUidOrSelf(101));
}

TEST(UidMapTest, TestMatching) {
    UidMap m;
Loading