1 // $Id: RawINetSocketHandle.test.cc 597 2008-01-15 09:16:20Z g0dil $
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
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
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.
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.
19 // The Original Code is Fraunhofer FOKUS code.
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.
26 // David Wagner <dw6@berlios.de>
28 \brief RawINetSocketHandle unit tests */
30 #include <sys/types.h>
31 #include <sys/ioctl.h>
34 #include <sys/socket.h>
35 #include <netinet/in.h>
36 #include "RawINetSocketHandle.hh"
39 #include <senf/Utils/auto_unit_test.hh>
40 #include <boost/test/test_tools.hpp>
44 void error(char const * fn, char const * proc="")
46 std::cerr << "\n" << proc << ((*proc)?": ":"") << fn << ": " << strerror(errno) << std::endl;
49 void fail(char const * proc, char const * fn)
57 void start(void (*fn)())
59 server_pid = ::fork();
60 if (server_pid < 0) BOOST_FAIL("fork()");
61 if (server_pid == 0) {
70 if (waitpid(server_pid,&status,0)<0)
71 BOOST_FAIL("waitpid()");
72 BOOST_CHECK_EQUAL( status , 0 );
86 //-/////////////////////////////////////////////////////////////////////////////////////////////////
90 void server_v4() //listen for packets with proto=47 (GRE) and resend them with proto=48
92 struct sockaddr_in sin;
93 ::memset(&sin,0,sizeof(sin));
94 sin.sin_family = AF_INET;
95 sin.sin_port = htons(0);
96 sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
98 int sockrec = socket(PF_INET,SOCK_RAW,47);
99 if (sockrec<0) fail("server_v4","socket(rec)");
100 int socksend = socket(PF_INET,SOCK_RAW,48);
101 if (socksend<0) fail("server_v4","socket(send)");
105 int n = read(sockrec,buffer,1024);
106 if (n == 20+4 )//&& strncmp(,"QUIT",4) == 0)
109 //jaja, fieses gehacke...
110 sendto(socksend,buffer+20,n-20,0,(struct sockaddr *)&sin,sizeof(sin));
113 if (close(sockrec) < 0) fail("server_v4","close(rec)");
114 if (close(socksend) < 0) fail("server_v4","close(send)");
116 void server_v6() //listen for packets with proto=47 (GRE) and resend them with proto=48
118 struct sockaddr_in6 sin;
119 ::memset(&sin,0,sizeof(sin));
120 sin.sin6_family = AF_INET6;
121 sin.sin6_port = htons(0);
122 inet_pton(AF_INET6, "::1", &sin.sin6_addr);
123 int sockrec6 = socket(PF_INET6,SOCK_RAW,47);
124 if (sockrec6<0) fail("server_v6","socket(rec)");
125 int socksend6 = socket(PF_INET6,SOCK_RAW,48);
126 if (socksend6<0) fail("server_v6","socket(send)");
129 int n = read(sockrec6,buffer,1024);
130 if (n<0) fail("server_v6","read(sockrec6)");
131 if (n == 4 && strncmp(buffer,"QUIT",4) == 0)
134 //jaja, fieses gehacke...
135 n = sendto(socksend6,buffer,n,0,(struct sockaddr *)&sin,sizeof(sin));
136 if (n<0) fail("server_v6","sendto(socksend6)");
138 if (close(sockrec6) < 0) fail("server_v6","close(rec)");
139 if (close(socksend6) < 0) fail("server_v6","close(send)");
144 SENF_AUTO_UNIT_TEST(RawV4ClientSocketHandle)
147 BOOST_WARN_MESSAGE(false, "Cannot test senf::RawV4SocketHandle as non-root user");
152 std::string test = "TEST-WRITE";
155 senf::RawV4ClientSocketHandle sock(47); //IPPROTO_GRE
156 SENF_CHECK_NO_THROW( sock.protocol().rcvbuf(2048) );
157 BOOST_CHECK_EQUAL( sock.protocol().rcvbuf(), 2048u );
158 SENF_CHECK_NO_THROW( sock.protocol().sndbuf(2048) );
159 BOOST_CHECK_EQUAL( sock.protocol().sndbuf(), 2048u );
160 SENF_CHECK_NO_THROW( sock.writeto(senf::INet4SocketAddress("127.0.0.1:0"), test) );
161 senf::RawV4ClientSocketHandle sockrec(48); //IPPROTO_GRE+1
162 std::string in = sockrec.read();
163 BOOST_CHECK_EQUAL(in.substr(20), test);
164 SENF_CHECK_NO_THROW( sock.writeto(senf::INet4SocketAddress("127.0.0.1:0"),"QUIT"));
174 SENF_AUTO_UNIT_TEST(RawV6ClientSocketHandle)
177 BOOST_WARN_MESSAGE(false, "Cannot test senf::RawV6SocketHandle as non-root user");
182 std::string test = "TEST-WRITE";
186 senf::RawV6ClientSocketHandle sock(47); //IPPROTO_GRE
187 SENF_CHECK_NO_THROW( sock.protocol().rcvbuf(2048) );
188 BOOST_CHECK_EQUAL( sock.protocol().rcvbuf(), 2048u );
189 SENF_CHECK_NO_THROW( sock.protocol().sndbuf(2048) );
190 BOOST_CHECK_EQUAL( sock.protocol().sndbuf(), 2048u );
191 SENF_CHECK_NO_THROW( sock.writeto(senf::INet6SocketAddress("[::1]:0"), test) );
192 senf::RawV6ClientSocketHandle sockrec(48); //IPPROTO_GRE+1
193 std::string in = sockrec.read();
194 BOOST_CHECK_EQUAL(in, test);
195 SENF_CHECK_NO_THROW( sock.writeto(senf::INet6SocketAddress("[::1]:0"),"QUIT"));
203 //-/////////////////////////////////////////////////////////////////////////////////////////////////
210 // comment-column: 40
211 // c-file-style: "senf"
212 // indent-tabs-mode: nil
213 // ispell-local-dictionary: "american"
214 // compile-command: "scons -u test"