* 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>
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.
#include "fcntl--.h"
+#define BASE "test-fcntl-safer.t"
+
#include "test-open.h"
+
+int
+main ()
+{
+ return test_open ();
+}
#include "stdio--.h"
+#define BASE "test-fopen-safer.t"
+
#include "test-fopen.h"
+
+int
+main ()
+{
+ return test_fopen ();
+}
#include <stdio.h>
+#define BASE "test-fopen.t"
+
#include "test-fopen.h"
+
+int
+main ()
+{
+ return test_fopen ();
+}
/* Include <config.h> and a form of <stdio.h> first. */
+#include <errno.h>
#include <stdlib.h>
+#include <unistd.h>
#define ASSERT(expr) \
do \
} \
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;
}
#include <fcntl.h>
+#define BASE "test-open.t"
+
#include "test-open.h"
+
+int
+main ()
+{
+ return test_open ();
+}
/* 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 \
} \
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;
}
} \
while (0)
+#define witness "test-openat-safer.txt"
+
int
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
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);