aee08d144ca6de93406df5e0bcdc6cb6a04d6737
[senf.git] / senf / Socket / Protocols / INet / net.test.cc
1 // $Id$
2 //
3 // Copyright (C) 2009
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 //
6 // The contents of this file are subject to the Fraunhofer FOKUS Public License
7 // Version 1.0 (the "License"); you may not use this file except in compliance
8 // with the License. You may obtain a copy of the License at 
9 // http://senf.berlios.de/license.html
10 //
11 // The Fraunhofer FOKUS Public License Version 1.0 is based on, 
12 // but modifies the Mozilla Public License Version 1.1.
13 // See the full license text for the amendments.
14 //
15 // Software distributed under the License is distributed on an "AS IS" basis, 
16 // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
17 // for the specific language governing rights and limitations under the License.
18 //
19 // The Original Code is Fraunhofer FOKUS code.
20 //
21 // The Initial Developer of the Original Code is Fraunhofer-Gesellschaft e.V. 
22 // (registered association), Hansastraße 27 c, 80686 Munich, Germany.
23 // All Rights Reserved.
24 //
25 // Contributor(s):
26 //   Stefan Bund <g0dil@berlios.de>
27
28 /** \file
29     \brief net.test public header */
30
31 #include "net.test.hh"
32
33 // Custom includes
34 #include <iostream>
35 #include <string.h>
36 #include <errno.h>
37 #include <signal.h>
38 #include <sys/wait.h>
39 #include <boost/format.hpp>
40 #include <senf/Utils/auto_unit_test.hh>
41 #include <boost/test/test_tools.hpp>
42
43 //-/////////////////////////////////////////////////////////////////////////////////////////////////
44 namespace {
45     int base_pid = 0;
46     int server_pid = 0;
47 }
48
49 void senf::test::error(char const * fn, char const * proc)
50 {
51     std::cerr << "\n" << proc << ((*proc)?": ":"") << fn << ": " << strerror(errno) << std::endl;
52 }
53
54 void senf::test::fail(char const * proc, char const * fn)
55 {
56     error(fn,proc);
57     _exit(1);
58 }
59
60 void senf::test::start(void (*fn)())
61 {
62     if (! base_pid)
63         base_pid = ::getpid();
64     server_pid = ::fork();
65     if (server_pid < 0) BOOST_FAIL("fork()");
66     if (server_pid == 0) {
67         signal(SIGCHLD, SIG_IGN);
68         (*fn)();
69         _exit(0);
70     }
71     signal(SIGCHLD, SIG_DFL);
72     sleep(1);
73 }
74
75 void senf::test::wait()
76 {
77     int status;
78     if (waitpid(server_pid,&status,0)<0)
79         BOOST_FAIL("waitpid()");
80     BOOST_CHECK_EQUAL( status , 0 );
81 }
82
83 void senf::test::stop()
84 {
85     if (server_pid) {
86         kill(server_pid,9);
87         wait();
88         server_pid = 0;
89     }
90 }
91
92 unsigned senf::test::port(unsigned i)
93 {
94     if (! base_pid)
95         base_pid = ::getpid();
96     return 23456u + (((base_pid^(base_pid>>8)^(base_pid>>16)^(base_pid>>24))&0xff)<<2) + i;
97 }
98
99 std::string senf::test::localhost4str(unsigned i)
100 {
101     return (boost::format("localhost:%d") % port(i)).str();
102 }
103
104 std::string senf::test::localhost6str(unsigned i)
105 {
106     return (boost::format("[::1]:%d") % port(i)).str();
107 }
108
109
110 //-/////////////////////////////////////////////////////////////////////////////////////////////////
111 //#include "net.test.cci"
112 //#include "net.test.ct"
113 //#include "net.test.cti"
114
115 \f
116 // Local Variables:
117 // mode: c++
118 // fill-column: 100
119 // comment-column: 40
120 // c-file-style: "senf"
121 // indent-tabs-mode: nil
122 // ispell-local-dictionary: "american"
123 // compile-command: "scons -u test"
124 // End: