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(DisconnectOnRead);
142 CPPUNIT_TEST(BreakAccept);
144 CPPUNIT_TEST_SUITE_END();
148 typedef uint32_t packet_size_indicator;
156 void send_hello(string hello_string, socket_server* ss, unsigned int conn_id)
158 server_connection *sc=ss->get_connection(conn_id);
159 sc->write(hello_string);
162 void send_slow_raw_socket(string data, socket_server* ss, unsigned int conn_id)
164 socket_server_connection *ssc=dynamic_cast<socket_server_connection*>(ss->get_connection(conn_id));
166 // this is an evil hack to get access to real_write, don't ever do this in an app!!!
167 real_write_connection *rwc=(real_write_connection*)ssc;
169 // we write one char each 0.2 sec
170 for (int pos=0; pos < data.size(); pos++)
174 rwc->real_write(onebyte);
179 void ConnectTimeout()
187 CPPUNIT_FAIL("fork error");
193 socket_server ss("./socket");
195 // don't call atexit and stuff
204 // wait till server is up
209 socket_client_connection sc("./socket");
211 CPPUNIT_ASSERT_EQUAL(true,sc.connection::is_closed());
216 void HelloTimeoutNothing()
224 CPPUNIT_FAIL("fork error");
230 socket_server ss("./socket");
233 for (int i=0; i < 10; i++)
234 ss.fill_buffer(1000000);
235 // don't call atexit and stuff
244 // wait till server is up
246 socket_client_connection sc("./socket");
252 command_client cc(&sc,1000000,1000000);
254 catch(t2n_transfer_error &e)
255 { errormsg=e.what(); }
259 CPPUNIT_ASSERT_EQUAL(string("timeout exceeded"),errormsg);
266 void HelloTimeoutSlowData()
274 CPPUNIT_FAIL("fork error");
280 socket_server ss("./socket");
282 // create a valid packet
284 hello << "T2Nv" << PROTOCOL_VERSION << ';';
285 int byteordercheck=1;
286 hello.write((char*)&byteordercheck,sizeof(byteordercheck));
289 packet_size_indicator psize=htonl(hello.str().size());
290 std::string send_data(hello.str());
291 send_data.insert(0,(char*)&psize,sizeof(packet_size_indicator));
293 ss.add_callback(new_connection,bind(&test_timeout::send_slow_raw_socket, boost::ref(*this), send_data,&ss, _1));
296 for (int i=0; i < 10; i++)
297 ss.fill_buffer(1000000);
298 // don't call atexit and stuff
307 // wait till server is up
309 socket_client_connection sc("./socket");
315 command_client cc(&sc,1000000,1000000);
317 catch(t2n_transfer_error &e)
318 { errormsg=e.what(); }
322 CPPUNIT_ASSERT_EQUAL(string("timeout exceeded"),errormsg);
329 void CommandTimeout()
337 CPPUNIT_FAIL("fork error");
343 socket_server ss("./socket");
346 hello << "T2Nv" << PROTOCOL_VERSION << ';';
347 int byteordercheck=1;
348 hello.write((char*)&byteordercheck,sizeof(byteordercheck));
351 ss.add_callback(new_connection,bind(&test_timeout::send_hello, boost::ref(*this), hello.str(),&ss, _1));
354 for (int i=0; i < 10; i++)
355 ss.fill_buffer(1000000);
356 // don't call atexit and stuff
365 // wait till server is up
367 socket_client_connection sc("./socket");
369 command_client cc(&sc,1000000,1000000);
376 cc.send_command(new testfunc2_cmd("hello"),rc);
378 catch(t2n_transfer_error &e)
379 { errormsg=e.what(); }
383 CPPUNIT_ASSERT_EQUAL(string("timeout exceeded"),errormsg);
390 void CommandSlowResponse()
398 CPPUNIT_FAIL("fork error");
404 socket_server ss("./socket");
407 hello << "T2Nv" << PROTOCOL_VERSION << ';';
408 int byteordercheck=1;
409 hello.write((char*)&byteordercheck,sizeof(byteordercheck));
412 ss.add_callback(new_connection,bind(&test_timeout::send_hello, boost::ref(*this), hello.str(),&ss, _1));
415 for (int i=0; i < 10; i++)
417 ss.fill_buffer(1000000);
422 if(ss.get_packet(data,cid))
424 // create a valid packet & send
425 string response="abcdefghijklmnopqrstuvwxyz";
426 packet_size_indicator psize=htonl(response.size());
427 std::string send_data(response);
428 send_data.insert(0,(char*)&psize,sizeof(packet_size_indicator));
429 send_slow_raw_socket(send_data,&ss,cid);
432 // don't call atexit and stuff
441 // wait till server is up
443 socket_client_connection sc("./socket");
445 command_client cc(&sc,1000000,1000000);
452 cc.send_command(new testfunc2_cmd("hello"),rc);
454 catch(t2n_transfer_error &e)
455 { errormsg=e.what(); }
459 CPPUNIT_ASSERT_EQUAL(string("timeout exceeded"),errormsg);
466 void DisconnectOnWrite()
474 CPPUNIT_FAIL("fork error");
480 socket_server ss("./socket");
482 // bail out as soon as we get something
484 // don't call atexit and stuff
493 // don't kill us on broken pipe
494 signal(SIGPIPE, SIG_IGN);
496 // wait till server is up
498 socket_client_connection sc("./socket");
502 string huge(1000000,'x');
508 catch(t2n_transfer_error &e)
509 { errormsg=e.what(); }
513 CPPUNIT_ASSERT_EQUAL(string("write() returned Broken pipe"),errormsg);
520 void DisconnectOnRead()
528 CPPUNIT_FAIL("fork error");
534 // wait till server is up
537 socket_client_connection sc("./socket");
539 // this is an evil hack to get access to real_write, don't ever do this in an app!!!
540 real_write_client_connection *rwc=(real_write_client_connection*)≻
541 rwc->real_write(string(10000,'x'));
543 // don't call atexit and stuff
550 // don't kill us on broken pipe
551 signal(SIGPIPE, SIG_IGN);
553 socket_server ss("./socket");
555 time_t t0 = time(NULL);
558 while (time(NULL) < t0 + 5 )
560 ss.fill_buffer(1000000);
566 // are we still alive and able to process data?
572 CPPUNIT_FAIL("fork error");
578 socket_client_connection *sc=new socket_client_connection("./socket");
579 sc->write(string(10000,'x'));
581 // socket is closed regularly
583 // don't run regular cleanup, otherwise cppunit stuff gets called
595 while (time(NULL) < t0 + 10 )
597 ss.fill_buffer(1000000);
599 if (ss.get_packet(received))
603 CPPUNIT_ASSERT_EQUAL(string(10000,'x'),received);
620 CPPUNIT_FAIL("fork error");
626 // wait till server is really up and waiting
629 // connect with very tight timeout and only 1 retry
630 socket_client_connection sc("./socket",50,1);
632 // don't call atexit and stuff
639 // don't kill us on broken pipe
640 signal(SIGPIPE, SIG_IGN);
642 socket_server ss("./socket");
644 // server is "working" while client wants to connect
647 time_t t0 = time(NULL);
650 while (time(NULL) < t0 + 5 )
652 ss.fill_buffer(1000000);
658 // are we still alive and able to process data?
664 CPPUNIT_FAIL("fork error");
670 socket_client_connection *sc=new socket_client_connection("./socket");
671 sc->write(string(10000,'x'));
673 // socket is closed regularly
675 // don't run regular cleanup, otherwise cppunit stuff gets called
687 while (time(NULL) < t0 + 10 )
689 ss.fill_buffer(1000000);
691 if (ss.get_packet(received))
695 CPPUNIT_ASSERT_EQUAL(string(10000,'x'),received);
705 CPPUNIT_TEST_SUITE_REGISTRATION(test_timeout);