several fixes for clang/llvm
[senf.git] / senf / Socket / Protocols / INet / RawINetSocketHandle.test.cc
1 // $Id: RawINetSocketHandle.test.cc 597 2008-01-15 09:16:20Z g0dil $
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 //   David Wagner <dw6@berlios.de>
27 /** \file
28     \brief RawINetSocketHandle unit tests */
29
30 #include <sys/types.h>
31 #include <sys/ioctl.h>
32 #include <unistd.h>
33 #include <sys/socket.h>
34 #include <netinet/in.h>
35 #include "RawINetSocketHandle.hh"
36 #include "net.test.hh"
37
38 #include <senf/Utils/auto_unit_test.hh>
39 #include <boost/test/test_tools.hpp>
40
41 //-/////////////////////////////////////////////////////////////////////////////////////////////////
42
43 using namespace senf::test;
44
45 namespace {
46
47     void server_v4() //listen for packets with proto=47 (GRE) and resend them with proto=48
48     {
49         struct sockaddr_in sin;
50         ::memset(&sin,0,sizeof(sin));
51         sin.sin_family = AF_INET;
52         sin.sin_port = htons(0);
53         sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
54
55         int sockrec = socket(PF_INET,SOCK_RAW,47);
56         if (sockrec<0) fail("server_v4","socket(rec)");
57         int socksend = socket(PF_INET,SOCK_RAW,48);
58         if (socksend<0) fail("server_v4","socket(send)");
59
60         char buffer[1024];
61         while (1) {
62             int n = read(sockrec,buffer,1024);
63             if (n == 20+4 )//&& strncmp(,"QUIT",4) == 0)
64                 break;
65             sleep(1);
66             //jaja, fieses gehacke...
67             sendto(socksend,buffer+20,n-20,0,(struct sockaddr *)&sin,sizeof(sin));
68         }
69
70         if (close(sockrec) < 0) fail("server_v4","close(rec)");
71         if (close(socksend) < 0) fail("server_v4","close(send)");
72     }
73     void server_v6() //listen for packets with proto=47 (GRE) and resend them with proto=48
74     {
75         struct sockaddr_in6 sin;
76         ::memset(&sin,0,sizeof(sin));
77         sin.sin6_family = AF_INET6;
78         sin.sin6_port = htons(0);
79         inet_pton(AF_INET6, "::1", &sin.sin6_addr);
80         int sockrec6 = socket(PF_INET6,SOCK_RAW,47);
81         if (sockrec6<0) fail("server_v6","socket(rec)");
82         int socksend6 = socket(PF_INET6,SOCK_RAW,48);
83         if (socksend6<0) fail("server_v6","socket(send)");
84         char buffer[1024];
85         while (1) {
86             int n = read(sockrec6,buffer,1024);
87             if (n<0) fail("server_v6","read(sockrec6)");
88             if (n == 4 && strncmp(buffer,"QUIT",4) == 0)
89                 break;
90             sleep(1);
91             //jaja, fieses gehacke...
92             n = sendto(socksend6,buffer,n,0,(struct sockaddr *)&sin,sizeof(sin));
93             if (n<0) fail("server_v6","sendto(socksend6)");
94         }
95         if (close(sockrec6) < 0) fail("server_v6","close(rec)");
96         if (close(socksend6) < 0) fail("server_v6","close(send)");
97     }
98
99 }
100
101 SENF_AUTO_UNIT_TEST(RawV4ClientSocketHandle)
102 {
103     if (getuid() != 0) {
104         BOOST_WARN_MESSAGE(false, "Cannot test senf::RawV4SocketHandle as non-root user");
105         BOOST_CHECK( true );
106         return;
107     }
108     try {
109         std::string test = "TEST-WRITE";
110         alarm(10);
111         start(server_v4);
112         senf::RawV4ClientSocketHandle sock(47);  //IPPROTO_GRE
113         SENF_CHECK_NO_THROW( sock.protocol().rcvbuf(2048) );
114         BOOST_CHECK_EQUAL( sock.protocol().rcvbuf(), 2048u );
115         SENF_CHECK_NO_THROW( sock.protocol().sndbuf(2048) );
116         BOOST_CHECK_EQUAL( sock.protocol().sndbuf(), 2048u );
117         SENF_CHECK_NO_THROW( sock.writeto(senf::INet4SocketAddress("127.0.0.1:0"), test) );
118         senf::RawV4ClientSocketHandle sockrec(48);  //IPPROTO_GRE+1
119         std::string in = sockrec.read();
120         BOOST_CHECK_EQUAL(in.substr(20), test);
121         SENF_CHECK_NO_THROW( sock.writeto(senf::INet4SocketAddress("127.0.0.1:0"),"QUIT"));
122         //sock.close();
123         //sockrec.close();
124         alarm(0);
125     } catch (...) {
126         alarm(0);
127         throw;
128     }
129 }
130
131 SENF_AUTO_UNIT_TEST(RawV6ClientSocketHandle)
132 {
133     if (getuid() != 0) {
134         BOOST_WARN_MESSAGE(false, "Cannot test senf::RawV6SocketHandle as non-root user");
135         BOOST_CHECK( true );
136         return;
137     }
138     try {
139         std::string test = "TEST-WRITE";
140         alarm(5);
141         start(server_v6);
142         sleep(1);
143         senf::RawV6ClientSocketHandle sock(47);  //IPPROTO_GRE
144         SENF_CHECK_NO_THROW( sock.protocol().rcvbuf(2048) );
145         BOOST_CHECK_EQUAL( sock.protocol().rcvbuf(), 2048u );
146         SENF_CHECK_NO_THROW( sock.protocol().sndbuf(2048) );
147         BOOST_CHECK_EQUAL( sock.protocol().sndbuf(), 2048u );
148         SENF_CHECK_NO_THROW( sock.writeto(senf::INet6SocketAddress("[::1]:0"), test) );
149         senf::RawV6ClientSocketHandle sockrec(48);  //IPPROTO_GRE+1
150         std::string in = sockrec.read();
151         BOOST_CHECK_EQUAL(in, test);
152         SENF_CHECK_NO_THROW( sock.writeto(senf::INet6SocketAddress("[::1]:0"),"QUIT"));
153         alarm(0);
154     } catch (...) {
155         alarm(0);
156         throw;
157     }
158 }
159
160 //-/////////////////////////////////////////////////////////////////////////////////////////////////
161 #undef prefix_
162
163 \f
164 // Local Variables:
165 // mode: c++
166 // fill-column: 100
167 // comment-column: 40
168 // c-file-style: "senf"
169 // indent-tabs-mode: nil
170 // ispell-local-dictionary: "american"
171 // compile-command: "scons -u test"
172 // End: