From 69374b85907050e3c6d8f845d5c9846c0fb581de Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Sat, 8 Nov 2008 14:32:20 +0100 Subject: [PATCH] More tests for 'select' module. --- ChangeLog | 12 +++++++ modules/select-tests | 13 ++++++-- tests/test-select-fd.c | 66 ++++++++++++++++++++++++++++++++++++++ tests/test-select-in.sh | 29 +++++++++++++++++ tests/test-select-out.sh | 29 +++++++++++++++++ tests/test-select-stdin.c | 80 +++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 227 insertions(+), 2 deletions(-) create mode 100644 tests/test-select-fd.c create mode 100755 tests/test-select-in.sh create mode 100755 tests/test-select-out.sh create mode 100644 tests/test-select-stdin.c diff --git a/ChangeLog b/ChangeLog index cec942185..1144726da 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,15 @@ +2008-11-07 Bruno Haible + + * tests/test-select-fd.c: New file. + * tests/test-select-in.sh: New file. + * tests/test-select-out.sh: New file. + * tests/test-select-stdin.c: New file. + * modules/select-tests (Files): Add the new files. + (Depends-on): Add gettimeofday. + (Makefile.am): Add test-select-in.sh, test-select-out.sh to TESTS. + Set TESTS_ENVIRONMENT. Add test-select-fd, test-select-stdin to + check_PROGRAMS. Define test_select_fd_LDADD, test_select_stdin_LDADD. + 2008-11-06 Alexander V. Lukyanov Bruno Haible diff --git a/modules/select-tests b/modules/select-tests index 3a49d15de..e4cdd7e78 100644 --- a/modules/select-tests +++ b/modules/select-tests @@ -1,5 +1,9 @@ Files: tests/test-select.c +tests/test-select-fd.c +tests/test-select-in.sh +tests/test-select-out.sh +tests/test-select-stdin.c Depends-on: stdbool @@ -19,13 +23,18 @@ connect accept ioctl close +gettimeofday configure.ac: Makefile.am: -TESTS += test-select -check_PROGRAMS += test-select +TESTS += test-select test-select-in.sh test-select-out.sh +# test-select-stdin has to be run by hand. +TESTS_ENVIRONMENT += EXEEXT='@EXEEXT@' +check_PROGRAMS += test-select test-select-fd test-select-stdin test_select_LDADD = $(LDADD) @LIBSOCKET@ +test_select_fd_LDADD = $(LDADD) @LIBSOCKET@ +test_select_stdin_LDADD = $(LDADD) @LIBSOCKET@ License: LGPL diff --git a/tests/test-select-fd.c b/tests/test-select-fd.c new file mode 100644 index 000000000..d362170a8 --- /dev/null +++ b/tests/test-select-fd.c @@ -0,0 +1,66 @@ +/* Test of select() substitute, reading or writing from a given file descriptor. + Copyright (C) 2008 Free Software Foundation, Inc. + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . */ + +/* Written by Bruno Haible , 2008. */ + +#include + +#include +#include +#include + +int +main (int argc, char *argv[]) +{ + if (argc == 3) + { + char mode = argv[1][0]; + + if (mode == 'r' || mode == 'w') + { + int fd = atoi (argv[2]); + + if (fd >= 0) + { + fd_set fds; + struct timeval timeout; + int ret; + + FD_ZERO (&fds); + FD_SET (fd, &fds); + timeout.tv_sec = 0; + timeout.tv_usec = 10000; + ret = (mode == 'r' + ? select (fd + 1, &fds, NULL, NULL, &timeout) + : select (fd + 1, NULL, &fds, NULL, &timeout)); + if (ret < 0) + { + perror ("select failed"); + exit (1); + } + if ((ret == 0) != ! FD_ISSET (fd, &fds)) + { + fprintf (stderr, "incorrect return value\n"); + exit (1); + } + fprintf (stderr, "%d\n", ret); + exit (0); + } + } + } + fprintf (stderr, "Usage: test-select-fd mode fd\n"); + exit (1); +} diff --git a/tests/test-select-in.sh b/tests/test-select-in.sh new file mode 100755 index 000000000..b93fee9a0 --- /dev/null +++ b/tests/test-select-in.sh @@ -0,0 +1,29 @@ +#!/bin/sh +# Test select() on file descriptors opened for reading. + +tmpfiles="" +trap 'rm -fr $tmpfiles' 1 2 3 15 + +tmpfiles="$tmpfiles t-select-in.tmp" + +# Regular files. + +./test-select-fd${EXEEXT} r 0 < ./test-select-fd${EXEEXT} 2> t-select-in.tmp +test `cat t-select-in.tmp` = "1" || exit 1 + +# Pipes. + +{ sleep 1; echo abc; } | ./test-select-fd${EXEEXT} r 0 2> t-select-in.tmp +test `cat t-select-in.tmp` = "0" || exit 1 + +echo abc | { sleep 1; ./test-select-fd${EXEEXT} r 0; } 2> t-select-in.tmp +test `cat t-select-in.tmp` = "1" || exit 1 + +# Special files. + +./test-select-fd${EXEEXT} r 0 < /dev/null 2> t-select-in.tmp +test `cat t-select-in.tmp` = "1" || exit 1 + +rm -fr $tmpfiles + +exit 0 diff --git a/tests/test-select-out.sh b/tests/test-select-out.sh new file mode 100755 index 000000000..f83beeb1f --- /dev/null +++ b/tests/test-select-out.sh @@ -0,0 +1,29 @@ +#!/bin/sh +# Test select() on file descriptors opened for writing. + +tmpfiles="" +trap 'rm -fr $tmpfiles' 1 2 3 15 + +tmpfiles="$tmpfiles t-select-out.out t-select-out.tmp" + +# Regular files. + +./test-select-fd${EXEEXT} w 1 > t-select-out.out 2> t-select-out.tmp +test `cat t-select-out.tmp` = "1" || exit 1 + +# Pipes. + +( { echo abc; ./test-select-fd${EXEEXT} w 1; } | { sleep 1; cat; } ) > /dev/null 2> t-select-out.tmp +test `cat t-select-out.tmp` = "0" || exit 1 + +( { sleep 1; echo abc; ./test-select-fd${EXEEXT} w 1; } | cat) > /dev/null 2> t-select-out.tmp +test `cat t-select-out.tmp` = "1" || exit 1 + +# Special files. + +./test-select-fd${EXEEXT} w 1 > /dev/null 2> t-select-out.tmp +test `cat t-select-out.tmp` = "1" || exit 1 + +rm -fr $tmpfiles + +exit 0 diff --git a/tests/test-select-stdin.c b/tests/test-select-stdin.c new file mode 100644 index 000000000..e855f92d2 --- /dev/null +++ b/tests/test-select-stdin.c @@ -0,0 +1,80 @@ +/* Test of select() substitute, reading from stdin. + Copyright (C) 2008 Free Software Foundation, Inc. + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . */ + +/* Written by Bruno Haible , 2008. */ + +#include + +#include +#include +#include +#include +#include + +int +main () +{ + printf ("Applying select() from standard input. Press Ctrl-C to abort.\n"); + for (;;) + { + struct timeval before; + struct timeval after; + unsigned long spent_usec; + fd_set readfds; + struct timeval timeout; + int ret; + + gettimeofday (&before, NULL); + + FD_ZERO (&readfds); + FD_SET (0, &readfds); + timeout.tv_sec = 0; + timeout.tv_usec = 500000; + ret = select (1, &readfds, NULL, NULL, &timeout); + + gettimeofday (&after, NULL); + spent_usec = (after.tv_sec - before.tv_sec) * 1000000 + + after.tv_usec - before.tv_usec; + + if (ret < 0) + { + perror ("select failed"); + exit (1); + } + if ((ret == 0) != ! FD_ISSET (0, &readfds)) + { + fprintf (stderr, "incorrect return value\n"); + exit (1); + } + if (ret == 0) + { + if (spent_usec < 250000) + { + fprintf (stderr, "returned too early\n"); + exit (1); + } + /* Timeout */ + printf ("."); fflush (stdout); + } + else + { + char c; + + printf ("Input available! Trying to read 1 byte...\n"); + read (0, &c, 1); + } + } +} -- 2.11.0