Fix 'occurred' typo
[libt2n] / test / wrapper.cpp
index 49041d8..5fedaf5 100644 (file)
@@ -30,9 +30,8 @@ on this file might be covered by the GNU General Public License.
 #include <sstream>
 #include <stdexcept>
 
-#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 <boost/archive/binary_oarchive.hpp>
 #include <boost/archive/binary_iarchive.hpp>
@@ -49,13 +48,12 @@ on this file might be covered by the GNU General Public License.
 #include <client_wrapper.hxx>
 #include <socket_wrapper.hxx>
 
-#ifdef HAVE_CONFIG_H
+#include "test_fixtures.hxx"
+
 #include <config.h>
-#endif
 
 using namespace std;
 using namespace libt2n;
-using namespace CppUnit;
 
 // the server part
 
@@ -238,26 +236,11 @@ std::auto_ptr<wraptype> wraptype::SingletonObject = std::auto_ptr<wraptype>();
 template<>
 std::auto_ptr<ConnectionWrapper> wraptype::WrappedConnection = std::auto_ptr<ConnectionWrapper>();
 
-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);
-
-    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;
@@ -266,7 +249,7 @@ class test_wrapper : public TestFixture
         {
             case -1:
             {
-                CPPUNIT_FAIL("fork error");
+                BOOST_FAIL("fork error");
                 break;
             }
             case 0:
@@ -299,319 +282,282 @@ 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);
-    }
+BOOST_FIXTURE_TEST_SUITE(test_wrapper, test_wrapperFixture)
 
-    void simple_wrap()
-    {
-        wraptype::set_connection(auto_ptr<ConnectionWrapper>
-            (new BasicSocketWrapper("./socket")));
+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);
+}
 
-        int i=t2n_exec(&cmd_group_x_client::serverfunc)(1);
+BOOST_AUTO_TEST_CASE(simple_wrap)
+{
+    wraptype::set_connection(auto_ptr<ConnectionWrapper>
+        (new BasicSocketWrapper("./socket")));
 
-        CPPUNIT_ASSERT_EQUAL(2,i);
-    }
+    int i=t2n_exec(&cmd_group_x_client::serverfunc)(1);
 
-    void double_use()
-    {
-        // only one connection used?
-        wraptype::set_connection(auto_ptr<ConnectionWrapper>
-            (new BasicSocketWrapper("./socket")));
+    BOOST_CHECK_EQUAL(2,i);
+}
 
-        t2n_exec(&cmd_group_x_client::serverfunc)(17);
-        string out=t2n_exec(&cmd_group_x_client::getserverlog)();
+BOOST_AUTO_TEST_CASE(double_use)
+{
+    // only one connection used?
+    wraptype::set_connection(auto_ptr<ConnectionWrapper>
+        (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);
+    string out=t2n_exec(&cmd_group_x_client::getserverlog)();
 
-        CPPUNIT_ASSERT_EQUAL(1,cnt);
-    }
+    // 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++;
 
-    void double_use_with_close()
-    {
-        wraptype::set_connection(auto_ptr<ConnectionWrapper>
-            (new BasicSocketWrapper("./socket")));
+    BOOST_CHECK_EQUAL(1,cnt);
+}
 
-        t2n_exec(&cmd_group_x_client::serverfunc)(17);
+BOOST_AUTO_TEST_CASE(double_use_with_close)
+{
+    wraptype::set_connection(auto_ptr<ConnectionWrapper>
+        (new BasicSocketWrapper("./socket")));
 
-        // closes the connection from the client side
-        wraptype::set_connection(auto_ptr<ConnectionWrapper>
-            (new BasicSocketWrapper("./socket")));
+    t2n_exec(&cmd_group_x_client::serverfunc)(17);
 
-        string out=t2n_exec(&cmd_group_x_client::getserverlog)();
+    // closes the connection from the client side
+    wraptype::set_connection(auto_ptr<ConnectionWrapper>
+        (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++;
+    string out=t2n_exec(&cmd_group_x_client::getserverlog)();
 
-        CPPUNIT_ASSERT_EQUAL(2,cnt);
-    }
+    // 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++;
 
-    void reconnect_after_close()
-    {
-        wraptype::set_connection(auto_ptr<ConnectionWrapper>
-            (new ReconnectSocketWrapper("./socket")));
+    BOOST_CHECK_EQUAL(2,cnt);
+}
 
-        wraptype::get_connection_wrapper()->set_command_timeout_usec(3000000);
-        wraptype::get_connection_wrapper()->set_hello_timeout_usec(3000000);
+BOOST_AUTO_TEST_CASE(reconnect_after_close)
+{
+    wraptype::set_connection(auto_ptr<ConnectionWrapper>
+        (new ReconnectSocketWrapper("./socket")));
 
-        // 42 closes connection on the server side
-        t2n_exec(&cmd_group_x_client::serverfunc)(42);
+    wraptype::get_connection_wrapper()->set_command_timeout_usec(3000000);
+    wraptype::get_connection_wrapper()->set_hello_timeout_usec(3000000);
 
-        string out=t2n_exec(&cmd_group_x_client::getserverlog)();
+    // 42 closes connection on the server side
+    t2n_exec(&cmd_group_x_client::serverfunc)(42);
 
-        // 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++;
+    string out=t2n_exec(&cmd_group_x_client::getserverlog)();
 
-        CPPUNIT_ASSERT_EQUAL(2,cnt);
-    }
+    // 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++;
 
-    void reconnect_not_possible()
-    {
-        wraptype::set_connection(auto_ptr<ConnectionWrapper>
-            (new ReconnectSocketWrapper("./socket")));
+    BOOST_CHECK_EQUAL(2,cnt);
+}
 
-        // the server doens't like the beast
-        t2n_exec(&cmd_group_x_client::serverfunc)(666);
+BOOST_AUTO_TEST_CASE(reconnect_not_possible)
+{
+    wraptype::set_connection(auto_ptr<ConnectionWrapper>
+        (new ReconnectSocketWrapper("./socket")));
 
-        CPPUNIT_ASSERT_THROW(t2n_exec(&cmd_group_x_client::serverfunc)(1),t2n_communication_error);
-    }
+    // the server doens't like the beast
+    t2n_exec(&cmd_group_x_client::serverfunc)(666);
 
-    void ignore_server_disconnect()
-    {
-        wraptype::set_connection(auto_ptr<ConnectionWrapper>
-            (new ReconnectIgnoreFailureSocketWrapper("./socket")));
+    BOOST_REQUIRE_THROW(t2n_exec(&cmd_group_x_client::serverfunc)(1),t2n_communication_error);
+}
 
-        // the server doens't like the beast
-        t2n_exec(&cmd_group_x_client::serverfunc)(666);
+BOOST_AUTO_TEST_CASE(ignore_server_disconnect)
+{
+    wraptype::set_connection(auto_ptr<ConnectionWrapper>
+        (new ReconnectIgnoreFailureSocketWrapper("./socket")));
 
-        int i=t2n_exec(&cmd_group_x_client::serverfunc)(1);
+    // the server doens't like the beast
+    t2n_exec(&cmd_group_x_client::serverfunc)(666);
 
-        // result is constructed with default constructor on error-and-ignore  -> i=0
+    int i=t2n_exec(&cmd_group_x_client::serverfunc)(1);
 
-        CPPUNIT_ASSERT_EQUAL(0,i);
-    }
+    // result is constructed with default constructor on error-and-ignore  -> i=0
 
-    void ignore_handler_reconnects()
-    {
-        wraptype::set_connection(auto_ptr<ConnectionWrapper>
-            (new ReconnectIgnoreFailureSocketWrapper("./socket")));
+    BOOST_CHECK_EQUAL(0,i);
+}
 
-        wraptype::get_connection_wrapper()->set_command_timeout_usec(3000000);
-        wraptype::get_connection_wrapper()->set_hello_timeout_usec(3000000);
+BOOST_AUTO_TEST_CASE(ignore_handler_reconnects)
+{
+    wraptype::set_connection(auto_ptr<ConnectionWrapper>
+        (new ReconnectIgnoreFailureSocketWrapper("./socket")));
 
-        // 42 closes connection on the server side
-        t2n_exec(&cmd_group_x_client::serverfunc)(42);
+    wraptype::get_connection_wrapper()->set_command_timeout_usec(3000000);
+    wraptype::get_connection_wrapper()->set_hello_timeout_usec(3000000);
 
-        string out=t2n_exec(&cmd_group_x_client::getserverlog)();
+    // 42 closes connection on the server side
+    t2n_exec(&cmd_group_x_client::serverfunc)(42);
 
-        // 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++;
+    string out=t2n_exec(&cmd_group_x_client::getserverlog)();
 
-        CPPUNIT_ASSERT_EQUAL(2,cnt);
-    }
+    // 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);
+    wraptype::set_connection(auto_ptr<ConnectionWrapper>
+        (new ReconnectIgnoreFailureSocketWrapper("./socket")));
 
-    CPPUNIT_TEST(ignore_noserver);
-    CPPUNIT_TEST(ignore_finds_lateserver);
-    CPPUNIT_TEST(ignore_wrongserver);
+    // wraptype::get_connection_wrapper()->set_logging(&cerr,debug);
 
-    CPPUNIT_TEST_SUITE_END();
+    // there is no server
 
-    public:
+    int i=t2n_exec(&cmd_group_x_client::serverfunc)(1);
 
-    pid_t child_pid;
-
-    void setUp()
-    {
-        child_pid=0;
-    }
-
-    void tearDown()
-    {
-        // make sure the server-child is dead before the next test runs
-        if (child_pid != 0)
-        {
-            kill(child_pid,SIGKILL);
-            sleep(1);
-        }
-    }
-
-    void ignore_noserver()
-    {
-        wraptype::set_connection(auto_ptr<ConnectionWrapper>
-            (new ReconnectIgnoreFailureSocketWrapper("./socket")));
+    // result is constructed with default constructor on error-and-ignore  -> i=0
 
-        // wraptype::get_connection_wrapper()->set_logging(&cerr,debug);
+    BOOST_CHECK_EQUAL(0,i);
+}
 
-        // there is no server
+BOOST_AUTO_TEST_CASE(ignore_finds_lateserver)
+{
+    wraptype::set_connection(auto_ptr<ConnectionWrapper>
+        (new ReconnectIgnoreFailureSocketWrapper("./socket")));
 
-        int i=t2n_exec(&cmd_group_x_client::serverfunc)(1);
+    // there is no server
+    t2n_exec(&cmd_group_x_client::serverfunc)(1);
 
-        // result is constructed with default constructor on error-and-ignore  -> i=0
+    // launch a server
+    close_server=false;
+    kill_server=false;
 
-        CPPUNIT_ASSERT_EQUAL(0,i);
-    }
-
-    void ignore_finds_lateserver()
+    switch(child_pid=fork())
     {
-        wraptype::set_connection(auto_ptr<ConnectionWrapper>
-            (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
             {
-                try
+                int i=0;
+                while(i < 10 && !kill_server)
                 {
-                    int i=0;
-                    while(i < 10 && !kill_server)
-                    {
-                        close_server=false;
+                    close_server=false;
 
-                        socket_server ss("./socket");
-                        group_command_server<cmd_group_x> cs(ss);
-                        ss.set_logging(&logstream,debug);
+                    socket_server ss("./socket");
+                    group_command_server<cmd_group_x> 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";
+                    // max 10 sec
+                    for (; !close_server && !kill_server && i < 10; i++)
+                        cs.handle(1000000);
                 }
-
-                // don't call atexit and stuff
-                _exit(0);
-            }
-
-            default:
-            // parent
+            } catch(...)
             {
-                // wait till server is up
-                sleep(1);
+                std::cerr << "exception in child. ignoring\n";
             }
-        }
 
-        // server should be active
-        int i=t2n_exec(&cmd_group_x_client::serverfunc)(1);
+            // don't call atexit and stuff
+            _exit(0);
+        }
 
-        CPPUNIT_ASSERT_EQUAL(2,i);
+        default:
+        // parent
+        {
+            // wait till server is up
+            sleep(1);
+        }
     }
 
-    void send_hello(string hello_string, socket_server* ss, int conn_id)
-    {
-        server_connection *sc=ss->get_connection(conn_id);
-        sc->write(hello_string);
-    }
+    // server should be active
+    int i=t2n_exec(&cmd_group_x_client::serverfunc)(1);
 
-    void ignore_wrongserver()
-    {
-        wraptype::set_connection(auto_ptr<ConnectionWrapper>
-            (new ReconnectIgnoreFailureSocketWrapper("./socket")));
+    BOOST_CHECK_EQUAL(2,i);
+}
 
-        // launch a server
+BOOST_AUTO_TEST_CASE(ignore_wrongserver)
+{
+    wraptype::set_connection(auto_ptr<ConnectionWrapper>
+        (new ReconnectIgnoreFailureSocketWrapper("./socket")));
 
-        switch(child_pid=fork())
+    // launch a server
+
+    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");
-
-                    // server sends garbage
+                socket_server ss("./socket");
 
-                    ostringstream hello;
-                    hello << "XYZ 123";
+                // server sends garbage
 
-                    ss.add_callback(new_connection,bind(&test_wrapper_noserver::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";
-                }
+                ostringstream hello;
+                hello << "XYZ 123";
 
-                // don't call atexit and stuff
-                _exit(0);
-            }
+                ss.add_callback(new_connection,bind(&test_wrapper_noserver::ignore_wrongserver::send_hello, boost::ref(*this), hello.str(),&ss, _1));
 
-            default:
-            // parent
+                // max 10 sec
+                for (int i=0; i < 10; i++)
+                    ss.fill_buffer(1000000);
+            } catch(...)
             {
-                // wait till server is up
-                sleep(1);
+                std::cerr << "exception in child. ignoring\n";
             }
-        }
 
-        // there is no valid server
+            // don't call atexit and stuff
+            _exit(0);
+        }
 
-        int i=t2n_exec(&cmd_group_x_client::serverfunc)(1);
+        default:
+        // parent
+        {
+            // wait till server is up
+            sleep(1);
+        }
+    }
 
-        // result is constructed with default constructor on error-and-ignore  -> i=0
+    // there is no valid server
 
-        CPPUNIT_ASSERT_EQUAL(0,i);
-    }
+    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()