1 /**
2 * Copyright (c) 2020-2021 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 <log.h>
17 #include <semaphore.h>
18 #include <string>
19
20 #include "gtest/gtest.h"
21 #include "securec.h"
22 #include "bundle_info.h"
23 #include "bundle_manager.h"
24 #include "want.h"
25
26 using namespace std;
27 using namespace testing::ext;
28 using namespace OHOS;
29 static bool g_installState = false;
30 static int g_errorCode = -1;
31 static sem_t g_sem;
32 static string g_testPath;
33
34 extern "C" {
HOS_SystemInit(void)35 void __attribute__((weak)) HOS_SystemInit(void) { }
36 }
37
38 /* callback */
TestBundleStateCallback(const uint8_t resultCode,const void * resultMessage)39 static void TestBundleStateCallback(const uint8_t resultCode, const void *resultMessage)
40 {
41 HILOG_DEBUG(HILOG_MODULE_APP, "TestBundleStateCallback resultCode: %d", resultCode);
42 HILOG_DEBUG(HILOG_MODULE_APP, "TestBundleStateCallback resultMessage: %s", (char *) resultMessage);
43 g_installState = (resultCode == 0);
44 g_errorCode = resultCode;
45 sem_post(&g_sem);
46 }
47
48 /* *
49 * get current dir
50 * @return string current file path of the test suits
51 */
GetCurDir()52 static string GetCurDir()
53 {
54 string filePath = "";
55 char *buffer;
56 if ((buffer = getcwd(NULL, 0)) == NULL) {
57 perror("get file path error");
58 } else {
59 printf("Current Dir: %s\r\n", buffer);
60 filePath = buffer;
61 free(buffer);
62 }
63 return filePath + "/";
64 }
65
66 class BundleMgrTest : public testing::Test {
67 protected:
SetUpTestCase(void)68 static void SetUpTestCase(void)
69 {
70 printf("----------test case with BundleMgrTest start-------------\n");
71 HOS_SystemInit();
72 sem_init(&g_sem, 0, 0);
73 InstallParam installParam = { .installLocation = 1, .keepData = false };
74 g_testPath = GetCurDir();
75 string hapPath = g_testPath + "testjsdemo.hap";
76 Install(hapPath.c_str(), &installParam, TestBundleStateCallback);
77 sem_wait(&g_sem);
78 printf("callback installresult is %d \n", g_errorCode);
79 EXPECT_EQ(g_errorCode, 0);
80 }
TearDownTestCase(void)81 static void TearDownTestCase(void)
82 {
83 sem_init(&g_sem, 0, 0);
84 InstallParam installParam = { .installLocation = 1, .keepData = false };
85 Uninstall("com.openharmony.testjsdemo", &installParam, TestBundleStateCallback);
86 sem_wait(&g_sem);
87 printf("callback uninstallresult is %d \n", g_errorCode);
88 EXPECT_EQ(g_errorCode, 0);
89 printf("----------test case with BundleMgrTest end-------------\n");
90 }
91 };
92
93
94 /**
95 * @tc.number : SUB_APPEXECFWK_BMS_API_0044
96 * @tc.name : ClearAbilityInfo parameter illegal test
97 * @tc.desc : [C- SOFTWARE -0200]
98 */
99 HWTEST_F(BundleMgrTest, testClearAbilityInfoIllegal, Function | MediumTest | Level2)
100 {
101 printf("------start testClearAbilityInfoIllegal------\n");
102 // abilityInfo is nullptr
103 AbilityInfo abilityInfo;
104 int32_t result = memset_s(&abilityInfo, sizeof(abilityInfo), 0, sizeof(abilityInfo));
105 EXPECT_EQ(result, 0);
106 abilityInfo.bundleName = (char*)"com.openharmony.testjsdemo";
107 printf("abilityInfo.bundleName is %s \n", abilityInfo.bundleName);
108 ClearAbilityInfo(nullptr);
109 EXPECT_STREQ(abilityInfo.bundleName, "com.openharmony.testjsdemo");
110 printf("------end testClearAbilityInfoIllegal------\n");
111 }
112
113 /**
114 * @tc.number : SUB_APPEXECFWK_BMS_API_0045
115 * @tc.name : ClearAbilityInfo parameter legal test with bundle name
116 * @tc.desc : [C- SOFTWARE -0200]
117 */
118 HWTEST_F(BundleMgrTest, testClearBundleInfoIllegal, Function | MediumTest | Level2)
119 {
120 printf("------start testClearBundleInfoIllegal------\n");
121 BundleInfo bundleInfo;
122 int32_t result = memset_s(&bundleInfo, sizeof(bundleInfo), 0, sizeof(bundleInfo));
123 EXPECT_EQ(result, 0);
124 bundleInfo.bundleName = (char*)"com.openharmony.testjsdemo";
125 printf("abilityInfo.bundleName is %s \n", bundleInfo.bundleName);
126 ClearBundleInfo(nullptr);
127 printf("abilityInfo.bundleName afterclear is %s \n", bundleInfo.bundleName);
128 EXPECT_STREQ(bundleInfo.bundleName, "com.openharmony.testjsdemo");
129 printf("------end testClearBundleInfoIllegal------\n");
130 }
131
132 /**
133 * @tc.number : SUB_APPEXECFWK_BMS_API_0046
134 * @tc.name : ClearAbilityInfo parameter legal test with module info
135 * @tc.desc : [C- SOFTWARE -0200]
136 */
137 HWTEST_F(BundleMgrTest, testClearModuleInfoIllegal, Function | MediumTest | Level1)
138 {
139 printf("------start testClearModuleInfoIllegal------\n");
140 ModuleInfo moduleInfo;
141 int32_t result = memset_s(&moduleInfo, sizeof(moduleInfo), 0, sizeof(moduleInfo));
142 EXPECT_EQ(result, 0);
143 moduleInfo.description = (char*)"test app";
144 moduleInfo.moduleType = (char*)"entry";
145 ClearModuleInfo(nullptr);
146 EXPECT_STREQ(moduleInfo.description, "test app");
147 EXPECT_STREQ(moduleInfo.moduleType, "entry");
148 printf("------end testClearModuleInfoIllegal------\n");
149 }
150
151 /**
152 * @tc.number : SUB_APPEXECFWK_AMS_API_0009
153 * @tc.name : testAbilityMgrSetWantElement parameter legal test
154 * @tc.desc : [C- SOFTWARE -0100]
155 */
156 HWTEST_F(BundleMgrTest, testSetElementAbilityName, Function | MediumTest | Level0)
157 {
158 printf("------start testSetElementAbilityName------\n");
159 Want want = { nullptr };
160 ElementName element = { nullptr };
161 SetElementAbilityName(&element, "SecondAbility");
162 SetWantElement(&want, element);
163 printf("element is %s \n", want.element->abilityName);
164 char aName[] = "SecondAbility";
165 printf("aName is %s \n", aName);
166 EXPECT_STREQ(want.element->abilityName, aName);
167 ClearElement(&element);
168 ClearWant(&want);
169 printf("------end testSetElementAbilityName------\n");
170 }
171
172 /**
173 * @tc.number : SUB_APPEXECFWK_AMS_API_0010
174 * @tc.name : testSetElementAbilityName parameter illegal test
175 * @tc.desc : [C- SOFTWARE -0100]
176 */
177 HWTEST_F(BundleMgrTest, testSetElementAbilityNameIllegal, Function | MediumTest | Level2)
178 {
179 printf("------start testSetElementAbilityNameIllegal------\n");
180 Want want = { nullptr };
181 ElementName element = { nullptr };
182 SetElementAbilityName(&element, nullptr);
183 SetWantElement(&want, element);
184 printf("AbilityName1 is %s \n", want.element->abilityName);
185 EXPECT_STREQ(want.element->abilityName, nullptr);
186 char aName[] = "";
187 SetElementAbilityName(&element, aName);
188 SetWantElement(&want, element);
189 printf("AbilityName2 is %s \n", want.element->abilityName);
190 EXPECT_STREQ(want.element->abilityName, "");
191 ClearElement(&element);
192 ClearWant(&want);
193 printf("------end testSetElementAbilityNameIllegal------\n");
194 }
195
196 /**
197 * @tc.number : SUB_APPEXECFWK_AMS_API_0007
198 * @tc.name : testSetElementBundleName parameter legal test
199 * @tc.desc : [C- SOFTWARE -0100]
200 */
201 HWTEST_F(BundleMgrTest, testSetElementBundleName, Function | MediumTest | Level0)
202 {
203 printf("------start testSetElementBundleName------\n");
204 Want want = { nullptr };
205 ElementName element = { nullptr };
206 SetElementBundleName(&element, "com.openharmony.testjsdemo");
207 SetWantElement(&want, element);
208 printf("element is %s \n", want.element->bundleName);
209 char bName[] = "com.openharmony.testjsdemo";
210 EXPECT_STREQ(want.element->bundleName, bName);
211 ClearElement(&element);
212 ClearWant(&want);
213 printf("------end testSetElementBundleName------\n");
214 }
215
216 /**
217 * @tc.number : SUB_APPEXECFWK_AMS_API_0008
218 * @tc.name : testAbilityMgrSetWantElement parameter illegal test
219 * @tc.desc : [C- SOFTWARE -0100]
220 */
221 HWTEST_F(BundleMgrTest, testSetElementBundleNameIllegal, Function | MediumTest | Level2)
222 {
223 printf("------start testSetElementBundleNameIllegal------\n");
224 Want want = { nullptr };
225 ElementName element = { nullptr };
226 SetElementBundleName(&element, "");
227 SetWantElement(&want, element);
228 printf("BundleName1 is %s \n", want.element->bundleName);
229 char bName[] = "";
230 EXPECT_STREQ(want.element->bundleName, bName);
231 SetElementBundleName(&element, nullptr);
232 SetWantElement(&want, element);
233 printf("BundleName2 is %s \n", want.element->bundleName);
234 EXPECT_STREQ(want.element->bundleName, nullptr);
235 ClearElement(&element);
236 ClearWant(&want);
237 printf("------end testSetElementBundleNameIllegal------\n");
238 }
239
240 /**
241 * @tc.number : SUB_APPEXECFWK_AMS_API_0005
242 * @tc.name : testSetElementDeviceID parameter legal test
243 * @tc.desc : [C- SOFTWARE -0100]
244 */
245 HWTEST_F(BundleMgrTest, testSetElementDeviceID, Function | MediumTest | Level0)
246 {
247 printf("------start testSetElementDeviceID------\n");
248 Want want = { nullptr };
249 ElementName element = { nullptr };
250 SetElementDeviceID(&element, "0001000");
251 SetWantElement(&want, element);
252 char dID[] = "0001000";
253 EXPECT_STREQ(want.element->deviceId, dID);
254 ClearElement(&element);
255 ClearWant(&want);
256 printf("------end testSetElementDeviceID------\n");
257 }
258
259 /**
260 * @tc.number : SUB_APPEXECFWK_AMS_API_0006
261 * @tc.name : testSetElementDeviceID parameter illegal test
262 * @tc.desc : [C- SOFTWARE -0100]
263 */
264 HWTEST_F(BundleMgrTest, testSetElementDeviceIDIllegal, Function | MediumTest | Level2)
265 {
266 printf("------start testSetElementDeviceIDIllegal------\n");
267 Want want = { nullptr };
268 ElementName element = { nullptr };
269 SetElementDeviceID(&element, "");
270 SetWantElement(&want, element);
271 char dID[] = "";
272 EXPECT_STREQ(want.element->deviceId, dID);
273 SetElementDeviceID(&element, nullptr);
274 SetWantElement(&want, element);
275 EXPECT_STREQ(want.element->deviceId, nullptr);
276 ClearElement(&element);
277 ClearWant(&want);
278 printf("------end testSetElementDeviceIDIllegal------\n");
279 }
280
281 /**
282 * @tc.number : SUB_APPEXECFWK_BMS_API_0007
283 * @tc.name : Install parameter illegal test that callback is null
284 * @tc.desc : [C- SOFTWARE -0200]
285 */
286 HWTEST_F(BundleMgrTest, testInstallWithNullptr, Function | MediumTest | Level2)
287 {
288 printf("------start testInstallWithNullptr------\n");
289 string hapPath = g_testPath + "testnative.hap";
290 InstallParam installParam = { .installLocation = 1, .keepData = false };
291 bool isInstallSuccess = Install(hapPath.c_str(), &installParam, nullptr);
292 EXPECT_FALSE(isInstallSuccess);
293 printf("install result is %d \n", isInstallSuccess);
294 printf("------end testInstallWithNullptr------\n");
295 }
296
297 /**
298 * @tc.number : SUB_APPEXECFWK_BMS_API_0004
299 * @tc.name : Install parameter illegal test that path is null
300 * @tc.desc : [C- SOFTWARE -0200]
301 */
302 HWTEST_F(BundleMgrTest, testInstallWithNullPath, Function | MediumTest | Level2)
303 {
304 printf("------start testInstallWithNullPath------\n");
305 InstallParam installParam = { .installLocation = 1, .keepData = false };
306 bool isInstallSuccess = Install(nullptr, &installParam, TestBundleStateCallback);
307 EXPECT_FALSE(isInstallSuccess);
308 printf("install result is %d \n", isInstallSuccess);
309 printf("------end testInstallWithNullPath------\n");
310 }
311
312 /**
313 * @tc.number : SUB_APPEXECFWK_BMS_API_0002
314 * @tc.name : Install parameter illegal test that ErrorPath is wrong
315 * @tc.desc : [C- SOFTWARE -0200]
316 */
317 HWTEST_F(BundleMgrTest, testInstallWithErrorPath, Function | MediumTest | Level2)
318 {
319 printf("------start testBundleMgrInstallWithErrorPath------\n");
320 string hapPath = "appexecfwk/nothishap.hap";
321 bool isInstallSuccess = false;
322 sem_init(&g_sem, 0, 0);
323 InstallParam installParam = { .installLocation = 1, .keepData = false };
324 bool installResult = Install(hapPath.c_str(), &installParam, TestBundleStateCallback);
325 sem_wait(&g_sem);
326 if (g_errorCode == 0) {
327 isInstallSuccess = true;
328 }else if (g_errorCode > 0) {
329 isInstallSuccess = false;
330 HILOG_DEBUG(HILOG_MODULE_APP, "TestBundleMgrInstall failed,g_errorCode is: %d", g_errorCode);
331 }
332 EXPECT_FALSE(isInstallSuccess);
333 printf("install result is %d", installResult);
334 printf("------end testBundleMgrInstallWithErrorPath------\n");
335 }
336
337 /**
338 * @tc.number : SUB_APPEXECFWK_BMS_API_0001
339 * @tc.name : Install parameter legal test
340 * @tc.desc : [C- SOFTWARE -0200]
341 */
342 HWTEST_F(BundleMgrTest, testBundleMgrInstallright, Function | MediumTest | Level0)
343 {
344 printf("------start testBundleMgrInstallright------\n");
345 string hapPath = g_testPath + "testnative.hap";
346 bool isInstallSuccess = false;
347 sem_init(&g_sem, 0, 0);
348 InstallParam installParam = { .installLocation = 1, .keepData = false };
349 bool installResult = Install(hapPath.c_str(), &installParam, TestBundleStateCallback);
350 sem_wait(&g_sem);
351 if (g_errorCode == 0) {
352 isInstallSuccess = true;
353 }else if (g_errorCode > 0) {
354 isInstallSuccess = false;
355 HILOG_DEBUG(HILOG_MODULE_APP, "TestBundleMgrInstall failed,g_errorCode is: %d", g_errorCode);
356 }
357 EXPECT_TRUE(isInstallSuccess);
358 printf("install result is %d \n", installResult);
359 printf("------end testBundleMgrInstallright------\n");
360 }
361 /**
362 * @tc.number : SUB_APPEXECFWK_BMS_API_0003
363 * @tc.name : Install parameter illegal test that Path is empty
364 * @tc.desc : [C- SOFTWARE -0200]
365 */
366 HWTEST_F(BundleMgrTest, testBundleMgrInstallEmpty, Function | MediumTest | Level2)
367 {
368 printf("------start testBundleMgrInstallEmpty------\n");
369 string hapPath = "";
370 bool isInstallSuccess = false;
371 sem_init(&g_sem, 0, 0);
372 InstallParam installParam = { .installLocation = 1, .keepData = false };
373 bool installResult = Install(hapPath.c_str(), &installParam, TestBundleStateCallback);
374 sem_wait(&g_sem);
375 if (g_errorCode == 0) {
376 isInstallSuccess = true;
377 }else if (g_errorCode > 0) {
378 isInstallSuccess = false;
379 HILOG_DEBUG(HILOG_MODULE_APP, "TestBundleMgrInstall failed,g_errorCode is: %d", g_errorCode);
380 }
381 EXPECT_FALSE(isInstallSuccess);
382 printf("install result is %d", installResult);
383 printf("------end testBundleMgrInstallEmpty------\n");
384 }
385
386 /**
387 * @tc.number : SUB_APPEXECFWK_BMS_API_0009
388 * @tc.name : Install parameter illegal test that file is bin
389 * @tc.desc : [C- SOFTWARE -0200]
390 */
391 HWTEST_F(BundleMgrTest, testBundleMgrInstallBin, Function | MediumTest | Level1)
392 {
393 printf("------start testBundleMgrInstallBin------\n");
394 string hapPath = g_testPath + "testdemo.bin";
395 bool isInstallSuccess = false;
396 sem_init(&g_sem, 0, 0);
397 InstallParam installParam = { .installLocation = 1, .keepData = false };
398 bool installResult = Install(hapPath.c_str(), &installParam, TestBundleStateCallback);
399 sem_wait(&g_sem);
400 if (g_errorCode == 0) {
401 isInstallSuccess = true;
402 }else if (g_errorCode > 0) {
403 isInstallSuccess = false;
404 HILOG_DEBUG(HILOG_MODULE_APP, "TestBundleMgrInstall failed,g_errorCode is: %d", g_errorCode);
405 }
406 EXPECT_FALSE(isInstallSuccess);
407 printf("install result is %d", installResult);
408 printf("------end testBundleMgrInstallBin------\n");
409 }
410
411 /**
412 * @tc.number : SUB_APPEXECFWK_BMS_API_0008
413 * @tc.name : Install parameter illegal test that hap is destroyed
414 * @tc.desc : [C- SOFTWARE -0200]
415 */
416 HWTEST_F(BundleMgrTest, testBundleMgrInstallBadfile, Function | MediumTest | Level2)
417 {
418 printf("------start testBundleMgrInstallBadfile------\n");
419 string hapPath = g_testPath + "errpinjie.hap";
420 bool isInstallSuccess = false;
421 sem_init(&g_sem, 0, 0);
422 InstallParam installParam = { .installLocation = 1, .keepData = false };
423 bool installResult = Install(hapPath.c_str(), &installParam, TestBundleStateCallback);
424 sem_wait(&g_sem);
425 if (g_errorCode == 0) {
426 isInstallSuccess = true;
427 }else if (g_errorCode > 0) {
428 isInstallSuccess = false;
429 HILOG_DEBUG(HILOG_MODULE_APP, "TestBundleMgrInstall failed,g_errorCode is: %d", g_errorCode);
430 }
431 EXPECT_FALSE(isInstallSuccess);
432 printf("install result is %d", installResult);
433 printf("------start testBundleMgrInstallBadfile------\n");
434 }
435
436 /**
437 * @tc.number : SUB_APPEXECFWK_BMS_API_0014
438 * @tc.name : Uninstall parameter illegal test that callback is null
439 * @tc.desc : [C- SOFTWARE -0200]
440 */
441 HWTEST_F(BundleMgrTest, testUninstallNullCallback, Function | MediumTest | Level2)
442 {
443 printf("------start testUninstallNullCallback------\n");
444 const char *bundleName = (char*)"com.openharmony.testdemo";
445 InstallParam installParam = { .installLocation = 1, .keepData = false };
446 bool isUninstallSuccess = Uninstall(bundleName, &installParam, nullptr);
447 EXPECT_FALSE(isUninstallSuccess);
448 printf("uninstall result is %d", isUninstallSuccess);
449 printf("------end testUninstallNullCallback------\n");
450 }
451
452 /**
453 * @tc.number : SUB_APPEXECFWK_BMS_API_0013
454 * @tc.name : Uninstall parameter illegal test that bundleName is null
455 * @tc.desc : [C- SOFTWARE -0200]
456 */
457 HWTEST_F(BundleMgrTest, testUninstallnullBundleName, Function | MediumTest | Level2)
458 {
459 printf("------start testUninstallnullBundleName------\n");
460 InstallParam installParam = { .installLocation = 1, .keepData = false };
461 bool isUninstallSuccess = Uninstall(nullptr, &installParam, TestBundleStateCallback);
462 EXPECT_FALSE(isUninstallSuccess);
463 printf("uninstall result is %d", isUninstallSuccess);
464 printf("------end testUninstallnullBundleName------\n");
465 }
466
467 /**
468 * @tc.number : SUB_APPEXECFWK_BMS_API_0010
469 * @tc.name : Uninstall parameter legal test
470 * @tc.desc : [C- SOFTWARE -0200]
471 */
472 HWTEST_F(BundleMgrTest, testUninstallright, Function | MediumTest | Level0)
473 {
474 printf("------start testUninstallright------\n");
475 string hapPath = g_testPath + "testnative.hap";
476 sem_init(&g_sem, 0, 0);
477 InstallParam installParam = { .installLocation = 1, .keepData = false };
478 bool installResult = Install(hapPath.c_str(), &installParam, TestBundleStateCallback);
479 sem_wait(&g_sem);
480 EXPECT_TRUE(installResult);
481 sleep(1);
482 const char *bundleName = (char*)"com.openharmony.testnative";
483 bool isUninstallSuccess = false;
484 sem_init(&g_sem, 0, 0);
485 bool uninstallState = Uninstall(bundleName, &installParam, TestBundleStateCallback);
486 sem_wait(&g_sem);
487 printf("uninstall result is %d", uninstallState);
488 if (g_installState) {
489 isUninstallSuccess = true;
490 }else if (g_errorCode > 0) {
491 isUninstallSuccess = false;
492 HILOG_DEBUG(HILOG_MODULE_APP, "TestBundleMgrUninstall failed,g_errorCode is: %d", g_errorCode);
493 }
494 EXPECT_TRUE(uninstallState);
495 printf("uninstall result is %d", isUninstallSuccess);
496 printf("------end testUninstallright------\n");
497 }
498
499 /**
500 * @tc.number : SUB_APPEXECFWK_BMS_API_0011
501 * @tc.name : Uninstall parameter illegal test that bundleName is wrong
502 * @tc.desc : [C- SOFTWARE -0200]
503 */
504 HWTEST_F(BundleMgrTest, testUninstallErrorName, Function | MediumTest | Level2)
505 {
506 printf("------start testUninstallErrorName------\n");
507 const char *bundleName = (char*)"com.openharmony.nothisBundleName";
508 bool isUninstallSuccess = false;
509 sem_init(&g_sem, 0, 0);
510 InstallParam installParam = { .installLocation = 1, .keepData = false };
511 bool uninstallState = Uninstall(bundleName, &installParam, TestBundleStateCallback);
512 sem_wait(&g_sem);
513 printf("uninstall result is %d", uninstallState);
514 if (g_installState) {
515 isUninstallSuccess = true;
516 }else if (g_errorCode > 0) {
517 isUninstallSuccess = false;
518 HILOG_DEBUG(HILOG_MODULE_APP, "TestBundleMgrUninstall failed,g_errorCode is: %d", g_errorCode);
519 }
520 EXPECT_FALSE(isUninstallSuccess);
521 printf("uninstall result is %d", isUninstallSuccess);
522 printf("------end testUninstallErrorName------\n");
523 }
524
525 /**
526 * @tc.number : SUB_APPEXECFWK_BMS_API_0012
527 * @tc.name : Uninstall parameter illegal test that bundleName is empty
528 * @tc.desc : [C- SOFTWARE -0200]
529 */
530 HWTEST_F(BundleMgrTest, testUninstallEmptyName, Function | MediumTest | Level2)
531 {
532 printf("------start testUninstallEmptyName------\n");
533 const char *bundleName = (char*)"";
534 bool isUninstallSuccess = false;
535 sem_init(&g_sem, 0, 0);
536 InstallParam installParam = { .installLocation = 1, .keepData = false };
537 bool uninstallState = Uninstall(bundleName, &installParam, TestBundleStateCallback);
538 sem_wait(&g_sem);
539 printf("uninstall resute is %d", uninstallState);
540 if (g_installState) {
541 isUninstallSuccess = true;
542 }else if (g_errorCode > 0) {
543 isUninstallSuccess = false;
544 HILOG_DEBUG(HILOG_MODULE_APP, "TestBundleMgrUninstall failed,g_errorCode is: %d", g_errorCode);
545 }
546 EXPECT_FALSE(isUninstallSuccess);
547 printf("uninstall result is %d", isUninstallSuccess);
548 printf("------end testUninstallEmptyName------\n");
549 }
550
551
552 /**
553 * @tc.number : SUB_APPEXECFWK_BMS_API_0040
554 * @tc.name : QueryAbilityInfo parameter legal test
555 * @tc.desc : [C- SOFTWARE -0200]
556 */
557 HWTEST_F(BundleMgrTest, testQueryAbilityInfoRight, Function | MediumTest | Level1)
558 {
559 printf("------start testQueryAbilityInfoRight------\n");
560 Want want;
561 int32_t resultWant = memset_s(&want, sizeof(Want), 0, sizeof(Want));
562 EXPECT_EQ(resultWant, 0);
563 ElementName element;
564 int32_t resultElementName = memset_s(&element, sizeof(ElementName), 0, sizeof(ElementName));
565 EXPECT_EQ(resultElementName, 0);
566 SetElementAbilityName(&element, "MainAbility");
567 SetElementBundleName(&element, "com.openharmony.testjsdemo");
568 SetWantElement(&want, element);
569 SetWantData(&want, "test", 4);
570 AbilityInfo abilityInfo;
571 int32_t result = memset_s(&abilityInfo, sizeof(abilityInfo), 0, sizeof(abilityInfo));
572 EXPECT_EQ(result, 0);
573 printf("element.elementname is %s \n", want.element->bundleName);
574 printf("AbilityName2 is %s \n", want.element->abilityName);
575 g_errorCode = QueryAbilityInfo(&want, &abilityInfo);
576 printf("abilityInfo.bundleName is %s \n", abilityInfo.bundleName);
577 printf("abilityInfo.label is %s \n", abilityInfo.label);
578 printf("abilityInfo.iconPath is %s \n", abilityInfo.iconPath);
579 printf("ret is %d \n", g_errorCode);
580 EXPECT_EQ(g_errorCode, 0);
581 printf("------end testQueryAbilityInfoRight------\n");
582 }
583 /**
584 * @tc.number : SUB_APPEXECFWK_BMS_API_0041
585 * @tc.name : QueryAbilityInfo parameter illegal test
586 * @tc.desc : [C- SOFTWARE -0200]
587 */
588 HWTEST_F(BundleMgrTest, testQueryAbilityInfoIllegal, Function | MediumTest | Level2)
589 {
590 printf("------start testQueryAbilityInfoIllegal------\n");
591 AbilityInfo abilityInfo;
592 int32_t result = memset_s(&abilityInfo, sizeof(AbilityInfo), 0, sizeof(AbilityInfo));
593 EXPECT_EQ(result, 0);
594 // want is nullptr
595 g_errorCode = QueryAbilityInfo(nullptr, &abilityInfo);
596 printf("ret is %d \n", g_errorCode);
597 EXPECT_EQ(g_errorCode, 4);
598 // abilityInfo is nullptr
599 Want want;
600 int32_t resultWant = memset_s(&want, sizeof(Want), 0, sizeof(Want));
601 EXPECT_EQ(resultWant, 0);
602 ElementName element;
603 int32_t resultElementName = memset_s(&element, sizeof(ElementName), 0, sizeof(ElementName));
604 EXPECT_EQ(resultElementName, 0);
605 SetElementAbilityName(&element, "MainAbility");
606 SetElementBundleName(&element, "com.openharmony.testjsdemo");
607 SetWantElement(&want, element);
608 SetWantData(&want, "test", 4);
609 g_errorCode = QueryAbilityInfo(&want, nullptr);
610 printf("ret is %d \n", g_errorCode);
611 EXPECT_EQ(g_errorCode, 4);
612 // content of want is ""
613 Want want1 = { nullptr };
614 ElementName element1 = { nullptr };
615 SetElementBundleName(&element1, "");
616 SetElementAbilityName(&element1, "");
617 SetWantElement(&want1, element1);
618 AbilityInfo abilityInfo1;
619 g_errorCode = QueryAbilityInfo(&want1, &abilityInfo1);
620 printf("abilityInfo is null \n");
621 printf("ret is %d \n", g_errorCode);
622 EXPECT_EQ(g_errorCode, 2);
623 printf("------end testQueryAbilityInfoIllegal------\n");
624 }
625
626 /**
627 * @tc.number : SUB_APPEXECFWK_BMS_API_0029
628 * @tc.name : GetBundleInfo parameter legal test.
629 * @tc.desc : [C- SOFTWARE -0200]
630 */
631 HWTEST_F(BundleMgrTest, testGetBundleInfoRight, Function | MediumTest | Level1)
632 {
633 printf("------start testGetBundleInfoRight------\n");
634 BundleInfo bundleInfo;
635 int32_t result = memset_s(&bundleInfo, sizeof(bundleInfo), 0, sizeof(bundleInfo));
636 EXPECT_EQ(result, 0);
637 const char *bundleName = (char*)"com.openharmony.testjsdemo";
638 int32_t flags = 0;
639 printf("bundleName is %s \n", bundleName);
640 sleep(2);
641 g_errorCode = GetBundleInfo(bundleName, flags, &bundleInfo);
642 printf("getBundleInfo result is %d \n", g_errorCode);
643 EXPECT_STREQ(bundleInfo.bundleName, bundleName);
644 EXPECT_EQ(bundleInfo.numOfAbility, 0);
645 EXPECT_EQ(g_errorCode, 0);
646 flags = 1;
647 printf("bundleName is %s \n", bundleName);
648 g_errorCode = GetBundleInfo(bundleName, flags, &bundleInfo);
649 sleep(2);
650 printf("getBundleInfo result is %d \n", g_errorCode);
651 EXPECT_EQ(g_errorCode, 0);
652 EXPECT_STREQ(bundleInfo.bundleName, bundleName);
653 EXPECT_EQ(bundleInfo.numOfAbility, 3);
654 ClearBundleInfo(&bundleInfo);
655 printf("------end testGetBundleInfoRight------\n");
656 }
657
658 /**
659 * @tc.number : SUB_APPEXECFWK_BMS_API_0030
660 * @tc.name : GetBundleInfo parameter illegal test.
661 * @tc.desc : [C- SOFTWARE -0200]
662 */
663 HWTEST_F(BundleMgrTest, testGetBundleInfoIllegal, Function | MediumTest | Level2)
664 {
665 printf("------start testGetBundleInfoIllegal------\n");
666 BundleInfo bundleInfo;
667 int32_t result = memset_s(&bundleInfo, sizeof(bundleInfo), 0, sizeof(bundleInfo));
668 EXPECT_EQ(result, 0);
669 const char *bundleName = (char*)"com.openharmony.nothishap";
670 int32_t flags = 0;
671 // error bundleName
672 g_errorCode = GetBundleInfo(bundleName, flags, &bundleInfo);
673 printf("bundleInfo1.bundleName is %s \n", bundleInfo.bundleName);
674 printf("bundleInfo1.versionCode is %d \n", bundleInfo.versionCode);
675 printf("bundleInfo1.codePath is %s \n", bundleInfo.codePath);
676 EXPECT_EQ(g_errorCode, 2);
677 // bundleName = nullptr
678 g_errorCode = GetBundleInfo(nullptr, flags, &bundleInfo);
679 printf("abilityInfo2 is %d \n", g_errorCode);
680 EXPECT_EQ(g_errorCode, 4);
681 printf("bundleInfo2.bundleName is %s \n", bundleInfo.bundleName);
682 printf("bundleInfo2.versionCode is %d \n", bundleInfo.versionCode);
683 printf("bundleInfo2.codePath is %s \n", bundleInfo.codePath);
684 // bunldeName = ""
685 g_errorCode = GetBundleInfo("", flags, &bundleInfo);
686 printf("bundleInfo3.bundleName is %s \n", bundleInfo.bundleName);
687 printf("bundleInfo3.versionCode is %d \n", bundleInfo.versionCode);
688 printf("bundleInfo3.codePath is %s \n", bundleInfo.codePath);
689 EXPECT_EQ(g_errorCode, 2);
690 // flags not exit
691 g_errorCode = GetBundleInfo("com.openharmony.testjsdemo", 2, &bundleInfo);
692 sleep(2);
693 printf("bundleInfo3.bundleName is %s \n", bundleInfo.bundleName);
694 printf("bundleInfo3.versionCode is %d \n", bundleInfo.versionCode);
695 printf("bundleInfo3.codePath is %s \n", bundleInfo.codePath);
696 EXPECT_EQ(g_errorCode, 1);
697 printf("------end testGetBundleInfoIllegal------\n");
698 }
699
700 /**
701 * @tc.number : SUB_APPEXECFWK_BMS_API_0042
702 * @tc.name : GetBundleInfos parameter legal test
703 * @tc.desc : [C- SOFTWARE -0200]
704 */
705 HWTEST_F(BundleMgrTest, testGetBundleInfosRight, Function | MediumTest | Level1)
706 {
707 printf("------start testGetBundleInfosRight------\n");
708 BundleInfo *bundleInfos = nullptr;
709 int32_t flags = 0;
710 int32_t length = 0;
711 g_errorCode = GetBundleInfos(flags, &bundleInfos, &length);
712 sleep(2);
713 printf("getBundleInfo result is %d \n", g_errorCode);
714 EXPECT_EQ(g_errorCode, 0);
715 if (g_errorCode == 0){
716 printf("bundleInfos.codePath is %s \n", bundleInfos[0].codePath);
717 printf("bundleInfos.bundleName is %s \n", bundleInfos[0].bundleName);
718 printf("bundleInfos.versionCode is %d \n", bundleInfos[0].versionCode);
719 }
720 flags = 1;
721 g_errorCode = GetBundleInfos(flags, &bundleInfos, &length);
722 printf("getBundleInfo result is %d \n", g_errorCode);
723 sleep(2);
724 EXPECT_EQ(g_errorCode, 0);
725 if (g_errorCode == 0){
726 printf("bundleInfos.codePath is %s \n", bundleInfos[0].codePath);
727 printf("bundleInfos.bundleName is %s \n", bundleInfos[0].bundleName);
728 printf("bundleInfos.versionCode is %d \n", bundleInfos[0].versionCode);
729 }
730 free(bundleInfos);
731 printf("------end testGetBundleInfosRight------\n");
732 }
733
734 /**
735 * @tc.number : SUB_APPEXECFWK_BMS_API_0043
736 * @tc.name : GetBundleInfos parameter illegal test
737 * @tc.desc : [C- SOFTWARE -0200]
738 */
739 HWTEST_F(BundleMgrTest, testGetBundleInfosIllegal, Function | MediumTest | Level2)
740 {
741 printf("------start testGetBundleInfosIllegal------\n");
742 BundleInfo *bundleInfos = {nullptr};
743 int32_t *length = nullptr;
744 int32_t flags = 0;
745 g_errorCode = GetBundleInfos(flags, nullptr, length);
746 EXPECT_EQ(g_errorCode, 4);
747 g_errorCode = GetBundleInfos(flags, &bundleInfos, nullptr);
748 printf("g_errorCode is %d \n", g_errorCode);
749 EXPECT_EQ(g_errorCode, 4);
750 g_errorCode = GetBundleInfos(2, &bundleInfos, length);
751 printf("g_errorCode is %d \n", g_errorCode);
752 EXPECT_EQ(g_errorCode, 4);
753 printf("------end testGetBundleInfosIllegal------\n");
754 }
755
756 /**
757 * @tc.number : SUB_APPEXECFWK_BMS_API_0039
758 * @tc.name : GetBundleInfosByMetaData parameter illegal test
759 * @tc.desc : [C- SOFTWARE -0200]
760 */
761 HWTEST_F(BundleMgrTest, testGetBundleInfosByMetaDataIllegal, Function | MediumTest | Level2)
762 {
763 printf("------start testGetBundleInfosByMetaDataIllegal------\n");
764 BundleInfo *bundleInfos = {nullptr};
765 int32_t length = 0;
766 const char *metaDataKey = "appId";
767 g_errorCode = GetBundleInfosByMetaData(nullptr, &bundleInfos, &length);
768 EXPECT_EQ(g_errorCode, 4);
769 g_errorCode = GetBundleInfosByMetaData(metaDataKey, &bundleInfos, nullptr);
770 printf("g_errorCode is %d \n", g_errorCode);
771 EXPECT_EQ(g_errorCode, 4);
772 g_errorCode = GetBundleInfosByMetaData(metaDataKey, nullptr, &length);
773 printf("g_errorCode is %d \n", g_errorCode);
774 EXPECT_EQ(g_errorCode, 4);
775 const char *metaDataKey1 = "noThisKey";
776 printf("metaDataKey is %s \n", metaDataKey1);
777 g_errorCode = GetBundleInfosByMetaData(metaDataKey1, &bundleInfos, &length);
778 printf("GetBundleInfosByMetaData result is %d \n", g_errorCode);
779 EXPECT_EQ(g_errorCode, 2);
780 const char *metaDataKey2 = "";
781 g_errorCode = GetBundleInfosByMetaData(metaDataKey2, &bundleInfos, &length);
782 printf("GetBundleInfosByMetaData result is %d \n", g_errorCode);
783 EXPECT_EQ(g_errorCode, 2);
784 printf("------end testGetBundleInfosByMetaDataIllegal------\n");
785 }
786
787 /**
788 * @tc.number : SUB_APPEXECFWK_BMS_API_0038
789 * @tc.name : GetBundleInfosByMetaData parameter legal test
790 * @tc.desc : [C- SOFTWARE -0200]
791 */
792 HWTEST_F(BundleMgrTest, testGetBundleInfosByMetaDataRight, Function | MediumTest | Level1)
793 {
794 printf("------start testGetBundleInfosByMetaDataRight------\n");
795 BundleInfo *bundleInfos = nullptr;
796 const char *metaDataKey = "appId";
797 int32_t length = 0;
798 printf("metaDataKey is %s \n", metaDataKey);
799 g_errorCode = GetBundleInfosByMetaData(metaDataKey, &bundleInfos, &length);
800 sleep(2);
801 printf("GetBundleInfosByMetaData result is %d \n", g_errorCode);
802 EXPECT_EQ(g_errorCode, 0);
803 if (g_errorCode == 0){
804 printf("bundleInfos.bundleName is %s \n", bundleInfos[0].bundleName);
805 }
806 printf("------end testGetBundleInfosByMetaDataRight------\n");
807 }
808
809 /**
810 * @tc.number : SUB_APPEXECFWK_BMS_API_0037
811 * @tc.name : QueryKeepAliveBundleInfos parameter illegal test
812 * @tc.desc : [C- SOFTWARE -0200]
813 */
814 HWTEST_F(BundleMgrTest, testQueryKeepAliveBundleInfosIllegal, Function | MediumTest | Level2)
815 {
816 printf("------start testQueryKeepAliveBundleInfosIllegal------\n");
817 BundleInfo *bundleInfos = {nullptr};
818 int32_t length = 0;
819 g_errorCode = QueryKeepAliveBundleInfos(nullptr, &length);
820 printf("g_errorCode1 is %d \n", g_errorCode);
821 EXPECT_EQ(g_errorCode, 4);
822 g_errorCode = QueryKeepAliveBundleInfos(&bundleInfos, nullptr);
823 printf("g_errorCode2 is %d \n", g_errorCode);
824 EXPECT_EQ(g_errorCode, 4);
825 printf("------end testQueryKeepAliveBundleInfosIllegal------\n");
826 }
827
828 /**
829 * @tc.number : SUB_APPEXECFWK_BMS_API_0034
830 * @tc.name : GetBundleNameForUid parameter nullptr test
831 * @tc.desc : [C- SOFTWARE -0200]
832 */
833 HWTEST_F(BundleMgrTest, testGetBundleNameForUidWithNullptr, Function | MediumTest | Level2)
834 {
835 printf("------start testGetBundleNameForUidWithNullptr------\n");
836 int32_t resultCode = GetBundleNameForUid(0, nullptr);
837 EXPECT_EQ(resultCode, 4);
838 printf("GetBundleNameForUid result is %d \n", resultCode);
839 printf("------end testGetBundleNameForUidWithNullptr------\n");
840 }
841
842 /**
843 * @tc.number : SUB_APPEXECFWK_BMS_API_0035
844 * @tc.name : GetBundleNameForUid parameter illegal test
845 * @tc.desc : [C- SOFTWARE -0200]
846 */
847 HWTEST_F(BundleMgrTest, testGetBundleNameForUidWithIllegal, Function | MediumTest | Level2)
848 {
849 printf("------start testGetBundleNameForUidWithIllegal------\n");
850 char *bundleName = nullptr;
851 int32_t resultCode = GetBundleNameForUid(0, &bundleName);
852 EXPECT_EQ(resultCode, 114);
853 printf("GetBundleNameForUid result is %d \n", resultCode);
854 if (bundleName != nullptr) {
855 free(bundleName);
856 }
857 printf("------end testGetBundleNameForUidWithIllegal------\n");
858 }
859
860 /**
861 * @tc.number : SUB_APPEXECFWK_BMS_SIZE_0001
862 * @tc.name : Test the GetBundleSize of the thirld hap can be obtained normally
863 * @tc.desc : [C- SOFTWARE -0200]
864 */
865 HWTEST_F(BundleMgrTest, testGetBundleSizeWithLegal_0001, Function | MediumTest | Level1)
866 {
867 printf("------start testGetBundleSizeWithLegal_0001------\n");
868 char *bundleName = (char*)"com.openharmony.testjsdemo";
869 uint32_t resultCode = GetBundleSize(bundleName);
870 EXPECT_GT(resultCode, 0);
871 printf("------end testGetBundleSizeWithLegal_0001------\n");
872 }
873
874 /**
875 * @tc.number : SUB_APPEXECFWK_BMS_SIZE_0002
876 * @tc.name : GetBundleSize parameter legal and bundleName length equal to 127 test
877 * @tc.desc : [C- SOFTWARE -0200]
878 */
879 HWTEST_F(BundleMgrTest, testGetBundleSizeWithLegal_0002, Function | MediumTest | Level1)
880 {
881 printf("------start testGetBundleSizeWithLegal_0002------\n");
882 char *bundleName = (char*)"com.openharmony.testjsdemoBundleNameleng" \
883 "thequalto127testjsdemoBundleNamelengthequalto127testjsdemoBundleNamelengthequalto127tes";
884 sem_init(&g_sem, 0, 0);
885 InstallParam installParam = {.installLocation = 1, .keepData = false };
886 string hapPath = g_testPath + "testGetBundleNameWithLegal127.hap";
887 Install(hapPath.c_str(), &installParam, TestBundleStateCallback);
888 sem_wait(&g_sem);
889 uint32_t resultCode = GetBundleSize(bundleName);
890 EXPECT_EQ(strlen(bundleName), 127);
891 EXPECT_GT(resultCode, 0);
892 // uninstall
893 sem_init(&g_sem, 0, 0);
894 Uninstall(bundleName, &installParam, TestBundleStateCallback);
895 sem_wait(&g_sem);
896 printf("------end testGetBundleSizeWithLegal_0002------\n");
897 }
898
899 /**
900 * @tc.number : SUB_APPEXECFWK_BMS_SIZE_0003
901 * @tc.name : GetBundleSize parameter illegal and bundleName length equal to 128 test
902 * @tc.desc : [C- SOFTWARE -0200]
903 */
904 HWTEST_F(BundleMgrTest, testGetBundleSizeWithIllegal_0001, Function | MediumTest | Level2)
905 {
906 printf("------start testGetBundleSizeWithIllegal_0001------\n");
907 char *bundleName = (char*)"com.openharmony.testjsdemoBundleNameLength128test" \
908 "jsdemoBundleNameLength128testjsdemoBundleNameLength128testjsdemoBundleNameLengt";
909 EXPECT_EQ(strlen(bundleName), 128);
910 uint32_t resultCode = GetBundleSize(bundleName);
911 EXPECT_EQ(resultCode, 0);
912 printf("------end testGetBundleSizeWithIllegal_0001------\n");
913 }
914
915 /**
916 * @tc.number : SUB_APPEXECFWK_BMS_SIZE_0004
917 * @tc.name : GetBundleSize parameter illegal and bundleName nullptr test
918 * @tc.desc : [C- SOFTWARE -0200]
919 */
920 HWTEST_F(BundleMgrTest, testGetBundleSizeWithIllegal_0002, Function | MediumTest | Level2)
921 {
922 printf("------start testGetBundleSizeWithIllegal_0002------\n");
923 char *bundleName = nullptr;
924 // bundleName nullptr
925 uint32_t resultCode = GetBundleSize(bundleName);
926 EXPECT_EQ(resultCode, 0);
927 printf("------end testGetBundleSizeWithIllegal_0002------\n");
928 }
929
930 /**
931 * @tc.number : SUB_APPEXECFWK_BMS_SIZE_0005
932 * @tc.name : GetBundleSize parameter illegal and bundleName error test
933 * @tc.desc : [C- SOFTWARE -0200]
934 */
935 HWTEST_F(BundleMgrTest, testGetBundleSizeWithIllegal_0003, Function | MediumTest | Level2)
936 {
937 printf("------start testGetBundleSizeWithIllegal_0003------\n");
938 char *bundleName = (char*)"com.openharmony.nothishap";
939 // error bundleName
940 uint32_t resultCode = GetBundleSize(bundleName);
941 EXPECT_EQ(resultCode, 0);
942 printf("------end testGetBundleSizeWithIllegal_0003------\n");
943 }
944
945 /**
946 * @tc.number : SUB_APPEXECFWK_BMS_SIZE_0006
947 * @tc.name : GetBundleSize parameter illegal and bundleName " " test
948 * @tc.desc : [C- SOFTWARE -0200]
949 */
950 HWTEST_F(BundleMgrTest, testGetBundleSizeWithIllegal_0004, Function | MediumTest | Level2)
951 {
952 printf("------start testGetBundleSizeWithIllegal_0004------\n");
953 char *bundleName = (char*)" ";
954 // bundleName " "
955 uint32_t resultCode = GetBundleSize(bundleName);
956 EXPECT_EQ(resultCode, 0);
957 printf("------end testGetBundleSizeWithIllegal_0004------\n");
958 }
959
960 /**
961 * @tc.number : SUB_APPEXECFWK_BMS_SIZE_0007
962 * @tc.name : stress test of the same application
963 * @tc.desc : [C- SOFTWARE -0200]
964 */
965 HWTEST_F(BundleMgrTest, testStressConfig_0001, Function | MediumTest | Level2)
966 {
967 printf("------start testStressConfig_0001------\n");
968 char *bundleName = (char*)"com.openharmony.testjsdemo";
969 for (int i = 1; i <= 100; i++) {
970 uint32_t resultCode = GetBundleSize(bundleName);
971 EXPECT_GT(resultCode, 0);
972 }
973 printf("------end testStressConfig_0001------\n");
974 }
975
976 /**
977 * @tc.number : SUB_APPEXECFWK_BMS_SIZE_0008
978 * @tc.name : stress test of the difference application
979 * @tc.desc : [C- SOFTWARE -0200]
980 */
981 HWTEST_F(BundleMgrTest, testStressConfig_0002, Function | MediumTest | Level2)
982 {
983 printf("------start testStressConfig_0002------\n");
984 char *bundleName = (char*)"com.openharmony.testjsdemo";
985 char *bundleName2 = (char*)"com.openharmony.testjsdemoBtestjsdemoB";
986 sem_init(&g_sem, 0, 0);
987 InstallParam installParam = {.installLocation = 1, .keepData = false };
988 string hapPath = g_testPath + "frequentlyStress.hap";
989 Install(hapPath.c_str(), &installParam, TestBundleStateCallback);
990 sem_wait(&g_sem);
991 for (int i = 1; i <= 100; i++) {
992 uint32_t resultCode = GetBundleSize(bundleName);
993 EXPECT_GT(resultCode, 0);
994 resultCode = GetBundleSize(bundleName2);
995 EXPECT_GT(resultCode, 0);
996 }
997 // uninstall
998 sem_init(&g_sem, 0, 0);
999 Uninstall(bundleName2, &installParam, TestBundleStateCallback);
1000 sem_wait(&g_sem);
1001 printf("------end testStressConfig_0002------\n");
1002 }
1003