X-Git-Url: http://developer.intra2net.com/git/?p=libt2n;a=blobdiff_plain;f=test%2Fhello.cpp;h=302fb482bbb90cced6518852efd44da36c3f8c98;hp=80bfec6814a687031f121cf194118d78b8387c6b;hb=276fd05261cb316a260784ec34db6da699e594de;hpb=b604df5f671b5e7abfd37570d76927a6ebd45f98 diff --git a/test/hello.cpp b/test/hello.cpp index 80bfec6..302fb48 100644 --- a/test/hello.cpp +++ b/test/hello.cpp @@ -56,13 +56,19 @@ class test_hello : public TestFixture 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); + } void send_hello(string hello_string, socket_server* ss, int conn_id) { @@ -81,9 +87,7 @@ class test_hello : public TestFixture void HelloOk() { - pid_t pid; - - switch(pid=fork()) + switch(child_pid=fork()) { case -1: { @@ -93,19 +97,26 @@ class test_hello : public TestFixture case 0: // child { - socket_server ss("./socket"); + try + { + socket_server ss("./socket"); - ostringstream hello; - hello << "T2Nv" << PROTOCOL_VERSION << ';'; - int byteordercheck=1; - hello.write((char*)&byteordercheck,sizeof(byteordercheck)); - hello << ';'; + ostringstream hello; + hello << "T2Nv" << PROTOCOL_VERSION << ';'; + int byteordercheck=1; + 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::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"; + } - // max 10 sec - for (int i=0; i < 10; i++) - ss.fill_buffer(1000000); // don't call atexit and stuff _exit(0); } @@ -118,16 +129,14 @@ class test_hello : public TestFixture // wait till server is up sleep(1); socket_client_connection sc("./socket"); - command_client cc(sc); + command_client cc(&sc); } } } void BadTag() { - pid_t pid; - - switch(pid=fork()) + switch(child_pid=fork()) { case -1: { @@ -137,16 +146,23 @@ class test_hello : public TestFixture case 0: // child { - socket_server ss("./socket"); + try + { + socket_server ss("./socket"); + + ostringstream hello; + hello << "XYZ 123"; - 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::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"; + } - // max 10 sec - for (int i=0; i < 10; i++) - ss.fill_buffer(1000000); // don't call atexit and stuff _exit(0); } @@ -160,16 +176,13 @@ class test_hello : public TestFixture 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(); + + string errormsg; + if (ep) + errormsg=ep->what(); CPPUNIT_ASSERT_EQUAL(string("illegal hello received (T2N)"),errormsg); } @@ -178,9 +191,7 @@ class test_hello : public TestFixture void BadVersion() { - pid_t pid; - - switch(pid=fork()) + switch(child_pid=fork()) { case -1: { @@ -190,20 +201,27 @@ class test_hello : public TestFixture case 0: // child { - socket_server ss("./socket"); + try + { + socket_server ss("./socket"); + + ostringstream hello; + // lets hope we don't ever get near such a version number... + hello << "T2Nv" << 4982271 << ';'; + int byteordercheck=1; + hello.write((char*)&byteordercheck,sizeof(byteordercheck)); + hello << ';'; - ostringstream hello; - // lets hope we don't ever get near such a version number... - hello << "T2Nv" << 4982271 << ';'; - int byteordercheck=1; - 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::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"; + } - // max 10 sec - for (int i=0; i < 10; i++) - ss.fill_buffer(1000000); // don't call atexit and stuff _exit(0); } @@ -217,16 +235,13 @@ class test_hello : public TestFixture 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(); + + string errormsg; + if (ep) + errormsg=ep->what(); CPPUNIT_ASSERT_EQUAL(string("not compatible with the server protocol version"),errormsg); } @@ -235,9 +250,7 @@ class test_hello : public TestFixture void SeparatorMissing() { - pid_t pid; - - switch(pid=fork()) + switch(child_pid=fork()) { case -1: { @@ -247,19 +260,26 @@ class test_hello : public TestFixture case 0: // child { - socket_server ss("./socket"); + try + { + socket_server ss("./socket"); - ostringstream hello; - hello << "T2Nv" << PROTOCOL_VERSION; - int byteordercheck=1; - hello.write((char*)&byteordercheck,sizeof(byteordercheck)); - hello << ';'; + ostringstream hello; + hello << "T2Nv" << PROTOCOL_VERSION; + int byteordercheck=1; + 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::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"; + } - // max 10 sec - for (int i=0; i < 10; i++) - ss.fill_buffer(1000000); // don't call atexit and stuff _exit(0); } @@ -273,16 +293,13 @@ class test_hello : public TestFixture 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(); + + string errormsg; + if (ep) + errormsg=ep->what(); CPPUNIT_ASSERT_EQUAL(string("illegal hello received (1. ;)"),errormsg); } @@ -291,9 +308,7 @@ class test_hello : public TestFixture void WrongByteOrder() { - pid_t pid; - - switch(pid=fork()) + switch(child_pid=fork()) { case -1: { @@ -303,28 +318,35 @@ class test_hello : public TestFixture case 0: // child { - socket_server ss("./socket"); + try + { + socket_server ss("./socket"); + + ostringstream hello; + hello << "T2Nv" << PROTOCOL_VERSION << ';'; + int byteordercheck=1; + int dst; + char* si=(char*)&byteordercheck; + char* di=(char*)&dst; - ostringstream hello; - hello << "T2Nv" << PROTOCOL_VERSION << ';'; - int byteordercheck=1; - int dst; - char* si=(char*)&byteordercheck; - char* di=(char*)&dst; + di[0]=si[3]; + di[1]=si[2]; + di[2]=si[1]; + di[3]=si[0]; - di[0]=si[3]; - di[1]=si[2]; - di[2]=si[1]; - di[3]=si[0]; + hello.write((char*)&dst,sizeof(dst)); + hello << ';'; - 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::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"; + } - // max 10 sec - for (int i=0; i < 10; i++) - ss.fill_buffer(1000000); // don't call atexit and stuff _exit(0); } @@ -338,16 +360,13 @@ class test_hello : public TestFixture 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(); + + string errormsg; + if (ep) + errormsg=ep->what(); CPPUNIT_ASSERT_EQUAL(string("host byte order not matching"),errormsg); } @@ -356,9 +375,7 @@ class test_hello : public TestFixture void OtherServerBig() { - pid_t pid; - - switch(pid=fork()) + switch(child_pid=fork()) { case -1: { @@ -368,17 +385,24 @@ class test_hello : public TestFixture case 0: // child { - socket_server ss("./socket"); + try + { + socket_server ss("./socket"); - ostringstream hello; - // hmm, we got the wrong socket - hello << "* OK intradev.net.lan Cyrus IMAP4 v2.2.13 server ready"; + ostringstream hello; + // 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::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"; + } - // max 3 sec - for (int i=0; i < 3; i++) - ss.fill_buffer(1000000); // don't call atexit and stuff _exit(0); } @@ -392,16 +416,13 @@ class test_hello : public TestFixture 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(); + + string errormsg; + if (ep) + errormsg=ep->what(); CPPUNIT_ASSERT_EQUAL(string("illegal hello received (T2N)"),errormsg); } @@ -410,9 +431,7 @@ class test_hello : public TestFixture void OtherServerSmall() { - pid_t pid; - - switch(pid=fork()) + switch(child_pid=fork()) { case -1: { @@ -422,17 +441,24 @@ class test_hello : public TestFixture case 0: // child { - socket_server ss("./socket"); + try + { + socket_server ss("./socket"); - ostringstream hello; - // hmm, we got the wrong socket - hello << "READY"; + ostringstream hello; + // hmm, we got the wrong socket + hello << "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::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"; + } - // max 3 sec - for (int i=0; i < 3; i++) - ss.fill_buffer(1000000); // don't call atexit and stuff _exit(0); } @@ -446,16 +472,13 @@ class test_hello : public TestFixture 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(); + + string errormsg; + if (ep) + errormsg=ep->what(); CPPUNIT_ASSERT_EQUAL(string("illegal hello received (T2N)"),errormsg); }