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>
33 #include <sys/socket.h>
34 #include <netinet/in.h>
35 #include "RawINetSocketHandle.hh"
36 #include "net.test.hh"
38 #include <senf/Utils/auto_unit_test.hh>
39 #include <boost/test/test_tools.hpp>
41 //-/////////////////////////////////////////////////////////////////////////////////////////////////
43 using namespace senf::test;
47 void server_v4() //listen for packets with proto=47 (GRE) and resend them with proto=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);
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)");
62 int n = read(sockrec,buffer,1024);
63 if (n == 20+4 )//&& strncmp(,"QUIT",4) == 0)
66 //jaja, fieses gehacke...
67 sendto(socksend,buffer+20,n-20,0,(struct sockaddr *)&sin,sizeof(sin));
70 if (close(sockrec) < 0) fail("server_v4","close(rec)");
71 if (close(socksend) < 0) fail("server_v4","close(send)");
73 void server_v6() //listen for packets with proto=47 (GRE) and resend them with proto=48
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)");
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)
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)");
95 if (close(sockrec6) < 0) fail("server_v6","close(rec)");
96 if (close(socksend6) < 0) fail("server_v6","close(send)");
101 SENF_AUTO_UNIT_TEST(RawV4ClientSocketHandle)
104 BOOST_WARN_MESSAGE(false, "Cannot test senf::RawV4SocketHandle as non-root user");
109 std::string test = "TEST-WRITE";
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"));
131 SENF_AUTO_UNIT_TEST(RawV6ClientSocketHandle)
134 BOOST_WARN_MESSAGE(false, "Cannot test senf::RawV6SocketHandle as non-root user");
139 std::string test = "TEST-WRITE";
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"));
160 //-/////////////////////////////////////////////////////////////////////////////////////////////////
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"