Fix SCons 1.2.0 build failure
[senf.git] / senf / Socket / Protocols / INet / UDPSocketHandle.test.cc
1 // $Id$
2 //
3 // Copyright (C) 2007
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 UDPSocketHandle unit tests */
25
26 //#include "UDPSocketHandle.test.hh"
27 //#include "UDPSocketHandle.test.ih"
28
29 // Custom includes
30 #include <sys/types.h>
31 #include <sys/ioctl.h>
32 #include <sys/wait.h>
33 #include <unistd.h>
34 #include <sys/socket.h>
35 #include <netinet/in.h>
36 #include "UDPSocketHandle.hh"
37 #include <iostream>
38
39 #include "../../../Utils/auto_unit_test.hh"
40 #include <boost/test/test_tools.hpp>
41
42 #define prefix_
43 ///////////////////////////////cc.p////////////////////////////////////////
44
45 namespace {
46
47     void error(char const * fn, char const * proc="")
48     {
49         std::cerr << "\n" << proc << ((*proc)?": ":"") << fn << ": " << strerror(errno) << std::endl;
50     }
51
52     void fail(char const * proc, char const * fn)
53     {
54         error(fn,proc);
55         _exit(1);
56     }
57
58     int server_pid = 0;
59
60     void start(void (*fn)())
61     {
62         server_pid = ::fork();
63         if (server_pid < 0) BOOST_FAIL("fork()");
64         if (server_pid == 0) {
65             signal(SIGCHLD, SIG_IGN);
66             (*fn)();
67             _exit(0);
68         }
69         signal(SIGCHLD, SIG_DFL);
70     }
71
72     void wait()
73     {
74         int status;
75         if (waitpid(server_pid,&status,0)<0)
76             BOOST_FAIL("waitpid()");
77         BOOST_CHECK_EQUAL( status , 0 );
78     }
79
80     void stop()
81     {
82         if (server_pid) {
83             kill(server_pid,9);
84             wait();
85             server_pid = 0;
86         }
87     }
88
89 }
90
91 ///////////////////////////////////////////////////////////////////////////
92
93 namespace {
94
95     void server_v4()
96     {
97         int sock = socket(PF_INET,SOCK_DGRAM,0);
98         if (sock<0) fail("server_v4","socket()");
99         struct sockaddr_in sin;
100         ::memset(&sin,0,sizeof(sin));
101         sin.sin_family = AF_INET;
102         sin.sin_port = htons(12345);
103         sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
104         if (bind(sock,(struct sockaddr *)&sin,sizeof(sin))<0) fail("server_v4","bind()");
105
106         sin.sin_port = htons(23456);
107         char buffer[1024];
108         while (1) {
109             int n = read(sock,buffer,1024);
110             if (n == 4 && strncmp(buffer,"QUIT",4) == 0)
111                 break;
112             sendto(sock,buffer,n,0,(struct sockaddr *)&sin,sizeof(sin));
113         }
114
115         if (close(sock) < 0) fail("server_v4","close()");
116     }
117
118     void server_v6()
119     {
120         int sock = socket(PF_INET6,SOCK_DGRAM,0);
121         if (sock<0) fail("server_v6","socket()");
122         struct sockaddr_in6 sin;
123         ::memset(&sin,0,sizeof(sin));
124         sin.sin6_family = AF_INET6;
125         sin.sin6_port = htons(12345);
126         sin.sin6_addr = in6addr_loopback;
127         if (bind(sock,(struct sockaddr *)&sin,sizeof(sin))<0) fail("server_v6","bind()");
128
129         sin.sin6_port = htons(23456);
130         char buffer[1024];
131         while (1) {
132             int n = read(sock,buffer,1024);
133             if (n == 4 && strncmp(buffer,"QUIT",4) == 0)
134                 break;
135             sendto(sock,buffer,n,0,(struct sockaddr *)&sin,sizeof(sin));
136         }
137
138         if (close(sock) < 0) fail("server_v6","close()");
139     }
140
141 }
142
143 BOOST_AUTO_UNIT_TEST(udpv4ClientSocketHandle)
144 {
145     try {
146         alarm(10);
147         start(server_v4);
148         senf::UDPv4ClientSocketHandle sock;
149         SENF_CHECK_NO_THROW( sock.bind(senf::INet4SocketAddress("127.0.0.1:23456")) );
150         BOOST_CHECK( sock.local() == senf::INet4SocketAddress("127.0.0.1:23456") );
151         SENF_CHECK_NO_THROW( sock.protocol().rcvbuf(2048) );
152         BOOST_CHECK_EQUAL( sock.protocol().rcvbuf(), 2048u );
153         SENF_CHECK_NO_THROW( sock.protocol().sndbuf(2048) );
154         BOOST_CHECK_EQUAL( sock.protocol().sndbuf(), 2048u );
155         SENF_CHECK_NO_THROW( sock.writeto(senf::INet4SocketAddress("127.0.0.1:12345"),
156                                            std::string("TEST-WRITE")) );
157         BOOST_CHECK_EQUAL( sock.read(), "TEST-WRITE" );
158         SENF_CHECK_NO_THROW( sock.protocol().timestamp() );
159         sock.writeto(senf::INet4SocketAddress("127.0.0.1:12345"), std::string("QUIT"));
160         sleep(1);
161         stop();
162         sleep(1);
163         alarm(0);
164     } catch (...) {
165         alarm(0);
166         sleep(1);
167         stop();
168         sleep(1);
169         throw;
170     }
171 }
172
173 ///////////////////////////////cc.e////////////////////////////////////////
174 #undef prefix_
175
176 \f
177 // Local Variables:
178 // mode: c++
179 // fill-column: 100
180 // c-file-style: "senf"
181 // indent-tabs-mode: nil
182 // ispell-local-dictionary: "american"
183 // compile-command: "scons -u test"
184 // comment-column: 40
185 // End: