1 /*
2 * Copyright (c) 2022 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 "distributed_sched_utils_test.h"
17
18 #include <string>
19
20 #include "config_policy_utils.h"
21
22 #include "distributed_sched_utils.h"
23 #include "dtbschedmgr_log.h"
24
25 using namespace testing;
26 using namespace testing::ext;
27
28 namespace OHOS {
29 namespace DistributedSchedule {
30 const std::string TAG = "DistributedSchedUtilsTest";
31 constexpr const char* PARAM_KEY_OS_TYPE = "OS_TYPE";
32 constexpr const char* PARAM_KEY_OS_VERSION = "OS_VERSION";
33 constexpr int32_t MAX_TEST_PATH_LEN = 1024;
34 const std::string TEST_CONFIG_RELATIVE_PATH = "etc/distributedhardware/distributed_hardware_components_cfg.json";
35
SetUpTestCase()36 void DistributedSchedUtilsTest::SetUpTestCase()
37 {
38 HILOGI("DistributedSchedUtilsTest::SetUpTestCase");
39 }
40
TearDownTestCase()41 void DistributedSchedUtilsTest::TearDownTestCase()
42 {
43 HILOGI("DistributedSchedUtilsTest::TearDownTestCase");
44 }
45
TearDown()46 void DistributedSchedUtilsTest::TearDown()
47 {
48 HILOGI("DistributedSchedUtilsTest::TearDown");
49 }
50
SetUp()51 void DistributedSchedUtilsTest::SetUp()
52 {
53 HILOGI("DistributedSchedUtilsTest::SetUp");
54 }
55
56 /**
57 * @tc.name: IsValidPath_001
58 * @tc.desc: File path is invalid
59 * @tc.type: FUNC
60 * @tc.require: I5WKCK
61 */
62 HWTEST_F(DistributedSchedUtilsTest, IsValidPath_001, TestSize.Level1)
63 {
64 std::string inFilePath = "invalid_path";
65 std::string realFilePath = "";
66 EXPECT_FALSE(IsValidPath(inFilePath, realFilePath));
67 EXPECT_EQ("", realFilePath);
68
69 inFilePath = "/data/123_test.json";
70 EXPECT_FALSE(IsValidPath(inFilePath, realFilePath));
71 EXPECT_EQ("", realFilePath);
72 }
73
74 /**
75 * @tc.name: IsValidPath_002
76 * @tc.desc: File path is valid
77 * @tc.type: FUNC
78 * @tc.require: I5WKCK
79 */
80 HWTEST_F(DistributedSchedUtilsTest, IsValidPath_002, TestSize.Level1)
81 {
82 std::string inFilePath(PATH_MAX + 1, 'a');
83 std::string realFilePath = "";
84 EXPECT_FALSE(IsValidPath(inFilePath, realFilePath));
85 }
86
87 /**
88 * @tc.name: IsValidPath_003
89 * @tc.desc: File path is invalid
90 * @tc.type: FUNC
91 * @tc.require: I5WKCK
92 */
93 HWTEST_F(DistributedSchedUtilsTest, IsValidPath_003, TestSize.Level1)
94 {
95 std::string inFilePath = "";
96 std::string realFilePath = "";
97 EXPECT_FALSE(IsValidPath(inFilePath, realFilePath));
98
99 inFilePath = "/data/123_test.json";
100 EXPECT_FALSE(IsValidPath(inFilePath, realFilePath));
101 }
102
103 /**
104 * @tc.name: IsValidPath_004
105 * @tc.desc: File path is valid
106 * @tc.type: FUNC
107 * @tc.require: I5WKCK
108 */
109 HWTEST_F(DistributedSchedUtilsTest, IsValidPath_004, TestSize.Level1)
110 {
111 std::string inFilePath(MAX_TEST_PATH_LEN, 'a');
112 std::string realFilePath = "";
113 EXPECT_FALSE(IsValidPath(inFilePath, realFilePath));
114 }
115
116 /**
117 * @tc.name: CheckBundleContinueConfig_001
118 * @tc.desc: Check bundle continue config when existing config file
119 * @tc.type: FUNC
120 * @tc.require: I5WKCK
121 */
122 HWTEST_F(DistributedSchedUtilsTest, CheckBundleContinueConfig_001, TestSize.Level1)
123 {
124 std::string cfgJsonStr = R"({
125 "allow_applist":["test_bundle_0"]
126 })";
127 EXPECT_TRUE(UpdateAllowAppList(cfgJsonStr));
128
129 std::string bundleName = "test";
130 EXPECT_FALSE(CheckBundleContinueConfig(bundleName));
131
132 bundleName = "test_bundle_0";
133 EXPECT_TRUE(CheckBundleContinueConfig(bundleName));
134 }
135
136 /**
137 * @tc.name: UpdateAllowAppList_001
138 * @tc.desc: Update allow app list with invalid cfgJsonStr
139 * @tc.type: FUNC
140 * @tc.require: I5WKCK
141 */
142 HWTEST_F(DistributedSchedUtilsTest, UpdateAllowAppList_001, TestSize.Level1)
143 {
144 std::string cfgJsonStr = "";
145 EXPECT_FALSE(UpdateAllowAppList(cfgJsonStr));
146
147 cfgJsonStr = "12345";
148 EXPECT_FALSE(UpdateAllowAppList(cfgJsonStr));
149
150 cfgJsonStr = R"({
151 "Name":["test_one"],
152 "ID":"12345"
153 })";
154 EXPECT_FALSE(UpdateAllowAppList(cfgJsonStr));
155
156 cfgJsonStr = R"({
157 "allow_applist":"12345"
158 })";
159 EXPECT_FALSE(UpdateAllowAppList(cfgJsonStr));
160 }
161
162 /**
163 * @tc.name: UpdateAllowAppList_002
164 * @tc.desc: Update allow app list with valid cfgJsonStr
165 * @tc.type: FUNC
166 * @tc.require: I5WKCK
167 */
168 HWTEST_F(DistributedSchedUtilsTest, UpdateAllowAppList_002, TestSize.Level1)
169 {
170 std::string cfgJsonStr = R"({
171 "allow_applist":["test_bundle_1"]
172 })";
173 EXPECT_TRUE(UpdateAllowAppList(cfgJsonStr));
174 }
175
176 /**
177 * @tc.name: LoadContinueConfig_001
178 * @tc.desc: Load continue config success
179 * @tc.type: FUNC
180 * @tc.require: I5WKCK
181 */
182 HWTEST_F(DistributedSchedUtilsTest, LoadContinueConfig_001, TestSize.Level1)
183 {
184 EXPECT_EQ(ERR_OK, LoadContinueConfig());
185 EXPECT_EQ(ERR_OK, LoadContinueConfig());
186 }
187
188 /**
189 * @tc.name: CheckBundleContinueConfig_002
190 * @tc.desc: Check bundle continue config when missing config file
191 * @tc.type: FUNC
192 * @tc.require: I5WKCK
193 */
194 HWTEST_F(DistributedSchedUtilsTest, CheckBundleContinueConfig_002, TestSize.Level1)
195 {
196 EXPECT_EQ(ERR_OK, LoadContinueConfig());
197
198 std::string bundleName = "test_bundle_1";
199 EXPECT_TRUE(CheckBundleContinueConfig(bundleName));
200 }
201
202 /**
203 * @tc.name: ParcelToBase64Str_001
204 * @tc.desc: ParcelToBase64Str
205 * @tc.type: FUNC
206 * @tc.require: I5WKCK
207 */
208 HWTEST_F(DistributedSchedUtilsTest, ParcelToBase64Str_001, TestSize.Level1)
209 {
210 EXPECT_EQ(INVALID_MISSION_ID, GetCurrentMissionId());
211
212 Parcel parcel;
213 std::string rawStr;
214 EXPECT_EQ("", ParcelToBase64Str(parcel));
215
216 EXPECT_EQ(INVALID_PARAMETERS_ERR, Base64StrToParcel(rawStr, parcel));
217
218 unsigned char *toEncode = nullptr;
219 unsigned int len = 0;
220 EXPECT_EQ("", Base64Encode(toEncode, len));
221
222 len = 1;
223 EXPECT_EQ("", Base64Encode(toEncode, len));
224
225 std::string basicString;
226 EXPECT_EQ("", Base64Decode(basicString));
227
228 EXPECT_EQ(true, IsBase64('+'));
229 EXPECT_EQ(true, IsBase64('/'));
230 EXPECT_EQ(true, IsBase64('3'));
231 }
232
233 /**
234 * @tc.name: Base64Encode_001
235 * @tc.desc: Base64Encode
236 * @tc.type: FUNC
237 * @tc.require: I5WKCK
238 */
239 HWTEST_F(DistributedSchedUtilsTest, Base64Encode_001, TestSize.Level1)
240 {
241 unsigned char *toEncode = nullptr;
242 unsigned int len = 0;
243 std::string basicString;
244
245 EXPECT_EQ(Base64Encode(toEncode, len), "");
246 len = 1;
247 EXPECT_EQ(Base64Encode(toEncode, len), "");
248
249 EXPECT_EQ(Base64Decode(basicString), "");
250 }
251
252 /**
253 * @tc.name: IsInt32_001
254 * @tc.desc: IsInt32
255 * @tc.type: FUNC
256 * @tc.require: I5WKCK
257 */
258 HWTEST_F(DistributedSchedUtilsTest, IsInt32_001, TestSize.Level1)
259 {
260 bool ret = IsInt32(nullptr);
261 EXPECT_FALSE(ret);
262
263 cJSON *paramValue = cJSON_CreateObject();
264 ASSERT_NE(paramValue, nullptr);
265 int32_t data = MAX_TEST_PATH_LEN;
266 cJSON_AddNumberToObject(paramValue, "data", data);
267 ret = IsInt32(paramValue);
268 EXPECT_FALSE(ret);
269 cJSON *dataValue = cJSON_GetObjectItem(paramValue, "data");
270 ret = IsInt32(dataValue);
271 EXPECT_TRUE(ret);
272 if (paramValue != nullptr) {
273 cJSON_Delete(paramValue);
274 paramValue = nullptr;
275 }
276 }
277
278 /**
279 * @tc.name: IsString_001
280 * @tc.desc: IsString
281 * @tc.type: FUNC
282 * @tc.require: I5WKCK
283 */
284 HWTEST_F(DistributedSchedUtilsTest, IsString_001, TestSize.Level1)
285 {
286 bool ret = IsString(nullptr);
287 EXPECT_FALSE(ret);
288
289 cJSON *paramValue = cJSON_CreateString("test string");
290 ASSERT_NE(paramValue, nullptr);
291 ret = IsString(paramValue);
292 EXPECT_TRUE(ret);
293 if (paramValue != nullptr) {
294 cJSON_Delete(paramValue);
295 paramValue = nullptr;
296 }
297 }
298
299 /**
300 * @tc.name: IsString_002
301 * @tc.desc: IsString
302 * @tc.type: FUNC
303 * @tc.require: I5WKCK
304 */
305 HWTEST_F(DistributedSchedUtilsTest, IsString_002, TestSize.Level1)
306 {
307 cJSON *paramValue = cJSON_CreateObject();
308 ASSERT_NE(paramValue, nullptr);
309 int32_t data = MAX_TEST_PATH_LEN;
310 cJSON_AddNumberToObject(paramValue, "data", data);
311 bool ret = IsString(paramValue);
312 EXPECT_FALSE(ret);
313 if (paramValue != nullptr) {
314 cJSON_Delete(paramValue);
315 paramValue = nullptr;
316 }
317 }
318
319 /**
320 * @tc.name: CJsonParamCheck_001
321 * @tc.desc: CJsonParamCheck
322 * @tc.type: FUNC
323 * @tc.require: I5WKCK
324 */
325 HWTEST_F(DistributedSchedUtilsTest, CJsonParamCheck_001, TestSize.Level1)
326 {
327 bool ret = CJsonParamCheck(nullptr, {PARAM_KEY_OS_TYPE, PARAM_KEY_OS_VERSION});
328 EXPECT_FALSE(ret);
329
330 cJSON *jsonObj = cJSON_CreateArray();
331 const std::initializer_list<std::string> keys = {"key1", "key2"};
332 ret = CJsonParamCheck(jsonObj, keys);
333 EXPECT_FALSE(ret);
334 cJSON_Delete(jsonObj);
335 }
336
337 /**
338 * @tc.name: CJsonParamCheck_002
339 * @tc.desc: CJsonParamCheck
340 * @tc.type: FUNC
341 * @tc.require: I5WKCK
342 */
343 HWTEST_F(DistributedSchedUtilsTest, CJsonParamCheck_002, TestSize.Level1)
344 {
345 cJSON *jsonObj = cJSON_CreateObject();
346 const std::initializer_list<std::string> keys = {"key1", "key2"};
347 bool ret = CJsonParamCheck(jsonObj, keys);
348 EXPECT_FALSE(ret);
349 cJSON_Delete(jsonObj);
350
351 cJSON *jsonObj1 = cJSON_CreateObject();
352 cJSON_AddItemToObject(jsonObj1, "key1", cJSON_CreateString("value1"));
353 const std::initializer_list<std::string> keys1 = {"key1", "key2"};
354 ret = CJsonParamCheck(jsonObj1, keys1);
355 EXPECT_FALSE(ret);
356 cJSON_Delete(jsonObj1);
357 }
358
359 /**
360 * @tc.name: CJsonParamCheck_003
361 * @tc.desc: CJsonParamCheck
362 * @tc.type: FUNC
363 * @tc.require: I5WKCK
364 */
365 HWTEST_F(DistributedSchedUtilsTest, CJsonParamCheck_003, TestSize.Level1)
366 {
367 cJSON *jsonObj = cJSON_CreateObject();
368 cJSON_AddItemToObject(jsonObj, PARAM_KEY_OS_TYPE, cJSON_CreateString("value1"));
369 cJSON_AddItemToObject(jsonObj, PARAM_KEY_OS_VERSION, cJSON_CreateString("value2"));
370 const std::initializer_list<std::string> keys = {PARAM_KEY_OS_TYPE, PARAM_KEY_OS_VERSION};
371 bool ret = CJsonParamCheck(jsonObj, keys);
372 EXPECT_FALSE(ret);
373 cJSON_Delete(jsonObj);
374
375 cJSON *jsonObj1 = cJSON_CreateObject();
376 cJSON_AddItemToObject(jsonObj1, PARAM_KEY_OS_TYPE, cJSON_CreateNumber(1));
377 cJSON_AddItemToObject(jsonObj1, PARAM_KEY_OS_VERSION, cJSON_CreateString("value2"));
378 const std::initializer_list<std::string> keys1 = {PARAM_KEY_OS_TYPE, PARAM_KEY_OS_VERSION};
379 ret = CJsonParamCheck(jsonObj1, keys1);
380 EXPECT_TRUE(ret);
381 cJSON_Delete(jsonObj1);
382 }
383
384 /**
385 * @tc.name: GetOsInfoFromDM_001
386 * @tc.desc: GetOsInfoFromDM
387 * @tc.type: FUNC
388 * @tc.require: I5WKCK
389 */
390 HWTEST_F(DistributedSchedUtilsTest, GetOsInfoFromDM_001, TestSize.Level1)
391 {
392 std::string dmInfoEx;
393 int32_t osType;
394 std::string osVersion;
395 bool ret = GetOsInfoFromDM(dmInfoEx, osType, osVersion);
396 EXPECT_FALSE(ret);
397 }
398
399 /**
400 * @tc.name: GetOsInfoFromDM_002
401 * @tc.desc: GetOsInfoFromDM
402 * @tc.type: FUNC
403 * @tc.require: I5WKCK
404 */
405 HWTEST_F(DistributedSchedUtilsTest, GetOsInfoFromDM_002, TestSize.Level1)
406 {
407 std::string dmInfoEx = "{\"osType\":\"1\",\"osVersion\":\"1.0\"}";
408 int32_t osType;
409 std::string osVersion;
410 bool ret = GetOsInfoFromDM(dmInfoEx, osType, osVersion);
411 EXPECT_FALSE(ret);
412 }
413 } // namespace DistributedSchedule
414 } // namespace OHOS
415