2 Copyright (C) 2004 by Intra2net AG
4 The software in this package is distributed under the GNU General
5 Public License version 2 (with a special exception described below).
7 A copy of GNU General Public License (GPL) is included in this distribution,
8 in the file COPYING.GPL.
10 As a special exception, if other files instantiate templates or use macros
11 or inline functions from this file, or you compile this file and link it
12 with other works to produce a work based on this file, this file
13 does not by itself cause the resulting work to be covered
14 by the GNU General Public License.
16 However the source code for this file must still be made available
17 in accordance with section (3) of the GNU General Public License.
19 This exception does not invalidate any other reasons why a work based
20 on this file might be covered by the GNU General Public License.
22 #include <sys/types.h>
34 #define BOOST_TEST_DYN_LINK
35 #include <boost/test/unit_test.hpp>
37 #include <socket_client.hxx>
38 #include <socket_server.hxx>
40 #include "test_fixtures.hxx"
47 using namespace libt2n;
49 BOOST_FIXTURE_TEST_SUITE(test_reconnect, KillChildOnShutdownFixture)
51 BOOST_AUTO_TEST_CASE(simple_reconnect)
53 switch(child_pid=fork())
57 BOOST_FAIL("fork error");
65 socket_server ss("./socket");
67 time_t t0 = time(NULL);
70 while (time(NULL) < t0 + 10 )
72 ss.fill_buffer(1000000);
77 if(ss.get_packet(data,cid))
79 server_connection* con=ss.get_connection(cid);
85 con->write(string().insert(0,100,'X'));
87 con->write(string().insert(0,100,'Y'));
92 std::cerr << "exception in child. ignoring\n";
95 // don't call atexit and stuff
102 // don't kill us on broken pipe
103 signal(SIGPIPE, SIG_IGN);
105 // wait till server is up
107 socket_client_connection sc("./socket");
113 while (!sc.get_packet(data))
114 sc.fill_buffer(1000000);
120 while (!sc.get_packet(data))
121 sc.fill_buffer(1000000);
123 BOOST_CHECK_EQUAL(string().insert(0,100,'X'),data);
128 BOOST_AUTO_TEST_CASE(reconnect_with_close)
130 switch(child_pid=fork())
134 BOOST_FAIL("fork error");
142 socket_server ss("./socket");
144 time_t t0 = time(NULL);
147 while (time(NULL) < t0 + 10 )
149 ss.fill_buffer(1000000);
154 if(ss.get_packet(data,cid))
156 server_connection* con=ss.get_connection(cid);
162 con->write(string().insert(0,100,'X'));
164 con->write(string().insert(0,100,'Y'));
169 std::cerr << "exception in child. ignoring\n";
172 // don't call atexit and stuff
179 // don't kill us on broken pipe
180 signal(SIGPIPE, SIG_IGN);
182 // wait till server is up
184 socket_client_connection sc("./socket");
190 while (!sc.get_packet(data))
191 sc.fill_buffer(1000000);
202 while (!sc.get_packet(data))
203 sc.fill_buffer(1000000);
205 BOOST_CHECK_EQUAL(string().insert(0,100,'X'),data);
210 BOOST_AUTO_TEST_CASE(reconnect_buffer_complete)
212 switch(child_pid=fork())
216 BOOST_FAIL("fork error");
224 socket_server ss("./socket");
226 time_t t0 = time(NULL);
229 while (time(NULL) < t0 + 10 )
231 ss.fill_buffer(1000000);
236 if(ss.get_packet(data,cid))
238 server_connection* con=ss.get_connection(cid);
244 con->write(string().insert(0,100,'X'));
249 std::cerr << "exception in child. ignoring\n";
252 // don't call atexit and stuff
259 // don't kill us on broken pipe
260 signal(SIGPIPE, SIG_IGN);
262 // wait till server is up
264 socket_client_connection sc("./socket");
270 while (!sc.packet_available())
271 sc.fill_buffer(1000000);
275 BOOST_CHECK_MESSAGE(sc.get_packet(data) == true, "packet not there");
277 BOOST_CHECK_MESSAGE(data == string().insert(0,100,'X'), "data incorrect");
282 BOOST_AUTO_TEST_CASE(reconnect_buffer_several_complete)
286 switch(child_pid=fork())
290 BOOST_FAIL("fork error");
298 socket_server ss("./socket");
300 time_t t0 = time(NULL);
303 while (time(NULL) < t0 + 10 )
305 ss.fill_buffer(1000000);
310 if(ss.get_packet(data,cid))
312 server_connection* con=ss.get_connection(cid);
319 for (int i=0; i<packets; i++)
320 con->write(string().insert(0,100,'X'));
326 std::cerr << "exception in child. ignoring\n";
329 // don't call atexit and stuff
336 // don't kill us on broken pipe
337 signal(SIGPIPE, SIG_IGN);
339 // wait till server is up
341 socket_client_connection sc("./socket");
345 // retrieve packets for 3 seconds
346 time_t t0 = time(NULL);
349 while (time(NULL) < t0 + 3 )
350 sc.fill_buffer(1000000);
352 // we now should have packets complete packets in the buffer
356 // are these packets still there?
358 for (int i=0; i < packets; i++)
363 os << "packet " << i << " not there";
365 BOOST_CHECK_MESSAGE(sc.get_packet(data) == true, os.str());
368 os << "packet " << i << " incorrect";
370 BOOST_CHECK_MESSAGE(string().insert(0,100,'X') == data, os.str());
376 BOOST_AUTO_TEST_CASE(reconnect_buffer_no_incomplete1)
378 switch(child_pid=fork())
382 BOOST_FAIL("fork error");
390 socket_server ss("./socket");
392 time_t t0 = time(NULL);
395 while (time(NULL) < t0 + 10 )
397 ss.fill_buffer(1000000);
402 if(ss.get_packet(data,cid))
404 server_connection* con=ss.get_connection(cid);
411 con->write(string().insert(0,100,'X'));
412 send_raw_socket("aaaab",&ss,cid);
418 std::cerr << "exception in child. ignoring\n";
421 // don't call atexit and stuff
428 // don't kill us on broken pipe
429 signal(SIGPIPE, SIG_IGN);
431 // wait till server is up
433 socket_client_connection sc("./socket");
437 // retrieve packets for 3 seconds
438 time_t t0 = time(NULL);
441 while (time(NULL) < t0 + 3 )
442 sc.fill_buffer(1000000);
444 // we now should have one complete packet and some stuff in the buffer
449 BOOST_CHECK_MESSAGE((sc.peek_packet(data))>0, "no incomplete packet");
453 BOOST_CHECK_MESSAGE((int)sc.peek_packet(data) == 0, "incomplete packet not removed");
458 BOOST_AUTO_TEST_CASE(reconnect_buffer_no_incomplete2)
460 switch(child_pid=fork())
464 BOOST_FAIL("fork error");
472 socket_server ss("./socket");
474 time_t t0 = time(NULL);
477 while (time(NULL) < t0 + 10 )
479 ss.fill_buffer(1000000);
484 if(ss.get_packet(data,cid))
486 server_connection* con=ss.get_connection(cid);
493 con->write(string().insert(0,100,'X'));
495 string blob=string().insert(0,100,'Y');
497 // one byte will be missing...
498 int size=blob.size()+1;
499 char sizetransfer[sizeof(int)+1];
500 memcpy(sizetransfer,(void*)&size,sizeof(int));
501 sizetransfer[sizeof(int)+1]=0;
503 string packet=string(sizetransfer)+blob;
505 send_raw_socket(packet,&ss,cid);
511 std::cerr << "exception in child. ignoring\n";
514 // don't call atexit and stuff
521 // don't kill us on broken pipe
522 signal(SIGPIPE, SIG_IGN);
524 // wait till server is up
526 socket_client_connection sc("./socket");
530 // retrieve packets for 3 seconds
531 time_t t0 = time(NULL);
534 while (time(NULL) < t0 + 3 )
535 sc.fill_buffer(1000000);
537 // we now should have one complete packet and some stuff in the buffer
543 BOOST_CHECK_MESSAGE(sc.get_packet(data) == true, "packet not there");
544 BOOST_CHECK_MESSAGE(string().insert(0,100,'X') == data, "data incorrect");
546 BOOST_CHECK_MESSAGE((int)sc.peek_packet(data) == 0, "incomplete packet not removed");
551 BOOST_AUTO_TEST_SUITE_END()