]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
clean up code
authorRazvan Becheriu <razvan@isc.org>
Fri, 6 Sep 2019 15:08:57 +0000 (18:08 +0300)
committerRazvan Becheriu <razvan@isc.org>
Wed, 6 Nov 2019 17:30:43 +0000 (19:30 +0200)
src/bin/dhcp4/tests/ctrl_dhcp4_srv_unittest.cc
src/bin/dhcp6/tests/ctrl_dhcp6_srv_unittest.cc

index 7b2f7ef3852d48bc78cfc9039b115d47f7fb0eb8..8223b3c23ab652bdf5a387473fbb0d72a426bc41 100644 (file)
@@ -6,12 +6,9 @@
 
 #include <config.h>
 
-#include <asiolink/interval_timer.h>
-#include <asiolink/io_service.h>
 #include <cc/command_interpreter.h>
 #include <config/command_mgr.h>
 #include <config/timeouts.h>
-#include <dhcp/dhcp4.h>
 #include <dhcp/libdhcp++.h>
 #include <dhcpsrv/cfgmgr.h>
 #include <dhcpsrv/lease.h>
 #include <gtest/gtest.h>
 
 #include <cstdlib>
-#include <fstream>
 #include <iomanip>
-#include <iostream>
 #include <sstream>
 #include <thread>
 
-#include <arpa/inet.h>
-#include <unistd.h>
-
 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<void>(remove(LOAD_MARKER_FILE));
+        static_cast<void>(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<void>(remove(LOAD_MARKER_FILE));
-        static_cast<void>(remove(UNLOAD_MARKER_FILE));
-
-        IfaceMgr::instance().deleteAllExternalSockets();
-        CfgMgr::instance().clear();
+        CtrlDhcpv4SrvTest::reset();
 
         // Remove unix socket file
         static_cast<void>(::remove(socket_path_.c_str()));
@@ -382,10 +390,10 @@ public:
     }
 };
 
-TEST_F(CtrlChannelDhcpv4SrvTest, commands) {
-
+TEST_F(CtrlDhcpv4SrvTest, commands) {
+    boost::scoped_ptr<ControlledDhcpv4Srv> 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<ControlledDhcpv4Srv> 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));
index 01cecaa41c46d60569517ed4ed2cfd8075b3d52f..ea508213d36e0b250b4ae3af512450ed6278ad27 100644 (file)
@@ -6,9 +6,9 @@
 
 #include <config.h>
 
-#include <asiolink/io_address.h>
 #include <cc/command_interpreter.h>
 #include <config/command_mgr.h>
+#include <config/timeouts.h>
 #include <dhcp/libdhcp++.h>
 #include <dhcpsrv/cfgmgr.h>
 #include <dhcpsrv/lease.h>
 #include <sstream>
 #include <thread>
 
-#include <sys/select.h>
-#include <sys/stat.h>
-#include <sys/ioctl.h>
-
 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<ControlledDhcpv6Srv> 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<std::string, isc::data::ConstElementPtr> 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<ControlledDhcpv6Srv> srv;
     ASSERT_NO_THROW(
-        srv.reset(new ControlledDhcpv6Srv(0));
+        srv.reset(new NakedControlledDhcpv6Srv());
     );
 
     EXPECT_NO_THROW(answer = CommandMgr::instance().processCommand(list_cmds));