Index: ossp-pkg/sio/sio_test.c RCS File: /v/ossp/cvs/ossp-pkg/sio/sio_test.c,v rcsdiff -q -kk '-r1.11' '-r1.12' -u '/v/ossp/cvs/ossp-pkg/sio/sio_test.c,v' 2>/dev/null --- 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 #include +#include #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= 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) {