/* Test that openat_safer leave standard fds alone.
- Copyright (C) 2009 Free Software Foundation, Inc.
+ Copyright (C) 2009-2013 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
#include <errno.h>
#include <stdio.h>
-#include <stdlib.h>
#include <sys/stat.h>
#include <unistd.h>
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;
{
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);