• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 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 <log.h>
17 #include <semaphore.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string>
21 
22 #include "gtest/gtest.h"
23 #include "securec.h"
24 
25 #include "ability.h"
26 #include "ability_connection.h"
27 #include "ability_env.h"
28 #include "ability_event_handler.h"
29 #include "ability_manager.h"
30 #include "ability_slice.h"
31 #include "abilityms_client.h"
32 #include "bundle_manager.h"
33 #include "want.h"
34 
35 using namespace std;
36 using namespace testing::ext;
37 using namespace OHOS;
38 static int32_t g_errorCode = -1;
39 static sem_t g_sem;
40 static const int32_t WAIT_TIMEOUT = 30;
41 static bool g_installState = false;
42 static string g_testPath;
43 
44 extern "C" {
HOS_SystemInit(void)45 void __attribute__((weak)) HOS_SystemInit(void){};
46 }
47 
48 /* install callback */
TestBundleStateCallback(const uint8_t resultCode,const void * resultMessage)49 static void TestBundleStateCallback(const uint8_t resultCode, const void *resultMessage)
50 {
51     HILOG_DEBUG(HILOG_MODULE_APP, "TestBundleStateCallback resultCode: %d", resultCode);
52     HILOG_DEBUG(HILOG_MODULE_APP, "TestBundleStateCallback resultMessage: %s", (char *) resultMessage);
53     g_installState = (resultCode == 0);
54     g_errorCode = resultCode;
55     sem_post(&g_sem);
56 }
57 
58 /* *
59  * get current dir
60  * @return  string current file path of the test suits
61  */
GetCurDir()62 static string GetCurDir()
63 {
64     string filePath = "";
65     char *buffer;
66     if ((buffer = getcwd(NULL, 0)) == NULL) {
67         perror("get file path error");
68     } else {
69         printf("Current Dir: %s\r\n", buffer);
70         filePath = buffer;
71         free(buffer);
72     }
73     return filePath + "/";
74 }
75 
76 /* connectAbility callback */
OnAbilityConnectDone(ElementName * elementName,SvcIdentity * serviceSid,int resultCode,void * storeData)77 static void OnAbilityConnectDone(ElementName *elementName, SvcIdentity *serviceSid, int resultCode, void *storeData)
78 {
79     printf("OnAbilityConnectDone, serviceSid is %p \n", serviceSid);
80     printf("elementName is %s, %s \n", elementName->bundleName, elementName->abilityName);
81     ClearElement(elementName);
82     if (serviceSid == nullptr) {
83         return;
84     }
85     printf("ipc callback success \n");
86     // push data
87     IpcIo request;
88     char data[MAX_IO_SIZE];
89     IpcIoInit(&request, data, MAX_IO_SIZE, 0);
90     int32_t data1 = 10;
91     int32_t data2 = 6;
92     WriteInt32(&request, data1);
93     WriteInt32(&request, data2);
94     // send and getReply
95     IpcIo reply = {nullptr};
96     uintptr_t ptr = 0;
97     MessageOption option;
98     MessageOptionInit(&option);
99     SendRequest(*serviceSid, 0, &request, &reply, option, &ptr);
100     ReadInt32(&reply, &g_errorCode);
101     if (g_errorCode != 0) {
102         printf("execute add method, result is %d\n", g_errorCode);
103     }
104     if (ptr != 0) {
105         FreeBuffer(reinterpret_cast<void *>(ptr));
106     }
107     sem_post(&g_sem);
108 }
109 
OnAbilityDisconnectDone(ElementName * elementName,int resultCode,void * storeData)110 static void OnAbilityDisconnectDone(ElementName *elementName, int resultCode, void *storeData)
111 {
112     printf("OnAbilityDisconnectDone\n");
113 }
114 
115 
116 static IAbilityConnection g_conn = {
117     .OnAbilityConnectDone = OnAbilityConnectDone,
118     .OnAbilityDisconnectDone = OnAbilityDisconnectDone
119 };
120 
121 class AbilityMgrTest : public testing::Test {
122 protected:
SetUpTestCase(void)123     static void SetUpTestCase(void)
124     {
125         printf("----------test case with AbilityMgrTest start-------------\n");
126         HOS_SystemInit();
127         AbilityMsClient::GetInstance().Initialize();
128         sem_init(&g_sem, 0, 0);
129         bool installResult = false;
130         InstallParam installParam = { .installLocation = 1, .keepData = false };
131         g_testPath = GetCurDir();
132 #ifdef __LINUX__
133         string hapPath = g_testPath + "testnative_hispark_taurus_linux.hap";
134 #else
135         string hapPath = g_testPath + "testnative_hispark_taurus_liteos.hap";
136 #endif
137         installResult = Install(hapPath.c_str(), &installParam, TestBundleStateCallback);
138         struct timespec ts = {};
139         clock_gettime(CLOCK_REALTIME, &ts);
140         ts.tv_sec += WAIT_TIMEOUT;
141         sem_timedwait(&g_sem, &ts);
142         if (installResult) {
143             printf("sem exit \n");
144         }
145     }
TearDownTestCase(void)146     static void TearDownTestCase(void)
147     {
148         bool uninstallResult = false;
149         sem_init(&g_sem, 0, 0);
150         InstallParam installParam = { .installLocation = 1, .keepData = false };
151         uninstallResult = Uninstall("com.openharmony.testnative", &installParam, TestBundleStateCallback);
152         sem_wait(&g_sem);
153         if (uninstallResult) {
154             printf("sem exit \n");
155         }
156         printf("----------test case with AbilityMgrTest end-------------\n");
157     }
158 };
159 
160 /**
161  * @tc.number    : SUB_APPEXECFWK_AMS_API_0013
162  * @tc.name      : testClearElement parameter legal test
163  * @tc.desc      : [C- SOFTWARE -0200]
164  */
165 HWTEST_F(AbilityMgrTest, testClearElement, Function | MediumTest | Level0)
166 {
167     printf("------start testClearElement------\n");
168     ElementName element = { nullptr };
169     bool setResult = SetElementAbilityName(&element, "SecondAbility");
170     if (setResult) {
171         char aName[] = "SecondAbility";
172         EXPECT_STREQ(element.abilityName, aName);
173         printf("abilityName is %s \n", element.abilityName);
174         ClearElement(&element);
175         EXPECT_STREQ(element.abilityName, nullptr);
176     }
177     printf("------end testClearElement------\n");
178 }
179 
180 /**
181  * @tc.number    : SUB_APPEXECFWK_AMS_API_0014
182  * @tc.name      : testClearElement parameter illegal test
183  * @tc.desc      : [C- SOFTWARE -0200]
184  */
185 HWTEST_F(AbilityMgrTest, testClearElementIllegal, Function | MediumTest | Level2)
186 {
187     printf("------start testClearElementIllegal------\n");
188     ElementName element = { nullptr };
189     bool setResult = SetElementAbilityName(&element, "SecondAbility");
190     if (setResult) {
191         char aName[] = "SecondAbility";
192         EXPECT_STREQ(element.abilityName, aName);
193         printf("abilityName is %s \n", element.abilityName);
194         ClearElement(nullptr);
195         EXPECT_STREQ(element.abilityName, aName);
196         printf("AbilityName of element is %s \n", element.abilityName);
197     }
198     printf("------end testClearElementIllegal------\n");
199 }
200 
201 /**
202  * @tc.number    : SUB_APPEXECFWK_AMS_API_0015
203  * @tc.name      : testSetWantElement parameter legal test
204  * @tc.desc      : [C- SOFTWARE -0200]
205  */
206 HWTEST_F(AbilityMgrTest, testSetWantElement, Function | MediumTest | Level0)
207 {
208     printf("------start testSetWantElement------\n");
209     Want want = { nullptr };
210     ElementName element = { nullptr };
211     SetElementDeviceID(&element, "0001000");
212     SetElementBundleName(&element, "com.openharmony.testnative");
213     SetElementAbilityName(&element, "SecondAbility");
214     if (element.abilityName != nullptr) {
215         bool setResult = SetWantElement(&want, element);
216         if (setResult) {
217             EXPECT_STREQ(want.element->deviceId, "0001000");
218             EXPECT_STREQ(want.element->abilityName, "SecondAbility");
219             EXPECT_STREQ(want.element->bundleName, "com.openharmony.testnative");
220         }
221     }
222     ClearElement(&element);
223     ClearWant(&want);
224     printf("------end testSetWantElement------\n");
225 }
226 
227 /**
228  * @tc.number    : SUB_APPEXECFWK_AMS_API_0016
229  * @tc.name      : testSetWantElement parameter illegal test
230  * @tc.desc      : [C- SOFTWARE -0200]
231  */
232 HWTEST_F(AbilityMgrTest, testSetWantElementIllegal, Function | MediumTest | Level2)
233 {
234     printf("------start testSetWantElementIllegal------\n");
235     Want want = { nullptr };
236     ElementName element = { nullptr };
237     bool setResult = SetWantElement(&want, element);
238     if (setResult) {
239         EXPECT_STREQ(want.element->deviceId, nullptr);
240         EXPECT_STREQ(want.element->abilityName, nullptr);
241         EXPECT_STREQ(want.element->bundleName, nullptr);
242     }
243     ClearElement(&element);
244     ClearWant(&want);
245     printf("------end testSetWantElementIllegal------\n");
246 }
247 
248 /**
249  * @tc.number    : SUB_APPEXECFWK_AMS_API_0022
250  * @tc.name      : testClearWant parameter illegal test
251  * @tc.desc      : [C- SOFTWARE -0200]
252  */
253 HWTEST_F(AbilityMgrTest, testClearWantIllegal, Function | MediumTest | Level2)
254 {
255     printf("------start testClearWantIllegal------\n");
256     Want want = { nullptr };
257     ElementName element = { nullptr };
258     bool setResult = SetElementAbilityName(&element, "SecondAbility");
259     if (setResult) {
260         setResult = SetWantElement(&want, element);
261         if (setResult) {
262             char aName[] = "SecondAbility";
263             EXPECT_STREQ(want.element->abilityName, aName);
264             printf("abilityName is %s \n", want.element->abilityName);
265             ClearWant(nullptr);
266             EXPECT_STREQ(want.element->abilityName, aName);
267         }
268     }
269     ClearElement(&element);
270     ClearWant(&want);
271     printf("------end testClearWantIllegal------\n");
272 }
273 
274 /**
275  * @tc.number    : SUB_APPEXECFWK_AMS_API_0025
276  * @tc.name      : testWantToUri parameter legal test
277  * @tc.desc      : [C- SOFTWARE -0200]
278  */
279 HWTEST_F(AbilityMgrTest, testWantToUri, Function | MediumTest | Level0)
280 {
281     printf("------start testWantToUri------\n");
282     Want want = { nullptr };
283     ElementName element = { nullptr };
284     SetElementDeviceID(&element, "0001000");
285     SetElementBundleName(&element, "com.openharmony.testnative");
286     SetElementAbilityName(&element, "SecondAbility");
287     if (element.abilityName !=nullptr) {
288         bool setResult = SetWantElement(&want, element);
289         if (setResult) {
290             const char *uri = WantToUri(want);
291             printf("uri is %s \n", uri);
292             const char *expectResult = "#Want;device=0001000;bundle=com.openharmony.testnative;ability=SecondAbility;end";
293             EXPECT_STREQ(uri, expectResult);
294             free((void*)uri);
295         }
296     }
297     ClearElement(&element);
298     ClearWant(&want);
299     printf("------end testWantToUri------\n");
300 }
301 
302 /**
303  * @tc.number    : SUB_APPEXECFWK_AMS_API_0026
304  * @tc.name      : testWantToUri parameter illegal test
305  * @tc.desc      : [C- SOFTWARE -0200]
306  */
307 HWTEST_F(AbilityMgrTest, testWantToUriIllegal, Function | MediumTest | Level2)
308 {
309     printf("------start testWantToUriIllegal------\n");
310     Want want = { nullptr };
311     const char *uri = WantToUri(want);
312     printf("uri is %s \n", uri);
313     const char *expectResult = "#Want;device=;bundle=;ability=;end";
314     EXPECT_STREQ(uri, expectResult);
315     if (uri != nullptr) {
316         free((void*)uri);
317     }
318     printf("------end testWantToUriIllegal------\n");
319 }
320 
321 /**
322  * @tc.number    : SUB_APPEXECFWK_AMS_API_0017
323  * @tc.name      : testSetWantDate parameter legal test
324  * @tc.desc      : [C- SOFTWARE -0200]
325  */
326 HWTEST_F(AbilityMgrTest, testSetWantDate, Function | MediumTest | Level0)
327 {
328     printf("------start testSetWantDate------\n");
329     Want want = { nullptr };
330     SetWantData(&want, "test", 5);
331     if (want.data != nullptr) {
332         printf("data is %s \n", (char*)(want.data));
333         printf("dataLength is %d \n", want.dataLength);
334         EXPECT_STREQ((char*)(want.data), "test");
335         EXPECT_TRUE(want.dataLength == 5);
336     }
337     ClearWant(&want);
338     printf("------end testSetWantDate------\n");
339 }
340 
341 /**
342  * @tc.number    : SUB_APPEXECFWK_AMS_API_0018
343  * @tc.name      : testSetWantDate parameter illegal test
344  * @tc.desc      : [C- SOFTWARE -0200]
345  */
346 HWTEST_F(AbilityMgrTest, testSetWantDateIllegal, Function | MediumTest | Level2)
347 {
348     printf("------start testSetWantDateIllegal------\n");
349     Want want = { nullptr };
350     SetWantData(&want, "test", -1);
351     printf("dataLength is %d \n", want.dataLength);
352     EXPECT_STREQ((char*)(want.data), nullptr);
353     EXPECT_TRUE(want.dataLength == 0);
354     SetWantData(&want, nullptr, 0);
355     printf("dataLength is %d \n", want.dataLength);
356     EXPECT_STREQ((char*)(want.data), nullptr);
357     EXPECT_TRUE(want.dataLength == 0);
358     printf("------end testSetWantDateIllegal------\n");
359 }
360 
361 /**
362  * @tc.number    : SUB_APPEXECFWK_AMS_API_0023
363  * @tc.name      : testWantParseUri parameter legal test
364  * @tc.desc      : [C- SOFTWARE -0200]
365  */
366 HWTEST_F(AbilityMgrTest, testWantParseUri, Function | MediumTest | Level0)
367 {
368     printf("------start testWantParseUri------\n");
369     Want want = { nullptr };
370     ElementName element = { nullptr };
371     SetElementDeviceID(&element, "0001000");
372     SetElementBundleName(&element, "com.openharmony.testnative");
373     SetElementAbilityName(&element, "SecondAbility");
374     if (element.abilityName != nullptr) {
375         bool setResult = SetWantElement(&want, element);
376         if (setResult) {
377             const char *uri = WantToUri(want);
378             Want *want2 = WantParseUri(uri);
379             printf("uri is %s \n", uri);
380             if (uri != nullptr) {
381                 free((void*)uri);
382             }
383             EXPECT_STREQ(want2->element->deviceId, want.element->deviceId);
384             EXPECT_STREQ(want2->element->abilityName, want.element->abilityName);
385             EXPECT_STREQ(want2->element->bundleName, want.element->bundleName);
386             free(want2);
387         }
388     }
389     ClearElement(&element);
390     ClearWant(&want);
391     printf("------end testWantParseUri------\n");
392 }
393 
394 /**
395  * @tc.number    : SUB_APPEXECFWK_AMS_API_0024
396  * @tc.name      : testWantParseUri parameter illegal test
397  * @tc.desc      : [C- SOFTWARE -0200]
398  */
399 HWTEST_F(AbilityMgrTest, testWantParseUriIllegal, Function | MediumTest | Level2)
400 {
401     printf("------start testWantParseUriIllegal------\n");
402     Want want = { nullptr };
403     ElementName element = { nullptr };
404     SetElementBundleName(&element, nullptr);
405     SetWantElement(&want, element);
406     const char *uri = WantToUri(want);
407     // empty
408     printf("uri is %s \n", uri);
409     Want *want2 = WantParseUri(uri);
410     if (uri != nullptr) {
411         free((void*)uri);
412     }
413     if (want2 != nullptr) {
414         printf("want is %s \n", want2->element->bundleName);
415         EXPECT_STREQ(want2->element->deviceId, "");
416         EXPECT_STREQ(want2->element->abilityName, "");
417         EXPECT_STREQ(want2->element->bundleName, "");
418         free(want2);
419     }
420     // nullptr
421     Want *want4 = WantParseUri(nullptr);
422     printf("want4 is %p \n", want4);
423     EXPECT_TRUE(want4 == nullptr);
424     const char *str = "test";
425     // error format
426     Want *want1 = WantParseUri(str);
427     printf("want is %p \n", want1);
428     EXPECT_TRUE(want1 == nullptr);
429     Want *want3 = WantParseUri("");
430     printf("want is %p \n", want3);
431     EXPECT_TRUE(want3 == nullptr);
432     free(want1);
433     free(want3);
434     ClearElement(&element);
435     ClearWant(&want);
436     printf("------end testWantParseUriIllegal------\n");
437 }
438 
439 /**
440  * @tc.number    : SUB_APPEXECFWK_AMS_API_0030
441  * @tc.name      : testGetBundleNameIllegal parameter illegal test
442  * @tc.desc      : [C- SOFTWARE -0200]
443  */
444 HWTEST_F(AbilityMgrTest, testGetBundleNameIllegal, Function | MediumTest | Level1)
445 {
446     printf("------start testGetBundleNameIllegal------\n");
447     Want want;
448     (void)memset_s(&want, sizeof(Want), 0, sizeof(Want));
449     ElementName element;
450     (void)memset_s(&element, sizeof(ElementName), 0, sizeof(ElementName));
451     SetElementBundleName(&element, "com.openharmony.testnative");
452     SetElementAbilityName(&element, "SecondAbility");
453     SetWantElement(&want, element);
454     int result = StartAbility(&want);
455     sleep(2);
456     printf("ret is %d \n", result);
457     const char * bundleName1 = GetBundleName();
458     printf("result of GetBundleName is %s \n", bundleName1);
459     EXPECT_STREQ(bundleName1, "");
460     printf("------end testGetBundleNameIllegal------\n");
461 }
462 
463 /**
464  * @tc.number    : SUB_APPEXECFWK_AMS_API_0031
465  * @tc.name      : testGetSrcPathIllegal parameter illegal test
466  * @tc.desc      : [C- SOFTWARE -0200]
467  */
468 HWTEST_F(AbilityMgrTest, testGetSrcPathIllegal, Function | MediumTest | Level1)
469 {
470     printf("------start testGetSrcPathIllegal------\n");
471     Want want = { nullptr };
472     ElementName element = { nullptr };
473     SetElementBundleName(&element, "com.openharmony.testnative");
474     SetElementAbilityName(&element, "SecondAbility");
475     SetWantElement(&want, element);
476     int result = StartAbility(&want);
477     sleep(2);
478     printf("ret is %d \n", result);
479     const char * srcPath = GetSrcPath();
480     EXPECT_STREQ(srcPath, "");
481     printf("------end testGetSrcPathIllegal------\n");
482 }
483 
484 /**
485  * @tc.number    : SUB_APPEXECFWK_AMS_API_0032
486  * @tc.name      : testGetDataPath parameter illegal test
487  * @tc.desc      : [C- SOFTWARE -0200]
488  */
489 HWTEST_F(AbilityMgrTest, testGetDataPathIllegal, Function | MediumTest | Level1)
490 {
491     printf("------start testGetDataPathIllegal------\n");
492     Want want = { nullptr };
493     ElementName element = { nullptr };
494     SetElementBundleName(&element, "com.openharmony.testnative");
495     SetElementAbilityName(&element, "SecondAbility");
496     SetWantElement(&want, element);
497     int result = StartAbility(&want);
498     sleep(2);
499     printf("ret is %d \n", result);
500     const char * dataPath = GetDataPath();
501     printf("result of GetDataPath is %s \n", dataPath);
502     EXPECT_STREQ(dataPath, "");
503     printf("------end testGetDataPathIllegal------\n");
504 }
505 
506 /**
507  * @tc.number    : SUB_APPEXECFWK_AMS_API_0019
508  * @tc.name      : testDump parameter illegal test
509  * @tc.desc      : [C- SOFTWARE -0200]
510  */
511 HWTEST_F(AbilityMgrTest, testDumpIllegal, Function | MediumTest | Level1)
512 {
513     printf("------start testDump------\n");
514     Want want;
515     (void)memset_s(&want, sizeof(Want), 0, sizeof(Want));
516     ElementName element;
517     (void)memset_s(&element, sizeof(ElementName), 0, sizeof(ElementName));
518     SetElementBundleName(&element, "com.openharmony.testnative");
519     SetElementAbilityName(&element, "SecondAbility");
520     SetWantElement(&want, element);
521     Ability *ability = new Ability();
522     int result = ability->StartAbility(want);
523     sleep(2);
524     printf("ret is %d \n", result);
525     EXPECT_EQ(result, 0);
526     char *extra = (char*)"test";
527     ability->Dump(extra);
528     printf("------end testDump------\n");
529 }
530 
531 /**
532  * @tc.number    : SUB_APPEXECFWK_AMS_API_0033
533  * @tc.name      : testStartAbility parameter legal test
534  * @tc.desc      : [C- SOFTWARE -0200]
535  */
536 HWTEST_F(AbilityMgrTest, testStartAbility, Function | MediumTest | Level1)
537 {
538     printf("------start testStartAbility------\n");
539     Want want = { nullptr };
540     ElementName element = { nullptr };
541     SetElementBundleName(&element, "com.openharmony.testnative");
542     SetElementAbilityName(&element, "MainAbility");
543     SetWantElement(&want, element);
544     int result = StartAbility(&want);
545     sleep(2);
546     printf("ret is %d \n", result);
547     EXPECT_EQ(result, 0);
548     ClearElement(&element);
549     ClearWant(&want);
550     printf("------end testStartAbility------\n");
551 }
552 
553 /**
554  * @tc.number    : SUB_APPEXECFWK_AMS_API_0005
555  * @tc.name      : testStartAbilityIllegal parameter illegal test
556  * @tc.desc      : [C- SOFTWARE -0200]
557  */
558 HWTEST_F(AbilityMgrTest, testStartAbilityIllegal, Function | MediumTest | Level2)
559 {
560     printf("------start testStartAbilityIllegal------\n");
561     int result = StartAbility(nullptr);
562     printf("ret is %d \n", result);
563     int expect = -1;
564     EXPECT_EQ(result, expect);
565     printf("------end testStartAbilityIllegal------\n");
566 }
567 
568 /**
569  * @tc.number    : SUB_APPEXECFWK_AMS_API_0006
570  * @tc.name      : testStopAbility parameter legal test
571  * @tc.desc      : [C- SOFTWARE -0200]
572  */
573 HWTEST_F(AbilityMgrTest, testStopAbility, Function | MediumTest | Level0)
574 {
575     printf("------start testStopAbility------\n");
576     Want want = { nullptr };
577     ElementName element = { nullptr };
578     SetElementBundleName(&element, "com.openharmony.testnative");
579     SetElementAbilityName(&element, "ServiceAbility");
580     SetWantElement(&want, element);
581     g_errorCode = StartAbility(&want);
582     sleep(2);
583     printf("ret is %d \n", g_errorCode);
584     EXPECT_EQ(g_errorCode, 0);
585     g_errorCode = StopAbility(&want);
586     sleep(2);
587     printf("ret of stop is %d \n", g_errorCode);
588     EXPECT_EQ(g_errorCode, 0);
589     ClearElement(&element);
590     ClearWant(&want);
591     printf("------end testStopAbility------\n");
592 }
593 
594 /**
595  * @tc.number    : SUB_APPEXECFWK_AMS_API_0007
596  * @tc.name      : testStopAbilityIllegal parameter illegal test
597  * @tc.desc      : [C- SOFTWARE -0200]
598  */
599 HWTEST_F(AbilityMgrTest, testStopAbilityIllegal, Function | MediumTest | Level2)
600 {
601     printf("------start testStopAbilityIllegal------\n");
602     g_errorCode = StopAbility(nullptr);
603     printf("ret of stop is %d \n", g_errorCode);
604     EXPECT_EQ(g_errorCode, -1);
605     printf("------end testStopAbilityIllegal------\n");
606 }
607 
608 /**
609  * @tc.number    : SUB_APPEXECFWK_AMS_API_0008
610  * @tc.name      : testConnectAbility parameter legal test
611  * @tc.desc      : [C- SOFTWARE -0200]
612  */
613 HWTEST_F(AbilityMgrTest, testConnectAbility, Function | MediumTest | Level1)
614 {
615     printf("------start testConnectAbility------\n");
616     Want want = { nullptr };
617     ElementName element = { nullptr };
618     SetElementBundleName(&element, "com.openharmony.testnative");
619     SetElementAbilityName(&element, "ServiceAbility");
620     SetWantElement(&want, element);
621     sem_init(&g_sem, 0, 0);
622     int result = ConnectAbility(&want, &g_conn, this);
623     struct timespec ts = {};
624     clock_gettime(CLOCK_REALTIME, &ts);
625     ts.tv_sec += WAIT_TIMEOUT;
626     sem_timedwait(&g_sem, &ts);
627     printf("sem exit \n");
628     printf("ret is %d \n ", result);
629     EXPECT_EQ(result, 0);
630     DisconnectAbility(&g_conn);
631     sleep(1);
632     ClearElement(&element);
633     ClearWant(&want);
634     printf("------end testConnectAbility------\n");
635 }
636 
637 /**
638  * @tc.number    : SUB_APPEXECFWK_AMS_API_0010
639  * @tc.name      : testWantMathBundle
640  * @tc.desc      : [C- SOFTWARE -0100]
641  * @tc.author    : lijiashan
642  */
643 HWTEST_F(AbilityMgrTest, testDisConnectAbility, Function | MediumTest | Level1)
644 {
645     printf("------start testDisConnectAbility------\n");
646     Want want = { nullptr };
647     ElementName element = { nullptr };
648     SetElementBundleName(&element, "com.openharmony.testnative");
649     SetElementAbilityName(&element, "ServiceAbility");
650     SetWantElement(&want, element);
651     sem_init(&g_sem, 0, 0);
652     int result = ConnectAbility(&want, &g_conn, this);
653     struct timespec ts = {};
654     clock_gettime(CLOCK_REALTIME, &ts);
655     ts.tv_sec += WAIT_TIMEOUT;
656     sem_timedwait(&g_sem, &ts);
657     printf("sem exit \n");
658     printf("ret of connect is %d \n ", result);
659     if (g_errorCode == 16) {
660         result = DisconnectAbility(&g_conn);
661         sleep(2);
662         EXPECT_EQ(result, 0);
663         printf("ret of disconnect is %d \n ", result);
664     }
665     ClearElement(&element);
666     ClearWant(&want);
667     printf("------end testDisConnectAbility------\n");
668 }
669 
670 /**
671  * @tc.number    : SUB_APPEXECFWK_AMS_API_0009
672  * @tc.name      : testConnectAbilityIllegal parameter illegal test
673  * @tc.desc      : [C- SOFTWARE -0200]
674  */
675 HWTEST_F(AbilityMgrTest, testConnectAbilityIllegal, Function | MediumTest | Level1)
676 {
677     printf("------start testConnectAbilityIllegal------\n");
678     Want want = { nullptr };
679     ElementName element = { nullptr };
680     SetElementBundleName(&element, "com.openharmony.testnative");
681     SetElementAbilityName(&element, "ServiceAbility");
682     SetWantElement(&want, element);
683     g_errorCode = ConnectAbility(nullptr, &g_conn, this);
684     printf("ret1 is %d \n ", g_errorCode);
685     EXPECT_EQ(g_errorCode, -1);
686     g_errorCode = ConnectAbility(&want, nullptr, this);
687     printf("ret2 is %d \n ", g_errorCode);
688     EXPECT_EQ(g_errorCode, -1);
689     ClearElement(&element);
690     ClearWant(&want);
691     printf("------end testConnectAbilityIllegal------\n");
692 }
693 
694 /**
695  * @tc.number    : SUB_APPEXECFWK_AMS_API_0011
696  * @tc.name      : testDisConnectAbilityIllegal parameter illegal test
697  * @tc.desc      : [C- SOFTWARE -0200]
698  */
699 HWTEST_F(AbilityMgrTest, testDisConnectAbilityIllegal, Function | MediumTest | Level1)
700 {
701     printf("------start testDisConnectAbilityIllegal------\n");
702     Want want = { nullptr };
703     ElementName element = { nullptr };
704     SetElementBundleName(&element, "com.openharmony.testnative");
705     SetElementAbilityName(&element, "ServiceAbility");
706     SetWantElement(&want, element);
707     sem_init(&g_sem, 0, 0);
708     int result = ConnectAbility(&want, &g_conn, this);
709     struct timespec ts = {};
710     clock_gettime(CLOCK_REALTIME, &ts);
711     ts.tv_sec += WAIT_TIMEOUT;
712     sem_timedwait(&g_sem, &ts);
713     printf("sem exit \n");
714     printf("ret of connect is %d \n ", result);
715     EXPECT_EQ(result, 0);
716     g_errorCode = DisconnectAbility(nullptr);
717     int expect = -2;
718     EXPECT_EQ(g_errorCode, expect);
719     printf("ret of disconnect is %d \n ", g_errorCode);
720     ClearElement(&element);
721     ClearWant(&want);
722     printf("------end testDisConnectAbilityIllegal------\n");
723 }
724 
725 /**
726  * @tc.number    : SUB_APPEXECFWK_AMS_API_0012
727  * @tc.name      : testTerminateAbility parameter legal test
728  * @tc.desc      : [C- SOFTWARE -0200]
729  */
730 HWTEST_F(AbilityMgrTest, testTerminateAbility, Function | MediumTest | Level1)
731 {
732     printf("------start testTerminateAbility------\n");
733     Want want = { nullptr };
734     ElementName element = { nullptr };
735     SetElementBundleName(&element, "com.openharmony.testnative");
736     SetElementAbilityName(&element, "SecondAbility");
737     SetWantElement(&want, element);
738     int result1 = StartAbility(&want);
739     sleep(2);
740     printf("result1 of startAbility is %d \n", result1);
741     EXPECT_EQ(result1, 0);
742     Ability *ability = new Ability();
743     int result2 = ability->TerminateAbility();
744     sleep(2);
745     printf("result2 of TerminateAbility is %d \n", result2);
746     EXPECT_EQ(result2, 0);
747     ClearElement(&element);
748     ClearWant(&want);
749     delete ability;
750     printf("------end testTerminateAbility------\n");
751 }
752 
753 /**
754  * @tc.number    : SUB_APPEXECFWK_AMS_WANT_0001
755  * @tc.name      : test Want Match BundleInfo
756  * @tc.desc      : [C- SOFTWARE -0200]
757  */
758 HWTEST_F(AbilityMgrTest, testWantMatchBundle, Function | MediumTest | Level1)
759 {
760     printf("------start testWantMathBundle------\n");
761     Want want;
762     (void)memset_s(&want, sizeof(Want), 0, sizeof(Want));
763     ElementName element;
764     (void)memset_s(&element, sizeof(ElementName), 0, sizeof(ElementName));
765     SetElementAbilityName(&element, "MainAbility");
766     SetElementBundleName(&element, "com.openharmony.testnative");
767     SetElementDeviceID(&element, "");
768     SetWantElement(&want, element);
769     AbilityInfo abilityInfo;
770     (void)memset_s(&abilityInfo, sizeof(AbilityInfo), 0, sizeof(AbilityInfo));
771     g_errorCode = QueryAbilityInfo(&want, &abilityInfo);
772     printf("ret of query is %d \n", g_errorCode);
773     EXPECT_EQ(g_errorCode, 0);
774     if (g_errorCode == 0) {
775         printf("abilityInfo.name is %s \n", abilityInfo.name);
776     }
777     int result = StartAbility(&want);
778     sleep(2);
779     printf("result of startAbility is %d \n", result);
780     EXPECT_EQ(result, 0);
781     printf("element is %s \n", want.element->bundleName);
782     printf("element is %s \n", want.element->abilityName);
783     StopAbility(&want);
784     sleep(1);
785     printf("------end testWantMathBundle------\n");
786 }
787 
788 /**
789  * @tc.number    : SUB_APPEXECFWK_AMS_WANT_0004
790  * @tc.name      : test Want Not Match BundleInfo
791  * @tc.desc      : [C- SOFTWARE -0200]
792  */
793 HWTEST_F(AbilityMgrTest, testWantNotMathBundle, Function | MediumTest | Level2)
794 {
795     printf("------start testWantNotMathBundle------\n");
796     Want want;
797     (void)memset_s(&want, sizeof(Want), 0, sizeof(Want));
798     ElementName element;
799     std::string aName = "NoThisAbility";
800     (void)memset_s(&element, sizeof(ElementName), 0, sizeof(ElementName));
801     SetElementAbilityName(&element, aName.c_str());
802     SetElementBundleName(&element, "com.openharmony.nothishap");
803     SetWantElement(&want, element);
804     AbilityInfo abilityInfo;
805     (void)memset_s(&abilityInfo, sizeof(AbilityInfo), 0, sizeof(AbilityInfo));
806     g_errorCode = QueryAbilityInfo(&want, &abilityInfo);
807     printf("ret is %d \n", g_errorCode);
808     EXPECT_TRUE(g_errorCode != 0);
809     int result = StartAbility(&want);
810     sleep(2);
811     printf("result of startAbility is %d \n", result);
812     EXPECT_TRUE(result == 0);
813     printf("element is %s \n", want.element->bundleName);
814     printf("------end testWantNotMathBundle------\n");
815 }
816 
817 /**
818  * @tc.number    : SUB_APPEXECFWK_AMS_WANT_0002
819  * @tc.name      : testWantOnlyMathBundle
820  * @tc.desc      : [C- SOFTWARE -0200]
821  */
822 HWTEST_F(AbilityMgrTest, testWantOnlyMathBundleName, Function | MediumTest | Level1)
823 {
824     printf("------start testWantOnlyMathBundleName------\n");
825     Want want;
826     (void)memset_s(&want, sizeof(Want), 0, sizeof(Want));
827     ElementName element;
828     std::string aName = "Ability";
829     (void)memset_s(&element, sizeof(ElementName), 0, sizeof(ElementName));
830     SetElementAbilityName(&element, aName.c_str());
831     SetElementBundleName(&element, "com.openharmony.testnative");
832     SetWantElement(&want, element);
833     AbilityInfo abilityInfo;
834     (void)memset_s(&abilityInfo, sizeof(AbilityInfo), 0, sizeof(AbilityInfo));
835     g_errorCode = QueryAbilityInfo(&want, &abilityInfo);
836     printf("ret is %d \n", g_errorCode);
837     EXPECT_TRUE(g_errorCode != 0);
838     int result = StartAbility(&want);
839     sleep(2);
840     printf("result of startAbility is %d \n", result);
841     EXPECT_TRUE(result == 0);
842     printf("element is %s \n", want.element->bundleName);
843     printf("------end testWantOnlyMathBundleName------\n");
844 }
845 
846 /**
847  * @tc.number    : SUB_APPEXECFWK_AMS_WANT_0003
848  * @tc.name      : testWantOnlyMathAbility
849  * @tc.desc      : [C- SOFTWARE -0200]
850  */
851 HWTEST_F(AbilityMgrTest, testWantOnlyMathAbility, Function | MediumTest | Level1)
852 {
853     printf("------start testWantOnlyMathAbility------\n");
854     Want want;
855     (void)memset_s(&want, sizeof(Want), 0, sizeof(Want));
856     ElementName element;
857     std::string aName = "MainAbility";
858     (void)memset_s(&element, sizeof(ElementName), 0, sizeof(ElementName));
859     SetElementAbilityName(&element, aName.c_str());
860     SetElementBundleName(&element, "com.openharmony.test");
861     SetWantElement(&want, element);
862     AbilityInfo abilityInfo;
863     (void)memset_s(&abilityInfo, sizeof(AbilityInfo), 0, sizeof(AbilityInfo));
864     g_errorCode = QueryAbilityInfo(&want, &abilityInfo);
865     printf("ret is %d \n", g_errorCode);
866     EXPECT_TRUE(g_errorCode != 0);
867     int result = StartAbility(&want);
868     sleep(2);
869     printf("result of startAbility is %d \n", result);
870     EXPECT_TRUE(result == 0);
871     printf("element is %s \n", want.element->abilityName);
872     printf("------end testWantOnlyMathAbility------\n");
873 }
874 
875 /**
876  * @tc.number    : SUB_APPEXECFWK_AMS_WANT_0005
877  * @tc.name      : test WantData Match DataLength
878  * @tc.desc      : [C- SOFTWARE -0200]
879  */
880 HWTEST_F(AbilityMgrTest, testWantDataMatchLength, Function | MediumTest | Level1)
881 {
882     printf("------start testWantDataMatchLength------\n");
883     Want want;
884     (void)memset_s(&want, sizeof(Want), 0, sizeof(Want));
885     ElementName element;
886     (void)memset_s(&element, sizeof(ElementName), 0, sizeof(ElementName));
887     SetElementAbilityName(&element, "MainAbility");
888     SetElementBundleName(&element, "com.openharmony.testnative");
889     SetWantElement(&want, element);
890     SetWantData(&want, "test", 5);
891     AbilityInfo abilityInfo;
892     (void)memset_s(&abilityInfo, sizeof(AbilityInfo), 0, sizeof(AbilityInfo));
893     g_errorCode = QueryAbilityInfo(&want, &abilityInfo);
894     printf("ret is %d \n", g_errorCode);
895     EXPECT_TRUE(g_errorCode == 0);
896     int result = StartAbility(&want);
897     sleep(2);
898     printf("result of startAbility is %d \n", result);
899     EXPECT_TRUE(result == 0);
900     EXPECT_STREQ((char*)(want.data), "test");
901     EXPECT_EQ(want.dataLength, 5);
902     StopAbility(&want);
903     sleep(1);
904     printf("------end testWantDataMatchLength------\n");
905 }
906 
907 
908 /**
909  * @tc.number    : SUB_APPEXECFWK_AMS_WANT_0006
910  * @tc.name      : test WantData Not Match DataLength
911  * @tc.desc      : [C- SOFTWARE -0200]
912  */
913 HWTEST_F(AbilityMgrTest, testWantDataNotMatchLength, Function | MediumTest | Level2)
914 {
915     printf("------start testWantDataNotMatchLength------\n");
916     Want want;
917     (void)memset_s(&want, sizeof(Want), 0, sizeof(Want));
918     ElementName element;
919     (void)memset_s(&element, sizeof(ElementName), 0, sizeof(ElementName));
920     SetElementAbilityName(&element, "SecondAbility");
921     SetElementBundleName(&element, "com.openharmony.testnative");
922     SetWantElement(&want, element);
923     SetWantData(&want, "test", 3);
924     int result = StartAbility(&want);
925     sleep(2);
926     printf("result of startAbility is %d \n", result);
927     EXPECT_TRUE(result == 0);
928     EXPECT_EQ(want.dataLength, 3);
929     printf("------end testWantDataNotMatchLength------\n");
930 }
931 
932 /**
933  * @tc.number    : SUB_APPEXECFWK_AMS_API_0040
934  * @tc.name      : PostTask parameter illegal test that callback is null
935  * @tc.desc      : [C- SOFTWARE -0200]
936  */
937 HWTEST_F(AbilityMgrTest, testPostTask, Function | MediumTest | Level1)
938 {
939     printf("------start testPostTask------\n");
940 #ifdef __LINUX__
941     string hapPath = g_testPath + "testnative_hispark_taurus_linux.hap";
942 #else
943     string hapPath = g_testPath + "testnative_hispark_taurus_liteos.hap";
944 #endif
945     AbilityEventHandler eventHandler1;
__anond210fe160102null946     auto task = [hapPath]{
947         sem_init(&g_sem, 0, 0);
948         InstallParam installParam = { .installLocation = 1, .keepData = false };
949         bool installResult = Install(hapPath.c_str(), &installParam, TestBundleStateCallback);
950         sem_wait(&g_sem);
951         printf("installResult is %d \n", installResult);
952         EXPECT_TRUE(installResult);
953 
954         AbilityEventHandler *eventHandler2 = AbilityEventHandler::GetCurrentHandler();
955         eventHandler2->PostQuit();
956     };
957     eventHandler1.PostTask(task);
958     eventHandler1.Run();
959     printf("------end testPostTask------\n");
960 }
961