Index: ossp-pkg/sio/sio_test.c RCS File: /v/ossp/cvs/ossp-pkg/sio/sio_test.c,v rcsdiff -q -kk '-r1.14' '-r1.15' -u '/v/ossp/cvs/ossp-pkg/sio/sio_test.c,v' 2>/dev/null --- sio_test.c 2003/02/04 15:07:21 1.14 +++ sio_test.c 2003/02/06 12:49:16 1.15 @@ -38,6 +38,7 @@ #include #include #include +#include #include #include @@ -76,15 +77,15 @@ ts_test_fail(TS_CTX, "%s -> %d[%s] (expected %d[%s])\n", \ name, rc, errf(rc), rc0, errf(rc0)) -#define EVAL0(name,block) EVAL(name,rc,SIO_OK,block,sio_error) -#define EVALS(name,block) EVAL(name,rc,SA_OK,block,sa_error) +#define EVAL0(X) EVAL(#X,rc,SIO_OK,{ rc = X; },sio_error) +#define EVALS(X) EVAL(#X,rc,SA_OK,{ rc = X; },sa_error) 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); -static int test_sio_hello_client(ts_test_t *, int); -static int test_sio_hello_server(ts_test_t *, int); +static int test_sio_hello_client(ts_test_t *, int, int); +static int test_sio_hello_server(ts_test_t *, int, int); sio_rc_t sreadloop(sio_t *sio, char *buf, size_t len, size_t *actualp) { @@ -95,6 +96,8 @@ rc = sio_read(sio, buf, len, &actual); if (rc != SIO_OK) break; + if (actual == 0) + break; buf += actual; total += actual; len -= actual; @@ -105,6 +108,57 @@ return rc; } +typedef int (*func)(ts_test_t *, int, int); +void session(ts_test_t *_t, func client, func server, int duplex) +{ + pid_t child; + int pd[2]; + int wcount = 147; + int status; + + fflush(stdout); + fflush(stderr); + + if (duplex) { + if (socketpair(AF_UNIX, SOCK_STREAM, 0, pd) == -1) { + ts_test_fail(TS_CTX, "cannot create socketpair (%s)\n", + strerror(errno)); + } + } else { + if (pipe(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)); + return; + } + + if (child == 0) { + int result; + + sleep(1); + + close(pd[1]); + result = (*server)(NULL, pd[0], wcount); + close(pd[0]); + exit(result ? 1 : 0); + } else { + close(pd[0]); + (*client)(_t, pd[1], wcount); + close(pd[1]); + waitpid(child, &status, 0); + if (status != 0) { + ts_test_fail(TS_CTX, "child returned status %08lx\n", + status); + } + } +} + TS_TEST(test_sio_buffer) { sio_rc_t rc; @@ -115,37 +169,23 @@ char S[] = "Hello world\n"; size_t actual, len = strlen(S); - EVAL0("sio_create", { - rc = sio_create(&sio); - }); + EVAL0(sio_create(&sio)); if (rc != SIO_OK) return; - EVAL0("sio_create_stage(&sios_hole)", { - rc = sio_create_stage(sio, &sio_module_hole, &sios_hole); - }); + EVAL0(sio_create_stage(sio, &sio_module_hole, &sios_hole)); if (rc != SIO_OK) return; - EVAL0("sio_create_stage(&sios_buffer)", { - rc = sio_create_stage(sio, &sio_module_buffer, &sios_buffer); - }); + EVAL0(sio_create_stage(sio, &sio_module_buffer, &sios_buffer)); if (rc != SIO_OK) return; - EVAL0("sio_configure_stage(sios_buffer, outputsize)", { - rc = sio_configure_stage(sio, sios_buffer, "outputsize", &bufsize); - }); + EVAL0(sio_configure_stage(sio, sios_buffer, "outputsize", &bufsize)); if (rc != SIO_OK) return; - EVAL0("sio_attach(sios_hole)", { - rc = sio_attach(sio, sios_hole, SIO_MODE_WRITE); - }); + EVAL0(sio_attach(sio, sios_hole, SIO_MODE_WRITE)); if (rc != SIO_OK) return; - EVAL0("sio_attach(sios_buffer)", { - rc = sio_attach(sio, sios_buffer, SIO_MODE_WRITE); - }); + EVAL0(sio_attach(sio, sios_buffer, SIO_MODE_WRITE)); if (rc != SIO_OK) return; for (i=0; i 0) break; - EVAL0("sio_read(C_sio)", { - rc = sreadloop(C_sio, buf, sizeof(buf), &actual); - if (rc == SIO_ERR_EOF) { - --nstreams; - rc = SIO_OK; - } - }); + EVAL0( + ( (rc = sreadloop(C_sio, buf, sizeof(buf), &actual)), + rc == SIO_ERR_EOF ? (--nstreams, SIO_OK) : rc ) + ); if (rc != SIO_OK) break; p = buf; @@ -821,47 +654,25 @@ /* * destroy EVEN stream */ - EVAL0("sio_detach(C_sios_sio)", { - rc = sio_detach(C_sio, C_sios_sio); - }); - EVAL0("sio_destroy_stage(C_sios_sio)", { - rc = sio_destroy_stage(C_sio, C_sios_sio); - }); - EVAL0("sio_destroy(C_sio)", { - rc = sio_destroy(C_sio); - }); + EVAL0(sio_detach(C_sio, C_sios_sio)); + EVAL0(sio_destroy_stage(C_sio, C_sios_sio)); + EVAL0(rc = sio_destroy(C_sio)); /* * destroy ODD stream */ - EVAL0("sio_detach(B_sios_sio)", { - rc = sio_detach(B_sio, B_sios_sio); - }); - EVAL0("sio_destroy_stage(B_sios_sio)", { - rc = sio_destroy_stage(B_sio, B_sios_sio); - }); - EVAL0("sio_destroy(B_sio)", { - rc = sio_destroy(B_sio); - }); + EVAL0(sio_detach(B_sio, B_sios_sio)); + EVAL0(sio_destroy_stage(B_sio, B_sios_sio)); + EVAL0(sio_destroy(B_sio)); /* * destroy MUX stream */ - EVAL0("sio_detach(A_sios_sillymux)", { - rc = sio_detach(A_sio, A_sios_sillymux); - }); - EVAL0("sio_detach(A_sios_fd)", { - rc = sio_detach(A_sio, A_sios_fd); - }); - EVAL0("sio_destroy_stage(A_sios_sillymux)", { - rc = sio_destroy_stage(A_sio, A_sios_sillymux); - }); - EVAL0("sio_destroy_stage(A_sios_fd)", { - rc = sio_destroy_stage(A_sio, A_sios_fd); - }); - EVAL0("sio_destroy(A_sio)", { - rc = sio_destroy(A_sio); - }); + EVAL0(sio_detach(A_sio, A_sios_sillymux)); + EVAL0(sio_detach(A_sio, A_sios_fd)); + EVAL0(sio_destroy_stage(A_sio, A_sios_sillymux)); + EVAL0(sio_destroy_stage(A_sio, A_sios_fd)); + EVAL0(sio_destroy(A_sio)); close_and_cleanup: close(fd); @@ -872,7 +683,7 @@ } static -int test_sio_hello_client(ts_test_t *_t, int fd) +int test_sio_hello_client(ts_test_t *_t, int fd, int dummy) { FILE *f; char buf[81]; @@ -929,7 +740,7 @@ } static -int test_sio_hello_server(ts_test_t *_t, int fd) +int test_sio_hello_server(ts_test_t *_t, int fd, int dummy) { int error = 0, result = -1; sio_rc_t rc; @@ -939,57 +750,33 @@ char S[] = "Welcome to the real world\r\n"; size_t actual, len = strlen(S); - EVAL0("sio_create", { - rc = sio_create(&sio); - }); + EVAL0(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); - }); + EVAL0(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); - }); + EVAL0(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); - }); + EVAL0(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); - }); + EVAL0(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); - }); + EVAL0(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); - }); + EVAL0(sio_attach(sio, sios_hello, SIO_MODE_READWRITE)); if (rc != SIO_OK) goto badwrite2; + EVAL0(sio_write(sio, S, len, &actual)); - EVAL0("sio_write", { - rc = sio_write(sio, S, len, &actual); - }); if (rc != SIO_OK) error = 1; - EVAL0("sio_push", { - rc = sio_push(sio); - }); + EVAL0(sio_push(sio)); if (rc != SIO_OK) error = 1; - - EVAL0("sio_detach(sios_hello)", { - rc = sio_detach(sio, sios_hello); - }); + EVAL0(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); - }); + EVAL0(sio_detach(sio, sios_fd)); if (rc != SIO_OK) result = -1; badwrite: @@ -998,17 +785,11 @@ * 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); - }); + EVAL0(sio_destroy_stage(sio, sios_hello)); + if (rc != SIO_OK) result = -1; + EVAL0(sio_destroy_stage(sio, sios_fd)); + if (rc != SIO_OK) result = -1; + EVAL0(sio_destroy(sio)); if (rc != SIO_OK) result = -1; return result; @@ -1016,46 +797,9 @@ 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; - - sleep(1); - - 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); - } - } + session(_t, test_sio_hello_server, test_sio_hello_client, 1); } - #if ENABLE_ZLIB TS_TEST(test_sio_zlib) { @@ -1074,38 +818,21 @@ char tempfile[] = "./sio_test_tmpfile.XXXXXX"; int do_unlink = 0; - EVAL0("sio_create", { - rc = sio_create(&sio); - }); - if (rc != SIO_OK) return; - EVAL0("sio_create_stage(&sios_fd)", { - rc = sio_create_stage(sio, &sio_module_fd, &sios_fd); - }); - if (rc != SIO_OK) return; - EVAL0("sio_create_stage(&sios_zlib)", { - rc = sio_create_stage(sio, &sio_module_zlib, &sios_zlib); - }); - if (rc != SIO_OK) return; - - EVAL0("sio_configure_stage(sios_fd, buflen)", { - rc = sio_configure_stage(sio, sios_fd, "buflen", &buflen); - }); - if (rc != SIO_OK) return; - EVAL0("sio_configure_stage(sios_zlib)", { - rc = sio_configure_stage(sio, sios_zlib, "outputsize", &bufsize); - }); - if (rc != SIO_OK) return; - EVAL0("sio_configure_stage(sios_zlib, inputsize)", { - rc = sio_configure_stage(sio, sios_zlib, "inputsize", &bufsize); - }); - if (rc != SIO_OK) return; - EVAL0("sio_configure_stage(sios_zlib, outputlevel)", { - rc = sio_configure_stage(sio, sios_zlib, "outputlevel", &zoutlevel); - }); - if (rc != SIO_OK) return; - EVAL0("sio_configure_stage(sios_zlib, inputlevel)", { - rc = sio_configure_stage(sio, sios_zlib, "inputlevel", &zinlevel); - }); + EVAL0(sio_create(&sio)); + if (rc != SIO_OK) return; + EVAL0(sio_create_stage(sio, &sio_module_fd, &sios_fd)); + if (rc != SIO_OK) return; + EVAL0(sio_create_stage(sio, &sio_module_zlib, &sios_zlib)); + if (rc != SIO_OK) return; + EVAL0(sio_configure_stage(sio, sios_fd, "buflen", &buflen)); + if (rc != SIO_OK) return; + EVAL0(sio_configure_stage(sio, sios_zlib, "outputsize", &bufsize)); + if (rc != SIO_OK) return; + EVAL0(sio_configure_stage(sio, sios_zlib, "inputsize", &bufsize)); + if (rc != SIO_OK) return; + EVAL0(sio_configure_stage(sio, sios_zlib, "outputlevel", &zoutlevel)); + if (rc != SIO_OK) return; + EVAL0(sio_configure_stage(sio, sios_zlib, "inputlevel", &zinlevel)); if (rc != SIO_OK) return; /* @@ -1119,23 +846,15 @@ tempfile, strerror(errno)); } else { do_unlink = 1; - EVAL0("sio_configure_stage(sios_fd, fd)", { - rc = sio_configure_stage(sio, sios_fd, "fd", &fd); - }); + EVAL0(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); - }); + EVAL0(sio_attach(sio, sios_fd, SIO_MODE_READWRITE)); if (rc != SIO_OK) goto badwrite; - EVAL0("sio_attach(sios_zlib)", { - rc = sio_attach(sio, sios_zlib, SIO_MODE_READWRITE); - }); + EVAL0(sio_attach(sio, sios_zlib, SIO_MODE_READWRITE)); if (rc != SIO_OK) goto badwrite2; for (i=0; i