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 <cerrno>
16 #include <cstdlib>
17 #include <cstring>
18 #include <memory>
19 #include <string>
20 #include <unistd.h>
21
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 "json_utils.h"
30 #include "parameter.h"
31 #include "securec.h"
32
33 #include "app_spawn_stub.h"
34 #include "app_spawn_test_helper.h"
35
36 using namespace testing;
37 using namespace testing::ext;
38 using namespace OHOS;
39
40 namespace OHOS {
41 static AppSpawnTestHelper g_testHelper;
42 class AppSpawnChildTest : public testing::Test {
43 public:
SetUpTestCase()44 static void SetUpTestCase() {}
TearDownTestCase()45 static void TearDownTestCase() {}
SetUp()46 void SetUp() {}
TearDown()47 void TearDown() {}
48 };
49
TestRunChildProcessor(AppSpawnContent * content,AppSpawnClient * client)50 static int TestRunChildProcessor(AppSpawnContent *content, AppSpawnClient *client)
51 {
52 APPSPAWN_LOGV("TestRunChildProcessor %{public}u", client->id);
53 AppSpawnEnvClear(content, client);
54 usleep(1000); // 1000 1ms
55 return 0;
56 }
57
CreateTestAppSpawnContent(const char * name,uint32_t mode)58 static AppSpawnContent *CreateTestAppSpawnContent(const char *name, uint32_t mode)
59 {
60 static char path[PATH_MAX] = {};
61 AppSpawnContent *content = AppSpawnCreateContent(APPSPAWN_SOCKET_NAME, path, sizeof(path), MODE_FOR_APP_SPAWN);
62 APPSPAWN_CHECK_ONLY_EXPER(content != nullptr, return nullptr);
63
64 ServerStageHookExecute(STAGE_SERVER_PRELOAD, content); // 预加载,解析sandbox
65 AddAppSpawnHook(STAGE_CHILD_PRE_RUN, HOOK_PRIO_LOWEST, AppSpawnClearEnv); // clear
66 return content;
67 }
68
69 /**
70 * @brief 测试appspanw spawn的后半部分,子进程的处理
71 *
72 */
73 HWTEST_F(AppSpawnChildTest, App_Spawn_Child_001, TestSize.Level0)
74 {
75 AppSpawnClientHandle clientHandle = nullptr;
76 AppSpawnReqMsgHandle reqHandle = 0;
77 AppSpawningCtx *property = nullptr;
78 AppSpawnContent *content = nullptr;
79 int ret = -1;
80 do {
81 ret = AppSpawnClientInit(APPSPAWN_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_APP_SPAWN, 0);
84 APPSPAWN_CHECK(reqHandle != INVALID_REQ_HANDLE, break, "Failed to create req %{public}s", APPSPAWN_SERVER_NAME);
85 content = CreateTestAppSpawnContent(APPSPAWN_SOCKET_NAME, MODE_FOR_APP_SPAWN);
86 APPSPAWN_CHECK_ONLY_EXPER(content != nullptr, break);
87
88 ret = APPSPAWN_ARG_INVALID;
89 property = g_testHelper.GetAppProperty(clientHandle, reqHandle);
90 reqHandle = nullptr;
91 APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break);
92
93 // spawn prepare process
94 AppSpawnHookExecute(STAGE_PARENT_PRE_FORK, 0, content, &property->client);
95
96 // spawn
97 ret = AppSpawnChild(content, &property->client);
98 property = nullptr;
99 } while (0);
100 DeleteAppSpawningCtx(property);
101 AppSpawnReqMsgFree(reqHandle);
102 AppSpawnClientDestroy(clientHandle);
103 ASSERT_EQ(ret, 0);
104 }
105
106 /**
107 * @brief 测试appspanw spawn的后半部分,子进程的处理
108 *
109 */
110 HWTEST_F(AppSpawnChildTest, App_Spawn_Child_002, TestSize.Level0)
111 {
112 AppSpawnClientHandle clientHandle = nullptr;
113 AppSpawnReqMsgHandle reqHandle = 0;
114 AppSpawningCtx *property = nullptr;
115 AppSpawnContent *content = nullptr;
116 int ret = -1;
117 do {
118 ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &clientHandle);
119 APPSPAWN_CHECK(ret == 0, break, "Failed to create reqMgr %{public}s", APPSPAWN_SERVER_NAME);
120 reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_APP_SPAWN, 0);
121 APPSPAWN_CHECK(reqHandle != INVALID_REQ_HANDLE, break, "Failed to create req %{public}s", APPSPAWN_SERVER_NAME);
122 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_DEBUGGABLE);
123 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_NATIVEDEBUG);
124 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_BUNDLE_RESOURCES);
125 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_ACCESS_BUNDLE_DIR);
126
127 content = CreateTestAppSpawnContent(APPSPAWN_SOCKET_NAME, MODE_FOR_APP_SPAWN);
128 APPSPAWN_CHECK_ONLY_EXPER(content != nullptr, break);
129
130 ret = APPSPAWN_ARG_INVALID;
131 property = g_testHelper.GetAppProperty(clientHandle, reqHandle);
132 reqHandle = nullptr;
133 APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break);
134
135 // spawn prepare process
136 AppSpawnHookExecute(STAGE_PARENT_PRE_FORK, 0, content, &property->client);
137 // spawn
138 ret = AppSpawnChild(content, &property->client);
139 property = nullptr;
140 } while (0);
141 DeleteAppSpawningCtx(property);
142 AppSpawnReqMsgFree(reqHandle);
143 AppSpawnClientDestroy(clientHandle);
144 ASSERT_EQ(ret, 0);
145 }
146
147 HWTEST_F(AppSpawnChildTest, App_Spawn_Child_003, TestSize.Level0)
148 {
149 AppSpawnClientHandle clientHandle = nullptr;
150 AppSpawnReqMsgHandle reqHandle = 0;
151 AppSpawningCtx *property = nullptr;
152 AppSpawnContent *content = nullptr;
153 int ret = -1;
154 do {
155 ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &clientHandle);
156 APPSPAWN_CHECK(ret == 0, break, "Failed to create reqMgr %{public}s", APPSPAWN_SERVER_NAME);
157
158 g_testHelper.SetTestUid(10010029); // 10010029
159 reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_APP_SPAWN, 1);
160 APPSPAWN_CHECK(reqHandle != INVALID_REQ_HANDLE, break, "Failed to create req %{public}s", APPSPAWN_SERVER_NAME);
161 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_DEBUGGABLE);
162 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_NATIVEDEBUG);
163 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_BUNDLE_RESOURCES);
164 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_ACCESS_BUNDLE_DIR);
165
166 content = CreateTestAppSpawnContent(APPSPAWN_SOCKET_NAME, MODE_FOR_APP_SPAWN);
167 APPSPAWN_CHECK_ONLY_EXPER(content != nullptr, break);
168
169 ret = APPSPAWN_ARG_INVALID;
170 property = g_testHelper.GetAppProperty(clientHandle, reqHandle);
171 reqHandle = nullptr;
172 APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break);
173
174 // spawn prepare process
175 AppSpawnHookExecute(STAGE_PARENT_PRE_FORK, 0, content, &property->client);
176 // spawn
177 ret = AppSpawnChild(content, &property->client);
178 property = nullptr;
179 } while (0);
180 DeleteAppSpawningCtx(property);
181 AppSpawnReqMsgFree(reqHandle);
182 AppSpawnClientDestroy(clientHandle);
183 ASSERT_EQ(ret, 0);
184 }
185
186 HWTEST_F(AppSpawnChildTest, App_Spawn_Child_004, TestSize.Level0)
187 {
188 AppSpawnClientHandle clientHandle = nullptr;
189 AppSpawnReqMsgHandle reqHandle = 0;
190 AppSpawningCtx *property = nullptr;
191 AppSpawnContent *content = nullptr;
192 int ret = -1;
193 do {
194 ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &clientHandle);
195 APPSPAWN_CHECK(ret == 0, break, "Failed to create reqMgr %{public}s", APPSPAWN_SERVER_NAME);
196 // MSG_SPAWN_NATIVE_PROCESS and no render cmd
197 g_testHelper.SetTestUid(10010029); // 10010029
198 reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_SPAWN_NATIVE_PROCESS, 1);
199 APPSPAWN_CHECK(reqHandle != INVALID_REQ_HANDLE, break, "Failed to create req %{public}s", APPSPAWN_SERVER_NAME);
200 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_DEBUGGABLE);
201 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_NATIVEDEBUG);
202 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_BUNDLE_RESOURCES);
203 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_ACCESS_BUNDLE_DIR);
204 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_GWP_ENABLED_NORMAL);
205
206 ret = APPSPAWN_ARG_INVALID;
207 property = g_testHelper.GetAppProperty(clientHandle, reqHandle);
208 reqHandle = nullptr;
209 APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break);
210
211 content = CreateTestAppSpawnContent(APPSPAWN_SOCKET_NAME, MODE_FOR_APP_SPAWN);
212 APPSPAWN_CHECK_ONLY_EXPER(content != nullptr, break);
213
214 // spawn prepare process
215 AppSpawnHookExecute(STAGE_PARENT_PRE_FORK, 0, content, &property->client);
216 // spawn
217 ret = AppSpawnChild(content, &property->client);
218 property = nullptr;
219 } while (0);
220 DeleteAppSpawningCtx(property);
221 AppSpawnReqMsgFree(reqHandle);
222 AppSpawnClientDestroy(clientHandle);
223 ASSERT_EQ(ret, 0);
224 }
225
226 HWTEST_F(AppSpawnChildTest, App_Spawn_Child_005, TestSize.Level0)
227 {
228 AppSpawnClientHandle clientHandle = nullptr;
229 AppSpawnReqMsgHandle reqHandle = 0;
230 AppSpawningCtx *property = nullptr;
231 AppSpawnContent *content = nullptr;
232 int ret = -1;
233 do {
234 ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &clientHandle);
235 APPSPAWN_CHECK(ret == 0, break, "Failed to create reqMgr %{public}s", APPSPAWN_SERVER_NAME);
236 // MSG_SPAWN_NATIVE_PROCESS and render
237 g_testHelper.SetTestUid(10010029); // 10010029
238 reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_SPAWN_NATIVE_PROCESS, 0);
239 APPSPAWN_CHECK(reqHandle != INVALID_REQ_HANDLE, break, "Failed to create req %{public}s", APPSPAWN_SERVER_NAME);
240 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_DEBUGGABLE);
241 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_NATIVEDEBUG);
242 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_BUNDLE_RESOURCES);
243 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_ACCESS_BUNDLE_DIR);
244 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_GWP_ENABLED_NORMAL);
245
246 ret = APPSPAWN_ARG_INVALID;
247 property = g_testHelper.GetAppProperty(clientHandle, reqHandle);
248 reqHandle = nullptr;
249 APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break);
250
251 content = CreateTestAppSpawnContent(APPSPAWN_SOCKET_NAME, MODE_FOR_APP_SPAWN);
252 APPSPAWN_CHECK_ONLY_EXPER(content != nullptr, break);
253
254 // spawn prepare process
255 AppSpawnHookExecute(STAGE_PARENT_PRE_FORK, 0, content, &property->client);
256 // spawn
257 ret = AppSpawnChild(content, &property->client);
258 property = nullptr;
259 ASSERT_EQ(ret, 0);
260 } while (0);
261 DeleteAppSpawningCtx(property);
262 AppSpawnReqMsgFree(reqHandle);
263 AppSpawnClientDestroy(clientHandle);
264 ASSERT_EQ(ret, 0);
265 }
266
267 HWTEST_F(AppSpawnChildTest, App_Spawn_Child_006, TestSize.Level0)
268 {
269 AppSpawnClientHandle clientHandle = nullptr;
270 AppSpawnReqMsgHandle reqHandle = 0;
271 AppSpawningCtx *property = nullptr;
272 AppSpawnContent *content = nullptr;
273 int ret = -1;
274 do {
275 ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &clientHandle);
276 APPSPAWN_CHECK(ret == 0, break, "Failed to create reqMgr %{public}s", APPSPAWN_SERVER_NAME);
277 // MSG_SPAWN_NATIVE_PROCESS and no render cmd
278 g_testHelper.SetTestUid(10010029); // 10010029
279 reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_SPAWN_NATIVE_PROCESS, 0);
280 APPSPAWN_CHECK(reqHandle != INVALID_REQ_HANDLE, break, "Failed to create req %{public}s", APPSPAWN_SERVER_NAME);
281 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_DEBUGGABLE);
282 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_NATIVEDEBUG);
283 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_BUNDLE_RESOURCES);
284 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_ACCESS_BUNDLE_DIR);
285 const char *appEnv = "{\"test.name1\": \"test.value1\", \"test.name2\": \"test.value2\"}";
286 ret = AppSpawnReqMsgAddExtInfo(reqHandle, "AppEnv",
287 reinterpret_cast<uint8_t *>(const_cast<char *>(appEnv)), strlen(appEnv) + 1);
288 APPSPAWN_CHECK(ret == 0, break, "Failed to add ext tlv %{public}s", appEnv);
289
290 ret = APPSPAWN_ARG_INVALID;
291 property = g_testHelper.GetAppProperty(clientHandle, reqHandle);
292 reqHandle = nullptr;
293 APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break);
294
295 content = CreateTestAppSpawnContent(APPSPAWN_SOCKET_NAME, MODE_FOR_APP_SPAWN);
296 APPSPAWN_CHECK_ONLY_EXPER(content != nullptr, break);
297
298 // spawn prepare process
299 AppSpawnHookExecute(STAGE_PARENT_PRE_FORK, 0, content, &property->client);
300 // spawn
301 ret = AppSpawnChild(content, &property->client);
302 property = nullptr;
303 ASSERT_EQ(ret, 0);
304 } while (0);
305 DeleteAppSpawningCtx(property);
306 AppSpawnReqMsgFree(reqHandle);
307 AppSpawnClientDestroy(clientHandle);
308 ASSERT_EQ(ret, 0);
309 }
310
311 /**
312 * @brief 子进程nweb后半部分处理
313 *
314 */
315 HWTEST_F(AppSpawnChildTest, NWeb_Spawn_Child_001, TestSize.Level0)
316 {
317 AppSpawnClientHandle clientHandle = nullptr;
318 AppSpawnReqMsgHandle reqHandle = 0;
319 AppSpawningCtx *property = nullptr;
320 AppSpawnContent *content = nullptr;
321 int ret = -1;
322 do {
323 ret = AppSpawnClientInit(NWEBSPAWN_SERVER_NAME, &clientHandle);
324 APPSPAWN_CHECK(ret == 0, break, "Failed to create reqMgr %{public}s", NWEBSPAWN_SERVER_NAME);
325 reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_APP_SPAWN, 0);
326 APPSPAWN_CHECK(reqHandle != INVALID_REQ_HANDLE, break, "Failed to create req ");
327
328 content = CreateTestAppSpawnContent(NWEBSPAWN_SOCKET_NAME, MODE_FOR_NWEB_SPAWN);
329 APPSPAWN_CHECK_ONLY_EXPER(content != nullptr, break);
330
331 ret = APPSPAWN_ARG_INVALID;
332 property = g_testHelper.GetAppProperty(clientHandle, reqHandle);
333 APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break);
334
335 // spawn prepare process
336 AppSpawnHookExecute(STAGE_PARENT_PRE_FORK, 0, content, &property->client);
337
338 // spawn
339 property->client.flags &= ~APP_COLD_START;
340 ret = AppSpawnChild(content, &property->client);
341 property = nullptr;
342 content = nullptr;
343 } while (0);
344 DeleteAppSpawningCtx(property);
345 AppSpawnClientDestroy(clientHandle);
346 AppSpawnDestroyContent(content);
347 LE_StopLoop(LE_GetDefaultLoop());
348 LE_CloseLoop(LE_GetDefaultLoop());
349 ASSERT_EQ(ret, 0);
350 }
351
352 HWTEST_F(AppSpawnChildTest, NWeb_Spawn_Child_002, TestSize.Level0)
353 {
354 AppSpawnClientHandle clientHandle = nullptr;
355 AppSpawnReqMsgHandle reqHandle = 0;
356 AppSpawningCtx *property = nullptr;
357 AppSpawnContent *content = nullptr;
358 int ret = -1;
359 do {
360 ret = AppSpawnClientInit(NWEBSPAWN_SERVER_NAME, &clientHandle);
361 APPSPAWN_CHECK(ret == 0, break, "Failed to create reqMgr %{public}s", NWEBSPAWN_SERVER_NAME);
362 reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_APP_SPAWN, 0);
363 APPSPAWN_CHECK(reqHandle != INVALID_REQ_HANDLE, break, "Failed to create req");
364
365 content = CreateTestAppSpawnContent(NWEBSPAWN_SOCKET_NAME, MODE_FOR_NWEB_SPAWN);
366 APPSPAWN_CHECK_ONLY_EXPER(content != nullptr, break);
367
368 ret = APPSPAWN_ARG_INVALID;
369 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_DEBUGGABLE);
370 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_NATIVEDEBUG);
371 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_BUNDLE_RESOURCES);
372 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_ACCESS_BUNDLE_DIR);
373 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_GWP_ENABLED_NORMAL);
374
375 property = g_testHelper.GetAppProperty(clientHandle, reqHandle);
376 APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break);
377 // spawn prepare process
378 AppSpawnHookExecute(STAGE_PARENT_PRE_FORK, 0, content, &property->client);
379
380 // spawn
381 ret = AppSpawnChild(content, &property->client);
382 property = nullptr;
383 content = nullptr;
384 } while (0);
385 DeleteAppSpawningCtx(property);
386 AppSpawnClientDestroy(clientHandle);
387 AppSpawnDestroyContent(content);
388 LE_StopLoop(LE_GetDefaultLoop());
389 LE_CloseLoop(LE_GetDefaultLoop());
390 ASSERT_EQ(ret, 0);
391 }
392
393 HWTEST_F(AppSpawnChildTest, NWeb_Spawn_Child_004, TestSize.Level0)
394 {
395 AppSpawnClientHandle clientHandle = nullptr;
396 AppSpawnReqMsgHandle reqHandle = 0;
397 AppSpawningCtx *property = nullptr;
398 AppSpawnContent *content = nullptr;
399 int ret = -1;
400 do {
401 ret = AppSpawnClientInit(NWEBSPAWN_SERVER_NAME, &clientHandle);
402 APPSPAWN_CHECK(ret == 0, break, "Failed to create reqMgr %{public}s", NWEBSPAWN_SERVER_NAME);
403 // MSG_SPAWN_NATIVE_PROCESS and no render cmd
404 g_testHelper.SetTestUid(10010029); // 10010029
405 reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_SPAWN_NATIVE_PROCESS, 0);
406 APPSPAWN_CHECK(reqHandle != INVALID_REQ_HANDLE, break, "Failed to create req");
407 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_DEBUGGABLE);
408 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_NATIVEDEBUG);
409 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_BUNDLE_RESOURCES);
410 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_ACCESS_BUNDLE_DIR);
411 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_GWP_ENABLED_NORMAL);
412
413 content = CreateTestAppSpawnContent(NWEBSPAWN_SOCKET_NAME, MODE_FOR_NWEB_SPAWN);
414 APPSPAWN_CHECK_ONLY_EXPER(content != nullptr, break);
415
416 ret = APPSPAWN_ARG_INVALID;
417 property = g_testHelper.GetAppProperty(clientHandle, reqHandle);
418 APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break);
419
420 // spawn prepare process
421 property->client.flags &= ~APP_COLD_START;
422 AppSpawnHookExecute(STAGE_PARENT_PRE_FORK, 0, content, &property->client);
423 // spawn
424 ret = AppSpawnChild(content, &property->client);
425 property = nullptr;
426 content = nullptr;
427 } while (0);
428 DeleteAppSpawningCtx(property);
429 AppSpawnClientDestroy(clientHandle);
430 AppSpawnDestroyContent(content);
431 LE_StopLoop(LE_GetDefaultLoop());
432 LE_CloseLoop(LE_GetDefaultLoop());
433 ASSERT_EQ(ret, 0);
434 }
435
436 HWTEST_F(AppSpawnChildTest, NWeb_Spawn_Child_005, TestSize.Level0)
437 {
438 AppSpawnClientHandle clientHandle = nullptr;
439 AppSpawnReqMsgHandle reqHandle = 0;
440 AppSpawningCtx *property = nullptr;
441 AppSpawnContent *content = nullptr;
442 int ret = -1;
443 do {
444 ret = AppSpawnClientInit(NWEBSPAWN_SERVER_NAME, &clientHandle);
445 APPSPAWN_CHECK(ret == 0, break, "Failed to create reqMgr %{public}s", NWEBSPAWN_SERVER_NAME);
446 // MSG_SPAWN_NATIVE_PROCESS and render
447 g_testHelper.SetTestUid(10010029); // 10010029
448 reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_SPAWN_NATIVE_PROCESS, 0);
449 APPSPAWN_CHECK(reqHandle != INVALID_REQ_HANDLE, break, "Failed to create req");
450 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_DEBUGGABLE);
451 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_NATIVEDEBUG);
452 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_BUNDLE_RESOURCES);
453 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_ACCESS_BUNDLE_DIR);
454 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_GWP_ENABLED_NORMAL);
455
456 content = CreateTestAppSpawnContent(NWEBSPAWN_SOCKET_NAME, MODE_FOR_NWEB_SPAWN);
457 APPSPAWN_CHECK_ONLY_EXPER(content != nullptr, break);
458
459 ret = APPSPAWN_ARG_INVALID;
460 property = g_testHelper.GetAppProperty(clientHandle, reqHandle);
461 APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break);
462
463 // spawn prepare process
464 AppSpawnHookExecute(STAGE_PARENT_PRE_FORK, 0, content, &property->client);
465
466 // spawn
467 property->client.flags &= ~APP_COLD_START;
468 ret = AppSpawnChild(content, &property->client);
469 property = nullptr;
470 content = nullptr;
471 } while (0);
472 DeleteAppSpawningCtx(property);
473 AppSpawnClientDestroy(clientHandle);
474 AppSpawnDestroyContent(content);
475 LE_StopLoop(LE_GetDefaultLoop());
476 LE_CloseLoop(LE_GetDefaultLoop());
477 ASSERT_EQ(ret, 0);
478 }
479
GetColdRunArgs(AppSpawningCtx * property,bool isNweb,const char * arg)480 static std::string GetColdRunArgs(AppSpawningCtx *property, bool isNweb, const char *arg)
481 {
482 std::string argStr = arg;
483 int ret = WriteMsgToChild(property, isNweb);
484 APPSPAWN_CHECK(ret == 0, return nullptr,
485 "Failed to get shm for %{public}s errno %{public}d", GetProcessName(property), errno);
486
487 argStr += " -fd -1 0 ";
488 argStr += std::to_string(property->forkCtx.msgSize);
489 argStr += " -param ";
490 argStr += GetProcessName(property);
491 argStr += " ";
492 argStr += std::to_string(property->client.id);
493 return argStr;
494 }
495
496 /**
497 * @brief 测试冷启动后半部处理
498 *
499 */
500 HWTEST_F(AppSpawnChildTest, App_Spawn_Cold_Run_001, TestSize.Level0)
501 {
502 AppSpawnClientHandle clientHandle = nullptr;
503 AppSpawnReqMsgHandle reqHandle = 0;
504 AppSpawningCtx *property = nullptr;
505 AppSpawnContent *content = nullptr;
506 CmdArgs *args = nullptr;
507 int ret = -1;
508 do {
509 ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &clientHandle);
510 APPSPAWN_CHECK(ret == 0, break, "Failed to create reqMgr %{public}s", APPSPAWN_SERVER_NAME);
511 reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_APP_SPAWN, 0);
512 APPSPAWN_CHECK(reqHandle != INVALID_REQ_HANDLE, break, "Failed to create req %{public}s", APPSPAWN_SERVER_NAME);
513 // set cold start flags
514 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_COLD_BOOT);
515
516 ret = APPSPAWN_ARG_INVALID;
517 property = g_testHelper.GetAppProperty(clientHandle, reqHandle);
518 APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break);
519
520 std::string cmd = GetColdRunArgs(property, false, "appspawn -mode app_cold ");
521 content = AppSpawnTestHelper::StartSpawnServer(cmd, args);
522 APPSPAWN_CHECK_ONLY_EXPER(content != nullptr, break);
523
524 // spawn prepare process
525 AppSpawnHookExecute(STAGE_PARENT_PRE_FORK, 0, content, &property->client);
526 AppSpawnHookExecute(STAGE_CHILD_PRE_COLDBOOT, 0, content, &property->client);
527 // run in cold mode
528 // child run in TestRunChildProcessor
529 RegChildLooper(content, TestRunChildProcessor);
530 content->runAppSpawn(content, args->argc, args->argv);
531 ret = 0;
532 } while (0);
533 if (args) {
534 free(args);
535 }
536 DeleteAppSpawningCtx(property);
537 AppSpawnClientDestroy(clientHandle);
538 ASSERT_EQ(ret, 0);
539 }
540
541 HWTEST_F(AppSpawnChildTest, App_Spawn_Cold_Run_002, TestSize.Level0)
542 {
543 AppSpawnClientHandle clientHandle = nullptr;
544 AppSpawnReqMsgHandle reqHandle = 0;
545 AppSpawningCtx *property = nullptr;
546 AppSpawnContent *content = nullptr;
547 CmdArgs *args = nullptr;
548 int ret = -1;
549 do {
550 ret = AppSpawnClientInit(NWEBSPAWN_SERVER_NAME, &clientHandle);
551 APPSPAWN_CHECK(ret == 0, break, "Failed to create reqMgr %{public}s", NWEBSPAWN_SERVER_NAME);
552 reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_APP_SPAWN, 0);
553 APPSPAWN_CHECK(reqHandle != INVALID_REQ_HANDLE, break, "Failed to create req ");
554 // set cold start flags
555 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_COLD_BOOT);
556
557 ret = APPSPAWN_ARG_INVALID;
558 property = g_testHelper.GetAppProperty(clientHandle, reqHandle);
559 APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break);
560
561 std::string cmd = GetColdRunArgs(property, true, "appspawn -mode nweb_cold ");
562 content = AppSpawnTestHelper::StartSpawnServer(cmd, args);
563 APPSPAWN_CHECK_ONLY_EXPER(content != nullptr, break);
564 ASSERT_EQ(content->mode, MODE_FOR_NWEB_COLD_RUN);
565
566 // spawn prepare process
567 AppSpawnHookExecute(STAGE_PARENT_PRE_FORK, 0, content, &property->client);
568 AppSpawnHookExecute(STAGE_CHILD_PRE_COLDBOOT, 0, content, &property->client);
569 // run in cold mode
570 // child run in TestRunChildProcessor
571 RegChildLooper(content, TestRunChildProcessor);
572 content->runAppSpawn(content, args->argc, args->argv);
573 ret = 0;
574 } while (0);
575 if (args) {
576 free(args);
577 }
578 DeleteAppSpawningCtx(property);
579 DeleteAppSpawnMgr(GetAppSpawnMgr());
580 AppSpawnClientDestroy(clientHandle);
581 ASSERT_EQ(ret, 0);
582 }
583
584 HWTEST_F(AppSpawnChildTest, App_Spawn_Cold_Run_003, TestSize.Level0)
585 {
586 AppSpawnClientHandle clientHandle = nullptr;
587 AppSpawnReqMsgHandle reqHandle = 0;
588 AppSpawningCtx *property = nullptr;
589 AppSpawnContent *content = nullptr;
590 CmdArgs *args = nullptr;
591 int ret = -1;
592 do {
593 ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &clientHandle);
594 APPSPAWN_CHECK(ret == 0, break, "Failed to create reqMgr %{public}s", APPSPAWN_SERVER_NAME);
595 reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_APP_SPAWN, 0);
596 APPSPAWN_CHECK(reqHandle != INVALID_REQ_HANDLE, break, "Failed to create req %{public}s", APPSPAWN_SERVER_NAME);
597
598 // asan set cold
599 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_DEBUGGABLE);
600 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_NATIVEDEBUG);
601 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_BUNDLE_RESOURCES);
602 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_ACCESS_BUNDLE_DIR);
603 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_ASANENABLED);
604 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_GWP_ENABLED_FORCE);
605
606 ret = APPSPAWN_ARG_INVALID;
607 property = g_testHelper.GetAppProperty(clientHandle, reqHandle);
608 APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break);
609
610 std::string cmd = GetColdRunArgs(property, false, "appspawn -mode app_cold ");
611 content = AppSpawnTestHelper::StartSpawnServer(cmd, args);
612 APPSPAWN_CHECK_ONLY_EXPER(content != nullptr, break);
613 ASSERT_EQ(content->mode, MODE_FOR_APP_COLD_RUN);
614
615 // spawn prepare process
616 AppSpawnHookExecute(STAGE_PARENT_PRE_FORK, 0, content, &property->client);
617 AppSpawnHookExecute(STAGE_CHILD_PRE_COLDBOOT, 0, content, &property->client);
618 // run in cold mode
619 // child run in TestRunChildProcessor
620 RegChildLooper(content, TestRunChildProcessor);
621 content->runAppSpawn(content, args->argc, args->argv);
622 ret = 0;
623 } while (0);
624 if (args) {
625 free(args);
626 }
627 DeleteAppSpawningCtx(property);
628 DeleteAppSpawnMgr(GetAppSpawnMgr());
629 AppSpawnClientDestroy(clientHandle);
630 ASSERT_EQ(ret, 0);
631 }
632
633 HWTEST_F(AppSpawnChildTest, App_Spawn_Cold_Run_004, TestSize.Level0)
634 {
635 AppSpawnClientHandle clientHandle = nullptr;
636 AppSpawnReqMsgHandle reqHandle = 0;
637 AppSpawningCtx *property = nullptr;
638 AppSpawnContent *content = nullptr;
639 CmdArgs *args = nullptr;
640 int ret = -1;
641 do {
642 ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &clientHandle);
643 APPSPAWN_CHECK(ret == 0, break, "Failed to create reqMgr %{public}s", APPSPAWN_SERVER_NAME);
644 reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_APP_SPAWN, 0);
645 APPSPAWN_CHECK(reqHandle != INVALID_REQ_HANDLE, break, "Failed to create req %{public}s", APPSPAWN_SERVER_NAME);
646
647 // asan set cold
648 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_DEBUGGABLE);
649 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_NATIVEDEBUG);
650 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_BUNDLE_RESOURCES);
651 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_ACCESS_BUNDLE_DIR);
652 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_ASANENABLED);
653 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_GWP_ENABLED_NORMAL);
654
655 ret = APPSPAWN_ARG_INVALID;
656 property = g_testHelper.GetAppProperty(clientHandle, reqHandle);
657 APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break);
658
659 std::string cmd = GetColdRunArgs(property, false, "appspawn -mode app_cold ");
660 content = AppSpawnTestHelper::StartSpawnServer(cmd, args);
661 APPSPAWN_CHECK_ONLY_EXPER(content != nullptr, break);
662 ASSERT_EQ(content->mode, MODE_FOR_APP_COLD_RUN);
663 // add property to content
664 OH_ListAddTail(&GetAppSpawnMgr()->appSpawnQueue, &property->node);
665 ProcessAppSpawnDumpMsg(nullptr);
666 // spawn prepare process
667 AppSpawnHookExecute(STAGE_PARENT_PRE_FORK, 0, content, &property->client);
668 AppSpawnHookExecute(STAGE_CHILD_PRE_COLDBOOT, 0, content, &property->client);
669 // run in cold mode
670 // child run in TestRunChildProcessor
671 RegChildLooper(content, TestRunChildProcessor);
672 content->runAppSpawn(content, args->argc, args->argv);
673 property = nullptr;
674 ret = 0;
675 } while (0);
676 if (args) {
677 free(args);
678 }
679 DeleteAppSpawningCtx(property);
680 DeleteAppSpawnMgr(GetAppSpawnMgr());
681 AppSpawnClientDestroy(clientHandle);
682 ASSERT_EQ(ret, 0);
683 }
684 } // namespace OHOS
685