libt2n: (gerd) fix bug receiving fragmented data
[libt2n] / test / comm.cpp
index 55c5632..169c183 100644 (file)
@@ -32,7 +32,9 @@ class test_comm : public TestFixture
 
     CPPUNIT_TEST(UnixCommToServer);
     CPPUNIT_TEST(UnixCommToServerAndBack);
+    CPPUNIT_TEST(UnixCommToServerAndBackBig);
     CPPUNIT_TEST(IPCommToServer);
+    CPPUNIT_TEST(IPCommToServerAndBack);
 
     CPPUNIT_TEST_SUITE_END();
 
@@ -155,6 +157,66 @@ class test_comm : public TestFixture
         }
     }
 
+    void UnixCommToServerAndBackBig()
+    {
+        pid_t pid;
+
+        switch(pid=fork())
+        {
+            case -1:
+            {
+                CPPUNIT_FAIL("fork error");
+                break;
+            }
+            case 0:
+            // child
+            {
+                socket_server ss("./socket");
+                ss.set_logging(&cerr,debug);
+
+                // max 10 sec
+                for (int i=0; i < 10; i++)
+                {
+                    ss.fill_buffer(1000000);
+
+                    string data;
+                    unsigned int cid;
+
+                    if(ss.get_packet(data,cid))
+                    {
+                        server_connection* con=ss.get_connection(cid);
+
+                        if (data=="QUIT")
+                            break;
+
+                        con->write(string().insert(0,100*1024,'y'));
+                    }
+                }
+                // don't call atexit and stuff
+                _exit(0);
+            }
+
+            default:
+            // parent
+            {
+                string data;
+
+                // wait till server is up
+                sleep(1);
+                socket_client_connection sc("./socket");
+
+                sc.write(string().insert(0,100*1024,'x'));
+
+                while (!sc.get_packet(data))
+                    sc.fill_buffer(1000000);
+
+                CPPUNIT_ASSERT_EQUAL(string().insert(0,100*1024,'y'),data);
+
+                sc.write("QUIT");
+            }
+        }
+    }
+
     void IPCommToServer()
     {
         pid_t pid;
@@ -172,7 +234,7 @@ class test_comm : public TestFixture
             {
                 // wait till server is up
                 sleep(1);
-                socket_client_connection sc("localhost",6666,socket_client_connection::max_retries_default);
+                socket_client_connection sc(6666);
                 sc.write("hello");
                 // don't call atexit and stuff
                 _exit(0);
@@ -196,6 +258,75 @@ class test_comm : public TestFixture
         CPPUNIT_ASSERT_EQUAL(string("hello"),data);
     }
 
+    void IPCommToServerAndBack()
+    {
+        pid_t pid;
+
+        switch(pid=fork())
+        {
+            case -1:
+            {
+                CPPUNIT_FAIL("fork error");
+                break;
+            }
+            case 0:
+            // child
+            {
+                socket_server ss(6666);
+                ss.set_logging(&cerr,debug);
+
+                // max 10 sec
+                for (int i=0; i < 10; i++)
+                {
+                    ss.fill_buffer(1000000);
+
+                    string data;
+                    unsigned int cid;
+
+                    if(ss.get_packet(data,cid))
+                    {
+                        server_connection* con=ss.get_connection(cid);
+
+                        if (data=="QUIT")
+                            break;
+
+                        if (data=="ABC")
+                            con->write("DEF");
+                        else
+                            con->write("xyz");
+                    }
+                }
+                // don't call atexit and stuff
+                _exit(0);
+            }
+
+            default:
+            // parent
+            {
+                string data;
+
+                // wait till server is up
+                sleep(1);
+                socket_client_connection sc(6666);
+                sc.write("ABC");
+
+                sc.fill_buffer(1000000);
+                sc.get_packet(data);
+
+                CPPUNIT_ASSERT_EQUAL(string("DEF"),data);
+
+                sc.write("HAHA");
+
+                sc.fill_buffer(1000000);
+                sc.get_packet(data);
+
+                CPPUNIT_ASSERT_EQUAL(string("xyz"),data);
+
+                sc.write("QUIT");
+            }
+        }
+    }
+
 };
 
 CPPUNIT_TEST_SUITE_REGISTRATION(test_comm);