]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[#1508] perfdhcp UT sendDiscoverMultiplePRLs
authorAndrei Pavel <andrei@isc.org>
Wed, 9 Dec 2020 11:10:53 +0000 (13:10 +0200)
committerAndrei Pavel <andrei@isc.org>
Wed, 9 Dec 2020 18:01:31 +0000 (20:01 +0200)
src/bin/perfdhcp/test_control.cc
src/bin/perfdhcp/test_control.h
src/bin/perfdhcp/tests/test_control_unittest.cc

index b72737f9179221e1c9f7bc9dcced5590911fb167..6f4ca935ff88d50fd29dd556dc43a792b2aafda8 100644 (file)
@@ -152,7 +152,7 @@ TestControl::copyIaOptions(const Pkt6Ptr& pkt_from, Pkt6Ptr& pkt_to) {
 }
 
 std::string
-TestControl::byte2Hex(const uint8_t b) const {
+TestControl::byte2Hex(const uint8_t b) {
     const int b1 = b / 16;
     const int b0 = b % 16;
     ostringstream stream;
@@ -698,7 +698,7 @@ TestControl::printStats() const {
 
 std::string
 TestControl::vector2Hex(const std::vector<uint8_t>& vec,
-                        const std::string& separator /* ="" */) const {
+                        const std::string& separator /* ="" */) {
     std::ostringstream stream;
     for (std::vector<uint8_t>::const_iterator it = vec.begin();
          it != vec.end();
index 54acd8a9db57cb7e1bd26139ccf48ff9f2860abb..e6a4c24432354770116c0d371347cae2fae6a3d3 100644 (file)
@@ -316,6 +316,24 @@ public:
     std::set<std::string>& getAllUniqueAddrAdvert() {
         return unique_address_;
     }
+
+    /// \brief Convert binary value to hex string.
+    ///
+    /// \todo Consider moving this function to src/lib/util.
+    ///
+    /// \param b byte to convert.
+    /// \return hex string.
+    static std::string byte2Hex(const uint8_t b);
+
+    /// \brief Convert vector in hexadecimal string.
+    ///
+    /// \todo Consider moving this function to src/lib/util.
+    ///
+    /// \param vec vector to be converted.
+    /// \param separator separator.
+    static std::string vector2Hex(const std::vector<uint8_t>& vec,
+                                  const std::string& separator = "");
+
     /// \brief Initialized at first exit condition with the time perfdhcp
     ///  should exit
     boost::posix_time::ptime exit_time_;
@@ -948,14 +966,6 @@ protected:
     /// is NULL.
     void copyIaOptions(const dhcp::Pkt6Ptr& pkt_from, dhcp::Pkt6Ptr& pkt_to);
 
-    /// \brief Convert binary value to hex string.
-    ///
-    /// \todo Consider moving this function to src/lib/util.
-    ///
-    /// \param b byte to convert.
-    /// \return hex string.
-    std::string byte2Hex(const uint8_t b) const;
-
     /// \brief Calculate elapsed time between two packets.
     ///
     /// This function calculates the time elapsed between two packets. If
@@ -999,15 +1009,6 @@ protected:
     /// \return transaction id offset in packet.
     int getTransactionIdOffset(const int arg_idx) const;
 
-    /// \brief Convert vector in hexadecimal string.
-    ///
-    /// \todo Consider moving this function to src/lib/util.
-    ///
-    /// \param vec vector to be converted.
-    /// \param separator separator.
-    std::string vector2Hex(const std::vector<uint8_t>& vec,
-                           const std::string& separator = "") const;
-
     /// \brief Handle child signal.
     ///
     /// Function handles child signal by waiting for
index 37b97d5e3ec4b6d8a1c19de9bb831c703cdf4ff3..7805b9c4418823b54eed407b8074b82e9b543c98 100644 (file)
@@ -20,6 +20,7 @@
 
 #include <boost/date_time/posix_time/posix_time.hpp>
 #include <boost/foreach.hpp>
+#include <boost/format.hpp>
 
 #include <algorithm>
 #include <cstddef>
@@ -185,7 +186,6 @@ public:
             1 : opt.getClientsNum();
         setMacAddrGenerator(NumberGeneratorPtr(new TestControl::SequentialGenerator(clients_num)));
     };
-
 };
 
 
@@ -1095,6 +1095,30 @@ public:
 
     }
 
+    /// @brief Check presence and content of v4 options 55.
+    ///
+    /// \param pkt packet to be checked
+    /// \param expected_option_requests only these option requests should be
+    ///     found under option 55 in the packet, nothing more, nothing less
+    void checkOptions55(Pkt4Ptr const& pkt,
+                        vector<uint8_t> const& expected_option_requests) {
+        // Sanity checks
+        ASSERT_TRUE(pkt);
+        OptionPtr const& opt(pkt->getOption(55));
+        ASSERT_TRUE(opt);
+        EXPECT_TRUE(opt->getUniverse() == Option::V4);
+
+        // Create the text of the expected option.
+        string const length(to_string(expected_option_requests.size()));
+        string const buffer(
+            TestControl::vector2Hex(expected_option_requests, ":"));
+        string const expected_option_text(boost::str(
+            boost::format("type=055, len=%03u: %s") % length % buffer));
+
+        // Compare.
+        EXPECT_EQ(opt->toText(), expected_option_text);
+    }
+
     /// @brief check if v4 options 200 and 201 are present.
     ///
     /// The options are expected to have specific format, as if parameters
@@ -1810,6 +1834,48 @@ TEST_F(TestControlTest, Packet6ExchangeExtraOpts) {
     checkOptions20x(req->second);
 }
 
+// Test checks if multiple v4 PRL options can be sent. They should be merged
+// into a single PRL option by perfdhcp.
+TEST_F(TestControlTest, sendDiscoverMultiplePRLs) {
+    // Important paramters here:
+    // -o 55,1234 - send option 55 with hex content '1234'
+    // -o 55,abcd - send option 55 with hex content 'abcd'
+    CommandOptions opt;
+    processCmdLine(
+        opt, "perfdhcp -4 -l fake -o 55,1234 -o 55,abcd -r 1 -xT 127.0.0.1");
+
+    // Create test control and set up some basic defaults.
+    NakedTestControl tc(opt);
+    tc.registerOptionFactories();
+    NakedTestControl::IncrementalGeneratorPtr gen(
+        boost::make_shared<NakedTestControl::IncrementalGenerator>());
+    tc.setTransidGenerator(gen);
+
+    // Send the packet.
+    tc.sendDiscover4();
+
+    // Let's find the packet and see if it includes the right option.
+    auto const pkt_it(tc.template_packets_v4_.find(DHCPDISCOVER));
+    ASSERT_TRUE(pkt_it != tc.template_packets_v4_.end());
+
+    checkOptions55(pkt_it->second,
+                   {
+                       // Added to all perfdhcp egress packets by default
+                       DHO_SUBNET_MASK,
+                       DHO_BROADCAST_ADDRESS,
+                       DHO_TIME_OFFSET,
+                       DHO_ROUTERS,
+                       DHO_DOMAIN_NAME,
+                       DHO_DOMAIN_NAME_SERVERS,
+                       DHO_HOST_NAME,
+                       // Explicitly added in this test
+                       0x12,
+                       0x34,
+                       0xab,
+                       0xcd,
+                   });
+}
+
 // This test checks if HA failure can be simulated using -y and -Y options with DHCPv4.
 TEST_F(TestControlTest, haFailure4) {
     CommandOptions opt;
@@ -1856,4 +1922,4 @@ TEST_F(TestControlTest, haFailure6) {
 
     EXPECT_EQ(elapsed1->getValue(), 100);
     EXPECT_GT(elapsed2->getValue(), 100);
-}
\ No newline at end of file
+}