From: Tomek Mrugalski Date: Wed, 24 Jun 2015 11:42:47 +0000 (+0200) Subject: [3918] Unix socket tests now use KEA_SOCKET_TEST_DIR env var (if available) X-Git-Tag: trac3922_base~1^2~2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=cce3574ab5252cd6ae61997ebc8d488acfc4094a;p=thirdparty%2Fkea.git [3918] Unix socket tests now use KEA_SOCKET_TEST_DIR env var (if available) --- diff --git a/src/bin/dhcp6/tests/ctrl_dhcp6_srv_unittest.cc b/src/bin/dhcp6/tests/ctrl_dhcp6_srv_unittest.cc index 23bc4ebf7f..eaa937707c 100644 --- a/src/bin/dhcp6/tests/ctrl_dhcp6_srv_unittest.cc +++ b/src/bin/dhcp6/tests/ctrl_dhcp6_srv_unittest.cc @@ -28,6 +28,7 @@ #include #include +#include using namespace std; using namespace isc::config; @@ -231,7 +232,12 @@ public: boost::shared_ptr server_; CtrlChannelDhcpv6SrvTest() { - socket_path_ = string(TEST_DATA_BUILDDIR) + "/kea6.sock"; + const char* env = getenv("KEA_SOCKET_TEST_DIR"); + if (env) { + socket_path_ = string(env) + "/kea6.sock"; + } else { + socket_path_ = string(TEST_DATA_BUILDDIR) + "/kea6.sock"; + } reset(); } diff --git a/src/lib/config/tests/command_socket_factory_unittests.cc b/src/lib/config/tests/command_socket_factory_unittests.cc index a43a0e067a..470aac6047 100644 --- a/src/lib/config/tests/command_socket_factory_unittests.cc +++ b/src/lib/config/tests/command_socket_factory_unittests.cc @@ -19,6 +19,7 @@ #include #include #include +#include using namespace isc::config; using namespace isc::data; @@ -28,7 +29,9 @@ class CommandSocketFactoryTest : public ::testing::Test { public: /// Default constructor - CommandSocketFactoryTest() { + CommandSocketFactoryTest() + :SOCKET_NAME(getSocketPath()) { + // Remove any stale socket files remove(SOCKET_NAME.c_str()); } @@ -40,11 +43,22 @@ public: remove(SOCKET_NAME.c_str()); } - static const std::string SOCKET_NAME; -}; + /// @brief Returns socket path (using either hardcoded path or env variable) + /// @return path to the unix socket + std::string getSocketPath() { + + std::string socket_path; + const char* env = getenv("KEA_SOCKET_TEST_DIR"); + if (env) { + socket_path = std::string(env) + "/test-socket"; + } else { + socket_path = std::string(TEST_DATA_BUILDDIR) + "/test-socket"; + } + return (socket_path); + } -const std::string CommandSocketFactoryTest::SOCKET_NAME = - std::string(TEST_DATA_BUILDDIR) + "/test-socket"; + std::string SOCKET_NAME; +}; TEST_F(CommandSocketFactoryTest, unixCreate) { // Null pointer is obviously a bad idea. @@ -74,4 +88,3 @@ TEST_F(CommandSocketFactoryTest, unixCreate) { // It should be possible to close the socket. EXPECT_NO_THROW(sock->close()); } - diff --git a/src/lib/util/tests/socketsession_unittest.cc b/src/lib/util/tests/socketsession_unittest.cc index 1f9c077607..d1bf685bdb 100644 --- a/src/lib/util/tests/socketsession_unittest.cc +++ b/src/lib/util/tests/socketsession_unittest.cc @@ -25,6 +25,7 @@ #include #include +#include #include #include @@ -51,7 +52,6 @@ using namespace isc::util::io::internal; namespace { -const char* const TEST_UNIX_FILE = TEST_DATA_TOPBUILDDIR "/test.unix"; const char* const TEST_PORT = "53535"; const char* const TEST_PORT2 = "53536"; // use this in case we need 2 ports const char TEST_DATA[] = "Kea test"; @@ -155,13 +155,30 @@ private: class ForwardTest : public ::testing::Test { protected: - ForwardTest() : listen_fd_(-1), forwarder_(TEST_UNIX_FILE), + + /// @brief Returns socket path (using either hardcoded path or env variable) + /// @return path to the unix socket + std::string getSocketPath() { + + std::string socket_path; + const char* env = getenv("KEA_SOCKET_TEST_DIR"); + if (env) { + socket_path = string(env) + "/test.unix"; + } else { + socket_path = string(TEST_DATA_BUILDDIR) + "/test.unix"; + } + return (socket_path); + } + + ForwardTest() : listen_fd_(-1), forwarder_(getSocketPath()), large_text_(65535, 'a'), - test_un_len_(2 + strlen(TEST_UNIX_FILE)) + test_un_len_(2 + strlen(getSocketPath().c_str())) { - unlink(TEST_UNIX_FILE); + std::string unix_file = getSocketPath(); + + unlink(unix_file.c_str()); test_un_.sun_family = AF_UNIX; - strncpy(test_un_.sun_path, TEST_UNIX_FILE, sizeof(test_un_.sun_path)); + strncpy(test_un_.sun_path, unix_file.c_str(), sizeof(test_un_.sun_path)); #ifdef HAVE_SA_LEN test_un_.sun_len = test_un_len_; #endif @@ -171,7 +188,7 @@ protected: if (listen_fd_ != -1) { close(listen_fd_); } - unlink(TEST_UNIX_FILE); + unlink(getSocketPath().c_str()); } // Start an internal "socket session server". @@ -238,7 +255,7 @@ protected: // and it's a TCP socket, it will also start listening to new connection // requests. int createSocket(int family, int type, int protocol, - const SockAddrInfo& sainfo, bool do_listen) + const SockAddrInfo& sainfo, bool do_listen) { int s = socket(family, type, protocol); if (s < 0) { @@ -378,7 +395,7 @@ TEST_F(ForwardTest, connect) { EXPECT_THROW(forwarder.close(), BadValue); // Set up the receiver and connect. It should succeed. - SocketSessionForwarder forwarder2(TEST_UNIX_FILE); + SocketSessionForwarder forwarder2(getSocketPath().c_str()); startListen(); forwarder2.connectToReceiver(); // And it can be closed successfully. @@ -396,14 +413,14 @@ TEST_F(ForwardTest, connect) { // Connect then destroy. Should be internally closed, but unfortunately // it's not easy to test it directly. We only check no disruption happens. SocketSessionForwarder* forwarderp = - new SocketSessionForwarder(TEST_UNIX_FILE); + new SocketSessionForwarder(getSocketPath().c_str()); forwarderp->connectToReceiver(); delete forwarderp; } TEST_F(ForwardTest, close) { // can't close before connect - EXPECT_THROW(SocketSessionForwarder(TEST_UNIX_FILE).close(), BadValue); + EXPECT_THROW(SocketSessionForwarder(getSocketPath().c_str()).close(), BadValue); } void @@ -674,7 +691,7 @@ TEST_F(ForwardTest, badPush) { } // A subroutine for pushTooFast, continuously pushing socket sessions -// with full-size DNS messages (65535 bytes) without receiving them. +// with full-size DNS messages (65535 bytes) without receiving them. // the push attempts will eventually fill the socket send buffer and trigger // an exception. Unfortunately exactly how many we can forward depends on // the internal system implementation; it should be close to 3, because