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