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);
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);
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);
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));
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;
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));
#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 {
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()");
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()");
{
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 );
}
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 );
{
senf::TCPv6ClientSocketHandle sock;
- BOOST_CHECK_THROW( sock.connect(senf::INet6SocketAddress("[::1]:12345")),
+ BOOST_CHECK_THROW( sock.connect(senf::INet6SocketAddress(localhost6str(0))),
senf::SystemException );
}
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 );
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()");
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()");
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);
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);
#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 {
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);
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);
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);
--- /dev/null
+// $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:
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;
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);
#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);