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 "dhcp_address_pool.h" 24 #include "address_utils.h" 25 #include "common_util.h" 26 #include "securec.h" 27 28 using namespace testing::ext; 29 namespace OHOS { 30 namespace Wifi { 31 class DhcpAddressPoolTest : 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 (InitAddressPool(&testPool, "lo", NULL)) { 40 printf("failed to initialized address pool.\n"); 41 } 42 } TearDown()43 virtual void TearDown() 44 { 45 ResetPollConfig(); 46 FreeAddressPool(&testPool); 47 } 48 SamplePoolConfig()49 bool SamplePoolConfig() 50 { 51 uint32_t beginIp = ParseIpAddr("192.168.100.100"); 52 uint32_t endIp = ParseIpAddr("192.168.100.150"); 53 uint32_t netmask = ParseIpAddr("255.255.255.0"); 54 uint32_t gateway = ParseIpAddr("192.168.100.254"); 55 uint32_t serverId = ParseIpAddr("192.168.100.1"); 56 if (beginIp != 0 && endIp != 0 && netmask != 0 && gateway != 0) { 57 testPool.addressRange.beginAddress = beginIp; 58 testPool.addressRange.endAddress = endIp; 59 testPool.netmask = netmask; 60 testPool.gateway = gateway; 61 testPool.serverId = serverId; 62 testPool.leaseTime = DHCP_LEASE_TIME; 63 return true; 64 } 65 return false; 66 } 67 ResetPollConfig()68 void ResetPollConfig() 69 { 70 testPool.addressRange.beginAddress = 0; 71 testPool.addressRange.endAddress = 0; 72 testPool.netmask = 0; 73 testPool.gateway = 0; 74 testPool.serverId = 0; 75 testPool.leaseTime = 0; 76 } 77 public: 78 DhcpAddressPool testPool; 79 }; 80 81 HWTEST_F(DhcpAddressPoolTest, AddBindingTest, TestSize.Level1) 82 { 83 AddressBinding bind = {0}; 84 uint8_t testMac1[DHCP_HWADDR_LENGTH] = {0x00, 0x0e, 0x3c, 0x65, 0x3a, 0x09, 0}; 85 uint8_t testMac2[DHCP_HWADDR_LENGTH] = {0x00, 0x0e, 0x3c, 0x65, 0x3a, 0x0a, 0}; 86 uint32_t testIp1 = ParseIpAddr("192.168.100.1"); 87 uint32_t testIp2 = ParseIpAddr("192.168.100.2"); 88 bind.ipAddress = testIp1; 89 for (int i = 0; i < MAC_ADDR_LENGTH; ++i) { 90 bind.chaddr[i] = testMac1[i]; 91 } 92 EXPECT_EQ(RET_SUCCESS, AddBinding(&bind)); 93 bind.ipAddress = testIp2; 94 for (int i = 0; i < MAC_ADDR_LENGTH; ++i) { 95 bind.chaddr[i] = testMac2[i]; 96 } 97 98 EXPECT_EQ(RET_ERROR, AddBinding(NULL)); 99 EXPECT_EQ(RET_SUCCESS, AddBinding(&bind)); 100 EXPECT_EQ(RET_FAILED, AddBinding(&bind)); 101 EXPECT_TRUE(memset_s(bind.chaddr, sizeof(bind.chaddr), 0, sizeof(bind.chaddr)) == EOK); 102 EXPECT_EQ(RET_SUCCESS, RemoveBinding(testMac1)); 103 EXPECT_EQ(RET_SUCCESS, RemoveBinding(testMac2)); 104 } 105 106 HWTEST_F(DhcpAddressPoolTest, AddNewBindingTest, TestSize.Level1) 107 { 108 uint8_t testMac1[DHCP_HWADDR_LENGTH] = {0x00, 0x0e, 0x3c, 0x65, 0x3a, 0x09, 0}; 109 ASSERT_TRUE(testPool.newBinding != NULL); 110 ASSERT_TRUE(testPool.newBinding(testMac1, NULL) != NULL); 111 EXPECT_EQ(RET_SUCCESS, RemoveBinding(testMac1)); 112 } 113 114 HWTEST_F(DhcpAddressPoolTest, FindBindingByIpTest, TestSize.Level1) 115 { 116 AddressBinding bind = {0}; 117 uint8_t testMac1[DHCP_HWADDR_LENGTH] = {0x00, 0x0e, 0x3c, 0x65, 0x3a, 0x09, 0}; 118 uint8_t testMac2[DHCP_HWADDR_LENGTH] = {0x00, 0x0e, 0x3c, 0x65, 0x3a, 0x0a, 0}; 119 uint32_t testIp1 = ParseIpAddr("192.168.100.1"); 120 uint32_t testIp2 = ParseIpAddr("192.168.100.2"); 121 bind.ipAddress = testIp1; 122 for (int i = 0; i < MAC_ADDR_LENGTH; ++i) { 123 bind.chaddr[i] = testMac1[i]; 124 } 125 EXPECT_EQ(RET_SUCCESS, AddBinding(&bind)); 126 bind.ipAddress = testIp2; 127 for (int i = 0; i < MAC_ADDR_LENGTH; ++i) { 128 bind.chaddr[i] = testMac2[i]; 129 } 130 EXPECT_EQ(RET_SUCCESS, AddBinding(&bind)); 131 EXPECT_EQ(RET_FAILED, AddBinding(&bind)); 132 133 AddressBinding *pBind1 = FindBindingByIp(testIp1); 134 AddressBinding *pBind2 = FindBindingByIp(testIp2); 135 EXPECT_TRUE(pBind1 != NULL); 136 EXPECT_TRUE(pBind2 != NULL); 137 if (pBind1 != NULL) { 138 EXPECT_EQ(testIp1, pBind1->ipAddress); 139 } 140 EXPECT_EQ(RET_SUCCESS, RemoveBinding(testMac1)); 141 EXPECT_EQ(RET_SUCCESS, RemoveBinding(testMac2)); 142 } 143 144 HWTEST_F(DhcpAddressPoolTest, AddressDistributeTest, TestSize.Level1) 145 { 146 uint8_t testMac1[DHCP_HWADDR_LENGTH] = {0x01, 0x0e, 0x3c, 0x65, 0x3a, 0x09, 0}; 147 uint8_t testMac2[DHCP_HWADDR_LENGTH] = {0x01, 0x0e, 0x3c, 0x65, 0x3a, 0x0a, 0}; 148 uint8_t testMac3[DHCP_HWADDR_LENGTH] = {0x01, 0x0e, 0x3c, 0x65, 0x3a, 0x09, 0}; 149 150 EXPECT_TRUE(testPool.distribue(NULL, testMac1) == 0); 151 EXPECT_TRUE(testPool.distribue(&testPool, testMac1) == 0); 152 ASSERT_TRUE(SamplePoolConfig()); 153 SetDistributeMode(0); 154 EXPECT_EQ(0, GetDistributeMode()); 155 uint32_t assertAddr = ParseIpAddr("192.168.100.101"); 156 EXPECT_EQ(assertAddr, testPool.distribue(&testPool, testMac1)); 157 assertAddr = ParseIpAddr("192.168.100.102"); 158 EXPECT_EQ(assertAddr, testPool.distribue(&testPool, testMac2)); 159 assertAddr = ParseIpAddr("192.168.100.103"); 160 EXPECT_EQ(assertAddr, testPool.distribue(&testPool, testMac3)); 161 } 162 163 HWTEST_F(DhcpAddressPoolTest, IsReservedTest, TestSize.Level1) 164 { 165 AddressBinding bind = {0}; 166 uint8_t testMac1[DHCP_HWADDR_LENGTH] = {0x00, 0x0e, 0x3c, 0x65, 0x3a, 0x09, 0}; 167 uint8_t testMac2[DHCP_HWADDR_LENGTH] = {0x00, 0x0e, 0x3c, 0x65, 0x3a, 0x0a, 0}; 168 uint8_t testMac3[DHCP_HWADDR_LENGTH] = {0x00, 0x0e, 0x3c, 0x65, 0x3a, 0x0b, 0}; 169 uint32_t testIp1 = ParseIpAddr("192.168.100.1"); 170 ASSERT_TRUE(testIp1 != 0); 171 uint32_t testIp2 = ParseIpAddr("192.168.100.2"); 172 ASSERT_TRUE(testIp2 != 0); 173 bind.ipAddress = testIp1; 174 for (int i = 0; i < MAC_ADDR_LENGTH; ++i) { 175 bind.chaddr[i] = testMac1[i]; 176 } 177 EXPECT_EQ(RET_SUCCESS, AddBinding(&bind)); 178 bind.ipAddress = testIp2; 179 for (int i = 0; i < MAC_ADDR_LENGTH; ++i) { 180 bind.chaddr[i] = testMac2[i]; 181 } 182 EXPECT_EQ(RET_SUCCESS, AddReservedBinding(testMac2)); 183 EXPECT_EQ(RET_SUCCESS, AddReservedBinding(testMac2)); 184 EXPECT_EQ(RET_FAILED, AddBinding(&bind)); 185 EXPECT_EQ(0, IsReserved(testMac1)); 186 EXPECT_EQ(1, IsReserved(testMac2)); 187 for (int i = 0; i < MAC_ADDR_LENGTH; ++i) { 188 bind.chaddr[i] = testMac3[i]; 189 } 190 EXPECT_EQ(0, IsReserved(testMac3)); 191 EXPECT_EQ(RET_SUCCESS, RemoveBinding(testMac1)); 192 EXPECT_EQ(RET_SUCCESS, RemoveBinding(testMac2)); 193 } 194 195 196 HWTEST_F(DhcpAddressPoolTest, IsReservedIpTest, TestSize.Level1) 197 { 198 AddressBinding bind = {0}; 199 bind.bindingMode = BIND_MODE_DYNAMIC; 200 const uint8_t testMac1[DHCP_HWADDR_LENGTH] = {0x00, 0x0e, 0x3c, 0x65, 0x3a, 0x09, 0}; 201 const uint8_t testMac2[DHCP_HWADDR_LENGTH] = {0x00, 0x0e, 0x3c, 0x65, 0x3a, 0x0a, 0}; 202 uint32_t testIp1 = ParseIpAddr("192.168.100.1"); 203 ASSERT_TRUE(testIp1 != 0); 204 uint32_t testIp2 = ParseIpAddr("192.168.100.2"); 205 ASSERT_TRUE(testIp2 != 0); 206 bind.ipAddress = testIp1; 207 for (int i = 0; i < MAC_ADDR_LENGTH; ++i) { 208 bind.chaddr[i] = testMac1[i]; 209 } 210 EXPECT_EQ(RET_SUCCESS, AddLease(&testPool, &bind)); 211 bind.ipAddress = testIp2; 212 for (int i = 0; i < MAC_ADDR_LENGTH; ++i) { 213 bind.chaddr[i] = testMac2[i]; 214 } 215 bind.bindingMode = BIND_MODE_RESERVED; 216 EXPECT_EQ(RET_SUCCESS, AddLease(&testPool, &bind)); 217 EXPECT_EQ(0, IsReservedIp(&testPool, testIp1)); 218 EXPECT_EQ(1, IsReservedIp(&testPool, testIp2)); 219 EXPECT_EQ(DHCP_FALSE, IsReservedIp(NULL, testIp1)); 220 EXPECT_EQ(DHCP_FALSE, IsReservedIp(&testPool, 0)); 221 bind.ipAddress = testIp1; 222 EXPECT_EQ(RET_SUCCESS, RemoveLease(&testPool, &bind)); 223 bind.ipAddress = testIp2; 224 EXPECT_EQ(RET_SUCCESS, RemoveLease(&testPool, &bind)); 225 } 226 227 HWTEST_F(DhcpAddressPoolTest, RemoveReservedBindingTest, TestSize.Level1) 228 { 229 AddressBinding bind = {0}, bind2 = {0}; 230 uint8_t testMac1[DHCP_HWADDR_LENGTH] = {0x00, 0x01, 0x3c, 0x65, 0x3a, 0x09, 0}; 231 uint8_t testMac2[DHCP_HWADDR_LENGTH] = {0x00, 0x02, 0x3c, 0x65, 0x3a, 0x0a, 0}; 232 uint8_t testMac3[DHCP_HWADDR_LENGTH] = {0x00, 0x03, 0x3c, 0x65, 0x3a, 0x0b, 0}; 233 uint8_t testMac4[DHCP_HWADDR_LENGTH] = {0x00, 0x03, 0x3c, 0x65, 0x3a, 0x0c, 0}; 234 uint32_t testIp1 = ParseIpAddr("192.168.100.1"); 235 EXPECT_TRUE(testIp1 != 0); 236 uint32_t testIp2 = ParseIpAddr("192.168.100.2"); 237 EXPECT_TRUE(testIp2 != 0); 238 bind.ipAddress = testIp1; 239 for (int i = 0; i < MAC_ADDR_LENGTH; ++i) { 240 bind.chaddr[i] = testMac1[i]; 241 } 242 EXPECT_EQ(RET_SUCCESS, AddBinding(&bind)); 243 bind2.ipAddress = testIp2; 244 for (int i = 0; i < MAC_ADDR_LENGTH; ++i) { 245 bind2.chaddr[i] = testMac2[i]; 246 } 247 bind2.bindingMode = BIND_MODE_RESERVED; 248 ASSERT_EQ(RET_SUCCESS, AddBinding(&bind2)); 249 EXPECT_EQ(RET_FAILED, RemoveReservedBinding(testMac1)); 250 EXPECT_EQ(RET_SUCCESS, RemoveBinding(testMac1)); 251 AddressBinding *binding = QueryBinding(testMac2, NULL); 252 ASSERT_TRUE(binding != NULL); 253 EXPECT_EQ(RET_SUCCESS, RemoveReservedBinding(testMac2)); 254 EXPECT_EQ(RET_FAILED, RemoveReservedBinding(testMac2)); 255 EXPECT_EQ(RET_FAILED, RemoveReservedBinding(testMac3)); 256 EXPECT_EQ(RET_FAILED, RemoveReservedBinding(testMac4)); 257 } 258 259 HWTEST_F(DhcpAddressPoolTest, ReleaseBindingTest, TestSize.Level1) 260 { 261 AddressBinding bind = {0}; 262 bind.bindingMode = BIND_ASSOCIATED; 263 uint8_t testMac1[DHCP_HWADDR_LENGTH] = {0x00, 0x0e, 0x3c, 0x65, 0x3a, 0x09, 0}; 264 uint8_t testMac2[DHCP_HWADDR_LENGTH] = {0x00, 0x0e, 0x3c, 0x65, 0x3a, 0x0a, 0}; 265 uint8_t testMac3[DHCP_HWADDR_LENGTH] = {0x00, 0x0e, 0x3c, 0x65, 0x3a, 0x0b, 0}; 266 uint32_t testIp1 = ParseIpAddr("192.168.100.1"); 267 ASSERT_TRUE(testIp1 != 0); 268 uint32_t testIp2 = ParseIpAddr("192.168.100.2"); 269 ASSERT_TRUE(testIp2 != 0); 270 bind.ipAddress = testIp1; 271 for (int i = 0; i < MAC_ADDR_LENGTH; ++i) { 272 bind.chaddr[i] = testMac1[i]; 273 } 274 ASSERT_EQ(RET_SUCCESS, AddBinding(&bind)); 275 bind.ipAddress = testIp2; 276 for (int i = 0; i < MAC_ADDR_LENGTH; ++i) { 277 bind.chaddr[i] = testMac2[i]; 278 } 279 ASSERT_EQ(RET_SUCCESS, AddBinding(&bind)); 280 EXPECT_EQ(RET_SUCCESS, ReleaseBinding(testMac1)); 281 EXPECT_EQ(RET_FAILED, ReleaseBinding(testMac3)); 282 EXPECT_EQ(RET_SUCCESS, RemoveBinding(testMac1)); 283 EXPECT_EQ(RET_SUCCESS, RemoveBinding(testMac2)); 284 } 285 286 HWTEST_F(DhcpAddressPoolTest, AddLeaseTest, TestSize.Level1) 287 { 288 AddressBinding lease = {0}; 289 lease.bindingMode = BIND_MODE_DYNAMIC; 290 lease.bindingStatus = BIND_ASSOCIATED; 291 const uint8_t testMac1[DHCP_HWADDR_LENGTH] = {0x01, 0x0e, 0x3c, 0x65, 0x3a, 0x09, 0}; 292 const uint8_t testMac2[DHCP_HWADDR_LENGTH] = {0x01, 0x0e, 0x3d, 0x65, 0x3a, 0x0a, 0}; 293 uint32_t testIp1 = ParseIpAddr("192.168.100.101"); 294 ASSERT_TRUE(testIp1 != 0); 295 lease.ipAddress = testIp1; 296 lease.leaseTime = DHCP_LEASE_TIME; 297 lease.pendingTime = 1631240659; 298 lease.bindingTime = 1631240659; 299 for (int i = 0; i < MAC_ADDR_LENGTH; ++i) { 300 lease.chaddr[i] = testMac1[i]; 301 } 302 ASSERT_EQ(RET_SUCCESS, AddLease(&testPool, &lease)); 303 for (int i = 0; i < MAC_ADDR_LENGTH; ++i) { 304 lease.chaddr[i] = testMac2[i]; 305 } 306 EXPECT_EQ(RET_ERROR, AddLease(NULL, &lease)); 307 EXPECT_EQ(RET_ERROR, AddLease(&testPool, NULL)); 308 EXPECT_EQ(RET_SUCCESS, AddLease(&testPool, &lease)); 309 EXPECT_EQ(RET_SUCCESS, RemoveLease(&testPool, &lease)); 310 } 311 312 HWTEST_F(DhcpAddressPoolTest, GetLeaseTest, TestSize.Level1) 313 { 314 AddressBinding lease = {0}; 315 lease.bindingMode = BIND_MODE_DYNAMIC; 316 lease.bindingStatus = BIND_ASSOCIATED; 317 const uint8_t testMac1[DHCP_HWADDR_LENGTH] = {0x01, 0x0e, 0x3c, 0x65, 0x3a, 0x09, 0}; 318 uint32_t testIp1 = ParseIpAddr("192.168.100.101"); 319 uint32_t testIp2 = ParseIpAddr("192.168.100.110"); 320 ASSERT_TRUE(testIp1 != 0); 321 ASSERT_TRUE(testIp2 != 0); 322 lease.ipAddress = testIp1; 323 lease.leaseTime = DHCP_LEASE_TIME; 324 lease.pendingTime = 1631240659; 325 lease.bindingTime = 1631240659; 326 for (int i = 0; i < MAC_ADDR_LENGTH; ++i) { 327 lease.chaddr[i] = testMac1[i]; 328 } 329 EXPECT_EQ(RET_SUCCESS, AddLease(&testPool, &lease)); 330 EXPECT_EQ(RET_SUCCESS, AddLease(&testPool, &lease)); 331 EXPECT_EQ(NULL, GetLease(&testPool, 0)); 332 EXPECT_EQ(NULL, GetLease(NULL, testIp1)); 333 EXPECT_EQ(NULL, GetLease(&testPool, testIp2)); 334 AddressBinding *leaseRec = GetLease(&testPool, testIp1); 335 ASSERT_TRUE(leaseRec != NULL); 336 EXPECT_EQ(lease.ipAddress, leaseRec->ipAddress); 337 EXPECT_EQ(lease.leaseTime, leaseRec->leaseTime); 338 EXPECT_EQ(lease.bindingMode, leaseRec->bindingMode); 339 EXPECT_EQ(lease.bindingStatus, leaseRec->bindingStatus); 340 EXPECT_EQ(RET_SUCCESS, RemoveLease(&testPool, &lease)); 341 } 342 343 HWTEST_F(DhcpAddressPoolTest, UpdateLeaseTest, TestSize.Level1) 344 { 345 AddressBinding lease = {0}; 346 lease.bindingMode = BIND_MODE_DYNAMIC; 347 lease.bindingStatus = BIND_ASSOCIATED; 348 const uint8_t testMac1[DHCP_HWADDR_LENGTH] = {0x01, 0x0e, 0x3c, 0x65, 0x3a, 0x09, 0}; 349 uint32_t testIp1 = ParseIpAddr("192.168.100.101"); 350 uint32_t testIp2 = ParseIpAddr("192.168.100.110"); 351 ASSERT_TRUE(testIp1 != 0); 352 ASSERT_TRUE(testIp2 != 0); 353 lease.ipAddress = testIp1; 354 lease.leaseTime = DHCP_LEASE_TIME; 355 lease.pendingTime = 1631240659; 356 lease.bindingTime = 1631240659; 357 for (int i = 0; i < MAC_ADDR_LENGTH; ++i) { 358 lease.chaddr[i] = testMac1[i]; 359 } 360 EXPECT_EQ(RET_SUCCESS, AddLease(&testPool, &lease)); 361 AddressBinding *leaseRec = GetLease(&testPool, testIp1); 362 ASSERT_TRUE(leaseRec != NULL); 363 EXPECT_EQ(lease.ipAddress, leaseRec->ipAddress); 364 EXPECT_EQ(lease.leaseTime, leaseRec->leaseTime); 365 EXPECT_EQ(lease.bindingMode, leaseRec->bindingMode); 366 EXPECT_EQ(lease.bindingStatus, leaseRec->bindingStatus); 367 lease.pendingTime = 1631260680; 368 lease.bindingTime = 1631260680; 369 EXPECT_EQ(RET_SUCCESS, UpdateLease(&testPool, &lease)); 370 EXPECT_EQ(lease.leaseTime, leaseRec->leaseTime); 371 EXPECT_EQ(lease.leaseTime, leaseRec->leaseTime); 372 EXPECT_EQ(RET_ERROR, UpdateLease(NULL, &lease)); 373 EXPECT_EQ(RET_ERROR, UpdateLease(&testPool, NULL)); 374 lease.ipAddress = testIp2; 375 EXPECT_EQ(RET_FAILED, UpdateLease(&testPool, &lease)); 376 lease.ipAddress = testIp1; 377 EXPECT_EQ(RET_SUCCESS, RemoveLease(&testPool, &lease)); 378 } 379 380 381 HWTEST_F(DhcpAddressPoolTest, LoadBindingRecodersTest, TestSize.Level1) 382 { 383 AddressBinding lease = {0}; 384 uint32_t testIp1 = ParseIpAddr("192.168.100.101"); 385 ASSERT_TRUE(testIp1 != 0); 386 uint32_t testIp2 = ParseIpAddr("192.168.100.102"); 387 ASSERT_TRUE(testIp2 != 0); 388 uint32_t testIp3 = ParseIpAddr("192.168.100.103"); 389 ASSERT_TRUE(testIp3!= 0); 390 391 lease.bindingMode = BIND_MODE_DYNAMIC; 392 lease.bindingStatus = BIND_ASSOCIATED; 393 lease.pendingTime = 1631260680; 394 lease.bindingTime = 1631260680; 395 396 const uint8_t testMac1[DHCP_HWADDR_LENGTH] = {0x01, 0x0e, 0x3c, 0x65, 0x3a, 0x09, 0}; 397 const uint8_t testMac2[DHCP_HWADDR_LENGTH] = {0x01, 0x0e, 0x3c, 0x65, 0x3a, 0x0a, 0}; 398 const uint8_t testMac3[DHCP_HWADDR_LENGTH] = {0x01, 0x0e, 0x3c, 0x65, 0x3a, 0x0b, 0}; 399 lease.ipAddress = testIp1; 400 for (int i = 0; i < MAC_ADDR_LENGTH; ++i) { 401 lease.chaddr[i] = testMac1[i]; 402 } 403 EXPECT_EQ(RET_SUCCESS, AddLease(&testPool, &lease)); 404 lease.ipAddress = testIp2; 405 for (int i = 0; i < MAC_ADDR_LENGTH; ++i) { 406 lease.chaddr[i] = testMac2[i]; 407 } 408 EXPECT_EQ(RET_SUCCESS, AddLease(&testPool, &lease)); 409 lease.ipAddress = testIp3; 410 for (int i = 0; i < MAC_ADDR_LENGTH; ++i) { 411 lease.chaddr[i] = testMac3[i]; 412 } 413 EXPECT_EQ(RET_SUCCESS, AddLease(&testPool, &lease)); 414 415 EXPECT_EQ(RET_SUCCESS, SaveBindingRecoders(&testPool, 1)); 416 EXPECT_EQ(HASH_SUCCESS, ClearAll(&testPool.leaseTable)); 417 EXPECT_TRUE(testPool.leaseTable.size == 0); 418 EXPECT_EQ(RET_FAILED, LoadBindingRecoders(NULL)); 419 EXPECT_EQ(RET_SUCCESS, LoadBindingRecoders(&testPool)); 420 EXPECT_TRUE(testPool.leaseTable.size == 0); 421 EXPECT_TRUE(GetLease(&testPool, testIp1) == NULL); 422 EXPECT_TRUE(GetLease(&testPool, testIp2) == NULL); 423 EXPECT_TRUE(GetLease(&testPool, testIp3) == NULL); 424 EXPECT_EQ(HASH_SUCCESS, ClearAll(&testPool.leaseTable)); 425 } 426 427 428 extern "C" int CheckRangeAvailability( 429 DhcpAddressPool *pool, uint8_t macAddr[DHCP_HWADDR_LENGTH], uint32_t distIp, int *outOfRange); 430 431 HWTEST_F(DhcpAddressPoolTest, CheckRangeAvailabilityTest, TestSize.Level1) 432 { 433 uint8_t testMac1[DHCP_HWADDR_LENGTH] = {0x01, 0x0e, 0x3c, 0x65, 0x3a, 0x09, 0}; 434 uint8_t testMac2[DHCP_HWADDR_LENGTH] = {0x01, 0x0e, 0x3c, 0x65, 0x3a, 0x0a, 0}; 435 uint8_t testMac3[DHCP_HWADDR_LENGTH] = {0x01, 0x0e, 0x3c, 0x65, 0x3b, 0x0b, 0}; 436 uint8_t testMac4[DHCP_HWADDR_LENGTH] = {0}; 437 438 uint32_t testIp1 = ParseIpAddr("192.168.100.101"); 439 uint32_t testIp2 = ParseIpAddr("192.168.100.102"); 440 uint32_t testIp3 = ParseIpAddr("192.168.100.3"); 441 442 int outOfRange = 0; 443 EXPECT_EQ(RET_ERROR, CheckRangeAvailability(NULL, testMac1, testIp1, &outOfRange)); 444 EXPECT_EQ(RET_ERROR, CheckRangeAvailability(&testPool, testMac1, testIp1, &outOfRange)); 445 EXPECT_TRUE(SamplePoolConfig()); 446 EXPECT_EQ(RET_ERROR, CheckRangeAvailability(&testPool, testMac4, testIp1, &outOfRange)); 447 EXPECT_EQ(0, outOfRange); 448 EXPECT_EQ(RET_SUCCESS, CheckRangeAvailability(&testPool, testMac1, testIp1, &outOfRange)); 449 EXPECT_EQ(0, outOfRange); 450 EXPECT_EQ(RET_SUCCESS, CheckRangeAvailability(&testPool, testMac2, testIp2, &outOfRange)); 451 EXPECT_EQ(0, outOfRange); 452 EXPECT_EQ(RET_FAILED, CheckRangeAvailability(&testPool, testMac3, testIp3, &outOfRange)); 453 EXPECT_EQ(1, outOfRange); 454 } 455 456 extern "C" int CheckIpAvailability(DhcpAddressPool *pool, uint8_t macAddr[DHCP_HWADDR_LENGTH], uint32_t distIp); 457 HWTEST_F(DhcpAddressPoolTest, CheckIpAvailabilityTest, TestSize.Level1) 458 { 459 AddressBinding lease = {0}; 460 lease.bindingMode = BIND_MODE_DYNAMIC; 461 lease.bindingStatus = BIND_ASSOCIATED; 462 uint8_t testMac1[DHCP_HWADDR_LENGTH] = {0x0d, 0x0a, 0x3c, 0x65, 0x3a, 0x09, 0}; 463 uint8_t testMac2[DHCP_HWADDR_LENGTH] = {0x0d, 0x0a, 0x3d, 0x65, 0x3a, 0x0a, 0}; 464 uint8_t testMac3[DHCP_HWADDR_LENGTH] = {0x0d, 0x0a, 0x3d, 0x65, 0x3a, 0x0b, 0}; 465 uint32_t testIp1 = ParseIpAddr("192.168.100.101"); 466 uint32_t testIp2 = ParseIpAddr("192.168.100.102"); 467 uint32_t testIp3 = ParseIpAddr("192.168.150.103"); 468 ASSERT_TRUE(testIp1 != 0); 469 ASSERT_TRUE(testIp2 != 0); 470 ASSERT_TRUE(testIp3 != 0); 471 lease.ipAddress = testIp1; 472 lease.leaseTime = DHCP_LEASE_TIME; 473 lease.pendingTime = 1631240659; 474 lease.bindingTime = 1631240659; 475 for (int i = 0; i < MAC_ADDR_LENGTH; ++i) { 476 lease.chaddr[i] = testMac1[i]; 477 } 478 lease.bindingMode = BIND_MODE_DYNAMIC; 479 lease.bindingStatus = BIND_ASSOCIATED; 480 EXPECT_EQ(RET_SUCCESS, AddBinding(&lease)); 481 ASSERT_EQ(RET_SUCCESS, AddLease(&testPool, &lease)); 482 EXPECT_EQ(DHCP_TRUE, CheckIpAvailability(&testPool, testMac1, testIp1)); 483 AddressBinding *pLease = GetLease(&testPool, testIp1); 484 ASSERT_TRUE(pLease != NULL); 485 pLease->bindingMode = BIND_MODE_STATIC; 486 EXPECT_EQ(DHCP_FALSE, CheckIpAvailability(&testPool, testMac2, testIp1)); 487 pLease->bindingMode = BIND_MODE_DYNAMIC; 488 489 for (int i = 0; i < MAC_ADDR_LENGTH; ++i) { 490 lease.chaddr[i] = testMac2[i]; 491 } 492 lease.ipAddress = testIp2; 493 lease.bindingMode = BIND_MODE_RESERVED; 494 EXPECT_EQ(RET_SUCCESS, AddBinding(&lease)); 495 EXPECT_EQ(RET_SUCCESS, AddLease(&testPool, &lease)); 496 EXPECT_EQ(DHCP_FALSE, CheckIpAvailability(&testPool, testMac2, testIp2)); 497 498 EXPECT_TRUE(SamplePoolConfig()); 499 EXPECT_EQ(DHCP_FALSE, CheckIpAvailability(NULL, testMac1, testIp1)); 500 EXPECT_EQ(DHCP_FALSE, CheckIpAvailability(&testPool, testMac3, testIp1)); 501 502 pLease->pendingTime = Tmspsec() - DHCP_LEASE_TIME - 2600; 503 pLease->bindingTime = pLease->pendingTime; 504 EXPECT_EQ(DHCP_TRUE, CheckIpAvailability(&testPool, testMac3, testIp1)); 505 } 506 507 508 extern "C" AddressBinding *GetBindingByMac(HashTable *bindTable, uint8_t macAddr[DHCP_HWADDR_LENGTH]); 509 510 HWTEST_F(DhcpAddressPoolTest, GetBindingByMacTest, TestSize.Level1) 511 { 512 AddressBinding lease = {0}; 513 uint32_t testIp1 = ParseIpAddr("192.168.100.101"); 514 ASSERT_TRUE(testIp1 != 0); 515 lease.bindingMode = BIND_MODE_DYNAMIC; 516 lease.bindingStatus = BIND_ASSOCIATED; 517 lease.pendingTime = 1631260680; 518 lease.bindingTime = 1631260680; 519 uint8_t testMac1[DHCP_HWADDR_LENGTH] = {0x01, 0x0e, 0x3c, 0x65, 0x3a, 0x09, 0}; 520 lease.ipAddress = testIp1; 521 for (int i = 0; i < MAC_ADDR_LENGTH; ++i) { 522 lease.chaddr[i] = testMac1[i]; 523 } 524 ASSERT_EQ(RET_SUCCESS, AddBinding(&lease)); 525 AddressBinding *binding = QueryBinding(testMac1, 0); 526 ASSERT_TRUE(binding != NULL); 527 HashTable tempTable = {0}; 528 EXPECT_TRUE(GetBindingByMac(NULL, testMac1) == NULL); 529 EXPECT_TRUE(GetBindingByMac(&tempTable, testMac1) == NULL); 530 EXPECT_EQ(lease.ipAddress, binding->ipAddress); 531 EXPECT_EQ(lease.leaseTime, binding->leaseTime); 532 EXPECT_EQ(lease.bindingMode, binding->bindingMode); 533 EXPECT_EQ(lease.bindingStatus, binding->bindingStatus); 534 EXPECT_EQ(RET_SUCCESS, RemoveBinding(testMac1)); 535 } 536 537 HWTEST_F(DhcpAddressPoolTest, GetBindingByIpTest, TestSize.Level1) 538 { 539 AddressBinding lease = {0}; 540 uint32_t testIp1 = ParseIpAddr("192.168.100.101"); 541 ASSERT_TRUE(testIp1 != 0); 542 lease.bindingMode = BIND_MODE_DYNAMIC; 543 lease.bindingStatus = BIND_ASSOCIATED; 544 lease.pendingTime = 1631260680; 545 lease.bindingTime = 1631260680; 546 const uint8_t testMac1[DHCP_HWADDR_LENGTH] = {0x01, 0x0e, 0x3c, 0x65, 0x3a, 0x09, 0}; 547 lease.ipAddress = testIp1; 548 for (int i = 0; i < MAC_ADDR_LENGTH; ++i) { 549 lease.chaddr[i] = testMac1[i]; 550 } 551 ASSERT_EQ(RET_SUCCESS, AddLease(&testPool, &lease)); 552 553 HashTable tempTable = {0}; 554 EXPECT_TRUE(GetBindingByIp(NULL, testIp1) == NULL); 555 EXPECT_TRUE(GetBindingByIp(&tempTable, testIp1) == NULL); 556 AddressBinding *pLease = GetLease(&testPool, testIp1); 557 ASSERT_TRUE(pLease != NULL); 558 EXPECT_EQ(lease.ipAddress, pLease->ipAddress); 559 EXPECT_EQ(lease.leaseTime, pLease->leaseTime); 560 EXPECT_EQ(lease.bindingMode, pLease->bindingMode); 561 EXPECT_EQ(lease.bindingStatus, pLease->bindingStatus); 562 } 563 564 HWTEST_F(DhcpAddressPoolTest, InitAddressPoolTest, TestSize.Level1) 565 { 566 DhcpAddressPool tempPool; 567 ASSERT_TRUE(memset_s(&tempPool, sizeof(DhcpAddressPool), 0, sizeof(DhcpAddressPool)) == EOK); 568 tempPool.fixedOptions.size = 100; 569 EXPECT_EQ(RET_ERROR, InitAddressPool(NULL, "test_if2", NULL)); 570 EXPECT_EQ(RET_SUCCESS, InitAddressPool(&tempPool, "test_if2", NULL)); 571 FreeAddressPool(NULL); 572 FreeAddressPool(&tempPool); 573 } 574 575 extern "C" uint32_t NextIpOffset(uint32_t netmask); 576 HWTEST_F(DhcpAddressPoolTest, NextIpOffsetTest, TestSize.Level1) 577 { 578 uint32_t netmask = ParseIpAddr("255.255.255.0"); 579 ASSERT_TRUE(netmask != 0); 580 SetDistributeMode(0); 581 EXPECT_TRUE(NextIpOffset(netmask) == 0); 582 SetDistributeMode(1); 583 usleep(10); 584 SetDistributeMode(0); 585 } 586 587 HWTEST_F(DhcpAddressPoolTest, RemoveLeaseFailedTest, TestSize.Level1) 588 { 589 AddressBinding lease = {0}; 590 uint32_t testIp1 = ParseIpAddr("192.168.100.110"); 591 ASSERT_TRUE(testIp1 != 0); 592 const uint8_t testMac1[DHCP_HWADDR_LENGTH] = {0x00, 0x0e, 0x3c, 0x65, 0x3a, 0x09, 0}; 593 lease.ipAddress = testIp1; 594 for (int i = 0; i < MAC_ADDR_LENGTH; ++i) { 595 lease.chaddr[i] = testMac1[i]; 596 } 597 EXPECT_EQ(RET_ERROR, RemoveLease(NULL, &lease)); 598 EXPECT_EQ(RET_ERROR, RemoveLease(&testPool, NULL)); 599 EXPECT_EQ(RET_FAILED, RemoveLease(&testPool, &lease)); 600 } 601 /** 602 * @tc.name: SaveBindingRecodersTest 603 * @tc.desc: SaveBindingRecoders() 604 * @tc.type: FUNC 605 * @tc.require: issue 606 */ 607 HWTEST_F(DhcpAddressPoolTest, SaveBindingRecodersTest, TestSize.Level1) 608 { 609 EXPECT_EQ(RET_FAILED, SaveBindingRecoders(NULL, 0)); 610 } 611 } 612 }