1 /* 2 * Copyright (c) 2024-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 <gtest/gtest.h> 17 18 #include <cerrno> 19 #include <cstdlib> 20 #include <cstring> 21 #include <iostream> 22 #include <memory> 23 #include <string> 24 #include <sys/stat.h> 25 #include <sys/types.h> 26 #include <unistd.h> 27 #include <vector> 28 29 #include "appspawn.h" 30 #include "appspawn_client.h" 31 #include "appspawn_hook.h" 32 #include "appspawn_mount_permission.h" 33 #include "appspawn_service.h" 34 #include "appspawn_utils.h" 35 #include "securec.h" 36 37 #include "app_spawn_stub.h" 38 39 using namespace testing; 40 using namespace testing::ext; 41 42 namespace OHOS { 43 class AppSpawnInterfaceTest : public testing::Test { 44 public: SetUpTestCase()45 static void SetUpTestCase() {} TearDownTestCase()46 static void TearDownTestCase() {} SetUp()47 void SetUp() {} TearDown()48 void TearDown() {} 49 }; 50 51 /** 52 * @brief 测试接口:AppSpawnClientInit 53 * 54 */ 55 HWTEST_F(AppSpawnInterfaceTest, App_Spawn_Interface_Init_001, TestSize.Level0) 56 { 57 AppSpawnClientHandle handle; 58 const char *serviceName[] = {APPSPAWN_SERVER_NAME, APPSPAWN_SOCKET_NAME, 59 NWEBSPAWN_SERVER_NAME, NWEBSPAWN_SOCKET_NAME, 60 "test", "", nullptr}; 61 AppSpawnClientHandle *inputhandle[] = {&handle, nullptr}; 62 63 for (size_t i = 0; i < ARRAY_LENGTH(serviceName); i++) { 64 for (size_t j = 0; j < ARRAY_LENGTH(inputhandle); j++) { 65 printf("App_Spawn_Interface_Init_001 %zu %zu \n", i, j); 66 int ret = AppSpawnClientInit(serviceName[i], inputhandle[j]); 67 EXPECT_EQ(((i <= 5) && j == 0 && ret == 0) || (ret != 0), 1); // 3 valid 68 if (ret == 0) { 69 AppSpawnClientDestroy(handle); 70 } 71 } 72 } 73 } 74 75 /** 76 * @brief 测试接口:AppSpawnClientDestroy 77 * 78 */ 79 HWTEST_F(AppSpawnInterfaceTest, App_Spawn_Interface_Destroy_001, TestSize.Level0) 80 { 81 AppSpawnClientHandle handle; 82 int ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &handle); 83 EXPECT_EQ(0, ret); 84 ret = AppSpawnClientDestroy(handle); 85 EXPECT_EQ(0, ret); 86 } 87 88 HWTEST_F(AppSpawnInterfaceTest, App_Spawn_Interface_Destroy_002, TestSize.Level0) 89 { 90 AppSpawnClientHandle handle; 91 int ret = AppSpawnClientInit(NWEBSPAWN_SERVER_NAME, &handle); 92 EXPECT_EQ(0, ret); 93 ret = AppSpawnClientDestroy(handle); 94 EXPECT_EQ(0, ret); 95 } 96 97 HWTEST_F(AppSpawnInterfaceTest, App_Spawn_Interface_Destroy_003, TestSize.Level0) 98 { 99 int ret = AppSpawnClientDestroy(nullptr); 100 EXPECT_EQ(APPSPAWN_SYSTEM_ERROR, ret); 101 } 102 103 /** 104 * @brief 测试接口:AppSpawnClientSendMsg 105 * 106 */ 107 HWTEST_F(AppSpawnInterfaceTest, App_Spawn_Interface_Send_Msg_001, TestSize.Level0) 108 { 109 int ret = 0; 110 AppSpawnClientHandle clientHandle = nullptr; 111 ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &clientHandle); 112 EXPECT_EQ(0, ret); 113 AppSpawnClientHandle clientHandle1 = nullptr; 114 ret = AppSpawnClientInit(NWEBSPAWN_SERVER_NAME, &clientHandle1); 115 EXPECT_EQ(0, ret); 116 117 AppSpawnResult result1 = {}; 118 AppSpawnClientHandle handle[] = {clientHandle, clientHandle1, nullptr}; 119 AppSpawnResult *result[] = {&result1, nullptr}; 120 121 for (size_t i = 0; i < ARRAY_LENGTH(handle); i++) { 122 for (size_t k = 0; k < ARRAY_LENGTH(result); k++) { 123 AppSpawnReqMsgHandle reqHandle1 = nullptr; 124 ret = AppSpawnReqMsgCreate(MSG_APP_SPAWN, "com.ohos.myapplication", &reqHandle1); 125 EXPECT_EQ(0, ret); 126 127 printf("App_Spawn_Interface_Send_Msg_001 %zu %zu %d \n", i, k, ret); 128 ret = AppSpawnClientSendMsg(handle[i], reqHandle1, result[k]); 129 EXPECT_EQ((i <= 1 && k == 0 && ret == 0) || (ret != 0), 1); 130 } 131 } 132 for (size_t i = 0; i < ARRAY_LENGTH(handle); i++) { 133 for (size_t k = 0; k < ARRAY_LENGTH(result); k++) { 134 ret = AppSpawnClientSendMsg(handle[i], nullptr, result[k]); 135 EXPECT_EQ(ret != 0, 1); 136 } 137 } 138 ret = AppSpawnClientDestroy(clientHandle); 139 EXPECT_EQ(0, ret); 140 ret = AppSpawnClientDestroy(clientHandle1); 141 EXPECT_EQ(0, ret); 142 } 143 144 /** 145 * @brief 测试接口:AppSpawnReqMsgCreate 146 * 147 */ 148 HWTEST_F(AppSpawnInterfaceTest, App_Spawn_Interface_Msg_Create_001, TestSize.Level0) 149 { 150 std::vector<char> name1(APP_LEN_PROC_NAME - 1, 'a'); 151 name1.push_back('\0'); 152 std::vector<char> name2(APP_LEN_PROC_NAME, 'b'); 153 name2.push_back('\0'); 154 std::vector<char> name3(APP_LEN_PROC_NAME + 8, 'c'); 155 name3.push_back('\0'); 156 157 AppSpawnMsgType msgType[] = {MSG_APP_SPAWN, MSG_GET_RENDER_TERMINATION_STATUS, 158 MSG_SPAWN_NATIVE_PROCESS, MSG_DUMP, MAX_TYPE_INVALID}; 159 const char *processName[] = {"com.ohos.myapplication", 160 "com.ohos.myapplication::@#$%^&*()test", 161 name1.data(), name2.data(), name3.data(), nullptr, ""}; 162 163 AppSpawnReqMsgHandle reqHandle = nullptr; 164 165 for (size_t i = 0; i < ARRAY_LENGTH(msgType); i++) { 166 for (size_t j = 0; j < ARRAY_LENGTH(processName); j++) { 167 int ret = AppSpawnReqMsgCreate(msgType[i], processName[j], &reqHandle); 168 printf("App_Spawn_Interface_Msg_Create_001 %zu %zu \n", i, j); 169 EXPECT_EQ(((i != 4) && (j < 3) && ret == 0) || (ret != 0), 1); // 4 3 valid index 170 if (ret == 0) { 171 AppSpawnReqMsgFree(reqHandle); 172 } 173 } 174 } 175 for (size_t i = 0; i < ARRAY_LENGTH(msgType); i++) { 176 for (size_t j = 0; j < ARRAY_LENGTH(processName); j++) { 177 int ret = AppSpawnReqMsgCreate(msgType[i], processName[j], nullptr); 178 EXPECT_EQ(ret != 0, 1); 179 } 180 } 181 } 182 183 /** 184 * @brief 测试接口:AppSpawnReqMsgFree 185 * 186 */ 187 HWTEST_F(AppSpawnInterfaceTest, App_Spawn_Interface_Msg_Free_001, TestSize.Level0) 188 { 189 AppSpawnReqMsgFree(nullptr); 190 AppSpawnReqMsgHandle reqHandle = nullptr; 191 int ret = AppSpawnReqMsgCreate(MSG_APP_SPAWN, "com.ohos.myapplication", &reqHandle); 192 EXPECT_EQ(0, ret); 193 AppSpawnReqMsgFree(reqHandle); 194 } 195 196 /** 197 * @brief 测试接口:AppSpawnReqMsgSetBundleInfo 198 * 199 */ 200 HWTEST_F(AppSpawnInterfaceTest, App_Spawn_Interface_ReqMsgSetBundleInfo_001, TestSize.Level0) 201 { 202 AppSpawnReqMsgHandle reqHandle = nullptr; 203 int ret = AppSpawnReqMsgCreate(MSG_APP_SPAWN, "com.ohos.myapplication", &reqHandle); 204 EXPECT_EQ(0, ret); 205 206 std::vector<char> name1(APP_LEN_BUNDLE_NAME - 1, 'a'); 207 name1.push_back('\0'); 208 std::vector<char> name2(APP_LEN_BUNDLE_NAME, 'b'); 209 name2.push_back('\0'); 210 std::vector<char> name3(APP_LEN_BUNDLE_NAME + 1, 'c'); 211 name3.push_back('\0'); 212 213 const AppSpawnReqMsgHandle inputHandle[] = {reqHandle, nullptr}; 214 const uint32_t bundleIndex[] = {0, 1}; 215 const char *bundleName[] = {"com.ohos.myapplication", name1.data(), name2.data(), name3.data(), nullptr}; 216 for (size_t i = 0; i < ARRAY_LENGTH(inputHandle); i++) { 217 for (size_t j = 0; j < ARRAY_LENGTH(bundleIndex); j++) { 218 for (size_t k = 0; k < ARRAY_LENGTH(bundleName); k++) { 219 printf("App_Spawn_Interface_ReqMsgSetBundleInfo_001 %zu %zu %zu %d \n", i, j, k, ret); 220 ret = AppSpawnReqMsgSetBundleInfo(inputHandle[i], bundleIndex[j], bundleName[k]); 221 EXPECT_EQ((i == 0 && (k <= 2) && ret == 0) || (ret != 0), 1); // 2 valid index 222 } 223 } 224 } 225 AppSpawnReqMsgFree(reqHandle); 226 } 227 228 /** 229 * @brief 测试接口:AppSpawnReqMsgSetAppDacInfo 230 * 231 */ 232 HWTEST_F(AppSpawnInterfaceTest, App_Spawn_Interface_Msg_App_Dac_001, TestSize.Level0) 233 { 234 AppDacInfo dacInfo; 235 dacInfo.uid = 20010029; // 20010029 test data 236 dacInfo.gid = 20010029; // 20010029 test data 237 dacInfo.gidCount = 2; // 2 count 238 dacInfo.gidTable[0] = 20010029; // 20010029 test data 239 dacInfo.gidTable[1] = 20010029 + 1; // 20010029 test data 240 241 AppSpawnReqMsgHandle reqHandle = nullptr; 242 int ret = AppSpawnReqMsgCreate(MSG_APP_SPAWN, "com.ohos.myapplication", &reqHandle); 243 EXPECT_EQ(0, ret); 244 const AppSpawnReqMsgHandle inputHandle[] = {reqHandle, nullptr}; 245 const AppDacInfo *dacInfo1[] = {&dacInfo, nullptr}; 246 247 for (size_t i = 0; i < ARRAY_LENGTH(inputHandle); i++) { 248 for (size_t j = 0; j < ARRAY_LENGTH(dacInfo1); j++) { 249 printf("App_Spawn_Interface_Msg_App_Dac_001 %zu %zu %d \n", i, j, ret); 250 ret = AppSpawnReqMsgSetAppDacInfo(inputHandle[i], dacInfo1[j]); 251 EXPECT_EQ((i == 0 && j == 0 && ret == 0) || (ret != 0), 1); 252 } 253 } 254 AppSpawnReqMsgFree(reqHandle); 255 } 256 257 /** 258 * @brief 测试接口:AppSpawnReqMsgSetAppDomainInfo 259 * 260 */ 261 HWTEST_F(AppSpawnInterfaceTest, App_Spawn_Interface_Set_App_Domain_001, TestSize.Level0) 262 { 263 AppSpawnReqMsgHandle reqHandle = nullptr; 264 char buffer[33]; 265 (void)memset_s(buffer, sizeof(buffer), 'A', sizeof(buffer)); 266 int ret = AppSpawnReqMsgCreate(MSG_APP_SPAWN, "com.ohos.myapplication", &reqHandle); 267 EXPECT_EQ(0, ret); 268 269 std::vector<char> name1(APP_APL_MAX_LEN - 1, 'a'); 270 name1.push_back('\0'); 271 std::vector<char> name2(APP_APL_MAX_LEN, 'b'); 272 name2.push_back('\0'); 273 std::vector<char> name3(APP_APL_MAX_LEN + 1, 'c'); 274 name3.push_back('\0'); 275 276 const AppSpawnReqMsgHandle inputHandle[] = {reqHandle, nullptr}; 277 const uint32_t hapFlags[] = {1}; 278 const char *apl[] = {"system_core", name1.data(), name2.data(), name3.data(), "", nullptr}; 279 280 for (size_t i = 0; i < ARRAY_LENGTH(inputHandle); i++) { 281 for (size_t j = 0; j < ARRAY_LENGTH(hapFlags); j++) { 282 for (size_t k = 0; k < ARRAY_LENGTH(apl); k++) { 283 printf("App_Spawn_Interface_Set_App_Domain_001 %zu %zu %zu %d \n", i, j, k, ret); 284 ret = AppSpawnReqMsgSetAppDomainInfo(inputHandle[i], hapFlags[j], apl[k]); 285 EXPECT_EQ((i == 0 && k <= 2 && ret == 0) || (ret != 0), 1); // 2 valid index 286 } 287 } 288 } 289 AppSpawnReqMsgFree(reqHandle); 290 } 291 292 /** 293 * @brief 测试接口:AppSpawnReqMsgSetAppInternetPermissionInfo 294 * 295 */ 296 HWTEST_F(AppSpawnInterfaceTest, App_Spawn_Interface_Set_App_Permission_Info_001, TestSize.Level0) 297 { 298 AppSpawnReqMsgHandle reqHandle = nullptr; 299 int ret = AppSpawnReqMsgCreate(MSG_APP_SPAWN, "com.ohos.myapplication", &reqHandle); 300 EXPECT_EQ(0, ret); 301 ret = AppSpawnReqMsgSetAppInternetPermissionInfo(reqHandle, 102, 102); 302 EXPECT_EQ(0, ret); 303 AppSpawnReqMsgFree(reqHandle); 304 } 305 306 HWTEST_F(AppSpawnInterfaceTest, App_Spawn_Interface_Set_App_Permission_Info_002, TestSize.Level0) 307 { 308 int ret = AppSpawnReqMsgSetAppInternetPermissionInfo(nullptr, 102, 102); 309 EXPECT_NE(0, ret); 310 } 311 312 /** 313 * @brief 测试接口:AppSpawnReqMsgSetAppAccessToken 314 * 315 */ 316 HWTEST_F(AppSpawnInterfaceTest, App_Spawn_Interface_Set_App_Access_Token_001, TestSize.Level0) 317 { 318 AppSpawnReqMsgHandle reqHandle = nullptr; 319 int ret = AppSpawnReqMsgCreate(MSG_APP_SPAWN, "com.ohos.myapplication", &reqHandle); 320 EXPECT_EQ(0, ret); 321 ret = AppSpawnReqMsgSetAppAccessToken(reqHandle, 12345678); 322 EXPECT_EQ(0, ret); 323 AppSpawnReqMsgFree(reqHandle); 324 } 325 326 HWTEST_F(AppSpawnInterfaceTest, App_Spawn_Interface_Set_App_Access_Token_002, TestSize.Level0) 327 { 328 int ret = AppSpawnReqMsgSetAppAccessToken(nullptr, 12345678); 329 EXPECT_NE(0, ret); 330 } 331 332 /** 333 * @brief 测试接口:AppSpawnReqMsgSetAppOwnerId 334 * 335 */ 336 HWTEST_F(AppSpawnInterfaceTest, App_Spawn_Interface_Set_App_Owner_001, TestSize.Level0) 337 { 338 AppSpawnReqMsgHandle reqHandle = nullptr; 339 int ret = AppSpawnReqMsgCreate(MSG_APP_SPAWN, "com.ohos.myapplication", &reqHandle); 340 EXPECT_EQ(0, ret); 341 342 std::vector<char> name1(APP_OWNER_ID_LEN - 1, 'a'); 343 name1.push_back('\0'); 344 std::vector<char> name2(APP_OWNER_ID_LEN, 'b'); 345 name2.push_back('\0'); 346 std::vector<char> name3(APP_OWNER_ID_LEN + 1, 'c'); 347 name3.push_back('\0'); 348 349 const AppSpawnReqMsgHandle inputHandle[] = {reqHandle, nullptr}; 350 const char *ownerId[] = {"FILE_ACCESS_MANAGER", name1.data(), name2.data(), name3.data(), "", nullptr}; 351 for (size_t i = 0; i < ARRAY_LENGTH(inputHandle); i++) { 352 for (size_t j = 0; j < ARRAY_LENGTH(ownerId); j++) { 353 printf("App_Spawn_Interface_Set_App_Owner_001 %zu %zu %d \n", i, j, ret); 354 ret = AppSpawnReqMsgSetAppOwnerId(inputHandle[i], ownerId[j]); 355 EXPECT_EQ((i == 0 && j <= 1 && ret == 0) || (ret != 0), 1); 356 } 357 } 358 AppSpawnReqMsgFree(reqHandle); 359 } 360 361 /** 362 * @brief 测试接口:AppSpawnReqMsgAddPermission 363 * 364 */ 365 HWTEST_F(AppSpawnInterfaceTest, App_Spawn_Interface_Add_Permission_001, TestSize.Level0) 366 { 367 AppSpawnReqMsgHandle reqHandle = nullptr; 368 int ret = AppSpawnReqMsgCreate(MSG_APP_SPAWN, "com.ohos.myapplication", &reqHandle); 369 EXPECT_EQ(0, ret); 370 371 const AppSpawnReqMsgHandle inputHandle[] = {reqHandle, nullptr}; 372 const char *permission[] = {"ohos.permission.READ_IMAGEVIDEO", "", nullptr}; 373 for (size_t i = 0; i < ARRAY_LENGTH(inputHandle); i++) { 374 for (size_t j = 0; j < ARRAY_LENGTH(permission); j++) { 375 printf("App_Spawn_Interface_Add_Permission_001 %zu %zu %d \n", i, j, ret); 376 ret = AppSpawnReqMsgAddPermission(inputHandle[i], permission[j]); 377 EXPECT_EQ((i == 0 && j == 0 && ret == 0) || (ret != 0), 1); 378 } 379 } 380 AppSpawnReqMsgFree(reqHandle); 381 } 382 383 /** 384 * @brief 测试接口:AppSpawnReqMsgAddFd 385 * 386 */ 387 HWTEST_F(AppSpawnInterfaceTest, App_Spawn_Interface_Add_Fd_001, TestSize.Level0) 388 { 389 AppSpawnReqMsgHandle reqHandle = nullptr; 390 int ret = AppSpawnReqMsgCreate(MSG_APP_SPAWN, "com.ohos.myapplication", &reqHandle); 391 EXPECT_EQ(0, ret); 392 const AppSpawnReqMsgHandle inputHandle[] = {reqHandle, nullptr}; 393 static const struct { 394 const char *name; 395 int fd; 396 } fdInfoMap[] = { 397 {"fd1", 10}, 398 {"fd2", -1}, 399 {"fd3000000000000000000000000000000", 101}, 400 }; 401 for (size_t i = 0; i < ARRAY_LENGTH(inputHandle); i++) { 402 for (size_t j = 0; j < ARRAY_LENGTH(fdInfoMap); j++) { 403 printf("App_Spawn_Interface_Add_fd_001 %zu %zu %d \n", i, j, ret); 404 ret = AppSpawnReqMsgAddFd(inputHandle[i], fdInfoMap[j].name, fdInfoMap[j].fd); 405 EXPECT_EQ((i == 0 && j == 0 && ret == 0) || (ret != 0), 1); 406 } 407 } 408 AppSpawnReqMsgFree(reqHandle); 409 } 410 411 /** 412 * @brief 测试接口:AppSpawnReqMsgAddExtInfo 413 * 414 */ 415 HWTEST_F(AppSpawnInterfaceTest, App_Spawn_Interface_Add_Ext_Info_001, TestSize.Level0) 416 { 417 AppSpawnReqMsgHandle reqHandle = nullptr; 418 int ret = AppSpawnReqMsgCreate(MSG_APP_SPAWN, "com.ohos.myapplication", &reqHandle); 419 EXPECT_EQ(0, ret); 420 const char *inputName[] = {"tlv-name-1", 421 "234567890123456789012345678901", 422 "2345678901234567890123456789012", 423 "23456789012345678901234567890123", 424 "", nullptr}; 425 std::vector<uint8_t> data(EXTRAINFO_TOTAL_LENGTH_MAX + 1, static_cast<uint8_t>('c')); 426 427 const uint8_t *inputData[] = {data.data(), data.data(), data.data(), data.data(), nullptr}; 428 const size_t inputvalueLen[] = {23, 429 EXTRAINFO_TOTAL_LENGTH_MAX - 1, 430 EXTRAINFO_TOTAL_LENGTH_MAX, 431 EXTRAINFO_TOTAL_LENGTH_MAX + 1, 432 0}; 433 434 for (size_t j = 0; j < ARRAY_LENGTH(inputName); j++) { 435 for (size_t k = 0; k < ARRAY_LENGTH(inputData); k++) { 436 for (size_t l = 0; l < ARRAY_LENGTH(inputvalueLen); l++) { 437 ret = AppSpawnReqMsgAddExtInfo(reqHandle, 438 inputName[j], inputData[k], static_cast<uint32_t>(inputvalueLen[l])); 439 printf("App_Spawn_Interface_Add_Ext_Info_001 %zu %zu %zu %d \n", j, k, l, ret); 440 EXPECT_EQ((j <= 2 && k <= 4 && l <= 2 && ret == 0) || (ret != 0), 1); // 2 4 valid index 441 } 442 } 443 } 444 for (size_t j = 0; j < ARRAY_LENGTH(inputName); j++) { 445 for (size_t k = 0; k < ARRAY_LENGTH(inputData); k++) { 446 for (size_t l = 0; l < ARRAY_LENGTH(inputvalueLen); l++) { 447 ret = AppSpawnReqMsgAddExtInfo(nullptr, 448 inputName[j], inputData[k], static_cast<uint32_t>(inputvalueLen[l])); 449 EXPECT_EQ(ret != 0, 1); 450 } 451 } 452 } 453 AppSpawnReqMsgFree(reqHandle); 454 } 455 456 /** 457 * @brief 测试接口:AppSpawnReqMsgAddStringInfo 458 * 459 */ 460 HWTEST_F(AppSpawnInterfaceTest, App_Spawn_Interface_Add_String_Info_001, TestSize.Level0) 461 { 462 AppSpawnReqMsgHandle reqHandle = nullptr; 463 int ret = AppSpawnReqMsgCreate(MSG_APP_SPAWN, "com.ohos.myapplication", &reqHandle); 464 EXPECT_EQ(0, ret); 465 466 std::vector<char> name1(23, 'a'); // 23 test data 467 name1.push_back('\0'); 468 std::vector<char> name2(EXTRAINFO_TOTAL_LENGTH_MAX - 1, 'b'); 469 name2.push_back('\0'); 470 std::vector<char> name3(EXTRAINFO_TOTAL_LENGTH_MAX, 'c'); 471 name3.push_back('\0'); 472 std::vector<char> name4(EXTRAINFO_TOTAL_LENGTH_MAX + 1, 'd'); 473 name4.push_back('\0'); 474 475 const char *inputName[] = {"tlv-name-1", 476 "234567890123456789012345678901", 477 "2345678901234567890123456789012", 478 "23456789012345678901234567890123", 479 "", nullptr}; 480 const char *inputData[] = {name1.data(), name2.data(), name3.data(), name4.data(), nullptr}; 481 482 for (size_t j = 0; j < ARRAY_LENGTH(inputName); j++) { 483 for (size_t k = 0; k < ARRAY_LENGTH(inputData); k++) { 484 printf("App_Spawn_Interface_Add_String_Info_001 %zu %zu %d \n", j, k, ret); 485 ret = AppSpawnReqMsgAddStringInfo(reqHandle, inputName[j], inputData[k]); 486 EXPECT_EQ((j <= 2 && k <= 1 && ret == 0) || (ret != 0), 1); 487 } 488 } 489 490 for (size_t j = 0; j < ARRAY_LENGTH(inputName); j++) { 491 for (size_t k = 0; k < ARRAY_LENGTH(inputData); k++) { 492 ret = AppSpawnReqMsgAddStringInfo(nullptr, inputName[j], inputData[k]); 493 EXPECT_EQ(ret != 0, 1); 494 } 495 } 496 AppSpawnReqMsgFree(reqHandle); 497 } 498 499 /** 500 * @brief 测试flags set 501 * 502 */ 503 HWTEST_F(AppSpawnInterfaceTest, App_Spawn_Set_Msg_Flags_001, TestSize.Level0) 504 { 505 AppSpawnReqMsgHandle reqHandle = 0; 506 int ret = AppSpawnReqMsgCreate(MSG_APP_SPAWN, "com.example.myapplication", &reqHandle); 507 EXPECT_EQ(ret, 0); 508 509 for (int32_t i = 0; i <= MAX_FLAGS_INDEX; i++) { 510 ret = AppSpawnReqMsgSetAppFlag(reqHandle, static_cast<AppFlagsIndex>(i)); 511 printf(" App_Spawn_Set_Msg_Flags_001 %d %d \n", i, ret); 512 EXPECT_EQ((i != MAX_FLAGS_INDEX && ret == 0) || (ret != 0), 1); 513 } 514 AppSpawnReqMsgFree(reqHandle); 515 } 516 517 HWTEST_F(AppSpawnInterfaceTest, App_Spawn_TerminateMsg_001, TestSize.Level0) 518 { 519 AppSpawnReqMsgHandle reqHandle = 0; 520 int ret = AppSpawnTerminateMsgCreate(0, &reqHandle); 521 EXPECT_EQ(ret, 0); 522 AppSpawnReqMsgFree(reqHandle); 523 524 ret = AppSpawnTerminateMsgCreate(0, nullptr); 525 EXPECT_EQ(ret, APPSPAWN_ARG_INVALID); 526 } 527 528 /** 529 * @brief 测试add permission 530 * 531 */ 532 HWTEST_F(AppSpawnInterfaceTest, App_Spawn_Permission_001, TestSize.Level0) 533 { 534 AppSpawnClientHandle clientHandle; 535 AppSpawnReqMsgHandle reqHandle = 0; 536 int ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &clientHandle); 537 EXPECT_EQ(ret, 0); 538 539 ret = AppSpawnReqMsgCreate(MSG_APP_SPAWN, "com.example.myapplication", &reqHandle); 540 EXPECT_EQ(ret, 0); 541 542 const int32_t inputCount = 2; 543 const char *permissions[] = {"ohos.permission.ACCESS_BUNDLE_DIR", "", "3333333333", nullptr}; 544 const AppSpawnClientHandle inputClientHandle[inputCount] = {clientHandle, nullptr}; 545 const AppSpawnReqMsgHandle inputReqHandle[inputCount] = {reqHandle, nullptr}; 546 for (int32_t i = 0; i < inputCount; i++) { 547 for (int32_t j = 0; j < inputCount; j++) { 548 for (int32_t k = 0; k < 4; k++) { // 4 sizeof permissions 549 ret = AppSpawnClientAddPermission(inputClientHandle[i], inputReqHandle[j], permissions[k]); 550 printf(" AppSpawnClientAddPermission %d %d %d %d \n", i, j, k, ret); 551 EXPECT_EQ(i == 0 && j == 0 && k == 0 && ret == 0 || ret != 0, 1); 552 } 553 } 554 } 555 AppSpawnReqMsgFree(reqHandle); 556 AppSpawnClientDestroy(clientHandle); 557 } 558 559 /** 560 * @brief 测试接口:GetPermissionByIndex 561 * 562 */ 563 HWTEST_F(AppSpawnInterfaceTest, App_Spawn_Interface_GetPermissionByIndex_001, TestSize.Level0) 564 { 565 int32_t maxIndex = GetMaxPermissionIndex(nullptr); 566 for (int i = 0; i < maxIndex; i++) { 567 EXPECT_NE(GetPermissionByIndex(nullptr, i), nullptr); 568 } 569 570 EXPECT_EQ(GetPermissionByIndex(nullptr, maxIndex), nullptr); 571 EXPECT_EQ(GetPermissionByIndex(nullptr, -1), nullptr); 572 573 int32_t index = GetPermissionIndex(nullptr, "ohos.permission.ACCESS_BUNDLE_DIR"); 574 int ret = index >= 0 && index < maxIndex ? 0 : -1; 575 EXPECT_EQ(0, ret); 576 } 577 578 HWTEST_F(AppSpawnInterfaceTest, App_Spawn_Interface_ClientSocket_001, TestSize.Level0) 579 { 580 int socketId = CreateClientSocket(CLIENT_FOR_APPSPAWN, 2); 581 CloseClientSocket(socketId); 582 socketId = CreateClientSocket(CLIENT_FOR_NWEBSPAWN, 2); 583 CloseClientSocket(socketId); 584 socketId = CreateClientSocket(CLIENT_MAX, 2); 585 CloseClientSocket(socketId); 586 CloseClientSocket(-1); 587 } 588 589 HWTEST_F(AppSpawnInterfaceTest, App_Spawn_Interface_GetSpawnTimeout_001, TestSize.Level0) 590 { 591 int timeout = GetSpawnTimeout(6); // 6 test 592 EXPECT_EQ(timeout >= 6, 1); // 6 test 593 timeout = GetSpawnTimeout(6); // 6 test 594 EXPECT_EQ(timeout >= 6, 1); // 6 test 595 timeout = GetSpawnTimeout(6); // 6 test 596 EXPECT_EQ(timeout >= 6, 1); // 6 test 597 timeout = GetSpawnTimeout(2); // 2 test 598 EXPECT_EQ(timeout >= 2, 1); // 2 test 599 timeout = GetSpawnTimeout(2); // 2 test 600 EXPECT_EQ(timeout >= 2, 1); // 2 test 601 timeout = GetSpawnTimeout(2); // 2 test 602 EXPECT_EQ(timeout >= 2, 1); // 2 test 603 } 604 } // namespace OHOS 605