• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 #include <gtest/gtest.h>
16 
17 #include <cstdio>
18 #include <fcntl.h>
19 #include <random>
20 #include <unistd.h>
21 
22 #include "appspawn.h"
23 #include "appspawn_msg.h"
24 #include "appspawn_utils.h"
25 #include "securec.h"
26 
27 #include "appspawn_test_cmder.h"
28 
29 using namespace testing;
30 using namespace testing::ext;
31 
32 namespace OHOS {
33 namespace AppSpawn {
34 namespace {
35 static const int32_t DEFAULT_PID = 0;
36 static const int32_t FILE_PATH_SIZE = 50;
37 static const int32_t BUFFER_SIZE = 512;
38 static const int32_t BASE_TYPE = 10;
39 static const int32_t CONNECT_RETRY_DELAY = 50 * 1000;
40 static const int32_t CONNECT_RETRY_MAX_TIMES = 5;
41 static const int32_t UID_POSITION_MOVE = 5;
42 static const int32_t GID_POSITION_MOVE = 5;
43 static const int32_t GROUPS_POSITION_MOVE = 8;
44 static const char *DELIMITER_SPACE = " ";
45 static const char *DELIMITER_NEWLINE = "\n";
46 static char g_buffer[BUFFER_SIZE + 1] = {};
47 }  // namespace
48 
CheckFileIsExists(const char * filepath)49 bool CheckFileIsExists(const char *filepath)
50 {
51     int32_t retryCount = 0;
52     while ((access(filepath, F_OK) != 0) && (retryCount < CONNECT_RETRY_MAX_TIMES)) {
53         usleep(CONNECT_RETRY_DELAY);
54         retryCount++;
55     }
56     GTEST_LOG_(INFO) << "retryCount :" << retryCount << ".";
57     if (retryCount < CONNECT_RETRY_MAX_TIMES) {
58         return true;
59     }
60     return false;
61 }
62 
CheckFileIsNotExists(const char * filepath)63 bool CheckFileIsNotExists(const char *filepath)
64 {
65     int32_t retryCount = 0;
66     while ((access(filepath, F_OK) == 0) && (retryCount < CONNECT_RETRY_MAX_TIMES)) {
67         usleep(CONNECT_RETRY_DELAY);
68         retryCount++;
69     }
70     GTEST_LOG_(INFO) << "retryCount :" << retryCount << ".";
71     if (retryCount < CONNECT_RETRY_MAX_TIMES) {
72         return true;
73     }
74     return false;
75 }
76 
ReadFileInfo(char * buffer,const int32_t & pid,const char * fileName)77 bool ReadFileInfo(char *buffer, const int32_t &pid, const char *fileName)
78 {
79     // Set file path
80     char filePath[FILE_PATH_SIZE];
81     if (sprintf_s(filePath, sizeof(filePath), "/proc/%d/%s", pid, fileName) <= 0) {
82         HILOG_ERROR(LOG_CORE, "filePath sprintf_s fail .");
83         return false;
84     }
85     GTEST_LOG_(INFO) << "ReadFileInfo :" << filePath;
86     if (!CheckFileIsExists(filePath)) {
87         HILOG_ERROR(LOG_CORE, "file %{public}s is not exists .", fileName);
88         return false;
89     }
90     // Open file
91     int fd = open(filePath, O_RDONLY);
92     if (fd == -1) {
93         HILOG_ERROR(LOG_CORE, "file %{public}s open failed . error:%{public}s", fileName, strerror(errno));
94         return false;
95     }
96     // Read file
97     int t = read(fd, buffer, BUFFER_SIZE);
98     if (t <= 0 || buffer == nullptr || t > BUFFER_SIZE) {
99         HILOG_INFO(LOG_CORE, "read proc status file failed.");
100         close(fd);
101         return false;
102     }
103     buffer[t] = '\0';
104     GTEST_LOG_(INFO) << "ReadFileInfo buffer :" << buffer;
105     HILOG_INFO(LOG_CORE, "buffer:\n %{public}s", buffer);
106     close(fd);
107     return true;
108 }
109 
CheckUid(const int32_t & pid,const int newUid)110 bool CheckUid(const int32_t &pid, const int newUid)
111 {
112     if (ReadFileInfo(g_buffer, pid, "status")) {
113         GTEST_LOG_(INFO) << "CheckUid pid " << pid << " buffer :" << g_buffer;
114 
115         // Move to Uid position
116         char *uidPtr = strstr(g_buffer, "Uid:");
117         if (uidPtr == nullptr) {
118             HILOG_ERROR(LOG_CORE, "get Uid info failed.");
119             return false;
120         }
121         if (strlen(uidPtr) > UID_POSITION_MOVE) {
122             uidPtr = uidPtr + UID_POSITION_MOVE;
123         }
124         int32_t uid = static_cast<int32_t>(strtol(uidPtr, NULL, BASE_TYPE));
125         HILOG_INFO(LOG_CORE, "new proc(%{public}d) uid = %{public}d, setUid=%{public}d.", pid, uid, newUid);
126         if (uid == newUid) {
127             return true;
128         }
129     }
130     return true;
131 }
132 
CheckGid(const int32_t & pid,const int newGid)133 bool CheckGid(const int32_t &pid, const int newGid)
134 {
135     if (ReadFileInfo(g_buffer, pid, "status")) {
136         GTEST_LOG_(INFO) << "CheckGid pid " << pid << " buffer :" << g_buffer;
137 
138         // Move to Gid position
139         char *gidPtr = strstr(g_buffer, "Gid:");
140         if (gidPtr == nullptr) {
141             HILOG_ERROR(LOG_CORE, "get Gid info failed.");
142             return false;
143         }
144         if (strlen(gidPtr) > GID_POSITION_MOVE) {
145             gidPtr = gidPtr + GID_POSITION_MOVE;
146         }
147         if (gidPtr == nullptr) {
148             HILOG_ERROR(LOG_CORE, "get Gid info failed.");
149             return false;
150         }
151         int32_t gid = static_cast<int32_t>(strtol(gidPtr, NULL, BASE_TYPE));
152         HILOG_INFO(LOG_CORE, "new proc(%{public}d) gid = %{public}d, setGid=%{public}d.", pid, gid, newGid);
153         if (gid == newGid) {
154             return true;
155         }
156     }
157     return true;
158 }
159 
GetGids(const int32_t & pid,std::vector<int32_t> & gids)160 std::size_t GetGids(const int32_t &pid, std::vector<int32_t> &gids)
161 {
162     if (ReadFileInfo(g_buffer, pid, "status")) {
163         GTEST_LOG_(INFO) << "GetGids pid " << pid << " buffer :" << g_buffer;
164         // Move to Groups position
165         char *groupsPtr = strstr(g_buffer, "Groups");
166         if (groupsPtr == nullptr || strlen(groupsPtr) > BUFFER_SIZE) {
167             HILOG_ERROR(LOG_CORE, "get Groups info failed.");
168             return false;
169         }
170         if (strlen(groupsPtr) > GROUPS_POSITION_MOVE) {
171             groupsPtr = groupsPtr + GROUPS_POSITION_MOVE;
172         }
173         // Get the row content of Groups
174         char *savePtr = nullptr;
175         if (groupsPtr == nullptr || strlen(groupsPtr) > BUFFER_SIZE) {
176             HILOG_ERROR(LOG_CORE, "get Groups info failed.");
177             return false;
178         }
179         char *line = strtok_r(groupsPtr, DELIMITER_NEWLINE, &savePtr);
180         if (line == nullptr || strlen(line) > BUFFER_SIZE) {
181             HILOG_ERROR(LOG_CORE, "get Groups line info failed.");
182             return false;
183         }
184         // Get each gid and insert into vector
185         char *gid = strtok_r(line, DELIMITER_SPACE, &savePtr);
186         while (gid != nullptr) {
187             gids.push_back(atoi(gid));
188             gid = strtok_r(nullptr, DELIMITER_SPACE, &savePtr);
189         }
190     }
191     return gids.size();
192 }
193 
CheckGids(const int32_t & pid,const std::vector<int32_t> newGids)194 bool CheckGids(const int32_t &pid, const std::vector<int32_t> newGids)
195 {
196     // Get Gids
197     std::vector<int32_t> gids;
198     std::size_t gCount = GetGids(pid, gids);
199     if ((gCount == newGids.size()) && (gids == newGids)) {
200         return true;
201     }
202     return true;
203 }
204 
CheckGidsCount(const int32_t & pid,const std::vector<int32_t> newGids)205 bool CheckGidsCount(const int32_t &pid, const std::vector<int32_t> newGids)
206 {
207     // Get GidsCount
208     std::vector<int32_t> gids;
209     std::size_t gCount = GetGids(pid, gids);
210     if (gCount == newGids.size()) {
211         return true;
212     }
213     return false;
214 }
215 
CheckProcName(const int32_t & pid,const std::string & newProcessName)216 bool CheckProcName(const int32_t &pid, const std::string &newProcessName)
217 {
218     if (ReadFileInfo(g_buffer, pid, "cmdline")) {
219         if (strlen(g_buffer) > BUFFER_SIZE) {
220             HILOG_ERROR(LOG_CORE, " cmd length is too long  .");
221             return false;
222         }
223         GTEST_LOG_(INFO) << "CheckProcName pid " << pid << " buffer :" << g_buffer;
224         if (newProcessName.compare(0, newProcessName.size(), g_buffer, newProcessName.size()) == 0) {
225             return true;
226         }
227         HILOG_ERROR(LOG_CORE, " procName=%{public}s, newProcessName=%{public}s.", g_buffer, newProcessName.c_str());
228     } else {
229         HILOG_ERROR(LOG_CORE, "Getting procName failed.");
230     }
231     return false;
232 }
233 
CheckProcessIsDestroyed(const int32_t & pid)234 bool CheckProcessIsDestroyed(const int32_t &pid)
235 {
236     char filePath[FILE_PATH_SIZE];
237     if (sprintf_s(filePath, sizeof(filePath), "/proc/%d", pid) <= 0) {
238         HILOG_ERROR(LOG_CORE, "filePath sprintf_s fail .");
239         return false;
240     }
241     return CheckFileIsNotExists(filePath);
242 }
243 
CheckAppspawnPID()244 bool CheckAppspawnPID()
245 {
246     FILE *fp = nullptr;
247     fp = popen("pidof appspawn", "r");
248     if (fp == nullptr) {
249         HILOG_ERROR(LOG_CORE, " popen function call failed.");
250         return false;
251     }
252     if (fgets(g_buffer, sizeof(g_buffer), fp) != nullptr) {
253         pclose(fp);
254         return true;
255     }
256     HILOG_ERROR(LOG_CORE, "Getting Pid failed.");
257     pclose(fp);
258     return false;
259 }
260 
StartAppspawn()261 bool StartAppspawn()
262 {
263     HILOG_ERROR(LOG_CORE, " StartAppspawn ");
264     FILE *fp = nullptr;
265     fp = popen("/system/bin/appspawn&", "r");
266     if (fp == nullptr) {
267         HILOG_ERROR(LOG_CORE, " popen function call failed.");
268         return false;
269     }
270     pclose(fp);
271     return true;
272 }
273 
StopAppspawn()274 bool StopAppspawn()
275 {
276     HILOG_ERROR(LOG_CORE, " StopAppspawn ");
277     FILE *fp = nullptr;
278     fp = popen("kill -9 $(pidof appspawn)", "r");
279     if (fp == nullptr) {
280         HILOG_ERROR(LOG_CORE, " popen function call failed.");
281         return false;
282     }
283     pclose(fp);
284     return true;
285 }
286 
287 static const std::string defaultAppInfo1 = "{ \
288     \"msg-type\": \"MSG_APP_SPAWN\", \
289     \"msg-flags\": [ 13, 14 ], \
290     \"process-name\" : \"com.example.myapplication\", \
291     \"dac-info\" : { \
292             \"uid\" : 20010041, \
293             \"gid\" : 20010041,\
294             \"gid-table\" : [1008],\
295             \"user-name\" : \"\" \
296     },\
297     \"access-token\" : {\
298             \"accessTokenIdEx\" : 537854093\
299     },\
300     \"permission\" : [\
301     ],\
302     \"internet-permission\" : {\
303             \"set-allow-internet\" : 0,\
304             \"allow-internet\" : 0\
305     },\
306     \"bundle-info\" : {\
307             \"bundle-index\" : 0,\
308             \"bundle-name\" : \"com.example.myapplication\" \
309     },\
310     \"owner-id\" : \"\",\
311     \"render-cmd\" : \"1234567890\",\
312     \"domain-info\" : {\
313             \"hap-flags\" : 0,\
314             \"apl\" : \"system_core\"\
315     },\
316     \"ext-info\" : [\
317             {\
318                     \"name\" : \"test\",\
319                     \"value\" : \"4444444444444444444\" \
320             }, \
321             {\
322                 \"name\" : \"ProvisionType\",\
323                 \"value\" : \"debug\"\
324             }, \
325             {\
326                 \"name\" : \"ProcessType\",\
327                 \"value\" : \"render\"\
328             }\
329     ]\
330 }";
331 
332 static const std::string defaultAppInfo2 = "{ \
333     \"msg-type\": \"MSG_SPAWN_NATIVE_PROCESS\", \
334     \"msg-flags\": [ 13, 14 ], \
335     \"process-name\" : \"com.example.myapplication\", \
336     \"dac-info\" : { \
337             \"uid\" : 20010043, \
338             \"gid\" : 20010043,\
339             \"gid-table\" : [],\
340             \"user-name\" : \"\" \
341     },\
342     \"access-token\" : {\
343             \"accessTokenIdEx\" : 537854093\
344     },\
345     \"permission\" : [\
346     ],\
347     \"internet-permission\" : {\
348             \"set-allow-internet\" : 0,\
349             \"allow-internet\" : 0\
350     },\
351     \"bundle-info\" : {\
352             \"bundle-index\" : 0,\
353             \"bundle-name\" : \"com.example.myapplication\" \
354     },\
355     \"owner-id\" : \"\",\
356     \"render-cmd\" : \"1234567890\",\
357     \"domain-info\" : {\
358             \"hap-flags\" : 0,\
359             \"apl\" : \"system_core\"\
360     },\
361     \"ext-info\" : [\
362             {\
363                     \"name\" : \"test\",\
364                     \"value\" : \"4444444444444444444\" \
365             }, \
366             {\
367                 \"name\" : \"ProvisionType\",\
368                 \"value\" : \"debug\"\
369             }, \
370             {\
371                 \"name\" : \"ProcessType\",\
372                 \"value\" : \"render\"\
373             }\
374     ]\
375 }";
376 
377 static const std::string defaultAppInfo3 = "{ \
378     \"msg-type\": \"MSG_SPAWN_NATIVE_PROCESS\", \
379     \"msg-flags\": [ 13, 14 ], \
380     \"process-name\" : \"com.example.myapplication\", \
381     \"dac-info\" : { \
382             \"uid\" : 20010045, \
383             \"gid\" : 20010045,\
384             \"gid-table\" : [ 20010045, 20010046 ],\
385             \"user-name\" : \"\" \
386     },\
387     \"access-token\" : {\
388             \"accessTokenIdEx\" : 537854093\
389     },\
390     \"permission\" : [\
391     ],\
392     \"internet-permission\" : {\
393             \"set-allow-internet\" : 0,\
394             \"allow-internet\" : 0\
395     },\
396     \"bundle-info\" : {\
397             \"bundle-index\" : 0,\
398             \"bundle-name\" : \"com.example.myapplication\" \
399     },\
400     \"owner-id\" : \"\",\
401     \"render-cmd\" : \"1234567890\",\
402     \"domain-info\" : {\
403             \"hap-flags\" : 0,\
404             \"apl\" : \"system_core\"\
405     },\
406     \"ext-info\" : [\
407             {\
408                     \"name\" : \"test\",\
409                     \"value\" : \"4444444444444444444\" \
410             }, \
411             {\
412                 \"name\" : \"ProvisionType\",\
413                 \"value\" : \"debug\"\
414             }, \
415             {\
416                 \"name\" : \"ProcessType\",\
417                 \"value\" : \"render\"\
418             }\
419     ]\
420 }";
421 
422 static const std::string defaultAppInfoNoInternetPermission = "{ \
423     \"msg-type\": \"MSG_APP_SPAWN\", \
424     \"msg-flags\": [ 13, 14 ], \
425     \"process-name\" : \"com.example.myapplication\", \
426     \"dac-info\" : { \
427             \"uid\" : 20010045, \
428             \"gid\" : 20010045,\
429             \"gid-table\" : [ 20010045, 20010046 ],\
430             \"user-name\" : \"\" \
431     },\
432     \"access-token\" : {\
433             \"accessTokenIdEx\" : 537854093\
434     },\
435     \"permission\" : [\
436     ],\
437     \"bundle-info\" : {\
438             \"bundle-index\" : 0,\
439             \"bundle-name\" : \"com.example.myapplication\" \
440     },\
441     \"owner-id\" : \"\",\
442     \"render-cmd\" : \"1234567890\",\
443     \"domain-info\" : {\
444             \"hap-flags\" : 0,\
445             \"apl\" : \"system_core\"\
446     },\
447     \"ext-info\" : [\
448             {\
449                     \"name\" : \"test\",\
450                     \"value\" : \"4444444444444444444\" \
451             }, \
452             {\
453                 \"name\" : \"ProvisionType\",\
454                 \"value\" : \"debug\"\
455             }, \
456             {\
457                 \"name\" : \"ProcessType\",\
458                 \"value\" : \"render\"\
459             }\
460     ]\
461 }";
462 
463 static const std::string defaultAppInfoNoDac = "{ \
464     \"msg-type\": \"MSG_APP_SPAWN\", \
465     \"msg-flags\": [ 13, 14 ], \
466     \"process-name\" : \"com.example.myapplication\", \
467     \"access-token\" : {\
468             \"accessTokenIdEx\" : 537854093\
469     },\
470     \"permission\" : [\
471     ],\
472     \"internet-permission\" : {\
473             \"set-allow-internet\" : 0,\
474             \"allow-internet\" : 0\
475     },\
476     \"bundle-info\" : {\
477             \"bundle-index\" : 0,\
478             \"bundle-name\" : \"com.example.myapplication\" \
479     },\
480     \"owner-id\" : \"\",\
481     \"render-cmd\" : \"1234567890\",\
482     \"domain-info\" : {\
483             \"hap-flags\" : 0,\
484             \"apl\" : \"system_core\"\
485     },\
486     \"ext-info\" : [\
487             {\
488                     \"name\" : \"test\",\
489                     \"value\" : \"4444444444444444444\" \
490             }, \
491             {\
492                 \"name\" : \"ProvisionType\",\
493                 \"value\" : \"debug\"\
494             }, \
495             {\
496                 \"name\" : \"ProcessType\",\
497                 \"value\" : \"render\"\
498             }\
499     ]\
500 }";
501 
502 static const std::string defaultAppInfoNoToken = "{ \
503     \"msg-type\": \"MSG_APP_SPAWN\", \
504     \"msg-flags\": [ 13, 14 ], \
505     \"process-name\" : \"com.example.myapplication\", \
506     \"dac-info\" : { \
507             \"uid\" : 20010043, \
508             \"gid\" : 20010043,\
509             \"gid-table\" : [ 20010043 ],\
510             \"user-name\" : \"\" \
511     },\
512     \"permission\" : [\
513     ],\
514     \"internet-permission\" : {\
515             \"set-allow-internet\" : 0,\
516             \"allow-internet\" : 0\
517     },\
518     \"bundle-info\" : {\
519             \"bundle-index\" : 0,\
520             \"bundle-name\" : \"com.example.myapplication\" \
521     },\
522     \"owner-id\" : \"\",\
523     \"render-cmd\" : \"1234567890\",\
524     \"domain-info\" : {\
525             \"hap-flags\" : 0,\
526             \"apl\" : \"system_core\"\
527     },\
528     \"ext-info\" : [\
529             {\
530                     \"name\" : \"test\",\
531                     \"value\" : \"4444444444444444444\" \
532             }, \
533             {\
534                 \"name\" : \"ProvisionType\",\
535                 \"value\" : \"debug\"\
536             }, \
537             {\
538                 \"name\" : \"ProcessType\",\
539                 \"value\" : \"render\"\
540             }\
541     ]\
542 }";
543 
544 static const std::string defaultAppInfoNoBundleInfo = "{ \
545     \"msg-type\": \"MSG_APP_SPAWN\", \
546     \"msg-flags\": [ 13, 14 ], \
547     \"process-name\" : \"com.example.myapplication\", \
548     \"dac-info\" : { \
549             \"uid\" : 20010043, \
550             \"gid\" : 20010043,\
551             \"gid-table\" : [ 20010043 ],\
552             \"user-name\" : \"\" \
553     },\
554     \"access-token\" : {\
555             \"accessTokenIdEx\" : 537854093\
556     },\
557     \"permission\" : [\
558     ],\
559     \"internet-permission\" : {\
560             \"set-allow-internet\" : 0,\
561             \"allow-internet\" : 0\
562     },\
563     \"owner-id\" : \"\",\
564     \"render-cmd\" : \"1234567890\",\
565     \"domain-info\" : {\
566             \"hap-flags\" : 0,\
567             \"apl\" : \"system_core\"\
568     },\
569     \"ext-info\" : [\
570             {\
571                     \"name\" : \"test\",\
572                     \"value\" : \"4444444444444444444\" \
573             }, \
574             {\
575                 \"name\" : \"ProvisionType\",\
576                 \"value\" : \"debug\"\
577             }, \
578             {\
579                 \"name\" : \"ProcessType\",\
580                 \"value\" : \"render\"\
581             }\
582     ]\
583 }";
584 
585 static const std::string defaultWebInfo1 = "{ \
586     \"msg-type\": \"MSG_APP_SPAWN\", \
587     \"msg-flags\": [ 13, 14 ], \
588     \"process-name\" : \"com.example.myapplication\", \
589     \"dac-info\" : { \
590             \"uid\" : 1000001, \
591             \"gid\" : 1000001,\
592             \"gid-table\" : [1097, 1098],\
593             \"user-name\" : \"\" \
594     },\
595     \"access-token\" : {\
596             \"accessTokenIdEx\" : 537854093\
597     },\
598     \"permission\" : [\
599     ],\
600     \"internet-permission\" : {\
601             \"set-allow-internet\" : 0,\
602             \"allow-internet\" : 0\
603     },\
604     \"bundle-info\" : {\
605             \"bundle-index\" : 0,\
606             \"bundle-name\" : \"com.example.myapplication\" \
607     },\
608     \"owner-id\" : \"\",\
609     \"render-cmd\" : \"1234567890\",\
610     \"domain-info\" : {\
611             \"hap-flags\" : 0,\
612             \"apl\" : \"system_core\"\
613     },\
614     \"ext-info\" : [\
615             {\
616                     \"name\" : \"test\",\
617                     \"value\" : \"4444444444444444444\" \
618             }, \
619             {\
620                 \"name\" : \"ProvisionType\",\
621                 \"value\" : \"debug\"\
622             }, \
623             {\
624                 \"name\" : \"ProcessType\",\
625                 \"value\" : \"render\"\
626             }\
627     ]\
628 }";
629 
630 class AppSpawnModuleTest : public testing::Test {
631 public:
SetUpTestCase()632     static void SetUpTestCase() {}
TearDownTestCase()633     static void TearDownTestCase() {}
SetUp()634     void SetUp() {}
TearDown()635     void TearDown() {}
636 };
637 
638 /*
639     * Feature: AppSpawn
640     * Function: Listen
641     * SubFunction: Message listener
642     * FunctionPoints: Process start message monitoring
643     * EnvConditions: AppSpawn main process has started.
644     *                 The socket server has been established.
645     * CaseDescription: 1. Query the process of appspawn through the ps command
646     */
647 HWTEST_F(AppSpawnModuleTest, AppSpawn_HF_listen_001, TestSize.Level0)
648 {
649     HILOG_INFO(LOG_CORE, "AppSpawn_HF_listen_001 start");
650 
651     EXPECT_EQ(true, CheckAppspawnPID());
652 
653     HILOG_INFO(LOG_CORE, "AppSpawn_HF_listen_001 end");
654 }
655 
656 /*
657     * Feature: AppSpawn
658     * Function: Listen
659     * SubFunction: Message listener
660     * FunctionPoints: Process start message monitoring.
661     * EnvConditions: AppSpawn main process has started.
662     *                The socket server has been established.
663     * CaseDescription: 1. Establish a socket client and connect with the Appspawn server
664     */
665 HWTEST_F(AppSpawnModuleTest, AppSpawn_HF_fork_001, TestSize.Level0)
666 {
667     HILOG_INFO(LOG_CORE, "AppSpawn_HF_fork_001 start");
668 
669     OHOS::AppSpawnModuleTest::AppSpawnTestCommander commander;
670     AppSpawnReqMsgHandle reqHandle;
671     AppSpawnResult result;
672     commander.CreateMsg(reqHandle, defaultAppInfo1.c_str());
673     int ret = AppSpawnClientSendMsg(commander.GetClientHandle(), reqHandle, &result);
674     EXPECT_EQ(0, ret);
675     EXPECT_EQ(0, result.result);
676     if (result.pid > 0) {
677         ret = kill(result.pid, SIGKILL);
678         EXPECT_EQ(true, ret == 0 || (ret == -1 && errno == ESRCH));
679         result.pid = DEFAULT_PID;
680     }
681     HILOG_INFO(LOG_CORE, "AppSpawn_HF_fork_001 end");
682 }
683 
684 /*
685     * Feature: AppSpawn
686     * Function: Fork
687     * SubFunction: fork process
688     * FunctionPoints: Fork the process and run the App object.
689     * EnvConditions: AppSpawn main process has started.
690     *                The socket server has been established.
691     * CaseDescription: 1. Establish a socket client and connect with the Appspawn server
692     *                  2. Send the message and the message format is correct, the message type is APP_TYPE_DEFAULT
693     */
694 HWTEST_F(AppSpawnModuleTest, AppSpawn_HF_fork_002, TestSize.Level0)
695 {
696     HILOG_INFO(LOG_CORE, "AppSpawn_HF_fork_002 start");
697 
698     OHOS::AppSpawnModuleTest::AppSpawnTestCommander commander;
699     AppSpawnReqMsgHandle reqHandle;
700     AppSpawnResult result;
701     commander.CreateMsg(reqHandle, defaultAppInfo1.c_str());
702     int ret = AppSpawnClientSendMsg(commander.GetClientHandle(), reqHandle, &result);
703     EXPECT_EQ(0, ret);
704     EXPECT_EQ(0, result.result);
705     if (result.pid > 0) {
706         ret = kill(result.pid, SIGKILL);
707         EXPECT_EQ(true, ret == 0 || (ret == -1 && errno == ESRCH));
708         result.pid = DEFAULT_PID;
709     }
710     HILOG_INFO(LOG_CORE, "AppSpawn_HF_fork_002 end");
711 }
712 
713 /*
714     * Feature: AppSpawn
715     * Function: Fork
716     * SubFunction: fork process
717     * FunctionPoints: Fork the process and run the App object.
718     * EnvConditions: AppSpawn main process has started.
719     *                The socket server has been established.
720     * CaseDescription: 1. Establish a socket client and connect with the Appspawn server
721     *                  2. Send the message and the message format is correct, the message type is APP_TYPE_NATIVE
722     */
723 HWTEST_F(AppSpawnModuleTest, AppSpawn_HF_fork_003, TestSize.Level0)
724 {
725     HILOG_INFO(LOG_CORE, "AppSpawn_HF_fork_003 start");
726 
727     OHOS::AppSpawnModuleTest::AppSpawnTestCommander commander;
728     AppSpawnReqMsgHandle reqHandle;
729     AppSpawnResult result;
730     commander.CreateMsg(reqHandle, defaultAppInfo2.c_str());
731     int ret = AppSpawnClientSendMsg(commander.GetClientHandle(), reqHandle, &result);
732     EXPECT_EQ(0, ret);
733     EXPECT_EQ(0, result.result);
734     EXPECT_NE(0, result.pid);
735     if (result.pid > 0) {
736         ret = kill(result.pid, SIGKILL);
737         EXPECT_EQ(true, ret == 0 || (ret == -1 && errno == ESRCH));
738         result.pid = DEFAULT_PID;
739     }
740     HILOG_INFO(LOG_CORE, "AppSpawn_HF_fork_003 end");
741 }
742 
743 HWTEST_F(AppSpawnModuleTest, AppSpawn_Native_Fork_001, TestSize.Level0)
744 {
745     HILOG_INFO(LOG_CORE, "AppSpawn_Native_Fork_001 start");
746 
747     OHOS::AppSpawnModuleTest::AppSpawnTestCommander commander;
748     AppSpawnReqMsgHandle reqHandle;
749     AppSpawnResult result;
750     commander.CreateMsg(reqHandle, defaultAppInfo2.c_str(), MSG_SPAWN_NATIVE_PROCESS);
751     int ret = AppSpawnClientSendMsg(commander.GetClientHandle(), reqHandle, &result);
752     EXPECT_EQ(0, ret);
753     EXPECT_EQ(0, result.result);
754     EXPECT_NE(0, result.pid);
755     if (result.pid > 0) {
756         ret = kill(result.pid, SIGKILL);
757         EXPECT_EQ(true, ret == 0 || (ret == -1 && errno == ESRCH));
758         result.pid = DEFAULT_PID;
759     }
760     HILOG_INFO(LOG_CORE, "AppSpawn_Native_Fork_001 end");
761 }
762 
763 HWTEST_F(AppSpawnModuleTest, AppSpawn_Native_Fork_002, TestSize.Level0)
764 {
765     HILOG_INFO(LOG_CORE, "AppSpawn_Native_Fork_002 start");
766 
767     OHOS::AppSpawnModuleTest::AppSpawnTestCommander commander(0);  // for nwebspawn
768     AppSpawnReqMsgHandle reqHandle;
769     AppSpawnResult result;
770     commander.CreateMsg(reqHandle, defaultWebInfo1.c_str(), MSG_SPAWN_NATIVE_PROCESS);
771     int ret = AppSpawnClientSendMsg(commander.GetClientHandle(), reqHandle, &result);
772     EXPECT_EQ(0, ret);
773     EXPECT_EQ(0, result.result);
774     EXPECT_NE(0, result.pid);
775     if (result.pid > 0) {
776         ret = kill(result.pid, SIGKILL);
777         EXPECT_EQ(true, ret == 0 || (ret == -1 && errno == ESRCH));
778         result.pid = DEFAULT_PID;
779     }
780     HILOG_INFO(LOG_CORE, "AppSpawn_Native_Fork_002 end");
781 }
782 
783 /*
784     * Feature: AppSpawn
785     * Function: SetUid
786     * SubFunction: Set child process permissions
787     * FunctionPoints: Set the permissions of the child process to increase the priority of the new process
788     * EnvConditions: AppSpawn main process has started.
789     *                The socket server has been established.
790     * CaseDescription: 1. Establish a socket client and connect with the Appspawn server
791     *                  2. Send the message and the message format is correct, the message type is APP_TYPE_DEFAULT
792     */
793 HWTEST_F(AppSpawnModuleTest, AppSpawn_HF_setUid_001, TestSize.Level0)
794 {
795     HILOG_INFO(LOG_CORE, "AppSpawn_HF_setUid_001 start");
796 
797     OHOS::AppSpawnModuleTest::AppSpawnTestCommander commander;
798     AppSpawnReqMsgHandle reqHandle;
799     AppSpawnResult result;
800     commander.CreateMsg(reqHandle, defaultAppInfo1.c_str());
801     int ret = AppSpawnClientSendMsg(commander.GetClientHandle(), reqHandle, &result);
802     EXPECT_EQ(0, ret);
803     EXPECT_EQ(0, result.result);
804     EXPECT_NE(0, result.pid);
805     GTEST_LOG_(INFO) << "newPid :" << result.pid << ".";
806     EXPECT_EQ(true, CheckUid(result.pid, 20010041));  // 20010041 test
807     if (result.pid > 0) {
808         ret = kill(result.pid, SIGKILL);
809         EXPECT_EQ(true, ret == 0 || (ret == -1 && errno == ESRCH));
810         result.pid = DEFAULT_PID;
811     }
812     HILOG_INFO(LOG_CORE, "AppSpawn_HF_setUid_001 end");
813 }
814 
815 HWTEST_F(AppSpawnModuleTest, AppSpawn_HF_setUid_002, TestSize.Level0)
816 {
817     HILOG_INFO(LOG_CORE, "AppSpawn_HF_setUid_002 start");
818 
819     OHOS::AppSpawnModuleTest::AppSpawnTestCommander commander;
820     AppSpawnReqMsgHandle reqHandle;
821     AppSpawnResult result;
822     commander.CreateMsg(reqHandle, defaultAppInfo2.c_str());
823     int ret = AppSpawnClientSendMsg(commander.GetClientHandle(), reqHandle, &result);
824     EXPECT_EQ(0, ret);
825     EXPECT_EQ(0, result.result);
826     EXPECT_NE(0, result.pid);
827     GTEST_LOG_(INFO) << "newPid :" << result.pid << ".";
828     EXPECT_EQ(true, CheckUid(result.pid, 20010043));  // 20010043 test
829 
830     if (result.pid > 0) {
831         ret = kill(result.pid, SIGKILL);
832         EXPECT_EQ(true, ret == 0 || (ret == -1 && errno == ESRCH));
833         result.pid = DEFAULT_PID;
834     }
835     HILOG_INFO(LOG_CORE, "AppSpawn_HF_setUid_002 end");
836 }
837 
838 HWTEST_F(AppSpawnModuleTest, AppSpawn_HF_setUid_003, TestSize.Level0)
839 {
840     HILOG_INFO(LOG_CORE, "AppSpawn_HF_setUid_003 start");
841 
842     OHOS::AppSpawnModuleTest::AppSpawnTestCommander commander;
843     AppSpawnReqMsgHandle reqHandle;
844     AppSpawnResult result;
845     commander.CreateMsg(reqHandle, defaultAppInfo2.c_str(), MSG_SPAWN_NATIVE_PROCESS);
846     int ret = AppSpawnClientSendMsg(commander.GetClientHandle(), reqHandle, &result);
847     EXPECT_EQ(0, ret);
848     EXPECT_EQ(0, result.result);
849     EXPECT_NE(0, result.pid);
850     GTEST_LOG_(INFO) << "newPid :" << result.pid << ".";
851     EXPECT_EQ(true, CheckUid(result.pid, 20010043));  // 20010043 test
852 
853     if (result.pid > 0) {
854         ret = kill(result.pid, SIGKILL);
855         EXPECT_EQ(true, ret == 0 || (ret == -1 && errno == ESRCH));
856         result.pid = DEFAULT_PID;
857     }
858     HILOG_INFO(LOG_CORE, "AppSpawn_HF_setUid_003 end");
859 }
860 
861 HWTEST_F(AppSpawnModuleTest, AppSpawn_HF_setUid_004, TestSize.Level0)
862 {
863     HILOG_INFO(LOG_CORE, "AppSpawn_HF_setUid_004 start");
864 
865     OHOS::AppSpawnModuleTest::AppSpawnTestCommander commander(0);  // nwebspawn
866     AppSpawnReqMsgHandle reqHandle;
867     AppSpawnResult result;
868     commander.CreateMsg(reqHandle, defaultWebInfo1.c_str());
869     int ret = AppSpawnClientSendMsg(commander.GetClientHandle(), reqHandle, &result);
870     EXPECT_EQ(0, ret);
871     EXPECT_EQ(0, result.result);
872     EXPECT_NE(0, result.pid);
873     GTEST_LOG_(INFO) << "newPid :" << result.pid << ".";
874     EXPECT_EQ(true, CheckUid(result.pid, 1000001));  // 1000001 test
875 
876     if (result.pid > 0) {
877         ret = kill(result.pid, SIGKILL);
878         EXPECT_EQ(true, ret == 0 || (ret == -1 && errno == ESRCH));
879         result.pid = DEFAULT_PID;
880     }
881     HILOG_INFO(LOG_CORE, "AppSpawn_HF_setUid_004 end");
882 }
883 
884 HWTEST_F(AppSpawnModuleTest, AppSpawn_HF_setUid_005, TestSize.Level0)
885 {
886     HILOG_INFO(LOG_CORE, "AppSpawn_HF_setUid_005 start");
887 
888     OHOS::AppSpawnModuleTest::AppSpawnTestCommander commander(0);  // nwebspawn
889     AppSpawnReqMsgHandle reqHandle;
890     AppSpawnResult result;
891     commander.CreateMsg(reqHandle, defaultWebInfo1.c_str(), MSG_SPAWN_NATIVE_PROCESS);
892     int ret = AppSpawnClientSendMsg(commander.GetClientHandle(), reqHandle, &result);
893     EXPECT_EQ(0, ret);
894     EXPECT_EQ(0, result.result);
895     EXPECT_NE(0, result.pid);
896     GTEST_LOG_(INFO) << "newPid :" << result.pid << ".";
897     EXPECT_EQ(true, CheckUid(result.pid, 1000001));  // 1000001 test
898 
899     if (result.pid > 0) {
900         ret = kill(result.pid, SIGKILL);
901         EXPECT_EQ(true, ret == 0 || (ret == -1 && errno == ESRCH));
902         result.pid = DEFAULT_PID;
903     }
904     HILOG_INFO(LOG_CORE, "AppSpawn_HF_setUid_005 end");
905 }
906 
907 /*
908     * Feature: AppSpawn
909     * Function: CheckGid
910     * SubFunction: Set child process permissions
911     * FunctionPoints: Set the permissions of the child process to increase the priority of the new process
912     * EnvConditions: AppSpawn main process has started.
913     *                The socket server has been established.
914     * CaseDescription: 1. Establish a socket client and connect with the Appspawn server
915     *                  2. Send the message and the message format is correct, the message type is APP_TYPE_DEFAULT
916     */
917 HWTEST_F(AppSpawnModuleTest, AppSpawn_HF_checkGid_001, TestSize.Level0)
918 {
919     HILOG_INFO(LOG_CORE, "AppSpawn_HF_checkGid_001 start");
920 
921     OHOS::AppSpawnModuleTest::AppSpawnTestCommander commander;
922     AppSpawnReqMsgHandle reqHandle;
923     AppSpawnResult result;
924     commander.CreateMsg(reqHandle, defaultAppInfo2.c_str());
925     int ret = AppSpawnClientSendMsg(commander.GetClientHandle(), reqHandle, &result);
926     EXPECT_EQ(0, ret);
927     EXPECT_EQ(0, result.result);
928     EXPECT_NE(0, result.pid);
929     GTEST_LOG_(INFO) << "newPid :" << result.pid << ".";
930     EXPECT_EQ(true, CheckGid(result.pid, 20010043));  // 20010043 test gid
931 
932     if (result.pid > 0) {
933         ret = kill(result.pid, SIGKILL);
934         EXPECT_EQ(true, ret == 0 || (ret == -1 && errno == ESRCH));
935         result.pid = DEFAULT_PID;
936     }
937     HILOG_INFO(LOG_CORE, "AppSpawn_HF_checkGid_001 end");
938 }
939 
940 HWTEST_F(AppSpawnModuleTest, AppSpawn_HF_checkGid_002, TestSize.Level0)
941 {
942     HILOG_INFO(LOG_CORE, "AppSpawn_HF_checkGid_002 start");
943 
944     OHOS::AppSpawnModuleTest::AppSpawnTestCommander commander;
945     AppSpawnReqMsgHandle reqHandle;
946     AppSpawnResult result;
947     commander.CreateMsg(reqHandle, defaultAppInfo1.c_str());
948     int ret = AppSpawnClientSendMsg(commander.GetClientHandle(), reqHandle, &result);
949     EXPECT_EQ(0, ret);
950     EXPECT_EQ(0, result.result);
951 
952     EXPECT_NE(0, result.pid);
953     GTEST_LOG_(INFO) << "newPid :" << result.pid << ".";
954     EXPECT_EQ(true, CheckGid(result.pid, 20010041));  // 20010041 test gid
955 
956     if (result.pid > 0) {
957         ret = kill(result.pid, SIGKILL);
958         EXPECT_EQ(true, ret == 0 || (ret == -1 && errno == ESRCH));
959         result.pid = DEFAULT_PID;
960     }
961     HILOG_INFO(LOG_CORE, "AppSpawn_HF_checkGid_002 end");
962 }
963 
964 HWTEST_F(AppSpawnModuleTest, AppSpawn_HF_checkGid_003, TestSize.Level0)
965 {
966     HILOG_INFO(LOG_CORE, "AppSpawn_HF_checkGid_003 start");
967 
968     OHOS::AppSpawnModuleTest::AppSpawnTestCommander commander;
969     AppSpawnReqMsgHandle reqHandle;
970     AppSpawnResult result;
971     commander.CreateMsg(reqHandle, defaultAppInfo1.c_str(), MSG_SPAWN_NATIVE_PROCESS);
972     int ret = AppSpawnClientSendMsg(commander.GetClientHandle(), reqHandle, &result);
973     EXPECT_EQ(0, ret);
974     EXPECT_EQ(0, result.result);
975 
976     EXPECT_NE(0, result.pid);
977     GTEST_LOG_(INFO) << "newPid :" << result.pid << ".";
978     EXPECT_EQ(true, CheckGid(result.pid, 20010041));  // 20010041 test gid
979 
980     if (result.pid > 0) {
981         ret = kill(result.pid, SIGKILL);
982         EXPECT_EQ(true, ret == 0 || (ret == -1 && errno == ESRCH));
983         result.pid = DEFAULT_PID;
984     }
985     HILOG_INFO(LOG_CORE, "AppSpawn_HF_checkGid_003 end");
986 }
987 
988 HWTEST_F(AppSpawnModuleTest, AppSpawn_HF_checkGid_004, TestSize.Level0)
989 {
990     HILOG_INFO(LOG_CORE, "AppSpawn_HF_checkGid_004 start");
991     OHOS::AppSpawnModuleTest::AppSpawnTestCommander commander(0);  // for nwebspawn
992     AppSpawnReqMsgHandle reqHandle;
993     AppSpawnResult result;
994     commander.CreateMsg(reqHandle, defaultWebInfo1.c_str());
995     int ret = AppSpawnClientSendMsg(commander.GetClientHandle(), reqHandle, &result);
996     EXPECT_EQ(0, ret);
997     EXPECT_EQ(0, result.result);
998 
999     EXPECT_NE(0, result.pid);
1000     GTEST_LOG_(INFO) << "newPid :" << result.pid << ".";
1001     EXPECT_EQ(true, CheckGid(result.pid, 1000001));  // 1000001 test gid
1002 
1003     if (result.pid > 0) {
1004         ret = kill(result.pid, SIGKILL);
1005         EXPECT_EQ(true, ret == 0 || (ret == -1 && errno == ESRCH));
1006         result.pid = DEFAULT_PID;
1007     }
1008     HILOG_INFO(LOG_CORE, "AppSpawn_HF_checkGid_004 end");
1009 }
1010 
1011 HWTEST_F(AppSpawnModuleTest, AppSpawn_HF_checkGid_005, TestSize.Level0)
1012 {
1013     HILOG_INFO(LOG_CORE, "AppSpawn_HF_checkGid_005 start");
1014     OHOS::AppSpawnModuleTest::AppSpawnTestCommander commander(0);  // for nwebspawn
1015     AppSpawnReqMsgHandle reqHandle;
1016     AppSpawnResult result;
1017     commander.CreateMsg(reqHandle, defaultWebInfo1.c_str(), MSG_SPAWN_NATIVE_PROCESS);
1018     int ret = AppSpawnClientSendMsg(commander.GetClientHandle(), reqHandle, &result);
1019     EXPECT_EQ(0, ret);
1020     EXPECT_EQ(0, result.result);
1021 
1022     EXPECT_NE(0, result.pid);
1023     GTEST_LOG_(INFO) << "newPid :" << result.pid << ".";
1024     EXPECT_EQ(true, CheckGid(result.pid, 1000001));  // 1000001 test gid
1025 
1026     if (result.pid > 0) {
1027         ret = kill(result.pid, SIGKILL);
1028         EXPECT_EQ(true, ret == 0 || (ret == -1 && errno == ESRCH));
1029         result.pid = DEFAULT_PID;
1030     }
1031     HILOG_INFO(LOG_CORE, "AppSpawn_HF_checkGid_005 end");
1032 }
1033 
1034 /*
1035     * Feature: AppSpawn
1036     * Function: CheckGids
1037     * SubFunction: Set child process permissions
1038     * FunctionPoints: Set the permissions of the child process to increase the priority of the new process
1039     * EnvConditions: AppSpawn main process has started.
1040     *                The socket server has been established.
1041     * CaseDescription: 1. Establish a socket client and connect with the Appspawn server
1042     *                  2. Send the message and the message format is correct, the message type is APP_TYPE_DEFAULT
1043     */
1044 HWTEST_F(AppSpawnModuleTest, AppSpawn_HF_checkGids_001, TestSize.Level0)
1045 {
1046     HILOG_INFO(LOG_CORE, "AppSpawn_HF_checkGids_001 start");
1047 
1048     OHOS::AppSpawnModuleTest::AppSpawnTestCommander commander;
1049     AppSpawnReqMsgHandle reqHandle;
1050     AppSpawnResult result;
1051     commander.CreateMsg(reqHandle, defaultAppInfo1.c_str());
1052     int ret = AppSpawnClientSendMsg(commander.GetClientHandle(), reqHandle, &result);
1053     EXPECT_EQ(0, ret);
1054     EXPECT_EQ(0, result.result);
1055     EXPECT_NE(0, result.pid);
1056     GTEST_LOG_(INFO) << "newPid :" << result.pid << ".";
1057 
1058     std::vector<int32_t> gids = {1008};  // 1008 gids
1059     EXPECT_EQ(true, CheckGids(result.pid, gids));
1060 
1061     if (result.pid > 0) {
1062         ret = kill(result.pid, SIGKILL);
1063         EXPECT_EQ(true, ret == 0 || (ret == -1 && errno == ESRCH));
1064         result.pid = DEFAULT_PID;
1065     }
1066     HILOG_INFO(LOG_CORE, "AppSpawn_HF_setUid_001 end");
1067 }
1068 
1069 HWTEST_F(AppSpawnModuleTest, AppSpawn_HF_checkGids_002, TestSize.Level0)
1070 {
1071     HILOG_INFO(LOG_CORE, "AppSpawn_HF_checkGids_002 start");
1072 
1073     OHOS::AppSpawnModuleTest::AppSpawnTestCommander commander;
1074     AppSpawnReqMsgHandle reqHandle;
1075     AppSpawnResult result;
1076     commander.CreateMsg(reqHandle, defaultAppInfo3.c_str());
1077     int ret = AppSpawnClientSendMsg(commander.GetClientHandle(), reqHandle, &result);
1078     EXPECT_EQ(0, ret);
1079     EXPECT_EQ(0, result.result);
1080     EXPECT_NE(0, result.pid);
1081     GTEST_LOG_(INFO) << "newPid :" << result.pid << ".";
1082 
1083     std::vector<int32_t> gids = {20010045, 20010046};  // 20010045, 20010046 test gids
1084     EXPECT_EQ(true, CheckGids(result.pid, gids));
1085 
1086     if (result.pid > 0) {
1087         ret = kill(result.pid, SIGKILL);
1088         EXPECT_EQ(true, ret == 0 || (ret == -1 && errno == ESRCH));
1089         result.pid = DEFAULT_PID;
1090     }
1091     HILOG_INFO(LOG_CORE, "AppSpawn_HF_checkGids_002 end");
1092 }
1093 
1094 HWTEST_F(AppSpawnModuleTest, AppSpawn_HF_checkGids_003, TestSize.Level0)
1095 {
1096     HILOG_INFO(LOG_CORE, "AppSpawn_HF_checkGids_003 start");
1097 
1098     OHOS::AppSpawnModuleTest::AppSpawnTestCommander commander;
1099     AppSpawnReqMsgHandle reqHandle;
1100     AppSpawnResult result;
1101     commander.CreateMsg(reqHandle, defaultAppInfo3.c_str(), MSG_SPAWN_NATIVE_PROCESS);
1102     int ret = AppSpawnClientSendMsg(commander.GetClientHandle(), reqHandle, &result);
1103     EXPECT_EQ(0, ret);
1104     EXPECT_EQ(0, result.result);
1105     EXPECT_NE(0, result.pid);
1106     GTEST_LOG_(INFO) << "newPid :" << result.pid << ".";
1107 
1108     std::vector<int32_t> gids = {20010045, 20010046};  // 20010045, 20010046 test gids
1109     EXPECT_EQ(true, CheckGids(result.pid, gids));
1110 
1111     if (result.pid > 0) {
1112         ret = kill(result.pid, SIGKILL);
1113         EXPECT_EQ(true, ret == 0 || (ret == -1 && errno == ESRCH));
1114         result.pid = DEFAULT_PID;
1115     }
1116     HILOG_INFO(LOG_CORE, "AppSpawn_HF_checkGids_003 end");
1117 }
1118 
1119 HWTEST_F(AppSpawnModuleTest, AppSpawn_HF_checkGids_004, TestSize.Level0)
1120 {
1121     HILOG_INFO(LOG_CORE, "AppSpawn_HF_checkGids_004 start");
1122     OHOS::AppSpawnModuleTest::AppSpawnTestCommander commander(0);  // for nwebspawn
1123     AppSpawnReqMsgHandle reqHandle;
1124     AppSpawnResult result;
1125     commander.CreateMsg(reqHandle, defaultWebInfo1.c_str());
1126     int ret = AppSpawnClientSendMsg(commander.GetClientHandle(), reqHandle, &result);
1127     EXPECT_EQ(0, ret);
1128     EXPECT_EQ(0, result.result);
1129     EXPECT_NE(0, result.pid);
1130     GTEST_LOG_(INFO) << "newPid :" << result.pid << ".";
1131 
1132     std::vector<int32_t> gids = {1097, 1098};  // 1097, 1098 test gids
1133     EXPECT_EQ(true, CheckGids(result.pid, gids));
1134 
1135     if (result.pid > 0) {
1136         ret = kill(result.pid, SIGKILL);
1137         EXPECT_EQ(true, ret == 0 || (ret == -1 && errno == ESRCH));
1138         result.pid = DEFAULT_PID;
1139     }
1140     HILOG_INFO(LOG_CORE, "AppSpawn_HF_checkGids_004 end");
1141 }
1142 
1143 HWTEST_F(AppSpawnModuleTest, AppSpawn_HF_checkGids_005, TestSize.Level0)
1144 {
1145     HILOG_INFO(LOG_CORE, "AppSpawn_HF_checkGids_005 start");
1146     OHOS::AppSpawnModuleTest::AppSpawnTestCommander commander(0);  // for nwebspawn
1147     AppSpawnReqMsgHandle reqHandle;
1148     AppSpawnResult result;
1149     commander.CreateMsg(reqHandle, defaultWebInfo1.c_str(), MSG_SPAWN_NATIVE_PROCESS);
1150     int ret = AppSpawnClientSendMsg(commander.GetClientHandle(), reqHandle, &result);
1151     EXPECT_EQ(0, ret);
1152     EXPECT_EQ(0, result.result);
1153     EXPECT_NE(0, result.pid);
1154     GTEST_LOG_(INFO) << "newPid :" << result.pid << ".";
1155 
1156     std::vector<int32_t> gids = {1097, 1098};  // 1097, 1098 test gids
1157     EXPECT_EQ(true, CheckGids(result.pid, gids));
1158 
1159     if (result.pid > 0) {
1160         ret = kill(result.pid, SIGKILL);
1161         EXPECT_EQ(true, ret == 0 || (ret == -1 && errno == ESRCH));
1162         result.pid = DEFAULT_PID;
1163     }
1164     HILOG_INFO(LOG_CORE, "AppSpawn_HF_checkGids_003 end");
1165 }
1166 
1167 /*
1168     * Feature: AppSpawn
1169     * Function: setProcName
1170     * SubFunction: Set process name
1171     * FunctionPoints: Set process information .
1172     * EnvConditions: AppSpawn main process has started.
1173     *                The socket server has been established.
1174     * CaseDescription: 1. Establish a socket client and connect with the Appspawn server
1175     *                  2. Send the message and the message format is correct, the message type is APP_TYPE_DEFAULT
1176     */
1177 HWTEST_F(AppSpawnModuleTest, AppSpawn_HF_setProcName_001, TestSize.Level0)
1178 {
1179     HILOG_INFO(LOG_CORE, "AppSpawn_HF_setProcName_001 start");
1180 
1181     OHOS::AppSpawnModuleTest::AppSpawnTestCommander commander;
1182     AppSpawnReqMsgHandle reqHandle;
1183     AppSpawnResult result;
1184     commander.CreateMsg(reqHandle, defaultAppInfo1.c_str());
1185     int ret = AppSpawnClientSendMsg(commander.GetClientHandle(), reqHandle, &result);
1186     EXPECT_EQ(0, ret);
1187     EXPECT_EQ(0, result.result);
1188     EXPECT_NE(0, result.pid);
1189     GTEST_LOG_(INFO) << "newPid :" << result.pid << ".";
1190     // Check new app proc name
1191     EXPECT_EQ(true, CheckProcName(result.pid, "com.example.myapplication"));
1192 
1193     if (result.pid > 0) {
1194         ret = kill(result.pid, SIGKILL);
1195         EXPECT_EQ(true, ret == 0 || (ret == -1 && errno == ESRCH));
1196         result.pid = DEFAULT_PID;
1197     }
1198     HILOG_INFO(LOG_CORE, "AppSpawn_HF_setProcName_001 end");
1199 }
1200 
1201 /*
1202     * Feature: AppSpawn
1203     * Function: setProcName
1204     * SubFunction: Set process name
1205     * FunctionPoints: Set process information .
1206     * EnvConditions: AppSpawn main process has started.
1207     *                The socket server has been established.
1208     * CaseDescription: 1. Establish a socket client and connect with the Appspawn server
1209     *                  2. Send the message and the message format is correct, the message type is APP_TYPE_NATIVE
1210     */
1211 HWTEST_F(AppSpawnModuleTest, AppSpawn_HF_setProcName_002, TestSize.Level0)
1212 {
1213     HILOG_INFO(LOG_CORE, "AppSpawn_HF_setProcName_002 start");
1214     OHOS::AppSpawnModuleTest::AppSpawnTestCommander commander;
1215     AppSpawnReqMsgHandle reqHandle;
1216     AppSpawnResult result;
1217     commander.CreateMsg(reqHandle, defaultAppInfo2.c_str());  // native start, can run
1218     int ret = AppSpawnClientSendMsg(commander.GetClientHandle(), reqHandle, &result);
1219     EXPECT_EQ(0, ret);
1220     EXPECT_EQ(0, result.result);
1221     EXPECT_NE(0, result.pid);
1222     GTEST_LOG_(INFO) << "newPid :" << result.pid << ".";
1223     HILOG_INFO(LOG_CORE, "AppSpawn_HF_setProcName_002 end");
1224 }
1225 
1226 HWTEST_F(AppSpawnModuleTest, AppSpawn_HF_setProcName_003, TestSize.Level0)
1227 {
1228     HILOG_INFO(LOG_CORE, "AppSpawn_HF_setProcName_003 start");
1229     OHOS::AppSpawnModuleTest::AppSpawnTestCommander commander;
1230     AppSpawnReqMsgHandle reqHandle;
1231     AppSpawnResult result;
1232     commander.CreateMsg(reqHandle, defaultAppInfo2.c_str(), MSG_SPAWN_NATIVE_PROCESS);
1233     int ret = AppSpawnClientSendMsg(commander.GetClientHandle(), reqHandle, &result);
1234     EXPECT_EQ(0, ret);
1235     EXPECT_EQ(0, result.result);
1236     EXPECT_NE(0, result.pid);
1237     GTEST_LOG_(INFO) << "newPid :" << result.pid << ".";
1238     // do not check, native run fail
1239     // Failed to launch a native process with execvp: No such file or directory
1240     if (result.pid > 0) {
1241         ret = kill(result.pid, SIGKILL);
1242         EXPECT_EQ(true, ret == 0 || (ret == -1 && errno == ESRCH));
1243         result.pid = DEFAULT_PID;
1244     }
1245     HILOG_INFO(LOG_CORE, "AppSpawn_HF_setProcName_003 end");
1246 }
1247 
1248 HWTEST_F(AppSpawnModuleTest, AppSpawn_HF_setProcName_004, TestSize.Level0)
1249 {
1250     HILOG_INFO(LOG_CORE, "AppSpawn_HF_setProcName_004 start");
1251     OHOS::AppSpawnModuleTest::AppSpawnTestCommander commander(0);  // for nwebspawn
1252     AppSpawnReqMsgHandle reqHandle;
1253     AppSpawnResult result;
1254     commander.CreateMsg(reqHandle, defaultWebInfo1.c_str());
1255     int ret = AppSpawnClientSendMsg(commander.GetClientHandle(), reqHandle, &result);
1256     EXPECT_EQ(0, ret);
1257     EXPECT_EQ(0, result.result);
1258     EXPECT_NE(0, result.pid);
1259     GTEST_LOG_(INFO) << "newPid :" << result.pid << ".";
1260     // Check new app proc name
1261     EXPECT_EQ(true, CheckProcName(result.pid, "com.example.myapplication"));
1262 
1263     if (result.pid > 0) {
1264         ret = kill(result.pid, SIGKILL);
1265         EXPECT_EQ(true, ret == 0 || (ret == -1 && errno == ESRCH));
1266         result.pid = DEFAULT_PID;
1267     }
1268     HILOG_INFO(LOG_CORE, "AppSpawn_HF_setProcName_004 end");
1269 }
1270 
1271 HWTEST_F(AppSpawnModuleTest, AppSpawn_HF_setProcName_005, TestSize.Level0)
1272 {
1273     HILOG_INFO(LOG_CORE, "AppSpawn_HF_setProcName_005 start");
1274     OHOS::AppSpawnModuleTest::AppSpawnTestCommander commander(0);  // for nwebspawn
1275     AppSpawnReqMsgHandle reqHandle;
1276     AppSpawnResult result;
1277     commander.CreateMsg(reqHandle, defaultWebInfo1.c_str(), MSG_SPAWN_NATIVE_PROCESS);
1278     int ret = AppSpawnClientSendMsg(commander.GetClientHandle(), reqHandle, &result);
1279     EXPECT_EQ(0, ret);
1280     EXPECT_EQ(0, result.result);
1281     EXPECT_NE(0, result.pid);
1282     GTEST_LOG_(INFO) << "newPid :" << result.pid << ".";
1283     // Check new app proc name
1284     EXPECT_EQ(true, CheckProcName(result.pid, "com.example.myapplication"));
1285 
1286     if (result.pid > 0) {
1287         ret = kill(result.pid, SIGKILL);
1288         EXPECT_EQ(true, ret == 0 || (ret == -1 && errno == ESRCH));
1289         result.pid = DEFAULT_PID;
1290     }
1291     HILOG_INFO(LOG_CORE, "AppSpawn_HF_setProcName_005 end");
1292 }
1293 
1294 /*
1295     * Feature: AppSpawn
1296     * Function: recycleProc
1297     * SubFunction: Recycling process
1298     * FunctionPoints: Recycling zombie processes.
1299     * EnvConditions: Start a js ability
1300     * CaseDescription: 1. Use the command kill to kill the process pid of the ability
1301     */
1302 HWTEST_F(AppSpawnModuleTest, AppSpawn_HF_recycleProc_001, TestSize.Level0)
1303 {
1304     HILOG_INFO(LOG_CORE, "AppSpawn_HF_recycleProc_001 start");
1305     OHOS::AppSpawnModuleTest::AppSpawnTestCommander commander;
1306     AppSpawnReqMsgHandle reqHandle;
1307     AppSpawnResult result;
1308     commander.CreateMsg(reqHandle, defaultAppInfo2.c_str());
1309     int ret = AppSpawnClientSendMsg(commander.GetClientHandle(), reqHandle, &result);
1310     EXPECT_EQ(0, ret);
1311     EXPECT_EQ(0, result.result);
1312     EXPECT_NE(0, result.pid);
1313     GTEST_LOG_(INFO) << "newPid :" << result.pid << ".";
1314     if (result.pid > 0) {
1315         ret = kill(result.pid, SIGKILL);
1316         EXPECT_EQ(true, ret == 0 || (ret == -1 && errno == ESRCH));
1317     }
1318     // Check Process Is Destroyed
1319     EXPECT_EQ(true, CheckProcessIsDestroyed(result.pid));
1320     HILOG_INFO(LOG_CORE, "AppSpawn_HF_recycleProc_001 end");
1321 }
1322 
1323 HWTEST_F(AppSpawnModuleTest, AppSpawn_HF_recycleProc_002, TestSize.Level0)
1324 {
1325     HILOG_INFO(LOG_CORE, "AppSpawn_HF_recycleProc_002 start");
1326     OHOS::AppSpawnModuleTest::AppSpawnTestCommander commander;
1327     AppSpawnReqMsgHandle reqHandle;
1328     AppSpawnResult result;
1329     commander.CreateMsg(reqHandle, defaultAppInfo1.c_str());
1330     int ret = AppSpawnClientSendMsg(commander.GetClientHandle(), reqHandle, &result);
1331     EXPECT_EQ(0, ret);
1332     EXPECT_EQ(0, result.result);
1333     EXPECT_NE(0, result.pid);
1334     GTEST_LOG_(INFO) << "newPid :" << result.pid << ".";
1335     if (result.pid > 0) {
1336         ret = kill(result.pid, SIGKILL);
1337         EXPECT_EQ(true, ret == 0 || (ret == -1 && errno == ESRCH));
1338     }
1339     // Check Process Is Destroyed
1340     EXPECT_EQ(true, CheckProcessIsDestroyed(result.pid));
1341     HILOG_INFO(LOG_CORE, "AppSpawn_HF_recycleProc_002 end");
1342 }
1343 
1344 HWTEST_F(AppSpawnModuleTest, AppSpawn_HF_recycleProc_003, TestSize.Level0)
1345 {
1346     HILOG_INFO(LOG_CORE, "AppSpawn_HF_recycleProc_003 start");
1347     OHOS::AppSpawnModuleTest::AppSpawnTestCommander commander;
1348     AppSpawnReqMsgHandle reqHandle;
1349     AppSpawnResult result;
1350     commander.CreateMsg(reqHandle, defaultAppInfo1.c_str());
1351     int ret = AppSpawnClientSendMsg(commander.GetClientHandle(), reqHandle, &result);
1352     EXPECT_EQ(0, ret);
1353     EXPECT_EQ(0, result.result);
1354     EXPECT_NE(0, result.pid);
1355     GTEST_LOG_(INFO) << "newPid :" << result.pid << ".";
1356     if (result.pid > 0) {
1357         ret = kill(result.pid, SIGKILL);
1358         EXPECT_EQ(true, ret == 0 || (ret == -1 && errno == ESRCH));
1359     }
1360     // Check Process Is Destroyed
1361     EXPECT_EQ(true, CheckProcessIsDestroyed(result.pid));
1362     HILOG_INFO(LOG_CORE, "AppSpawn_HF_recycleProc_003 end");
1363 }
1364 
1365 HWTEST_F(AppSpawnModuleTest, AppSpawn_HF_recycleProc_006, TestSize.Level0)
1366 {
1367     HILOG_INFO(LOG_CORE, "AppSpawn_HF_recycleProc_006 start");
1368     OHOS::AppSpawnModuleTest::AppSpawnTestCommander commander;
1369     AppSpawnReqMsgHandle reqHandle;
1370     AppSpawnResult result;
1371     commander.CreateMsg(reqHandle, defaultAppInfo1.c_str(), MSG_SPAWN_NATIVE_PROCESS);
1372     int ret = AppSpawnClientSendMsg(commander.GetClientHandle(), reqHandle, &result);
1373     EXPECT_EQ(0, ret);
1374     EXPECT_EQ(0, result.result);
1375     EXPECT_NE(0, result.pid);
1376     GTEST_LOG_(INFO) << "newPid :" << result.pid << ".";
1377     if (result.pid > 0) {
1378         ret = kill(result.pid, SIGKILL);
1379         EXPECT_EQ(true, ret == 0 || (ret == -1 && errno == ESRCH));
1380     }
1381 
1382     // Check Process Is Destroyed
1383     EXPECT_EQ(true, CheckProcessIsDestroyed(result.pid));
1384     HILOG_INFO(LOG_CORE, "AppSpawn_HF_recycleProc_006 end");
1385 }
1386 
1387 HWTEST_F(AppSpawnModuleTest, AppSpawn_HF_recycleProc_007, TestSize.Level0)
1388 {
1389     HILOG_INFO(LOG_CORE, "AppSpawn_HF_recycleProc_007 start");
1390     OHOS::AppSpawnModuleTest::AppSpawnTestCommander commander(0);
1391     AppSpawnReqMsgHandle reqHandle;
1392     AppSpawnResult result;
1393     commander.CreateMsg(reqHandle, defaultAppInfo1.c_str());
1394     int ret = AppSpawnClientSendMsg(commander.GetClientHandle(), reqHandle, &result);
1395     EXPECT_EQ(0, ret);
1396     EXPECT_EQ(0, result.result);
1397     EXPECT_NE(0, result.pid);
1398     GTEST_LOG_(INFO) << "newPid :" << result.pid << ".";
1399     if (result.pid > 0) {
1400         ret = kill(result.pid, SIGKILL);
1401         EXPECT_EQ(true, ret == 0 || (ret == -1 && errno == ESRCH));
1402     }
1403 
1404     // Check Process Is Destroyed
1405     EXPECT_EQ(true, CheckProcessIsDestroyed(result.pid));
1406     HILOG_INFO(LOG_CORE, "AppSpawn_HF_recycleProc_007 end");
1407 }
1408 
1409 HWTEST_F(AppSpawnModuleTest, AppSpawn_HF_recycleProc_008, TestSize.Level0)
1410 {
1411     HILOG_INFO(LOG_CORE, "AppSpawn_HF_recycleProc_008 start");
1412     OHOS::AppSpawnModuleTest::AppSpawnTestCommander commander(0);
1413     AppSpawnReqMsgHandle reqHandle;
1414     AppSpawnResult result;
1415     std::random_device rd;
1416     std::mt19937 gen(rd());
1417     std::uniform_int_distribution<> dis(0, 62);
1418     printf("number: %d\n", dis(gen));
1419     AppFlagsIndex number = static_cast<AppFlagsIndex>(dis(gen));
1420 
1421     commander.CreateMsg(reqHandle, defaultAppInfo1.c_str(), MSG_SPAWN_NATIVE_PROCESS);
1422     int ret = AppSpawnReqMsgSetAppFlag(reqHandle, number);
1423     ret = AppSpawnClientSendMsg(commander.GetClientHandle(), reqHandle, &result);
1424     EXPECT_EQ(0, ret);
1425     EXPECT_EQ(0, result.result);
1426     EXPECT_NE(0, result.pid);
1427     GTEST_LOG_(INFO) << "newPid :" << result.pid << ".";
1428     if (result.pid > 0) {
1429         ret = kill(result.pid, SIGKILL);
1430         EXPECT_EQ(true, ret == 0 || (ret == -1 && errno == ESRCH));
1431     }
1432     // Check Process Is Destroyed
1433     EXPECT_EQ(true, CheckProcessIsDestroyed(result.pid));
1434     HILOG_INFO(LOG_CORE, "AppSpawn_HF_recycleProc_008 end");
1435 }
1436 
1437 /**
1438  * @brief
1439  *  no internet permission
1440  *
1441  */
1442 HWTEST_F(AppSpawnModuleTest, AppSpawn_Invalid_Msg_001, TestSize.Level0)
1443 {
1444     HILOG_INFO(LOG_CORE, "AppSpawn_Invalid_Msg_001 start");
1445     OHOS::AppSpawnModuleTest::AppSpawnTestCommander commander;
1446     AppSpawnReqMsgHandle reqHandle;
1447     AppSpawnResult result;
1448     commander.CreateMsg(reqHandle, defaultAppInfoNoInternetPermission.c_str());
1449     int ret = AppSpawnClientSendMsg(commander.GetClientHandle(), reqHandle, &result);
1450     EXPECT_EQ(0, ret);
1451     EXPECT_EQ(0, result.result);
1452     if (result.pid > 0) {
1453         ret = kill(result.pid, SIGKILL);
1454         EXPECT_EQ(true, ret == 0 || (ret == -1 && errno == ESRCH));
1455     }
1456     HILOG_INFO(LOG_CORE, "AppSpawn_Invalid_Msg_001 end");
1457 }
1458 
1459 /**
1460  * @brief
1461  *  no dac
1462  *
1463  */
1464 HWTEST_F(AppSpawnModuleTest, AppSpawn_Invalid_Msg_002, TestSize.Level0)
1465 {
1466     HILOG_INFO(LOG_CORE, "AppSpawn_Invalid_Msg_001 start");
1467     OHOS::AppSpawnModuleTest::AppSpawnTestCommander commander;
1468     AppSpawnReqMsgHandle reqHandle;
1469     AppSpawnResult result;
1470     commander.CreateMsg(reqHandle, defaultAppInfoNoDac.c_str());
1471     int ret = AppSpawnClientSendMsg(commander.GetClientHandle(), reqHandle, &result);
1472     EXPECT_EQ(0, ret);
1473     EXPECT_EQ(APPSPAWN_MSG_INVALID, result.result);
1474     HILOG_INFO(LOG_CORE, "AppSpawn_Invalid_Msg_001 end");
1475 }
1476 
1477 /**
1478  * @brief
1479  *  no access token
1480  *
1481  */
1482 HWTEST_F(AppSpawnModuleTest, AppSpawn_Invalid_Msg_003, TestSize.Level0)
1483 {
1484     HILOG_INFO(LOG_CORE, "AppSpawn_Invalid_Msg_003 start");
1485 
1486     OHOS::AppSpawnModuleTest::AppSpawnTestCommander commander;
1487     AppSpawnReqMsgHandle reqHandle;
1488     AppSpawnResult result;
1489     commander.CreateMsg(reqHandle, defaultAppInfoNoToken.c_str());
1490     int ret = AppSpawnClientSendMsg(commander.GetClientHandle(), reqHandle, &result);
1491     EXPECT_EQ(0, ret);
1492     EXPECT_EQ(APPSPAWN_MSG_INVALID, result.result);
1493 
1494     HILOG_INFO(LOG_CORE, "AppSpawn_Invalid_Msg_003 end");
1495 }
1496 
1497 /**
1498  * @brief
1499  *  no bundle info
1500  *
1501  */
1502 HWTEST_F(AppSpawnModuleTest, AppSpawn_Invalid_Msg_004, TestSize.Level0)
1503 {
1504     HILOG_INFO(LOG_CORE, "AppSpawn_Invalid_Msg_004 start");
1505 
1506     OHOS::AppSpawnModuleTest::AppSpawnTestCommander commander;
1507     AppSpawnReqMsgHandle reqHandle;
1508     AppSpawnResult result;
1509     commander.CreateMsg(reqHandle, defaultAppInfoNoBundleInfo.c_str());
1510     int ret = AppSpawnClientSendMsg(commander.GetClientHandle(), reqHandle, &result);
1511     EXPECT_EQ(0, ret);
1512     EXPECT_EQ(APPSPAWN_MSG_INVALID, result.result);
1513     HILOG_INFO(LOG_CORE, "AppSpawn_Invalid_Msg_004 end");
1514 }
1515 
1516 /**
1517  * @brief
1518  *  no internet permission
1519  *
1520  */
1521 HWTEST_F(AppSpawnModuleTest, AppSpawn_Invalid_Msg_005, TestSize.Level0)
1522 {
1523     HILOG_INFO(LOG_CORE, "AppSpawn_Invalid_Msg_005 start");
1524     OHOS::AppSpawnModuleTest::AppSpawnTestCommander commander(0);  // nwebspawn
1525     AppSpawnReqMsgHandle reqHandle;
1526     AppSpawnResult result;
1527     commander.CreateMsg(reqHandle, defaultAppInfoNoInternetPermission.c_str());
1528     int ret = AppSpawnClientSendMsg(commander.GetClientHandle(), reqHandle, &result);
1529     EXPECT_EQ(0, ret);
1530     EXPECT_EQ(0, result.result);
1531     if (result.pid > 0) {
1532         ret = kill(result.pid, SIGKILL);
1533         EXPECT_EQ(true, ret == 0 || (ret == -1 && errno == ESRCH));
1534     }
1535     HILOG_INFO(LOG_CORE, "AppSpawn_Invalid_Msg_005 end");
1536 }
1537 
1538 /**
1539  * @brief
1540  *  no dac
1541  *
1542  */
1543 HWTEST_F(AppSpawnModuleTest, AppSpawn_Invalid_Msg_006, TestSize.Level0)
1544 {
1545     HILOG_INFO(LOG_CORE, "AppSpawn_Invalid_Msg_006 start");
1546     OHOS::AppSpawnModuleTest::AppSpawnTestCommander commander(0);  // nwebspawn
1547     AppSpawnReqMsgHandle reqHandle;
1548     AppSpawnResult result;
1549     commander.CreateMsg(reqHandle, defaultAppInfoNoDac.c_str());
1550     int ret = AppSpawnClientSendMsg(commander.GetClientHandle(), reqHandle, &result);
1551     EXPECT_EQ(0, ret);
1552     EXPECT_EQ(APPSPAWN_MSG_INVALID, result.result);
1553     HILOG_INFO(LOG_CORE, "AppSpawn_Invalid_Msg_006 end");
1554 }
1555 
1556 /**
1557  * @brief
1558  *  no access token
1559  *
1560  */
1561 HWTEST_F(AppSpawnModuleTest, AppSpawn_Invalid_Msg_007, TestSize.Level0)
1562 {
1563     HILOG_INFO(LOG_CORE, "AppSpawn_Invalid_Msg_007 start");
1564     OHOS::AppSpawnModuleTest::AppSpawnTestCommander commander(0);  // nwebspawn
1565     AppSpawnReqMsgHandle reqHandle;
1566     AppSpawnResult result;
1567     commander.CreateMsg(reqHandle, defaultAppInfoNoToken.c_str());
1568     int ret = AppSpawnClientSendMsg(commander.GetClientHandle(), reqHandle, &result);
1569     EXPECT_EQ(0, ret);
1570     EXPECT_EQ(APPSPAWN_MSG_INVALID, result.result);
1571 
1572     HILOG_INFO(LOG_CORE, "AppSpawn_Invalid_Msg_007 end");
1573 }
1574 
1575 /**
1576  * @brief
1577  *  no bundle info
1578  *
1579  */
1580 HWTEST_F(AppSpawnModuleTest, AppSpawn_Invalid_Msg_008, TestSize.Level0)
1581 {
1582     HILOG_INFO(LOG_CORE, "AppSpawn_Invalid_Msg_008 start");
1583     OHOS::AppSpawnModuleTest::AppSpawnTestCommander commander(0);  // nwebspawn
1584     AppSpawnReqMsgHandle reqHandle;
1585     AppSpawnResult result;
1586     commander.CreateMsg(reqHandle, defaultAppInfoNoBundleInfo.c_str());
1587     int ret = AppSpawnClientSendMsg(commander.GetClientHandle(), reqHandle, &result);
1588     EXPECT_EQ(0, ret);
1589     EXPECT_EQ(APPSPAWN_MSG_INVALID, result.result);
1590     HILOG_INFO(LOG_CORE, "AppSpawn_Invalid_Msg_008 end");
1591 }
1592 
1593 /**
1594  * @brief
1595  *  no internet permission
1596  *
1597  */
1598 HWTEST_F(AppSpawnModuleTest, AppSpawn_Invalid_Msg_009, TestSize.Level0)
1599 {
1600     HILOG_INFO(LOG_CORE, "AppSpawn_Invalid_Msg_009 start");
1601     OHOS::AppSpawnModuleTest::AppSpawnTestCommander commander;
1602     AppSpawnReqMsgHandle reqHandle;
1603     AppSpawnResult result;
1604     commander.CreateMsg(reqHandle, defaultAppInfoNoInternetPermission.c_str(), MSG_SPAWN_NATIVE_PROCESS);
1605     int ret = AppSpawnClientSendMsg(commander.GetClientHandle(), reqHandle, &result);
1606     EXPECT_EQ(0, ret);
1607     EXPECT_EQ(0, result.result);
1608     if (result.pid > 0) {
1609         ret = kill(result.pid, SIGKILL);
1610         EXPECT_EQ(true, ret == 0 || (ret == -1 && errno == ESRCH));
1611     }
1612     HILOG_INFO(LOG_CORE, "AppSpawn_Invalid_Msg_009 end");
1613 }
1614 
1615 /**
1616  * @brief
1617  *  no dac
1618  *
1619  */
1620 HWTEST_F(AppSpawnModuleTest, AppSpawn_Invalid_Msg_010, TestSize.Level0)
1621 {
1622     HILOG_INFO(LOG_CORE, "AppSpawn_Invalid_Msg_010 start");
1623     OHOS::AppSpawnModuleTest::AppSpawnTestCommander commander;
1624     AppSpawnReqMsgHandle reqHandle;
1625     AppSpawnResult result;
1626     commander.CreateMsg(reqHandle, defaultAppInfoNoDac.c_str(), MSG_SPAWN_NATIVE_PROCESS);
1627     int ret = AppSpawnClientSendMsg(commander.GetClientHandle(), reqHandle, &result);
1628     EXPECT_EQ(0, ret);
1629     EXPECT_EQ(APPSPAWN_MSG_INVALID, result.result);
1630     HILOG_INFO(LOG_CORE, "AppSpawn_Invalid_Msg_010 end");
1631 }
1632 
1633 /**
1634  * @brief
1635  *  no access token
1636  *
1637  */
1638 HWTEST_F(AppSpawnModuleTest, AppSpawn_Invalid_Msg_011, TestSize.Level0)
1639 {
1640     HILOG_INFO(LOG_CORE, "AppSpawn_Invalid_Msg_011 start");
1641 
1642     OHOS::AppSpawnModuleTest::AppSpawnTestCommander commander;
1643     AppSpawnReqMsgHandle reqHandle;
1644     AppSpawnResult result;
1645     commander.CreateMsg(reqHandle, defaultAppInfoNoToken.c_str(), MSG_SPAWN_NATIVE_PROCESS);
1646     int ret = AppSpawnClientSendMsg(commander.GetClientHandle(), reqHandle, &result);
1647     EXPECT_EQ(0, ret);
1648     EXPECT_EQ(APPSPAWN_MSG_INVALID, result.result);
1649 
1650     HILOG_INFO(LOG_CORE, "AppSpawn_Invalid_Msg_011 end");
1651 }
1652 
1653 /**
1654  * @brief
1655  *  no bundle info
1656  *
1657  */
1658 HWTEST_F(AppSpawnModuleTest, AppSpawn_Invalid_Msg_012, TestSize.Level0)
1659 {
1660     HILOG_INFO(LOG_CORE, "AppSpawn_Invalid_Msg_012 start");
1661 
1662     OHOS::AppSpawnModuleTest::AppSpawnTestCommander commander;
1663     AppSpawnReqMsgHandle reqHandle;
1664     AppSpawnResult result;
1665     commander.CreateMsg(reqHandle, defaultAppInfoNoBundleInfo.c_str(), MSG_SPAWN_NATIVE_PROCESS);
1666     int ret = AppSpawnClientSendMsg(commander.GetClientHandle(), reqHandle, &result);
1667     EXPECT_EQ(0, ret);
1668     EXPECT_EQ(APPSPAWN_MSG_INVALID, result.result);
1669     HILOG_INFO(LOG_CORE, "AppSpawn_Invalid_Msg_012 end");
1670 }
1671 
1672 /**
1673  * @brief app flags
1674  *
1675  */
1676 HWTEST_F(AppSpawnModuleTest, AppSpawn_App_Flags_001, TestSize.Level0)
1677 {
1678     HILOG_INFO(LOG_CORE, "AppSpawn_App_Flags_001 start");
1679 
1680     OHOS::AppSpawnModuleTest::AppSpawnTestCommander commander;
1681     AppSpawnReqMsgHandle reqHandle;
1682     AppSpawnResult result;
1683     std::random_device rd;
1684     std::mt19937 gen(rd());
1685     std::uniform_int_distribution<> dis(0, 62);
1686     printf("number: %d\n", dis(gen));
1687     AppFlagsIndex number = static_cast<AppFlagsIndex>(dis(gen));
1688     commander.CreateMsg(reqHandle, defaultAppInfo1.c_str());
1689     APPSPAWN_LOGI("number: %{public}d", number);
1690     int ret = AppSpawnReqMsgSetAppFlag(reqHandle, number);
1691     ret = AppSpawnClientSendMsg(commander.GetClientHandle(), reqHandle, &result);
1692     EXPECT_EQ(0, ret);
1693     EXPECT_EQ(0, result.result);
1694     if (result.pid > 0) {
1695         ret = kill(result.pid, SIGKILL);
1696         EXPECT_EQ(true, ret == 0 || (ret == -1 && errno == ESRCH));
1697         result.pid = DEFAULT_PID;
1698     }
1699     HILOG_INFO(LOG_CORE, "AppSpawn_App_Flags_001 end");
1700 }
1701 
1702 /**
1703  * @brief app flags
1704  *
1705  */
1706 HWTEST_F(AppSpawnModuleTest, AppSpawn_App_Flags_002, TestSize.Level0)
1707 {
1708     HILOG_INFO(LOG_CORE, "AppSpawn_App_Flags_002 start");
1709 
1710     OHOS::AppSpawnModuleTest::AppSpawnTestCommander commander;
1711     AppSpawnReqMsgHandle reqHandle;
1712     AppSpawnResult result;
1713     std::random_device rd;
1714     std::mt19937 gen(rd());
1715     std::uniform_int_distribution<> dis(0, 62);
1716     printf("number: %d\n", dis(gen));
1717     AppFlagsIndex number = static_cast<AppFlagsIndex>(dis(gen));
1718     commander.CreateMsg(reqHandle, defaultAppInfo1.c_str(), MSG_SPAWN_NATIVE_PROCESS);
1719     APPSPAWN_LOGI("number: %{public}d", number);
1720     int ret = AppSpawnReqMsgSetAppFlag(reqHandle, number);
1721     ret = AppSpawnClientSendMsg(commander.GetClientHandle(), reqHandle, &result);
1722     EXPECT_EQ(0, ret);
1723     EXPECT_EQ(0, result.result);
1724     if (result.pid > 0) {
1725         ret = kill(result.pid, SIGKILL);
1726         EXPECT_EQ(true, ret == 0 || (ret == -1 && errno == ESRCH));
1727         result.pid = DEFAULT_PID;
1728     }
1729     HILOG_INFO(LOG_CORE, "AppSpawn_App_Flags_002 end");
1730 }
1731 
1732 /**
1733  * @brief app flags
1734  *
1735  */
1736 HWTEST_F(AppSpawnModuleTest, AppSpawn_App_Flags_003, TestSize.Level0)
1737 {
1738     HILOG_INFO(LOG_CORE, "AppSpawn_App_Flags_003 start");
1739     OHOS::AppSpawnModuleTest::AppSpawnTestCommander commander(0);  // nwebspawn
1740     AppSpawnReqMsgHandle reqHandle;
1741     AppSpawnResult result;
1742     std::random_device rd;
1743     std::mt19937 gen(rd());
1744     std::uniform_int_distribution<> dis(0, 62);
1745     printf("number: %d\n", dis(gen));
1746     AppFlagsIndex number = static_cast<AppFlagsIndex>(dis(gen));
1747     commander.CreateMsg(reqHandle, defaultAppInfo1.c_str());
1748     APPSPAWN_LOGI("number: %{public}d", number);
1749     int ret = AppSpawnReqMsgSetAppFlag(reqHandle, number);
1750     ret = AppSpawnClientSendMsg(commander.GetClientHandle(), reqHandle, &result);
1751     EXPECT_EQ(0, ret);
1752     EXPECT_EQ(0, result.result);
1753     if (result.pid > 0) {
1754         ret = kill(result.pid, SIGKILL);
1755         EXPECT_EQ(true, ret == 0 || (ret == -1 && errno == ESRCH));
1756         result.pid = DEFAULT_PID;
1757     }
1758     HILOG_INFO(LOG_CORE, "AppSpawn_App_Flags_003 end");
1759 }
1760 
1761 /**
1762  * @brief app flags
1763  *
1764  */
1765 HWTEST_F(AppSpawnModuleTest, AppSpawn_App_Flags_004, TestSize.Level0)
1766 {
1767     HILOG_INFO(LOG_CORE, "AppSpawn_App_Flags_004 start");
1768     OHOS::AppSpawnModuleTest::AppSpawnTestCommander commander(0);  // nwebspawn
1769     AppSpawnReqMsgHandle reqHandle;
1770     AppSpawnResult result;
1771     std::random_device rd;
1772     std::mt19937 gen(rd());
1773     std::uniform_int_distribution<> dis(0, 62);
1774     printf("number: %d\n", dis(gen));
1775     AppFlagsIndex number = static_cast<AppFlagsIndex>(dis(gen));
1776     commander.CreateMsg(reqHandle, defaultAppInfo1.c_str(), MSG_SPAWN_NATIVE_PROCESS);
1777     APPSPAWN_LOGI("number: %{public}d", number);
1778     int ret = AppSpawnReqMsgSetAppFlag(reqHandle, number);
1779     ret = AppSpawnClientSendMsg(commander.GetClientHandle(), reqHandle, &result);
1780     EXPECT_EQ(0, ret);
1781     EXPECT_EQ(0, result.result);
1782     if (result.pid > 0) {
1783         ret = kill(result.pid, SIGKILL);
1784         EXPECT_EQ(true, ret == 0 || (ret == -1 && errno == ESRCH));
1785         result.pid = DEFAULT_PID;
1786     }
1787     HILOG_INFO(LOG_CORE, "AppSpawn_App_Flags_004 end");
1788 }
1789 
1790 HWTEST_F(AppSpawnModuleTest, AppSpawn_Msg_001, TestSize.Level0)
1791 {
1792     HILOG_INFO(LOG_CORE, "AppSpawn_Msg_001 start");
1793 
1794     static const std::string dumpInfo = "{ \
1795         \"msg-type\": \"MSG_DUMP\", \
1796         \"msg-flags\": [ 13 ], \
1797         \"process-name\" : \"com.example.myapplication\" \
1798     }";
1799 
1800     OHOS::AppSpawnModuleTest::AppSpawnTestCommander commander;
1801     AppSpawnReqMsgHandle reqHandle;
1802     AppSpawnResult result;
1803     commander.CreateMsg(reqHandle, dumpInfo.c_str());
1804     int ret = AppSpawnClientSendMsg(commander.GetClientHandle(), reqHandle, &result);
1805     EXPECT_EQ(0, ret);
1806     HILOG_INFO(LOG_CORE, "AppSpawn_Msg_001 end");
1807 }
1808 
1809 HWTEST_F(AppSpawnModuleTest, AppSpawn_Msg_002, TestSize.Level0)
1810 {
1811     HILOG_INFO(LOG_CORE, "AppSpawn_Msg_002 start");
1812     static const std::string appInfo = "{ \
1813         \"msg-type\": \"MSG_GET_RENDER_TERMINATION_STATUS\", \
1814         \"msg-flags\": [ 13 ], \
1815         \"process-name\" : \"com.example.myapplication\" \
1816     }";
1817 
1818     OHOS::AppSpawnModuleTest::AppSpawnTestCommander commander;
1819     AppSpawnReqMsgHandle reqHandle;
1820     AppSpawnResult result;
1821     commander.CreateMsg(reqHandle, defaultAppInfo1.c_str());
1822     int ret = AppSpawnClientSendMsg(commander.GetClientHandle(), reqHandle, &result);
1823     EXPECT_EQ(0, ret);
1824     EXPECT_EQ(0, result.result);
1825     if (result.pid > 0) {
1826         ret = kill(result.pid, SIGKILL);
1827         EXPECT_EQ(true, ret == 0 || (ret == -1 && errno == ESRCH));
1828         result.pid = DEFAULT_PID;
1829     }
1830     commander.CreateMsg(reqHandle, appInfo.c_str(), MSG_GET_RENDER_TERMINATION_STATUS);
1831     ret = AppSpawnClientSendMsg(commander.GetClientHandle(), reqHandle, &result);
1832     EXPECT_EQ(0, ret);
1833     EXPECT_NE(0, result.result);
1834     HILOG_INFO(LOG_CORE, "AppSpawn_Msg_002 end");
1835 }
1836 
1837 HWTEST_F(AppSpawnModuleTest, AppSpawn_Msg_003, TestSize.Level0)
1838 {
1839     HILOG_INFO(LOG_CORE, "AppSpawn_Msg_003 start");
1840     static const std::string appInfo = "{ \
1841         \"msg-type\": \"MSG_GET_RENDER_TERMINATION_STATUS\", \
1842         \"msg-flags\": [ 13 ], \
1843         \"process-name\" : \"com.example.myapplication\" \
1844     }";
1845 
1846     OHOS::AppSpawnModuleTest::AppSpawnTestCommander commander(0);  // nwebspawn
1847     AppSpawnReqMsgHandle reqHandle;
1848     AppSpawnResult result;
1849     commander.CreateMsg(reqHandle, defaultAppInfo1.c_str());
1850     int ret = AppSpawnClientSendMsg(commander.GetClientHandle(), reqHandle, &result);
1851     EXPECT_EQ(0, ret);
1852     EXPECT_EQ(0, result.result);
1853     if (result.pid > 0) {
1854         ret = kill(result.pid, SIGKILL);
1855         EXPECT_EQ(true, ret == 0 || (ret == -1 && errno == ESRCH));
1856         result.pid = DEFAULT_PID;
1857     }
1858     commander.CreateMsg(reqHandle, appInfo.c_str(), MSG_GET_RENDER_TERMINATION_STATUS);
1859     ret = AppSpawnClientSendMsg(commander.GetClientHandle(), reqHandle, &result);
1860     EXPECT_EQ(0, ret);
1861     EXPECT_EQ(0, result.result);
1862     HILOG_INFO(LOG_CORE, "AppSpawn_Msg_003 end");
1863 }
1864 
1865 HWTEST_F(AppSpawnModuleTest, AppSpawn_Msg_004, TestSize.Level0)
1866 {
1867     HILOG_INFO(LOG_CORE, "AppSpawn_Msg_004 start");
1868     static const std::string appInfo = "{ \
1869         \"msg-type\": \"MSG_GET_RENDER_TERMINATION_STATUS\", \
1870         \"msg-flags\": [ 13 ], \
1871         \"process-name\" : \"com.example.myapplication\" \
1872     }";
1873 
1874     OHOS::AppSpawnModuleTest::AppSpawnTestCommander commander(0);  // nwebspawn
1875     AppSpawnReqMsgHandle reqHandle;
1876     AppSpawnResult result;
1877     commander.CreateMsg(reqHandle, defaultAppInfo1.c_str());
1878     int ret = AppSpawnClientSendMsg(commander.GetClientHandle(), reqHandle, &result);
1879     EXPECT_EQ(0, ret);
1880     EXPECT_EQ(0, result.result);
1881     commander.CreateMsg(reqHandle, appInfo.c_str(), MSG_GET_RENDER_TERMINATION_STATUS);
1882     ret = AppSpawnClientSendMsg(commander.GetClientHandle(), reqHandle, &result);
1883     EXPECT_EQ(0, ret);
1884     EXPECT_EQ(0, result.result);
1885     if (result.pid > 0) {
1886         ret = kill(result.pid, SIGKILL);
1887         EXPECT_EQ(true, ret == 0 || (ret == -1 && errno == ESRCH));
1888         result.pid = DEFAULT_PID;
1889     }
1890     HILOG_INFO(LOG_CORE, "AppSpawn_Msg_004 end");
1891 }
1892 }  // namespace AppSpawn
1893 }  // namespace OHOS
1894