X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;f=tests%2Ftest-openat-safer.c;h=1c6774f62e3ecdcb15c4a520f44bca00c807f056;hb=2408f02086a20c0a02241cd4a1cf11f126a95fe3;hp=221a8801fbee726d8406dca73f903056b49ed76a;hpb=ddf870c4d82b62ad0fbe8a202adab357c7b94a30;p=gnulib.git diff --git a/tests/test-openat-safer.c b/tests/test-openat-safer.c index 221a8801f..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,24 +30,15 @@ duplicate the original stderr. */ #define BACKUP_STDERR_FILENO 10 -static FILE *myerr; +#define ASSERT_STREAM myerr +#include "macros.h" -#define ASSERT(expr) \ - do \ - { \ - if (!(expr)) \ - { \ - fprintf (myerr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \ - fflush (myerr); \ - abort (); \ - } \ - } \ - while (0) +static FILE *myerr; #define witness "test-openat-safer.txt" int -main () +main (void) { int i; int j; @@ -78,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. */ - 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); - } + { + 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);