Fix network port collisions on concurrent unit-tests
[senf.git] / senf / Socket / Protocols / INet / net.test.hh
1 // $Id$
2 //
3 // Copyright (C) 2009 
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 //     Stefan Bund <g0dil@berlios.de>
7 //
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 // GNU General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the
20 // Free Software Foundation, Inc.,
21 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
23 /** \file
24     \brief net.test public header */
25
26 #ifndef HH_SENF_senf_Socket_Protocols_INet_net_test_
27 #define HH_SENF_senf_Socket_Protocols_INet_net_test_ 1
28
29 // Custom includes
30 #include <boost/format.hpp>
31 #include <boost/test/test_tools.hpp>
32
33 //#include "net.test.mpp"
34 ///////////////////////////////hh.p////////////////////////////////////////
35
36 namespace {
37
38     void error(char const * fn, char const * proc="")
39     {
40         std::cerr << "\n" << proc << ((*proc)?": ":"") << fn << ": " << strerror(errno) << std::endl;
41     }
42
43     void fail(char const * proc, char const * fn)
44     {
45         error(fn,proc);
46         _exit(1);
47     }
48
49     int base_pid = 0;
50     int server_pid = 0;
51
52     void start(void (*fn)())
53     {
54         if (! base_pid)
55             base_pid = ::getpid();
56         server_pid = ::fork();
57         if (server_pid < 0) BOOST_FAIL("fork()");
58         if (server_pid == 0) {
59             signal(SIGCHLD, SIG_IGN);
60             (*fn)();
61             _exit(0);
62         }
63         signal(SIGCHLD, SIG_DFL);
64     }
65
66     void wait()
67     {
68         int status;
69         if (waitpid(server_pid,&status,0)<0)
70             BOOST_FAIL("waitpid()");
71         BOOST_CHECK_EQUAL( status , 0 );
72     }
73
74     void stop()
75     {
76         if (server_pid) {
77             kill(server_pid,9);
78             wait();
79             server_pid = 0;
80         }
81     }
82
83     unsigned port(unsigned i)
84     {
85         if (! base_pid)
86             base_pid = ::getpid();
87         return 23456u + (((base_pid^(base_pid>>8)^(base_pid>>16)^(base_pid>>24))&0xff)<<2) + i;
88     }
89
90     std::string localhost4str(unsigned i)
91     {
92         return (boost::format("localhost:%d") % port(i)).str();
93     }
94
95     std::string localhost6str(unsigned i)
96     {
97         return (boost::format("[::1]:%d") % port(i)).str();
98     }
99
100 }
101
102 ///////////////////////////////hh.e////////////////////////////////////////
103 //#include "net.test.cci"
104 //#include "net.test.ct"
105 //#include "net.test.cti"
106 #endif
107
108 \f
109 // Local Variables:
110 // mode: c++
111 // fill-column: 100
112 // comment-column: 40
113 // c-file-style: "senf"
114 // indent-tabs-mode: nil
115 // ispell-local-dictionary: "american"
116 // compile-command: "scons -u test"
117 // End: