• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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 <gtest/gtest.h>
17 
18 #include "mmi_log.h"
19 #include "proto.h"
20 #include "uds_server.h"
21 
22 #undef MMI_LOG_TAG
23 #define MMI_LOG_TAG "UDSServerTest"
24 namespace OHOS {
25 namespace MMI {
26 namespace {
27 using namespace testing::ext;
28 const std::string PROGRAM_NAME = "uds_server_test";
29 constexpr int32_t MODULE_TYPE = 1;
30 constexpr int32_t UDS_FD = -1;
31 constexpr int32_t UDS_UID = 100;
32 constexpr int32_t UDS_PID = 100;
33 } // namespace
34 
35 class UDSServerTest : public testing::Test {
36 public:
SetUpTestCase(void)37     static void SetUpTestCase(void) {}
TearDownTestCase(void)38     static void TearDownTestCase(void) {}
39 };
40 
41 class UDSServerUnitTest : public UDSServer {
42 public:
SetFd(int32_t fd)43     void SetFd(int32_t fd)
44     {
45         fd_ = fd;
46     }
47 };
48 
49 /**
50  * @tc.name: SendMsg_001
51  * @tc.desc: Test the function SendMsg_001
52  * @tc.type: FUNC
53  * @tc.require:
54  */
55 HWTEST_F(UDSServerTest, SendMsg_001, TestSize.Level1)
56 {
57     CALL_TEST_DEBUG;
58     MmiMessageId msgId = MmiMessageId::INVALID;
59     NetPacket pkt(msgId);
60     int32_t fd = 1000;
61     UDSServer serObj;
62     bool retResult = serObj.SendMsg(fd, pkt);
63     EXPECT_FALSE(retResult);
64 }
65 
66 /**
67  * @tc.name: SendMsg_002
68  * @tc.desc: Test the function SendMsg_002
69  * @tc.type: FUNC
70  * @tc.require:
71  */
72 HWTEST_F(UDSServerTest, SendMsg_002, TestSize.Level1)
73 {
74     CALL_TEST_DEBUG;
75     MmiMessageId msgId = MmiMessageId::INVALID;
76     NetPacket pkt(msgId);
77 
78     int32_t fd = -1001;
79     UDSServer serObj;
80     bool retResult = serObj.SendMsg(fd, pkt);
81     ASSERT_FALSE(retResult);
82 }
83 
84 /**
85  * @tc.name: SendMsg_003
86  * @tc.desc: Test the function SendMsg_003
87  * @tc.type: FUNC
88  * @tc.require:
89  */
90 HWTEST_F(UDSServerTest, SendMsg_003, TestSize.Level1)
91 {
92     CALL_TEST_DEBUG;
93     MmiMessageId msgId = MmiMessageId::INVALID;
94     NetPacket pkt(msgId);
95 
96     int32_t fd = 3333;
97     UDSServer serObj;
98     bool retResult = serObj.SendMsg(fd, pkt);
99     ASSERT_FALSE(retResult);
100 }
101 
102 /**
103  * @tc.name: Multicast
104  * @tc.desc: Test the function Multicast
105  * @tc.type: FUNC
106  * @tc.require:
107  */
108 HWTEST_F(UDSServerTest, Multicast, TestSize.Level1)
109 {
110     CALL_TEST_DEBUG;
111     MmiMessageId msgId = MmiMessageId::INVALID;
112     NetPacket pkt(msgId);
113     std::vector<int32_t> fds;
114     ASSERT_NO_FATAL_FAILURE(fds.push_back(1));
115 
116     UDSServer serObj;
117     serObj.Multicast(fds, pkt);
118 }
119 
120 /**
121  * @tc.name: Stop_001
122  * @tc.desc: Test the function Stop_001
123  * @tc.type: FUNC
124  * @tc.require:
125  */
126 HWTEST_F(UDSServerTest, Stop_001, TestSize.Level1)
127 {
128     CALL_TEST_DEBUG;
129     UDSServer serObj;
130     ASSERT_NO_FATAL_FAILURE(serObj.UdsStop());
131 }
132 
133 /**
134  * @tc.name: GetSession_001
135  * @tc.desc: Test the function GetSession_001
136  * @tc.type: FUNC
137  * @tc.require:
138  */
139 HWTEST_F(UDSServerTest, GetSession_001, TestSize.Level1)
140 {
141     CALL_TEST_DEBUG;
142     UDSServer UDS_server;
143     int32_t fd = 0;
144     auto retResult = UDS_server.GetSession(fd);
145     EXPECT_TRUE(retResult == nullptr);
146 }
147 
148 /**
149  * @tc.name: GetSession_002
150  * @tc.desc: Test the function GetSession_002
151  * @tc.type: FUNC
152  * @tc.require:
153  */
154 HWTEST_F(UDSServerTest, GetSession_002, TestSize.Level1)
155 {
156     CALL_TEST_DEBUG;
157     UDSServer UDS_server;
158     int32_t fd = 1000000;
159     auto retResult = UDS_server.GetSession(fd);
160     EXPECT_TRUE(retResult == nullptr);
161 }
162 
163 /**
164  * @tc.name: GetSession_003
165  * @tc.desc: Test the function GetSession_003
166  * @tc.type: FUNC
167  * @tc.require:
168  */
169 HWTEST_F(UDSServerTest, GetSession_003, TestSize.Level1)
170 {
171     CALL_TEST_DEBUG;
172     UDSServer UDS_server;
173     int32_t fd = -1;
174     auto retResult = UDS_server.GetSession(fd);
175     EXPECT_TRUE(retResult == nullptr);
176 }
177 
178 /**
179  * @tc.name: UdsStop_001
180  * @tc.desc: Test the function UdsStop
181  * @tc.type: FUNC
182  * @tc.require:
183  */
184 HWTEST_F(UDSServerTest, UdsStop_001, TestSize.Level1)
185 {
186     CALL_TEST_DEBUG;
187     UDSServer udsServer;
188     const std::string programName = "program";
189     const int32_t moduleType = 1;
190     const int32_t uid = 2;
191     const int32_t pid = 10;
192     int32_t serverFd = 1;
193     int32_t toReturnClientFd = 1;
194     int32_t tokenType = 1;
195 
196     udsServer.AddSocketPairInfo(programName, moduleType, uid, pid, serverFd, toReturnClientFd, tokenType);
197     udsServer.UdsStop();
198 }
199 
200 /**
201  * @tc.name: GetClientPid_001
202  * @tc.desc: Test the function GetClientPid
203  * @tc.type: FUNC
204  * @tc.require:
205  */
206 HWTEST_F(UDSServerTest, GetClientPid_001, TestSize.Level1)
207 {
208     CALL_TEST_DEBUG;
209     UDSServer udsServer;
210     int32_t fd = 1;
211     int32_t pid1 = 0;
212     const std::string programName = "program";
213     const int32_t moduleType = 1;
214     const int32_t uid = 2;
215     const int32_t pid = 10;
216     int32_t serverFd = 1;
217     int32_t toReturnClientFd = 1;
218     int32_t tokenType = 1;
219 
220     udsServer.AddSocketPairInfo(programName, moduleType, uid, pid, serverFd, toReturnClientFd, tokenType);
221     pid1 = udsServer.GetClientPid(fd);
222     EXPECT_EQ(pid1, INVALID_PID);
223 }
224 
225 /**
226  * @tc.name: AddSocketPairInfo_001
227  * @tc.desc: Test the function AddSocketPairInfo
228  * @tc.type: FUNC
229  * @tc.require:
230  */
231 HWTEST_F(UDSServerTest, AddSocketPairInfo_001, TestSize.Level1)
232 {
233     CALL_TEST_DEBUG;
234     UDSServer udsServer;
235     const std::string programName = "program";
236     const int32_t moduleType = 1;
237     const int32_t uid = 2;
238     const int32_t pid = 10;
239     int32_t serverFd = 1;
240     int32_t toReturnClientFd = 1;
241     int32_t tokenType = 1;
242     int32_t ret = 0;
243 
244     ret = udsServer.AddSocketPairInfo(programName, moduleType, uid, pid, serverFd, toReturnClientFd, tokenType);
245     EXPECT_EQ(ret, RET_ERR);
246 }
247 
248 /**
249  * @tc.name: SetFdProperty_001
250  * @tc.desc: Test the function SetFdProperty
251  * @tc.type: FUNC
252  * @tc.require:
253  */
254 HWTEST_F(UDSServerTest, SetFdProperty_001, TestSize.Level1)
255 {
256     CALL_TEST_DEBUG;
257     UDSServer udsServer;
258     int32_t ret = RET_ERR;
259     int32_t tokenType = TokenType::TOKEN_NATIVE;
260     int32_t serverFd = 1;
261     const std::string programName = "program";
262     const int32_t moduleType = 1;
263     const int32_t uid = 2;
264     const int32_t pid = 10;
265     int32_t toReturnClientFd = 1;
266     bool readOnly = false;
267 
268     udsServer.AddSocketPairInfo(programName, moduleType, uid, pid, serverFd, toReturnClientFd, tokenType);
269     ret = udsServer.SetFdProperty(tokenType, serverFd, toReturnClientFd, programName, readOnly);
270     EXPECT_EQ(ret, RET_ERR);
271 }
272 
273 /**
274  * @tc.name: SetFdProperty_002
275  * @tc.desc: Test the function SetFdProperty
276  * @tc.type: FUNC
277  * @tc.require:
278  */
279 HWTEST_F(UDSServerTest, SetFdProperty_002, TestSize.Level1)
280 {
281     CALL_TEST_DEBUG;
282     UDSServer udsServer;
283     int32_t ret = RET_ERR;
284     int32_t tokenType = TokenType::TOKEN_SHELL;
285     int32_t serverFd = 1;
286     const std::string programName = "program";
287     const int32_t moduleType = 1;
288     const int32_t uid = 2;
289     const int32_t pid = 10;
290     int32_t toReturnClientFd = 1;
291     bool readOnly = false;
292 
293     udsServer.AddSocketPairInfo(programName, moduleType, uid, pid, serverFd, toReturnClientFd, tokenType);
294     ret = udsServer.SetFdProperty(tokenType, serverFd, toReturnClientFd, programName, readOnly);
295     EXPECT_EQ(ret, RET_ERR);
296 }
297 
298 /**
299  * @tc.name: Dump_001
300  * @tc.desc: Test the function Dump
301  * @tc.type: FUNC
302  * @tc.require:
303  */
304 HWTEST_F(UDSServerTest, Dump_001, TestSize.Level1)
305 {
306     CALL_TEST_DEBUG;
307     UDSServer udsServer;
308     int32_t fd = 1;
309     const std::vector<std::string> args = {"help"};
310     int32_t tokenType = TokenType::TOKEN_SHELL;
311     int32_t serverFd = 1;
312     const std::string programName = "program";
313     const int32_t moduleType = 1;
314     const int32_t uid = 2;
315     const int32_t pid = 10;
316     int32_t toReturnClientFd = 1;
317 
318     udsServer.AddSocketPairInfo(programName, moduleType, uid, pid, serverFd, toReturnClientFd, tokenType);
319     udsServer.Dump(fd, args);
320 }
321 
322 /**
323  * @tc.name: OnConnected_001
324  * @tc.desc: Test the function OnConnected
325  * @tc.type: FUNC
326  * @tc.require:
327  */
328 HWTEST_F(UDSServerTest, OnConnected_001, TestSize.Level1)
329 {
330     CALL_TEST_DEBUG;
331     UDSServer udsServer;
332     SessionPtr sess;
333     int32_t tokenType = TokenType::TOKEN_SHELL;
334     int32_t serverFd = 1;
335     const std::string programName = "program";
336     const int32_t moduleType = 1;
337     const int32_t uid = 2;
338     const int32_t pid = 10;
339     int32_t toReturnClientFd = 1;
340 
341     udsServer.AddSocketPairInfo(programName, moduleType, uid, pid, serverFd, toReturnClientFd, tokenType);
342     udsServer.OnConnected(sess);
343 }
344 
345 /**
346  * @tc.name: SetRecvFun_001
347  * @tc.desc: Test the function SetRecvFun
348  * @tc.type: FUNC
349  * @tc.require:
350  */
351 HWTEST_F(UDSServerTest, SetRecvFun_001, TestSize.Level1)
352 {
353     CALL_TEST_DEBUG;
354     UDSServer udsServer;
355     MsgServerFunCallback fun{ nullptr };
356     int32_t tokenType = TokenType::TOKEN_SHELL;
357     int32_t serverFd = 1;
358     const std::string programName = "program";
359     const int32_t moduleType = 1;
360     const int32_t uid = 2;
361     const int32_t pid = 10;
362     int32_t toReturnClientFd = 1;
363 
364     udsServer.AddSocketPairInfo(programName, moduleType, uid, pid, serverFd, toReturnClientFd, tokenType);
365     udsServer.SetRecvFun(fun);
366 }
367 
368 /**
369  * @tc.name: OnEpollRecv_001
370  * @tc.desc: Test the function OnEpollRecv
371  * @tc.type: FUNC
372  * @tc.require:
373  */
374 HWTEST_F(UDSServerTest, OnEpollRecv_001, TestSize.Level1)
375 {
376     CALL_TEST_DEBUG;
377     UDSServer udsServer;
378     int32_t size = 100;
379     epoll_event ev;
380     int32_t tokenType = TokenType::TOKEN_SHELL;
381     int32_t serverFd = 1;
382     const std::string programName = "program";
383     const int32_t moduleType = 1;
384     const int32_t uid = 2;
385     const int32_t pid = 10;
386     int32_t toReturnClientFd = 1;
387 
388     udsServer.AddSocketPairInfo(programName, moduleType, uid, pid, serverFd, toReturnClientFd, tokenType);
389     int32_t fd = epoll_create(size);
390     udsServer.OnEpollRecv(fd, ev);
391 }
392 
393 /**
394  * @tc.name: OnEpollRecv_002
395  * @tc.desc: Test the function OnEpollRecv
396  * @tc.type: FUNC
397  * @tc.require:
398  */
399 HWTEST_F(UDSServerTest, OnEpollRecv_002, TestSize.Level1)
400 {
401     CALL_TEST_DEBUG;
402     UDSServer udsServer;
403     epoll_event ev;
404     int32_t fd = -1;
405     int32_t tokenType = TokenType::TOKEN_SHELL;
406     int32_t serverFd = 1;
407     const std::string programName = "program";
408     const int32_t moduleType = 1;
409     const int32_t uid = 2;
410     const int32_t pid = 10;
411     int32_t toReturnClientFd = 1;
412 
413     udsServer.AddSocketPairInfo(programName, moduleType, uid, pid, serverFd, toReturnClientFd, tokenType);
414     udsServer.OnEpollRecv(fd, ev);
415 }
416 
417 /**
418  * @tc.name: AddEpollEvent_001
419  * @tc.desc: Test the function AddEpollEvent
420  * @tc.type: FUNC
421  * @tc.require:
422  */
423 HWTEST_F(UDSServerTest, AddEpollEvent_001, TestSize.Level1)
424 {
425     CALL_TEST_DEBUG;
426     UDSServer udsServer;
427     std::shared_ptr<mmi_epoll_event> epollEvent=std::make_shared<mmi_epoll_event>();
428     int32_t fd = 1;
429     int32_t tokenType = TokenType::TOKEN_SHELL;
430     int32_t serverFd = 1;
431     const std::string programName = "program";
432     const int32_t moduleType = 1;
433     const int32_t uid = 2;
434     const int32_t pid = 10;
435     int32_t toReturnClientFd = 1;
436 
437     udsServer.AddSocketPairInfo(programName, moduleType, uid, pid, serverFd, toReturnClientFd, tokenType);
438     udsServer.AddEpollEvent(fd, epollEvent);
439 }
440 
441 /**
442  * @tc.name: DumpSession_001
443  * @tc.desc: Test the function DumpSession
444  * @tc.type: FUNC
445  * @tc.require:
446  */
447 HWTEST_F(UDSServerTest, DumpSession_001, TestSize.Level1)
448 {
449     CALL_TEST_DEBUG;
450     UDSServer udsServer;
451     const std::string title = "test_title";
452     int32_t tokenType = TokenType::TOKEN_SHELL;
453     int32_t serverFd = 1;
454     const std::string programName = "program";
455     const int32_t moduleType = 1;
456     const int32_t uid = 2;
457     const int32_t pid = 10;
458     int32_t toReturnClientFd = 1;
459 
460     udsServer.AddSocketPairInfo(programName, moduleType, uid, pid, serverFd, toReturnClientFd, tokenType);
461     udsServer.DumpSession(title);
462 }
463 
464 /**
465  * @tc.name: AddSession_001
466  * @tc.desc: Test the function AddSession
467  * @tc.type: FUNC
468  * @tc.require:
469  */
470 HWTEST_F(UDSServerTest, AddSession_001, TestSize.Level1)
471 {
472     CALL_TEST_DEBUG;
473     UDSServer udsServer;
474     SessionPtr sess = nullptr;
475     int32_t tokenType = TokenType::TOKEN_SHELL;
476     int32_t serverFd = 1;
477     const std::string programName = "program";
478     const int32_t moduleType = 1;
479     const int32_t uid = 2;
480     const int32_t pid = 10;
481     int32_t toReturnClientFd = 1;
482 
483     udsServer.AddSocketPairInfo(programName, moduleType, uid, pid, serverFd, toReturnClientFd, tokenType);
484     sess = std::make_shared<UDSSession>(programName, moduleType, serverFd, uid, pid);
485     udsServer.AddSession(sess);
486 }
487 
488 /**
489  * @tc.name: DelSession_001
490  * @tc.desc: Test the function DelSession
491  * @tc.type: FUNC
492  * @tc.require:
493  */
494 HWTEST_F(UDSServerTest, DelSession_001, TestSize.Level1)
495 {
496     CALL_TEST_DEBUG;
497     UDSServer udsServer;
498     int32_t fd = -1;
499     int32_t tokenType = TokenType::TOKEN_SHELL;
500     int32_t serverFd = 1;
501     const std::string programName = "program";
502     const int32_t moduleType = 1;
503     const int32_t uid = 2;
504     const int32_t pid = 10;
505     int32_t toReturnClientFd = 1;
506 
507     udsServer.AddSocketPairInfo(programName, moduleType, uid, pid, serverFd, toReturnClientFd, tokenType);
508     udsServer.DelSession(fd);
509 }
510 
511 /**
512  * @tc.name: DelSession_002
513  * @tc.desc: Test the function DelSession
514  * @tc.type: FUNC
515  * @tc.require:
516  */
517 HWTEST_F(UDSServerTest, DelSession_002, TestSize.Level1)
518 {
519     CALL_TEST_DEBUG;
520     UDSServer udsServer;
521     int32_t fd = 1;
522     int32_t tokenType = TokenType::TOKEN_SHELL;
523     int32_t serverFd = 1;
524     const std::string programName = "program";
525     const int32_t moduleType = 1;
526     const int32_t uid = 2;
527     const int32_t pid = 10;
528     int32_t toReturnClientFd = 1;
529 
530     udsServer.AddSocketPairInfo(programName, moduleType, uid, pid, serverFd, toReturnClientFd, tokenType);
531     udsServer.DelSession(fd);
532 }
533 
534 /**
535  * @tc.name: NotifySessionDeleted_001
536  * @tc.desc: Test the function NotifySessionDeleted
537  * @tc.type: FUNC
538  * @tc.require:
539  */
540 HWTEST_F(UDSServerTest, NotifySessionDeleted_001, TestSize.Level1)
541 {
542     CALL_TEST_DEBUG;
543     UDSServer udsServer;
544     SessionPtr sess = nullptr;
545     int32_t tokenType = TokenType::TOKEN_SHELL;
546     int32_t serverFd = 1;
547     const std::string programName = "program";
548     const int32_t moduleType = 1;
549     const int32_t uid = 2;
550     const int32_t pid = 10;
551     int32_t toReturnClientFd = 1;
552 
553     udsServer.AddSocketPairInfo(programName, moduleType, uid, pid, serverFd, toReturnClientFd, tokenType);
554     sess = std::make_shared<UDSSession>(programName, moduleType, serverFd, uid, pid);
555     udsServer.NotifySessionDeleted(sess);
556 }
557 
558 /**
559  * @tc.name: GetClientPid_002
560  * @tc.desc: Test the scenario of obtaining the client process ID
561  * @tc.type: FUNC
562  * @tc.require:
563  */
564 HWTEST_F(UDSServerTest, GetClientPid_002, TestSize.Level1)
565 {
566     CALL_TEST_DEBUG;
567     UDSServer udsServer;
568     int32_t fd = 123;
569     auto ret = udsServer.GetClientPid(fd);
570     EXPECT_EQ(ret, RET_ERR);
571 }
572 
573 /**
574  * @tc.name: AddSocketPairInfo_002
575  * @tc.desc: Test the scenario of adding socket pair information
576  * @tc.type: FUNC
577  * @tc.require:
578  */
579 HWTEST_F(UDSServerTest, AddSocketPairInfo_002, TestSize.Level1)
580 {
581     CALL_TEST_DEBUG;
582     UDSServer udsServer;
583     std::string programName = "program";
584     int32_t moduleType = 1;
585     int32_t uid = 2;
586     int32_t pid = 10;
587     int32_t serverFd = 123;
588     int32_t toReturnClientFd = 456;
589     int32_t tokenType = 1;
590     auto ret = udsServer.AddSocketPairInfo(programName, moduleType, uid, pid, serverFd, toReturnClientFd, tokenType);
591     if (serverFd != -1) {
592         close(serverFd);
593     }
594     if (toReturnClientFd != -1) {
595         close(toReturnClientFd);
596     }
597     EXPECT_EQ(ret, RET_ERR);
598 }
599 
600 /**
601  * @tc.name: Dump_002
602  * @tc.desc: Test the Dump functionality of UDSServer
603  * @tc.type: FUNC
604  * @tc.require:
605  */
606 HWTEST_F(UDSServerTest, Dump_002, TestSize.Level1)
607 {
608     CALL_TEST_DEBUG;
609     UDSServer udsServer;
610     int32_t fd = 1;
611     std::vector<std::string> args = {"help"};
612     ASSERT_NO_FATAL_FAILURE(udsServer.Dump(fd, args));
613 }
614 
615 /**
616  * @tc.name: OnConnected_002
617  * @tc.desc: Test the OnConnected function of UDSServer
618  * @tc.type: FUNC
619  * @tc.require:
620  */
621 HWTEST_F(UDSServerTest, OnConnected_002, TestSize.Level1)
622 {
623     CALL_TEST_DEBUG;
624     UDSServer udsServer;
625     SessionPtr sess = nullptr;
626     ASSERT_NO_FATAL_FAILURE(udsServer.OnConnected(sess));
627 }
628 
629 /**
630  * @tc.name: OnDisconnected_001
631  * @tc.desc: Test the OnDisconnected function of UDSServer
632  * @tc.type: FUNC
633  * @tc.require:
634  */
635 HWTEST_F(UDSServerTest, OnDisconnected_001, TestSize.Level1)
636 {
637     CALL_TEST_DEBUG;
638     UDSServer udsServer;
639     SessionPtr sess = nullptr;
640     ASSERT_NO_FATAL_FAILURE(udsServer.OnConnected(sess));
641     ASSERT_NO_FATAL_FAILURE(udsServer.OnDisconnected(sess));
642 }
643 
644 /**
645  * @tc.name: AddEpoll_001
646  * @tc.desc: Test the AddEpoll function of UDSServer
647  * @tc.type: FUNC
648  * @tc.require:
649  */
650 HWTEST_F(UDSServerTest, AddEpoll_001, TestSize.Level1)
651 {
652     CALL_TEST_DEBUG;
653     UDSServer udsServer;
654     EpollEventType type = EPOLL_EVENT_BEGIN;
655     int32_t fd = 1;
656     auto ret = udsServer.AddEpoll(type, fd);
657     EXPECT_EQ(ret, RET_ERR);
658 }
659 
660 /**
661  * @tc.name: SetRecvFun_002
662  * @tc.desc: Test the SetRecvFun function of UDSServer
663  * @tc.type: FUNC
664  * @tc.require:
665  */
666 HWTEST_F(UDSServerTest, SetRecvFun_002, TestSize.Level1)
667 {
668     CALL_TEST_DEBUG;
669     UDSServer udsServer;
670     MsgServerFunCallback fun{ nullptr };
671     ASSERT_NO_FATAL_FAILURE(udsServer.SetRecvFun(fun));
672 }
673 
674 /**
675  * @tc.name: OnPacket_001
676  * @tc.desc: Test the OnPacket function of UDSServer
677  * @tc.type: FUNC
678  * @tc.require:
679  */
680 HWTEST_F(UDSServerTest, OnPacket_001, TestSize.Level1)
681 {
682     CALL_TEST_DEBUG;
683     UDSServer udsServer;
684     MmiMessageId msgId = MmiMessageId::INVALID;
685     NetPacket pkt(msgId);
686     int32_t fd = 1;
687     ASSERT_NO_FATAL_FAILURE(udsServer.OnPacket(fd, pkt));
688 }
689 
690 /**
691  * @tc.name: OnEpollRecv_003
692  * @tc.desc: Test the OnEpollRecv function of UDSServer
693  * @tc.type: FUNC
694  * @tc.require:
695  */
696 HWTEST_F(UDSServerTest, OnEpollRecv_003, TestSize.Level1)
697 {
698     CALL_TEST_DEBUG;
699     UDSServer udsServer;
700     int32_t fd = -1;
701     epoll_event ev;
702     ASSERT_NO_FATAL_FAILURE(udsServer.OnEpollRecv(fd, ev));
703 }
704 
705 /**
706  * @tc.name: OnEpollEvent_001
707  * @tc.desc: Test the OnEpollEvent function of UDSServer
708  * @tc.type: FUNC
709  * @tc.require:
710  */
711 HWTEST_F(UDSServerTest, OnEpollEvent_001, TestSize.Level1)
712 {
713     CALL_TEST_DEBUG;
714     UDSServer udsServer;
715     epoll_event ev;
716     ASSERT_NO_FATAL_FAILURE(udsServer.OnEpollEvent(ev));
717 }
718 
719 /**
720  * @tc.name: AddEpollEvent_002
721  * @tc.desc: Test the AddEpollEvent function of UDSServer
722  * @tc.type: FUNC
723  * @tc.require:
724  */
725 HWTEST_F(UDSServerTest, AddEpollEvent_002, TestSize.Level1)
726 {
727     CALL_TEST_DEBUG;
728     UDSServer udsServer;
729     int32_t fd = 1;
730     std::shared_ptr<mmi_epoll_event> epollEvent=std::make_shared<mmi_epoll_event>();
731     ASSERT_NO_FATAL_FAILURE(udsServer.AddEpollEvent(fd, epollEvent));
732 }
733 
734 /**
735  * @tc.name: RemoveEpollEvent_001
736  * @tc.desc: Test the RemoveEpollEvent function of UDSServer
737  * @tc.type: FUNC
738  * @tc.require:
739  */
740 HWTEST_F(UDSServerTest, RemoveEpollEvent_001, TestSize.Level1)
741 {
742     CALL_TEST_DEBUG;
743     UDSServer udsServer;
744     int32_t fd = 1;
745     ASSERT_NO_FATAL_FAILURE(udsServer.RemoveEpollEvent(fd));
746 }
747 
748 /**
749  * @tc.name: DumpSession_002
750  * @tc.desc: The DumpSession function of UDSServer properly outputs session information
751  * @tc.type: FUNC
752  * @tc.require:
753  */
754 HWTEST_F(UDSServerTest, DumpSession_002, TestSize.Level1)
755 {
756     CALL_TEST_DEBUG;
757     UDSServer udsServer;
758     std::string title = "test_title";
759     ASSERT_NO_FATAL_FAILURE(udsServer.DumpSession(title));
760 }
761 
762 /**
763  * @tc.name: AddSession_002
764  * @tc.desc: The AddSession function of UDSServer properly adds a session
765  * @tc.type: FUNC
766  * @tc.require:
767  */
768 HWTEST_F(UDSServerTest, AddSession_002, TestSize.Level1)
769 {
770     CALL_TEST_DEBUG;
771     UDSServer udsServer;
772     SessionPtr sess = nullptr;
773     bool ret = udsServer.AddSession(sess);
774     EXPECT_FALSE(ret);
775 }
776 
777 /**
778  * @tc.name: DelSession_003
779  * @tc.desc: The DelSession function of UDSServer properly deletes a session
780  * @tc.type: FUNC
781  * @tc.require:
782  */
783 HWTEST_F(UDSServerTest, DelSession_003, TestSize.Level1)
784 {
785     CALL_TEST_DEBUG;
786     UDSServer udsServer;
787     int32_t fd = -1;
788     ASSERT_NO_FATAL_FAILURE(udsServer.DelSession(fd));
789     int32_t fds = 1;
790     ASSERT_NO_FATAL_FAILURE(udsServer.DelSession(fds));
791 }
792 
793 /**
794  * @tc.name: NotifySessionDeleted_002
795  * @tc.desc: Test the NotifySessionDeleted function of UDSServer
796  * @tc.type: FUNC
797  * @tc.require:
798  */
799 HWTEST_F(UDSServerTest, NotifySessionDeleted_002, TestSize.Level1)
800 {
801     CALL_TEST_DEBUG;
802     UDSServer udsServer;
803     SessionPtr ses = nullptr;
804     ASSERT_NO_FATAL_FAILURE(udsServer.NotifySessionDeleted(ses));
805 }
806 
807 /**
808  * @tc.name: UDSServerTest_GetClientFd
809  * @tc.desc: Test Get Client Fd
810  * @tc.type: FUNC
811  * @tc.require:
812  */
813 HWTEST_F(UDSServerTest, UDSServerTest_GetClientFd, TestSize.Level1)
814 {
815     CALL_TEST_DEBUG;
816     UDSServer udsServer;
817     int32_t pid = 1000;
818     int32_t fd = 150;
819     udsServer.idxPidMap_.insert(std::make_pair(pid, fd));
820     ASSERT_EQ(udsServer.GetClientFd(pid), fd);
821 }
822 
823 /**
824  * @tc.name: UDSServerTest_GetClientPid
825  * @tc.desc: Test Get Client Pid
826  * @tc.type: FUNC
827  * @tc.require:
828  */
829 HWTEST_F(UDSServerTest, UDSServerTest_GetClientPid, TestSize.Level1)
830 {
831     CALL_TEST_DEBUG;
832     UDSServer udsServer;
833     int32_t fd = 150;
834     SessionPtr session = std::make_shared<UDSSession>(PROGRAM_NAME, MODULE_TYPE, UDS_FD, UDS_UID, UDS_PID);
835     udsServer.sessionsMap_.insert(std::make_pair(fd, session));
836     ASSERT_EQ(udsServer.GetClientPid(fd), UDS_PID);
837 }
838 
839 /**
840  * @tc.name: UDSServerTest_SendMsg
841  * @tc.desc: Test Send Msg
842  * @tc.type: FUNC
843  * @tc.require:
844  */
845 HWTEST_F(UDSServerTest, UDSServerTest_SendMsg, TestSize.Level1)
846 {
847     CALL_TEST_DEBUG;
848     UDSServer udsServer;
849     int32_t fd = 150;
850     MmiMessageId msgId = MmiMessageId::INVALID;
851     NetPacket pkt(msgId);
852     SessionPtr session = std::make_shared<UDSSession>(PROGRAM_NAME, MODULE_TYPE, UDS_FD, UDS_UID, UDS_PID);
853     udsServer.sessionsMap_.insert(std::make_pair(fd, session));
854     ASSERT_FALSE(udsServer.SendMsg(fd, pkt));
855 }
856 
857 /**
858  * @tc.name: UDSServerTest_GetSession
859  * @tc.desc: Test Get Session
860  * @tc.type: FUNC
861  * @tc.require:
862  */
863 HWTEST_F(UDSServerTest, UDSServerTest_GetSession, TestSize.Level1)
864 {
865     CALL_TEST_DEBUG;
866     UDSServer udsServer;
867     int32_t fd = 150;
868     SessionPtr session = std::make_shared<UDSSession>(PROGRAM_NAME, MODULE_TYPE, UDS_FD, UDS_UID, UDS_PID);
869     udsServer.sessionsMap_.insert(std::make_pair(fd, session));
870     ASSERT_EQ(udsServer.GetSession(fd), session);
871 }
872 
873 /**
874  * @tc.name: UDSServerTest_GetSessionByPid
875  * @tc.desc: Test Get Session By Pid
876  * @tc.type: FUNC
877  * @tc.require:
878  */
879 HWTEST_F(UDSServerTest, UDSServerTest_GetSessionByPid, TestSize.Level1)
880 {
881     CALL_TEST_DEBUG;
882     UDSServer udsServer;
883     int32_t fd = 150;
884     SessionPtr session = std::make_shared<UDSSession>(PROGRAM_NAME, MODULE_TYPE, UDS_FD, UDS_UID, UDS_PID);
885     udsServer.sessionsMap_.insert(std::make_pair(fd, session));
886     udsServer.idxPidMap_.insert(std::make_pair(UDS_PID, fd));
887     ASSERT_EQ(udsServer.GetSessionByPid(UDS_PID), session);
888 }
889 
890 /**
891  * @tc.name: UDSServerTest_DelSession
892  * @tc.desc: Test Delete Session
893  * @tc.type: FUNC
894  * @tc.require:
895  */
896 HWTEST_F(UDSServerTest, UDSServerTest_DelSession, TestSize.Level1)
897 {
898     CALL_TEST_DEBUG;
899     UDSServer udsServer;
900     int32_t fd = 100;
901     SessionPtr session = std::make_shared<UDSSession>(PROGRAM_NAME, MODULE_TYPE, UDS_FD, UDS_UID, UDS_PID);
902     udsServer.sessionsMap_.insert(std::make_pair(fd, session));
903     udsServer.idxPidMap_.insert(std::make_pair(UDS_PID, fd));
904     ASSERT_NO_FATAL_FAILURE(udsServer.DelSession(fd));
905 }
906 } // namespace MMI
907 } // namespace OHOS
908