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 #include "udp_wrap.h"
22 #include "uds_server.h"
23 
24 #undef MMI_LOG_TAG
25 #define MMI_LOG_TAG "UDSServerTest"
26 namespace OHOS {
27 namespace MMI {
28 namespace {
29 using namespace testing::ext;
30 const std::string PROGRAM_NAME = "uds_server_test";
31 constexpr int32_t MODULE_TYPE = 1;
32 constexpr int32_t UDS_FD = -1;
33 constexpr int32_t UDS_UID = 100;
34 constexpr int32_t UDS_PID = 100;
35 } // namespace
36 
37 class UDSServerTest : public testing::Test {
38 public:
SetUpTestCase(void)39     static void SetUpTestCase(void) {}
TearDownTestCase(void)40     static void TearDownTestCase(void) {}
41 };
42 
43 class UDSServerUnitTest : public UDSServer {
44 public:
SetFd(int32_t fd)45     void SetFd(int32_t fd)
46     {
47         fd_ = fd;
48     }
49 };
50 
51 /**
52  * @tc.name: SendMsg_001
53  * @tc.desc: Test the function SendMsg_001
54  * @tc.type: FUNC
55  * @tc.require:
56  */
57 HWTEST_F(UDSServerTest, SendMsg_001, TestSize.Level1)
58 {
59     CALL_TEST_DEBUG;
60     MmiMessageId msgId = MmiMessageId::INVALID;
61     NetPacket pkt(msgId);
62     int32_t fd = 1000;
63     UDSServer serObj;
64     bool retResult = serObj.SendMsg(fd, pkt);
65     EXPECT_FALSE(retResult);
66 }
67 
68 /**
69  * @tc.name: SendMsg_002
70  * @tc.desc: Test the function SendMsg_002
71  * @tc.type: FUNC
72  * @tc.require:
73  */
74 HWTEST_F(UDSServerTest, SendMsg_002, TestSize.Level1)
75 {
76     CALL_TEST_DEBUG;
77     MmiMessageId msgId = MmiMessageId::INVALID;
78     NetPacket pkt(msgId);
79 
80     int32_t fd = -1001;
81     UDSServer serObj;
82     bool retResult = serObj.SendMsg(fd, pkt);
83     ASSERT_FALSE(retResult);
84 }
85 
86 /**
87  * @tc.name: SendMsg_003
88  * @tc.desc: Test the function SendMsg_003
89  * @tc.type: FUNC
90  * @tc.require:
91  */
92 HWTEST_F(UDSServerTest, SendMsg_003, TestSize.Level1)
93 {
94     CALL_TEST_DEBUG;
95     MmiMessageId msgId = MmiMessageId::INVALID;
96     NetPacket pkt(msgId);
97 
98     int32_t fd = 3333;
99     UDSServer serObj;
100     bool retResult = serObj.SendMsg(fd, pkt);
101     ASSERT_FALSE(retResult);
102 }
103 
104 /**
105  * @tc.name: Multicast
106  * @tc.desc: Test the function Multicast
107  * @tc.type: FUNC
108  * @tc.require:
109  */
110 HWTEST_F(UDSServerTest, Multicast, TestSize.Level1)
111 {
112     CALL_TEST_DEBUG;
113     MmiMessageId msgId = MmiMessageId::INVALID;
114     NetPacket pkt(msgId);
115     std::vector<int32_t> fds;
116     ASSERT_NO_FATAL_FAILURE(fds.push_back(1));
117 
118     UDSServer serObj;
119     serObj.Multicast(fds, pkt);
120 }
121 
122 /**
123  * @tc.name: Stop_001
124  * @tc.desc: Test the function Stop_001
125  * @tc.type: FUNC
126  * @tc.require:
127  */
128 HWTEST_F(UDSServerTest, Stop_001, TestSize.Level1)
129 {
130     CALL_TEST_DEBUG;
131     UDSServer serObj;
132     ASSERT_NO_FATAL_FAILURE(serObj.UdsStop());
133 }
134 
135 /**
136  * @tc.name: GetSession_001
137  * @tc.desc: Test the function GetSession_001
138  * @tc.type: FUNC
139  * @tc.require:
140  */
141 HWTEST_F(UDSServerTest, GetSession_001, TestSize.Level1)
142 {
143     CALL_TEST_DEBUG;
144     UDSServer UDS_server;
145     int32_t fd = 0;
146     auto retResult = UDS_server.GetSession(fd);
147     EXPECT_TRUE(retResult == nullptr);
148 }
149 
150 /**
151  * @tc.name: GetSession_002
152  * @tc.desc: Test the function GetSession_002
153  * @tc.type: FUNC
154  * @tc.require:
155  */
156 HWTEST_F(UDSServerTest, GetSession_002, TestSize.Level1)
157 {
158     CALL_TEST_DEBUG;
159     UDSServer UDS_server;
160     int32_t fd = 1000000;
161     auto retResult = UDS_server.GetSession(fd);
162     EXPECT_TRUE(retResult == nullptr);
163 }
164 
165 /**
166  * @tc.name: GetSession_003
167  * @tc.desc: Test the function GetSession_003
168  * @tc.type: FUNC
169  * @tc.require:
170  */
171 HWTEST_F(UDSServerTest, GetSession_003, TestSize.Level1)
172 {
173     CALL_TEST_DEBUG;
174     UDSServer UDS_server;
175     int32_t fd = -1;
176     auto retResult = UDS_server.GetSession(fd);
177     EXPECT_TRUE(retResult == nullptr);
178 }
179 
180 /**
181  * @tc.name: UdsStop_001
182  * @tc.desc: Test the function UdsStop
183  * @tc.type: FUNC
184  * @tc.require:
185  */
186 HWTEST_F(UDSServerTest, UdsStop_001, TestSize.Level1)
187 {
188     CALL_TEST_DEBUG;
189     UDSServer udsServer;
190     const std::string programName = "program";
191     const int32_t moduleType = 1;
192     const int32_t uid = 2;
193     const int32_t pid = 10;
194     int32_t serverFd = 1;
195     int32_t toReturnClientFd = 1;
196     int32_t tokenType = 1;
197 
198     udsServer.AddSocketPairInfo(programName, moduleType, uid, pid, serverFd, toReturnClientFd, tokenType);
199     udsServer.UdsStop();
200 }
201 
202 /**
203  * @tc.name: GetClientPid_001
204  * @tc.desc: Test the function GetClientPid
205  * @tc.type: FUNC
206  * @tc.require:
207  */
208 HWTEST_F(UDSServerTest, GetClientPid_001, TestSize.Level1)
209 {
210     CALL_TEST_DEBUG;
211     UDSServer udsServer;
212     int32_t fd = 1;
213     int32_t pid1 = 0;
214     const std::string programName = "program";
215     const int32_t moduleType = 1;
216     const int32_t uid = 2;
217     const int32_t pid = 10;
218     int32_t serverFd = 1;
219     int32_t toReturnClientFd = 1;
220     int32_t tokenType = 1;
221 
222     udsServer.AddSocketPairInfo(programName, moduleType, uid, pid, serverFd, toReturnClientFd, tokenType);
223     pid1 = udsServer.GetClientPid(fd);
224     EXPECT_EQ(pid1, INVALID_PID);
225 }
226 
227 /**
228  * @tc.name: AddSocketPairInfo_001
229  * @tc.desc: Test the function AddSocketPairInfo
230  * @tc.type: FUNC
231  * @tc.require:
232  */
233 HWTEST_F(UDSServerTest, AddSocketPairInfo_001, TestSize.Level1)
234 {
235     CALL_TEST_DEBUG;
236     UDSServer udsServer;
237     const std::string programName = "program";
238     const int32_t moduleType = 1;
239     const int32_t uid = 2;
240     const int32_t pid = 10;
241     int32_t serverFd = 1;
242     int32_t toReturnClientFd = 1;
243     int32_t tokenType = 1;
244     int32_t ret = 0;
245 
246     ret = udsServer.AddSocketPairInfo(programName, moduleType, uid, pid, serverFd, toReturnClientFd, tokenType);
247     EXPECT_EQ(ret, RET_ERR);
248 }
249 
250 /**
251  * @tc.name: SetFdProperty_001
252  * @tc.desc: Test the function SetFdProperty
253  * @tc.type: FUNC
254  * @tc.require:
255  */
256 HWTEST_F(UDSServerTest, SetFdProperty_001, TestSize.Level1)
257 {
258     CALL_TEST_DEBUG;
259     UDSServer udsServer;
260     int32_t ret = RET_ERR;
261     int32_t tokenType = TokenType::TOKEN_NATIVE;
262     int32_t serverFd = 1;
263     const std::string programName = "program";
264     const int32_t moduleType = 1;
265     const int32_t uid = 2;
266     const int32_t pid = 10;
267     int32_t toReturnClientFd = 1;
268 
269     udsServer.AddSocketPairInfo(programName, moduleType, uid, pid, serverFd, toReturnClientFd, tokenType);
270     ret = udsServer.SetFdProperty(tokenType, serverFd, toReturnClientFd);
271     EXPECT_EQ(ret, RET_ERR);
272 }
273 
274 /**
275  * @tc.name: SetFdProperty_002
276  * @tc.desc: Test the function SetFdProperty
277  * @tc.type: FUNC
278  * @tc.require:
279  */
280 HWTEST_F(UDSServerTest, SetFdProperty_002, TestSize.Level1)
281 {
282     CALL_TEST_DEBUG;
283     UDSServer udsServer;
284     int32_t ret = RET_ERR;
285     int32_t tokenType = TokenType::TOKEN_SHELL;
286     int32_t serverFd = 1;
287     const std::string programName = "program";
288     const int32_t moduleType = 1;
289     const int32_t uid = 2;
290     const int32_t pid = 10;
291     int32_t toReturnClientFd = 1;
292 
293     udsServer.AddSocketPairInfo(programName, moduleType, uid, pid, serverFd, toReturnClientFd, tokenType);
294     ret = udsServer.SetFdProperty(tokenType, serverFd, toReturnClientFd);
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: SetFdProperty_003
602  * @tc.desc: Test the scenario of setting file descriptor properties
603  * @tc.type: FUNC
604  * @tc.require:
605  */
606 HWTEST_F(UDSServerTest, SetFdProperty_003, TestSize.Level1)
607 {
608     CALL_TEST_DEBUG;
609     UDSServer udsServer;
610     int32_t tokenType = TokenType::TOKEN_NATIVE;
611     int32_t serverFd = 123;
612     int32_t toReturnClientFd = 456;
613     auto ret = udsServer.SetFdProperty(tokenType, serverFd, toReturnClientFd);
614     EXPECT_EQ(ret, RET_ERR);
615 }
616 
617 /**
618  * @tc.name: Dump_002
619  * @tc.desc: Test the Dump functionality of UDSServer
620  * @tc.type: FUNC
621  * @tc.require:
622  */
623 HWTEST_F(UDSServerTest, Dump_002, TestSize.Level1)
624 {
625     CALL_TEST_DEBUG;
626     UDSServer udsServer;
627     int32_t fd = 1;
628     std::vector<std::string> args = {"help"};
629     ASSERT_NO_FATAL_FAILURE(udsServer.Dump(fd, args));
630 }
631 
632 /**
633  * @tc.name: OnConnected_002
634  * @tc.desc: Test the OnConnected function of UDSServer
635  * @tc.type: FUNC
636  * @tc.require:
637  */
638 HWTEST_F(UDSServerTest, OnConnected_002, TestSize.Level1)
639 {
640     CALL_TEST_DEBUG;
641     UDSServer udsServer;
642     SessionPtr sess = nullptr;
643     ASSERT_NO_FATAL_FAILURE(udsServer.OnConnected(sess));
644 }
645 
646 /**
647  * @tc.name: OnDisconnected_001
648  * @tc.desc: Test the OnDisconnected function of UDSServer
649  * @tc.type: FUNC
650  * @tc.require:
651  */
652 HWTEST_F(UDSServerTest, OnDisconnected_001, TestSize.Level1)
653 {
654     CALL_TEST_DEBUG;
655     UDSServer udsServer;
656     SessionPtr sess = nullptr;
657     ASSERT_NO_FATAL_FAILURE(udsServer.OnConnected(sess));
658     ASSERT_NO_FATAL_FAILURE(udsServer.OnDisconnected(sess));
659 }
660 
661 /**
662  * @tc.name: AddEpoll_001
663  * @tc.desc: Test the AddEpoll function of UDSServer
664  * @tc.type: FUNC
665  * @tc.require:
666  */
667 HWTEST_F(UDSServerTest, AddEpoll_001, TestSize.Level1)
668 {
669     CALL_TEST_DEBUG;
670     UDSServer udsServer;
671     EpollEventType type = EPOLL_EVENT_BEGIN;
672     int32_t fd = 1;
673     auto ret = udsServer.AddEpoll(type, fd);
674     EXPECT_EQ(ret, RET_ERR);
675 }
676 
677 /**
678  * @tc.name: SetRecvFun_002
679  * @tc.desc: Test the SetRecvFun function of UDSServer
680  * @tc.type: FUNC
681  * @tc.require:
682  */
683 HWTEST_F(UDSServerTest, SetRecvFun_002, TestSize.Level1)
684 {
685     CALL_TEST_DEBUG;
686     UDSServer udsServer;
687     MsgServerFunCallback fun{ nullptr };
688     ASSERT_NO_FATAL_FAILURE(udsServer.SetRecvFun(fun));
689 }
690 
691 /**
692  * @tc.name: ReleaseSession_002
693  * @tc.desc: Test the ReleaseSession function of UDSServer
694  * @tc.type: FUNC
695  * @tc.require:
696  */
697 HWTEST_F(UDSServerTest, ReleaseSession_002, TestSize.Level1)
698 {
699     CALL_TEST_DEBUG;
700     UDSServer udsServer;
701     int32_t fd = 1;
702     epoll_event ev;
703     ASSERT_NO_FATAL_FAILURE(udsServer.ReleaseSession(fd, ev));
704 }
705 
706 /**
707  * @tc.name: OnPacket_001
708  * @tc.desc: Test the OnPacket function of UDSServer
709  * @tc.type: FUNC
710  * @tc.require:
711  */
712 HWTEST_F(UDSServerTest, OnPacket_001, TestSize.Level1)
713 {
714     CALL_TEST_DEBUG;
715     UDSServer udsServer;
716     MmiMessageId msgId = MmiMessageId::INVALID;
717     NetPacket pkt(msgId);
718     int32_t fd = 1;
719     ASSERT_NO_FATAL_FAILURE(udsServer.OnPacket(fd, pkt));
720 }
721 
722 /**
723  * @tc.name: OnEpollRecv_003
724  * @tc.desc: Test the OnEpollRecv function of UDSServer
725  * @tc.type: FUNC
726  * @tc.require:
727  */
728 HWTEST_F(UDSServerTest, OnEpollRecv_003, TestSize.Level1)
729 {
730     CALL_TEST_DEBUG;
731     UDSServer udsServer;
732     int32_t fd = -1;
733     epoll_event ev;
734     ASSERT_NO_FATAL_FAILURE(udsServer.OnEpollRecv(fd, ev));
735 }
736 
737 /**
738  * @tc.name: OnEpollEvent_001
739  * @tc.desc: Test the OnEpollEvent function of UDSServer
740  * @tc.type: FUNC
741  * @tc.require:
742  */
743 HWTEST_F(UDSServerTest, OnEpollEvent_001, TestSize.Level1)
744 {
745     CALL_TEST_DEBUG;
746     UDSServer udsServer;
747     epoll_event ev;
748     ASSERT_NO_FATAL_FAILURE(udsServer.OnEpollEvent(ev));
749 }
750 
751 /**
752  * @tc.name: AddEpollEvent_002
753  * @tc.desc: Test the AddEpollEvent function of UDSServer
754  * @tc.type: FUNC
755  * @tc.require:
756  */
757 HWTEST_F(UDSServerTest, AddEpollEvent_002, TestSize.Level1)
758 {
759     CALL_TEST_DEBUG;
760     UDSServer udsServer;
761     int32_t fd = 1;
762     std::shared_ptr<mmi_epoll_event> epollEvent=std::make_shared<mmi_epoll_event>();
763     ASSERT_NO_FATAL_FAILURE(udsServer.AddEpollEvent(fd, epollEvent));
764 }
765 
766 /**
767  * @tc.name: RemoveEpollEvent_001
768  * @tc.desc: Test the RemoveEpollEvent function of UDSServer
769  * @tc.type: FUNC
770  * @tc.require:
771  */
772 HWTEST_F(UDSServerTest, RemoveEpollEvent_001, TestSize.Level1)
773 {
774     CALL_TEST_DEBUG;
775     UDSServer udsServer;
776     int32_t fd = 1;
777     ASSERT_NO_FATAL_FAILURE(udsServer.RemoveEpollEvent(fd));
778 }
779 
780 /**
781  * @tc.name: DumpSession_002
782  * @tc.desc: The DumpSession function of UDSServer properly outputs session information
783  * @tc.type: FUNC
784  * @tc.require:
785  */
786 HWTEST_F(UDSServerTest, DumpSession_002, TestSize.Level1)
787 {
788     CALL_TEST_DEBUG;
789     UDSServer udsServer;
790     std::string title = "test_title";
791     ASSERT_NO_FATAL_FAILURE(udsServer.DumpSession(title));
792 }
793 
794 /**
795  * @tc.name: AddSession_002
796  * @tc.desc: The AddSession function of UDSServer properly adds a session
797  * @tc.type: FUNC
798  * @tc.require:
799  */
800 HWTEST_F(UDSServerTest, AddSession_002, TestSize.Level1)
801 {
802     CALL_TEST_DEBUG;
803     UDSServer udsServer;
804     SessionPtr sess = nullptr;
805     bool ret = udsServer.AddSession(sess);
806     EXPECT_FALSE(ret);
807 }
808 
809 /**
810  * @tc.name: DelSession_003
811  * @tc.desc: The DelSession function of UDSServer properly deletes a session
812  * @tc.type: FUNC
813  * @tc.require:
814  */
815 HWTEST_F(UDSServerTest, DelSession_003, TestSize.Level1)
816 {
817     CALL_TEST_DEBUG;
818     UDSServer udsServer;
819     int32_t fd = -1;
820     ASSERT_NO_FATAL_FAILURE(udsServer.DelSession(fd));
821     int32_t fds = 1;
822     ASSERT_NO_FATAL_FAILURE(udsServer.DelSession(fds));
823 }
824 
825 /**
826  * @tc.name: NotifySessionDeleted_002
827  * @tc.desc: Test the NotifySessionDeleted function of UDSServer
828  * @tc.type: FUNC
829  * @tc.require:
830  */
831 HWTEST_F(UDSServerTest, NotifySessionDeleted_002, TestSize.Level1)
832 {
833     CALL_TEST_DEBUG;
834     UDSServer udsServer;
835     SessionPtr ses = nullptr;
836     ASSERT_NO_FATAL_FAILURE(udsServer.NotifySessionDeleted(ses));
837 }
838 
839 /**
840  * @tc.name: UDSServerTest_GetClientFd
841  * @tc.desc: Test Get Client Fd
842  * @tc.type: FUNC
843  * @tc.require:
844  */
845 HWTEST_F(UDSServerTest, UDSServerTest_GetClientFd, TestSize.Level1)
846 {
847     CALL_TEST_DEBUG;
848     UDSServer udsServer;
849     int32_t pid = 1000;
850     int32_t fd = 150;
851     udsServer.idxPidMap_.insert(std::make_pair(pid, fd));
852     ASSERT_EQ(udsServer.GetClientFd(pid), fd);
853 }
854 
855 /**
856  * @tc.name: UDSServerTest_GetClientPid
857  * @tc.desc: Test Get Client Pid
858  * @tc.type: FUNC
859  * @tc.require:
860  */
861 HWTEST_F(UDSServerTest, UDSServerTest_GetClientPid, TestSize.Level1)
862 {
863     CALL_TEST_DEBUG;
864     UDSServer udsServer;
865     int32_t fd = 150;
866     SessionPtr session = std::make_shared<UDSSession>(PROGRAM_NAME, MODULE_TYPE, UDS_FD, UDS_UID, UDS_PID);
867     udsServer.sessionsMap_.insert(std::make_pair(fd, session));
868     ASSERT_EQ(udsServer.GetClientPid(fd), UDS_PID);
869 }
870 
871 /**
872  * @tc.name: UDSServerTest_SendMsg
873  * @tc.desc: Test Send Msg
874  * @tc.type: FUNC
875  * @tc.require:
876  */
877 HWTEST_F(UDSServerTest, UDSServerTest_SendMsg, TestSize.Level1)
878 {
879     CALL_TEST_DEBUG;
880     UDSServer udsServer;
881     int32_t fd = 150;
882     MmiMessageId msgId = MmiMessageId::INVALID;
883     NetPacket pkt(msgId);
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     ASSERT_FALSE(udsServer.SendMsg(fd, pkt));
887 }
888 
889 /**
890  * @tc.name: UDSServerTest_GetSession
891  * @tc.desc: Test Get Session
892  * @tc.type: FUNC
893  * @tc.require:
894  */
895 HWTEST_F(UDSServerTest, UDSServerTest_GetSession, TestSize.Level1)
896 {
897     CALL_TEST_DEBUG;
898     UDSServer udsServer;
899     int32_t fd = 150;
900     SessionPtr session = std::make_shared<UDSSession>(PROGRAM_NAME, MODULE_TYPE, UDS_FD, UDS_UID, UDS_PID);
901     udsServer.sessionsMap_.insert(std::make_pair(fd, session));
902     ASSERT_EQ(udsServer.GetSession(fd), session);
903 }
904 
905 /**
906  * @tc.name: UDSServerTest_GetSessionByPid
907  * @tc.desc: Test Get Session By Pid
908  * @tc.type: FUNC
909  * @tc.require:
910  */
911 HWTEST_F(UDSServerTest, UDSServerTest_GetSessionByPid, TestSize.Level1)
912 {
913     CALL_TEST_DEBUG;
914     UDSServer udsServer;
915     int32_t fd = 150;
916     SessionPtr session = std::make_shared<UDSSession>(PROGRAM_NAME, MODULE_TYPE, UDS_FD, UDS_UID, UDS_PID);
917     udsServer.sessionsMap_.insert(std::make_pair(fd, session));
918     udsServer.idxPidMap_.insert(std::make_pair(UDS_PID, fd));
919     ASSERT_EQ(udsServer.GetSessionByPid(UDS_PID), session);
920 }
921 
922 /**
923  * @tc.name: UDSServerTest_AddSession
924  * @tc.desc: Test Add Session
925  * @tc.type: FUNC
926  * @tc.require:
927  */
928 HWTEST_F(UDSServerTest, UDSServerTest_AddSession, TestSize.Level1)
929 {
930     CALL_TEST_DEBUG;
931     UDSServer udsServer;
932     int32_t udsSessionFd = 100;
933     int32_t udsSessionPid = -1;
934     SessionPtr session = std::make_shared<UDSSession>(PROGRAM_NAME, MODULE_TYPE, udsSessionFd, UDS_UID, udsSessionPid);
935     ASSERT_FALSE(udsServer.AddSession(session));
936 
937     udsSessionPid = 1000;
938     SessionPtr sess = std::make_shared<UDSSession>(PROGRAM_NAME, MODULE_TYPE, udsSessionFd, UDS_UID, udsSessionPid);
939     udsServer.sessionsMap_.insert(std::make_pair(udsSessionFd, sess));
940     ASSERT_TRUE(udsServer.AddSession(sess));
941     SessionPtr sessPtr = std::make_shared<UDSSession>(PROGRAM_NAME, MODULE_TYPE, udsSessionFd, UDS_UID, udsSessionPid);
942     for (int32_t i = 0; i < MAX_SESSON_ALARM + 1; ++i) {
943         udsServer.sessionsMap_.insert(std::make_pair(i, sessPtr));
944     }
945     ASSERT_TRUE(udsServer.AddSession(sessPtr));
946 }
947 
948 /**
949  * @tc.name: UDSServerTest_DelSession
950  * @tc.desc: Test Delete Session
951  * @tc.type: FUNC
952  * @tc.require:
953  */
954 HWTEST_F(UDSServerTest, UDSServerTest_DelSession, TestSize.Level1)
955 {
956     CALL_TEST_DEBUG;
957     UDSServer udsServer;
958     int32_t fd = 100;
959     SessionPtr session = std::make_shared<UDSSession>(PROGRAM_NAME, MODULE_TYPE, UDS_FD, UDS_UID, UDS_PID);
960     udsServer.sessionsMap_.insert(std::make_pair(fd, session));
961     udsServer.idxPidMap_.insert(std::make_pair(UDS_PID, fd));
962     ASSERT_NO_FATAL_FAILURE(udsServer.DelSession(fd));
963 }
964 } // namespace MMI
965 } // namespace OHOS
966