• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <stdio.h>
17 #include <stdlib.h>
18 #include <string.h>
19 #include <sys/types.h>
20 #include <sys/stat.h>
21 #include <fcntl.h>
22 
23 #include "hctest.h"
24 #include "ohos_types.h"
25 #include "slite/ability_manager.h"
26 #include "ability_errors.h"
27 #include "want.h"
28 
29 #define DATA_EXTERN (2 * 3)
30 
31 /**
32 * @brief  register a test suit named AbilityMgrTestSuite
33 * @param  subsystem name is aafwk
34 * @param  module name is  abilitymgr
35 * @param  test suit name is AbilityMgrTestSuite
36 */
37 LITE_TEST_SUIT(aafwk, abilitymgr, AbilityMgrTestSuite);
38 
AbilityMgrTestSuiteSetUp(void)39 static BOOL AbilityMgrTestSuiteSetUp(void)
40 {
41     printf("----------test case with AbilityMgrTest start-------------\n");
42     return TRUE;
43 }
44 
AbilityMgrTestSuiteTearDown(void)45 static BOOL AbilityMgrTestSuiteTearDown(void)
46 {
47     printf("----------test case with AbilityMgrTest end-------------\n");
48     return TRUE;
49 }
50 
51 /**
52  * @tc.number    : SUB_AAFWK_ABILITY_0001
53  * @tc.name      : testClearElementLegal
54  * @tc.desc      : testClearElement parameter legal test
55  */
56 LITE_TEST_CASE(AbilityMgrTestSuite, testClearElementLegal, Function | MediumTest | Level0)
57 {
58     printf("------start testClearElementLegal------\n");
59     ElementName element = { 0 };
60     SetElementDeviceID(&element, "0001000");
61     SetElementBundleName(&element, "com.openharmony.testnative");
62     bool ret = SetElementAbilityName(&element, "SecondAbility");
63     if (ret) {
64         char aName[] = "SecondAbility";
65         char bName[] = "com.openharmony.testnative";
66         char dID[] = "0001000";
67         TEST_ASSERT_EQUAL_STRING(element.abilityName, aName);
68         TEST_ASSERT_EQUAL_STRING(element.bundleName, bName);
69         TEST_ASSERT_EQUAL_STRING(element.deviceId, dID);
70         ClearElement(&element);
71         TEST_ASSERT_EQUAL_STRING(element.abilityName, NULL);
72         TEST_ASSERT_EQUAL_STRING(element.bundleName, NULL);
73         TEST_ASSERT_EQUAL_STRING(element.deviceId, NULL);
74     }
75     printf("------end testClearElementLegal------\n");
76 }
77 
78 /**
79  * @tc.number    : SUB_AAFWK_ABILITY_0002
80  * @tc.name      : testClearElementIllegal
81  * @tc.desc      : testClearElement parameter illegal test
82  */
83 LITE_TEST_CASE(AbilityMgrTestSuite, testClearElementIllegal, Function | MediumTest | Level0)
84 {
85     printf("------start testClearElementIllegal------\n");
86     ElementName element = { 0 };
87     SetElementDeviceID(&element, "0001000");
88     SetElementBundleName(&element, "com.openharmony.testnative");
89     bool ret = SetElementAbilityName(&element, "SecondAbility");
90     if (ret) {
91         char aName[] = "SecondAbility";
92         char bName[] = "com.openharmony.testnative";
93         char dID[] = "0001000";
94         TEST_ASSERT_EQUAL_STRING(element.abilityName, aName);
95         TEST_ASSERT_EQUAL_STRING(element.bundleName, bName);
96         TEST_ASSERT_EQUAL_STRING(element.deviceId, dID);
97         ClearElement(NULL);
98         TEST_ASSERT_EQUAL_STRING(element.abilityName, aName);
99         TEST_ASSERT_EQUAL_STRING(element.bundleName, bName);
100         TEST_ASSERT_EQUAL_STRING(element.deviceId, dID);
101     }
102     printf("------end testClearElementIllegal------\n");
103 }
104 
105 /**
106  * @tc.number    : SUB_AAFWK_ABILITY_0003
107  * @tc.name      : testSetWantElementLegal
108  * @tc.desc      : testSetWantElement parameter legal test
109  */
110 LITE_TEST_CASE(AbilityMgrTestSuite, testSetWantElementLegal, Function | MediumTest | Level0)
111 {
112     printf("------start testSetWantElementLegal------\n");
113     Want want = { 0 };
114     ElementName element = { 0 };
115     SetElementDeviceID(&element, "0001000");
116     SetElementBundleName(&element, "com.openharmony.testnative");
117     SetElementAbilityName(&element, "SecondAbility");
118     if (element.abilityName != NULL) {
119         bool ret = SetWantElement(&want, element);
120         if (ret) {
121             TEST_ASSERT_EQUAL_STRING(want.element->deviceId, "0001000");
122             TEST_ASSERT_EQUAL_STRING(want.element->abilityName, "SecondAbility");
123             TEST_ASSERT_EQUAL_STRING(want.element->bundleName, "com.openharmony.testnative");
124         }
125     }
126     ClearElement(&element);
127     ClearWant(&want);
128     TEST_ASSERT_EQUAL_STRING(want.element, NULL);
129     printf("------end testSetWantElementLegal------\n");
130 }
131 
132 /**
133  * @tc.number    : SUB_AAFWK_ABILITY_0004
134  * @tc.name      : testSetWantElementIllegal
135  * @tc.desc      : testSetWantElement parameter illegal test
136  */
137 LITE_TEST_CASE(AbilityMgrTestSuite, testSetWantElementIllegal, Function | MediumTest | Level2)
138 {
139     printf("------start testSetWantElementIllegal------\n");
140     Want want = { 0 };
141     ElementName element = { 0 };
142     bool ret = SetWantElement(&want, element);
143     if (ret) {
144         TEST_ASSERT_EQUAL_STRING(want.element->deviceId, NULL);
145         TEST_ASSERT_EQUAL_STRING(want.element->abilityName, NULL);
146         TEST_ASSERT_EQUAL_STRING(want.element->bundleName, NULL);
147     }
148     ClearElement(&element);
149     TEST_ASSERT_EQUAL_STRING(element.abilityName, NULL);
150     ClearWant(&want);
151     TEST_ASSERT_EQUAL_STRING(want.element, NULL);
152     printf("------end testSetWantElementIllegal------\n");
153 }
154 
155 /**
156  * @tc.number    : SUB_AAFWK_ABILITY_0005
157  * @tc.name      : testClearWantIllegal
158  * @tc.desc      : testClearWant parameter illegal test
159  */
160 LITE_TEST_CASE(AbilityMgrTestSuite, testClearWantIllegal, Function | MediumTest | Level2)
161 {
162     printf("------start testClearWantIllegal------\n");
163     Want want = { 0 };
164     ElementName element = { 0 };
165     SetElementDeviceID(&element, "0001000");
166     SetElementBundleName(&element, "com.openharmony.testnative");
167     bool ret = SetElementAbilityName(&element, "SecondAbility");
168     if (ret) {
169         ret = SetWantElement(&want, element);
170         if (ret) {
171             TEST_ASSERT_EQUAL_STRING(want.element->deviceId, "0001000");
172             TEST_ASSERT_EQUAL_STRING(want.element->abilityName, "SecondAbility");
173             TEST_ASSERT_EQUAL_STRING(want.element->bundleName, "com.openharmony.testnative");
174             ClearWant(NULL);
175             TEST_ASSERT_EQUAL_STRING(want.element->deviceId, "0001000");
176             TEST_ASSERT_EQUAL_STRING(want.element->abilityName, "SecondAbility");
177             TEST_ASSERT_EQUAL_STRING(want.element->bundleName, "com.openharmony.testnative");
178         }
179     }
180     ClearElement(&element);
181     ClearWant(&want);
182     printf("------end testClearWantIllegal------\n");
183 }
184 
185 /**
186  * @tc.number    : SUB_AAFWK_ABILITY_0006
187  * @tc.name      : testSetWantDateLegal
188  * @tc.desc      : testSetWantDate parameter legal test
189  */
190 LITE_TEST_CASE(AbilityMgrTestSuite, testSetWantDateLegal, Function | MediumTest | Level0)
191 {
192     printf("------start testSetWantDateLegal------\n");
193     Want want = { 0 };
194     char *data = "test";
195     SetWantData(&want, (void *)data, strlen(data) + 1);
196     if (want.data != NULL) {
197         TEST_ASSERT_EQUAL_STRING((char*)(want.data), data);
198         TEST_ASSERT_TRUE(want.dataLength == strlen(data) + 1);
199     }
200     ClearWant(&want);
201     TEST_ASSERT_EQUAL_STRING(want.data, NULL);
202     TEST_ASSERT_TRUE(want.dataLength == strlen(data) + 1);
203     printf("------end testSetWantDateLegal------\n");
204 }
205 
206 /**
207  * @tc.number    : SUB_AAFWK_ABILITY_0007
208  * @tc.name      : testSetWantDateIllegal
209  * @tc.desc      : testSetWantDate parameter illegal test
210  */
211 LITE_TEST_CASE(AbilityMgrTestSuite, testSetWantDateIllegal, Function | MediumTest | Level2)
212 {
213     printf("------start testSetWantDateIllegal------\n");
214     Want want = { 0 };
215     SetWantData(&want, "test", -1);
216     TEST_ASSERT_EQUAL_STRING((char*)(want.data), NULL);
217     TEST_ASSERT_FALSE(want.dataLength);
218     SetWantData(&want, NULL, 0);
219     TEST_ASSERT_EQUAL_STRING((char*)(want.data), NULL);
220     TEST_ASSERT_FALSE(want.dataLength);
221     printf("------end testSetWantDateIllegal------\n");
222 }
223 
224 /**
225  * @tc.number    : SUB_AAFWK_ABILITY_0008
226  * @tc.name      : testStartAbilityIllegal
227  * @tc.desc      : testStartAbility parameter illegal test
228  */
229 LITE_TEST_CASE(AbilityMgrTestSuite, testStartAbilityIllegal, Function | MediumTest | Level0)
230 {
231     printf("------start testStartAbilityIllegal------\n");
232     int ret = StartAbility(NULL);
233     TEST_ASSERT_TRUE(ret == PARAM_CHECK_ERROR);
234     printf("------end testStartAbilityIllegal------\n");
235 }
236 
237 /**
238  * @tc.number    : SUB_AAFWK_ABILITY_0010
239  * @tc.name      : testSetIntParamIllegal
240  * @tc.desc      : testSetIntParam parameter illegal test
241  */
242 LITE_TEST_CASE(AbilityMgrTestSuite, testSetIntParamIllegal, Function | MediumTest | Level0)
243 {
244     printf("------start testSetIntParamIllegal------\n");
245     bool ret = SetIntParam(NULL, NULL, 0, 0);
246     TEST_ASSERT_FALSE(ret);
247     printf("------end testSetIntParamIllegal------\n");
248 }
249 
250 /**
251  * @tc.number    : SUB_AAFWK_ABILITY_0011
252  * @tc.name      : testSetIntParamLegal
253  * @tc.desc      : testSetIntParam parameter legal test
254  */
255 LITE_TEST_CASE(AbilityMgrTestSuite, testSetIntParamLegal, Function | MediumTest | Level0)
256 {
257     printf("------start testSetIntParamLegal------\n");
258     Want want = { 0 };
259     int32_t value = 1;
260     char *key = "key";
261     bool ret = SetIntParam(&want, key, strlen(key), value);
262     TEST_ASSERT_TRUE(ret);
263     TEST_ASSERT_TRUE(want.dataLength == (strlen(key) + sizeof(int) + DATA_EXTERN));
264     TEST_ASSERT_TRUE(want.data != NULL);
265     printf("------end testSetIntParamLegal------\n");
266 }
267 
268 /**
269  * @tc.number    : SUB_AAFWK_ABILITY_0012
270  * @tc.name      : testSetStrParamIllegal
271  * @tc.desc      : testSetStrParam parameter illegal test
272  */
273 LITE_TEST_CASE(AbilityMgrTestSuite, testSetStrParamIllegal, Function | MediumTest | Level0)
274 {
275     printf("------start testSetStrParamIllegal------\n");
276     bool ret = SetStrParam(NULL, NULL, 0, NULL, 0);
277     TEST_ASSERT_FALSE(ret);
278     printf("------end testSetStrParamIllegal------\n");
279 }
280 
281 /**
282  * @tc.number    : SUB_AAFWK_ABILITY_0013
283  * @tc.name      : testSetStrParamLegal
284  * @tc.desc      : testSetStrParam parameter legal test
285  */
286 LITE_TEST_CASE(AbilityMgrTestSuite, testSetStrParamLegal, Function | MediumTest | Level2)
287 {
288     printf("------start testSetStrParamLegal------\n");
289     Want want = { 0 };
290     char *key = "key";
291     char *value = "value";
292     bool ret = SetStrParam(&want, key, strlen(key), value, strlen(value));
293     TEST_ASSERT_TRUE(ret);
294     TEST_ASSERT_TRUE(want.dataLength == (strlen(value) + strlen(key) + DATA_EXTERN));
295     TEST_ASSERT_TRUE(want.data != NULL);
296     printf("------end testSetStrParamLegal------\n");
297 }
298 
299 RUN_TEST_SUITE(AbilityMgrTestSuite);