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 29using namespace testing; 30using namespace testing::ext; 31 32namespace OHOS { 33namespace AppSpawn { 34namespace { 35static const int32_t DEFAULT_PID = 0; 36static const int32_t FILE_PATH_SIZE = 50; 37static const int32_t BUFFER_SIZE = 512; 38static const int32_t BASE_TYPE = 10; 39static const int32_t CONNECT_RETRY_DELAY = 50 * 1000; 40static const int32_t CONNECT_RETRY_MAX_TIMES = 5; 41static const int32_t UID_POSITION_MOVE = 5; 42static const int32_t GID_POSITION_MOVE = 5; 43static const int32_t GROUPS_POSITION_MOVE = 8; 44static const char *DELIMITER_SPACE = " "; 45static const char *DELIMITER_NEWLINE = "\n"; 46static char g_buffer[BUFFER_SIZE + 1] = {}; 47} // namespace 48 49bool 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 63bool 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 77bool 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 110bool 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 false; 131} 132 133bool 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 false; 158} 159 160std::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 194bool 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 false; 203} 204 205bool 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 216bool 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 234bool 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 244bool 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 261bool 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 274bool 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 287static 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 332static 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 377static 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 422static 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 463static 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 502static 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 544static 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 585static 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 630class AppSpawnModuleTest : public testing::Test { 631public: 632 static void SetUpTestCase() {} 633 static void TearDownTestCase() {} 634 void SetUp() {} 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 */ 647HWTEST_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 */ 665HWTEST_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 EXPECT_EQ(0, kill(result.pid, SIGKILL)); 678 result.pid = DEFAULT_PID; 679 } 680 HILOG_INFO(LOG_CORE, "AppSpawn_HF_fork_001 end"); 681} 682 683/* 684 * Feature: AppSpawn 685 * Function: Fork 686 * SubFunction: fork process 687 * FunctionPoints: Fork the process and run the App object. 688 * EnvConditions: AppSpawn main process has started. 689 * The socket server has been established. 690 * CaseDescription: 1. Establish a socket client and connect with the Appspawn server 691 * 2. Send the message and the message format is correct, the message type is APP_TYPE_DEFAULT 692 */ 693HWTEST_F(AppSpawnModuleTest, AppSpawn_HF_fork_002, TestSize.Level0) 694{ 695 HILOG_INFO(LOG_CORE, "AppSpawn_HF_fork_002 start"); 696 697 OHOS::AppSpawnModuleTest::AppSpawnTestCommander commander; 698 AppSpawnReqMsgHandle reqHandle; 699 AppSpawnResult result; 700 commander.CreateMsg(reqHandle, defaultAppInfo1.c_str()); 701 int ret = AppSpawnClientSendMsg(commander.GetClientHandle(), reqHandle, &result); 702 EXPECT_EQ(0, ret); 703 EXPECT_EQ(0, result.result); 704 if (result.pid > 0) { 705 EXPECT_EQ(0, kill(result.pid, SIGKILL)); 706 result.pid = DEFAULT_PID; 707 } 708 HILOG_INFO(LOG_CORE, "AppSpawn_HF_fork_002 end"); 709} 710 711/* 712 * Feature: AppSpawn 713 * Function: Fork 714 * SubFunction: fork process 715 * FunctionPoints: Fork the process and run the App object. 716 * EnvConditions: AppSpawn main process has started. 717 * The socket server has been established. 718 * CaseDescription: 1. Establish a socket client and connect with the Appspawn server 719 * 2. Send the message and the message format is correct, the message type is APP_TYPE_NATIVE 720 */ 721HWTEST_F(AppSpawnModuleTest, AppSpawn_HF_fork_003, TestSize.Level0) 722{ 723 HILOG_INFO(LOG_CORE, "AppSpawn_HF_fork_003 start"); 724 725 OHOS::AppSpawnModuleTest::AppSpawnTestCommander commander; 726 AppSpawnReqMsgHandle reqHandle; 727 AppSpawnResult result; 728 commander.CreateMsg(reqHandle, defaultAppInfo2.c_str()); 729 int ret = AppSpawnClientSendMsg(commander.GetClientHandle(), reqHandle, &result); 730 EXPECT_EQ(0, ret); 731 EXPECT_EQ(0, result.result); 732 EXPECT_NE(0, result.pid); 733 if (result.pid > 0) { 734 EXPECT_EQ(0, kill(result.pid, SIGKILL)); 735 result.pid = DEFAULT_PID; 736 } 737 HILOG_INFO(LOG_CORE, "AppSpawn_HF_fork_003 end"); 738} 739 740HWTEST_F(AppSpawnModuleTest, AppSpawn_Native_Fork_001, TestSize.Level0) 741{ 742 HILOG_INFO(LOG_CORE, "AppSpawn_Native_Fork_001 start"); 743 744 OHOS::AppSpawnModuleTest::AppSpawnTestCommander commander; 745 AppSpawnReqMsgHandle reqHandle; 746 AppSpawnResult result; 747 commander.CreateMsg(reqHandle, defaultAppInfo2.c_str(), MSG_SPAWN_NATIVE_PROCESS); 748 int ret = AppSpawnClientSendMsg(commander.GetClientHandle(), reqHandle, &result); 749 EXPECT_EQ(0, ret); 750 EXPECT_EQ(0, result.result); 751 EXPECT_NE(0, result.pid); 752 if (result.pid > 0) { 753 EXPECT_EQ(0, kill(result.pid, SIGKILL)); 754 result.pid = DEFAULT_PID; 755 } 756 HILOG_INFO(LOG_CORE, "AppSpawn_Native_Fork_001 end"); 757} 758 759HWTEST_F(AppSpawnModuleTest, AppSpawn_Native_Fork_002, TestSize.Level0) 760{ 761 HILOG_INFO(LOG_CORE, "AppSpawn_Native_Fork_002 start"); 762 763 OHOS::AppSpawnModuleTest::AppSpawnTestCommander commander(0); // for nwebspawn 764 AppSpawnReqMsgHandle reqHandle; 765 AppSpawnResult result; 766 commander.CreateMsg(reqHandle, defaultWebInfo1.c_str(), MSG_SPAWN_NATIVE_PROCESS); 767 int ret = AppSpawnClientSendMsg(commander.GetClientHandle(), reqHandle, &result); 768 EXPECT_EQ(0, ret); 769 EXPECT_EQ(0, result.result); 770 EXPECT_NE(0, result.pid); 771 if (result.pid > 0) { 772 EXPECT_EQ(0, kill(result.pid, SIGKILL)); 773 result.pid = DEFAULT_PID; 774 } 775 HILOG_INFO(LOG_CORE, "AppSpawn_Native_Fork_002 end"); 776} 777 778/* 779 * Feature: AppSpawn 780 * Function: SetUid 781 * SubFunction: Set child process permissions 782 * FunctionPoints: Set the permissions of the child process to increase the priority of the new process 783 * EnvConditions: AppSpawn main process has started. 784 * The socket server has been established. 785 * CaseDescription: 1. Establish a socket client and connect with the Appspawn server 786 * 2. Send the message and the message format is correct, the message type is APP_TYPE_DEFAULT 787 */ 788HWTEST_F(AppSpawnModuleTest, AppSpawn_HF_setUid_001, TestSize.Level0) 789{ 790 HILOG_INFO(LOG_CORE, "AppSpawn_HF_setUid_001 start"); 791 792 OHOS::AppSpawnModuleTest::AppSpawnTestCommander commander; 793 AppSpawnReqMsgHandle reqHandle; 794 AppSpawnResult result; 795 commander.CreateMsg(reqHandle, defaultAppInfo1.c_str()); 796 int ret = AppSpawnClientSendMsg(commander.GetClientHandle(), reqHandle, &result); 797 EXPECT_EQ(0, ret); 798 EXPECT_EQ(0, result.result); 799 EXPECT_NE(0, result.pid); 800 GTEST_LOG_(INFO) << "newPid :" << result.pid << "."; 801 EXPECT_EQ(true, CheckUid(result.pid, 20010041)); // 20010041 test 802 if (result.pid > 0) { 803 EXPECT_EQ(0, kill(result.pid, SIGKILL)); 804 result.pid = DEFAULT_PID; 805 } 806 HILOG_INFO(LOG_CORE, "AppSpawn_HF_setUid_001 end"); 807} 808 809HWTEST_F(AppSpawnModuleTest, AppSpawn_HF_setUid_002, TestSize.Level0) 810{ 811 HILOG_INFO(LOG_CORE, "AppSpawn_HF_setUid_002 start"); 812 813 OHOS::AppSpawnModuleTest::AppSpawnTestCommander commander; 814 AppSpawnReqMsgHandle reqHandle; 815 AppSpawnResult result; 816 commander.CreateMsg(reqHandle, defaultAppInfo2.c_str()); 817 int ret = AppSpawnClientSendMsg(commander.GetClientHandle(), reqHandle, &result); 818 EXPECT_EQ(0, ret); 819 EXPECT_EQ(0, result.result); 820 EXPECT_NE(0, result.pid); 821 GTEST_LOG_(INFO) << "newPid :" << result.pid << "."; 822 EXPECT_EQ(true, CheckUid(result.pid, 20010043)); // 20010043 test 823 824 if (result.pid > 0) { 825 EXPECT_EQ(0, kill(result.pid, SIGKILL)); 826 result.pid = DEFAULT_PID; 827 } 828 HILOG_INFO(LOG_CORE, "AppSpawn_HF_setUid_002 end"); 829} 830 831HWTEST_F(AppSpawnModuleTest, AppSpawn_HF_setUid_003, TestSize.Level0) 832{ 833 HILOG_INFO(LOG_CORE, "AppSpawn_HF_setUid_003 start"); 834 835 OHOS::AppSpawnModuleTest::AppSpawnTestCommander commander; 836 AppSpawnReqMsgHandle reqHandle; 837 AppSpawnResult result; 838 commander.CreateMsg(reqHandle, defaultAppInfo2.c_str(), MSG_SPAWN_NATIVE_PROCESS); 839 int ret = AppSpawnClientSendMsg(commander.GetClientHandle(), reqHandle, &result); 840 EXPECT_EQ(0, ret); 841 EXPECT_EQ(0, result.result); 842 EXPECT_NE(0, result.pid); 843 GTEST_LOG_(INFO) << "newPid :" << result.pid << "."; 844 EXPECT_EQ(true, CheckUid(result.pid, 20010043)); // 20010043 test 845 846 if (result.pid > 0) { 847 EXPECT_EQ(0, kill(result.pid, SIGKILL)); 848 result.pid = DEFAULT_PID; 849 } 850 HILOG_INFO(LOG_CORE, "AppSpawn_HF_setUid_003 end"); 851} 852 853HWTEST_F(AppSpawnModuleTest, AppSpawn_HF_setUid_004, TestSize.Level0) 854{ 855 HILOG_INFO(LOG_CORE, "AppSpawn_HF_setUid_004 start"); 856 857 OHOS::AppSpawnModuleTest::AppSpawnTestCommander commander(0); // nwebspawn 858 AppSpawnReqMsgHandle reqHandle; 859 AppSpawnResult result; 860 commander.CreateMsg(reqHandle, defaultWebInfo1.c_str()); 861 int ret = AppSpawnClientSendMsg(commander.GetClientHandle(), reqHandle, &result); 862 EXPECT_EQ(0, ret); 863 EXPECT_EQ(0, result.result); 864 EXPECT_NE(0, result.pid); 865 GTEST_LOG_(INFO) << "newPid :" << result.pid << "."; 866 EXPECT_EQ(true, CheckUid(result.pid, 1000001)); // 1000001 test 867 868 if (result.pid > 0) { 869 EXPECT_EQ(0, kill(result.pid, SIGKILL)); 870 result.pid = DEFAULT_PID; 871 } 872 HILOG_INFO(LOG_CORE, "AppSpawn_HF_setUid_004 end"); 873} 874 875HWTEST_F(AppSpawnModuleTest, AppSpawn_HF_setUid_005, TestSize.Level0) 876{ 877 HILOG_INFO(LOG_CORE, "AppSpawn_HF_setUid_005 start"); 878 879 OHOS::AppSpawnModuleTest::AppSpawnTestCommander commander(0); // nwebspawn 880 AppSpawnReqMsgHandle reqHandle; 881 AppSpawnResult result; 882 commander.CreateMsg(reqHandle, defaultWebInfo1.c_str(), MSG_SPAWN_NATIVE_PROCESS); 883 int ret = AppSpawnClientSendMsg(commander.GetClientHandle(), reqHandle, &result); 884 EXPECT_EQ(0, ret); 885 EXPECT_EQ(0, result.result); 886 EXPECT_NE(0, result.pid); 887 GTEST_LOG_(INFO) << "newPid :" << result.pid << "."; 888 EXPECT_EQ(true, CheckUid(result.pid, 1000001)); // 1000001 test 889 890 if (result.pid > 0) { 891 EXPECT_EQ(0, kill(result.pid, SIGKILL)); 892 result.pid = DEFAULT_PID; 893 } 894 HILOG_INFO(LOG_CORE, "AppSpawn_HF_setUid_005 end"); 895} 896 897/* 898 * Feature: AppSpawn 899 * Function: CheckGid 900 * SubFunction: Set child process permissions 901 * FunctionPoints: Set the permissions of the child process to increase the priority of the new process 902 * EnvConditions: AppSpawn main process has started. 903 * The socket server has been established. 904 * CaseDescription: 1. Establish a socket client and connect with the Appspawn server 905 * 2. Send the message and the message format is correct, the message type is APP_TYPE_DEFAULT 906 */ 907HWTEST_F(AppSpawnModuleTest, AppSpawn_HF_checkGid_001, TestSize.Level0) 908{ 909 HILOG_INFO(LOG_CORE, "AppSpawn_HF_checkGid_001 start"); 910 911 OHOS::AppSpawnModuleTest::AppSpawnTestCommander commander; 912 AppSpawnReqMsgHandle reqHandle; 913 AppSpawnResult result; 914 commander.CreateMsg(reqHandle, defaultAppInfo2.c_str()); 915 int ret = AppSpawnClientSendMsg(commander.GetClientHandle(), reqHandle, &result); 916 EXPECT_EQ(0, ret); 917 EXPECT_EQ(0, result.result); 918 EXPECT_NE(0, result.pid); 919 GTEST_LOG_(INFO) << "newPid :" << result.pid << "."; 920 EXPECT_EQ(true, CheckGid(result.pid, 20010043)); // 20010043 test gid 921 922 if (result.pid > 0) { 923 EXPECT_EQ(0, kill(result.pid, SIGKILL)); 924 result.pid = DEFAULT_PID; 925 } 926 HILOG_INFO(LOG_CORE, "AppSpawn_HF_checkGid_001 end"); 927} 928 929HWTEST_F(AppSpawnModuleTest, AppSpawn_HF_checkGid_002, TestSize.Level0) 930{ 931 HILOG_INFO(LOG_CORE, "AppSpawn_HF_checkGid_002 start"); 932 933 OHOS::AppSpawnModuleTest::AppSpawnTestCommander commander; 934 AppSpawnReqMsgHandle reqHandle; 935 AppSpawnResult result; 936 commander.CreateMsg(reqHandle, defaultAppInfo1.c_str()); 937 int ret = AppSpawnClientSendMsg(commander.GetClientHandle(), reqHandle, &result); 938 EXPECT_EQ(0, ret); 939 EXPECT_EQ(0, result.result); 940 941 EXPECT_NE(0, result.pid); 942 GTEST_LOG_(INFO) << "newPid :" << result.pid << "."; 943 EXPECT_EQ(true, CheckGid(result.pid, 20010041)); // 20010041 test gid 944 945 if (result.pid > 0) { 946 EXPECT_EQ(0, kill(result.pid, SIGKILL)); 947 result.pid = DEFAULT_PID; 948 } 949 HILOG_INFO(LOG_CORE, "AppSpawn_HF_checkGid_002 end"); 950} 951 952HWTEST_F(AppSpawnModuleTest, AppSpawn_HF_checkGid_003, TestSize.Level0) 953{ 954 HILOG_INFO(LOG_CORE, "AppSpawn_HF_checkGid_003 start"); 955 956 OHOS::AppSpawnModuleTest::AppSpawnTestCommander commander; 957 AppSpawnReqMsgHandle reqHandle; 958 AppSpawnResult result; 959 commander.CreateMsg(reqHandle, defaultAppInfo1.c_str(), MSG_SPAWN_NATIVE_PROCESS); 960 int ret = AppSpawnClientSendMsg(commander.GetClientHandle(), reqHandle, &result); 961 EXPECT_EQ(0, ret); 962 EXPECT_EQ(0, result.result); 963 964 EXPECT_NE(0, result.pid); 965 GTEST_LOG_(INFO) << "newPid :" << result.pid << "."; 966 EXPECT_EQ(true, CheckGid(result.pid, 20010041)); // 20010041 test gid 967 968 if (result.pid > 0) { 969 EXPECT_EQ(0, kill(result.pid, SIGKILL)); 970 result.pid = DEFAULT_PID; 971 } 972 HILOG_INFO(LOG_CORE, "AppSpawn_HF_checkGid_003 end"); 973} 974 975HWTEST_F(AppSpawnModuleTest, AppSpawn_HF_checkGid_004, TestSize.Level0) 976{ 977 HILOG_INFO(LOG_CORE, "AppSpawn_HF_checkGid_004 start"); 978 OHOS::AppSpawnModuleTest::AppSpawnTestCommander commander(0); // for nwebspawn 979 AppSpawnReqMsgHandle reqHandle; 980 AppSpawnResult result; 981 commander.CreateMsg(reqHandle, defaultWebInfo1.c_str()); 982 int ret = AppSpawnClientSendMsg(commander.GetClientHandle(), reqHandle, &result); 983 EXPECT_EQ(0, ret); 984 EXPECT_EQ(0, result.result); 985 986 EXPECT_NE(0, result.pid); 987 GTEST_LOG_(INFO) << "newPid :" << result.pid << "."; 988 EXPECT_EQ(true, CheckGid(result.pid, 1000001)); // 1000001 test gid 989 990 if (result.pid > 0) { 991 EXPECT_EQ(0, kill(result.pid, SIGKILL)); 992 result.pid = DEFAULT_PID; 993 } 994 HILOG_INFO(LOG_CORE, "AppSpawn_HF_checkGid_004 end"); 995} 996 997HWTEST_F(AppSpawnModuleTest, AppSpawn_HF_checkGid_005, TestSize.Level0) 998{ 999 HILOG_INFO(LOG_CORE, "AppSpawn_HF_checkGid_005 start"); 1000 OHOS::AppSpawnModuleTest::AppSpawnTestCommander commander(0); // for nwebspawn 1001 AppSpawnReqMsgHandle reqHandle; 1002 AppSpawnResult result; 1003 commander.CreateMsg(reqHandle, defaultWebInfo1.c_str(), MSG_SPAWN_NATIVE_PROCESS); 1004 int ret = AppSpawnClientSendMsg(commander.GetClientHandle(), reqHandle, &result); 1005 EXPECT_EQ(0, ret); 1006 EXPECT_EQ(0, result.result); 1007 1008 EXPECT_NE(0, result.pid); 1009 GTEST_LOG_(INFO) << "newPid :" << result.pid << "."; 1010 EXPECT_EQ(true, CheckGid(result.pid, 1000001)); // 1000001 test gid 1011 1012 if (result.pid > 0) { 1013 EXPECT_EQ(0, kill(result.pid, SIGKILL)); 1014 result.pid = DEFAULT_PID; 1015 } 1016 HILOG_INFO(LOG_CORE, "AppSpawn_HF_checkGid_005 end"); 1017} 1018 1019/* 1020 * Feature: AppSpawn 1021 * Function: CheckGids 1022 * SubFunction: Set child process permissions 1023 * FunctionPoints: Set the permissions of the child process to increase the priority of the new process 1024 * EnvConditions: AppSpawn main process has started. 1025 * The socket server has been established. 1026 * CaseDescription: 1. Establish a socket client and connect with the Appspawn server 1027 * 2. Send the message and the message format is correct, the message type is APP_TYPE_DEFAULT 1028 */ 1029HWTEST_F(AppSpawnModuleTest, AppSpawn_HF_checkGids_001, TestSize.Level0) 1030{ 1031 HILOG_INFO(LOG_CORE, "AppSpawn_HF_checkGids_001 start"); 1032 1033 OHOS::AppSpawnModuleTest::AppSpawnTestCommander commander; 1034 AppSpawnReqMsgHandle reqHandle; 1035 AppSpawnResult result; 1036 commander.CreateMsg(reqHandle, defaultAppInfo1.c_str()); 1037 int ret = AppSpawnClientSendMsg(commander.GetClientHandle(), reqHandle, &result); 1038 EXPECT_EQ(0, ret); 1039 EXPECT_EQ(0, result.result); 1040 EXPECT_NE(0, result.pid); 1041 GTEST_LOG_(INFO) << "newPid :" << result.pid << "."; 1042 1043 std::vector<int32_t> gids = {1008}; // 1008 gids 1044 EXPECT_EQ(true, CheckGids(result.pid, gids)); 1045 1046 if (result.pid > 0) { 1047 EXPECT_EQ(0, kill(result.pid, SIGKILL)); 1048 result.pid = DEFAULT_PID; 1049 } 1050 HILOG_INFO(LOG_CORE, "AppSpawn_HF_setUid_001 end"); 1051} 1052 1053HWTEST_F(AppSpawnModuleTest, AppSpawn_HF_checkGids_002, TestSize.Level0) 1054{ 1055 HILOG_INFO(LOG_CORE, "AppSpawn_HF_checkGids_002 start"); 1056 1057 OHOS::AppSpawnModuleTest::AppSpawnTestCommander commander; 1058 AppSpawnReqMsgHandle reqHandle; 1059 AppSpawnResult result; 1060 commander.CreateMsg(reqHandle, defaultAppInfo3.c_str()); 1061 int ret = AppSpawnClientSendMsg(commander.GetClientHandle(), reqHandle, &result); 1062 EXPECT_EQ(0, ret); 1063 EXPECT_EQ(0, result.result); 1064 EXPECT_NE(0, result.pid); 1065 GTEST_LOG_(INFO) << "newPid :" << result.pid << "."; 1066 1067 std::vector<int32_t> gids = {20010045, 20010046}; // 20010045, 20010046 test gids 1068 EXPECT_EQ(true, CheckGids(result.pid, gids)); 1069 1070 if (result.pid > 0) { 1071 EXPECT_EQ(0, kill(result.pid, SIGKILL)); 1072 result.pid = DEFAULT_PID; 1073 } 1074 HILOG_INFO(LOG_CORE, "AppSpawn_HF_checkGids_002 end"); 1075} 1076 1077HWTEST_F(AppSpawnModuleTest, AppSpawn_HF_checkGids_003, TestSize.Level0) 1078{ 1079 HILOG_INFO(LOG_CORE, "AppSpawn_HF_checkGids_003 start"); 1080 1081 OHOS::AppSpawnModuleTest::AppSpawnTestCommander commander; 1082 AppSpawnReqMsgHandle reqHandle; 1083 AppSpawnResult result; 1084 commander.CreateMsg(reqHandle, defaultAppInfo3.c_str(), MSG_SPAWN_NATIVE_PROCESS); 1085 int ret = AppSpawnClientSendMsg(commander.GetClientHandle(), reqHandle, &result); 1086 EXPECT_EQ(0, ret); 1087 EXPECT_EQ(0, result.result); 1088 EXPECT_NE(0, result.pid); 1089 GTEST_LOG_(INFO) << "newPid :" << result.pid << "."; 1090 1091 std::vector<int32_t> gids = {20010045, 20010046}; // 20010045, 20010046 test gids 1092 EXPECT_EQ(true, CheckGids(result.pid, gids)); 1093 1094 if (result.pid > 0) { 1095 EXPECT_EQ(0, kill(result.pid, SIGKILL)); 1096 result.pid = DEFAULT_PID; 1097 } 1098 HILOG_INFO(LOG_CORE, "AppSpawn_HF_checkGids_003 end"); 1099} 1100 1101HWTEST_F(AppSpawnModuleTest, AppSpawn_HF_checkGids_004, TestSize.Level0) 1102{ 1103 HILOG_INFO(LOG_CORE, "AppSpawn_HF_checkGids_004 start"); 1104 OHOS::AppSpawnModuleTest::AppSpawnTestCommander commander(0); // for nwebspawn 1105 AppSpawnReqMsgHandle reqHandle; 1106 AppSpawnResult result; 1107 commander.CreateMsg(reqHandle, defaultWebInfo1.c_str()); 1108 int ret = AppSpawnClientSendMsg(commander.GetClientHandle(), reqHandle, &result); 1109 EXPECT_EQ(0, ret); 1110 EXPECT_EQ(0, result.result); 1111 EXPECT_NE(0, result.pid); 1112 GTEST_LOG_(INFO) << "newPid :" << result.pid << "."; 1113 1114 std::vector<int32_t> gids = {1097, 1098}; // 1097, 1098 test gids 1115 EXPECT_EQ(true, CheckGids(result.pid, gids)); 1116 1117 if (result.pid > 0) { 1118 EXPECT_EQ(0, kill(result.pid, SIGKILL)); 1119 result.pid = DEFAULT_PID; 1120 } 1121 HILOG_INFO(LOG_CORE, "AppSpawn_HF_checkGids_004 end"); 1122} 1123 1124HWTEST_F(AppSpawnModuleTest, AppSpawn_HF_checkGids_005, TestSize.Level0) 1125{ 1126 HILOG_INFO(LOG_CORE, "AppSpawn_HF_checkGids_005 start"); 1127 OHOS::AppSpawnModuleTest::AppSpawnTestCommander commander(0); // for nwebspawn 1128 AppSpawnReqMsgHandle reqHandle; 1129 AppSpawnResult result; 1130 commander.CreateMsg(reqHandle, defaultWebInfo1.c_str(), MSG_SPAWN_NATIVE_PROCESS); 1131 int ret = AppSpawnClientSendMsg(commander.GetClientHandle(), reqHandle, &result); 1132 EXPECT_EQ(0, ret); 1133 EXPECT_EQ(0, result.result); 1134 EXPECT_NE(0, result.pid); 1135 GTEST_LOG_(INFO) << "newPid :" << result.pid << "."; 1136 1137 std::vector<int32_t> gids = {1097, 1098}; // 1097, 1098 test gids 1138 EXPECT_EQ(true, CheckGids(result.pid, gids)); 1139 1140 if (result.pid > 0) { 1141 EXPECT_EQ(0, kill(result.pid, SIGKILL)); 1142 result.pid = DEFAULT_PID; 1143 } 1144 HILOG_INFO(LOG_CORE, "AppSpawn_HF_checkGids_003 end"); 1145} 1146 1147/* 1148 * Feature: AppSpawn 1149 * Function: setProcName 1150 * SubFunction: Set process name 1151 * FunctionPoints: Set process information . 1152 * EnvConditions: AppSpawn main process has started. 1153 * The socket server has been established. 1154 * CaseDescription: 1. Establish a socket client and connect with the Appspawn server 1155 * 2. Send the message and the message format is correct, the message type is APP_TYPE_DEFAULT 1156 */ 1157HWTEST_F(AppSpawnModuleTest, AppSpawn_HF_setProcName_001, TestSize.Level0) 1158{ 1159 HILOG_INFO(LOG_CORE, "AppSpawn_HF_setProcName_001 start"); 1160 1161 OHOS::AppSpawnModuleTest::AppSpawnTestCommander commander; 1162 AppSpawnReqMsgHandle reqHandle; 1163 AppSpawnResult result; 1164 commander.CreateMsg(reqHandle, defaultAppInfo1.c_str()); 1165 int ret = AppSpawnClientSendMsg(commander.GetClientHandle(), reqHandle, &result); 1166 EXPECT_EQ(0, ret); 1167 EXPECT_EQ(0, result.result); 1168 EXPECT_NE(0, result.pid); 1169 GTEST_LOG_(INFO) << "newPid :" << result.pid << "."; 1170 // Check new app proc name 1171 EXPECT_EQ(true, CheckProcName(result.pid, "com.example.myapplication")); 1172 1173 if (result.pid > 0) { 1174 EXPECT_EQ(0, kill(result.pid, SIGKILL)); 1175 result.pid = DEFAULT_PID; 1176 } 1177 HILOG_INFO(LOG_CORE, "AppSpawn_HF_setProcName_001 end"); 1178} 1179 1180/* 1181 * Feature: AppSpawn 1182 * Function: setProcName 1183 * SubFunction: Set process name 1184 * FunctionPoints: Set process information . 1185 * EnvConditions: AppSpawn main process has started. 1186 * The socket server has been established. 1187 * CaseDescription: 1. Establish a socket client and connect with the Appspawn server 1188 * 2. Send the message and the message format is correct, the message type is APP_TYPE_NATIVE 1189 */ 1190HWTEST_F(AppSpawnModuleTest, AppSpawn_HF_setProcName_002, TestSize.Level0) 1191{ 1192 HILOG_INFO(LOG_CORE, "AppSpawn_HF_setProcName_002 start"); 1193 OHOS::AppSpawnModuleTest::AppSpawnTestCommander commander; 1194 AppSpawnReqMsgHandle reqHandle; 1195 AppSpawnResult result; 1196 commander.CreateMsg(reqHandle, defaultAppInfo2.c_str()); // native start, can run 1197 int ret = AppSpawnClientSendMsg(commander.GetClientHandle(), reqHandle, &result); 1198 EXPECT_EQ(0, ret); 1199 EXPECT_EQ(0, result.result); 1200 EXPECT_NE(0, result.pid); 1201 GTEST_LOG_(INFO) << "newPid :" << result.pid << "."; 1202 HILOG_INFO(LOG_CORE, "AppSpawn_HF_setProcName_002 end"); 1203} 1204 1205HWTEST_F(AppSpawnModuleTest, AppSpawn_HF_setProcName_003, TestSize.Level0) 1206{ 1207 HILOG_INFO(LOG_CORE, "AppSpawn_HF_setProcName_003 start"); 1208 OHOS::AppSpawnModuleTest::AppSpawnTestCommander commander; 1209 AppSpawnReqMsgHandle reqHandle; 1210 AppSpawnResult result; 1211 commander.CreateMsg(reqHandle, defaultAppInfo2.c_str(), MSG_SPAWN_NATIVE_PROCESS); 1212 int ret = AppSpawnClientSendMsg(commander.GetClientHandle(), reqHandle, &result); 1213 EXPECT_EQ(0, ret); 1214 EXPECT_EQ(0, result.result); 1215 EXPECT_NE(0, result.pid); 1216 GTEST_LOG_(INFO) << "newPid :" << result.pid << "."; 1217 // do not check, native run fail 1218 // Failed to launch a native process with execvp: No such file or directory 1219 if (result.pid > 0) { 1220 EXPECT_EQ(0, kill(result.pid, SIGKILL)); 1221 result.pid = DEFAULT_PID; 1222 } 1223 HILOG_INFO(LOG_CORE, "AppSpawn_HF_setProcName_003 end"); 1224} 1225 1226HWTEST_F(AppSpawnModuleTest, AppSpawn_HF_setProcName_004, TestSize.Level0) 1227{ 1228 HILOG_INFO(LOG_CORE, "AppSpawn_HF_setProcName_004 start"); 1229 OHOS::AppSpawnModuleTest::AppSpawnTestCommander commander(0); // for nwebspawn 1230 AppSpawnReqMsgHandle reqHandle; 1231 AppSpawnResult result; 1232 commander.CreateMsg(reqHandle, defaultWebInfo1.c_str()); 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 // Check new app proc name 1239 EXPECT_EQ(true, CheckProcName(result.pid, "com.example.myapplication")); 1240 1241 if (result.pid > 0) { 1242 EXPECT_EQ(0, kill(result.pid, SIGKILL)); 1243 result.pid = DEFAULT_PID; 1244 } 1245 HILOG_INFO(LOG_CORE, "AppSpawn_HF_setProcName_004 end"); 1246} 1247 1248HWTEST_F(AppSpawnModuleTest, AppSpawn_HF_setProcName_005, TestSize.Level0) 1249{ 1250 HILOG_INFO(LOG_CORE, "AppSpawn_HF_setProcName_005 start"); 1251 OHOS::AppSpawnModuleTest::AppSpawnTestCommander commander(0); // for nwebspawn 1252 AppSpawnReqMsgHandle reqHandle; 1253 AppSpawnResult result; 1254 commander.CreateMsg(reqHandle, defaultWebInfo1.c_str(), MSG_SPAWN_NATIVE_PROCESS); 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 EXPECT_EQ(0, kill(result.pid, SIGKILL)); 1265 result.pid = DEFAULT_PID; 1266 } 1267 HILOG_INFO(LOG_CORE, "AppSpawn_HF_setProcName_005 end"); 1268} 1269 1270/* 1271 * Feature: AppSpawn 1272 * Function: recycleProc 1273 * SubFunction: Recycling process 1274 * FunctionPoints: Recycling zombie processes. 1275 * EnvConditions: Start a js ability 1276 * CaseDescription: 1. Use the command kill to kill the process pid of the ability 1277 */ 1278HWTEST_F(AppSpawnModuleTest, AppSpawn_HF_recycleProc_001, TestSize.Level0) 1279{ 1280 HILOG_INFO(LOG_CORE, "AppSpawn_HF_recycleProc_001 start"); 1281 OHOS::AppSpawnModuleTest::AppSpawnTestCommander commander; 1282 AppSpawnReqMsgHandle reqHandle; 1283 AppSpawnResult result; 1284 commander.CreateMsg(reqHandle, defaultAppInfo2.c_str()); 1285 int ret = AppSpawnClientSendMsg(commander.GetClientHandle(), reqHandle, &result); 1286 EXPECT_EQ(0, ret); 1287 EXPECT_EQ(0, result.result); 1288 EXPECT_NE(0, result.pid); 1289 GTEST_LOG_(INFO) << "newPid :" << result.pid << "."; 1290 if (result.pid > 0) { 1291 EXPECT_EQ(0, kill(result.pid, SIGKILL)); 1292 } 1293 // Check Process Is Destroyed 1294 EXPECT_EQ(true, CheckProcessIsDestroyed(result.pid)); 1295 HILOG_INFO(LOG_CORE, "AppSpawn_HF_recycleProc_001 end"); 1296} 1297 1298HWTEST_F(AppSpawnModuleTest, AppSpawn_HF_recycleProc_002, TestSize.Level0) 1299{ 1300 HILOG_INFO(LOG_CORE, "AppSpawn_HF_recycleProc_002 start"); 1301 OHOS::AppSpawnModuleTest::AppSpawnTestCommander commander; 1302 AppSpawnReqMsgHandle reqHandle; 1303 AppSpawnResult result; 1304 commander.CreateMsg(reqHandle, defaultAppInfo1.c_str()); 1305 int ret = AppSpawnClientSendMsg(commander.GetClientHandle(), reqHandle, &result); 1306 EXPECT_EQ(0, ret); 1307 EXPECT_EQ(0, result.result); 1308 EXPECT_NE(0, result.pid); 1309 GTEST_LOG_(INFO) << "newPid :" << result.pid << "."; 1310 if (result.pid > 0) { 1311 EXPECT_EQ(0, kill(result.pid, SIGKILL)); 1312 } 1313 // Check Process Is Destroyed 1314 EXPECT_EQ(true, CheckProcessIsDestroyed(result.pid)); 1315 HILOG_INFO(LOG_CORE, "AppSpawn_HF_recycleProc_002 end"); 1316} 1317 1318HWTEST_F(AppSpawnModuleTest, AppSpawn_HF_recycleProc_003, TestSize.Level0) 1319{ 1320 HILOG_INFO(LOG_CORE, "AppSpawn_HF_recycleProc_003 start"); 1321 OHOS::AppSpawnModuleTest::AppSpawnTestCommander commander; 1322 AppSpawnReqMsgHandle reqHandle; 1323 AppSpawnResult result; 1324 commander.CreateMsg(reqHandle, defaultAppInfo1.c_str()); 1325 int ret = AppSpawnClientSendMsg(commander.GetClientHandle(), reqHandle, &result); 1326 EXPECT_EQ(0, ret); 1327 EXPECT_EQ(0, result.result); 1328 EXPECT_NE(0, result.pid); 1329 GTEST_LOG_(INFO) << "newPid :" << result.pid << "."; 1330 if (result.pid > 0) { 1331 EXPECT_EQ(0, kill(result.pid, SIGKILL)); 1332 } 1333 // Check Process Is Destroyed 1334 EXPECT_EQ(true, CheckProcessIsDestroyed(result.pid)); 1335 HILOG_INFO(LOG_CORE, "AppSpawn_HF_recycleProc_003 end"); 1336} 1337 1338HWTEST_F(AppSpawnModuleTest, AppSpawn_HF_recycleProc_006, TestSize.Level0) 1339{ 1340 HILOG_INFO(LOG_CORE, "AppSpawn_HF_recycleProc_006 start"); 1341 OHOS::AppSpawnModuleTest::AppSpawnTestCommander commander; 1342 AppSpawnReqMsgHandle reqHandle; 1343 AppSpawnResult result; 1344 commander.CreateMsg(reqHandle, defaultAppInfo1.c_str(), MSG_SPAWN_NATIVE_PROCESS); 1345 int ret = AppSpawnClientSendMsg(commander.GetClientHandle(), reqHandle, &result); 1346 EXPECT_EQ(0, ret); 1347 EXPECT_EQ(0, result.result); 1348 EXPECT_NE(0, result.pid); 1349 GTEST_LOG_(INFO) << "newPid :" << result.pid << "."; 1350 if (result.pid > 0) { 1351 EXPECT_EQ(0, kill(result.pid, SIGKILL)); 1352 } 1353 1354 // Check Process Is Destroyed 1355 EXPECT_EQ(true, CheckProcessIsDestroyed(result.pid)); 1356 HILOG_INFO(LOG_CORE, "AppSpawn_HF_recycleProc_006 end"); 1357} 1358 1359HWTEST_F(AppSpawnModuleTest, AppSpawn_HF_recycleProc_007, TestSize.Level0) 1360{ 1361 HILOG_INFO(LOG_CORE, "AppSpawn_HF_recycleProc_007 start"); 1362 OHOS::AppSpawnModuleTest::AppSpawnTestCommander commander(0); 1363 AppSpawnReqMsgHandle reqHandle; 1364 AppSpawnResult result; 1365 commander.CreateMsg(reqHandle, defaultAppInfo1.c_str()); 1366 int ret = AppSpawnClientSendMsg(commander.GetClientHandle(), reqHandle, &result); 1367 EXPECT_EQ(0, ret); 1368 EXPECT_EQ(0, result.result); 1369 EXPECT_NE(0, result.pid); 1370 GTEST_LOG_(INFO) << "newPid :" << result.pid << "."; 1371 if (result.pid > 0) { 1372 EXPECT_EQ(0, kill(result.pid, SIGKILL)); 1373 } 1374 1375 // Check Process Is Destroyed 1376 EXPECT_EQ(true, CheckProcessIsDestroyed(result.pid)); 1377 HILOG_INFO(LOG_CORE, "AppSpawn_HF_recycleProc_007 end"); 1378} 1379 1380HWTEST_F(AppSpawnModuleTest, AppSpawn_HF_recycleProc_008, TestSize.Level0) 1381{ 1382 HILOG_INFO(LOG_CORE, "AppSpawn_HF_recycleProc_008 start"); 1383 OHOS::AppSpawnModuleTest::AppSpawnTestCommander commander(0); 1384 AppSpawnReqMsgHandle reqHandle; 1385 AppSpawnResult result; 1386 std::random_device rd; 1387 std::mt19937 gen(rd()); 1388 std::uniform_int_distribution<> dis(0, 62); 1389 printf("number: %d\n", dis(gen)); 1390 AppFlagsIndex number = static_cast<AppFlagsIndex>(dis(gen)); 1391 1392 commander.CreateMsg(reqHandle, defaultAppInfo1.c_str(), MSG_SPAWN_NATIVE_PROCESS); 1393 int ret = AppSpawnReqMsgSetAppFlag(reqHandle, number); 1394 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 EXPECT_EQ(0, kill(result.pid, SIGKILL)); 1401 } 1402 // Check Process Is Destroyed 1403 EXPECT_EQ(true, CheckProcessIsDestroyed(result.pid)); 1404 HILOG_INFO(LOG_CORE, "AppSpawn_HF_recycleProc_008 end"); 1405} 1406 1407/** 1408 * @brief 1409 * no internet permission 1410 * 1411 */ 1412HWTEST_F(AppSpawnModuleTest, AppSpawn_Invalid_Msg_001, TestSize.Level0) 1413{ 1414 HILOG_INFO(LOG_CORE, "AppSpawn_Invalid_Msg_001 start"); 1415 OHOS::AppSpawnModuleTest::AppSpawnTestCommander commander; 1416 AppSpawnReqMsgHandle reqHandle; 1417 AppSpawnResult result; 1418 commander.CreateMsg(reqHandle, defaultAppInfoNoInternetPermission.c_str()); 1419 int ret = AppSpawnClientSendMsg(commander.GetClientHandle(), reqHandle, &result); 1420 EXPECT_EQ(0, ret); 1421 EXPECT_EQ(0, result.result); 1422 if (result.pid > 0) { 1423 EXPECT_EQ(0, kill(result.pid, SIGKILL)); 1424 } 1425 HILOG_INFO(LOG_CORE, "AppSpawn_Invalid_Msg_001 end"); 1426} 1427 1428/** 1429 * @brief 1430 * no dac 1431 * 1432 */ 1433HWTEST_F(AppSpawnModuleTest, AppSpawn_Invalid_Msg_002, TestSize.Level0) 1434{ 1435 HILOG_INFO(LOG_CORE, "AppSpawn_Invalid_Msg_001 start"); 1436 OHOS::AppSpawnModuleTest::AppSpawnTestCommander commander; 1437 AppSpawnReqMsgHandle reqHandle; 1438 AppSpawnResult result; 1439 commander.CreateMsg(reqHandle, defaultAppInfoNoDac.c_str()); 1440 int ret = AppSpawnClientSendMsg(commander.GetClientHandle(), reqHandle, &result); 1441 EXPECT_EQ(0, ret); 1442 EXPECT_EQ(APPSPAWN_MSG_INVALID, result.result); 1443 HILOG_INFO(LOG_CORE, "AppSpawn_Invalid_Msg_001 end"); 1444} 1445 1446/** 1447 * @brief 1448 * no access token 1449 * 1450 */ 1451HWTEST_F(AppSpawnModuleTest, AppSpawn_Invalid_Msg_003, TestSize.Level0) 1452{ 1453 HILOG_INFO(LOG_CORE, "AppSpawn_Invalid_Msg_003 start"); 1454 1455 OHOS::AppSpawnModuleTest::AppSpawnTestCommander commander; 1456 AppSpawnReqMsgHandle reqHandle; 1457 AppSpawnResult result; 1458 commander.CreateMsg(reqHandle, defaultAppInfoNoToken.c_str()); 1459 int ret = AppSpawnClientSendMsg(commander.GetClientHandle(), reqHandle, &result); 1460 EXPECT_EQ(0, ret); 1461 EXPECT_EQ(APPSPAWN_MSG_INVALID, result.result); 1462 1463 HILOG_INFO(LOG_CORE, "AppSpawn_Invalid_Msg_003 end"); 1464} 1465 1466/** 1467 * @brief 1468 * no bundle info 1469 * 1470 */ 1471HWTEST_F(AppSpawnModuleTest, AppSpawn_Invalid_Msg_004, TestSize.Level0) 1472{ 1473 HILOG_INFO(LOG_CORE, "AppSpawn_Invalid_Msg_004 start"); 1474 1475 OHOS::AppSpawnModuleTest::AppSpawnTestCommander commander; 1476 AppSpawnReqMsgHandle reqHandle; 1477 AppSpawnResult result; 1478 commander.CreateMsg(reqHandle, defaultAppInfoNoBundleInfo.c_str()); 1479 int ret = AppSpawnClientSendMsg(commander.GetClientHandle(), reqHandle, &result); 1480 EXPECT_EQ(0, ret); 1481 EXPECT_EQ(APPSPAWN_MSG_INVALID, result.result); 1482 HILOG_INFO(LOG_CORE, "AppSpawn_Invalid_Msg_004 end"); 1483} 1484 1485/** 1486 * @brief 1487 * no internet permission 1488 * 1489 */ 1490HWTEST_F(AppSpawnModuleTest, AppSpawn_Invalid_Msg_005, TestSize.Level0) 1491{ 1492 HILOG_INFO(LOG_CORE, "AppSpawn_Invalid_Msg_005 start"); 1493 OHOS::AppSpawnModuleTest::AppSpawnTestCommander commander(0); // nwebspawn 1494 AppSpawnReqMsgHandle reqHandle; 1495 AppSpawnResult result; 1496 commander.CreateMsg(reqHandle, defaultAppInfoNoInternetPermission.c_str()); 1497 int ret = AppSpawnClientSendMsg(commander.GetClientHandle(), reqHandle, &result); 1498 EXPECT_EQ(0, ret); 1499 EXPECT_EQ(0, result.result); 1500 if (result.pid > 0) { 1501 EXPECT_EQ(0, kill(result.pid, SIGKILL)); 1502 } 1503 HILOG_INFO(LOG_CORE, "AppSpawn_Invalid_Msg_005 end"); 1504} 1505 1506/** 1507 * @brief 1508 * no dac 1509 * 1510 */ 1511HWTEST_F(AppSpawnModuleTest, AppSpawn_Invalid_Msg_006, TestSize.Level0) 1512{ 1513 HILOG_INFO(LOG_CORE, "AppSpawn_Invalid_Msg_006 start"); 1514 OHOS::AppSpawnModuleTest::AppSpawnTestCommander commander(0); // nwebspawn 1515 AppSpawnReqMsgHandle reqHandle; 1516 AppSpawnResult result; 1517 commander.CreateMsg(reqHandle, defaultAppInfoNoDac.c_str()); 1518 int ret = AppSpawnClientSendMsg(commander.GetClientHandle(), reqHandle, &result); 1519 EXPECT_EQ(0, ret); 1520 EXPECT_EQ(APPSPAWN_MSG_INVALID, result.result); 1521 HILOG_INFO(LOG_CORE, "AppSpawn_Invalid_Msg_006 end"); 1522} 1523 1524/** 1525 * @brief 1526 * no access token 1527 * 1528 */ 1529HWTEST_F(AppSpawnModuleTest, AppSpawn_Invalid_Msg_007, TestSize.Level0) 1530{ 1531 HILOG_INFO(LOG_CORE, "AppSpawn_Invalid_Msg_007 start"); 1532 OHOS::AppSpawnModuleTest::AppSpawnTestCommander commander(0); // nwebspawn 1533 AppSpawnReqMsgHandle reqHandle; 1534 AppSpawnResult result; 1535 commander.CreateMsg(reqHandle, defaultAppInfoNoToken.c_str()); 1536 int ret = AppSpawnClientSendMsg(commander.GetClientHandle(), reqHandle, &result); 1537 EXPECT_EQ(0, ret); 1538 EXPECT_EQ(APPSPAWN_MSG_INVALID, result.result); 1539 1540 HILOG_INFO(LOG_CORE, "AppSpawn_Invalid_Msg_007 end"); 1541} 1542 1543/** 1544 * @brief 1545 * no bundle info 1546 * 1547 */ 1548HWTEST_F(AppSpawnModuleTest, AppSpawn_Invalid_Msg_008, TestSize.Level0) 1549{ 1550 HILOG_INFO(LOG_CORE, "AppSpawn_Invalid_Msg_008 start"); 1551 OHOS::AppSpawnModuleTest::AppSpawnTestCommander commander(0); // nwebspawn 1552 AppSpawnReqMsgHandle reqHandle; 1553 AppSpawnResult result; 1554 commander.CreateMsg(reqHandle, defaultAppInfoNoBundleInfo.c_str()); 1555 int ret = AppSpawnClientSendMsg(commander.GetClientHandle(), reqHandle, &result); 1556 EXPECT_EQ(0, ret); 1557 EXPECT_EQ(APPSPAWN_MSG_INVALID, result.result); 1558 HILOG_INFO(LOG_CORE, "AppSpawn_Invalid_Msg_008 end"); 1559} 1560 1561/** 1562 * @brief 1563 * no internet permission 1564 * 1565 */ 1566HWTEST_F(AppSpawnModuleTest, AppSpawn_Invalid_Msg_009, TestSize.Level0) 1567{ 1568 HILOG_INFO(LOG_CORE, "AppSpawn_Invalid_Msg_009 start"); 1569 OHOS::AppSpawnModuleTest::AppSpawnTestCommander commander; 1570 AppSpawnReqMsgHandle reqHandle; 1571 AppSpawnResult result; 1572 commander.CreateMsg(reqHandle, defaultAppInfoNoInternetPermission.c_str(), MSG_SPAWN_NATIVE_PROCESS); 1573 int ret = AppSpawnClientSendMsg(commander.GetClientHandle(), reqHandle, &result); 1574 EXPECT_EQ(0, ret); 1575 EXPECT_EQ(0, result.result); 1576 if (result.pid > 0) { 1577 EXPECT_EQ(0, kill(result.pid, SIGKILL)); 1578 } 1579 HILOG_INFO(LOG_CORE, "AppSpawn_Invalid_Msg_009 end"); 1580} 1581 1582/** 1583 * @brief 1584 * no dac 1585 * 1586 */ 1587HWTEST_F(AppSpawnModuleTest, AppSpawn_Invalid_Msg_010, TestSize.Level0) 1588{ 1589 HILOG_INFO(LOG_CORE, "AppSpawn_Invalid_Msg_010 start"); 1590 OHOS::AppSpawnModuleTest::AppSpawnTestCommander commander; 1591 AppSpawnReqMsgHandle reqHandle; 1592 AppSpawnResult result; 1593 commander.CreateMsg(reqHandle, defaultAppInfoNoDac.c_str(), MSG_SPAWN_NATIVE_PROCESS); 1594 int ret = AppSpawnClientSendMsg(commander.GetClientHandle(), reqHandle, &result); 1595 EXPECT_EQ(0, ret); 1596 EXPECT_EQ(APPSPAWN_MSG_INVALID, result.result); 1597 HILOG_INFO(LOG_CORE, "AppSpawn_Invalid_Msg_010 end"); 1598} 1599 1600/** 1601 * @brief 1602 * no access token 1603 * 1604 */ 1605HWTEST_F(AppSpawnModuleTest, AppSpawn_Invalid_Msg_011, TestSize.Level0) 1606{ 1607 HILOG_INFO(LOG_CORE, "AppSpawn_Invalid_Msg_011 start"); 1608 1609 OHOS::AppSpawnModuleTest::AppSpawnTestCommander commander; 1610 AppSpawnReqMsgHandle reqHandle; 1611 AppSpawnResult result; 1612 commander.CreateMsg(reqHandle, defaultAppInfoNoToken.c_str(), MSG_SPAWN_NATIVE_PROCESS); 1613 int ret = AppSpawnClientSendMsg(commander.GetClientHandle(), reqHandle, &result); 1614 EXPECT_EQ(0, ret); 1615 EXPECT_EQ(APPSPAWN_MSG_INVALID, result.result); 1616 1617 HILOG_INFO(LOG_CORE, "AppSpawn_Invalid_Msg_011 end"); 1618} 1619 1620/** 1621 * @brief 1622 * no bundle info 1623 * 1624 */ 1625HWTEST_F(AppSpawnModuleTest, AppSpawn_Invalid_Msg_012, TestSize.Level0) 1626{ 1627 HILOG_INFO(LOG_CORE, "AppSpawn_Invalid_Msg_012 start"); 1628 1629 OHOS::AppSpawnModuleTest::AppSpawnTestCommander commander; 1630 AppSpawnReqMsgHandle reqHandle; 1631 AppSpawnResult result; 1632 commander.CreateMsg(reqHandle, defaultAppInfoNoBundleInfo.c_str(), MSG_SPAWN_NATIVE_PROCESS); 1633 int ret = AppSpawnClientSendMsg(commander.GetClientHandle(), reqHandle, &result); 1634 EXPECT_EQ(0, ret); 1635 EXPECT_EQ(APPSPAWN_MSG_INVALID, result.result); 1636 HILOG_INFO(LOG_CORE, "AppSpawn_Invalid_Msg_012 end"); 1637} 1638 1639/** 1640 * @brief app flags 1641 * 1642 */ 1643HWTEST_F(AppSpawnModuleTest, AppSpawn_App_Flags_001, TestSize.Level0) 1644{ 1645 HILOG_INFO(LOG_CORE, "AppSpawn_App_Flags_001 start"); 1646 1647 OHOS::AppSpawnModuleTest::AppSpawnTestCommander commander; 1648 AppSpawnReqMsgHandle reqHandle; 1649 AppSpawnResult result; 1650 std::random_device rd; 1651 std::mt19937 gen(rd()); 1652 std::uniform_int_distribution<> dis(0, 62); 1653 printf("number: %d\n", dis(gen)); 1654 AppFlagsIndex number = static_cast<AppFlagsIndex>(dis(gen)); 1655 commander.CreateMsg(reqHandle, defaultAppInfo1.c_str()); 1656 APPSPAWN_LOGI("number: %{public}d", number); 1657 int ret = AppSpawnReqMsgSetAppFlag(reqHandle, number); 1658 ret = AppSpawnClientSendMsg(commander.GetClientHandle(), reqHandle, &result); 1659 EXPECT_EQ(0, ret); 1660 EXPECT_EQ(0, result.result); 1661 if (result.pid > 0) { 1662 EXPECT_EQ(0, kill(result.pid, SIGKILL)); 1663 result.pid = DEFAULT_PID; 1664 } 1665 HILOG_INFO(LOG_CORE, "AppSpawn_App_Flags_001 end"); 1666} 1667 1668/** 1669 * @brief app flags 1670 * 1671 */ 1672HWTEST_F(AppSpawnModuleTest, AppSpawn_App_Flags_002, TestSize.Level0) 1673{ 1674 HILOG_INFO(LOG_CORE, "AppSpawn_App_Flags_002 start"); 1675 1676 OHOS::AppSpawnModuleTest::AppSpawnTestCommander commander; 1677 AppSpawnReqMsgHandle reqHandle; 1678 AppSpawnResult result; 1679 std::random_device rd; 1680 std::mt19937 gen(rd()); 1681 std::uniform_int_distribution<> dis(0, 62); 1682 printf("number: %d\n", dis(gen)); 1683 AppFlagsIndex number = static_cast<AppFlagsIndex>(dis(gen)); 1684 commander.CreateMsg(reqHandle, defaultAppInfo1.c_str(), MSG_SPAWN_NATIVE_PROCESS); 1685 APPSPAWN_LOGI("number: %{public}d", number); 1686 int ret = AppSpawnReqMsgSetAppFlag(reqHandle, number); 1687 ret = AppSpawnClientSendMsg(commander.GetClientHandle(), reqHandle, &result); 1688 EXPECT_EQ(0, ret); 1689 EXPECT_EQ(0, result.result); 1690 if (result.pid > 0) { 1691 EXPECT_EQ(0, kill(result.pid, SIGKILL)); 1692 result.pid = DEFAULT_PID; 1693 } 1694 HILOG_INFO(LOG_CORE, "AppSpawn_App_Flags_002 end"); 1695} 1696 1697/** 1698 * @brief app flags 1699 * 1700 */ 1701HWTEST_F(AppSpawnModuleTest, AppSpawn_App_Flags_003, TestSize.Level0) 1702{ 1703 HILOG_INFO(LOG_CORE, "AppSpawn_App_Flags_003 start"); 1704 OHOS::AppSpawnModuleTest::AppSpawnTestCommander commander(0); // nwebspawn 1705 AppSpawnReqMsgHandle reqHandle; 1706 AppSpawnResult result; 1707 std::random_device rd; 1708 std::mt19937 gen(rd()); 1709 std::uniform_int_distribution<> dis(0, 62); 1710 printf("number: %d\n", dis(gen)); 1711 AppFlagsIndex number = static_cast<AppFlagsIndex>(dis(gen)); 1712 commander.CreateMsg(reqHandle, defaultAppInfo1.c_str()); 1713 APPSPAWN_LOGI("number: %{public}d", number); 1714 int ret = AppSpawnReqMsgSetAppFlag(reqHandle, number); 1715 ret = AppSpawnClientSendMsg(commander.GetClientHandle(), reqHandle, &result); 1716 EXPECT_EQ(0, ret); 1717 EXPECT_EQ(0, result.result); 1718 if (result.pid > 0) { 1719 EXPECT_EQ(0, kill(result.pid, SIGKILL)); 1720 result.pid = DEFAULT_PID; 1721 } 1722 HILOG_INFO(LOG_CORE, "AppSpawn_App_Flags_003 end"); 1723} 1724 1725/** 1726 * @brief app flags 1727 * 1728 */ 1729HWTEST_F(AppSpawnModuleTest, AppSpawn_App_Flags_004, TestSize.Level0) 1730{ 1731 HILOG_INFO(LOG_CORE, "AppSpawn_App_Flags_004 start"); 1732 OHOS::AppSpawnModuleTest::AppSpawnTestCommander commander(0); // nwebspawn 1733 AppSpawnReqMsgHandle reqHandle; 1734 AppSpawnResult result; 1735 std::random_device rd; 1736 std::mt19937 gen(rd()); 1737 std::uniform_int_distribution<> dis(0, 62); 1738 printf("number: %d\n", dis(gen)); 1739 AppFlagsIndex number = static_cast<AppFlagsIndex>(dis(gen)); 1740 commander.CreateMsg(reqHandle, defaultAppInfo1.c_str(), MSG_SPAWN_NATIVE_PROCESS); 1741 APPSPAWN_LOGI("number: %{public}d", number); 1742 int ret = AppSpawnReqMsgSetAppFlag(reqHandle, number); 1743 ret = AppSpawnClientSendMsg(commander.GetClientHandle(), reqHandle, &result); 1744 EXPECT_EQ(0, ret); 1745 EXPECT_EQ(0, result.result); 1746 if (result.pid > 0) { 1747 EXPECT_EQ(0, kill(result.pid, SIGKILL)); 1748 result.pid = DEFAULT_PID; 1749 } 1750 HILOG_INFO(LOG_CORE, "AppSpawn_App_Flags_004 end"); 1751} 1752 1753HWTEST_F(AppSpawnModuleTest, AppSpawn_Msg_001, TestSize.Level0) 1754{ 1755 HILOG_INFO(LOG_CORE, "AppSpawn_Msg_001 start"); 1756 1757 static const std::string dumpInfo = "{ \ 1758 \"msg-type\": \"MSG_DUMP\", \ 1759 \"msg-flags\": [ 13 ], \ 1760 \"process-name\" : \"com.example.myapplication\" \ 1761 }"; 1762 1763 OHOS::AppSpawnModuleTest::AppSpawnTestCommander commander; 1764 AppSpawnReqMsgHandle reqHandle; 1765 AppSpawnResult result; 1766 commander.CreateMsg(reqHandle, dumpInfo.c_str()); 1767 int ret = AppSpawnClientSendMsg(commander.GetClientHandle(), reqHandle, &result); 1768 EXPECT_EQ(0, ret); 1769 HILOG_INFO(LOG_CORE, "AppSpawn_Msg_001 end"); 1770} 1771 1772HWTEST_F(AppSpawnModuleTest, AppSpawn_Msg_002, TestSize.Level0) 1773{ 1774 HILOG_INFO(LOG_CORE, "AppSpawn_Msg_002 start"); 1775 static const std::string appInfo = "{ \ 1776 \"msg-type\": \"MSG_GET_RENDER_TERMINATION_STATUS\", \ 1777 \"msg-flags\": [ 13 ], \ 1778 \"process-name\" : \"com.example.myapplication\" \ 1779 }"; 1780 1781 OHOS::AppSpawnModuleTest::AppSpawnTestCommander commander; 1782 AppSpawnReqMsgHandle reqHandle; 1783 AppSpawnResult result; 1784 commander.CreateMsg(reqHandle, defaultAppInfo1.c_str()); 1785 int ret = AppSpawnClientSendMsg(commander.GetClientHandle(), reqHandle, &result); 1786 EXPECT_EQ(0, ret); 1787 EXPECT_EQ(0, result.result); 1788 if (result.pid > 0) { 1789 EXPECT_EQ(0, kill(result.pid, SIGKILL)); 1790 result.pid = DEFAULT_PID; 1791 } 1792 commander.CreateMsg(reqHandle, appInfo.c_str(), MSG_GET_RENDER_TERMINATION_STATUS); 1793 ret = AppSpawnClientSendMsg(commander.GetClientHandle(), reqHandle, &result); 1794 EXPECT_EQ(0, ret); 1795 EXPECT_NE(0, result.result); 1796 HILOG_INFO(LOG_CORE, "AppSpawn_Msg_002 end"); 1797} 1798 1799HWTEST_F(AppSpawnModuleTest, AppSpawn_Msg_003, TestSize.Level0) 1800{ 1801 HILOG_INFO(LOG_CORE, "AppSpawn_Msg_003 start"); 1802 static const std::string appInfo = "{ \ 1803 \"msg-type\": \"MSG_GET_RENDER_TERMINATION_STATUS\", \ 1804 \"msg-flags\": [ 13 ], \ 1805 \"process-name\" : \"com.example.myapplication\" \ 1806 }"; 1807 1808 OHOS::AppSpawnModuleTest::AppSpawnTestCommander commander(0); // nwebspawn 1809 AppSpawnReqMsgHandle reqHandle; 1810 AppSpawnResult result; 1811 commander.CreateMsg(reqHandle, defaultAppInfo1.c_str()); 1812 int ret = AppSpawnClientSendMsg(commander.GetClientHandle(), reqHandle, &result); 1813 EXPECT_EQ(0, ret); 1814 EXPECT_EQ(0, result.result); 1815 if (result.pid > 0) { 1816 EXPECT_EQ(0, kill(result.pid, SIGKILL)); 1817 result.pid = DEFAULT_PID; 1818 } 1819 commander.CreateMsg(reqHandle, appInfo.c_str(), MSG_GET_RENDER_TERMINATION_STATUS); 1820 ret = AppSpawnClientSendMsg(commander.GetClientHandle(), reqHandle, &result); 1821 EXPECT_EQ(0, ret); 1822 EXPECT_EQ(0, result.result); 1823 HILOG_INFO(LOG_CORE, "AppSpawn_Msg_003 end"); 1824} 1825 1826HWTEST_F(AppSpawnModuleTest, AppSpawn_Msg_004, TestSize.Level0) 1827{ 1828 HILOG_INFO(LOG_CORE, "AppSpawn_Msg_004 start"); 1829 static const std::string appInfo = "{ \ 1830 \"msg-type\": \"MSG_GET_RENDER_TERMINATION_STATUS\", \ 1831 \"msg-flags\": [ 13 ], \ 1832 \"process-name\" : \"com.example.myapplication\" \ 1833 }"; 1834 1835 OHOS::AppSpawnModuleTest::AppSpawnTestCommander commander(0); // nwebspawn 1836 AppSpawnReqMsgHandle reqHandle; 1837 AppSpawnResult result; 1838 commander.CreateMsg(reqHandle, defaultAppInfo1.c_str()); 1839 int ret = AppSpawnClientSendMsg(commander.GetClientHandle(), reqHandle, &result); 1840 EXPECT_EQ(0, ret); 1841 EXPECT_EQ(0, result.result); 1842 commander.CreateMsg(reqHandle, appInfo.c_str(), MSG_GET_RENDER_TERMINATION_STATUS); 1843 ret = AppSpawnClientSendMsg(commander.GetClientHandle(), reqHandle, &result); 1844 EXPECT_EQ(0, ret); 1845 EXPECT_EQ(0, result.result); 1846 if (result.pid > 0) { 1847 EXPECT_EQ(0, kill(result.pid, SIGKILL)); 1848 result.pid = DEFAULT_PID; 1849 } 1850 HILOG_INFO(LOG_CORE, "AppSpawn_Msg_004 end"); 1851} 1852} // namespace AppSpawn 1853} // namespace OHOS 1854