• 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_manager.h"
27 #include "appspawn_modulemgr.h"
28 #include "appspawn_server.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 APPSPAWN_STATIC int PreLoadNwebSpawn(AppSpawnMgr *content);
41 
42 namespace OHOS {
43 class NWebSpawnServiceTest : public testing::Test {
44 public:
SetUpTestCase()45     static void SetUpTestCase() {}
TearDownTestCase()46     static void TearDownTestCase() {}
SetUp()47     void SetUp()
48     {
49         testServer = std::make_unique<OHOS::AppSpawnTestServer>("appspawn -mode nwebspawn");
50         if (testServer != nullptr) {
51             testServer->Start(nullptr);
52         }
53     }
TearDown()54     void TearDown()
55     {
56         if (testServer != nullptr) {
57             testServer->Stop();
58         }
59     }
60 public:
61     std::unique_ptr<OHOS::AppSpawnTestServer> testServer = nullptr;
62 };
63 
64 /**
65  * @brief 正常消息发送和接收,完整应用孵化过程
66  *
67  */
68 HWTEST_F(NWebSpawnServiceTest, NWeb_Spawn_001, TestSize.Level0)
69 {
70     int ret = 0;
71     AppSpawnClientHandle clientHandle = nullptr;
72     do {
73         APPSPAWN_LOGV("NWeb_Spawn_001 start");
74         ret = AppSpawnClientInit(NWEBSPAWN_SERVER_NAME, &clientHandle);
75         APPSPAWN_CHECK(ret == 0, break, "Failed to create client %{public}s", NWEBSPAWN_SERVER_NAME);
76         AppSpawnReqMsgHandle reqHandle = testServer->CreateMsg(clientHandle, MSG_APP_SPAWN, 0);
77 
78         AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_NO_SANDBOX);
79         AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_DEBUGGABLE);
80         AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_NATIVEDEBUG);
81         AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_BUNDLE_RESOURCES);
82         AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_ACCESS_BUNDLE_DIR);
83 
84         AppSpawnResult result = {};
85         ret = AppSpawnClientSendMsg(clientHandle, reqHandle, &result);
86         APPSPAWN_LOGV("NWeb_Spawn_001 recv result %{public}d  %{public}d", result.result, result.pid);
87         APPSPAWN_CHECK(ret == 0, break, "Failed to send msg %{public}d", ret);
88         if (ret == 0 && result.pid > 0) {
89             APPSPAWN_LOGV("NWeb_Spawn_001 Kill pid %{public}d ", result.pid);
90             kill(result.pid, SIGKILL);
91         }
92     } while (0);
93 
94     AppSpawnClientDestroy(clientHandle);
95     ASSERT_EQ(ret, 0);
96 }
97 
98 /**
99  * @brief 模拟测试,孵化进程退出后,MSG_GET_RENDER_TERMINATION_STATUS
100  *
101  */
102 HWTEST_F(NWebSpawnServiceTest, NWeb_Spawn_002, TestSize.Level0)
103 {
104     int ret = 0;
105     AppSpawnClientHandle clientHandle = nullptr;
106     do {
107         APPSPAWN_LOGV("NWeb_Spawn_002 start");
108         ret = AppSpawnClientInit(NWEBSPAWN_SERVER_NAME, &clientHandle);
109         APPSPAWN_CHECK(ret == 0, break, "Failed to create client %{public}s", NWEBSPAWN_SERVER_NAME);
110         AppSpawnReqMsgHandle reqHandle = testServer->CreateMsg(clientHandle, MSG_APP_SPAWN, 0);
111         AppSpawnResult result = {};
112         ret = AppSpawnClientSendMsg(clientHandle, reqHandle, &result);
113         APPSPAWN_LOGV("NWeb_Spawn_002 recv result %{public}d  %{public}d", result.result, result.pid);
114         if (ret != 0 || result.pid == 0) {
115             ret = -1;
116             break;
117         }
118         // stop child and termination
119         APPSPAWN_LOGV("NWeb_Spawn_002 kill pid %{public}d", result.pid);
120         kill(result.pid, SIGKILL);
121         // MSG_GET_RENDER_TERMINATION_STATUS
122         ret = AppSpawnTerminateMsgCreate(result.pid, &reqHandle);
123         APPSPAWN_CHECK(ret == 0, break, "Failed to create termination msg %{public}s", NWEBSPAWN_SERVER_NAME);
124         ret = AppSpawnClientSendMsg(clientHandle, reqHandle, &result);
125         APPSPAWN_LOGV("Send MSG_GET_RENDER_TERMINATION_STATUS %{public}d", ret);
126     } while (0);
127 
128     AppSpawnClientDestroy(clientHandle);
129     ASSERT_EQ(ret, 0);
130 }
131 
132 /**
133  * @brief 模拟测试,MSG_GET_RENDER_TERMINATION_STATUS 关闭孵化进程
134  *
135  */
136 HWTEST_F(NWebSpawnServiceTest, NWeb_Spawn_003, TestSize.Level0)
137 {
138     int ret = 0;
139     AppSpawnClientHandle clientHandle = nullptr;
140     do {
141         ret = AppSpawnClientInit(NWEBSPAWN_SERVER_NAME, &clientHandle);
142         APPSPAWN_CHECK(ret == 0, break, "Failed to create client %{public}s", NWEBSPAWN_SERVER_NAME);
143         AppSpawnReqMsgHandle reqHandle = testServer->CreateMsg(clientHandle, MSG_APP_SPAWN, 0);
144         AppSpawnResult result = {};
145         ret = AppSpawnClientSendMsg(clientHandle, reqHandle, &result);
146         APPSPAWN_LOGV("NWeb_Spawn_003 recv result %{public}d  %{public}d", result.result, result.pid);
147         if (ret != 0 || result.pid == 0) {
148             ret = -1;
149             break;
150         }
151         // MSG_GET_RENDER_TERMINATION_STATUS
152         ret = AppSpawnTerminateMsgCreate(result.pid, &reqHandle);
153         APPSPAWN_CHECK(ret == 0, break, "Failed to create termination msg %{public}s", NWEBSPAWN_SERVER_NAME);
154         ret = AppSpawnClientSendMsg(clientHandle, reqHandle, &result);
155         APPSPAWN_LOGV("Send MSG_GET_RENDER_TERMINATION_STATUS %{public}d", ret);
156     } while (0);
157 
158     AppSpawnClientDestroy(clientHandle);
159     ASSERT_EQ(ret, 0);
160 }
161 
162 /**
163  * @brief dump 消息
164  *
165  */
166 HWTEST_F(NWebSpawnServiceTest, NWeb_Spawn_004, TestSize.Level0)
167 {
168     int ret = 0;
169     AppSpawnClientHandle clientHandle = nullptr;
170     do {
171         ret = AppSpawnClientInit(NWEBSPAWN_SERVER_NAME, &clientHandle);
172         APPSPAWN_CHECK(ret == 0, break, "Failed to create client %{public}s", NWEBSPAWN_SERVER_NAME);
173         AppSpawnReqMsgHandle reqHandle = testServer->CreateMsg(clientHandle, MSG_DUMP, 0);
174         AppSpawnResult result = {};
175         ret = AppSpawnClientSendMsg(clientHandle, reqHandle, &result);
176         APPSPAWN_CHECK(ret == 0, break, "Failed to send msg %{public}d", ret);
177     } while (0);
178 
179     AppSpawnClientDestroy(clientHandle);
180     ASSERT_EQ(ret, 0);
181 }
182 
183 /**
184  * @brief MSG_SPAWN_NATIVE_PROCESS 正常消息发送和接收,完整应用孵化过程
185  *
186  */
187 HWTEST_F(NWebSpawnServiceTest, NWeb_Spawn_005, TestSize.Level0)
188 {
189     int ret = 0;
190     AppSpawnClientHandle clientHandle = nullptr;
191     do {
192         ret = AppSpawnClientInit(NWEBSPAWN_SERVER_NAME, &clientHandle);
193         APPSPAWN_CHECK(ret == 0, break, "Failed to create client %{public}s", NWEBSPAWN_SERVER_NAME);
194         AppSpawnReqMsgHandle reqHandle = testServer->CreateMsg(clientHandle, MSG_SPAWN_NATIVE_PROCESS, 0);
195 
196         AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_DEBUGGABLE);
197         AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_NATIVEDEBUG);
198         AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_BUNDLE_RESOURCES);
199         AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_ACCESS_BUNDLE_DIR);
200 
201         AppSpawnResult result = {};
202         ret = AppSpawnClientSendMsg(clientHandle, reqHandle, &result);
203         APPSPAWN_CHECK(ret == 0, break, "Failed to send msg %{public}d", ret);
204         if (ret == 0 && result.pid > 0) {
205             APPSPAWN_LOGI("NWeb_Spawn_Msg_001 Kill pid %{public}d ", result.pid);
206             kill(result.pid, SIGKILL);
207         }
208     } while (0);
209 
210     AppSpawnClientDestroy(clientHandle);
211     ASSERT_EQ(ret, 0);
212 }
213 
214 HWTEST_F(NWebSpawnServiceTest, NWeb_Spawn_Msg_001, TestSize.Level0)
215 {
216     int ret = -1;
217     int socketId = -1;
218     // 没有tlv的消息,返回错误
219     do {
220         socketId = testServer->CreateSocket(1);
221         APPSPAWN_CHECK(socketId >= 0, break, "Failed to create socket %{public}s", NWEBSPAWN_SERVER_NAME);
222 
223         std::vector<uint8_t> buffer(sizeof(AppSpawnResponseMsg));
224         uint32_t msgLen = 0;
225         ret = testServer->CreateSendMsg(buffer, MSG_APP_SPAWN, msgLen, {});
226         APPSPAWN_CHECK(ret == 0, break, "Failed to create msg %{public}s", testServer->GetDefaultTestAppBundleName());
227 
228         int len = write(socketId, buffer.data(), msgLen);
229         APPSPAWN_CHECK(len > 0, break, "Failed to send msg %{public}s", testServer->GetDefaultTestAppBundleName());
230         // recv
231         APPSPAWN_LOGV("Start recv ... ");
232         len = read(socketId, buffer.data(), buffer.size());
233         APPSPAWN_CHECK(len >= static_cast<int>(sizeof(AppSpawnResponseMsg)), ret = -1;
234             break, "Failed to recv msg errno: %{public}d", errno);
235         AppSpawnResponseMsg *respMsg = reinterpret_cast<AppSpawnResponseMsg *>(buffer.data());
236         APPSPAWN_LOGV("Recv msg %{public}s result: %{public}d", respMsg->msgHdr.processName, respMsg->result.result);
237         ret = respMsg->result.result;
238     } while (0);
239     if (socketId >= 0) {
240         CloseClientSocket(socketId);
241     }
242     ASSERT_NE(ret, 0);
243 }
244 
RecvMsg(int socketId,uint8_t * buffer,uint32_t buffSize)245 static int RecvMsg(int socketId, uint8_t *buffer, uint32_t buffSize)
246 {
247     ssize_t rLen = TEMP_FAILURE_RETRY(read(socketId, buffer, buffSize));
248     return static_cast<int>(rLen);
249 }
250 
251 /**
252  * @brief 消息不完整,断开连接
253  *
254  */
255 HWTEST_F(NWebSpawnServiceTest, NWeb_Spawn_Msg_002, TestSize.Level0)
256 {
257     int ret = 0;
258     int socketId = -1;
259     do {
260         socketId = testServer->CreateSocket(1);
261         APPSPAWN_CHECK(socketId >= 0, break, "Failed to create socket %{public}s", NWEBSPAWN_SERVER_NAME);
262 
263         std::vector<uint8_t> buffer(sizeof(AppSpawnResponseMsg));
264         uint32_t msgLen = 0;
265         ret = testServer->CreateSendMsg(buffer, MSG_APP_SPAWN, msgLen, {});
266         APPSPAWN_CHECK(ret == 0, break, "Failed to create msg %{public}s", testServer->GetDefaultTestAppBundleName());
267 
268         ret = -1;
269         int len = write(socketId, buffer.data(), msgLen - 10);  // 10
270         APPSPAWN_CHECK(len > 0, break, "Failed to send msg %{public}s", testServer->GetDefaultTestAppBundleName());
271         // recv timeout
272         len = RecvMsg(socketId, buffer.data(), buffer.size());
273         APPSPAWN_CHECK(len <= 0, break, "Failed to recv msg len: %{public}d", len);
274         ret = 0;
275     } while (0);
276     if (socketId >= 0) {
277         CloseClientSocket(socketId);
278     }
279     ASSERT_EQ(ret, 0);
280 }
281 
282 /**
283  * @brief 测试异常tlv
284  *
285  */
286 HWTEST_F(NWebSpawnServiceTest, NWeb_Spawn_Msg_003, TestSize.Level0)
287 {
288     int ret = -1;
289     int socketId = -1;
290     do {
291         socketId = testServer->CreateSocket(1);
292         APPSPAWN_CHECK(socketId >= 0, break, "Failed to create socket %{public}s", NWEBSPAWN_SERVER_NAME);
293 
294         std::vector<uint8_t> buffer(sizeof(AppSpawnResponseMsg));
295         uint32_t msgLen = 0;
296         ret = testServer->CreateSendMsg(buffer, MSG_APP_SPAWN, msgLen, {});
297         APPSPAWN_CHECK(ret == 0, break, "Failed to create msg %{public}s", testServer->GetDefaultTestAppBundleName());
298 
299         int len = write(socketId, buffer.data(), msgLen);
300         APPSPAWN_CHECK(len > 0, break, "Failed to send msg %{public}s", testServer->GetDefaultTestAppBundleName());
301         // recv
302         len = RecvMsg(socketId, buffer.data(), buffer.size());
303         APPSPAWN_CHECK(len >= static_cast<int>(sizeof(AppSpawnResponseMsg)), ret = -1;
304             break, "Failed to recv msg %{public}s", NWEBSPAWN_SERVER_NAME);
305         AppSpawnResponseMsg *respMsg = reinterpret_cast<AppSpawnResponseMsg *>(buffer.data());
306         APPSPAWN_LOGV("Recv msg %{public}s result: %{public}d", respMsg->msgHdr.processName, respMsg->result.result);
307         ret = respMsg->result.result == APPSPAWN_MSG_INVALID ? 0 : -1;
308     } while (0);
309     if (socketId >= 0) {
310         CloseClientSocket(socketId);
311     }
312     ASSERT_EQ(ret, 0);
313 }
314 
AddProcessTypeExtTlv(uint8_t * buffer,uint32_t bufferLen,uint32_t & realLen,uint32_t & tlvCount)315 int AddProcessTypeExtTlv(uint8_t *buffer, uint32_t bufferLen, uint32_t &realLen, uint32_t &tlvCount)
316 {
317     const char *testData = "render";
318     uint32_t currLen = 0;
319     AppSpawnTlvExt tlv = {};
320     tlv.tlvType = TLV_MAX;
321     int ret = strcpy_s(tlv.tlvName, sizeof(tlv.tlvName), "ProcessType");
322     APPSPAWN_CHECK(ret == 0, return -1, "Failed to strcpy");
323     tlv.dataLen = strlen(testData) + 1;
324     tlv.tlvLen = sizeof(AppSpawnTlvExt) + APPSPAWN_ALIGN(tlv.dataLen);
325 
326     ret = memcpy_s(buffer, bufferLen, &tlv, sizeof(tlv));
327     if (ret != 0) {
328         APPSPAWN_LOGE("Failed to memcpy_s bufferSize");
329         return -1;
330     }
331 
332     ret = memcpy_s(buffer + sizeof(tlv), bufferLen - sizeof(tlv), testData, tlv.dataLen + 1);
333     if (ret != 0) {
334         APPSPAWN_LOGE("Failed to memcpy_s bufferSize");
335         return -1;
336     }
337 
338     currLen += tlv.tlvLen;
339     tlvCount++;
340     realLen = currLen;
341     return 0;
342 }
343 
344 /**
345  * @brief 测试小包发送,随机获取发送大小,发送数据。消息20时,按33发送
346  *
347  */
348 HWTEST_F(NWebSpawnServiceTest, NWeb_Spawn_Msg_004, TestSize.Level0)
349 {
350     int ret = -1;
351     int socketId = -1;
352     do {
353         socketId = testServer->CreateSocket(1);
354         APPSPAWN_CHECK(socketId >= 0, break, "Failed to create socket %{public}s", NWEBSPAWN_SERVER_NAME);
355 
356         std::vector<uint8_t> buffer(1024, 0);  // 1024 1k
357         uint32_t msgLen = 0;
358         ret = testServer->CreateSendMsg(buffer, MSG_APP_SPAWN, msgLen,
359             {AppSpawnTestHelper::AddBaseTlv, AddProcessTypeExtTlv});
360         APPSPAWN_CHECK(ret == 0, break, "Failed to create msg %{public}s", testServer->GetDefaultTestAppBundleName());
361 
362         // 分片发送
363         uint32_t sendStep = OHOS::AppSpawnTestHelper::GenRandom() % 70;  // 70 一次发送的字节数
364         sendStep = (sendStep < 20) ? 33 : sendStep;                      // 20 33 一次发送的字节数
365         APPSPAWN_LOGV("NWeb_Spawn_Msg_005 msgLen %{public}u sendStep: %{public}u", msgLen, sendStep);
366         uint32_t currIndex = 0;
367         int len = 0;
368         do {
369             if ((currIndex + sendStep) > msgLen) {
370                 break;
371             }
372             len = write(socketId, buffer.data() + currIndex, sendStep);
373             APPSPAWN_CHECK(len > 0, break, "Failed to send %{public}s", testServer->GetDefaultTestAppBundleName());
374             usleep(2000);  // wait recv
375             currIndex += sendStep;
376         } while (1);
377         APPSPAWN_CHECK(len > 0, break, "Failed to send %{public}s", testServer->GetDefaultTestAppBundleName());
378         if (msgLen > currIndex) {
379             len = write(socketId, buffer.data() + currIndex, msgLen - currIndex);
380             APPSPAWN_CHECK(len > 0, break, "Failed to send %{public}s", testServer->GetDefaultTestAppBundleName());
381         }
382 
383         // recv
384         len = RecvMsg(socketId, buffer.data(), buffer.size());
385         APPSPAWN_CHECK(len >= static_cast<int>(sizeof(AppSpawnResponseMsg)), ret = -1;
386             break, "Failed to recv msg %{public}s", NWEBSPAWN_SERVER_NAME);
387         AppSpawnResponseMsg *respMsg = reinterpret_cast<AppSpawnResponseMsg *>(buffer.data());
388         APPSPAWN_LOGV("NWeb_Spawn_Msg_005 recv msg %{public}s result: %{public}d",
389             respMsg->msgHdr.processName, respMsg->result.result);
390         ret = respMsg->result.result;
391     } while (0);
392     if (socketId >= 0) {
393         CloseClientSocket(socketId);
394     }
395     ASSERT_EQ(ret, 0);
396 }
397 
398 /**
399  * @brief 测试2个消息一起发送
400  *
401  */
402 HWTEST_F(NWebSpawnServiceTest, NWeb_Spawn_Msg_005, TestSize.Level0)
403 {
404     int ret = -1;
405     int socketId = -1;
406     do {
407         socketId = testServer->CreateSocket(1);
408         APPSPAWN_CHECK(socketId >= 0, break, "Failed to create socket %{public}s", NWEBSPAWN_SERVER_NAME);
409 
410         std::vector<uint8_t> buffer1(1024);  // 1024
411         std::vector<uint8_t> buffer2(1024);  // 1024
412         uint32_t msgLen1 = 0;
413         uint32_t msgLen2 = 0;
414         ret = testServer->CreateSendMsg(buffer1, MSG_APP_SPAWN, msgLen1,
415             {AppSpawnTestHelper::AddBaseTlv, AddProcessTypeExtTlv});
416         APPSPAWN_CHECK(ret == 0, break, "Failed to create msg %{public}s", testServer->GetDefaultTestAppBundleName());
417         ret = testServer->CreateSendMsg(buffer2, MSG_APP_SPAWN, msgLen2,
418             {AppSpawnTestHelper::AddBaseTlv, AddProcessTypeExtTlv});
419         APPSPAWN_CHECK(ret == 0, break, "Failed to create msg %{public}s", testServer->GetDefaultTestAppBundleName());
420 
421         buffer1.insert(buffer1.begin() + msgLen1, buffer2.begin(), buffer2.end());
422         int len = write(socketId, buffer1.data(), msgLen1 + msgLen2);
423         APPSPAWN_CHECK(len > 0, break, "Failed to send msg %{public}s", testServer->GetDefaultTestAppBundleName());
424         // recv
425         len = RecvMsg(socketId, buffer2.data(), buffer2.size());
426         APPSPAWN_CHECK(len >= static_cast<int>(sizeof(AppSpawnResponseMsg)), ret = -1;
427             break, "Failed to recv msg %{public}s", NWEBSPAWN_SERVER_NAME);
428         AppSpawnResponseMsg *respMsg = reinterpret_cast<AppSpawnResponseMsg *>(buffer2.data());
429         APPSPAWN_LOGV("NWeb_Spawn_Msg_005 Recv msg %{public}s result: %{public}d",
430             respMsg->msgHdr.processName, respMsg->result.result);
431         ret = respMsg->result.result;
432         (void)RecvMsg(socketId, buffer2.data(), buffer2.size());
433     } while (0);
434     if (socketId >= 0) {
435         CloseClientSocket(socketId);
436     }
437     ASSERT_EQ(ret, 0);
438 }
439 
440 /**
441  * @brief 测试连续2个消息,spawn和dump 消息
442  *
443  */
444 HWTEST_F(NWebSpawnServiceTest, NWeb_Spawn_Msg_006, TestSize.Level0)
445 {
446     int ret = -1;
447     int socketId = -1;
448     do {
449         socketId = testServer->CreateSocket(1);
450         APPSPAWN_CHECK(socketId >= 0, break, "Failed to create socket %{public}s", NWEBSPAWN_SERVER_NAME);
451 
452         std::vector<uint8_t> buffer1(2 * 1024);  // 2 * 1024
453         std::vector<uint8_t> buffer2(1024);  // 1024
454         uint32_t msgLen1 = 0;
455         uint32_t msgLen2 = 0;
456         ret = testServer->CreateSendMsg(buffer1, MSG_APP_SPAWN, msgLen1, {AppSpawnTestHelper::AddBaseTlv});
457         APPSPAWN_CHECK(ret == 0, break, "Failed to create msg %{public}s", testServer->GetDefaultTestAppBundleName());
458         ret = testServer->CreateSendMsg(buffer2, MSG_DUMP, msgLen2, {});
459         APPSPAWN_CHECK(ret == 0, break, "Failed to create msg %{public}s", testServer->GetDefaultTestAppBundleName());
460 
461         buffer1.insert(buffer1.begin() + msgLen1, buffer2.begin(), buffer2.end());
462         int len = write(socketId, buffer1.data(), msgLen1 + msgLen2);
463         APPSPAWN_CHECK(len > 0, break, "Failed to send msg %{public}s", testServer->GetDefaultTestAppBundleName());
464         // recv
465         len = RecvMsg(socketId, buffer2.data(), buffer2.size());
466         APPSPAWN_CHECK(len >= static_cast<int>(sizeof(AppSpawnResponseMsg)), ret = -1;
467             break, "Failed to recv msg %{public}s", NWEBSPAWN_SERVER_NAME);
468         AppSpawnResponseMsg *respMsg = reinterpret_cast<AppSpawnResponseMsg *>(buffer2.data());
469         APPSPAWN_LOGV("NWeb_Spawn_Msg_006 recv msg %{public}s result: %{public}d",
470             respMsg->msgHdr.processName, respMsg->result.result);
471         ret = respMsg->result.result;
472         (void)RecvMsg(socketId, buffer2.data(), buffer2.size());
473     } while (0);
474     if (socketId >= 0) {
475         CloseClientSocket(socketId);
476     }
477     ASSERT_EQ(ret, 0);
478 }
479 
480 /**
481  * @brief 测试连接中断
482  *
483  */
484 HWTEST_F(NWebSpawnServiceTest, NWeb_Spawn_Msg_007, TestSize.Level0)
485 {
486     int ret = -1;
487     int socketId = -1;
488     do {
489         socketId = testServer->CreateSocket(1);
490         APPSPAWN_CHECK(socketId >= 0, break, "Failed to create socket %{public}s", NWEBSPAWN_SERVER_NAME);
491         std::vector<uint8_t> buffer(1024, 0);  // 1024 1k
492         uint32_t msgLen = 0;
493         ret = testServer->CreateSendMsg(buffer, MSG_APP_SPAWN, msgLen, {AppSpawnTestHelper::AddBaseTlv});
494         APPSPAWN_CHECK(ret == 0, break, "Failed to create msg %{public}s", testServer->GetDefaultTestAppBundleName());
495 
496         int len = write(socketId, buffer.data(), msgLen);
497         APPSPAWN_CHECK(len > 0, break, "Failed to send msg %{public}s", testServer->GetDefaultTestAppBundleName());
498         // close socket
499         APPSPAWN_LOGV("CloseClientSocket");
500         CloseClientSocket(socketId);
501         socketId = -1;
502     } while (0);
503     if (socketId >= 0) {
504         CloseClientSocket(socketId);
505     }
506     ASSERT_EQ(ret, 0);
507 }
508 
509 /**
510  * @brief 发送不完整报文,等待超时
511  *
512  */
513 HWTEST_F(NWebSpawnServiceTest, NWeb_Spawn_Msg_008, TestSize.Level0)
514 {
515     int ret = 0;
516     int socketId = -1;
517     do {
518         socketId = testServer->CreateSocket(1);
519         APPSPAWN_CHECK(socketId >= 0, break, "Failed to create socket %{public}s", NWEBSPAWN_SERVER_NAME);
520         std::vector<uint8_t> buffer(1024, 0);  // 1024 1k
521         uint32_t msgLen = 0;
522         ret = testServer->CreateSendMsg(buffer, MSG_APP_SPAWN, msgLen, {AppSpawnTestHelper::AddBaseTlv});
523         APPSPAWN_CHECK(ret == 0, break, "Failed to create msg %{public}s", testServer->GetDefaultTestAppBundleName());
524         int len = write(socketId, buffer.data(), msgLen - 20);  // 20 test
525         APPSPAWN_CHECK(len > 0, break, "Failed to send msg %{public}s", testServer->GetDefaultTestAppBundleName());
526         // recv
527         len = read(socketId, buffer.data(), buffer.size());  // timeout EAGAIN
528         APPSPAWN_CHECK(len <= 0, ret = -1; break, "Can not receive timeout %{public}d", errno);
529     } while (0);
530     if (socketId >= 0) {
531         CloseClientSocket(socketId);
532     }
533     ASSERT_EQ(ret, 0);
534 }
535 
536 /**
537  * @brief nwebspawn进行预加载
538  * @note 预期结果: nwebspawn不支持预加载,直接返回
539  *
540  */
541 HWTEST_F(NWebSpawnServiceTest, NWeb_Spawn_Msg_009, TestSize.Level0)
542 {
543     GTEST_LOG_(INFO) << "NWebSpawnServiceTest NWeb_Spawn_Msg_009 start run";
544     AppSpawnMgr *mgr = CreateAppSpawnMgr(MODE_FOR_APP_SPAWN);
545     ASSERT_NE(mgr, nullptr);
546     mgr->content.longProcName = const_cast<char *>(APPSPAWN_SERVER_NAME);
547     mgr->content.longProcNameLen = APP_LEN_PROC_NAME;
548 
549     int ret = PreLoadNwebSpawn(mgr);
550     EXPECT_EQ(ret, 0);
551 }
552 
553 /**
554  * @brief nwebspawn进行预加载
555  * @note 预期结果: 通过校验,进行nwebspawn预加载
556  *
557  */
558 HWTEST_F(NWebSpawnServiceTest, NWeb_Spawn_Msg_010, TestSize.Level0)
559 {
560     GTEST_LOG_(INFO) << "NWebSpawnServiceTest NWeb_Spawn_Msg_010 start run";
561     AppSpawnMgr *mgr = CreateAppSpawnMgr(MODE_FOR_NWEB_SPAWN);
562     ASSERT_NE(mgr, nullptr);
563     mgr->content.longProcName = const_cast<char *>(NWEBSPAWN_SERVER_NAME);
564     mgr->content.longProcNameLen = APP_LEN_PROC_NAME;
565 
566     int ret = PreLoadNwebSpawn(mgr);
567     EXPECT_EQ(ret, 0);
568 }
569 }  // namespace OHOS
570