From: Marcin Siodelski Date: Mon, 9 Nov 2015 16:32:08 +0000 (+0100) Subject: [4106] Implemented test for the DHCPv4 endpoint of DHCPv4o6 IPC. X-Git-Tag: trac4296_base~2^2~4 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=7c29cafaaa975fe7c78d0e7eee967fd5c7f5d96d;p=thirdparty%2Fkea.git [4106] Implemented test for the DHCPv4 endpoint of DHCPv4o6 IPC. --- diff --git a/src/bin/dhcp4/tests/Makefile.am b/src/bin/dhcp4/tests/Makefile.am index 9967247f69..609de7043e 100644 --- a/src/bin/dhcp4/tests/Makefile.am +++ b/src/bin/dhcp4/tests/Makefile.am @@ -92,6 +92,7 @@ dhcp4_unittests_SOURCES += release_unittest.cc dhcp4_unittests_SOURCES += out_of_range_unittest.cc dhcp4_unittests_SOURCES += decline_unittest.cc dhcp4_unittests_SOURCES += kea_controller_unittest.cc +dhcp4_unittests_SOURCES += dhcp4_dhcp4o6_ipc_unittest.cc nodist_dhcp4_unittests_SOURCES = marker_file.h test_libraries.h diff --git a/src/bin/dhcp4/tests/dhcp4_dhcp4o6_ipc_unittest.cc b/src/bin/dhcp4/tests/dhcp4_dhcp4o6_ipc_unittest.cc new file mode 100644 index 0000000000..149adc0901 --- /dev/null +++ b/src/bin/dhcp4/tests/dhcp4_dhcp4o6_ipc_unittest.cc @@ -0,0 +1,115 @@ +// Copyright (C) 2015 Internet Systems Consortium, Inc. ("ISC") +// +// Permission to use, copy, modify, and/or distribute this software for any +// purpose with or without fee is hereby granted, provided that the above +// copyright notice and this permission notice appear in all copies. +// +// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH +// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY +// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, +// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM +// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE +// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR +// PERFORMANCE OF THIS SOFTWARE. + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace isc; +using namespace isc::dhcp; +using namespace isc::dhcp::test; +using namespace isc::util; + +namespace { + +/// @brief Port number used in tests. +const uint16_t TEST_PORT = 32000; + +/// @brief Define short name for the test IPC. +typedef Dhcp4o6TestIpc TestIpc; + +/// @brief Test fixture class for DHCPv4 endpoint of DHCPv4o6 IPC. +class Dhcp4o6IpcTest : public ::testing::Test { +public: + + /// @brief Constructor + /// + /// Configures IPC to use a test port. It also provides a fake + /// configuration of interfaces. + Dhcp4o6IpcTest() + : iface_mgr_test_config_(true) { + configurePort(TEST_PORT); + } + + /// @brief Configure DHCP4o6 port. + /// + /// @param port New port. + void configurePort(const uint16_t port); + + /// @brief Creates an instance of the DHCPv4o6 Message option. + /// + /// @return Pointer to the instance of the DHCPv4-query Message option. + OptionPtr createDHCPv4MsgOption() const; + +private: + + /// @brief Provides fake configuration of interfaces. + IfaceMgrTestConfig iface_mgr_test_config_; + +}; + +void +Dhcp4o6IpcTest::configurePort(const uint16_t port) { + CfgMgr::instance().getStagingCfg()->setDhcp4o6Port(port); +} + +OptionPtr +Dhcp4o6IpcTest::createDHCPv4MsgOption() const { + // Create the DHCPv4 message. + Pkt4Ptr pkt(new Pkt4(DHCPREQUEST, 1234)); + // Make a wire representation of the DHCPv4 message. + pkt->pack(); + OutputBuffer& output_buffer = pkt->getBuffer(); + const uint8_t* data = static_cast(output_buffer.getData()); + OptionBuffer option_buffer(data, data + output_buffer.getLength()); + + // Create the DHCPv4 Message option holding the created message. + OptionPtr opt_msg(new Option(Option::V6, D6O_DHCPV4_MSG, option_buffer)); + return (opt_msg); +} + +// This test verifies that the DHCPv4 endpoint of the DHCPv4o6 IPC can +// receive messages. +TEST_F(Dhcp4o6IpcTest, receive) { + // Create instance of the IPC endpoint under test. + Dhcp4o6Ipc& ipc = Dhcp4o6Ipc::instance(); + // Create instance of the IPC endpoint being used as a source of messages. + TestIpc src_ipc(TEST_PORT, TestIpc::ENDPOINT_TYPE_V6); + + // Open both endpoints. + ASSERT_NO_THROW(ipc.open()); + ASSERT_NO_THROW(src_ipc.open()); + + // Create message to be sent over IPC. + Pkt6Ptr pkt(new Pkt6(DHCPV6_DHCPV4_QUERY, 1234)); + pkt->addOption(createDHCPv4MsgOption()); + pkt->setIface("eth0"); + ASSERT_NO_THROW(pkt->pack()); + + // Send and wait up to 1 second to receive it. + ASSERT_NO_THROW(src_ipc.send(pkt)); + ASSERT_NO_THROW(IfaceMgr::instance().receive6(1, 0)); + + // Make sure that the message has been received. + Pkt4o6Ptr pkt_received = ipc.getReceived(); + ASSERT_TRUE(pkt_received); +} + +} // end of anonymous namespace diff --git a/src/lib/dhcpsrv/tests/dhcp4o6_ipc_unittest.cc b/src/lib/dhcpsrv/tests/dhcp4o6_ipc_unittest.cc index 812bfb5084..6ebd767440 100644 --- a/src/lib/dhcpsrv/tests/dhcp4o6_ipc_unittest.cc +++ b/src/lib/dhcpsrv/tests/dhcp4o6_ipc_unittest.cc @@ -120,10 +120,10 @@ protected: static Pkt6Ptr createDHCPv4o6MsgWithAnyVendorOption(const uint16_t msg_type, const uint16_t postfix); - /// @brief Creates an instance of the DHCPv4-query Message option. + /// @brief Creates an instance of the DHCPv4o6 Message option. /// /// @param src Type of the source endpoint. It can be 4 or 6. - /// @return Pointer to the instance of the DHCPv4-query Message option. + /// @return Pointer to the instance of the option. static OptionPtr createDHCPv4MsgOption(const TestIpc::EndpointType& src); /// @brief Tests sending and receiving packets over the IPC.