1 /* 2 * Copyright (C) 2024 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 #include <gtest/gtest.h> 16 #include "dhcp_logger.h" 17 #include "dhcp_arp_checker.h" 18 #include "mock_system_func.h" 19 #include "securec.h" 20 21 DEFINE_DHCPLOG_DHCP_LABEL("DhcpArpCheckerTest"); 22 using namespace testing::ext; 23 using namespace OHOS::DHCP; 24 25 constexpr int32_t MAC_ADDR_LEN = 6; 26 constexpr int32_t TIMEOUT = 50; 27 28 namespace OHOS { 29 class DhcpArpCheckerTest : public testing::Test { 30 public: SetUpTestCase()31 static void SetUpTestCase() 32 {} TearDownTestCase()33 static void TearDownTestCase() 34 {} SetUp()35 virtual void SetUp() 36 {} TearDown()37 virtual void TearDown() 38 {} 39 }; 40 /** 41 * @tc.name: DoArpTest_SUCCESS 42 * @tc.desc: DoArpTest 43 * @tc.type: FUNC 44 * @tc.require: issue 45 */ 46 HWTEST_F(DhcpArpCheckerTest, DoArpTest_SUCCESS, TestSize.Level1) 47 { 48 DHCP_LOGE("enter DoArpTest_SUCCESS"); 49 DhcpArpChecker dhcpArpChecker; 50 std::string ifName = "wlantest"; 51 std::string ifaceMac = "11:22:33:44:55:66"; 52 std::string ipAddr = "0.0.0.1"; 53 int32_t timeoutMillis = TIMEOUT; 54 uint64_t timeCost = 0; 55 std::string senderIp = "0.0.0.2"; 56 EXPECT_FALSE(dhcpArpChecker.Start(ifName, ifaceMac, senderIp, ipAddr)); 57 EXPECT_FALSE(dhcpArpChecker.DoArpCheck(timeoutMillis, false, timeCost)); 58 ifName = "wlan0"; 59 dhcpArpChecker.Start(ifName, ifaceMac, senderIp, ipAddr); 60 dhcpArpChecker.DoArpCheck(timeoutMillis, false, timeCost); 61 } 62 63 /** 64 * @tc.name: CreateSocketTest_001 65 * @tc.desc: CreateSocketTest 66 * @tc.type: FUNC 67 * @tc.require: issue 68 */ 69 HWTEST_F(DhcpArpCheckerTest, CreateSocketTest_001, TestSize.Level1) 70 { 71 DHCP_LOGE("enter CreateSocketTest_001"); 72 DhcpArpChecker dhcpArpChecker; 73 std::string ifName = ""; 74 uint16_t protocol = ETH_P_ARP; 75 MockSystemFunc::SetMockFlag(true); 76 EXPECT_TRUE(dhcpArpChecker.CreateSocket(ifName.c_str(), protocol) == -1); 77 ifName = "wlantest"; 78 EXPECT_TRUE(dhcpArpChecker.CreateSocket(ifName.c_str(), protocol) == -1); 79 ifName = "wlan0"; 80 EXPECT_CALL(MockSystemFunc::GetInstance(), socket(_, _, _)).WillOnce(Return(-1)).WillRepeatedly(Return(-1)); 81 EXPECT_TRUE(dhcpArpChecker.CreateSocket(ifName.c_str(), protocol) == -1); 82 MockSystemFunc::SetMockFlag(false); 83 } 84 85 /** 86 * @tc.name: CreateSocketTest_002 87 * @tc.desc: CreateSocketTest 88 * @tc.type: FUNC 89 * @tc.require: issue 90 */ 91 HWTEST_F(DhcpArpCheckerTest, CreateSocketTest_002, TestSize.Level1) 92 { 93 DHCP_LOGE("enter CreateSocketTest_002"); 94 DhcpArpChecker dhcpArpChecker; 95 std::string ifName = "wlan0"; 96 uint16_t protocol = ETH_P_ARP; 97 MockSystemFunc::SetMockFlag(true); 98 EXPECT_CALL(MockSystemFunc::GetInstance(), socket(_, _, _)).WillOnce(Return(1)); 99 EXPECT_CALL(MockSystemFunc::GetInstance(), bind(_, _, _)).WillRepeatedly(Return(0)); 100 EXPECT_TRUE(dhcpArpChecker.CreateSocket(ifName.c_str(), protocol) == 0); 101 MockSystemFunc::SetMockFlag(false); 102 } 103 104 /** 105 * @tc.name: StopTest_001 106 * @tc.desc: StopTest 107 * @tc.type: FUNC 108 * @tc.require: issue 109 */ 110 HWTEST_F(DhcpArpCheckerTest, StopTest_001, TestSize.Level1) 111 { 112 DHCP_LOGE("enter StopTest_001"); 113 DhcpArpChecker dhcpArpChecker; 114 dhcpArpChecker.m_isSocketCreated = false; 115 dhcpArpChecker.Stop(); 116 dhcpArpChecker.m_isSocketCreated = true; 117 dhcpArpChecker.Stop(); 118 } 119 120 /** 121 * @tc.name: SendDataTest_001 122 * @tc.desc: SendDataTest 123 * @tc.type: FUNC 124 * @tc.require: issue 125 */ 126 HWTEST_F(DhcpArpCheckerTest, SendDataTest_001, TestSize.Level1) 127 { 128 DHCP_LOGE("enter SendDataTest_001"); 129 DhcpArpChecker dhcpArpChecker; 130 uint8_t *buff = nullptr; 131 int32_t count = 1; 132 uint8_t *destHwaddr = nullptr; 133 dhcpArpChecker.m_socketFd = -1; 134 dhcpArpChecker.m_ifaceIndex = 0; 135 EXPECT_TRUE(dhcpArpChecker.SendData(buff, count, destHwaddr) == -1); 136 uint8_t bufferTest = 1; 137 buff = &bufferTest; 138 EXPECT_TRUE(dhcpArpChecker.SendData(buff, count, destHwaddr) == -1); 139 destHwaddr = &bufferTest; 140 EXPECT_TRUE(dhcpArpChecker.SendData(buff, count, destHwaddr) == -1); 141 dhcpArpChecker.m_socketFd = 1; 142 EXPECT_TRUE(dhcpArpChecker.SendData(buff, count, destHwaddr) == -1); 143 dhcpArpChecker.m_ifaceIndex = 1; 144 dhcpArpChecker.SendData(buff, count, destHwaddr); 145 } 146 147 /** 148 * @tc.name: RecvDataTest_001 149 * @tc.desc: RecvDataTest 150 * @tc.type: FUNC 151 * @tc.require: issue 152 */ 153 HWTEST_F(DhcpArpCheckerTest, RecvDataTest_001, TestSize.Level1) 154 { 155 DHCP_LOGE("enter RecvDataTest_001"); 156 DhcpArpChecker dhcpArpChecker; 157 158 uint8_t buff[MAC_ADDR_LEN] = {0}; 159 dhcpArpChecker.m_socketFd = -1; 160 EXPECT_TRUE(dhcpArpChecker.RecvData(buff, 1, 1) == -1); 161 dhcpArpChecker.m_socketFd = 1; 162 dhcpArpChecker.RecvData(buff, 1, 1); 163 } 164 } // namespace OHOS