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: OnConnected_001 300 * @tc.desc: Test the function OnConnected 301 * @tc.type: FUNC 302 * @tc.require: 303 */ 304 HWTEST_F(UDSServerTest, OnConnected_001, TestSize.Level1) 305 { 306 CALL_TEST_DEBUG; 307 UDSServer udsServer; 308 SessionPtr sess; 309 int32_t tokenType = TokenType::TOKEN_SHELL; 310 int32_t serverFd = 1; 311 const std::string programName = "program"; 312 const int32_t moduleType = 1; 313 const int32_t uid = 2; 314 const int32_t pid = 10; 315 int32_t toReturnClientFd = 1; 316 317 udsServer.AddSocketPairInfo(programName, moduleType, uid, pid, serverFd, toReturnClientFd, tokenType); 318 udsServer.OnConnected(sess); 319 } 320 321 /** 322 * @tc.name: SetRecvFun_001 323 * @tc.desc: Test the function SetRecvFun 324 * @tc.type: FUNC 325 * @tc.require: 326 */ 327 HWTEST_F(UDSServerTest, SetRecvFun_001, TestSize.Level1) 328 { 329 CALL_TEST_DEBUG; 330 UDSServer udsServer; 331 MsgServerFunCallback fun{ nullptr }; 332 int32_t tokenType = TokenType::TOKEN_SHELL; 333 int32_t serverFd = 1; 334 const std::string programName = "program"; 335 const int32_t moduleType = 1; 336 const int32_t uid = 2; 337 const int32_t pid = 10; 338 int32_t toReturnClientFd = 1; 339 340 udsServer.AddSocketPairInfo(programName, moduleType, uid, pid, serverFd, toReturnClientFd, tokenType); 341 udsServer.SetRecvFun(fun); 342 } 343 344 /** 345 * @tc.name: OnEpollRecv_001 346 * @tc.desc: Test the function OnEpollRecv 347 * @tc.type: FUNC 348 * @tc.require: 349 */ 350 HWTEST_F(UDSServerTest, OnEpollRecv_001, TestSize.Level1) 351 { 352 CALL_TEST_DEBUG; 353 UDSServer udsServer; 354 int32_t size = 100; 355 epoll_event ev; 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 int32_t fd = epoll_create(size); 366 udsServer.OnEpollRecv(fd, ev); 367 } 368 369 /** 370 * @tc.name: OnEpollRecv_002 371 * @tc.desc: Test the function OnEpollRecv 372 * @tc.type: FUNC 373 * @tc.require: 374 */ 375 HWTEST_F(UDSServerTest, OnEpollRecv_002, TestSize.Level1) 376 { 377 CALL_TEST_DEBUG; 378 UDSServer udsServer; 379 epoll_event ev; 380 int32_t fd = -1; 381 int32_t tokenType = TokenType::TOKEN_SHELL; 382 int32_t serverFd = 1; 383 const std::string programName = "program"; 384 const int32_t moduleType = 1; 385 const int32_t uid = 2; 386 const int32_t pid = 10; 387 int32_t toReturnClientFd = 1; 388 389 udsServer.AddSocketPairInfo(programName, moduleType, uid, pid, serverFd, toReturnClientFd, tokenType); 390 udsServer.OnEpollRecv(fd, ev); 391 } 392 393 /** 394 * @tc.name: AddEpollEvent_001 395 * @tc.desc: Test the function AddEpollEvent 396 * @tc.type: FUNC 397 * @tc.require: 398 */ 399 HWTEST_F(UDSServerTest, AddEpollEvent_001, TestSize.Level1) 400 { 401 CALL_TEST_DEBUG; 402 UDSServer udsServer; 403 std::shared_ptr<mmi_epoll_event> epollEvent=std::make_shared<mmi_epoll_event>(); 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.AddEpollEvent(fd, epollEvent); 415 } 416 417 /** 418 * @tc.name: DumpSession_001 419 * @tc.desc: Test the function DumpSession 420 * @tc.type: FUNC 421 * @tc.require: 422 */ 423 HWTEST_F(UDSServerTest, DumpSession_001, TestSize.Level1) 424 { 425 CALL_TEST_DEBUG; 426 UDSServer udsServer; 427 const std::string title = "test_title"; 428 int32_t tokenType = TokenType::TOKEN_SHELL; 429 int32_t serverFd = 1; 430 const std::string programName = "program"; 431 const int32_t moduleType = 1; 432 const int32_t uid = 2; 433 const int32_t pid = 10; 434 int32_t toReturnClientFd = 1; 435 436 udsServer.AddSocketPairInfo(programName, moduleType, uid, pid, serverFd, toReturnClientFd, tokenType); 437 udsServer.DumpSession(title); 438 } 439 440 /** 441 * @tc.name: AddSession_001 442 * @tc.desc: Test the function AddSession 443 * @tc.type: FUNC 444 * @tc.require: 445 */ 446 HWTEST_F(UDSServerTest, AddSession_001, TestSize.Level1) 447 { 448 CALL_TEST_DEBUG; 449 UDSServer udsServer; 450 SessionPtr sess = nullptr; 451 int32_t tokenType = TokenType::TOKEN_SHELL; 452 int32_t serverFd = 1; 453 const std::string programName = "program"; 454 const int32_t moduleType = 1; 455 const int32_t uid = 2; 456 const int32_t pid = 10; 457 int32_t toReturnClientFd = 1; 458 459 udsServer.AddSocketPairInfo(programName, moduleType, uid, pid, serverFd, toReturnClientFd, tokenType); 460 sess = std::make_shared<UDSSession>(programName, moduleType, serverFd, uid, pid); 461 udsServer.AddSession(sess); 462 } 463 464 /** 465 * @tc.name: DelSession_001 466 * @tc.desc: Test the function DelSession 467 * @tc.type: FUNC 468 * @tc.require: 469 */ 470 HWTEST_F(UDSServerTest, DelSession_001, TestSize.Level1) 471 { 472 CALL_TEST_DEBUG; 473 UDSServer udsServer; 474 int32_t fd = -1; 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 udsServer.DelSession(fd); 485 } 486 487 /** 488 * @tc.name: DelSession_002 489 * @tc.desc: Test the function DelSession 490 * @tc.type: FUNC 491 * @tc.require: 492 */ 493 HWTEST_F(UDSServerTest, DelSession_002, TestSize.Level1) 494 { 495 CALL_TEST_DEBUG; 496 UDSServer udsServer; 497 int32_t fd = 1; 498 int32_t tokenType = TokenType::TOKEN_SHELL; 499 int32_t serverFd = 1; 500 const std::string programName = "program"; 501 const int32_t moduleType = 1; 502 const int32_t uid = 2; 503 const int32_t pid = 10; 504 int32_t toReturnClientFd = 1; 505 506 udsServer.AddSocketPairInfo(programName, moduleType, uid, pid, serverFd, toReturnClientFd, tokenType); 507 udsServer.DelSession(fd); 508 } 509 510 /** 511 * @tc.name: NotifySessionDeleted_001 512 * @tc.desc: Test the function NotifySessionDeleted 513 * @tc.type: FUNC 514 * @tc.require: 515 */ 516 HWTEST_F(UDSServerTest, NotifySessionDeleted_001, TestSize.Level1) 517 { 518 CALL_TEST_DEBUG; 519 UDSServer udsServer; 520 SessionPtr sess = nullptr; 521 int32_t tokenType = TokenType::TOKEN_SHELL; 522 int32_t serverFd = 1; 523 const std::string programName = "program"; 524 const int32_t moduleType = 1; 525 const int32_t uid = 2; 526 const int32_t pid = 10; 527 int32_t toReturnClientFd = 1; 528 529 udsServer.AddSocketPairInfo(programName, moduleType, uid, pid, serverFd, toReturnClientFd, tokenType); 530 sess = std::make_shared<UDSSession>(programName, moduleType, serverFd, uid, pid); 531 udsServer.NotifySessionDeleted(sess); 532 } 533 534 /** 535 * @tc.name: GetClientPid_002 536 * @tc.desc: Test the scenario of obtaining the client process ID 537 * @tc.type: FUNC 538 * @tc.require: 539 */ 540 HWTEST_F(UDSServerTest, GetClientPid_002, TestSize.Level1) 541 { 542 CALL_TEST_DEBUG; 543 UDSServer udsServer; 544 int32_t fd = 123; 545 auto ret = udsServer.GetClientPid(fd); 546 EXPECT_EQ(ret, RET_ERR); 547 } 548 549 /** 550 * @tc.name: AddSocketPairInfo_002 551 * @tc.desc: Test the scenario of adding socket pair information 552 * @tc.type: FUNC 553 * @tc.require: 554 */ 555 HWTEST_F(UDSServerTest, AddSocketPairInfo_002, TestSize.Level1) 556 { 557 CALL_TEST_DEBUG; 558 UDSServer udsServer; 559 std::string programName = "program"; 560 int32_t moduleType = 1; 561 int32_t uid = 2; 562 int32_t pid = 10; 563 int32_t serverFd = 123; 564 int32_t toReturnClientFd = 456; 565 int32_t tokenType = 1; 566 auto ret = udsServer.AddSocketPairInfo(programName, moduleType, uid, pid, serverFd, toReturnClientFd, tokenType); 567 if (serverFd != -1) { 568 close(serverFd); 569 } 570 if (toReturnClientFd != -1) { 571 close(toReturnClientFd); 572 } 573 EXPECT_EQ(ret, RET_ERR); 574 } 575 576 /** 577 * @tc.name: OnConnected_002 578 * @tc.desc: Test the OnConnected function of UDSServer 579 * @tc.type: FUNC 580 * @tc.require: 581 */ 582 HWTEST_F(UDSServerTest, OnConnected_002, TestSize.Level1) 583 { 584 CALL_TEST_DEBUG; 585 UDSServer udsServer; 586 SessionPtr sess = nullptr; 587 ASSERT_NO_FATAL_FAILURE(udsServer.OnConnected(sess)); 588 } 589 590 /** 591 * @tc.name: OnDisconnected_001 592 * @tc.desc: Test the OnDisconnected function of UDSServer 593 * @tc.type: FUNC 594 * @tc.require: 595 */ 596 HWTEST_F(UDSServerTest, OnDisconnected_001, TestSize.Level1) 597 { 598 CALL_TEST_DEBUG; 599 UDSServer udsServer; 600 SessionPtr sess = nullptr; 601 ASSERT_NO_FATAL_FAILURE(udsServer.OnConnected(sess)); 602 ASSERT_NO_FATAL_FAILURE(udsServer.OnDisconnected(sess)); 603 } 604 605 /** 606 * @tc.name: AddEpoll_001 607 * @tc.desc: Test the AddEpoll function of UDSServer 608 * @tc.type: FUNC 609 * @tc.require: 610 */ 611 HWTEST_F(UDSServerTest, AddEpoll_001, TestSize.Level1) 612 { 613 CALL_TEST_DEBUG; 614 UDSServer udsServer; 615 EpollEventType type = EPOLL_EVENT_BEGIN; 616 int32_t fd = 1; 617 ASSERT_NO_FATAL_FAILURE(udsServer.AddEpoll(type, fd)); 618 } 619 620 /** 621 * @tc.name: SetRecvFun_002 622 * @tc.desc: Test the SetRecvFun function of UDSServer 623 * @tc.type: FUNC 624 * @tc.require: 625 */ 626 HWTEST_F(UDSServerTest, SetRecvFun_002, TestSize.Level1) 627 { 628 CALL_TEST_DEBUG; 629 UDSServer udsServer; 630 MsgServerFunCallback fun{ nullptr }; 631 ASSERT_NO_FATAL_FAILURE(udsServer.SetRecvFun(fun)); 632 } 633 634 /** 635 * @tc.name: OnPacket_001 636 * @tc.desc: Test the OnPacket function of UDSServer 637 * @tc.type: FUNC 638 * @tc.require: 639 */ 640 HWTEST_F(UDSServerTest, OnPacket_001, TestSize.Level1) 641 { 642 CALL_TEST_DEBUG; 643 UDSServer udsServer; 644 MmiMessageId msgId = MmiMessageId::INVALID; 645 NetPacket pkt(msgId); 646 int32_t fd = 1; 647 ASSERT_NO_FATAL_FAILURE(udsServer.OnPacket(fd, pkt)); 648 } 649 650 /** 651 * @tc.name: OnEpollRecv_003 652 * @tc.desc: Test the OnEpollRecv function of UDSServer 653 * @tc.type: FUNC 654 * @tc.require: 655 */ 656 HWTEST_F(UDSServerTest, OnEpollRecv_003, TestSize.Level1) 657 { 658 CALL_TEST_DEBUG; 659 UDSServer udsServer; 660 int32_t fd = -1; 661 epoll_event ev; 662 ASSERT_NO_FATAL_FAILURE(udsServer.OnEpollRecv(fd, ev)); 663 } 664 665 /** 666 * @tc.name: OnEpollEvent_001 667 * @tc.desc: Test the OnEpollEvent function of UDSServer 668 * @tc.type: FUNC 669 * @tc.require: 670 */ 671 HWTEST_F(UDSServerTest, OnEpollEvent_001, TestSize.Level1) 672 { 673 CALL_TEST_DEBUG; 674 UDSServer udsServer; 675 epoll_event ev; 676 ASSERT_NO_FATAL_FAILURE(udsServer.OnEpollEvent(ev)); 677 } 678 679 /** 680 * @tc.name: AddEpollEvent_002 681 * @tc.desc: Test the AddEpollEvent function of UDSServer 682 * @tc.type: FUNC 683 * @tc.require: 684 */ 685 HWTEST_F(UDSServerTest, AddEpollEvent_002, TestSize.Level1) 686 { 687 CALL_TEST_DEBUG; 688 UDSServer udsServer; 689 int32_t fd = 1; 690 std::shared_ptr<mmi_epoll_event> epollEvent=std::make_shared<mmi_epoll_event>(); 691 ASSERT_NO_FATAL_FAILURE(udsServer.AddEpollEvent(fd, epollEvent)); 692 } 693 694 /** 695 * @tc.name: RemoveEpollEvent_001 696 * @tc.desc: Test the RemoveEpollEvent function of UDSServer 697 * @tc.type: FUNC 698 * @tc.require: 699 */ 700 HWTEST_F(UDSServerTest, RemoveEpollEvent_001, TestSize.Level1) 701 { 702 CALL_TEST_DEBUG; 703 UDSServer udsServer; 704 int32_t fd = 1; 705 ASSERT_NO_FATAL_FAILURE(udsServer.RemoveEpollEvent(fd)); 706 } 707 708 /** 709 * @tc.name: DumpSession_002 710 * @tc.desc: The DumpSession function of UDSServer properly outputs session information 711 * @tc.type: FUNC 712 * @tc.require: 713 */ 714 HWTEST_F(UDSServerTest, DumpSession_002, TestSize.Level1) 715 { 716 CALL_TEST_DEBUG; 717 UDSServer udsServer; 718 std::string title = "test_title"; 719 ASSERT_NO_FATAL_FAILURE(udsServer.DumpSession(title)); 720 } 721 722 /** 723 * @tc.name: AddSession_002 724 * @tc.desc: The AddSession function of UDSServer properly adds a session 725 * @tc.type: FUNC 726 * @tc.require: 727 */ 728 HWTEST_F(UDSServerTest, AddSession_002, TestSize.Level1) 729 { 730 CALL_TEST_DEBUG; 731 UDSServer udsServer; 732 SessionPtr sess = nullptr; 733 bool ret = udsServer.AddSession(sess); 734 EXPECT_FALSE(ret); 735 } 736 737 /** 738 * @tc.name: DelSession_003 739 * @tc.desc: The DelSession function of UDSServer properly deletes a session 740 * @tc.type: FUNC 741 * @tc.require: 742 */ 743 HWTEST_F(UDSServerTest, DelSession_003, TestSize.Level1) 744 { 745 CALL_TEST_DEBUG; 746 UDSServer udsServer; 747 int32_t fd = -1; 748 ASSERT_NO_FATAL_FAILURE(udsServer.DelSession(fd)); 749 int32_t fds = 1; 750 ASSERT_NO_FATAL_FAILURE(udsServer.DelSession(fds)); 751 } 752 753 /** 754 * @tc.name: NotifySessionDeleted_002 755 * @tc.desc: Test the NotifySessionDeleted function of UDSServer 756 * @tc.type: FUNC 757 * @tc.require: 758 */ 759 HWTEST_F(UDSServerTest, NotifySessionDeleted_002, TestSize.Level1) 760 { 761 CALL_TEST_DEBUG; 762 UDSServer udsServer; 763 SessionPtr ses = nullptr; 764 ASSERT_NO_FATAL_FAILURE(udsServer.NotifySessionDeleted(ses)); 765 } 766 767 /** 768 * @tc.name: UDSServerTest_GetClientFd 769 * @tc.desc: Test Get Client Fd 770 * @tc.type: FUNC 771 * @tc.require: 772 */ 773 HWTEST_F(UDSServerTest, UDSServerTest_GetClientFd, TestSize.Level1) 774 { 775 CALL_TEST_DEBUG; 776 UDSServer udsServer; 777 int32_t pid = 1000; 778 int32_t fd = 150; 779 udsServer.idxPidMap_.insert(std::make_pair(pid, fd)); 780 ASSERT_EQ(udsServer.GetClientFd(pid), fd); 781 } 782 783 /** 784 * @tc.name: UDSServerTest_GetClientPid 785 * @tc.desc: Test Get Client Pid 786 * @tc.type: FUNC 787 * @tc.require: 788 */ 789 HWTEST_F(UDSServerTest, UDSServerTest_GetClientPid, TestSize.Level1) 790 { 791 CALL_TEST_DEBUG; 792 UDSServer udsServer; 793 int32_t fd = 150; 794 SessionPtr session = std::make_shared<UDSSession>(PROGRAM_NAME, MODULE_TYPE, UDS_FD, UDS_UID, UDS_PID); 795 udsServer.sessionsMap_.insert(std::make_pair(fd, session)); 796 ASSERT_EQ(udsServer.GetClientPid(fd), UDS_PID); 797 } 798 799 /** 800 * @tc.name: UDSServerTest_SendMsg 801 * @tc.desc: Test Send Msg 802 * @tc.type: FUNC 803 * @tc.require: 804 */ 805 HWTEST_F(UDSServerTest, UDSServerTest_SendMsg, TestSize.Level1) 806 { 807 CALL_TEST_DEBUG; 808 UDSServer udsServer; 809 int32_t fd = 150; 810 MmiMessageId msgId = MmiMessageId::INVALID; 811 NetPacket pkt(msgId); 812 SessionPtr session = std::make_shared<UDSSession>(PROGRAM_NAME, MODULE_TYPE, UDS_FD, UDS_UID, UDS_PID); 813 udsServer.sessionsMap_.insert(std::make_pair(fd, session)); 814 ASSERT_FALSE(udsServer.SendMsg(fd, pkt)); 815 } 816 817 /** 818 * @tc.name: UDSServerTest_GetSession 819 * @tc.desc: Test Get Session 820 * @tc.type: FUNC 821 * @tc.require: 822 */ 823 HWTEST_F(UDSServerTest, UDSServerTest_GetSession, TestSize.Level1) 824 { 825 CALL_TEST_DEBUG; 826 UDSServer udsServer; 827 int32_t fd = 150; 828 SessionPtr session = std::make_shared<UDSSession>(PROGRAM_NAME, MODULE_TYPE, UDS_FD, UDS_UID, UDS_PID); 829 udsServer.sessionsMap_.insert(std::make_pair(fd, session)); 830 ASSERT_EQ(udsServer.GetSession(fd), session); 831 } 832 833 /** 834 * @tc.name: UDSServerTest_GetSessionByPid 835 * @tc.desc: Test Get Session By Pid 836 * @tc.type: FUNC 837 * @tc.require: 838 */ 839 HWTEST_F(UDSServerTest, UDSServerTest_GetSessionByPid, TestSize.Level1) 840 { 841 CALL_TEST_DEBUG; 842 UDSServer udsServer; 843 int32_t fd = 150; 844 SessionPtr session = std::make_shared<UDSSession>(PROGRAM_NAME, MODULE_TYPE, UDS_FD, UDS_UID, UDS_PID); 845 udsServer.sessionsMap_.insert(std::make_pair(fd, session)); 846 udsServer.idxPidMap_.insert(std::make_pair(UDS_PID, fd)); 847 ASSERT_EQ(udsServer.GetSessionByPid(UDS_PID), session); 848 } 849 850 /** 851 * @tc.name: UDSServerTest_DelSession 852 * @tc.desc: Test Delete Session 853 * @tc.type: FUNC 854 * @tc.require: 855 */ 856 HWTEST_F(UDSServerTest, UDSServerTest_DelSession, TestSize.Level1) 857 { 858 CALL_TEST_DEBUG; 859 UDSServer udsServer; 860 int32_t fd = 100; 861 SessionPtr session = std::make_shared<UDSSession>(PROGRAM_NAME, MODULE_TYPE, UDS_FD, UDS_UID, UDS_PID); 862 udsServer.sessionsMap_.insert(std::make_pair(fd, session)); 863 udsServer.idxPidMap_.insert(std::make_pair(UDS_PID, fd)); 864 ASSERT_NO_FATAL_FAILURE(udsServer.DelSession(fd)); 865 } 866 867 /** 868 * @tc.name: UdsStop_002 869 * @tc.desc: Test the function UdsStop 870 * @tc.type: FUNC 871 * @tc.require: 872 */ 873 HWTEST_F(UDSServerTest, UdsStop_002, TestSize.Level1) 874 { 875 CALL_TEST_DEBUG; 876 UDSServer udsServer; 877 udsServer.epollFd_ = 2; 878 ASSERT_NO_FATAL_FAILURE(udsServer.UdsStop()); 879 } 880 881 /** 882 * @tc.name: GetClientPid_003 883 * @tc.desc: Test the function GetClientPid 884 * @tc.type: FUNC 885 * @tc.require: 886 */ 887 HWTEST_F(UDSServerTest, GetClientPid_003, TestSize.Level1) 888 { 889 CALL_TEST_DEBUG; 890 UDSServer udsServer; 891 int32_t fd = 125; 892 int32_t ret = udsServer.GetClientPid(fd); 893 EXPECT_EQ(ret, INVALID_PID); 894 } 895 896 /** 897 * @tc.name: GetClientPid_004 898 * @tc.desc: Test the function GetClientPid 899 * @tc.type: FUNC 900 * @tc.require: 901 */ 902 HWTEST_F(UDSServerTest, GetClientPid_004, TestSize.Level1) 903 { 904 CALL_TEST_DEBUG; 905 UDSServer udsServer; 906 int32_t fd = 125; 907 SessionPtr session = std::make_shared<UDSSession>(PROGRAM_NAME, MODULE_TYPE, UDS_FD, UDS_UID, UDS_PID); 908 udsServer.sessionsMap_.insert(std::make_pair(fd, session)); 909 int32_t ret = udsServer.GetClientPid(fd); 910 EXPECT_EQ(ret, 100); 911 } 912 913 /** 914 * @tc.name: SendMsg_004 915 * @tc.desc: Test the function SendMsg 916 * @tc.type: FUNC 917 * @tc.require: 918 */ 919 HWTEST_F(UDSServerTest, SendMsg_004, TestSize.Level1) 920 { 921 CALL_TEST_DEBUG; 922 UDSServer udsServer; 923 int32_t fd = -10; 924 MmiMessageId msgId = MmiMessageId::INVALID; 925 NetPacket pkt(msgId); 926 bool ret = udsServer.SendMsg(fd, pkt); 927 ASSERT_FALSE(ret); 928 } 929 930 /** 931 * @tc.name: SendMsg_005 932 * @tc.desc: Test the function SendMsg 933 * @tc.type: FUNC 934 * @tc.require: 935 */ 936 HWTEST_F(UDSServerTest, SendMsg_005, TestSize.Level1) 937 { 938 CALL_TEST_DEBUG; 939 UDSServer udsServer; 940 int32_t fd = 10; 941 MmiMessageId msgId = MmiMessageId::INVALID; 942 NetPacket pkt(msgId); 943 SessionPtr session = std::make_shared<UDSSession>(PROGRAM_NAME, MODULE_TYPE, UDS_FD, UDS_UID, UDS_PID); 944 udsServer.sessionsMap_.insert(std::make_pair(fd, session)); 945 bool ret = udsServer.SendMsg(fd, pkt); 946 ASSERT_FALSE(ret); 947 } 948 949 /** 950 * @tc.name: ReleaseSession_001 951 * @tc.desc: Test the function ReleaseSession 952 * @tc.type: FUNC 953 * @tc.require: 954 */ 955 HWTEST_F(UDSServerTest, ReleaseSession_001, TestSize.Level1) 956 { 957 CALL_TEST_DEBUG; 958 UDSServer udsServer; 959 int32_t fd = 10; 960 epoll_event ev; 961 ASSERT_NO_FATAL_FAILURE(udsServer.ReleaseSession(fd, ev)); 962 } 963 964 /** 965 * @tc.name: ReleaseSession_002 966 * @tc.desc: Test the function ReleaseSession 967 * @tc.type: FUNC 968 * @tc.require: 969 */ 970 HWTEST_F(UDSServerTest, ReleaseSession_002, TestSize.Level1) 971 { 972 CALL_TEST_DEBUG; 973 UDSServer udsServer; 974 int32_t fd = 10; 975 epoll_event ev; 976 SessionPtr session = std::make_shared<UDSSession>(PROGRAM_NAME, MODULE_TYPE, UDS_FD, UDS_UID, UDS_PID); 977 udsServer.sessionsMap_.insert(std::make_pair(fd, session)); 978 ev.data.ptr = nullptr; 979 ASSERT_NO_FATAL_FAILURE(udsServer.ReleaseSession(fd, ev)); 980 } 981 struct device_status_epoll_event { 982 int32_t fd { -1 }; 983 EpollEventType event_type { EPOLL_EVENT_BEGIN }; 984 }; 985 986 /** 987 * @tc.name: ReleaseSession_003 988 * @tc.desc: Test the function ReleaseSession 989 * @tc.type: FUNC 990 * @tc.require: 991 */ 992 HWTEST_F(UDSServerTest, ReleaseSession_003, TestSize.Level1) 993 { 994 CALL_TEST_DEBUG; 995 UDSServer udsServer; 996 int32_t fd = 10; 997 epoll_event ev; 998 SessionPtr session = std::make_shared<UDSSession>(PROGRAM_NAME, MODULE_TYPE, UDS_FD, UDS_UID, UDS_PID); 999 udsServer.sessionsMap_.insert(std::make_pair(fd, session)); 1000 auto eventData = static_cast<device_status_epoll_event*>(malloc(sizeof(device_status_epoll_event))); 1001 ASSERT_NE(eventData, nullptr); 1002 ev.data.ptr = eventData; 1003 ASSERT_NO_FATAL_FAILURE(udsServer.ReleaseSession(fd, ev)); 1004 free(eventData); 1005 eventData = nullptr; 1006 } 1007 1008 /** 1009 * @tc.name: OnEpollRecv_004 1010 * @tc.desc: Test the OnEpollRecv function of UDSServer 1011 * @tc.type: FUNC 1012 * @tc.require: 1013 */ 1014 HWTEST_F(UDSServerTest, OnEpollRecv_004, TestSize.Level1) 1015 { 1016 CALL_TEST_DEBUG; 1017 UDSServer udsServer; 1018 int32_t fd = -10; 1019 epoll_event ev; 1020 ASSERT_NO_FATAL_FAILURE(udsServer.OnEpollRecv(fd, ev)); 1021 } 1022 1023 /** 1024 * @tc.name: OnEpollRecv_005 1025 * @tc.desc: Test the OnEpollRecv function of UDSServer 1026 * @tc.type: FUNC 1027 * @tc.require: 1028 */ 1029 HWTEST_F(UDSServerTest, OnEpollRecv_005, TestSize.Level1) 1030 { 1031 CALL_TEST_DEBUG; 1032 UDSServer udsServer; 1033 int32_t fd = 10; 1034 epoll_event ev; 1035 ASSERT_NO_FATAL_FAILURE(udsServer.OnEpollRecv(fd, ev)); 1036 } 1037 1038 /** 1039 * @tc.name: OnEpollEvent_002 1040 * @tc.desc: Test the OnEpollEvent function of UDSServer 1041 * @tc.type: FUNC 1042 * @tc.require: 1043 */ 1044 HWTEST_F(UDSServerTest, OnEpollEvent_002, TestSize.Level1) 1045 { 1046 CALL_TEST_DEBUG; 1047 UDSServer udsServer; 1048 epoll_event ev; 1049 ev.data.fd = -10; 1050 ASSERT_NO_FATAL_FAILURE(udsServer.OnEpollEvent(ev)); 1051 } 1052 1053 /** 1054 * @tc.name: OnEpollEvent_003 1055 * @tc.desc: Test the OnEpollEvent function of UDSServer 1056 * @tc.type: FUNC 1057 * @tc.require: 1058 */ 1059 HWTEST_F(UDSServerTest, OnEpollEvent_003, TestSize.Level1) 1060 { 1061 CALL_TEST_DEBUG; 1062 UDSServer udsServer; 1063 epoll_event ev; 1064 ev.data.fd = 10; 1065 ASSERT_NO_FATAL_FAILURE(udsServer.OnEpollEvent(ev)); 1066 } 1067 1068 /** 1069 * @tc.name: EarseSessionByFd_001 1070 * @tc.desc: Test the EarseSessionByFd function of UDSServer 1071 * @tc.type: FUNC 1072 * @tc.require: 1073 */ 1074 HWTEST_F(UDSServerTest, EarseSessionByFd_001, TestSize.Level1) 1075 { 1076 CALL_TEST_DEBUG; 1077 UDSServer udsServer; 1078 int32_t fd = 10; 1079 ASSERT_NO_FATAL_FAILURE(udsServer.EarseSessionByFd(fd)); 1080 } 1081 1082 /** 1083 * @tc.name: EarseSessionByFd_002 1084 * @tc.desc: Test the EarseSessionByFd function of UDSServer 1085 * @tc.type: FUNC 1086 * @tc.require: 1087 */ 1088 HWTEST_F(UDSServerTest, EarseSessionByFd_002, TestSize.Level1) 1089 { 1090 CALL_TEST_DEBUG; 1091 UDSServer udsServer; 1092 int32_t fd = 10; 1093 SessionPtr session = std::make_shared<UDSSession>(PROGRAM_NAME, MODULE_TYPE, UDS_FD, UDS_UID, UDS_PID); 1094 udsServer.sessionsMap_.insert(std::make_pair(fd, session)); 1095 ASSERT_NO_FATAL_FAILURE(udsServer.EarseSessionByFd(fd)); 1096 } 1097 1098 /** 1099 * @tc.name: InsertSession_001 1100 * @tc.desc: Test the InsertSession function of UDSServer 1101 * @tc.type: FUNC 1102 * @tc.require: 1103 */ 1104 HWTEST_F(UDSServerTest, InsertSession_001, TestSize.Level1) 1105 { 1106 CALL_TEST_DEBUG; 1107 UDSServer udsServer; 1108 int32_t fd = -10; 1109 SessionPtr sp = nullptr; 1110 bool ret = udsServer.InsertSession(fd, sp); 1111 ASSERT_FALSE(ret); 1112 } 1113 1114 /** 1115 * @tc.name: InsertSession_002 1116 * @tc.desc: Test the InsertSession function of UDSServer 1117 * @tc.type: FUNC 1118 * @tc.require: 1119 */ 1120 HWTEST_F(UDSServerTest, InsertSession_002, TestSize.Level1) 1121 { 1122 CALL_TEST_DEBUG; 1123 UDSServer udsServer; 1124 int32_t fd = -10; 1125 SessionPtr sp = std::make_shared<UDSSession>(PROGRAM_NAME, MODULE_TYPE, UDS_FD, UDS_UID, UDS_PID); 1126 bool ret = udsServer.InsertSession(fd, sp); 1127 ASSERT_FALSE(ret); 1128 } 1129 1130 /** 1131 * @tc.name: InsertSession_003 1132 * @tc.desc: Test the InsertSession function of UDSServer 1133 * @tc.type: FUNC 1134 * @tc.require: 1135 */ 1136 HWTEST_F(UDSServerTest, InsertSession_003, TestSize.Level1) 1137 { 1138 CALL_TEST_DEBUG; 1139 UDSServer udsServer; 1140 int32_t fd = 10; 1141 SessionPtr sp = std::make_shared<UDSSession>(PROGRAM_NAME, MODULE_TYPE, UDS_FD, UDS_UID, UDS_PID); 1142 bool ret = udsServer.InsertSession(fd, sp); 1143 ASSERT_TRUE(ret); 1144 } 1145 1146 /** 1147 * @tc.name: InsertSession_004 1148 * @tc.desc: Test the InsertSession function of UDSServer 1149 * @tc.type: FUNC 1150 * @tc.require: 1151 */ 1152 HWTEST_F(UDSServerTest, InsertSession_004, TestSize.Level1) 1153 { 1154 CALL_TEST_DEBUG; 1155 UDSServer udsServer; 1156 int32_t fd = 10; 1157 SessionPtr sp = nullptr; 1158 bool ret = udsServer.InsertSession(fd, sp); 1159 ASSERT_FALSE(ret); 1160 } 1161 1162 /** 1163 * @tc.name: UDSServerTest_GetSessionByPid_001 1164 * @tc.desc: Test Get Session By Pid 1165 * @tc.type: FUNC 1166 * @tc.require: 1167 */ 1168 HWTEST_F(UDSServerTest, UDSServerTest_GetSessionByPid_001, TestSize.Level1) 1169 { 1170 CALL_TEST_DEBUG; 1171 UDSServer udsServer; 1172 int32_t pid = 150; 1173 udsServer.pid_ = 10; 1174 ASSERT_EQ(udsServer.GetSessionByPid(pid), nullptr); 1175 } 1176 1177 /** 1178 * @tc.name: AddSession_003 1179 * @tc.desc: The AddSession function of UDSServer properly adds a session 1180 * @tc.type: FUNC 1181 * @tc.require: 1182 */ 1183 HWTEST_F(UDSServerTest, AddSession_003, TestSize.Level1) 1184 { 1185 CALL_TEST_DEBUG; 1186 UDSServer udsServer; 1187 SessionPtr ses = std::make_shared<UDSSession>(PROGRAM_NAME, MODULE_TYPE, UDS_FD, UDS_UID, UDS_PID); 1188 ses->fd_ = -10; 1189 bool ret = udsServer.AddSession(ses); 1190 EXPECT_FALSE(ret); 1191 } 1192 1193 /** 1194 * @tc.name: AddSession_004 1195 * @tc.desc: The AddSession function of UDSServer properly adds a session 1196 * @tc.type: FUNC 1197 * @tc.require: 1198 */ 1199 HWTEST_F(UDSServerTest, AddSession_004, TestSize.Level1) 1200 { 1201 CALL_TEST_DEBUG; 1202 UDSServer udsServer; 1203 SessionPtr ses = std::make_shared<UDSSession>(PROGRAM_NAME, MODULE_TYPE, UDS_FD, UDS_UID, UDS_PID); 1204 ses->fd_ = 10; 1205 bool ret = udsServer.AddSession(ses); 1206 EXPECT_TRUE(ret); 1207 } 1208 1209 /** 1210 * @tc.name: DelSession_004 1211 * @tc.desc: The DelSession function of UDSServer properly deletes a session 1212 * @tc.type: FUNC 1213 * @tc.require: 1214 */ 1215 HWTEST_F(UDSServerTest, DelSession_004, TestSize.Level1) 1216 { 1217 CALL_TEST_DEBUG; 1218 UDSServer udsServer; 1219 int32_t fd = -10; 1220 ASSERT_NO_FATAL_FAILURE(udsServer.DelSession(fd)); 1221 int32_t fds = 10; 1222 ASSERT_NO_FATAL_FAILURE(udsServer.DelSession(fds)); 1223 SessionPtr session = std::make_shared<UDSSession>(PROGRAM_NAME, MODULE_TYPE, UDS_FD, UDS_UID, UDS_PID); 1224 udsServer.sessionsMap_.insert(std::make_pair(fd, session)); 1225 ASSERT_NO_FATAL_FAILURE(udsServer.DelSession(fds)); 1226 } 1227 1228 /** 1229 * @tc.name: UdsStop_003 1230 * @tc.desc: Test the function WhenEpollFdIsValid 1231 * @tc.type: FUNC 1232 * @tc.require: 1233 */ 1234 HWTEST_F(UDSServerTest, UdsStop_003, TestSize.Level1) 1235 { 1236 CALL_TEST_DEBUG; 1237 UDSServer udsServer; 1238 udsServer.epollFd_ = 2; 1239 udsServer.UdsStop(); 1240 EXPECT_EQ(udsServer.epollFd_, -1); 1241 } 1242 1243 /** 1244 * @tc.name: UdsStop_004 1245 * @tc.desc: Test the function WhenEpollFdIsInvalid 1246 * @tc.type: FUNC 1247 * @tc.require: 1248 */ 1249 HWTEST_F(UDSServerTest, UdsStop_004, TestSize.Level1) 1250 { 1251 CALL_TEST_DEBUG; 1252 UDSServer udsServer; 1253 udsServer.epollFd_ = -1; 1254 udsServer.UdsStop(); 1255 EXPECT_EQ(udsServer.epollFd_, -1); 1256 } 1257 1258 /** 1259 * @tc.name: UdsStop_005 1260 * @tc.desc: Test the function WhenSessionMapIsNotEmpty 1261 * @tc.type: FUNC 1262 * @tc.require: 1263 */ 1264 HWTEST_F(UDSServerTest, UdsStop_005, TestSize.Level1) 1265 { 1266 CALL_TEST_DEBUG; 1267 UDSServer udsServer; 1268 int32_t fd = 2; 1269 SessionPtr session = std::make_shared<UDSSession>(PROGRAM_NAME, MODULE_TYPE, UDS_FD, UDS_UID, UDS_PID); 1270 udsServer.sessionsMap_.insert(std::make_pair(fd, session)); 1271 udsServer.UdsStop(); 1272 EXPECT_TRUE(udsServer.GetSessionMapCopy().empty()); 1273 } 1274 1275 /** 1276 * @tc.name: UdsStop_006 1277 * @tc.desc: Test the function WhenSessionMapIsEmpty 1278 * @tc.type: FUNC 1279 * @tc.require: 1280 */ 1281 HWTEST_F(UDSServerTest, UdsStop_006, TestSize.Level1) 1282 { 1283 CALL_TEST_DEBUG; 1284 UDSServer udsServer; 1285 udsServer.ClearSessionMap(); 1286 udsServer.UdsStop(); 1287 EXPECT_TRUE(udsServer.GetSessionMapCopy().empty()); 1288 } 1289 1290 /** 1291 * @tc.name: SetFdProperty_003 1292 * @tc.desc: Test the function SetFdProperty_ShouldReturnRET_ERR_WhenSetServerFdSendBufferFails 1293 * @tc.type: FUNC 1294 * @tc.require: 1295 */ 1296 HWTEST_F(UDSServerTest, SetFdProperty_003, TestSize.Level1) 1297 { 1298 CALL_TEST_DEBUG; 1299 UDSServer udsServer; 1300 int32_t tokenType = 1; 1301 int32_t serverFd = -1; 1302 const std::string programName = "program"; 1303 int32_t toReturnClientFd = 1; 1304 bool readOnly = false; 1305 int32_t ret = udsServer.SetFdProperty(tokenType, serverFd, toReturnClientFd, programName, readOnly); 1306 EXPECT_EQ(ret, RET_ERR); 1307 } 1308 1309 /** 1310 * @tc.name: SetFdProperty_004 1311 * @tc.desc: Test the function SetFdProperty_WhenSetServerFdRecvBufferFails 1312 * @tc.type: FUNC 1313 * @tc.require: 1314 */ 1315 HWTEST_F(UDSServerTest, SetFdProperty_004, TestSize.Level1) 1316 { 1317 CALL_TEST_DEBUG; 1318 UDSServer udsServer; 1319 int32_t tokenType = 1; 1320 int32_t serverFd = 1; 1321 const std::string programName = "program"; 1322 int32_t toReturnClientFd = -1; 1323 bool readOnly = false; 1324 int32_t ret = udsServer.SetFdProperty(tokenType, serverFd, toReturnClientFd, programName, readOnly); 1325 EXPECT_EQ(ret, RET_ERR); 1326 } 1327 1328 /** 1329 * @tc.name: SetFdProperty_005 1330 * @tc.desc: Test the function SetFdProperty_WhenSetClientFdSendBufferFailsForNativeToken 1331 * @tc.type: FUNC 1332 * @tc.require: 1333 */ 1334 HWTEST_F(UDSServerTest, SetFdProperty_005, TestSize.Level1) 1335 { 1336 CALL_TEST_DEBUG; 1337 UDSServer udsServer; 1338 int32_t tokenType = TokenType::TOKEN_NATIVE; 1339 int32_t serverFd = -1; 1340 const std::string programName = "program"; 1341 int32_t toReturnClientFd = 1; 1342 bool readOnly = false; 1343 int32_t ret = udsServer.SetFdProperty(tokenType, serverFd, toReturnClientFd, programName, readOnly); 1344 EXPECT_EQ(ret, RET_ERR); 1345 } 1346 1347 /** 1348 * @tc.name: SetFdProperty_006 1349 * @tc.desc: Test the function SetFdProperty_WhenSetClientFdRecvBufferFailsForNativeToken 1350 * @tc.type: FUNC 1351 * @tc.require: 1352 */ 1353 HWTEST_F(UDSServerTest, SetFdProperty_006, TestSize.Level1) 1354 { 1355 CALL_TEST_DEBUG; 1356 UDSServer udsServer; 1357 int32_t tokenType = TokenType::TOKEN_NATIVE; 1358 int32_t serverFd = 1; 1359 const std::string programName = "program"; 1360 int32_t toReturnClientFd = -1; 1361 bool readOnly = false; 1362 int32_t ret = udsServer.SetFdProperty(tokenType, serverFd, toReturnClientFd, programName, readOnly); 1363 EXPECT_EQ(ret, RET_ERR); 1364 } 1365 1366 /** 1367 * @tc.name: SetFdProperty_007 1368 * @tc.desc: Test the function SetFdProperty_WhenProgramNameNotInWhitelist 1369 * @tc.type: FUNC 1370 * @tc.require: 1371 */ 1372 HWTEST_F(UDSServerTest, SetFdProperty_007, TestSize.Level1) 1373 { 1374 CALL_TEST_DEBUG; 1375 UDSServer udsServer; 1376 int32_t tokenType = 1; 1377 int32_t serverFd = 1; 1378 const std::string programName = "program"; 1379 int32_t toReturnClientFd = 1; 1380 bool readOnly = false; 1381 int32_t ret = udsServer.SetFdProperty(tokenType, serverFd, toReturnClientFd, programName, readOnly); 1382 EXPECT_EQ(ret, RET_ERR); 1383 EXPECT_FALSE(readOnly); 1384 } 1385 1386 /** 1387 * @tc.name: SetFdProperty_008 1388 * @tc.desc: Test the function SetFdProperty_WhenProgramNameIsInWhitelist 1389 * @tc.type: FUNC 1390 * @tc.require: 1391 */ 1392 HWTEST_F(UDSServerTest, SetFdProperty_008, TestSize.Level1) 1393 { 1394 CALL_TEST_DEBUG; 1395 UDSServer udsServer; 1396 int32_t tokenType = 1; 1397 int32_t serverFd = 1; 1398 const std::string programName = "com.ohos.sceneboard"; 1399 int32_t toReturnClientFd = 1; 1400 bool readOnly = false; 1401 int32_t ret = udsServer.SetFdProperty(tokenType, serverFd, toReturnClientFd, programName, readOnly); 1402 EXPECT_EQ(ret, RET_ERR); 1403 EXPECT_FALSE(readOnly); 1404 } 1405 1406 /** 1407 * @tc.name: Dump_001 1408 * @tc.desc: Test the function Dump_WhenSessionMapIsEmpty 1409 * @tc.type: FUNC 1410 * @tc.require: 1411 */ 1412 HWTEST_F(UDSServerTest, Dump_001, TestSize.Level1) 1413 { 1414 CALL_TEST_DEBUG; 1415 UDSServer udsServer; 1416 int32_t fd = 1; 1417 SessionPtr session = std::make_shared<UDSSession>(PROGRAM_NAME, MODULE_TYPE, UDS_FD, UDS_UID, UDS_PID); 1418 udsServer.sessionsMap_.insert(std::make_pair(fd, session)); 1419 std::vector<std::string> args; 1420 udsServer.GetSessionMapCopy(); 1421 ASSERT_NO_FATAL_FAILURE(udsServer.Dump(fd, args)); 1422 } 1423 1424 /** 1425 * @tc.name: Dump_002 1426 * @tc.desc: Test the function Dump_WhenSessionMapIsNotEmpty 1427 * @tc.type: FUNC 1428 * @tc.require: 1429 */ 1430 HWTEST_F(UDSServerTest, Dump_002, TestSize.Level1) 1431 { 1432 CALL_TEST_DEBUG; 1433 UDSServer udsServer; 1434 int32_t fd = 1; 1435 std::vector<std::string> args; 1436 std::map<int, std::shared_ptr<UDSSession>> sessionMap; 1437 SessionPtr session = std::make_shared<UDSSession>(PROGRAM_NAME, MODULE_TYPE, UDS_FD, UDS_UID, UDS_PID); 1438 int32_t tokenType =0; 1439 int32_t serverFd = 1; 1440 const std::string programName = "program"; 1441 const int32_t moduleType = 1; 1442 const int32_t uid = 2; 1443 const int32_t pid = 10; 1444 int32_t toReturnClientFd = 1; 1445 udsServer.AddSocketPairInfo(programName, moduleType, uid, pid, serverFd, toReturnClientFd, tokenType); 1446 1447 sessionMap[1] = session; 1448 udsServer.GetSessionMapCopy(); 1449 ASSERT_NO_FATAL_FAILURE(udsServer.Dump(fd, args)); 1450 } 1451 1452 /** 1453 * @tc.name: DumpSession_003 1454 * @tc.desc: Test the function DumpSession_WhenSessionMapNotEmpty 1455 * @tc.type: FUNC 1456 * @tc.require: 1457 */ 1458 HWTEST_F(UDSServerTest, DumpSession_003, TestSize.Level1) 1459 { 1460 CALL_TEST_DEBUG; 1461 UDSServer UDS_server; 1462 std::string title = "test_title"; 1463 int32_t fd = 2; 1464 SessionPtr session = std::make_shared<UDSSession>(PROGRAM_NAME, MODULE_TYPE, UDS_FD, UDS_UID, UDS_PID); 1465 UDS_server.sessionsMap_.insert(std::make_pair(fd, session)); 1466 ASSERT_NO_FATAL_FAILURE(UDS_server.DumpSession(title)); 1467 } 1468 1469 /** 1470 * @tc.name: DumpSession_005 1471 * @tc.desc: The DumpSession function WhenTitleIsEmpty 1472 * @tc.type: FUNC 1473 * @tc.require: 1474 */ 1475 HWTEST_F(UDSServerTest, DumpSession_005, TestSize.Level1) 1476 { 1477 CALL_TEST_DEBUG; 1478 UDSServer udsServer; 1479 std::string title = ""; 1480 ASSERT_NO_FATAL_FAILURE(udsServer.DumpSession(title)); 1481 } 1482 1483 /** 1484 * @tc.name: GetSessionMapCopy_001 1485 * @tc.desc: The GetSessionMapCopy function WhenSessionMapIsEmpty 1486 * @tc.type: FUNC 1487 * @tc.require: 1488 */ 1489 HWTEST_F(UDSServerTest, GetSessionMapCopy_001, TestSize.Level1) 1490 { 1491 CALL_TEST_DEBUG; 1492 UDSServer udsServer; 1493 auto result = udsServer.GetSessionMapCopy(); 1494 EXPECT_EQ(udsServer.GetSessionSize(), 0); 1495 } 1496 1497 /** 1498 * @tc.name: GetSessionMapCopy_002 1499 * @tc.desc: The GetSessionMapCopy function WhenSessionMapHasMultipleSessions 1500 * @tc.type: FUNC 1501 * @tc.require: 1502 */ 1503 HWTEST_F(UDSServerTest, GetSessionMapCopy_002, TestSize.Level1) 1504 { 1505 CALL_TEST_DEBUG; 1506 UDSServer udsServer; 1507 int32_t fd = 150; 1508 SessionPtr session = std::make_shared<UDSSession>(PROGRAM_NAME, MODULE_TYPE, UDS_FD, UDS_UID, UDS_PID); 1509 udsServer.sessionsMap_.insert(std::make_pair(fd, session)); 1510 auto result = udsServer.GetSessionMapCopy(); 1511 EXPECT_EQ(udsServer.GetSessionSize(), 1); 1512 } 1513 1514 /** 1515 * @tc.name: GetSessionMapCopy_003 1516 * @tc.desc: The GetSessionMapCopy function WhenSessionMapHasMultipleSessions 1517 * @tc.type: FUNC 1518 * @tc.require: 1519 */ 1520 HWTEST_F(UDSServerTest, GetSessionMapCopy_003, TestSize.Level1) 1521 { 1522 CALL_TEST_DEBUG; 1523 UDSServer udsServer; 1524 int32_t fd1 = 150; 1525 int32_t fd2 = 200; 1526 SessionPtr session1 = std::make_shared<UDSSession>(PROGRAM_NAME, MODULE_TYPE, UDS_FD, UDS_UID, UDS_PID); 1527 SessionPtr session2 = std::make_shared<UDSSession>(PROGRAM_NAME, MODULE_TYPE, UDS_FD, UDS_UID, UDS_PID); 1528 udsServer.sessionsMap_.insert(std::make_pair(fd1, session1)); 1529 udsServer.sessionsMap_.insert(std::make_pair(fd2, session2)); 1530 auto result = udsServer.GetSessionMapCopy(); 1531 udsServer.sessionsMap_[2] = session2; 1532 EXPECT_EQ(result.size(), 2); 1533 } 1534 1535 /** 1536 * @tc.name: UDSServerTest_GetClientFd_001 1537 * @tc.desc: Test Get InvalidFd_WhenPidNotExistsAndPidNotEqual 1538 * @tc.type: FUNC 1539 * @tc.require: 1540 */ 1541 HWTEST_F(UDSServerTest, UDSServerTest_GetClientFd_001, TestSize.Level1) 1542 { 1543 CALL_TEST_DEBUG; 1544 UDSServer udsServer; 1545 int32_t pid = 1000; 1546 int32_t fd = INVALID_FD; 1547 udsServer.idxPidMap_.insert(std::make_pair(pid, fd)); 1548 int result = udsServer.GetClientFd(pid); 1549 ASSERT_EQ(result, fd); 1550 } 1551 1552 /** 1553 * @tc.name: UDSServerTest_GetClientFd_002 1554 * @tc.desc: Test Get InvalidFd_WhenPidNotExistsAndPidEqual 1555 * @tc.type: FUNC 1556 * @tc.require: 1557 */ 1558 HWTEST_F(UDSServerTest, UDSServerTest_GetClientFd_002, TestSize.Level1) 1559 { 1560 CALL_TEST_DEBUG; 1561 UDSServer udsServer; 1562 int32_t pid = 1000; 1563 int32_t fd = INVALID_FD; 1564 udsServer.pid_ = 1000; 1565 udsServer.idxPidMap_.insert(std::make_pair(pid, fd)); 1566 int result = udsServer.GetClientFd(pid); 1567 ASSERT_EQ(result, fd); 1568 EXPECT_EQ(udsServer.pid_, 1000); 1569 } 1570 1571 /** 1572 * @tc.name: UDSServerTest_GetClientPid_001 1573 * @tc.desc: Test Get ShouldReturnInvalidPid_WhenSessionIsNull 1574 * @tc.type: FUNC 1575 * @tc.require: 1576 */ 1577 HWTEST_F(UDSServerTest, UDSServerTest_GetClientPid_001, TestSize.Level1) 1578 { 1579 CALL_TEST_DEBUG; 1580 UDSServer udsServer; 1581 int32_t fd = INVALID_FD; 1582 SessionPtr session = std::make_shared<UDSSession>(PROGRAM_NAME, MODULE_TYPE, UDS_FD, UDS_UID, UDS_PID); 1583 udsServer.sessionsMap_.insert(std::make_pair(fd, session)); 1584 ASSERT_EQ(udsServer.GetClientPid(fd), UDS_PID); 1585 } 1586 } // namespace MMI 1587 } // namespace OHOS 1588