1 /*
2  * Copyright (C) 2021 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_prod.h>
20 #include <server_configurable_flags/get_flags.h>
21 
22 #include <functional>
23 #include <mutex>
24 #include <string>
25 #include <unordered_map>
26 #include <vector>
27 
28 #include "stats_util.h"
29 
30 namespace android {
31 namespace os {
32 namespace statsd {
33 
34 using GetServerFlagFunc =
35         std::function<std::string(const std::string&, const std::string&, const std::string&)>;
36 using IsAtLeastSFunc = std::function<bool()>;
37 
38 const std::string STATSD_NATIVE_NAMESPACE = "statsd_native";
39 const std::string STATSD_NATIVE_BOOT_NAMESPACE = "statsd_native_boot";
40 
41 const std::string FLAG_TRUE = "true";
42 const std::string FLAG_FALSE = "false";
43 const std::string FLAG_EMPTY = "";
44 
45 class FlagProvider {
46 public:
47     static FlagProvider& getInstance();
48 
49     std::string getFlagString(const std::string& flagName, const std::string& defaultValue) const;
50 
51     // Returns true IFF flagName has a value of "true".
52     bool getFlagBool(const std::string& flagName, const std::string& defaultValue) const;
53 
54     std::string getBootFlagString(const std::string& flagName,
55                                   const std::string& defaultValue) const;
56 
57     // Returns true IFF flagName has a value of "true".
58     bool getBootFlagBool(const std::string& flagName, const std::string& defaultValue) const;
59 
60     // Queries the boot flags. Should only be called once at boot.
61     void initBootFlags(const std::vector<std::string>& flags);
62 
63 private:
64     FlagProvider();
65 
66     // TODO(b/194347008): Remove the GetServerConfigurableFlag override.
67     void overrideFuncs(const IsAtLeastSFunc& isAtLeastSFunc = &isAtLeastS,
68                        const GetServerFlagFunc& getServerFlagFunc =
69                                &server_configurable_flags::GetServerConfigurableFlag);
70 
71     void overrideFuncsLocked(const IsAtLeastSFunc& isAtLeastSFunc = &isAtLeastS,
72                              const GetServerFlagFunc& getServerFlagFunc =
73                                      &server_configurable_flags::GetServerConfigurableFlag);
74 
resetOverrides()75     inline void resetOverrides() {
76         std::lock_guard<std::mutex> lock(mFlagsMutex);
77         overrideFuncsLocked();
78         mLocalFlags.clear();
79     }
80 
81     void overrideFlag(const std::string& flagName, const std::string& flagValue,
82                       const bool isBootFlag = false);
83 
84     std::string getFlagStringInternal(const std::string& flagName, const std::string& flagValue,
85                                       const bool isBootFlag) const;
86 
87     std::string getLocalFlagKey(const std::string& flagName, const bool isBootFlag = false) const;
88 
89     IsAtLeastSFunc mIsAtLeastSFunc;
90     GetServerFlagFunc mGetServerFlagFunc;
91 
92     // Flag values updated only at boot. Used to store boot flags.
93     std::unordered_map<std::string, std::string> mBootFlags;
94 
95     // Flag values to be locally overwritten. Only used in tests.
96     std::unordered_map<std::string, std::string> mLocalFlags;
97 
98     mutable std::mutex mFlagsMutex;
99 
100     friend class ConfigUpdateE2eTest;
101     friend class ConfigUpdateTest;
102     friend class EventMetricE2eTest;
103     friend class ValueMetricE2eTest;
104     friend class GaugeMetricE2ePulledTest;
105     friend class GaugeMetricE2ePushedTest;
106     friend class EventMetricProducerTest;
107     friend class FlagProviderTest_RMinus;
108     friend class FlagProviderTest_SPlus;
109     friend class FlagProviderTest_SPlus_RealValues;
110     friend class KllMetricE2eAbTest;
111     friend class MetricsManagerTest;
112     friend class StatsLogProcessorTest;
113     friend class StatsLogProcessorTestRestricted;
114     friend class RestrictedEventMetricProducerTest;
115     friend class RestrictedConfigE2ETest;
116     friend class RestrictedEventMetricE2eTest;
117     friend class LogEvent_FieldRestrictionTest;
118 
119     FRIEND_TEST(ConfigUpdateE2eTest, TestEventMetric);
120     FRIEND_TEST(ConfigUpdateE2eTest, TestGaugeMetric);
121     FRIEND_TEST(ConfigUpdateE2eTest, TestConfigUpdateRestrictedDelegateCleared);
122     FRIEND_TEST(EventMetricE2eTest, TestEventMetricDataAggregated);
123     FRIEND_TEST(EventMetricProducerTest, TestOneAtomTagAggregatedEvents);
124     FRIEND_TEST(EventMetricProducerTest, TestTwoAtomTagAggregatedEvents);
125     FRIEND_TEST(GaugeMetricE2ePulledTest, TestRandomSamplePulledEventsNoCondition);
126     FRIEND_TEST(FlagProviderTest_SPlus, TestGetFlagBoolServerFlagTrue);
127     FRIEND_TEST(FlagProviderTest_SPlus, TestGetFlagBoolServerFlagFalse);
128     FRIEND_TEST(FlagProviderTest_SPlus, TestOverrideLocalFlags);
129     FRIEND_TEST(FlagProviderTest_SPlus, TestGetFlagBoolServerFlagEmptyDefaultFalse);
130     FRIEND_TEST(FlagProviderTest_SPlus, TestGetFlagBoolServerFlagEmptyDefaultTrue);
131     FRIEND_TEST(FlagProviderTest_SPlus_RealValues, TestGetBootFlagBoolServerFlagTrue);
132     FRIEND_TEST(FlagProviderTest_SPlus_RealValues, TestGetBootFlagBoolServerFlagFalse);
133     FRIEND_TEST(MetricsManagerTest_SPlus, TestRestrictedMetricsConfig);
134     FRIEND_TEST(RestrictedEventMetricE2eTest, TestFlagDisabled);
135     FRIEND_TEST(LogEventTest, TestRestrictionCategoryAnnotation);
136     FRIEND_TEST(LogEventTest, TestInvalidRestrictionCategoryAnnotation);
137     FRIEND_TEST(LogEvent_FieldRestrictionTest, TestFieldRestrictionAnnotation);
138     FRIEND_TEST(LogEvent_FieldRestrictionTest, TestInvalidAnnotationIntType);
139     FRIEND_TEST(LogEvent_FieldRestrictionTest, TestInvalidAnnotationAtomLevel);
140 };
141 
142 }  // namespace statsd
143 }  // namespace os
144 }  // namespace android
145