• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include <fcntl.h>
17 #include <unistd.h>
18 
19 #include <cstdio>
20 
21 #include "gtest/gtest.h"
22 #include "app_spawn_client.h"
23 #include "hilog/log.h"
24 #include "securec.h"
25 
26 using namespace testing::ext;
27 using namespace OHOS;
28 using namespace OHOS::AppExecFwk;
29 using namespace OHOS::HiviewDFX;
30 
31 namespace OHOS {
32 namespace AppSpawn {
33 static constexpr HiLogLabel LABEL = {LOG_CORE, 0, "AppSpawnMST"};
34 
35 namespace {
36 const bool CHECK_OK = true;
37 const bool CHECK_ERROR = false;
38 const int32_t DEFAULT_PID = 0;
39 const int32_t FILE_PATH_SIZE = 50;
40 const int32_t CMD_SIZE = 50;
41 const int32_t BUFFER_SIZE = 512;
42 const int32_t BASE_TYPE = 10;
43 const int32_t CONNECT_RETRY_DELAY = 50 * 1000;
44 const int32_t CONNECT_RETRY_MAX_TIMES = 5;
45 const int32_t UID_POSITION_MOVE = 5;
46 const int32_t GID_POSITION_MOVE = 5;
47 const int32_t GROUPS_POSITION_MOVE = 8;
48 const char *DELIMITER_SPACE = " ";
49 const char *DELIMITER_NEWLINE = "\n";
50 
51 char buffer[BUFFER_SIZE] = {"\0"};
52 int32_t newPid = 0;
53 int32_t retryCount = 0;
54 }  // namespace
55 
checkFileIsExists(const char * filepath)56 bool checkFileIsExists(const char *filepath)
57 {
58     retryCount = 0;
59     while ((access(filepath, F_OK) != 0) && (retryCount < CONNECT_RETRY_MAX_TIMES)) {
60         usleep(CONNECT_RETRY_DELAY);
61         retryCount++;
62     }
63     GTEST_LOG_(INFO) << "retryCount :" << retryCount << ".";
64     if (retryCount < CONNECT_RETRY_MAX_TIMES) {
65         return CHECK_OK;
66     }
67     return CHECK_ERROR;
68 }
69 
readFileInfo(char * buffer,const int32_t & pid,const char * fileName)70 bool readFileInfo(char *buffer, const int32_t &pid, const char *fileName)
71 {
72     // Set file path
73     char filePath[FILE_PATH_SIZE];
74     if (sprintf_s(filePath, sizeof(filePath), "/proc/%d/%s", pid, fileName) <= 0) {
75         HiLog::Error(LABEL, "filePath sprintf_s fail .");
76         return CHECK_ERROR;
77     }
78     if (!checkFileIsExists(filePath)) {
79         HiLog::Error(LABEL, "file %{public}s is not exists .", fileName);
80         return CHECK_ERROR;
81     }
82     // Open file
83     int fd = open(filePath, O_RDONLY);
84     if (fd == -1) {
85         HiLog::Error(LABEL, "file %{public}s open failed . error:%{publid}s", fileName, strerror(errno));
86         return CHECK_ERROR;
87     }
88     // Read file
89     int t = read(fd, buffer, BUFFER_SIZE);
90     if (t <= 0 || buffer == nullptr) {
91         HiLog::Info(LABEL, "read proc status file failed.");
92         close(fd);
93         return CHECK_ERROR;
94     }
95     HiLog::Info(LABEL, "buffer:\n %{public}s", buffer);
96     close(fd);
97 
98     return CHECK_OK;
99 }
100 
checkUid(const int32_t & pid,const AppSpawnStartMsg & params)101 bool checkUid(const int32_t &pid, const AppSpawnStartMsg &params)
102 {
103     if (readFileInfo(buffer, pid, "status")) {
104         // Move to Uid position
105         char *uidPtr = strstr(buffer, "Uid:");
106         if (uidPtr == nullptr) {
107             HiLog::Error(LABEL, "get Uid info failed.");
108             return CHECK_ERROR;
109         }
110         if (strlen(uidPtr) > UID_POSITION_MOVE) {
111             uidPtr = uidPtr + UID_POSITION_MOVE;
112         }
113         int32_t uid = (int32_t)strtol(uidPtr, NULL, BASE_TYPE);
114         HiLog::Info(LABEL, "new proc(%{public}d) uid = %{public}d, setUid=%{public}d.", pid, uid, params.uid);
115         if (uid == params.uid) {
116             return CHECK_OK;
117         }
118     }
119     return CHECK_ERROR;
120 }
121 
checkGid(const int32_t & pid,const AppSpawnStartMsg & params)122 bool checkGid(const int32_t &pid, const AppSpawnStartMsg &params)
123 {
124     if (readFileInfo(buffer, pid, "status")) {
125         // Move to Gid position
126         char *gidPtr = strstr(buffer, "Gid:");
127         if (gidPtr == nullptr) {
128             HiLog::Error(LABEL, "get Gid info failed.");
129             return CHECK_ERROR;
130         }
131         if (strlen(gidPtr) > GID_POSITION_MOVE) {
132             gidPtr = gidPtr + GID_POSITION_MOVE;
133         }
134         if (gidPtr == nullptr) {
135             HiLog::Error(LABEL, "get Gid info failed.");
136             return CHECK_ERROR;
137         }
138         int32_t gid = (int32_t)strtol(gidPtr, NULL, BASE_TYPE);
139         HiLog::Info(LABEL, "new proc(%{public}d) gid = %{public}d, setGid=%{public}d.", pid, gid, params.gid);
140         if (gid == params.gid) {
141             return CHECK_OK;
142         }
143     }
144     return CHECK_ERROR;
145 }
getGids(const int32_t & pid,std::vector<int32_t> & gids)146 std::size_t getGids(const int32_t &pid, std::vector<int32_t> &gids)
147 {
148     if (readFileInfo(buffer, pid, "status")) {
149         // Move to Groups position
150         char *groupsPtr = strstr(buffer, "Groups");
151         if (groupsPtr == nullptr || strlen(groupsPtr) > BUFFER_SIZE) {
152             HiLog::Error(LABEL, "get Groups info failed.");
153             return CHECK_ERROR;
154         }
155         if (strlen(groupsPtr) > GROUPS_POSITION_MOVE) {
156             groupsPtr = groupsPtr + GROUPS_POSITION_MOVE;
157         }
158         // Get the row content of Groups
159         char *saveptr = nullptr;
160         if (groupsPtr == nullptr || strlen(groupsPtr) > BUFFER_SIZE) {
161             HiLog::Error(LABEL, "get Groups info failed.");
162             return CHECK_ERROR;
163         }
164         char *line = strtok_r(groupsPtr, DELIMITER_NEWLINE, &saveptr);
165         if (line == nullptr || strlen(line) > BUFFER_SIZE) {
166             HiLog::Error(LABEL, "get Groups line info failed.");
167             return CHECK_ERROR;
168         }
169         // Get each gid and insert into vector
170         char *gid = strtok_r(line, DELIMITER_SPACE, &saveptr);
171         while (gid != nullptr) {
172             gids.push_back(atoi(gid));
173             gid = strtok_r(nullptr, DELIMITER_SPACE, &saveptr);
174         }
175     }
176 
177     return gids.size();
178 }
179 
checkGids(const int32_t & pid,const AppSpawnStartMsg & params)180 bool checkGids(const int32_t &pid, const AppSpawnStartMsg &params)
181 {
182     // Get Gids
183     std::vector<int32_t> gids;
184     std::size_t gCount = getGids(pid, gids);
185     if ((gCount == params.gids.size()) && (gids == params.gids)) {
186         return CHECK_OK;
187     }
188 
189     return CHECK_ERROR;
190 }
191 
checkGidsCount(const int32_t & pid,const AppSpawnStartMsg & params)192 bool checkGidsCount(const int32_t &pid, const AppSpawnStartMsg &params)
193 {
194     // Get GidsCount
195     std::vector<int32_t> gids;
196     std::size_t gCount = getGids(pid, gids);
197     if (gCount == params.gids.size()) {
198         return CHECK_OK;
199     }
200 
201     return CHECK_ERROR;
202 }
203 
checkProcName(const int32_t & pid,const AppSpawnStartMsg & params)204 bool checkProcName(const int32_t &pid, const AppSpawnStartMsg &params)
205 {
206     FILE *fp = nullptr;
207     char cmd[CMD_SIZE];
208     if (sprintf_s(cmd, sizeof(cmd), "ps -o ARGS=CMD -p %d |grep -v CMD", pid) <= 0) {
209         HiLog::Error(LABEL, "cmd sprintf_s fail .");
210         return CHECK_ERROR;
211     }
212     if(strlen(cmd) > CMD_SIZE) {
213         HiLog::Error(LABEL, " cmd length is too long  .");
214         return CHECK_ERROR;
215     }
216     fp = popen(cmd, "r");
217     if (fp == nullptr) {
218         HiLog::Error(LABEL, " popen function call failed .");
219         return CHECK_ERROR;
220     }
221     char procName[BUFFER_SIZE];
222     if (fgets(procName, sizeof(procName), fp) != nullptr) {
223         for (unsigned int i = 0; i < sizeof(procName); i++) {
224             if (procName[i] == '\n') {
225                 procName[i] = '\0';
226                 break;
227             }
228         }
229         GTEST_LOG_(INFO) << "strcmp"
230                          << " :" << strcmp(params.procName.c_str(), procName) << ".";
231 
232         if (params.procName.compare(0, params.procName.size(), procName, params.procName.size()) == 0) {
233             pclose(fp);
234             return CHECK_OK;
235         }
236         HiLog::Error(LABEL, " procName=%{public}s, params.procName=%{public}s.", procName, params.procName.c_str());
237     } else {
238         HiLog::Error(LABEL, "Getting procName failed.");
239     }
240     pclose(fp);
241 
242     return CHECK_ERROR;
243 }
checkProcessIsDestroyed(const int32_t & pid)244 bool checkProcessIsDestroyed(const int32_t &pid)
245 {
246     char filePath[FILE_PATH_SIZE];
247     if (sprintf_s(filePath, sizeof(filePath), "/proc/%d", pid) <= 0) {
248         HiLog::Error(LABEL, "filePath sprintf_s fail .");
249         return CHECK_ERROR;
250     }
251 
252     if (checkFileIsExists(filePath)) {
253         HiLog::Error(LABEL, "File %{public}d is not exists .", pid);
254         return CHECK_ERROR;
255     }
256 
257     return CHECK_OK;
258 }
259 
checkAppspawnPID()260 bool checkAppspawnPID()
261 {
262     FILE *fp = nullptr;
263     fp = popen("pidof appspawn", "r");
264     if (fp == nullptr) {
265         HiLog::Error(LABEL, " popen function call failed.");
266         return CHECK_ERROR;
267     }
268     char pid[BUFFER_SIZE];
269     if (fgets(pid, sizeof(pid), fp) != nullptr) {
270         pclose(fp);
271         return CHECK_OK;
272     }
273 
274     HiLog::Error(LABEL, "Getting Pid failed.");
275 
276     pclose(fp);
277 
278     return CHECK_ERROR;
279 }
280 
startAppspawn()281 bool startAppspawn()
282 {
283     FILE *fp = nullptr;
284     fp = popen("/system/bin/appspawn&", "r");
285     if (fp == nullptr) {
286         HiLog::Error(LABEL, " popen function call failed.");
287         return CHECK_ERROR;
288     }
289 
290     pclose(fp);
291 
292     return CHECK_OK;
293 }
294 
stopAppspawn()295 bool stopAppspawn()
296 {
297     FILE *fp = nullptr;
298     fp = popen("kill -9 $(pidof appspawn)", "r");
299     if (fp == nullptr) {
300         HiLog::Error(LABEL, " popen function call failed.");
301         return CHECK_ERROR;
302     }
303 
304     pclose(fp);
305 
306     return CHECK_OK;
307 }
308 
309 class AppSpawnModuleTest : public testing::Test {
310 public:
311     static void SetUpTestCase();
312     static void TearDownTestCase();
313     void SetUp();
314     void TearDown();
315 };
316 
SetUpTestCase()317 void AppSpawnModuleTest::SetUpTestCase()
318 {
319     if (!checkAppspawnPID()) {
320         EXPECT_EQ(startAppspawn(), CHECK_OK);
321     }
322 }
323 
TearDownTestCase()324 void AppSpawnModuleTest::TearDownTestCase()
325 {
326     if (checkAppspawnPID()) {
327         EXPECT_EQ(stopAppspawn(), CHECK_OK);
328     }
329 }
330 
SetUp()331 void AppSpawnModuleTest::SetUp()
332 {
333     newPid = 0;
334     auto ret = memset_s(buffer, sizeof(buffer), 0x00, BUFFER_SIZE);
335     if (ret != EOK) {
336         HiLog::Error(LABEL, "memset_s is failed.");
337     }
338 }
339 
TearDown()340 void AppSpawnModuleTest::TearDown()
341 {}
342 
343 /*
344  * Feature: AppSpawn
345  * Function: Listen
346  * SubFunction: Message listener
347  * FunctionPoints: Process start message monitoring
348  * EnvConditions: AppSpawn main process has started.
349  *                 The socket server has been established.
350  * CaseDescription: 1. Query the process of appspawn through the ps command
351  */
352 HWTEST_F(AppSpawnModuleTest, AppSpawn_HF_listen_001, TestSize.Level0)
353 {
354     HiLog::Info(LABEL, "AppSpawn_HF_listen_001 start");
355 
356     EXPECT_EQ(CHECK_OK, checkAppspawnPID());
357 
358     HiLog::Info(LABEL, "AppSpawn_HF_listen_001 end");
359 }
360 
361 /*
362  * Feature: AppSpawn
363  * Function: Listen
364  * SubFunction: Message listener
365  * FunctionPoints: Process start message monitoring.
366  * EnvConditions: AppSpawn main process has started.
367  *                The socket server has been established.
368  * CaseDescription: 1. Establish a socket client and connect with the Appspawn server
369  */
370 HWTEST_F(AppSpawnModuleTest, AppSpawn_HF_listen_002, TestSize.Level0)
371 {
372     HiLog::Info(LABEL, "AppSpawn_HF_listen_002 start");
373     std::unique_ptr<AppSpawnClient> appSpawnClient = std::make_unique<AppSpawnClient>();
374     std::shared_ptr<AppExecFwk::AppSpawnSocket> appSpawnSocket = std::make_shared<AppExecFwk::AppSpawnSocket>();
375 
376     appSpawnClient->SetSocket(appSpawnSocket);
377 
378     EXPECT_EQ(ERR_OK, appSpawnClient->OpenConnection());
379 
380     appSpawnClient->CloseConnection();
381     EXPECT_EQ(SpawnConnectionState::STATE_NOT_CONNECT, appSpawnClient->QueryConnectionState());
382     HiLog::Info(LABEL, "AppSpawn_HF_listen_002 end");
383 }
384 
385 /*
386  * Feature: AppSpawn
387  * Function: Fork
388  * SubFunction: fork process
389  * FunctionPoints: Fork the process and run the App object.
390  * EnvConditions: AppSpawn main process has started.
391  *                The socket server has been established.
392  * CaseDescription: 1. Establish a socket client and connect with the Appspawn server
393  *                  2. Send the message and the message format is correct, the message type is APP_TYPE_DEFAULT
394  */
395 HWTEST_F(AppSpawnModuleTest, AppSpawn_HF_fork_001, TestSize.Level0)
396 {
397     HiLog::Info(LABEL, "AppSpawn_HF_fork_001 start");
398     std::unique_ptr<AppSpawnClient> appSpawnClient = std::make_unique<AppSpawnClient>();
399     std::shared_ptr<AppExecFwk::AppSpawnSocket> appSpawnSocket = std::make_shared<AppExecFwk::AppSpawnSocket>();
400     appSpawnClient->SetSocket(appSpawnSocket);
401     EXPECT_EQ(ERR_OK, appSpawnClient->OpenConnection());
402     AppSpawnStartMsg params = {10003, 10004, {10003, 10004},
403         "processName-fork_001", "soPath", 0, "system_core", "moduleTestProcessName-fork_001"};
404 
405     appSpawnClient->StartProcess(params, newPid);
406     // 0 < newPid, new process fork success
407     GTEST_LOG_(INFO) << "newPid :" << newPid << ".";
408     EXPECT_LT(DEFAULT_PID, newPid);
409 
410     EXPECT_EQ(ERR_OK, appSpawnSocket->OpenAppSpawnConnection());
411     appSpawnClient->CloseConnection();
412     EXPECT_EQ(SpawnConnectionState::STATE_NOT_CONNECT, appSpawnClient->QueryConnectionState());
413     HiLog::Info(LABEL, "AppSpawn_HF_fork_001 end");
414 }
415 
416 /*
417  * Feature: AppSpawn
418  * Function: Fork
419  * SubFunction: fork process
420  * FunctionPoints: Fork the process and run the App object.
421  * EnvConditions: AppSpawn main process has started.
422  *                The socket server has been established.
423  * CaseDescription: 1. Establish a socket client and connect with the Appspawn server
424  *                  2. Send the message and the message format is correct, the message type is APP_TYPE_NATIVE
425  */
426 HWTEST_F(AppSpawnModuleTest, AppSpawn_HF_fork_002, TestSize.Level0)
427 {
428     HiLog::Info(LABEL, "AppSpawn_HF_fork_002 start");
429     std::unique_ptr<AppSpawnClient> appSpawnClient = std::make_unique<AppSpawnClient>();
430     std::shared_ptr<AppExecFwk::AppSpawnSocket> appSpawnSocket = std::make_shared<AppExecFwk::AppSpawnSocket>();
431     appSpawnClient->SetSocket(appSpawnSocket);
432     EXPECT_EQ(ERR_OK, appSpawnClient->OpenConnection());
433     AppSpawnStartMsg params = {10003, 10004, {10003, 10004},
434         "processName-fork_002", "soPath", 0, "system_core", "moduleTestProcessName-fork_002"};
435 
436     GTEST_LOG_(INFO) << "AppSpawn_HF_fork_002 start " << params.procName.size();
437     appSpawnClient->StartProcess(params, newPid);
438     // 0 < newPid, new process fork success
439     GTEST_LOG_(INFO) << "newPid :" << newPid << ".";
440     EXPECT_LT(DEFAULT_PID, newPid);
441 
442     EXPECT_EQ(ERR_OK, appSpawnSocket->OpenAppSpawnConnection());
443     appSpawnClient->CloseConnection();
444     EXPECT_EQ(SpawnConnectionState::STATE_NOT_CONNECT, appSpawnClient->QueryConnectionState());
445     HiLog::Info(LABEL, "AppSpawn_HF_fork_002 end");
446 }
447 
448 /*
449  * Feature: AppSpawn
450  * Function: SetUid
451  * SubFunction: Set child process permissions
452  * FunctionPoints: Set the permissions of the child process to increase the priority of the new process
453  * EnvConditions: AppSpawn main process has started.
454  *                The socket server has been established.
455  * CaseDescription: 1. Establish a socket client and connect with the Appspawn server
456  *                  2. Send the message and the message format is correct, the message type is APP_TYPE_DEFAULT
457  */
458 HWTEST_F(AppSpawnModuleTest, AppSpawn_HF_setUid_001, TestSize.Level0)
459 {
460     HiLog::Info(LABEL, "AppSpawn_HF_setUid_001 start");
461     std::unique_ptr<AppSpawnClient> appSpawnClient = std::make_unique<AppSpawnClient>();
462     std::shared_ptr<AppExecFwk::AppSpawnSocket> appSpawnSocket = std::make_shared<AppExecFwk::AppSpawnSocket>();
463     appSpawnClient->SetSocket(appSpawnSocket);
464     EXPECT_EQ(ERR_OK, appSpawnClient->OpenConnection());
465     AppSpawnStartMsg params = {10003, 10004, {10003, 10004},
466         "processName-setUid_001", "soPath", 0, "system_core", "moduleTestProcessName-setUid_001"};
467 
468     appSpawnClient->StartProcess(params, newPid);
469     // 0 < newPid, new process fork success
470     GTEST_LOG_(INFO) << "newPid :" << newPid << ".";
471     EXPECT_LT(DEFAULT_PID, newPid);
472 
473     EXPECT_EQ(CHECK_OK, checkUid(newPid, params));
474 
475     EXPECT_EQ(ERR_OK, appSpawnSocket->OpenAppSpawnConnection());
476     appSpawnClient->CloseConnection();
477     EXPECT_EQ(SpawnConnectionState::STATE_NOT_CONNECT, appSpawnClient->QueryConnectionState());
478     HiLog::Info(LABEL, "AppSpawn_HF_setUid_001 end");
479 }
480 
481 /*
482  * Feature: AppSpawn
483  * Function: SetUid
484  * SubFunction: Set child process permissions
485  * FunctionPoints: Set the permissions of the child process to increase the priority of the new process
486  * EnvConditions: AppSpawn main process has started.
487  *                The socket server has been established.
488  * CaseDescription: 1. Establish a socket client and connect with the Appspawn server
489  *                  2. Send the message and the message format is correct, the message type is APP_TYPE_DEFAULT
490  */
491 HWTEST_F(AppSpawnModuleTest, AppSpawn_HF_setUid_002, TestSize.Level0)
492 {
493     HiLog::Info(LABEL, "AppSpawn_HF_setUid_002 start");
494     std::unique_ptr<AppSpawnClient> appSpawnClient = std::make_unique<AppSpawnClient>();
495     std::shared_ptr<AppExecFwk::AppSpawnSocket> appSpawnSocket = std::make_shared<AppExecFwk::AppSpawnSocket>();
496     appSpawnClient->SetSocket(appSpawnSocket);
497     EXPECT_EQ(ERR_OK, appSpawnClient->OpenConnection());
498     AppSpawnStartMsg params = {10003, 10004, {10003, 10004},
499         "processName-setUid_002", "soPath", 0, "system_core", "moduleTestProcessName-setUid_002"};
500 
501     appSpawnClient->StartProcess(params, newPid);
502     // 0 < newPid, new process fork success
503     GTEST_LOG_(INFO) << "newPid :" << newPid << ".";
504     EXPECT_LT(DEFAULT_PID, newPid);
505 
506     EXPECT_EQ(CHECK_OK, checkGid(newPid, params));
507 
508     EXPECT_EQ(ERR_OK, appSpawnSocket->OpenAppSpawnConnection());
509     appSpawnClient->CloseConnection();
510     EXPECT_EQ(SpawnConnectionState::STATE_NOT_CONNECT, appSpawnClient->QueryConnectionState());
511     HiLog::Info(LABEL, "AppSpawn_HF_setUid_002 end");
512 }
513 
514 /*
515  * Feature: AppSpawn
516  * Function: SetUid
517  * SubFunction: Set child process permissions
518  * FunctionPoints: Set the permissions of the child process to increase the priority of the new process
519  * EnvConditions: AppSpawn main process has started.
520  *                The socket server has been established.
521  * CaseDescription: 1. Establish a socket client and connect with the Appspawn server
522  *                  2. Send the message and the message format is correct, the message type is APP_TYPE_DEFAULT
523  */
524 HWTEST_F(AppSpawnModuleTest, AppSpawn_HF_setUid_003, TestSize.Level0)
525 {
526     HiLog::Info(LABEL, "AppSpawn_HF_setUid_003 start");
527     std::unique_ptr<AppSpawnClient> appSpawnClient = std::make_unique<AppSpawnClient>();
528     std::shared_ptr<AppExecFwk::AppSpawnSocket> appSpawnSocket = std::make_shared<AppExecFwk::AppSpawnSocket>();
529     appSpawnClient->SetSocket(appSpawnSocket);
530     EXPECT_EQ(ERR_OK, appSpawnClient->OpenConnection());
531     AppSpawnStartMsg params = {10003, 10004, {10003, 10004},
532         "processName-setUid_003", "soPath", 0, "system_core", "moduleTestProcessName-setUid_003"};
533 
534     appSpawnClient->StartProcess(params, newPid);
535     // 0 < newPid, new process fork success
536     GTEST_LOG_(INFO) << "newPid :" << newPid << ".";
537     EXPECT_LT(DEFAULT_PID, newPid);
538 
539     EXPECT_EQ(CHECK_OK, checkGids(newPid, params));
540 
541     EXPECT_EQ(ERR_OK, appSpawnSocket->OpenAppSpawnConnection());
542     appSpawnClient->CloseConnection();
543     EXPECT_EQ(SpawnConnectionState::STATE_NOT_CONNECT, appSpawnClient->QueryConnectionState());
544     HiLog::Info(LABEL, "AppSpawn_HF_setUid_003 end");
545 }
546 
547 /*
548  * Feature: AppSpawn
549  * Function: SetUid
550  * SubFunction: Set child process permissions
551  * FunctionPoints: Set the permissions of the child process to increase the priority of the new process
552  * EnvConditions: AppSpawn main process has started.
553  *                The socket server has been established.
554  * CaseDescription: 1. Establish a socket client and connect with the Appspawn server
555  *                  2. Send the message and the message format is correct, the message type is APP_TYPE_DEFAULT
556  */
557 HWTEST_F(AppSpawnModuleTest, AppSpawn_HF_setUid_004, TestSize.Level0)
558 {
559     HiLog::Info(LABEL, "AppSpawn_HF_setUid_004 start");
560     std::unique_ptr<AppSpawnClient> appSpawnClient = std::make_unique<AppSpawnClient>();
561     std::shared_ptr<AppExecFwk::AppSpawnSocket> appSpawnSocket = std::make_shared<AppExecFwk::AppSpawnSocket>();
562     appSpawnClient->SetSocket(appSpawnSocket);
563     EXPECT_EQ(ERR_OK, appSpawnClient->OpenConnection());
564     AppSpawnStartMsg params = {10003, 10004, {10003, 10004},
565         "processName-setUid_004", "soPath", 0, "system_core", "moduleTestProcessName-setUid_004"};
566 
567     appSpawnClient->StartProcess(params, newPid);
568     // 0 < newPid, new process fork success
569     GTEST_LOG_(INFO) << "newPid :" << newPid << ".";
570     EXPECT_LT(DEFAULT_PID, newPid);
571 
572     EXPECT_EQ(CHECK_OK, checkGidsCount(newPid, params));
573 
574     EXPECT_EQ(ERR_OK, appSpawnSocket->OpenAppSpawnConnection());
575     appSpawnClient->CloseConnection();
576     EXPECT_EQ(SpawnConnectionState::STATE_NOT_CONNECT, appSpawnClient->QueryConnectionState());
577     HiLog::Info(LABEL, "AppSpawn_HF_setUid_004 end");
578 }
579 
580 /*
581  * Feature: AppSpawn
582  * Function: SetUid
583  * SubFunction: Set child process permissions
584  * FunctionPoints: Set the permissions of the child process to increase the priority of the new process
585  * EnvConditions: AppSpawn main process has started.
586  *                The socket server has been established.
587  * CaseDescription: 1. Establish a socket client and connect with the Appspawn server
588  *                  2. Send the message and the message format is correct, the message type is APP_TYPE_NATIVE
589  */
590 HWTEST_F(AppSpawnModuleTest, AppSpawn_HF_setUid_005, TestSize.Level0)
591 {
592     HiLog::Info(LABEL, "AppSpawn_HF_setUid_005 start");
593     std::unique_ptr<AppSpawnClient> appSpawnClient = std::make_unique<AppSpawnClient>();
594     std::shared_ptr<AppExecFwk::AppSpawnSocket> appSpawnSocket = std::make_shared<AppExecFwk::AppSpawnSocket>();
595     appSpawnClient->SetSocket(appSpawnSocket);
596     EXPECT_EQ(ERR_OK, appSpawnClient->OpenConnection());
597     AppSpawnStartMsg params = {10003, 10004, {10003, 10004},
598         "processName-setUid_005", "soPath", 0, "system_core", "moduleTestProcessName-setUid_005"};
599 
600     appSpawnClient->StartProcess(params, newPid);
601     // 0 < newPid, new process fork success
602     GTEST_LOG_(INFO) << "newPid :" << newPid << ".";
603     EXPECT_LT(DEFAULT_PID, newPid);
604 
605     EXPECT_EQ(CHECK_OK, checkUid(newPid, params));
606 
607     EXPECT_EQ(ERR_OK, appSpawnSocket->OpenAppSpawnConnection());
608     appSpawnClient->CloseConnection();
609     EXPECT_EQ(SpawnConnectionState::STATE_NOT_CONNECT, appSpawnClient->QueryConnectionState());
610     HiLog::Info(LABEL, "AppSpawn_HF_setUid_005 end");
611 }
612 
613 /*
614  * Feature: AppSpawn
615  * Function: SetUid
616  * SubFunction: Set child process permissions
617  * FunctionPoints: Set the permissions of the child process to increase the priority of the new process
618  * EnvConditions: AppSpawn main process has started.
619  *                The socket server has been established.
620  * CaseDescription: 1. Establish a socket client and connect with the Appspawn server
621  *                  2. Send the message and the message format is correct, the message type is APP_TYPE_NATIVE
622  */
623 HWTEST_F(AppSpawnModuleTest, AppSpawn_HF_setUid_006, TestSize.Level0)
624 {
625     HiLog::Info(LABEL, "AppSpawn_HF_setUid_006 start");
626     std::unique_ptr<AppSpawnClient> appSpawnClient = std::make_unique<AppSpawnClient>();
627     std::shared_ptr<AppExecFwk::AppSpawnSocket> appSpawnSocket = std::make_shared<AppExecFwk::AppSpawnSocket>();
628     appSpawnClient->SetSocket(appSpawnSocket);
629     EXPECT_EQ(ERR_OK, appSpawnClient->OpenConnection());
630     AppSpawnStartMsg params = {10003, 10004, {10003, 10004},
631         "processName-setUid_006", "soPath", 0, "system_core", "moduleTestProcessName-setUid_006"};
632 
633     appSpawnClient->StartProcess(params, newPid);
634     // 0 < newPid, new process fork success
635     GTEST_LOG_(INFO) << "newPid :" << newPid << ".";
636     EXPECT_LT(DEFAULT_PID, newPid);
637 
638     EXPECT_EQ(CHECK_OK, checkGid(newPid, params));
639 
640     EXPECT_EQ(ERR_OK, appSpawnSocket->OpenAppSpawnConnection());
641     appSpawnClient->CloseConnection();
642     EXPECT_EQ(SpawnConnectionState::STATE_NOT_CONNECT, appSpawnClient->QueryConnectionState());
643     HiLog::Info(LABEL, "AppSpawn_HF_setUid_006 end");
644 }
645 
646 /*
647  * Feature: AppSpawn
648  * Function: SetUid
649  * SubFunction: Set child process permissions
650  * FunctionPoints: Set the permissions of the child process to increase the priority of the new process
651  * EnvConditions: AppSpawn main process has started.
652  *                The socket server has been established.
653  * CaseDescription: 1. Establish a socket client and connect with the Appspawn server
654  *                  2. Send the message and the message format is correct, the message type is APP_TYPE_NATIVE
655  */
656 HWTEST_F(AppSpawnModuleTest, AppSpawn_HF_setUid_007, TestSize.Level0)
657 {
658     HiLog::Info(LABEL, "AppSpawn_HF_setUid_007 start");
659     std::unique_ptr<AppSpawnClient> appSpawnClient = std::make_unique<AppSpawnClient>();
660     std::shared_ptr<AppExecFwk::AppSpawnSocket> appSpawnSocket = std::make_shared<AppExecFwk::AppSpawnSocket>();
661     appSpawnClient->SetSocket(appSpawnSocket);
662     EXPECT_EQ(ERR_OK, appSpawnClient->OpenConnection());
663     AppSpawnStartMsg params = {10003, 10004, {10003, 10004},
664         "processName-setUid_007", "soPath", 0, "system_core", "moduleTestProcessName-setUid_007"};
665 
666     appSpawnClient->StartProcess(params, newPid);
667     // 0 < newPid, new process fork success
668     GTEST_LOG_(INFO) << "newPid :" << newPid << ".";
669     EXPECT_LT(DEFAULT_PID, newPid);
670 
671     EXPECT_EQ(CHECK_OK, checkGids(newPid, params));
672 
673     EXPECT_EQ(ERR_OK, appSpawnSocket->OpenAppSpawnConnection());
674     appSpawnClient->CloseConnection();
675     EXPECT_EQ(SpawnConnectionState::STATE_NOT_CONNECT, appSpawnClient->QueryConnectionState());
676     HiLog::Info(LABEL, "AppSpawn_HF_setUid_007 end");
677 }
678 
679 /*
680  * Feature: AppSpawn
681  * Function: SetUid
682  * SubFunction: Set child process permissions
683  * FunctionPoints: Set the permissions of the child process to increase the priority of the new process
684  * EnvConditions: AppSpawn main process has started.
685  *                The socket server has been established.
686  * CaseDescription: 1. Establish a socket client and connect with the Appspawn server
687  *                  2. Send the message and the message format is correct, the message type is APP_TYPE_NATIVE
688  */
689 HWTEST_F(AppSpawnModuleTest, AppSpawn_HF_setUid_008, TestSize.Level0)
690 {
691     HiLog::Info(LABEL, "AppSpawn_HF_setUid_008 start");
692     std::unique_ptr<AppSpawnClient> appSpawnClient = std::make_unique<AppSpawnClient>();
693     std::shared_ptr<AppExecFwk::AppSpawnSocket> appSpawnSocket = std::make_shared<AppExecFwk::AppSpawnSocket>();
694     appSpawnClient->SetSocket(appSpawnSocket);
695     EXPECT_EQ(ERR_OK, appSpawnClient->OpenConnection());
696     AppSpawnStartMsg params = {10003, 10004, {10003, 10004},
697         "processName-setUid_008", "soPath", 0, "system_core", "moduleTestProcessName-setUid_008"};
698 
699     appSpawnClient->StartProcess(params, newPid);
700     // 0 < newPid, new process fork success
701     GTEST_LOG_(INFO) << "newPid :" << newPid << ".";
702     EXPECT_LT(DEFAULT_PID, newPid);
703 
704     EXPECT_EQ(CHECK_OK, checkGidsCount(newPid, params));
705 
706     EXPECT_EQ(ERR_OK, appSpawnSocket->OpenAppSpawnConnection());
707     appSpawnClient->CloseConnection();
708     EXPECT_EQ(SpawnConnectionState::STATE_NOT_CONNECT, appSpawnClient->QueryConnectionState());
709     HiLog::Info(LABEL, "AppSpawn_HF_setUid_008 end");
710 }
711 
712 /*
713  * Feature: AppSpawn
714  * Function: setProcName
715  * SubFunction: Set process name
716  * FunctionPoints: Set process information .
717  * EnvConditions: AppSpawn main process has started.
718  *                The socket server has been established.
719  * CaseDescription: 1. Establish a socket client and connect with the Appspawn server
720  *                  2. Send the message and the message format is correct, the message type is APP_TYPE_DEFAULT
721  */
722 HWTEST_F(AppSpawnModuleTest, AppSpawn_HF_setProcName_001, TestSize.Level0)
723 {
724     HiLog::Info(LABEL, "AppSpawn_HF_setProcName_001 start");
725     std::unique_ptr<AppSpawnClient> appSpawnClient = std::make_unique<AppSpawnClient>();
726     std::shared_ptr<AppExecFwk::AppSpawnSocket> appSpawnSocket = std::make_shared<AppExecFwk::AppSpawnSocket>();
727     appSpawnClient->SetSocket(appSpawnSocket);
728     EXPECT_EQ(ERR_OK, appSpawnClient->OpenConnection());
729     AppSpawnStartMsg params = {10003, 10004, {10003, 10004},
730         "processName-setProcName_001", "soPath", 0, "system_core", "moduleTestProcessName-setProcName_001"};
731 
732     appSpawnClient->StartProcess(params, newPid);
733     // 0 < newPid, new process fork success
734     GTEST_LOG_(INFO) << "newPid :" << newPid << ".";
735     EXPECT_LT(DEFAULT_PID, newPid);
736 
737     // Check new app proc name
738     EXPECT_EQ(CHECK_OK, checkProcName(newPid, params));
739 
740     EXPECT_EQ(ERR_OK, appSpawnSocket->OpenAppSpawnConnection());
741     appSpawnClient->CloseConnection();
742     EXPECT_EQ(SpawnConnectionState::STATE_NOT_CONNECT, appSpawnClient->QueryConnectionState());
743     HiLog::Info(LABEL, "AppSpawn_HF_setProcName_001 end");
744 }
745 
746 /*
747  * Feature: AppSpawn
748  * Function: setProcName
749  * SubFunction: Set process name
750  * FunctionPoints: Set process information .
751  * EnvConditions: AppSpawn main process has started.
752  *                The socket server has been established.
753  * CaseDescription: 1. Establish a socket client and connect with the Appspawn server
754  *                  2. Send the message and the message format is correct, the message type is APP_TYPE_NATIVE
755  */
756 HWTEST_F(AppSpawnModuleTest, AppSpawn_HF_setProcName_002, TestSize.Level0)
757 {
758     HiLog::Info(LABEL, "AppSpawn_HF_setProcName_002 start");
759     std::unique_ptr<AppSpawnClient> appSpawnClient = std::make_unique<AppSpawnClient>();
760     std::shared_ptr<AppExecFwk::AppSpawnSocket> appSpawnSocket = std::make_shared<AppExecFwk::AppSpawnSocket>();
761     appSpawnClient->SetSocket(appSpawnSocket);
762     EXPECT_EQ(ERR_OK, appSpawnClient->OpenConnection());
763     AppSpawnStartMsg params = {10003, 10004, {10003, 10004},
764         "processName-setProcName_002", "soPath", 0, "system_core", "moduleTestProcessName-setProcName_002"};
765 
766     appSpawnClient->StartProcess(params, newPid);
767     // 0 < newPid, new process fork success
768     GTEST_LOG_(INFO) << "newPid :" << newPid << ".";
769     EXPECT_LT(DEFAULT_PID, newPid);
770 
771     // Check new app proc name
772     EXPECT_EQ(CHECK_OK, checkProcName(newPid, params));
773 
774     EXPECT_EQ(ERR_OK, appSpawnSocket->OpenAppSpawnConnection());
775     appSpawnClient->CloseConnection();
776     EXPECT_EQ(SpawnConnectionState::STATE_NOT_CONNECT, appSpawnClient->QueryConnectionState());
777     HiLog::Info(LABEL, "AppSpawn_HF_setProcName_002 end");
778 }
779 
780 /*
781  * Feature: AppSpawn
782  * Function: recycleProc
783  * SubFunction: Recycling process
784  * FunctionPoints: Recycling zombie processes.
785  * EnvConditions: Start a js ability
786  * CaseDescription: 1. Use the command kill to kill the process pid of the ability
787  */
788 HWTEST_F(AppSpawnModuleTest, AppSpawn_HF_recycleProc_001, TestSize.Level0)
789 {
790     HiLog::Info(LABEL, "AppSpawn_HF_recycleProc_001 start");
791     std::unique_ptr<AppSpawnClient> appSpawnClient = std::make_unique<AppSpawnClient>();
792     std::shared_ptr<AppExecFwk::AppSpawnSocket> appSpawnSocket = std::make_shared<AppExecFwk::AppSpawnSocket>();
793     appSpawnClient->SetSocket(appSpawnSocket);
794     EXPECT_EQ(ERR_OK, appSpawnClient->OpenConnection());
795     AppSpawnStartMsg params = {10003, 10004, {10003, 10004},
796         "processName-recycleProc_001", "soPath", 0, "system_core", "moduleTestProcessName-recycleProc_001"};
797 
798     appSpawnClient->StartProcess(params, newPid);
799     // 0 < newPid, new process fork success
800     GTEST_LOG_(INFO) << "newPid :" << newPid << ".";
801     EXPECT_LT(DEFAULT_PID, newPid);
802 
803     EXPECT_EQ(ERR_OK, kill(newPid, SIGKILL));
804     newPid = DEFAULT_PID;
805 
806     // Check Process Is Destroyed
807     EXPECT_EQ(CHECK_OK, checkProcessIsDestroyed(newPid));
808 
809     EXPECT_EQ(ERR_OK, appSpawnSocket->OpenAppSpawnConnection());
810     appSpawnClient->CloseConnection();
811     EXPECT_EQ(SpawnConnectionState::STATE_NOT_CONNECT, appSpawnClient->QueryConnectionState());
812     HiLog::Info(LABEL, "AppSpawn_HF_recycleProc_001 end");
813 }
814 
815 /*
816  * Feature: AppSpawn
817  * Function: recycleProc
818  * SubFunction: Recycling process
819  * FunctionPoints: Recycling zombie processes
820  * EnvConditions: Start a native ability .
821  * CaseDescription: 1. Use the command kill to kill the process pid of the ability
822  */
823 HWTEST_F(AppSpawnModuleTest, AppSpawn_HF_recycleProc_002, TestSize.Level0)
824 {
825     HiLog::Info(LABEL, "AppSpawn_HF_recycleProc_002 start");
826     std::unique_ptr<AppSpawnClient> appSpawnClient = std::make_unique<AppSpawnClient>();
827     std::shared_ptr<AppExecFwk::AppSpawnSocket> appSpawnSocket = std::make_shared<AppExecFwk::AppSpawnSocket>();
828     appSpawnClient->SetSocket(appSpawnSocket);
829     EXPECT_EQ(ERR_OK, appSpawnClient->OpenConnection());
830     AppSpawnStartMsg params = {10003, 10004, {10003, 10004},
831         "processName-recycleProc_002", "soPath", 0, "system_core", "moduleTestProcessName-recycleProc_002"};
832 
833     appSpawnClient->StartProcess(params, newPid);
834     // 0 < newPid, new process fork success
835     GTEST_LOG_(INFO) << "newPid :" << newPid << ".";
836     EXPECT_LT(DEFAULT_PID, newPid);
837 
838     EXPECT_EQ(ERR_OK, kill(newPid, SIGKILL));
839     newPid = DEFAULT_PID;
840 
841     // Check Process Is Destroyed
842     EXPECT_EQ(CHECK_OK, checkProcessIsDestroyed(newPid));
843 
844     EXPECT_EQ(ERR_OK, appSpawnSocket->OpenAppSpawnConnection());
845     appSpawnClient->CloseConnection();
846     EXPECT_EQ(SpawnConnectionState::STATE_NOT_CONNECT, appSpawnClient->QueryConnectionState());
847     HiLog::Info(LABEL, "AppSpawn_HF_recycleProc_002 end");
848 }
849 }  // namespace AppSpawn
850 }  // namespace OHOS
851