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     config.add_allowed_log_source("AID_ROOT"); // LogEvent defaults to UID of root.
34     auto wakelockAcquireMatcher = CreateAcquireWakelockAtomMatcher();
35 
36     *config.add_atom_matcher() = wakelockAcquireMatcher;
37 
38     auto countMetric = config.add_count_metric();
39     countMetric->set_id(123456);
40     countMetric->set_what(wakelockAcquireMatcher.id());
41     *countMetric->mutable_dimensions_in_what() = CreateAttributionUidDimensions(
42             android::util::WAKELOCK_STATE_CHANGED, {Position::FIRST});
43     countMetric->set_bucket(FIVE_MINUTES);
44 
45     auto alert = config.add_alert();
46     alert->set_id(StringToId("alert"));
47     alert->set_metric_id(123456);
48     alert->set_num_buckets(num_buckets);
49     alert->set_refractory_period_secs(10);
50     alert->set_trigger_if_sum_gt(threshold);
51 
52     // Two hours
53     config.set_ttl_in_seconds(2 * 3600);
54     return config;
55 }
56 
57 }  // namespace
58 
TEST(ConfigTtlE2eTest,TestCountMetric)59 TEST(ConfigTtlE2eTest, TestCountMetric) {
60     const int num_buckets = 1;
61     const int threshold = 3;
62     auto config = CreateStatsdConfig(num_buckets, threshold);
63     const uint64_t alert_id = config.alert(0).id();
64     const uint32_t refractory_period_sec = config.alert(0).refractory_period_secs();
65 
66     int64_t bucketStartTimeNs = 10000000000;
67     int64_t bucketSizeNs =
68         TimeUnitToBucketSizeInMillis(config.count_metric(0).bucket()) * 1000000;
69 
70     ConfigKey cfgKey;
71     auto processor = CreateStatsLogProcessor(bucketStartTimeNs, bucketStartTimeNs, config, cfgKey);
72     EXPECT_EQ(processor->mMetricsManagers.size(), 1u);
73     EXPECT_TRUE(processor->mMetricsManagers.begin()->second->isConfigValid());
74 
75     std::vector<AttributionNodeInternal> attributions1 = {CreateAttribution(111, "App1")};
76 
77     FieldValue fieldValue1(Field(android::util::WAKELOCK_STATE_CHANGED, (int32_t)0x02010101),
78                            Value((int32_t)111));
79     HashableDimensionKey whatKey1({fieldValue1});
80     MetricDimensionKey dimensionKey1(whatKey1, DEFAULT_DIMENSION_KEY);
81 
82     FieldValue fieldValue2(Field(android::util::WAKELOCK_STATE_CHANGED, (int32_t)0x02010101),
83                            Value((int32_t)222));
84     HashableDimensionKey whatKey2({fieldValue2});
85     MetricDimensionKey dimensionKey2(whatKey2, DEFAULT_DIMENSION_KEY);
86 
87     auto event = CreateAcquireWakelockEvent(attributions1, "wl1", bucketStartTimeNs + 2);
88     processor->OnLogEvent(event.get());
89 
90     event = CreateAcquireWakelockEvent(attributions1, "wl2", bucketStartTimeNs + bucketSizeNs + 2);
91     processor->OnLogEvent(event.get());
92 
93     event = CreateAcquireWakelockEvent(
94         attributions1, "wl1", bucketStartTimeNs + 25 * bucketSizeNs + 2);
95     processor->OnLogEvent(event.get());
96 
97     EXPECT_EQ((int64_t)(bucketStartTimeNs + 25 * bucketSizeNs + 2 + 2 * 3600 * NS_PER_SEC),
98               processor->mMetricsManagers.begin()->second->getTtlEndNs());
99 }
100 
101 
102 #else
103 GTEST_LOG_(INFO) << "This test does nothing.\n";
104 #endif
105 
106 }  // namespace statsd
107 }  // namespace os
108 }  // namespace android
109