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();
63 void send_raw_socket(string hello_string, socket_server* ss, int conn_id)
65 socket_server_connection *ssc=dynamic_cast<socket_server_connection*>(ss->get_connection(conn_id));
67 // this is an evil hack to get access to real_write, don't ever do this in an app!!!
68 real_write_connection *rwc=(real_write_connection*)ssc;
69 rwc->real_write(hello_string);
72 void simple_reconnect()
80 CPPUNIT_FAIL("fork error");
86 socket_server ss("./socket");
88 time_t t0 = time(NULL);
91 while (time(NULL) < t0 + 10 )
93 ss.fill_buffer(1000000);
98 if(ss.get_packet(data,cid))
100 server_connection* con=ss.get_connection(cid);
106 con->write(string().insert(0,100,'X'));
108 con->write(string().insert(0,100,'Y'));
112 // don't call atexit and stuff
119 // don't kill us on broken pipe
120 signal(SIGPIPE, SIG_IGN);
122 // wait till server is up
124 socket_client_connection sc("./socket");
130 while (!sc.get_packet(data))
131 sc.fill_buffer(1000000);
137 while (!sc.get_packet(data))
138 sc.fill_buffer(1000000);
140 CPPUNIT_ASSERT_EQUAL(string().insert(0,100,'X'),data);
145 void reconnect_with_close()
153 CPPUNIT_FAIL("fork error");
159 socket_server ss("./socket");
161 time_t t0 = time(NULL);
164 while (time(NULL) < t0 + 10 )
166 ss.fill_buffer(1000000);
171 if(ss.get_packet(data,cid))
173 server_connection* con=ss.get_connection(cid);
179 con->write(string().insert(0,100,'X'));
181 con->write(string().insert(0,100,'Y'));
185 // don't call atexit and stuff
192 // don't kill us on broken pipe
193 signal(SIGPIPE, SIG_IGN);
195 // wait till server is up
197 socket_client_connection sc("./socket");
203 while (!sc.get_packet(data))
204 sc.fill_buffer(1000000);
215 while (!sc.get_packet(data))
216 sc.fill_buffer(1000000);
218 CPPUNIT_ASSERT_EQUAL(string().insert(0,100,'X'),data);
223 void reconnect_buffer_complete()
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()
301 CPPUNIT_FAIL("fork error");
307 socket_server ss("./socket");
309 time_t t0 = time(NULL);
312 while (time(NULL) < t0 + 10 )
314 ss.fill_buffer(1000000);
319 if(ss.get_packet(data,cid))
321 server_connection* con=ss.get_connection(cid);
328 for (int i=0; i<packets; i++)
329 con->write(string().insert(0,100,'X'));
334 // don't call atexit and stuff
341 // don't kill us on broken pipe
342 signal(SIGPIPE, SIG_IGN);
344 // wait till server is up
346 socket_client_connection sc("./socket");
350 // retrieve packets for 3 seconds
351 time_t t0 = time(NULL);
354 while (time(NULL) < t0 + 3 )
355 sc.fill_buffer(1000000);
357 // we now should have packets complete packets in the buffer
361 // are these packets still there?
363 for (int i=0; i < packets; i++)
368 os << "packet " << i << " not there";
370 CPPUNIT_ASSERT_EQUAL_MESSAGE(os.str(),true,sc.get_packet(data));
373 os << "packet " << i << " incorrect";
375 CPPUNIT_ASSERT_EQUAL_MESSAGE(os.str(),string().insert(0,100,'X'),data);
381 void reconnect_buffer_no_incomplete1()
389 CPPUNIT_FAIL("fork error");
395 socket_server ss("./socket");
397 time_t t0 = time(NULL);
400 while (time(NULL) < t0 + 10 )
402 ss.fill_buffer(1000000);
407 if(ss.get_packet(data,cid))
409 server_connection* con=ss.get_connection(cid);
416 con->write(string().insert(0,100,'X'));
417 send_raw_socket("aaaab",&ss,cid);
422 // don't call atexit and stuff
429 // don't kill us on broken pipe
430 signal(SIGPIPE, SIG_IGN);
432 // wait till server is up
434 socket_client_connection sc("./socket");
438 // retrieve packets for 3 seconds
439 time_t t0 = time(NULL);
442 while (time(NULL) < t0 + 3 )
443 sc.fill_buffer(1000000);
445 // we now should have one complete packet and some stuff in the buffer
450 CPPUNIT_ASSERT_EQUAL_MESSAGE("no incomplete packet",true,(sc.peek_packet(data))>0);
454 CPPUNIT_ASSERT_EQUAL_MESSAGE("incomplete packet not removed",0,(int)sc.peek_packet(data));
459 void reconnect_buffer_no_incomplete2()
467 CPPUNIT_FAIL("fork error");
473 socket_server ss("./socket");
475 time_t t0 = time(NULL);
478 while (time(NULL) < t0 + 10 )
480 ss.fill_buffer(1000000);
485 if(ss.get_packet(data,cid))
487 server_connection* con=ss.get_connection(cid);
494 con->write(string().insert(0,100,'X'));
496 string blob=string().insert(0,100,'Y');
498 // one byte will be missing...
499 int size=blob.size()+1;
500 char sizetransfer[sizeof(int)+1];
501 memcpy(sizetransfer,(void*)&size,sizeof(int));
502 sizetransfer[sizeof(int)+1]=0;
504 string packet=string(sizetransfer)+blob;
506 send_raw_socket(packet,&ss,cid);
511 // don't call atexit and stuff
518 // don't kill us on broken pipe
519 signal(SIGPIPE, SIG_IGN);
521 // wait till server is up
523 socket_client_connection sc("./socket");
527 // retrieve packets for 3 seconds
528 time_t t0 = time(NULL);
531 while (time(NULL) < t0 + 3 )
532 sc.fill_buffer(1000000);
534 // we now should have one complete packet and some stuff in the buffer
540 CPPUNIT_ASSERT_EQUAL_MESSAGE("packet not there",true,sc.get_packet(data));
541 CPPUNIT_ASSERT_EQUAL_MESSAGE("data incorrect",string().insert(0,100,'X'),data);
543 CPPUNIT_ASSERT_EQUAL_MESSAGE("incomplete packet not removed",0,(int)sc.peek_packet(data));
550 CPPUNIT_TEST_SUITE_REGISTRATION(test_reconnect);