• 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 
16 #include <gtest/gtest.h>
17 #include <cstdint>
18 #include <cstdbool>
19 #include "dhcp_define.h"
20 #include "dhcp_ipv4.h"
21 #include "dhcp_message.h"
22 #include "dhcp_option.h"
23 #include "address_utils.h"
24 
25 using namespace testing::ext;
26 namespace OHOS {
27 namespace Wifi {
28 class DhcpOptionTest : public testing::Test {
29 public:
SetUpTestCase()30     static void SetUpTestCase()
31     {}
TearDownTestCase()32     static void TearDownTestCase()
33     {}
SetUp()34     virtual void SetUp()
35     {
36         if (InitOptionList(&options)) {
37             printf("failed to initialized hash table.\n");
38         }
39     }
TearDown()40     virtual void TearDown()
41     {
42         FreeOptionList(&options);
43     }
44 public:
45     DhcpOptionList options = {0};
46 };
47 
48 HWTEST_F(DhcpOptionTest, InitOptionListTest, TestSize.Level1)
49 {
50     DhcpOptionList testOpts = {0};
51     EXPECT_EQ(RET_SUCCESS, InitOptionList(&testOpts));
52     FreeOptionList(&testOpts);
53     EXPECT_EQ(RET_SUCCESS, InitOptionList(&options));
54     EXPECT_EQ(RET_ERROR, InitOptionList(NULL));
55 }
56 
57 HWTEST_F(DhcpOptionTest, HasInitializedTest, TestSize.Level1)
58 {
59     DhcpOptionList testOpts = {0};
60     EXPECT_EQ(0, HasInitialized(NULL));
61     EXPECT_EQ(0, HasInitialized(&testOpts));
62     ASSERT_EQ(RET_SUCCESS, InitOptionList(&testOpts));
63     EXPECT_EQ(1, HasInitialized(&testOpts));
64     FreeOptionList(&testOpts);
65 }
66 
67 HWTEST_F(DhcpOptionTest, PushBackOptionTest, TestSize.Level1)
68 {
69     DhcpOption optRouter = {ROUTER_OPTION, 0, {0}};
70     EXPECT_EQ(RET_SUCCESS, PushBackOption(&options, &optRouter));
71 
72     DhcpOption optMsgType = {DHCP_MESSAGE_TYPE_OPTION, 1, {DHCPOFFER, 0}};
73     EXPECT_EQ(RET_ERROR, PushBackOption(NULL, &optMsgType));
74     EXPECT_EQ(RET_ERROR, PushBackOption(&options, NULL));
75     EXPECT_EQ(RET_SUCCESS, PushBackOption(&options, &optMsgType));
76     EXPECT_TRUE(options.size == 2);
77     ClearOptions(&options);
78     EXPECT_TRUE(options.size == 0);
79     ClearOptions(NULL);
80 }
81 
82 HWTEST_F(DhcpOptionTest, PushFrontOptionTest, TestSize.Level1)
83 {
84     DhcpOption optRouter = {ROUTER_OPTION, 0, {0}};
85     EXPECT_EQ(RET_SUCCESS, PushFrontOption(&options, &optRouter));
86 
87     DhcpOption optMsgType = {DHCP_MESSAGE_TYPE_OPTION, 1, {DHCPOFFER, 0}};
88     EXPECT_EQ(RET_ERROR, PushFrontOption(NULL, &optMsgType));
89     EXPECT_EQ(RET_ERROR, PushFrontOption(&options, NULL));
90     EXPECT_EQ(RET_SUCCESS, PushFrontOption(&options, &optMsgType));
91     EXPECT_TRUE(options.size == 2);
92     ClearOptions(&options);
93     EXPECT_TRUE(options.size == 0);
94 }
95 
96 HWTEST_F(DhcpOptionTest, GetOptionNodeTest, TestSize.Level1)
97 {
98     DhcpOption optRouter = {ROUTER_OPTION, 0, {0}};
99     EXPECT_EQ(RET_SUCCESS, PushFrontOption(&options, &optRouter));
100     DhcpOption optMsgType = {DHCP_MESSAGE_TYPE_OPTION, 1, {DHCPOFFER, 0}};
101     EXPECT_EQ(RET_SUCCESS, PushFrontOption(&options, &optMsgType));
102     EXPECT_TRUE(options.size == 2);
103 
104     DhcpOptionNode *node = GetOptionNode(&options, DHCP_MESSAGE_TYPE_OPTION);
105     EXPECT_TRUE(node!=NULL);
106     ClearOptions(&options);
107     EXPECT_TRUE(options.size == 0);
108 }
109 
110 HWTEST_F(DhcpOptionTest, GetOptionTest, TestSize.Level1)
111 {
112     DhcpOption optRouter = {ROUTER_OPTION, 0, {0}};
113     EXPECT_EQ(RET_SUCCESS, PushFrontOption(&options, &optRouter));
114     DhcpOption optMsgType = {DHCP_MESSAGE_TYPE_OPTION, 1, {DHCPOFFER, 0}};
115     EXPECT_EQ(RET_SUCCESS, PushFrontOption(&options, &optMsgType));
116     EXPECT_TRUE(options.size == 2);
117 
118     DhcpOption *node = GetOption(&options, DHCP_MESSAGE_TYPE_OPTION);
119     EXPECT_TRUE(node!=NULL);
120     ClearOptions(&options);
121     EXPECT_TRUE(options.size == 0);
122 }
123 
124 
125 HWTEST_F(DhcpOptionTest, RemoveOptionTest, TestSize.Level1)
126 {
127     DhcpOption optRouter = {ROUTER_OPTION, 0, {0}};
128     EXPECT_EQ(RET_SUCCESS, PushFrontOption(&options, &optRouter));
129     EXPECT_TRUE(options.size == 1);
130     EXPECT_EQ(RET_ERROR, RemoveOption(NULL, DOMAIN_NAME_SERVER_OPTION));
131     EXPECT_EQ(RET_FAILED, RemoveOption(&options, DOMAIN_NAME_SERVER_OPTION));
132     EXPECT_EQ(RET_SUCCESS, RemoveOption(&options, ROUTER_OPTION));
133     EXPECT_EQ(RET_FAILED, RemoveOption(&options, ROUTER_OPTION));
134     EXPECT_TRUE(options.size == 0);
135     ClearOptions(&options);
136 }
137 
138 HWTEST_F(DhcpOptionTest, FillOptionTest, TestSize.Level1)
139 {
140     const char *serverInfo = "dhcp server 1.0";
141     DhcpOptionList optRouter;
142     optRouter.first = NULL;
143     FreeOptionList(&optRouter);
144     DhcpOption optVendorInfo = {VENDOR_SPECIFIC_INFO_OPTION, 0, {0}};
145     EXPECT_EQ(RET_FAILED, FillOption(&optVendorInfo, NULL, 0));
146     EXPECT_EQ(RET_ERROR, FillOption(NULL, serverInfo, strlen(serverInfo)));
147     EXPECT_EQ(RET_SUCCESS, FillOption(&optVendorInfo, serverInfo, strlen(serverInfo)));
148 }
149 
150 HWTEST_F(DhcpOptionTest, FillOptionDataTest, TestSize.Level1)
151 {
152     uint8_t testData[] = {192, 168, 100, 254};
153     DhcpOption optRouter = {ROUTER_OPTION, 0, {0}};
154     EXPECT_EQ(RET_ERROR, FillOptionData(NULL, testData, sizeof(testData)));
155     EXPECT_EQ(RET_FAILED, FillOptionData(&optRouter, NULL, sizeof(testData)));
156     EXPECT_EQ(RET_SUCCESS, FillOptionData(&optRouter, testData, sizeof(testData)));
157 }
158 
159 HWTEST_F(DhcpOptionTest, FillU32OptionTest, TestSize.Level1)
160 {
161     uint32_t testIp = ParseIpAddr("192.168.100.254");
162     EXPECT_TRUE(testIp != 0);
163     DhcpOption optRouter = {ROUTER_OPTION, 0, {0}};
164     EXPECT_EQ(RET_SUCCESS, FillU32Option(&optRouter, testIp));
165     EXPECT_EQ(RET_ERROR, FillU32Option(NULL, testIp));
166 }
167 
168 HWTEST_F(DhcpOptionTest, AppendAddressOptionTest, TestSize.Level1)
169 {
170     uint32_t testDns1 = ParseIpAddr("192.168.100.1");
171     EXPECT_TRUE(testDns1 != 0);
172     uint32_t testDns2 = ParseIpAddr("192.168.100.2");
173     EXPECT_TRUE(testDns2 != 0);
174     uint32_t testDns3 = ParseIpAddr("192.168.100.3");
175     EXPECT_TRUE(testDns3 != 0);
176     FreeOptionList(NULL);
177 
178     DhcpOption optDns = {DOMAIN_NAME_SERVER_OPTION, 0, {0}};
179     EXPECT_EQ(RET_ERROR, AppendAddressOption(NULL, testDns1));
180     EXPECT_EQ(RET_SUCCESS, AppendAddressOption(&optDns, testDns1));
181     EXPECT_EQ(RET_SUCCESS, AppendAddressOption(&optDns, testDns2));
182     EXPECT_EQ(RET_SUCCESS, AppendAddressOption(&optDns, testDns3));
183     EXPECT_EQ(12, optDns.length);
184 }
185 }
186 }