1 /***************************************************************************
2 * Copyright (C) 2004 by Intra2net AG *
5 ***************************************************************************/
18 #include <cppunit/extensions/TestFactoryRegistry.h>
19 #include <cppunit/ui/text/TestRunner.h>
20 #include <cppunit/extensions/HelperMacros.h>
22 #include <socket_client.hxx>
23 #include <socket_server.hxx>
30 using namespace libt2n;
31 using namespace CppUnit;
33 // this is an evil hack to get access to real_write, don't ever do this in an app!!!
34 class real_write_connection: public socket_server_connection
37 void real_write(const std::string& data)
38 { socket_write(data); }
41 class test_reconnect : public TestFixture
43 CPPUNIT_TEST_SUITE(test_reconnect);
45 CPPUNIT_TEST(simple_reconnect);
46 CPPUNIT_TEST(reconnect_with_close);
47 CPPUNIT_TEST(reconnect_buffer_complete);
48 CPPUNIT_TEST(reconnect_buffer_several_complete);
49 CPPUNIT_TEST(reconnect_buffer_no_incomplete1);
50 CPPUNIT_TEST(reconnect_buffer_no_incomplete2);
52 CPPUNIT_TEST_SUITE_END();
62 void send_raw_socket(string hello_string, socket_server* ss, int conn_id)
64 socket_server_connection *ssc=dynamic_cast<socket_server_connection*>(ss->get_connection(conn_id));
66 // this is an evil hack to get access to real_write, don't ever do this in an app!!!
67 real_write_connection *rwc=(real_write_connection*)ssc;
68 rwc->real_write(hello_string);
71 void simple_reconnect()
79 CPPUNIT_FAIL("fork error");
85 socket_server ss("./socket");
87 time_t t0 = time(NULL);
90 while (time(NULL) < t0 + 10 )
92 ss.fill_buffer(1000000);
97 if(ss.get_packet(data,cid))
99 server_connection* con=ss.get_connection(cid);
105 con->write(string().insert(0,100,'X'));
107 con->write(string().insert(0,100,'Y'));
111 // don't call atexit and stuff
118 // don't kill us on broken pipe
119 signal(SIGPIPE, SIG_IGN);
121 // wait till server is up
123 socket_client_connection sc("./socket");
129 while (!sc.get_packet(data))
130 sc.fill_buffer(1000000);
136 while (!sc.get_packet(data))
137 sc.fill_buffer(1000000);
139 CPPUNIT_ASSERT_EQUAL(string().insert(0,100,'X'),data);
144 void reconnect_with_close()
152 CPPUNIT_FAIL("fork error");
158 socket_server ss("./socket");
160 time_t t0 = time(NULL);
163 while (time(NULL) < t0 + 10 )
165 ss.fill_buffer(1000000);
170 if(ss.get_packet(data,cid))
172 server_connection* con=ss.get_connection(cid);
178 con->write(string().insert(0,100,'X'));
180 con->write(string().insert(0,100,'Y'));
184 // don't call atexit and stuff
191 // don't kill us on broken pipe
192 signal(SIGPIPE, SIG_IGN);
194 // wait till server is up
196 socket_client_connection sc("./socket");
202 while (!sc.get_packet(data))
203 sc.fill_buffer(1000000);
214 while (!sc.get_packet(data))
215 sc.fill_buffer(1000000);
217 CPPUNIT_ASSERT_EQUAL(string().insert(0,100,'X'),data);
222 void reconnect_buffer_complete()
230 CPPUNIT_FAIL("fork error");
236 socket_server ss("./socket");
238 time_t t0 = time(NULL);
241 while (time(NULL) < t0 + 10 )
243 ss.fill_buffer(1000000);
248 if(ss.get_packet(data,cid))
250 server_connection* con=ss.get_connection(cid);
256 con->write(string().insert(0,100,'X'));
260 // don't call atexit and stuff
267 // don't kill us on broken pipe
268 signal(SIGPIPE, SIG_IGN);
270 // wait till server is up
272 socket_client_connection sc("./socket");
278 while (!sc.packet_available())
279 sc.fill_buffer(1000000);
283 CPPUNIT_ASSERT_EQUAL_MESSAGE("packet not there",true,sc.get_packet(data));
285 CPPUNIT_ASSERT_EQUAL_MESSAGE("data incorrect",string().insert(0,100,'X'),data);
290 void reconnect_buffer_several_complete()
300 CPPUNIT_FAIL("fork error");
306 socket_server ss("./socket");
308 time_t t0 = time(NULL);
311 while (time(NULL) < t0 + 10 )
313 ss.fill_buffer(1000000);
318 if(ss.get_packet(data,cid))
320 server_connection* con=ss.get_connection(cid);
327 for (int i=0; i<packets; i++)
328 con->write(string().insert(0,100,'X'));
333 // don't call atexit and stuff
340 // don't kill us on broken pipe
341 signal(SIGPIPE, SIG_IGN);
343 // wait till server is up
345 socket_client_connection sc("./socket");
349 // retrieve packets for 3 seconds
350 time_t t0 = time(NULL);
353 while (time(NULL) < t0 + 3 )
354 sc.fill_buffer(1000000);
356 // we now should have packets complete packets in the buffer
360 // are these packets still there?
362 for (int i=0; i < packets; i++)
367 os << "packet " << i << " not there";
369 CPPUNIT_ASSERT_EQUAL_MESSAGE(os.str(),true,sc.get_packet(data));
372 os << "packet " << i << " incorrect";
374 CPPUNIT_ASSERT_EQUAL_MESSAGE(os.str(),string().insert(0,100,'X'),data);
380 void reconnect_buffer_no_incomplete1()
388 CPPUNIT_FAIL("fork error");
394 socket_server ss("./socket");
396 time_t t0 = time(NULL);
399 while (time(NULL) < t0 + 10 )
401 ss.fill_buffer(1000000);
406 if(ss.get_packet(data,cid))
408 server_connection* con=ss.get_connection(cid);
415 con->write(string().insert(0,100,'X'));
416 send_raw_socket("aaaab",&ss,cid);
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 CPPUNIT_ASSERT_EQUAL_MESSAGE("no incomplete packet",true,(sc.peek_packet(data))>0);
453 CPPUNIT_ASSERT_EQUAL_MESSAGE("incomplete packet not removed",0,(int)sc.peek_packet(data));
458 void reconnect_buffer_no_incomplete2()
466 CPPUNIT_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);
510 // don't call atexit and stuff
517 // don't kill us on broken pipe
518 signal(SIGPIPE, SIG_IGN);
520 // wait till server is up
522 socket_client_connection sc("./socket");
526 // retrieve packets for 3 seconds
527 time_t t0 = time(NULL);
530 while (time(NULL) < t0 + 3 )
531 sc.fill_buffer(1000000);
533 // we now should have one complete packet and some stuff in the buffer
539 CPPUNIT_ASSERT_EQUAL_MESSAGE("packet not there",true,sc.get_packet(data));
540 CPPUNIT_ASSERT_EQUAL_MESSAGE("data incorrect",string().insert(0,100,'X'),data);
542 CPPUNIT_ASSERT_EQUAL_MESSAGE("incomplete packet not removed",0,(int)sc.peek_packet(data));
549 CPPUNIT_TEST_SUITE_REGISTRATION(test_reconnect);