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
516 // don't call atexit and stuff
525 // don't kill us on broken pipe
526 signal(SIGPIPE, SIG_IGN);
528 // wait till server is up
530 socket_client_connection sc("./socket");
534 sc.write("somedata");
538 // server should disconnect now
541 sc.write("other data");
543 catch(t2n_transfer_error &e)
544 { errormsg=e.what(); }
548 CPPUNIT_ASSERT_EQUAL(string("write() returned Broken pipe"),errormsg);
553 void DisconnectOnRead()
557 switch(child_pid=fork())
561 CPPUNIT_FAIL("fork error");
567 // wait till server is up
570 socket_client_connection sc("./socket");
572 // this is an evil hack to get access to real_write, don't ever do this in an app!!!
573 real_write_client_connection *rwc=(real_write_client_connection*)≻
574 rwc->real_write(string(10000,'x'));
576 // don't call atexit and stuff
583 // don't kill us on broken pipe
584 signal(SIGPIPE, SIG_IGN);
586 socket_server ss("./socket");
588 time_t t0 = time(NULL);
591 while (time(NULL) < t0 + 5 )
593 ss.fill_buffer(1000000);
599 // are we still alive and able to process data?
605 CPPUNIT_FAIL("fork error");
611 socket_client_connection *sc=new socket_client_connection("./socket");
612 sc->write(string(10000,'x'));
614 // socket is closed regularly
616 // don't run regular cleanup, otherwise cppunit stuff gets called
628 while (time(NULL) < t0 + 10 )
630 ss.fill_buffer(1000000);
632 if (ss.get_packet(received))
636 CPPUNIT_ASSERT_EQUAL(string(10000,'x'),received);
648 switch(child_pid=fork())
652 CPPUNIT_FAIL("fork error");
658 // wait till server is really up and waiting
661 // connect with very tight timeout and only 1 retry
662 socket_client_connection sc("./socket",50,1);
664 // don't call atexit and stuff
671 // don't kill us on broken pipe
672 signal(SIGPIPE, SIG_IGN);
674 socket_server ss("./socket");
676 // server is "working" while client wants to connect
679 time_t t0 = time(NULL);
682 while (time(NULL) < t0 + 5 )
684 ss.fill_buffer(1000000);
690 // are we still alive and able to process data?
696 CPPUNIT_FAIL("fork error");
702 socket_client_connection *sc=new socket_client_connection("./socket");
703 sc->write(string(10000,'x'));
705 // socket is closed regularly
707 // don't run regular cleanup, otherwise cppunit stuff gets called
719 while (time(NULL) < t0 + 10 )
721 ss.fill_buffer(1000000);
723 if (ss.get_packet(received))
727 CPPUNIT_ASSERT_EQUAL(string(10000,'x'),received);
736 CPPUNIT_TEST_SUITE_REGISTRATION(test_timeout);