1 /* Test that openat_safer leave standard fds alone.
2 Copyright (C) 2009, 2010 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
17 /* Written by Eric Blake <ebb9@byu.net>, 2009. */
28 /* This test intentionally closes stderr. So, we arrange to have fd 10
29 (outside the range of interesting fd's during the test) set up to
30 duplicate the original stderr. */
32 #define BACKUP_STDERR_FILENO 10
33 #define ASSERT_STREAM myerr
38 #define witness "test-openat-safer.txt"
49 /* We close fd 2 later, so save it in fd 10. */
50 if (dup2 (STDERR_FILENO, BACKUP_STDERR_FILENO) != BACKUP_STDERR_FILENO
51 || (myerr = fdopen (BACKUP_STDERR_FILENO, "w")) == NULL)
54 /* Create handle for future use. */
55 dfd = openat (AT_FDCWD, ".", O_RDONLY);
56 ASSERT (STDERR_FILENO < dfd);
58 /* Create file for later checks. */
60 fd = openat (dfd, witness, O_WRONLY | O_CREAT | O_EXCL, 0600);
61 ASSERT (STDERR_FILENO < fd);
62 ASSERT (write (fd, "hi", 2) == 2);
63 ASSERT (close (fd) == 0);
65 /* Four iterations, with progressively more standard descriptors
67 for (i = -1; i <= STDERR_FILENO; i++)
69 ASSERT (fchdir (dfd) == 0);
71 ASSERT (close (i) == 0);
73 /* Execute once in ".", once in "..". */
74 for (j = 0; j <= 1; j++)
77 ASSERT (chdir ("..") == 0);
79 /* Check for error detection. */
81 ASSERT (openat (AT_FDCWD, "", O_RDONLY) == -1);
82 ASSERT (errno == ENOENT);
84 ASSERT (openat (dfd, "", O_RDONLY) == -1);
85 ASSERT (errno == ENOENT);
87 ASSERT (openat (-1, ".", O_RDONLY) == -1);
88 ASSERT (errno == EBADF);
90 /* Check for trailing slash and /dev/null handling. */
92 ASSERT (openat (dfd, "nonexist.ent/", O_CREAT | O_RDONLY,
93 S_IRUSR | S_IWUSR) == -1);
94 ASSERT (errno == ENOTDIR || errno == EISDIR || errno == ENOENT
97 ASSERT (openat (dfd, witness "/", O_RDONLY) == -1);
98 ASSERT (errno == ENOTDIR || errno == EISDIR || errno == EINVAL);
99 /* Using a bad directory is okay for absolute paths. */
100 fd = openat (-1, "/dev/null", O_WRONLY);
101 ASSERT (STDERR_FILENO < fd);
102 /* Using a non-directory is wrong for relative paths. */
104 ASSERT (openat (fd, ".", O_RDONLY) == -1);
105 ASSERT (errno == EBADF || errno == ENOTDIR);
106 ASSERT (close (fd) == 0);
108 /* Check for our witness file. */
109 fd = openat (dfd, witness, O_RDONLY | O_NOFOLLOW);
110 ASSERT (STDERR_FILENO < fd);
111 ASSERT (read (fd, buf, 2) == 2);
112 ASSERT (buf[0] == 'h' && buf[1] == 'i');
113 ASSERT (close (fd) == 0);
116 ASSERT (fchdir (dfd) == 0);
117 ASSERT (unlink (witness) == 0);
118 ASSERT (close (dfd) == 0);