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