bundle_mgr_test.c 13.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/**
 * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <string.h>
#include <unistd.h>

S
add  
shilei 已提交
19
#include "adapter.h"
20 21 22 23 24 25
#include "hctest.h"
#include "securec.h"
#include "bundle_info.h"
#include "bundle_manager.h"
#include "want.h"

S
add  
shilei 已提交
26 27
#define nullptr NULL

28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
/**
* @brief  register a test suit named BundleMgrTestSuite
* @param  subsystem name is appexecfwk
* @param  module name is  bundlemgr
* @param  test suit name is BundleMgrTestSuite
*/
LITE_TEST_SUIT(appexecfwk, bundlemgr, BundleMgrTestSuite);

static BOOL BundleMgrTestSuiteSetUp(void)
{
    printf("----------test case with BundleMgrTest start-------------\n");
    return TRUE;
}

static BOOL BundleMgrTestSuiteTearDown(void)
{
    printf("----------test case with BundleMgrTest end-------------\n");
    return TRUE;
}

/**
 * @tc.number    : SUB_APPEXECFWK_0001
50 51 52 53 54 55 56
 * @tc.name      : testClearAbilityInfoLegal
 * @tc.desc      : testClearAbilityInfo parameter legal test with bundle name
 */
LITE_TEST_CASE(BundleMgrTestSuite, testClearAbilityInfoLegal, Function | MediumTest | Level2)
{
    printf("------start testClearAbilityInfo------\n");
    AbilityInfo abilityInfo;
R
ry 已提交
57 58
    int result = memset_s(&abilityInfo, sizeof(abilityInfo), 0, sizeof(abilityInfo));
    TEST_ASSERT_TRUE(result == 0);
S
add  
shilei 已提交
59 60
    char *name = "com.openharmony.testjsdemo";
    size_t len = strlen(name);
S
add  
shilei 已提交
61
    abilityInfo.bundleName = (char *)(AdapterMalloc(len + 1));
S
add  
shilei 已提交
62 63 64 65
    TEST_ASSERT_NOT_NULL(abilityInfo.bundleName);
    errno_t err = strncpy_s(abilityInfo.bundleName, len + 1, name, len);
    TEST_ASSERT_EQUAL(err, EOK);
    TEST_ASSERT_EQUAL_STRING(abilityInfo.bundleName, name);
66 67 68 69 70 71 72 73 74
    ClearAbilityInfo(&abilityInfo);
    TEST_ASSERT_EQUAL_STRING(abilityInfo.bundleName, NULL);
    printf("------end testClearAbilityInfo------\n");
}

/**
 * @tc.number    : SUB_APPEXECFWK_0002
 * @tc.name      : testClearAbilityInfoIllegal
 * @tc.desc      : testClearAbilityInfo parameter illegal test
75 76 77 78 79
 */
LITE_TEST_CASE(BundleMgrTestSuite, testClearAbilityInfoIllegal, Function | MediumTest | Level2)
{
    printf("------start testClearAbilityInfoIllegal------\n");
    AbilityInfo abilityInfo = { 0 };
R
ry 已提交
80 81
    int result = memset_s(&abilityInfo, sizeof(abilityInfo), 0, sizeof(abilityInfo));
    TEST_ASSERT_TRUE(result == 0);
S
add  
shilei 已提交
82 83
    char *name = "com.openharmony.testjsdemo";
    size_t len = strlen(name);
S
add  
shilei 已提交
84
    abilityInfo.bundleName = (char *)(AdapterMalloc(len + 1));
S
add  
shilei 已提交
85 86 87
    TEST_ASSERT_NOT_NULL(abilityInfo.bundleName);
    errno_t err = strncpy_s(abilityInfo.bundleName, len + 1, name, len);
    TEST_ASSERT_EQUAL(err, EOK);
88
    ClearAbilityInfo(NULL);
S
add  
shilei 已提交
89
    TEST_ASSERT_EQUAL_STRING(abilityInfo.bundleName, name);
S
add  
shilei 已提交
90
    AdapterFree(abilityInfo.bundleName);
91 92 93 94
    printf("------end testClearAbilityInfoIllegal------\n");
}

/**
95 96 97 98 99 100 101 102
 * @tc.number    : SUB_APPEXECFWK_0003
 * @tc.name      : testClearBundleInfoLegal
 * @tc.desc      : testClearBundleInfo parameter legal test with bundle name
 */
LITE_TEST_CASE(BundleMgrTestSuite, testClearBundleInfoLegal, Function | MediumTest | Level2)
{
    printf("------start testClearBundleInfo------\n");
    BundleInfo bundleInfo = { 0 };
R
ry 已提交
103 104
    int result = memset_s(&bundleInfo, sizeof(bundleInfo), 0, sizeof(bundleInfo));
    TEST_ASSERT_TRUE(result == 0);
S
add  
shilei 已提交
105 106
    char *name = "com.openharmony.testjsdemo";
    size_t len = strlen(name);
S
add  
shilei 已提交
107
    bundleInfo.bundleName = (char *)(AdapterMalloc(len + 1));
S
add  
shilei 已提交
108 109 110 111
    TEST_ASSERT_NOT_NULL(bundleInfo.bundleName);
    errno_t err = strncpy_s(bundleInfo.bundleName, len + 1, name, len);
    TEST_ASSERT_EQUAL(err, EOK);
    TEST_ASSERT_EQUAL_STRING(bundleInfo.bundleName, name);
112 113 114 115 116 117 118 119 120
    ClearBundleInfo(&bundleInfo);
    TEST_ASSERT_EQUAL_STRING(bundleInfo.bundleName, NULL);
    printf("------end testClearBundleInfo------\n");
}

/**
 * @tc.number    : SUB_APPEXECFWK_0004
 * @tc.name      : testClearBundleInfoIllegal
 * @tc.desc      : testClearBundleInfo parameter illegal test
121 122 123 124
 */
LITE_TEST_CASE(BundleMgrTestSuite, testClearBundleInfoIllegal, Function | MediumTest | Level2)
{
    printf("------start testClearBundleInfoIllegal------\n");
125
    BundleInfo bundleInfo;
R
ry 已提交
126 127
    int result = memset_s(&bundleInfo, sizeof(bundleInfo), 0, sizeof(bundleInfo));
    TEST_ASSERT_TRUE(result == 0);
S
add  
shilei 已提交
128 129
    char *name = "com.openharmony.testjsdemo";
    size_t len = strlen(name);
S
add  
shilei 已提交
130
    bundleInfo.bundleName = (char *)(AdapterMalloc(len + 1));
S
add  
shilei 已提交
131 132 133
    TEST_ASSERT_NOT_NULL(bundleInfo.bundleName);
    errno_t err = strncpy_s(bundleInfo.bundleName, len + 1, name, len);
    TEST_ASSERT_EQUAL(err, EOK);
134
    ClearBundleInfo(NULL);
S
add  
shilei 已提交
135
    TEST_ASSERT_EQUAL_STRING(bundleInfo.bundleName, name);
S
add  
shilei 已提交
136
    AdapterFree(bundleInfo.bundleName);
137 138 139 140
    printf("------end testClearBundleInfoIllegal------\n");
}

/**
141 142 143
 * @tc.number    : SUB_APPEXECFWK_0005
 * @tc.name      : testClearModuleInfoLegal
 * @tc.desc      : ClearAbilityInfo parameter legal test with module info
144
 */
145 146 147 148
LITE_TEST_CASE(BundleMgrTestSuite, testClearModuleInfoLegal, Function | MediumTest | Level1)
{
    printf("------start testClearModuleInfo------\n");
    ModuleInfo moduleInfo = { 0 };
R
ry 已提交
149 150
    int result = memset_s(&moduleInfo, sizeof(moduleInfo), 0, sizeof(moduleInfo));
    TEST_ASSERT_TRUE(result == 0);
S
add  
shilei 已提交
151 152
    char *name = "test";
    size_t len = strlen(name);
S
add  
shilei 已提交
153
    moduleInfo.moduleName = (char *)(AdapterMalloc(len + 1));
S
add  
shilei 已提交
154 155 156 157
    TEST_ASSERT_NOT_NULL(moduleInfo.moduleName);
    errno_t err = strncpy_s(moduleInfo.moduleName, len + 1, name, len);
    TEST_ASSERT_EQUAL(err, EOK);
    TEST_ASSERT_EQUAL_STRING(moduleInfo.moduleName, name);
158 159 160 161 162 163 164 165 166 167 168 169 170 171
    ClearModuleInfo(&moduleInfo);
    TEST_ASSERT_EQUAL_STRING(moduleInfo.moduleName, NULL);
    printf("------end testClearModuleInfo------\n");
}

/**
 * @tc.number    : SUB_APPEXECFWK_0006
 * @tc.name      : testClearModuleInfoIllegal
 * @tc.desc      : ClearAbilityInfo parameter illegal test
 */
LITE_TEST_CASE(BundleMgrTestSuite, testClearModuleInfoIllegal, Function | MediumTest | Level1)
{
    printf("------start testClearModuleInfoIllegal------\n");
    ModuleInfo moduleInfo = { 0 };
R
ry 已提交
172 173
    int result = memset_s(&moduleInfo, sizeof(moduleInfo), 0, sizeof(moduleInfo));
    TEST_ASSERT_TRUE(result == 0);
S
add  
shilei 已提交
174 175
    char *name = "test";
    size_t len = strlen(name);
S
add  
shilei 已提交
176
    moduleInfo.moduleName = (char *)(AdapterMalloc(len + 1));
S
add  
shilei 已提交
177 178 179
    TEST_ASSERT_NOT_NULL(moduleInfo.moduleName);
    errno_t err = strncpy_s(moduleInfo.moduleName, len + 1, name, len);
    TEST_ASSERT_EQUAL(err, EOK);
180
    ClearModuleInfo(NULL);
S
add  
shilei 已提交
181
    TEST_ASSERT_EQUAL_STRING(moduleInfo.moduleName, name);
S
add  
shilei 已提交
182
    AdapterFree(moduleInfo.moduleName);
183 184 185 186 187 188 189 190 191
    printf("------end testClearModuleInfoIllegal------\n");
}

/**
 * @tc.number    : SUB_APPEXECFWK_0007
 * @tc.name      : testSetElementAbilityNameLegal
 * @tc.desc      : testSetElementAbilityName parameter legal test
 */
LITE_TEST_CASE(BundleMgrTestSuite, testSetElementAbilityNameLegal, Function | MediumTest | Level0)
192 193 194 195 196 197 198 199 200 201 202 203 204 205
{
    printf("------start testSetElementAbilityName------\n");
    Want want = { 0 };
    ElementName element = { 0 };
    SetElementAbilityName(&element, "SecondAbility");
    SetWantElement(&want, element);
    char *aName = "SecondAbility";
    TEST_ASSERT_EQUAL_STRING(want.element->abilityName, aName);
    ClearElement(&element);
    ClearWant(&want);
    printf("------end testSetElementAbilityName------\n");
}

/**
206 207 208
 * @tc.number    : SUB_APPEXECFWK_0008
 * @tc.name      : testSetElementAbilityNameIllegal
 * @tc.desc      : testSetElementAbilityName parameter illegal test
209 210 211 212 213 214 215 216 217 218 219 220 221
 */
LITE_TEST_CASE(BundleMgrTestSuite, testSetElementAbilityNameIllegal, Function | MediumTest | Level2)
{
    printf("------start testSetElementAbilityNameIllegal------\n");
    Want want = { 0 };
    ElementName element = { 0 };
    char *aName = "";
    SetElementAbilityName(&element, aName);
    SetWantElement(&want, element);
    TEST_ASSERT_EQUAL_STRING(want.element->abilityName, "");
    SetElementAbilityName(&element, NULL);
    SetWantElement(&want, element);
    TEST_ASSERT_EQUAL_STRING(want.element->abilityName, NULL);
222 223
    int ret = SetElementAbilityName(NULL, aName);
    TEST_ASSERT_FALSE(ret);
224 225 226 227 228 229
    ClearElement(&element);
    ClearWant(&want);
    printf("------end testSetElementAbilityNameIllegal------\n");
}

/**
230 231 232
 * @tc.number    : SUB_APPEXECFWK_0009
 * @tc.name      : testSetElementBundleNameLegal
 * @tc.desc      : testSetElementBundleName parameter legal test
233
 */
234
LITE_TEST_CASE(BundleMgrTestSuite, testSetElementBundleNameLegal, Function | MediumTest | Level0)
235 236 237 238 239 240 241 242 243 244 245 246 247 248
{
    printf("------start testSetElementBundleName------\n");
    Want want = { 0 };
    ElementName element = { 0 };
    SetElementBundleName(&element, "com.openharmony.testjsdemo");
    SetWantElement(&want, element);
    char *bName = "com.openharmony.testjsdemo";
    TEST_ASSERT_EQUAL_STRING(want.element->bundleName, bName);
    ClearElement(&element);
    ClearWant(&want);
    printf("------end testSetElementBundleName------\n");
}

/**
249 250 251
 * @tc.number    : SUB_APPEXECFWK_0010
 * @tc.name      : testSetElementBundleNameIllegal
 * @tc.desc      : testSetElementBundleName parameter illegal test
252 253 254 255 256 257 258 259 260 261 262 263 264
 */
LITE_TEST_CASE(BundleMgrTestSuite, testSetElementBundleNameIllegal, Function | MediumTest | Level2)
{
    printf("------start testSetElementBundleNameIllegal------\n");
    Want want = { 0 };
    ElementName element = { 0 };
    SetElementBundleName(&element, "");
    SetWantElement(&want, element);
    char *bName = "";
    TEST_ASSERT_EQUAL_STRING(want.element->bundleName, bName);
    SetElementBundleName(&element, NULL);
    SetWantElement(&want, element);
    TEST_ASSERT_EQUAL_STRING(want.element->bundleName, NULL);
265 266
    bool ret = SetElementBundleName(NULL, bName);
    TEST_ASSERT_FALSE(ret);
267 268 269 270 271
    ClearElement(&element);
    ClearWant(&want);
    printf("------end testSetElementBundleNameIllegal------\n");
}
/**
272 273 274
 * @tc.number    : SUB_APPEXECFWK_0011
 * @tc.name      : testSetElementDeviceIDLegal
 * @tc.desc      : testSetElementDeviceID parameter legal test
275
 */
276
LITE_TEST_CASE(BundleMgrTestSuite, testSetElementDeviceIDLegal, Function | MediumTest | Level0)
277 278 279 280 281 282 283 284 285 286 287 288 289 290
{
    printf("------start testSetElementDeviceID------\n");
    Want want = { 0 };
    ElementName element = { 0 };
    SetElementDeviceID(&element, "0001000");
    SetWantElement(&want, element);
    char *dID = "0001000";
    TEST_ASSERT_EQUAL_STRING(want.element->deviceId, dID);
    ClearElement(&element);
    ClearWant(&want);
    printf("------end testSetElementDeviceID------\n");
}

/**
291 292 293
 * @tc.number    : SUB_APPEXECFWK_0012
 * @tc.name      : testSetElementDeviceIDIllegal
 * @tc.desc      : testSetElementDeviceID parameter illegal test
294 295 296 297 298 299 300 301 302 303 304 305 306
 */
LITE_TEST_CASE(BundleMgrTestSuite, testSetElementDeviceIDIllegal, Function | MediumTest | Level2)
{
    printf("------start testSetElementDeviceIDIllegal------\n");
    Want want = { 0 };
    ElementName element = { 0 };
    SetElementDeviceID(&element, "");
    SetWantElement(&want, element);
    char *dID = "";
    TEST_ASSERT_EQUAL_STRING(want.element->deviceId, dID);
    SetElementDeviceID(&element, NULL);
    SetWantElement(&want, element);
    TEST_ASSERT_EQUAL_STRING(want.element->deviceId, NULL);
307 308
    int ret = SetElementDeviceID(NULL, "0001000");
    TEST_ASSERT_FALSE(ret);
309 310 311 312 313
    ClearElement(&element);
    ClearWant(&want);
    printf("------end testSetElementDeviceIDIllegal------\n");
}

314
/**
315 316 317
 * @tc.number    : SUB_APPEXECFWK_0013
 * @tc.name      : testGetBundleInfoIllegal
 * @tc.desc      : GetBundleInfo parameter illegal test
318 319 320 321 322
 */
LITE_TEST_CASE(BundleMgrTestSuite, testGetBundleInfoIllegal, Function | MediumTest | Level2)
{
    printf("------start testGetBundleInfoIllegal------\n");
    BundleInfo bundleInfo;
R
ry 已提交
323 324
    int result = memset_s(&bundleInfo, sizeof(bundleInfo), 0, sizeof(bundleInfo));
    TEST_ASSERT_TRUE(result == 0);
325
    const char *bundleName = "com.openharmony.nothishap";
R
ry 已提交
326
    int flags = 0;
327 328 329 330 331 332 333 334 335 336
    uint8_t ret = GetBundleInfo(bundleName, flags, &bundleInfo);
    TEST_ASSERT_TRUE(ret == 2);
    ret = GetBundleInfo(NULL, flags, &bundleInfo);
    printf("abilityInfo2 is %d \n", ret);
    TEST_ASSERT_TRUE(ret == 1);
    ret = GetBundleInfo("", flags, &bundleInfo);
    TEST_ASSERT_TRUE(ret == 2);
    ret = GetBundleInfo("com.openharmony.testjsdemo", 2, &bundleInfo);
    sleep(2);
    TEST_ASSERT_TRUE(ret == 2);
337
    printf("------end testGetBundleInfoIllegal------\n");
338 339 340
}

/**
341 342 343
 * @tc.number    : SUB_APPEXECFWK_0014
 * @tc.name      : testGetBundleInfosIllegal
 * @tc.desc      : GetBundleInfos parameter illegal test
344 345 346 347 348
 */
LITE_TEST_CASE(BundleMgrTestSuite, testGetBundleInfosIllegal, Function | MediumTest | Level2)
{
    printf("------start testGetBundleInfosIllegal------\n");
    BundleInfo *bundleInfos = {NULL};
R
ry 已提交
349 350
    int *length = NULL;
    int flags = 0;
351
    uint8_t ret = GetBundleInfos(flags, NULL, length);
352
    TEST_ASSERT_TRUE(ret == 1);
353 354 355 356 357 358 359 360 361
    ret = GetBundleInfos(flags, &bundleInfos, NULL);
    printf("ret is %d \n", ret);
    TEST_ASSERT_TRUE(ret == 2);
    ret = GetBundleInfos(2, &bundleInfos, length);
    printf("ret is %d \n", ret);
    TEST_ASSERT_TRUE(ret == 2);
    printf("------end testGetBundleInfosIllegal------\n");
}

362
RUN_TEST_SUITE(BundleMgrTestSuite);