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, Ip4IntConvertToStr_SUCCESS, TestSize.Level1)
60 {
61     uint32_t uSerIp = 3226272231;
62     std::string pSerIp = Ip4IntConvertToStr(uSerIp, true);
63     EXPECT_STRNE(pSerIp.c_str(), "");
64 }
65 
66 HWTEST_F(DhcpFunctionTest, Ip6StrConToChar_SUCCESS, TestSize.Level1)
67 {
68     char serIp[INET6_ADDRSTRLEN] = "2001:db8:0:1::231";
69     uint8_t	addr6[sizeof(struct in6_addr)] = {0};
70     EXPECT_EQ(true, Ip6StrConToChar(serIp, addr6, sizeof(addr6)));
71 }
72 
73 HWTEST_F(DhcpFunctionTest, Ip6StrConToChar_FAILED, TestSize.Level1)
74 {
75     char serIp[INET6_ADDRSTRLEN] = {0};
76     uint8_t	addr6[sizeof(struct in6_addr)] = {0};
77     EXPECT_EQ(false, Ip6StrConToChar(serIp, addr6, sizeof(addr6)));
78 
79     char serIp1[INET6_ADDRSTRLEN] = "231";
80     uint8_t	addr61[sizeof(struct in6_addr)] = {0};
81     EXPECT_EQ(false, Ip6StrConToChar(serIp1, addr61, sizeof(addr61)));
82 }
83 
84 HWTEST_F(DhcpFunctionTest, MacChConToMacStr_SUCCESS, TestSize.Level1)
85 {
86     EXPECT_EQ(nullptr, MacChConToMacStr(NULL, 0, NULL, 0));
87 
88     char interface[INFNAME_SIZE] = "wlan0";
89     int  ifindex;
90     unsigned char hwaddr[MAC_ADDR_LEN];
91     ASSERT_EQ(GetLocalInterface(interface, &ifindex, hwaddr, NULL), DHCP_OPT_SUCCESS);
92     EXPECT_EQ(nullptr, MacChConToMacStr(hwaddr, MAC_ADDR_LEN, NULL, 0));
93 
94     char buf[MAC_ADDR_LEN * MAC_ADDR_CHAR_NUM] = {0};
95     EXPECT_NE(nullptr, MacChConToMacStr(hwaddr, MAC_ADDR_LEN, buf, sizeof(buf)));
96 }
97 
98 HWTEST_F(DhcpFunctionTest, GetLocalInterface_SUCCESS, TestSize.Level1)
99 {
100     EXPECT_EQ(GetLocalInterface(NULL, NULL, NULL, NULL), DHCP_OPT_FAILED);
101 
102     MockSystemFunc::SetMockFlag(true);
103 
104     EXPECT_CALL(MockSystemFunc::GetInstance(), socket(_, _, _)).WillOnce(Return(-1)).WillRepeatedly(Return(1));
105     EXPECT_CALL(MockSystemFunc::GetInstance(), ioctl(_, _, _))
106         .WillRepeatedly(Return(0));
107     EXPECT_CALL(MockSystemFunc::GetInstance(), close(_)).WillRepeatedly(Return(0));
108 
109     char interface[INFNAME_SIZE] = "wlan0";
110     EXPECT_EQ(GetLocalInterface(interface, NULL, NULL, NULL), DHCP_OPT_FAILED);
111     int ifindex = 0;
112     EXPECT_EQ(GetLocalInterface(interface, &ifindex, NULL, NULL), DHCP_OPT_FAILED);
113     unsigned char hwaddr[MAC_ADDR_LEN];
114     EXPECT_EQ(GetLocalInterface(interface, &ifindex, hwaddr, NULL), DHCP_OPT_FAILED);
115     uint32_t ifaddr4;
116     EXPECT_EQ(GetLocalInterface(interface, &ifindex, hwaddr, &ifaddr4), DHCP_OPT_SUCCESS);
117     EXPECT_EQ(GetLocalInterface(interface, &ifindex, hwaddr, &ifaddr4), DHCP_OPT_SUCCESS);
118 
119     MockSystemFunc::SetMockFlag(false);
120 }
121 
122 HWTEST_F(DhcpFunctionTest, GetLocalIp_SUCCESS, TestSize.Level1)
123 {
124     char interface[INFNAME_SIZE] = "wlan0";
125     uint32_t ipaddr4;
126     EXPECT_EQ(DHCP_OPT_SUCCESS, GetLocalIp(interface, &ipaddr4));
127 }
128 
129 HWTEST_F(DhcpFunctionTest, GetLocalIp_FAILED, TestSize.Level1)
130 {
131     char interface[INFNAME_SIZE] = {0};
132     uint32_t ipaddr4;
133     EXPECT_EQ(DHCP_OPT_FAILED, GetLocalIp(interface, &ipaddr4));
134 }
135 
136 HWTEST_F(DhcpFunctionTest, SetLocalInterface_SUCCESS, TestSize.Level1)
137 {
138     char interface[INFNAME_SIZE] = "wlan0";
139     uint32_t ipaddr4 = 2981805322;
140     uint32_t netMask = 16318463;
141     EXPECT_EQ(DHCP_OPT_SUCCESS, SetLocalInterface(interface, ipaddr4, netMask));
142 }
143 
144 HWTEST_F(DhcpFunctionTest, SetLocalInterface_FAILED, TestSize.Level1)
145 {
146     char interface[INFNAME_SIZE] = {0};
147     uint32_t ipaddr4 = 0;
148     uint32_t netMask = 0;
149     EXPECT_EQ(DHCP_OPT_FAILED, SetLocalInterface(interface, ipaddr4, netMask));
150     EXPECT_EQ(DHCP_OPT_FAILED, SetLocalInterface("wlan", ipaddr4, netMask));
151 }
152 
153 HWTEST_F(DhcpFunctionTest, InitPidfile_SUCCESS, TestSize.Level1)
154 {
155     char workDir[DIR_MAX_LEN] = "./";
156     char pidFile[DIR_MAX_LEN] = "./wlan0.pid";
157 
158     EXPECT_EQ(DHCP_OPT_SUCCESS, InitPidfile(workDir, pidFile, getpid()));
159     unlink(pidFile);
160     usleep(SLEEP_TIME_200_MS);
161 }
162 
163 HWTEST_F(DhcpFunctionTest, InitPidfile_FAILED, TestSize.Level1)
164 {
165     char *workDir = nullptr;
166     char *pidFile = nullptr;
167 
168     EXPECT_EQ(DHCP_OPT_FAILED, InitPidfile(workDir, pidFile, getpid()));
169     unlink(pidFile);
170     usleep(SLEEP_TIME_200_MS);
171 }
172 
173 HWTEST_F(DhcpFunctionTest, GetPID_SUCCESS, TestSize.Level1)
174 {
175     char workDir[DIR_MAX_LEN] = "./";
176     char pidFile[DIR_MAX_LEN] = "./wlan0.pid";
177 
178     EXPECT_EQ(DHCP_OPT_SUCCESS, InitPidfile(workDir, pidFile, getpid()));
179     EXPECT_GT(GetPID(pidFile), 0);
180     unlink(pidFile);
181 }
182 
183 HWTEST_F(DhcpFunctionTest, CreateDirs_SUCCESS, TestSize.Level1)
184 {
185     EXPECT_EQ(DHCP_OPT_FAILED, CreateDirs(NULL, 0));
186 
187     const char *path = "test";
188     EXPECT_EQ(DHCP_OPT_SUCCESS, CreateDirs(path, S_IRWXU));
189     rmdir(path);
190 }
191 }  // namespace OHOS