X-Git-Url: http://developer.intra2net.com/git/?p=libt2n;a=blobdiff_plain;f=test%2Fhello.cpp;h=a7a885dc9d5b75e7326ec45a2c965fca82418d23;hp=a0cc532aed892e9bb54bfaf99b7e69c30651809e;hb=a63e08b83794273da3840e0b8bf15bf0085fe3c4;hpb=04d86ba4ad4f14ab08f38804e772ec46a8ac92b0 diff --git a/test/hello.cpp b/test/hello.cpp index a0cc532..a7a885d 100644 --- a/test/hello.cpp +++ b/test/hello.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 @@ -17,80 +32,37 @@ #include -#include -#include -#include +#define BOOST_TEST_DYN_LINK +#include #include #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); } -}; - -class test_hello : public TestFixture -{ - CPPUNIT_TEST_SUITE(test_hello); - - CPPUNIT_TEST(HelloOk); - CPPUNIT_TEST(BadTag); - CPPUNIT_TEST(BadVersion); - CPPUNIT_TEST(SeparatorMissing); - CPPUNIT_TEST(WrongByteOrder); - CPPUNIT_TEST(OtherServer); - - CPPUNIT_TEST_SUITE_END(); - - public: - - void setUp() - { } - - void tearDown() - { } - - void send_hello(string hello_string, socket_server* ss, int conn_id) - { - server_connection *sc=ss->get_connection(conn_id); - sc->write(hello_string); - } - 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); - } +BOOST_FIXTURE_TEST_SUITE(test_hello, KillChildOnShutdownFixture) - void HelloOk() +BOOST_AUTO_TEST_CASE(HelloOk) +{ + switch(child_pid=fork()) { - pid_t pid; - - switch(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"); @@ -100,94 +72,104 @@ class test_hello : public TestFixture hello.write((char*)&byteordercheck,sizeof(byteordercheck)); hello << ';'; - ss.add_callback(new_connection,bind(&test_hello::send_hello, boost::ref(*this), hello.str(),&ss, _1)); + ss.add_callback(new_connection,bind(&test_hello::HelloOk::send_hello, boost::ref(*this), hello.str(),&ss, _1)); // max 10 sec for (int i=0; i < 10; i++) ss.fill_buffer(1000000); - // don't call atexit and stuff - _exit(0); + } catch(...) + { + std::cerr << "exception in child. ignoring\n"; } - default: - // parent - { - string data; + // don't call atexit and stuff + _exit(0); + } - // wait till server is up - sleep(1); - socket_client_connection sc("./socket"); - command_client cc(sc); - } + default: + // parent + { + string data; + + // wait till server is up + sleep(1); + socket_client_connection sc("./socket"); + command_client cc(&sc); + + // All fine we reached this point + BOOST_CHECK(true); } } +} - void BadTag() +BOOST_AUTO_TEST_CASE(BadTag) +{ + switch(child_pid=fork()) { - pid_t pid; - - switch(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"); ostringstream hello; hello << "XYZ 123"; - ss.add_callback(new_connection,bind(&test_hello::send_hello, boost::ref(*this), hello.str(),&ss, _1)); + ss.add_callback(new_connection,bind(&test_hello::BadTag::send_hello, boost::ref(*this), hello.str(),&ss, _1)); // max 10 sec for (int i=0; i < 10; i++) ss.fill_buffer(1000000); - // don't call atexit and stuff - _exit(0); + } catch(...) + { + std::cerr << "exception in child. ignoring\n"; } - default: - // parent - { - string data; + // don't call atexit and stuff + _exit(0); + } + + default: + // parent + { + string data; - // wait till server is up - sleep(1); - socket_client_connection sc("./socket"); + // wait till server is up + sleep(1); + socket_client_connection sc("./socket"); - string errormsg; + command_client cc(&sc); - try - { - command_client cc(sc); - } - catch(t2n_version_mismatch &e) - { errormsg=e.what(); } - catch(...) - { throw; } + t2n_exception* ep=cc.get_constuctor_exception(); - CPPUNIT_ASSERT_EQUAL(string("illegal hello received (T2N)"),errormsg); - } + string errormsg; + if (ep) + errormsg=ep->what(); + + BOOST_CHECK_EQUAL(string("illegal hello received (T2N)"),errormsg); } } +} - void BadVersion() +BOOST_AUTO_TEST_CASE(BadVersion) +{ + switch(child_pid=fork()) { - pid_t pid; - - switch(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"); @@ -198,53 +180,55 @@ class test_hello : public TestFixture hello.write((char*)&byteordercheck,sizeof(byteordercheck)); hello << ';'; - ss.add_callback(new_connection,bind(&test_hello::send_hello, boost::ref(*this), hello.str(),&ss, _1)); + ss.add_callback(new_connection,bind(&test_hello::BadVersion::send_hello, boost::ref(*this), hello.str(),&ss, _1)); // max 10 sec for (int i=0; i < 10; i++) ss.fill_buffer(1000000); - // don't call atexit and stuff - _exit(0); + } catch(...) + { + std::cerr << "exception in child. ignoring\n"; } - default: - // parent - { - string data; + // don't call atexit and stuff + _exit(0); + } - // wait till server is up - sleep(1); - socket_client_connection sc("./socket"); + default: + // parent + { + string data; - string errormsg; + // wait till server is up + sleep(1); + socket_client_connection sc("./socket"); - try - { - command_client cc(sc); - } - catch(t2n_version_mismatch &e) - { errormsg=e.what(); } - catch(...) - { throw; } + command_client cc(&sc); - CPPUNIT_ASSERT_EQUAL(string("not compatible with the server protocol version"),errormsg); - } + t2n_exception* ep=cc.get_constuctor_exception(); + + string errormsg; + if (ep) + errormsg=ep->what(); + + BOOST_CHECK_EQUAL(string("not compatible with the server protocol version"),errormsg); } } +} - void SeparatorMissing() +BOOST_AUTO_TEST_CASE(SeparatorMissing) +{ + switch(child_pid=fork()) { - pid_t pid; - - switch(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"); @@ -254,53 +238,55 @@ class test_hello : public TestFixture hello.write((char*)&byteordercheck,sizeof(byteordercheck)); hello << ';'; - ss.add_callback(new_connection,bind(&test_hello::send_hello, boost::ref(*this), hello.str(),&ss, _1)); + ss.add_callback(new_connection,bind(&test_hello::SeparatorMissing::send_hello, boost::ref(*this), hello.str(),&ss, _1)); // max 10 sec for (int i=0; i < 10; i++) ss.fill_buffer(1000000); - // don't call atexit and stuff - _exit(0); + } catch(...) + { + std::cerr << "exception in child. ignoring\n"; } - default: - // parent - { - string data; + // don't call atexit and stuff + _exit(0); + } - // wait till server is up - sleep(1); - socket_client_connection sc("./socket"); + default: + // parent + { + string data; - string errormsg; + // wait till server is up + sleep(1); + socket_client_connection sc("./socket"); - try - { - command_client cc(sc); - } - catch(t2n_version_mismatch &e) - { errormsg=e.what(); } - catch(...) - { throw; } + command_client cc(&sc); - CPPUNIT_ASSERT_EQUAL(string("illegal hello received (1. ;)"),errormsg); - } + t2n_exception* ep=cc.get_constuctor_exception(); + + string errormsg; + if (ep) + errormsg=ep->what(); + + BOOST_CHECK_EQUAL(string("illegal hello received (1. ;)"),errormsg); } } +} - void WrongByteOrder() +BOOST_AUTO_TEST_CASE(WrongByteOrder) +{ + switch(child_pid=fork()) { - pid_t pid; - - switch(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"); @@ -319,53 +305,55 @@ class test_hello : public TestFixture hello.write((char*)&dst,sizeof(dst)); hello << ';'; - ss.add_callback(new_connection,bind(&test_hello::send_hello, boost::ref(*this), hello.str(),&ss, _1)); + ss.add_callback(new_connection,bind(&test_hello::WrongByteOrder::send_hello, boost::ref(*this), hello.str(),&ss, _1)); // max 10 sec for (int i=0; i < 10; i++) ss.fill_buffer(1000000); - // don't call atexit and stuff - _exit(0); + } catch(...) + { + std::cerr << "exception in child. ignoring\n"; } - default: - // parent - { - string data; + // don't call atexit and stuff + _exit(0); + } + + default: + // parent + { + string data; - // wait till server is up - sleep(1); - socket_client_connection sc("./socket"); + // wait till server is up + sleep(1); + socket_client_connection sc("./socket"); - string errormsg; + command_client cc(&sc); - try - { - command_client cc(sc); - } - catch(t2n_version_mismatch &e) - { errormsg=e.what(); } - catch(...) - { throw; } + t2n_exception* ep=cc.get_constuctor_exception(); - CPPUNIT_ASSERT_EQUAL(string("host byte order not matching"),errormsg); - } + string errormsg; + if (ep) + errormsg=ep->what(); + + BOOST_CHECK_EQUAL(string("host byte order not matching"),errormsg); } } +} - void OtherServer() +BOOST_AUTO_TEST_CASE(OtherServerBig) +{ + switch(child_pid=fork()) { - pid_t pid; - - switch(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"); @@ -373,40 +361,96 @@ class test_hello : public TestFixture // hmm, we got the wrong socket hello << "* OK intradev.net.lan Cyrus IMAP4 v2.2.13 server ready"; - ss.add_callback(new_connection,bind(&test_hello::send_raw_socket, boost::ref(*this), hello.str(),&ss, _1)); + ss.add_callback(new_connection,bind(&test_hello::OtherServerBig::send_raw_socket, boost::ref(*this), hello.str(),&ss, _1)); // max 3 sec for (int i=0; i < 3; i++) ss.fill_buffer(1000000); - // don't call atexit and stuff - _exit(0); + } catch(...) + { + std::cerr << "exception in child. ignoring\n"; } - default: - // parent - { - string data; + // don't call atexit and stuff + _exit(0); + } + + default: + // parent + { + string data; - // wait till server is up - sleep(1); - socket_client_connection sc("./socket"); + // wait till server is up + sleep(1); + socket_client_connection sc("./socket"); - string errormsg; + command_client cc(&sc); - try - { - command_client cc(sc); - } - catch(t2n_version_mismatch &e) - { errormsg=e.what(); } - catch(...) - { throw; } + t2n_exception* ep=cc.get_constuctor_exception(); - CPPUNIT_ASSERT_EQUAL(string("illegal hello received (T2N)"),errormsg); - } + string errormsg; + if (ep) + errormsg=ep->what(); + + BOOST_CHECK_EQUAL(string("illegal hello received (T2N)"),errormsg); } } +} + +BOOST_AUTO_TEST_CASE(OtherServerSmall) +{ + switch(child_pid=fork()) + { + case -1: + { + BOOST_FAIL("fork error"); + break; + } + case 0: + // child + { + try + { + socket_server ss("./socket"); + + ostringstream hello; + // hmm, we got the wrong socket + hello << "READY"; -}; + ss.add_callback(new_connection,bind(&test_hello::OtherServerSmall::send_raw_socket, boost::ref(*this), hello.str(),&ss, _1)); + + // max 3 sec + for (int i=0; i < 3; i++) + ss.fill_buffer(1000000); + } catch(...) + { + std::cerr << "exception in child. ignoring\n"; + } + + // don't call atexit and stuff + _exit(0); + } + + default: + // parent + { + string data; + + // wait till server is up + sleep(1); + socket_client_connection sc("./socket"); + + command_client cc(&sc); + + t2n_exception* ep=cc.get_constuctor_exception(); + + string errormsg; + if (ep) + errormsg=ep->what(); + + BOOST_CHECK_EQUAL(string("illegal hello received (T2N)"),errormsg); + } + } +} -CPPUNIT_TEST_SUITE_REGISTRATION(test_hello); +BOOST_AUTO_TEST_SUITE_END()