X-Git-Url: http://developer.intra2net.com/git/?p=libt2n;a=blobdiff_plain;f=test%2Fwrapper.cpp;h=5fedaf57babbbd62425b8f503aa638c1f23c73c0;hp=c820531afd9a912469bdd81e922badbb78757d3c;hb=a63e08b83794273da3840e0b8bf15bf0085fe3c4;hpb=fb3345ada7ea94225b78994fd50e3de693a2a3d5 diff --git a/test/wrapper.cpp b/test/wrapper.cpp index c820531..5fedaf5 100644 --- a/test/wrapper.cpp +++ b/test/wrapper.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 @@ -15,9 +30,8 @@ #include #include -#include -#include -#include +#define BOOST_TEST_DYN_LINK +#include #include #include @@ -34,13 +48,12 @@ #include #include -#ifdef HAVE_CONFIG_H +#include "test_fixtures.hxx" + #include -#endif using namespace std; using namespace libt2n; -using namespace CppUnit; // the server part @@ -223,30 +236,11 @@ std::auto_ptr wraptype::SingletonObject = std::auto_ptr(); template<> std::auto_ptr wraptype::WrappedConnection = std::auto_ptr(); -class test_wrapper : public TestFixture -{ - CPPUNIT_TEST_SUITE(test_wrapper); - - CPPUNIT_TEST(no_init_exception); // must be called first!!! - CPPUNIT_TEST(simple_wrap); - CPPUNIT_TEST(double_use); - CPPUNIT_TEST(double_use_with_close); - CPPUNIT_TEST(reconnect_after_close); - CPPUNIT_TEST(reconnect_not_possible); - CPPUNIT_TEST(ignore_server_disconnect); - CPPUNIT_TEST(ignore_handler_reconnects); - -// TODO: missing tests: -// ignore: init, no server, ignore -// ignore: init, no server, ignore, server ok, connect? - CPPUNIT_TEST_SUITE_END(); - - public: - - pid_t child_pid; - - void setUp() +class test_wrapperFixture : public KillChildOnShutdownFixture +{ +public: + test_wrapperFixture() { close_server=false; kill_server=false; @@ -255,24 +249,30 @@ class test_wrapper : public TestFixture { case -1: { - CPPUNIT_FAIL("fork error"); + BOOST_FAIL("fork error"); break; } case 0: // child { - int i=0; - while(i < 10 && !kill_server) + try { - close_server=false; - - socket_server ss("./socket"); - group_command_server cs(ss); - ss.set_logging(&logstream,debug); - - // max 10 sec - for (; !close_server && !kill_server && i < 10; i++) - cs.handle(1000000); + int i=0; + while(i < 10 && !kill_server) + { + close_server=false; + + socket_server ss("./socket"); + group_command_server cs(ss); + ss.set_logging(&logstream,debug); + + // max 10 sec + for (; !close_server && !kill_server && i < 10; i++) + cs.handle(1000000); + } + } catch(...) + { + std::cerr << "exception in child. ignoring\n"; } // don't call atexit and stuff @@ -282,209 +282,190 @@ class test_wrapper : public TestFixture default: // parent { + // don't kill us on broken pipe + signal(SIGPIPE, SIG_IGN); + // wait till server is up sleep(1); - } } } - void tearDown() + ~test_wrapperFixture() { - // make sure the server-child is dead before the next test runs - kill(child_pid,SIGKILL); - sleep(1); } +}; - void no_init_exception() - { - CPPUNIT_ASSERT_THROW(t2n_exec(&cmd_group_x_client::serverfunc)(1),std::logic_error); - } - - void simple_wrap() - { - wraptype::set_connection(auto_ptr - (new BasicSocketWrapper("./socket"))); - - int i=t2n_exec(&cmd_group_x_client::serverfunc)(1); +BOOST_FIXTURE_TEST_SUITE(test_wrapper, test_wrapperFixture) - CPPUNIT_ASSERT_EQUAL(2,i); - } +BOOST_AUTO_TEST_CASE(no_init_exception) // must be called first! +{ + BOOST_REQUIRE_THROW(t2n_exec(&cmd_group_x_client::serverfunc)(1),std::logic_error); +} - void double_use() - { - // only one connection used? - wraptype::set_connection(auto_ptr - (new BasicSocketWrapper("./socket"))); +BOOST_AUTO_TEST_CASE(simple_wrap) +{ + wraptype::set_connection(auto_ptr + (new BasicSocketWrapper("./socket"))); - t2n_exec(&cmd_group_x_client::serverfunc)(17); - string out=t2n_exec(&cmd_group_x_client::getserverlog)(); + int i=t2n_exec(&cmd_group_x_client::serverfunc)(1); - // count the number of times that "new connection accepted" appears in the server log - string::size_type p=0; - int cnt=0; - while ((p=out.find("new connection accepted",p))++ != string::npos) - cnt++; + BOOST_CHECK_EQUAL(2,i); +} - CPPUNIT_ASSERT_EQUAL(1,cnt); - } +BOOST_AUTO_TEST_CASE(double_use) +{ + // only one connection used? + wraptype::set_connection(auto_ptr + (new BasicSocketWrapper("./socket"))); - void double_use_with_close() - { - wraptype::set_connection(auto_ptr - (new BasicSocketWrapper("./socket"))); + t2n_exec(&cmd_group_x_client::serverfunc)(17); + string out=t2n_exec(&cmd_group_x_client::getserverlog)(); - t2n_exec(&cmd_group_x_client::serverfunc)(17); + // count the number of times that "new connection accepted" appears in the server log + string::size_type p=0; + int cnt=0; + while ((p=out.find("new connection accepted",p))++ != string::npos) + cnt++; - // closes the connection from the client side - wraptype::set_connection(auto_ptr - (new BasicSocketWrapper("./socket"))); + BOOST_CHECK_EQUAL(1,cnt); +} - string out=t2n_exec(&cmd_group_x_client::getserverlog)(); +BOOST_AUTO_TEST_CASE(double_use_with_close) +{ + wraptype::set_connection(auto_ptr + (new BasicSocketWrapper("./socket"))); - // count the number of times that "new connection accepted" appears in the server log - string::size_type p=0; - int cnt=0; - while ((p=out.find("new connection accepted",p))++ != string::npos) - cnt++; + t2n_exec(&cmd_group_x_client::serverfunc)(17); - CPPUNIT_ASSERT_EQUAL(2,cnt); - } + // closes the connection from the client side + wraptype::set_connection(auto_ptr + (new BasicSocketWrapper("./socket"))); - void reconnect_after_close() - { - wraptype::set_connection(auto_ptr - (new ReconnectSocketWrapper("./socket"))); + string out=t2n_exec(&cmd_group_x_client::getserverlog)(); - wraptype::get_connection_wrapper()->set_command_timeout_usec(3000000); - wraptype::get_connection_wrapper()->set_hello_timeout_usec(3000000); + // count the number of times that "new connection accepted" appears in the server log + string::size_type p=0; + int cnt=0; + while ((p=out.find("new connection accepted",p))++ != string::npos) + cnt++; - // 42 closes connection on the server side - t2n_exec(&cmd_group_x_client::serverfunc)(42); + BOOST_CHECK_EQUAL(2,cnt); +} - string out=t2n_exec(&cmd_group_x_client::getserverlog)(); +BOOST_AUTO_TEST_CASE(reconnect_after_close) +{ + wraptype::set_connection(auto_ptr + (new ReconnectSocketWrapper("./socket"))); - // count the number of times that "new connection accepted" appears in the server log - string::size_type p=0; - int cnt=0; - while ((p=out.find("new connection accepted",p))++ != string::npos) - cnt++; + wraptype::get_connection_wrapper()->set_command_timeout_usec(3000000); + wraptype::get_connection_wrapper()->set_hello_timeout_usec(3000000); - CPPUNIT_ASSERT_EQUAL(2,cnt); - } + // 42 closes connection on the server side + t2n_exec(&cmd_group_x_client::serverfunc)(42); - void reconnect_not_possible() - { - wraptype::set_connection(auto_ptr - (new ReconnectSocketWrapper("./socket"))); + string out=t2n_exec(&cmd_group_x_client::getserverlog)(); - // the server doens't like the beast - t2n_exec(&cmd_group_x_client::serverfunc)(666); + // count the number of times that "new connection accepted" appears in the server log + string::size_type p=0; + int cnt=0; + while ((p=out.find("new connection accepted",p))++ != string::npos) + cnt++; - CPPUNIT_ASSERT_THROW(t2n_exec(&cmd_group_x_client::serverfunc)(1),t2n_communication_error); - } + BOOST_CHECK_EQUAL(2,cnt); +} - void ignore_server_disconnect() - { - wraptype::set_connection(auto_ptr - (new ReconnectIgnoreFailureSocketWrapper("./socket"))); +BOOST_AUTO_TEST_CASE(reconnect_not_possible) +{ + wraptype::set_connection(auto_ptr + (new ReconnectSocketWrapper("./socket"))); - // the server doens't like the beast - t2n_exec(&cmd_group_x_client::serverfunc)(666); + // the server doens't like the beast + t2n_exec(&cmd_group_x_client::serverfunc)(666); - int i=t2n_exec(&cmd_group_x_client::serverfunc)(1); + BOOST_REQUIRE_THROW(t2n_exec(&cmd_group_x_client::serverfunc)(1),t2n_communication_error); +} - // result is constructed with default constructor on error-and-ignore -> i=0 +BOOST_AUTO_TEST_CASE(ignore_server_disconnect) +{ + wraptype::set_connection(auto_ptr + (new ReconnectIgnoreFailureSocketWrapper("./socket"))); - CPPUNIT_ASSERT_EQUAL(0,i); - } + // the server doens't like the beast + t2n_exec(&cmd_group_x_client::serverfunc)(666); - void ignore_handler_reconnects() - { - wraptype::set_connection(auto_ptr - (new ReconnectIgnoreFailureSocketWrapper("./socket"))); + int i=t2n_exec(&cmd_group_x_client::serverfunc)(1); - wraptype::get_connection_wrapper()->set_command_timeout_usec(3000000); - wraptype::get_connection_wrapper()->set_hello_timeout_usec(3000000); + // result is constructed with default constructor on error-and-ignore -> i=0 - // 42 closes connection on the server side - t2n_exec(&cmd_group_x_client::serverfunc)(42); + BOOST_CHECK_EQUAL(0,i); +} - string out=t2n_exec(&cmd_group_x_client::getserverlog)(); +BOOST_AUTO_TEST_CASE(ignore_handler_reconnects) +{ + wraptype::set_connection(auto_ptr + (new ReconnectIgnoreFailureSocketWrapper("./socket"))); - // count the number of times that "new connection accepted" appears in the server log - string::size_type p=0; - int cnt=0; - while ((p=out.find("new connection accepted",p))++ != string::npos) - cnt++; + wraptype::get_connection_wrapper()->set_command_timeout_usec(3000000); + wraptype::get_connection_wrapper()->set_hello_timeout_usec(3000000); - CPPUNIT_ASSERT_EQUAL(2,cnt); - } + // 42 closes connection on the server side + t2n_exec(&cmd_group_x_client::serverfunc)(42); + string out=t2n_exec(&cmd_group_x_client::getserverlog)(); + // count the number of times that "new connection accepted" appears in the server log + string::size_type p=0; + int cnt=0; + while ((p=out.find("new connection accepted",p))++ != string::npos) + cnt++; -}; + BOOST_CHECK_EQUAL(2,cnt); +} -CPPUNIT_TEST_SUITE_REGISTRATION(test_wrapper); +BOOST_AUTO_TEST_SUITE_END() +BOOST_FIXTURE_TEST_SUITE(test_wrapper_noserver, KillChildOnShutdownFixture) -class test_wrapper_noserver : public TestFixture +BOOST_AUTO_TEST_CASE(ignore_noserver) { - CPPUNIT_TEST_SUITE(test_wrapper_noserver); - - CPPUNIT_TEST(ignore_noserver); - CPPUNIT_TEST(ignore_finds_lateserver); - - CPPUNIT_TEST_SUITE_END(); - - public: - - pid_t child_pid; + wraptype::set_connection(auto_ptr + (new ReconnectIgnoreFailureSocketWrapper("./socket"))); - void setUp() - { } + // wraptype::get_connection_wrapper()->set_logging(&cerr,debug); - void tearDown() - { } + // there is no server - void ignore_noserver() - { - wraptype::set_connection(auto_ptr - (new ReconnectIgnoreFailureSocketWrapper("./socket"))); + int i=t2n_exec(&cmd_group_x_client::serverfunc)(1); - // wraptype::get_connection_wrapper()->set_logging(&cerr,debug); + // result is constructed with default constructor on error-and-ignore -> i=0 - // there is no server + BOOST_CHECK_EQUAL(0,i); +} - int i=t2n_exec(&cmd_group_x_client::serverfunc)(1); +BOOST_AUTO_TEST_CASE(ignore_finds_lateserver) +{ + wraptype::set_connection(auto_ptr + (new ReconnectIgnoreFailureSocketWrapper("./socket"))); - // result is constructed with default constructor on error-and-ignore -> i=0 + // there is no server + t2n_exec(&cmd_group_x_client::serverfunc)(1); - CPPUNIT_ASSERT_EQUAL(0,i); - } + // launch a server + close_server=false; + kill_server=false; - void ignore_finds_lateserver() + switch(child_pid=fork()) { - wraptype::set_connection(auto_ptr - (new ReconnectIgnoreFailureSocketWrapper("./socket"))); - - // there is no server - t2n_exec(&cmd_group_x_client::serverfunc)(1); - - // launch a server - - close_server=false; - kill_server=false; - - 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 { int i=0; while(i < 10 && !kill_server) @@ -499,30 +480,84 @@ class test_wrapper_noserver : public TestFixture for (; !close_server && !kill_server && i < 10; i++) cs.handle(1000000); } - - // don't call atexit and stuff - _exit(0); + } catch(...) + { + std::cerr << "exception in child. ignoring\n"; } - default: - // parent + // don't call atexit and stuff + _exit(0); + } + + default: + // parent + { + // wait till server is up + sleep(1); + } + } + + // server should be active + int i=t2n_exec(&cmd_group_x_client::serverfunc)(1); + + BOOST_CHECK_EQUAL(2,i); +} + +BOOST_AUTO_TEST_CASE(ignore_wrongserver) +{ + wraptype::set_connection(auto_ptr + (new ReconnectIgnoreFailureSocketWrapper("./socket"))); + + // launch a server + + switch(child_pid=fork()) + { + case -1: + { + BOOST_FAIL("fork error"); + break; + } + case 0: + // child + { + try { - // wait till server is up - sleep(1); + socket_server ss("./socket"); + + // server sends garbage + + ostringstream hello; + hello << "XYZ 123"; + + ss.add_callback(new_connection,bind(&test_wrapper_noserver::ignore_wrongserver::send_hello, boost::ref(*this), hello.str(),&ss, _1)); + + // max 10 sec + for (int i=0; i < 10; i++) + ss.fill_buffer(1000000); + } catch(...) + { + std::cerr << "exception in child. ignoring\n"; } + + // don't call atexit and stuff + _exit(0); } - // server should be active - int i=t2n_exec(&cmd_group_x_client::serverfunc)(1); + default: + // parent + { + // wait till server is up + sleep(1); + } + } - CPPUNIT_ASSERT_EQUAL(2,i); + // there is no valid server - // make sure the server-child is dead before the next test runs - kill(child_pid,SIGKILL); - sleep(1); - } + int i=t2n_exec(&cmd_group_x_client::serverfunc)(1); + // result is constructed with default constructor on error-and-ignore -> i=0 -}; + BOOST_CHECK_EQUAL(0,i); +} -CPPUNIT_TEST_SUITE_REGISTRATION(test_wrapper_noserver); +BOOST_AUTO_TEST_SUITE_END()