• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 HWTEST_F(AppSpawnChildTest, App_Spawn_Child_Illegal_001, TestSize.Level0)
696 {
697     ASSERT_EQ(AppSpawnChild(nullptr, nullptr), -1);
698 }
699 
700 HWTEST_F(AppSpawnChildTest, App_Spawn_Child_Illegal_002, TestSize.Level0)
701 {
702     AppSpawnContent *content = nullptr;
703     content = CreateTestAppSpawnContent(NWEBSPAWN_SERVER_NAME, MODE_FOR_APP_COLD_RUN);
704     ASSERT_NE(content, nullptr);
705     AppSpawnDestroyContent(content);
706     content = nullptr;
707     ASSERT_EQ(AppSpawnChild(nullptr, nullptr), -1);
708 }
709 
710 HWTEST_F(AppSpawnChildTest, App_Spawn_Child_Illegal_003, TestSize.Level0)
711 {
712     AppSpawnClientHandle clientHandle = nullptr;
713     AppSpawnReqMsgHandle reqHandle = 0;
714     AppSpawningCtx *property = nullptr;
715     AppSpawnContent *content = nullptr;
716     int ret = -1;
717     do {
718         ret = AppSpawnClientInit(NWEBSPAWN_SERVER_NAME, &clientHandle);
719         APPSPAWN_CHECK(ret == 0, break, "Failed to create reqMgr %{public}s", NWEBSPAWN_SERVER_NAME);
720         // MSG_SPAWN_NATIVE_PROCESS and render
721         g_testHelper.SetTestUid(10010029);  // 10010029
722         reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_SPAWN_NATIVE_PROCESS, 0);
723         APPSPAWN_CHECK(reqHandle != INVALID_REQ_HANDLE, break, "Failed to create req");
724         AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_DEBUGGABLE);
725         AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_NATIVEDEBUG);
726         AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_BUNDLE_RESOURCES);
727         AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_ACCESS_BUNDLE_DIR);
728         AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_GWP_ENABLED_NORMAL);
729         ret = APPSPAWN_ARG_INVALID;
730         property = g_testHelper.GetAppProperty(clientHandle, reqHandle);
731         APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break);
732 
733         // spawn
734         property->client.flags = APP_COLD_START;
735         ret = AppSpawnChild(content, &property->client);
736         property = nullptr;
737         content = nullptr;
738     } while (0);
739     DeleteAppSpawningCtx(property);
740     AppSpawnClientDestroy(clientHandle);
741     AppSpawnDestroyContent(content);
742     LE_StopLoop(LE_GetDefaultLoop());
743     LE_CloseLoop(LE_GetDefaultLoop());
744     ASSERT_EQ(ret, -1);
745 }
746 
GetColdRunArgs(AppSpawningCtx * property,bool isNweb,const char * arg)747 static std::string GetColdRunArgs(AppSpawningCtx *property, bool isNweb, const char *arg)
748 {
749     std::string argStr = arg;
750     int ret = WriteMsgToChild(property, isNweb);
751     APPSPAWN_CHECK(ret == 0, return nullptr,
752         "Failed to get shm for %{public}s errno %{public}d", GetProcessName(property), errno);
753 
754     argStr += "  -fd -1 0  ";
755     argStr += std::to_string(property->forkCtx.msgSize);
756     argStr += "  -param ";
757     argStr += GetProcessName(property);
758     argStr += "  ";
759     argStr += std::to_string(property->client.id);
760     return argStr;
761 }
762 
763 /**
764  * @brief 测试冷启动后半部处理
765  *
766  */
767 HWTEST_F(AppSpawnChildTest, App_Spawn_Cold_Run_001, TestSize.Level0)
768 {
769     AppSpawnClientHandle clientHandle = nullptr;
770     AppSpawnReqMsgHandle reqHandle = 0;
771     AppSpawningCtx *property = nullptr;
772     AppSpawnContent *content = nullptr;
773     CmdArgs *args = nullptr;
774     int ret = -1;
775     do {
776         ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &clientHandle);
777         APPSPAWN_CHECK(ret == 0, break, "Failed to create reqMgr %{public}s", APPSPAWN_SERVER_NAME);
778         reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_APP_SPAWN, 0);
779         APPSPAWN_CHECK(reqHandle != INVALID_REQ_HANDLE, break, "Failed to create req %{public}s", APPSPAWN_SERVER_NAME);
780         // set cold start flags
781         AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_COLD_BOOT);
782 
783         ret = APPSPAWN_ARG_INVALID;
784         property = g_testHelper.GetAppProperty(clientHandle, reqHandle);
785         APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break);
786 
787         std::string cmd = GetColdRunArgs(property, false, "appspawn -mode app_cold ");
788         content = AppSpawnTestHelper::StartSpawnServer(cmd, args);
789         APPSPAWN_CHECK_ONLY_EXPER(content != nullptr, break);
790 
791         // spawn prepare process
792         AppSpawnHookExecute(STAGE_PARENT_PRE_FORK, 0, content, &property->client);
793         AppSpawnHookExecute(STAGE_CHILD_PRE_COLDBOOT, 0, content, &property->client);
794         // run in cold mode
795         // child run in TestRunChildProcessor
796         RegChildLooper(content, TestRunChildProcessor);
797         content->runAppSpawn(content, args->argc, args->argv);
798         ret = 0;
799     } while (0);
800     if (args) {
801         free(args);
802     }
803     DeleteAppSpawningCtx(property);
804     AppSpawnClientDestroy(clientHandle);
805     ASSERT_EQ(ret, 0);
806 }
807 
808 HWTEST_F(AppSpawnChildTest, App_Spawn_Cold_Run_002, TestSize.Level0)
809 {
810     AppSpawnClientHandle clientHandle = nullptr;
811     AppSpawnReqMsgHandle reqHandle = 0;
812     AppSpawningCtx *property = nullptr;
813     AppSpawnContent *content = nullptr;
814     CmdArgs *args = nullptr;
815     int ret = -1;
816     do {
817         ret = AppSpawnClientInit(NWEBSPAWN_SERVER_NAME, &clientHandle);
818         APPSPAWN_CHECK(ret == 0, break, "Failed to create reqMgr %{public}s", NWEBSPAWN_SERVER_NAME);
819         reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_APP_SPAWN, 0);
820         APPSPAWN_CHECK(reqHandle != INVALID_REQ_HANDLE, break, "Failed to create req ");
821         // set cold start flags
822         AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_COLD_BOOT);
823 
824         ret = APPSPAWN_ARG_INVALID;
825         property = g_testHelper.GetAppProperty(clientHandle, reqHandle);
826         APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break);
827 
828         std::string cmd = GetColdRunArgs(property, true, "appspawn -mode nweb_cold ");
829         content = AppSpawnTestHelper::StartSpawnServer(cmd, args);
830         APPSPAWN_CHECK_ONLY_EXPER(content != nullptr, break);
831         ASSERT_EQ(content->mode, MODE_FOR_NWEB_COLD_RUN);
832 
833         // spawn prepare process
834         AppSpawnHookExecute(STAGE_PARENT_PRE_FORK, 0, content, &property->client);
835         AppSpawnHookExecute(STAGE_CHILD_PRE_COLDBOOT, 0, content, &property->client);
836         // run in cold mode
837         // child run in TestRunChildProcessor
838         RegChildLooper(content, TestRunChildProcessor);
839         content->runAppSpawn(content, args->argc, args->argv);
840         ret = 0;
841     } while (0);
842     if (args) {
843         free(args);
844     }
845     DeleteAppSpawningCtx(property);
846     DeleteAppSpawnMgr(GetAppSpawnMgr());
847     AppSpawnClientDestroy(clientHandle);
848     ASSERT_EQ(ret, 0);
849 }
850 
851 HWTEST_F(AppSpawnChildTest, App_Spawn_Cold_Run_003, TestSize.Level0)
852 {
853     AppSpawnClientHandle clientHandle = nullptr;
854     AppSpawnReqMsgHandle reqHandle = 0;
855     AppSpawningCtx *property = nullptr;
856     AppSpawnContent *content = nullptr;
857     CmdArgs *args = nullptr;
858     int ret = -1;
859     do {
860         ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &clientHandle);
861         APPSPAWN_CHECK(ret == 0, break, "Failed to create reqMgr %{public}s", APPSPAWN_SERVER_NAME);
862         reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_APP_SPAWN, 0);
863         APPSPAWN_CHECK(reqHandle != INVALID_REQ_HANDLE, break, "Failed to create req %{public}s", APPSPAWN_SERVER_NAME);
864 
865         // asan set cold
866         AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_DEBUGGABLE);
867         AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_NATIVEDEBUG);
868         AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_BUNDLE_RESOURCES);
869         AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_ACCESS_BUNDLE_DIR);
870         AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_ASANENABLED);
871         AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_GWP_ENABLED_FORCE);
872 
873         ret = APPSPAWN_ARG_INVALID;
874         property = g_testHelper.GetAppProperty(clientHandle, reqHandle);
875         APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break);
876 
877         std::string cmd = GetColdRunArgs(property, false, "appspawn -mode app_cold ");
878         content = AppSpawnTestHelper::StartSpawnServer(cmd, args);
879         APPSPAWN_CHECK_ONLY_EXPER(content != nullptr, break);
880         ASSERT_EQ(content->mode, MODE_FOR_APP_COLD_RUN);
881 
882         // spawn prepare process
883         AppSpawnHookExecute(STAGE_PARENT_PRE_FORK, 0, content, &property->client);
884         AppSpawnHookExecute(STAGE_CHILD_PRE_COLDBOOT, 0, content, &property->client);
885         // run in cold mode
886         // child run in TestRunChildProcessor
887         RegChildLooper(content, TestRunChildProcessor);
888         content->runAppSpawn(content, args->argc, args->argv);
889         ret = 0;
890     } while (0);
891     if (args) {
892         free(args);
893     }
894     DeleteAppSpawningCtx(property);
895     DeleteAppSpawnMgr(GetAppSpawnMgr());
896     AppSpawnClientDestroy(clientHandle);
897     ASSERT_EQ(ret, 0);
898 }
899 
900 HWTEST_F(AppSpawnChildTest, App_Spawn_Cold_Run_004, TestSize.Level0)
901 {
902     AppSpawnClientHandle clientHandle = nullptr;
903     AppSpawnReqMsgHandle reqHandle = 0;
904     AppSpawningCtx *property = nullptr;
905     AppSpawnContent *content = nullptr;
906     CmdArgs *args = nullptr;
907     int ret = -1;
908     do {
909         ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &clientHandle);
910         APPSPAWN_CHECK(ret == 0, break, "Failed to create reqMgr %{public}s", APPSPAWN_SERVER_NAME);
911         reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_APP_SPAWN, 0);
912         APPSPAWN_CHECK(reqHandle != INVALID_REQ_HANDLE, break, "Failed to create req %{public}s", APPSPAWN_SERVER_NAME);
913 
914         // asan set cold
915         AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_DEBUGGABLE);
916         AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_NATIVEDEBUG);
917         AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_BUNDLE_RESOURCES);
918         AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_ACCESS_BUNDLE_DIR);
919         AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_ASANENABLED);
920         AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_GWP_ENABLED_NORMAL);
921 
922         ret = APPSPAWN_ARG_INVALID;
923         property = g_testHelper.GetAppProperty(clientHandle, reqHandle);
924         APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break);
925 
926         std::string cmd = GetColdRunArgs(property, false, "appspawn -mode app_cold ");
927         content = AppSpawnTestHelper::StartSpawnServer(cmd, args);
928         APPSPAWN_CHECK_ONLY_EXPER(content != nullptr, break);
929         ASSERT_EQ(content->mode, MODE_FOR_APP_COLD_RUN);
930         // add property to content
931         OH_ListAddTail(&GetAppSpawnMgr()->appSpawnQueue, &property->node);
932         ProcessAppSpawnDumpMsg(nullptr);
933         // spawn prepare process
934         AppSpawnHookExecute(STAGE_PARENT_PRE_FORK, 0, content, &property->client);
935         AppSpawnHookExecute(STAGE_CHILD_PRE_COLDBOOT, 0, content, &property->client);
936         // run in cold mode
937         // child run in TestRunChildProcessor
938         RegChildLooper(content, TestRunChildProcessor);
939         content->runAppSpawn(content, args->argc, args->argv);
940         property = nullptr;
941         ret = 0;
942     } while (0);
943     if (args) {
944         free(args);
945     }
946     DeleteAppSpawningCtx(property);
947     DeleteAppSpawnMgr(GetAppSpawnMgr());
948     AppSpawnClientDestroy(clientHandle);
949     ASSERT_EQ(ret, 0);
950 }
951 }  // namespace OHOS
952