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 #ifndef CPP_WATCHDOG_SERVER_TESTS_MOCKIOOVERUSECONFIGS_H_
18 #define CPP_WATCHDOG_SERVER_TESTS_MOCKIOOVERUSECONFIGS_H_
19 
20 #include "IoOveruseConfigs.h"
21 
22 #include <aidl/android/automotive/watchdog/PerStateBytes.h>
23 #include <aidl/android/automotive/watchdog/internal/ApplicationCategoryType.h>
24 #include <aidl/android/automotive/watchdog/internal/ComponentType.h>
25 #include <aidl/android/automotive/watchdog/internal/PackageInfo.h>
26 #include <aidl/android/automotive/watchdog/internal/ResourceOveruseConfiguration.h>
27 #include <android-base/result.h>
28 #include <gmock/gmock.h>
29 
30 namespace android {
31 namespace automotive {
32 namespace watchdog {
33 
34 class MockIoOveruseConfigs : public IoOveruseConfigsInterface {
35 public:
MockIoOveruseConfigs()36     MockIoOveruseConfigs() {}
~MockIoOveruseConfigs()37     ~MockIoOveruseConfigs() {}
38     MOCK_METHOD(
39             android::base::Result<void>, update,
40             (const std::vector<
41                     aidl::android::automotive::watchdog::internal::ResourceOveruseConfiguration>&),
42             (override));
43 
44     MOCK_METHOD(
45             void, get,
46             (std::vector<
47                     aidl::android::automotive::watchdog::internal::ResourceOveruseConfiguration>*),
48             (const, override));
49 
50     MOCK_METHOD(android::base::Result<void>, writeToDisk, (), (override));
51 
52     MOCK_METHOD((const std::unordered_set<std::string>&), vendorPackagePrefixes, (), (override));
53 
54     MOCK_METHOD((const std::unordered_map<
55                         std::string,
56                         aidl::android::automotive::watchdog::internal::ApplicationCategoryType>&),
57                 packagesToAppCategories, (), (override));
58 
59     MOCK_METHOD(aidl::android::automotive::watchdog::PerStateBytes, fetchThreshold,
60                 (const aidl::android::automotive::watchdog::internal::PackageInfo&),
61                 (const, override));
62 
63     MOCK_METHOD(bool, isSafeToKill,
64                 (const aidl::android::automotive::watchdog::internal::PackageInfo&),
65                 (const, override));
66 
67     MOCK_METHOD((const IoOveruseAlertThresholdSet&), systemWideAlertThresholds, (), (override));
68 
69     struct PackageConfig {
70         aidl::android::automotive::watchdog::PerStateBytes threshold;
71         bool isSafeToKill = false;
72     };
73 
injectPackageConfigs(const std::unordered_map<std::string,PackageConfig> & perPackageConfig)74     void injectPackageConfigs(
75             const std::unordered_map<std::string, PackageConfig>& perPackageConfig) {
76         ON_CALL(*this, fetchThreshold(testing::_))
77                 .WillByDefault(
78                         [perPackageConfig = perPackageConfig](
79                                 const aidl::android::automotive::watchdog::internal::PackageInfo&
80                                         packageInfo) {
81                             const std::string packageName = packageInfo.packageIdentifier.name;
82                             if (const auto it = perPackageConfig.find(packageName);
83                                 it != perPackageConfig.end()) {
84                                 return it->second.threshold;
85                             }
86                             return defaultThreshold().perStateWriteBytes;
87                         });
88         ON_CALL(*this, isSafeToKill(testing::_))
89                 .WillByDefault(
90                         [perPackageConfig = perPackageConfig](
91                                 const aidl::android::automotive::watchdog::internal::PackageInfo&
92                                         packageInfo) {
93                             const std::string packageName = packageInfo.packageIdentifier.name;
94                             if (const auto it = perPackageConfig.find(packageName);
95                                 it != perPackageConfig.end()) {
96                                 return it->second.isSafeToKill;
97                             }
98                             return true;
99                         });
100     }
101 };
102 
103 }  // namespace watchdog
104 }  // namespace automotive
105 }  // namespace android
106 
107 #endif  //  CPP_WATCHDOG_SERVER_TESTS_MOCKIOOVERUSECONFIGS_H_
108