1 /**
2 * Copyright (c) 2021-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 <string.h>
17 #include <unistd.h>
18
19 #include "adapter.h"
20 #include "hctest.h"
21 #include "securec.h"
22 #include "bundle_info.h"
23 #include "bundle_manager.h"
24 #include "want.h"
25
26 #define nullptr NULL
27
28 /**
29 * @brief register a test suit named BundleMgrTestSuite
30 * @param subsystem name is appexecfwk
31 * @param module name is bundlemgr
32 * @param test suit name is BundleMgrTestSuite
33 */
34 LITE_TEST_SUIT(appexecfwk, bundlemgr, BundleMgrTestSuite);
35
BundleMgrTestSuiteSetUp(void)36 static BOOL BundleMgrTestSuiteSetUp(void)
37 {
38 printf("----------test case with BundleMgrTest start-------------\n");
39 return TRUE;
40 }
41
BundleMgrTestSuiteTearDown(void)42 static BOOL BundleMgrTestSuiteTearDown(void)
43 {
44 printf("----------test case with BundleMgrTest end-------------\n");
45 return TRUE;
46 }
47
48 /**
49 * @tc.number : SUB_APPEXECFWK_0001
50 * @tc.name : testClearAbilityInfoLegal
51 * @tc.desc : testClearAbilityInfo parameter legal test with bundle name
52 */
53 LITE_TEST_CASE(BundleMgrTestSuite, testClearAbilityInfoLegal, Function | MediumTest | Level2)
54 {
55 printf("------start testClearAbilityInfo------\n");
56 AbilityInfo abilityInfo;
57 int result = memset_s(&abilityInfo, sizeof(abilityInfo), 0, sizeof(abilityInfo));
58 TEST_ASSERT_TRUE(result == 0);
59 char *name = "com.openharmony.testjsdemo";
60 size_t len = strlen(name);
61 abilityInfo.bundleName = (char *)(AdapterMalloc(len + 1));
62 TEST_ASSERT_NOT_NULL(abilityInfo.bundleName);
63 errno_t err = strncpy_s(abilityInfo.bundleName, len + 1, name, len);
64 TEST_ASSERT_EQUAL(err, EOK);
65 TEST_ASSERT_EQUAL_STRING(abilityInfo.bundleName, name);
66 ClearAbilityInfo(&abilityInfo);
67 TEST_ASSERT_EQUAL_STRING(abilityInfo.bundleName, NULL);
68 printf("------end testClearAbilityInfo------\n");
69 }
70
71 /**
72 * @tc.number : SUB_APPEXECFWK_0002
73 * @tc.name : testClearAbilityInfoIllegal
74 * @tc.desc : testClearAbilityInfo parameter illegal test
75 */
76 LITE_TEST_CASE(BundleMgrTestSuite, testClearAbilityInfoIllegal, Function | MediumTest | Level2)
77 {
78 printf("------start testClearAbilityInfoIllegal------\n");
79 AbilityInfo abilityInfo = { 0 };
80 int result = memset_s(&abilityInfo, sizeof(abilityInfo), 0, sizeof(abilityInfo));
81 TEST_ASSERT_TRUE(result == 0);
82 char *name = "com.openharmony.testjsdemo";
83 size_t len = strlen(name);
84 abilityInfo.bundleName = (char *)(AdapterMalloc(len + 1));
85 TEST_ASSERT_NOT_NULL(abilityInfo.bundleName);
86 errno_t err = strncpy_s(abilityInfo.bundleName, len + 1, name, len);
87 TEST_ASSERT_EQUAL(err, EOK);
88 ClearAbilityInfo(NULL);
89 TEST_ASSERT_EQUAL_STRING(abilityInfo.bundleName, name);
90 AdapterFree(abilityInfo.bundleName);
91 printf("------end testClearAbilityInfoIllegal------\n");
92 }
93
94 /**
95 * @tc.number : SUB_APPEXECFWK_0003
96 * @tc.name : testClearBundleInfoLegal
97 * @tc.desc : testClearBundleInfo parameter legal test with bundle name
98 */
99 LITE_TEST_CASE(BundleMgrTestSuite, testClearBundleInfoLegal, Function | MediumTest | Level2)
100 {
101 printf("------start testClearBundleInfo------\n");
102 BundleInfo bundleInfo = { 0 };
103 int result = memset_s(&bundleInfo, sizeof(bundleInfo), 0, sizeof(bundleInfo));
104 TEST_ASSERT_TRUE(result == 0);
105 char *name = "com.openharmony.testjsdemo";
106 size_t len = strlen(name);
107 bundleInfo.bundleName = (char *)(AdapterMalloc(len + 1));
108 TEST_ASSERT_NOT_NULL(bundleInfo.bundleName);
109 errno_t err = strncpy_s(bundleInfo.bundleName, len + 1, name, len);
110 TEST_ASSERT_EQUAL(err, EOK);
111 TEST_ASSERT_EQUAL_STRING(bundleInfo.bundleName, name);
112 ClearBundleInfo(&bundleInfo);
113 TEST_ASSERT_EQUAL_STRING(bundleInfo.bundleName, NULL);
114 printf("------end testClearBundleInfo------\n");
115 }
116
117 /**
118 * @tc.number : SUB_APPEXECFWK_0004
119 * @tc.name : testClearBundleInfoIllegal
120 * @tc.desc : testClearBundleInfo parameter illegal test
121 */
122 LITE_TEST_CASE(BundleMgrTestSuite, testClearBundleInfoIllegal, Function | MediumTest | Level2)
123 {
124 printf("------start testClearBundleInfoIllegal------\n");
125 BundleInfo bundleInfo;
126 int result = memset_s(&bundleInfo, sizeof(bundleInfo), 0, sizeof(bundleInfo));
127 TEST_ASSERT_TRUE(result == 0);
128 char *name = "com.openharmony.testjsdemo";
129 size_t len = strlen(name);
130 bundleInfo.bundleName = (char *)(AdapterMalloc(len + 1));
131 TEST_ASSERT_NOT_NULL(bundleInfo.bundleName);
132 errno_t err = strncpy_s(bundleInfo.bundleName, len + 1, name, len);
133 TEST_ASSERT_EQUAL(err, EOK);
134 ClearBundleInfo(NULL);
135 TEST_ASSERT_EQUAL_STRING(bundleInfo.bundleName, name);
136 AdapterFree(bundleInfo.bundleName);
137 printf("------end testClearBundleInfoIllegal------\n");
138 }
139
140 /**
141 * @tc.number : SUB_APPEXECFWK_0005
142 * @tc.name : testClearModuleInfoLegal
143 * @tc.desc : ClearAbilityInfo parameter legal test with module info
144 */
145 LITE_TEST_CASE(BundleMgrTestSuite, testClearModuleInfoLegal, Function | MediumTest | Level1)
146 {
147 printf("------start testClearModuleInfo------\n");
148 ModuleInfo moduleInfo = { 0 };
149 int result = memset_s(&moduleInfo, sizeof(moduleInfo), 0, sizeof(moduleInfo));
150 TEST_ASSERT_TRUE(result == 0);
151 char *name = "test";
152 size_t len = strlen(name);
153 moduleInfo.moduleName = (char *)(AdapterMalloc(len + 1));
154 TEST_ASSERT_NOT_NULL(moduleInfo.moduleName);
155 errno_t err = strncpy_s(moduleInfo.moduleName, len + 1, name, len);
156 TEST_ASSERT_EQUAL(err, EOK);
157 TEST_ASSERT_EQUAL_STRING(moduleInfo.moduleName, name);
158 ClearModuleInfo(&moduleInfo);
159 TEST_ASSERT_EQUAL_STRING(moduleInfo.moduleName, NULL);
160 printf("------end testClearModuleInfo------\n");
161 }
162
163 /**
164 * @tc.number : SUB_APPEXECFWK_0006
165 * @tc.name : testClearModuleInfoIllegal
166 * @tc.desc : ClearAbilityInfo parameter illegal test
167 */
168 LITE_TEST_CASE(BundleMgrTestSuite, testClearModuleInfoIllegal, Function | MediumTest | Level1)
169 {
170 printf("------start testClearModuleInfoIllegal------\n");
171 ModuleInfo moduleInfo = { 0 };
172 int result = memset_s(&moduleInfo, sizeof(moduleInfo), 0, sizeof(moduleInfo));
173 TEST_ASSERT_TRUE(result == 0);
174 char *name = "test";
175 size_t len = strlen(name);
176 moduleInfo.moduleName = (char *)(AdapterMalloc(len + 1));
177 TEST_ASSERT_NOT_NULL(moduleInfo.moduleName);
178 errno_t err = strncpy_s(moduleInfo.moduleName, len + 1, name, len);
179 TEST_ASSERT_EQUAL(err, EOK);
180 ClearModuleInfo(NULL);
181 TEST_ASSERT_EQUAL_STRING(moduleInfo.moduleName, name);
182 AdapterFree(moduleInfo.moduleName);
183 printf("------end testClearModuleInfoIllegal------\n");
184 }
185
186 /**
187 * @tc.number : SUB_APPEXECFWK_0007
188 * @tc.name : testSetElementAbilityNameLegal
189 * @tc.desc : testSetElementAbilityName parameter legal test
190 */
191 LITE_TEST_CASE(BundleMgrTestSuite, testSetElementAbilityNameLegal, Function | MediumTest | Level0)
192 {
193 printf("------start testSetElementAbilityName------\n");
194 Want want = { 0 };
195 ElementName element = { 0 };
196 SetElementAbilityName(&element, "SecondAbility");
197 SetWantElement(&want, element);
198 char *aName = "SecondAbility";
199 TEST_ASSERT_EQUAL_STRING(want.element->abilityName, aName);
200 ClearElement(&element);
201 ClearWant(&want);
202 printf("------end testSetElementAbilityName------\n");
203 }
204
205 /**
206 * @tc.number : SUB_APPEXECFWK_0008
207 * @tc.name : testSetElementAbilityNameIllegal
208 * @tc.desc : testSetElementAbilityName parameter illegal test
209 */
210 LITE_TEST_CASE(BundleMgrTestSuite, testSetElementAbilityNameIllegal, Function | MediumTest | Level2)
211 {
212 printf("------start testSetElementAbilityNameIllegal------\n");
213 Want want = { 0 };
214 ElementName element = { 0 };
215 char *aName = "";
216 SetElementAbilityName(&element, aName);
217 SetWantElement(&want, element);
218 TEST_ASSERT_EQUAL_STRING(want.element->abilityName, "");
219 SetElementAbilityName(&element, NULL);
220 SetWantElement(&want, element);
221 TEST_ASSERT_EQUAL_STRING(want.element->abilityName, NULL);
222 int ret = SetElementAbilityName(NULL, aName);
223 TEST_ASSERT_FALSE(ret);
224 ClearElement(&element);
225 ClearWant(&want);
226 printf("------end testSetElementAbilityNameIllegal------\n");
227 }
228
229 /**
230 * @tc.number : SUB_APPEXECFWK_0009
231 * @tc.name : testSetElementBundleNameLegal
232 * @tc.desc : testSetElementBundleName parameter legal test
233 */
234 LITE_TEST_CASE(BundleMgrTestSuite, testSetElementBundleNameLegal, Function | MediumTest | Level0)
235 {
236 printf("------start testSetElementBundleName------\n");
237 Want want = { 0 };
238 ElementName element = { 0 };
239 SetElementBundleName(&element, "com.openharmony.testjsdemo");
240 SetWantElement(&want, element);
241 char *bName = "com.openharmony.testjsdemo";
242 TEST_ASSERT_EQUAL_STRING(want.element->bundleName, bName);
243 ClearElement(&element);
244 ClearWant(&want);
245 printf("------end testSetElementBundleName------\n");
246 }
247
248 /**
249 * @tc.number : SUB_APPEXECFWK_0010
250 * @tc.name : testSetElementBundleNameIllegal
251 * @tc.desc : testSetElementBundleName parameter illegal test
252 */
253 LITE_TEST_CASE(BundleMgrTestSuite, testSetElementBundleNameIllegal, Function | MediumTest | Level2)
254 {
255 printf("------start testSetElementBundleNameIllegal------\n");
256 Want want = { 0 };
257 ElementName element = { 0 };
258 SetElementBundleName(&element, "");
259 SetWantElement(&want, element);
260 char *bName = "";
261 TEST_ASSERT_EQUAL_STRING(want.element->bundleName, bName);
262 SetElementBundleName(&element, NULL);
263 SetWantElement(&want, element);
264 TEST_ASSERT_EQUAL_STRING(want.element->bundleName, NULL);
265 bool ret = SetElementBundleName(NULL, bName);
266 TEST_ASSERT_FALSE(ret);
267 ClearElement(&element);
268 ClearWant(&want);
269 printf("------end testSetElementBundleNameIllegal------\n");
270 }
271 /**
272 * @tc.number : SUB_APPEXECFWK_0011
273 * @tc.name : testSetElementDeviceIDLegal
274 * @tc.desc : testSetElementDeviceID parameter legal test
275 */
276 LITE_TEST_CASE(BundleMgrTestSuite, testSetElementDeviceIDLegal, Function | MediumTest | Level0)
277 {
278 printf("------start testSetElementDeviceID------\n");
279 Want want = { 0 };
280 ElementName element = { 0 };
281 SetElementDeviceID(&element, "0001000");
282 SetWantElement(&want, element);
283 char *dID = "0001000";
284 TEST_ASSERT_EQUAL_STRING(want.element->deviceId, dID);
285 ClearElement(&element);
286 ClearWant(&want);
287 printf("------end testSetElementDeviceID------\n");
288 }
289
290 /**
291 * @tc.number : SUB_APPEXECFWK_0012
292 * @tc.name : testSetElementDeviceIDIllegal
293 * @tc.desc : testSetElementDeviceID parameter illegal test
294 */
295 LITE_TEST_CASE(BundleMgrTestSuite, testSetElementDeviceIDIllegal, Function | MediumTest | Level2)
296 {
297 printf("------start testSetElementDeviceIDIllegal------\n");
298 Want want = { 0 };
299 ElementName element = { 0 };
300 SetElementDeviceID(&element, "");
301 SetWantElement(&want, element);
302 char *dID = "";
303 TEST_ASSERT_EQUAL_STRING(want.element->deviceId, dID);
304 SetElementDeviceID(&element, NULL);
305 SetWantElement(&want, element);
306 TEST_ASSERT_EQUAL_STRING(want.element->deviceId, NULL);
307 int ret = SetElementDeviceID(NULL, "0001000");
308 TEST_ASSERT_FALSE(ret);
309 ClearElement(&element);
310 ClearWant(&want);
311 printf("------end testSetElementDeviceIDIllegal------\n");
312 }
313
314 /**
315 * @tc.number : SUB_APPEXECFWK_0013
316 * @tc.name : testGetBundleInfoIllegal
317 * @tc.desc : GetBundleInfo parameter illegal test
318 */
319 LITE_TEST_CASE(BundleMgrTestSuite, testGetBundleInfoIllegal, Function | MediumTest | Level2)
320 {
321 printf("------start testGetBundleInfoIllegal------\n");
322 BundleInfo bundleInfo;
323 int result = memset_s(&bundleInfo, sizeof(bundleInfo), 0, sizeof(bundleInfo));
324 TEST_ASSERT_TRUE(result == 0);
325 const char *bundleName = "com.openharmony.nothishap";
326 int flags = 0;
327 uint8_t ret = GetBundleInfo(bundleName, flags, &bundleInfo);
328 TEST_ASSERT_TRUE(ret == 2);
329 ret = GetBundleInfo(NULL, flags, &bundleInfo);
330 printf("abilityInfo2 is %d \n", ret);
331 TEST_ASSERT_TRUE(ret == 1);
332 ret = GetBundleInfo("", flags, &bundleInfo);
333 TEST_ASSERT_TRUE(ret == 2);
334 ret = GetBundleInfo("com.openharmony.testjsdemo", 2, &bundleInfo);
335 sleep(2);
336 TEST_ASSERT_TRUE(ret == 2);
337 printf("------end testGetBundleInfoIllegal------\n");
338 }
339
340 /**
341 * @tc.number : SUB_APPEXECFWK_0014
342 * @tc.name : testGetBundleInfosIllegal
343 * @tc.desc : GetBundleInfos parameter illegal test
344 */
345 LITE_TEST_CASE(BundleMgrTestSuite, testGetBundleInfosIllegal, Function | MediumTest | Level2)
346 {
347 printf("------start testGetBundleInfosIllegal------\n");
348 BundleInfo *bundleInfos = {NULL};
349 int *length = NULL;
350 int flags = 0;
351 uint8_t ret = GetBundleInfos(flags, NULL, length);
352 TEST_ASSERT_TRUE(ret == 1);
353 ret = GetBundleInfos(flags, &bundleInfos, NULL);
354 printf("ret is %d \n", ret);
355 TEST_ASSERT_TRUE(ret == 2);
356 ret = GetBundleInfos(2, &bundleInfos, length);
357 printf("ret is %d \n", ret);
358 TEST_ASSERT_TRUE(ret == 2);
359 printf("------end testGetBundleInfosIllegal------\n");
360 }
361
362 RUN_TEST_SUITE(BundleMgrTestSuite);
363