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