changeset 12105:edba99785b75

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 <ebb9@byu.net>
author Eric Blake <ebb9@byu.net>
date Fri, 02 Oct 2009 18:06:21 -0600
parents 39e066ee16aa
children 5f350c5819bd
files ChangeLog tests/test-fcntl-safer.c tests/test-fopen-safer.c tests/test-fopen.c tests/test-fopen.h tests/test-open.c tests/test-open.h tests/test-openat-safer.c
diffstat 8 files changed, 138 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,18 @@
 2009-10-02  Eric Blake  <ebb9@byu.net>
 
+	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.
--- 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 ();
+}
--- 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 ();
+}
--- a/tests/test-fopen.c
+++ b/tests/test-fopen.c
@@ -20,4 +20,12 @@
 
 #include <stdio.h>
 
+#define BASE "test-fopen.t"
+
 #include "test-fopen.h"
+
+int
+main ()
+{
+  return test_fopen ();
+}
--- a/tests/test-fopen.h
+++ b/tests/test-fopen.h
@@ -18,7 +18,9 @@
 
 /* Include <config.h> and a form of <stdio.h> first.  */
 
+#include <errno.h>
 #include <stdlib.h>
+#include <unistd.h>
 
 #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);
 
-  ASSERT (fopen ("/dev/null", "r") != NULL);
+  /* /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);
+
+  /* Cleanup.  */
+  ASSERT (unlink (BASE "file") == 0);
 
   return 0;
 }
--- a/tests/test-open.c
+++ b/tests/test-open.c
@@ -20,4 +20,12 @@
 
 #include <fcntl.h>
 
+#define BASE "test-open.t"
+
 #include "test-open.h"
+
+int
+main ()
+{
+  return test_open ();
+}
--- a/tests/test-open.h
+++ b/tests/test-open.h
@@ -18,8 +18,10 @@
 
 /* Include <config.h> and a form of <fcntl.h> first.  */
 
+#include <errno.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <unistd.h>
 
 #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);
 
-  ASSERT (open ("/dev/null", O_RDONLY) >= 0);
+  /* 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);
+
+  /* Cleanup.  */
+  ASSERT (unlink (BASE "file") == 0);
 
   return 0;
 }
--- a/tests/test-openat-safer.c
+++ b/tests/test-openat-safer.c
@@ -45,6 +45,8 @@
     }                                                                        \
   while (0)
 
+#define witness "test-openat-safer.txt"
+
 int
 main ()
 {
@@ -53,7 +55,6 @@
   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 @@
 	  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);