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