libt2n: (gerd) fix client-connection-logic, finish wrappers, all tests are working...
[libt2n] / test / timeout.cpp
index 0493d62..6618ef6 100644 (file)
@@ -120,14 +120,26 @@ class real_write_connection: public socket_server_connection
             { socket_write(data); }
 };
 
+// this is an evil hack to get access to real_write, don't ever do this in an app!!!
+class real_write_client_connection: public socket_client_connection
+{
+    public:
+        void real_write(const std::string& data)
+            { socket_write(data); }
+};
+
 class test_timeout : public TestFixture
 {
     CPPUNIT_TEST_SUITE(test_timeout);
 
+    CPPUNIT_TEST(ConnectTimeout);
     CPPUNIT_TEST(HelloTimeoutNothing);
     CPPUNIT_TEST(HelloTimeoutSlowData);
     CPPUNIT_TEST(CommandTimeout);
     CPPUNIT_TEST(CommandSlowResponse);
+    CPPUNIT_TEST(DisconnectOnWrite);
+    CPPUNIT_TEST(DisconnectOnRead);
+    CPPUNIT_TEST(BreakAccept);
 
     CPPUNIT_TEST_SUITE_END();
 
@@ -164,6 +176,43 @@ class test_timeout : public TestFixture
         }
     }
 
+    void ConnectTimeout()
+    {
+        pid_t pid;
+
+        switch(pid=fork())
+        {
+            case -1:
+            {
+                CPPUNIT_FAIL("fork error");
+                break;
+            }
+            case 0:
+            // child
+            {
+                socket_server ss("./socket");
+
+                // don't call atexit and stuff
+                _exit(0);
+            }
+
+            default:
+            // parent
+            {
+                string data;
+
+                // wait till server is up
+                sleep(1);
+
+                string errormsg;
+
+                socket_client_connection sc("./socket");
+
+                CPPUNIT_ASSERT_EQUAL(true,sc.connection::is_closed());
+            }
+        }
+    }
+
     void HelloTimeoutNothing()
     {
         pid_t pid;
@@ -200,7 +249,7 @@ class test_timeout : public TestFixture
 
                 try
                 {
-                    command_client cc(sc,1000000,1000000);
+                    command_client cc(&sc,1000000,1000000);
                 }
                 catch(t2n_transfer_error &e)
                 { errormsg=e.what(); }
@@ -208,6 +257,8 @@ class test_timeout : public TestFixture
                 { throw; }
 
                 CPPUNIT_ASSERT_EQUAL(string("timeout exceeded"),errormsg);
+
+                kill(pid,SIGKILL);
             }
         }
     }
@@ -261,7 +312,7 @@ class test_timeout : public TestFixture
 
                 try
                 {
-                    command_client cc(sc,1000000,1000000);
+                    command_client cc(&sc,1000000,1000000);
                 }
                 catch(t2n_transfer_error &e)
                 { errormsg=e.what(); }
@@ -269,6 +320,8 @@ class test_timeout : public TestFixture
                 { throw; }
 
                 CPPUNIT_ASSERT_EQUAL(string("timeout exceeded"),errormsg);
+
+                kill(pid,SIGKILL);
             }
         }
     }
@@ -313,7 +366,7 @@ class test_timeout : public TestFixture
                 sleep(1);
                 socket_client_connection sc("./socket");
 
-                command_client cc(sc,1000000,1000000);
+                command_client cc(&sc,1000000,1000000);
                 result_container rc;
 
                 string errormsg;
@@ -328,6 +381,8 @@ class test_timeout : public TestFixture
                 { throw; }
 
                 CPPUNIT_ASSERT_EQUAL(string("timeout exceeded"),errormsg);
+
+                kill(pid,SIGKILL);
             }
         }
     }
@@ -387,7 +442,7 @@ class test_timeout : public TestFixture
                 sleep(1);
                 socket_client_connection sc("./socket");
 
-                command_client cc(sc,1000000,1000000);
+                command_client cc(&sc,1000000,1000000);
                 result_container rc;
 
                 string errormsg;
@@ -402,10 +457,249 @@ class test_timeout : public TestFixture
                 { throw; }
 
                 CPPUNIT_ASSERT_EQUAL(string("timeout exceeded"),errormsg);
+
+                kill(pid,SIGKILL);
             }
         }
     }
 
+    void DisconnectOnWrite()
+    {
+        pid_t pid;
+
+        switch(pid=fork())
+        {
+            case -1:
+            {
+                CPPUNIT_FAIL("fork error");
+                break;
+            }
+            case 0:
+            // child
+            {
+                socket_server ss("./socket");
+
+                // bail out as soon as we get something
+                ss.fill_buffer(-1);
+                // don't call atexit and stuff
+                _exit(0);
+            }
+
+            default:
+            // parent
+            {
+                string data;
+
+                // don't kill us on broken pipe
+                signal(SIGPIPE, SIG_IGN);
+
+                // wait till server is up
+                sleep(1);
+                socket_client_connection sc("./socket");
+
+                string errormsg;
+
+                string huge(1000000,'x');
+
+                try
+                {
+                    sc.write(huge);
+                }
+                catch(t2n_transfer_error &e)
+                { errormsg=e.what(); }
+                catch(...)
+                { throw; }
+
+                CPPUNIT_ASSERT_EQUAL(string("write() returned Broken pipe"),errormsg);
+
+                kill(pid,SIGKILL);
+            }
+        }
+    }
+
+    void DisconnectOnRead()
+    {
+        pid_t pid1, pid2;
+
+        switch(pid1=fork())
+        {
+            case -1:
+            {
+                CPPUNIT_FAIL("fork error");
+                break;
+            }
+            case 0:
+            // child
+            {
+                // wait till server is up
+                sleep(1);
+
+                socket_client_connection sc("./socket");
+
+                // this is an evil hack to get access to real_write, don't ever do this in an app!!!
+                real_write_client_connection *rwc=(real_write_client_connection*)≻
+                rwc->real_write(string(10000,'x'));
+
+                // don't call atexit and stuff
+                _exit(0);
+            }
+
+            default:
+            // parent
+            {
+                // don't kill us on broken pipe
+                signal(SIGPIPE, SIG_IGN);
+
+                socket_server ss("./socket");
+
+                time_t t0 = time(NULL);
+
+                // max 5 sec
+                while (time(NULL) < t0 + 5 )
+                {
+                    ss.fill_buffer(1000000);
+
+                    string data;
+                    ss.get_packet(data);
+                }
+
+                // are we still alive and able to process data?
+
+                switch(pid2=fork())
+                {
+                    case -1:
+                    {
+                        CPPUNIT_FAIL("fork error");
+                        break;
+                    }
+                    case 0:
+                    // child
+                    {
+                        socket_client_connection *sc=new socket_client_connection("./socket");
+                        sc->write(string(10000,'x'));
+                        delete sc;
+                        // socket is closed regularly
+
+                        // don't run regular cleanup, otherwise cppunit stuff gets called
+                        _exit(0);
+                    }
+
+                    default:
+                    // parent
+                    {
+                        string received;
+
+                        t0 = time(NULL);
+
+                        // max 10 sec
+                        while (time(NULL) < t0 + 10 )
+                        {
+                            ss.fill_buffer(1000000);
+
+                            if (ss.get_packet(received))
+                                break;
+                        }
+
+                        CPPUNIT_ASSERT_EQUAL(string(10000,'x'),received);
+                    }
+                }
+            }
+        }
+        kill(pid1,SIGKILL);
+        kill(pid2,SIGKILL);
+    }
+
+    void BreakAccept()
+    {
+        pid_t pid1,pid2;
+
+        switch(pid1=fork())
+        {
+            case -1:
+            {
+                CPPUNIT_FAIL("fork error");
+                break;
+            }
+            case 0:
+            // child
+            {
+                // wait till server is really up and waiting
+                sleep(2);
+
+                // connect with very tight timeout and only 1 retry
+                socket_client_connection sc("./socket",50,1);
+
+                // don't call atexit and stuff
+                _exit(0);
+            }
+
+            default:
+            // parent
+            {
+                // don't kill us on broken pipe
+                signal(SIGPIPE, SIG_IGN);
+
+                socket_server ss("./socket");
+
+                // server is "working" while client wants to connect
+                sleep(5);
+
+                time_t t0 = time(NULL);
+
+                // max 5 sec
+                while (time(NULL) < t0 + 5 )
+                {
+                    ss.fill_buffer(1000000);
+
+                    string data;
+                    ss.get_packet(data);
+                }
+
+                // are we still alive and able to process data?
+
+                switch(pid2=fork())
+                {
+                    case -1:
+                    {
+                        CPPUNIT_FAIL("fork error");
+                        break;
+                    }
+                    case 0:
+                    // child
+                    {
+                        socket_client_connection *sc=new socket_client_connection("./socket");
+                        sc->write(string(10000,'x'));
+                        delete sc;
+                        // socket is closed regularly
+
+                        // don't run regular cleanup, otherwise cppunit stuff gets called
+                        _exit(0);
+                    }
+
+                    default:
+                    // parent
+                    {
+                        string received;
+
+                        t0 = time(NULL);
+
+                        // max 10 sec
+                        while (time(NULL) < t0 + 10 )
+                        {
+                            ss.fill_buffer(1000000);
+
+                            if (ss.get_packet(received))
+                                break;
+                        }
+
+                        CPPUNIT_ASSERT_EQUAL(string(10000,'x'),received);
+                    }
+                }
+            }
+        }
+        kill(pid1,SIGKILL);
+        kill(pid2,SIGKILL);
+    }
 };
 
 CPPUNIT_TEST_SUITE_REGISTRATION(test_timeout);