Fix network port collisions on concurrent unit-tests
g0dil [Mon, 7 Sep 2009 15:14:04 +0000 (15:14 +0000)]
git-svn-id: https://svn.berlios.de/svnroot/repos/senf/trunk@1392 270642c3-0616-0410-b53a-bc976706d245

senf/PPI/SocketSink.test.cc
senf/PPI/SocketSource.test.cc
senf/Socket/Protocols/INet/TCPSocketHandle.test.cc
senf/Socket/Protocols/INet/UDPSocketHandle.test.cc
senf/Socket/Protocols/INet/net.test.hh [new file with mode: 0644]
senf/Utils/Console/UDPServer.test.cc
senf/Utils/Logger/SyslogUDPTarget.test.cc

index 9fc999a..3f424fa 100644 (file)
@@ -47,12 +47,31 @@ namespace {
     void timeout() {
         senf::scheduler::terminate();
     }
+
+    int base_pid = 0;
+
+    unsigned port(unsigned i)
+    {
+        if (! base_pid)
+            base_pid = ::getpid();
+        return 23456u + (((base_pid^(base_pid>>8)^(base_pid>>16)^(base_pid>>24))&0xff)<<2) + i;
+    }
+
+    std::string localhost4str(unsigned i)
+    {
+        return (boost::format("localhost:%d") % port(i)).str();
+    }
+
+    std::string localhost6str(unsigned i)
+    {
+        return (boost::format("[::1]:%d") % port(i)).str();
+    }
 }
 
 BOOST_AUTO_UNIT_TEST(passiveSocketSink)
 {
     senf::ConnectedUDPv4ClientSocketHandle outputSocket (
-        senf::INet4SocketAddress("localhost:44344"));
+        senf::INet4SocketAddress(localhost4str(0)));
     module::PassiveSocketSink<> udpSink(outputSocket);
     debug::ActiveSource source;
     ppi::connect(source, udpSink);
@@ -61,7 +80,7 @@ BOOST_AUTO_UNIT_TEST(passiveSocketSink)
     senf::Packet p (senf::DataPacket::create(data));
 
     senf::UDPv4ClientSocketHandle inputSocket;
-    inputSocket.bind(senf::INet4SocketAddress("localhost:44344"));
+    inputSocket.bind(senf::INet4SocketAddress(localhost4str(0)));
     senf::ppi::init();
     source.submit(p);
 
@@ -72,7 +91,7 @@ BOOST_AUTO_UNIT_TEST(passiveSocketSink)
 BOOST_AUTO_UNIT_TEST(activeSocketSink)
 {
     senf::ConnectedUDPv4ClientSocketHandle outputSocket (
-        senf::INet4SocketAddress("localhost:44344"));
+        senf::INet4SocketAddress(localhost4str(0)));
     module::ActiveSocketSink<> udpSink(outputSocket);
     debug::PassiveSource source;
     ppi::connect(source, udpSink);
@@ -81,7 +100,7 @@ BOOST_AUTO_UNIT_TEST(activeSocketSink)
     senf::Packet p (senf::DataPacket::create(data));
 
     senf::UDPv4ClientSocketHandle inputSocket;
-    inputSocket.bind(senf::INet4SocketAddress("localhost:44344"));
+    inputSocket.bind(senf::INet4SocketAddress(localhost4str(0)));
     senf::scheduler::TimerEvent timer (
         "activeSocketSink test timer", &timeout,
         senf::ClockService::now() + senf::ClockService::milliseconds(100));
index 4bf23ef..7daad72 100644 (file)
@@ -47,12 +47,31 @@ namespace {
     void timeout() {
         senf::scheduler::terminate();
     }
+
+    int base_pid = 0;
+
+    unsigned port(unsigned i)
+    {
+        if (! base_pid)
+            base_pid = ::getpid();
+        return 23456u + (((base_pid^(base_pid>>8)^(base_pid>>16)^(base_pid>>24))&0xff)<<2) + i;
+    }
+
+    std::string localhost4str(unsigned i)
+    {
+        return (boost::format("localhost:%d") % port(i)).str();
+    }
+
+    std::string localhost6str(unsigned i)
+    {
+        return (boost::format("[::1]:%d") % port(i)).str();
+    }
 }
 
 BOOST_AUTO_UNIT_TEST(socketSource)
 {
     senf::UDPv4ClientSocketHandle inputSocket;
-    inputSocket.bind(senf::INet4SocketAddress("localhost:44344"));
+    inputSocket.bind(senf::INet4SocketAddress(localhost4str(0)));
     inputSocket.blocking(false);
     module::ActiveSocketSource<> udpSource(inputSocket);
     debug::PassiveSink sink;
@@ -61,7 +80,7 @@ BOOST_AUTO_UNIT_TEST(socketSource)
     std::string data ("TEST");
 
     senf::UDPv4ClientSocketHandle outputSocket;
-    outputSocket.writeto(senf::INet4SocketAddress("localhost:44344"),data);
+    outputSocket.writeto(senf::INet4SocketAddress(localhost4str(0)),data);
     senf::scheduler::TimerEvent timer (
         "socketSource test timer", &timeout,
         senf::ClockService::now() + senf::ClockService::milliseconds(100));
index 8a00385..f323450 100644 (file)
@@ -35,6 +35,7 @@
 #include <netinet/in.h>
 #include <iostream>
 #include "TCPSocketHandle.hh"
+#include "net.test.hh"
 
 #include <senf/Utils/auto_unit_test.hh>
 #include <boost/test/test_tools.hpp>
 #define prefix_
 ///////////////////////////////cc.p////////////////////////////////////////
 
-namespace {
-
-    void error(char const * fn, char const * proc="")
-    {
-        std::cerr << "\n" << proc << ((*proc)?": ":"") << fn << ": " << strerror(errno) << std::endl;
-    }
-
-    void fail(char const * proc, char const * fn)
-    {
-        error(fn,proc);
-        _exit(1);
-    }
-
-    int server_pid = 0;
-
-    void start(void (*fn)())
-    {
-        server_pid = ::fork();
-        if (server_pid < 0) BOOST_FAIL("fork()");
-        if (server_pid == 0) {
-            signal(SIGCHLD, SIG_IGN);
-            (*fn)();
-            _exit(0);
-        }
-        signal(SIGCHLD, SIG_DFL);
-        ::sleep(1);
-    }
-
-    void wait()
-    {
-        int status;
-        if (waitpid(server_pid,&status,0)<0)
-            BOOST_FAIL("waitpid()");
-        BOOST_CHECK_EQUAL( status , 0 );
-    }
-
-    void stop()
-    {
-        if (server_pid) {
-            kill(server_pid,9);
-            wait();
-            server_pid = 0;
-        }
-    }
-
-}
-
 ///////////////////////////////////////////////////////////////////////////
 
 namespace {
@@ -103,7 +57,7 @@ namespace {
         struct sockaddr_in sin;
         ::memset(&sin,0,sizeof(sin));
         sin.sin_family = AF_INET;
-        sin.sin_port = htons(12345);
+        sin.sin_port = htons(port(0));
         sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
         if (bind(serv,(struct sockaddr *)&sin,sizeof(sin))<0) fail("server_v4","bind()");
         if (listen(serv,1)<0) fail("server_v4","listen()");
@@ -133,7 +87,7 @@ namespace {
         struct sockaddr_in6 sin;
         ::memset(&sin,0,sizeof(sin));
         sin.sin6_family = AF_INET6;
-        sin.sin6_port = htons(12345);
+        sin.sin6_port = htons(port(0));
         sin.sin6_addr = in6addr_loopback;
         if (bind(serv,(struct sockaddr *)&sin,sizeof(sin))<0) fail("server_v6","bind()");
         if (listen(serv,1)<0) fail("server_v6","listen()");
@@ -160,7 +114,7 @@ BOOST_AUTO_UNIT_TEST(tcpv4ClientSocketHandle)
     {
         senf::TCPv4ClientSocketHandle sock;
 
-        BOOST_CHECK_THROW( sock.connect(senf::INet4SocketAddress("127.0.0.1:12345")), 
+        BOOST_CHECK_THROW( sock.connect(senf::INet4SocketAddress(localhost4str(0))), 
                            senf::SystemException );
     }
 
@@ -168,10 +122,10 @@ BOOST_AUTO_UNIT_TEST(tcpv4ClientSocketHandle)
         alarm(10);
         start(server_v4);
         senf::TCPv4ClientSocketHandle sock;
-        SENF_CHECK_NO_THROW( sock.bind(senf::INet4SocketAddress("127.0.0.1:23456")) );
-        SENF_CHECK_NO_THROW( sock.connect(senf::INet4SocketAddress("127.0.0.1:12345")) );
-        BOOST_CHECK( sock.peer() == senf::INet4SocketAddress("127.0.0.1:12345") );
-        BOOST_CHECK( sock.local() == senf::INet4SocketAddress("127.0.0.1:23456") );
+        SENF_CHECK_NO_THROW( sock.bind(senf::INet4SocketAddress(localhost4str(1)) );
+                             SENF_CHECK_NO_THROW( sock.connect(senf::INet4SocketAddress(localhost4str(0)))) );
+        BOOST_CHECK( sock.peer() == senf::INet4SocketAddress(localhost4str(0)) );
+        BOOST_CHECK( sock.local() == senf::INet4SocketAddress(localhost4str(1)) );
         BOOST_CHECK( sock.blocking() );
         SENF_CHECK_NO_THROW( sock.protocol().rcvbuf(2048) );
         BOOST_CHECK_EQUAL( sock.protocol().rcvbuf(), 2048u );
@@ -231,7 +185,7 @@ BOOST_AUTO_UNIT_TEST(tcpv6ClientSocketHandle)
     {
         senf::TCPv6ClientSocketHandle sock;
 
-        BOOST_CHECK_THROW( sock.connect(senf::INet6SocketAddress("[::1]:12345")), 
+        BOOST_CHECK_THROW( sock.connect(senf::INet6SocketAddress(localhost6str(0))), 
                            senf::SystemException );
     }
 
@@ -239,10 +193,10 @@ BOOST_AUTO_UNIT_TEST(tcpv6ClientSocketHandle)
         alarm(10);
         start(server_v6);
         senf::TCPv6ClientSocketHandle sock;
-        SENF_CHECK_NO_THROW( sock.bind(senf::INet6SocketAddress("[::1]:23456")) );
-        SENF_CHECK_NO_THROW( sock.connect(senf::INet6SocketAddress("[::1]:12345")) );
-        BOOST_CHECK( sock.peer() == senf::INet6SocketAddress("[::1]:12345") );
-        BOOST_CHECK( sock.local() == senf::INet6SocketAddress("[::1]:23456") );
+        SENF_CHECK_NO_THROW( sock.bind(senf::INet6SocketAddress(localhost6str(1))) );
+        SENF_CHECK_NO_THROW( sock.connect(senf::INet6SocketAddress(localhost6str(0))) );
+        BOOST_CHECK( sock.peer() == senf::INet6SocketAddress(localhost6str(0)) );
+        BOOST_CHECK( sock.local() == senf::INet6SocketAddress(localhost6str(1)) );
         BOOST_CHECK( sock.blocking() );
         SENF_CHECK_NO_THROW( sock.protocol().rcvbuf(2048) );
         BOOST_CHECK_EQUAL( sock.protocol().rcvbuf(), 2048u );
@@ -296,7 +250,7 @@ namespace {
         struct sockaddr_in sin;
         ::memset(&sin,0,sizeof(sin));
         sin.sin_family = AF_INET;
-        sin.sin_port = htons(12346);
+        sin.sin_port = htons(port(2));
         sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
         if (connect(sock,(struct sockaddr *)&sin,sizeof(sin)) < 0)
             fail("client_v4","connect()");
@@ -320,7 +274,7 @@ namespace {
         struct sockaddr_in6 sin;
         ::memset(&sin,0,sizeof(sin));
         sin.sin6_family = AF_INET6;
-        sin.sin6_port = htons(12347);
+        sin.sin6_port = htons(port(3));
         sin.sin6_addr = in6addr_loopback;
         if (connect(sock,(struct sockaddr *)&sin,sizeof(sin)) < 0)
             fail("client_v6","connect()");
@@ -344,7 +298,7 @@ BOOST_AUTO_UNIT_TEST(tcpv4ServerSocketHandle)
     try {
         alarm(10);
         BOOST_CHECKPOINT("Opening server socket");
-        senf::TCPv4ServerSocketHandle server (senf::INet4SocketAddress("127.0.0.1:12346"));
+        senf::TCPv4ServerSocketHandle server (senf::INet4SocketAddress(localhost4str(2)));
         BOOST_CHECKPOINT("Starting client");
         start(client_v4);
 
@@ -370,7 +324,7 @@ BOOST_AUTO_UNIT_TEST(tcpv6ServerSocketHandle)
     try {
         alarm(10);
         BOOST_CHECKPOINT("Opening server socket");
-        senf::TCPv6ServerSocketHandle server (senf::INet6SocketAddress("[::1]:12347"));
+        senf::TCPv6ServerSocketHandle server (senf::INet6SocketAddress(localhost6str(3)));
         BOOST_CHECKPOINT("Starting client");
         start(client_v6);
 
index 728faf1..9bbd272 100644 (file)
@@ -33,8 +33,9 @@
 #include <unistd.h>
 #include <sys/socket.h>
 #include <netinet/in.h>
-#include "UDPSocketHandle.hh"
 #include <iostream>
+#include "UDPSocketHandle.hh"
+#include "net.test.hh"
 
 #include <senf/Utils/auto_unit_test.hh>
 #include <boost/test/test_tools.hpp>
 #define prefix_
 ///////////////////////////////cc.p////////////////////////////////////////
 
-namespace {
-
-    void error(char const * fn, char const * proc="")
-    {
-        std::cerr << "\n" << proc << ((*proc)?": ":"") << fn << ": " << strerror(errno) << std::endl;
-    }
-
-    void fail(char const * proc, char const * fn)
-    {
-        error(fn,proc);
-        _exit(1);
-    }
-
-    int server_pid = 0;
-
-    void start(void (*fn)())
-    {
-        server_pid = ::fork();
-        if (server_pid < 0) BOOST_FAIL("fork()");
-        if (server_pid == 0) {
-            signal(SIGCHLD, SIG_IGN);
-            (*fn)();
-            _exit(0);
-        }
-        signal(SIGCHLD, SIG_DFL);
-    }
-
-    void wait()
-    {
-        int status;
-        if (waitpid(server_pid,&status,0)<0)
-            BOOST_FAIL("waitpid()");
-        BOOST_CHECK_EQUAL( status , 0 );
-    }
-
-    void stop()
-    {
-        if (server_pid) {
-            kill(server_pid,9);
-            wait();
-            server_pid = 0;
-        }
-    }
-
-}
-
 ///////////////////////////////////////////////////////////////////////////
 
 namespace {
@@ -99,11 +54,11 @@ namespace {
         struct sockaddr_in sin;
         ::memset(&sin,0,sizeof(sin));
         sin.sin_family = AF_INET;
-        sin.sin_port = htons(12345);
+        sin.sin_port = htons(port(0));
         sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
         if (bind(sock,(struct sockaddr *)&sin,sizeof(sin))<0) fail("server_v4","bind()");
 
-        sin.sin_port = htons(23456);
+        sin.sin_port = htons(port(1));
         char buffer[1024];
         while (1) {
             int n = read(sock,buffer,1024);
@@ -122,11 +77,11 @@ namespace {
         struct sockaddr_in6 sin;
         ::memset(&sin,0,sizeof(sin));
         sin.sin6_family = AF_INET6;
-        sin.sin6_port = htons(12345);
+        sin.sin6_port = htons(port(0));
         sin.sin6_addr = in6addr_loopback;
         if (bind(sock,(struct sockaddr *)&sin,sizeof(sin))<0) fail("server_v6","bind()");
 
-        sin.sin6_port = htons(23456);
+        sin.sin6_port = htons(port(1));
         char buffer[1024];
         while (1) {
             int n = read(sock,buffer,1024);
@@ -146,17 +101,17 @@ BOOST_AUTO_UNIT_TEST(udpv4ClientSocketHandle)
         alarm(10);
         start(server_v4);
         senf::UDPv4ClientSocketHandle sock;
-        SENF_CHECK_NO_THROW( sock.bind(senf::INet4SocketAddress("127.0.0.1:23456")) );
-        BOOST_CHECK( sock.local() == senf::INet4SocketAddress("127.0.0.1:23456") );
+        SENF_CHECK_NO_THROW( sock.bind(senf::INet4SocketAddress(localhost4str(1))) );
+        BOOST_CHECK( sock.local() == senf::INet4SocketAddress(localhost4str(1)) );
         SENF_CHECK_NO_THROW( sock.protocol().rcvbuf(2048) );
         BOOST_CHECK_EQUAL( sock.protocol().rcvbuf(), 2048u );
         SENF_CHECK_NO_THROW( sock.protocol().sndbuf(2048) );
         BOOST_CHECK_EQUAL( sock.protocol().sndbuf(), 2048u );
-        SENF_CHECK_NO_THROW( sock.writeto(senf::INet4SocketAddress("127.0.0.1:12345"),
+        SENF_CHECK_NO_THROW( sock.writeto(senf::INet4SocketAddress(localhost4str(0)),
                                            std::string("TEST-WRITE")) );
         BOOST_CHECK_EQUAL( sock.read(), "TEST-WRITE" );
         SENF_CHECK_NO_THROW( sock.protocol().timestamp() );
-        sock.writeto(senf::INet4SocketAddress("127.0.0.1:12345"), std::string("QUIT"));
+        sock.writeto(senf::INet4SocketAddress(localhost4str(0)), std::string("QUIT"));
         sleep(1);
         stop();
         sleep(1);
diff --git a/senf/Socket/Protocols/INet/net.test.hh b/senf/Socket/Protocols/INet/net.test.hh
new file mode 100644 (file)
index 0000000..bf398da
--- /dev/null
@@ -0,0 +1,117 @@
+// $Id$
+//
+// Copyright (C) 2009 
+// Fraunhofer Institute for Open Communication Systems (FOKUS)
+// Competence Center NETwork research (NET), St. Augustin, GERMANY
+//     Stefan Bund <g0dil@berlios.de>
+//
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; either version 2 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the
+// Free Software Foundation, Inc.,
+// 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+
+/** \file
+    \brief net.test public header */
+
+#ifndef HH_SENF_senf_Socket_Protocols_INet_net_test_
+#define HH_SENF_senf_Socket_Protocols_INet_net_test_ 1
+
+// Custom includes
+#include <boost/format.hpp>
+#include <boost/test/test_tools.hpp>
+
+//#include "net.test.mpp"
+///////////////////////////////hh.p////////////////////////////////////////
+
+namespace {
+
+    void error(char const * fn, char const * proc="")
+    {
+        std::cerr << "\n" << proc << ((*proc)?": ":"") << fn << ": " << strerror(errno) << std::endl;
+    }
+
+    void fail(char const * proc, char const * fn)
+    {
+        error(fn,proc);
+        _exit(1);
+    }
+
+    int base_pid = 0;
+    int server_pid = 0;
+
+    void start(void (*fn)())
+    {
+        if (! base_pid)
+            base_pid = ::getpid();
+        server_pid = ::fork();
+        if (server_pid < 0) BOOST_FAIL("fork()");
+        if (server_pid == 0) {
+            signal(SIGCHLD, SIG_IGN);
+            (*fn)();
+            _exit(0);
+        }
+        signal(SIGCHLD, SIG_DFL);
+    }
+
+    void wait()
+    {
+        int status;
+        if (waitpid(server_pid,&status,0)<0)
+            BOOST_FAIL("waitpid()");
+        BOOST_CHECK_EQUAL( status , 0 );
+    }
+
+    void stop()
+    {
+        if (server_pid) {
+            kill(server_pid,9);
+            wait();
+            server_pid = 0;
+        }
+    }
+
+    unsigned port(unsigned i)
+    {
+        if (! base_pid)
+            base_pid = ::getpid();
+        return 23456u + (((base_pid^(base_pid>>8)^(base_pid>>16)^(base_pid>>24))&0xff)<<2) + i;
+    }
+
+    std::string localhost4str(unsigned i)
+    {
+        return (boost::format("localhost:%d") % port(i)).str();
+    }
+
+    std::string localhost6str(unsigned i)
+    {
+        return (boost::format("[::1]:%d") % port(i)).str();
+    }
+
+}
+
+///////////////////////////////hh.e////////////////////////////////////////
+//#include "net.test.cci"
+//#include "net.test.ct"
+//#include "net.test.cti"
+#endif
+
+\f
+// Local Variables:
+// mode: c++
+// fill-column: 100
+// comment-column: 40
+// c-file-style: "senf"
+// indent-tabs-mode: nil
+// ispell-local-dictionary: "american"
+// compile-command: "scons -u test"
+// End:
index ca59bfa..c4dc8a9 100644 (file)
@@ -42,6 +42,25 @@ namespace {
         senf::scheduler::terminate();
     }
 
+    int base_pid = 0;
+
+    unsigned port(unsigned i)
+    {
+        if (! base_pid)
+            base_pid = ::getpid();
+        return 23456u + (((base_pid^(base_pid>>8)^(base_pid>>16)^(base_pid>>24))&0xff)<<2) + i;
+    }
+
+    std::string localhost4str(unsigned i)
+    {
+        return (boost::format("localhost:%d") % port(i)).str();
+    }
+
+    std::string localhost6str(unsigned i)
+    {
+        return (boost::format("[::1]:%d") % port(i)).str();
+    }
+
     unsigned nread (0);
     unsigned flags (0);
     std::string data;
@@ -62,8 +81,8 @@ namespace {
 
 BOOST_AUTO_UNIT_TEST(udpServer)
 {
-    senf::console::UDPServer server (senf::INet4SocketAddress("127.0.0.1:23232"));
-    senf::ConnectedUDPv4ClientSocketHandle socket (senf::INet4SocketAddress("127.0.0.1:23232"));
+    senf::console::UDPServer server (senf::INet4SocketAddress(localhost4str(0)));
+    senf::ConnectedUDPv4ClientSocketHandle socket (senf::INet4SocketAddress(localhost4str(0)));
     senf::scheduler::TimerEvent timer ("udpServer test timer", &timeout);
     senf::scheduler::FdEvent fdev ("udpServer test fd", boost::bind(&read, socket, _1),
                                    socket, senf::scheduler::FdEvent::EV_READ);
index 1f39fe5..fb3a1ed 100644 (file)
 #define prefix_
 ///////////////////////////////cc.p////////////////////////////////////////
 
+namespace {
+
+    int base_pid = 0;
+
+    unsigned port(unsigned i)
+    {
+        if (! base_pid)
+            base_pid = ::getpid();
+        return 23456u + (((base_pid^(base_pid>>8)^(base_pid>>16)^(base_pid>>24))&0xff)<<2) + i;
+    }
+
+    std::string localhost4str(unsigned i)
+    {
+        return (boost::format("localhost:%d") % port(i)).str();
+    }
+
+    std::string localhost6str(unsigned i)
+    {
+        return (boost::format("[::1]:%d") % port(i)).str();
+    }
+
+}
+
 BOOST_AUTO_UNIT_TEST(syslogUDPTarget)
 {
     senf::log::SyslogUDPTarget udplog (
-        senf::INet4SocketAddress(senf::INet4Address::Loopback, 23444u));
+        senf::INet4SocketAddress(senf::INet4Address::Loopback, port(0)));
     senf::UDPv4ClientSocketHandle server (
-        senf::INet4SocketAddress(senf::INet4Address::Loopback, 23444u));
+        senf::INet4SocketAddress(senf::INet4Address::Loopback, port(0)));
 
     udplog.tag("");
     udplog.showTime(false);