4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 // Stefan Bund <g0dil@berlios.de>
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.
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.
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.
24 \brief UDPSocketHandle.test unit tests */
26 //#include "UDPSocketHandle.test.hh"
27 //#include "UDPSocketHandle.test.ih"
30 #include <sys/types.h>
31 #include <sys/ioctl.h>
34 #include <sys/socket.h>
35 #include <netinet/in.h>
36 #include "UDPSocketHandle.hh"
39 #include "../../../Utils/auto_unit_test.hh"
40 #include <boost/test/test_tools.hpp>
43 ///////////////////////////////cc.p////////////////////////////////////////
47 void error(char const * fn, char const * proc="")
49 std::cerr << "\n" << proc << ((*proc)?": ":"") << fn << ": " << strerror(errno) << std::endl;
52 void fail(char const * proc, char const * fn)
60 void start(void (*fn)())
62 server_pid = ::fork();
63 if (server_pid < 0) BOOST_FAIL("fork()");
64 if (server_pid == 0) {
65 signal(SIGCHLD, SIG_IGN);
69 signal(SIGCHLD, SIG_DFL);
75 if (waitpid(server_pid,&status,0)<0)
76 BOOST_FAIL("waitpid()");
77 BOOST_CHECK_EQUAL( status , 0 );
91 ///////////////////////////////////////////////////////////////////////////
97 int sock = socket(PF_INET,SOCK_DGRAM,0);
98 if (sock<0) fail("server_v4","socket()");
99 struct sockaddr_in sin;
100 ::memset(&sin,0,sizeof(sin));
101 sin.sin_family = AF_INET;
102 sin.sin_port = htons(12345);
103 sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
104 if (bind(sock,(struct sockaddr *)&sin,sizeof(sin))<0) fail("server_v4","bind()");
106 sin.sin_port = htons(23456);
109 int n = read(sock,buffer,1024);
110 if (n == 4 && strncmp(buffer,"QUIT",4) == 0)
112 sendto(sock,buffer,n,0,(struct sockaddr *)&sin,sizeof(sin));
115 if (close(sock) < 0) fail("server_v4","close()");
120 int sock = socket(PF_INET6,SOCK_DGRAM,0);
121 if (sock<0) fail("server_v6","socket()");
122 struct sockaddr_in6 sin;
123 ::memset(&sin,0,sizeof(sin));
124 sin.sin6_family = AF_INET6;
125 sin.sin6_port = htons(12345);
126 sin.sin6_addr = in6addr_loopback;
127 if (bind(sock,(struct sockaddr *)&sin,sizeof(sin))<0) fail("server_v6","bind()");
129 sin.sin6_port = htons(23456);
132 int n = read(sock,buffer,1024);
133 if (n == 4 && strncmp(buffer,"QUIT",4) == 0)
135 sendto(sock,buffer,n,0,(struct sockaddr *)&sin,sizeof(sin));
138 if (close(sock) < 0) fail("server_v6","close()");
143 BOOST_AUTO_UNIT_TEST(udpv4ClientSocketHandle)
148 senf::UDPv4ClientSocketHandle sock;
149 BOOST_CHECK_NO_THROW( sock.bind(senf::INet4SocketAddress("127.0.0.1:23456")) );
150 BOOST_CHECK( sock.local() == senf::INet4SocketAddress("127.0.0.1:23456") );
151 BOOST_CHECK_NO_THROW( sock.protocol().rcvbuf(2048) );
152 BOOST_CHECK_EQUAL( sock.protocol().rcvbuf(), 2048u );
153 BOOST_CHECK_NO_THROW( sock.protocol().sndbuf(2048) );
154 BOOST_CHECK_EQUAL( sock.protocol().sndbuf(), 2048u );
155 BOOST_CHECK_NO_THROW( sock.writeto(senf::INet4SocketAddress("127.0.0.1:12345"),
156 std::string("TEST-WRITE")) );
157 BOOST_CHECK_EQUAL( sock.read(), "TEST-WRITE" );
158 BOOST_CHECK_NO_THROW( sock.protocol().timestamp() );
159 sock.writeto(senf::INet4SocketAddress("127.0.0.1:12345"), std::string("QUIT"));
173 ///////////////////////////////cc.e////////////////////////////////////////
180 // c-file-style: "senf"
181 // indent-tabs-mode: nil
182 // ispell-local-dictionary: "american"
183 // compile-command: "scons -u test"
184 // comment-column: 40