fd558846724fb1b4a86b9d73cb93cc87e305ddab
[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 <iostream>
37 #include "UDPSocketHandle.hh"
38 #include "net.test.hh"
39
40 #include <senf/Utils/auto_unit_test.hh>
41 #include <boost/test/test_tools.hpp>
42
43 #define prefix_
44 //-/////////////////////////////////////////////////////////////////////////////////////////////////
45
46 //-/////////////////////////////////////////////////////////////////////////////////////////////////
47
48 namespace {
49
50     void server_v4()
51     {
52         int sock = socket(PF_INET,SOCK_DGRAM,0);
53         if (sock<0) fail("server_v4","socket()");
54         struct sockaddr_in sin;
55         ::memset(&sin,0,sizeof(sin));
56         sin.sin_family = AF_INET;
57         sin.sin_port = htons(port(0));
58         sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
59         if (bind(sock,(struct sockaddr *)&sin,sizeof(sin))<0) fail("server_v4","bind()");
60
61         sin.sin_port = htons(port(1));
62         char buffer[1024];
63         while (1) {
64             int n = read(sock,buffer,1024);
65             if (n == 4 && strncmp(buffer,"QUIT",4) == 0)
66                 break;
67             sendto(sock,buffer,n,0,(struct sockaddr *)&sin,sizeof(sin));
68         }
69
70         if (close(sock) < 0) fail("server_v4","close()");
71     }
72
73     void server_v6()
74     {
75         int sock = socket(PF_INET6,SOCK_DGRAM,0);
76         if (sock<0) fail("server_v6","socket()");
77         struct sockaddr_in6 sin;
78         ::memset(&sin,0,sizeof(sin));
79         sin.sin6_family = AF_INET6;
80         sin.sin6_port = htons(port(0));
81         sin.sin6_addr = in6addr_loopback;
82         if (bind(sock,(struct sockaddr *)&sin,sizeof(sin))<0) fail("server_v6","bind()");
83
84         sin.sin6_port = htons(port(1));
85         char buffer[1024];
86         while (1) {
87             int n = read(sock,buffer,1024);
88             if (n == 4 && strncmp(buffer,"QUIT",4) == 0)
89                 break;
90             sendto(sock,buffer,n,0,(struct sockaddr *)&sin,sizeof(sin));
91         }
92
93         if (close(sock) < 0) fail("server_v6","close()");
94     }
95
96 }
97
98 SENF_AUTO_UNIT_TEST(udpv4ClientSocketHandle)
99 {
100     try {
101         alarm(10);
102         start(server_v4);
103         senf::UDPv4ClientSocketHandle sock;
104         SENF_CHECK_NO_THROW( sock.bind(senf::INet4SocketAddress(localhost4str(1))) );
105         BOOST_CHECK( sock.local() == senf::INet4SocketAddress(localhost4str(1)) );
106         SENF_CHECK_NO_THROW( sock.protocol().rcvbuf(2048) );
107         BOOST_CHECK_EQUAL( sock.protocol().rcvbuf(), 2048u );
108         SENF_CHECK_NO_THROW( sock.protocol().sndbuf(2048) );
109         BOOST_CHECK_EQUAL( sock.protocol().sndbuf(), 2048u );
110         SENF_CHECK_NO_THROW( sock.writeto(senf::INet4SocketAddress(localhost4str(0)),
111                                            std::string("TEST-WRITE")) );
112         BOOST_CHECK_EQUAL( sock.read(), "TEST-WRITE" );
113         SENF_CHECK_NO_THROW( sock.protocol().timestamp() );
114         sock.writeto(senf::INet4SocketAddress(localhost4str(0)), std::string("QUIT"));
115         sleep(1);
116         stop();
117         sleep(1);
118         alarm(0);
119     } catch (...) {
120         alarm(0);
121         sleep(1);
122         stop();
123         sleep(1);
124         throw;
125     }
126 }
127
128 //-/////////////////////////////////////////////////////////////////////////////////////////////////
129 #undef prefix_
130
131 \f
132 // Local Variables:
133 // mode: c++
134 // fill-column: 100
135 // c-file-style: "senf"
136 // indent-tabs-mode: nil
137 // ispell-local-dictionary: "american"
138 // compile-command: "scons -u test"
139 // comment-column: 40
140 // End: