X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;f=tests%2Ftest-openat-safer.c;h=1c6774f62e3ecdcb15c4a520f44bca00c807f056;hb=414111a8d8020459f65506d4c229d4071ad9d51a;hp=47d3ada9804f21bf1ba2e09baf22edae8a1e28d3;hpb=a11b390609cf4b3315c4752a764148494b0a3b44;p=gnulib.git diff --git a/tests/test-openat-safer.c b/tests/test-openat-safer.c index 47d3ada98..1c6774f62 100644 --- a/tests/test-openat-safer.c +++ b/tests/test-openat-safer.c @@ -1,5 +1,5 @@ /* Test that openat_safer leave standard fds alone. - Copyright (C) 2009 Free Software Foundation, Inc. + Copyright (C) 2009-2011 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 @@ -22,7 +22,6 @@ #include #include -#include #include #include @@ -31,29 +30,21 @@ duplicate the original stderr. */ #define BACKUP_STDERR_FILENO 10 +#define ASSERT_STREAM myerr +#include "macros.h" + static FILE *myerr; -#define ASSERT(expr) \ - do \ - { \ - if (!(expr)) \ - { \ - fprintf (myerr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \ - fflush (myerr); \ - abort (); \ - } \ - } \ - while (0) +#define witness "test-openat-safer.txt" int -main () +main (void) { int i; int j; int dfd; int fd; char buf[2]; - const char *witness = "test-openat-safer.txt"; /* We close fd 2 later, so save it in fd 10. */ if (dup2 (STDERR_FILENO, BACKUP_STDERR_FILENO) != BACKUP_STDERR_FILENO @@ -77,50 +68,50 @@ main () { ASSERT (fchdir (dfd) == 0); if (0 <= i) - ASSERT (close (i) == 0); + ASSERT (close (i) == 0); /* Execute once in ".", once in "..". */ for (j = 0; j <= 1; j++) - { - if (j) - ASSERT (chdir ("..") == 0); - - /* Check for error detection. */ - errno = 0; - ASSERT (openat (AT_FDCWD, "", O_RDONLY) == -1); - ASSERT (errno == ENOENT); - errno = 0; - ASSERT (openat (dfd, "", O_RDONLY) == -1); - ASSERT (errno == ENOENT); - errno = 0; - ASSERT (openat (-1, ".", O_RDONLY) == -1); - ASSERT (errno == EBADF); - - /* Check for trailing slash and /dev/null handling; the - particular errno might be ambiguous. */ - errno = 0; - ASSERT (openat (dfd, "nonexist.ent/", O_CREAT | O_RDONLY, - S_IRUSR | S_IWUSR) == -1); - /* ASSERT (errno == ENOTDIR); */ - errno = 0; - ASSERT (openat (dfd, "/dev/null/", O_RDONLY) == -1); - /* ASSERT (errno == ENOTDIR); */ - /* Using a bad directory is okay for absolute paths. */ - fd = openat (-1, "/dev/null", O_WRONLY); - ASSERT (STDERR_FILENO < fd); - /* Using a non-directory is wrong for relative paths. */ - errno = 0; - ASSERT (openat (fd, ".", O_RDONLY) == -1); - ASSERT (errno == EBADF || errno == ENOTDIR); - ASSERT (close (fd) == 0); - - /* Check for our witness file. */ - fd = openat (dfd, witness, O_RDONLY | O_NOFOLLOW); - ASSERT (STDERR_FILENO < fd); - ASSERT (read (fd, buf, 2) == 2); - ASSERT (buf[0] == 'h' && buf[1] == 'i'); - ASSERT (close (fd) == 0); - } + { + if (j) + ASSERT (chdir ("..") == 0); + + /* Check for error detection. */ + errno = 0; + ASSERT (openat (AT_FDCWD, "", O_RDONLY) == -1); + ASSERT (errno == ENOENT); + errno = 0; + ASSERT (openat (dfd, "", O_RDONLY) == -1); + ASSERT (errno == ENOENT); + errno = 0; + ASSERT (openat (-1, ".", O_RDONLY) == -1); + ASSERT (errno == EBADF); + + /* Check for trailing slash and /dev/null handling. */ + errno = 0; + ASSERT (openat (dfd, "nonexist.ent/", O_CREAT | O_RDONLY, + S_IRUSR | S_IWUSR) == -1); + ASSERT (errno == ENOTDIR || errno == EISDIR || errno == ENOENT + || errno == EINVAL); + errno = 0; + ASSERT (openat (dfd, witness "/", O_RDONLY) == -1); + ASSERT (errno == ENOTDIR || errno == EISDIR || errno == EINVAL); + /* Using a bad directory is okay for absolute paths. */ + fd = openat (-1, "/dev/null", O_WRONLY); + ASSERT (STDERR_FILENO < fd); + /* Using a non-directory is wrong for relative paths. */ + errno = 0; + ASSERT (openat (fd, ".", O_RDONLY) == -1); + ASSERT (errno == EBADF || errno == ENOTDIR); + ASSERT (close (fd) == 0); + + /* Check for our witness file. */ + fd = openat (dfd, witness, O_RDONLY | O_NOFOLLOW); + ASSERT (STDERR_FILENO < fd); + ASSERT (read (fd, buf, 2) == 2); + ASSERT (buf[0] == 'h' && buf[1] == 'i'); + ASSERT (close (fd) == 0); + } } ASSERT (fchdir (dfd) == 0); ASSERT (unlink (witness) == 0);