diff --git a/configure.ac b/configure.ac index 4d554da8e9..f3d804857e 100644 --- a/configure.ac +++ b/configure.ac @@ -1606,6 +1606,7 @@ AC_CONFIG_FILES([src/lib/database/Makefile]) AC_CONFIG_FILES([src/lib/database/tests/Makefile]) AC_CONFIG_FILES([src/lib/database/testutils/Makefile]) AC_CONFIG_FILES([src/lib/dhcp/Makefile]) +AC_CONFIG_FILES([src/lib/dhcp/testutils/Makefile]) AC_CONFIG_FILES([src/lib/dhcp/tests/Makefile]) AC_CONFIG_FILES([src/lib/dhcp_ddns/Makefile]) AC_CONFIG_FILES([src/lib/dhcp_ddns/tests/Makefile]) diff --git a/src/bin/dhcp4/tests/Makefile.am b/src/bin/dhcp4/tests/Makefile.am index 8a2c084089..b4c4c2db63 100644 --- a/src/bin/dhcp4/tests/Makefile.am +++ b/src/bin/dhcp4/tests/Makefile.am @@ -120,7 +120,7 @@ dhcp4_unittests_LDADD += $(top_builddir)/src/lib/dhcp_ddns/libkea-dhcp_ddns.la dhcp4_unittests_LDADD += $(top_builddir)/src/lib/stats/libkea-stats.la dhcp4_unittests_LDADD += $(top_builddir)/src/lib/config/libkea-cfgclient.la dhcp4_unittests_LDADD += $(top_builddir)/src/lib/http/libkea-http.la -dhcp4_unittests_LDADD += $(top_builddir)/src/lib/dhcp/tests/libdhcptest.la +dhcp4_unittests_LDADD += $(top_builddir)/src/lib/dhcp/testutils/libdhcptest.la dhcp4_unittests_LDADD += $(top_builddir)/src/lib/dhcp/libkea-dhcp++.la dhcp4_unittests_LDADD += $(top_builddir)/src/lib/hooks/libkea-hooks.la diff --git a/src/bin/dhcp4/tests/classify_unittest.cc b/src/bin/dhcp4/tests/classify_unittest.cc index 2d06a4985e..cf9dbb7c8a 100644 --- a/src/bin/dhcp4/tests/classify_unittest.cc +++ b/src/bin/dhcp4/tests/classify_unittest.cc @@ -7,7 +7,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/src/bin/dhcp4/tests/config_backend_unittest.cc b/src/bin/dhcp4/tests/config_backend_unittest.cc index f3aa486976..5010177871 100644 --- a/src/bin/dhcp4/tests/config_backend_unittest.cc +++ b/src/bin/dhcp4/tests/config_backend_unittest.cc @@ -10,7 +10,7 @@ #include #include -#include +#include #include #include #include diff --git a/src/bin/dhcp4/tests/config_parser_unittest.cc b/src/bin/dhcp4/tests/config_parser_unittest.cc index 3cdcf86f8d..a0dde7828d 100644 --- a/src/bin/dhcp4/tests/config_parser_unittest.cc +++ b/src/bin/dhcp4/tests/config_parser_unittest.cc @@ -18,7 +18,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/src/bin/dhcp4/tests/ctrl_dhcp4_srv_unittest.cc b/src/bin/dhcp4/tests/ctrl_dhcp4_srv_unittest.cc index e9fdcc072e..a47ca2c581 100644 --- a/src/bin/dhcp4/tests/ctrl_dhcp4_srv_unittest.cc +++ b/src/bin/dhcp4/tests/ctrl_dhcp4_srv_unittest.cc @@ -13,7 +13,7 @@ #include #include #include -#include +#include #include #include #include @@ -86,7 +86,7 @@ private: class NakedControlledDhcpv4Srv: public ControlledDhcpv4Srv { // "Naked" DHCPv4 server, exposes internal fields public: - NakedControlledDhcpv4Srv():ControlledDhcpv4Srv(0) { + NakedControlledDhcpv4Srv() : ControlledDhcpv4Srv(0) { CfgMgr::instance().setFamily(AF_INET); } @@ -103,13 +103,16 @@ public: /// @brief Path to the UNIX socket being used to communicate with the server std::string socket_path_; + /// @brief List of interfaces (defaults to "*"). + std::string interfaces_; + /// @brief Pointer to the tested server object boost::shared_ptr server_; /// @brief Default constructor /// /// Sets socket path to its default value. - CtrlChannelDhcpv4SrvTest() { + CtrlChannelDhcpv4SrvTest() : interfaces_("\"*\"") { const char* env = getenv("KEA_SOCKET_TEST_DIR"); if (env) { socket_path_ = string(env) + "/kea4.sock"; @@ -117,6 +120,8 @@ public: socket_path_ = sandbox.join("kea4.sock"); } reset(); + IfaceMgr::instance().setTestMode(false); + IfaceMgr::instance().setDetectCallback(isc::dhcp::IfaceMgr::DetectCallback()); } /// @brief Destructor @@ -129,6 +134,11 @@ public: CommandMgr::instance().setConnectionTimeout(TIMEOUT_DHCP_SERVER_RECEIVE_COMMAND); server_.reset(); + IfaceMgr::instance().setTestMode(false); + IfaceMgr::instance().setDetectCallback(isc::dhcp::IfaceMgr::DetectCallback()); + IfaceMgr::instance().clearIfaces(); + IfaceMgr::instance().closeSockets(); + IfaceMgr::instance().detectIfaces(); }; /// @brief Returns pointer to the server's IO service. @@ -147,7 +157,9 @@ public: std::string header = "{" " \"interfaces-config\": {" - " \"interfaces\": [ \"*\" ]" + " \"interfaces\": ["; + + std::string body = "]" " }," " \"expired-leases-processing\": {" " \"reclaim-timer-wait-time\": 60," @@ -175,8 +187,7 @@ public: // Fill in the socket-name value with socket_path_ to // make the actual configuration text. - std::string config_txt = header + socket_path_ + footer; - + std::string config_txt = header + interfaces_ + body + socket_path_ + footer; ASSERT_NO_THROW(server_.reset(new NakedControlledDhcpv4Srv())); ConstElementPtr config; @@ -210,7 +221,6 @@ public: ASSERT_GT(isc::config::CommandMgr::instance().getControlSocketFD(), -1); } - /// @brief Reset hooks data /// /// Resets the data for the hooks-related portion of the test by ensuring @@ -817,7 +827,7 @@ TEST_F(CtrlChannelDhcpv4SrvTest, configSet) { sendUnixCommand(os.str(), response); // Verify the configuration was successful. The config contains random - // socket name (/tmp/kea-/kea6.sock), so the + // socket name (/tmp/kea-/kea4.sock), so the // hash will be different each time. As such, we can do simplified checks: // - verify the "result": 0 is there // - verify the "text": "Configuration successful." is there @@ -885,7 +895,7 @@ TEST_F(CtrlChannelDhcpv4SrvTest, configSet) { EXPECT_FALSE(fileExists(socket_path_)); // With no command channel, should still receive the response. The config contains random - // socket name (/tmp/kea-/kea6.sock), so the + // socket name (/tmp/kea-/kea4.sock), so the // hash will be different each time. As such, we can do simplified checks: // - verify the "result": 0 is there // - verify the "text": "Configuration successful." is there @@ -1030,7 +1040,7 @@ TEST_F(CtrlChannelDhcpv4SrvTest, configTest) { sendUnixCommand(os.str(), response); // Verify the configuration was successful. The config contains random - // socket name (/tmp/kea-/kea6.sock), so the + // socket name (/tmp/kea-/kea4.sock), so the // hash will be different each time. As such, we can do simplified checks: // - verify the "result": 0 is there // - verify the "text": "Configuration successful." is there @@ -1508,6 +1518,7 @@ TEST_F(CtrlChannelDhcpv4SrvTest, configWriteFilename) { sendUnixCommand("{ \"command\": \"config-write\", " "\"arguments\": { \"filename\": \"test2.json\" } }", response); + checkConfigWrite(response, CONTROL_RESULT_SUCCESS, "test2.json"); ::remove("test2.json"); } @@ -1591,8 +1602,83 @@ TEST_F(CtrlChannelDhcpv4SrvTest, configReloadValid) { // This command should reload test8.json config. sendUnixCommand("{ \"command\": \"config-reload\" }", response); + // Verify the configuration was successful. The config contains random - // socket name (/tmp/kea-/kea6.sock), so the + // socket name (/tmp/kea-/kea4.sock), so the + // hash will be different each time. As such, we can do simplified checks: + // - verify the "result": 0 is there + // - verify the "text": "Configuration successful." is there + EXPECT_NE(response.find("\"result\": 0"), std::string::npos); + EXPECT_NE(response.find("\"text\": \"Configuration successful.\""), std::string::npos); + + // Check that the config was indeed applied. + const Subnet4Collection* subnets = + CfgMgr::instance().getCurrentCfg()->getCfgSubnets4()->getAll(); + EXPECT_EQ(2, subnets->size()); + + ::remove("test8.json"); +} + +// Tests if config-reload attempts to reload a file and reports that the +// file is loaded correctly. +TEST_F(CtrlChannelDhcpv4SrvTest, configReloadDetectInterfaces) { + interfaces_ = "\"eth0\""; + IfacePtr eth0 = IfaceMgrTestConfig::createIface("eth0", 0); + auto detectIfaces = [&](bool update_only) { + if (!update_only) { + eth0->addAddress(IOAddress("10.0.0.1")); + eth0->addAddress(IOAddress("fe80::3a60:77ff:fed5:cdef")); + eth0->addAddress(IOAddress("2001:db8:1::1")); + IfaceMgr::instance().addInterface(eth0); + } + return (false); + }; + IfaceMgr::instance().setDetectCallback(detectIfaces); + IfaceMgr::instance().clearIfaces(); + IfaceMgr::instance().closeSockets(); + IfaceMgr::instance().detectIfaces(); + createUnixChannelServer(); + std::string response; + + // This is normally set to whatever value is passed to -c when the server is + // started, but we're not starting it that way, so need to set it by hand. + server_->setConfigFile("test8.json"); + + // Ok, enough fooling around. Let's create a valid config. + const std::string cfg_txt = + "{ \"Dhcp4\": {" + " \"interfaces-config\": {" + " \"interfaces\": [ \"eth1\" ]" + " }," + " \"subnet4\": [" + " { \"id\": 1, \"subnet\": \"192.0.2.0/24\" }," + " { \"id\": 2, \"subnet\": \"192.0.3.0/24\" }" + " ]," + " \"valid-lifetime\": 4000," + " \"lease-database\": {" + " \"type\": \"memfile\", \"persist\": false }" + "} }"; + ofstream f("test8.json", ios::trunc); + f << cfg_txt; + f.close(); + + IfacePtr eth1 = IfaceMgrTestConfig::createIface("eth1", 1); + auto detectUpdateIfaces = [&](bool update_only) { + if (!update_only) { + eth1->addAddress(IOAddress("192.0.2.3")); + eth1->addAddress(IOAddress("fe80::3a60:77ff:fed5:abcd")); + eth1->addAddress(IOAddress("3001:db8:100::1")); + IfaceMgr::instance().addInterface(eth1); + } + return (false); + }; + IfaceMgr::instance().setDetectCallback(detectUpdateIfaces); + + // This command should reload test8.json config. + sendUnixCommand("{ \"command\": \"config-reload\" }", response); + + // Verify the configuration was successful. The config contains random + // socket name (/tmp/kea-/kea4.sock), so the // hash will be different each time. As such, we can do simplified checks: // - verify the "result": 0 is there // - verify the "text": "Configuration successful." is there diff --git a/src/bin/dhcp4/tests/decline_unittest.cc b/src/bin/dhcp4/tests/decline_unittest.cc index ef3bc1d8da..782b3aaf65 100644 --- a/src/bin/dhcp4/tests/decline_unittest.cc +++ b/src/bin/dhcp4/tests/decline_unittest.cc @@ -9,7 +9,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/src/bin/dhcp4/tests/dhcp4_client.cc b/src/bin/dhcp4/tests/dhcp4_client.cc index b054a0d2aa..151c23229b 100644 --- a/src/bin/dhcp4/tests/dhcp4_client.cc +++ b/src/bin/dhcp4/tests/dhcp4_client.cc @@ -9,7 +9,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/src/bin/dhcp4/tests/dhcp4_srv_unittest.cc b/src/bin/dhcp4/tests/dhcp4_srv_unittest.cc index 7a1c4604a2..5917972ec8 100644 --- a/src/bin/dhcp4/tests/dhcp4_srv_unittest.cc +++ b/src/bin/dhcp4/tests/dhcp4_srv_unittest.cc @@ -15,7 +15,7 @@ #include #include #include -#include +#include #include #include #include @@ -26,7 +26,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/src/bin/dhcp4/tests/dhcp4_test_utils.cc b/src/bin/dhcp4/tests/dhcp4_test_utils.cc index 045fe6fb67..c3aa7c44c8 100644 --- a/src/bin/dhcp4/tests/dhcp4_test_utils.cc +++ b/src/bin/dhcp4/tests/dhcp4_test_utils.cc @@ -17,8 +17,8 @@ #include #include #include -#include -#include +#include +#include #include #include #include diff --git a/src/bin/dhcp4/tests/dhcp4to6_ipc_unittest.cc b/src/bin/dhcp4/tests/dhcp4to6_ipc_unittest.cc index 2a47d71821..b59d825f58 100644 --- a/src/bin/dhcp4/tests/dhcp4to6_ipc_unittest.cc +++ b/src/bin/dhcp4/tests/dhcp4to6_ipc_unittest.cc @@ -9,7 +9,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/src/bin/dhcp4/tests/direct_client_unittest.cc b/src/bin/dhcp4/tests/direct_client_unittest.cc index 27439e2baa..913885ea63 100644 --- a/src/bin/dhcp4/tests/direct_client_unittest.cc +++ b/src/bin/dhcp4/tests/direct_client_unittest.cc @@ -8,7 +8,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/src/bin/dhcp4/tests/dora_unittest.cc b/src/bin/dhcp4/tests/dora_unittest.cc index be2c7cf25a..9520f14d2b 100644 --- a/src/bin/dhcp4/tests/dora_unittest.cc +++ b/src/bin/dhcp4/tests/dora_unittest.cc @@ -8,7 +8,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/src/bin/dhcp4/tests/fqdn_unittest.cc b/src/bin/dhcp4/tests/fqdn_unittest.cc index a030d8846f..d7e92bcb6b 100644 --- a/src/bin/dhcp4/tests/fqdn_unittest.cc +++ b/src/bin/dhcp4/tests/fqdn_unittest.cc @@ -9,7 +9,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/src/bin/dhcp4/tests/get_config_unittest.cc b/src/bin/dhcp4/tests/get_config_unittest.cc index e67c5a13c3..575caabae5 100644 --- a/src/bin/dhcp4/tests/get_config_unittest.cc +++ b/src/bin/dhcp4/tests/get_config_unittest.cc @@ -11,7 +11,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/src/bin/dhcp4/tests/get_config_unittest.cc.skel b/src/bin/dhcp4/tests/get_config_unittest.cc.skel index a25a5abbbb..b30ba0b058 100644 --- a/src/bin/dhcp4/tests/get_config_unittest.cc.skel +++ b/src/bin/dhcp4/tests/get_config_unittest.cc.skel @@ -11,7 +11,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/src/bin/dhcp4/tests/hooks_unittest.cc b/src/bin/dhcp4/tests/hooks_unittest.cc index 474f1ca341..27746cb6cf 100644 --- a/src/bin/dhcp4/tests/hooks_unittest.cc +++ b/src/bin/dhcp4/tests/hooks_unittest.cc @@ -10,7 +10,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/src/bin/dhcp4/tests/host_options_unittest.cc b/src/bin/dhcp4/tests/host_options_unittest.cc index db6ccb1bff..ae6cbe6964 100644 --- a/src/bin/dhcp4/tests/host_options_unittest.cc +++ b/src/bin/dhcp4/tests/host_options_unittest.cc @@ -10,7 +10,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/src/bin/dhcp4/tests/host_unittest.cc b/src/bin/dhcp4/tests/host_unittest.cc index 5a39c85b94..3eb81b897f 100644 --- a/src/bin/dhcp4/tests/host_unittest.cc +++ b/src/bin/dhcp4/tests/host_unittest.cc @@ -8,7 +8,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/src/bin/dhcp4/tests/inform_unittest.cc b/src/bin/dhcp4/tests/inform_unittest.cc index ed3114f0c5..50697d5717 100644 --- a/src/bin/dhcp4/tests/inform_unittest.cc +++ b/src/bin/dhcp4/tests/inform_unittest.cc @@ -9,7 +9,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/src/bin/dhcp4/tests/out_of_range_unittest.cc b/src/bin/dhcp4/tests/out_of_range_unittest.cc index b0dba8d44e..8b86d59619 100644 --- a/src/bin/dhcp4/tests/out_of_range_unittest.cc +++ b/src/bin/dhcp4/tests/out_of_range_unittest.cc @@ -9,7 +9,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/src/bin/dhcp4/tests/release_unittest.cc b/src/bin/dhcp4/tests/release_unittest.cc index c59bd45d86..8acc433843 100644 --- a/src/bin/dhcp4/tests/release_unittest.cc +++ b/src/bin/dhcp4/tests/release_unittest.cc @@ -8,7 +8,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/src/bin/dhcp4/tests/shared_network_unittest.cc b/src/bin/dhcp4/tests/shared_network_unittest.cc index a5cefbe90a..67bac00040 100644 --- a/src/bin/dhcp4/tests/shared_network_unittest.cc +++ b/src/bin/dhcp4/tests/shared_network_unittest.cc @@ -8,7 +8,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/src/bin/dhcp4/tests/vendor_opts_unittest.cc b/src/bin/dhcp4/tests/vendor_opts_unittest.cc index cf38afcf70..44553b5a93 100644 --- a/src/bin/dhcp4/tests/vendor_opts_unittest.cc +++ b/src/bin/dhcp4/tests/vendor_opts_unittest.cc @@ -27,13 +27,13 @@ #include #include #include -#include +#include #include #include #include #include #include -#include +#include #include #include #include diff --git a/src/bin/dhcp6/tests/Makefile.am b/src/bin/dhcp6/tests/Makefile.am index 35e16918e4..f7853b32e1 100644 --- a/src/bin/dhcp6/tests/Makefile.am +++ b/src/bin/dhcp6/tests/Makefile.am @@ -124,7 +124,7 @@ dhcp6_unittests_LDADD += $(top_builddir)/src/lib/dhcp_ddns/libkea-dhcp_ddns.la dhcp6_unittests_LDADD += $(top_builddir)/src/lib/stats/libkea-stats.la dhcp6_unittests_LDADD += $(top_builddir)/src/lib/config/libkea-cfgclient.la dhcp6_unittests_LDADD += $(top_builddir)/src/lib/http/libkea-http.la -dhcp6_unittests_LDADD += $(top_builddir)/src/lib/dhcp/tests/libdhcptest.la +dhcp6_unittests_LDADD += $(top_builddir)/src/lib/dhcp/testutils/libdhcptest.la dhcp6_unittests_LDADD += $(top_builddir)/src/lib/dhcp/libkea-dhcp++.la dhcp6_unittests_LDADD += $(top_builddir)/src/lib/hooks/libkea-hooks.la diff --git a/src/bin/dhcp6/tests/classify_unittests.cc b/src/bin/dhcp6/tests/classify_unittests.cc index fdc82cab8d..c04c58a944 100644 --- a/src/bin/dhcp6/tests/classify_unittests.cc +++ b/src/bin/dhcp6/tests/classify_unittests.cc @@ -10,12 +10,12 @@ #include #include #include -#include +#include #include #include #include #include -#include +#include #include #include #include diff --git a/src/bin/dhcp6/tests/config_backend_unittest.cc b/src/bin/dhcp6/tests/config_backend_unittest.cc index e2d87762d2..f1427ce5f8 100644 --- a/src/bin/dhcp6/tests/config_backend_unittest.cc +++ b/src/bin/dhcp6/tests/config_backend_unittest.cc @@ -12,7 +12,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/src/bin/dhcp6/tests/config_parser_unittest.cc b/src/bin/dhcp6/tests/config_parser_unittest.cc index dcc5b609a9..8ef97b887b 100644 --- a/src/bin/dhcp6/tests/config_parser_unittest.cc +++ b/src/bin/dhcp6/tests/config_parser_unittest.cc @@ -14,7 +14,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/src/bin/dhcp6/tests/confirm_unittest.cc b/src/bin/dhcp6/tests/confirm_unittest.cc index f100c2ba62..4776714a9b 100644 --- a/src/bin/dhcp6/tests/confirm_unittest.cc +++ b/src/bin/dhcp6/tests/confirm_unittest.cc @@ -7,7 +7,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/src/bin/dhcp6/tests/ctrl_dhcp6_srv_unittest.cc b/src/bin/dhcp6/tests/ctrl_dhcp6_srv_unittest.cc index 0b981e86f3..46372e8ccb 100644 --- a/src/bin/dhcp6/tests/ctrl_dhcp6_srv_unittest.cc +++ b/src/bin/dhcp6/tests/ctrl_dhcp6_srv_unittest.cc @@ -10,7 +10,7 @@ #include #include #include -#include +#include #include #include #include @@ -86,7 +86,7 @@ private: class NakedControlledDhcpv6Srv: public ControlledDhcpv6Srv { // "Naked" DHCPv6 server, exposes internal fields public: - NakedControlledDhcpv6Srv():ControlledDhcpv6Srv(DHCP6_SERVER_PORT + 10000) { + NakedControlledDhcpv6Srv() : ControlledDhcpv6Srv(DHCP6_SERVER_PORT + 10000) { CfgMgr::instance().setFamily(AF_INET6); } @@ -114,7 +114,6 @@ public: reset(); }; - /// @brief Reset hooks data /// /// Resets the data for the hooks-related portion of the test by ensuring @@ -139,13 +138,16 @@ public: /// @brief Path to the UNIX socket being used to communicate with the server std::string socket_path_; + /// @brief List of interfaces (defaults to "*"). + std::string interfaces_; + /// @brief Pointer to the tested server object boost::shared_ptr server_; /// @brief Default constructor /// /// Sets socket path to its default value. - CtrlChannelDhcpv6SrvTest() { + CtrlChannelDhcpv6SrvTest() : interfaces_("\"*\"") { const char* env = getenv("KEA_SOCKET_TEST_DIR"); if (env) { socket_path_ = string(env) + "/kea6.sock"; @@ -153,12 +155,19 @@ public: socket_path_ = sandbox.join("/kea6.sock"); } reset(); + IfaceMgr::instance().setTestMode(false); + IfaceMgr::instance().setDetectCallback(isc::dhcp::IfaceMgr::DetectCallback()); } /// @brief Destructor ~CtrlChannelDhcpv6SrvTest() { server_.reset(); reset(); + IfaceMgr::instance().setTestMode(false); + IfaceMgr::instance().setDetectCallback(isc::dhcp::IfaceMgr::DetectCallback()); + IfaceMgr::instance().clearIfaces(); + IfaceMgr::instance().closeSockets(); + IfaceMgr::instance().detectIfaces(); }; /// @brief Returns pointer to the server's IO service. @@ -177,7 +186,9 @@ public: std::string header = "{" " \"interfaces-config\": {" - " \"interfaces\": [ \"*\" ]" + " \"interfaces\": ["; + + std::string body = "]" " }," " \"expired-leases-processing\": {" " \"reclaim-timer-wait-time\": 60," @@ -205,8 +216,7 @@ public: // Fill in the socket-name value with socket_path_ to // make the actual configuration text. - std::string config_txt = header + socket_path_ + footer; - + std::string config_txt = header + interfaces_ + body + socket_path_ + footer; ASSERT_NO_THROW(server_.reset(new NakedControlledDhcpv6Srv())); ConstElementPtr config; @@ -1541,6 +1551,7 @@ TEST_F(CtrlChannelDhcpv6SrvTest, configWriteFilename) { sendUnixCommand("{ \"command\": \"config-write\", " "\"arguments\": { \"filename\": \"test2.json\" } }", response); + checkConfigWrite(response, CONTROL_RESULT_SUCCESS, "test2.json"); ::remove("test2.json"); } @@ -1640,6 +1651,79 @@ TEST_F(CtrlChannelDhcpv6SrvTest, configReloadValid) { ::remove("test8.json"); } +// Tests if config-reload attempts to reload a file and reports that the +// file is loaded correctly. +TEST_F(CtrlChannelDhcpv6SrvTest, configReloadDetectInterfaces) { + interfaces_ = "\"eth0\""; + IfacePtr eth0 = IfaceMgrTestConfig::createIface("eth0", 0); + auto detectIfaces = [&](bool update_only) { + if (!update_only) { + eth0->addAddress(IOAddress("10.0.0.1")); + eth0->addAddress(IOAddress("fe80::3a60:77ff:fed5:cdef")); + eth0->addAddress(IOAddress("2001:db8:1::1")); + IfaceMgr::instance().addInterface(eth0); + } + return (false); + }; + IfaceMgr::instance().setDetectCallback(detectIfaces); + IfaceMgr::instance().clearIfaces(); + IfaceMgr::instance().closeSockets(); + IfaceMgr::instance().detectIfaces(); + createUnixChannelServer(); + std::string response; + + // This is normally set to whatever value is passed to -c when the server is + // started, but we're not starting it that way, so need to set it by hand. + server_->setConfigFile("test8.json"); + + // Ok, enough fooling around. Let's create a valid config. + const std::string cfg_txt = + "{ \"Dhcp6\": {" + " \"interfaces-config\": {" + " \"interfaces\": [ \"eth1\" ]" + " }," + " \"subnet6\": [" + " { \"subnet\": \"2001:db8:1::/64\", \"id\": 1 }," + " { \"subnet\": \"2001:db8:2::/64\", \"id\": 2 }" + " ]," + " \"lease-database\": {" + " \"type\": \"memfile\", \"persist\": false }" + "} }"; + ofstream f("test8.json", ios::trunc); + f << cfg_txt; + f.close(); + + IfacePtr eth1 = IfaceMgrTestConfig::createIface("eth1", ETH1_INDEX); + auto detectUpdateIfaces = [&](bool update_only) { + if (!update_only) { + eth1->addAddress(IOAddress("192.0.2.3")); + eth1->addAddress(IOAddress("fe80::3a60:77ff:fed5:abcd")); + eth1->addAddress(IOAddress("3001:db8:100::1")); + IfaceMgr::instance().addInterface(eth1); + } + return (false); + }; + IfaceMgr::instance().setDetectCallback(detectUpdateIfaces); + + // This command should reload test8.json config. + sendUnixCommand("{ \"command\": \"config-reload\" }", response); + + // Verify the configuration was successful. The config contains random + // socket name (/tmp/kea-/kea6.sock), so the + // hash will be different each time. As such, we can do simplified checks: + // - verify the "result": 0 is there + // - verify the "text": "Configuration successful." is there + EXPECT_NE(response.find("\"result\": 0"), std::string::npos); + EXPECT_NE(response.find("\"text\": \"Configuration successful.\""), std::string::npos); + + // Check that the config was indeed applied. + const Subnet6Collection* subnets = + CfgMgr::instance().getCurrentCfg()->getCfgSubnets6()->getAll(); + EXPECT_EQ(2, subnets->size()); + + ::remove("test8.json"); +} + // This test verifies that disable DHCP service command performs sanity check on // parameters. TEST_F(CtrlChannelDhcpv6SrvTest, dhcpDisableBadParam) { diff --git a/src/bin/dhcp6/tests/decline_unittest.cc b/src/bin/dhcp6/tests/decline_unittest.cc index b3db3adbcd..92f511d7c2 100644 --- a/src/bin/dhcp6/tests/decline_unittest.cc +++ b/src/bin/dhcp6/tests/decline_unittest.cc @@ -7,7 +7,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/src/bin/dhcp6/tests/dhcp6_client.cc b/src/bin/dhcp6/tests/dhcp6_client.cc index 7021176bf4..2ab1c9d227 100644 --- a/src/bin/dhcp6/tests/dhcp6_client.cc +++ b/src/bin/dhcp6/tests/dhcp6_client.cc @@ -15,7 +15,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/src/bin/dhcp6/tests/dhcp6_message_test.h b/src/bin/dhcp6/tests/dhcp6_message_test.h index 0b7e7adabe..911f0e54a0 100644 --- a/src/bin/dhcp6/tests/dhcp6_message_test.h +++ b/src/bin/dhcp6/tests/dhcp6_message_test.h @@ -8,7 +8,7 @@ #define DHCP6_MESSAGE_TEST_H #include -#include +#include #include #include diff --git a/src/bin/dhcp6/tests/dhcp6_srv_unittest.cc b/src/bin/dhcp6/tests/dhcp6_srv_unittest.cc index 40ee751cae..d3d587a868 100644 --- a/src/bin/dhcp6/tests/dhcp6_srv_unittest.cc +++ b/src/bin/dhcp6/tests/dhcp6_srv_unittest.cc @@ -13,7 +13,7 @@ #include #include #include -#include +#include #include #include #include @@ -25,7 +25,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/src/bin/dhcp6/tests/dhcp6_test_utils.cc b/src/bin/dhcp6/tests/dhcp6_test_utils.cc index 212a4d769d..a2f9acff61 100644 --- a/src/bin/dhcp6/tests/dhcp6_test_utils.cc +++ b/src/bin/dhcp6/tests/dhcp6_test_utils.cc @@ -8,7 +8,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/src/bin/dhcp6/tests/dhcp6_test_utils.h b/src/bin/dhcp6/tests/dhcp6_test_utils.h index b5c1d7befe..202bd8e2f4 100644 --- a/src/bin/dhcp6/tests/dhcp6_test_utils.h +++ b/src/bin/dhcp6/tests/dhcp6_test_utils.h @@ -24,7 +24,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/src/bin/dhcp6/tests/dhcp6to4_ipc_unittest.cc b/src/bin/dhcp6/tests/dhcp6to4_ipc_unittest.cc index ce7e077f7c..0ff86b4bdb 100644 --- a/src/bin/dhcp6/tests/dhcp6to4_ipc_unittest.cc +++ b/src/bin/dhcp6/tests/dhcp6to4_ipc_unittest.cc @@ -9,8 +9,8 @@ #include #include #include -#include -#include +#include +#include #include #include #include diff --git a/src/bin/dhcp6/tests/fqdn_unittest.cc b/src/bin/dhcp6/tests/fqdn_unittest.cc index c10c349757..f3b93930d3 100644 --- a/src/bin/dhcp6/tests/fqdn_unittest.cc +++ b/src/bin/dhcp6/tests/fqdn_unittest.cc @@ -16,7 +16,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/src/bin/dhcp6/tests/get_config_unittest.cc b/src/bin/dhcp6/tests/get_config_unittest.cc index d5691841d7..2a931f3b2d 100644 --- a/src/bin/dhcp6/tests/get_config_unittest.cc +++ b/src/bin/dhcp6/tests/get_config_unittest.cc @@ -11,7 +11,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/src/bin/dhcp6/tests/get_config_unittest.cc.skel b/src/bin/dhcp6/tests/get_config_unittest.cc.skel index 44f3f1e6c0..e9bd4a7fb7 100644 --- a/src/bin/dhcp6/tests/get_config_unittest.cc.skel +++ b/src/bin/dhcp6/tests/get_config_unittest.cc.skel @@ -11,7 +11,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/src/bin/dhcp6/tests/hooks_unittest.cc b/src/bin/dhcp6/tests/hooks_unittest.cc index c34aa763c6..bdf876d0f2 100644 --- a/src/bin/dhcp6/tests/hooks_unittest.cc +++ b/src/bin/dhcp6/tests/hooks_unittest.cc @@ -12,8 +12,8 @@ #include #include #include -#include -#include +#include +#include #include #include #include diff --git a/src/bin/dhcp6/tests/host_unittest.cc b/src/bin/dhcp6/tests/host_unittest.cc index efc3f463d0..8f887f6aa3 100644 --- a/src/bin/dhcp6/tests/host_unittest.cc +++ b/src/bin/dhcp6/tests/host_unittest.cc @@ -10,7 +10,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/src/bin/dhcp6/tests/infrequest_unittest.cc b/src/bin/dhcp6/tests/infrequest_unittest.cc index df3ac76c69..38cce54bdd 100644 --- a/src/bin/dhcp6/tests/infrequest_unittest.cc +++ b/src/bin/dhcp6/tests/infrequest_unittest.cc @@ -5,7 +5,7 @@ // file, You can obtain one at http://mozilla.org/MPL/2.0/. #include -#include +#include #include #include #include diff --git a/src/bin/dhcp6/tests/rebind_unittest.cc b/src/bin/dhcp6/tests/rebind_unittest.cc index 48801e6d96..0c37370da2 100644 --- a/src/bin/dhcp6/tests/rebind_unittest.cc +++ b/src/bin/dhcp6/tests/rebind_unittest.cc @@ -10,7 +10,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/src/bin/dhcp6/tests/renew_unittest.cc b/src/bin/dhcp6/tests/renew_unittest.cc index 644bc970b7..e20867b183 100644 --- a/src/bin/dhcp6/tests/renew_unittest.cc +++ b/src/bin/dhcp6/tests/renew_unittest.cc @@ -11,7 +11,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/src/bin/dhcp6/tests/sarr_unittest.cc b/src/bin/dhcp6/tests/sarr_unittest.cc index 2d379bedfc..055ddd0729 100644 --- a/src/bin/dhcp6/tests/sarr_unittest.cc +++ b/src/bin/dhcp6/tests/sarr_unittest.cc @@ -5,7 +5,7 @@ // file, You can obtain one at http://mozilla.org/MPL/2.0/. #include -#include +#include #include #include #include diff --git a/src/bin/dhcp6/tests/shared_network_unittest.cc b/src/bin/dhcp6/tests/shared_network_unittest.cc index aaa7ff10f5..02a8283e74 100644 --- a/src/bin/dhcp6/tests/shared_network_unittest.cc +++ b/src/bin/dhcp6/tests/shared_network_unittest.cc @@ -10,7 +10,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/src/bin/dhcp6/tests/tee_times_unittest.cc b/src/bin/dhcp6/tests/tee_times_unittest.cc index 1caec5a7b2..95ace971cc 100644 --- a/src/bin/dhcp6/tests/tee_times_unittest.cc +++ b/src/bin/dhcp6/tests/tee_times_unittest.cc @@ -8,7 +8,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/src/bin/dhcp6/tests/vendor_opts_unittest.cc b/src/bin/dhcp6/tests/vendor_opts_unittest.cc index 92234def27..0cbc224752 100644 --- a/src/bin/dhcp6/tests/vendor_opts_unittest.cc +++ b/src/bin/dhcp6/tests/vendor_opts_unittest.cc @@ -23,9 +23,9 @@ #include #include #include -#include +#include +#include #include -#include #include #include diff --git a/src/lib/dhcp/Makefile.am b/src/lib/dhcp/Makefile.am index 4213c449e8..2f4053a6b1 100644 --- a/src/lib/dhcp/Makefile.am +++ b/src/lib/dhcp/Makefile.am @@ -1,4 +1,4 @@ -SUBDIRS = . tests +SUBDIRS = . testutils tests AM_CPPFLAGS = -I$(top_builddir)/src/lib -I$(top_srcdir)/src/lib AM_CPPFLAGS += $(BOOST_INCLUDES) diff --git a/src/lib/dhcp/duid_factory.cc b/src/lib/dhcp/duid_factory.cc index 9b34d15d79..1a023445bd 100644 --- a/src/lib/dhcp/duid_factory.cc +++ b/src/lib/dhcp/duid_factory.cc @@ -296,7 +296,7 @@ DUIDFactory::createLinkLayerId(std::vector& identifier, // used for generating DUID-LLT. if (identifier.empty()) { isc_throw(Unexpected, "unable to find suitable interface for " - " generating a DUID-LLT"); + "generating a DUID-LLT"); } } diff --git a/src/lib/dhcp/iface_mgr.cc b/src/lib/dhcp/iface_mgr.cc index 6bf1a0853f..11f6e2f143 100644 --- a/src/lib/dhcp/iface_mgr.cc +++ b/src/lib/dhcp/iface_mgr.cc @@ -477,44 +477,6 @@ IfaceMgr::hasOpenSocket(const IOAddress& addr) const { return (false); } -void IfaceMgr::stubDetectIfaces() { - string ifaceName; - const string v4addr("127.0.0.1"), v6addr("::1"); - - // This is a stub implementation for interface detection. Actual detection - // is faked by detecting loopback interface (lo or lo0). It will eventually - // be removed once we have actual implementations for all supported systems. - - if (if_nametoindex("lo") > 0) { - ifaceName = "lo"; - // this is Linux-like OS - } else if (if_nametoindex("lo0") > 0) { - ifaceName = "lo0"; - // this is BSD-like OS - } else { - // we give up. What OS is this, anyway? Solaris? Hurd? - isc_throw(NotImplemented, - "Interface detection on this OS is not supported."); - } - - IfacePtr iface(new Iface(ifaceName, if_nametoindex(ifaceName.c_str()))); - iface->flag_up_ = true; - iface->flag_running_ = true; - - // Note that we claim that this is not a loopback. iface_mgr tries to open a - // socket on all interfaces that are up, running and not loopback. As this is - // the only interface we were able to detect, let's pretend this is a normal - // interface. - iface->flag_loopback_ = false; - iface->flag_multicast_ = true; - iface->flag_broadcast_ = true; - iface->setHWType(HWTYPE_ETHERNET); - - iface->addAddress(IOAddress(v4addr)); - iface->addAddress(IOAddress(v6addr)); - addInterface(iface); -} - bool IfaceMgr::openSockets4(const uint16_t port, const bool use_bcast, IfaceMgrErrorMsgCallback error_handler, diff --git a/src/lib/dhcp/iface_mgr.h b/src/lib/dhcp/iface_mgr.h index bccfeee01c..a3b0596ec7 100644 --- a/src/lib/dhcp/iface_mgr.h +++ b/src/lib/dhcp/iface_mgr.h @@ -659,6 +659,14 @@ public: /// @param fd socket descriptor of the ready socket typedef std::function SocketCallback; + /// Defines callback used when detecting interfaces. + /// @param update_only Only add interfaces that do not exist and update + /// existing interfaces. + /// + /// @return true if callback exited with no issue and @ref detectIfaces + /// should continue with specific system calls, false otherwise. + typedef std::function DetectCallback; + /// Keeps callback information for external sockets. struct SocketCallbackInfo { /// Socket descriptor of the external socket. @@ -788,11 +796,18 @@ public: /// @c PktFilter class to mimic socket operation on these interfaces. void clearIfaces(); + /// @brief Set a callback to perform operations before executing specific + /// system calls. + /// + /// @param cb The callback used before executing specific system calls. + void setDetectCallback(const DetectCallback& cb) { + detect_callback_ = cb; + } + /// @brief Detects network interfaces. /// - /// This method will eventually detect available interfaces. For now - /// it offers stub implementation. First interface name and link-local - /// IPv6 address is read from interfaces.txt file. + /// If the @ref detect_callback_ returns true, the specific system calls are + /// executed, otherwise the @ref detectIfaces will return immediately. /// /// @param update_only Only add interfaces that do not exist and update /// existing interfaces. @@ -973,7 +988,6 @@ public: int openSocketFromRemoteAddress(const isc::asiolink::IOAddress& remote_addr, const uint16_t port); - /// @brief Opens IPv6 sockets on detected interfaces. /// /// This method opens sockets only on interfaces which have the @@ -1444,15 +1458,6 @@ protected: /// @return Pkt6 object representing received packet (or null) Pkt6Ptr receive6Indirect(uint32_t timeout_sec, uint32_t timeout_usec = 0); - - /// @brief Stub implementation of network interface detection. - /// - /// This implementations reads a single line from interfaces.txt file - /// and pretends to detect such interface. First interface name and - /// link-local IPv6 address or IPv4 address is read from the - /// interfaces.txt file. - void stubDetectIfaces(); - /// @brief List of available interfaces IfaceCollection ifaces_; @@ -1488,7 +1493,6 @@ private: getLocalAddress(const isc::asiolink::IOAddress& remote_addr, const uint16_t port); - /// @brief Open an IPv6 socket with multicast support. /// /// This function opens a socket capable of receiving messages sent to @@ -1586,6 +1590,13 @@ private: /// @brief Indicates if the IfaceMgr is in the test mode. bool test_mode_; + /// @brief Detect callback used to perform action before system dependent + /// function calls. Currently this function is used in unittests only. + /// + /// If the @ref detect_callback_ returns true, the specific system calls are + /// executed, otherwise the @ref detectIfaces will return immediately. + DetectCallback detect_callback_; + /// @brief Allows to use loopback bool allow_loopback_; diff --git a/src/lib/dhcp/iface_mgr_bsd.cc b/src/lib/dhcp/iface_mgr_bsd.cc index bc77d967f1..3b328cb781 100644 --- a/src/lib/dhcp/iface_mgr_bsd.cc +++ b/src/lib/dhcp/iface_mgr_bsd.cc @@ -31,6 +31,12 @@ namespace dhcp { /// This is a BSD specific interface detection method. void IfaceMgr::detectIfaces(bool update_only) { + if (detect_callback_) { + if (!detect_callback_(update_only)) { + return; + } + } + if (isTestMode() && update_only) { return; } diff --git a/src/lib/dhcp/iface_mgr_linux.cc b/src/lib/dhcp/iface_mgr_linux.cc index 4821da64b6..222e3d648f 100644 --- a/src/lib/dhcp/iface_mgr_linux.cc +++ b/src/lib/dhcp/iface_mgr_linux.cc @@ -414,6 +414,12 @@ namespace dhcp { /// Uses the socket-based netlink protocol to retrieve the list of interfaces /// from the Linux kernel. void IfaceMgr::detectIfaces(bool update_only) { + if (detect_callback_) { + if (!detect_callback_(update_only)) { + return; + } + } + if (isTestMode() && update_only) { return; } diff --git a/src/lib/dhcp/iface_mgr_sun.cc b/src/lib/dhcp/iface_mgr_sun.cc index 56b36e027f..c7d7e461ae 100644 --- a/src/lib/dhcp/iface_mgr_sun.cc +++ b/src/lib/dhcp/iface_mgr_sun.cc @@ -31,6 +31,12 @@ namespace dhcp { /// only, as earlier versions did not support getifaddrs() API. void IfaceMgr::detectIfaces(bool update_only) { + if (detect_callback_) { + if (!detect_callback_(update_only)) { + return; + } + } + if (isTestMode() && update_only) { return; } diff --git a/src/lib/dhcp/tests/Makefile.am b/src/lib/dhcp/tests/Makefile.am index 23d2f42275..87416b8236 100644 --- a/src/lib/dhcp/tests/Makefile.am +++ b/src/lib/dhcp/tests/Makefile.am @@ -18,25 +18,6 @@ TESTS_ENVIRONMENT = $(LIBTOOL) --mode=execute $(VALGRIND_COMMAND) TESTS = if HAVE_GTEST -# Creates a library which is shared by various unit tests which require -# configuration of fake interfaces. -# The libdhcp++ does not link with this library because this would cause -# build failures being a result of concurrency between build of this -# library and the unit tests when make -j option was used, as they -# are built out of the same makefile. Instead, the libdhcp++ tests link to -# files belonging to this library, directly. -noinst_LTLIBRARIES = libdhcptest.la - -libdhcptest_la_SOURCES = iface_mgr_test_config.cc iface_mgr_test_config.h -libdhcptest_la_SOURCES += pkt_filter_test_stub.cc pkt_filter_test_stub.h -libdhcptest_la_SOURCES += pkt_filter6_test_stub.cc pkt_filter6_test_stub.h -libdhcptest_la_SOURCES += pkt_captures4.cc pkt_captures6.cc pkt_captures.h -libdhcptest_la_SOURCES += packet_queue_testutils.h -libdhcptest_la_CXXFLAGS = $(AM_CXXFLAGS) -libdhcptest_la_CPPFLAGS = $(AM_CPPFLAGS) -libdhcptest_la_LDFLAGS = $(AM_LDFLAGS) -libdhcptest_la_LIBADD = $(top_builddir)/src/lib/dhcp/libkea-dhcp++.la - TESTS += libdhcp++_unittests libdhcp___unittests_SOURCES = run_unittests.cc @@ -44,7 +25,6 @@ libdhcp___unittests_SOURCES += classify_unittest.cc libdhcp___unittests_SOURCES += duid_factory_unittest.cc libdhcp___unittests_SOURCES += hwaddr_unittest.cc libdhcp___unittests_SOURCES += iface_mgr_unittest.cc -libdhcp___unittests_SOURCES += iface_mgr_test_config.cc iface_mgr_test_config.h libdhcp___unittests_SOURCES += libdhcp++_unittest.cc libdhcp___unittests_SOURCES += opaque_data_tuple_unittest.cc libdhcp___unittests_SOURCES += option4_addrlst_unittest.cc @@ -71,7 +51,6 @@ libdhcp___unittests_SOURCES += option_space_unittest.cc libdhcp___unittests_SOURCES += option_string_unittest.cc libdhcp___unittests_SOURCES += option_vendor_unittest.cc libdhcp___unittests_SOURCES += option_vendor_class_unittest.cc -libdhcp___unittests_SOURCES += pkt_captures4.cc pkt_captures6.cc pkt_captures.h libdhcp___unittests_SOURCES += packet_queue4_unittest.cc libdhcp___unittests_SOURCES += packet_queue6_unittest.cc libdhcp___unittests_SOURCES += packet_queue_mgr4_unittest.cc @@ -83,8 +62,6 @@ libdhcp___unittests_SOURCES += pkt4o6_unittest.cc libdhcp___unittests_SOURCES += pkt_filter_unittest.cc libdhcp___unittests_SOURCES += pkt_filter_inet_unittest.cc libdhcp___unittests_SOURCES += pkt_filter_inet6_unittest.cc -libdhcp___unittests_SOURCES += pkt_filter_test_stub.cc pkt_filter_test_stub.h -libdhcp___unittests_SOURCES += pkt_filter6_test_stub.cc pkt_filter_test_stub.h libdhcp___unittests_SOURCES += pkt_filter_test_utils.h pkt_filter_test_utils.cc libdhcp___unittests_SOURCES += pkt_filter6_test_utils.h pkt_filter6_test_utils.cc @@ -107,7 +84,8 @@ libdhcp___unittests_LDFLAGS = $(AM_LDFLAGS) $(CRYPTO_LDFLAGS) $(GTEST_LDFLAGS) libdhcp___unittests_CXXFLAGS = $(AM_CXXFLAGS) -libdhcp___unittests_LDADD = $(top_builddir)/src/lib/dhcp/libkea-dhcp++.la +libdhcp___unittests_LDADD = $(top_builddir)/src/lib/dhcp/testutils/libdhcptest.la +libdhcp___unittests_LDADD += $(top_builddir)/src/lib/dhcp/libkea-dhcp++.la libdhcp___unittests_LDADD += $(top_builddir)/src/lib/hooks/libkea-hooks.la libdhcp___unittests_LDADD += $(top_builddir)/src/lib/testutils/libkea-testutils.la libdhcp___unittests_LDADD += $(top_builddir)/src/lib/cc/libkea-cc.la diff --git a/src/lib/dhcp/tests/duid_factory_unittest.cc b/src/lib/dhcp/tests/duid_factory_unittest.cc index 1669983d5d..c5b54f62f0 100644 --- a/src/lib/dhcp/tests/duid_factory_unittest.cc +++ b/src/lib/dhcp/tests/duid_factory_unittest.cc @@ -7,7 +7,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/src/lib/dhcp/tests/iface_mgr_unittest.cc b/src/lib/dhcp/tests/iface_mgr_unittest.cc index 2761319b30..ec411a87a0 100644 --- a/src/lib/dhcp/tests/iface_mgr_unittest.cc +++ b/src/lib/dhcp/tests/iface_mgr_unittest.cc @@ -12,7 +12,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/src/lib/dhcp/tests/pkt4_unittest.cc b/src/lib/dhcp/tests/pkt4_unittest.cc index 70bc624d6b..bed3f860e5 100644 --- a/src/lib/dhcp/tests/pkt4_unittest.cc +++ b/src/lib/dhcp/tests/pkt4_unittest.cc @@ -7,6 +7,7 @@ #include #include +#include #include #include #include @@ -19,7 +20,6 @@ #include #include #include -#include #include #include diff --git a/src/lib/dhcp/tests/pkt6_unittest.cc b/src/lib/dhcp/tests/pkt6_unittest.cc index 616b89482e..c626d05e73 100644 --- a/src/lib/dhcp/tests/pkt6_unittest.cc +++ b/src/lib/dhcp/tests/pkt6_unittest.cc @@ -7,6 +7,7 @@ #include #include +#include #include #include #include @@ -21,7 +22,6 @@ #include #include #include -#include #include #include #include diff --git a/src/lib/dhcp/testutils/Makefile.am b/src/lib/dhcp/testutils/Makefile.am new file mode 100644 index 0000000000..ae75a27d4b --- /dev/null +++ b/src/lib/dhcp/testutils/Makefile.am @@ -0,0 +1,25 @@ +SUBDIRS = . + +AM_CPPFLAGS = -I$(top_builddir)/src/lib -I$(top_srcdir)/src/lib +AM_CPPFLAGS += $(BOOST_INCLUDES) + +AM_CXXFLAGS = $(KEA_CXXFLAGS) + +CLEANFILES = *.gcno *.gcda + +if HAVE_GTEST + +noinst_LTLIBRARIES = libdhcptest.la + +libdhcptest_la_SOURCES = iface_mgr_test_config.cc iface_mgr_test_config.h +libdhcptest_la_SOURCES += pkt_filter_test_stub.cc pkt_filter_test_stub.h +libdhcptest_la_SOURCES += pkt_filter6_test_stub.cc pkt_filter6_test_stub.h +libdhcptest_la_SOURCES += pkt_captures4.cc pkt_captures6.cc pkt_captures.h + +libdhcptest_la_CXXFLAGS = $(AM_CXXFLAGS) +libdhcptest_la_CPPFLAGS = $(AM_CPPFLAGS) $(GTEST_INCLUDES) + +libdhcptest_la_LIBADD = +libdhcptest_la_LIBADD += $(top_builddir)/src/lib/dhcp/libkea-dhcp++.la + +endif diff --git a/src/lib/dhcp/tests/iface_mgr_test_config.cc b/src/lib/dhcp/testutils/iface_mgr_test_config.cc similarity index 91% rename from src/lib/dhcp/tests/iface_mgr_test_config.cc rename to src/lib/dhcp/testutils/iface_mgr_test_config.cc index a36786734c..5532fec255 100644 --- a/src/lib/dhcp/tests/iface_mgr_test_config.cc +++ b/src/lib/dhcp/testutils/iface_mgr_test_config.cc @@ -9,9 +9,9 @@ #include #include #include -#include -#include -#include +#include +#include +#include #include @@ -67,12 +67,15 @@ IfaceMgrTestConfig::addIface(const IfacePtr& iface) { } void -IfaceMgrTestConfig::addIface(const std::string& name, const unsigned int ifindex) { +IfaceMgrTestConfig::addIface(const std::string& name, + const unsigned int ifindex) { IfaceMgr::instance().addInterface(createIface(name, ifindex)); } IfacePtr -IfaceMgrTestConfig::createIface(const std::string &name, const unsigned int ifindex) { +IfaceMgrTestConfig::createIface(const std::string& name, + const unsigned int ifindex, + const std::string& mac) { IfacePtr iface(new Iface(name, ifindex)); if (name == "lo") { iface->flag_loopback_ = true; @@ -93,8 +96,9 @@ IfaceMgrTestConfig::createIface(const std::string &name, const unsigned int ifin iface->flag_up_ = true; iface->flag_running_ = true; - // Set MAC address to 08:08:08:08:08:08. - std::vector mac_vec(6, 8); + // Set MAC address. + HWAddr hwaddr = HWAddr::fromText(mac); + std::vector mac_vec = hwaddr.hwaddr_; iface->setMac(&mac_vec[0], mac_vec.size()); iface->setHWType(HTYPE_ETHER); @@ -202,6 +206,6 @@ IfaceMgrTestConfig::unicastOpen(const std::string& iface_name) const { return (false); } -} -} -} +} // end of namespace isc::dhcp::test +} // end of namespace isc::dhcp +} // end of namespace isc diff --git a/src/lib/dhcp/tests/iface_mgr_test_config.h b/src/lib/dhcp/testutils/iface_mgr_test_config.h similarity index 96% rename from src/lib/dhcp/tests/iface_mgr_test_config.h rename to src/lib/dhcp/testutils/iface_mgr_test_config.h index 108aca67b8..2839e4e987 100644 --- a/src/lib/dhcp/tests/iface_mgr_test_config.h +++ b/src/lib/dhcp/testutils/iface_mgr_test_config.h @@ -191,14 +191,14 @@ public: /// - multicast always to true /// - broadcast always to false /// - /// If one needs to modify the default flag settings, the setIfaceFlags - /// function should be used. - /// /// @param name A name of the interface to be created. /// @param ifindex An index of the interface to be created. + /// @param mac The mac of the interface. /// /// @return An object representing interface. - static IfacePtr createIface(const std::string& name, const unsigned int ifindex); + static IfacePtr createIface(const std::string& name, + const unsigned int ifindex, + const std::string& mac = "08:08:08:08:08:08"); /// @brief Creates a default (example) set of fake interfaces. void createIfaces(); @@ -258,7 +258,6 @@ public: /// @param iface_name Interface name. bool unicastOpen(const std::string& iface_name) const; - private: /// @brief Currently used packet filter for DHCPv4. PktFilterPtr packet_filter4_; @@ -267,8 +266,8 @@ private: PktFilter6Ptr packet_filter6_; }; -}; -}; -}; +} // end of namespace isc::dhcp::test +} // end of namespace isc::dhcp +} // end of namespace isc #endif // IFACE_MGR_TEST_CONFIG_H diff --git a/src/lib/dhcp/tests/pkt_captures.h b/src/lib/dhcp/testutils/pkt_captures.h similarity index 100% rename from src/lib/dhcp/tests/pkt_captures.h rename to src/lib/dhcp/testutils/pkt_captures.h diff --git a/src/lib/dhcp/tests/pkt_captures4.cc b/src/lib/dhcp/testutils/pkt_captures4.cc similarity index 99% rename from src/lib/dhcp/tests/pkt_captures4.cc rename to src/lib/dhcp/testutils/pkt_captures4.cc index 018506ed43..348a17fa6b 100644 --- a/src/lib/dhcp/tests/pkt_captures4.cc +++ b/src/lib/dhcp/testutils/pkt_captures4.cc @@ -5,12 +5,14 @@ // file, You can obtain one at http://mozilla.org/MPL/2.0/. #include -#include -#include + #include +#include #include -/// @file wireshark.cc +#include + +/// @file pkt_captures.cc /// /// @brief contains packet captures imported from Wireshark /// diff --git a/src/lib/dhcp/tests/pkt_captures6.cc b/src/lib/dhcp/testutils/pkt_captures6.cc similarity index 99% rename from src/lib/dhcp/tests/pkt_captures6.cc rename to src/lib/dhcp/testutils/pkt_captures6.cc index d1fa4165b6..3dee224555 100644 --- a/src/lib/dhcp/tests/pkt_captures6.cc +++ b/src/lib/dhcp/testutils/pkt_captures6.cc @@ -5,9 +5,11 @@ // file, You can obtain one at http://mozilla.org/MPL/2.0/. #include + #include +#include #include -#include + #include /// @file pkt_captures6.cc diff --git a/src/lib/dhcp/tests/pkt_filter6_test_stub.cc b/src/lib/dhcp/testutils/pkt_filter6_test_stub.cc similarity index 95% rename from src/lib/dhcp/tests/pkt_filter6_test_stub.cc rename to src/lib/dhcp/testutils/pkt_filter6_test_stub.cc index b582cc4463..e556fa36cf 100644 --- a/src/lib/dhcp/tests/pkt_filter6_test_stub.cc +++ b/src/lib/dhcp/testutils/pkt_filter6_test_stub.cc @@ -7,7 +7,7 @@ #include #include -#include +#include namespace isc { namespace dhcp { diff --git a/src/lib/dhcp/tests/pkt_filter6_test_stub.h b/src/lib/dhcp/testutils/pkt_filter6_test_stub.h similarity index 100% rename from src/lib/dhcp/tests/pkt_filter6_test_stub.h rename to src/lib/dhcp/testutils/pkt_filter6_test_stub.h diff --git a/src/lib/dhcp/tests/pkt_filter_test_stub.cc b/src/lib/dhcp/testutils/pkt_filter_test_stub.cc similarity index 96% rename from src/lib/dhcp/tests/pkt_filter_test_stub.cc rename to src/lib/dhcp/testutils/pkt_filter_test_stub.cc index 7e87af504e..a7e17b3897 100644 --- a/src/lib/dhcp/tests/pkt_filter_test_stub.cc +++ b/src/lib/dhcp/testutils/pkt_filter_test_stub.cc @@ -9,7 +9,7 @@ #include #include -#include +#include namespace isc { namespace dhcp { diff --git a/src/lib/dhcp/tests/pkt_filter_test_stub.h b/src/lib/dhcp/testutils/pkt_filter_test_stub.h similarity index 100% rename from src/lib/dhcp/tests/pkt_filter_test_stub.h rename to src/lib/dhcp/testutils/pkt_filter_test_stub.h diff --git a/src/lib/dhcpsrv/parsers/ifaces_config_parser.cc b/src/lib/dhcpsrv/parsers/ifaces_config_parser.cc index adc821f2d6..77a23c281f 100644 --- a/src/lib/dhcpsrv/parsers/ifaces_config_parser.cc +++ b/src/lib/dhcpsrv/parsers/ifaces_config_parser.cc @@ -59,7 +59,6 @@ IfacesConfigParser::parse(const CfgIfacePtr& cfg, if (element.first == "interfaces") { parseInterfacesList(cfg, element.second); continue; - } if (element.first == "dhcp-socket-type") { diff --git a/src/lib/dhcpsrv/tests/Makefile.am b/src/lib/dhcpsrv/tests/Makefile.am index a73d0f34a7..c59b10374b 100644 --- a/src/lib/dhcpsrv/tests/Makefile.am +++ b/src/lib/dhcpsrv/tests/Makefile.am @@ -166,7 +166,7 @@ libdhcpsrv_unittests_LDADD += $(top_builddir)/src/lib/dhcp_ddns/libkea-dhcp_ddns libdhcpsrv_unittests_LDADD += $(top_builddir)/src/lib/stats/libkea-stats.la libdhcpsrv_unittests_LDADD += $(top_builddir)/src/lib/config/libkea-cfgclient.la libdhcpsrv_unittests_LDADD += $(top_builddir)/src/lib/http/libkea-http.la -libdhcpsrv_unittests_LDADD += $(top_builddir)/src/lib/dhcp/tests/libdhcptest.la +libdhcpsrv_unittests_LDADD += $(top_builddir)/src/lib/dhcp/testutils/libdhcptest.la libdhcpsrv_unittests_LDADD += $(top_builddir)/src/lib/dhcp/libkea-dhcp++.la libdhcpsrv_unittests_LDADD += $(top_builddir)/src/lib/hooks/libkea-hooks.la diff --git a/src/lib/dhcpsrv/tests/cfg_iface_unittest.cc b/src/lib/dhcpsrv/tests/cfg_iface_unittest.cc index 03591c0104..be0856ee05 100644 --- a/src/lib/dhcpsrv/tests/cfg_iface_unittest.cc +++ b/src/lib/dhcpsrv/tests/cfg_iface_unittest.cc @@ -6,9 +6,9 @@ #include #include -#include -#include -#include +#include +#include +#include #include #include #include diff --git a/src/lib/dhcpsrv/tests/cfg_subnets4_unittest.cc b/src/lib/dhcpsrv/tests/cfg_subnets4_unittest.cc index 73c36a7260..f1472e645c 100644 --- a/src/lib/dhcpsrv/tests/cfg_subnets4_unittest.cc +++ b/src/lib/dhcpsrv/tests/cfg_subnets4_unittest.cc @@ -13,7 +13,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/src/lib/dhcpsrv/tests/cfg_subnets6_unittest.cc b/src/lib/dhcpsrv/tests/cfg_subnets6_unittest.cc index ece583fb07..83fad92123 100644 --- a/src/lib/dhcpsrv/tests/cfg_subnets6_unittest.cc +++ b/src/lib/dhcpsrv/tests/cfg_subnets6_unittest.cc @@ -11,7 +11,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/src/lib/dhcpsrv/tests/cfgmgr_unittest.cc b/src/lib/dhcpsrv/tests/cfgmgr_unittest.cc index 47f9cff104..1be8c68456 100644 --- a/src/lib/dhcpsrv/tests/cfgmgr_unittest.cc +++ b/src/lib/dhcpsrv/tests/cfgmgr_unittest.cc @@ -8,7 +8,7 @@ #include #include -#include +#include #include #include #include diff --git a/src/lib/dhcpsrv/tests/dhcp4o6_ipc_unittest.cc b/src/lib/dhcpsrv/tests/dhcp4o6_ipc_unittest.cc index 192bb63d54..4a327cde13 100644 --- a/src/lib/dhcpsrv/tests/dhcp4o6_ipc_unittest.cc +++ b/src/lib/dhcpsrv/tests/dhcp4o6_ipc_unittest.cc @@ -8,7 +8,7 @@ #include #include -#include +#include #include #include #include diff --git a/src/lib/dhcpsrv/tests/dhcp_parsers_unittest.cc b/src/lib/dhcpsrv/tests/dhcp_parsers_unittest.cc index 3a8bb7ca5d..e893ce7579 100644 --- a/src/lib/dhcpsrv/tests/dhcp_parsers_unittest.cc +++ b/src/lib/dhcpsrv/tests/dhcp_parsers_unittest.cc @@ -14,7 +14,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/src/lib/dhcpsrv/tests/ifaces_config_parser_unittest.cc b/src/lib/dhcpsrv/tests/ifaces_config_parser_unittest.cc index 9e6546246b..aec05d9e90 100644 --- a/src/lib/dhcpsrv/tests/ifaces_config_parser_unittest.cc +++ b/src/lib/dhcpsrv/tests/ifaces_config_parser_unittest.cc @@ -7,7 +7,7 @@ #include #include -#include +#include #include #include #include diff --git a/src/lib/dhcpsrv/tests/shared_network_parser_unittest.cc b/src/lib/dhcpsrv/tests/shared_network_parser_unittest.cc index ea2f3b3af9..3472e0f190 100644 --- a/src/lib/dhcpsrv/tests/shared_network_parser_unittest.cc +++ b/src/lib/dhcpsrv/tests/shared_network_parser_unittest.cc @@ -12,7 +12,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/src/lib/dhcpsrv/tests/shared_networks_list_parser_unittest.cc b/src/lib/dhcpsrv/tests/shared_networks_list_parser_unittest.cc index cf7516f4f1..9aa886dfcc 100644 --- a/src/lib/dhcpsrv/tests/shared_networks_list_parser_unittest.cc +++ b/src/lib/dhcpsrv/tests/shared_networks_list_parser_unittest.cc @@ -6,7 +6,7 @@ #include #include -#include +#include #include #include #include diff --git a/src/lib/dhcpsrv/tests/srv_config_unittest.cc b/src/lib/dhcpsrv/tests/srv_config_unittest.cc index b5545a6264..3cf7032c41 100644 --- a/src/lib/dhcpsrv/tests/srv_config_unittest.cc +++ b/src/lib/dhcpsrv/tests/srv_config_unittest.cc @@ -6,7 +6,7 @@ #include -#include +#include #include #include #include