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 
313 /**
314  * @brief 测试小包发送,随机获取发送大小,发送数据。消息20时,按33发送
315  *
316  */
317 HWTEST_F(NWebSpawnServiceTest, NWeb_Spawn_Msg_004, TestSize.Level0)
318 {
319     int ret = -1;
320     int socketId = -1;
321     do {
322         socketId = testServer->CreateSocket(1);
323         APPSPAWN_CHECK(socketId >= 0, break, "Failed to create socket %{public}s", NWEBSPAWN_SERVER_NAME);
324 
325         std::vector<uint8_t> buffer(1024, 0);  // 1024 1k
326         uint32_t msgLen = 0;
327         ret = testServer->CreateSendMsg(buffer, MSG_APP_SPAWN, msgLen, {AppSpawnTestHelper::AddBaseTlv});
328         APPSPAWN_CHECK(ret == 0, break, "Failed to create msg %{public}s", testServer->GetDefaultTestAppBundleName());
329 
330         // 分片发送
331         uint32_t sendStep = OHOS::AppSpawnTestHelper::GenRandom() % 70;  // 70 一次发送的字节数
332         sendStep = (sendStep < 20) ? 33 : sendStep;                      // 20 33 一次发送的字节数
333         APPSPAWN_LOGV("NWeb_Spawn_Msg_005 msgLen %{public}u sendStep: %{public}u", msgLen, sendStep);
334         uint32_t currIndex = 0;
335         int len = 0;
336         do {
337             if ((currIndex + sendStep) > msgLen) {
338                 break;
339             }
340             len = write(socketId, buffer.data() + currIndex, sendStep);
341             APPSPAWN_CHECK(len > 0, break, "Failed to send %{public}s", testServer->GetDefaultTestAppBundleName());
342             usleep(2000);  // wait recv
343             currIndex += sendStep;
344         } while (1);
345         APPSPAWN_CHECK(len > 0, break, "Failed to send %{public}s", testServer->GetDefaultTestAppBundleName());
346         if (msgLen > currIndex) {
347             len = write(socketId, buffer.data() + currIndex, msgLen - currIndex);
348             APPSPAWN_CHECK(len > 0, break, "Failed to send %{public}s", testServer->GetDefaultTestAppBundleName());
349         }
350 
351         // recv
352         len = RecvMsg(socketId, buffer.data(), buffer.size());
353         APPSPAWN_CHECK(len >= static_cast<int>(sizeof(AppSpawnResponseMsg)), ret = -1;
354             break, "Failed to recv msg %{public}s", NWEBSPAWN_SERVER_NAME);
355         AppSpawnResponseMsg *respMsg = reinterpret_cast<AppSpawnResponseMsg *>(buffer.data());
356         APPSPAWN_LOGV("NWeb_Spawn_Msg_005 recv msg %{public}s result: %{public}d",
357             respMsg->msgHdr.processName, respMsg->result.result);
358         ret = respMsg->result.result;
359     } while (0);
360     if (socketId >= 0) {
361         CloseClientSocket(socketId);
362     }
363     ASSERT_EQ(ret, 0);
364 }
365 
366 /**
367  * @brief 测试2个消息一起发送
368  *
369  */
370 HWTEST_F(NWebSpawnServiceTest, NWeb_Spawn_Msg_005, TestSize.Level0)
371 {
372     int ret = -1;
373     int socketId = -1;
374     do {
375         socketId = testServer->CreateSocket(1);
376         APPSPAWN_CHECK(socketId >= 0, break, "Failed to create socket %{public}s", NWEBSPAWN_SERVER_NAME);
377 
378         std::vector<uint8_t> buffer1(1024);  // 1024
379         std::vector<uint8_t> buffer2(1024);  // 1024
380         uint32_t msgLen1 = 0;
381         uint32_t msgLen2 = 0;
382         ret = testServer->CreateSendMsg(buffer1, MSG_APP_SPAWN, msgLen1, {AppSpawnTestHelper::AddBaseTlv});
383         APPSPAWN_CHECK(ret == 0, break, "Failed to create msg %{public}s", testServer->GetDefaultTestAppBundleName());
384         ret = testServer->CreateSendMsg(buffer2, MSG_APP_SPAWN, msgLen2, {AppSpawnTestHelper::AddBaseTlv});
385         APPSPAWN_CHECK(ret == 0, break, "Failed to create msg %{public}s", testServer->GetDefaultTestAppBundleName());
386 
387         buffer1.insert(buffer1.begin() + msgLen1, buffer2.begin(), buffer2.end());
388         int len = write(socketId, buffer1.data(), msgLen1 + msgLen2);
389         APPSPAWN_CHECK(len > 0, break, "Failed to send msg %{public}s", testServer->GetDefaultTestAppBundleName());
390         // recv
391         len = RecvMsg(socketId, buffer2.data(), buffer2.size());
392         APPSPAWN_CHECK(len >= static_cast<int>(sizeof(AppSpawnResponseMsg)), ret = -1;
393             break, "Failed to recv msg %{public}s", NWEBSPAWN_SERVER_NAME);
394         AppSpawnResponseMsg *respMsg = reinterpret_cast<AppSpawnResponseMsg *>(buffer2.data());
395         APPSPAWN_LOGV("NWeb_Spawn_Msg_005 Recv msg %{public}s result: %{public}d",
396             respMsg->msgHdr.processName, respMsg->result.result);
397         ret = respMsg->result.result;
398         (void)RecvMsg(socketId, buffer2.data(), buffer2.size());
399     } while (0);
400     if (socketId >= 0) {
401         CloseClientSocket(socketId);
402     }
403     ASSERT_EQ(ret, 0);
404 }
405 
406 /**
407  * @brief 测试连续2个消息,spawn和dump 消息
408  *
409  */
410 HWTEST_F(NWebSpawnServiceTest, NWeb_Spawn_Msg_006, TestSize.Level0)
411 {
412     int ret = -1;
413     int socketId = -1;
414     do {
415         socketId = testServer->CreateSocket(1);
416         APPSPAWN_CHECK(socketId >= 0, break, "Failed to create socket %{public}s", NWEBSPAWN_SERVER_NAME);
417 
418         std::vector<uint8_t> buffer1(2 * 1024);  // 2 * 1024
419         std::vector<uint8_t> buffer2(1024);  // 1024
420         uint32_t msgLen1 = 0;
421         uint32_t msgLen2 = 0;
422         ret = testServer->CreateSendMsg(buffer1, MSG_APP_SPAWN, msgLen1, {AppSpawnTestHelper::AddBaseTlv});
423         APPSPAWN_CHECK(ret == 0, break, "Failed to create msg %{public}s", testServer->GetDefaultTestAppBundleName());
424         ret = testServer->CreateSendMsg(buffer2, MSG_DUMP, msgLen2, {});
425         APPSPAWN_CHECK(ret == 0, break, "Failed to create msg %{public}s", testServer->GetDefaultTestAppBundleName());
426 
427         buffer1.insert(buffer1.begin() + msgLen1, buffer2.begin(), buffer2.end());
428         int len = write(socketId, buffer1.data(), msgLen1 + msgLen2);
429         APPSPAWN_CHECK(len > 0, break, "Failed to send msg %{public}s", testServer->GetDefaultTestAppBundleName());
430         // recv
431         len = RecvMsg(socketId, buffer2.data(), buffer2.size());
432         APPSPAWN_CHECK(len >= static_cast<int>(sizeof(AppSpawnResponseMsg)), ret = -1;
433             break, "Failed to recv msg %{public}s", NWEBSPAWN_SERVER_NAME);
434         AppSpawnResponseMsg *respMsg = reinterpret_cast<AppSpawnResponseMsg *>(buffer2.data());
435         APPSPAWN_LOGV("NWeb_Spawn_Msg_006 recv msg %{public}s result: %{public}d",
436             respMsg->msgHdr.processName, respMsg->result.result);
437         ret = respMsg->result.result;
438         (void)RecvMsg(socketId, buffer2.data(), buffer2.size());
439     } while (0);
440     if (socketId >= 0) {
441         CloseClientSocket(socketId);
442     }
443     ASSERT_EQ(ret, 0);
444 }
445 
446 /**
447  * @brief 测试连接中断
448  *
449  */
450 HWTEST_F(NWebSpawnServiceTest, NWeb_Spawn_Msg_007, TestSize.Level0)
451 {
452     int ret = -1;
453     int socketId = -1;
454     do {
455         socketId = testServer->CreateSocket(1);
456         APPSPAWN_CHECK(socketId >= 0, break, "Failed to create socket %{public}s", NWEBSPAWN_SERVER_NAME);
457         std::vector<uint8_t> buffer(1024, 0);  // 1024 1k
458         uint32_t msgLen = 0;
459         ret = testServer->CreateSendMsg(buffer, MSG_APP_SPAWN, msgLen, {AppSpawnTestHelper::AddBaseTlv});
460         APPSPAWN_CHECK(ret == 0, break, "Failed to create msg %{public}s", testServer->GetDefaultTestAppBundleName());
461 
462         int len = write(socketId, buffer.data(), msgLen);
463         APPSPAWN_CHECK(len > 0, break, "Failed to send msg %{public}s", testServer->GetDefaultTestAppBundleName());
464         // close socket
465         APPSPAWN_LOGV("CloseClientSocket");
466         CloseClientSocket(socketId);
467         socketId = -1;
468     } while (0);
469     if (socketId >= 0) {
470         CloseClientSocket(socketId);
471     }
472     ASSERT_EQ(ret, 0);
473 }
474 
475 /**
476  * @brief 发送不完整报文,等待超时
477  *
478  */
479 HWTEST_F(NWebSpawnServiceTest, NWeb_Spawn_Msg_008, TestSize.Level0)
480 {
481     int ret = 0;
482     int socketId = -1;
483     do {
484         socketId = testServer->CreateSocket(1);
485         APPSPAWN_CHECK(socketId >= 0, break, "Failed to create socket %{public}s", NWEBSPAWN_SERVER_NAME);
486         std::vector<uint8_t> buffer(1024, 0);  // 1024 1k
487         uint32_t msgLen = 0;
488         ret = testServer->CreateSendMsg(buffer, MSG_APP_SPAWN, msgLen, {AppSpawnTestHelper::AddBaseTlv});
489         APPSPAWN_CHECK(ret == 0, break, "Failed to create msg %{public}s", testServer->GetDefaultTestAppBundleName());
490         int len = write(socketId, buffer.data(), msgLen - 20);  // 20 test
491         APPSPAWN_CHECK(len > 0, break, "Failed to send msg %{public}s", testServer->GetDefaultTestAppBundleName());
492         // recv
493         len = read(socketId, buffer.data(), buffer.size());  // timeout EAGAIN
494         APPSPAWN_CHECK(len <= 0, ret = -1; break, "Can not receive timeout %{public}d", errno);
495     } while (0);
496     if (socketId >= 0) {
497         CloseClientSocket(socketId);
498     }
499     ASSERT_EQ(ret, 0);
500 }
501 }  // namespace OHOS
502