summaryrefslogtreecommitdiff
path: root/lib/libstatspull/tests/stats_subscription_test.cpp
blob: 4455e329a79eb4bde03f38b3e0d4c60173b64dfc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/*
 * Copyright (C) 2023, The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <binder/ProcessState.h>
#include <gmock/gmock.h>
#include <google/protobuf/util/message_differencer.h>
#include <gtest/gtest.h>
#include <stats_subscription.h>
#include <stdint.h>
#include <utils/Looper.h>

#include <chrono>
#include <string>
#include <thread>
#include <vector>

#include "packages/modules/StatsD/statsd/src/shell/shell_config.pb.h"
#include "packages/modules/StatsD/statsd/src/shell/shell_data.pb.h"
#include "statslog_statsdtest.h"

#ifdef __ANDROID__

using namespace testing;
using android::Looper;
using android::ProcessState;
using android::sp;
using android::os::statsd::Atom;
using android::os::statsd::ShellData;
using android::os::statsd::ShellSubscription;
using android::os::statsd::TestAtomReported_State_OFF;
using android::os::statsd::TrainExperimentIds;
using android::os::statsd::util::BytesField;
using android::os::statsd::util::SCREEN_BRIGHTNESS_CHANGED;
using android::os::statsd::util::stats_write;
using android::os::statsd::util::TEST_ATOM_REPORTED;
using android::os::statsd::util::TEST_ATOM_REPORTED__REPEATED_ENUM_FIELD__OFF;
using google::protobuf::util::MessageDifferencer;
using std::string;
using std::vector;
using std::this_thread::sleep_for;

namespace {

// Checks equality on explicitly set values.
MATCHER_P(ProtoEq, otherArg, "") {
    return MessageDifferencer::Equals(arg, otherArg);
}

class SubscriptionTest : public Test {
public:
    SubscriptionTest() : looper(Looper::prepare(/*opts=*/0)) {
        const TestInfo* const test_info = UnitTest::GetInstance()->current_test_info();
        ALOGD("**** Setting up for %s.%s\n", test_info->test_case_name(), test_info->name());

        *trainExpIds.mutable_experiment_id() = {expIds.begin(), expIds.end()};
        trainExpIds.SerializeToString(&trainExpIdsBytes);
    }

    ~SubscriptionTest() {
        const TestInfo* const test_info = UnitTest::GetInstance()->current_test_info();
        ALOGD("**** Tearing down after %s.%s\n", test_info->test_case_name(), test_info->name());
    }

protected:
    void SetUp() override {
        // Start the Binder thread pool.
        ProcessState::self()->startThreadPool();
    }

    void TearDown() {
        // Clear any dangling subscriptions from statsd.
        if (__builtin_available(android __STATSD_SUBS_MIN_API__, *)) {
            AStatsManager_removeSubscription(subId);
        }
    }

    void LogTestAtomReported(int32_t intFieldValue) {
        const BytesField bytesField(trainExpIdsBytes.data(), trainExpIdsBytes.size());
        stats_write(TEST_ATOM_REPORTED, uids.data(), uids.size(), tags, intFieldValue,
                    /*long_field=*/2LL, /*float_field=*/3.0F,
                    /*string_field=*/string1.c_str(),
                    /*boolean_field=*/false, /*state=*/TEST_ATOM_REPORTED__REPEATED_ENUM_FIELD__OFF,
                    bytesField, repeatedInts, repeatedLongs, repeatedFloats, repeatedStrings,
                    &(repeatedBool[0]), /*repeatedBoolSize=*/2, repeatedEnums);
    }

    int32_t subId;

    // TestAtomReported fields.
    const vector<int32_t> uids = {1};
    const string tag = "test";
    const vector<char const*> tags = {tag.c_str()};

    // 100 int64s for the MODE_BYTES field to push atom size to over 1K.
    const vector<int64_t> expIds = vector<int64_t>(100, INT64_MAX);

    const vector<int32_t> repeatedInts{1};
    const vector<int64_t> repeatedLongs{2LL};
    const vector<float> repeatedFloats{3.0F};
    const string string1 = "ABC";
    const vector<char const*> repeatedStrings = {string1.c_str()};
    const bool repeatedBool[2] = {false, true};
    const vector<int32_t> repeatedEnums = {TEST_ATOM_REPORTED__REPEATED_ENUM_FIELD__OFF};
    TrainExperimentIds trainExpIds;
    string trainExpIdsBytes;

private:
    sp<Looper> looper;
};

// Stores arguments passed in subscription callback.
struct CallbackData {
    int32_t subId;
    AStatsManager_SubscriptionCallbackReason reason;
    vector<uint8_t> payload;
    int count;  // Stores number of times the callback is invoked.
};

static void callback(int32_t subscription_id, AStatsManager_SubscriptionCallbackReason reason,
                     uint8_t* _Nonnull payload, size_t num_bytes, void* _Nullable cookie) {
    CallbackData* data = static_cast<CallbackData*>(cookie);
    data->subId = subscription_id;
    data->reason = reason;
    data->payload.assign(payload, payload + num_bytes);
    data->count++;
}

constexpr static int WAIT_MS = 500;

TEST_F(SubscriptionTest, TestSubscription) {
    if (__builtin_available(android __STATSD_SUBS_MIN_API__, *)) {
        ShellSubscription config;
        config.add_pushed()->set_atom_id(TEST_ATOM_REPORTED);
        config.add_pushed()->set_atom_id(SCREEN_BRIGHTNESS_CHANGED);

        string configBytes;
        config.SerializeToString(&configBytes);

        CallbackData callbackData{/*subId=*/0,
                                  ASTATSMANAGER_SUBSCRIPTION_CALLBACK_REASON_SUBSCRIPTION_ENDED,
                                  /*payload=*/{},
                                  /*count=*/0};

        // Add subscription.
        subId = AStatsManager_addSubscription(reinterpret_cast<const uint8_t*>(configBytes.data()),
                                              configBytes.size(), &callback, &callbackData);
        ASSERT_GT(subId, 0);
        sleep_for(std::chrono::milliseconds(WAIT_MS));

        // Log events without exceeding statsd cache.
        stats_write(SCREEN_BRIGHTNESS_CHANGED, 100);
        LogTestAtomReported(1);
        sleep_for(std::chrono::milliseconds(WAIT_MS));

        // Verify no callback occurred yet.
        EXPECT_EQ(callbackData.subId, 0);
        EXPECT_EQ(callbackData.reason,
                  ASTATSMANAGER_SUBSCRIPTION_CALLBACK_REASON_SUBSCRIPTION_ENDED);
        EXPECT_EQ(callbackData.count, 0);
        ASSERT_TRUE(callbackData.payload.empty());

        // Log another TestAtomReported to overflow cache.
        LogTestAtomReported(2);
        sleep_for(std::chrono::milliseconds(WAIT_MS));

        // Verify callback occurred.
        EXPECT_EQ(callbackData.subId, subId);
        EXPECT_EQ(callbackData.reason, ASTATSMANAGER_SUBSCRIPTION_CALLBACK_REASON_STATSD_INITIATED);
        EXPECT_EQ(callbackData.count, 1);
        ASSERT_GT(callbackData.payload.size(), 0);

        ShellData actualShellData;
        ASSERT_TRUE(actualShellData.ParseFromArray(callbackData.payload.data(),
                                                   callbackData.payload.size()));

        ASSERT_EQ(actualShellData.elapsed_timestamp_nanos_size(), 3);
        EXPECT_THAT(actualShellData.elapsed_timestamp_nanos(), Each(Gt(0LL)));

        ASSERT_EQ(actualShellData.atom_size(), 3);

        // Verify atom 1.
        Atom expectedAtom;
        expectedAtom.mutable_screen_brightness_changed()->set_level(100);
        EXPECT_THAT(actualShellData.atom(0), ProtoEq(expectedAtom));

        // Verify atom 2.
        expectedAtom.Clear();
        auto* testAtomReported = expectedAtom.mutable_test_atom_reported();
        auto* attributionNode = testAtomReported->add_attribution_node();
        attributionNode->set_uid(uids[0]);
        attributionNode->set_tag(tag);
        testAtomReported->set_int_field(1);
        testAtomReported->set_long_field(2LL);
        testAtomReported->set_float_field(3.0F);
        testAtomReported->set_string_field(string1);
        testAtomReported->set_boolean_field(false);
        testAtomReported->set_state(TestAtomReported_State_OFF);
        *testAtomReported->mutable_bytes_field() = trainExpIds;
        *testAtomReported->mutable_repeated_int_field() = {repeatedInts.begin(),
                                                           repeatedInts.end()};
        *testAtomReported->mutable_repeated_long_field() = {repeatedLongs.begin(),
                                                            repeatedLongs.end()};
        *testAtomReported->mutable_repeated_float_field() = {repeatedFloats.begin(),
                                                             repeatedFloats.end()};
        *testAtomReported->mutable_repeated_string_field() = {repeatedStrings.begin(),
                                                              repeatedStrings.end()};
        *testAtomReported->mutable_repeated_boolean_field() = {&repeatedBool[0],
                                                               &repeatedBool[0] + 2};
        *testAtomReported->mutable_repeated_enum_field() = {repeatedEnums.begin(),
                                                            repeatedEnums.end()};
        EXPECT_THAT(actualShellData.atom(1), ProtoEq(expectedAtom));

        // Verify atom 3.
        testAtomReported->set_int_field(2);
        EXPECT_THAT(actualShellData.atom(2), ProtoEq(expectedAtom));

        // Log another ScreenBrightnessChanged atom. No callback should occur.
        stats_write(SCREEN_BRIGHTNESS_CHANGED, 99);
        sleep_for(std::chrono::milliseconds(WAIT_MS));
        EXPECT_EQ(callbackData.count, 1);

        // Flush subscription. Callback should occur.
        AStatsManager_flushSubscription(subId);
        sleep_for(std::chrono::milliseconds(WAIT_MS));

        EXPECT_EQ(callbackData.subId, subId);
        EXPECT_EQ(callbackData.reason, ASTATSMANAGER_SUBSCRIPTION_CALLBACK_REASON_FLUSH_REQUESTED);
        EXPECT_EQ(callbackData.count, 2);
        ASSERT_GT(callbackData.payload.size(), 0);

        ASSERT_TRUE(actualShellData.ParseFromArray(callbackData.payload.data(),
                                                   callbackData.payload.size()));

        ASSERT_EQ(actualShellData.elapsed_timestamp_nanos_size(), 1);
        EXPECT_THAT(actualShellData.elapsed_timestamp_nanos(), Each(Gt(0LL)));

        ASSERT_EQ(actualShellData.atom_size(), 1);

        // Verify atom 1.
        expectedAtom.Clear();
        expectedAtom.mutable_screen_brightness_changed()->set_level(99);
        EXPECT_THAT(actualShellData.atom(0), ProtoEq(expectedAtom));

        // Log another ScreenBrightnessChanged atom. No callback should occur.
        stats_write(SCREEN_BRIGHTNESS_CHANGED, 98);
        sleep_for(std::chrono::milliseconds(WAIT_MS));
        EXPECT_EQ(callbackData.count, 2);

        // Trigger callback through cache timeout.
        // Two 500 ms sleeps have occurred already so the total sleep is 5000 ms since last callback
        // invocation.
        sleep_for(std::chrono::milliseconds(4000));
        EXPECT_EQ(callbackData.subId, subId);
        EXPECT_EQ(callbackData.reason, ASTATSMANAGER_SUBSCRIPTION_CALLBACK_REASON_STATSD_INITIATED);
        EXPECT_EQ(callbackData.count, 3);
        ASSERT_GT(callbackData.payload.size(), 0);

        ASSERT_TRUE(actualShellData.ParseFromArray(callbackData.payload.data(),
                                                   callbackData.payload.size()));

        ASSERT_EQ(actualShellData.elapsed_timestamp_nanos_size(), 1);
        EXPECT_THAT(actualShellData.elapsed_timestamp_nanos(), Each(Gt(0LL)));

        ASSERT_EQ(actualShellData.atom_size(), 1);

        // Verify atom 1.
        expectedAtom.Clear();
        expectedAtom.mutable_screen_brightness_changed()->set_level(98);
        EXPECT_THAT(actualShellData.atom(0), ProtoEq(expectedAtom));

        // Log another ScreenBrightnessChanged atom. No callback should occur.
        stats_write(SCREEN_BRIGHTNESS_CHANGED, 97);
        sleep_for(std::chrono::milliseconds(WAIT_MS));
        EXPECT_EQ(callbackData.count, 3);

        // End subscription. Final callback should occur.
        AStatsManager_removeSubscription(subId);
        sleep_for(std::chrono::milliseconds(WAIT_MS));

        EXPECT_EQ(callbackData.subId, subId);
        EXPECT_EQ(callbackData.reason,
                  ASTATSMANAGER_SUBSCRIPTION_CALLBACK_REASON_SUBSCRIPTION_ENDED);
        EXPECT_EQ(callbackData.count, 4);
        ASSERT_GT(callbackData.payload.size(), 0);

        ASSERT_TRUE(actualShellData.ParseFromArray(callbackData.payload.data(),
                                                   callbackData.payload.size()));

        ASSERT_EQ(actualShellData.elapsed_timestamp_nanos_size(), 1);
        EXPECT_THAT(actualShellData.elapsed_timestamp_nanos(), Each(Gt(0LL)));

        ASSERT_EQ(actualShellData.atom_size(), 1);

        // Verify atom 1.
        expectedAtom.Clear();
        expectedAtom.mutable_screen_brightness_changed()->set_level(97);
        EXPECT_THAT(actualShellData.atom(0), ProtoEq(expectedAtom));
    } else {
        GTEST_SKIP();
    }
}

}  // namespace

#else
GTEST_LOG_(INFO) << "This test does nothing.\n";
#endif