]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[4206_0.9.2] Unit-tests implemented.
authorTomek Mrugalski <tomasz@isc.org>
Tue, 8 Dec 2015 16:16:35 +0000 (17:16 +0100)
committerTomek Mrugalski <tomasz@isc.org>
Tue, 8 Dec 2015 16:16:35 +0000 (17:16 +0100)
src/bin/dhcp4/tests/dhcp4_client.cc
src/bin/dhcp4/tests/dhcp4_client.h
src/bin/dhcp4/tests/dhcp4_srv_unittest.cc
src/bin/dhcp6/tests/dhcp6_client.cc
src/bin/dhcp6/tests/dhcp6_client.h
src/bin/dhcp6/tests/dhcp6_srv_unittest.cc

index 750e6657b24a4b089b9eba5594c6ce002e409fcc..e89ce45c731b531e31d49e1746dbcc0c5ca2a52e 100644 (file)
@@ -205,6 +205,17 @@ Dhcp4Client::createMsg(const uint8_t msg_type) {
     return (msg);
 }
 
+void
+Dhcp4Client::appendExtraOptions() {
+    // If there are any custom options specified, add them all to the message.
+    if (!extra_options_.empty()) {
+        for (OptionCollection::iterator opt = extra_options_.begin();
+             opt != extra_options_.end(); ++opt) {
+            context_.query_->addOption(opt->second);
+        }
+    }
+}
+
 void
 Dhcp4Client::doDiscover(const boost::shared_ptr<IOAddress>& requested_addr) {
     context_.query_ = createMsg(DHCPDISCOVER);
@@ -221,6 +232,9 @@ Dhcp4Client::doDiscover(const boost::shared_ptr<IOAddress>& requested_addr) {
     if (ciaddr_.isSpecified()) {
         context_.query_->setCiaddr(ciaddr_.get());
     }
+
+    appendExtraOptions();
+
     // Send the message to the server.
     sendMsg(context_.query_);
     // Expect response.
@@ -450,6 +464,11 @@ Dhcp4Client::setHWAddress(const std::string& hwaddr_str) {
     }
 }
 
+void
+Dhcp4Client::addExtraOption(const OptionPtr& opt) {
+    extra_options_.insert(std::make_pair(opt->getType(), opt));
+}
+
 } // end of namespace isc::dhcp::test
 } // end of namespace isc::dhcp
 } // end of namespace isc
index 4f50129c50e8da8d9a3b344978dfd4541fe5147b..35572d23dea4c484a474eb007d6b7f1b0458e097 100644 (file)
@@ -348,7 +348,16 @@ public:
     /// in the client's messages.
     isc::util::OptionalValue<asiolink::IOAddress> ciaddr_;
 
+    /// @brief Adds extra option (an option the client will always send)
+    ///
+    /// @param opt additional option to be sent
+    void addExtraOption(const OptionPtr& opt);
+
 private:
+    /// @brief Appends extra options, previously added with addExtraOption()
+    ///
+    /// Copies options from extra_options_ into outgoing message.
+    void appendExtraOptions();
 
     /// @brief Creates and adds Requested IP Address option to the client's
     /// query.
@@ -454,6 +463,8 @@ private:
     /// @brief Enable relaying messages to the server.
     bool use_relay_;
 
+    /// @brief Extra options the client will send.
+    OptionCollection extra_options_;
 };
 
 } // end of namespace isc::dhcp::test
index 2277b926294250396ea30ba8d04eb5e3141eb2fc..221cc63da54175d27a9dd015ab49c65ec5e8852a 100644 (file)
@@ -19,6 +19,7 @@
 #include <cc/command_interpreter.h>
 #include <config/command_mgr.h>
 #include <dhcp4/tests/dhcp4_test_utils.h>
+#include <dhcp4/tests/dhcp4_client.h>
 #include <dhcp/tests/pkt_captures.h>
 #include <dhcp/dhcp4.h>
 #include <dhcp/iface_mgr.h>
@@ -3409,4 +3410,26 @@ TEST_F(Dhcpv4SrvTest, statisticsUnknownRcvd) {
     EXPECT_EQ(1, drop_stat->getInteger().first);
 }
 
+// This test verifies that the server is able to handle an empty client-id
+// in incoming client message.
+TEST_F(Dhcpv4SrvTest, emptyClientId) {
+    Dhcp4Client client;
+
+    EXPECT_NO_THROW(configure(CONFIGS[0], *client.getServer()));
+
+    // Tell the client to not send client-id on its own.
+    client.includeClientId("");
+
+    // Instead, tell him to send this extra option, which happens to be
+    // an empty client-id.
+    OptionPtr empty_client_id(new Option(Option::V4, DHO_DHCP_CLIENT_IDENTIFIER));
+    client.addExtraOption(empty_client_id);
+
+    // Let's check whether the server is able to process this packet without
+    // throwing any exceptions. We don't care whether the server sent any
+    // responses or not. The goal is to check that the server didn't throw
+    // any exceptions.
+    EXPECT_NO_THROW(client.doDORA());
+}
+
 }; // end of anonymous namespace
index 9de4755c8b034cf659fe713baef18f524e62f54c..0576d199732d0bdb6807ab3d1ca5883a2190e4c5 100644 (file)
@@ -280,6 +280,14 @@ Dhcp6Client::createMsg(const uint8_t msg_type) {
         msg->addOption(oro);
     };
 
+    // If there are any custom options specified, add them all to the message.
+    if (!extra_options_.empty()) {
+        for (OptionCollection::iterator opt = extra_options_.begin();
+             opt != extra_options_.end(); ++opt) {
+            msg->addOption(opt->second);
+        }
+    }
+
     return (msg);
 }
 
@@ -561,7 +569,10 @@ Dhcp6Client::useFQDN(const uint8_t flags, const std::string& fqdn_name,
     fqdn_.reset(new Option6ClientFqdn(flags, fqdn_name, fqdn_type));
 }
 
-
+void
+Dhcp6Client::addExtraOption(const OptionPtr& opt) {
+    extra_options_.insert(std::make_pair(opt->getType(), opt));
+}
 
 } // end of namespace isc::dhcp::test
 } // end of namespace isc::dhcp
index 35462321ce5a00f48aef9d20d77420ce6cdfde68..86039626de830f37228fd94889bfdf6ee75c3a76 100644 (file)
@@ -504,6 +504,10 @@ public:
         return (duid_);
     }
 
+    /// @brief Adds extra option (an option the client will always send)
+    ///
+    /// @param opt additional option to be sent
+    void addExtraOption(const OptionPtr& opt);
 private:
 
     /// @brief Applies the new leases for the client.
@@ -628,6 +632,9 @@ private:
     /// @brief forced (Overridden) value of the server-id option (may be NULL)
     OptionPtr forced_server_id_;
 
+    /// @brief Extra options the client will send.
+    OptionCollection extra_options_;
+
     /// @brief FQDN requested by the client.
     Option6ClientFqdnPtr fqdn_;
 };
index 2013b289184a03be4a491b59fde7e21ea17cb4af..bd82bd600a60f135a1bd22ccd304bc5f9473b3cd 100644 (file)
@@ -2411,6 +2411,75 @@ TEST_F(Dhcpv6SrvTest, receiveParseFailedStat) {
     EXPECT_EQ(1, recv_drop->getInteger().first);
 }
 
+// This test verifies that the server is able to handle an empty DUID (client-id)
+// in incoming client message.
+TEST_F(Dhcpv6SrvTest, emptyClientId) {
+    Dhcp6Client client;
+
+    // The following configuration enables RSOO options: 110 and 120.
+    // It also configures the server with option 120 which should
+    // "override" the option 120 sent in the RSOO by the relay.
+    string config =
+        "{"
+        "    \"preferred-lifetime\": 3000,"
+        "    \"rebind-timer\": 2000, "
+        "    \"renew-timer\": 1000, "
+        "    \"subnet6\": [ { "
+        "        \"pools\": [ { \"pool\": \"2001:db8::/64\" } ],"
+        "        \"subnet\": \"2001:db8::/48\" "
+        "     } ],"
+        "    \"valid-lifetime\": 4000"
+        "}";
+
+    EXPECT_NO_THROW(configure(config, *client.getServer()));
+
+    // Tell the client to not send client-id on its own.
+    client.useClientId(false);
+
+    // Instead, tell him to send this extra option, which happens to be
+    // an empty client-id.
+    OptionPtr empty_client_id(new Option(Option::V6, D6O_CLIENTID));
+    client.addExtraOption(empty_client_id);
+
+    // Let's check whether the server is able to process this packet without
+    // throwing any exceptions. We don't care whether the server sent any
+    // responses or not. The goal is to check that the server didn't throw
+    // any exceptions.
+    EXPECT_NO_THROW(client.doSARR());
+}
+
+// This test verifies that the server is able to handle an empty DUID (server-id)
+// in incoming client message.
+TEST_F(Dhcpv6SrvTest, emptyServerId) {
+    Dhcp6Client client;
+
+    // The following configuration enables RSOO options: 110 and 120.
+    // It also configures the server with option 120 which should
+    // "override" the option 120 sent in the RSOO by the relay.
+    string config =
+        "{"
+        "    \"preferred-lifetime\": 3000,"
+        "    \"rebind-timer\": 2000, "
+        "    \"renew-timer\": 1000, "
+        "    \"subnet6\": [ { "
+        "        \"pools\": [ { \"pool\": \"2001:db8::/64\" } ],"
+        "        \"subnet\": \"2001:db8::/48\" "
+        "     } ],"
+        "    \"valid-lifetime\": 4000"
+        "}";
+
+    EXPECT_NO_THROW(configure(config, *client.getServer()));
+
+    // Tell the client to use this specific server-id.
+    OptionPtr empty_server_id(new Option(Option::V6, D6O_SERVERID));
+    client.useServerId(empty_server_id);
+
+    // Let's check whether the server is able to process this packet without
+    // throwing any exceptions. We don't care whether the server sent any
+    // responses or not. The goal is to check that the server didn't throw
+    // any exceptions.
+    EXPECT_NO_THROW(client.doSARR());
+}
 
 /// @todo: Add more negative tests for processX(), e.g. extend sanityCheck() test
 /// to call processX() methods.