libt2n: (gerd) make sure no exception is thrown in command_client constructor, fix...
[libt2n] / test / hello.cpp
index a0cc532..1036e30 100644 (file)
@@ -51,17 +51,24 @@ class test_hello : public TestFixture
     CPPUNIT_TEST(BadVersion);
     CPPUNIT_TEST(SeparatorMissing);
     CPPUNIT_TEST(WrongByteOrder);
-    CPPUNIT_TEST(OtherServer);
+    CPPUNIT_TEST(OtherServerBig);
+    CPPUNIT_TEST(OtherServerSmall);
 
     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)
     {
@@ -80,9 +87,7 @@ class test_hello : public TestFixture
 
     void HelloOk()
     {
-        pid_t pid;
-
-        switch(pid=fork())
+        switch(child_pid=fork())
         {
             case -1:
             {
@@ -117,16 +122,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:
             {
@@ -159,16 +162,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);
             }
@@ -177,9 +177,7 @@ class test_hello : public TestFixture
 
     void BadVersion()
     {
-        pid_t pid;
-
-        switch(pid=fork())
+        switch(child_pid=fork())
         {
             case -1:
             {
@@ -216,16 +214,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);
             }
@@ -234,9 +229,7 @@ class test_hello : public TestFixture
 
     void SeparatorMissing()
     {
-        pid_t pid;
-
-        switch(pid=fork())
+        switch(child_pid=fork())
         {
             case -1:
             {
@@ -272,16 +265,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);
             }
@@ -290,9 +280,7 @@ class test_hello : public TestFixture
 
     void WrongByteOrder()
     {
-        pid_t pid;
-
-        switch(pid=fork())
+        switch(child_pid=fork())
         {
             case -1:
             {
@@ -337,27 +325,22 @@ 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);
             }
         }
     }
 
-    void OtherServer()
+    void OtherServerBig()
     {
-        pid_t pid;
-
-        switch(pid=fork())
+        switch(child_pid=fork())
         {
             case -1:
             {
@@ -391,16 +374,62 @@ class test_hello : public TestFixture
                 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();
+
+                CPPUNIT_ASSERT_EQUAL(string("illegal hello received (T2N)"),errormsg);
+            }
+        }
+    }
 
-                try
-                {
-                    command_client cc(sc);
-                }
-                catch(t2n_version_mismatch &e)
-                { errormsg=e.what(); }
-                catch(...)
-                { throw; }
+    void OtherServerSmall()
+    {
+        switch(child_pid=fork())
+        {
+            case -1:
+            {
+                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::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);
+            }
+
+            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();
 
                 CPPUNIT_ASSERT_EQUAL(string("illegal hello received (T2N)"),errormsg);
             }