From: Marcin Siodelski Date: Fri, 2 Oct 2020 16:57:00 +0000 (+0200) Subject: [#1428] Improve non-unique reservations tests X-Git-Tag: Kea-1.9.1~133 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=27db071b3c583c63108e6e10e8b39e4738df1b65;p=thirdparty%2Fkea.git [#1428] Improve non-unique reservations tests The tests now verify that returned hosts are different. Also added a test for prefixes. As a result of review. --- diff --git a/src/lib/dhcpsrv/tests/cfg_hosts_unittest.cc b/src/lib/dhcpsrv/tests/cfg_hosts_unittest.cc index 5baf7dad26..c1141d9519 100644 --- a/src/lib/dhcpsrv/tests/cfg_hosts_unittest.cc +++ b/src/lib/dhcpsrv/tests/cfg_hosts_unittest.cc @@ -953,9 +953,15 @@ TEST_F(CfgHostsTest, allow4AlreadyReserved) { // Adding this should work because the HW address is different. ASSERT_NO_THROW(cfg.add(host2)); + // Get both hosts. ConstHostCollection returned; ASSERT_NO_THROW(returned = cfg.getAll4(host1->getIPv4SubnetID(), IOAddress("192.0.2.1"))); EXPECT_EQ(2, returned.size()); + + // Make sure the address is the same but the identifiers are different. + EXPECT_NE(returned[0]->getIdentifierAsText(), returned[1]->getIdentifierAsText()); + EXPECT_EQ(returned[0]->getIPv4Reservation().toText(), + returned[1]->getIPv4Reservation().toText()); } // Checks that it's not possible for two hosts to have the same address @@ -986,7 +992,7 @@ TEST_F(CfgHostsTest, add6Invalid2Hosts) { // Test that it is possible to allow inserting multiple reservations for // the same IPv6 address. -TEST_F(CfgHostsTest, allow6AlreadyReserved) { +TEST_F(CfgHostsTest, allowAddress6AlreadyReserved) { CfgHosts cfg; // Allow creating multiple reservations for the same IP address. ASSERT_TRUE(cfg.setIPReservationUnique(false)); @@ -1013,6 +1019,57 @@ TEST_F(CfgHostsTest, allow6AlreadyReserved) { ConstHostCollection returned; ASSERT_NO_THROW(returned = cfg.getAll6(host1->getIPv6SubnetID(), IOAddress("2001:db8::1"))); EXPECT_EQ(2, returned.size()); + + // Make sure the address is the same but the identifiers are different. + EXPECT_NE(returned[0]->getIdentifierAsText(), returned[1]->getIdentifierAsText()); + + auto range0 = returned[0]->getIPv6Reservations(IPv6Resrv::TYPE_NA); + EXPECT_EQ(1, std::distance(range0.first, range0.second)); + auto range1 = returned[1]->getIPv6Reservations(IPv6Resrv::TYPE_NA); + EXPECT_EQ(1, std::distance(range1.first, range1.second)); + EXPECT_EQ(range0.first->second.getPrefix().toText(), + range1.first->second.getPrefix().toText()); +} + +// Test that it is possible to allow inserting multiple reservations for +// the same IPv6 delegated prefix. +TEST_F(CfgHostsTest, allowPrefix6AlreadyReserved) { + CfgHosts cfg; + // Allow creating multiple reservations for the same IP address. + ASSERT_TRUE(cfg.setIPReservationUnique(false)); + + // First host has a reservation for address 3000::/64. + HostPtr host1 = HostPtr(new Host(duids_[0]->toText(), "duid", + SUBNET_ID_UNUSED, SubnetID(1), + IOAddress("0.0.0.0"))); + host1->addReservation(IPv6Resrv(IPv6Resrv::TYPE_PD, + IOAddress("3000::"), 64)); + // Adding this should work. + EXPECT_NO_THROW(cfg.add(host1)); + + // The second host has a reservation for the same address. + HostPtr host2 = HostPtr(new Host(duids_[1]->toText(), "duid", + SUBNET_ID_UNUSED, SubnetID(1), + IOAddress("0.0.0.0"))); + host2->addReservation(IPv6Resrv(IPv6Resrv::TYPE_PD, + IOAddress("3000::"), 64)); + + // Adding this should work because the DUID is different. + ASSERT_NO_THROW(cfg.add(host2)); + + ConstHostCollection returned; + ASSERT_NO_THROW(returned = cfg.getAll6(host1->getIPv6SubnetID(), IOAddress("3000::"))); + EXPECT_EQ(2, returned.size()); + + // Make sure the address is the same but the identifiers are different. + EXPECT_NE(returned[0]->getIdentifierAsText(), returned[1]->getIdentifierAsText()); + + auto range0 = returned[0]->getIPv6Reservations(IPv6Resrv::TYPE_PD); + EXPECT_EQ(1, std::distance(range0.first, range0.second)); + auto range1 = returned[1]->getIPv6Reservations(IPv6Resrv::TYPE_PD); + EXPECT_EQ(1, std::distance(range1.first, range1.second)); + EXPECT_EQ(range0.first->second.getPrefix().toText(), + range1.first->second.getPrefix().toText()); } // Check that no error is reported when adding a host with subnet diff --git a/src/lib/dhcpsrv/tests/host_mgr_unittest.cc b/src/lib/dhcpsrv/tests/host_mgr_unittest.cc index 5facee2ace..214f446881 100644 --- a/src/lib/dhcpsrv/tests/host_mgr_unittest.cc +++ b/src/lib/dhcpsrv/tests/host_mgr_unittest.cc @@ -1189,7 +1189,9 @@ HostMgrTest::testGetAll4BySubnetIP(BaseHostDataSource& data_source1, EXPECT_EQ(1, hosts[0]->getIPv4SubnetID()); EXPECT_EQ(1, hosts[1]->getIPv4SubnetID()); - // Make sure that two hosts were returned. + // Make sure that two hosts were returned with different identifiers + // but the same address. + EXPECT_NE(hosts[0]->getIdentifierAsText(), hosts[1]->getIdentifierAsText()); EXPECT_EQ("192.0.2.5", hosts[0]->getIPv4Reservation().toText()); EXPECT_EQ("192.0.2.5", hosts[1]->getIPv4Reservation().toText()); } @@ -1225,7 +1227,9 @@ HostMgrTest::testGetAll6BySubnetIP(BaseHostDataSource& data_source1, EXPECT_EQ(1, hosts[0]->getIPv6SubnetID()); EXPECT_EQ(1, hosts[1]->getIPv6SubnetID()); - // Make sure that two different hosts were returned. + // Make sure that two hosts were returned with different identifiers + // but the same address. + EXPECT_NE(hosts[0]->getIdentifierAsText(), hosts[1]->getIdentifierAsText()); EXPECT_TRUE(hosts[0]->hasReservation( IPv6Resrv(IPv6Resrv::TYPE_NA, IOAddress("2001:db8:1::5")))); EXPECT_TRUE(hosts[1]->hasReservation(