2 Copyright (C) 2004 by Intra2net AG
4 The software in this package is distributed under the GNU General
5 Public License version 2 (with a special exception described below).
7 A copy of GNU General Public License (GPL) is included in this distribution,
8 in the file COPYING.GPL.
10 As a special exception, if other files instantiate templates or use macros
11 or inline functions from this file, or you compile this file and link it
12 with other works to produce a work based on this file, this file
13 does not by itself cause the resulting work to be covered
14 by the GNU General Public License.
16 However the source code for this file must still be made available
17 in accordance with section (3) of the GNU General Public License.
19 This exception does not invalidate any other reasons why a work based
20 on this file might be covered by the GNU General Public License.
22 #include <sys/types.h>
34 #include <boost/bind.hpp>
36 #include <cppunit/extensions/TestFactoryRegistry.h>
37 #include <cppunit/ui/text/TestRunner.h>
38 #include <cppunit/extensions/HelperMacros.h>
40 #include <socket_client.hxx>
41 #include <socket_server.hxx>
44 using namespace libt2n;
45 using namespace CppUnit;
47 class test_callback : public TestFixture
49 CPPUNIT_TEST_SUITE(test_callback);
51 CPPUNIT_TEST(ServerNewConnCallback);
52 CPPUNIT_TEST(ServerConnClosedCallback);
53 CPPUNIT_TEST(ServerConnDeletedCallback);
54 CPPUNIT_TEST(ServerCallbackOrder);
55 CPPUNIT_TEST(ClientConnClosedCallback);
56 CPPUNIT_TEST(ClientConnDeletedCallback);
58 CPPUNIT_TEST_SUITE_END();
60 std::vector<bool> callback_done;
68 callback_done.resize(__events_end);
73 callback_done.clear();
75 // make sure the server-child is dead before the next test runs
76 kill(child_pid,SIGKILL);
80 void callback_func(callback_event_type ev, int conn_id)
82 // we don't care for the conn_id, we just mark the callback as done
83 if (callback_done[ev])
84 throw runtime_error("callback already done for this event");
86 callback_done[ev]=true;
89 void ServerNewConnCallback()
91 switch(child_pid=fork())
95 CPPUNIT_FAIL("fork error");
104 // wait till server is up
108 socket_client_connection sc("./socket");
113 sc.fill_buffer(500000);
116 // close the connection
120 std::cerr << "exception in child. ignoring\n";
123 // don't call atexit and stuff
130 socket_server ss("./socket");
132 ss.add_callback(new_connection,bind(&test_callback::callback_func, boost::ref(*this), new_connection, _1));
135 for (int i=0; i < 3; i++)
137 ss.fill_buffer(1000000);
141 if(ss.get_packet(data,cid))
143 server_connection* con=ss.get_connection(cid);
147 CPPUNIT_ASSERT_EQUAL(true,static_cast<bool>(callback_done[new_connection]));
148 CPPUNIT_ASSERT_EQUAL(false,static_cast<bool>(callback_done[connection_closed]));
149 CPPUNIT_ASSERT_EQUAL(false,static_cast<bool>(callback_done[connection_deleted]));
154 void ServerConnClosedCallback()
156 switch(child_pid=fork())
160 CPPUNIT_FAIL("fork error");
169 // wait till server is up
173 socket_client_connection sc("./socket");
178 sc.fill_buffer(500000);
181 // close the connection
185 std::cerr << "exception in child. ignoring\n";
188 // don't call atexit and stuff
195 socket_server ss("./socket");
197 ss.add_callback(connection_closed,bind(&test_callback::callback_func, boost::ref(*this), connection_closed, _1));
200 for (int i=0; i < 3; i++)
202 ss.fill_buffer(1000000);
206 if(ss.get_packet(data,cid))
208 server_connection* con=ss.get_connection(cid);
212 CPPUNIT_ASSERT_EQUAL(false,static_cast<bool>(callback_done[new_connection]));
213 CPPUNIT_ASSERT_EQUAL(true,static_cast<bool>(callback_done[connection_closed]));
214 CPPUNIT_ASSERT_EQUAL(false,static_cast<bool>(callback_done[connection_deleted]));
219 void ServerConnDeletedCallback()
221 switch(child_pid=fork())
225 CPPUNIT_FAIL("fork error");
234 // wait till server is up
238 socket_client_connection sc("./socket");
243 sc.fill_buffer(500000);
246 // close the connection
250 std::cerr << "exception in child. ignoring\n";
253 // don't call atexit and stuff
260 socket_server ss("./socket");
262 ss.add_callback(connection_deleted,bind(&test_callback::callback_func, boost::ref(*this), connection_deleted, _1));
265 for (int i=0; i < 3; i++)
267 ss.fill_buffer(1000000);
271 if(ss.get_packet(data,cid))
273 server_connection* con=ss.get_connection(cid);
280 CPPUNIT_ASSERT_EQUAL(false,static_cast<bool>(callback_done[new_connection]));
281 CPPUNIT_ASSERT_EQUAL(false,static_cast<bool>(callback_done[connection_closed]));
282 CPPUNIT_ASSERT_EQUAL(true,static_cast<bool>(callback_done[connection_deleted]));
287 void ServerCallbackOrder()
289 switch(child_pid=fork())
293 CPPUNIT_FAIL("fork error");
302 // wait till server is up
306 socket_client_connection sc("./socket");
311 sc.fill_buffer(500000);
316 // close the connection
320 std::cerr << "exception in child. ignoring\n";
323 // don't call atexit and stuff
330 socket_server ss("./socket");
332 ss.add_callback(connection_closed,bind(&test_callback::callback_func, boost::ref(*this), connection_closed, _1));
333 ss.add_callback(connection_deleted,bind(&test_callback::callback_func, boost::ref(*this), connection_deleted, _1));
337 for (int i=0; i < 5; i++)
339 ss.fill_buffer(500000);
343 if(!got_1 && ss.get_packet(data,cid))
345 server_connection* con=ss.get_connection(cid);
348 // don't call get_packet anymore
354 CPPUNIT_ASSERT_EQUAL_MESSAGE("closed done",true,static_cast<bool>(callback_done[connection_closed]));
355 CPPUNIT_ASSERT_EQUAL_MESSAGE("not deleted yet",false,static_cast<bool>(callback_done[connection_deleted]));
357 for (int i=0; i < 4; i++)
359 ss.fill_buffer(500000);
363 ss.get_packet(data,cid);
368 CPPUNIT_ASSERT_EQUAL_MESSAGE("closed done (2)",true,static_cast<bool>(callback_done[connection_closed]));
369 CPPUNIT_ASSERT_EQUAL_MESSAGE("deleted done",true,static_cast<bool>(callback_done[connection_deleted]));
374 void ClientConnClosedCallback()
376 switch(child_pid=fork())
380 CPPUNIT_FAIL("fork error");
388 socket_server ss("./socket");
391 for (int i=0; i < 3; i++)
393 ss.fill_buffer(1000000);
397 if(ss.get_packet(data,cid))
402 std::cerr << "exception in child. ignoring\n";
405 // don't call atexit and stuff
413 // wait till server is up
416 socket_client_connection sc("./socket");
418 sc.add_callback(connection_closed,bind(&test_callback::callback_func, boost::ref(*this), connection_closed, 0));
423 sc.fill_buffer(500000);
426 CPPUNIT_ASSERT_EQUAL(false,static_cast<bool>(callback_done[new_connection]));
427 CPPUNIT_ASSERT_EQUAL(true,static_cast<bool>(callback_done[connection_closed]));
428 CPPUNIT_ASSERT_EQUAL(false,static_cast<bool>(callback_done[connection_deleted]));
433 void ClientConnDeletedCallback()
435 switch(child_pid=fork())
439 CPPUNIT_FAIL("fork error");
447 socket_server ss("./socket");
450 for (int i=0; i < 3; i++)
452 ss.fill_buffer(1000000);
456 if(ss.get_packet(data,cid))
461 std::cerr << "exception in child. ignoring\n";
464 // don't call atexit and stuff
472 // wait till server is up
476 socket_client_connection sc("./socket");
478 sc.add_callback(connection_deleted,bind(&test_callback::callback_func, boost::ref(*this), connection_deleted, 0));
483 sc.fill_buffer(500000);
487 CPPUNIT_ASSERT_EQUAL(false,static_cast<bool>(callback_done[new_connection]));
488 CPPUNIT_ASSERT_EQUAL(false,static_cast<bool>(callback_done[connection_closed]));
489 CPPUNIT_ASSERT_EQUAL(true,static_cast<bool>(callback_done[connection_deleted]));
495 CPPUNIT_TEST_SUITE_REGISTRATION(test_callback);