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 class test_timeout : public TestFixture
125 CPPUNIT_TEST_SUITE(test_timeout);
127 CPPUNIT_TEST(HelloTimeoutNothing);
128 CPPUNIT_TEST(HelloTimeoutSlowData);
129 CPPUNIT_TEST(CommandTimeout);
130 CPPUNIT_TEST(CommandSlowResponse);
132 CPPUNIT_TEST_SUITE_END();
136 typedef uint32_t packet_size_indicator;
144 void send_hello(string hello_string, socket_server* ss, unsigned int conn_id)
146 server_connection *sc=ss->get_connection(conn_id);
147 sc->write(hello_string);
150 void send_slow_raw_socket(string data, socket_server* ss, unsigned int conn_id)
152 socket_server_connection *ssc=dynamic_cast<socket_server_connection*>(ss->get_connection(conn_id));
154 // this is an evil hack to get access to real_write, don't ever do this in an app!!!
155 real_write_connection *rwc=(real_write_connection*)ssc;
157 // we write one char each 0.2 sec
158 for (int pos=0; pos < data.size(); pos++)
162 rwc->real_write(onebyte);
167 void HelloTimeoutNothing()
175 CPPUNIT_FAIL("fork error");
181 socket_server ss("./socket");
184 for (int i=0; i < 10; i++)
185 ss.fill_buffer(1000000);
186 // don't call atexit and stuff
195 // wait till server is up
197 socket_client_connection sc("./socket");
203 command_client cc(sc,1000000,1000000);
205 catch(t2n_transfer_error &e)
206 { errormsg=e.what(); }
210 CPPUNIT_ASSERT_EQUAL(string("timeout exceeded"),errormsg);
215 void HelloTimeoutSlowData()
223 CPPUNIT_FAIL("fork error");
229 socket_server ss("./socket");
231 // create a valid packet
233 hello << "T2Nv" << PROTOCOL_VERSION << ';';
234 int byteordercheck=1;
235 hello.write((char*)&byteordercheck,sizeof(byteordercheck));
238 packet_size_indicator psize=htonl(hello.str().size());
239 std::string send_data(hello.str());
240 send_data.insert(0,(char*)&psize,sizeof(packet_size_indicator));
242 ss.add_callback(new_connection,bind(&test_timeout::send_slow_raw_socket, boost::ref(*this), send_data,&ss, _1));
245 for (int i=0; i < 10; i++)
246 ss.fill_buffer(1000000);
247 // don't call atexit and stuff
256 // wait till server is up
258 socket_client_connection sc("./socket");
264 command_client cc(sc,1000000,1000000);
266 catch(t2n_transfer_error &e)
267 { errormsg=e.what(); }
271 CPPUNIT_ASSERT_EQUAL(string("timeout exceeded"),errormsg);
276 void CommandTimeout()
284 CPPUNIT_FAIL("fork error");
290 socket_server ss("./socket");
293 hello << "T2Nv" << PROTOCOL_VERSION << ';';
294 int byteordercheck=1;
295 hello.write((char*)&byteordercheck,sizeof(byteordercheck));
298 ss.add_callback(new_connection,bind(&test_timeout::send_hello, boost::ref(*this), hello.str(),&ss, _1));
301 for (int i=0; i < 10; i++)
302 ss.fill_buffer(1000000);
303 // don't call atexit and stuff
312 // wait till server is up
314 socket_client_connection sc("./socket");
316 command_client cc(sc,1000000,1000000);
323 cc.send_command(new testfunc2_cmd("hello"),rc);
325 catch(t2n_transfer_error &e)
326 { errormsg=e.what(); }
330 CPPUNIT_ASSERT_EQUAL(string("timeout exceeded"),errormsg);
335 void CommandSlowResponse()
343 CPPUNIT_FAIL("fork error");
349 socket_server ss("./socket");
352 hello << "T2Nv" << PROTOCOL_VERSION << ';';
353 int byteordercheck=1;
354 hello.write((char*)&byteordercheck,sizeof(byteordercheck));
357 ss.add_callback(new_connection,bind(&test_timeout::send_hello, boost::ref(*this), hello.str(),&ss, _1));
360 for (int i=0; i < 10; i++)
362 ss.fill_buffer(1000000);
367 if(ss.get_packet(data,cid))
369 // create a valid packet & send
370 string response="abcdefghijklmnopqrstuvwxyz";
371 packet_size_indicator psize=htonl(response.size());
372 std::string send_data(response);
373 send_data.insert(0,(char*)&psize,sizeof(packet_size_indicator));
374 send_slow_raw_socket(send_data,&ss,cid);
377 // don't call atexit and stuff
386 // wait till server is up
388 socket_client_connection sc("./socket");
390 command_client cc(sc,1000000,1000000);
397 cc.send_command(new testfunc2_cmd("hello"),rc);
399 catch(t2n_transfer_error &e)
400 { errormsg=e.what(); }
404 CPPUNIT_ASSERT_EQUAL(string("timeout exceeded"),errormsg);
411 CPPUNIT_TEST_SUITE_REGISTRATION(test_timeout);