1 /*
2  * Copyright (C) 2024 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <gtest/gtest.h>
20 #include <utils/RefBase.h>
21 
22 #include <cstdint>
23 #include <map>
24 #include <optional>
25 #include <set>
26 #include <unordered_map>
27 #include <vector>
28 
29 #include "src/anomaly/AlarmMonitor.h"
30 #include "src/anomaly/AlarmTracker.h"
31 #include "src/anomaly/AnomalyTracker.h"
32 #include "src/condition/ConditionTracker.h"
33 #include "src/config/ConfigMetadataProvider.h"
34 #include "src/external/StatsPullerManager.h"
35 #include "src/guardrail/StatsdStats.h"
36 #include "src/matchers/AtomMatchingTracker.h"
37 #include "src/metrics/MetricProducer.h"
38 #include "src/packages/UidMap.h"
39 #include "src/stats_util.h"
40 #include "src/statsd_config.pb.h"
41 
42 namespace android {
43 namespace os {
44 namespace statsd {
45 
46 constexpr int kConfigId = 12345;
47 const ConfigKey kConfigKey(0, kConfigId);
48 constexpr long timeBaseSec = 1000;
49 constexpr long kAlertId = 3;
50 
51 class InitConfigTest : public ::testing::Test {
52 protected:
53     InitConfigTest();
54 
55     void clearData();
56 
57     std::optional<InvalidConfigReason> initConfig(const StatsdConfig& config);
58 
59     void SetUp() override;
60 
61     sp<UidMap> uidMap;
62     sp<StatsPullerManager> pullerManager;
63     sp<AlarmMonitor> anomalyAlarmMonitor;
64     sp<AlarmMonitor> periodicAlarmMonitor;
65     sp<ConfigMetadataProvider> configMetadataProvider;
66     std::unordered_map<int, vector<int>> allTagIdsToMatchersMap;
67     std::vector<sp<AtomMatchingTracker>> allAtomMatchingTrackers;
68     std::unordered_map<int64_t, int> atomMatchingTrackerMap;
69     std::vector<sp<ConditionTracker>> allConditionTrackers;
70     std::unordered_map<int64_t, int> conditionTrackerMap;
71     std::vector<sp<MetricProducer>> allMetricProducers;
72     std::unordered_map<int64_t, int> metricProducerMap;
73     std::vector<sp<AnomalyTracker>> allAnomalyTrackers;
74     std::unordered_map<int64_t, int> alertTrackerMap;
75     std::vector<sp<AlarmTracker>> allAlarmTrackers;
76     std::unordered_map<int, std::vector<int>> conditionToMetricMap;
77     std::unordered_map<int, std::vector<int>> trackerToMetricMap;
78     std::unordered_map<int, std::vector<int>> trackerToConditionMap;
79     std::unordered_map<int, std::vector<int>> activationAtomTrackerToMetricMap;
80     std::unordered_map<int, std::vector<int>> deactivationAtomTrackerToMetricMap;
81     std::vector<int> metricsWithActivation;
82     std::map<int64_t, uint64_t> stateProtoHashes;
83     std::set<int64_t> noReportMetricIds;
84 };
85 
86 StatsdConfig createHistogramStatsdConfig();
87 
88 StatsdConfig createExplicitHistogramStatsdConfig(BinStarts bins);
89 
90 StatsdConfig createGeneratedHistogramStatsdConfig(
91         float binsMin, float binsMax, int binsCount,
92         HistogramBinConfig::GeneratedBins::Strategy binStrategy);
93 
94 }  // namespace statsd
95 }  // namespace os
96 }  // namespace android
97