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"
24namespace OHOS {
25namespace MMI {
26namespace {
27using namespace testing::ext;
28const std::string PROGRAM_NAME = "uds_server_test";
29constexpr int32_t MODULE_TYPE = 1;
30constexpr int32_t UDS_FD = -1;
31constexpr int32_t UDS_UID = 100;
32constexpr int32_t UDS_PID = 100;
33} // namespace
34
35class UDSServerTest : public testing::Test {
36public:
37    static void SetUpTestCase(void) {}
38    static void TearDownTestCase(void) {}
39};
40
41class UDSServerUnitTest : public UDSServer {
42public:
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 */
55HWTEST_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 */
72HWTEST_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 */
90HWTEST_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 */
108HWTEST_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 */
126HWTEST_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 */
139HWTEST_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 */
154HWTEST_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 */
169HWTEST_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 */
184HWTEST_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 */
206HWTEST_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 */
231HWTEST_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 */
254HWTEST_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
267    udsServer.AddSocketPairInfo(programName, moduleType, uid, pid, serverFd, toReturnClientFd, tokenType);
268    ret = udsServer.SetFdProperty(tokenType, serverFd, toReturnClientFd);
269    EXPECT_EQ(ret, RET_ERR);
270}
271
272/**
273 * @tc.name: SetFdProperty_002
274 * @tc.desc: Test the function SetFdProperty
275 * @tc.type: FUNC
276 * @tc.require:
277 */
278HWTEST_F(UDSServerTest, SetFdProperty_002, TestSize.Level1)
279{
280    CALL_TEST_DEBUG;
281    UDSServer udsServer;
282    int32_t ret = RET_ERR;
283    int32_t tokenType = TokenType::TOKEN_SHELL;
284    int32_t serverFd = 1;
285    const std::string programName = "program";
286    const int32_t moduleType = 1;
287    const int32_t uid = 2;
288    const int32_t pid = 10;
289    int32_t toReturnClientFd = 1;
290
291    udsServer.AddSocketPairInfo(programName, moduleType, uid, pid, serverFd, toReturnClientFd, tokenType);
292    ret = udsServer.SetFdProperty(tokenType, serverFd, toReturnClientFd);
293    EXPECT_EQ(ret, RET_ERR);
294}
295
296/**
297 * @tc.name: Dump_001
298 * @tc.desc: Test the function Dump
299 * @tc.type: FUNC
300 * @tc.require:
301 */
302HWTEST_F(UDSServerTest, Dump_001, TestSize.Level1)
303{
304    CALL_TEST_DEBUG;
305    UDSServer udsServer;
306    int32_t fd = 1;
307    const std::vector<std::string> args = {"help"};
308    int32_t tokenType = TokenType::TOKEN_SHELL;
309    int32_t serverFd = 1;
310    const std::string programName = "program";
311    const int32_t moduleType = 1;
312    const int32_t uid = 2;
313    const int32_t pid = 10;
314    int32_t toReturnClientFd = 1;
315
316    udsServer.AddSocketPairInfo(programName, moduleType, uid, pid, serverFd, toReturnClientFd, tokenType);
317    udsServer.Dump(fd, args);
318}
319
320/**
321 * @tc.name: OnConnected_001
322 * @tc.desc: Test the function OnConnected
323 * @tc.type: FUNC
324 * @tc.require:
325 */
326HWTEST_F(UDSServerTest, OnConnected_001, TestSize.Level1)
327{
328    CALL_TEST_DEBUG;
329    UDSServer udsServer;
330    SessionPtr sess;
331    int32_t tokenType = TokenType::TOKEN_SHELL;
332    int32_t serverFd = 1;
333    const std::string programName = "program";
334    const int32_t moduleType = 1;
335    const int32_t uid = 2;
336    const int32_t pid = 10;
337    int32_t toReturnClientFd = 1;
338
339    udsServer.AddSocketPairInfo(programName, moduleType, uid, pid, serverFd, toReturnClientFd, tokenType);
340    udsServer.OnConnected(sess);
341}
342
343/**
344 * @tc.name: SetRecvFun_001
345 * @tc.desc: Test the function SetRecvFun
346 * @tc.type: FUNC
347 * @tc.require:
348 */
349HWTEST_F(UDSServerTest, SetRecvFun_001, TestSize.Level1)
350{
351    CALL_TEST_DEBUG;
352    UDSServer udsServer;
353    MsgServerFunCallback fun{ nullptr };
354    int32_t tokenType = TokenType::TOKEN_SHELL;
355    int32_t serverFd = 1;
356    const std::string programName = "program";
357    const int32_t moduleType = 1;
358    const int32_t uid = 2;
359    const int32_t pid = 10;
360    int32_t toReturnClientFd = 1;
361
362    udsServer.AddSocketPairInfo(programName, moduleType, uid, pid, serverFd, toReturnClientFd, tokenType);
363    udsServer.SetRecvFun(fun);
364}
365
366/**
367 * @tc.name: OnEpollRecv_001
368 * @tc.desc: Test the function OnEpollRecv
369 * @tc.type: FUNC
370 * @tc.require:
371 */
372HWTEST_F(UDSServerTest, OnEpollRecv_001, TestSize.Level1)
373{
374    CALL_TEST_DEBUG;
375    UDSServer udsServer;
376    int32_t size = 100;
377    epoll_event ev;
378    int32_t tokenType = TokenType::TOKEN_SHELL;
379    int32_t serverFd = 1;
380    const std::string programName = "program";
381    const int32_t moduleType = 1;
382    const int32_t uid = 2;
383    const int32_t pid = 10;
384    int32_t toReturnClientFd = 1;
385
386    udsServer.AddSocketPairInfo(programName, moduleType, uid, pid, serverFd, toReturnClientFd, tokenType);
387    int32_t fd = epoll_create(size);
388    udsServer.OnEpollRecv(fd, ev);
389}
390
391/**
392 * @tc.name: OnEpollRecv_002
393 * @tc.desc: Test the function OnEpollRecv
394 * @tc.type: FUNC
395 * @tc.require:
396 */
397HWTEST_F(UDSServerTest, OnEpollRecv_002, TestSize.Level1)
398{
399    CALL_TEST_DEBUG;
400    UDSServer udsServer;
401    epoll_event ev;
402    int32_t fd = -1;
403    int32_t tokenType = TokenType::TOKEN_SHELL;
404    int32_t serverFd = 1;
405    const std::string programName = "program";
406    const int32_t moduleType = 1;
407    const int32_t uid = 2;
408    const int32_t pid = 10;
409    int32_t toReturnClientFd = 1;
410
411    udsServer.AddSocketPairInfo(programName, moduleType, uid, pid, serverFd, toReturnClientFd, tokenType);
412    udsServer.OnEpollRecv(fd, ev);
413}
414
415/**
416 * @tc.name: AddEpollEvent_001
417 * @tc.desc: Test the function AddEpollEvent
418 * @tc.type: FUNC
419 * @tc.require:
420 */
421HWTEST_F(UDSServerTest, AddEpollEvent_001, TestSize.Level1)
422{
423    CALL_TEST_DEBUG;
424    UDSServer udsServer;
425    std::shared_ptr<mmi_epoll_event> epollEvent=std::make_shared<mmi_epoll_event>();
426    int32_t fd = 1;
427    int32_t tokenType = TokenType::TOKEN_SHELL;
428    int32_t serverFd = 1;
429    const std::string programName = "program";
430    const int32_t moduleType = 1;
431    const int32_t uid = 2;
432    const int32_t pid = 10;
433    int32_t toReturnClientFd = 1;
434
435    udsServer.AddSocketPairInfo(programName, moduleType, uid, pid, serverFd, toReturnClientFd, tokenType);
436    udsServer.AddEpollEvent(fd, epollEvent);
437}
438
439/**
440 * @tc.name: DumpSession_001
441 * @tc.desc: Test the function DumpSession
442 * @tc.type: FUNC
443 * @tc.require:
444 */
445HWTEST_F(UDSServerTest, DumpSession_001, TestSize.Level1)
446{
447    CALL_TEST_DEBUG;
448    UDSServer udsServer;
449    const std::string title = "test_title";
450    int32_t tokenType = TokenType::TOKEN_SHELL;
451    int32_t serverFd = 1;
452    const std::string programName = "program";
453    const int32_t moduleType = 1;
454    const int32_t uid = 2;
455    const int32_t pid = 10;
456    int32_t toReturnClientFd = 1;
457
458    udsServer.AddSocketPairInfo(programName, moduleType, uid, pid, serverFd, toReturnClientFd, tokenType);
459    udsServer.DumpSession(title);
460}
461
462/**
463 * @tc.name: AddSession_001
464 * @tc.desc: Test the function AddSession
465 * @tc.type: FUNC
466 * @tc.require:
467 */
468HWTEST_F(UDSServerTest, AddSession_001, TestSize.Level1)
469{
470    CALL_TEST_DEBUG;
471    UDSServer udsServer;
472    SessionPtr sess = nullptr;
473    int32_t tokenType = TokenType::TOKEN_SHELL;
474    int32_t serverFd = 1;
475    const std::string programName = "program";
476    const int32_t moduleType = 1;
477    const int32_t uid = 2;
478    const int32_t pid = 10;
479    int32_t toReturnClientFd = 1;
480
481    udsServer.AddSocketPairInfo(programName, moduleType, uid, pid, serverFd, toReturnClientFd, tokenType);
482    sess = std::make_shared<UDSSession>(programName, moduleType, serverFd, uid, pid);
483    udsServer.AddSession(sess);
484}
485
486/**
487 * @tc.name: DelSession_001
488 * @tc.desc: Test the function DelSession
489 * @tc.type: FUNC
490 * @tc.require:
491 */
492HWTEST_F(UDSServerTest, DelSession_001, TestSize.Level1)
493{
494    CALL_TEST_DEBUG;
495    UDSServer udsServer;
496    int32_t fd = -1;
497    int32_t tokenType = TokenType::TOKEN_SHELL;
498    int32_t serverFd = 1;
499    const std::string programName = "program";
500    const int32_t moduleType = 1;
501    const int32_t uid = 2;
502    const int32_t pid = 10;
503    int32_t toReturnClientFd = 1;
504
505    udsServer.AddSocketPairInfo(programName, moduleType, uid, pid, serverFd, toReturnClientFd, tokenType);
506    udsServer.DelSession(fd);
507}
508
509/**
510 * @tc.name: DelSession_002
511 * @tc.desc: Test the function DelSession
512 * @tc.type: FUNC
513 * @tc.require:
514 */
515HWTEST_F(UDSServerTest, DelSession_002, TestSize.Level1)
516{
517    CALL_TEST_DEBUG;
518    UDSServer udsServer;
519    int32_t fd = 1;
520    int32_t tokenType = TokenType::TOKEN_SHELL;
521    int32_t serverFd = 1;
522    const std::string programName = "program";
523    const int32_t moduleType = 1;
524    const int32_t uid = 2;
525    const int32_t pid = 10;
526    int32_t toReturnClientFd = 1;
527
528    udsServer.AddSocketPairInfo(programName, moduleType, uid, pid, serverFd, toReturnClientFd, tokenType);
529    udsServer.DelSession(fd);
530}
531
532/**
533 * @tc.name: NotifySessionDeleted_001
534 * @tc.desc: Test the function NotifySessionDeleted
535 * @tc.type: FUNC
536 * @tc.require:
537 */
538HWTEST_F(UDSServerTest, NotifySessionDeleted_001, TestSize.Level1)
539{
540    CALL_TEST_DEBUG;
541    UDSServer udsServer;
542    SessionPtr sess = nullptr;
543    int32_t tokenType = TokenType::TOKEN_SHELL;
544    int32_t serverFd = 1;
545    const std::string programName = "program";
546    const int32_t moduleType = 1;
547    const int32_t uid = 2;
548    const int32_t pid = 10;
549    int32_t toReturnClientFd = 1;
550
551    udsServer.AddSocketPairInfo(programName, moduleType, uid, pid, serverFd, toReturnClientFd, tokenType);
552    sess = std::make_shared<UDSSession>(programName, moduleType, serverFd, uid, pid);
553    udsServer.NotifySessionDeleted(sess);
554}
555
556/**
557 * @tc.name: GetClientPid_002
558 * @tc.desc: Test the scenario of obtaining the client process ID
559 * @tc.type: FUNC
560 * @tc.require:
561 */
562HWTEST_F(UDSServerTest, GetClientPid_002, TestSize.Level1)
563{
564    CALL_TEST_DEBUG;
565    UDSServer udsServer;
566    int32_t fd = 123;
567    auto ret = udsServer.GetClientPid(fd);
568    EXPECT_EQ(ret, RET_ERR);
569}
570
571/**
572 * @tc.name: AddSocketPairInfo_002
573 * @tc.desc: Test the scenario of adding socket pair information
574 * @tc.type: FUNC
575 * @tc.require:
576 */
577HWTEST_F(UDSServerTest, AddSocketPairInfo_002, TestSize.Level1)
578{
579    CALL_TEST_DEBUG;
580    UDSServer udsServer;
581    std::string programName = "program";
582    int32_t moduleType = 1;
583    int32_t uid = 2;
584    int32_t pid = 10;
585    int32_t serverFd = 123;
586    int32_t toReturnClientFd = 456;
587    int32_t tokenType = 1;
588    auto ret = udsServer.AddSocketPairInfo(programName, moduleType, uid, pid, serverFd, toReturnClientFd, tokenType);
589    if (serverFd != -1) {
590        close(serverFd);
591    }
592    if (toReturnClientFd != -1) {
593        close(toReturnClientFd);
594    }
595    EXPECT_EQ(ret, RET_ERR);
596}
597
598/**
599 * @tc.name: SetFdProperty_003
600 * @tc.desc: Test the scenario of setting file descriptor properties
601 * @tc.type: FUNC
602 * @tc.require:
603 */
604HWTEST_F(UDSServerTest, SetFdProperty_003, TestSize.Level1)
605{
606    CALL_TEST_DEBUG;
607    UDSServer udsServer;
608    int32_t tokenType = TokenType::TOKEN_NATIVE;
609    int32_t serverFd = 123;
610    int32_t toReturnClientFd = 456;
611    auto ret = udsServer.SetFdProperty(tokenType, serverFd, toReturnClientFd);
612    EXPECT_EQ(ret, RET_ERR);
613}
614
615/**
616 * @tc.name: Dump_002
617 * @tc.desc: Test the Dump functionality of UDSServer
618 * @tc.type: FUNC
619 * @tc.require:
620 */
621HWTEST_F(UDSServerTest, Dump_002, TestSize.Level1)
622{
623    CALL_TEST_DEBUG;
624    UDSServer udsServer;
625    int32_t fd = 1;
626    std::vector<std::string> args = {"help"};
627    ASSERT_NO_FATAL_FAILURE(udsServer.Dump(fd, args));
628}
629
630/**
631 * @tc.name: OnConnected_002
632 * @tc.desc: Test the OnConnected function of UDSServer
633 * @tc.type: FUNC
634 * @tc.require:
635 */
636HWTEST_F(UDSServerTest, OnConnected_002, TestSize.Level1)
637{
638    CALL_TEST_DEBUG;
639    UDSServer udsServer;
640    SessionPtr sess = nullptr;
641    ASSERT_NO_FATAL_FAILURE(udsServer.OnConnected(sess));
642}
643
644/**
645 * @tc.name: OnDisconnected_001
646 * @tc.desc: Test the OnDisconnected function of UDSServer
647 * @tc.type: FUNC
648 * @tc.require:
649 */
650HWTEST_F(UDSServerTest, OnDisconnected_001, TestSize.Level1)
651{
652    CALL_TEST_DEBUG;
653    UDSServer udsServer;
654    SessionPtr sess = nullptr;
655    ASSERT_NO_FATAL_FAILURE(udsServer.OnConnected(sess));
656    ASSERT_NO_FATAL_FAILURE(udsServer.OnDisconnected(sess));
657}
658
659/**
660 * @tc.name: AddEpoll_001
661 * @tc.desc: Test the AddEpoll function of UDSServer
662 * @tc.type: FUNC
663 * @tc.require:
664 */
665HWTEST_F(UDSServerTest, AddEpoll_001, TestSize.Level1)
666{
667    CALL_TEST_DEBUG;
668    UDSServer udsServer;
669    EpollEventType type = EPOLL_EVENT_BEGIN;
670    int32_t fd = 1;
671    auto ret = udsServer.AddEpoll(type, fd);
672    EXPECT_EQ(ret, RET_OK);
673}
674
675/**
676 * @tc.name: SetRecvFun_002
677 * @tc.desc: Test the SetRecvFun function of UDSServer
678 * @tc.type: FUNC
679 * @tc.require:
680 */
681HWTEST_F(UDSServerTest, SetRecvFun_002, TestSize.Level1)
682{
683    CALL_TEST_DEBUG;
684    UDSServer udsServer;
685    MsgServerFunCallback fun{ nullptr };
686    ASSERT_NO_FATAL_FAILURE(udsServer.SetRecvFun(fun));
687}
688
689/**
690 * @tc.name: ReleaseSession_002
691 * @tc.desc: Test the ReleaseSession function of UDSServer
692 * @tc.type: FUNC
693 * @tc.require:
694 */
695HWTEST_F(UDSServerTest, ReleaseSession_002, TestSize.Level1)
696{
697    CALL_TEST_DEBUG;
698    UDSServer udsServer;
699    int32_t fd = 1;
700    epoll_event ev;
701    ASSERT_NO_FATAL_FAILURE(udsServer.ReleaseSession(fd, ev));
702}
703
704/**
705 * @tc.name: OnPacket_001
706 * @tc.desc: Test the OnPacket function of UDSServer
707 * @tc.type: FUNC
708 * @tc.require:
709 */
710HWTEST_F(UDSServerTest, OnPacket_001, TestSize.Level1)
711{
712    CALL_TEST_DEBUG;
713    UDSServer udsServer;
714    MmiMessageId msgId = MmiMessageId::INVALID;
715    NetPacket pkt(msgId);
716    int32_t fd = 1;
717    ASSERT_NO_FATAL_FAILURE(udsServer.OnPacket(fd, pkt));
718}
719
720/**
721 * @tc.name: OnEpollRecv_003
722 * @tc.desc: Test the OnEpollRecv function of UDSServer
723 * @tc.type: FUNC
724 * @tc.require:
725 */
726HWTEST_F(UDSServerTest, OnEpollRecv_003, TestSize.Level1)
727{
728    CALL_TEST_DEBUG;
729    UDSServer udsServer;
730    int32_t fd = -1;
731    epoll_event ev;
732    ASSERT_NO_FATAL_FAILURE(udsServer.OnEpollRecv(fd, ev));
733}
734
735/**
736 * @tc.name: OnEpollEvent_001
737 * @tc.desc: Test the OnEpollEvent function of UDSServer
738 * @tc.type: FUNC
739 * @tc.require:
740 */
741HWTEST_F(UDSServerTest, OnEpollEvent_001, TestSize.Level1)
742{
743    CALL_TEST_DEBUG;
744    UDSServer udsServer;
745    epoll_event ev;
746    ASSERT_NO_FATAL_FAILURE(udsServer.OnEpollEvent(ev));
747}
748
749/**
750 * @tc.name: AddEpollEvent_002
751 * @tc.desc: Test the AddEpollEvent function of UDSServer
752 * @tc.type: FUNC
753 * @tc.require:
754 */
755HWTEST_F(UDSServerTest, AddEpollEvent_002, TestSize.Level1)
756{
757    CALL_TEST_DEBUG;
758    UDSServer udsServer;
759    int32_t fd = 1;
760    std::shared_ptr<mmi_epoll_event> epollEvent=std::make_shared<mmi_epoll_event>();
761    ASSERT_NO_FATAL_FAILURE(udsServer.AddEpollEvent(fd, epollEvent));
762}
763
764/**
765 * @tc.name: RemoveEpollEvent_001
766 * @tc.desc: Test the RemoveEpollEvent function of UDSServer
767 * @tc.type: FUNC
768 * @tc.require:
769 */
770HWTEST_F(UDSServerTest, RemoveEpollEvent_001, TestSize.Level1)
771{
772    CALL_TEST_DEBUG;
773    UDSServer udsServer;
774    int32_t fd = 1;
775    ASSERT_NO_FATAL_FAILURE(udsServer.RemoveEpollEvent(fd));
776}
777
778/**
779 * @tc.name: DumpSession_002
780 * @tc.desc: The DumpSession function of UDSServer properly outputs session information
781 * @tc.type: FUNC
782 * @tc.require:
783 */
784HWTEST_F(UDSServerTest, DumpSession_002, TestSize.Level1)
785{
786    CALL_TEST_DEBUG;
787    UDSServer udsServer;
788    std::string title = "test_title";
789    ASSERT_NO_FATAL_FAILURE(udsServer.DumpSession(title));
790}
791
792/**
793 * @tc.name: AddSession_002
794 * @tc.desc: The AddSession function of UDSServer properly adds a session
795 * @tc.type: FUNC
796 * @tc.require:
797 */
798HWTEST_F(UDSServerTest, AddSession_002, TestSize.Level1)
799{
800    CALL_TEST_DEBUG;
801    UDSServer udsServer;
802    SessionPtr sess = nullptr;
803    bool ret = udsServer.AddSession(sess);
804    EXPECT_FALSE(ret);
805}
806
807/**
808 * @tc.name: DelSession_003
809 * @tc.desc: The DelSession function of UDSServer properly deletes a session
810 * @tc.type: FUNC
811 * @tc.require:
812 */
813HWTEST_F(UDSServerTest, DelSession_003, TestSize.Level1)
814{
815    CALL_TEST_DEBUG;
816    UDSServer udsServer;
817    int32_t fd = -1;
818    ASSERT_NO_FATAL_FAILURE(udsServer.DelSession(fd));
819    int32_t fds = 1;
820    ASSERT_NO_FATAL_FAILURE(udsServer.DelSession(fds));
821}
822
823/**
824 * @tc.name: NotifySessionDeleted_002
825 * @tc.desc: Test the NotifySessionDeleted function of UDSServer
826 * @tc.type: FUNC
827 * @tc.require:
828 */
829HWTEST_F(UDSServerTest, NotifySessionDeleted_002, TestSize.Level1)
830{
831    CALL_TEST_DEBUG;
832    UDSServer udsServer;
833    SessionPtr ses = nullptr;
834    ASSERT_NO_FATAL_FAILURE(udsServer.NotifySessionDeleted(ses));
835}
836
837/**
838 * @tc.name: UDSServerTest_GetClientFd
839 * @tc.desc: Test Get Client Fd
840 * @tc.type: FUNC
841 * @tc.require:
842 */
843HWTEST_F(UDSServerTest, UDSServerTest_GetClientFd, TestSize.Level1)
844{
845    CALL_TEST_DEBUG;
846    UDSServer udsServer;
847    int32_t pid = 1000;
848    int32_t fd = 150;
849    udsServer.idxPidMap_.insert(std::make_pair(pid, fd));
850    ASSERT_EQ(udsServer.GetClientFd(pid), fd);
851}
852
853/**
854 * @tc.name: UDSServerTest_GetClientPid
855 * @tc.desc: Test Get Client Pid
856 * @tc.type: FUNC
857 * @tc.require:
858 */
859HWTEST_F(UDSServerTest, UDSServerTest_GetClientPid, TestSize.Level1)
860{
861    CALL_TEST_DEBUG;
862    UDSServer udsServer;
863    int32_t fd = 150;
864    SessionPtr session = std::make_shared<UDSSession>(PROGRAM_NAME, MODULE_TYPE, UDS_FD, UDS_UID, UDS_PID);
865    udsServer.sessionsMap_.insert(std::make_pair(fd, session));
866    ASSERT_EQ(udsServer.GetClientPid(fd), UDS_PID);
867}
868
869/**
870 * @tc.name: UDSServerTest_SendMsg
871 * @tc.desc: Test Send Msg
872 * @tc.type: FUNC
873 * @tc.require:
874 */
875HWTEST_F(UDSServerTest, UDSServerTest_SendMsg, TestSize.Level1)
876{
877    CALL_TEST_DEBUG;
878    UDSServer udsServer;
879    int32_t fd = 150;
880    MmiMessageId msgId = MmiMessageId::INVALID;
881    NetPacket pkt(msgId);
882    SessionPtr session = std::make_shared<UDSSession>(PROGRAM_NAME, MODULE_TYPE, UDS_FD, UDS_UID, UDS_PID);
883    udsServer.sessionsMap_.insert(std::make_pair(fd, session));
884    ASSERT_FALSE(udsServer.SendMsg(fd, pkt));
885}
886
887/**
888 * @tc.name: UDSServerTest_GetSession
889 * @tc.desc: Test Get Session
890 * @tc.type: FUNC
891 * @tc.require:
892 */
893HWTEST_F(UDSServerTest, UDSServerTest_GetSession, TestSize.Level1)
894{
895    CALL_TEST_DEBUG;
896    UDSServer udsServer;
897    int32_t fd = 150;
898    SessionPtr session = std::make_shared<UDSSession>(PROGRAM_NAME, MODULE_TYPE, UDS_FD, UDS_UID, UDS_PID);
899    udsServer.sessionsMap_.insert(std::make_pair(fd, session));
900    ASSERT_EQ(udsServer.GetSession(fd), session);
901}
902
903/**
904 * @tc.name: UDSServerTest_GetSessionByPid
905 * @tc.desc: Test Get Session By Pid
906 * @tc.type: FUNC
907 * @tc.require:
908 */
909HWTEST_F(UDSServerTest, UDSServerTest_GetSessionByPid, TestSize.Level1)
910{
911    CALL_TEST_DEBUG;
912    UDSServer udsServer;
913    int32_t fd = 150;
914    SessionPtr session = std::make_shared<UDSSession>(PROGRAM_NAME, MODULE_TYPE, UDS_FD, UDS_UID, UDS_PID);
915    udsServer.sessionsMap_.insert(std::make_pair(fd, session));
916    udsServer.idxPidMap_.insert(std::make_pair(UDS_PID, fd));
917    ASSERT_EQ(udsServer.GetSessionByPid(UDS_PID), session);
918}
919
920/**
921 * @tc.name: UDSServerTest_AddSession
922 * @tc.desc: Test Add Session
923 * @tc.type: FUNC
924 * @tc.require:
925 */
926HWTEST_F(UDSServerTest, UDSServerTest_AddSession, TestSize.Level1)
927{
928    CALL_TEST_DEBUG;
929    UDSServer udsServer;
930    int32_t udsSessionFd = 100;
931    int32_t udsSessionPid = -1;
932    SessionPtr session = std::make_shared<UDSSession>(PROGRAM_NAME, MODULE_TYPE, udsSessionFd, UDS_UID, udsSessionPid);
933    ASSERT_FALSE(udsServer.AddSession(session));
934
935    udsSessionPid = 1000;
936    SessionPtr sess = std::make_shared<UDSSession>(PROGRAM_NAME, MODULE_TYPE, udsSessionFd, UDS_UID, udsSessionPid);
937    udsServer.sessionsMap_.insert(std::make_pair(udsSessionFd, sess));
938    ASSERT_TRUE(udsServer.AddSession(sess));
939    SessionPtr sessPtr = std::make_shared<UDSSession>(PROGRAM_NAME, MODULE_TYPE, udsSessionFd, UDS_UID, udsSessionPid);
940    for (int32_t i = 0; i < MAX_SESSON_ALARM + 1; ++i) {
941        udsServer.sessionsMap_.insert(std::make_pair(i, sessPtr));
942    }
943    ASSERT_TRUE(udsServer.AddSession(sessPtr));
944}
945
946/**
947 * @tc.name: UDSServerTest_DelSession
948 * @tc.desc: Test Delete Session
949 * @tc.type: FUNC
950 * @tc.require:
951 */
952HWTEST_F(UDSServerTest, UDSServerTest_DelSession, TestSize.Level1)
953{
954    CALL_TEST_DEBUG;
955    UDSServer udsServer;
956    int32_t fd = 100;
957    SessionPtr session = std::make_shared<UDSSession>(PROGRAM_NAME, MODULE_TYPE, UDS_FD, UDS_UID, UDS_PID);
958    udsServer.sessionsMap_.insert(std::make_pair(fd, session));
959    udsServer.idxPidMap_.insert(std::make_pair(UDS_PID, fd));
960    ASSERT_NO_FATAL_FAILURE(udsServer.DelSession(fd));
961}
962} // namespace MMI
963} // namespace OHOS
964