]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[5437] Created tests for reservation-mode set to "disabled" in DHCPv4.
authorMarcin Siodelski <marcin@isc.org>
Tue, 27 Feb 2018 11:39:14 +0000 (12:39 +0100)
committerMarcin Siodelski <marcin@isc.org>
Tue, 27 Feb 2018 11:39:14 +0000 (12:39 +0100)
src/bin/dhcp4/tests/dora_unittest.cc
src/lib/dhcpsrv/tests/alloc_engine4_unittest.cc

index 88b19127bf3f1ea94e472a3a1fb4dbf53098b4cb..5feaf6904e14b32a6f46e3c1798e5bffe5c3a2ee 100644 (file)
@@ -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<int>(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<int>(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) {
index eb557f5bf569560b03fcda0bcb58e4506d0b4a40..64b7b842bd6803a9f5c7a72eda40270ad36cc246 100644 (file)
@@ -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));