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 HWTEST_F(AppSpawnChildTest, App_Spawn_Child_007, TestSize.Level0)
312 {
313 AppSpawnClientHandle clientHandle = nullptr;
314 AppSpawnReqMsgHandle reqHandle = 0;
315 AppSpawningCtx *property = nullptr;
316 AppSpawnContent *content = nullptr;
317 int ret = -1;
318 do {
319 ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &clientHandle);
320 APPSPAWN_CHECK(ret == 0, break, "Failed to create reqMgr %{public}s", APPSPAWN_SERVER_NAME);
321 // MSG_SPAWN_NATIVE_PROCESS and render
322 g_testHelper.SetTestUid(10010029); // 10010029
323 reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_SPAWN_NATIVE_PROCESS, 0);
324 APPSPAWN_CHECK(reqHandle != INVALID_REQ_HANDLE, break, "Failed to create req");
325 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_DEBUGGABLE);
326 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_NATIVEDEBUG);
327 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_BUNDLE_RESOURCES);
328 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_ACCESS_BUNDLE_DIR);
329 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_GWP_ENABLED_NORMAL);
330
331 content = CreateTestAppSpawnContent(APPSPAWN_SOCKET_NAME, MODE_FOR_APP_SPAWN);
332 APPSPAWN_CHECK_ONLY_EXPER(content != nullptr, break);
333
334 ret = APPSPAWN_ARG_INVALID;
335 property = g_testHelper.GetAppProperty(clientHandle, reqHandle);
336 APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break);
337
338 // spawn prepare process
339 AppSpawnHookExecute(STAGE_PARENT_PRE_FORK, 0, content, &property->client);
340
341 // spawn
342 property->client.flags = APP_COLD_START;
343 ret = AppSpawnChild(content, &property->client);
344 property = nullptr;
345 content = nullptr;
346 } while (0);
347 DeleteAppSpawningCtx(property);
348 AppSpawnClientDestroy(clientHandle);
349 AppSpawnDestroyContent(content);
350 LE_StopLoop(LE_GetDefaultLoop());
351 LE_CloseLoop(LE_GetDefaultLoop());
352 ASSERT_EQ(ret, 0);
353 }
354
355 HWTEST_F(AppSpawnChildTest, App_Spawn_Child_008, TestSize.Level0)
356 {
357 AppSpawnClientHandle clientHandle = nullptr;
358 AppSpawnReqMsgHandle reqHandle = 0;
359 AppSpawningCtx *property = nullptr;
360 AppSpawnContent *content = nullptr;
361 int ret = -1;
362 do {
363 ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &clientHandle);
364 APPSPAWN_CHECK(ret == 0, break, "Failed to create reqMgr %{public}s", APPSPAWN_SERVER_NAME);
365 // MSG_SPAWN_NATIVE_PROCESS and render
366 g_testHelper.SetTestUid(10010029); // 10010029
367 reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_SPAWN_NATIVE_PROCESS, 0);
368 APPSPAWN_CHECK(reqHandle != INVALID_REQ_HANDLE, break, "Failed to create req");
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 content = CreateTestAppSpawnContent(APPSPAWN_SERVER_NAME, MODE_FOR_APP_COLD_RUN);
376 APPSPAWN_CHECK_ONLY_EXPER(content != nullptr, break);
377 content->coldStartApp = nullptr;
378
379 ret = APPSPAWN_ARG_INVALID;
380 property = g_testHelper.GetAppProperty(clientHandle, reqHandle);
381 APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break);
382 // spawn
383 property->client.flags = APP_COLD_START;
384 ret = AppSpawnChild(content, &property->client);
385 property = nullptr;
386 content = nullptr;
387 } while (0);
388 DeleteAppSpawningCtx(property);
389 AppSpawnClientDestroy(clientHandle);
390 AppSpawnDestroyContent(content);
391 LE_StopLoop(LE_GetDefaultLoop());
392 LE_CloseLoop(LE_GetDefaultLoop());
393 ASSERT_EQ(ret, 0);
394 }
395
396 /**
397 * @brief 子进程nweb后半部分处理
398 *
399 */
400 HWTEST_F(AppSpawnChildTest, NWeb_Spawn_Child_001, TestSize.Level0)
401 {
402 AppSpawnClientHandle clientHandle = nullptr;
403 AppSpawnReqMsgHandle reqHandle = 0;
404 AppSpawningCtx *property = nullptr;
405 AppSpawnContent *content = nullptr;
406 int ret = -1;
407 do {
408 ret = AppSpawnClientInit(NWEBSPAWN_SERVER_NAME, &clientHandle);
409 APPSPAWN_CHECK(ret == 0, break, "Failed to create reqMgr %{public}s", NWEBSPAWN_SERVER_NAME);
410 reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_APP_SPAWN, 0);
411 APPSPAWN_CHECK(reqHandle != INVALID_REQ_HANDLE, break, "Failed to create req ");
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 AppSpawnHookExecute(STAGE_PARENT_PRE_FORK, 0, content, &property->client);
422
423 // spawn
424 property->client.flags &= ~APP_COLD_START;
425 ret = AppSpawnChild(content, &property->client);
426 property = nullptr;
427 content = nullptr;
428 } while (0);
429 DeleteAppSpawningCtx(property);
430 AppSpawnClientDestroy(clientHandle);
431 AppSpawnDestroyContent(content);
432 LE_StopLoop(LE_GetDefaultLoop());
433 LE_CloseLoop(LE_GetDefaultLoop());
434 ASSERT_EQ(ret, 0);
435 }
436
437 HWTEST_F(AppSpawnChildTest, NWeb_Spawn_Child_002, TestSize.Level0)
438 {
439 AppSpawnClientHandle clientHandle = nullptr;
440 AppSpawnReqMsgHandle reqHandle = 0;
441 AppSpawningCtx *property = nullptr;
442 AppSpawnContent *content = nullptr;
443 int ret = -1;
444 do {
445 ret = AppSpawnClientInit(NWEBSPAWN_SERVER_NAME, &clientHandle);
446 APPSPAWN_CHECK(ret == 0, break, "Failed to create reqMgr %{public}s", NWEBSPAWN_SERVER_NAME);
447 reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_APP_SPAWN, 0);
448 APPSPAWN_CHECK(reqHandle != INVALID_REQ_HANDLE, break, "Failed to create req");
449
450 content = CreateTestAppSpawnContent(NWEBSPAWN_SOCKET_NAME, MODE_FOR_NWEB_SPAWN);
451 APPSPAWN_CHECK_ONLY_EXPER(content != nullptr, break);
452
453 ret = APPSPAWN_ARG_INVALID;
454 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_DEBUGGABLE);
455 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_NATIVEDEBUG);
456 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_BUNDLE_RESOURCES);
457 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_ACCESS_BUNDLE_DIR);
458 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_GWP_ENABLED_NORMAL);
459
460 property = g_testHelper.GetAppProperty(clientHandle, reqHandle);
461 APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break);
462 // spawn prepare process
463 AppSpawnHookExecute(STAGE_PARENT_PRE_FORK, 0, content, &property->client);
464
465 // spawn
466 ret = AppSpawnChild(content, &property->client);
467 property = nullptr;
468 content = nullptr;
469 } while (0);
470 DeleteAppSpawningCtx(property);
471 AppSpawnClientDestroy(clientHandle);
472 AppSpawnDestroyContent(content);
473 LE_StopLoop(LE_GetDefaultLoop());
474 LE_CloseLoop(LE_GetDefaultLoop());
475 ASSERT_EQ(ret, 0);
476 }
477
478 HWTEST_F(AppSpawnChildTest, NWeb_Spawn_Child_004, TestSize.Level0)
479 {
480 AppSpawnClientHandle clientHandle = nullptr;
481 AppSpawnReqMsgHandle reqHandle = 0;
482 AppSpawningCtx *property = nullptr;
483 AppSpawnContent *content = nullptr;
484 int ret = -1;
485 do {
486 ret = AppSpawnClientInit(NWEBSPAWN_SERVER_NAME, &clientHandle);
487 APPSPAWN_CHECK(ret == 0, break, "Failed to create reqMgr %{public}s", NWEBSPAWN_SERVER_NAME);
488 // MSG_SPAWN_NATIVE_PROCESS and no render cmd
489 g_testHelper.SetTestUid(10010029); // 10010029
490 reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_SPAWN_NATIVE_PROCESS, 0);
491 APPSPAWN_CHECK(reqHandle != INVALID_REQ_HANDLE, break, "Failed to create req");
492 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_DEBUGGABLE);
493 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_NATIVEDEBUG);
494 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_BUNDLE_RESOURCES);
495 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_ACCESS_BUNDLE_DIR);
496 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_GWP_ENABLED_NORMAL);
497
498 content = CreateTestAppSpawnContent(NWEBSPAWN_SOCKET_NAME, MODE_FOR_NWEB_SPAWN);
499 APPSPAWN_CHECK_ONLY_EXPER(content != nullptr, break);
500
501 ret = APPSPAWN_ARG_INVALID;
502 property = g_testHelper.GetAppProperty(clientHandle, reqHandle);
503 APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break);
504
505 // spawn prepare process
506 property->client.flags &= ~APP_COLD_START;
507 AppSpawnHookExecute(STAGE_PARENT_PRE_FORK, 0, content, &property->client);
508 // spawn
509 ret = AppSpawnChild(content, &property->client);
510 property = nullptr;
511 content = nullptr;
512 } while (0);
513 DeleteAppSpawningCtx(property);
514 AppSpawnClientDestroy(clientHandle);
515 AppSpawnDestroyContent(content);
516 LE_StopLoop(LE_GetDefaultLoop());
517 LE_CloseLoop(LE_GetDefaultLoop());
518 ASSERT_EQ(ret, 0);
519 }
520
521 HWTEST_F(AppSpawnChildTest, NWeb_Spawn_Child_005, TestSize.Level0)
522 {
523 AppSpawnClientHandle clientHandle = nullptr;
524 AppSpawnReqMsgHandle reqHandle = 0;
525 AppSpawningCtx *property = nullptr;
526 AppSpawnContent *content = nullptr;
527 int ret = -1;
528 do {
529 ret = AppSpawnClientInit(NWEBSPAWN_SERVER_NAME, &clientHandle);
530 APPSPAWN_CHECK(ret == 0, break, "Failed to create reqMgr %{public}s", NWEBSPAWN_SERVER_NAME);
531 // MSG_SPAWN_NATIVE_PROCESS and render
532 g_testHelper.SetTestUid(10010029); // 10010029
533 reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_SPAWN_NATIVE_PROCESS, 0);
534 APPSPAWN_CHECK(reqHandle != INVALID_REQ_HANDLE, break, "Failed to create req");
535 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_DEBUGGABLE);
536 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_NATIVEDEBUG);
537 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_BUNDLE_RESOURCES);
538 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_ACCESS_BUNDLE_DIR);
539 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_GWP_ENABLED_NORMAL);
540
541 content = CreateTestAppSpawnContent(NWEBSPAWN_SOCKET_NAME, MODE_FOR_NWEB_SPAWN);
542 APPSPAWN_CHECK_ONLY_EXPER(content != nullptr, break);
543
544 ret = APPSPAWN_ARG_INVALID;
545 property = g_testHelper.GetAppProperty(clientHandle, reqHandle);
546 APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break);
547
548 // spawn prepare process
549 AppSpawnHookExecute(STAGE_PARENT_PRE_FORK, 0, content, &property->client);
550
551 // spawn
552 property->client.flags &= ~APP_COLD_START;
553 ret = AppSpawnChild(content, &property->client);
554 property = nullptr;
555 content = nullptr;
556 } while (0);
557 DeleteAppSpawningCtx(property);
558 AppSpawnClientDestroy(clientHandle);
559 AppSpawnDestroyContent(content);
560 LE_StopLoop(LE_GetDefaultLoop());
561 LE_CloseLoop(LE_GetDefaultLoop());
562 ASSERT_EQ(ret, 0);
563 }
564
565 HWTEST_F(AppSpawnChildTest, NWeb_Spawn_Child_006, TestSize.Level0)
566 {
567 AppSpawnClientHandle clientHandle = nullptr;
568 AppSpawnReqMsgHandle reqHandle = 0;
569 AppSpawningCtx *property = nullptr;
570 AppSpawnContent *content = nullptr;
571 int ret = -1;
572 do {
573 ret = AppSpawnClientInit(NWEBSPAWN_SERVER_NAME, &clientHandle);
574 APPSPAWN_CHECK(ret == 0, break, "Failed to create reqMgr %{public}s", NWEBSPAWN_SERVER_NAME);
575 // MSG_SPAWN_NATIVE_PROCESS and render
576 g_testHelper.SetTestUid(10010029); // 10010029
577 reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_SPAWN_NATIVE_PROCESS, 0);
578 APPSPAWN_CHECK(reqHandle != INVALID_REQ_HANDLE, break, "Failed to create req");
579 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_DEBUGGABLE);
580 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_NATIVEDEBUG);
581 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_BUNDLE_RESOURCES);
582 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_ACCESS_BUNDLE_DIR);
583 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_GWP_ENABLED_NORMAL);
584
585 content = CreateTestAppSpawnContent(NWEBSPAWN_SERVER_NAME, MODE_FOR_NWEB_SPAWN);
586 APPSPAWN_CHECK_ONLY_EXPER(content != nullptr, break);
587
588 ret = APPSPAWN_ARG_INVALID;
589 property = g_testHelper.GetAppProperty(clientHandle, reqHandle);
590 APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break);
591
592 // spawn prepare process
593 AppSpawnHookExecute(STAGE_PARENT_PRE_FORK, 0, content, &property->client);
594
595 // spawn
596 property->client.flags = APP_COLD_START;
597 ret = AppSpawnChild(content, &property->client);
598 property = nullptr;
599 content = nullptr;
600 } while (0);
601 DeleteAppSpawningCtx(property);
602 AppSpawnClientDestroy(clientHandle);
603 AppSpawnDestroyContent(content);
604 LE_StopLoop(LE_GetDefaultLoop());
605 LE_CloseLoop(LE_GetDefaultLoop());
606 ASSERT_EQ(ret, 0);
607 }
608
609 HWTEST_F(AppSpawnChildTest, NWeb_Spawn_Child_007, TestSize.Level0)
610 {
611 AppSpawnClientHandle clientHandle = nullptr;
612 AppSpawnReqMsgHandle reqHandle = 0;
613 AppSpawningCtx *property = nullptr;
614 AppSpawnContent *content = nullptr;
615 int ret = -1;
616 do {
617 ret = AppSpawnClientInit(CJAPPSPAWN_SERVER_NAME, &clientHandle);
618 APPSPAWN_CHECK(ret == 0, break, "Failed to create reqMgr %{public}s", CJAPPSPAWN_SERVER_NAME);
619 // MSG_SPAWN_NATIVE_PROCESS and render
620 g_testHelper.SetTestUid(10010029); // 10010029
621 reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_SPAWN_NATIVE_PROCESS, 0);
622 APPSPAWN_CHECK(reqHandle != INVALID_REQ_HANDLE, break, "Failed to create req");
623 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_DEBUGGABLE);
624 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_NATIVEDEBUG);
625 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_BUNDLE_RESOURCES);
626 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_ACCESS_BUNDLE_DIR);
627 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_GWP_ENABLED_NORMAL);
628
629 content = CreateTestAppSpawnContent(CJAPPSPAWN_SERVER_NAME, MODE_FOR_APP_COLD_RUN);
630 APPSPAWN_CHECK_ONLY_EXPER(content != nullptr, break);
631
632 ret = APPSPAWN_ARG_INVALID;
633 property = g_testHelper.GetAppProperty(clientHandle, reqHandle);
634 APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break);
635
636 // spawn prepare process
637 AppSpawnHookExecute(STAGE_PARENT_PRE_FORK, 0, content, &property->client);
638
639 // spawn
640 property->client.flags = APP_COLD_START;
641 ret = AppSpawnChild(content, &property->client);
642 property = nullptr;
643 content = nullptr;
644 } while (0);
645 DeleteAppSpawningCtx(property);
646 AppSpawnClientDestroy(clientHandle);
647 AppSpawnDestroyContent(content);
648 LE_StopLoop(LE_GetDefaultLoop());
649 LE_CloseLoop(LE_GetDefaultLoop());
650 ASSERT_EQ(ret, 0);
651 }
652
653 HWTEST_F(AppSpawnChildTest, NWeb_Spawn_Child_008, TestSize.Level0)
654 {
655 AppSpawnClientHandle clientHandle = nullptr;
656 AppSpawnReqMsgHandle reqHandle = 0;
657 AppSpawningCtx *property = nullptr;
658 AppSpawnContent *content = nullptr;
659 int ret = -1;
660 do {
661 ret = AppSpawnClientInit(NWEBSPAWN_SERVER_NAME, &clientHandle);
662 APPSPAWN_CHECK(ret == 0, break, "Failed to create reqMgr %{public}s", NWEBSPAWN_SERVER_NAME);
663 // MSG_SPAWN_NATIVE_PROCESS and render
664 g_testHelper.SetTestUid(10010029); // 10010029
665 reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_SPAWN_NATIVE_PROCESS, 0);
666 APPSPAWN_CHECK(reqHandle != INVALID_REQ_HANDLE, break, "Failed to create req");
667 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_DEBUGGABLE);
668 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_NATIVEDEBUG);
669 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_BUNDLE_RESOURCES);
670 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_ACCESS_BUNDLE_DIR);
671 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_GWP_ENABLED_NORMAL);
672
673 content = CreateTestAppSpawnContent(NWEBSPAWN_SERVER_NAME, MODE_FOR_APP_COLD_RUN);
674 APPSPAWN_CHECK_ONLY_EXPER(content != nullptr, break);
675 content->coldStartApp = nullptr;
676
677 ret = APPSPAWN_ARG_INVALID;
678 property = g_testHelper.GetAppProperty(clientHandle, reqHandle);
679 APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break);
680
681 // spawn
682 property->client.flags = APP_COLD_START;
683 ret = AppSpawnChild(content, &property->client);
684 property = nullptr;
685 content = nullptr;
686 } while (0);
687 DeleteAppSpawningCtx(property);
688 AppSpawnClientDestroy(clientHandle);
689 AppSpawnDestroyContent(content);
690 LE_StopLoop(LE_GetDefaultLoop());
691 LE_CloseLoop(LE_GetDefaultLoop());
692 ASSERT_EQ(ret, 0);
693 }
694
695 /**
696 * @brief 测试hybridspawn spawn的后半部分,子进程的处理
697 * @note 创建基础的应用请求信息
698 *
699 */
700 HWTEST_F(AppSpawnChildTest, Hybrid_Spawn_Child_001, TestSize.Level0)
701 {
702 AppSpawnClientHandle clientHandle = nullptr;
703 AppSpawnReqMsgHandle reqHandle = 0;
704 AppSpawningCtx *property = nullptr;
705 AppSpawnContent *content = nullptr;
706 int ret = -1;
707 do {
708 ret = AppSpawnClientInit(HYBRIDSPAWN_SERVER_NAME, &clientHandle);
709 APPSPAWN_CHECK(ret == 0, break, "Failed to create reqMgr %{public}s", HYBRIDSPAWN_SERVER_NAME);
710 reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_APP_SPAWN, 0);
711 APPSPAWN_CHECK(reqHandle != INVALID_REQ_HANDLE, break, "Failed to create req %{public}s",
712 HYBRIDSPAWN_SERVER_NAME);
713 content = CreateTestAppSpawnContent(HYBRIDSPAWN_SOCKET_NAME, MODE_FOR_HYBRID_SPAWN);
714 APPSPAWN_CHECK_ONLY_EXPER(content != nullptr, break);
715
716 ret = APPSPAWN_ARG_INVALID;
717 property = g_testHelper.GetAppProperty(clientHandle, reqHandle);
718 reqHandle = nullptr;
719 APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break);
720
721 // spawn prepare process
722 AppSpawnHookExecute(STAGE_PARENT_PRE_FORK, 0, content, &property->client);
723
724 // spawn
725 ret = AppSpawnChild(content, &property->client);
726 property = nullptr;
727 } while (0);
728 DeleteAppSpawningCtx(property);
729 AppSpawnReqMsgFree(reqHandle);
730 AppSpawnClientDestroy(clientHandle);
731 ASSERT_EQ(ret, 0);
732 }
733
734 /**
735 * @brief 测试hybridspawn spawn的后半部分,子进程的处理
736 * @note 创建基础的应用请求信息,设置多个flag位
737 *
738 */
739 HWTEST_F(AppSpawnChildTest, Hybrid_Spawn_Child_002, TestSize.Level0)
740 {
741 AppSpawnClientHandle clientHandle = nullptr;
742 AppSpawnReqMsgHandle reqHandle = 0;
743 AppSpawningCtx *property = nullptr;
744 AppSpawnContent *content = nullptr;
745 int ret = -1;
746 do {
747 ret = AppSpawnClientInit(HYBRIDSPAWN_SERVER_NAME, &clientHandle);
748 APPSPAWN_CHECK(ret == 0, break, "Failed to create reqMgr %{public}s", HYBRIDSPAWN_SERVER_NAME);
749 reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_APP_SPAWN, 0);
750 APPSPAWN_CHECK(reqHandle != INVALID_REQ_HANDLE, break, "Failed to create req %{public}s",
751 HYBRIDSPAWN_SERVER_NAME);
752 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_DEBUGGABLE);
753 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_NATIVEDEBUG);
754 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_BUNDLE_RESOURCES);
755 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_ACCESS_BUNDLE_DIR);
756
757 content = CreateTestAppSpawnContent(HYBRIDSPAWN_SOCKET_NAME, MODE_FOR_HYBRID_SPAWN);
758 APPSPAWN_CHECK_ONLY_EXPER(content != nullptr, break);
759
760 ret = APPSPAWN_ARG_INVALID;
761 property = g_testHelper.GetAppProperty(clientHandle, reqHandle);
762 reqHandle = nullptr;
763 APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break);
764
765 // spawn prepare process
766 AppSpawnHookExecute(STAGE_PARENT_PRE_FORK, 0, content, &property->client);
767 // spawn
768 ret = AppSpawnChild(content, &property->client);
769 property = nullptr;
770 } while (0);
771 DeleteAppSpawningCtx(property);
772 AppSpawnReqMsgFree(reqHandle);
773 AppSpawnClientDestroy(clientHandle);
774 ASSERT_EQ(ret, 0);
775 }
776
777 /**
778 * @brief 测试hybridspawn spawn的后半部分,子进程的处理
779 * @note 创建基础的应用请求信息中不含有拓展字段,设置多个flag位
780 *
781 */
782 HWTEST_F(AppSpawnChildTest, Hybrid_Spawn_Child_003, TestSize.Level0)
783 {
784 AppSpawnClientHandle clientHandle = nullptr;
785 AppSpawnReqMsgHandle reqHandle = 0;
786 AppSpawningCtx *property = nullptr;
787 AppSpawnContent *content = nullptr;
788 int ret = -1;
789 do {
790 ret = AppSpawnClientInit(HYBRIDSPAWN_SERVER_NAME, &clientHandle);
791 APPSPAWN_CHECK(ret == 0, break, "Failed to create reqMgr %{public}s", HYBRIDSPAWN_SERVER_NAME);
792
793 g_testHelper.SetTestUid(10010029); // 10010029
794 reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_APP_SPAWN, 1);
795 APPSPAWN_CHECK(reqHandle != INVALID_REQ_HANDLE, break, "Failed to create req %{public}s",
796 HYBRIDSPAWN_SERVER_NAME);
797 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_DEBUGGABLE);
798 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_NATIVEDEBUG);
799 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_BUNDLE_RESOURCES);
800 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_ACCESS_BUNDLE_DIR);
801
802 content = CreateTestAppSpawnContent(HYBRIDSPAWN_SOCKET_NAME, MODE_FOR_HYBRID_SPAWN);
803 APPSPAWN_CHECK_ONLY_EXPER(content != nullptr, break);
804
805 ret = APPSPAWN_ARG_INVALID;
806 property = g_testHelper.GetAppProperty(clientHandle, reqHandle);
807 reqHandle = nullptr;
808 APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break);
809
810 // spawn prepare process
811 AppSpawnHookExecute(STAGE_PARENT_PRE_FORK, 0, content, &property->client);
812 // spawn
813 ret = AppSpawnChild(content, &property->client);
814 property = nullptr;
815 } while (0);
816 DeleteAppSpawningCtx(property);
817 AppSpawnReqMsgFree(reqHandle);
818 AppSpawnClientDestroy(clientHandle);
819 ASSERT_EQ(ret, 0);
820 }
821
822 /**
823 * @brief 测试hybridspawn spawn的后半部分,子进程的处理
824 * @note 测试native process进程的孵化请求,创建基础的消息中不含有拓展字段,设置多个flag位
825 *
826 */
827 HWTEST_F(AppSpawnChildTest, Hybrid_Spawn_Child_004, TestSize.Level0)
828 {
829 AppSpawnClientHandle clientHandle = nullptr;
830 AppSpawnReqMsgHandle reqHandle = 0;
831 AppSpawningCtx *property = nullptr;
832 AppSpawnContent *content = nullptr;
833 int ret = -1;
834 do {
835 ret = AppSpawnClientInit(HYBRIDSPAWN_SERVER_NAME, &clientHandle);
836 APPSPAWN_CHECK(ret == 0, break, "Failed to create reqMgr %{public}s", HYBRIDSPAWN_SERVER_NAME);
837 // MSG_SPAWN_NATIVE_PROCESS and no render cmd
838 g_testHelper.SetTestUid(10010029); // 10010029
839 reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_SPAWN_NATIVE_PROCESS, 1);
840 APPSPAWN_CHECK(reqHandle != INVALID_REQ_HANDLE, break, "Failed to create req %{public}s",
841 HYBRIDSPAWN_SERVER_NAME);
842 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_DEBUGGABLE);
843 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_NATIVEDEBUG);
844 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_BUNDLE_RESOURCES);
845 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_ACCESS_BUNDLE_DIR);
846 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_GWP_ENABLED_NORMAL);
847
848 ret = APPSPAWN_ARG_INVALID;
849 property = g_testHelper.GetAppProperty(clientHandle, reqHandle);
850 reqHandle = nullptr;
851 APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break);
852
853 content = CreateTestAppSpawnContent(HYBRIDSPAWN_SOCKET_NAME, MODE_FOR_HYBRID_SPAWN);
854 APPSPAWN_CHECK_ONLY_EXPER(content != nullptr, break);
855
856 // spawn prepare process
857 AppSpawnHookExecute(STAGE_PARENT_PRE_FORK, 0, content, &property->client);
858 // spawn
859 ret = AppSpawnChild(content, &property->client);
860 property = nullptr;
861 } while (0);
862 DeleteAppSpawningCtx(property);
863 AppSpawnReqMsgFree(reqHandle);
864 AppSpawnClientDestroy(clientHandle);
865 ASSERT_EQ(ret, 0);
866 }
867
868 /**
869 * @brief 测试hybridspawn spawn的后半部分,子进程的处理
870 * @note 测试native process进程的孵化请求,创建基础的消息,设置多个flag位
871 *
872 */
873 HWTEST_F(AppSpawnChildTest, Hybrid_Spawn_Child_005, TestSize.Level0)
874 {
875 AppSpawnClientHandle clientHandle = nullptr;
876 AppSpawnReqMsgHandle reqHandle = 0;
877 AppSpawningCtx *property = nullptr;
878 AppSpawnContent *content = nullptr;
879 int ret = -1;
880 do {
881 ret = AppSpawnClientInit(HYBRIDSPAWN_SERVER_NAME, &clientHandle);
882 APPSPAWN_CHECK(ret == 0, break, "Failed to create reqMgr %{public}s", HYBRIDSPAWN_SERVER_NAME);
883 // MSG_SPAWN_NATIVE_PROCESS and render
884 g_testHelper.SetTestUid(10010029); // 10010029
885 reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_SPAWN_NATIVE_PROCESS, 0);
886 APPSPAWN_CHECK(reqHandle != INVALID_REQ_HANDLE, break, "Failed to create req %{public}s",
887 HYBRIDSPAWN_SERVER_NAME);
888 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_DEBUGGABLE);
889 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_NATIVEDEBUG);
890 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_BUNDLE_RESOURCES);
891 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_ACCESS_BUNDLE_DIR);
892 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_GWP_ENABLED_NORMAL);
893
894 ret = APPSPAWN_ARG_INVALID;
895 property = g_testHelper.GetAppProperty(clientHandle, reqHandle);
896 reqHandle = nullptr;
897 APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break);
898
899 content = CreateTestAppSpawnContent(HYBRIDSPAWN_SOCKET_NAME, MODE_FOR_HYBRID_SPAWN);
900 APPSPAWN_CHECK_ONLY_EXPER(content != nullptr, break);
901
902 // spawn prepare process
903 AppSpawnHookExecute(STAGE_PARENT_PRE_FORK, 0, content, &property->client);
904 // spawn
905 ret = AppSpawnChild(content, &property->client);
906 property = nullptr;
907 ASSERT_EQ(ret, 0);
908 } while (0);
909 DeleteAppSpawningCtx(property);
910 AppSpawnReqMsgFree(reqHandle);
911 AppSpawnClientDestroy(clientHandle);
912 ASSERT_EQ(ret, 0);
913 }
914
915 /**
916 * @brief 测试hybridspawn spawn的后半部分,子进程的处理
917 * @note 测试native process的孵化请求,创建基础的信息,添加多个flag位,新增环境变量扩展字段
918 *
919 */
920 HWTEST_F(AppSpawnChildTest, Hybrid_Spawn_Child_006, TestSize.Level0)
921 {
922 AppSpawnClientHandle clientHandle = nullptr;
923 AppSpawnReqMsgHandle reqHandle = 0;
924 AppSpawningCtx *property = nullptr;
925 AppSpawnContent *content = nullptr;
926 int ret = -1;
927 do {
928 ret = AppSpawnClientInit(HYBRIDSPAWN_SERVER_NAME, &clientHandle);
929 APPSPAWN_CHECK(ret == 0, break, "Failed to create reqMgr %{public}s", HYBRIDSPAWN_SERVER_NAME);
930 // MSG_SPAWN_NATIVE_PROCESS and no render cmd
931 g_testHelper.SetTestUid(10010029); // 10010029
932 reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_SPAWN_NATIVE_PROCESS, 0);
933 APPSPAWN_CHECK(reqHandle != INVALID_REQ_HANDLE, break, "Failed to create req %{public}s",
934 HYBRIDSPAWN_SERVER_NAME);
935 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_DEBUGGABLE);
936 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_NATIVEDEBUG);
937 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_BUNDLE_RESOURCES);
938 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_ACCESS_BUNDLE_DIR);
939 const char *appEnv = "{\"test.name1\": \"test.value1\", \"test.name2\": \"test.value2\"}";
940 ret = AppSpawnReqMsgAddExtInfo(reqHandle, "AppEnv",
941 reinterpret_cast<uint8_t *>(const_cast<char *>(appEnv)), strlen(appEnv) + 1);
942 APPSPAWN_CHECK(ret == 0, break, "Failed to add ext tlv %{public}s", appEnv);
943
944 ret = APPSPAWN_ARG_INVALID;
945 property = g_testHelper.GetAppProperty(clientHandle, reqHandle);
946 reqHandle = nullptr;
947 APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break);
948
949 content = CreateTestAppSpawnContent(HYBRIDSPAWN_SOCKET_NAME, MODE_FOR_HYBRID_SPAWN);
950 APPSPAWN_CHECK_ONLY_EXPER(content != nullptr, break);
951
952 // spawn prepare process
953 AppSpawnHookExecute(STAGE_PARENT_PRE_FORK, 0, content, &property->client);
954 // spawn
955 ret = AppSpawnChild(content, &property->client);
956 property = nullptr;
957 ASSERT_EQ(ret, 0);
958 } while (0);
959 DeleteAppSpawningCtx(property);
960 AppSpawnReqMsgFree(reqHandle);
961 AppSpawnClientDestroy(clientHandle);
962 ASSERT_EQ(ret, 0);
963 }
964
965 /**
966 * @brief 测试hybridspawn spawn的后半部分,子进程的处理
967 * @note 测试native process的孵化请求,创建基础的信息,添加多个flag位,使能冷启动
968 *
969 */
970 HWTEST_F(AppSpawnChildTest, Hybrid_Spawn_Child_007, TestSize.Level0)
971 {
972 AppSpawnClientHandle clientHandle = nullptr;
973 AppSpawnReqMsgHandle reqHandle = 0;
974 AppSpawningCtx *property = nullptr;
975 AppSpawnContent *content = nullptr;
976 int ret = -1;
977 do {
978 ret = AppSpawnClientInit(HYBRIDSPAWN_SERVER_NAME, &clientHandle);
979 APPSPAWN_CHECK(ret == 0, break, "Failed to create reqMgr %{public}s", HYBRIDSPAWN_SERVER_NAME);
980 // MSG_SPAWN_NATIVE_PROCESS and render
981 g_testHelper.SetTestUid(10010029); // 10010029
982 reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_SPAWN_NATIVE_PROCESS, 0);
983 APPSPAWN_CHECK(reqHandle != INVALID_REQ_HANDLE, break, "Failed to create req");
984 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_DEBUGGABLE);
985 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_NATIVEDEBUG);
986 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_BUNDLE_RESOURCES);
987 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_ACCESS_BUNDLE_DIR);
988 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_GWP_ENABLED_NORMAL);
989
990 content = CreateTestAppSpawnContent(HYBRIDSPAWN_SOCKET_NAME, MODE_FOR_HYBRID_SPAWN);
991 APPSPAWN_CHECK_ONLY_EXPER(content != nullptr, break);
992
993 ret = APPSPAWN_ARG_INVALID;
994 property = g_testHelper.GetAppProperty(clientHandle, reqHandle);
995 APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break);
996
997 // spawn prepare process
998 AppSpawnHookExecute(STAGE_PARENT_PRE_FORK, 0, content, &property->client);
999
1000 // spawn
1001 property->client.flags = APP_COLD_START;
1002 ret = AppSpawnChild(content, &property->client);
1003 property = nullptr;
1004 content = nullptr;
1005 } while (0);
1006 DeleteAppSpawningCtx(property);
1007 AppSpawnClientDestroy(clientHandle);
1008 AppSpawnDestroyContent(content);
1009 LE_StopLoop(LE_GetDefaultLoop());
1010 LE_CloseLoop(LE_GetDefaultLoop());
1011 ASSERT_EQ(ret, 0);
1012 }
1013
1014 /**
1015 * @brief 测试hybridspawn spawn的后半部分,子进程的处理
1016 * @note 测试普通应用的孵化请求,创建基础的信息,添加多个flag位,使能冷启动
1017 *
1018 */
1019 HWTEST_F(AppSpawnChildTest, Hybrid_Spawn_Child_008, TestSize.Level0)
1020 {
1021 AppSpawnClientHandle clientHandle = nullptr;
1022 AppSpawnReqMsgHandle reqHandle = 0;
1023 AppSpawningCtx *property = nullptr;
1024 AppSpawnContent *content = nullptr;
1025 int ret = -1;
1026 do {
1027 ret = AppSpawnClientInit(HYBRIDSPAWN_SERVER_NAME, &clientHandle);
1028 APPSPAWN_CHECK(ret == 0, break, "Failed to create reqMgr %{public}s", HYBRIDSPAWN_SERVER_NAME);
1029 // MSG_SPAWN_NATIVE_PROCESS and render
1030 g_testHelper.SetTestUid(10010029); // 10010029
1031 reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_APP_SPAWN, 0);
1032 APPSPAWN_CHECK(reqHandle != INVALID_REQ_HANDLE, break, "Failed to create req");
1033 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_DEBUGGABLE);
1034 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_NATIVEDEBUG);
1035 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_BUNDLE_RESOURCES);
1036 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_ACCESS_BUNDLE_DIR);
1037 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_GWP_ENABLED_NORMAL);
1038
1039 content = CreateTestAppSpawnContent(HYBRIDSPAWN_SOCKET_NAME, MODE_FOR_APP_COLD_RUN);
1040 APPSPAWN_CHECK_ONLY_EXPER(content != nullptr, break);
1041 content->coldStartApp = nullptr;
1042
1043 ret = APPSPAWN_ARG_INVALID;
1044 property = g_testHelper.GetAppProperty(clientHandle, reqHandle);
1045 APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break);
1046 // spawn
1047 property->client.flags = APP_COLD_START;
1048 ret = AppSpawnChild(content, &property->client);
1049 property = nullptr;
1050 content = nullptr;
1051 } while (0);
1052 DeleteAppSpawningCtx(property);
1053 AppSpawnClientDestroy(clientHandle);
1054 AppSpawnDestroyContent(content);
1055 LE_StopLoop(LE_GetDefaultLoop());
1056 LE_CloseLoop(LE_GetDefaultLoop());
1057 ASSERT_EQ(ret, 0);
1058 }
1059
1060
1061 /**
1062 * @brief 测试nativespawn spawn的后半部分,子进程的处理
1063 * @note 创建基础的应用请求信息
1064 *
1065 */
1066 HWTEST_F(AppSpawnChildTest, Native_Spawn_Child_001, TestSize.Level0)
1067 {
1068 AppSpawnClientHandle clientHandle = nullptr;
1069 AppSpawnReqMsgHandle reqHandle = 0;
1070 AppSpawningCtx *property = nullptr;
1071 AppSpawnContent *content = nullptr;
1072 int ret = -1;
1073 do {
1074 ret = AppSpawnClientInit(NATIVESPAWN_SERVER_NAME, &clientHandle);
1075 APPSPAWN_CHECK(ret == 0, break, "Failed to create reqMgr %{public}s", NATIVESPAWN_SERVER_NAME);
1076 reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_APP_SPAWN, 0);
1077 APPSPAWN_CHECK(reqHandle != INVALID_REQ_HANDLE, break, "Failed to create req %{public}s",
1078 NATIVESPAWN_SERVER_NAME);
1079 content = CreateTestAppSpawnContent(NATIVESPAWN_SOCKET_NAME, MODE_FOR_NATIVE_SPAWN);
1080 APPSPAWN_CHECK_ONLY_EXPER(content != nullptr, break);
1081
1082 ret = APPSPAWN_ARG_INVALID;
1083 property = g_testHelper.GetAppProperty(clientHandle, reqHandle);
1084 reqHandle = nullptr;
1085 APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break);
1086
1087 // spawn prepare process
1088 AppSpawnHookExecute(STAGE_PARENT_PRE_FORK, 0, content, &property->client);
1089
1090 // spawn
1091 ret = AppSpawnChild(content, &property->client);
1092 property = nullptr;
1093 } while (0);
1094 DeleteAppSpawningCtx(property);
1095 AppSpawnReqMsgFree(reqHandle);
1096 AppSpawnClientDestroy(clientHandle);
1097 ASSERT_EQ(ret, 0);
1098 }
1099
1100 /**
1101 * @brief 测试nativespawn spawn的后半部分,子进程的处理
1102 * @note 创建基础的应用请求信息,设置多个flag位
1103 *
1104 */
1105 HWTEST_F(AppSpawnChildTest, Native_Spawn_Child_002, TestSize.Level0)
1106 {
1107 AppSpawnClientHandle clientHandle = nullptr;
1108 AppSpawnReqMsgHandle reqHandle = 0;
1109 AppSpawningCtx *property = nullptr;
1110 AppSpawnContent *content = nullptr;
1111 int ret = -1;
1112 do {
1113 ret = AppSpawnClientInit(NATIVESPAWN_SERVER_NAME, &clientHandle);
1114 APPSPAWN_CHECK(ret == 0, break, "Failed to create reqMgr %{public}s", NATIVESPAWN_SERVER_NAME);
1115 reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_APP_SPAWN, 0);
1116 APPSPAWN_CHECK(reqHandle != INVALID_REQ_HANDLE, break, "Failed to create req %{public}s",
1117 NATIVESPAWN_SERVER_NAME);
1118 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_DEBUGGABLE);
1119 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_NATIVEDEBUG);
1120 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_BUNDLE_RESOURCES);
1121 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_ACCESS_BUNDLE_DIR);
1122
1123 content = CreateTestAppSpawnContent(NATIVESPAWN_SOCKET_NAME, MODE_FOR_NATIVE_SPAWN);
1124 APPSPAWN_CHECK_ONLY_EXPER(content != nullptr, break);
1125
1126 ret = APPSPAWN_ARG_INVALID;
1127 property = g_testHelper.GetAppProperty(clientHandle, reqHandle);
1128 reqHandle = nullptr;
1129 APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break);
1130
1131 // spawn prepare process
1132 AppSpawnHookExecute(STAGE_PARENT_PRE_FORK, 0, content, &property->client);
1133 // spawn
1134 ret = AppSpawnChild(content, &property->client);
1135 property = nullptr;
1136 } while (0);
1137 DeleteAppSpawningCtx(property);
1138 AppSpawnReqMsgFree(reqHandle);
1139 AppSpawnClientDestroy(clientHandle);
1140 ASSERT_EQ(ret, 0);
1141 }
1142
1143 HWTEST_F(AppSpawnChildTest, App_Spawn_Child_Illegal_001, TestSize.Level0)
1144 {
1145 ASSERT_EQ(AppSpawnChild(nullptr, nullptr), -1);
1146 }
1147
1148 HWTEST_F(AppSpawnChildTest, App_Spawn_Child_Illegal_002, TestSize.Level0)
1149 {
1150 AppSpawnContent *content = nullptr;
1151 content = CreateTestAppSpawnContent(NWEBSPAWN_SERVER_NAME, MODE_FOR_APP_COLD_RUN);
1152 ASSERT_NE(content, nullptr);
1153 AppSpawnDestroyContent(content);
1154 content = nullptr;
1155 ASSERT_EQ(AppSpawnChild(nullptr, nullptr), -1);
1156 }
1157
1158 HWTEST_F(AppSpawnChildTest, App_Spawn_Child_Illegal_003, TestSize.Level0)
1159 {
1160 AppSpawnClientHandle clientHandle = nullptr;
1161 AppSpawnReqMsgHandle reqHandle = 0;
1162 AppSpawningCtx *property = nullptr;
1163 AppSpawnContent *content = nullptr;
1164 int ret = -1;
1165 do {
1166 ret = AppSpawnClientInit(NWEBSPAWN_SERVER_NAME, &clientHandle);
1167 APPSPAWN_CHECK(ret == 0, break, "Failed to create reqMgr %{public}s", NWEBSPAWN_SERVER_NAME);
1168 // MSG_SPAWN_NATIVE_PROCESS and render
1169 g_testHelper.SetTestUid(10010029); // 10010029
1170 reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_SPAWN_NATIVE_PROCESS, 0);
1171 APPSPAWN_CHECK(reqHandle != INVALID_REQ_HANDLE, break, "Failed to create req");
1172 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_DEBUGGABLE);
1173 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_NATIVEDEBUG);
1174 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_BUNDLE_RESOURCES);
1175 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_ACCESS_BUNDLE_DIR);
1176 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_GWP_ENABLED_NORMAL);
1177 ret = APPSPAWN_ARG_INVALID;
1178 property = g_testHelper.GetAppProperty(clientHandle, reqHandle);
1179 APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break);
1180
1181 // spawn
1182 property->client.flags = APP_COLD_START;
1183 ret = AppSpawnChild(content, &property->client);
1184 property = nullptr;
1185 content = nullptr;
1186 } while (0);
1187 DeleteAppSpawningCtx(property);
1188 AppSpawnClientDestroy(clientHandle);
1189 AppSpawnDestroyContent(content);
1190 LE_StopLoop(LE_GetDefaultLoop());
1191 LE_CloseLoop(LE_GetDefaultLoop());
1192 ASSERT_EQ(ret, -1);
1193 }
1194
GetColdRunArgs(AppSpawningCtx * property,bool isNweb,const char * arg)1195 static std::string GetColdRunArgs(AppSpawningCtx *property, bool isNweb, const char *arg)
1196 {
1197 std::string argStr = arg;
1198 int ret = WriteMsgToChild(property, isNweb);
1199 APPSPAWN_CHECK(ret == 0, return nullptr,
1200 "Failed to get shm for %{public}s errno %{public}d", GetProcessName(property), errno);
1201
1202 argStr += " -fd -1 0 ";
1203 argStr += std::to_string(property->forkCtx.msgSize);
1204 argStr += " -param ";
1205 argStr += GetProcessName(property);
1206 argStr += " ";
1207 argStr += std::to_string(property->client.id);
1208 return argStr;
1209 }
1210
1211 /**
1212 * @brief 测试冷启动后半部处理
1213 *
1214 */
1215 HWTEST_F(AppSpawnChildTest, App_Spawn_Cold_Run_001, TestSize.Level0)
1216 {
1217 AppSpawnClientHandle clientHandle = nullptr;
1218 AppSpawnReqMsgHandle reqHandle = 0;
1219 AppSpawningCtx *property = nullptr;
1220 AppSpawnContent *content = nullptr;
1221 CmdArgs *args = nullptr;
1222 int ret = -1;
1223 do {
1224 ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &clientHandle);
1225 APPSPAWN_CHECK(ret == 0, break, "Failed to create reqMgr %{public}s", APPSPAWN_SERVER_NAME);
1226 reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_APP_SPAWN, 0);
1227 APPSPAWN_CHECK(reqHandle != INVALID_REQ_HANDLE, break, "Failed to create req %{public}s", APPSPAWN_SERVER_NAME);
1228 // set cold start flags
1229 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_COLD_BOOT);
1230
1231 ret = APPSPAWN_ARG_INVALID;
1232 property = g_testHelper.GetAppProperty(clientHandle, reqHandle);
1233 APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break);
1234
1235 std::string cmd = GetColdRunArgs(property, false, "appspawn -mode app_cold ");
1236 content = AppSpawnTestHelper::StartSpawnServer(cmd, args);
1237 APPSPAWN_CHECK_ONLY_EXPER(content != nullptr, break);
1238
1239 // spawn prepare process
1240 AppSpawnHookExecute(STAGE_PARENT_PRE_FORK, 0, content, &property->client);
1241 AppSpawnHookExecute(STAGE_CHILD_PRE_COLDBOOT, 0, content, &property->client);
1242 // run in cold mode
1243 // child run in TestRunChildProcessor
1244 RegChildLooper(content, TestRunChildProcessor);
1245 content->runAppSpawn(content, args->argc, args->argv);
1246 ret = 0;
1247 } while (0);
1248 if (args) {
1249 free(args);
1250 }
1251 DeleteAppSpawningCtx(property);
1252 AppSpawnClientDestroy(clientHandle);
1253 ASSERT_EQ(ret, 0);
1254 }
1255
1256 HWTEST_F(AppSpawnChildTest, App_Spawn_Cold_Run_002, TestSize.Level0)
1257 {
1258 AppSpawnClientHandle clientHandle = nullptr;
1259 AppSpawnReqMsgHandle reqHandle = 0;
1260 AppSpawningCtx *property = nullptr;
1261 AppSpawnContent *content = nullptr;
1262 CmdArgs *args = nullptr;
1263 int ret = -1;
1264 do {
1265 ret = AppSpawnClientInit(NWEBSPAWN_SERVER_NAME, &clientHandle);
1266 APPSPAWN_CHECK(ret == 0, break, "Failed to create reqMgr %{public}s", NWEBSPAWN_SERVER_NAME);
1267 reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_APP_SPAWN, 0);
1268 APPSPAWN_CHECK(reqHandle != INVALID_REQ_HANDLE, break, "Failed to create req ");
1269 // set cold start flags
1270 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_COLD_BOOT);
1271
1272 ret = APPSPAWN_ARG_INVALID;
1273 property = g_testHelper.GetAppProperty(clientHandle, reqHandle);
1274 APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break);
1275
1276 std::string cmd = GetColdRunArgs(property, true, "appspawn -mode nweb_cold ");
1277 content = AppSpawnTestHelper::StartSpawnServer(cmd, args);
1278 APPSPAWN_CHECK_ONLY_EXPER(content != nullptr, break);
1279 ASSERT_EQ(content->mode, MODE_FOR_NWEB_COLD_RUN);
1280
1281 // spawn prepare process
1282 AppSpawnHookExecute(STAGE_PARENT_PRE_FORK, 0, content, &property->client);
1283 AppSpawnHookExecute(STAGE_CHILD_PRE_COLDBOOT, 0, content, &property->client);
1284 // run in cold mode
1285 // child run in TestRunChildProcessor
1286 RegChildLooper(content, TestRunChildProcessor);
1287 content->runAppSpawn(content, args->argc, args->argv);
1288 ret = 0;
1289 } while (0);
1290 if (args) {
1291 free(args);
1292 }
1293 DeleteAppSpawningCtx(property);
1294 DeleteAppSpawnMgr(GetAppSpawnMgr());
1295 AppSpawnClientDestroy(clientHandle);
1296 ASSERT_EQ(ret, 0);
1297 }
1298
1299 HWTEST_F(AppSpawnChildTest, App_Spawn_Cold_Run_003, TestSize.Level0)
1300 {
1301 AppSpawnClientHandle clientHandle = nullptr;
1302 AppSpawnReqMsgHandle reqHandle = 0;
1303 AppSpawningCtx *property = nullptr;
1304 AppSpawnContent *content = nullptr;
1305 CmdArgs *args = nullptr;
1306 int ret = -1;
1307 do {
1308 ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &clientHandle);
1309 APPSPAWN_CHECK(ret == 0, break, "Failed to create reqMgr %{public}s", APPSPAWN_SERVER_NAME);
1310 reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_APP_SPAWN, 0);
1311 APPSPAWN_CHECK(reqHandle != INVALID_REQ_HANDLE, break, "Failed to create req %{public}s", APPSPAWN_SERVER_NAME);
1312
1313 // asan set cold
1314 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_DEBUGGABLE);
1315 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_NATIVEDEBUG);
1316 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_BUNDLE_RESOURCES);
1317 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_ACCESS_BUNDLE_DIR);
1318 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_ASANENABLED);
1319 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_GWP_ENABLED_FORCE);
1320
1321 ret = APPSPAWN_ARG_INVALID;
1322 property = g_testHelper.GetAppProperty(clientHandle, reqHandle);
1323 APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break);
1324
1325 std::string cmd = GetColdRunArgs(property, false, "appspawn -mode app_cold ");
1326 content = AppSpawnTestHelper::StartSpawnServer(cmd, args);
1327 APPSPAWN_CHECK_ONLY_EXPER(content != nullptr, break);
1328 ASSERT_EQ(content->mode, MODE_FOR_APP_COLD_RUN);
1329
1330 // spawn prepare process
1331 AppSpawnHookExecute(STAGE_PARENT_PRE_FORK, 0, content, &property->client);
1332 AppSpawnHookExecute(STAGE_CHILD_PRE_COLDBOOT, 0, content, &property->client);
1333 // run in cold mode
1334 // child run in TestRunChildProcessor
1335 RegChildLooper(content, TestRunChildProcessor);
1336 content->runAppSpawn(content, args->argc, args->argv);
1337 ret = 0;
1338 } while (0);
1339 if (args) {
1340 free(args);
1341 }
1342 DeleteAppSpawningCtx(property);
1343 DeleteAppSpawnMgr(GetAppSpawnMgr());
1344 AppSpawnClientDestroy(clientHandle);
1345 ASSERT_EQ(ret, 0);
1346 }
1347
1348 HWTEST_F(AppSpawnChildTest, App_Spawn_Cold_Run_004, TestSize.Level0)
1349 {
1350 AppSpawnClientHandle clientHandle = nullptr;
1351 AppSpawnReqMsgHandle reqHandle = 0;
1352 AppSpawningCtx *property = nullptr;
1353 AppSpawnContent *content = nullptr;
1354 CmdArgs *args = nullptr;
1355 int ret = -1;
1356 do {
1357 ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &clientHandle);
1358 APPSPAWN_CHECK(ret == 0, break, "Failed to create reqMgr %{public}s", APPSPAWN_SERVER_NAME);
1359 reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_APP_SPAWN, 0);
1360 APPSPAWN_CHECK(reqHandle != INVALID_REQ_HANDLE, break, "Failed to create req %{public}s", APPSPAWN_SERVER_NAME);
1361
1362 // asan set cold
1363 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_DEBUGGABLE);
1364 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_NATIVEDEBUG);
1365 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_BUNDLE_RESOURCES);
1366 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_ACCESS_BUNDLE_DIR);
1367 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_ASANENABLED);
1368 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_GWP_ENABLED_NORMAL);
1369
1370 ret = APPSPAWN_ARG_INVALID;
1371 property = g_testHelper.GetAppProperty(clientHandle, reqHandle);
1372 APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break);
1373
1374 std::string cmd = GetColdRunArgs(property, false, "appspawn -mode app_cold ");
1375 content = AppSpawnTestHelper::StartSpawnServer(cmd, args);
1376 APPSPAWN_CHECK_ONLY_EXPER(content != nullptr, break);
1377 ASSERT_EQ(content->mode, MODE_FOR_APP_COLD_RUN);
1378 // add property to content
1379 OH_ListAddTail(&GetAppSpawnMgr()->appSpawnQueue, &property->node);
1380 ProcessAppSpawnDumpMsg(nullptr);
1381 // spawn prepare process
1382 AppSpawnHookExecute(STAGE_PARENT_PRE_FORK, 0, content, &property->client);
1383 AppSpawnHookExecute(STAGE_CHILD_PRE_COLDBOOT, 0, content, &property->client);
1384 // run in cold mode
1385 // child run in TestRunChildProcessor
1386 RegChildLooper(content, TestRunChildProcessor);
1387 content->runAppSpawn(content, args->argc, args->argv);
1388 property = nullptr;
1389 ret = 0;
1390 } while (0);
1391 if (args) {
1392 free(args);
1393 }
1394 DeleteAppSpawningCtx(property);
1395 DeleteAppSpawnMgr(GetAppSpawnMgr());
1396 AppSpawnClientDestroy(clientHandle);
1397 ASSERT_EQ(ret, 0);
1398 }
1399 } // namespace OHOS
1400