From: Marcin Siodelski Date: Tue, 27 Feb 2018 11:39:14 +0000 (+0100) Subject: [5437] Created tests for reservation-mode set to "disabled" in DHCPv4. X-Git-Tag: ha_checkpoints12~10^2~8 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=41cd8fa68baf0bd33ec97873cab10b51ad8b4faf;p=thirdparty%2Fkea.git [5437] Created tests for reservation-mode set to "disabled" in DHCPv4. --- diff --git a/src/bin/dhcp4/tests/dora_unittest.cc b/src/bin/dhcp4/tests/dora_unittest.cc index 88b19127bf..5feaf6904e 100644 --- a/src/bin/dhcp4/tests/dora_unittest.cc +++ b/src/bin/dhcp4/tests/dora_unittest.cc @@ -409,6 +409,42 @@ const char* DORA_CONFIGS[] = { " ]" "} ]" "}", + +// Configuration 12 + "{ \"interfaces-config\": {" + " \"interfaces\": [ \"*\" ]" + "}," + "\"valid-lifetime\": 600," + "\"subnet4\": [ { " + " \"subnet\": \"10.0.0.0/24\"," + " \"pools\": [ { \"pool\": \"10.0.0.10-10.0.0.100\" } ]," + " \"reservation-mode\": \"all\"," + " \"reservations\": [ " + " {" + " \"hw-address\": \"aa:bb:cc:dd:ee:ff\"," + " \"ip-address\": \"10.0.0.65\"" + " }" + " ]" + "} ]" + "}", + +// Configuration 13 + "{ \"interfaces-config\": {" + " \"interfaces\": [ \"*\" ]" + "}," + "\"valid-lifetime\": 600," + "\"subnet4\": [ { " + " \"subnet\": \"10.0.0.0/24\"," + " \"pools\": [ { \"pool\": \"10.0.0.10-10.0.0.100\" } ]," + " \"reservation-mode\": \"disabled\"," + " \"reservations\": [ " + " {" + " \"hw-address\": \"aa:bb:cc:dd:ee:ff\"," + " \"ip-address\": \"10.0.0.65\"" + " }" + " ]" + "} ]" + "}" }; /// @brief Test fixture class for testing 4-way (DORA) exchanges. @@ -1454,6 +1490,69 @@ TEST_F(DORATest, reservationsWithConflicts) { ASSERT_EQ(in_pool_addr, clientB.config_.lease_.addr_); } +// This test verifies that the allocation engine ignores reservations when +// reservation-mode is set to "disabled". +TEST_F(DORATest, reservationModeDisabled) { + // Client has a reservation. + Dhcp4Client client(Dhcp4Client::SELECTING); + // Set explicit HW address so as it matches the reservation in the + // configuration used below. + client.setHWAddress("aa:bb:cc:dd:ee:ff"); + // Configure DHCP server. In this configuration the reservation mode is + // set to disabled. Thus, the server should ignore the reservation for + // this client. + configure(DORA_CONFIGS[13], *client.getServer()); + // Client requests the 10.0.0.50 address and the server should assign it + // as it ignores the reservation in the current mode. + ASSERT_NO_THROW(client.doDORA(boost::shared_ptr< + IOAddress>(new IOAddress("10.0.0.50")))); + // Make sure that the server responded. + ASSERT_TRUE(client.getContext().response_); + Pkt4Ptr resp = client.getContext().response_; + // Make sure that the server has responded with DHCPACK. + ASSERT_EQ(DHCPACK, static_cast(resp->getType())); + + // Check that the requested IP address was assigned. + ASSERT_EQ("10.0.0.50", client.config_.lease_.addr_.toText()); + + // Reconfigure the server to respect the host reservations. + configure(DORA_CONFIGS[12], *client.getServer()); + + // The client requests the previously allocated address again, but the + // server should allocate the reserved address this time. + ASSERT_NO_THROW(client.doDORA(boost::shared_ptr< + IOAddress>(new IOAddress("10.0.0.50")))); + // Check that the reserved IP address has been assigned. + ASSERT_EQ("10.0.0.65", client.config_.lease_.addr_.toText()); +} + +// This test verifies that allocation engine assigns a reserved address to +// the client which doesn't own this reservation. We want to avoid such +// cases in the real deployments, but this is just a test that the allocation +// engine skips checking if the reservation exists when it allocates an +// address. In the real deployment the reservation simply wouldn't exist. +TEST_F(DORATest, reservationModeDisabledAddressHijacking) { + // Client has a reservation. + Dhcp4Client client(Dhcp4Client::SELECTING); + // Set MAC address which doesn't match the reservation configured. + client.setHWAddress("11:22:33:44:55:66"); + // Configure DHCP server. In this configuration the reservation mode is + // set to disabled. Any client should be able to hijack the reserved + // address. + configure(DORA_CONFIGS[13], *client.getServer()); + // Client requests the 10.0.0.65 address reserved for another client. + ASSERT_NO_THROW(client.doDORA(boost::shared_ptr< + IOAddress>(new IOAddress("10.0.0.65")))); + // Make sure that the server responded. + ASSERT_TRUE(client.getContext().response_); + Pkt4Ptr resp = client.getContext().response_; + // Make sure that the server has responded with DHCPACK. + ASSERT_EQ(DHCPACK, static_cast(resp->getType())); + + // Check that the address was hijacked. + ASSERT_EQ("10.0.0.65", client.config_.lease_.addr_.toText()); +} + /// This test verifies that after a client completes its DORA exchange, /// appropriate statistics are updated. TEST_F(DORATest, statisticsDORA) { diff --git a/src/lib/dhcpsrv/tests/alloc_engine4_unittest.cc b/src/lib/dhcpsrv/tests/alloc_engine4_unittest.cc index eb557f5bf5..64b7b842bd 100644 --- a/src/lib/dhcpsrv/tests/alloc_engine4_unittest.cc +++ b/src/lib/dhcpsrv/tests/alloc_engine4_unittest.cc @@ -2285,6 +2285,11 @@ TEST_F(AllocEngine4Test, findReservation) { EXPECT_TRUE(ctx.currentHost()); EXPECT_EQ(ctx.currentHost()->getIPv4Reservation(), host->getIPv4Reservation()); + // It shouldn't be returned when HR_DISABLED mode is enabled. + subnet_->setHostReservationMode(Network::HR_DISABLED); + ASSERT_NO_THROW(engine.findReservation(ctx)); + EXPECT_FALSE(ctx.currentHost()); + // Check the out of the pool reservation mode. subnet_->setHostReservationMode(Network::HR_OUT_OF_POOL); ASSERT_NO_THROW(engine.findReservation(ctx));