From: Marcin Siodelski Date: Thu, 2 Jan 2014 14:05:04 +0000 (+0100) Subject: [3231] Implemented test fixture class to run tests with fake interfaces. X-Git-Tag: bind10-1.2.0beta1-release~159^2~9 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=a1beae5fd28331a41e5947213b353fcfb97b2243;p=thirdparty%2Fkea.git [3231] Implemented test fixture class to run tests with fake interfaces. --- diff --git a/src/bin/dhcp4/tests/dhcp4_test_utils.cc b/src/bin/dhcp4/tests/dhcp4_test_utils.cc index e29c31d2f0..2f94cde47a 100644 --- a/src/bin/dhcp4/tests/dhcp4_test_utils.cc +++ b/src/bin/dhcp4/tests/dhcp4_test_utils.cc @@ -547,6 +547,36 @@ void Dhcpv4SrvTest::TearDown() { } } +Dhcpv4SrvFakeIfaceTest::Dhcpv4SrvFakeIfaceTest() : Dhcpv4SrvTest() { + IfaceMgr& ifacemgr = IfaceMgr::instance(); + ifacemgr.clearIfaces(); + + ifacemgr.addInterface(createIface("lo", 0, "127.0.0.1")); + ifacemgr.addInterface(createIface("eth0", 0, "192.0.3.1")); + ifacemgr.addInterface(createIface("eth1", 0, "10.0.0.1")); +} + +Dhcpv4SrvFakeIfaceTest::~Dhcpv4SrvFakeIfaceTest() { + IfaceMgr& ifacemgr = IfaceMgr::instance(); + ifacemgr.setPacketFilter(PktFilterPtr(new PktFilterInet())); + ifacemgr.clearIfaces(); + ifacemgr.detectIfaces(); +} + +Iface +Dhcpv4SrvFakeIfaceTest::createIface(const std::string& name, const int ifindex, + const std::string& addr) { + Iface iface(name, ifindex); + iface.addAddress(IOAddress(addr)); + if (name == "lo") { + iface.flag_loopback_ = true; + } + iface.flag_up_ = true; + iface.flag_running_ = true; + iface.inactive4_ = false; + return (iface); +} + }; // end of isc::dhcp::test namespace }; // end of isc::dhcp namespace }; // end of isc namespace diff --git a/src/bin/dhcp4/tests/dhcp4_test_utils.h b/src/bin/dhcp4/tests/dhcp4_test_utils.h index c448a2f80b..d63e2c8e17 100644 --- a/src/bin/dhcp4/tests/dhcp4_test_utils.h +++ b/src/bin/dhcp4/tests/dhcp4_test_utils.h @@ -31,6 +31,8 @@ #include #include +#include + namespace isc { namespace dhcp { namespace test { @@ -71,6 +73,8 @@ public: }; +typedef boost::shared_ptr PktFilterTestPtr; + class Dhcpv4SrvTest : public ::testing::Test { public: @@ -271,6 +275,47 @@ public: std::string valid_iface_; }; +/// @brief Test fixture class to be used for tests which require fake +/// interfaces. +/// +/// The DHCPv4 server must always append the server identifier to its response. +/// The server identifier is typically an IP address assigned to the interface +/// on which the query has been received. The DHCPv4 server uses IfaceMgr to +/// check this address. In order to test this functionality, a set of interfaces +/// must be known to the test. This test fixture class creates a set of well +/// known (fake) interfaces which can be assigned to the test DHCPv4 messages +/// so as the response (including server identifier) can be validated. +/// The real interfaces are removed from the IfaceMgr in the constructor and +/// they are re-assigned in the destructor. +class Dhcpv4SrvFakeIfaceTest : public Dhcpv4SrvTest { +public: + /// @brief Constructor. + /// + /// Creates a set of fake interfaces: + /// - lo, index: 0, address: 127.0.0.1 + /// - eth0, index: 1, address: 192.0.3.1 + /// - eth1, index: 2, address: 10.0.0.1 + /// + /// These interfaces replace the real interfaces detected by the IfaceMgr. + Dhcpv4SrvFakeIfaceTest(); + + /// @brief Destructor. + /// + /// Re-detects the network interfaces. Also, sets the default packet filter + /// class, in case the test has changed it. + virtual ~Dhcpv4SrvFakeIfaceTest(); + + /// @brief Creates an instance of the interface. + /// + /// @param name Name of the interface. + /// @param ifindex Index of the interface. + /// @param addr IP address assigned to the interface, represented as string. + /// + /// @return Iface Instance of the interface. + static Iface createIface(const std::string& name, const int ifindex, + const std::string& addr); +}; + /// @brief "Naked" DHCPv4 server, exposes internal fields class NakedDhcpv4Srv: public Dhcpv4Srv { public: