1 /* 2 * Copyright (C) 2021 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 "dhcp_client_service_impl.h" 19 #include "dhcp_result_notify.h" 20 #include "mock_system_func.h" 21 #include "dhcp_func.h" 22 #include "securec.h" 23 24 using namespace testing::ext; 25 using namespace OHOS; 26 using namespace OHOS::Wifi; 27 28 class DhcpClientServiceTest : public testing::Test { 29 public: SetUpTestCase()30 static void SetUpTestCase(){} TearDownTestCase()31 static void TearDownTestCase(){} SetUp()32 virtual void SetUp() 33 { 34 printf("DhcpClientServiceTest SetUp()...\n"); 35 pClientService = std::make_unique<DhcpClientServiceImpl>(); 36 if (pClientService == nullptr) { 37 printf("DhcpClientServiceTest SetUp() make_unique DhcpClientServiceImpl failed\n"); 38 } 39 } TearDown()40 virtual void TearDown() 41 { 42 printf("DhcpClientServiceTest TearDown()...\n"); 43 if (pClientService != nullptr) { 44 pClientService.reset(nullptr); 45 } 46 } 47 public: 48 std::unique_ptr<DhcpClientServiceImpl> pClientService; 49 }; 50 51 HWTEST_F(DhcpClientServiceTest, DhcpClientService_Test2, TestSize.Level1) 52 { 53 ASSERT_TRUE(pClientService != nullptr); 54 55 MockSystemFunc::SetMockFlag(true); 56 57 EXPECT_CALL(MockSystemFunc::GetInstance(), vfork()) 58 .WillOnce(Return(-1)).WillOnce(Return(1)) 59 .WillOnce(Return(-1)).WillOnce(Return(1)) 60 .WillRepeatedly(Return(1)); 61 EXPECT_CALL(MockSystemFunc::GetInstance(), waitpid(_, _, _)).WillRepeatedly(Return(0)); 62 EXPECT_CALL(MockSystemFunc::GetInstance(), open(_, _)).WillRepeatedly(Return(1)); 63 EXPECT_CALL(MockSystemFunc::GetInstance(), close(_)).WillRepeatedly(Return(0)); 64 65 std::string ifname = "wlan0"; 66 std::string strFile4 = DHCP_WORK_DIR + ifname + DHCP_RESULT_FILETYPE; 67 std::string strData4 = "IP4 0 * * * * * * * * 0"; 68 ASSERT_TRUE(DhcpFunc::CreateFile(strFile4, strData4)); 69 bool bIpv6 = true; 70 EXPECT_EQ(DHCP_OPT_FAILED, pClientService->StartDhcpClient(ifname, bIpv6)); 71 EXPECT_EQ(DHCP_OPT_SUCCESS, pClientService->StartDhcpClient(ifname, bIpv6)); 72 EXPECT_EQ(DHCP_OPT_SUCCESS, pClientService->StartDhcpClient(ifname, bIpv6)); 73 74 DhcpResultNotify dhcpResultNotify; 75 EXPECT_EQ(DHCP_OPT_FAILED, pClientService->GetDhcpResult("", &dhcpResultNotify, 0)); 76 EXPECT_EQ(DHCP_OPT_FAILED, pClientService->GetDhcpResult(ifname, nullptr, 0)); 77 EXPECT_EQ(DHCP_OPT_SUCCESS, pClientService->GetDhcpResult(ifname, &dhcpResultNotify, 0)); 78 DhcpResultNotify dhcpResultNotify1; 79 EXPECT_EQ(DHCP_OPT_SUCCESS, pClientService->GetDhcpResult(ifname, &dhcpResultNotify1, 10)); 80 DhcpResultNotify dhcpResultNotify2; 81 EXPECT_EQ(DHCP_OPT_SUCCESS, pClientService->GetDhcpResult(ifname, &dhcpResultNotify2, 20)); 82 DhcpResultNotify dhcpResultNotify3; 83 EXPECT_EQ(DHCP_OPT_SUCCESS, pClientService->GetDhcpResult("wlan1", &dhcpResultNotify3, 30)); 84 85 sleep(DHCP_NUM_ONE); 86 87 EXPECT_EQ(DHCP_OPT_FAILED, pClientService->StopDhcpClient(ifname, true)); 88 EXPECT_EQ(DHCP_OPT_SUCCESS, pClientService->StopDhcpClient(ifname, true)); 89 ASSERT_TRUE(DhcpFunc::RemoveFile(strFile4)); 90 91 MockSystemFunc::SetMockFlag(false); 92 } 93 94 HWTEST_F(DhcpClientServiceTest, DhcpClientService_Test3, TestSize.Level1) 95 { 96 ASSERT_TRUE(pClientService != nullptr); 97 98 MockSystemFunc::SetMockFlag(true); 99 100 EXPECT_CALL(MockSystemFunc::GetInstance(), vfork()).WillRepeatedly(Return(1)); 101 EXPECT_CALL(MockSystemFunc::GetInstance(), waitpid(_, _, _)).WillRepeatedly(Return(0)); 102 EXPECT_CALL(MockSystemFunc::GetInstance(), kill(_, _)) 103 .WillOnce(Return(-1)).WillOnce(Return(0)) 104 .WillOnce(Return(-1)).WillOnce(Return(0)) 105 .WillRepeatedly(Return(0)); 106 EXPECT_CALL(MockSystemFunc::GetInstance(), open(_, _)).WillRepeatedly(Return(1)); 107 EXPECT_CALL(MockSystemFunc::GetInstance(), close(_)).WillRepeatedly(Return(0)); 108 109 std::string ifname = "wlan0"; 110 EXPECT_EQ(0, pClientService->GetDhcpClientProPid("")); 111 EXPECT_EQ(0, pClientService->GetDhcpClientProPid(ifname)); 112 113 std::string strFile4 = DHCP_WORK_DIR + ifname + DHCP_RESULT_FILETYPE; 114 std::string strData4 = "IP4 0 * * * * * * * * 0"; 115 ASSERT_TRUE(DhcpFunc::CreateFile(strFile4, strData4)); 116 bool bIpv6 = true; 117 EXPECT_EQ(DHCP_OPT_SUCCESS, pClientService->StopDhcpClient(ifname, bIpv6)); 118 EXPECT_EQ(DHCP_OPT_SUCCESS, pClientService->StartDhcpClient(ifname, bIpv6)); 119 120 EXPECT_EQ(DHCP_OPT_FAILED, pClientService->RenewDhcpClient(ifname)); 121 EXPECT_EQ(DHCP_OPT_SUCCESS, pClientService->RenewDhcpClient(ifname)); 122 EXPECT_EQ(DHCP_OPT_FAILED, pClientService->ReleaseDhcpClient(ifname)); 123 EXPECT_EQ(DHCP_OPT_SUCCESS, pClientService->ReleaseDhcpClient(ifname)); 124 125 EXPECT_EQ(DHCP_OPT_SUCCESS, pClientService->StopDhcpClient(ifname, bIpv6)); 126 ASSERT_TRUE(DhcpFunc::RemoveFile(strFile4)); 127 128 EXPECT_EQ(DHCP_OPT_SUCCESS, pClientService->StartDhcpClient("wlan1", false)); 129 130 MockSystemFunc::SetMockFlag(false); 131 } 132