libt2n: (gerd) fixes & real unit tests
[libt2n] / test / comm.cpp
diff --git a/test/comm.cpp b/test/comm.cpp
new file mode 100644 (file)
index 0000000..60907b2
--- /dev/null
@@ -0,0 +1,159 @@
+/***************************************************************************
+ *   Copyright (C) 2004 by Intra2net AG                                    *
+ *   info@intra2net.com                                                    *
+ *                                                                         *
+ ***************************************************************************/
+
+#include <sys/types.h>
+#include <unistd.h>
+#include <errno.h>
+#include <signal.h>
+#include <stdio.h>
+
+#include <iostream>
+#include <string>
+#include <sstream>
+#include <stdexcept>
+
+#include <cppunit/extensions/TestFactoryRegistry.h>
+#include <cppunit/ui/text/TestRunner.h>
+#include <cppunit/extensions/HelperMacros.h>
+
+#include <socket_client.hxx>
+#include <socket_server.hxx>
+
+using namespace std;
+using namespace libt2n;
+using namespace CppUnit;
+
+class test_comm : public TestFixture
+{
+    CPPUNIT_TEST_SUITE(test_comm);
+
+    CPPUNIT_TEST(UnixCommToServer);
+    CPPUNIT_TEST(UnixCommToServerAndBack);
+
+    CPPUNIT_TEST_SUITE_END();
+
+    public:
+
+    void setUp()
+    { }
+
+    void tearDown()
+    { }
+
+    void UnixCommToServer()
+    {
+        pid_t pid;
+        string data;
+
+        switch(pid=fork())
+        {
+            case -1:
+            {
+                CPPUNIT_FAIL("fork error");
+                break;
+            }
+            case 0:
+            // child
+            {
+                // wait till server is up
+                sleep(1);
+                socket_client_connection sc("./socket");
+                sc.write("hello");
+                // don't call atexit and stuff
+                _exit(0);
+            }
+
+            default:
+            // parent
+            {
+                socket_server ss("./socket");
+
+                // max 10 sec
+                for (int i=0; i < 10; i++)
+                {
+                    ss.fill_buffer(1000000);
+
+                    if(ss.get_packet(data))
+                        break;
+                }
+            }
+        }
+        CPPUNIT_ASSERT_EQUAL(string("hello"),data);
+    }
+
+    void UnixCommToServerAndBack()
+    {
+        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;
+
+                        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("./socket");
+
+                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);