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
26using namespace std;
27using namespace testing::ext;
28using namespace OHOS;
29static bool g_installState = false;
30static int g_errorCode = -1;
31static sem_t g_sem;
32static string g_testPath;
33
34extern "C" {
35void __attribute__((weak)) HOS_SystemInit(void) { }
36}
37
38/* callback */
39static 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 */
52static 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
66class BundleMgrTest : public testing::Test {
67protected:
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    }
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 */
99HWTEST_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 */
118HWTEST_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 */
137HWTEST_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 */
156HWTEST_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 */
177HWTEST_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 */
201HWTEST_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 */
221HWTEST_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 */
245HWTEST_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 */
264HWTEST_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 */
286HWTEST_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 */
302HWTEST_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 */
317HWTEST_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 */
342HWTEST_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 */
366HWTEST_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 */
391HWTEST_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 */
416HWTEST_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 */
441HWTEST_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 */
457HWTEST_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 */
472HWTEST_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    const char *bundleName = (char*)"com.openharmony.testnative";
482    bool isUninstallSuccess = false;
483    sem_init(&g_sem, 0, 0);
484    bool uninstallState = Uninstall(bundleName, &installParam, TestBundleStateCallback);
485    sem_wait(&g_sem);
486    printf("uninstall result is %d", uninstallState);
487    if (g_installState) {
488        isUninstallSuccess = true;
489    }else if (g_errorCode > 0) {
490        isUninstallSuccess = false;
491        HILOG_DEBUG(HILOG_MODULE_APP, "TestBundleMgrUninstall failed,g_errorCode is: %d", g_errorCode);
492    }
493    EXPECT_TRUE(uninstallState);
494    printf("uninstall result is %d", isUninstallSuccess);
495    printf("------end testUninstallright------\n");
496}
497
498/**
499 * @tc.number    : SUB_APPEXECFWK_BMS_API_0011
500 * @tc.name      : Uninstall parameter illegal test that bundleName is wrong
501 * @tc.desc      : [C- SOFTWARE -0200]
502 */
503HWTEST_F(BundleMgrTest, testUninstallErrorName, Function | MediumTest | Level2)
504{
505    printf("------start testUninstallErrorName------\n");
506    const char *bundleName = (char*)"com.openharmony.nothisBundleName";
507    bool isUninstallSuccess = false;
508    sem_init(&g_sem, 0, 0);
509    InstallParam installParam = { .installLocation = 1, .keepData = false };
510    bool uninstallState = Uninstall(bundleName, &installParam, TestBundleStateCallback);
511    sem_wait(&g_sem);
512    printf("uninstall result is %d", uninstallState);
513    if (g_installState) {
514        isUninstallSuccess = true;
515    }else if (g_errorCode > 0) {
516        isUninstallSuccess = false;
517        HILOG_DEBUG(HILOG_MODULE_APP, "TestBundleMgrUninstall failed,g_errorCode is: %d", g_errorCode);
518    }
519    EXPECT_FALSE(isUninstallSuccess);
520    printf("uninstall result is %d", isUninstallSuccess);
521    printf("------end testUninstallErrorName------\n");
522}
523
524/**
525 * @tc.number    : SUB_APPEXECFWK_BMS_API_0012
526 * @tc.name      : Uninstall parameter illegal test that bundleName is empty
527 * @tc.desc      : [C- SOFTWARE -0200]
528 */
529HWTEST_F(BundleMgrTest, testUninstallEmptyName, Function | MediumTest | Level2)
530{
531    printf("------start testUninstallEmptyName------\n");
532    const char *bundleName = (char*)"";
533    bool isUninstallSuccess = false;
534    sem_init(&g_sem, 0, 0);
535    InstallParam installParam = { .installLocation = 1, .keepData = false };
536    bool uninstallState = Uninstall(bundleName, &installParam, TestBundleStateCallback);
537    sem_wait(&g_sem);
538    printf("uninstall resute is %d", uninstallState);
539    if (g_installState) {
540        isUninstallSuccess = true;
541    }else if (g_errorCode > 0) {
542        isUninstallSuccess = false;
543        HILOG_DEBUG(HILOG_MODULE_APP, "TestBundleMgrUninstall failed,g_errorCode is: %d", g_errorCode);
544    }
545    EXPECT_FALSE(isUninstallSuccess);
546    printf("uninstall result is %d", isUninstallSuccess);
547    printf("------end testUninstallEmptyName------\n");
548}
549
550
551/**
552 * @tc.number    : SUB_APPEXECFWK_BMS_API_0040
553 * @tc.name      : QueryAbilityInfo parameter legal test
554 * @tc.desc      : [C- SOFTWARE -0200]
555 */
556HWTEST_F(BundleMgrTest, testQueryAbilityInfoRight, Function | MediumTest | Level1)
557{
558    printf("------start testQueryAbilityInfoRight------\n");
559    Want want;
560    int32_t resultWant = memset_s(&want, sizeof(Want), 0, sizeof(Want));
561    EXPECT_EQ(resultWant, 0);
562    ElementName element;
563    int32_t resultElementName = memset_s(&element, sizeof(ElementName), 0, sizeof(ElementName));
564    EXPECT_EQ(resultElementName, 0);
565    SetElementAbilityName(&element, "MainAbility");
566    SetElementBundleName(&element, "com.openharmony.testjsdemo");
567    SetWantElement(&want, element);
568    SetWantData(&want, "test", 4);
569    AbilityInfo abilityInfo;
570    int32_t result = memset_s(&abilityInfo, sizeof(abilityInfo), 0, sizeof(abilityInfo));
571    EXPECT_EQ(result, 0);
572    printf("element.elementname is %s \n",  want.element->bundleName);
573    printf("AbilityName2 is %s \n", want.element->abilityName);
574    g_errorCode = QueryAbilityInfo(&want, &abilityInfo);
575    printf("abilityInfo.bundleName is %s \n", abilityInfo.bundleName);
576    printf("abilityInfo.label is %s \n", abilityInfo.label);
577    printf("abilityInfo.iconPath is %s \n", abilityInfo.iconPath);
578    printf("ret is %d \n", g_errorCode);
579    EXPECT_EQ(g_errorCode, 0);
580    printf("------end testQueryAbilityInfoRight------\n");
581}
582/**
583 * @tc.number    : SUB_APPEXECFWK_BMS_API_0041
584 * @tc.name      : QueryAbilityInfo parameter illegal test
585 * @tc.desc      : [C- SOFTWARE -0200]
586 */
587HWTEST_F(BundleMgrTest, testQueryAbilityInfoIllegal, Function | MediumTest | Level2)
588{
589    printf("------start testQueryAbilityInfoIllegal------\n");
590    AbilityInfo abilityInfo;
591    int32_t result = memset_s(&abilityInfo, sizeof(AbilityInfo), 0, sizeof(AbilityInfo));
592    EXPECT_EQ(result, 0);
593    // want is nullptr
594    g_errorCode = QueryAbilityInfo(nullptr, &abilityInfo);
595    printf("ret is %d \n", g_errorCode);
596    EXPECT_EQ(g_errorCode, 4);
597    // abilityInfo is nullptr
598    Want want;
599    int32_t resultWant = memset_s(&want, sizeof(Want), 0, sizeof(Want));
600    EXPECT_EQ(resultWant, 0);
601    ElementName element;
602    int32_t resultElementName = memset_s(&element, sizeof(ElementName), 0, sizeof(ElementName));
603    EXPECT_EQ(resultElementName, 0);
604    SetElementAbilityName(&element, "MainAbility");
605    SetElementBundleName(&element, "com.openharmony.testjsdemo");
606    SetWantElement(&want, element);
607    SetWantData(&want, "test", 4);
608    g_errorCode = QueryAbilityInfo(&want, nullptr);
609    printf("ret is %d \n", g_errorCode);
610    EXPECT_EQ(g_errorCode, 4);
611    // content of want is ""
612    Want want1 = { nullptr };
613    ElementName element1 = { nullptr };
614    SetElementBundleName(&element1, "");
615    SetElementAbilityName(&element1, "");
616    SetWantElement(&want1, element1);
617    AbilityInfo abilityInfo1;
618    g_errorCode = QueryAbilityInfo(&want1, &abilityInfo1);
619    printf("abilityInfo is null \n");
620    printf("ret is %d \n", g_errorCode);
621    EXPECT_EQ(g_errorCode, 2);
622    printf("------end testQueryAbilityInfoIllegal------\n");
623}
624
625/**
626 * @tc.number    : SUB_APPEXECFWK_BMS_API_0029
627 * @tc.name      : GetBundleInfo parameter legal test.
628 * @tc.desc      : [C- SOFTWARE -0200]
629 */
630HWTEST_F(BundleMgrTest, testGetBundleInfoRight, Function | MediumTest | Level1)
631{
632    printf("------start testGetBundleInfoRight------\n");
633    BundleInfo bundleInfo;
634    int32_t result = memset_s(&bundleInfo, sizeof(bundleInfo), 0, sizeof(bundleInfo));
635    EXPECT_EQ(result, 0);
636    const char *bundleName = (char*)"com.openharmony.testjsdemo";
637    int32_t flags = 0;
638    printf("bundleName is %s \n", bundleName);
639    g_errorCode = GetBundleInfo(bundleName, flags, &bundleInfo);
640    printf("getBundleInfo result is %d \n", g_errorCode);
641    EXPECT_STREQ(bundleInfo.bundleName, bundleName);
642    EXPECT_EQ(bundleInfo.numOfAbility, 0);
643    EXPECT_EQ(g_errorCode, 0);
644    flags = 1;
645    printf("bundleName is %s \n", bundleName);
646    g_errorCode = GetBundleInfo(bundleName, flags, &bundleInfo);
647    printf("getBundleInfo result is %d \n", g_errorCode);
648    EXPECT_EQ(g_errorCode, 0);
649    EXPECT_STREQ(bundleInfo.bundleName, bundleName);
650    EXPECT_EQ(bundleInfo.numOfAbility, 3);
651    ClearBundleInfo(&bundleInfo);
652    printf("------end testGetBundleInfoRight------\n");
653}
654
655/**
656 * @tc.number    : SUB_APPEXECFWK_BMS_API_0030
657 * @tc.name      : GetBundleInfo parameter illegal test.
658 * @tc.desc      : [C- SOFTWARE -0200]
659 */
660HWTEST_F(BundleMgrTest, testGetBundleInfoIllegal, Function | MediumTest | Level2)
661{
662    printf("------start testGetBundleInfoIllegal------\n");
663    BundleInfo bundleInfo;
664    int32_t result = memset_s(&bundleInfo, sizeof(bundleInfo), 0, sizeof(bundleInfo));
665    EXPECT_EQ(result, 0);
666    const char *bundleName = (char*)"com.openharmony.nothishap";
667    int32_t flags = 0;
668    // error bundleName
669    g_errorCode = GetBundleInfo(bundleName, flags, &bundleInfo);
670    printf("bundleInfo1.bundleName is %s \n", bundleInfo.bundleName);
671    printf("bundleInfo1.versionCode is %d \n", bundleInfo.versionCode);
672    printf("bundleInfo1.codePath is %s \n", bundleInfo.codePath);
673    EXPECT_EQ(g_errorCode, 2);
674    // bundleName = nullptr
675    g_errorCode = GetBundleInfo(nullptr, flags, &bundleInfo);
676    printf("abilityInfo2 is %d \n", g_errorCode);
677    EXPECT_EQ(g_errorCode, 4);
678    printf("bundleInfo2.bundleName is %s \n", bundleInfo.bundleName);
679    printf("bundleInfo2.versionCode is %d \n", bundleInfo.versionCode);
680    printf("bundleInfo2.codePath is %s \n", bundleInfo.codePath);
681    // bunldeName = ""
682    g_errorCode = GetBundleInfo("", flags, &bundleInfo);
683    printf("bundleInfo3.bundleName is %s \n", bundleInfo.bundleName);
684    printf("bundleInfo3.versionCode is %d \n", bundleInfo.versionCode);
685    printf("bundleInfo3.codePath is %s \n", bundleInfo.codePath);
686    EXPECT_EQ(g_errorCode, 2);
687    // flags not exit
688    g_errorCode = GetBundleInfo("com.openharmony.testjsdemo", 2, &bundleInfo);
689    printf("bundleInfo3.bundleName is %s \n", bundleInfo.bundleName);
690    printf("bundleInfo3.versionCode is %d \n", bundleInfo.versionCode);
691    printf("bundleInfo3.codePath is %s \n", bundleInfo.codePath);
692    EXPECT_EQ(g_errorCode, 1);
693    printf("------end testGetBundleInfoIllegal------\n");
694}
695
696/**
697 * @tc.number    : SUB_APPEXECFWK_BMS_API_0042
698 * @tc.name      : GetBundleInfos parameter legal test
699 * @tc.desc      : [C- SOFTWARE -0200]
700 */
701HWTEST_F(BundleMgrTest, testGetBundleInfosRight, Function | MediumTest | Level1)
702{
703    printf("------start testGetBundleInfosRight------\n");
704    BundleInfo *bundleInfos = nullptr;
705    int32_t flags = 0;
706    int32_t length = 0;
707    g_errorCode = GetBundleInfos(flags, &bundleInfos, &length);
708    printf("getBundleInfo result is %d \n", g_errorCode);
709    EXPECT_EQ(g_errorCode, 0);
710    if (g_errorCode == 0){
711        printf("bundleInfos.codePath is %s \n", bundleInfos[0].codePath);
712        printf("bundleInfos.bundleName is %s \n", bundleInfos[0].bundleName);
713        printf("bundleInfos.versionCode is %d \n", bundleInfos[0].versionCode);
714    }
715    flags = 1;
716    g_errorCode = GetBundleInfos(flags, &bundleInfos, &length);
717    printf("getBundleInfo result is %d \n", g_errorCode);
718    EXPECT_EQ(g_errorCode, 0);
719        if (g_errorCode == 0){
720        printf("bundleInfos.codePath is %s \n", bundleInfos[0].codePath);
721        printf("bundleInfos.bundleName is %s \n", bundleInfos[0].bundleName);
722        printf("bundleInfos.versionCode is %d \n", bundleInfos[0].versionCode);
723    }
724    free(bundleInfos);
725    printf("------end testGetBundleInfosRight------\n");
726}
727
728/**
729 * @tc.number    : SUB_APPEXECFWK_BMS_API_0043
730 * @tc.name      : GetBundleInfos parameter illegal test
731 * @tc.desc      : [C- SOFTWARE -0200]
732 */
733HWTEST_F(BundleMgrTest, testGetBundleInfosIllegal, Function | MediumTest | Level2)
734{
735    printf("------start testGetBundleInfosIllegal------\n");
736    BundleInfo *bundleInfos = {nullptr};
737    int32_t *length = nullptr;
738    int32_t flags = 0;
739    g_errorCode = GetBundleInfos(flags, nullptr, length);
740    EXPECT_EQ(g_errorCode, 4);
741    g_errorCode = GetBundleInfos(flags, &bundleInfos, nullptr);
742    printf("g_errorCode is %d \n", g_errorCode);
743    EXPECT_EQ(g_errorCode, 4);
744    g_errorCode = GetBundleInfos(2, &bundleInfos, length);
745    printf("g_errorCode is %d \n", g_errorCode);
746    EXPECT_EQ(g_errorCode, 4);
747    printf("------end testGetBundleInfosIllegal------\n");
748}
749
750/**
751 * @tc.number    : SUB_APPEXECFWK_BMS_API_0039
752 * @tc.name      : GetBundleInfosByMetaData parameter illegal test
753 * @tc.desc      : [C- SOFTWARE -0200]
754 */
755HWTEST_F(BundleMgrTest, testGetBundleInfosByMetaDataIllegal, Function | MediumTest | Level2)
756{
757    printf("------start testGetBundleInfosByMetaDataIllegal------\n");
758    BundleInfo *bundleInfos = {nullptr};
759    int32_t length = 0;
760    const char *metaDataKey = "appId";
761    g_errorCode = GetBundleInfosByMetaData(nullptr, &bundleInfos, &length);
762    EXPECT_EQ(g_errorCode, 4);
763    g_errorCode = GetBundleInfosByMetaData(metaDataKey, &bundleInfos, nullptr);
764    printf("g_errorCode is %d \n", g_errorCode);
765    EXPECT_EQ(g_errorCode, 4);
766    g_errorCode = GetBundleInfosByMetaData(metaDataKey, nullptr, &length);
767    printf("g_errorCode is %d \n", g_errorCode);
768    EXPECT_EQ(g_errorCode, 4);
769    const char *metaDataKey1 = "noThisKey";
770    printf("metaDataKey is %s \n", metaDataKey1);
771    g_errorCode = GetBundleInfosByMetaData(metaDataKey1, &bundleInfos, &length);
772    printf("GetBundleInfosByMetaData result is %d \n", g_errorCode);
773    EXPECT_EQ(g_errorCode, 2);
774    const char *metaDataKey2 = "";
775    g_errorCode = GetBundleInfosByMetaData(metaDataKey2, &bundleInfos, &length);
776    printf("GetBundleInfosByMetaData result is %d \n", g_errorCode);
777    EXPECT_EQ(g_errorCode, 2);
778    printf("------end testGetBundleInfosByMetaDataIllegal------\n");
779}
780
781/**
782 * @tc.number    : SUB_APPEXECFWK_BMS_API_0038
783 * @tc.name      : GetBundleInfosByMetaData parameter legal test
784 * @tc.desc      : [C- SOFTWARE -0200]
785 */
786HWTEST_F(BundleMgrTest, testGetBundleInfosByMetaDataRight, Function | MediumTest | Level1)
787{
788    printf("------start testGetBundleInfosByMetaDataRight------\n");
789    BundleInfo *bundleInfos = nullptr;
790    const char *metaDataKey = "appId";
791    int32_t length = 0;
792    printf("metaDataKey is %s \n", metaDataKey);
793    g_errorCode = GetBundleInfosByMetaData(metaDataKey, &bundleInfos, &length);
794    printf("GetBundleInfosByMetaData result is %d \n", g_errorCode);
795    EXPECT_EQ(g_errorCode, 0);
796    if (g_errorCode == 0){
797        printf("bundleInfos.bundleName is %s \n", bundleInfos[0].bundleName);
798    }
799    printf("------end testGetBundleInfosByMetaDataRight------\n");
800}
801
802/**
803 * @tc.number    : SUB_APPEXECFWK_BMS_API_0037
804 * @tc.name      : QueryKeepAliveBundleInfos parameter illegal test
805 * @tc.desc      : [C- SOFTWARE -0200]
806 */
807HWTEST_F(BundleMgrTest, testQueryKeepAliveBundleInfosIllegal, Function | MediumTest | Level2)
808{
809    printf("------start testQueryKeepAliveBundleInfosIllegal------\n");
810    BundleInfo *bundleInfos = {nullptr};
811    int32_t length = 0;
812    g_errorCode = QueryKeepAliveBundleInfos(nullptr, &length);
813    printf("g_errorCode1 is %d \n", g_errorCode);
814    EXPECT_EQ(g_errorCode, 4);
815    g_errorCode = QueryKeepAliveBundleInfos(&bundleInfos, nullptr);
816    printf("g_errorCode2 is %d \n", g_errorCode);
817    EXPECT_EQ(g_errorCode, 4);
818    printf("------end testQueryKeepAliveBundleInfosIllegal------\n");
819}
820
821/**
822 * @tc.number    : SUB_APPEXECFWK_BMS_API_0034
823 * @tc.name      : GetBundleNameForUid parameter nullptr test
824 * @tc.desc      : [C- SOFTWARE -0200]
825 */
826HWTEST_F(BundleMgrTest, testGetBundleNameForUidWithNullptr, Function | MediumTest | Level2)
827{
828    printf("------start testGetBundleNameForUidWithNullptr------\n");
829    int32_t resultCode = GetBundleNameForUid(0, nullptr);
830    EXPECT_EQ(resultCode, 4);
831    printf("GetBundleNameForUid result is %d \n", resultCode);
832    printf("------end testGetBundleNameForUidWithNullptr------\n");
833}
834
835/**
836 * @tc.number    : SUB_APPEXECFWK_BMS_API_0035
837 * @tc.name      : GetBundleNameForUid parameter illegal test
838 * @tc.desc      : [C- SOFTWARE -0200]
839 */
840HWTEST_F(BundleMgrTest, testGetBundleNameForUidWithIllegal, Function | MediumTest | Level2)
841{
842    printf("------start testGetBundleNameForUidWithIllegal------\n");
843    char *bundleName = nullptr;
844    int32_t resultCode = GetBundleNameForUid(0, &bundleName);
845    EXPECT_EQ(resultCode, 114);
846    printf("GetBundleNameForUid result is %d \n", resultCode);
847    if (bundleName != nullptr) {
848        free(bundleName);
849    }
850    printf("------end testGetBundleNameForUidWithIllegal------\n");
851}
852
853/**
854 * @tc.number    : SUB_APPEXECFWK_BMS_SIZE_0001
855 * @tc.name      : Test the GetBundleSize of the thirld hap can be obtained normally
856 * @tc.desc      : [C- SOFTWARE -0200]
857 */
858HWTEST_F(BundleMgrTest, testGetBundleSizeWithLegal_0001, Function | MediumTest | Level1)
859{
860    printf("------start testGetBundleSizeWithLegal_0001------\n");
861    char *bundleName = (char*)"com.openharmony.testjsdemo";
862    uint32_t resultCode = GetBundleSize(bundleName);
863    EXPECT_GT(resultCode, 0);
864    printf("------end testGetBundleSizeWithLegal_0001------\n");
865}
866
867/**
868 * @tc.number    : SUB_APPEXECFWK_BMS_SIZE_0002
869 * @tc.name      : GetBundleSize parameter legal and bundleName length equal to 127 test
870 * @tc.desc      : [C- SOFTWARE -0200]
871 */
872HWTEST_F(BundleMgrTest, testGetBundleSizeWithLegal_0002, Function | MediumTest | Level1)
873{
874    printf("------start testGetBundleSizeWithLegal_0002------\n");
875    char *bundleName = (char*)"com.openharmony.testjsdemoBundleNameleng" \
876"thequalto127testjsdemoBundleNamelengthequalto127testjsdemoBundleNamelengthequalto127tes";
877    sem_init(&g_sem, 0, 0);
878    InstallParam installParam = {.installLocation = 1, .keepData = false };
879    string hapPath = g_testPath + "testGetBundleNameWithLegal127.hap";
880    Install(hapPath.c_str(), &installParam, TestBundleStateCallback);
881    sem_wait(&g_sem);
882    uint32_t resultCode = GetBundleSize(bundleName);
883    EXPECT_EQ(strlen(bundleName), 127);
884    EXPECT_GT(resultCode, 0);
885    // uninstall
886    sem_init(&g_sem, 0, 0);
887    Uninstall(bundleName, &installParam, TestBundleStateCallback);
888    sem_wait(&g_sem);
889    printf("------end testGetBundleSizeWithLegal_0002------\n");
890}
891
892/**
893 * @tc.number    : SUB_APPEXECFWK_BMS_SIZE_0003
894 * @tc.name      : GetBundleSize parameter illegal and bundleName length equal to 128 test
895 * @tc.desc      : [C- SOFTWARE -0200]
896 */
897HWTEST_F(BundleMgrTest, testGetBundleSizeWithIllegal_0001, Function | MediumTest | Level2)
898{
899    printf("------start testGetBundleSizeWithIllegal_0001------\n");
900    char *bundleName = (char*)"com.openharmony.testjsdemoBundleNameLength128test" \
901"jsdemoBundleNameLength128testjsdemoBundleNameLength128testjsdemoBundleNameLengt";
902    EXPECT_EQ(strlen(bundleName), 128);
903    uint32_t resultCode = GetBundleSize(bundleName);
904    EXPECT_EQ(resultCode, 0);
905    printf("------end testGetBundleSizeWithIllegal_0001------\n");
906}
907
908/**
909 * @tc.number    : SUB_APPEXECFWK_BMS_SIZE_0004
910 * @tc.name      : GetBundleSize parameter illegal and bundleName nullptr test
911 * @tc.desc      : [C- SOFTWARE -0200]
912 */
913HWTEST_F(BundleMgrTest, testGetBundleSizeWithIllegal_0002, Function | MediumTest | Level2)
914{
915    printf("------start testGetBundleSizeWithIllegal_0002------\n");
916    char *bundleName = nullptr;
917    // bundleName nullptr
918    uint32_t resultCode = GetBundleSize(bundleName);
919    EXPECT_EQ(resultCode, 0);
920    printf("------end testGetBundleSizeWithIllegal_0002------\n");
921}
922
923/**
924 * @tc.number    : SUB_APPEXECFWK_BMS_SIZE_0005
925 * @tc.name      : GetBundleSize parameter illegal and bundleName error test
926 * @tc.desc      : [C- SOFTWARE -0200]
927 */
928HWTEST_F(BundleMgrTest, testGetBundleSizeWithIllegal_0003, Function | MediumTest | Level2)
929{
930    printf("------start testGetBundleSizeWithIllegal_0003------\n");
931    char *bundleName = (char*)"com.openharmony.nothishap";
932    // error bundleName
933    uint32_t resultCode = GetBundleSize(bundleName);
934    EXPECT_EQ(resultCode, 0);
935    printf("------end testGetBundleSizeWithIllegal_0003------\n");
936}
937
938/**
939 * @tc.number    : SUB_APPEXECFWK_BMS_SIZE_0006
940 * @tc.name      : GetBundleSize parameter illegal and bundleName " " test
941 * @tc.desc      : [C- SOFTWARE -0200]
942 */
943HWTEST_F(BundleMgrTest, testGetBundleSizeWithIllegal_0004, Function | MediumTest | Level2)
944{
945    printf("------start testGetBundleSizeWithIllegal_0004------\n");
946    char *bundleName = (char*)" ";
947    // bundleName " "
948    uint32_t resultCode = GetBundleSize(bundleName);
949    EXPECT_EQ(resultCode, 0);
950    printf("------end testGetBundleSizeWithIllegal_0004------\n");
951}
952
953/**
954 * @tc.number    : SUB_APPEXECFWK_BMS_SIZE_0007
955 * @tc.name      : stress test of the same application
956 * @tc.desc      : [C- SOFTWARE -0200]
957 */
958HWTEST_F(BundleMgrTest, testStressConfig_0001, Function | MediumTest | Level2)
959{
960    printf("------start testStressConfig_0001------\n");
961    char *bundleName = (char*)"com.openharmony.testjsdemo";
962    for (int i = 1; i <= 100; i++) {
963        uint32_t resultCode = GetBundleSize(bundleName);
964        EXPECT_GT(resultCode, 0);
965    }
966    printf("------end testStressConfig_0001------\n");
967}
968
969/**
970 * @tc.number    : SUB_APPEXECFWK_BMS_SIZE_0008
971 * @tc.name      : stress test of the difference application
972 * @tc.desc      : [C- SOFTWARE -0200]
973 */
974HWTEST_F(BundleMgrTest, testStressConfig_0002, Function | MediumTest | Level2)
975{
976    printf("------start testStressConfig_0002------\n");
977    char *bundleName = (char*)"com.openharmony.testjsdemo";
978    char *bundleName2 = (char*)"com.openharmony.testjsdemoBtestjsdemoB";
979    sem_init(&g_sem, 0, 0);
980    InstallParam installParam = {.installLocation = 1, .keepData = false };
981    string hapPath = g_testPath + "frequentlyStress.hap";
982    Install(hapPath.c_str(), &installParam, TestBundleStateCallback);
983    sem_wait(&g_sem);
984    for (int i = 1; i <= 100; i++) {
985        uint32_t resultCode = GetBundleSize(bundleName);
986        EXPECT_GT(resultCode, 0);
987        resultCode = GetBundleSize(bundleName2);
988        EXPECT_GT(resultCode, 0);
989    }
990    // uninstall
991    sem_init(&g_sem, 0, 0);
992    Uninstall(bundleName2, &installParam, TestBundleStateCallback);
993    sem_wait(&g_sem);
994    printf("------end testStressConfig_0002------\n");
995}
996