diff options
author | Eric Blake <ebb9@byu.net> | 2007-05-28 12:43:30 +0000 |
---|---|---|
committer | Eric Blake <ebb9@byu.net> | 2007-05-28 12:43:30 +0000 |
commit | d2d77c0eacd5e95e63cbea8abca35c70ce6a9a55 (patch) | |
tree | 9a76601bd05ccc18f51ff0d5cfc2900632b84a92 /tests | |
parent | af9f82f95032d5edab29c28594d8dea640758c5e (diff) | |
download | gnulib-d2d77c0eacd5e95e63cbea8abca35c70ce6a9a55.tar.gz |
Improve lseek module.
* lib/lseek.c (rpl_lseek): Detect EBADF on mingw.
* lib/unistd_.h (lseek): Scale back link warning message.
* tests/test-lseek.c: Beef up test.
* tests/test-lseek.sh: Exercise more facets of lseek.
Reported by Bruno Haible.
Diffstat (limited to 'tests')
-rw-r--r-- | tests/test-lseek.c | 64 | ||||
-rwxr-xr-x | tests/test-lseek.sh | 18 |
2 files changed, 75 insertions, 7 deletions
diff --git a/tests/test-lseek.c b/tests/test-lseek.c index ae53d98cad..d70629e1cd 100644 --- a/tests/test-lseek.c +++ b/tests/test-lseek.c @@ -19,11 +19,69 @@ #include <config.h> +#include <errno.h> +#include <stdio.h> #include <unistd.h> +#define ASSERT(expr) \ + do \ + { \ + if (!(expr)) \ + { \ + fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \ + abort (); \ + } \ + } \ + while (0) + +/* ARGC must be 2; *ARGV[1] is '0' if stdin and stdout are files, '1' + if they are pipes, and '2' if they are closed. Check for proper + semantics of lseek. */ int -main () +main (int argc, char **argv) { - /* Exit with success only if stdin is seekable. */ - return lseek (0, (off_t)0, SEEK_CUR) < 0; + if (argc != 2) + return 2; + switch (*argv[1]) + { + case '0': /* regular files */ + ASSERT (lseek (0, (off_t)2, SEEK_SET) == 2); + ASSERT (lseek (0, (off_t)-4, SEEK_CUR) == -1); + ASSERT (errno == EINVAL); + errno = 0; + ASSERT (lseek (0, (off_t)0, SEEK_CUR) == 2); + ASSERT (lseek (0, (off_t)0, (SEEK_SET | SEEK_CUR | SEEK_END) + 1) == -1); + ASSERT (errno == EINVAL); + ASSERT (lseek (1, (off_t)2, SEEK_SET) == 2); + errno = 0; + ASSERT (lseek (1, (off_t)-4, SEEK_CUR) == -1); + ASSERT (errno == EINVAL); + errno = 0; + ASSERT (lseek (1, (off_t)0, SEEK_CUR) == 2); + ASSERT (lseek (1, (off_t)0, (SEEK_SET | SEEK_CUR | SEEK_END) + 1) == -1); + ASSERT (errno == EINVAL); + break; + + case '1': /* pipes */ + errno = 0; + ASSERT (lseek (0, (off_t)0, SEEK_CUR) == -1); + ASSERT (errno == ESPIPE); + errno = 0; + ASSERT (lseek (1, (off_t)0, SEEK_CUR) == -1); + ASSERT (errno == ESPIPE); + break; + + case '2': /* closed */ + errno = 0; + ASSERT (lseek (0, (off_t)0, SEEK_CUR) == -1); + ASSERT (errno == EBADF); + errno = 0; + ASSERT (lseek (1, (off_t)0, SEEK_CUR) == -1); + ASSERT (errno == EBADF); + break; + + default: + return 1; + } + return 0; } diff --git a/tests/test-lseek.sh b/tests/test-lseek.sh index 966dd2e89b..e84c2bb493 100755 --- a/tests/test-lseek.sh +++ b/tests/test-lseek.sh @@ -1,7 +1,17 @@ #!/bin/sh -# Succeed on seekable stdin -./test-lseek${EXEEXT} < "$srcdir/test-lseek.sh" || exit 1 -# Fail on pipe stdin -echo hi | ./test-lseek${EXEEXT} && exit 1 +tmpfiles= +trap 'rm -fr $tmpfiles' 1 2 3 15 + +tmpfiles=t-lseek.tmp +# seekable files +./test-lseek${EXEEXT} 0 < "$srcdir/test-lseek.sh" > t-lseek.tmp || exit 1 + +# pipes +echo hi | ./test-lseek${EXEEXT} 1 | cat || exit 1 + +# closed descriptors +./test-lseek${EXEEXT} 2 <&- >&- || exit 1 + +rm -rf $tmpfiles exit 0 |