OSSP CVS Repository

ossp - Check-in [2689]
Not logged in
[Honeypot]  [Browse]  [Home]  [Login]  [Reports
[Search]  [Ticket]  [Timeline
  [Patchset]  [Tagging/Branching

Check-in Number: 2689
Date: 2002-Oct-30 10:41:51 (local)
2002-Oct-30 09:41:51 (UTC)
User:rse
Branch:
Comment: first cut for more covering test suite: client/server test via TCP stream communication
Tickets:
Inspections:
Files:
ossp-pkg/sa/sa_test.c      1.17 -> 1.18     109 inserted, 1 deleted

ossp-pkg/sa/sa_test.c 1.17 -> 1.18

--- sa_test.c    2002/10/26 15:45:32     1.17
+++ sa_test.c    2002/10/30 09:41:51     1.18
@@ -38,6 +38,9 @@
 #include <time.h>
 #include <sys/utsname.h>
 #include <unistd.h>
+#include <errno.h>
+#include <sys/types.h>
+#include <sys/wait.h>
 
 #include "ts.h"
 #include "sa.h"
@@ -137,10 +140,115 @@
     sa_addr_destroy(saa2);
 }
 
+#define ex(proc, cmd) \
+    do { \
+        sa_rc_t __rv; \
+        ts_test_check(TS_CTX, "%s: %s", #proc, #cmd); \
+        if ((__rv = (cmd)) != SA_OK) { \
+            if (__rv == SA_ERR_SYS) \
+                ts_test_fail(TS_CTX, "%s -> error: %s (rc=%d) (system: %s)", \
+                             #cmd, sa_error(__rv), __rv, strerror(errno)); \
+            else \
+                ts_test_fail(TS_CTX, "%s -> error: %s (rc=%d)", \
+                             #cmd, sa_error(__rv), __rv); \
+        } \
+    } while (0)
+
+#define MSG_SRV_WELCOME "Welcome client\n"
+#define MSG_CLT_WELCOME "Welcome server\n"
+#define MSG_SRV_GOODBYE "Goodbye client\n"
+#define MSG_CLT_GOODBYE "Goodbye server\n"
+
 /* test: client/server stream communication */
 TS_TEST(test_sa_stream)
 {
-    /* FIXME */
+    sa_t *sa_clt;
+    sa_t *sa_srv;
+    sa_addr_t *saa_srv;
+    sa_addr_t *saa_clt;
+    pid_t pid_srv;
+    sa_t *sa_cld;
+    sa_addr_t *saa_cld;
+    char buf_srv[1024];
+    char buf_clt[1024];
+    size_t l;
+
+    if ((pid_srv = fork()) == 0) {
+        /*
+        ** SERVER
+        */
+
+        /* create server socket */
+        ex(SRV, sa_create(&sa_srv));
+        ex(SRV, sa_option(sa_srv, SA_OPTION_REUSEADDR, 1));
+        ex(SRV, sa_option(sa_srv, SA_OPTION_REUSEPORT, 1));
+        ex(SRV, sa_timeout(sa_srv, SA_TIMEOUT_ALL, 10, 0));
+
+        /* bind socket to local port */
+        ex(SRV, sa_addr_create(&saa_srv));
+        ex(SRV, sa_addr_u2a(saa_srv, "inet://127.0.0.1:12345#tcp"));
+        ex(SRV, sa_addr_destroy(saa_srv));
+        ex(SRV, sa_bind(sa_srv, saa_srv));
+
+        /* receive client connection */
+        ex(SRV, sa_listen(sa_srv, 10));
+        ex(SRV, sa_accept(sa_srv, &saa_cld, &sa_cld));
+
+        /* communicate with client */
+        ex(SRV, sa_writef(sa_cld, "%s", MSG_SRV_WELCOME));
+        ex(SRV, sa_readln(sa_cld, buf_srv, sizeof(buf_srv), &l));
+        if (strcmp(buf_srv, MSG_CLT_WELCOME) != 0)
+            ts_test_fail(TS_CTX, "server: got \"%s\", expected \"%s\"",
+                         buf_srv, MSG_CLT_WELCOME);
+        ex(SRV, sa_writef(sa_cld, "%s", MSG_SRV_GOODBYE));
+        ex(SRV, sa_shutdown(sa_cld, "w"));
+        ex(SRV, sa_readln(sa_cld, buf_srv, sizeof(buf_srv), &l));
+        if (strcmp(buf_srv, MSG_CLT_GOODBYE) != 0)
+            ts_test_fail(TS_CTX, "server: got \"%s\", expected \"%s\"",
+                         buf_srv, MSG_CLT_GOODBYE);
+        ex(SRV, sa_shutdown(sa_cld, "r"));
+
+        /* destroy client connection */
+        ex(SRV, sa_destroy(sa_cld));
+        ex(SRV, sa_addr_destroy(saa_cld));
+
+        /* destroy server socket and die */
+        ex(SRV, sa_destroy(sa_srv));
+        exit(0);
+    }
+    else {
+        /*
+        ** CLIENT
+        **/
+        sleep(2);
+
+        /* create client socket */
+        ex(CLT, sa_create(&sa_clt));
+        ex(CLT, sa_timeout(sa_clt, SA_TIMEOUT_ALL, 10, 0));
+
+        /* connect to server */
+        ex(CLT, sa_addr_create(&saa_clt));
+        ex(CLT, sa_addr_u2a(saa_clt, "inet://127.0.0.1:12345#tcp"));
+        ex(CLT, sa_addr_destroy(saa_clt));
+        ex(CLT, sa_connect(sa_clt, saa_clt));
+
+        /* communicate with server */
+        ex(CLT, sa_readln(sa_clt, buf_clt, sizeof(buf_clt), &l));
+        if (strcmp(buf_clt, MSG_SRV_WELCOME) != 0)
+            ts_test_fail(TS_CTX, "client: got \"%s\", expected \"%s\"",
+                         buf_clt, MSG_SRV_WELCOME);
+        ex(CLT, sa_writef(sa_clt, "%s", MSG_CLT_WELCOME));
+        ex(CLT, sa_readln(sa_clt, buf_clt, sizeof(buf_clt), &l));
+        if (strcmp(buf_clt, MSG_SRV_GOODBYE) != 0)
+            ts_test_fail(TS_CTX, "client: got \"%s\", expected \"%s\"",
+                         buf_clt, MSG_SRV_GOODBYE);
+        ex(CLT, sa_writef(sa_clt, "%s", MSG_CLT_GOODBYE));
+        ex(CLT, sa_shutdown(sa_clt, "rw"));
+
+        /* destroy server connection and wait for server to exit */
+        ex(CLT, sa_destroy(sa_clt));
+        waitpid(pid_srv, NULL, 0);
+    }
 }
 
 /* test: client/server datagram communication */

CVSTrac 2.0.1