1 /***************************************************************************
2 * Copyright (C) 2004 by Intra2net AG *
5 ***************************************************************************/
19 #include <cppunit/extensions/TestFactoryRegistry.h>
20 #include <cppunit/ui/text/TestRunner.h>
21 #include <cppunit/extensions/HelperMacros.h>
23 #include <socket_client.hxx>
24 #include <socket_server.hxx>
31 using namespace libt2n;
32 using namespace CppUnit;
34 // this is an evil hack to get access to real_write, don't ever do this in an app!!!
35 class real_write_connection: public socket_server_connection
38 void real_write(const std::string& data)
39 { socket_write(data); }
42 class test_reconnect : public TestFixture
44 CPPUNIT_TEST_SUITE(test_reconnect);
46 CPPUNIT_TEST(simple_reconnect);
47 CPPUNIT_TEST(reconnect_with_close);
48 CPPUNIT_TEST(reconnect_buffer_complete);
49 CPPUNIT_TEST(reconnect_buffer_several_complete);
50 CPPUNIT_TEST(reconnect_buffer_no_incomplete1);
51 CPPUNIT_TEST(reconnect_buffer_no_incomplete2);
53 CPPUNIT_TEST_SUITE_END();
64 // make sure the server-child is dead before the next test runs
65 kill(child_pid,SIGKILL);
69 void send_raw_socket(string hello_string, socket_server* ss, int conn_id)
71 socket_server_connection *ssc=dynamic_cast<socket_server_connection*>(ss->get_connection(conn_id));
73 // this is an evil hack to get access to real_write, don't ever do this in an app!!!
74 real_write_connection *rwc=(real_write_connection*)ssc;
75 rwc->real_write(hello_string);
78 void simple_reconnect()
80 switch(child_pid=fork())
84 CPPUNIT_FAIL("fork error");
90 socket_server ss("./socket");
92 time_t t0 = time(NULL);
95 while (time(NULL) < t0 + 10 )
97 ss.fill_buffer(1000000);
102 if(ss.get_packet(data,cid))
104 server_connection* con=ss.get_connection(cid);
110 con->write(string().insert(0,100,'X'));
112 con->write(string().insert(0,100,'Y'));
116 // don't call atexit and stuff
123 // don't kill us on broken pipe
124 signal(SIGPIPE, SIG_IGN);
126 // wait till server is up
128 socket_client_connection sc("./socket");
134 while (!sc.get_packet(data))
135 sc.fill_buffer(1000000);
141 while (!sc.get_packet(data))
142 sc.fill_buffer(1000000);
144 CPPUNIT_ASSERT_EQUAL(string().insert(0,100,'X'),data);
149 void reconnect_with_close()
151 switch(child_pid=fork())
155 CPPUNIT_FAIL("fork error");
161 socket_server ss("./socket");
163 time_t t0 = time(NULL);
166 while (time(NULL) < t0 + 10 )
168 ss.fill_buffer(1000000);
173 if(ss.get_packet(data,cid))
175 server_connection* con=ss.get_connection(cid);
181 con->write(string().insert(0,100,'X'));
183 con->write(string().insert(0,100,'Y'));
187 // don't call atexit and stuff
194 // don't kill us on broken pipe
195 signal(SIGPIPE, SIG_IGN);
197 // wait till server is up
199 socket_client_connection sc("./socket");
205 while (!sc.get_packet(data))
206 sc.fill_buffer(1000000);
217 while (!sc.get_packet(data))
218 sc.fill_buffer(1000000);
220 CPPUNIT_ASSERT_EQUAL(string().insert(0,100,'X'),data);
225 void reconnect_buffer_complete()
227 switch(child_pid=fork())
231 CPPUNIT_FAIL("fork error");
237 socket_server ss("./socket");
239 time_t t0 = time(NULL);
242 while (time(NULL) < t0 + 10 )
244 ss.fill_buffer(1000000);
249 if(ss.get_packet(data,cid))
251 server_connection* con=ss.get_connection(cid);
257 con->write(string().insert(0,100,'X'));
261 // don't call atexit and stuff
268 // don't kill us on broken pipe
269 signal(SIGPIPE, SIG_IGN);
271 // wait till server is up
273 socket_client_connection sc("./socket");
279 while (!sc.packet_available())
280 sc.fill_buffer(1000000);
284 CPPUNIT_ASSERT_EQUAL_MESSAGE("packet not there",true,sc.get_packet(data));
286 CPPUNIT_ASSERT_EQUAL_MESSAGE("data incorrect",string().insert(0,100,'X'),data);
291 void reconnect_buffer_several_complete()
295 switch(child_pid=fork())
299 CPPUNIT_FAIL("fork error");
305 socket_server ss("./socket");
307 time_t t0 = time(NULL);
310 while (time(NULL) < t0 + 10 )
312 ss.fill_buffer(1000000);
317 if(ss.get_packet(data,cid))
319 server_connection* con=ss.get_connection(cid);
326 for (int i=0; i<packets; i++)
327 con->write(string().insert(0,100,'X'));
332 // don't call atexit and stuff
339 // don't kill us on broken pipe
340 signal(SIGPIPE, SIG_IGN);
342 // wait till server is up
344 socket_client_connection sc("./socket");
348 // retrieve packets for 3 seconds
349 time_t t0 = time(NULL);
352 while (time(NULL) < t0 + 3 )
353 sc.fill_buffer(1000000);
355 // we now should have packets complete packets in the buffer
359 // are these packets still there?
361 for (int i=0; i < packets; i++)
366 os << "packet " << i << " not there";
368 CPPUNIT_ASSERT_EQUAL_MESSAGE(os.str(),true,sc.get_packet(data));
371 os << "packet " << i << " incorrect";
373 CPPUNIT_ASSERT_EQUAL_MESSAGE(os.str(),string().insert(0,100,'X'),data);
379 void reconnect_buffer_no_incomplete1()
381 switch(child_pid=fork())
385 CPPUNIT_FAIL("fork error");
391 socket_server ss("./socket");
393 time_t t0 = time(NULL);
396 while (time(NULL) < t0 + 10 )
398 ss.fill_buffer(1000000);
403 if(ss.get_packet(data,cid))
405 server_connection* con=ss.get_connection(cid);
412 con->write(string().insert(0,100,'X'));
413 send_raw_socket("aaaab",&ss,cid);
418 // don't call atexit and stuff
425 // don't kill us on broken pipe
426 signal(SIGPIPE, SIG_IGN);
428 // wait till server is up
430 socket_client_connection sc("./socket");
434 // retrieve packets for 3 seconds
435 time_t t0 = time(NULL);
438 while (time(NULL) < t0 + 3 )
439 sc.fill_buffer(1000000);
441 // we now should have one complete packet and some stuff in the buffer
446 CPPUNIT_ASSERT_EQUAL_MESSAGE("no incomplete packet",true,(sc.peek_packet(data))>0);
450 CPPUNIT_ASSERT_EQUAL_MESSAGE("incomplete packet not removed",0,(int)sc.peek_packet(data));
455 void reconnect_buffer_no_incomplete2()
457 switch(child_pid=fork())
461 CPPUNIT_FAIL("fork error");
467 socket_server ss("./socket");
469 time_t t0 = time(NULL);
472 while (time(NULL) < t0 + 10 )
474 ss.fill_buffer(1000000);
479 if(ss.get_packet(data,cid))
481 server_connection* con=ss.get_connection(cid);
488 con->write(string().insert(0,100,'X'));
490 string blob=string().insert(0,100,'Y');
492 // one byte will be missing...
493 int size=blob.size()+1;
494 char sizetransfer[sizeof(int)+1];
495 memcpy(sizetransfer,(void*)&size,sizeof(int));
496 sizetransfer[sizeof(int)+1]=0;
498 string packet=string(sizetransfer)+blob;
500 send_raw_socket(packet,&ss,cid);
505 // don't call atexit and stuff
512 // don't kill us on broken pipe
513 signal(SIGPIPE, SIG_IGN);
515 // wait till server is up
517 socket_client_connection sc("./socket");
521 // retrieve packets for 3 seconds
522 time_t t0 = time(NULL);
525 while (time(NULL) < t0 + 3 )
526 sc.fill_buffer(1000000);
528 // we now should have one complete packet and some stuff in the buffer
534 CPPUNIT_ASSERT_EQUAL_MESSAGE("packet not there",true,sc.get_packet(data));
535 CPPUNIT_ASSERT_EQUAL_MESSAGE("data incorrect",string().insert(0,100,'X'),data);
537 CPPUNIT_ASSERT_EQUAL_MESSAGE("incomplete packet not removed",0,(int)sc.peek_packet(data));
544 CPPUNIT_TEST_SUITE_REGISTRATION(test_reconnect);