1 /*
2  * Copyright (c) 2024 Huawei Device Co., Ltd.
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 "configmanager_fuzzer.h"
16 #include "securec.h"
17 #include "json_utils.h"
18 #include "nlohmann/json.hpp"
19 #include "standby_config_manager.h"
20 
21 
22 namespace OHOS {
23 namespace DevStandbyMgr {
24     constexpr size_t U32_AT_SIZE = 6;
25     const std::string TAG_APPS_LIMIT = "apps_limit";
26     const std::string TAG_TEST = "test";
27     const std::string TAG_TEST_ONE = "test01";
28     bool g_initFlag = false;
29     const uint8_t *g_baseFuzzData = nullptr;
30     size_t g_baseFuzzSize = 0;
31     size_t g_baseFuzzPos;
32 
GetData()33     template <class T> T GetData()
34     {
35         T object{};
36         size_t objectSize = sizeof(object);
37         if (g_baseFuzzData == nullptr || objectSize > g_baseFuzzSize - g_baseFuzzPos) {
38             return object;
39         }
40         errno_t ret = memcpy_s(&object, objectSize, g_baseFuzzData + g_baseFuzzPos, objectSize);
41         if (ret != EOK) {
42             return {};
43         }
44         g_baseFuzzPos += objectSize;
45         return object;
46     }
47 
PreciseCoverageParseTimeLimitedConfig()48     void PreciseCoverageParseTimeLimitedConfig()
49     {
50         auto func = [](bool isApp, std::set<TimeLtdProcess>& eligibleResCtrlConfig,
51             const DefaultResourceConfig& config) {
52         };
53         auto defaultResConfigPtr = std::make_shared<std::vector<DefaultResourceConfig>>();
54         DefaultResourceConfig defaultResourceConfig;
55         defaultResConfigPtr->emplace_back(std::move(defaultResourceConfig));
56         DelayedSingleton<StandbyConfigManager>::GetInstance()->
57             defaultResourceConfigMap_[TAG_TEST] = defaultResConfigPtr;
58         DelayedSingleton<StandbyConfigManager>::GetInstance()->
59             GetEligibleAllowTimeConfig(TAG_TEST, 0, false, false);
60         DelayedSingleton<StandbyConfigManager>::GetInstance()->
61             GetEligibleAllowConfig<TimeLtdProcess>(TAG_TEST, 0, false, false, func);
62         DefaultResourceConfig defaultResourceConfig01;
63         std::vector<uint32_t> conditions {0};
64         defaultResourceConfig01.conditions_ = conditions;
65         defaultResConfigPtr->emplace_back(std::move(defaultResourceConfig01));
66         DelayedSingleton<StandbyConfigManager>::GetInstance()->
67             defaultResourceConfigMap_[TAG_TEST_ONE] = defaultResConfigPtr;
68         DelayedSingleton<StandbyConfigManager>::GetInstance()->
69             GetEligibleAllowTimeConfig(TAG_TEST_ONE, 0, false, false);
70         DelayedSingleton<StandbyConfigManager>::GetInstance()->
71             GetEligibleAllowTimeConfig(TAG_TEST_ONE, 0, false, true);
72         DelayedSingleton<StandbyConfigManager>::GetInstance()->
73             GetEligibleAllowConfig<TimeLtdProcess>(TAG_TEST_ONE, 0, false, false, func);
74         nlohmann::json jsonValue = nlohmann::json::parse("{\"apps_limit\":[\"1\",\"2\"]}", nullptr, false);
75         std::vector<TimeLtdProcess> timeLimitedConfig {};
76         DelayedSingleton<StandbyConfigManager>::GetInstance()->
77             ParseTimeLimitedConfig(jsonValue, TAG_APPS_LIMIT, timeLimitedConfig);
78         jsonValue = nlohmann::json::parse("{\"apps_limit\":[{\"name\":\"bundleName\"}]}", nullptr, false);
79         DelayedSingleton<StandbyConfigManager>::GetInstance()->
80             ParseTimeLimitedConfig(jsonValue, TAG_APPS_LIMIT, timeLimitedConfig);
81         jsonValue = nlohmann::json::parse("{\"apps_limit\":[{\"name\":\"bundleName\", \"duration\":0}]}",
82             nullptr, false);
83         DelayedSingleton<StandbyConfigManager>::GetInstance()->
84             ParseTimeLimitedConfig(jsonValue, TAG_APPS_LIMIT, timeLimitedConfig);
85     }
86 
PreciseCoverageParseTimerResCtrlConfig()87     void PreciseCoverageParseTimerResCtrlConfig()
88     {
89         nlohmann::json resConfigArray = nlohmann::json::parse("{}", nullptr, false);
90         DelayedSingleton<StandbyConfigManager>::GetInstance()->ParseDefaultResCtrlConfig("test", resConfigArray);
91         StandbyConfigManager::GetInstance()->ParseStrategyListConfig(resConfigArray);
92         DelayedSingleton<StandbyConfigManager>::GetInstance()->ParseResCtrlConfig(resConfigArray);
93         DelayedSingleton<StandbyConfigManager>::GetInstance()->ParseTimerResCtrlConfig(resConfigArray);
94         resConfigArray = nlohmann::json::parse("{\"condition\":[]}", nullptr, false);
95         DelayedSingleton<StandbyConfigManager>::GetInstance()->ParseDefaultResCtrlConfig("test", resConfigArray);
96         DelayedSingleton<StandbyConfigManager>::GetInstance()->ParseTimerResCtrlConfig(resConfigArray);
97 
98         std::string content = "{\"condition\":[\"day\"], \"action\":\"allow\"}";
99         resConfigArray = nlohmann::json::parse(content, nullptr, false);
100         DelayedSingleton<StandbyConfigManager>::GetInstance()->ParseDefaultResCtrlConfig("test", resConfigArray);
101         DelayedSingleton<StandbyConfigManager>::GetInstance()->ParseTimerResCtrlConfig(resConfigArray);
102 
103         content = "{\"condition\":[\"day\"], \"action\":\"allow\",\"time_clock_apps\":[]}";
104         resConfigArray = nlohmann::json::parse(content, nullptr, false);
105         DelayedSingleton<StandbyConfigManager>::GetInstance()->ParseTimerResCtrlConfig(resConfigArray);
106 
107         content = "[{\"condition\":[\"day\"], \"action\":\"allow\",\"time_clock_apps\":[{\"name\":\"bundleName\"}]}]";
108         resConfigArray = nlohmann::json::parse(content, nullptr, false);
109         DelayedSingleton<StandbyConfigManager>::GetInstance()->ParseTimerResCtrlConfig(resConfigArray);
110 
111         content = "[{\"condition\":[\"day\"], \"action\":\"allow\",\"time_clock_apps\":[{\"name\":\"bundleName\","\
112         "\"timer_clock\":true, \"timer_period\":0}]}]";
113         resConfigArray = nlohmann::json::parse(content, nullptr, false);
114         DelayedSingleton<StandbyConfigManager>::GetInstance()->ParseTimerResCtrlConfig(resConfigArray);
115     }
116 
PreciseCoverage()117     void PreciseCoverage()
118     {
119         if (g_initFlag) {
120             return;
121         }
122         g_initFlag = true;
123         DelayedSingleton<StandbyConfigManager>::GetInstance()->Init();
124         PreciseCoverageParseTimeLimitedConfig();
125         PreciseCoverageParseTimerResCtrlConfig();
126         DelayedSingleton<StandbyConfigManager>::GetInstance()->GetPluginName();
127         DelayedSingleton<StandbyConfigManager>::GetInstance()->GetStrategyConfigList();
128         DelayedSingleton<StandbyConfigManager>::GetInstance()->GetTimerResConfig();
129     }
130 
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)131     bool DoSomethingInterestingWithMyAPI(const uint8_t *data, size_t size)
132     {
133         g_baseFuzzData = data;
134         g_baseFuzzSize = size;
135         g_baseFuzzPos = 0;
136         uint32_t  condition = GetData<uint32_t>();
137         bool debugMode = GetData<bool>();
138         bool isAllow = GetData<bool>();
139         std::string str((const char *) g_baseFuzzData + g_baseFuzzPos, g_baseFuzzSize - g_baseFuzzPos);
140         PreciseCoverage();
141         DelayedSingleton<StandbyConfigManager>::GetInstance()->GetStandbyDurationList(str);
142         DelayedSingleton<StandbyConfigManager>::GetInstance()->GetStandbySwitch(str);
143         DelayedSingleton<StandbyConfigManager>::GetInstance()->GetStandbyParam(str);
144         DelayedSingleton<StandbyConfigManager>::GetInstance()->GetStrategySwitch(str);
145         DelayedSingleton<StandbyConfigManager>::GetInstance()->GetHalfHourSwitch(str);
146         DelayedSingleton<StandbyConfigManager>::GetInstance()->GetResCtrlConfig(str);
147         DelayedSingleton<StandbyConfigManager>::GetInstance()->GetMaxDuration(str, str,
148             condition, debugMode);
149         DelayedSingleton<StandbyConfigManager>::GetInstance()->GetEligibleAllowTimeConfig(str,
150             condition, debugMode, isAllow);
151         DelayedSingleton<StandbyConfigManager>::GetInstance()->GetEligiblePersistAllowConfig(str,
152             condition, debugMode, isAllow);
153         DelayedSingleton<StandbyConfigManager>::GetInstance()->DumpSetDebugMode(debugMode);
154         DelayedSingleton<StandbyConfigManager>::GetInstance()->DumpSetSwitch(str, debugMode, str);
155         DelayedSingleton<StandbyConfigManager>::GetInstance()->DumpSetParameter(str, condition, str);
156         DelayedSingleton<StandbyConfigManager>::GetInstance()->DumpStandbyConfigInfo(str);
157         return true;
158     }
159 } // namespace DevStandbyMgr
160 } // namespace OHOS
161 
162 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)163 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
164 {
165     /* Run your code on data */
166     if (data == nullptr) {
167         return 0;
168     }
169 
170     if (size < OHOS::DevStandbyMgr::U32_AT_SIZE) {
171         return 0;
172     }
173     OHOS::DevStandbyMgr::DoSomethingInterestingWithMyAPI(data, size);
174     return 0;
175 }
176