1 // Copyright (C) 2018 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include <gtest/gtest.h>
16 
17 #include "src/StatsLogProcessor.h"
18 #include "src/stats_log_util.h"
19 #include "tests/statsd_test_util.h"
20 
21 #include <vector>
22 
23 namespace android {
24 namespace os {
25 namespace statsd {
26 
27 #ifdef __ANDROID__
28 
29 namespace {
30 
CreateStatsdConfig(int num_buckets,int threshold)31 StatsdConfig CreateStatsdConfig(int num_buckets, int threshold) {
32     StatsdConfig config;
33     auto wakelockAcquireMatcher = CreateAcquireWakelockAtomMatcher();
34 
35     *config.add_atom_matcher() = wakelockAcquireMatcher;
36 
37     auto countMetric = config.add_count_metric();
38     countMetric->set_id(123456);
39     countMetric->set_what(wakelockAcquireMatcher.id());
40     *countMetric->mutable_dimensions_in_what() = CreateAttributionUidDimensions(
41             util::WAKELOCK_STATE_CHANGED, {Position::FIRST});
42     countMetric->set_bucket(FIVE_MINUTES);
43 
44     auto alert = config.add_alert();
45     alert->set_id(StringToId("alert"));
46     alert->set_metric_id(123456);
47     alert->set_num_buckets(num_buckets);
48     alert->set_refractory_period_secs(10);
49     alert->set_trigger_if_sum_gt(threshold);
50 
51     // Two hours
52     config.set_ttl_in_seconds(2 * 3600);
53     return config;
54 }
55 
56 }  // namespace
57 
TEST(ConfigTtlE2eTest,TestCountMetric)58 TEST(ConfigTtlE2eTest, TestCountMetric) {
59     const int num_buckets = 1;
60     const int threshold = 3;
61     auto config = CreateStatsdConfig(num_buckets, threshold);
62     const uint64_t alert_id = config.alert(0).id();
63     const uint32_t refractory_period_sec = config.alert(0).refractory_period_secs();
64 
65     int64_t bucketStartTimeNs = 10000000000;
66     int64_t bucketSizeNs = TimeUnitToBucketSizeInMillis(config.count_metric(0).bucket()) * 1000000;
67 
68     ConfigKey cfgKey;
69     auto processor = CreateStatsLogProcessor(bucketStartTimeNs, bucketStartTimeNs, config, cfgKey);
70     ASSERT_EQ(processor->mMetricsManagers.size(), 1u);
71     EXPECT_TRUE(processor->mMetricsManagers.begin()->second->isConfigValid());
72 
73     std::vector<int> attributionUids1 = {111};
74     std::vector<string> attributionTags1 = {"App1"};
75 
76     FieldValue fieldValue1(Field(util::WAKELOCK_STATE_CHANGED, (int32_t)0x02010101),
77                            Value((int32_t)111));
78     HashableDimensionKey whatKey1({fieldValue1});
79     MetricDimensionKey dimensionKey1(whatKey1, DEFAULT_DIMENSION_KEY);
80 
81     FieldValue fieldValue2(Field(util::WAKELOCK_STATE_CHANGED, (int32_t)0x02010101),
82                            Value((int32_t)222));
83     HashableDimensionKey whatKey2({fieldValue2});
84     MetricDimensionKey dimensionKey2(whatKey2, DEFAULT_DIMENSION_KEY);
85 
86     auto event = CreateAcquireWakelockEvent(bucketStartTimeNs + 2, attributionUids1,
87                                             attributionTags1, "wl1");
88     processor->OnLogEvent(event.get());
89 
90     event = CreateAcquireWakelockEvent(bucketStartTimeNs + bucketSizeNs + 2, attributionUids1,
91                                        attributionTags1, "wl2");
92     processor->OnLogEvent(event.get());
93 
94     event = CreateAcquireWakelockEvent(bucketStartTimeNs + 25 * bucketSizeNs + 2, attributionUids1,
95                                        attributionTags1, "wl1");
96     processor->OnLogEvent(event.get());
97 
98     EXPECT_EQ((int64_t)(bucketStartTimeNs + 25 * bucketSizeNs + 2 + 2 * 3600 * NS_PER_SEC),
99               processor->mMetricsManagers.begin()->second->getTtlEndNs());
100 
101     // Clear the data stored on disk as a result of the ttl.
102     vector<uint8_t> buffer;
103     processor->onDumpReport(cfgKey, bucketStartTimeNs + 25 * bucketSizeNs + 3, false, true,
104                             ADB_DUMP, FAST, &buffer);
105 }
106 
107 #else
108 GTEST_LOG_(INFO) << "This test does nothing.\n";
109 #endif
110 
111 }  // namespace statsd
112 }  // namespace os
113 }  // namespace android
114