Prepare libt2n 0.7 release
[libt2n] / test / comm.cpp
index 7bd8b24..7b301a6 100644 (file)
@@ -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 <sys/types.h>
 #include <unistd.h>
 #include <errno.h>
 #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 <socket_client.hxx>
 #include <socket_server.hxx>
 
+#include "test_fixtures.hxx"
+
 using namespace std;
 using namespace libt2n;
-using namespace CppUnit;
 
+BOOST_FIXTURE_TEST_SUITE(test_comm, KillChildOnShutdownFixture)
 
-class test_comm : public TestFixture
+BOOST_AUTO_TEST_CASE(UnixCommToServer)
 {
-    CPPUNIT_TEST_SUITE(test_comm);
-
-    CPPUNIT_TEST(UnixCommToServer);
-    CPPUNIT_TEST(UnixCommToServerAndBack);
-    CPPUNIT_TEST(UnixCommToServerAndBackBig);
-    CPPUNIT_TEST(IPCommToServer);
-    CPPUNIT_TEST(IPCommToServerAndBack);
-    CPPUNIT_TEST(IPCommToServerAndBackBig);
-
-    CPPUNIT_TEST_SUITE_END();
+    string data;
 
-    pid_t child_pid;
-
-    public:
-
-    void setUp()
-    { }
-
-    void tearDown()
+    switch(child_pid=fork())
     {
-        // make sure the server-child is dead before the next test runs
-        kill(child_pid,SIGKILL);
-        sleep(1);
-    }
-
-    void UnixCommToServer()
-    {
-        string data;
-
-        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
             {
                 // wait till server is up
                 sleep(1);
                 socket_client_connection sc("./socket");
                 sc.write("hello");
-                // don't call atexit and stuff
-                _exit(0);
+            } catch(...)
+            {
+                std::cerr << "exception in child. ignoring\n";
             }
 
-            default:
-            // parent
-            {
-                socket_server ss("./socket");
+            // don't call atexit and stuff
+            _exit(0);
+        }
 
-                time_t t0 = time(NULL);
+        default:
+        // parent
+        {
+            socket_server ss("./socket");
 
-                // max 10 sec
-                while (time(NULL) < t0 + 10 )
-                {
-                    ss.fill_buffer(1000000);
+            time_t t0 = time(NULL);
 
-                    if(ss.get_packet(data))
-                        break;
-                }
+            // max 10 sec
+            while (time(NULL) < t0 + 10 )
+            {
+                ss.fill_buffer(1000000);
+
+                if(ss.get_packet(data))
+                    break;
             }
         }
-        CPPUNIT_ASSERT_EQUAL(string("hello"),data);
     }
+    BOOST_CHECK_EQUAL(string("hello"),data);
+}
 
-    void UnixCommToServerAndBack()
+BOOST_AUTO_TEST_CASE(UnixCommToServerAndBack)
+{
+    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
             {
                 socket_server ss("./socket");
                 ss.set_logging(&cerr,debug);
@@ -135,49 +133,56 @@ class test_comm : public TestFixture
                             con->write("xyz");
                     }
                 }
-                // 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;
 
-                sc.write("ABC");
+            // wait till server is up
+            sleep(1);
+            socket_client_connection sc("./socket");
 
-                sc.fill_buffer(1000000);
-                sc.get_packet(data);
+            sc.write("ABC");
 
-                CPPUNIT_ASSERT_EQUAL(string("DEF"),data);
+            sc.fill_buffer(1000000);
+            sc.get_packet(data);
 
-                sc.write("HAHA");
+            BOOST_CHECK_EQUAL(string("DEF"),data);
 
-                sc.fill_buffer(1000000);
-                sc.get_packet(data);
+            sc.write("HAHA");
 
-                CPPUNIT_ASSERT_EQUAL(string("xyz"),data);
+            sc.fill_buffer(1000000);
+            sc.get_packet(data);
 
-                sc.write("QUIT");
-            }
+            BOOST_CHECK_EQUAL(string("xyz"),data);
+
+            sc.write("QUIT");
         }
     }
+}
 
-    void UnixCommToServerAndBackBig()
+BOOST_AUTO_TEST_CASE(UnixCommToServerAndBackBig)
+{
+    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
             {
                 socket_server ss("./socket");
                 ss.set_logging(&cerr,debug);
@@ -203,84 +208,98 @@ class test_comm : public TestFixture
                     }
                 }
                 std::cerr << "child: OVER" << std::endl;
-                // 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;
 
-                sc.write(string().insert(0,100*1024,'x'));
+            // wait till server is up
+            sleep(1);
+            socket_client_connection sc("./socket");
 
-                while (!sc.get_packet(data))
-                    sc.fill_buffer(1000000);
+            sc.write(string().insert(0,100*1024,'x'));
 
-                CPPUNIT_ASSERT_EQUAL(string().insert(0,100*1024,'y'),data);
+            while (!sc.get_packet(data))
+                sc.fill_buffer(1000000);
 
-                sc.write("QUIT");
-            }
+            BOOST_CHECK_EQUAL(string().insert(0,100*1024,'y'),data);
+
+            sc.write("QUIT");
         }
     }
+}
 
-    void IPCommToServer()
-    {
-        string data;
+BOOST_AUTO_TEST_CASE(IPCommToServer)
+{
+    string data;
 
-        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
             {
                 // wait till server is up
                 sleep(1);
                 socket_client_connection sc(6666);
                 sc.write("hello");
-                // don't call atexit and stuff
-                _exit(0);
+            } catch(...)
+            {
+                std::cerr << "exception in child. ignoring\n";
             }
 
-            default:
-            // parent
-            {
-                socket_server ss(6666);
+            // don't call atexit and stuff
+            _exit(0);
+        }
 
-                time_t t0 = time(NULL);
+        default:
+        // parent
+        {
+            socket_server ss(6666);
 
-                // max 10 sec
-                while (time(NULL) < t0 + 10 )
-                {
-                    ss.fill_buffer(1000000);
+            time_t t0 = time(NULL);
 
-                    if(ss.get_packet(data))
-                        break;
-                }
+            // max 10 sec
+            while (time(NULL) < t0 + 10 )
+            {
+                ss.fill_buffer(1000000);
+
+                if(ss.get_packet(data))
+                    break;
             }
         }
-        CPPUNIT_ASSERT_EQUAL(string("hello"),data);
     }
+    BOOST_CHECK_EQUAL(string("hello"),data);
+}
 
-    void IPCommToServerAndBack()
+BOOST_AUTO_TEST_CASE(IPCommToServerAndBack)
+{
+    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
             {
                 socket_server ss(6666);
                 ss.set_logging(&cerr,debug);
@@ -308,49 +327,55 @@ class test_comm : public TestFixture
                             con->write("xyz");
                     }
                 }
-                // don't call atexit and stuff
-                _exit(0);
+            } catch(...)
+            {
+                std::cerr << "exception in child. ignoring\n";
             }
+            // don't call atexit and stuff
+            _exit(0);
+        }
 
-            default:
-            // parent
-            {
-                string data;
+        default:
+        // parent
+        {
+            string data;
 
-                // wait till server is up
-                sleep(1);
-                socket_client_connection sc(6666);
-                sc.write("ABC");
+            // wait till server is up
+            sleep(1);
+            socket_client_connection sc(6666);
+            sc.write("ABC");
 
-                sc.fill_buffer(1000000);
-                sc.get_packet(data);
+            sc.fill_buffer(1000000);
+            sc.get_packet(data);
 
-                CPPUNIT_ASSERT_EQUAL(string("DEF"),data);
+            BOOST_CHECK_EQUAL(string("DEF"),data);
 
-                sc.write("HAHA");
+            sc.write("HAHA");
 
-                sc.fill_buffer(1000000);
-                sc.get_packet(data);
+            sc.fill_buffer(1000000);
+            sc.get_packet(data);
 
-                CPPUNIT_ASSERT_EQUAL(string("xyz"),data);
+            BOOST_CHECK_EQUAL(string("xyz"),data);
 
-                sc.write("QUIT");
-            }
+            sc.write("QUIT");
         }
     }
+}
 
 
-    void IPCommToServerAndBackBig()
+BOOST_AUTO_TEST_CASE(IPCommToServerAndBackBig)
+{
+    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
             {
                 socket_server ss(6666);
                 ss.set_logging(&cerr,debug);
@@ -378,30 +403,33 @@ class test_comm : public TestFixture
                         con->write(string().insert(0,2048*1024,'y'));
                     }
                 }
-                // don't call atexit and stuff
-                _exit(0);
+            } catch(...)
+            {
+                std::cerr << "exception in child. ignoring\n";
             }
+            // don't call atexit and stuff
+            _exit(0);
+        }
 
-            default:
-            // parent
-            {
-                string data;
+        default:
+        // parent
+        {
+            string data;
 
-                // wait till server is up
-                sleep(1);
-                socket_client_connection sc(6666);
+            // wait till server is up
+            sleep(1);
+            socket_client_connection sc(6666);
 
-                sc.write(string().insert(0,100*1024,'x'));
+            sc.write(string().insert(0,100*1024,'x'));
 
-                while (!sc.get_packet(data))
-                    sc.fill_buffer(1000000);
+            while (!sc.get_packet(data))
+                sc.fill_buffer(1000000);
 
-                CPPUNIT_ASSERT_EQUAL(string().insert(0,2048*1024,'y'),data);
+            BOOST_CHECK_EQUAL(string().insert(0,2048*1024,'y'),data);
 
-                sc.write("QUIT");
-            }
+            sc.write("QUIT");
         }
-    } // eo IPCommToServerAndBackBig()
-};
+    }
+} // eo IPCommToServerAndBackBig()
 
-CPPUNIT_TEST_SUITE_REGISTRATION(test_comm);
+BOOST_AUTO_TEST_SUITE_END()