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 16 #include <cerrno> 17 #include <cstdlib> 18 #include <cstring> 19 #include <memory> 20 #include <string> 21 #include <unistd.h> 22 #include <gtest/gtest.h> 23 #include <sys/stat.h> 24 #include <sys/types.h> 25 #include <cJSON.h> 26 27 #include "appspawn_modulemgr.h" 28 #include "appspawn_server.h" 29 #include "appspawn_manager.h" 30 #include "appspawn_adapter.h" 31 #include "appspawn.h" 32 #include "appspawn_hook.h" 33 #include "appspawn_permission.h" 34 #include "appspawn_hisysevent.h" 35 #include "app_spawn_stub.h" 36 #include "app_spawn_test_helper.h" 37 #include "securec.h" 38 39 using namespace testing; 40 using namespace testing::ext; 41 using namespace OHOS; 42 43 APPSPAWN_STATIC int BuildFdInfoMap(const AppSpawnMsgNode *message, std::map<std::string, int> &fdMap, int isColdRun); 44 APPSPAWN_STATIC void LoadExtendCJLib(void); 45 APPSPAWN_STATIC int PreLoadAppSpawn(AppSpawnMgr *content); 46 APPSPAWN_STATIC int RunChildByRenderCmd(const AppSpawnMgr *content, const AppSpawningCtx *property); 47 48 namespace OHOS { 49 static AppSpawnTestHelper g_testHelper; 50 class AppSpawnCommonTest : public testing::Test { 51 public: SetUpTestCase()52 static void SetUpTestCase() {} TearDownTestCase()53 static void TearDownTestCase() {} SetUp()54 void SetUp() {} TearDown()55 void TearDown() {} 56 }; 57 58 HWTEST_F(AppSpawnCommonTest, App_Spawn_Common_001, TestSize.Level0) 59 { 60 int ret = SetSelinuxConNweb(nullptr, nullptr); 61 EXPECT_EQ(ret, 0); 62 } 63 64 HWTEST_F(AppSpawnCommonTest, App_Spawn_Common_002, TestSize.Level0) 65 { 66 int ret = SetAppAccessToken(nullptr, nullptr); 67 EXPECT_NE(ret, 0); 68 } 69 70 HWTEST_F(AppSpawnCommonTest, App_Spawn_Common_003, TestSize.Level0) 71 { 72 AppSpawnClientHandle clientHandle = nullptr; 73 AppSpawnReqMsgHandle reqHandle = 0; 74 AppSpawningCtx *property = nullptr; 75 AppSpawnMgr *mgr = nullptr; 76 int ret = -1; 77 do { 78 mgr = CreateAppSpawnMgr(MODE_FOR_NWEB_SPAWN); 79 EXPECT_EQ(mgr != nullptr, 1); 80 // create msg 81 ret = AppSpawnClientInit(NWEBSPAWN_SERVER_NAME, &clientHandle); 82 APPSPAWN_CHECK(ret == 0, break, "Failed to create reqMgr %{public}s", APPSPAWN_SERVER_NAME); 83 reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_SPAWN_NATIVE_PROCESS, 0); 84 APPSPAWN_CHECK(reqHandle != INVALID_REQ_HANDLE, break, 85 "Failed to create req %{public}s", APPSPAWN_SERVER_NAME); 86 property = g_testHelper.GetAppProperty(clientHandle, reqHandle); 87 property->client.flags |= APP_DEVELOPER_MODE; 88 APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break); 89 ret = SetSelinuxCon(mgr, property); 90 } while (0); 91 DeleteAppSpawningCtx(property); 92 AppSpawnClientDestroy(clientHandle); 93 DeleteAppSpawnMgr(mgr); 94 ASSERT_EQ(ret, 0); 95 } 96 97 HWTEST_F(AppSpawnCommonTest, App_Spawn_Common_004, TestSize.Level0) 98 { 99 AppSpawnClientHandle clientHandle = nullptr; 100 AppSpawnReqMsgHandle reqHandle = 0; 101 AppSpawningCtx *property = nullptr; 102 AppSpawnMgr *mgr = nullptr; 103 int ret = -1; 104 do { 105 mgr = CreateAppSpawnMgr(MODE_FOR_NWEB_SPAWN); 106 EXPECT_EQ(mgr != nullptr, 1); 107 // create msg 108 ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &clientHandle); 109 APPSPAWN_CHECK(ret == 0, break, "Failed to create reqMgr %{public}s", APPSPAWN_SERVER_NAME); 110 reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_SPAWN_NATIVE_PROCESS, 0); 111 APPSPAWN_CHECK(reqHandle != INVALID_REQ_HANDLE, break, 112 "Failed to create req %{public}s", APPSPAWN_SERVER_NAME); 113 property = g_testHelper.GetAppProperty(clientHandle, reqHandle); 114 property->client.flags |= APP_BEGETCTL_BOOT; 115 APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break); 116 ret = SetSelinuxCon(mgr, property); 117 } while (0); 118 DeleteAppSpawningCtx(property); 119 AppSpawnClientDestroy(clientHandle); 120 DeleteAppSpawnMgr(mgr); 121 ASSERT_EQ(ret, APPSPAWN_NATIVE_NOT_SUPPORT); 122 } 123 124 HWTEST_F(AppSpawnCommonTest, App_Spawn_Common_005, TestSize.Level0) 125 { 126 AppSpawnClientHandle clientHandle = nullptr; 127 AppSpawnReqMsgHandle reqHandle = 0; 128 AppSpawningCtx *property = nullptr; 129 AppSpawnMgr *mgr = nullptr; 130 int ret = -1; 131 do { 132 mgr = CreateAppSpawnMgr(MODE_FOR_NWEB_SPAWN); 133 EXPECT_EQ(mgr != nullptr, 1); 134 // create msg 135 ret = AppSpawnClientInit(NWEBSPAWN_SERVER_NAME, &clientHandle); 136 APPSPAWN_CHECK(ret == 0, break, "Failed to create reqMgr %{public}s", APPSPAWN_SERVER_NAME); 137 reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_SPAWN_NATIVE_PROCESS, 0); 138 APPSPAWN_CHECK(reqHandle != INVALID_REQ_HANDLE, break, 139 "Failed to create req %{public}s", APPSPAWN_SERVER_NAME); 140 141 ret = AppSpawnReqMsgAddExtInfo(reqHandle, MSG_EXT_NAME_PROCESS_TYPE, 142 reinterpret_cast<uint8_t *>(const_cast<char *>("render")), 7); // 7 is value len 143 APPSPAWN_CHECK_ONLY_EXPER(ret == 0, break); 144 property = g_testHelper.GetAppProperty(clientHandle, reqHandle); 145 APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break); 146 ret = SetSeccompFilter(mgr, property); 147 } while (0); 148 DeleteAppSpawningCtx(property); 149 AppSpawnClientDestroy(clientHandle); 150 DeleteAppSpawnMgr(mgr); 151 ASSERT_EQ(ret, 0); 152 } 153 154 HWTEST_F(AppSpawnCommonTest, App_Spawn_Common_006, TestSize.Level0) 155 { 156 AppSpawnClientHandle clientHandle = nullptr; 157 AppSpawnReqMsgHandle reqHandle = 0; 158 AppSpawningCtx *property = nullptr; 159 AppSpawnMgr *mgr = nullptr; 160 int ret = -1; 161 do { 162 mgr = CreateAppSpawnMgr(MODE_FOR_NWEB_SPAWN); 163 EXPECT_EQ(mgr != nullptr, 1); 164 // create msg 165 ret = AppSpawnClientInit(NWEBSPAWN_SERVER_NAME, &clientHandle); 166 APPSPAWN_CHECK(ret == 0, break, "Failed to create reqMgr %{public}s", APPSPAWN_SERVER_NAME); 167 reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_SPAWN_NATIVE_PROCESS, 0); 168 APPSPAWN_CHECK(reqHandle != INVALID_REQ_HANDLE, break, 169 "Failed to create req %{public}s", APPSPAWN_SERVER_NAME); 170 171 ret = AppSpawnReqMsgAddExtInfo(reqHandle, MSG_EXT_NAME_PROCESS_TYPE, 172 reinterpret_cast<uint8_t *>(const_cast<char *>("xxx")), 7); // 7 is value len 173 APPSPAWN_CHECK_ONLY_EXPER(ret == 0, break); 174 ret = AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_ISOLATED_SANDBOX); 175 APPSPAWN_CHECK_ONLY_EXPER(ret == 0, break); 176 property = g_testHelper.GetAppProperty(clientHandle, reqHandle); 177 APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break); 178 ret = SetSeccompFilter(mgr, property); 179 } while (0); 180 DeleteAppSpawningCtx(property); 181 AppSpawnClientDestroy(clientHandle); 182 DeleteAppSpawnMgr(mgr); 183 ASSERT_EQ(ret, 0); 184 } 185 186 HWTEST_F(AppSpawnCommonTest, App_Spawn_Common_007, TestSize.Level0) 187 { 188 AppSpawnClientHandle clientHandle = nullptr; 189 AppSpawnReqMsgHandle reqHandle = 0; 190 AppSpawningCtx *property = nullptr; 191 AppSpawnMgr *mgr = nullptr; 192 int ret = -1; 193 do { 194 mgr = CreateAppSpawnMgr(MODE_FOR_NWEB_SPAWN); 195 EXPECT_EQ(mgr != nullptr, 1); 196 // create msg 197 ret = AppSpawnClientInit(NWEBSPAWN_SERVER_NAME, &clientHandle); 198 APPSPAWN_CHECK(ret == 0, break, "Failed to create reqMgr %{public}s", APPSPAWN_SERVER_NAME); 199 reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_SPAWN_NATIVE_PROCESS, 0); 200 APPSPAWN_CHECK(reqHandle != INVALID_REQ_HANDLE, break, 201 "Failed to create req %{public}s", APPSPAWN_SERVER_NAME); 202 203 ret = AppSpawnReqMsgAddExtInfo(reqHandle, MSG_EXT_NAME_PROCESS_TYPE, 204 reinterpret_cast<uint8_t *>(const_cast<char *>("xxx")), 7); // 7 is value len 205 APPSPAWN_CHECK_ONLY_EXPER(ret == 0, break); 206 ret = AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_IGNORE_SANDBOX); 207 APPSPAWN_CHECK_ONLY_EXPER(ret == 0, break); 208 property = g_testHelper.GetAppProperty(clientHandle, reqHandle); 209 APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break); 210 ret = SetSeccompFilter(mgr, property); 211 } while (0); 212 DeleteAppSpawningCtx(property); 213 AppSpawnClientDestroy(clientHandle); 214 DeleteAppSpawnMgr(mgr); 215 ASSERT_EQ(ret, 0); 216 } 217 218 HWTEST_F(AppSpawnCommonTest, App_Spawn_Common_008, TestSize.Level0) 219 { 220 AppSpawnClientHandle clientHandle = nullptr; 221 AppSpawnReqMsgHandle reqHandle = 0; 222 AppSpawningCtx *property = nullptr; 223 AppSpawnMgr *mgr = nullptr; 224 int ret = -1; 225 do { 226 mgr = CreateAppSpawnMgr(MODE_FOR_APP_SPAWN); 227 EXPECT_EQ(mgr != nullptr, 1); 228 // create msg 229 ret = AppSpawnClientInit(NWEBSPAWN_SERVER_NAME, &clientHandle); 230 APPSPAWN_CHECK(ret == 0, break, "Failed to create reqMgr %{public}s", APPSPAWN_SERVER_NAME); 231 reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_SPAWN_NATIVE_PROCESS, 0); 232 APPSPAWN_CHECK(reqHandle != INVALID_REQ_HANDLE, break, 233 "Failed to create req %{public}s", APPSPAWN_SERVER_NAME); 234 235 ret = AppSpawnReqMsgAddExtInfo(reqHandle, MSG_EXT_NAME_PROCESS_TYPE, 236 reinterpret_cast<uint8_t *>(const_cast<char *>("xxx")), 7); // 7 is value len 237 APPSPAWN_CHECK_ONLY_EXPER(ret == 0, break); 238 ret = AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_IGNORE_SANDBOX); 239 APPSPAWN_CHECK_ONLY_EXPER(ret == 0, break); 240 property = g_testHelper.GetAppProperty(clientHandle, reqHandle); 241 APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break); 242 ret = SetSeccompFilter(mgr, property); 243 } while (0); 244 DeleteAppSpawningCtx(property); 245 AppSpawnClientDestroy(clientHandle); 246 DeleteAppSpawnMgr(mgr); 247 ASSERT_EQ(ret, 0); 248 } 249 250 HWTEST_F(AppSpawnCommonTest, App_Spawn_Common_009, TestSize.Level0) 251 { 252 AppSpawnClientHandle clientHandle = nullptr; 253 AppSpawnReqMsgHandle reqHandle = 0; 254 AppSpawningCtx *property = nullptr; 255 int ret = -1; 256 do { 257 // create msg 258 ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &clientHandle); 259 APPSPAWN_CHECK(ret == 0, break, "Failed to create reqMgr %{public}s", APPSPAWN_SERVER_NAME); 260 reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_SPAWN_NATIVE_PROCESS, 0); 261 APPSPAWN_CHECK(reqHandle != INVALID_REQ_HANDLE, break, 262 "Failed to create req %{public}s", APPSPAWN_SERVER_NAME); 263 ret = AppSpawnReqMsgSetAppInternetPermissionInfo(reqHandle, 0, 0); 264 property = g_testHelper.GetAppProperty(clientHandle, reqHandle); 265 ret = SetInternetPermission(property); 266 } while (0); 267 DeleteAppSpawningCtx(property); 268 AppSpawnClientDestroy(clientHandle); 269 ASSERT_EQ(ret, 0); 270 } 271 272 HWTEST_F(AppSpawnCommonTest, App_Spawn_Common_010, TestSize.Level0) 273 { 274 AppSpawnClientHandle clientHandle = nullptr; 275 AppSpawnReqMsgHandle reqHandle = 0; 276 AppSpawningCtx *property = nullptr; 277 int ret = -1; 278 do { 279 // create msg 280 ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &clientHandle); 281 APPSPAWN_CHECK(ret == 0, break, "Failed to create reqMgr %{public}s", APPSPAWN_SERVER_NAME); 282 reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_SPAWN_NATIVE_PROCESS, 0); 283 APPSPAWN_CHECK(reqHandle != INVALID_REQ_HANDLE, break, 284 "Failed to create req %{public}s", APPSPAWN_SERVER_NAME); 285 ret = AppSpawnReqMsgSetAppInternetPermissionInfo(reqHandle, 0, 1); 286 property = g_testHelper.GetAppProperty(clientHandle, reqHandle); 287 ret = SetInternetPermission(property); 288 } while (0); 289 DeleteAppSpawningCtx(property); 290 AppSpawnClientDestroy(clientHandle); 291 ASSERT_EQ(ret, 0); 292 } 293 294 HWTEST_F(AppSpawnCommonTest, App_Spawn_Common_011, TestSize.Level0) 295 { 296 AppSpawnClientHandle clientHandle = nullptr; 297 AppSpawnReqMsgHandle reqHandle = 0; 298 AppSpawningCtx *property = nullptr; 299 int ret = -1; 300 do { 301 // create msg 302 ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &clientHandle); 303 APPSPAWN_CHECK(ret == 0, break, "Failed to create reqMgr %{public}s", APPSPAWN_SERVER_NAME); 304 reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_SPAWN_NATIVE_PROCESS, 0); 305 APPSPAWN_CHECK(reqHandle != INVALID_REQ_HANDLE, break, 306 "Failed to create req %{public}s", APPSPAWN_SERVER_NAME); 307 ret = AppSpawnReqMsgSetAppInternetPermissionInfo(reqHandle, 1, 0); 308 property = g_testHelper.GetAppProperty(clientHandle, reqHandle); 309 ret = SetInternetPermission(property); 310 } while (0); 311 DeleteAppSpawningCtx(property); 312 AppSpawnClientDestroy(clientHandle); 313 ASSERT_EQ(ret, 0); 314 } 315 316 HWTEST_F(AppSpawnCommonTest, App_Spawn_Common_012, TestSize.Level0) 317 { 318 AppSpawnClientHandle clientHandle = nullptr; 319 AppSpawnReqMsgHandle reqHandle = 0; 320 AppSpawningCtx *property = nullptr; 321 int ret = -1; 322 do { 323 // create msg 324 ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &clientHandle); 325 APPSPAWN_CHECK(ret == 0, break, "Failed to create reqMgr %{public}s", APPSPAWN_SERVER_NAME); 326 reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_SPAWN_NATIVE_PROCESS, 0); 327 APPSPAWN_CHECK(reqHandle != INVALID_REQ_HANDLE, break, 328 "Failed to create req %{public}s", APPSPAWN_SERVER_NAME); 329 ret = AppSpawnReqMsgSetAppInternetPermissionInfo(reqHandle, 1, 1); 330 property = g_testHelper.GetAppProperty(clientHandle, reqHandle); 331 ret = SetInternetPermission(property); 332 } while (0); 333 DeleteAppSpawningCtx(property); 334 AppSpawnClientDestroy(clientHandle); 335 ASSERT_EQ(ret, 0); 336 } 337 338 HWTEST_F(AppSpawnCommonTest, App_Spawn_Common_013, TestSize.Level0) 339 { 340 AppSpawnClientHandle clientHandle = nullptr; 341 AppSpawnReqMsgHandle reqHandle = 0; 342 AppSpawningCtx *property = nullptr; 343 AppSpawnMgr *mgr = nullptr; 344 int ret = -1; 345 do { 346 mgr = CreateAppSpawnMgr(MODE_FOR_APP_SPAWN); 347 EXPECT_EQ(mgr != nullptr, 1); 348 // create msg 349 ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &clientHandle); 350 APPSPAWN_CHECK(ret == 0, break, "Failed to create reqMgr %{public}s", APPSPAWN_SERVER_NAME); 351 reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_SPAWN_NATIVE_PROCESS, 0); 352 APPSPAWN_CHECK(reqHandle != INVALID_REQ_HANDLE, break, 353 "Failed to create req %{public}s", APPSPAWN_SERVER_NAME); 354 property = g_testHelper.GetAppProperty(clientHandle, reqHandle); 355 APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break); 356 ret = SetFdEnv(nullptr, nullptr); 357 ASSERT_EQ(ret, -1); 358 ret = SetFdEnv(mgr, property); 359 } while (0); 360 DeleteAppSpawningCtx(property); 361 AppSpawnClientDestroy(clientHandle); 362 DeleteAppSpawnMgr(mgr); 363 ASSERT_EQ(ret, -1); 364 } 365 366 HWTEST_F(AppSpawnCommonTest, App_Spawn_Common_014, TestSize.Level0) 367 { 368 NsInitFunc(); 369 EXPECT_EQ(GetNsPidFd(-1), -1); 370 GetPidByName("///////"); 371 } 372 373 HWTEST_F(AppSpawnCommonTest, App_Spawn_Common_015, TestSize.Level0) 374 { 375 AppSpawnMgr *mgr = nullptr; 376 int ret = -1; 377 mgr = CreateAppSpawnMgr(MODE_FOR_APP_SPAWN); 378 EXPECT_EQ(mgr != nullptr, 1); 379 mgr->content.sandboxNsFlags = 0; 380 PreLoadEnablePidNs(mgr); 381 DeleteAppSpawnMgr(mgr); 382 ASSERT_NE(ret, 0); 383 } 384 385 HWTEST_F(AppSpawnCommonTest, App_Spawn_Common_016, TestSize.Level0) 386 { 387 HOOK_MGR *hookMgr = GetAppSpawnHookMgr(); 388 EXPECT_EQ(hookMgr != nullptr, 1); 389 DeleteAppSpawnHookMgr(); 390 AppSpawnHiSysEventWrite(); 391 } 392 393 HWTEST_F(AppSpawnCommonTest, App_Spawn_Common_017, TestSize.Level0) 394 { 395 AppSpawnMgr *content = CreateAppSpawnMgr(MODE_FOR_NWEB_SPAWN); 396 EXPECT_EQ(content != nullptr, 1); 397 int ret = -1; 398 do { 399 LoadExtendCJLib(); 400 } while (0); 401 DeleteAppSpawnMgr(content); 402 ASSERT_EQ(ret, -1); 403 } 404 405 HWTEST_F(AppSpawnCommonTest, App_Spawn_Common_018, TestSize.Level0) 406 { 407 AppSpawnMgr *content = CreateAppSpawnMgr(MODE_FOR_NWEB_SPAWN); 408 EXPECT_EQ(content != nullptr, 1); 409 int ret = -1; 410 do { 411 // spawn 412 ret = PreLoadAppSpawn(content); 413 } while (0); 414 DeleteAppSpawnMgr(content); 415 ASSERT_EQ(ret, 0); 416 } 417 418 HWTEST_F(AppSpawnCommonTest, App_Spawn_Common_019, TestSize.Level0) 419 { 420 AppSpawnMgr *content = CreateAppSpawnMgr(MODE_FOR_NWEB_SPAWN); 421 EXPECT_EQ(content != nullptr, 1); 422 int ret = -1; 423 do { 424 RunChildByRenderCmd(nullptr, nullptr); 425 } while (0); 426 DeleteAppSpawnMgr(content); 427 ASSERT_EQ(ret, -1); 428 } 429 430 HWTEST_F(AppSpawnCommonTest, App_Spawn_Common_020, TestSize.Level0) 431 { 432 AppSpawnMgr *content = CreateAppSpawnMgr(MODE_FOR_NWEB_SPAWN); 433 EXPECT_EQ(content != nullptr, 1); 434 int ret = -1; 435 do { 436 // spawn 437 AppSpawnMsgNode *msgNode = CreateAppSpawnMsg(); 438 EXPECT_EQ(msgNode != nullptr, 1); 439 std::map<std::string, int> fdMap; 440 ret = BuildFdInfoMap(msgNode, fdMap, 0); 441 } while (0); 442 DeleteAppSpawnMgr(content); 443 ASSERT_EQ(ret, -1); 444 } 445 446 447 HWTEST_F(AppSpawnCommonTest, App_Spawn_Common_021, TestSize.Level0) 448 { 449 int ret = SetInternetPermission(nullptr); 450 ASSERT_EQ(ret, 0); 451 } 452 453 HWTEST_F(AppSpawnCommonTest, App_Spawn_Common_022, TestSize.Level0) 454 { 455 AppSpawnClientHandle clientHandle = nullptr; 456 AppSpawnReqMsgHandle reqHandle = 0; 457 AppSpawningCtx *property = nullptr; 458 int ret = -1; 459 do { 460 ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &clientHandle); 461 APPSPAWN_CHECK(ret == 0, break, "Failed to create reqMgr %{public}s", APPSPAWN_SERVER_NAME); 462 reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_SPAWN_NATIVE_PROCESS, 0); 463 APPSPAWN_CHECK(reqHandle != INVALID_REQ_HANDLE, break, "Failed to create req %{public}s", APPSPAWN_SERVER_NAME); 464 const char *appEnv = "{\"test\"}"; 465 ret = AppSpawnReqMsgAddExtInfo(reqHandle, "AppEnv", 466 reinterpret_cast<uint8_t *>(const_cast<char *>(appEnv)), strlen(appEnv) + 1); 467 APPSPAWN_CHECK(ret == 0, break, "Failed to add ext tlv %{public}s", appEnv); 468 469 property = g_testHelper.GetAppProperty(clientHandle, reqHandle); 470 reqHandle = nullptr; 471 APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break); 472 SetEnvInfo(nullptr, property); 473 // spawn 474 property = nullptr; 475 } while (0); 476 DeleteAppSpawningCtx(property); 477 AppSpawnReqMsgFree(reqHandle); 478 AppSpawnClientDestroy(clientHandle); 479 ASSERT_EQ(ret, 0); 480 } 481 482 HWTEST_F(AppSpawnCommonTest, App_Spawn_Common_023, TestSize.Level0) 483 { 484 AppSpawnClientHandle clientHandle = nullptr; 485 AppSpawnReqMsgHandle reqHandle = 0; 486 AppSpawningCtx *property = nullptr; 487 int ret = -1; 488 do { 489 ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &clientHandle); 490 APPSPAWN_CHECK(ret == 0, break, "Failed to create reqMgr %{public}s", APPSPAWN_SERVER_NAME); 491 reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_SPAWN_NATIVE_PROCESS, 0); 492 APPSPAWN_CHECK(reqHandle != INVALID_REQ_HANDLE, break, "Failed to create req %{public}s", APPSPAWN_SERVER_NAME); 493 const char *appEnv = "%"; 494 ret = AppSpawnReqMsgAddExtInfo(reqHandle, "AppEnv", 495 reinterpret_cast<uint8_t *>(const_cast<char *>(appEnv)), strlen(appEnv) + 1); 496 APPSPAWN_CHECK(ret == 0, break, "Failed to add ext tlv %{public}s", appEnv); 497 498 property = g_testHelper.GetAppProperty(clientHandle, reqHandle); 499 reqHandle = nullptr; 500 APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break); 501 SetEnvInfo(nullptr, property); 502 // spawn 503 property = nullptr; 504 } while (0); 505 DeleteAppSpawningCtx(property); 506 AppSpawnReqMsgFree(reqHandle); 507 AppSpawnClientDestroy(clientHandle); 508 ASSERT_EQ(ret, 0); 509 } 510 511 HWTEST_F(AppSpawnCommonTest, App_Spawn_Common_024, TestSize.Level0) 512 { 513 AppSpawnClientHandle clientHandle = nullptr; 514 AppSpawnReqMsgHandle reqHandle = 0; 515 AppSpawningCtx *property = nullptr; 516 int ret = -1; 517 do { 518 ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &clientHandle); 519 APPSPAWN_CHECK(ret == 0, break, "Failed to create reqMgr %{public}s", APPSPAWN_SERVER_NAME); 520 reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_SPAWN_NATIVE_PROCESS, 0); 521 APPSPAWN_CHECK(reqHandle != INVALID_REQ_HANDLE, break, "Failed to create req %{public}s", APPSPAWN_SERVER_NAME); 522 property = g_testHelper.GetAppProperty(clientHandle, reqHandle); 523 reqHandle = nullptr; 524 APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break); 525 SetEnvInfo(nullptr, property); 526 // spawn 527 property = nullptr; 528 } while (0); 529 DeleteAppSpawningCtx(property); 530 AppSpawnReqMsgFree(reqHandle); 531 AppSpawnClientDestroy(clientHandle); 532 ASSERT_EQ(ret, 0); 533 } 534 535 HWTEST_F(AppSpawnCommonTest, App_Spawn_Common_025, TestSize.Level0) 536 { 537 AppSpawnClientHandle clientHandle = nullptr; 538 AppSpawnReqMsgHandle reqHandle = 0; 539 AppSpawningCtx *property = nullptr; 540 AppSpawnMgr *mgr = nullptr; 541 int ret = -1; 542 do { 543 mgr = CreateAppSpawnMgr(MODE_FOR_NWEB_SPAWN); 544 EXPECT_EQ(mgr != nullptr, 1); 545 // create msg 546 ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &clientHandle); 547 APPSPAWN_CHECK(ret == 0, break, "Failed to create reqMgr %{public}s", APPSPAWN_SERVER_NAME); 548 reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_SPAWN_NATIVE_PROCESS, 0); 549 APPSPAWN_CHECK(reqHandle != INVALID_REQ_HANDLE, break, 550 "Failed to create req %{public}s", APPSPAWN_SERVER_NAME); 551 property = g_testHelper.GetAppProperty(clientHandle, reqHandle); 552 property->client.flags |= APP_BEGETCTL_BOOT; 553 APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break); 554 SetAppSpawnMsgFlag(property->message, APP_FLAGS_ISOLATED_SANDBOX, 16); 555 ret = SetSelinuxCon(mgr, property); 556 } while (0); 557 DeleteAppSpawningCtx(property); 558 AppSpawnClientDestroy(clientHandle); 559 DeleteAppSpawnMgr(mgr); 560 ASSERT_EQ(ret, APPSPAWN_NATIVE_NOT_SUPPORT); 561 } 562 563 HWTEST_F(AppSpawnCommonTest, App_Spawn_Common_026, TestSize.Level0) 564 { 565 AppSpawnClientHandle clientHandle = nullptr; 566 AppSpawnReqMsgHandle reqHandle = 0; 567 AppSpawningCtx *property = nullptr; 568 AppSpawnMgr *mgr = nullptr; 569 int ret = -1; 570 do { 571 mgr = CreateAppSpawnMgr(MODE_FOR_NWEB_SPAWN); 572 EXPECT_EQ(mgr != nullptr, 1); 573 // create msg 574 ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &clientHandle); 575 APPSPAWN_CHECK(ret == 0, break, "Failed to create reqMgr %{public}s", APPSPAWN_SERVER_NAME); 576 reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_SPAWN_NATIVE_PROCESS, 0); 577 APPSPAWN_CHECK(reqHandle != INVALID_REQ_HANDLE, break, 578 "Failed to create req %{public}s", APPSPAWN_SERVER_NAME); 579 property = g_testHelper.GetAppProperty(clientHandle, reqHandle); 580 property->client.flags |= APP_BEGETCTL_BOOT; 581 APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break); 582 SetAppSpawnMsgFlag(property->message, APP_FLAGS_DLP_MANAGER, 1); 583 ret = SetSelinuxCon(mgr, property); 584 } while (0); 585 DeleteAppSpawningCtx(property); 586 AppSpawnClientDestroy(clientHandle); 587 DeleteAppSpawnMgr(mgr); 588 ASSERT_EQ(ret, APPSPAWN_NATIVE_NOT_SUPPORT); 589 } 590 591 HWTEST_F(AppSpawnCommonTest, App_Spawn_Common_027, TestSize.Level0) 592 { 593 AppSpawnClientHandle clientHandle = nullptr; 594 AppSpawnReqMsgHandle reqHandle = 0; 595 AppSpawningCtx *property = nullptr; 596 AppSpawnMgr *mgr = nullptr; 597 int ret = -1; 598 do { 599 mgr = CreateAppSpawnMgr(MODE_FOR_NWEB_SPAWN); 600 EXPECT_EQ(mgr != nullptr, 1); 601 // create msg 602 ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &clientHandle); 603 APPSPAWN_CHECK(ret == 0, break, "Failed to create reqMgr %{public}s", APPSPAWN_SERVER_NAME); 604 reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_SPAWN_NATIVE_PROCESS, 0); 605 APPSPAWN_CHECK(reqHandle != INVALID_REQ_HANDLE, break, 606 "Failed to create req %{public}s", APPSPAWN_SERVER_NAME); 607 property = g_testHelper.GetAppProperty(clientHandle, reqHandle); 608 property->client.flags |= APP_BEGETCTL_BOOT; 609 APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break); 610 SetAppSpawnMsgFlag(property->message, APP_FLAGS_DEBUGGABLE, 1); 611 ret = SetSelinuxCon(mgr, property); 612 } while (0); 613 DeleteAppSpawningCtx(property); 614 AppSpawnClientDestroy(clientHandle); 615 DeleteAppSpawnMgr(mgr); 616 ASSERT_EQ(ret, APPSPAWN_NATIVE_NOT_SUPPORT); 617 } 618 619 HWTEST_F(AppSpawnCommonTest, App_Spawn_Common_028, TestSize.Level0) 620 { 621 ASSERT_EQ(GetAppSpawnNamespace(nullptr), nullptr); 622 } 623 624 HWTEST_F(AppSpawnCommonTest, App_Spawn_Common_029, TestSize.Level0) 625 { 626 DeleteAppSpawnNamespace(nullptr); 627 FreeAppSpawnNamespace(nullptr); 628 } 629 630 HWTEST_F(AppSpawnCommonTest, App_Spawn_Common_030, TestSize.Level0) 631 { 632 int ret = -1; 633 AppSpawnMgr *mgr = CreateAppSpawnMgr(MODE_FOR_APP_SPAWN); 634 EXPECT_EQ(mgr != nullptr, 1); 635 mgr->content.sandboxNsFlags = 1; 636 ret = PreForkSetPidNamespace(mgr, nullptr); 637 DeleteAppSpawnMgr(mgr); 638 ASSERT_EQ(ret, 0); 639 } 640 641 HWTEST_F(AppSpawnCommonTest, App_Spawn_Common_031, TestSize.Level0) 642 { 643 int ret = -1; 644 AppSpawnMgr *mgr = CreateAppSpawnMgr(MODE_FOR_APP_SPAWN); 645 EXPECT_EQ(mgr != nullptr, 1); 646 mgr->content.sandboxNsFlags = 0; 647 ret = PostForkSetPidNamespace(mgr, nullptr); 648 DeleteAppSpawnMgr(mgr); 649 ASSERT_EQ(ret, 0); 650 AppSpawnClientInit(nullptr, nullptr); 651 } 652 653 HWTEST_F(AppSpawnCommonTest, App_Spawn_Encaps_001, TestSize.Level0) 654 { 655 const char encapsJsonStr[] = "{\"name\":\"Permissions\",\"ohos.encaps.count\":0,\"permissions\":" 656 "[{\"ohos.permission.bool\":true},{\"ohos.permission.int\":3225},{\"ohos.permission.string\":\"test\"}," 657 "{\"ohos.permission.array\":[1,2,3,4,5]}]}"; 658 659 cJSON *encapsJson = cJSON_Parse(encapsJsonStr); 660 uint32_t permissionCount = 0; 661 int ret = AddPermissionToEncaps(encapsJson, NULL, &permissionCount); 662 EXPECT_EQ(ret, APPSPAWN_ARG_INVALID); 663 664 cJSON_Delete(encapsJson); 665 } 666 667 HWTEST_F(AppSpawnCommonTest, App_Spawn_Encaps_002, TestSize.Level0) 668 { 669 const char encapsJsonStr[] = "{\"name\":\"Permissions\",\"ohos.encaps.count\":4,\"permissions\":" 670 "[{\"ohos.permission.bool\":true},{\"ohos.permission.int\":3225},{\"ohos.permission.string\":\"test\"}," 671 "{\"ohos.permission.array\":[1,2,3,4,5]}]}"; 672 673 cJSON *encapsJson = cJSON_Parse(encapsJsonStr); 674 uint32_t permissionCount = 0; 675 int ret = AddPermissionToEncaps(encapsJson, NULL, &permissionCount); 676 EXPECT_EQ(ret, APPSPAWN_ERROR_UTILS_ADD_JSON_FAIL); 677 678 cJSON_Delete(encapsJson); 679 } 680 681 HWTEST_F(AppSpawnCommonTest, App_Spawn_Encaps_003, TestSize.Level0) 682 { 683 const char encapsJsonStr[] = "{\"name\":\"Permissions\",\"ohos.encaps.count\":5,\"permissions\":" 684 "[{\"ohos.permission.bool\":true},{\"ohos.permission.int\":3225},{\"ohos.permission.string\":\"test\"}," 685 "{\"ohos.permission.array\":[1,2,3,4,5]}]}"; 686 687 cJSON *encapsOut = cJSON_CreateObject(); 688 EXPECT_NE(encapsOut, nullptr); 689 cJSON *encapsJson = cJSON_Parse(encapsJsonStr); 690 uint32_t permissionCount = 0; 691 692 cJSON *permissions = cJSON_GetObjectItemCaseSensitive(encapsJson, "permissions"); 693 EXPECT_NE(permissions, nullptr); 694 cJSON *emptyItem = cJSON_CreateObject(); 695 cJSON_AddItemToArray(permissions, emptyItem); 696 697 int ret = AddPermissionToEncaps(encapsJson, encapsOut, &permissionCount); 698 EXPECT_EQ(ret, APPSPAWN_ERROR_UTILS_DECODE_JSON_FAIL); 699 700 cJSON_Delete(encapsJson); 701 cJSON_Delete(encapsOut); 702 } 703 704 HWTEST_F(AppSpawnCommonTest, App_Spawn_Encaps_004, TestSize.Level0) 705 { 706 const char encapsJsonStr[] = "{\"name\":\"Permissions\",\"ohos.encaps.count\":4,\"permissions\":" 707 "[{\"ohos.permission.bool\":true},{\"ohos.permission.int\":3225},{\"ohos.permission.string\":\"test\"}," 708 "{\"ohos.permission.array\":[1,2,3,4,5]}]}"; 709 710 cJSON *encapsOut = cJSON_CreateObject(); 711 EXPECT_NE(encapsOut, nullptr); 712 cJSON *encapsJson = cJSON_Parse(encapsJsonStr); 713 uint32_t permissionCount = 0; 714 715 cJSON *permissions = cJSON_GetObjectItemCaseSensitive(encapsJson, "permissions"); 716 EXPECT_NE(permissions, nullptr); 717 cJSON *permission = cJSON_GetArrayItem(permissions, 0); 718 EXPECT_NE(permission, nullptr); 719 permission->child = nullptr; 720 int ret = AddPermissionToEncaps(encapsJson, encapsOut, &permissionCount); 721 EXPECT_EQ(ret, APPSPAWN_ERROR_UTILS_DECODE_JSON_FAIL); 722 723 cJSON_Delete(encapsJson); 724 cJSON_Delete(encapsOut); 725 } 726 727 HWTEST_F(AppSpawnCommonTest, App_Spawn_Encaps_005, TestSize.Level0) 728 { 729 const char encapsJsonStr[] = "{\"name\":\"Permissions\",\"ohos.encaps.count\":4,\"permissions\":" 730 "[{\"ohos.permission.bool\":true},{\"ohos.permission.int\":3225},{\"ohos.permission.string\":\"test\"}," 731 "{\"ohos.permission.array\":[1,2,3,4,5]}]}"; 732 733 cJSON *encapsOut = cJSON_CreateObject(); 734 EXPECT_NE(encapsOut, nullptr); 735 cJSON *encapsJson = cJSON_Parse(encapsJsonStr); 736 uint32_t permissionCount = 0; 737 738 int ret = AddPermissionToEncaps(encapsJson, encapsOut, &permissionCount); 739 EXPECT_EQ(ret, 0); 740 EXPECT_EQ(permissionCount, 4); 741 742 cJSON_Delete(encapsJson); 743 cJSON_Delete(encapsOut); 744 } 745 746 HWTEST_F(AppSpawnCommonTest, App_Spawn_SetFdEnv, TestSize.Level0) 747 { 748 int ret = SetFdEnv(nullptr, nullptr); 749 EXPECT_EQ(ret, -1); 750 751 AppSpawningCtx property; 752 ret = SetFdEnv(nullptr, &property); 753 EXPECT_EQ(ret, -1); // message == null 754 755 property.message = (AppSpawnMsgNode *)malloc(sizeof(AppSpawnMsgNode) + sizeof(AppSpawnMsgNode) + APP_LEN_PROC_NAME); 756 ASSERT_EQ(property.message != nullptr, 1); 757 AppSpawnConnection *connection = (AppSpawnConnection *)malloc(sizeof(AppSpawnConnection)); 758 ASSERT_EQ(connection != nullptr, 1); 759 uint8_t *buffer = (uint8_t*)malloc(sizeof(uint8_t) * 10); 760 ASSERT_EQ(buffer != nullptr, 1); 761 762 property.message->buffer = nullptr; 763 property.message->connection = nullptr; 764 ret = SetFdEnv(nullptr, &property); 765 EXPECT_EQ(ret, -1); // message != null, message->buffer == null, message->connection == null 766 767 property.message->buffer = nullptr; 768 property.message->connection = connection; 769 ret = SetFdEnv(nullptr, &property); // message != null, message->connection != null, message->buffer == null 770 EXPECT_EQ(ret, -1); 771 772 property.message->buffer = buffer; 773 property.message->connection = nullptr; 774 ret = SetFdEnv(nullptr, &property); // message != null, message->connection == null, message->buffer != null 775 EXPECT_EQ(ret, -1); 776 777 free(buffer); 778 free(connection); 779 free(property.message); 780 } 781 782 } // namespace OHOS 783