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 26 #include "appspawn_modulemgr.h" 27 #include "appspawn_server.h" 28 #include "appspawn_manager.h" 29 #include "appspawn_adapter.h" 30 #include "appspawn.h" 31 #include "appspawn_hook.h" 32 #include "appspawn_permission.h" 33 #include "appspawn_hisysevent.h" 34 #include "app_spawn_stub.h" 35 #include "app_spawn_test_helper.h" 36 #include "securec.h" 37 38 using namespace testing; 39 using namespace testing::ext; 40 using namespace OHOS; 41 42 namespace OHOS { 43 static AppSpawnTestHelper g_testHelper; 44 class AppSpawnCommonTest : public testing::Test { 45 public: SetUpTestCase()46 static void SetUpTestCase() {} TearDownTestCase()47 static void TearDownTestCase() {} SetUp()48 void SetUp() {} TearDown()49 void TearDown() {} 50 }; 51 52 HWTEST_F(AppSpawnCommonTest, App_Spawn_Common_001, TestSize.Level0) 53 { 54 SetSelinuxConNweb(nullptr, nullptr); 55 } 56 57 HWTEST_F(AppSpawnCommonTest, App_Spawn_Common_002, TestSize.Level0) 58 { 59 SetAppAccessToken(nullptr, nullptr); 60 } 61 62 HWTEST_F(AppSpawnCommonTest, App_Spawn_Common_003, TestSize.Level0) 63 { 64 AppSpawnClientHandle clientHandle = nullptr; 65 AppSpawnReqMsgHandle reqHandle = 0; 66 AppSpawningCtx *property = nullptr; 67 AppSpawnMgr *mgr = nullptr; 68 int ret = -1; 69 do { 70 mgr = CreateAppSpawnMgr(MODE_FOR_NWEB_SPAWN); 71 EXPECT_EQ(mgr != nullptr, 1); 72 // create msg 73 ret = AppSpawnClientInit(NWEBSPAWN_SERVER_NAME, &clientHandle); 74 APPSPAWN_CHECK(ret == 0, break, "Failed to create reqMgr %{public}s", APPSPAWN_SERVER_NAME); 75 reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_SPAWN_NATIVE_PROCESS, 0); 76 APPSPAWN_CHECK(reqHandle != INVALID_REQ_HANDLE, break, 77 "Failed to create req %{public}s", APPSPAWN_SERVER_NAME); 78 property = g_testHelper.GetAppProperty(clientHandle, reqHandle); 79 property->client.flags |= APP_DEVELOPER_MODE; 80 APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break); 81 ret = SetSelinuxCon(mgr, property); 82 } while (0); 83 DeleteAppSpawningCtx(property); 84 AppSpawnClientDestroy(clientHandle); 85 DeleteAppSpawnMgr(mgr); 86 ASSERT_EQ(ret, 0); 87 } 88 89 HWTEST_F(AppSpawnCommonTest, App_Spawn_Common_004, TestSize.Level0) 90 { 91 AppSpawnClientHandle clientHandle = nullptr; 92 AppSpawnReqMsgHandle reqHandle = 0; 93 AppSpawningCtx *property = nullptr; 94 AppSpawnMgr *mgr = nullptr; 95 int ret = -1; 96 do { 97 mgr = CreateAppSpawnMgr(MODE_FOR_NWEB_SPAWN); 98 EXPECT_EQ(mgr != nullptr, 1); 99 // create msg 100 ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &clientHandle); 101 APPSPAWN_CHECK(ret == 0, break, "Failed to create reqMgr %{public}s", APPSPAWN_SERVER_NAME); 102 reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_SPAWN_NATIVE_PROCESS, 0); 103 APPSPAWN_CHECK(reqHandle != INVALID_REQ_HANDLE, break, 104 "Failed to create req %{public}s", APPSPAWN_SERVER_NAME); 105 property = g_testHelper.GetAppProperty(clientHandle, reqHandle); 106 property->client.flags |= APP_BEGETCTL_BOOT; 107 APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break); 108 ret = SetSelinuxCon(mgr, property); 109 } while (0); 110 DeleteAppSpawningCtx(property); 111 AppSpawnClientDestroy(clientHandle); 112 DeleteAppSpawnMgr(mgr); 113 ASSERT_EQ(ret, APPSPAWN_NATIVE_NOT_SUPPORT); 114 } 115 116 HWTEST_F(AppSpawnCommonTest, App_Spawn_Common_005, TestSize.Level0) 117 { 118 AppSpawnClientHandle clientHandle = nullptr; 119 AppSpawnReqMsgHandle reqHandle = 0; 120 AppSpawningCtx *property = nullptr; 121 AppSpawnMgr *mgr = nullptr; 122 int ret = -1; 123 do { 124 mgr = CreateAppSpawnMgr(MODE_FOR_NWEB_SPAWN); 125 EXPECT_EQ(mgr != nullptr, 1); 126 // create msg 127 ret = AppSpawnClientInit(NWEBSPAWN_SERVER_NAME, &clientHandle); 128 APPSPAWN_CHECK(ret == 0, break, "Failed to create reqMgr %{public}s", APPSPAWN_SERVER_NAME); 129 reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_SPAWN_NATIVE_PROCESS, 0); 130 APPSPAWN_CHECK(reqHandle != INVALID_REQ_HANDLE, break, 131 "Failed to create req %{public}s", APPSPAWN_SERVER_NAME); 132 133 ret = AppSpawnReqMsgAddExtInfo(reqHandle, MSG_EXT_NAME_PROCESS_TYPE, 134 reinterpret_cast<uint8_t *>(const_cast<char *>("render")), 7); // 7 is value len 135 APPSPAWN_CHECK_ONLY_EXPER(ret == 0, break); 136 property = g_testHelper.GetAppProperty(clientHandle, reqHandle); 137 APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break); 138 ret = SetSeccompFilter(mgr, property); 139 } while (0); 140 DeleteAppSpawningCtx(property); 141 AppSpawnClientDestroy(clientHandle); 142 DeleteAppSpawnMgr(mgr); 143 ASSERT_EQ(ret, 0); 144 } 145 146 HWTEST_F(AppSpawnCommonTest, App_Spawn_Common_006, TestSize.Level0) 147 { 148 AppSpawnClientHandle clientHandle = nullptr; 149 AppSpawnReqMsgHandle reqHandle = 0; 150 AppSpawningCtx *property = nullptr; 151 AppSpawnMgr *mgr = nullptr; 152 int ret = -1; 153 do { 154 mgr = CreateAppSpawnMgr(MODE_FOR_NWEB_SPAWN); 155 EXPECT_EQ(mgr != nullptr, 1); 156 // create msg 157 ret = AppSpawnClientInit(NWEBSPAWN_SERVER_NAME, &clientHandle); 158 APPSPAWN_CHECK(ret == 0, break, "Failed to create reqMgr %{public}s", APPSPAWN_SERVER_NAME); 159 reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_SPAWN_NATIVE_PROCESS, 0); 160 APPSPAWN_CHECK(reqHandle != INVALID_REQ_HANDLE, break, 161 "Failed to create req %{public}s", APPSPAWN_SERVER_NAME); 162 163 ret = AppSpawnReqMsgAddExtInfo(reqHandle, MSG_EXT_NAME_PROCESS_TYPE, 164 reinterpret_cast<uint8_t *>(const_cast<char *>("xxx")), 7); // 7 is value len 165 APPSPAWN_CHECK_ONLY_EXPER(ret == 0, break); 166 ret = AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_ISOLATED_SANDBOX); 167 APPSPAWN_CHECK_ONLY_EXPER(ret == 0, break); 168 property = g_testHelper.GetAppProperty(clientHandle, reqHandle); 169 APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break); 170 ret = SetSeccompFilter(mgr, property); 171 } while (0); 172 DeleteAppSpawningCtx(property); 173 AppSpawnClientDestroy(clientHandle); 174 DeleteAppSpawnMgr(mgr); 175 ASSERT_EQ(ret, 0); 176 } 177 178 HWTEST_F(AppSpawnCommonTest, App_Spawn_Common_007, TestSize.Level0) 179 { 180 AppSpawnClientHandle clientHandle = nullptr; 181 AppSpawnReqMsgHandle reqHandle = 0; 182 AppSpawningCtx *property = nullptr; 183 AppSpawnMgr *mgr = nullptr; 184 int ret = -1; 185 do { 186 mgr = CreateAppSpawnMgr(MODE_FOR_NWEB_SPAWN); 187 EXPECT_EQ(mgr != nullptr, 1); 188 // create msg 189 ret = AppSpawnClientInit(NWEBSPAWN_SERVER_NAME, &clientHandle); 190 APPSPAWN_CHECK(ret == 0, break, "Failed to create reqMgr %{public}s", APPSPAWN_SERVER_NAME); 191 reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_SPAWN_NATIVE_PROCESS, 0); 192 APPSPAWN_CHECK(reqHandle != INVALID_REQ_HANDLE, break, 193 "Failed to create req %{public}s", APPSPAWN_SERVER_NAME); 194 195 ret = AppSpawnReqMsgAddExtInfo(reqHandle, MSG_EXT_NAME_PROCESS_TYPE, 196 reinterpret_cast<uint8_t *>(const_cast<char *>("xxx")), 7); // 7 is value len 197 APPSPAWN_CHECK_ONLY_EXPER(ret == 0, break); 198 ret = AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_IGNORE_SANDBOX); 199 APPSPAWN_CHECK_ONLY_EXPER(ret == 0, break); 200 property = g_testHelper.GetAppProperty(clientHandle, reqHandle); 201 APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break); 202 ret = SetSeccompFilter(mgr, property); 203 } while (0); 204 DeleteAppSpawningCtx(property); 205 AppSpawnClientDestroy(clientHandle); 206 DeleteAppSpawnMgr(mgr); 207 ASSERT_EQ(ret, 0); 208 } 209 210 HWTEST_F(AppSpawnCommonTest, App_Spawn_Common_008, TestSize.Level0) 211 { 212 AppSpawnClientHandle clientHandle = nullptr; 213 AppSpawnReqMsgHandle reqHandle = 0; 214 AppSpawningCtx *property = nullptr; 215 AppSpawnMgr *mgr = nullptr; 216 int ret = -1; 217 do { 218 mgr = CreateAppSpawnMgr(MODE_FOR_APP_SPAWN); 219 EXPECT_EQ(mgr != nullptr, 1); 220 // create msg 221 ret = AppSpawnClientInit(NWEBSPAWN_SERVER_NAME, &clientHandle); 222 APPSPAWN_CHECK(ret == 0, break, "Failed to create reqMgr %{public}s", APPSPAWN_SERVER_NAME); 223 reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_SPAWN_NATIVE_PROCESS, 0); 224 APPSPAWN_CHECK(reqHandle != INVALID_REQ_HANDLE, break, 225 "Failed to create req %{public}s", APPSPAWN_SERVER_NAME); 226 227 ret = AppSpawnReqMsgAddExtInfo(reqHandle, MSG_EXT_NAME_PROCESS_TYPE, 228 reinterpret_cast<uint8_t *>(const_cast<char *>("xxx")), 7); // 7 is value len 229 APPSPAWN_CHECK_ONLY_EXPER(ret == 0, break); 230 ret = AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_IGNORE_SANDBOX); 231 APPSPAWN_CHECK_ONLY_EXPER(ret == 0, break); 232 property = g_testHelper.GetAppProperty(clientHandle, reqHandle); 233 APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break); 234 ret = SetSeccompFilter(mgr, property); 235 } while (0); 236 DeleteAppSpawningCtx(property); 237 AppSpawnClientDestroy(clientHandle); 238 DeleteAppSpawnMgr(mgr); 239 ASSERT_EQ(ret, 0); 240 } 241 242 HWTEST_F(AppSpawnCommonTest, App_Spawn_Common_009, TestSize.Level0) 243 { 244 AppSpawnClientHandle clientHandle = nullptr; 245 AppSpawnReqMsgHandle reqHandle = 0; 246 AppSpawningCtx *property = nullptr; 247 int ret = -1; 248 do { 249 // create msg 250 ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &clientHandle); 251 APPSPAWN_CHECK(ret == 0, break, "Failed to create reqMgr %{public}s", APPSPAWN_SERVER_NAME); 252 reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_SPAWN_NATIVE_PROCESS, 0); 253 APPSPAWN_CHECK(reqHandle != INVALID_REQ_HANDLE, break, 254 "Failed to create req %{public}s", APPSPAWN_SERVER_NAME); 255 ret = AppSpawnReqMsgSetAppInternetPermissionInfo(reqHandle, 0, 0); 256 property = g_testHelper.GetAppProperty(clientHandle, reqHandle); 257 ret = SetInternetPermission(property); 258 } while (0); 259 DeleteAppSpawningCtx(property); 260 AppSpawnClientDestroy(clientHandle); 261 ASSERT_EQ(ret, 0); 262 } 263 264 HWTEST_F(AppSpawnCommonTest, App_Spawn_Common_010, TestSize.Level0) 265 { 266 AppSpawnClientHandle clientHandle = nullptr; 267 AppSpawnReqMsgHandle reqHandle = 0; 268 AppSpawningCtx *property = nullptr; 269 int ret = -1; 270 do { 271 // create msg 272 ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &clientHandle); 273 APPSPAWN_CHECK(ret == 0, break, "Failed to create reqMgr %{public}s", APPSPAWN_SERVER_NAME); 274 reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_SPAWN_NATIVE_PROCESS, 0); 275 APPSPAWN_CHECK(reqHandle != INVALID_REQ_HANDLE, break, 276 "Failed to create req %{public}s", APPSPAWN_SERVER_NAME); 277 ret = AppSpawnReqMsgSetAppInternetPermissionInfo(reqHandle, 0, 1); 278 property = g_testHelper.GetAppProperty(clientHandle, reqHandle); 279 ret = SetInternetPermission(property); 280 } while (0); 281 DeleteAppSpawningCtx(property); 282 AppSpawnClientDestroy(clientHandle); 283 ASSERT_EQ(ret, 0); 284 } 285 286 HWTEST_F(AppSpawnCommonTest, App_Spawn_Common_011, TestSize.Level0) 287 { 288 AppSpawnClientHandle clientHandle = nullptr; 289 AppSpawnReqMsgHandle reqHandle = 0; 290 AppSpawningCtx *property = nullptr; 291 int ret = -1; 292 do { 293 // create msg 294 ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &clientHandle); 295 APPSPAWN_CHECK(ret == 0, break, "Failed to create reqMgr %{public}s", APPSPAWN_SERVER_NAME); 296 reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_SPAWN_NATIVE_PROCESS, 0); 297 APPSPAWN_CHECK(reqHandle != INVALID_REQ_HANDLE, break, 298 "Failed to create req %{public}s", APPSPAWN_SERVER_NAME); 299 ret = AppSpawnReqMsgSetAppInternetPermissionInfo(reqHandle, 1, 0); 300 property = g_testHelper.GetAppProperty(clientHandle, reqHandle); 301 ret = SetInternetPermission(property); 302 } while (0); 303 DeleteAppSpawningCtx(property); 304 AppSpawnClientDestroy(clientHandle); 305 ASSERT_EQ(ret, 0); 306 } 307 308 HWTEST_F(AppSpawnCommonTest, App_Spawn_Common_012, TestSize.Level0) 309 { 310 AppSpawnClientHandle clientHandle = nullptr; 311 AppSpawnReqMsgHandle reqHandle = 0; 312 AppSpawningCtx *property = nullptr; 313 int ret = -1; 314 do { 315 // create msg 316 ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &clientHandle); 317 APPSPAWN_CHECK(ret == 0, break, "Failed to create reqMgr %{public}s", APPSPAWN_SERVER_NAME); 318 reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_SPAWN_NATIVE_PROCESS, 0); 319 APPSPAWN_CHECK(reqHandle != INVALID_REQ_HANDLE, break, 320 "Failed to create req %{public}s", APPSPAWN_SERVER_NAME); 321 ret = AppSpawnReqMsgSetAppInternetPermissionInfo(reqHandle, 1, 1); 322 property = g_testHelper.GetAppProperty(clientHandle, reqHandle); 323 ret = SetInternetPermission(property); 324 } while (0); 325 DeleteAppSpawningCtx(property); 326 AppSpawnClientDestroy(clientHandle); 327 ASSERT_EQ(ret, 0); 328 } 329 330 331 HWTEST_F(AppSpawnCommonTest, App_Spawn_Common_013, TestSize.Level0) 332 { 333 DumpSpawnStack(0); 334 } 335 336 HWTEST_F(AppSpawnCommonTest, App_Spawn_Common_014, TestSize.Level0) 337 { 338 AppSpawnClientHandle clientHandle = nullptr; 339 AppSpawnReqMsgHandle reqHandle = 0; 340 AppSpawningCtx *property = nullptr; 341 AppSpawnMgr *mgr = nullptr; 342 int ret = -1; 343 do { 344 mgr = CreateAppSpawnMgr(MODE_FOR_APP_SPAWN); 345 EXPECT_EQ(mgr != nullptr, 1); 346 // create msg 347 ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &clientHandle); 348 APPSPAWN_CHECK(ret == 0, break, "Failed to create reqMgr %{public}s", APPSPAWN_SERVER_NAME); 349 reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_SPAWN_NATIVE_PROCESS, 0); 350 APPSPAWN_CHECK(reqHandle != INVALID_REQ_HANDLE, break, 351 "Failed to create req %{public}s", APPSPAWN_SERVER_NAME); 352 property = g_testHelper.GetAppProperty(clientHandle, reqHandle); 353 APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break); 354 ret = SetFdEnv(nullptr, nullptr); 355 ASSERT_EQ(ret, -1); 356 ret = SetFdEnv(mgr, property); 357 } while (0); 358 DeleteAppSpawningCtx(property); 359 AppSpawnClientDestroy(clientHandle); 360 DeleteAppSpawnMgr(mgr); 361 ASSERT_EQ(ret, -1); 362 } 363 364 HWTEST_F(AppSpawnCommonTest, App_Spawn_Common_015, TestSize.Level0) 365 { 366 NsInitFunc(); 367 EXPECT_EQ(GetNsPidFd(-1), -1); 368 GetPidByName("///////"); 369 } 370 371 HWTEST_F(AppSpawnCommonTest, App_Spawn_Common_016, TestSize.Level0) 372 { 373 AppSpawnMgr *mgr = nullptr; 374 int ret = -1; 375 mgr = CreateAppSpawnMgr(MODE_FOR_APP_SPAWN); 376 EXPECT_EQ(mgr != nullptr, 1); 377 mgr->content.sandboxNsFlags = 0; 378 PreLoadEnablePidNs(mgr); 379 DeleteAppSpawnMgr(mgr); 380 ASSERT_NE(ret, 0); 381 } 382 383 HWTEST_F(AppSpawnCommonTest, App_Spawn_Common_017, TestSize.Level0) 384 { 385 DeleteAppSpawnHookMgr(); 386 AppSpawnHiSysEventWrite(); 387 } 388 } // namespace OHOS