OSSP CVS Repository

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

Check-in Number: 3122
Date: 2003-Jan-30 17:38:32 (local)
2003-Jan-30 16:38:32 (UTC)
User:mlelstv
Branch:
Comment: add protocol test, client-server connection via socketpair()
Tickets:
Inspections:
Files:
ossp-pkg/sio/sio_test.c      1.11 -> 1.12     192 inserted, 8 deleted

ossp-pkg/sio/sio_test.c 1.11 -> 1.12

--- sio_test.c   2003/01/30 15:06:54     1.11
+++ sio_test.c   2003/01/30 16:38:32     1.12
@@ -41,6 +41,7 @@
 
 #include <sys/types.h>
 #include <sys/wait.h>
+#include <sys/socket.h>
 
 #include "ts.h"
 #include "al.h"
@@ -77,11 +78,14 @@
 
 #define EVAL0(name,block) EVAL(name,rc,SIO_OK,block)
 
-sio_rc_t readloop(sio_t *, char *, size_t, size_t *);
-int test_sio_pipe_read(ts_test_t *, int, int);
-int test_sio_pipe_write(ts_test_t *, int, int);
+static sio_rc_t sreadloop(sio_t *, char *, size_t, size_t *);
+static int test_sio_pipe_read(ts_test_t *, int, int);
+static int test_sio_pipe_write(ts_test_t *, int, int);
 
-sio_rc_t readloop(sio_t *sio, char *buf, size_t len, size_t *actualp)
+static int test_sio_hello_client(ts_test_t *, int);
+static int test_sio_hello_server(ts_test_t *, int);
+
+sio_rc_t sreadloop(sio_t *sio, char *buf, size_t len, size_t *actualp)
 {
     sio_rc_t rc = SIO_OK;
     size_t actual, total = 0;
@@ -282,7 +286,7 @@
 
         for (i=0; i<wcount; ++i) {
             EVAL0("sio_read", {
-                rc = readloop(sio, buf, len, &actual);
+                rc = sreadloop(sio, buf, len, &actual);
             });
             if (rc != SIO_OK) break;
             if (actual != len) {
@@ -331,6 +335,7 @@
     });
 }
 
+static
 int test_sio_pipe_read(ts_test_t *_t, int fd, int wcount)
 {
     int error = 0, result = -1;
@@ -386,7 +391,7 @@
 
     for (i=0; i<wcount; ++i) {
         EVAL0("sio_read", {
-            rc = readloop(sio, buf, len, &actual);
+            rc = sreadloop(sio, buf, len, &actual);
         });
         if (rc != SIO_OK) error = 1;
         if (rc != SIO_OK) break;
@@ -435,6 +440,7 @@
     return result;
 }
 
+static
 int test_sio_pipe_write(ts_test_t *_t, int fd, int wcount)
 {
     int error = 0, result = -1;
@@ -737,7 +743,7 @@
         int nstreams = 2;
 
         EVAL0("sio_read(B_sio)", {
-            rc = readloop(B_sio, buf, sizeof(buf), &actual);
+            rc = sreadloop(B_sio, buf, sizeof(buf), &actual);
             if (rc == SIO_ERR_EOF) {
                 --nstreams;
                 rc = SIO_OK;
@@ -767,7 +773,7 @@
             break;
 
         EVAL0("sio_read(C_sio)", {
-            rc = readloop(C_sio, buf, sizeof(buf), &actual);
+            rc = sreadloop(C_sio, buf, sizeof(buf), &actual);
             if (rc == SIO_ERR_EOF) {
                 --nstreams;
                 rc = SIO_OK;
@@ -861,10 +867,188 @@
     }
 }
 
+static
+int test_sio_hello_client(ts_test_t *_t, int fd)
+{
+    FILE *f;
+    char buf[81];
+    int c, i;
+    int do_login = 1;
+    char S[] = "Welcome to the real world\r\n";
+    int result = 0;
+
+    f = fdopen(fd, "r+");
+    if (f == NULL) {
+        ts_test_fail(TS_CTX, "cannot make FILE handle\n");
+        return -1;
+    }
+
+    i = 0;
+    for (;;) {
+        c = fgetc(f);
+        if (c == EOF) {
+            ts_test_fail(TS_CTX, "EOF from server\n");
+            result = -1;
+            break;
+        }
+        if (i >= 80) {
+            ts_test_fail(TS_CTX, "input buffer overflow\n");
+            result = -1;
+            break;
+        }
+        buf[i++] = c;
+        buf[i] = '\0';
+        if (do_login) {
+            if (i > 7) {
+                ts_test_fail(TS_CTX, "handshake failure\n");
+                result = -1;
+                break;
+            }
+            if (strcmp(buf, "Login: ") == 0) {
+                fputs("Geheim\r\n", f);
+                fflush(f);
+                i = 0;
+                do_login = 0;
+            }
+        } else if (c == '\n')
+            break;
+    }
+
+    if (strcmp(buf, S)) {
+        ts_test_fail(TS_CTX, "data mismatch\n");
+        result = -1;
+    }
+
+    fclose(f);
+
+    return result;
+}
+
+static
+int test_sio_hello_server(ts_test_t *_t, int fd)
+{
+    int error = 0, result = -1;
+    sio_rc_t rc;
+    sio_t *sio;
+    sio_stage_t *sios_hello, *sios_fd;
+    size_t buflen  =   47;     /* fd input buffer size */
+    char S[] = "Welcome to the real world\r\n";
+    size_t actual, len = strlen(S);
+
+    EVAL0("sio_create", {
+        rc = sio_create(&sio);
+    });
+    if (rc != SIO_OK) return -1;
+    EVAL0("sio_create_stage(&sios_fd)", {
+        rc = sio_create_stage(sio, &sio_module_fd, &sios_fd);
+    });
+    if (rc != SIO_OK) return -1;
+    EVAL0("sio_create_stage(&sios_hello)", {
+        rc = sio_create_stage(sio, &sio_module_hello, &sios_hello);
+    });
+    if (rc != SIO_OK) return -1;
+
+    EVAL0("sio_configure_stage(sios_fd, buflen)", {
+        rc = sio_configure_stage(sio, sios_fd, "buflen", &buflen);
+    });
+    if (rc != SIO_OK) return -1;
+
+    EVAL0("sio_configure_stage(sios_fd, fd)", {
+        rc = sio_configure_stage(sio, sios_fd, "fd", &fd);
+    });
+    if (rc != SIO_OK) goto badwrite;
+    EVAL0("sio_attach(sios_fd)", {
+        rc = sio_attach(sio, sios_fd, SIO_MODE_READWRITE);
+    });
+    if (rc != SIO_OK) goto badwrite;
+    EVAL0("sio_attach(sios_hello)", {
+        rc = sio_attach(sio, sios_hello, SIO_MODE_READWRITE);
+    });
+    if (rc != SIO_OK) goto badwrite2;
+
+    EVAL0("sio_write", {
+        rc = sio_write(sio, S, len, &actual);
+    });
+    if (rc != SIO_OK) error = 1;
+    EVAL0("sio_push", {
+        rc = sio_push(sio);
+    });
+    if (rc != SIO_OK) error = 1;
+
+    EVAL0("sio_detach(sios_hello)", {
+        rc = sio_detach(sio, sios_hello);
+    });
+    if (rc != SIO_OK) error = 1;
+
+    result = error;
+
+    badwrite2:
+    EVAL0("sio_detach(sios_fd)", {
+        rc = sio_detach(sio, sios_fd);
+    });
+    if (rc != SIO_OK) result = -1;
+
+    badwrite:
+
+    /*
+     * common cleanup
+     */
+
+    EVAL0("sio_destroy_stage", {
+        rc = sio_destroy_stage(sio, sios_hello);
+    });
+    if (rc != SIO_OK) result = -1;
+    EVAL0("sio_destroy_stage(sios_fd)", {
+        rc = sio_destroy_stage(sio, sios_fd);
+    });
+    if (rc != SIO_OK) result = -1;
+    EVAL0("sio_destroy", {
+        rc = sio_destroy(sio);
+    });
+    if (rc != SIO_OK) result = -1;
+
+    return result;
+}
+
 TS_TEST(test_sio_hello)
 {
+    pid_t child;
+    int pd[2];
+    int status;
+
+    fflush(stdout);
+    fflush(stderr);
+
+    if (socketpair(AF_UNIX, SOCK_STREAM, 0, pd) == -1) {
+        ts_test_fail(TS_CTX, "cannot create pipe (%s)\n",
+            strerror(errno));
+    }
+
+    child = fork();
+    if (child == -1) {
+        ts_test_fail(TS_CTX, "cannot fork (%s)\n",
+            strerror(errno));
+    }
+
+    if (child == 0) {
+        int result;
+        close(pd[1]);
+        result = test_sio_hello_client(NULL, pd[0]);
+        close(pd[0]);
+        exit(result ? 1 : 0);
+    } else {
+        close(pd[0]);
+        test_sio_hello_server(_t, pd[1]);
+        close(pd[1]);
+        waitpid(child, &status, 0);
+        if (status != 0) {
+            ts_test_fail(TS_CTX, "child returned status %08lx\n",
+                status);
+        }
+    }
 }
 
+
 #if ENABLE_ZLIB
 TS_TEST(test_sio_zlib)
 {

CVSTrac 2.0.1