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