1 /***************************************************************************
2 * Copyright (C) 2004 by Intra2net AG *
5 ***************************************************************************/
18 #include <boost/bind.hpp>
20 #include <cppunit/extensions/TestFactoryRegistry.h>
21 #include <cppunit/ui/text/TestRunner.h>
22 #include <cppunit/extensions/HelperMacros.h>
24 #include <boost/archive/binary_oarchive.hpp>
25 #include <boost/archive/binary_iarchive.hpp>
26 #include <boost/archive/xml_oarchive.hpp>
27 #include <boost/archive/xml_iarchive.hpp>
28 #include <boost/serialization/serialization.hpp>
30 #include <container.hxx>
31 #include <socket_client.hxx>
32 #include <socket_server.hxx>
33 #include <command_client.hxx>
34 #include <command_server.hxx>
41 using namespace libt2n;
42 using namespace CppUnit;
44 string testfunc2(const string& str)
47 throw libt2n::t2n_runtime_error("throw me around");
49 ret+=", testfunc() was here";
53 class testfunc2_res : public libt2n::result
58 friend class boost::serialization::access;
59 template<class Archive>
60 void serialize(Archive & ar, const unsigned int version)
62 ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(libt2n::result);
63 ar & BOOST_SERIALIZATION_NVP(res);
70 testfunc2_res(const string& str)
82 class testfunc2_cmd : public libt2n::command
87 friend class boost::serialization::access;
88 template<class Archive>
89 void serialize(Archive & ar, const unsigned int version)
91 ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(libt2n::command);
92 ar & BOOST_SERIALIZATION_NVP(param);
99 testfunc2_cmd(const string& str)
104 libt2n::result* operator()()
106 return new testfunc2_res(testfunc2(param));
110 #include <boost/serialization/export.hpp>
112 BOOST_CLASS_EXPORT(testfunc2_cmd)
113 BOOST_CLASS_EXPORT(testfunc2_res)
115 // this is an evil hack to get access to real_write, don't ever do this in an app!!!
116 class real_write_connection: public socket_server_connection
119 void real_write(const std::string& data)
120 { socket_write(data); }
123 // this is an evil hack to get access to real_write, don't ever do this in an app!!!
124 class real_write_client_connection: public socket_client_connection
127 void real_write(const std::string& data)
128 { socket_write(data); }
131 class test_timeout : public TestFixture
133 CPPUNIT_TEST_SUITE(test_timeout);
135 CPPUNIT_TEST(ConnectTimeout);
136 CPPUNIT_TEST(HelloTimeoutNothing);
137 CPPUNIT_TEST(HelloTimeoutSlowData);
138 CPPUNIT_TEST(CommandTimeout);
139 CPPUNIT_TEST(CommandSlowResponse);
140 CPPUNIT_TEST(DisconnectOnWrite);
141 CPPUNIT_TEST(WriteTwice);
142 CPPUNIT_TEST(DisconnectOnRead);
143 CPPUNIT_TEST(BreakAccept);
145 CPPUNIT_TEST_SUITE_END();
151 typedef uint32_t packet_size_indicator;
158 // make sure the server-child is dead before the next test runs
159 kill(child_pid,SIGKILL);
163 void send_hello(string hello_string, socket_server* ss, unsigned int conn_id)
165 server_connection *sc=ss->get_connection(conn_id);
166 sc->write(hello_string);
169 void send_slow_raw_socket(string data, socket_server* ss, unsigned int conn_id)
171 socket_server_connection *ssc=dynamic_cast<socket_server_connection*>(ss->get_connection(conn_id));
173 // this is an evil hack to get access to real_write, don't ever do this in an app!!!
174 real_write_connection *rwc=(real_write_connection*)ssc;
176 // we write one char each 0.2 sec
177 for (int pos=0; pos < data.size(); pos++)
181 rwc->real_write(onebyte);
186 void ConnectTimeout()
188 switch(child_pid=fork())
192 CPPUNIT_FAIL("fork error");
198 socket_server ss("./socket");
200 // don't call atexit and stuff
209 // wait till server is up
214 socket_client_connection sc("./socket");
216 CPPUNIT_ASSERT_EQUAL_MESSAGE("connection not closed",true,sc.connection::is_closed());
218 CPPUNIT_ASSERT_EQUAL_MESSAGE("wrong errormessage",string("no more retries left after connect error"),sc.get_last_error_msg());
223 void HelloTimeoutNothing()
225 switch(child_pid=fork())
229 CPPUNIT_FAIL("fork error");
235 socket_server ss("./socket");
238 for (int i=0; i < 10; i++)
239 ss.fill_buffer(1000000);
240 // don't call atexit and stuff
249 // wait till server is up
251 socket_client_connection sc("./socket");
252 command_client cc(&sc,1000000,1000000);
254 t2n_exception* ep=cc.get_constuctor_exception();
260 CPPUNIT_ASSERT_EQUAL(string("timeout exceeded"),errormsg);
265 void HelloTimeoutSlowData()
267 switch(child_pid=fork())
271 CPPUNIT_FAIL("fork error");
277 socket_server ss("./socket");
279 // create a valid packet
281 hello << "T2Nv" << PROTOCOL_VERSION << ';';
282 int byteordercheck=1;
283 hello.write((char*)&byteordercheck,sizeof(byteordercheck));
286 packet_size_indicator psize=htonl(hello.str().size());
287 std::string send_data(hello.str());
288 send_data.insert(0,(char*)&psize,sizeof(packet_size_indicator));
290 ss.add_callback(new_connection,bind(&test_timeout::send_slow_raw_socket, boost::ref(*this), send_data,&ss, _1));
293 for (int i=0; i < 10; i++)
294 ss.fill_buffer(1000000);
295 // don't call atexit and stuff
304 // wait till server is up
306 socket_client_connection sc("./socket");
307 command_client cc(&sc,1000000,1000000);
309 t2n_exception* ep=cc.get_constuctor_exception();
315 CPPUNIT_ASSERT_EQUAL(string("timeout exceeded"),errormsg);
320 void CommandTimeout()
322 switch(child_pid=fork())
326 CPPUNIT_FAIL("fork error");
332 socket_server ss("./socket");
335 hello << "T2Nv" << PROTOCOL_VERSION << ';';
336 int byteordercheck=1;
337 hello.write((char*)&byteordercheck,sizeof(byteordercheck));
340 ss.add_callback(new_connection,bind(&test_timeout::send_hello, boost::ref(*this), hello.str(),&ss, _1));
343 for (int i=0; i < 10; i++)
344 ss.fill_buffer(1000000);
345 // don't call atexit and stuff
354 // wait till server is up
356 socket_client_connection sc("./socket");
358 command_client cc(&sc,1000000,1000000);
365 cc.send_command(new testfunc2_cmd("hello"),rc);
367 catch(t2n_transfer_error &e)
368 { errormsg=e.what(); }
372 CPPUNIT_ASSERT_EQUAL(string("timeout exceeded"),errormsg);
377 void CommandSlowResponse()
379 switch(child_pid=fork())
383 CPPUNIT_FAIL("fork error");
389 socket_server ss("./socket");
392 hello << "T2Nv" << PROTOCOL_VERSION << ';';
393 int byteordercheck=1;
394 hello.write((char*)&byteordercheck,sizeof(byteordercheck));
397 ss.add_callback(new_connection,bind(&test_timeout::send_hello, boost::ref(*this), hello.str(),&ss, _1));
400 for (int i=0; i < 10; i++)
402 ss.fill_buffer(1000000);
407 if(ss.get_packet(data,cid))
409 // create a valid packet & send
410 string response="abcdefghijklmnopqrstuvwxyz";
411 packet_size_indicator psize=htonl(response.size());
412 std::string send_data(response);
413 send_data.insert(0,(char*)&psize,sizeof(packet_size_indicator));
414 send_slow_raw_socket(send_data,&ss,cid);
417 // don't call atexit and stuff
426 // wait till server is up
428 socket_client_connection sc("./socket");
430 command_client cc(&sc,1000000,1000000);
437 cc.send_command(new testfunc2_cmd("hello"),rc);
439 catch(t2n_transfer_error &e)
440 { errormsg=e.what(); }
444 CPPUNIT_ASSERT_EQUAL(string("timeout exceeded"),errormsg);
449 void DisconnectOnWrite()
451 switch(child_pid=fork())
455 CPPUNIT_FAIL("fork error");
461 socket_server ss("./socket");
463 // bail out as soon as we get something
465 // don't call atexit and stuff
474 // don't kill us on broken pipe
475 signal(SIGPIPE, SIG_IGN);
477 // wait till server is up
479 socket_client_connection sc("./socket");
483 string huge(5000000,'x');
489 catch(t2n_transfer_error &e)
490 { errormsg=e.what(); }
494 CPPUNIT_ASSERT_EQUAL(string("write() returned Broken pipe"),errormsg);
501 switch(child_pid=fork())
505 CPPUNIT_FAIL("fork error");
511 socket_server ss("./socket");
513 // bail out as soon as we get something
515 // don't call atexit and stuff
524 // don't kill us on broken pipe
525 signal(SIGPIPE, SIG_IGN);
527 // wait till server is up
529 socket_client_connection sc("./socket");
533 sc.write("somedata");
537 // server should disconnect now
540 sc.write("other data");
542 catch(t2n_transfer_error &e)
543 { errormsg=e.what(); }
547 CPPUNIT_ASSERT_EQUAL(string("write() returned Broken pipe"),errormsg);
552 void DisconnectOnRead()
556 switch(child_pid=fork())
560 CPPUNIT_FAIL("fork error");
566 // wait till server is up
569 socket_client_connection sc("./socket");
571 // this is an evil hack to get access to real_write, don't ever do this in an app!!!
572 real_write_client_connection *rwc=(real_write_client_connection*)≻
573 rwc->real_write(string(10000,'x'));
575 // don't call atexit and stuff
582 // don't kill us on broken pipe
583 signal(SIGPIPE, SIG_IGN);
585 socket_server ss("./socket");
587 time_t t0 = time(NULL);
590 while (time(NULL) < t0 + 5 )
592 ss.fill_buffer(1000000);
598 // are we still alive and able to process data?
604 CPPUNIT_FAIL("fork error");
610 socket_client_connection *sc=new socket_client_connection("./socket");
611 sc->write(string(10000,'x'));
613 // socket is closed regularly
615 // don't run regular cleanup, otherwise cppunit stuff gets called
627 while (time(NULL) < t0 + 10 )
629 ss.fill_buffer(1000000);
631 if (ss.get_packet(received))
635 CPPUNIT_ASSERT_EQUAL(string(10000,'x'),received);
647 switch(child_pid=fork())
651 CPPUNIT_FAIL("fork error");
657 // wait till server is really up and waiting
660 // connect with very tight timeout and only 1 retry
661 socket_client_connection sc("./socket",50,1);
663 // don't call atexit and stuff
670 // don't kill us on broken pipe
671 signal(SIGPIPE, SIG_IGN);
673 socket_server ss("./socket");
675 // server is "working" while client wants to connect
678 time_t t0 = time(NULL);
681 while (time(NULL) < t0 + 5 )
683 ss.fill_buffer(1000000);
689 // are we still alive and able to process data?
695 CPPUNIT_FAIL("fork error");
701 socket_client_connection *sc=new socket_client_connection("./socket");
702 sc->write(string(10000,'x'));
704 // socket is closed regularly
706 // don't run regular cleanup, otherwise cppunit stuff gets called
718 while (time(NULL) < t0 + 10 )
720 ss.fill_buffer(1000000);
722 if (ss.get_packet(received))
726 CPPUNIT_ASSERT_EQUAL(string(10000,'x'),received);
735 CPPUNIT_TEST_SUITE_REGISTRATION(test_timeout);