From: Andrei Pavel Date: Wed, 9 Dec 2020 11:10:53 +0000 (+0200) Subject: [#1508] perfdhcp UT sendDiscoverMultiplePRLs X-Git-Tag: Kea-1.9.3~67 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=cb60e73255ac7dbdd9da4f57a924f03beae2877b;p=thirdparty%2Fkea.git [#1508] perfdhcp UT sendDiscoverMultiplePRLs --- diff --git a/src/bin/perfdhcp/test_control.cc b/src/bin/perfdhcp/test_control.cc index b72737f917..6f4ca935ff 100644 --- a/src/bin/perfdhcp/test_control.cc +++ b/src/bin/perfdhcp/test_control.cc @@ -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& vec, - const std::string& separator /* ="" */) const { + const std::string& separator /* ="" */) { std::ostringstream stream; for (std::vector::const_iterator it = vec.begin(); it != vec.end(); diff --git a/src/bin/perfdhcp/test_control.h b/src/bin/perfdhcp/test_control.h index 54acd8a9db..e6a4c24432 100644 --- a/src/bin/perfdhcp/test_control.h +++ b/src/bin/perfdhcp/test_control.h @@ -316,6 +316,24 @@ public: std::set& 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& 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& vec, - const std::string& separator = "") const; - /// \brief Handle child signal. /// /// Function handles child signal by waiting for diff --git a/src/bin/perfdhcp/tests/test_control_unittest.cc b/src/bin/perfdhcp/tests/test_control_unittest.cc index 37b97d5e3e..7805b9c441 100644 --- a/src/bin/perfdhcp/tests/test_control_unittest.cc +++ b/src/bin/perfdhcp/tests/test_control_unittest.cc @@ -20,6 +20,7 @@ #include #include +#include #include #include @@ -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 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()); + 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 +}