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