• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 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 #include <cstdint>
18 #include <cstdbool>
19 #include <cstdlib>
20 #include <cstdio>
21 #include <thread>
22 #include "dhcp_s_server.h"
23 #include "address_utils.h"
24 #include "dhcp_option.h"
25 #include "dhcp_logger.h"
26 #include "system_func_mock.h"
27 #include "securec.h"
28 
29 using namespace testing::ext;
30 using namespace std;
31 using namespace OHOS::Wifi;
32 
33 DEFINE_DHCPLOG_DHCP_LABEL("DhcpServerTest");
34 
35 struct ServerContext {
36     int broadCastFlagEnable;
37     DhcpAddressPool addressPool;
38     DhcpServerCallback callback;
39     DhcpConfig config;
40     int serverFd;
41     int looperState;
42     int initialized;
43 };
44 
45 namespace OHOS {
46 namespace Wifi {
47 class DhcpServerTest : public testing::Test {
48 public:
SetUpTestCase()49     static void SetUpTestCase()
50     {}
TearDownTestCase()51     static void TearDownTestCase()
52     {}
SetUp()53     virtual void SetUp()
54     {}
TearDown()55     virtual void TearDown()
56     {
57         SystemFuncMock::GetInstance().SetMockFlag(false);
58     }
59 
60     int InitServerConfig(DhcpConfig *config);
61     int FreeServerConfig(DhcpConfig *config);
62 
63 private:
64     DhcpServerContext *m_pServerCtx = nullptr;
65     DhcpConfig m_serverConfg;
66 };
67 
InitServerConfig(DhcpConfig * config)68 int DhcpServerTest::InitServerConfig(DhcpConfig *config)
69 {
70     if (!config) {
71         return RET_FAILED;
72     }
73     const char* testIfaceName = "test_if0";
74     uint32_t serverId = ParseIpAddr("192.168.189.254");
75     uint32_t netmask = ParseIpAddr("255.255.255.0");
76     uint32_t beginIp = ParseIpAddr("192.168.189.100");
77     uint32_t endIp = ParseIpAddr("192.168.189.200");
78     if (serverId == 0 || netmask == 0 || beginIp == 0 || endIp == 0) {
79         printf("failed to parse address.\n");
80         return RET_FAILED;
81     }
82     if (memset_s(config, sizeof(DhcpConfig), 0, sizeof(DhcpConfig)) != EOK) {
83         return RET_FAILED;
84     }
85     if (memset_s(config->ifname, sizeof(config->ifname), '\0', sizeof(config->ifname)) != EOK) {
86         return RET_FAILED;
87     }
88     if (strncpy_s(config->ifname, sizeof(config->ifname), testIfaceName, strlen(testIfaceName)) != EOK) {
89         return RET_FAILED;
90     }
91     config->serverId = serverId;
92     config->netmask = netmask;
93     config->pool.beginAddress = beginIp;
94     config->pool.endAddress = endIp;
95     config->broadcast = 1;
96     if (InitOptionList(&config->options) != RET_SUCCESS) {
97         return RET_FAILED;
98     }
99     return RET_SUCCESS;
100 }
101 
FreeServerConfig(DhcpConfig * config)102 int DhcpServerTest::FreeServerConfig(DhcpConfig *config)
103 {
104     if (!config) {
105         return RET_FAILED;
106     }
107     FreeOptionList(&config->options);
108     return RET_SUCCESS;
109 }
110 
111 HWTEST_F(DhcpServerTest, StartServerTest, TestSize.Level1)
112 {
113     DhcpServerContext SerCtx;
114     EXPECT_EQ(RET_FAILED, StartDhcpServer(nullptr));
115     ASSERT_TRUE(memset_s(SerCtx.ifname, sizeof(IFACE_NAME_SIZE), 0, sizeof(IFACE_NAME_SIZE)) == EOK);
116     EXPECT_EQ(RET_FAILED, StartDhcpServer(&SerCtx));
117 }
118 
119 HWTEST_F(DhcpServerTest, InitializeServerTest, TestSize.Level1)
120 {
121     DHCP_LOGE("enter InitializeServerTest");
122     DhcpConfig config;
123     PDhcpServerContext ctx = InitializeServer(nullptr);
124     EXPECT_TRUE(ctx == nullptr);
125     ASSERT_TRUE(memset_s(&config, sizeof(DhcpConfig), 0, sizeof(DhcpConfig)) == EOK);
126     ctx = InitializeServer(&config);
127     EXPECT_TRUE(ctx == nullptr);
128     EXPECT_TRUE(strcpy_s(config.ifname, sizeof(config.ifname), "test_if0") == EOK);
129     ctx = InitializeServer(&config);
130     EXPECT_TRUE(ctx == nullptr);
131 
132     EXPECT_EQ(RET_SUCCESS, InitServerConfig(&config));
133     ctx = InitializeServer(&config);
134     ASSERT_TRUE(ctx != nullptr);
135 
136     EXPECT_EQ(RET_SUCCESS, FreeServerConfig(&config));
137     EXPECT_EQ(RET_SUCCESS, FreeServerContext(&ctx));
138 }
139 
140 HWTEST_F(DhcpServerTest, ReceiveDhcpMessageFailedTest, TestSize.Level1)
141 {
142     SystemFuncMock::GetInstance().SetMockFlag(true);
143     EXPECT_CALL(SystemFuncMock::GetInstance(), select(_, _, _, _, _)).WillRepeatedly(Return(0));
144     ON_CALL(SystemFuncMock::GetInstance(), recvfrom(_, _, _, _, _, _))
145         .WillByDefault(Return((int)sizeof(DhcpMsgInfo)));
146     DhcpMsgInfo msgInfo = {{0}, 0, {0}};
147     const uint8_t testMac1[DHCP_HWADDR_LENGTH] = {0x00, 0x0e, 0x3c, 0x65, 0x3a, 0x09, 0};
148 
149     int ret = ReceiveDhcpMessage(1, &msgInfo); // failed to select isset.
150     EXPECT_TRUE(ret == RET_SELECT_TIME_OUT || ret == RET_ERROR || ret == RET_FAILED);
151     ret = ReceiveDhcpMessage(1, &msgInfo); // message length error
152     EXPECT_TRUE(ret == RET_SELECT_TIME_OUT || ret == RET_ERROR || ret == RET_FAILED);
153     ret = ReceiveDhcpMessage(1, &msgInfo); // dhcp message type error
154     EXPECT_TRUE(ret == RET_SELECT_TIME_OUT || ret == RET_ERROR || ret == RET_FAILED);
155     msgInfo.packet.hlen = 128;
156     ret = ReceiveDhcpMessage(1, &msgInfo); // hlen error
157     EXPECT_TRUE(ret == RET_SELECT_TIME_OUT || ret == RET_ERROR || ret == RET_FAILED);
158     msgInfo.packet.hlen = 16;
159     msgInfo.packet.op = BOOTREPLY;
160     ret = ReceiveDhcpMessage(1, &msgInfo); // client op type error
161     EXPECT_TRUE(ret == RET_SELECT_TIME_OUT || ret == RET_ERROR || ret == RET_FAILED);
162     msgInfo.packet.op = BOOTREQUEST;
163     ret = ReceiveDhcpMessage(1, &msgInfo); // client hardware address error
164     EXPECT_TRUE(ret == RET_SELECT_TIME_OUT || ret == RET_ERROR || ret == RET_FAILED);
165     for (int i = 0; i < MAC_ADDR_LENGTH; ++i) {
166         msgInfo.packet.chaddr[i] = testMac1[i];
167     }
168     ret = ReceiveDhcpMessage(1, &msgInfo);
169     EXPECT_TRUE(ret == RET_SELECT_TIME_OUT || ret == RET_ERROR || ret == RET_FAILED);
170 }
171 
172 HWTEST_F(DhcpServerTest, GetServerStatusTest, TestSize.Level1)
173 {
174     EXPECT_EQ(-1, GetServerStatus(nullptr));
175 }
176 
177 HWTEST_F(DhcpServerTest, StopDhcpServerTest, TestSize.Level1)
178 {
179     EXPECT_EQ(RET_FAILED, StopDhcpServer(nullptr));
180 }
181 
182 HWTEST_F(DhcpServerTest, FreeServerContextTest, TestSize.Level1)
183 {
184     EXPECT_EQ(RET_FAILED, FreeServerContext(nullptr));
185 }
186 
187 HWTEST_F(DhcpServerTest, SaveLeaseFailedTest, TestSize.Level1)
188 {
189     DhcpServerContext tempCtx;
190     tempCtx.instance = nullptr;
191     ASSERT_TRUE(memset_s(&tempCtx, sizeof(DhcpServerContext), 0, sizeof(DhcpServerContext)) == EOK);
192     EXPECT_EQ(RET_FAILED, SaveLease(nullptr));
193     EXPECT_EQ(RET_FAILED, SaveLease(&tempCtx));
194 }
195 }
196 }
197