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 "dhcp_func.h" 19 #include "mock_system_func.h" 20 #include "securec.h" 21 22 using namespace testing::ext; 23 using namespace OHOS; 24 using namespace OHOS::Wifi; 25 26 class DhcpFuncTest : public testing::Test { 27 public: SetUpTestCase()28 static void SetUpTestCase(){} TearDownTestCase()29 static void TearDownTestCase(){} SetUp()30 virtual void SetUp() 31 { 32 printf("DhcpFuncTest SetUp()...\n"); 33 } TearDown()34 virtual void TearDown() 35 { 36 printf("DhcpFuncTest TearDown()...\n"); 37 } 38 }; 39 40 HWTEST_F(DhcpFuncTest, Ip4StrConToInt_SUCCESS, TestSize.Level1) 41 { 42 std::string strIp = "192.77.1.232"; 43 uint32_t uSerIp = 0; 44 EXPECT_EQ(true, DhcpFunc::Ip4StrConToInt(strIp, uSerIp)); 45 printf("DhcpFuncTest Ip4StrConToInt_SUCCESS strIp:%s -> uSerIp:%u.\n", strIp.c_str(), uSerIp); 46 } 47 48 HWTEST_F(DhcpFuncTest, Ip4StrConToInt_FAILED, TestSize.Level1) 49 { 50 std::string strIp = "test4"; 51 uint32_t uSerIp = 0; 52 EXPECT_EQ(false, DhcpFunc::Ip4StrConToInt(strIp, uSerIp)); 53 54 strIp.clear(); 55 uSerIp = 0; 56 EXPECT_EQ(false, DhcpFunc::Ip4StrConToInt(strIp, uSerIp)); 57 } 58 59 HWTEST_F(DhcpFuncTest, Ip4IntConToStr_SUCCESS, TestSize.Level1) 60 { 61 uint32_t uSerIp = 3226272232; 62 std::string strIp = DhcpFunc::Ip4IntConToStr(uSerIp); 63 EXPECT_STRNE(strIp.c_str(), ""); 64 printf("DhcpFuncTest Ip4IntConToStr_SUCCESS uSerIp:%u -> strIp:%s.\n", uSerIp, strIp.c_str()); 65 } 66 67 HWTEST_F(DhcpFuncTest, Ip4IntConToStr_FAILED, TestSize.Level1) 68 { 69 uint32_t uSerIp = 0; 70 std::string strIp = DhcpFunc::Ip4IntConToStr(uSerIp); 71 EXPECT_STREQ(strIp.c_str(), "0.0.0.0"); 72 } 73 74 HWTEST_F(DhcpFuncTest, Ip6StrConToChar_SUCCESS, TestSize.Level1) 75 { 76 std::string strIp = "fe80::20c:29ff:fed7:fac8"; 77 uint8_t addr6[sizeof(struct in6_addr)] = {0}; 78 EXPECT_EQ(true, DhcpFunc::Ip6StrConToChar(strIp, addr6, sizeof(struct in6_addr))); 79 } 80 81 HWTEST_F(DhcpFuncTest, Ip6StrConToChar_FAILED, TestSize.Level1) 82 { 83 std::string strIp = "test6"; 84 uint8_t addr6[sizeof(struct in6_addr)] = {0}; 85 EXPECT_EQ(false, DhcpFunc::Ip6StrConToChar(strIp, addr6, sizeof(struct in6_addr))); 86 87 strIp.clear(); 88 EXPECT_EQ(false, DhcpFunc::Ip6StrConToChar(strIp, addr6, sizeof(struct in6_addr))); 89 } 90 91 HWTEST_F(DhcpFuncTest, Ip6CharConToStr_SUCCESS, TestSize.Level1) 92 { 93 uint8_t addr6[sizeof(struct in6_addr)] = {0}; 94 EXPECT_STREQ(DhcpFunc::Ip6CharConToStr(addr6, sizeof(struct in6_addr)).c_str(), ""); 95 } 96 97 HWTEST_F(DhcpFuncTest, Ip6CharConToStr_FAILED, TestSize.Level1) 98 { 99 uint8_t addr6[sizeof(struct in6_addr)] = {0}; 100 EXPECT_STREQ(DhcpFunc::Ip6CharConToStr(addr6, 0).c_str(), ""); 101 } 102 103 HWTEST_F(DhcpFuncTest, CheckIpStr_SUCCESS, TestSize.Level1) 104 { 105 std::string strIp4 = "192.77.1.232"; 106 EXPECT_EQ(true, DhcpFunc::CheckIpStr(strIp4)); 107 108 std::string strIp6 = "fe80::20c:29ff:fed7:fac8"; 109 EXPECT_EQ(true, DhcpFunc::CheckIpStr(strIp6)); 110 } 111 112 HWTEST_F(DhcpFuncTest, CheckIpStr_FAILED, TestSize.Level1) 113 { 114 std::string strIp1 = "192.77.232"; 115 EXPECT_EQ(false, DhcpFunc::CheckIpStr(strIp1)); 116 strIp1.clear(); 117 EXPECT_EQ(false, DhcpFunc::CheckIpStr(strIp1)); 118 119 std::string strIp2 = "fe80:fac8"; 120 EXPECT_EQ(false, DhcpFunc::CheckIpStr(strIp2)); 121 122 std::string strIp3 = "192.77.232:fe80:fac8"; 123 EXPECT_EQ(false, DhcpFunc::CheckIpStr(strIp3)); 124 } 125 126 HWTEST_F(DhcpFuncTest, GetLocalIp_TEST, TestSize.Level1) 127 { 128 std::string ifname, ip, netmask; 129 EXPECT_EQ(DHCP_OPT_ERROR, DhcpFunc::GetLocalIp(ifname, ip, netmask)); 130 131 MockSystemFunc::SetMockFlag(true); 132 133 EXPECT_CALL(MockSystemFunc::GetInstance(), socket(_, _, _)).WillRepeatedly(Return(1)); 134 EXPECT_CALL(MockSystemFunc::GetInstance(), ioctl(_, _, _)) 135 .WillOnce(Return(-1)) 136 .WillOnce(Return(0)).WillOnce(Return(-1)) 137 .WillRepeatedly(Return(0)); 138 EXPECT_CALL(MockSystemFunc::GetInstance(), close(_)).WillRepeatedly(Return(0)); 139 140 ifname = "wlan"; 141 EXPECT_EQ(DHCP_OPT_FAILED, DhcpFunc::GetLocalIp(ifname, ip, netmask)); 142 EXPECT_EQ(DHCP_OPT_FAILED, DhcpFunc::GetLocalIp(ifname, ip, netmask)); 143 EXPECT_EQ(DHCP_OPT_SUCCESS, DhcpFunc::GetLocalIp(ifname, ip, netmask)); 144 145 MockSystemFunc::SetMockFlag(false); 146 } 147 148 HWTEST_F(DhcpFuncTest, GetLocalMac_TEST, TestSize.Level1) 149 { 150 MockSystemFunc::SetMockFlag(true); 151 152 EXPECT_CALL(MockSystemFunc::GetInstance(), socket(_, _, _)).WillOnce(Return(-1)).WillRepeatedly(Return(1)); 153 EXPECT_CALL(MockSystemFunc::GetInstance(), ioctl(_, _, _)) 154 .WillOnce(Return(-1)).WillRepeatedly(Return(0)); 155 EXPECT_CALL(MockSystemFunc::GetInstance(), close(_)).WillRepeatedly(Return(0)); 156 157 std::string ifname = "wlan"; 158 std::string mac; 159 EXPECT_EQ(-1, DhcpFunc::GetLocalMac(ifname, mac)); 160 EXPECT_EQ(-1, DhcpFunc::GetLocalMac(ifname, mac)); 161 EXPECT_EQ(0, DhcpFunc::GetLocalMac(ifname, mac)); 162 163 MockSystemFunc::SetMockFlag(false); 164 } 165 166 HWTEST_F(DhcpFuncTest, CheckRangeNetwork_TEST, TestSize.Level1) 167 { 168 std::string ifname, begin, end; 169 EXPECT_EQ(DHCP_OPT_ERROR, DhcpFunc::CheckRangeNetwork(ifname, begin, end)); 170 } 171 172 HWTEST_F(DhcpFuncTest, FileManage_SUCCESS, TestSize.Level1) 173 { 174 std::string strFile = "./wlan0.result"; 175 bool bExist = DhcpFunc::IsExistFile(strFile); 176 if (bExist) { 177 EXPECT_EQ(true, DhcpFunc::RemoveFile(strFile)); 178 usleep(200); 179 } else { 180 EXPECT_EQ(false, DhcpFunc::RemoveFile(strFile)); 181 } 182 183 std::string strData = "IP4 1624421132 192.168.1.207 192.168.1.2 255.255.255.0 192.168.1.2 * 192.168.1.2 * * 43200"; 184 EXPECT_EQ(true, DhcpFunc::CreateFile(strFile, strData)); 185 186 std::string strAdd = "test add str"; 187 EXPECT_EQ(true, DhcpFunc::AddFileLineData(strFile, strData, strAdd)); 188 std::string strModify = "test modify str"; 189 EXPECT_EQ(true, DhcpFunc::ModifyFileLineData(strFile, strAdd, strModify)); 190 EXPECT_EQ(true, DhcpFunc::DelFileLineData(strFile, strModify)); 191 192 EXPECT_EQ(true, DhcpFunc::RemoveFile(strFile)); 193 } 194 195 HWTEST_F(DhcpFuncTest, FileManage_FAILED, TestSize.Level1) 196 { 197 std::string strFile = "./test/wlan0.result"; 198 EXPECT_EQ(false, DhcpFunc::RemoveFile(strFile)); 199 } 200 201 HWTEST_F(DhcpFuncTest, FormatString_SUCCESS, TestSize.Level1) 202 { 203 struct DhcpPacketResult result; 204 ASSERT_TRUE(memset_s(&result, sizeof(result), 0, sizeof(result)) == EOK); 205 ASSERT_TRUE(strncpy_s(result.strYiaddr, INET_ADDRSTRLEN, "*", INET_ADDRSTRLEN - 1) == EOK); 206 ASSERT_TRUE(strncpy_s(result.strOptServerId, INET_ADDRSTRLEN, "*", INET_ADDRSTRLEN - 1) == EOK); 207 ASSERT_TRUE(strncpy_s(result.strOptSubnet, INET_ADDRSTRLEN, "*", INET_ADDRSTRLEN - 1) == EOK); 208 ASSERT_TRUE(strncpy_s(result.strOptDns1, INET_ADDRSTRLEN, "*", INET_ADDRSTRLEN - 1) == EOK); 209 ASSERT_TRUE(strncpy_s(result.strOptDns2, INET_ADDRSTRLEN, "*", INET_ADDRSTRLEN - 1) == EOK); 210 ASSERT_TRUE(strncpy_s(result.strOptRouter1, INET_ADDRSTRLEN, "*", INET_ADDRSTRLEN - 1) == EOK); 211 ASSERT_TRUE(strncpy_s(result.strOptRouter2, INET_ADDRSTRLEN, "*", INET_ADDRSTRLEN - 1) == EOK); 212 ASSERT_TRUE(strncpy_s(result.strOptVendor, DHCP_FILE_MAX_BYTES, "*", DHCP_FILE_MAX_BYTES - 1) == EOK); 213 EXPECT_EQ(0, DhcpFunc::FormatString(result)); 214 } 215 216 HWTEST_F(DhcpFuncTest, InitPidfile_TEST, TestSize.Level1) 217 { 218 std::string pidDir, pidFile; 219 EXPECT_EQ(DHCP_OPT_FAILED, DhcpFunc::InitPidfile(pidDir, pidFile)); 220 221 MockSystemFunc::SetMockFlag(true); 222 223 EXPECT_CALL(MockSystemFunc::GetInstance(), open(_, _)).WillOnce(Return(-1)).WillRepeatedly(Return(1)); 224 EXPECT_CALL(MockSystemFunc::GetInstance(), close(_)).WillRepeatedly(Return(0)); 225 226 pidDir = "./"; 227 pidFile = "./wlan.pid"; 228 EXPECT_EQ(DHCP_OPT_FAILED, DhcpFunc::InitPidfile(pidDir, pidFile)); 229 230 MockSystemFunc::SetMockFlag(false); 231 } 232 233 HWTEST_F(DhcpFuncTest, GetPID_TEST, TestSize.Level1) 234 { 235 std::string pidDir = "./"; 236 std::string pidFile = "./wlan.pid"; 237 EXPECT_EQ(DHCP_OPT_SUCCESS, DhcpFunc::InitPidfile(pidDir, pidFile)); 238 EXPECT_GT(DhcpFunc::GetPID(pidFile), 0); 239 unlink(pidFile.c_str()); 240 EXPECT_EQ(DhcpFunc::GetPID(pidFile), -1); 241 } 242 243 HWTEST_F(DhcpFuncTest, CreateDirs_TEST, TestSize.Level1) 244 { 245 std::string strDir; 246 EXPECT_EQ(DhcpFunc::CreateDirs(strDir), DHCP_OPT_FAILED); 247 } 248