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  */
HWTEST_F(DistributedSchedUtilsTest, IsValidPath_001, TestSize.Level1)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  */
HWTEST_F(DistributedSchedUtilsTest, IsValidPath_002, TestSize.Level1)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: CheckBundleContinueConfig_001
95  * @tc.desc: Check bundle continue config when existing config file
96  * @tc.type: FUNC
97  * @tc.require: I5WKCK
98  */
HWTEST_F(DistributedSchedUtilsTest, CheckBundleContinueConfig_001, TestSize.Level1)99 HWTEST_F(DistributedSchedUtilsTest, CheckBundleContinueConfig_001, TestSize.Level1)
100 {
101     std::string cfgJsonStr = R"({
102         "allow_applist":["test_bundle_0"]
103     })";
104     EXPECT_TRUE(UpdateAllowAppList(cfgJsonStr));
105 
106     std::string bundleName = "test";
107     EXPECT_FALSE(CheckBundleContinueConfig(bundleName));
108 
109     bundleName = "test_bundle_0";
110     EXPECT_TRUE(CheckBundleContinueConfig(bundleName));
111 }
112 
113 /**
114  * @tc.name: UpdateAllowAppList_001
115  * @tc.desc: Update allow app list with invalid cfgJsonStr
116  * @tc.type: FUNC
117  * @tc.require: I5WKCK
118  */
HWTEST_F(DistributedSchedUtilsTest, UpdateAllowAppList_001, TestSize.Level1)119 HWTEST_F(DistributedSchedUtilsTest, UpdateAllowAppList_001, TestSize.Level1)
120 {
121     std::string cfgJsonStr = "";
122     EXPECT_FALSE(UpdateAllowAppList(cfgJsonStr));
123 
124     cfgJsonStr = "12345";
125     EXPECT_FALSE(UpdateAllowAppList(cfgJsonStr));
126 
127     cfgJsonStr = R"({
128         "Name":["test_one"],
129         "ID":"12345"
130     })";
131     EXPECT_FALSE(UpdateAllowAppList(cfgJsonStr));
132 
133     cfgJsonStr = R"({
134         "allow_applist":"12345"
135     })";
136     EXPECT_FALSE(UpdateAllowAppList(cfgJsonStr));
137 }
138 
139 /**
140  * @tc.name: UpdateAllowAppList_002
141  * @tc.desc: Update allow app list with valid cfgJsonStr
142  * @tc.type: FUNC
143  * @tc.require: I5WKCK
144  */
HWTEST_F(DistributedSchedUtilsTest, UpdateAllowAppList_002, TestSize.Level1)145 HWTEST_F(DistributedSchedUtilsTest, UpdateAllowAppList_002, TestSize.Level1)
146 {
147     std::string cfgJsonStr = R"({
148         "allow_applist":["test_bundle_1"]
149     })";
150     EXPECT_TRUE(UpdateAllowAppList(cfgJsonStr));
151 }
152 
153 /**
154  * @tc.name: LoadContinueConfig_001
155  * @tc.desc: Load continue config success
156  * @tc.type: FUNC
157  * @tc.require: I5WKCK
158  */
HWTEST_F(DistributedSchedUtilsTest, LoadContinueConfig_001, TestSize.Level1)159 HWTEST_F(DistributedSchedUtilsTest, LoadContinueConfig_001, TestSize.Level1)
160 {
161     EXPECT_EQ(ERR_OK, LoadContinueConfig());
162     EXPECT_EQ(ERR_OK, LoadContinueConfig());
163 }
164 
165 /**
166  * @tc.name: CheckBundleContinueConfig_002
167  * @tc.desc: Check bundle continue config when missing config file
168  * @tc.type: FUNC
169  * @tc.require: I5WKCK
170  */
HWTEST_F(DistributedSchedUtilsTest, CheckBundleContinueConfig_002, TestSize.Level1)171 HWTEST_F(DistributedSchedUtilsTest, CheckBundleContinueConfig_002, TestSize.Level1)
172 {
173     EXPECT_EQ(ERR_OK, LoadContinueConfig());
174 
175     std::string bundleName = "test_bundle_1";
176     EXPECT_TRUE(CheckBundleContinueConfig(bundleName));
177 }
178 
179 /**
180  * @tc.name: ParcelToBase64Str_001
181  * @tc.desc: ParcelToBase64Str
182  * @tc.type: FUNC
183  * @tc.require: I5WKCK
184  */
HWTEST_F(DistributedSchedUtilsTest, ParcelToBase64Str_001, TestSize.Level1)185 HWTEST_F(DistributedSchedUtilsTest, ParcelToBase64Str_001, TestSize.Level1)
186 {
187     EXPECT_EQ(INVALID_MISSION_ID, GetCurrentMissionId());
188 
189     Parcel parcel;
190     std::string rawStr;
191     EXPECT_EQ("", ParcelToBase64Str(parcel));
192 
193     EXPECT_EQ(INVALID_PARAMETERS_ERR, Base64StrToParcel(rawStr, parcel));
194 
195     unsigned char *toEncode = nullptr;
196     unsigned int len = 0;
197     EXPECT_EQ("", Base64Encode(toEncode, len));
198 
199     len = 1;
200     EXPECT_EQ("", Base64Encode(toEncode, len));
201 
202     std::string basicString;
203     EXPECT_EQ("", Base64Decode(basicString));
204 
205     EXPECT_EQ(true, IsBase64('+'));
206     EXPECT_EQ(true, IsBase64('/'));
207     EXPECT_EQ(true, IsBase64('3'));
208 }
209 
210 /**
211  * @tc.name: Base64Encode_001
212  * @tc.desc: Base64Encode
213  * @tc.type: FUNC
214  * @tc.require: I5WKCK
215  */
HWTEST_F(DistributedSchedUtilsTest, Base64Encode_001, TestSize.Level1)216 HWTEST_F(DistributedSchedUtilsTest, Base64Encode_001, TestSize.Level1)
217 {
218     unsigned char *toEncode = nullptr;
219     unsigned int len = 0;
220     std::string basicString;
221 
222     EXPECT_EQ(Base64Encode(toEncode, len), "");
223     len = 1;
224     EXPECT_EQ(Base64Encode(toEncode, len), "");
225 
226     EXPECT_EQ(Base64Decode(basicString), "");
227 }
228 
229 /**
230  * @tc.name: IsInt32_001
231  * @tc.desc: IsInt32
232  * @tc.type: FUNC
233  * @tc.require: I5WKCK
234  */
HWTEST_F(DistributedSchedUtilsTest, IsInt32_001, TestSize.Level1)235 HWTEST_F(DistributedSchedUtilsTest, IsInt32_001, TestSize.Level1)
236 {
237     bool ret = IsInt32(nullptr);
238     EXPECT_FALSE(ret);
239 
240     cJSON *paramValue = cJSON_CreateObject();
241     if (paramValue == nullptr) {
242         return;
243     }
244     int32_t data = MAX_TEST_PATH_LEN;
245     cJSON_AddNumberToObject(paramValue, "data", data);
246     ret = IsInt32(paramValue);
247     EXPECT_FALSE(ret);
248     cJSON *dataValue = cJSON_GetObjectItem(paramValue, "data");
249     ret = IsInt32(dataValue);
250     EXPECT_TRUE(ret);
251     if (paramValue != nullptr) {
252         cJSON_Delete(paramValue);
253         paramValue = nullptr;
254     }
255 }
256 
257 /**
258  * @tc.name: IsString_001
259  * @tc.desc: IsString
260  * @tc.type: FUNC
261  * @tc.require: I5WKCK
262  */
HWTEST_F(DistributedSchedUtilsTest, IsString_001, TestSize.Level1)263 HWTEST_F(DistributedSchedUtilsTest, IsString_001, TestSize.Level1)
264 {
265     bool ret = IsString(nullptr);
266     EXPECT_FALSE(ret);
267 
268     cJSON *paramValue = nullptr;
269     std::string str("string");
270     paramValue = cJSON_Parse(str.c_str());
271     if (paramValue == nullptr) {
272         return;
273     }
274     ret = IsString(paramValue);
275     EXPECT_TRUE(ret);
276     if (paramValue != nullptr) {
277         cJSON_Delete(paramValue);
278         paramValue = nullptr;
279     }
280 }
281 
282 /**
283  * @tc.name: IsString_002
284  * @tc.desc: IsString
285  * @tc.type: FUNC
286  * @tc.require: I5WKCK
287  */
HWTEST_F(DistributedSchedUtilsTest, IsString_002, TestSize.Level1)288 HWTEST_F(DistributedSchedUtilsTest, IsString_002, TestSize.Level1)
289 {
290     cJSON *paramValue = cJSON_CreateObject();
291     if (paramValue == nullptr) {
292         return;
293     }
294     int32_t data = MAX_TEST_PATH_LEN;
295     cJSON_AddNumberToObject(paramValue, "data", data);
296     bool ret = IsString(paramValue);
297     EXPECT_FALSE(ret);
298     if (paramValue != nullptr) {
299         cJSON_Delete(paramValue);
300         paramValue = nullptr;
301     }
302 }
303 
304 /**
305  * @tc.name: CJsonParamCheck_001
306  * @tc.desc: CJsonParamCheck
307  * @tc.type: FUNC
308  * @tc.require: I5WKCK
309  */
HWTEST_F(DistributedSchedUtilsTest, CJsonParamCheck_001, TestSize.Level1)310 HWTEST_F(DistributedSchedUtilsTest, CJsonParamCheck_001, TestSize.Level1)
311 {
312     bool ret = CJsonParamCheck(nullptr, {PARAM_KEY_OS_TYPE, PARAM_KEY_OS_VERSION});
313     EXPECT_FALSE(ret);
314 }
315 
316 /**
317  * @tc.name: GetOsInfoFromDM_001
318  * @tc.desc: GetOsInfoFromDM
319  * @tc.type: FUNC
320  * @tc.require: I5WKCK
321  */
HWTEST_F(DistributedSchedUtilsTest, GetOsInfoFromDM_001, TestSize.Level1)322 HWTEST_F(DistributedSchedUtilsTest, GetOsInfoFromDM_001, TestSize.Level1)
323 {
324     std::string dmInfoEx;
325     int32_t osType;
326     std::string osVersion;
327     bool ret = GetOsInfoFromDM(dmInfoEx, osType, osVersion);
328     EXPECT_FALSE(ret);
329 }
330 } // namespace DistributedSchedule
331 } // namespace OHOS
332