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