From ec65f38e49b3db77ef9866f888f0a8d743cfe742 Mon Sep 17 00:00:00 2001 From: Eric Blake Date: Fri, 2 Oct 2009 18:06:21 -0600 Subject: [PATCH] test-open: on GNU/Hurd, /dev/null is a directory * tests/test-fopen.h (main): Rename... (test_fopen): ...to this. Use a guaranteed non-directory when confirming open behavior on trailing slash. * tests/test-openat-safer.c (main): Likewise. * tests/test-open.h (main): Likewise.... (test_open): ...to this. * tests/test-fopen.c (main): Adjust caller. * tests/test-fopen-safer.c (main): Likewise. * tests/test-open.c (main): Likewise. * tests/test-fcntl-safer.c (main): Likewise. Reported by Samuel Thibault. Signed-off-by: Eric Blake --- ChangeLog | 13 +++++++++++ tests/test-fcntl-safer.c | 8 +++++++ tests/test-fopen-safer.c | 8 +++++++ tests/test-fopen.c | 8 +++++++ tests/test-fopen.h | 47 +++++++++++++++++++++++++++++++++---- tests/test-open.c | 8 +++++++ tests/test-open.h | 49 +++++++++++++++++++++++++++++++++++---- tests/test-openat-safer.c | 12 +++++----- 8 files changed, 138 insertions(+), 15 deletions(-) diff --git a/ChangeLog b/ChangeLog index 1125c58dcf..3ef349e998 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,18 @@ 2009-10-02 Eric Blake + test-open: on GNU/Hurd, /dev/null is a directory + * tests/test-fopen.h (main): Rename... + (test_fopen): ...to this. Use a guaranteed non-directory when + confirming open behavior on trailing slash. + * tests/test-openat-safer.c (main): Likewise. + * tests/test-open.h (main): Likewise.... + (test_open): ...to this. + * tests/test-fopen.c (main): Adjust caller. + * tests/test-fopen-safer.c (main): Likewise. + * tests/test-open.c (main): Likewise. + * tests/test-fcntl-safer.c (main): Likewise. + Reported by Samuel Thibault. + rename, fchdir: don't ignore chdir failure * lib/fchdir.c (get_name): Abort on unexpected chdir failure. * lib/rename.c (rpl_rename) [W32]: Likewise. diff --git a/tests/test-fcntl-safer.c b/tests/test-fcntl-safer.c index 3b3ff75139..433160bf66 100644 --- a/tests/test-fcntl-safer.c +++ b/tests/test-fcntl-safer.c @@ -20,4 +20,12 @@ #include "fcntl--.h" +#define BASE "test-fcntl-safer.t" + #include "test-open.h" + +int +main () +{ + return test_open (); +} diff --git a/tests/test-fopen-safer.c b/tests/test-fopen-safer.c index 701af354c1..e6133649d7 100644 --- a/tests/test-fopen-safer.c +++ b/tests/test-fopen-safer.c @@ -20,4 +20,12 @@ #include "stdio--.h" +#define BASE "test-fopen-safer.t" + #include "test-fopen.h" + +int +main () +{ + return test_fopen (); +} diff --git a/tests/test-fopen.c b/tests/test-fopen.c index 473d274f47..6efd480643 100644 --- a/tests/test-fopen.c +++ b/tests/test-fopen.c @@ -20,4 +20,12 @@ #include +#define BASE "test-fopen.t" + #include "test-fopen.h" + +int +main () +{ + return test_fopen (); +} diff --git a/tests/test-fopen.h b/tests/test-fopen.h index b1dafbb213..01369f83c4 100644 --- a/tests/test-fopen.h +++ b/tests/test-fopen.h @@ -18,7 +18,9 @@ /* Include and a form of first. */ +#include #include +#include #define ASSERT(expr) \ do \ @@ -32,13 +34,50 @@ } \ while (0) -int -main () +/* Test fopen. Assumes BASE is defined. */ + +static int +test_fopen (void) { + FILE *f; + /* Remove anything from prior partial run. */ + unlink (BASE "file"); + + /* Read requires existing file. */ + errno = 0; + ASSERT (fopen (BASE "file", "r") == NULL); + ASSERT (errno == ENOENT); + + /* Write can create a file. */ + f = fopen (BASE "file", "w"); + ASSERT (f); + ASSERT (fclose (f) == 0); + + /* Trailing slash is invalid on non-directory. */ + errno = 0; + ASSERT (fopen (BASE "file/", "r") == NULL); + ASSERT (errno == ENOTDIR || errno == EISDIR); + + /* Cannot create a directory. */ + errno = 0; ASSERT (fopen ("nonexist.ent/", "w") == NULL); - ASSERT (fopen ("/dev/null/", "r") == NULL); + ASSERT (errno == ENOTDIR || errno == EISDIR || errno == ENOENT); + + /* Directories cannot be opened for writing. */ + errno = 0; + ASSERT (fopen (".", "w") == NULL); + ASSERT (errno == EISDIR || errno == EINVAL); + + /* /dev/null must exist, and be writable. */ + f = fopen ("/dev/null", "r"); + ASSERT (f); + ASSERT (fclose (f) == 0); + f = fopen ("/dev/null", "w"); + ASSERT (f); + ASSERT (fclose (f) == 0); - ASSERT (fopen ("/dev/null", "r") != NULL); + /* Cleanup. */ + ASSERT (unlink (BASE "file") == 0); return 0; } diff --git a/tests/test-open.c b/tests/test-open.c index df7e36fd83..6b97e184d3 100644 --- a/tests/test-open.c +++ b/tests/test-open.c @@ -20,4 +20,12 @@ #include +#define BASE "test-open.t" + #include "test-open.h" + +int +main () +{ + return test_open (); +} diff --git a/tests/test-open.h b/tests/test-open.h index 466cab3d0e..738103792a 100644 --- a/tests/test-open.h +++ b/tests/test-open.h @@ -18,8 +18,10 @@ /* Include and a form of first. */ +#include #include #include +#include #define ASSERT(expr) \ do \ @@ -33,13 +35,50 @@ } \ while (0) -int -main () +/* Test fopen. Assumes BASE is defined. */ + +static int +test_open (void) { - ASSERT (open ("nonexist.ent/", O_CREAT | O_RDONLY, 0600) < 0); - ASSERT (open ("/dev/null/", O_RDONLY) < 0); + int fd; + /* Remove anything from prior partial run. */ + unlink (BASE "file"); + + /* Cannot create directory. */ + errno = 0; + ASSERT (open ("nonexist.ent/", O_CREAT | O_RDONLY, 0600) == -1); + ASSERT (errno == ENOTDIR || errno == EISDIR || errno == ENOENT); + + /* Create a regular file. */ + fd = open (BASE "file", O_CREAT | O_RDONLY, 0600); + ASSERT (0 <= fd); + ASSERT (close (fd) == 0); + + /* Trailing slash handling. */ + errno = 0; + ASSERT (open (BASE "file/", O_RDONLY) == -1); + ASSERT (errno == ENOTDIR || errno == EISDIR); + + /* Directories cannot be opened for writing. */ + errno = 0; + ASSERT (open (".", O_WRONLY) == -1); + ASSERT (errno == EISDIR); + + /* /dev/null must exist, and be writable. */ + fd = open ("/dev/null", O_RDONLY); + ASSERT (0 <= fd); + { + char c; + ASSERT (read (fd, &c, 1) == 0); + } + ASSERT (close (fd) == 0); + fd = open ("/dev/null", O_WRONLY); + ASSERT (0 <= fd); + ASSERT (write (fd, "c", 1) == 1); + ASSERT (close (fd) == 0); - ASSERT (open ("/dev/null", O_RDONLY) >= 0); + /* Cleanup. */ + ASSERT (unlink (BASE "file") == 0); return 0; } diff --git a/tests/test-openat-safer.c b/tests/test-openat-safer.c index 47d3ada980..7e44d674dc 100644 --- a/tests/test-openat-safer.c +++ b/tests/test-openat-safer.c @@ -45,6 +45,8 @@ static FILE *myerr; } \ while (0) +#define witness "test-openat-safer.txt" + int main () { @@ -53,7 +55,6 @@ main () 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 @@ -96,15 +97,14 @@ main () ASSERT (openat (-1, ".", O_RDONLY) == -1); ASSERT (errno == EBADF); - /* Check for trailing slash and /dev/null handling; the - particular errno might be ambiguous. */ + /* 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); */ + ASSERT (errno == ENOTDIR || errno == EISDIR || errno == ENOENT); errno = 0; - ASSERT (openat (dfd, "/dev/null/", O_RDONLY) == -1); - /* ASSERT (errno == ENOTDIR); */ + ASSERT (openat (dfd, witness "/", O_RDONLY) == -1); + ASSERT (errno == ENOTDIR || errno == EISDIR); /* Using a bad directory is okay for absolute paths. */ fd = openat (-1, "/dev/null", O_WRONLY); ASSERT (STDERR_FILENO < fd); -- 2.30.2