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 "dhcp_address_pool.h" 24 #include "address_utils.h" 25 #include "common_util.h" 26 #include "dhcp_logger.h" 27 #include "securec.h" 28 29 DEFINE_DHCPLOG_DHCP_LABEL("DhcpAddressPoolTest"); 30 31 using namespace testing::ext; 32 namespace OHOS { 33 namespace DHCP { 34 class DhcpAddressPoolTest : public testing::Test { 35 public: SetUpTestCase()36 static void SetUpTestCase() 37 {} TearDownTestCase()38 static void TearDownTestCase() 39 {} SetUp()40 virtual void SetUp() 41 { 42 if (InitAddressPool(&testPool, "lo", NULL)) { 43 printf("failed to initialized address pool.\n"); 44 } 45 } TearDown()46 virtual void TearDown() 47 { 48 ResetPollConfig(); 49 FreeAddressPool(&testPool); 50 } 51 SamplePoolConfig()52 bool SamplePoolConfig() 53 { 54 uint32_t beginIp = ParseIpAddr("192.168.100.100"); 55 uint32_t endIp = ParseIpAddr("192.168.100.150"); 56 uint32_t netmask = ParseIpAddr("255.255.255.0"); 57 uint32_t gateway = ParseIpAddr("192.168.100.254"); 58 uint32_t serverId = ParseIpAddr("192.168.100.1"); 59 if (beginIp != 0 && endIp != 0 && netmask != 0 && gateway != 0) { 60 testPool.addressRange.beginAddress = beginIp; 61 testPool.addressRange.endAddress = endIp; 62 testPool.netmask = netmask; 63 testPool.gateway = gateway; 64 testPool.serverId = serverId; 65 testPool.leaseTime = DHCP_LEASE_TIME; 66 return true; 67 } 68 return false; 69 } 70 ResetPollConfig()71 void ResetPollConfig() 72 { 73 testPool.addressRange.beginAddress = 0; 74 testPool.addressRange.endAddress = 0; 75 testPool.netmask = 0; 76 testPool.gateway = 0; 77 testPool.serverId = 0; 78 testPool.leaseTime = 0; 79 } 80 public: 81 DhcpAddressPool testPool; 82 }; 83 84 HWTEST_F(DhcpAddressPoolTest, AddBindingTest, TestSize.Level1) 85 { 86 DHCP_LOGE("enter AddBindingTest"); 87 AddressBinding bind = {0}; 88 uint8_t testMac1[DHCP_HWADDR_LENGTH] = {0x00, 0x0e, 0x3c, 0x65, 0x3a, 0x09, 0}; 89 uint8_t testMac2[DHCP_HWADDR_LENGTH] = {0x00, 0x0e, 0x3c, 0x65, 0x3a, 0x0a, 0}; 90 uint32_t testIp1 = ParseIpAddr("192.168.100.1"); 91 uint32_t testIp2 = ParseIpAddr("192.168.100.2"); 92 bind.ipAddress = testIp1; 93 for (int i = 0; i < MAC_ADDR_LENGTH; ++i) { 94 bind.chaddr[i] = testMac1[i]; 95 } 96 EXPECT_EQ(RET_SUCCESS, AddBinding(&bind)); 97 bind.ipAddress = testIp2; 98 for (int i = 0; i < MAC_ADDR_LENGTH; ++i) { 99 bind.chaddr[i] = testMac2[i]; 100 } 101 102 EXPECT_EQ(RET_ERROR, AddBinding(NULL)); 103 EXPECT_EQ(RET_SUCCESS, AddBinding(&bind)); 104 EXPECT_EQ(RET_FAILED, AddBinding(&bind)); 105 EXPECT_TRUE(memset_s(bind.chaddr, sizeof(bind.chaddr), 0, sizeof(bind.chaddr)) == EOK); 106 EXPECT_EQ(RET_SUCCESS, RemoveBinding(testMac1)); 107 EXPECT_EQ(RET_SUCCESS, RemoveBinding(testMac2)); 108 } 109 110 HWTEST_F(DhcpAddressPoolTest, AddNewBindingTest, TestSize.Level1) 111 { 112 uint8_t testMac1[DHCP_HWADDR_LENGTH] = {0x00, 0x0e, 0x3c, 0x65, 0x3a, 0x09, 0}; 113 ASSERT_TRUE(testPool.newBinding != NULL); 114 ASSERT_TRUE(testPool.newBinding(testMac1, NULL) != NULL); 115 EXPECT_EQ(RET_SUCCESS, RemoveBinding(testMac1)); 116 } 117 118 HWTEST_F(DhcpAddressPoolTest, FindBindingByIpTest, TestSize.Level1) 119 { 120 AddressBinding bind = {0}; 121 uint8_t testMac1[DHCP_HWADDR_LENGTH] = {0x00, 0x0e, 0x3c, 0x65, 0x3a, 0x09, 0}; 122 uint8_t testMac2[DHCP_HWADDR_LENGTH] = {0x00, 0x0e, 0x3c, 0x65, 0x3a, 0x0a, 0}; 123 uint32_t testIp1 = ParseIpAddr("192.168.100.1"); 124 uint32_t testIp2 = ParseIpAddr("192.168.100.2"); 125 bind.ipAddress = testIp1; 126 for (int i = 0; i < MAC_ADDR_LENGTH; ++i) { 127 bind.chaddr[i] = testMac1[i]; 128 } 129 EXPECT_EQ(RET_SUCCESS, AddBinding(&bind)); 130 bind.ipAddress = testIp2; 131 for (int i = 0; i < MAC_ADDR_LENGTH; ++i) { 132 bind.chaddr[i] = testMac2[i]; 133 } 134 EXPECT_EQ(RET_SUCCESS, AddBinding(&bind)); 135 EXPECT_EQ(RET_FAILED, AddBinding(&bind)); 136 137 AddressBinding *pBind1 = FindBindingByIp(testIp1); 138 AddressBinding *pBind2 = FindBindingByIp(testIp2); 139 EXPECT_TRUE(pBind1 != NULL); 140 EXPECT_TRUE(pBind2 != NULL); 141 142 EXPECT_EQ(RET_SUCCESS, RemoveBinding(testMac1)); 143 EXPECT_EQ(RET_SUCCESS, RemoveBinding(testMac2)); 144 } 145 146 HWTEST_F(DhcpAddressPoolTest, AddressDistributeTest, TestSize.Level1) 147 { 148 uint8_t testMac1[DHCP_HWADDR_LENGTH] = {0x01, 0x0e, 0x3c, 0x65, 0x3a, 0x09, 0}; 149 uint8_t testMac2[DHCP_HWADDR_LENGTH] = {0x01, 0x0e, 0x3c, 0x65, 0x3a, 0x0a, 0}; 150 uint8_t testMac3[DHCP_HWADDR_LENGTH] = {0x01, 0x0e, 0x3c, 0x65, 0x3a, 0x09, 0}; 151 152 EXPECT_TRUE(testPool.distribue(NULL, testMac1) == 0); 153 EXPECT_TRUE(testPool.distribue(&testPool, testMac1) == 0); 154 ASSERT_TRUE(SamplePoolConfig()); 155 SetDistributeMode(0); 156 EXPECT_EQ(0, GetDistributeMode()); 157 uint32_t assertAddr = ParseIpAddr("192.168.100.101"); 158 EXPECT_EQ(assertAddr, testPool.distribue(&testPool, testMac1)); 159 assertAddr = ParseIpAddr("192.168.100.102"); 160 EXPECT_EQ(assertAddr, testPool.distribue(&testPool, testMac2)); 161 assertAddr = ParseIpAddr("192.168.100.103"); 162 EXPECT_EQ(assertAddr, testPool.distribue(&testPool, testMac3)); 163 } 164 165 HWTEST_F(DhcpAddressPoolTest, IsReservedTest, TestSize.Level1) 166 { 167 AddressBinding bind = {0}; 168 uint8_t testMac1[DHCP_HWADDR_LENGTH] = {0x00, 0x0e, 0x3c, 0x65, 0x3a, 0x09, 0}; 169 uint8_t testMac2[DHCP_HWADDR_LENGTH] = {0x00, 0x0e, 0x3c, 0x65, 0x3a, 0x0a, 0}; 170 uint8_t testMac3[DHCP_HWADDR_LENGTH] = {0x00, 0x0e, 0x3c, 0x65, 0x3a, 0x0b, 0}; 171 uint32_t testIp1 = ParseIpAddr("192.168.100.1"); 172 ASSERT_TRUE(testIp1 != 0); 173 uint32_t testIp2 = ParseIpAddr("192.168.100.2"); 174 ASSERT_TRUE(testIp2 != 0); 175 bind.ipAddress = testIp1; 176 for (int i = 0; i < MAC_ADDR_LENGTH; ++i) { 177 bind.chaddr[i] = testMac1[i]; 178 } 179 EXPECT_EQ(RET_SUCCESS, AddBinding(&bind)); 180 bind.ipAddress = testIp2; 181 for (int i = 0; i < MAC_ADDR_LENGTH; ++i) { 182 bind.chaddr[i] = testMac2[i]; 183 } 184 EXPECT_EQ(RET_SUCCESS, AddReservedBinding(testMac2)); 185 EXPECT_EQ(RET_SUCCESS, AddReservedBinding(testMac2)); 186 EXPECT_EQ(RET_FAILED, AddBinding(&bind)); 187 EXPECT_EQ(0, IsReserved(testMac1)); 188 EXPECT_EQ(1, IsReserved(testMac2)); 189 for (int i = 0; i < MAC_ADDR_LENGTH; ++i) { 190 bind.chaddr[i] = testMac3[i]; 191 } 192 EXPECT_EQ(0, IsReserved(testMac3)); 193 EXPECT_EQ(RET_SUCCESS, RemoveBinding(testMac1)); 194 EXPECT_EQ(RET_SUCCESS, RemoveBinding(testMac2)); 195 } 196 197 198 HWTEST_F(DhcpAddressPoolTest, IsReservedIpTest, TestSize.Level1) 199 { 200 AddressBinding bind = {0}; 201 bind.bindingMode = BIND_MODE_DYNAMIC; 202 const uint8_t testMac1[DHCP_HWADDR_LENGTH] = {0x00, 0x0e, 0x3c, 0x65, 0x3a, 0x09, 0}; 203 const uint8_t testMac2[DHCP_HWADDR_LENGTH] = {0x00, 0x0e, 0x3c, 0x65, 0x3a, 0x0a, 0}; 204 uint32_t testIp1 = ParseIpAddr("192.168.100.1"); 205 ASSERT_TRUE(testIp1 != 0); 206 uint32_t testIp2 = ParseIpAddr("192.168.100.2"); 207 ASSERT_TRUE(testIp2 != 0); 208 bind.ipAddress = testIp1; 209 for (int i = 0; i < MAC_ADDR_LENGTH; ++i) { 210 bind.chaddr[i] = testMac1[i]; 211 } 212 EXPECT_EQ(RET_SUCCESS, AddLease(&testPool, &bind)); 213 bind.ipAddress = testIp2; 214 for (int i = 0; i < MAC_ADDR_LENGTH; ++i) { 215 bind.chaddr[i] = testMac2[i]; 216 } 217 bind.bindingMode = BIND_MODE_RESERVED; 218 EXPECT_EQ(RET_SUCCESS, AddLease(&testPool, &bind)); 219 EXPECT_EQ(0, IsReservedIp(&testPool, testIp1)); 220 EXPECT_EQ(1, IsReservedIp(&testPool, testIp2)); 221 EXPECT_EQ(DHCP_FALSE, IsReservedIp(NULL, testIp1)); 222 EXPECT_EQ(DHCP_FALSE, IsReservedIp(&testPool, 0)); 223 bind.ipAddress = testIp1; 224 EXPECT_EQ(RET_SUCCESS, RemoveLease(&testPool, &bind)); 225 bind.ipAddress = testIp2; 226 EXPECT_EQ(RET_SUCCESS, RemoveLease(&testPool, &bind)); 227 } 228 229 HWTEST_F(DhcpAddressPoolTest, RemoveReservedBindingTest, TestSize.Level1) 230 { 231 AddressBinding bind = {0}, bind2 = {0}; 232 uint8_t testMac1[DHCP_HWADDR_LENGTH] = {0x00, 0x01, 0x3c, 0x65, 0x3a, 0x09, 0}; 233 uint8_t testMac2[DHCP_HWADDR_LENGTH] = {0x00, 0x02, 0x3c, 0x65, 0x3a, 0x0a, 0}; 234 uint8_t testMac3[DHCP_HWADDR_LENGTH] = {0x00, 0x03, 0x3c, 0x65, 0x3a, 0x0b, 0}; 235 uint8_t testMac4[DHCP_HWADDR_LENGTH] = {0x00, 0x03, 0x3c, 0x65, 0x3a, 0x0c, 0}; 236 uint32_t testIp1 = ParseIpAddr("192.168.100.1"); 237 EXPECT_TRUE(testIp1 != 0); 238 uint32_t testIp2 = ParseIpAddr("192.168.100.2"); 239 EXPECT_TRUE(testIp2 != 0); 240 bind.ipAddress = testIp1; 241 for (int i = 0; i < MAC_ADDR_LENGTH; ++i) { 242 bind.chaddr[i] = testMac1[i]; 243 } 244 EXPECT_EQ(RET_SUCCESS, AddBinding(&bind)); 245 bind2.ipAddress = testIp2; 246 for (int i = 0; i < MAC_ADDR_LENGTH; ++i) { 247 bind2.chaddr[i] = testMac2[i]; 248 } 249 bind2.bindingMode = BIND_MODE_RESERVED; 250 ASSERT_EQ(RET_SUCCESS, AddBinding(&bind2)); 251 EXPECT_EQ(RET_FAILED, RemoveReservedBinding(testMac1)); 252 EXPECT_EQ(RET_SUCCESS, RemoveBinding(testMac1)); 253 AddressBinding *binding = QueryBinding(testMac2, NULL); 254 ASSERT_TRUE(binding != NULL); 255 EXPECT_EQ(RET_SUCCESS, RemoveReservedBinding(testMac2)); 256 EXPECT_EQ(RET_FAILED, RemoveReservedBinding(testMac2)); 257 EXPECT_EQ(RET_FAILED, RemoveReservedBinding(testMac3)); 258 EXPECT_EQ(RET_FAILED, RemoveReservedBinding(testMac4)); 259 } 260 261 HWTEST_F(DhcpAddressPoolTest, ReleaseBindingTest, TestSize.Level1) 262 { 263 AddressBinding bind = {0}; 264 bind.bindingMode = BIND_ASSOCIATED; 265 uint8_t testMac1[DHCP_HWADDR_LENGTH] = {0x00, 0x0e, 0x3c, 0x65, 0x3a, 0x09, 0}; 266 uint8_t testMac2[DHCP_HWADDR_LENGTH] = {0x00, 0x0e, 0x3c, 0x65, 0x3a, 0x0a, 0}; 267 uint8_t testMac3[DHCP_HWADDR_LENGTH] = {0x00, 0x0e, 0x3c, 0x65, 0x3a, 0x0b, 0}; 268 uint32_t testIp1 = ParseIpAddr("192.168.100.1"); 269 ASSERT_TRUE(testIp1 != 0); 270 uint32_t testIp2 = ParseIpAddr("192.168.100.2"); 271 ASSERT_TRUE(testIp2 != 0); 272 bind.ipAddress = testIp1; 273 for (int i = 0; i < MAC_ADDR_LENGTH; ++i) { 274 bind.chaddr[i] = testMac1[i]; 275 } 276 ASSERT_EQ(RET_SUCCESS, AddBinding(&bind)); 277 bind.ipAddress = testIp2; 278 for (int i = 0; i < MAC_ADDR_LENGTH; ++i) { 279 bind.chaddr[i] = testMac2[i]; 280 } 281 ASSERT_EQ(RET_SUCCESS, AddBinding(&bind)); 282 EXPECT_EQ(RET_SUCCESS, ReleaseBinding(testMac1)); 283 EXPECT_EQ(RET_FAILED, ReleaseBinding(testMac3)); 284 EXPECT_EQ(RET_SUCCESS, RemoveBinding(testMac1)); 285 EXPECT_EQ(RET_SUCCESS, RemoveBinding(testMac2)); 286 } 287 288 HWTEST_F(DhcpAddressPoolTest, AddLeaseTest, TestSize.Level1) 289 { 290 AddressBinding lease = {0}; 291 lease.bindingMode = BIND_MODE_DYNAMIC; 292 lease.bindingStatus = BIND_ASSOCIATED; 293 const uint8_t testMac1[DHCP_HWADDR_LENGTH] = {0x01, 0x0e, 0x3c, 0x65, 0x3a, 0x09, 0}; 294 const uint8_t testMac2[DHCP_HWADDR_LENGTH] = {0x01, 0x0e, 0x3d, 0x65, 0x3a, 0x0a, 0}; 295 uint32_t testIp1 = ParseIpAddr("192.168.100.101"); 296 ASSERT_TRUE(testIp1 != 0); 297 lease.ipAddress = testIp1; 298 lease.leaseTime = DHCP_LEASE_TIME; 299 lease.pendingTime = 1631240659; 300 lease.bindingTime = 1631240659; 301 for (int i = 0; i < MAC_ADDR_LENGTH; ++i) { 302 lease.chaddr[i] = testMac1[i]; 303 } 304 ASSERT_EQ(RET_SUCCESS, AddLease(&testPool, &lease)); 305 for (int i = 0; i < MAC_ADDR_LENGTH; ++i) { 306 lease.chaddr[i] = testMac2[i]; 307 } 308 EXPECT_EQ(RET_ERROR, AddLease(NULL, &lease)); 309 EXPECT_EQ(RET_ERROR, AddLease(&testPool, NULL)); 310 EXPECT_EQ(RET_SUCCESS, AddLease(&testPool, &lease)); 311 EXPECT_EQ(RET_SUCCESS, RemoveLease(&testPool, &lease)); 312 } 313 314 HWTEST_F(DhcpAddressPoolTest, GetLeaseTest, TestSize.Level1) 315 { 316 AddressBinding lease = {0}; 317 lease.bindingMode = BIND_MODE_DYNAMIC; 318 lease.bindingStatus = BIND_ASSOCIATED; 319 const uint8_t testMac1[DHCP_HWADDR_LENGTH] = {0x01, 0x0e, 0x3c, 0x65, 0x3a, 0x09, 0}; 320 uint32_t testIp1 = ParseIpAddr("192.168.100.101"); 321 uint32_t testIp2 = ParseIpAddr("192.168.100.110"); 322 ASSERT_TRUE(testIp1 != 0); 323 ASSERT_TRUE(testIp2 != 0); 324 lease.ipAddress = testIp1; 325 lease.leaseTime = DHCP_LEASE_TIME; 326 lease.pendingTime = 1631240659; 327 lease.bindingTime = 1631240659; 328 for (int i = 0; i < MAC_ADDR_LENGTH; ++i) { 329 lease.chaddr[i] = testMac1[i]; 330 } 331 EXPECT_EQ(RET_SUCCESS, AddLease(&testPool, &lease)); 332 EXPECT_EQ(RET_SUCCESS, AddLease(&testPool, &lease)); 333 EXPECT_EQ(NULL, GetLease(&testPool, 0)); 334 EXPECT_EQ(NULL, GetLease(NULL, testIp1)); 335 EXPECT_EQ(NULL, GetLease(&testPool, testIp2)); 336 AddressBinding *leaseRec = GetLease(&testPool, testIp1); 337 ASSERT_TRUE(leaseRec != NULL); 338 EXPECT_EQ(lease.ipAddress, leaseRec->ipAddress); 339 EXPECT_EQ(lease.leaseTime, leaseRec->leaseTime); 340 EXPECT_EQ(lease.bindingMode, leaseRec->bindingMode); 341 EXPECT_EQ(lease.bindingStatus, leaseRec->bindingStatus); 342 EXPECT_EQ(RET_SUCCESS, RemoveLease(&testPool, &lease)); 343 } 344 345 HWTEST_F(DhcpAddressPoolTest, UpdateLeaseTest, TestSize.Level1) 346 { 347 AddressBinding lease = {0}; 348 lease.bindingMode = BIND_MODE_DYNAMIC; 349 lease.bindingStatus = BIND_ASSOCIATED; 350 const uint8_t testMac1[DHCP_HWADDR_LENGTH] = {0x01, 0x0e, 0x3c, 0x65, 0x3a, 0x09, 0}; 351 uint32_t testIp1 = ParseIpAddr("192.168.100.101"); 352 uint32_t testIp2 = ParseIpAddr("192.168.100.110"); 353 ASSERT_TRUE(testIp1 != 0); 354 ASSERT_TRUE(testIp2 != 0); 355 lease.ipAddress = testIp1; 356 lease.leaseTime = DHCP_LEASE_TIME; 357 lease.pendingTime = 1631240659; 358 lease.bindingTime = 1631240659; 359 for (int i = 0; i < MAC_ADDR_LENGTH; ++i) { 360 lease.chaddr[i] = testMac1[i]; 361 } 362 EXPECT_EQ(RET_SUCCESS, AddLease(&testPool, &lease)); 363 AddressBinding *leaseRec = GetLease(&testPool, testIp1); 364 ASSERT_TRUE(leaseRec != NULL); 365 EXPECT_EQ(lease.ipAddress, leaseRec->ipAddress); 366 EXPECT_EQ(lease.leaseTime, leaseRec->leaseTime); 367 EXPECT_EQ(lease.bindingMode, leaseRec->bindingMode); 368 EXPECT_EQ(lease.bindingStatus, leaseRec->bindingStatus); 369 lease.pendingTime = 1631260680; 370 lease.bindingTime = 1631260680; 371 EXPECT_EQ(RET_SUCCESS, UpdateLease(&testPool, &lease)); 372 EXPECT_EQ(lease.leaseTime, leaseRec->leaseTime); 373 EXPECT_EQ(lease.leaseTime, leaseRec->leaseTime); 374 EXPECT_EQ(RET_ERROR, UpdateLease(NULL, &lease)); 375 EXPECT_EQ(RET_ERROR, UpdateLease(&testPool, NULL)); 376 lease.ipAddress = testIp2; 377 EXPECT_EQ(RET_FAILED, UpdateLease(&testPool, &lease)); 378 lease.ipAddress = testIp1; 379 EXPECT_EQ(RET_SUCCESS, RemoveLease(&testPool, &lease)); 380 } 381 382 383 HWTEST_F(DhcpAddressPoolTest, LoadBindingRecodersTest, TestSize.Level1) 384 { 385 AddressBinding lease = {0}; 386 uint32_t testIp1 = ParseIpAddr("192.168.100.101"); 387 ASSERT_TRUE(testIp1 != 0); 388 uint32_t testIp2 = ParseIpAddr("192.168.100.102"); 389 ASSERT_TRUE(testIp2 != 0); 390 uint32_t testIp3 = ParseIpAddr("192.168.100.103"); 391 ASSERT_TRUE(testIp3!= 0); 392 393 lease.bindingMode = BIND_MODE_DYNAMIC; 394 lease.bindingStatus = BIND_ASSOCIATED; 395 lease.pendingTime = 1631260680; 396 lease.bindingTime = 1631260680; 397 398 const uint8_t testMac1[DHCP_HWADDR_LENGTH] = {0x01, 0x0e, 0x3c, 0x65, 0x3a, 0x09, 0}; 399 const uint8_t testMac2[DHCP_HWADDR_LENGTH] = {0x01, 0x0e, 0x3c, 0x65, 0x3a, 0x0a, 0}; 400 const uint8_t testMac3[DHCP_HWADDR_LENGTH] = {0x01, 0x0e, 0x3c, 0x65, 0x3a, 0x0b, 0}; 401 lease.ipAddress = testIp1; 402 for (int i = 0; i < MAC_ADDR_LENGTH; ++i) { 403 lease.chaddr[i] = testMac1[i]; 404 } 405 EXPECT_EQ(RET_SUCCESS, AddLease(&testPool, &lease)); 406 lease.ipAddress = testIp2; 407 for (int i = 0; i < MAC_ADDR_LENGTH; ++i) { 408 lease.chaddr[i] = testMac2[i]; 409 } 410 EXPECT_EQ(RET_SUCCESS, AddLease(&testPool, &lease)); 411 lease.ipAddress = testIp3; 412 for (int i = 0; i < MAC_ADDR_LENGTH; ++i) { 413 lease.chaddr[i] = testMac3[i]; 414 } 415 EXPECT_EQ(RET_SUCCESS, AddLease(&testPool, &lease)); 416 417 EXPECT_EQ(RET_SUCCESS, SaveBindingRecoders(&testPool, 1)); 418 testPool.leaseTable.clear(); 419 EXPECT_TRUE(testPool.leaseTable.size() == 0); 420 EXPECT_EQ(RET_FAILED, LoadBindingRecoders(NULL)); 421 EXPECT_EQ(RET_SUCCESS, LoadBindingRecoders(&testPool)); 422 EXPECT_TRUE(testPool.leaseTable.size() == 0); 423 EXPECT_TRUE(GetLease(&testPool, testIp1) == NULL); 424 EXPECT_TRUE(GetLease(&testPool, testIp2) == NULL); 425 EXPECT_TRUE(GetLease(&testPool, testIp3) == NULL); 426 testPool.leaseTable.clear(); 427 } 428 429 HWTEST_F(DhcpAddressPoolTest, InitAddressPoolTest, TestSize.Level1) 430 { 431 DhcpAddressPool tempPool; 432 ASSERT_TRUE(memset_s(&tempPool, sizeof(DhcpAddressPool), 0, sizeof(DhcpAddressPool)) == EOK); 433 tempPool.fixedOptions.size = 100; 434 EXPECT_EQ(RET_ERROR, InitAddressPool(NULL, "test_if2", NULL)); 435 EXPECT_EQ(RET_SUCCESS, InitAddressPool(&tempPool, "test_if2", NULL)); 436 FreeAddressPool(NULL); 437 FreeAddressPool(&tempPool); 438 } 439 440 HWTEST_F(DhcpAddressPoolTest, RemoveLeaseFailedTest, TestSize.Level1) 441 { 442 AddressBinding lease = {0}; 443 uint32_t testIp1 = ParseIpAddr("192.168.100.110"); 444 ASSERT_TRUE(testIp1 != 0); 445 const uint8_t testMac1[DHCP_HWADDR_LENGTH] = {0x00, 0x0e, 0x3c, 0x65, 0x3a, 0x09, 0}; 446 lease.ipAddress = testIp1; 447 for (int i = 0; i < MAC_ADDR_LENGTH; ++i) { 448 lease.chaddr[i] = testMac1[i]; 449 } 450 EXPECT_EQ(RET_ERROR, RemoveLease(NULL, &lease)); 451 EXPECT_EQ(RET_ERROR, RemoveLease(&testPool, NULL)); 452 EXPECT_EQ(RET_FAILED, RemoveLease(&testPool, &lease)); 453 } 454 /** 455 * @tc.name: SaveBindingRecodersTest 456 * @tc.desc: SaveBindingRecoders() 457 * @tc.type: FUNC 458 * @tc.require: issue 459 */ 460 HWTEST_F(DhcpAddressPoolTest, SaveBindingRecodersTest, TestSize.Level1) 461 { 462 EXPECT_EQ(RET_FAILED, SaveBindingRecoders(NULL, 0)); 463 } 464 465 HWTEST_F(DhcpAddressPoolTest, DeleteMacInLeaseTest, TestSize.Level1) 466 { 467 DhcpAddressPool pool; 468 AddressBinding lease = {0}; 469 EXPECT_EQ(RET_ERROR, DeleteMacInLease(nullptr, &lease)); 470 EXPECT_EQ(RET_ERROR, DeleteMacInLease(&pool, nullptr)); 471 EXPECT_EQ(RET_SUCCESS, DeleteMacInLease(&pool, &lease)); 472 } 473 } 474 }