X-Git-Url: http://developer.intra2net.com/git/?p=libt2n;a=blobdiff_plain;f=test%2Freconnect.cpp;fp=test%2Freconnect.cpp;h=6c704588698231955c28ea99ce3168b88a29fe58;hp=f078a95472dc275a26f4692c4411d3c839322ee6;hb=307b5e74c506b609d5c407be0943f45255ab5122;hpb=3d9c0861e86edcbfad10b84da332093fd6438f32 diff --git a/test/reconnect.cpp b/test/reconnect.cpp index f078a95..6c70458 100644 --- a/test/reconnect.cpp +++ b/test/reconnect.cpp @@ -31,20 +31,20 @@ on this file might be covered by the GNU General Public License. #include #include -#include -#include -#include +#define BOOST_TEST_DYN_LINK +#include #include #include +#include "test_fixtures.hxx" + #ifdef HAVE_CONFIG_H #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 @@ -54,33 +54,9 @@ class real_write_connection: public socket_server_connection { socket_write(data); } }; -class test_reconnect : public TestFixture +class test_reconnectFixture : public KillChildOnShutdownFixture { - 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() - { - // make sure the server-child is dead before the next test runs - kill(child_pid,SIGKILL); - sleep(1); - } - +protected: void send_raw_socket(string hello_string, socket_server* ss, int conn_id) { socket_server_connection *ssc=dynamic_cast(ss->get_connection(conn_id)); @@ -90,506 +66,516 @@ class test_reconnect : public TestFixture rwc->real_write(hello_string); } - void simple_reconnect() +public: + test_reconnectFixture() { - switch(child_pid=fork()) + } + + ~test_reconnectFixture() + { + } +}; + +BOOST_FIXTURE_TEST_SUITE(test_reconnect, test_reconnectFixture) + +BOOST_AUTO_TEST_CASE(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 { - try - { - socket_server ss("./socket"); + socket_server ss("./socket"); - time_t t0 = time(NULL); + time_t t0 = time(NULL); - // max 10 sec - while (time(NULL) < t0 + 10 ) - { - ss.fill_buffer(1000000); + // max 10 sec + while (time(NULL) < t0 + 10 ) + { + ss.fill_buffer(1000000); - string data; - unsigned int cid; + string data; + unsigned int cid; - if(ss.get_packet(data,cid)) - { - server_connection* con=ss.get_connection(cid); + if(ss.get_packet(data,cid)) + { + server_connection* con=ss.get_connection(cid); - if (data=="QUIT") - break; + if (data=="QUIT") + break; - if (data=="x") - con->write(string().insert(0,100,'X')); - else - con->write(string().insert(0,100,'Y')); - } + if (data=="x") + con->write(string().insert(0,100,'X')); + else + con->write(string().insert(0,100,'Y')); } - } catch(...) - { - std::cerr << "exception in child. ignoring\n"; } - - // 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 { - try - { - socket_server ss("./socket"); + socket_server ss("./socket"); - time_t t0 = time(NULL); + time_t t0 = time(NULL); - // max 10 sec - while (time(NULL) < t0 + 10 ) - { - ss.fill_buffer(1000000); + // max 10 sec + while (time(NULL) < t0 + 10 ) + { + ss.fill_buffer(1000000); - string data; - unsigned int cid; + string data; + unsigned int cid; - if(ss.get_packet(data,cid)) - { - server_connection* con=ss.get_connection(cid); + if(ss.get_packet(data,cid)) + { + server_connection* con=ss.get_connection(cid); - if (data=="QUIT") - break; + if (data=="QUIT") + break; - if (data=="x") - con->write(string().insert(0,100,'X')); - else - con->write(string().insert(0,100,'Y')); - } + if (data=="x") + con->write(string().insert(0,100,'X')); + else + con->write(string().insert(0,100,'Y')); } - } catch(...) - { - std::cerr << "exception in child. ignoring\n"; } - - // 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 { - try - { - socket_server ss("./socket"); + socket_server ss("./socket"); - time_t t0 = time(NULL); + time_t t0 = time(NULL); - // max 10 sec - while (time(NULL) < t0 + 10 ) - { - ss.fill_buffer(1000000); + // max 10 sec + while (time(NULL) < t0 + 10 ) + { + ss.fill_buffer(1000000); - string data; - unsigned int cid; + string data; + unsigned int cid; - if(ss.get_packet(data,cid)) - { - server_connection* con=ss.get_connection(cid); + if(ss.get_packet(data,cid)) + { + server_connection* con=ss.get_connection(cid); - if (data=="QUIT") - break; + if (data=="QUIT") + break; - if (data=="x") - con->write(string().insert(0,100,'X')); - } + if (data=="x") + con->write(string().insert(0,100,'X')); } - } catch(...) - { - std::cerr << "exception in child. ignoring\n"; } - - // 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 { - try + socket_server ss("./socket"); + + time_t t0 = time(NULL); + + // max 10 sec + while (time(NULL) < t0 + 10 ) { - socket_server ss("./socket"); + ss.fill_buffer(1000000); - time_t t0 = time(NULL); + string data; + unsigned int cid; - // max 10 sec - while (time(NULL) < t0 + 10 ) + if(ss.get_packet(data,cid)) { - ss.fill_buffer(1000000); + server_connection* con=ss.get_connection(cid); - string data; - unsigned int cid; + if (data=="QUIT") + break; - if(ss.get_packet(data,cid)) + if (data=="x") { - server_connection* con=ss.get_connection(cid); - - if (data=="QUIT") - break; - - if (data=="x") - { - for (int i=0; iwrite(string().insert(0,100,'X')); - } + for (int i=0; iwrite(string().insert(0,100,'X')); } } - } catch(...) - { - std::cerr << "exception in child. ignoring\n"; } - - // 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 { - try + socket_server ss("./socket"); + + time_t t0 = time(NULL); + + // max 10 sec + while (time(NULL) < t0 + 10 ) { - socket_server ss("./socket"); + ss.fill_buffer(1000000); - time_t t0 = time(NULL); + string data; + unsigned int cid; - // max 10 sec - while (time(NULL) < t0 + 10 ) + if(ss.get_packet(data,cid)) { - ss.fill_buffer(1000000); + server_connection* con=ss.get_connection(cid); - string data; - unsigned int cid; + if (data=="QUIT") + break; - if(ss.get_packet(data,cid)) + if (data=="x") { - server_connection* con=ss.get_connection(cid); - - if (data=="QUIT") - break; - - if (data=="x") - { - con->write(string().insert(0,100,'X')); - send_raw_socket("aaaab",&ss,cid); - } + con->write(string().insert(0,100,'X')); + send_raw_socket("aaaab",&ss,cid); } } - } catch(...) - { - std::cerr << "exception in child. ignoring\n"; } - - // 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 { - try + socket_server ss("./socket"); + + time_t t0 = time(NULL); + + // max 10 sec + while (time(NULL) < t0 + 10 ) { - socket_server ss("./socket"); + ss.fill_buffer(1000000); - time_t t0 = time(NULL); + string data; + unsigned int cid; - // max 10 sec - while (time(NULL) < t0 + 10 ) + if(ss.get_packet(data,cid)) { - ss.fill_buffer(1000000); + server_connection* con=ss.get_connection(cid); - string data; - unsigned int cid; + if (data=="QUIT") + break; - if(ss.get_packet(data,cid)) + if (data=="x") { - server_connection* con=ss.get_connection(cid); + con->write(string().insert(0,100,'X')); - if (data=="QUIT") - break; + string blob=string().insert(0,100,'Y'); - if (data=="x") - { - con->write(string().insert(0,100,'X')); - - string blob=string().insert(0,100,'Y'); - - // one byte will be missing... - int size=blob.size()+1; - char sizetransfer[sizeof(int)+1]; - memcpy(sizetransfer,(void*)&size,sizeof(int)); - sizetransfer[sizeof(int)+1]=0; + // one byte will be missing... + int size=blob.size()+1; + char sizetransfer[sizeof(int)+1]; + memcpy(sizetransfer,(void*)&size,sizeof(int)); + sizetransfer[sizeof(int)+1]=0; - string packet=string(sizetransfer)+blob; + string packet=string(sizetransfer)+blob; - send_raw_socket(packet,&ss,cid); - } + send_raw_socket(packet,&ss,cid); } } - } catch(...) - { - std::cerr << "exception in child. ignoring\n"; } - - // 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()