1 /**
2 * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <string.h>
17 #include <unistd.h>
18
19 #include "hctest.h"
20 #include "securec.h"
21 #include "bundle_info.h"
22 #include "bundle_manager.h"
23 #include "want.h"
24
25 /**
26 * @brief register a test suit named BundleMgrTestSuite
27 * @param subsystem name is appexecfwk
28 * @param module name is bundlemgr
29 * @param test suit name is BundleMgrTestSuite
30 */
31 LITE_TEST_SUIT(appexecfwk, bundlemgr, BundleMgrTestSuite);
32
BundleMgrTestSuiteSetUp(void)33 static BOOL BundleMgrTestSuiteSetUp(void)
34 {
35 printf("----------test case with BundleMgrTest start-------------\n");
36 return TRUE;
37 }
38
BundleMgrTestSuiteTearDown(void)39 static BOOL BundleMgrTestSuiteTearDown(void)
40 {
41 printf("----------test case with BundleMgrTest end-------------\n");
42 return TRUE;
43 }
44
45 /**
46 * @tc.number : SUB_APPEXECFWK_0001
47 * @tc.name : testClearAbilityInfoLegal
48 * @tc.desc : testClearAbilityInfo parameter legal test with bundle name
49 */
50 LITE_TEST_CASE(BundleMgrTestSuite, testClearAbilityInfoLegal, Function | MediumTest | Level2)
51 {
52 printf("------start testClearAbilityInfo------\n");
53 AbilityInfo abilityInfo;
54 int result = memset_s(&abilityInfo, sizeof(abilityInfo), 0, sizeof(abilityInfo));
55 TEST_ASSERT_TRUE(result == 0);
56 abilityInfo.bundleName = "com.openharmony.testjsdemo";
57 TEST_ASSERT_EQUAL_STRING(abilityInfo.bundleName, "com.openharmony.testjsdemo");
58 ClearAbilityInfo(&abilityInfo);
59 TEST_ASSERT_EQUAL_STRING(abilityInfo.bundleName, NULL);
60 printf("------end testClearAbilityInfo------\n");
61 }
62
63 /**
64 * @tc.number : SUB_APPEXECFWK_0002
65 * @tc.name : testClearAbilityInfoIllegal
66 * @tc.desc : testClearAbilityInfo parameter illegal test
67 */
68 LITE_TEST_CASE(BundleMgrTestSuite, testClearAbilityInfoIllegal, Function | MediumTest | Level2)
69 {
70 printf("------start testClearAbilityInfoIllegal------\n");
71 AbilityInfo abilityInfo = { 0 };
72 int result = memset_s(&abilityInfo, sizeof(abilityInfo), 0, sizeof(abilityInfo));
73 TEST_ASSERT_TRUE(result == 0);
74 abilityInfo.bundleName = "com.openharmony.testjsdemo";
75 ClearAbilityInfo(NULL);
76 TEST_ASSERT_EQUAL_STRING(abilityInfo.bundleName, "com.openharmony.testjsdemo");
77 printf("------end testClearAbilityInfoIllegal------\n");
78 }
79
80 /**
81 * @tc.number : SUB_APPEXECFWK_0003
82 * @tc.name : testClearBundleInfoLegal
83 * @tc.desc : testClearBundleInfo parameter legal test with bundle name
84 */
85 LITE_TEST_CASE(BundleMgrTestSuite, testClearBundleInfoLegal, Function | MediumTest | Level2)
86 {
87 printf("------start testClearBundleInfo------\n");
88 BundleInfo bundleInfo = { 0 };
89 int result = memset_s(&bundleInfo, sizeof(bundleInfo), 0, sizeof(bundleInfo));
90 TEST_ASSERT_TRUE(result == 0);
91 bundleInfo.bundleName = "com.openharmony.testjsdemo";
92 TEST_ASSERT_EQUAL_STRING(bundleInfo.bundleName, "com.openharmony.testjsdemo");
93 ClearBundleInfo(&bundleInfo);
94 TEST_ASSERT_EQUAL_STRING(bundleInfo.bundleName, NULL);
95 printf("------end testClearBundleInfo------\n");
96 }
97
98 /**
99 * @tc.number : SUB_APPEXECFWK_0004
100 * @tc.name : testClearBundleInfoIllegal
101 * @tc.desc : testClearBundleInfo parameter illegal test
102 */
103 LITE_TEST_CASE(BundleMgrTestSuite, testClearBundleInfoIllegal, Function | MediumTest | Level2)
104 {
105 printf("------start testClearBundleInfoIllegal------\n");
106 BundleInfo bundleInfo;
107 int result = memset_s(&bundleInfo, sizeof(bundleInfo), 0, sizeof(bundleInfo));
108 TEST_ASSERT_TRUE(result == 0);
109 bundleInfo.bundleName = "com.openharmony.testjsdemo";
110 ClearBundleInfo(NULL);
111 TEST_ASSERT_EQUAL_STRING(bundleInfo.bundleName, "com.openharmony.testjsdemo");
112 printf("------end testClearBundleInfoIllegal------\n");
113 }
114
115 /**
116 * @tc.number : SUB_APPEXECFWK_0005
117 * @tc.name : testClearModuleInfoLegal
118 * @tc.desc : ClearAbilityInfo parameter legal test with module info
119 */
120 LITE_TEST_CASE(BundleMgrTestSuite, testClearModuleInfoLegal, Function | MediumTest | Level1)
121 {
122 printf("------start testClearModuleInfo------\n");
123 ModuleInfo moduleInfo = { 0 };
124 int result = memset_s(&moduleInfo, sizeof(moduleInfo), 0, sizeof(moduleInfo));
125 TEST_ASSERT_TRUE(result == 0);
126 moduleInfo.moduleName = "test";
127 TEST_ASSERT_EQUAL_STRING(moduleInfo.moduleName, "test");
128 ClearModuleInfo(&moduleInfo);
129 TEST_ASSERT_EQUAL_STRING(moduleInfo.moduleName, NULL);
130 printf("------end testClearModuleInfo------\n");
131 }
132
133 /**
134 * @tc.number : SUB_APPEXECFWK_0006
135 * @tc.name : testClearModuleInfoIllegal
136 * @tc.desc : ClearAbilityInfo parameter illegal test
137 */
138 LITE_TEST_CASE(BundleMgrTestSuite, testClearModuleInfoIllegal, Function | MediumTest | Level1)
139 {
140 printf("------start testClearModuleInfoIllegal------\n");
141 ModuleInfo moduleInfo = { 0 };
142 int result = memset_s(&moduleInfo, sizeof(moduleInfo), 0, sizeof(moduleInfo));
143 TEST_ASSERT_TRUE(result == 0);
144 moduleInfo.moduleName = "test";
145 ClearModuleInfo(NULL);
146 TEST_ASSERT_EQUAL_STRING(moduleInfo.moduleName, "test");
147 printf("------end testClearModuleInfoIllegal------\n");
148 }
149
150 /**
151 * @tc.number : SUB_APPEXECFWK_0007
152 * @tc.name : testSetElementAbilityNameLegal
153 * @tc.desc : testSetElementAbilityName parameter legal test
154 */
155 LITE_TEST_CASE(BundleMgrTestSuite, testSetElementAbilityNameLegal, Function | MediumTest | Level0)
156 {
157 printf("------start testSetElementAbilityName------\n");
158 Want want = { 0 };
159 ElementName element = { 0 };
160 SetElementAbilityName(&element, "SecondAbility");
161 SetWantElement(&want, element);
162 char *aName = "SecondAbility";
163 TEST_ASSERT_EQUAL_STRING(want.element->abilityName, aName);
164 ClearElement(&element);
165 ClearWant(&want);
166 printf("------end testSetElementAbilityName------\n");
167 }
168
169 /**
170 * @tc.number : SUB_APPEXECFWK_0008
171 * @tc.name : testSetElementAbilityNameIllegal
172 * @tc.desc : testSetElementAbilityName parameter illegal test
173 */
174 LITE_TEST_CASE(BundleMgrTestSuite, testSetElementAbilityNameIllegal, Function | MediumTest | Level2)
175 {
176 printf("------start testSetElementAbilityNameIllegal------\n");
177 Want want = { 0 };
178 ElementName element = { 0 };
179 char *aName = "";
180 SetElementAbilityName(&element, aName);
181 SetWantElement(&want, element);
182 TEST_ASSERT_EQUAL_STRING(want.element->abilityName, "");
183 SetElementAbilityName(&element, NULL);
184 SetWantElement(&want, element);
185 TEST_ASSERT_EQUAL_STRING(want.element->abilityName, NULL);
186 int ret = SetElementAbilityName(NULL, aName);
187 TEST_ASSERT_FALSE(ret);
188 ClearElement(&element);
189 ClearWant(&want);
190 printf("------end testSetElementAbilityNameIllegal------\n");
191 }
192
193 /**
194 * @tc.number : SUB_APPEXECFWK_0009
195 * @tc.name : testSetElementBundleNameLegal
196 * @tc.desc : testSetElementBundleName parameter legal test
197 */
198 LITE_TEST_CASE(BundleMgrTestSuite, testSetElementBundleNameLegal, Function | MediumTest | Level0)
199 {
200 printf("------start testSetElementBundleName------\n");
201 Want want = { 0 };
202 ElementName element = { 0 };
203 SetElementBundleName(&element, "com.openharmony.testjsdemo");
204 SetWantElement(&want, element);
205 char *bName = "com.openharmony.testjsdemo";
206 TEST_ASSERT_EQUAL_STRING(want.element->bundleName, bName);
207 ClearElement(&element);
208 ClearWant(&want);
209 printf("------end testSetElementBundleName------\n");
210 }
211
212 /**
213 * @tc.number : SUB_APPEXECFWK_0010
214 * @tc.name : testSetElementBundleNameIllegal
215 * @tc.desc : testSetElementBundleName parameter illegal test
216 */
217 LITE_TEST_CASE(BundleMgrTestSuite, testSetElementBundleNameIllegal, Function | MediumTest | Level2)
218 {
219 printf("------start testSetElementBundleNameIllegal------\n");
220 Want want = { 0 };
221 ElementName element = { 0 };
222 SetElementBundleName(&element, "");
223 SetWantElement(&want, element);
224 char *bName = "";
225 TEST_ASSERT_EQUAL_STRING(want.element->bundleName, bName);
226 SetElementBundleName(&element, NULL);
227 SetWantElement(&want, element);
228 TEST_ASSERT_EQUAL_STRING(want.element->bundleName, NULL);
229 bool ret = SetElementBundleName(NULL, bName);
230 TEST_ASSERT_FALSE(ret);
231 ClearElement(&element);
232 ClearWant(&want);
233 printf("------end testSetElementBundleNameIllegal------\n");
234 }
235 /**
236 * @tc.number : SUB_APPEXECFWK_0011
237 * @tc.name : testSetElementDeviceIDLegal
238 * @tc.desc : testSetElementDeviceID parameter legal test
239 */
240 LITE_TEST_CASE(BundleMgrTestSuite, testSetElementDeviceIDLegal, Function | MediumTest | Level0)
241 {
242 printf("------start testSetElementDeviceID------\n");
243 Want want = { 0 };
244 ElementName element = { 0 };
245 SetElementDeviceID(&element, "0001000");
246 SetWantElement(&want, element);
247 char *dID = "0001000";
248 TEST_ASSERT_EQUAL_STRING(want.element->deviceId, dID);
249 ClearElement(&element);
250 ClearWant(&want);
251 printf("------end testSetElementDeviceID------\n");
252 }
253
254 /**
255 * @tc.number : SUB_APPEXECFWK_0012
256 * @tc.name : testSetElementDeviceIDIllegal
257 * @tc.desc : testSetElementDeviceID parameter illegal test
258 */
259 LITE_TEST_CASE(BundleMgrTestSuite, testSetElementDeviceIDIllegal, Function | MediumTest | Level2)
260 {
261 printf("------start testSetElementDeviceIDIllegal------\n");
262 Want want = { 0 };
263 ElementName element = { 0 };
264 SetElementDeviceID(&element, "");
265 SetWantElement(&want, element);
266 char *dID = "";
267 TEST_ASSERT_EQUAL_STRING(want.element->deviceId, dID);
268 SetElementDeviceID(&element, NULL);
269 SetWantElement(&want, element);
270 TEST_ASSERT_EQUAL_STRING(want.element->deviceId, NULL);
271 int ret = SetElementDeviceID(NULL, "0001000");
272 TEST_ASSERT_FALSE(ret);
273 ClearElement(&element);
274 ClearWant(&want);
275 printf("------end testSetElementDeviceIDIllegal------\n");
276 }
277
278 /**
279 * @tc.number : SUB_APPEXECFWK_0013
280 * @tc.name : testGetBundleInfoIllegal
281 * @tc.desc : GetBundleInfo parameter illegal test
282 */
283 LITE_TEST_CASE(BundleMgrTestSuite, testGetBundleInfoIllegal, Function | MediumTest | Level2)
284 {
285 printf("------start testGetBundleInfoIllegal------\n");
286 BundleInfo bundleInfo;
287 int result = memset_s(&bundleInfo, sizeof(bundleInfo), 0, sizeof(bundleInfo));
288 TEST_ASSERT_TRUE(result == 0);
289 const char *bundleName = "com.openharmony.nothishap";
290 int flags = 0;
291 uint8_t ret = GetBundleInfo(bundleName, flags, &bundleInfo);
292 TEST_ASSERT_TRUE(ret == 2);
293 ret = GetBundleInfo(NULL, flags, &bundleInfo);
294 printf("abilityInfo2 is %d \n", ret);
295 TEST_ASSERT_TRUE(ret == 1);
296 ret = GetBundleInfo("", flags, &bundleInfo);
297 TEST_ASSERT_TRUE(ret == 2);
298 ret = GetBundleInfo("com.openharmony.testjsdemo", 2, &bundleInfo);
299 sleep(2);
300 TEST_ASSERT_TRUE(ret == 2);
301 printf("------end testGetBundleInfoIllegal------\n");
302 }
303
304 /**
305 * @tc.number : SUB_APPEXECFWK_0014
306 * @tc.name : testGetBundleInfosIllegal
307 * @tc.desc : GetBundleInfos parameter illegal test
308 */
309 LITE_TEST_CASE(BundleMgrTestSuite, testGetBundleInfosIllegal, Function | MediumTest | Level2)
310 {
311 printf("------start testGetBundleInfosIllegal------\n");
312 BundleInfo *bundleInfos = {NULL};
313 int *length = NULL;
314 int flags = 0;
315 uint8_t ret = GetBundleInfos(flags, NULL, length);
316 TEST_ASSERT_TRUE(ret == 1);
317 ret = GetBundleInfos(flags, &bundleInfos, NULL);
318 printf("ret is %d \n", ret);
319 TEST_ASSERT_TRUE(ret == 2);
320 ret = GetBundleInfos(2, &bundleInfos, length);
321 printf("ret is %d \n", ret);
322 TEST_ASSERT_TRUE(ret == 2);
323 printf("------end testGetBundleInfosIllegal------\n");
324 }
325
326 RUN_TEST_SUITE(BundleMgrTestSuite);
327