1 /***************************************************************************
2 * Copyright (C) 2004 by Intra2net AG *
5 ***************************************************************************/
19 #include <boost/bind.hpp>
21 #include <cppunit/extensions/TestFactoryRegistry.h>
22 #include <cppunit/ui/text/TestRunner.h>
23 #include <cppunit/extensions/HelperMacros.h>
25 #include <socket_client.hxx>
26 #include <socket_server.hxx>
29 using namespace libt2n;
30 using namespace CppUnit;
32 class test_callback : public TestFixture
34 CPPUNIT_TEST_SUITE(test_callback);
36 CPPUNIT_TEST(ServerNewConnCallback);
37 CPPUNIT_TEST(ServerConnClosedCallback);
38 CPPUNIT_TEST(ServerConnDeletedCallback);
39 CPPUNIT_TEST(ServerCallbackOrder);
40 CPPUNIT_TEST(ClientConnClosedCallback);
41 CPPUNIT_TEST(ClientConnDeletedCallback);
43 CPPUNIT_TEST_SUITE_END();
45 std::vector<bool> callback_done;
53 callback_done.resize(__events_end);
58 callback_done.clear();
60 // make sure the server-child is dead before the next test runs
61 kill(child_pid,SIGKILL);
65 void callback_func(callback_event_type ev, int conn_id)
67 // we don't care for the conn_id, we just mark the callback as done
68 if (callback_done[ev])
69 throw runtime_error("callback already done for this event");
71 callback_done[ev]=true;
74 void ServerNewConnCallback()
76 switch(child_pid=fork())
80 CPPUNIT_FAIL("fork error");
89 // wait till server is up
93 socket_client_connection sc("./socket");
98 sc.fill_buffer(500000);
101 // close the connection
105 std::cerr << "exception in child. ignoring\n";
108 // don't call atexit and stuff
115 socket_server ss("./socket");
117 ss.add_callback(new_connection,bind(&test_callback::callback_func, boost::ref(*this), new_connection, _1));
120 for (int i=0; i < 3; i++)
122 ss.fill_buffer(1000000);
126 if(ss.get_packet(data,cid))
128 server_connection* con=ss.get_connection(cid);
132 CPPUNIT_ASSERT_EQUAL(true,static_cast<bool>(callback_done[new_connection]));
133 CPPUNIT_ASSERT_EQUAL(false,static_cast<bool>(callback_done[connection_closed]));
134 CPPUNIT_ASSERT_EQUAL(false,static_cast<bool>(callback_done[connection_deleted]));
139 void ServerConnClosedCallback()
141 switch(child_pid=fork())
145 CPPUNIT_FAIL("fork error");
154 // wait till server is up
158 socket_client_connection sc("./socket");
163 sc.fill_buffer(500000);
166 // close the connection
170 std::cerr << "exception in child. ignoring\n";
173 // don't call atexit and stuff
180 socket_server ss("./socket");
182 ss.add_callback(connection_closed,bind(&test_callback::callback_func, boost::ref(*this), connection_closed, _1));
185 for (int i=0; i < 3; i++)
187 ss.fill_buffer(1000000);
191 if(ss.get_packet(data,cid))
193 server_connection* con=ss.get_connection(cid);
197 CPPUNIT_ASSERT_EQUAL(false,static_cast<bool>(callback_done[new_connection]));
198 CPPUNIT_ASSERT_EQUAL(true,static_cast<bool>(callback_done[connection_closed]));
199 CPPUNIT_ASSERT_EQUAL(false,static_cast<bool>(callback_done[connection_deleted]));
204 void ServerConnDeletedCallback()
206 switch(child_pid=fork())
210 CPPUNIT_FAIL("fork error");
219 // wait till server is up
223 socket_client_connection sc("./socket");
228 sc.fill_buffer(500000);
231 // close the connection
235 std::cerr << "exception in child. ignoring\n";
238 // don't call atexit and stuff
245 socket_server ss("./socket");
247 ss.add_callback(connection_deleted,bind(&test_callback::callback_func, boost::ref(*this), connection_deleted, _1));
250 for (int i=0; i < 3; i++)
252 ss.fill_buffer(1000000);
256 if(ss.get_packet(data,cid))
258 server_connection* con=ss.get_connection(cid);
265 CPPUNIT_ASSERT_EQUAL(false,static_cast<bool>(callback_done[new_connection]));
266 CPPUNIT_ASSERT_EQUAL(false,static_cast<bool>(callback_done[connection_closed]));
267 CPPUNIT_ASSERT_EQUAL(true,static_cast<bool>(callback_done[connection_deleted]));
272 void ServerCallbackOrder()
274 switch(child_pid=fork())
278 CPPUNIT_FAIL("fork error");
287 // wait till server is up
291 socket_client_connection sc("./socket");
296 sc.fill_buffer(500000);
301 // close the connection
305 std::cerr << "exception in child. ignoring\n";
308 // don't call atexit and stuff
315 socket_server ss("./socket");
317 ss.add_callback(connection_closed,bind(&test_callback::callback_func, boost::ref(*this), connection_closed, _1));
318 ss.add_callback(connection_deleted,bind(&test_callback::callback_func, boost::ref(*this), connection_deleted, _1));
322 for (int i=0; i < 5; i++)
324 ss.fill_buffer(500000);
328 if(!got_1 && ss.get_packet(data,cid))
330 server_connection* con=ss.get_connection(cid);
333 // don't call get_packet anymore
339 CPPUNIT_ASSERT_EQUAL_MESSAGE("closed done",true,static_cast<bool>(callback_done[connection_closed]));
340 CPPUNIT_ASSERT_EQUAL_MESSAGE("not deleted yet",false,static_cast<bool>(callback_done[connection_deleted]));
342 for (int i=0; i < 4; i++)
344 ss.fill_buffer(500000);
348 ss.get_packet(data,cid);
353 CPPUNIT_ASSERT_EQUAL_MESSAGE("closed done (2)",true,static_cast<bool>(callback_done[connection_closed]));
354 CPPUNIT_ASSERT_EQUAL_MESSAGE("deleted done",true,static_cast<bool>(callback_done[connection_deleted]));
359 void ClientConnClosedCallback()
361 switch(child_pid=fork())
365 CPPUNIT_FAIL("fork error");
373 socket_server ss("./socket");
376 for (int i=0; i < 3; i++)
378 ss.fill_buffer(1000000);
382 if(ss.get_packet(data,cid))
387 std::cerr << "exception in child. ignoring\n";
390 // don't call atexit and stuff
398 // wait till server is up
401 socket_client_connection sc("./socket");
403 sc.add_callback(connection_closed,bind(&test_callback::callback_func, boost::ref(*this), connection_closed, 0));
408 sc.fill_buffer(500000);
411 CPPUNIT_ASSERT_EQUAL(false,static_cast<bool>(callback_done[new_connection]));
412 CPPUNIT_ASSERT_EQUAL(true,static_cast<bool>(callback_done[connection_closed]));
413 CPPUNIT_ASSERT_EQUAL(false,static_cast<bool>(callback_done[connection_deleted]));
418 void ClientConnDeletedCallback()
420 switch(child_pid=fork())
424 CPPUNIT_FAIL("fork error");
432 socket_server ss("./socket");
435 for (int i=0; i < 3; i++)
437 ss.fill_buffer(1000000);
441 if(ss.get_packet(data,cid))
446 std::cerr << "exception in child. ignoring\n";
449 // don't call atexit and stuff
457 // wait till server is up
461 socket_client_connection sc("./socket");
463 sc.add_callback(connection_deleted,bind(&test_callback::callback_func, boost::ref(*this), connection_deleted, 0));
468 sc.fill_buffer(500000);
472 CPPUNIT_ASSERT_EQUAL(false,static_cast<bool>(callback_done[new_connection]));
473 CPPUNIT_ASSERT_EQUAL(false,static_cast<bool>(callback_done[connection_closed]));
474 CPPUNIT_ASSERT_EQUAL(true,static_cast<bool>(callback_done[connection_deleted]));
480 CPPUNIT_TEST_SUITE_REGISTRATION(test_callback);