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 #include <gtest/gtest.h> 16 17 #include "dhcp_logger.h" 18 #include "dhcp_function.h" 19 #include "dhcp_client_def.h" 20 #include "mock_system_func.h" 21 22 DEFINE_DHCPLOG_DHCP_LABEL("DhcpFunctionTest"); 23 24 using namespace testing::ext; 25 using namespace OHOS::DHCP; 26 27 namespace OHOS { 28 class DhcpFunctionTest : public testing::Test { 29 public: SetUpTestCase()30 static void SetUpTestCase() 31 {} TearDownTestCase()32 static void TearDownTestCase() 33 {} SetUp()34 virtual void SetUp() 35 {} TearDown()36 virtual void TearDown() 37 {} 38 }; 39 40 HWTEST_F(DhcpFunctionTest, Ip4StrConToInt_SUCCESS, TestSize.Level1) 41 { 42 DHCP_LOGE("enter Ip4StrConToInt_SUCCESS"); 43 char serIp[INET_ADDRSTRLEN] = "192.77.1.231"; 44 uint32_t uSerIp = 0; 45 EXPECT_EQ(true, Ip4StrConToInt(serIp, &uSerIp, true)); 46 } 47 48 HWTEST_F(DhcpFunctionTest, Ip4StrConToInt_FAILED, TestSize.Level1) 49 { 50 char serIp[INET_ADDRSTRLEN] = {0}; 51 uint32_t uSerIp = 0; 52 EXPECT_EQ(false, Ip4StrConToInt(serIp, &uSerIp, true)); 53 54 char serIp1[INET_ADDRSTRLEN] = "192.77.231"; 55 uint32_t uSerIp1 = 0; 56 EXPECT_EQ(false, Ip4StrConToInt(serIp1, &uSerIp1, true)); 57 } 58 59 HWTEST_F(DhcpFunctionTest, Ip6StrConToChar_SUCCESS, TestSize.Level1) 60 { 61 char serIp[INET6_ADDRSTRLEN] = "2001:db8:0:1::231"; 62 uint8_t addr6[sizeof(struct in6_addr)] = {0}; 63 EXPECT_EQ(true, Ip6StrConToChar(serIp, addr6, sizeof(addr6))); 64 } 65 66 HWTEST_F(DhcpFunctionTest, Ip6StrConToChar_FAILED, TestSize.Level1) 67 { 68 char serIp[INET6_ADDRSTRLEN] = {0}; 69 uint8_t addr6[sizeof(struct in6_addr)] = {0}; 70 EXPECT_EQ(false, Ip6StrConToChar(serIp, addr6, sizeof(addr6))); 71 72 char serIp1[INET6_ADDRSTRLEN] = "231"; 73 uint8_t addr61[sizeof(struct in6_addr)] = {0}; 74 EXPECT_EQ(false, Ip6StrConToChar(serIp1, addr61, sizeof(addr61))); 75 } 76 77 HWTEST_F(DhcpFunctionTest, MacChConToMacStr_SUCCESS, TestSize.Level1) 78 { 79 EXPECT_EQ(nullptr, MacChConToMacStr(NULL, 0, NULL, 0)); 80 81 char interface[INFNAME_SIZE] = "wlan0"; 82 int ifindex; 83 unsigned char hwaddr[MAC_ADDR_LEN]; 84 ASSERT_EQ(GetLocalInterface(interface, &ifindex, hwaddr, NULL), DHCP_OPT_SUCCESS); 85 EXPECT_EQ(nullptr, MacChConToMacStr(hwaddr, MAC_ADDR_LEN, NULL, 0)); 86 87 char buf[MAC_ADDR_LEN * MAC_ADDR_CHAR_NUM] = {0}; 88 EXPECT_NE(nullptr, MacChConToMacStr(hwaddr, MAC_ADDR_LEN, buf, sizeof(buf))); 89 } 90 91 HWTEST_F(DhcpFunctionTest, GetLocalInterface_SUCCESS, TestSize.Level1) 92 { 93 EXPECT_EQ(GetLocalInterface(NULL, NULL, NULL, NULL), DHCP_OPT_FAILED); 94 95 MockSystemFunc::SetMockFlag(true); 96 97 EXPECT_CALL(MockSystemFunc::GetInstance(), socket(_, _, _)).WillOnce(Return(-1)).WillRepeatedly(Return(1)); 98 EXPECT_CALL(MockSystemFunc::GetInstance(), ioctl(_, _, _)) 99 .WillRepeatedly(Return(0)); 100 EXPECT_CALL(MockSystemFunc::GetInstance(), close(_)).WillRepeatedly(Return(0)); 101 102 char interface[INFNAME_SIZE] = "wlan0"; 103 EXPECT_EQ(GetLocalInterface(interface, NULL, NULL, NULL), DHCP_OPT_FAILED); 104 int ifindex = 0; 105 EXPECT_EQ(GetLocalInterface(interface, &ifindex, NULL, NULL), DHCP_OPT_FAILED); 106 unsigned char hwaddr[MAC_ADDR_LEN]; 107 EXPECT_EQ(GetLocalInterface(interface, &ifindex, hwaddr, NULL), DHCP_OPT_FAILED); 108 uint32_t ifaddr4; 109 EXPECT_EQ(GetLocalInterface(interface, &ifindex, hwaddr, &ifaddr4), DHCP_OPT_SUCCESS); 110 EXPECT_EQ(GetLocalInterface(interface, &ifindex, hwaddr, &ifaddr4), DHCP_OPT_SUCCESS); 111 112 MockSystemFunc::SetMockFlag(false); 113 } 114 115 HWTEST_F(DhcpFunctionTest, GetLocalIp_SUCCESS, TestSize.Level1) 116 { 117 char interface[INFNAME_SIZE] = "wlan0"; 118 uint32_t ipaddr4; 119 EXPECT_EQ(DHCP_OPT_SUCCESS, GetLocalIp(interface, &ipaddr4)); 120 } 121 122 HWTEST_F(DhcpFunctionTest, GetLocalIp_FAILED, TestSize.Level1) 123 { 124 char interface[INFNAME_SIZE] = {0}; 125 uint32_t ipaddr4; 126 EXPECT_EQ(DHCP_OPT_FAILED, GetLocalIp(interface, &ipaddr4)); 127 } 128 129 HWTEST_F(DhcpFunctionTest, SetLocalInterface_SUCCESS, TestSize.Level1) 130 { 131 char interface[INFNAME_SIZE] = "wlan0"; 132 uint32_t ipaddr4 = 2981805322; 133 uint32_t netMask = 16318463; 134 EXPECT_EQ(DHCP_OPT_SUCCESS, SetLocalInterface(interface, ipaddr4, netMask)); 135 } 136 137 HWTEST_F(DhcpFunctionTest, SetLocalInterface_FAILED, TestSize.Level1) 138 { 139 char interface[INFNAME_SIZE] = {0}; 140 uint32_t ipaddr4 = 0; 141 uint32_t netMask = 0; 142 EXPECT_EQ(DHCP_OPT_FAILED, SetLocalInterface(interface, ipaddr4, netMask)); 143 EXPECT_EQ(DHCP_OPT_FAILED, SetLocalInterface("wlan", ipaddr4, netMask)); 144 } 145 146 HWTEST_F(DhcpFunctionTest, CreateDirs_SUCCESS, TestSize.Level1) 147 { 148 EXPECT_EQ(DHCP_OPT_FAILED, CreateDirs(NULL, 0)); 149 150 const char *path = "test"; 151 EXPECT_EQ(DHCP_OPT_SUCCESS, CreateDirs(path, S_IRWXU)); 152 rmdir(path); 153 } 154 } // namespace OHOS