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 
16 #include "socket_session_test.h"
17 
18 #include "ipc_skeleton.h"
19 #include "message_parcel.h"
20 
21 #include "i_context.h"
22 #include "i_plugin.h"
23 #include "socket_client.h"
24 #include "socket_params.h"
25 #include "socket_session_manager.h"
26 #include "socket_server.h"
27 #include "tunnel_client.h"
28 
29 namespace OHOS {
30 namespace Msdp {
31 namespace DeviceStatus {
32 using namespace testing::ext;
33 namespace {
34 constexpr size_t BUF_CMD_SIZE { 512 };
35 std::shared_ptr<TunnelClient> g_tunnel {nullptr};
36 std::unique_ptr<SocketClient> g_client {nullptr};
37 std::shared_ptr<SocketConnection> g_socket { nullptr };
38 std::shared_ptr<SocketServer> g_socketServer { nullptr };
39 std::shared_ptr<SocketSession> g_session { nullptr };
40 std::shared_ptr<SocketSession> g_sessionOne { nullptr };
41 std::shared_ptr<SocketSessionManager> g_socketSessionManager { nullptr };
42 IContext *g_context { nullptr };
43 Intention g_intention { Intention::UNKNOWN_INTENTION };
44 constexpr int32_t TIME_WAIT_FOR_OP_MS { 20 };
45 } // namespace
46 
SetUpTestCase()47 void SocketSessionTest::SetUpTestCase() {}
48 
SetUp()49 void SocketSessionTest::SetUp()
50 {
51     g_tunnel = std::make_shared<TunnelClient>();
52     g_client = std::make_unique<SocketClient>(g_tunnel);
53     g_socketServer = std::make_unique<SocketServer>(g_context);
54     g_socketSessionManager = std::make_shared<SocketSessionManager>();
55     int32_t moduleType = 1;
56     int32_t tokenType = 1;
57     int32_t uid = IPCSkeleton::GetCallingUid();
58     int32_t pid = IPCSkeleton::GetCallingPid();
59     int32_t sockFds[2] { 0, -1 };
60     g_session = std::make_shared<SocketSession>("test", moduleType, tokenType, sockFds[0], uid, pid);
61     g_sessionOne = std::make_shared<SocketSession>("test1", moduleType, tokenType, sockFds[1], uid, pid);
62 }
TearDown()63 void SocketSessionTest::TearDown()
64 {
65     g_tunnel = nullptr;
66     g_client = nullptr;
67     g_socket = nullptr;
68     g_socketSessionManager = nullptr;
69     g_session = nullptr;
70     g_socketServer = nullptr;
71     g_sessionOne = nullptr;
72     std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP_MS));
73 }
74 
75 /**
76  * @tc.name: SocketSessionTest1
77  * @tc.desc: Drag Drawing
78  * @tc.type: FUNC
79  * @tc.require:
80  */
81 HWTEST_F(SocketSessionTest, SocketSessionTest1, TestSize.Level0)
82 {
83     CALL_TEST_DEBUG;
84     int32_t ret = g_client->Connect();
85     EXPECT_TRUE(ret);
86     ret = g_client->Connect();
87     EXPECT_TRUE(ret);
88     g_client->Stop();
89     g_client->OnDisconnected();
90 }
91 
92 /**
93  * @tc.name: SocketSessionTest2
94  * @tc.desc: Drag Drawing
95  * @tc.type: FUNC
96  * @tc.require:
97  */
98 HWTEST_F(SocketSessionTest, SocketSessionTest2, TestSize.Level0)
99 {
100     CALL_TEST_DEBUG;
101     int32_t ret = g_client->Socket();
102     MessageId msgId { MessageId::INVALID };
103     NetPacket pkt(msgId);
104     g_client->OnPacket(pkt);
105     EXPECT_GT(ret, -1);
106 }
107 
108 /**
109  * @tc.name: SocketSessionTest3
110  * @tc.desc: Drag Drawing
111  * @tc.type: FUNC
112  * @tc.require:
113  */
114 HWTEST_F(SocketSessionTest, SocketSessionTest3, TestSize.Level0)
115 {
116     CALL_TEST_DEBUG;
117     g_client->Reconnect();
118     int32_t ret = g_client->Connect();
119     EXPECT_TRUE(ret);
120     g_client->Reconnect();
121     g_client->OnDisconnected();
122 }
123 
124 /**
125  * @tc.name: SocketSessionTest4
126  * @tc.desc: Drag Drawing
127  * @tc.type: FUNC
128  * @tc.require:
129  */
130 HWTEST_F(SocketSessionTest, SocketSessionTest4, TestSize.Level0)
131 {
132     CALL_TEST_DEBUG;
133     MessageParcel data;
134     AllocSocketPairParam param;
135     bool ret = param.Unmarshalling(data);
136     EXPECT_FALSE(ret);
137 }
138 
139 /**
140  * @tc.name: SocketSessionTest5
141  * @tc.desc: Drag Drawing
142  * @tc.type: FUNC
143  * @tc.require:
144  */
145 HWTEST_F(SocketSessionTest, SocketSessionTest5, TestSize.Level0)
146 {
147     CALL_TEST_DEBUG;
148     MessageParcel data;
149     AllocSocketPairParam param;
150     bool ret = param.Marshalling(data);
151     EXPECT_TRUE(ret);
152 }
153 
154 /**
155  * @tc.name: SocketSessionTest6
156  * @tc.desc: Drag Drawing
157  * @tc.type: FUNC
158  * @tc.require:
159  */
160 HWTEST_F(SocketSessionTest, SocketSessionTest6, TestSize.Level0)
161 {
162     CALL_TEST_DEBUG;
163     MessageParcel data;
164     AllocSocketPairReply replyData(1, 1);
165     bool ret = replyData.Marshalling(data);
166     EXPECT_TRUE(ret);
167 }
168 
169 /**
170  * @tc.name: SocketSessionTest7
171  * @tc.desc: Drag Drawing
172  * @tc.type: FUNC
173  * @tc.require:
174  */
175 HWTEST_F(SocketSessionTest, SocketSessionTest7, TestSize.Level0)
176 {
177     CALL_TEST_DEBUG;
178     MessageParcel data;
179     AllocSocketPairReply replyData(1, 1);
180     bool ret = replyData.Unmarshalling(data);
181     EXPECT_FALSE(ret);
182 }
183 
184 /**
185  * @tc.name: SocketSessionTest8
186  * @tc.desc: Drag Drawing
187  * @tc.type: FUNC
188  * @tc.require:
189  */
190 HWTEST_F(SocketSessionTest, SocketSessionTest8, TestSize.Level0)
191 {
192     CALL_TEST_DEBUG;
193     CallingContext context {
194         .intention = g_intention,
195         .tokenId = IPCSkeleton::GetCallingTokenID(),
196         .uid = IPCSkeleton::GetCallingUid(),
197         .pid = IPCSkeleton::GetCallingPid(),
198     };
199     MessageParcel datas;
200     MessageParcel reply;
201     int32_t ret = g_socketServer->Enable(context, datas, reply);
202     EXPECT_EQ(ret, RET_ERR);
203 }
204 
205 /**
206  * @tc.name: SocketSessionTest9
207  * @tc.desc: Drag Drawing
208  * @tc.type: FUNC
209  * @tc.require:
210  */
211 HWTEST_F(SocketSessionTest, SocketSessionTest9, TestSize.Level0)
212 {
213     CALL_TEST_DEBUG;
214     CallingContext context {
215         .intention = g_intention,
216         .tokenId = IPCSkeleton::GetCallingTokenID(),
217         .uid = IPCSkeleton::GetCallingUid(),
218         .pid = IPCSkeleton::GetCallingPid(),
219     };
220     MessageParcel reply;
221     MessageParcel datas;
222     int32_t ret = g_socketServer->Disable(context, datas, reply);
223     EXPECT_EQ(ret, RET_ERR);
224 }
225 
226 /**
227  * @tc.name: SocketSessionTest10
228  * @tc.desc: Drag Drawing
229  * @tc.type: FUNC
230  * @tc.require:
231  */
232 HWTEST_F(SocketSessionTest, SocketSessionTest10, TestSize.Level0)
233 {
234     CALL_TEST_DEBUG;
235     CallingContext context {
236         .intention = g_intention,
237         .tokenId = IPCSkeleton::GetCallingTokenID(),
238         .uid = IPCSkeleton::GetCallingUid(),
239         .pid = IPCSkeleton::GetCallingPid(),
240     };
241     MessageParcel reply;
242     MessageParcel datas;
243     int32_t ret = g_socketServer->Start(context, datas, reply);
244     EXPECT_EQ(ret, RET_ERR);
245 }
246 
247 /**
248  * @tc.name: SocketSessionTest11
249  * @tc.desc: Drag Drawing
250  * @tc.type: FUNC
251  * @tc.require:
252  */
253 HWTEST_F(SocketSessionTest, SocketSessionTest11, TestSize.Level0)
254 {
255     CALL_TEST_DEBUG;
256 
257     CallingContext context {
258         .intention = g_intention,
259         .tokenId = IPCSkeleton::GetCallingTokenID(),
260         .uid = IPCSkeleton::GetCallingUid(),
261         .pid = IPCSkeleton::GetCallingPid(),
262     };
263     MessageParcel reply;
264     MessageParcel datas;
265     int32_t ret = g_socketServer->Stop(context, datas, reply);
266     EXPECT_EQ(ret, RET_ERR);
267 }
268 
269 /**
270  * @tc.name: SocketSessionTest12
271  * @tc.desc: Drag Drawing
272  * @tc.type: FUNC
273  * @tc.require:
274  */
275 HWTEST_F(SocketSessionTest, SocketSessionTest12, TestSize.Level0)
276 {
277     CALL_TEST_DEBUG;
278     CallingContext context {
279         .intention = g_intention,
280         .tokenId = IPCSkeleton::GetCallingTokenID(),
281         .uid = IPCSkeleton::GetCallingUid(),
282         .pid = IPCSkeleton::GetCallingPid(),
283     };
284     MessageParcel reply;
285     MessageParcel datas;
286     int32_t ret = g_socketServer->AddWatch(context, 1, datas, reply);
287     EXPECT_EQ(ret, RET_ERR);
288 }
289 
290 /**
291  * @tc.name: SocketSessionTest13
292  * @tc.desc: Drag Drawing
293  * @tc.type: FUNC
294  * @tc.require:
295  */
296 HWTEST_F(SocketSessionTest, SocketSessionTest13, TestSize.Level0)
297 {
298     CALL_TEST_DEBUG;
299     CallingContext context {
300         .intention = g_intention,
301         .tokenId = IPCSkeleton::GetCallingTokenID(),
302         .uid = IPCSkeleton::GetCallingUid(),
303         .pid = IPCSkeleton::GetCallingPid(),
304     };
305     MessageParcel reply;
306     MessageParcel datas;
307     int32_t ret = g_socketServer->RemoveWatch(context, 1, datas, reply);
308     EXPECT_EQ(ret, RET_ERR);
309 }
310 
311 /**
312  * @tc.name: SocketSessionTest14
313  * @tc.desc: Drag Drawing
314  * @tc.type: FUNC
315  * @tc.require:
316  */
317 HWTEST_F(SocketSessionTest, SocketSessionTest14, TestSize.Level0)
318 {
319     CALL_TEST_DEBUG;
320     CallingContext context {
321         .intention = g_intention,
322         .tokenId = IPCSkeleton::GetCallingTokenID(),
323         .uid = IPCSkeleton::GetCallingUid(),
324         .pid = IPCSkeleton::GetCallingPid(),
325     };
326     MessageParcel reply;
327     MessageParcel datas;
328     int32_t ret = g_socketServer->SetParam(context, 1, datas, reply);
329     EXPECT_EQ(ret, RET_ERR);
330 }
331 
332 /**
333  * @tc.name: SocketSessionTest15
334  * @tc.desc: Drag Drawing
335  * @tc.type: FUNC
336  * @tc.require:
337  */
338 HWTEST_F(SocketSessionTest, SocketSessionTest15, TestSize.Level0)
339 {
340     CALL_TEST_DEBUG;
341     CallingContext context {
342         .intention = g_intention,
343         .tokenId = IPCSkeleton::GetCallingTokenID(),
344         .uid = IPCSkeleton::GetCallingUid(),
345         .pid = IPCSkeleton::GetCallingPid(),
346     };
347     MessageParcel reply;
348     MessageParcel datas;
349     int32_t ret = g_socketServer->GetParam(context, 1, datas, reply);
350     EXPECT_EQ(ret, RET_ERR);
351 }
352 
353 /**
354  * @tc.name: SocketSessionTest16
355  * @tc.desc: Drag Drawing
356  * @tc.type: FUNC
357  * @tc.require:
358  */
359 HWTEST_F(SocketSessionTest, SocketSessionTest16, TestSize.Level0)
360 {
361     CALL_TEST_DEBUG;
362     CallingContext context {
363         .intention = g_intention,
364         .tokenId = IPCSkeleton::GetCallingTokenID(),
365         .uid = IPCSkeleton::GetCallingUid(),
366         .pid = IPCSkeleton::GetCallingPid(),
367     };
368     MessageParcel reply;
369     MessageParcel datas;
370     int32_t ret = g_socketServer->Control(context, 1, datas, reply);
371     EXPECT_EQ(ret, RET_ERR);
372 }
373 
374 /**
375  * @tc.name: SocketSessionTest17
376  * @tc.desc: Drag Drawing
377  * @tc.type: FUNC
378  * @tc.require:
379  */
380 HWTEST_F(SocketSessionTest, SocketSessionTest17, TestSize.Level0)
381 {
382     CALL_TEST_DEBUG;
383     CallingContext context {
384         .intention = g_intention,
385         .tokenId = IPCSkeleton::GetCallingTokenID(),
386         .uid = IPCSkeleton::GetCallingUid(),
387         .pid = IPCSkeleton::GetCallingPid(),
388     };
389     MessageParcel reply;
390     MessageParcel datas;
391     int32_t ret = g_socketServer->Control(context, -1, datas, reply);
392     EXPECT_EQ(ret, RET_ERR);
393 }
394 
395 /**
396  * @tc.name: SocketSessionTest18
397  * @tc.desc: Drag Drawing
398  * @tc.type: FUNC
399  * @tc.require:
400  */
401 HWTEST_F(SocketSessionTest, SocketSessionTest18, TestSize.Level0)
402 {
403     CALL_TEST_DEBUG;
404     g_socketSessionManager->GetAppMgr();
405     int32_t ret = g_socketSessionManager->Init();
406     EXPECT_EQ(ret, RET_OK);
407 }
408 
409 /**
410  * @tc.name: SocketSessionTest19
411  * @tc.desc: Drag Drawing
412  * @tc.type: FUNC
413  * @tc.require:
414  */
415 HWTEST_F(SocketSessionTest, SocketSessionTest19, TestSize.Level0)
416 {
417     CALL_TEST_DEBUG;
418     int32_t clientFd { -1 };
419     int32_t ret = g_socketSessionManager->AllocSocketFd("", 1, 1, 1, 1, clientFd);
420     EXPECT_EQ(ret, RET_ERR);
421 }
422 
423 /**
424  * @tc.name: SocketSessionTest20
425  * @tc.desc: Drag Drawing
426  * @tc.type: FUNC
427  * @tc.require:
428  */
429 HWTEST_F(SocketSessionTest, SocketSessionTest20, TestSize.Level0)
430 {
431     CALL_TEST_DEBUG;
432     int32_t pid { 1 };
433     g_socketSessionManager->FindSessionByPid(pid);
434     g_socketSessionManager->DispatchOne();
435     int32_t fd { 1 };
436     g_socketSessionManager->ReleaseSession(fd);
437     g_socketSessionManager->FindSession(fd);
438     g_socketSessionManager->DumpSession("");
439     g_socketSessionManager->RemoveSessionDeletedCallback(pid);
440     int32_t sockFd { -1 };
441     int32_t bufSize { -1 };
442     int32_t ret = g_socketSessionManager->SetBufferSize(sockFd, bufSize);
443     EXPECT_EQ(ret, RET_OK);
444 }
445 
446 /**
447  * @tc.name: SocketSessionTest21
448  * @tc.desc: Drag Drawing
449  * @tc.type: FUNC
450  * @tc.require:
451  */
452 HWTEST_F(SocketSessionTest, SocketSessionTest21, TestSize.Level0)
453 {
454     CALL_TEST_DEBUG;
455     int32_t fd { 1 };
456     bool ret = g_socketSessionManager->AddSession(g_session);
457     g_socketSessionManager->NotifySessionDeleted(g_session);
458     EXPECT_TRUE(ret);
459     g_socketSessionManager->ReleaseSession(fd);
460 }
461 
462 /**
463  * @tc.name: SocketSessionTest22
464  * @tc.desc: Drag Drawing
465  * @tc.type: FUNC
466  * @tc.require:
467  */
468 HWTEST_F(SocketSessionTest, SocketSessionTest22, TestSize.Level0)
469 {
470     CALL_TEST_DEBUG;
471     MessageId msgId { MessageId::INVALID };
472     NetPacket pkt(msgId);
473     bool ret = g_session->SendMsg(pkt);
474     EXPECT_FALSE(ret);
475 }
476 
477 /**
478  * @tc.name: SocketSessionTest23
479  * @tc.desc: Drag Drawing
480  * @tc.type: FUNC
481  * @tc.require:
482  */
483 HWTEST_F(SocketSessionTest, SocketSessionTest23, TestSize.Level0)
484 {
485     CALL_TEST_DEBUG;
486     char buf[BUF_CMD_SIZE] = { 0 };
487     size_t size = 1;
488     ASSERT_NO_FATAL_FAILURE(g_session->SendMsg(buf, size));
489 }
490 
491 /**
492  * @tc.name: SocketSessionTest24
493  * @tc.desc: Drag Drawing
494  * @tc.type: FUNC
495  * @tc.require:
496  */
497 HWTEST_F(SocketSessionTest, SocketSessionTest24, TestSize.Level0)
498 {
499     CALL_TEST_DEBUG;
500     char buf[BUF_CMD_SIZE] = { 0 };
501     size_t size = 1;
502     struct epoll_event ev {};
503     ev.events = 0;
504     ev.events |= EPOLLIN;
505     g_session->Dispatch(ev);
506     g_socketSessionManager->Dispatch(ev);
507 
508     ev.events = 0;
509     ev.events |= EPOLLHUP;
510     g_session->Dispatch(ev);
511     g_socketSessionManager->Dispatch(ev);
512     bool ret = g_session->SendMsg(buf, size);
513     g_session->ToString();
514     EXPECT_FALSE(ret);
515 }
516 
517 /**
518  * @tc.name: SocketSessionTest25
519  * @tc.desc: Drag Drawing
520  * @tc.type: FUNC
521  * @tc.require:
522  */
523 HWTEST_F(SocketSessionTest, SocketSessionTest25, TestSize.Level0)
524 {
525     CALL_TEST_DEBUG;
526     g_socketSessionManager->AddSessionDeletedCallback(1, nullptr);
__anon05cb38b30202(SocketSessionPtr ptr)527     g_socketSessionManager->AddSessionDeletedCallback(1, [](SocketSessionPtr ptr){});
528     bool ret = g_socketSessionManager->AddSession(nullptr);
529     EXPECT_FALSE(ret);
530 }
531 
532 /**
533  * @tc.name: SocketSessionTest26
534  * @tc.desc: Drag Drawing
535  * @tc.type: FUNC
536  * @tc.require:
537  */
538 HWTEST_F(SocketSessionTest, SocketSessionTest26, TestSize.Level0)
539 {
540     CALL_TEST_DEBUG;
541     int32_t fd = 12;
542     int32_t ret = g_client->Connect();
543     EXPECT_TRUE(ret);
544     g_client->socket_->OnReadable(fd);
545     fd = 1;
546     g_client->socket_->OnReadable(fd);
547     g_client->socket_->OnException(fd);
548     g_client->OnDisconnected();
549 }
550 
551 /**
552  * @tc.name: SocketSessionTest27
553  * @tc.desc: Drag Drawing
554  * @tc.type: FUNC
555  * @tc.require:
556  */
557 HWTEST_F(SocketSessionTest, SocketSessionTest27, TestSize.Level0)
558 {
559     CALL_TEST_DEBUG;
560     char buf[BUF_CMD_SIZE] = { 0 };
561     size_t size = 0;
562     bool ret = g_session->SendMsg(buf, size);
563     EXPECT_FALSE(ret);
564 }
565 
566 /**
567  * @tc.name: SocketSessionTest28
568  * @tc.desc: Drag Drawing
569  * @tc.type: FUNC
570  * @tc.require:
571  */
572 HWTEST_F(SocketSessionTest, SocketSessionTest28, TestSize.Level0)
573 {
574     CALL_TEST_DEBUG;
575     int32_t fd = g_session->GetFd();
576     bool ret = g_socketSessionManager->AddSession(g_session);
577     EXPECT_TRUE(ret);
578     g_socketSessionManager->ReleaseSession(fd);
579 }
580 
581 /**
582  * @tc.name: SocketSessionTest29
583  * @tc.desc: Drag Drawing
584  * @tc.type: FUNC
585  * @tc.require:
586  */
587 HWTEST_F(SocketSessionTest, SocketSessionTest29, TestSize.Level0)
588 {
589     CALL_TEST_DEBUG;
__anon05cb38b30302(SocketSessionPtr ptr)590     g_socketSessionManager->callbacks_.emplace(g_session->GetPid(), [](SocketSessionPtr ptr){});
591     g_socketSessionManager->NotifySessionDeleted(g_session);
592     int32_t ARG_101 = 101;
593     for (size_t i = 0; i < ARG_101; i++) {
594         g_socketSessionManager->sessions_.emplace(i, nullptr);
595     }
596     bool ret = g_socketSessionManager->AddSession(g_session);
597     EXPECT_FALSE(ret);
598     g_socketSessionManager->sessions_.clear();
599     g_socketSessionManager->callbacks_.clear();
600 }
601 
602 /**
603  * @tc.name: SocketSessionTest30
604  * @tc.desc: Drag Drawing
605  * @tc.type: FUNC
606  * @tc.require:
607  */
608 HWTEST_F(SocketSessionTest, SocketSessionTest30, TestSize.Level0)
609 {
610     CALL_TEST_DEBUG;
611     char buf[BUF_CMD_SIZE] = { 0 };
612     size_t size = 1;
613     bool ret = g_sessionOne->SendMsg(buf, size);
614     EXPECT_FALSE(ret);
615 }
616 
617 /**
618  * @tc.name: SocketSessionTest31
619  * @tc.desc: Drag Drawing
620  * @tc.type: FUNC
621  * @tc.require:
622  */
623 HWTEST_F(SocketSessionTest, SocketSessionTest31, TestSize.Level0)
624 {
625     CALL_TEST_DEBUG;
626     ASSERT_NO_FATAL_FAILURE(g_socketSessionManager->RegisterApplicationState());
627 }
628 
629 /**
630  * @tc.name: SocketSessionTest32
631  * @tc.desc: Drag Drawing
632  * @tc.type: FUNC
633  * @tc.require:
634  */
635 HWTEST_F(SocketSessionTest, SocketSessionTest32, TestSize.Level0)
636 {
637     CALL_TEST_DEBUG;
638     int32_t pid = IPCSkeleton::GetCallingPid();
639     ASSERT_NO_FATAL_FAILURE(g_socketSessionManager->ReleaseSessionByPid(pid));
640 }
641 } // namespace DeviceStatus
642 } // namespace Msdp
643 } // namespace OHOS
644