X-Git-Url: http://developer.intra2net.com/git/?p=libt2n;a=blobdiff_plain;f=test%2Freconnect.cpp;h=227df5672eef4c4453796f1abe0eb3c3cc0c4d6a;hp=394ba25e6102cb7fb0eee8a3c14091807fb89e31;hb=44b4600fd51677e54dd167734ca9252b58237cda;hpb=b592218402bfa779c024da2f32c4c782d52eaf88 diff --git a/test/reconnect.cpp b/test/reconnect.cpp index 394ba25..227df56 100644 --- a/test/reconnect.cpp +++ b/test/reconnect.cpp @@ -1,9 +1,24 @@ -/*************************************************************************** - * Copyright (C) 2004 by Intra2net AG * - * info@intra2net.com * - * * - ***************************************************************************/ +/* +Copyright (C) 2004 by Intra2net AG +The software in this package is distributed under the GNU General +Public License version 2 (with a special exception described below). + +A copy of GNU General Public License (GPL) is included in this distribution, +in the file COPYING.GPL. + +As a special exception, if other files instantiate templates or use macros +or inline functions from this file, or you compile this file and link it +with other works to produce a work based on this file, this file +does not by itself cause the resulting work to be covered +by the GNU General Public License. + +However the source code for this file must still be made available +in accordance with section (3) of the GNU General Public License. + +This exception does not invalidate any other reasons why a work based +on this file might be covered by the GNU General Public License. +*/ #include #include #include @@ -16,76 +31,34 @@ #include #include -#include -#include -#include +#define BOOST_TEST_DYN_LINK +#include #include #include -#ifdef HAVE_CONFIG_H +#include "test_fixtures.hxx" + #include -#endif using namespace std; using namespace libt2n; -using namespace CppUnit; -// this is an evil hack to get access to real_write, don't ever do this in an app!!! -class real_write_connection: public socket_server_connection -{ - public: - void real_write(const std::string& data) - { socket_write(data); } -}; +BOOST_FIXTURE_TEST_SUITE(test_reconnect, KillChildOnShutdownFixture) -class test_reconnect : public TestFixture +BOOST_AUTO_TEST_CASE(simple_reconnect) { - CPPUNIT_TEST_SUITE(test_reconnect); - - CPPUNIT_TEST(simple_reconnect); - CPPUNIT_TEST(reconnect_with_close); - CPPUNIT_TEST(reconnect_buffer_complete); - CPPUNIT_TEST(reconnect_buffer_several_complete); - CPPUNIT_TEST(reconnect_buffer_no_incomplete1); - CPPUNIT_TEST(reconnect_buffer_no_incomplete2); - - CPPUNIT_TEST_SUITE_END(); - - pid_t child_pid; - - public: - - void setUp() - { } - - void tearDown() + switch(child_pid=fork()) { - // make sure the server-child is dead before the next test runs - kill(child_pid,SIGKILL); - sleep(1); - } - - void send_raw_socket(string hello_string, socket_server* ss, int conn_id) - { - socket_server_connection *ssc=dynamic_cast(ss->get_connection(conn_id)); - - // this is an evil hack to get access to real_write, don't ever do this in an app!!! - real_write_connection *rwc=(real_write_connection*)ssc; - rwc->real_write(hello_string); - } - - void simple_reconnect() - { - switch(child_pid=fork()) + case -1: { - case -1: - { - CPPUNIT_FAIL("fork error"); - break; - } - case 0: - // child + BOOST_FAIL("fork error"); + break; + } + case 0: + // child + { + try { socket_server ss("./socket"); @@ -112,51 +85,57 @@ class test_reconnect : public TestFixture con->write(string().insert(0,100,'Y')); } } - - // don't call atexit and stuff - _exit(0); + } catch(...) + { + std::cerr << "exception in child. ignoring\n"; } - default: - // parent - { - // don't kill us on broken pipe - signal(SIGPIPE, SIG_IGN); + // don't call atexit and stuff + _exit(0); + } - // wait till server is up - sleep(1); - socket_client_connection sc("./socket"); + default: + // parent + { + // don't kill us on broken pipe + signal(SIGPIPE, SIG_IGN); - sc.write("abc"); + // wait till server is up + sleep(1); + socket_client_connection sc("./socket"); - string data; + sc.write("abc"); - while (!sc.get_packet(data)) - sc.fill_buffer(1000000); + string data; - sc.reconnect(); + while (!sc.get_packet(data)) + sc.fill_buffer(1000000); - sc.write("x"); + sc.reconnect(); - while (!sc.get_packet(data)) - sc.fill_buffer(1000000); + sc.write("x"); - CPPUNIT_ASSERT_EQUAL(string().insert(0,100,'X'),data); - } + while (!sc.get_packet(data)) + sc.fill_buffer(1000000); + + BOOST_CHECK_EQUAL(string().insert(0,100,'X'),data); } } +} - void reconnect_with_close() +BOOST_AUTO_TEST_CASE(reconnect_with_close) +{ + switch(child_pid=fork()) { - switch(child_pid=fork()) + case -1: { - case -1: - { - CPPUNIT_FAIL("fork error"); - break; - } - case 0: - // child + BOOST_FAIL("fork error"); + break; + } + case 0: + // child + { + try { socket_server ss("./socket"); @@ -183,56 +162,62 @@ class test_reconnect : public TestFixture con->write(string().insert(0,100,'Y')); } } - - // don't call atexit and stuff - _exit(0); + } catch(...) + { + std::cerr << "exception in child. ignoring\n"; } - default: - // parent - { - // don't kill us on broken pipe - signal(SIGPIPE, SIG_IGN); + // don't call atexit and stuff + _exit(0); + } - // wait till server is up - sleep(1); - socket_client_connection sc("./socket"); + default: + // parent + { + // don't kill us on broken pipe + signal(SIGPIPE, SIG_IGN); - sc.write("abc"); + // wait till server is up + sleep(1); + socket_client_connection sc("./socket"); - string data; + sc.write("abc"); - while (!sc.get_packet(data)) - sc.fill_buffer(1000000); + string data; - sc.close(); + while (!sc.get_packet(data)) + sc.fill_buffer(1000000); - // empty buffer - sc.get_packet(data); + sc.close(); - sc.reconnect(); + // empty buffer + sc.get_packet(data); - sc.write("x"); + sc.reconnect(); - while (!sc.get_packet(data)) - sc.fill_buffer(1000000); + sc.write("x"); - CPPUNIT_ASSERT_EQUAL(string().insert(0,100,'X'),data); - } + while (!sc.get_packet(data)) + sc.fill_buffer(1000000); + + BOOST_CHECK_EQUAL(string().insert(0,100,'X'),data); } } +} - void reconnect_buffer_complete() +BOOST_AUTO_TEST_CASE(reconnect_buffer_complete) +{ + switch(child_pid=fork()) { - switch(child_pid=fork()) + case -1: { - case -1: - { - CPPUNIT_FAIL("fork error"); - break; - } - case 0: - // child + BOOST_FAIL("fork error"); + break; + } + case 0: + // child + { + try { socket_server ss("./socket"); @@ -257,50 +242,56 @@ class test_reconnect : public TestFixture con->write(string().insert(0,100,'X')); } } - - // don't call atexit and stuff - _exit(0); + } catch(...) + { + std::cerr << "exception in child. ignoring\n"; } - default: - // parent - { - // don't kill us on broken pipe - signal(SIGPIPE, SIG_IGN); + // don't call atexit and stuff + _exit(0); + } - // wait till server is up - sleep(1); - socket_client_connection sc("./socket"); + default: + // parent + { + // don't kill us on broken pipe + signal(SIGPIPE, SIG_IGN); - sc.write("x"); + // wait till server is up + sleep(1); + socket_client_connection sc("./socket"); - string data; + sc.write("x"); - while (!sc.packet_available()) - sc.fill_buffer(1000000); + string data; - sc.reconnect(); + while (!sc.packet_available()) + sc.fill_buffer(1000000); - CPPUNIT_ASSERT_EQUAL_MESSAGE("packet not there",true,sc.get_packet(data)); + sc.reconnect(); - CPPUNIT_ASSERT_EQUAL_MESSAGE("data incorrect",string().insert(0,100,'X'),data); - } + BOOST_CHECK_MESSAGE(sc.get_packet(data) == true, "packet not there"); + + BOOST_CHECK_MESSAGE(data == string().insert(0,100,'X'), "data incorrect"); } } +} - void reconnect_buffer_several_complete() - { - const int packets=3; +BOOST_AUTO_TEST_CASE(reconnect_buffer_several_complete) +{ + const int packets=3; - switch(child_pid=fork()) + switch(child_pid=fork()) + { + case -1: { - case -1: - { - CPPUNIT_FAIL("fork error"); - break; - } - case 0: - // child + BOOST_FAIL("fork error"); + break; + } + case 0: + // child + { + try { socket_server ss("./socket"); @@ -328,65 +319,71 @@ class test_reconnect : public TestFixture } } } - - // don't call atexit and stuff - _exit(0); + } catch(...) + { + std::cerr << "exception in child. ignoring\n"; } - default: - // parent - { - // don't kill us on broken pipe - signal(SIGPIPE, SIG_IGN); + // don't call atexit and stuff + _exit(0); + } - // wait till server is up - sleep(1); - socket_client_connection sc("./socket"); + default: + // parent + { + // don't kill us on broken pipe + signal(SIGPIPE, SIG_IGN); - sc.write("x"); + // wait till server is up + sleep(1); + socket_client_connection sc("./socket"); - // retrieve packets for 3 seconds - time_t t0 = time(NULL); + sc.write("x"); - // max 3 sec - while (time(NULL) < t0 + 3 ) - sc.fill_buffer(1000000); + // retrieve packets for 3 seconds + time_t t0 = time(NULL); - // we now should have packets complete packets in the buffer + // max 3 sec + while (time(NULL) < t0 + 3 ) + sc.fill_buffer(1000000); - sc.reconnect(); + // we now should have packets complete packets in the buffer - // are these packets still there? + sc.reconnect(); - for (int i=0; i < packets; i++) - { - string data; + // are these packets still there? - ostringstream os; - os << "packet " << i << " not there"; + for (int i=0; i < packets; i++) + { + string data; - CPPUNIT_ASSERT_EQUAL_MESSAGE(os.str(),true,sc.get_packet(data)); + ostringstream os; + os << "packet " << i << " not there"; - os.str(""); - os << "packet " << i << " incorrect"; + BOOST_CHECK_MESSAGE(sc.get_packet(data) == true, os.str()); - CPPUNIT_ASSERT_EQUAL_MESSAGE(os.str(),string().insert(0,100,'X'),data); - } + os.str(""); + os << "packet " << i << " incorrect"; + + BOOST_CHECK_MESSAGE(string().insert(0,100,'X') == data, os.str()); } } } +} - void reconnect_buffer_no_incomplete1() +BOOST_AUTO_TEST_CASE(reconnect_buffer_no_incomplete1) +{ + switch(child_pid=fork()) { - switch(child_pid=fork()) + case -1: { - case -1: - { - CPPUNIT_FAIL("fork error"); - break; - } - case 0: - // child + BOOST_FAIL("fork error"); + break; + } + case 0: + // child + { + try { socket_server ss("./socket"); @@ -414,55 +411,61 @@ class test_reconnect : public TestFixture } } } - - // don't call atexit and stuff - _exit(0); + } catch(...) + { + std::cerr << "exception in child. ignoring\n"; } - default: - // parent - { - // don't kill us on broken pipe - signal(SIGPIPE, SIG_IGN); + // don't call atexit and stuff + _exit(0); + } - // wait till server is up - sleep(1); - socket_client_connection sc("./socket"); + default: + // parent + { + // don't kill us on broken pipe + signal(SIGPIPE, SIG_IGN); - sc.write("x"); + // wait till server is up + sleep(1); + socket_client_connection sc("./socket"); - // retrieve packets for 3 seconds - time_t t0 = time(NULL); + sc.write("x"); - // max 3 sec - while (time(NULL) < t0 + 3 ) - sc.fill_buffer(1000000); + // retrieve packets for 3 seconds + time_t t0 = time(NULL); - // we now should have one complete packet and some stuff in the buffer + // max 3 sec + while (time(NULL) < t0 + 3 ) + sc.fill_buffer(1000000); - string data; - sc.get_packet(data); + // we now should have one complete packet and some stuff in the buffer - CPPUNIT_ASSERT_EQUAL_MESSAGE("no incomplete packet",true,(sc.peek_packet(data))>0); + string data; + sc.get_packet(data); - sc.reconnect(); + BOOST_CHECK_MESSAGE((sc.peek_packet(data))>0, "no incomplete packet"); - CPPUNIT_ASSERT_EQUAL_MESSAGE("incomplete packet not removed",0,(int)sc.peek_packet(data)); - } + sc.reconnect(); + + BOOST_CHECK_MESSAGE((int)sc.peek_packet(data) == 0, "incomplete packet not removed"); } } +} - void reconnect_buffer_no_incomplete2() +BOOST_AUTO_TEST_CASE(reconnect_buffer_no_incomplete2) +{ + switch(child_pid=fork()) { - switch(child_pid=fork()) + case -1: { - case -1: - { - CPPUNIT_FAIL("fork error"); - break; - } - case 0: - // child + BOOST_FAIL("fork error"); + break; + } + case 0: + // child + { + try { socket_server ss("./socket"); @@ -501,44 +504,46 @@ class test_reconnect : public TestFixture } } } - - // don't call atexit and stuff - _exit(0); + } catch(...) + { + std::cerr << "exception in child. ignoring\n"; } - default: - // parent - { - // don't kill us on broken pipe - signal(SIGPIPE, SIG_IGN); + // don't call atexit and stuff + _exit(0); + } - // wait till server is up - sleep(1); - socket_client_connection sc("./socket"); + default: + // parent + { + // don't kill us on broken pipe + signal(SIGPIPE, SIG_IGN); - sc.write("x"); + // wait till server is up + sleep(1); + socket_client_connection sc("./socket"); - // retrieve packets for 3 seconds - time_t t0 = time(NULL); + sc.write("x"); - // max 3 sec - while (time(NULL) < t0 + 3 ) - sc.fill_buffer(1000000); + // retrieve packets for 3 seconds + time_t t0 = time(NULL); - // we now should have one complete packet and some stuff in the buffer + // max 3 sec + while (time(NULL) < t0 + 3 ) + sc.fill_buffer(1000000); - sc.reconnect(); + // we now should have one complete packet and some stuff in the buffer - string data; + sc.reconnect(); - CPPUNIT_ASSERT_EQUAL_MESSAGE("packet not there",true,sc.get_packet(data)); - CPPUNIT_ASSERT_EQUAL_MESSAGE("data incorrect",string().insert(0,100,'X'),data); + string data; - CPPUNIT_ASSERT_EQUAL_MESSAGE("incomplete packet not removed",0,(int)sc.peek_packet(data)); - } + BOOST_CHECK_MESSAGE(sc.get_packet(data) == true, "packet not there"); + BOOST_CHECK_MESSAGE(string().insert(0,100,'X') == data, "data incorrect"); + + BOOST_CHECK_MESSAGE((int)sc.peek_packet(data) == 0, "incomplete packet not removed"); } } +} -}; - -CPPUNIT_TEST_SUITE_REGISTRATION(test_reconnect); +BOOST_AUTO_TEST_SUITE_END()