Remove code duplication in test fixtures
[libt2n] / test / hello.cpp
index 2f804fd..6bce679 100644 (file)
@@ -32,474 +32,427 @@ on this file might be covered by the GNU General Public License.
 
 #include <boost/bind.hpp>
 
-#include <cppunit/extensions/TestFactoryRegistry.h>
-#include <cppunit/ui/text/TestRunner.h>
-#include <cppunit/extensions/HelperMacros.h>
+#define BOOST_TEST_DYN_LINK
+#include <boost/test/unit_test.hpp>
 
 #include <t2n_exception.hxx>
 #include <socket_client.hxx>
 #include <socket_server.hxx>
 #include <command_client.hxx>
 
+#include "test_fixtures.hxx"
+
 #ifdef HAVE_CONFIG_H
 #include <config.h>
 #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(OtherServerBig);
-    CPPUNIT_TEST(OtherServerSmall);
-
-    CPPUNIT_TEST_SUITE_END();
 
-    pid_t child_pid;
 
-    public:
+BOOST_FIXTURE_TEST_SUITE(test_hello, KillChildOnShutdownFixture)
 
-    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)
+BOOST_AUTO_TEST_CASE(HelloOk)
+{
+    switch(child_pid=fork())
     {
-        server_connection *sc=ss->get_connection(conn_id);
-        sc->write(hello_string);
-    }
+        case -1:
+        {
+            BOOST_FAIL("fork error");
+            break;
+        }
+        case 0:
+        // child
+        {
+            try
+            {
+                socket_server ss("./socket");
 
-    void send_raw_socket(string hello_string, socket_server* ss, int conn_id)
-    {
-        socket_server_connection *ssc=dynamic_cast<socket_server_connection*>(ss->get_connection(conn_id));
+                ostringstream hello;
+                hello << "T2Nv" << PROTOCOL_VERSION << ';';
+                int byteordercheck=1;
+                hello.write((char*)&byteordercheck,sizeof(byteordercheck));
+                hello << ';';
 
-        // 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);
-    }
+                ss.add_callback(new_connection,bind(&test_hello::HelloOk::send_hello, boost::ref(*this), hello.str(),&ss, _1));
 
-    void HelloOk()
-    {
-        switch(child_pid=fork())
-        {
-            case -1:
-            {
-                CPPUNIT_FAIL("fork error");
-                break;
-            }
-            case 0:
-            // child
+                // max 10 sec
+                for (int i=0; i < 10; i++)
+                    ss.fill_buffer(1000000);
+            } catch(...)
             {
-                try
-                {
-                    socket_server ss("./socket");
-
-                    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));
-
-                    // 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);
+                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())
     {
-        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");
-
-                    ostringstream hello;
-                    hello << "XYZ 123";
+                socket_server ss("./socket");
 
-                    ss.add_callback(new_connection,bind(&test_hello::send_hello, boost::ref(*this), hello.str(),&ss, _1));
+                ostringstream hello;
+                hello << "XYZ 123";
 
-                    // max 10 sec
-                    for (int i=0; i < 10; i++)
-                        ss.fill_buffer(1000000);
-                } catch(...)
-                {
-                    std::cerr << "exception in child. ignoring\n";
-                }
+                ss.add_callback(new_connection,bind(&test_hello::BadTag::send_hello, boost::ref(*this), hello.str(),&ss, _1));
 
-                // don't call atexit and stuff
-                _exit(0);
+                // max 10 sec
+                for (int i=0; i < 10; i++)
+                    ss.fill_buffer(1000000);
+            } 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");
 
-                command_client cc(&sc);
+            command_client cc(&sc);
 
-                t2n_exception* ep=cc.get_constuctor_exception();
+            t2n_exception* ep=cc.get_constuctor_exception();
 
-                string errormsg;
-                if (ep)
-                    errormsg=ep->what();
+            string errormsg;
+            if (ep)
+                errormsg=ep->what();
 
-                CPPUNIT_ASSERT_EQUAL(string("illegal hello received (T2N)"),errormsg);
-            }
+            BOOST_CHECK_EQUAL(string("illegal hello received (T2N)"),errormsg);
         }
     }
+}
 
-    void BadVersion()
+BOOST_AUTO_TEST_CASE(BadVersion)
+{
+    switch(child_pid=fork())
     {
-        switch(child_pid=fork())
+        case -1:
         {
-            case -1:
+            BOOST_FAIL("fork error");
+            break;
+        }
+        case 0:
+        // child
+        {
+            try
             {
-                CPPUNIT_FAIL("fork error");
-                break;
-            }
-            case 0:
-            // child
+                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 << ';';
+
+                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);
+            } catch(...)
             {
-                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 << ';';
-
-                    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";
-                }
-
-                // don't call atexit and stuff
-                _exit(0);
+                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;
 
-                command_client cc(&sc);
+            // wait till server is up
+            sleep(1);
+            socket_client_connection sc("./socket");
 
-                t2n_exception* ep=cc.get_constuctor_exception();
+            command_client cc(&sc);
 
-                string errormsg;
-                if (ep)
-                    errormsg=ep->what();
+            t2n_exception* ep=cc.get_constuctor_exception();
 
-                CPPUNIT_ASSERT_EQUAL(string("not compatible with the server protocol version"),errormsg);
-            }
+            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())
     {
-        switch(child_pid=fork())
+        case -1:
+        {
+            BOOST_FAIL("fork error");
+            break;
+        }
+        case 0:
+        // child
         {
-            case -1:
+            try
             {
-                CPPUNIT_FAIL("fork error");
-                break;
-            }
-            case 0:
-            // child
+                socket_server ss("./socket");
+
+                ostringstream hello;
+                hello << "T2Nv" << PROTOCOL_VERSION;
+                int byteordercheck=1;
+                hello.write((char*)&byteordercheck,sizeof(byteordercheck));
+                hello << ';';
+
+                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);
+            } catch(...)
             {
-                try
-                {
-                    socket_server ss("./socket");
-
-                    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));
-
-                    // 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);
+                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;
 
-                command_client cc(&sc);
+            // wait till server is up
+            sleep(1);
+            socket_client_connection sc("./socket");
 
-                t2n_exception* ep=cc.get_constuctor_exception();
+            command_client cc(&sc);
 
-                string errormsg;
-                if (ep)
-                    errormsg=ep->what();
+            t2n_exception* ep=cc.get_constuctor_exception();
 
-                CPPUNIT_ASSERT_EQUAL(string("illegal hello received (1. ;)"),errormsg);
-            }
+            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())
     {
-        switch(child_pid=fork())
+        case -1:
+        {
+            BOOST_FAIL("fork error");
+            break;
+        }
+        case 0:
+        // child
         {
-            case -1:
+            try
             {
-                CPPUNIT_FAIL("fork error");
-                break;
-            }
-            case 0:
-            // child
+                socket_server ss("./socket");
+
+                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];
+
+                hello.write((char*)&dst,sizeof(dst));
+                hello << ';';
+
+                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);
+            } catch(...)
             {
-                try
-                {
-                    socket_server ss("./socket");
-
-                    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];
-
-                    hello.write((char*)&dst,sizeof(dst));
-                    hello << ';';
-
-                    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";
-                }
-
-                // don't call atexit and stuff
-                _exit(0);
+                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;
 
-                command_client cc(&sc);
+            // wait till server is up
+            sleep(1);
+            socket_client_connection sc("./socket");
 
-                t2n_exception* ep=cc.get_constuctor_exception();
+            command_client cc(&sc);
 
-                string errormsg;
-                if (ep)
-                    errormsg=ep->what();
+            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 OtherServerBig()
+BOOST_AUTO_TEST_CASE(OtherServerBig)
+{
+    switch(child_pid=fork())
     {
-        switch(child_pid=fork())
+        case -1:
+        {
+            BOOST_FAIL("fork error");
+            break;
+        }
+        case 0:
+        // child
         {
-            case -1:
+            try
             {
-                CPPUNIT_FAIL("fork error");
-                break;
-            }
-            case 0:
-            // child
+                socket_server ss("./socket");
+
+                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::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);
+            } catch(...)
             {
-                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";
-
-                    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";
-                }
-
-                // don't call atexit and stuff
-                _exit(0);
+                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;
 
-                command_client cc(&sc);
+            // wait till server is up
+            sleep(1);
+            socket_client_connection sc("./socket");
 
-                t2n_exception* ep=cc.get_constuctor_exception();
+            command_client cc(&sc);
 
-                string errormsg;
-                if (ep)
-                    errormsg=ep->what();
+            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 OtherServerSmall()
+BOOST_AUTO_TEST_CASE(OtherServerSmall)
+{
+    switch(child_pid=fork())
     {
-        switch(child_pid=fork())
+        case -1:
+        {
+            BOOST_FAIL("fork error");
+            break;
+        }
+        case 0:
+        // child
         {
-            case -1:
+            try
             {
-                CPPUNIT_FAIL("fork error");
-                break;
-            }
-            case 0:
-            // child
+                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(...)
             {
-                try
-                {
-                    socket_server ss("./socket");
-
-                    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));
-
-                    // 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);
+                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");
 
-                command_client cc(&sc);
+            command_client cc(&sc);
 
-                t2n_exception* ep=cc.get_constuctor_exception();
+            t2n_exception* ep=cc.get_constuctor_exception();
 
-                string errormsg;
-                if (ep)
-                    errormsg=ep->what();
+            string errormsg;
+            if (ep)
+                errormsg=ep->what();
 
-                CPPUNIT_ASSERT_EQUAL(string("illegal hello received (T2N)"),errormsg);
-            }
+            BOOST_CHECK_EQUAL(string("illegal hello received (T2N)"),errormsg);
         }
     }
+}
 
-};
-
-CPPUNIT_TEST_SUITE_REGISTRATION(test_hello);
+BOOST_AUTO_TEST_SUITE_END()