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 #define BOOST_TEST_DYN_LINK
37 #include <boost/test/unit_test.hpp>
39 #include <socket_client.hxx>
40 #include <socket_server.hxx>
42 #include "test_fixtures.hxx"
45 using namespace libt2n;
47 class test_callbackFixture : public KillChildOnShutdownFixture
50 std::vector<bool> callback_done;
52 void callback_func(callback_event_type ev, int conn_id)
54 // we don't care for the conn_id, we just mark the callback as done
55 if (callback_done[ev])
56 throw runtime_error("callback already done for this event");
58 callback_done[ev]=true;
62 test_callbackFixture()
64 callback_done.resize(__events_end);
67 ~test_callbackFixture()
69 callback_done.clear();
73 BOOST_FIXTURE_TEST_SUITE(test_callback, test_callbackFixture)
75 BOOST_AUTO_TEST_CASE(ServerNewConnCallback)
77 switch(child_pid=fork())
81 BOOST_FAIL("fork error");
90 // wait till server is up
94 socket_client_connection sc("./socket");
99 sc.fill_buffer(500000);
102 // close the connection
106 std::cerr << "exception in child. ignoring\n";
109 // don't call atexit and stuff
116 socket_server ss("./socket");
118 ss.add_callback(new_connection,bind(&test_callback::ServerNewConnCallback::callback_func, boost::ref(*this), new_connection, _1));
121 for (int i=0; i < 3; i++)
123 ss.fill_buffer(1000000);
127 if(ss.get_packet(data,cid))
129 server_connection* con=ss.get_connection(cid);
133 BOOST_CHECK_EQUAL(true,static_cast<bool>(callback_done[new_connection]));
134 BOOST_CHECK_EQUAL(false,static_cast<bool>(callback_done[connection_closed]));
135 BOOST_CHECK_EQUAL(false,static_cast<bool>(callback_done[connection_deleted]));
140 BOOST_AUTO_TEST_CASE(ServerConnClosedCallback)
142 switch(child_pid=fork())
146 BOOST_FAIL("fork error");
155 // wait till server is up
159 socket_client_connection sc("./socket");
164 sc.fill_buffer(500000);
167 // close the connection
171 std::cerr << "exception in child. ignoring\n";
174 // don't call atexit and stuff
181 socket_server ss("./socket");
183 ss.add_callback(connection_closed,bind(&test_callback::ServerConnClosedCallback::callback_func, boost::ref(*this), connection_closed, _1));
186 for (int i=0; i < 3; i++)
188 ss.fill_buffer(1000000);
192 if(ss.get_packet(data,cid))
194 server_connection* con=ss.get_connection(cid);
198 BOOST_CHECK_EQUAL(false,static_cast<bool>(callback_done[new_connection]));
199 BOOST_CHECK_EQUAL(true,static_cast<bool>(callback_done[connection_closed]));
200 BOOST_CHECK_EQUAL(false,static_cast<bool>(callback_done[connection_deleted]));
205 BOOST_AUTO_TEST_CASE(ServerConnDeletedCallback)
207 switch(child_pid=fork())
211 BOOST_FAIL("fork error");
220 // wait till server is up
224 socket_client_connection sc("./socket");
229 sc.fill_buffer(500000);
232 // close the connection
236 std::cerr << "exception in child. ignoring\n";
239 // don't call atexit and stuff
246 socket_server ss("./socket");
248 ss.add_callback(connection_deleted,bind(&test_callback::ServerConnDeletedCallback::callback_func, boost::ref(*this), connection_deleted, _1));
251 for (int i=0; i < 3; i++)
253 ss.fill_buffer(1000000);
257 if(ss.get_packet(data,cid))
259 server_connection* con=ss.get_connection(cid);
266 BOOST_CHECK_EQUAL(false,static_cast<bool>(callback_done[new_connection]));
267 BOOST_CHECK_EQUAL(false,static_cast<bool>(callback_done[connection_closed]));
268 BOOST_CHECK_EQUAL(true,static_cast<bool>(callback_done[connection_deleted]));
273 BOOST_AUTO_TEST_CASE(ServerCallbackOrder)
275 switch(child_pid=fork())
279 BOOST_FAIL("fork error");
288 // wait till server is up
292 socket_client_connection sc("./socket");
297 sc.fill_buffer(500000);
302 // close the connection
306 std::cerr << "exception in child. ignoring\n";
309 // don't call atexit and stuff
316 socket_server ss("./socket");
318 ss.add_callback(connection_closed,bind(&test_callback::ServerCallbackOrder::callback_func, boost::ref(*this), connection_closed, _1));
319 ss.add_callback(connection_deleted,bind(&test_callback::ServerCallbackOrder::callback_func, boost::ref(*this), connection_deleted, _1));
323 for (int i=0; i < 5; i++)
325 ss.fill_buffer(500000);
329 if(!got_1 && ss.get_packet(data,cid))
331 server_connection* con=ss.get_connection(cid);
334 // don't call get_packet anymore
340 BOOST_CHECK_MESSAGE(static_cast<bool>(callback_done[connection_closed]) == true, "closed done");
341 BOOST_CHECK_MESSAGE(static_cast<bool>(callback_done[connection_deleted]) == false, "not deleted yet");
343 for (int i=0; i < 4; i++)
345 ss.fill_buffer(500000);
349 ss.get_packet(data,cid);
354 BOOST_CHECK_MESSAGE(static_cast<bool>(callback_done[connection_closed]) == true, "closed done (2)");
355 BOOST_CHECK_MESSAGE(static_cast<bool>(callback_done[connection_deleted]) == true, "not deleted yet(2)");
360 BOOST_AUTO_TEST_CASE(ClientConnClosedCallback)
362 switch(child_pid=fork())
366 BOOST_FAIL("fork error");
374 socket_server ss("./socket");
377 for (int i=0; i < 3; i++)
379 ss.fill_buffer(1000000);
383 if(ss.get_packet(data,cid))
388 std::cerr << "exception in child. ignoring\n";
391 // don't call atexit and stuff
399 // wait till server is up
402 socket_client_connection sc("./socket");
404 sc.add_callback(connection_closed,bind(&test_callback::ClientConnClosedCallback::callback_func, boost::ref(*this), connection_closed, 0));
409 sc.fill_buffer(500000);
412 BOOST_CHECK_EQUAL(false,static_cast<bool>(callback_done[new_connection]));
413 BOOST_CHECK_EQUAL(true,static_cast<bool>(callback_done[connection_closed]));
414 BOOST_CHECK_EQUAL(false,static_cast<bool>(callback_done[connection_deleted]));
419 BOOST_AUTO_TEST_CASE(ClientConnDeletedCallback)
421 switch(child_pid=fork())
425 BOOST_FAIL("fork error");
433 socket_server ss("./socket");
436 for (int i=0; i < 3; i++)
438 ss.fill_buffer(1000000);
442 if(ss.get_packet(data,cid))
447 std::cerr << "exception in child. ignoring\n";
450 // don't call atexit and stuff
458 // wait till server is up
462 socket_client_connection sc("./socket");
464 sc.add_callback(connection_deleted,bind(&test_callback::ClientConnDeletedCallback::callback_func, boost::ref(*this), connection_deleted, 0));
469 sc.fill_buffer(500000);
473 BOOST_CHECK_EQUAL(false,static_cast<bool>(callback_done[new_connection]));
474 BOOST_CHECK_EQUAL(false,static_cast<bool>(callback_done[connection_closed]));
475 BOOST_CHECK_EQUAL(true,static_cast<bool>(callback_done[connection_deleted]));
480 BOOST_AUTO_TEST_SUITE_END()