1 /*
2 * Copyright 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 #include "OveruseConfigurationTestUtils.h"
18
19 namespace android {
20 namespace automotive {
21 namespace watchdog {
22
23 using ::aidl::android::automotive::watchdog::PerStateBytes;
24 using ::aidl::android::automotive::watchdog::internal::ApplicationCategoryType;
25 using ::aidl::android::automotive::watchdog::internal::ComponentType;
26 using ::aidl::android::automotive::watchdog::internal::IoOveruseAlertThreshold;
27 using ::aidl::android::automotive::watchdog::internal::IoOveruseConfiguration;
28 using ::aidl::android::automotive::watchdog::internal::PackageMetadata;
29 using ::aidl::android::automotive::watchdog::internal::PerStateIoOveruseThreshold;
30 using ::aidl::android::automotive::watchdog::internal::ResourceOveruseConfiguration;
31 using ::aidl::android::automotive::watchdog::internal::ResourceSpecificConfiguration;
32 using ::testing::AllOf;
33 using ::testing::ExplainMatchResult;
34 using ::testing::Field;
35 using ::testing::Matcher;
36 using ::testing::UnorderedElementsAreArray;
37
38 namespace {
39
40 MATCHER_P(IsIoOveruseConfiguration, config, "") {
41 return arg.componentLevelThresholds == config.componentLevelThresholds &&
42 ExplainMatchResult(UnorderedElementsAreArray(config.packageSpecificThresholds),
43 arg.packageSpecificThresholds, result_listener) &&
44 ExplainMatchResult(UnorderedElementsAreArray(config.categorySpecificThresholds),
45 arg.categorySpecificThresholds, result_listener) &&
46 ExplainMatchResult(UnorderedElementsAreArray(config.systemWideThresholds),
47 arg.systemWideThresholds, result_listener);
48 }
49
50 MATCHER_P(IsResourceSpecificConfiguration, config, "") {
51 if (arg.getTag() != config.getTag()) {
52 return false;
53 }
54 // Reference with the actual datatype so the templated get method can be called.
55 const ResourceSpecificConfiguration& expected = config;
56 const ResourceSpecificConfiguration& actual = arg;
57 switch (arg.getTag()) {
58 case ResourceSpecificConfiguration::ioOveruseConfiguration: {
59 const auto& expectedIoConfig =
60 expected.get<ResourceSpecificConfiguration::ioOveruseConfiguration>();
61 const auto& actualIoConfig =
62 actual.get<ResourceSpecificConfiguration::ioOveruseConfiguration>();
63 return ExplainMatchResult(IsIoOveruseConfiguration(expectedIoConfig), actualIoConfig,
64 result_listener);
65 }
66 default:
67 return true;
68 }
69 }
70
71 MATCHER_P(IsPackageMetadata, expected, "") {
72 return arg.packageName == expected.packageName &&
73 arg.appCategoryType == expected.appCategoryType;
74 }
75
76 } // namespace
77
constructResourceOveruseConfig(const ComponentType type,const std::vector<std::string> && safeToKill,const std::vector<std::string> && vendorPrefixes,const std::vector<PackageMetadata> packageMetadata,const IoOveruseConfiguration & ioOveruseConfiguration)78 ResourceOveruseConfiguration constructResourceOveruseConfig(
79 const ComponentType type, const std::vector<std::string>&& safeToKill,
80 const std::vector<std::string>&& vendorPrefixes,
81 const std::vector<PackageMetadata> packageMetadata,
82 const IoOveruseConfiguration& ioOveruseConfiguration) {
83 ResourceOveruseConfiguration resourceOveruseConfig;
84 resourceOveruseConfig.componentType = type;
85 resourceOveruseConfig.safeToKillPackages = safeToKill;
86 resourceOveruseConfig.vendorPackagePrefixes = vendorPrefixes;
87 resourceOveruseConfig.packageMetadata = packageMetadata;
88 ResourceSpecificConfiguration config;
89 config.set<ResourceSpecificConfiguration::ioOveruseConfiguration>(ioOveruseConfiguration);
90 resourceOveruseConfig.resourceSpecificConfigurations.push_back(config);
91 return resourceOveruseConfig;
92 }
93
constructIoOveruseConfig(PerStateIoOveruseThreshold componentLevel,const std::vector<PerStateIoOveruseThreshold> & packageSpecific,const std::vector<PerStateIoOveruseThreshold> & categorySpecific,const std::vector<IoOveruseAlertThreshold> & systemWide)94 IoOveruseConfiguration constructIoOveruseConfig(
95 PerStateIoOveruseThreshold componentLevel,
96 const std::vector<PerStateIoOveruseThreshold>& packageSpecific,
97 const std::vector<PerStateIoOveruseThreshold>& categorySpecific,
98 const std::vector<IoOveruseAlertThreshold>& systemWide) {
99 IoOveruseConfiguration config;
100 config.componentLevelThresholds = componentLevel;
101 config.packageSpecificThresholds = packageSpecific;
102 config.categorySpecificThresholds = categorySpecific;
103 config.systemWideThresholds = systemWide;
104 return config;
105 }
106
toPerStateBytes(const int64_t fgBytes,const int64_t bgBytes,const int64_t garageModeBytes)107 PerStateBytes toPerStateBytes(const int64_t fgBytes, const int64_t bgBytes,
108 const int64_t garageModeBytes) {
109 PerStateBytes perStateBytes;
110 perStateBytes.foregroundBytes = fgBytes;
111 perStateBytes.backgroundBytes = bgBytes;
112 perStateBytes.garageModeBytes = garageModeBytes;
113 return perStateBytes;
114 }
115
toPerStateIoOveruseThreshold(const std::string & name,const PerStateBytes & perStateBytes)116 PerStateIoOveruseThreshold toPerStateIoOveruseThreshold(const std::string& name,
117 const PerStateBytes& perStateBytes) {
118 PerStateIoOveruseThreshold threshold;
119 threshold.name = name;
120 threshold.perStateWriteBytes = perStateBytes;
121 return threshold;
122 }
123
toPerStateIoOveruseThreshold(const std::string & name,const int64_t fgBytes,const int64_t bgBytes,const int64_t garageModeBytes)124 PerStateIoOveruseThreshold toPerStateIoOveruseThreshold(const std::string& name,
125 const int64_t fgBytes,
126 const int64_t bgBytes,
127 const int64_t garageModeBytes) {
128 return toPerStateIoOveruseThreshold(name, toPerStateBytes(fgBytes, bgBytes, garageModeBytes));
129 }
130
toPerStateIoOveruseThreshold(const ComponentType type,const PerStateBytes & perStateBytes)131 PerStateIoOveruseThreshold toPerStateIoOveruseThreshold(const ComponentType type,
132 const PerStateBytes& perStateBytes) {
133 return toPerStateIoOveruseThreshold(toString(type), perStateBytes);
134 }
135
toPerStateIoOveruseThreshold(const ComponentType type,const int64_t fgBytes,const int64_t bgBytes,const int64_t garageModeBytes)136 PerStateIoOveruseThreshold toPerStateIoOveruseThreshold(const ComponentType type,
137 const int64_t fgBytes,
138 const int64_t bgBytes,
139 const int64_t garageModeBytes) {
140 return toPerStateIoOveruseThreshold(type, toPerStateBytes(fgBytes, bgBytes, garageModeBytes));
141 }
142
toPackageMetadata(std::string packageName,ApplicationCategoryType type)143 PackageMetadata toPackageMetadata(std::string packageName, ApplicationCategoryType type) {
144 PackageMetadata meta;
145 meta.packageName = packageName;
146 meta.appCategoryType = type;
147 return meta;
148 }
149
toIoOveruseAlertThreshold(const int64_t durationInSeconds,const int64_t writtenBytesPerSecond)150 IoOveruseAlertThreshold toIoOveruseAlertThreshold(const int64_t durationInSeconds,
151 const int64_t writtenBytesPerSecond) {
152 IoOveruseAlertThreshold threshold;
153 threshold.durationInSeconds = durationInSeconds;
154 threshold.writtenBytesPerSecond = writtenBytesPerSecond;
155 return threshold;
156 }
157
ResourceOveruseConfigurationMatcher(const ResourceOveruseConfiguration & config)158 Matcher<const ResourceOveruseConfiguration&> ResourceOveruseConfigurationMatcher(
159 const ResourceOveruseConfiguration& config) {
160 std::vector<Matcher<const ResourceSpecificConfiguration&>> resourceSpecificConfigMatchers;
161 for (const auto& resourceSpecificConfig : config.resourceSpecificConfigurations) {
162 resourceSpecificConfigMatchers.push_back(
163 IsResourceSpecificConfiguration(resourceSpecificConfig));
164 }
165
166 std::vector<Matcher<const PackageMetadata&>> metadataMatchers;
167 for (const auto& metadata : config.packageMetadata) {
168 metadataMatchers.push_back(IsPackageMetadata(metadata));
169 }
170
171 return AllOf(Field(&ResourceOveruseConfiguration::componentType, config.componentType),
172 Field(&ResourceOveruseConfiguration::safeToKillPackages,
173 UnorderedElementsAreArray(config.safeToKillPackages)),
174 Field(&ResourceOveruseConfiguration::vendorPackagePrefixes,
175 UnorderedElementsAreArray(config.vendorPackagePrefixes)),
176 Field(&ResourceOveruseConfiguration::packageMetadata,
177 UnorderedElementsAreArray(metadataMatchers)),
178 Field(&ResourceOveruseConfiguration::resourceSpecificConfigurations,
179 UnorderedElementsAreArray(resourceSpecificConfigMatchers)));
180 }
181
182 } // namespace watchdog
183 } // namespace automotive
184 } // namespace android
185