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