Browse Source

[3200] Test that NAK lacks requested options.

Marcin Siodelski 11 years ago
parent
commit
5baa1aeb94

+ 2 - 2
src/bin/dhcp4/dhcp4_messages.mes

@@ -118,7 +118,7 @@ a lease. It is up to the client to choose one server out of othe advertised
 and continue allocation with that server. This is a normal behavior and
 indicates successful operation.
 
-% DHCP4_LEASE_ADVERT_FAIL failed to advertise a lease for client client-id %1, hwaddr %2
+% DHCP4_LEASE_ADVERT_FAIL failed to advertise a lease for client client-id %1, hwaddr %2, yiaddr %3
 This message indicates that the server has failed to offer a lease to
 the specified client after receiving a DISCOVER message from it. There are
 many possible reasons for such a failure.
@@ -128,7 +128,7 @@ This debug message indicates that the server successfully granted a lease
 in response to client's REQUEST message. This is a normal behavior and
 indicates successful operation.
 
-% DHCP4_LEASE_ALLOC_FAIL failed to grant a lease for client-id %1, hwaddr %2
+% DHCP4_LEASE_ALLOC_FAIL failed to grant a lease for client-id %1, hwaddr %2, yiaddr %3
 This message indicates that the server failed to grant a lease to the
 specified client after receiving a REQUEST message from it.  There are many
 possible reasons for such a failure. Additional messages will indicate the

+ 45 - 0
src/bin/dhcp4/tests/dhcp4_test_utils.cc

@@ -22,6 +22,7 @@
 #include <dhcp/option_custom.h>
 #include <dhcp/iface_mgr.h>
 #include <dhcpsrv/cfgmgr.h>
+#include <dhcpsrv/lease.h>
 #include <dhcpsrv/lease_mgr.h>
 #include <dhcpsrv/lease_mgr_factory.h>
 
@@ -156,6 +157,20 @@ void Dhcpv4SrvTest::optionsCheck(const Pkt4Ptr& pkt) {
         << " expected not to be present";
 }
 
+void Dhcpv4SrvTest::noOptionsCheck(const Pkt4Ptr& pkt) {
+    // Check that certain options are not returned in the packet.
+    // This is the case, when client didn't ask for them or when
+    // NAK was returned by the server.
+    EXPECT_FALSE(pkt->getOption(DHO_DOMAIN_NAME))
+        << "domain-name present in the response";
+    EXPECT_FALSE(pkt->getOption(DHO_DOMAIN_NAME_SERVERS))
+        << "dns-servers present in the response";
+    EXPECT_FALSE(pkt->getOption(DHO_LOG_SERVERS))
+        << "log-servers present in the response";
+    EXPECT_FALSE(pkt->getOption(DHO_COOKIE_SERVERS))
+        << "cookie-servers present in the response";
+}
+
 OptionPtr Dhcpv4SrvTest::generateClientId(size_t size /*= 4*/) {
 
     OptionBuffer clnt_id(size);
@@ -412,6 +427,36 @@ void Dhcpv4SrvTest::testDiscoverRequest(const uint8_t msg_type) {
 
     // Check that the requested options are returned.
     optionsCheck(rsp);
+
+    // The following part of the test will test that the NAK is sent when
+    // there is no address pool configured. In the same time, we expect
+    // that the requested options are not included in NAK message, but that
+    // they are only included when yiaddr is set to non-zero value.
+    ASSERT_NO_THROW(subnet_->delPools(Lease::TYPE_V4));
+
+    // There has been a lease allocated for the particular client. So,
+    // even though we deleted the subnet, the client would get the
+    // existing lease (not a NAK). Therefore, we have to change the chaddr
+    // in the packet so as the existing lease is not returned.
+    req->setHWAddr(1, 6, std::vector<uint8_t>(2, 6));
+    ASSERT_TRUE(createPacketFromBuffer(req, received));
+    ASSERT_TRUE(received->getOption(DHO_DHCP_PARAMETER_REQUEST_LIST));
+
+    if (msg_type == DHCPDISCOVER) {
+        ASSERT_NO_THROW(rsp = srv->processDiscover(received));
+        // Should return non-NULL packet.
+        ASSERT_TRUE(rsp);
+    } else {
+        ASSERT_NO_THROW(rsp = srv->processRequest(received));
+        // Should return non-NULL packet.
+        ASSERT_TRUE(rsp);
+    }
+    // We should get the NAK packet with yiaddr set to 0.
+    EXPECT_EQ(DHCPNAK, rsp->getType());
+    ASSERT_EQ("0.0.0.0", rsp->getYiaddr().toText());
+
+    // Make sure that none of the requested options is returned in NAK.
+    noOptionsCheck(rsp);
 }
 
 /// @brief This function cleans up after the test.

+ 5 - 0
src/bin/dhcp4/tests/dhcp4_test_utils.h

@@ -114,6 +114,11 @@ public:
     /// @param pkt packet to be checked.
     void optionsCheck(const Pkt4Ptr& pkt);
 
+    /// @brief Check that certain options are not present.
+    ///
+    /// @param pkt A packet to be checked.
+    void noOptionsCheck(const Pkt4Ptr& pkt);
+
     /// @brief generates client-id option
     ///
     /// Generate client-id option of specified length