1 /*
2  * Copyright (c) 2023 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 
16 #include "gtest/gtest.h"
17 #include "gtest/hwext/gtest-multithread.h"
18 
19 #include "standby_config_manager.h"
20 #include "nlohmann/json.hpp"
21 
22 #include "standby_service_log.h"
23 #include "json_utils.h"
24 #include "common_constant.h"
25 
26 using namespace testing::ext;
27 using namespace testing::mt;
28 
29 namespace OHOS {
30 namespace DevStandbyMgr {
31 namespace {
32     const std::string JSON_KEY = "key";
33     const std::string JSON_ERROR_KEY = "error_key";
34     const std::string TAG_APPS_LIMIT = "apps_limit";
35 }
36 class StandbyUtilsUnitTest : public testing::Test {
37 public:
38     static void SetUpTestCase();
TearDownTestCase()39     static void TearDownTestCase() {}
SetUp()40     void SetUp() override {}
TearDown()41     void TearDown() override {}
42 };
43 
SetUpTestCase()44 void StandbyUtilsUnitTest::SetUpTestCase()
45 {
46     StandbyConfigManager::GetInstance()->Init();
47 }
48 
49 /**
50  * @tc.name: StandbyUtilsUnitTest_002
51  * @tc.desc: test GetInt32FromJsonValue.
52  * @tc.type: FUNC
53  * @tc.require:
54  */
55 HWTEST_F(StandbyUtilsUnitTest, StandbyUtilsUnitTest_002, TestSize.Level1)
56 {
57     nlohmann::json jsonValue {};
58     int32_t value {0};
59     bool ret = JsonUtils::GetInt32FromJsonValue(jsonValue, "", value);
60     EXPECT_FALSE(ret);
61     jsonValue = nlohmann::json::parse("{\"key\":1}", nullptr, false);
62     JsonUtils::GetInt32FromJsonValue(jsonValue, "", value);
63     JsonUtils::GetInt32FromJsonValue(jsonValue, JSON_KEY, value);
64     JsonUtils::GetInt32FromJsonValue(jsonValue, JSON_ERROR_KEY, value);
65     jsonValue = nlohmann::json::parse("{\"key\":\"1\"}", nullptr, false);
66     ret = JsonUtils::GetInt32FromJsonValue(jsonValue, JSON_ERROR_KEY, value);
67     EXPECT_FALSE(ret);
68     JsonUtils::GetInt32FromJsonValue(jsonValue, JSON_KEY, value);
69 }
70 
71 /**
72  * @tc.name: StandbyUtilsUnitTest_003
73  * @tc.desc: test GetBoolFromJsonValue.
74  * @tc.type: FUNC
75  * @tc.require:
76  */
77 HWTEST_F(StandbyUtilsUnitTest, StandbyUtilsUnitTest_003, TestSize.Level1)
78 {
79     nlohmann::json jsonValue {};
80     bool value {false};
81     bool ret = JsonUtils::GetBoolFromJsonValue(jsonValue, "", value);
82     EXPECT_FALSE(ret);
83     jsonValue = nlohmann::json::parse("{\"key\":true}", nullptr, false);
84     JsonUtils::GetBoolFromJsonValue(jsonValue, "", value);
85     JsonUtils::GetBoolFromJsonValue(jsonValue, JSON_KEY, value);
86     JsonUtils::GetBoolFromJsonValue(jsonValue, JSON_ERROR_KEY, value);
87     jsonValue = nlohmann::json::parse("{\"key\":\"true\"}", nullptr, false);
88     ret = JsonUtils::GetBoolFromJsonValue(jsonValue, JSON_ERROR_KEY, value);
89     EXPECT_FALSE(ret);
90     JsonUtils::GetBoolFromJsonValue(jsonValue, JSON_KEY, value);
91 }
92 
93 /**
94  * @tc.name: StandbyUtilsUnitTest_004
95  * @tc.desc: test GetStringFromJsonValue.
96  * @tc.type: FUNC
97  * @tc.require:
98  */
99 HWTEST_F(StandbyUtilsUnitTest, StandbyUtilsUnitTest_004, TestSize.Level1)
100 {
101     nlohmann::json jsonValue {};
102     std::string value {""};
103     bool ret = JsonUtils::GetStringFromJsonValue(jsonValue, "", value);
104     EXPECT_FALSE(ret);
105     jsonValue = nlohmann::json::parse("{\"key\":\"str\"}", nullptr, false);
106     JsonUtils::GetStringFromJsonValue(jsonValue, "", value);
107     JsonUtils::GetStringFromJsonValue(jsonValue, JSON_KEY, value);
108     JsonUtils::GetStringFromJsonValue(jsonValue, JSON_ERROR_KEY, value);
109     jsonValue = nlohmann::json::parse("{\"key\":1}", nullptr, false);
110     ret = JsonUtils::GetStringFromJsonValue(jsonValue, JSON_ERROR_KEY, value);
111     EXPECT_FALSE(ret);
112 }
113 
114 /**
115  * @tc.name: StandbyUtilsUnitTest_005
116  * @tc.desc: test GetObjFromJsonValue.
117  * @tc.type: FUNC
118  * @tc.require:
119  */
120 HWTEST_F(StandbyUtilsUnitTest, StandbyUtilsUnitTest_005, TestSize.Level1)
121 {
122     nlohmann::json jsonValue {};
123     nlohmann::json value {};
124     bool ret = JsonUtils::GetObjFromJsonValue(jsonValue, "", value);
125     EXPECT_FALSE(ret);
126     jsonValue = nlohmann::json::parse("{\"key\":{\"value\":1}}", nullptr, false);
127     JsonUtils::GetObjFromJsonValue(jsonValue, "", value);
128     JsonUtils::GetObjFromJsonValue(jsonValue, JSON_KEY, value);
129     JsonUtils::GetObjFromJsonValue(jsonValue, JSON_ERROR_KEY, value);
130     jsonValue = nlohmann::json::parse("{\"key\":\"str\"}", nullptr, false);
131     ret = JsonUtils::GetObjFromJsonValue(jsonValue, JSON_ERROR_KEY, value);
132     EXPECT_FALSE(ret);
133 }
134 
135 /**
136  * @tc.name: StandbyUtilsUnitTest_006
137  * @tc.desc: test GetArrayFromJsonValue.
138  * @tc.type: FUNC
139  * @tc.require:
140  */
141 HWTEST_F(StandbyUtilsUnitTest, StandbyUtilsUnitTest_006, TestSize.Level1)
142 {
143     nlohmann::json jsonValue {};
144     nlohmann::json value {};
145     bool ret = JsonUtils::GetArrayFromJsonValue(jsonValue, "", value);
146     EXPECT_FALSE(ret);
147     jsonValue = nlohmann::json::parse("{\"key\":[1,2.3]}", nullptr, false);
148     JsonUtils::GetArrayFromJsonValue(jsonValue, "", value);
149     JsonUtils::GetArrayFromJsonValue(jsonValue, JSON_KEY, value);
150     JsonUtils::GetArrayFromJsonValue(jsonValue, JSON_ERROR_KEY, value);
151     jsonValue = nlohmann::json::parse("{\"key\":true}", nullptr, false);
152     ret = JsonUtils::GetArrayFromJsonValue(jsonValue, JSON_ERROR_KEY, value);
153     EXPECT_FALSE(ret);
154 }
155 
156 /**
157  * @tc.name: StandbyUtilsUnitTest_007
158  * @tc.desc: test GetStrArrFromJsonValue.
159  * @tc.type: FUNC
160  * @tc.require:
161  */
162 HWTEST_F(StandbyUtilsUnitTest, StandbyUtilsUnitTest_007, TestSize.Level1)
163 {
164     nlohmann::json jsonValue {};
165     std::vector<std::string> value {};
166     bool ret = JsonUtils::GetStrArrFromJsonValue(jsonValue, "", value);
167     EXPECT_FALSE(ret);
168     jsonValue = nlohmann::json::parse("{\"key\":[\"1\",\"2\"]}", nullptr, false);
169     JsonUtils::GetStrArrFromJsonValue(jsonValue, "", value);
170     JsonUtils::GetStrArrFromJsonValue(jsonValue, JSON_KEY, value);
171     JsonUtils::GetStrArrFromJsonValue(jsonValue, JSON_ERROR_KEY, value);
172     jsonValue = nlohmann::json::parse("{\"key\":true}", nullptr, false);
173     ret = JsonUtils::GetStrArrFromJsonValue(jsonValue, JSON_ERROR_KEY, value);
174     EXPECT_FALSE(ret);
175     jsonValue = nlohmann::json::parse("{\"key\":[1,2]}", nullptr, false);
176     JsonUtils::GetStrArrFromJsonValue(jsonValue, JSON_KEY, value);
177 }
178 
179 /**
180  * @tc.name: StandbyUtilsUnitTest_008
181  * @tc.desc: test GetRealPath.
182  * @tc.type: FUNC
183  * @tc.require:
184  */
185 HWTEST_F(StandbyUtilsUnitTest, StandbyUtilsUnitTest_008, TestSize.Level1)
186 {
187     std::string partialPath (PATH_MAX + 1, 'a');
188     std::string fullPath;
189     EXPECT_FALSE(JsonUtils::GetRealPath(partialPath, fullPath));
190     JsonUtils::CreateNodeFile("/data/service/el1/public/device_standby/allow_record");
191     JsonUtils::CreateNodeFile("/data/service/el1/public/device_standby/allow_record");
192     nlohmann::json jsonValue;
193     JsonUtils::DumpJsonValueToFile(jsonValue, "/data/service/el1/public/device_standby/record");
194 }
195 
196 /**
197  * @tc.name: StandbyUtilsUnitTest_009
198  * @tc.desc: test ParseCondition.
199  * @tc.type: FUNC
200  * @tc.require:
201  */
202 HWTEST_F(StandbyUtilsUnitTest, StandbyUtilsUnitTest_009, TestSize.Level1)
203 {
204     EXPECT_TRUE(StandbyConfigManager::GetInstance()->ParseCondition("test") == 0);
205 }
206 
207 /**
208  * @tc.name: StandbyUtilsUnitTest_010
209  * @tc.desc: test DUMP.
210  * @tc.type: FUNC
211  * @tc.require:
212  */
213 HWTEST_F(StandbyUtilsUnitTest, StandbyUtilsUnitTest_010, TestSize.Level1)
214 {
215     std::string result {""};
216     StandbyConfigManager::GetInstance()->DumpSetDebugMode(true);
217     StandbyConfigManager::GetInstance()->DumpSetDebugMode(false);
218     StandbyConfigManager::GetInstance()->DumpSetSwitch("test", true, result);
219     StandbyConfigManager::GetInstance()->DumpSetSwitch(NAP_SWITCH, true, result);
220     StandbyConfigManager::GetInstance()->DumpSetParameter("test", 0, result);
221     StandbyConfigManager::GetInstance()->DumpSetParameter(NAP_TIMEOUT, 0, result);
222 
223     StandbyConfigManager::GetInstance()->DumpStandbyConfigInfo(result);
224     EXPECT_FALSE(result.empty());
225 }
226 
227 /**
228  * @tc.name: StandbyUtilsUnitTest_011
229  * @tc.desc: test ParseTimeLimitedConfig.
230  * @tc.type: FUNC
231  * @tc.require:
232  */
233 HWTEST_F(StandbyUtilsUnitTest, StandbyUtilsUnitTest_011, TestSize.Level1)
234 {
235     nlohmann::json jsonValue = nlohmann::json::parse("{\"apps_limit\":[\"1\",\"2\"]}", nullptr, false);
236     std::vector<TimeLtdProcess> timeLimitedConfig {};
237     StandbyConfigManager::GetInstance()->ParseTimeLimitedConfig(jsonValue, TAG_APPS_LIMIT, timeLimitedConfig);
238     EXPECT_TRUE(timeLimitedConfig.empty());
239     jsonValue = nlohmann::json::parse("{\"apps_limit\":[{\"name\":\"bundleName\"}]}", nullptr, false);
240     StandbyConfigManager::GetInstance()->ParseTimeLimitedConfig(jsonValue, TAG_APPS_LIMIT, timeLimitedConfig);
241     jsonValue = nlohmann::json::parse("{\"apps_limit\":[{\"name\":\"bundleName\", \"duration\":0}]}", nullptr, false);
242     StandbyConfigManager::GetInstance()->ParseTimeLimitedConfig(jsonValue, TAG_APPS_LIMIT, timeLimitedConfig);
243 }
244 
245 /**
246  * @tc.name: StandbyUtilsUnitTest_012
247  * @tc.desc: test ParseCommonResCtrlConfig.
248  * @tc.type: FUNC
249  * @tc.require:
250  */
251 HWTEST_F(StandbyUtilsUnitTest, StandbyUtilsUnitTest_012, TestSize.Level1)
252 {
253     nlohmann::json jsonValue = nlohmann::json::parse("{}", nullptr, false);
254     DefaultResourceConfig resCtrlConfig {};
255     StandbyConfigManager::GetInstance()->ParseCommonResCtrlConfig(jsonValue, resCtrlConfig);
256     EXPECT_TRUE(resCtrlConfig.conditions_.empty());
257     jsonValue = nlohmann::json::parse("{\"action\":\"allow\"}", nullptr, false);
258     StandbyConfigManager::GetInstance()->ParseCommonResCtrlConfig(jsonValue, resCtrlConfig);
259 
260     jsonValue = nlohmann::json::parse("{\"condition\":[\"day_standby\"], \"action\":\"allow\"}",
261         nullptr, false);
262     StandbyConfigManager::GetInstance()->ParseCommonResCtrlConfig(jsonValue, resCtrlConfig);
263     jsonValue = nlohmann::json::parse("{\"condition\":0, \"action\":0}",
264         nullptr, false);
265     StandbyConfigManager::GetInstance()->ParseCommonResCtrlConfig(jsonValue, resCtrlConfig);
266     jsonValue = nlohmann::json::parse("{\"condition\":[\"day\"], \"action\":\"allow\"}", nullptr, false);
267     StandbyConfigManager::GetInstance()->ParseCommonResCtrlConfig(jsonValue, resCtrlConfig);
268 }
269 
270 /**
271  * @tc.name: StandbyUtilsUnitTest_013
272  * @tc.desc: test ParseDefaultResCtrlConfig.
273  * @tc.type: FUNC
274  * @tc.require:
275  */
276 HWTEST_F(StandbyUtilsUnitTest, StandbyUtilsUnitTest_013, TestSize.Level1)
277 {
278     nlohmann::json resConfigArray = nlohmann::json::parse("{}", nullptr, false);
279     EXPECT_FALSE(StandbyConfigManager::GetInstance()->ParseDefaultResCtrlConfig("test", resConfigArray));
280     StandbyConfigManager::GetInstance()->ParseStrategyListConfig(resConfigArray);
281     StandbyConfigManager::GetInstance()->ParseResCtrlConfig(resConfigArray);
282     StandbyConfigManager::GetInstance()->ParseTimerResCtrlConfig(resConfigArray);
283     resConfigArray = nlohmann::json::parse("{\"condition\":[]}", nullptr, false);
284     StandbyConfigManager::GetInstance()->ParseDefaultResCtrlConfig("test", resConfigArray);
285     StandbyConfigManager::GetInstance()->ParseTimerResCtrlConfig(resConfigArray);
286 
287     std::string content = "{\"condition\":[\"day\"], \"action\":\"allow\"}";
288     resConfigArray = nlohmann::json::parse(content, nullptr, false);
289     StandbyConfigManager::GetInstance()->ParseDefaultResCtrlConfig("test", resConfigArray);
290     StandbyConfigManager::GetInstance()->ParseTimerResCtrlConfig(resConfigArray);
291 
292     content = "{\"condition\":[\"day\"], \"action\":\"allow\",\"time_clock_apps\":[]}";
293     resConfigArray = nlohmann::json::parse(content, nullptr, false);
294     StandbyConfigManager::GetInstance()->ParseTimerResCtrlConfig(resConfigArray);
295 
296     content = "{\"condition\":[\"day\"], \"action\":\"allow\",\"time_clock_apps\":[{\"name\":\"bundleName\"}]}";
297     resConfigArray = nlohmann::json::parse(content, nullptr, false);
298     StandbyConfigManager::GetInstance()->ParseTimerResCtrlConfig(resConfigArray);
299 
300     content = "{\"condition\":[\"day\"], \"action\":\"allow\",\"time_clock_apps\":[{\"name\":\"bundleName\","\
301         "\"timer_clock\":true, \"timer_period\":0}]}";
302     resConfigArray = nlohmann::json::parse(content, nullptr, false);
303     StandbyConfigManager::GetInstance()->ParseTimerResCtrlConfig(resConfigArray);
304 }
305 
306 /**
307  * @tc.name: StandbyUtilsUnitTest_014
308  * @tc.desc: test ParseDeviceStanbyConfig.
309  * @tc.type: FUNC
310  * @tc.require:
311  */
312 HWTEST_F(StandbyUtilsUnitTest, StandbyUtilsUnitTest_014, TestSize.Level1)
313 {
314     nlohmann::json devStandbyConfigRoot = nlohmann::json::parse("{}", nullptr, false);
315     EXPECT_TRUE(StandbyConfigManager::GetInstance()->ParseDeviceStanbyConfig(devStandbyConfigRoot));
316     std::string content = "{\"halfhour_switch_setting\":[\"test\":0]}";
317     devStandbyConfigRoot = nlohmann::json::parse(content, nullptr, false);
318     StandbyConfigManager::GetInstance()->ParseDeviceStanbyConfig(devStandbyConfigRoot);
319     StandbyConfigManager::GetInstance()->ParseHalfHourSwitchConfig(devStandbyConfigRoot);
320     content = "{\"halfhour_switch_setting\":[\"test\":true]}";
321     devStandbyConfigRoot = nlohmann::json::parse(content, nullptr, false);
322     StandbyConfigManager::GetInstance()->ParseHalfHourSwitchConfig(devStandbyConfigRoot);
323     content = "{\"standby\":[\"test\":0]}";
324     devStandbyConfigRoot = nlohmann::json::parse(content, nullptr, false);
325     StandbyConfigManager::GetInstance()->ParseDeviceStanbyConfig(devStandbyConfigRoot);
326     content = "{\"detect_list\":[\"test\":0]}";
327     devStandbyConfigRoot = nlohmann::json::parse(content, nullptr, false);
328     StandbyConfigManager::GetInstance()->ParseDeviceStanbyConfig(devStandbyConfigRoot);
329     content = "{\"maintenance_list\":[\"test\":[1, 2, 3]]}";
330     devStandbyConfigRoot = nlohmann::json::parse(content, nullptr, false);
331     StandbyConfigManager::GetInstance()->ParseDeviceStanbyConfig(devStandbyConfigRoot);
332 }
333 
334 /**
335  * @tc.name: StandbyUtilsUnitTest_015
336  * @tc.desc: test StandbyConfigManager.
337  * @tc.type: FUNC
338  * @tc.require:
339  */
340 HWTEST_F(StandbyUtilsUnitTest, StandbyUtilsUnitTest_015, TestSize.Level1)
341 {
342     StandbyConfigManager::GetInstance()->GetStrategySwitch("test");
343     StandbyConfigManager::GetInstance()->GetTimerResConfig();
344     TimeLtdProcess process1 {"process1", 10};
345     TimeLtdProcess process2 {"process2", 20};
346     EXPECT_TRUE(process1 < process2);
347 }
348 }  // namespace DevStandbyMgr
349 }  // namespace OHOS
350