From: Razvan Becheriu Date: Fri, 6 Sep 2019 15:08:57 +0000 (+0300) Subject: clean up code X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4a8bcccc5ec723bf92461a591a3146f536bfddfd;p=thirdparty%2Fkea.git clean up code --- diff --git a/src/bin/dhcp4/tests/ctrl_dhcp4_srv_unittest.cc b/src/bin/dhcp4/tests/ctrl_dhcp4_srv_unittest.cc index 7b2f7ef385..8223b3c23a 100644 --- a/src/bin/dhcp4/tests/ctrl_dhcp4_srv_unittest.cc +++ b/src/bin/dhcp4/tests/ctrl_dhcp4_srv_unittest.cc @@ -6,12 +6,9 @@ #include -#include -#include #include #include #include -#include #include #include #include @@ -33,15 +30,10 @@ #include #include -#include #include -#include #include #include -#include -#include - using namespace std; using namespace isc; using namespace isc::asiolink; @@ -84,7 +76,7 @@ private: class NakedControlledDhcpv4Srv: public ControlledDhcpv4Srv { // "Naked" DHCPv4 server, exposes internal fields public: - NakedControlledDhcpv4Srv():ControlledDhcpv4Srv(0) { + NakedControlledDhcpv4Srv() : ControlledDhcpv4Srv(DHCP4_SERVER_PORT + 10000) { CfgMgr::instance().setFamily(AF_INET); } @@ -93,8 +85,42 @@ public: using Dhcpv4Srv::network_state_; }; -/// @brief Fixture class intended for testing control channel in the DHCPv4Srv -class CtrlChannelDhcpv4SrvTest : public ::testing::Test { +class CtrlDhcpv4SrvTest : public BaseServerTest { +public: + CtrlDhcpv4SrvTest() + : BaseServerTest() { + reset(); + } + + virtual ~CtrlDhcpv4SrvTest() { + LeaseMgrFactory::destroy(); + StatsMgr::instance().removeAll(); + CommandMgr::instance().closeCommandSocket(); + CommandMgr::instance().deregisterAll(); + CommandMgr::instance().setConnectionTimeout(TIMEOUT_DHCP_SERVER_RECEIVE_COMMAND); + + reset(); + }; + + + /// @brief Reset hooks data + /// + /// Resets the data for the hooks-related portion of the test by ensuring + /// that no libraries are loaded and that any marker files are deleted. + virtual void reset() { + // Unload any previously-loaded libraries. + HooksManager::unloadLibraries(); + + // Get rid of any marker files. + static_cast(remove(LOAD_MARKER_FILE)); + static_cast(remove(UNLOAD_MARKER_FILE)); + IfaceMgr::instance().deleteAllExternalSockets(); + CfgMgr::instance().clear(); + } + +}; + +class CtrlChannelDhcpv4SrvTest : public CtrlDhcpv4SrvTest { public: isc::test::Sandbox sandbox; @@ -112,21 +138,15 @@ public: if (env) { socket_path_ = string(env) + "/kea4.sock"; } else { - socket_path_ = sandbox.join("kea4.sock"); + socket_path_ = sandbox.join("/kea4.sock"); } reset(); } /// @brief Destructor ~CtrlChannelDhcpv4SrvTest() { - LeaseMgrFactory::destroy(); - StatsMgr::instance().removeAll(); - - CommandMgr::instance().closeCommandSocket(); - CommandMgr::instance().deregisterAll(); - CommandMgr::instance().setConnectionTimeout(TIMEOUT_DHCP_SERVER_RECEIVE_COMMAND); - server_.reset(); + reset(); }; /// @brief Returns pointer to the server's IO service. @@ -192,21 +212,9 @@ 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 - /// that no libraries are loaded and that any marker files are deleted. + /// @brief Reset void reset() { - // Unload any previously-loaded libraries. - HooksManager::unloadLibraries(); - - // Get rid of any marker files. - static_cast(remove(LOAD_MARKER_FILE)); - static_cast(remove(UNLOAD_MARKER_FILE)); - - IfaceMgr::instance().deleteAllExternalSockets(); - CfgMgr::instance().clear(); + CtrlDhcpv4SrvTest::reset(); // Remove unix socket file static_cast(::remove(socket_path_.c_str())); @@ -382,10 +390,10 @@ public: } }; -TEST_F(CtrlChannelDhcpv4SrvTest, commands) { - +TEST_F(CtrlDhcpv4SrvTest, commands) { + boost::scoped_ptr srv; ASSERT_NO_THROW( - server_.reset(new NakedControlledDhcpv4Srv()); + srv.reset(new NakedControlledDhcpv4Srv()) ); // Use empty parameters list @@ -454,7 +462,7 @@ TEST_F(CtrlChannelDhcpv4SrvTest, libreload) { } // This test checks which commands are registered by the DHCPv4 server. -TEST_F(CtrlChannelDhcpv4SrvTest, commandsRegistration) { +TEST_F(CtrlDhcpv4SrvTest, commandsRegistration) { ConstElementPtr list_cmds = createCommand("list-commands"); ConstElementPtr answer; @@ -467,8 +475,9 @@ TEST_F(CtrlChannelDhcpv4SrvTest, commandsRegistration) { EXPECT_EQ("[ \"list-commands\" ]", answer->get("arguments")->str()); // Created server should register several additional commands. + boost::scoped_ptr srv; ASSERT_NO_THROW( - server_.reset(new NakedControlledDhcpv4Srv()); + srv.reset(new NakedControlledDhcpv4Srv()); ); EXPECT_NO_THROW(answer = CommandMgr::instance().processCommand(list_cmds)); @@ -500,7 +509,7 @@ TEST_F(CtrlChannelDhcpv4SrvTest, commandsRegistration) { EXPECT_TRUE(command_list.find("\"version-get\"") != string::npos); // Ok, and now delete the server. It should deregister its commands. - server_.reset(); + srv.reset(); // The list should be (almost) empty again. EXPECT_NO_THROW(answer = CommandMgr::instance().processCommand(list_cmds)); diff --git a/src/bin/dhcp6/tests/ctrl_dhcp6_srv_unittest.cc b/src/bin/dhcp6/tests/ctrl_dhcp6_srv_unittest.cc index 01cecaa41c..ea508213d3 100644 --- a/src/bin/dhcp6/tests/ctrl_dhcp6_srv_unittest.cc +++ b/src/bin/dhcp6/tests/ctrl_dhcp6_srv_unittest.cc @@ -6,9 +6,9 @@ #include -#include #include #include +#include #include #include #include @@ -34,10 +34,6 @@ #include #include -#include -#include -#include - using namespace std; using namespace isc; using namespace isc::asiolink; @@ -80,7 +76,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); } @@ -89,9 +85,6 @@ public: using Dhcpv6Srv::network_state_; }; -/// @brief Default control connection timeout. -const size_t DEFAULT_CONNECTION_TIMEOUT = 10000; - class CtrlDhcpv6SrvTest : public BaseServerTest { public: CtrlDhcpv6SrvTest() @@ -102,8 +95,9 @@ public: virtual ~CtrlDhcpv6SrvTest() { LeaseMgrFactory::destroy(); StatsMgr::instance().removeAll(); + CommandMgr::instance().closeCommandSocket(); CommandMgr::instance().deregisterAll(); - CommandMgr::instance().setConnectionTimeout(DEFAULT_CONNECTION_TIMEOUT); + CommandMgr::instance().setConnectionTimeout(TIMEOUT_DHCP_SERVER_RECEIVE_COMMAND); reset(); }; @@ -397,10 +391,9 @@ public: }; TEST_F(CtrlDhcpv6SrvTest, commands) { - boost::scoped_ptr srv; ASSERT_NO_THROW( - srv.reset(new ControlledDhcpv6Srv(DHCP6_SERVER_PORT + 10000)) + srv.reset(new NakedControlledDhcpv6Srv()) ); // Use empty parameters list @@ -468,8 +461,6 @@ TEST_F(CtrlChannelDhcpv6SrvTest, libreload) { EXPECT_TRUE(checkMarkerFile(LOAD_MARKER_FILE, "1212")); } -typedef std::map ElementMap; - // This test checks which commands are registered by the DHCPv6 server. TEST_F(CtrlDhcpv6SrvTest, commandsRegistration) { @@ -486,7 +477,7 @@ TEST_F(CtrlDhcpv6SrvTest, commandsRegistration) { // Created server should register several additional commands. boost::scoped_ptr srv; ASSERT_NO_THROW( - srv.reset(new ControlledDhcpv6Srv(0)); + srv.reset(new NakedControlledDhcpv6Srv()); ); EXPECT_NO_THROW(answer = CommandMgr::instance().processCommand(list_cmds));