Add additional emacs file variable 'compile-command'
[senf.git] / Socket / UDPSocketHandle.test.cc
1 // Copyright (C) 2007 
2 // Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
3 // Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
4 //     Stefan Bund <g0dil@berlios.de>
5 //
6 // This program is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2 of the License, or
9 // (at your option) any later version.
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the
18 // Free Software Foundation, Inc.,
19 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20
21 /** \file
22     \brief UDPSocketHandle.test unit tests */
23
24 //#include "UDPSocketHandle.test.hh"
25 //#include "UDPSocketHandle.test.ih"
26
27 // Custom includes
28 #include <sys/types.h>
29 #include <sys/ioctl.h>
30 #include <sys/wait.h>
31 #include <unistd.h>
32 #include <sys/socket.h>
33 #include <netinet/in.h>
34 #include "UDPSocketHandle.hh"
35 #include <iostream>
36
37 #include <boost/test/auto_unit_test.hpp>
38 #include <boost/test/test_tools.hpp>
39
40 #define prefix_
41 ///////////////////////////////cc.p////////////////////////////////////////
42
43 namespace {
44
45     void error(char const * fn, char const * proc="")
46     {
47         std::cerr << "\n" << proc << ((*proc)?": ":"") << fn << ": " << strerror(errno) << std::endl;
48     }
49
50     void fail(char const * proc, char const * fn)
51     {
52         error(fn,proc);
53         _exit(1);
54     }
55
56     int server_pid = 0;
57
58     void start(void (*fn)())
59     {
60         server_pid = ::fork();
61         if (server_pid < 0) BOOST_FAIL("fork()");
62         if (server_pid == 0) {
63             (*fn)();
64             _exit(0);
65         }
66     }
67
68     void wait()
69     {
70         int status;
71         if (waitpid(server_pid,&status,0)<0)
72             BOOST_FAIL("waitpid()");
73         BOOST_CHECK_EQUAL( status , 0 );
74     }
75
76     void stop()
77     {
78         if (server_pid) {
79             kill(server_pid,9);
80             wait();
81             server_pid = 0;
82         }
83     }
84
85 }
86
87 ///////////////////////////////////////////////////////////////////////////
88
89 namespace {
90
91     void server_v4()
92     {
93         int sock = socket(PF_INET,SOCK_DGRAM,0);
94         if (sock<0) fail("server_v4","socket()");
95         struct sockaddr_in sin;
96         ::memset(&sin,0,sizeof(sin));
97         sin.sin_family = AF_INET;
98         sin.sin_port = htons(12345);
99         sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
100         if (bind(sock,(struct sockaddr *)&sin,sizeof(sin))<0) fail("server_v4","bind()");
101
102         sin.sin_port = htons(23456);
103         char buffer[1024];
104         while (1) {
105             int n = read(sock,buffer,1024);
106             if (n == 4 && strncmp(buffer,"QUIT",4) == 0)
107                 break;
108             sendto(sock,buffer,n,0,(struct sockaddr *)&sin,sizeof(sin));
109         }
110
111         if (close(sock) < 0) fail("server_v4","close()");
112     }
113
114     void server_v6()
115     {
116         int sock = socket(PF_INET6,SOCK_DGRAM,0);
117         if (sock<0) fail("server_v6","socket()");
118         struct sockaddr_in6 sin;
119         ::memset(&sin,0,sizeof(sin));
120         sin.sin6_family = AF_INET6;
121         sin.sin6_port = htons(12345);
122         sin.sin6_addr = in6addr_loopback;
123         if (bind(sock,(struct sockaddr *)&sin,sizeof(sin))<0) fail("server_v6","bind()");
124
125         sin.sin6_port = htons(23456);
126         char buffer[1024];
127         while (1) {
128             int n = read(sock,buffer,1024);
129             if (n == 4 && strncmp(buffer,"QUIT",4) == 0)
130                 break;
131             sendto(sock,buffer,n,0,(struct sockaddr *)&sin,sizeof(sin));
132         }
133
134         if (close(sock) < 0) fail("server_v6","close()");
135     }
136
137 }
138
139 BOOST_AUTO_UNIT_TEST(udpv4ClientSocketHandle)
140 {
141     try {
142         alarm(10);
143         start(server_v4);
144         senf::UDPv4ClientSocketHandle sock;
145         BOOST_CHECK_NO_THROW( sock.bind("127.0.0.1:23456") );
146         BOOST_CHECK( sock.local() == "127.0.0.1:23456" );
147         BOOST_CHECK_NO_THROW( sock.rcvbuf(2048) );
148         BOOST_CHECK_EQUAL( sock.rcvbuf(), 2048u );
149         BOOST_CHECK_NO_THROW( sock.sndbuf(2048) );
150         BOOST_CHECK_EQUAL( sock.sndbuf(), 2048u );
151         BOOST_CHECK_NO_THROW( sock.writeto("127.0.0.1:12345", std::string("TEST-WRITE")) );
152         BOOST_CHECK_EQUAL( sock.read(), "TEST-WRITE" );
153         BOOST_CHECK_NO_THROW( sock.protocol().timestamp() );
154         sock.writeto("127.0.0.1:12345","QUIT");
155         sleep(1);
156         stop();
157         sleep(1);
158         alarm(0);
159     } catch (...) {
160         alarm(0);
161         sleep(1);
162         stop();
163         sleep(1);
164         throw;
165     }
166 }
167
168 ///////////////////////////////cc.e////////////////////////////////////////
169 #undef prefix_
170
171 \f
172 // Local Variables:
173 // mode: c++
174 // fill-column: 100
175 // c-file-style: "senf"
176 // indent-tabs-mode: nil
177 // ispell-local-dictionary: "american"
178 // compile-command: "scons -u test"
179 // End: