Browse Source

[4106] Test IPC class moved to a common library.

Marcin Siodelski 9 years ago
parent
commit
e1441462a9

+ 2 - 1
src/bin/dhcp4/dhcp4_dhcp4o6_ipc.cc

@@ -24,7 +24,8 @@ using namespace std;
 namespace isc {
 namespace dhcp {
 
-Dhcp4o6Ipc::Dhcp4o6Ipc() : Dhcp4o6IpcBase() {}
+Dhcp4o6Ipc::Dhcp4o6Ipc() : Dhcp4o6IpcBase() {
+}
 
 Dhcp4o6Ipc& Dhcp4o6Ipc::instance() {
     static Dhcp4o6Ipc dhcp4o6_ipc;

+ 3 - 86
src/lib/dhcpsrv/tests/dhcp4o6_ipc_unittest.cc

@@ -21,6 +21,7 @@
 #include <dhcp/option_string.h>
 #include <dhcp/option_vendor.h>
 #include <dhcpsrv/dhcp4o6_ipc.h>
+#include <dhcpsrv/testutils/dhcp4o6_test_ipc.h>
 #include <boost/bind.hpp>
 #include <boost/function.hpp>
 #include <gtest/gtest.h>
@@ -43,92 +44,8 @@ const uint16_t TEST_ITERATIONS = 10;
 /// @brief Type definition for the function creating DHCP message.
 typedef boost::function<Pkt6Ptr(const uint16_t, const uint16_t)> CreateMsgFun;
 
-/// @brief Implements a simple IPC for the test.
-class TestIpc : public  Dhcp4o6IpcBase {
-public:
-
-    /// @brief Constructor.
-    ///
-    /// @param port Desired port.
-    /// @param endpoint_type Type of the IPC endpoint. It should be 4 or 6.
-    TestIpc(const uint16_t port, const EndpointType& endpoint_type);
-
-    /// @brief Sets new port to be used with @c open.
-    ///
-    /// @param desired_port New desired port.
-    void setDesiredPort(const uint16_t desired_port) {
-        desired_port_ = desired_port;
-    }
-
-    /// @brief Opens the IPC socket and registers it in @c IfaceMgr.
-    ///
-    /// This method opens the IPC socket and registers it as external
-    /// socket in the IfaceMgr. The @c TestIpc::receiveHandler is used as a
-    /// callback to be called by the @c IfaceMgr when the data is received
-    /// over the socket.
-    virtual void open();
-
-    /// @brief Retrieve port which socket is bound to.
-    uint16_t getPort() const {
-        return (port_);
-    }
-
-    /// @brief Retrieve socket descriptor.
-    int getSocketFd() const {
-        return (socket_fd_);
-    }
-
-    /// @brief Pops and returns a received message.
-    ///
-    /// @return Pointer to the received message over the IPC.
-    Pkt6Ptr popPktReceived() {
-        // Copy the received message.
-        Pkt6Ptr pkt_copy(pkt_received_);
-        // Set the received message to NULL (pop).
-        pkt_received_.reset();
-        // Return the copy.
-        return (pkt_copy);
-    }
-
-private:
-
-    /// @brief Callback for the IPC socket.
-    ///
-    /// This callback is called by the @c IfaceMgr when the data is received
-    /// over the IPC socket.
-    void receiveHandler();
-
-    /// @brief Port number.
-    uint16_t desired_port_;
-
-    /// @brief Endpoint type, i.e. 4 or 6.
-    EndpointType endpoint_type_;
-
-    /// @brief Pointer to the last received message.
-    Pkt6Ptr pkt_received_;
-};
-
-TestIpc::TestIpc(const uint16_t port, const EndpointType& endpoint_type)
-    : desired_port_(port), endpoint_type_(endpoint_type), pkt_received_() {
-}
-
-void
-TestIpc::open() {
-    // Use the base IPC to open the socket.
-    socket_fd_ = Dhcp4o6IpcBase::open(desired_port_, endpoint_type_);
-    // If the socket has been opened correctly, register it in the @c IfaceMgr.
-    if (socket_fd_ != -1) {
-        IfaceMgr& iface_mgr = IfaceMgr::instance();
-        iface_mgr.addExternalSocket(socket_fd_,
-                                    boost::bind(&TestIpc::receiveHandler,
-                                                this));
-    }
-}
-
-void
-TestIpc::receiveHandler() {
-    pkt_received_ = receive();
-}
+/// @brief Define short name for test IPC class.
+typedef Dhcp4o6TestIpc TestIpc;
 
 /// @brief Test fixture class for @c Dhcp4o6IpcBase.
 class Dhcp4o6IpcBaseTest : public ::testing::Test {

+ 2 - 0
src/lib/dhcpsrv/testutils/Makefile.am

@@ -16,6 +16,8 @@ if HAVE_GTEST
 noinst_LTLIBRARIES = libdhcpsrvtest.la
 
 libdhcpsrvtest_la_SOURCES  = config_result_check.cc config_result_check.h
+libdhcpsrvtest_la_SOURCES += dhcp4o6_test_ipc.cc dhcp4o6_test_ipc.h
+
 libdhcpsrvtest_la_CXXFLAGS = $(AM_CXXFLAGS)
 libdhcpsrvtest_la_CPPFLAGS = $(AM_CPPFLAGS)
 libdhcpsrvtest_la_LDFLAGS  = $(AM_LDFLAGS)

+ 50 - 0
src/lib/dhcpsrv/testutils/dhcp4o6_test_ipc.cc

@@ -0,0 +1,50 @@
+// 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 <config.h>
+#include <dhcp/iface_mgr.h>
+#include <dhcpsrv/testutils/dhcp4o6_test_ipc.h>
+#include <boost/bind.hpp>
+
+namespace isc {
+namespace dhcp {
+namespace test {
+
+Dhcp4o6TestIpc::Dhcp4o6TestIpc(const uint16_t port,
+                               const EndpointType& endpoint_type)
+    : desired_port_(port), endpoint_type_(endpoint_type), pkt_received_() {
+}
+
+void
+Dhcp4o6TestIpc::open() {
+    // Use the base IPC to open the socket.
+    socket_fd_ = Dhcp4o6IpcBase::open(desired_port_, endpoint_type_);
+    // If the socket has been opened correctly, register it in the @c IfaceMgr.
+    if (socket_fd_ != -1) {
+        IfaceMgr& iface_mgr = IfaceMgr::instance();
+        iface_mgr.addExternalSocket(socket_fd_,
+                                    boost::bind(&Dhcp4o6TestIpc::receiveHandler,
+                                                this));
+    }
+}
+
+void
+Dhcp4o6TestIpc::receiveHandler() {
+    pkt_received_ = receive();
+}
+
+
+} // end of isc::dhcp::test namespace
+} // end of isc::dhcp namespace
+} // end of isc namespace

+ 100 - 0
src/lib/dhcpsrv/testutils/dhcp4o6_test_ipc.h

@@ -0,0 +1,100 @@
+// 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.
+
+#ifndef DHCP4O6_TEST_IPC_H
+#define DHCP4O6_TEST_IPC_H
+
+#include <asiolink/io_address.h>
+#include <dhcp/iface_mgr.h>
+#include <dhcp/pkt6.h>
+#include <dhcpsrv/dhcp4o6_ipc.h>
+#include <boost/noncopyable.hpp>
+#include <stdint.h>
+
+namespace isc {
+namespace dhcp {
+namespace test {
+
+/// @brief Implements a simple IPC for the test.
+class Dhcp4o6TestIpc : public  Dhcp4o6IpcBase {
+public:
+
+    /// @brief Constructor.
+    ///
+    /// @param port Desired port.
+    /// @param endpoint_type Type of the IPC endpoint. It should be 4 or 6.
+    Dhcp4o6TestIpc(const uint16_t port, const EndpointType& endpoint_type);
+
+    /// @brief Sets new port to be used with @c open.
+    ///
+    /// @param desired_port New desired port.
+    void setDesiredPort(const uint16_t desired_port) {
+        desired_port_ = desired_port;
+    }
+
+    /// @brief Opens the IPC socket and registers it in @c IfaceMgr.
+    ///
+    /// This method opens the IPC socket and registers it as external
+    /// socket in the IfaceMgr. The @c TestIpc::receiveHandler is used as a
+    /// callback to be called by the @c IfaceMgr when the data is received
+    /// over the socket.
+    virtual void open();
+
+    /// @brief Retrieve port which socket is bound to.
+    uint16_t getPort() const {
+        return (port_);
+    }
+
+    /// @brief Retrieve socket descriptor.
+    int getSocketFd() const {
+        return (socket_fd_);
+    }
+
+    /// @brief Pops and returns a received message.
+    ///
+    /// @return Pointer to the received message over the IPC.
+    Pkt6Ptr popPktReceived() {
+        // Copy the received message.
+        Pkt6Ptr pkt_copy(pkt_received_);
+        // Set the received message to NULL (pop).
+        pkt_received_.reset();
+        // Return the copy.
+        return (pkt_copy);
+    }
+
+private:
+
+    /// @brief Callback for the IPC socket.
+    ///
+    /// This callback is called by the @c IfaceMgr when the data is received
+    /// over the IPC socket.
+    void receiveHandler();
+
+    /// @brief Port number.
+    uint16_t desired_port_;
+
+    /// @brief Endpoint type, i.e. 4 or 6.
+    EndpointType endpoint_type_;
+
+    /// @brief Pointer to the last received message.
+    Pkt6Ptr pkt_received_;
+};
+
+
+
+}; // end of isc::dhcp::test namespace
+}; // end of isc::dhcp namespace
+}; // end of isc namespace
+
+#endif // DHCP4O6_TEST_IPC_H