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