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