annotate tests/test-openat-safer.c @ 40057:b06060465f09

maint: Run 'make update-copyright'
author Paul Eggert <eggert@cs.ucla.edu>
date Tue, 01 Jan 2019 00:25:11 +0100
parents 49522e0c0097
children 7972ff22b3cf
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
11937
ded3ad24a7f4 openat-safer: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
1 /* Test that openat_safer leave standard fds alone.
40057
b06060465f09 maint: Run 'make update-copyright'
Paul Eggert <eggert@cs.ucla.edu>
parents: 40031
diff changeset
2 Copyright (C) 2009-2019 Free Software Foundation, Inc.
11937
ded3ad24a7f4 openat-safer: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
3
ded3ad24a7f4 openat-safer: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
4 This program is free software: you can redistribute it and/or modify
ded3ad24a7f4 openat-safer: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
5 it under the terms of the GNU General Public License as published by
ded3ad24a7f4 openat-safer: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
6 the Free Software Foundation; either version 3 of the License, or
ded3ad24a7f4 openat-safer: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
7 (at your option) any later version.
ded3ad24a7f4 openat-safer: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
8
ded3ad24a7f4 openat-safer: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
9 This program is distributed in the hope that it will be useful,
ded3ad24a7f4 openat-safer: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
ded3ad24a7f4 openat-safer: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
ded3ad24a7f4 openat-safer: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
12 GNU General Public License for more details.
ded3ad24a7f4 openat-safer: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
13
ded3ad24a7f4 openat-safer: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
14 You should have received a copy of the GNU General Public License
19190
9759915b2aca all: prefer https: URLs
Paul Eggert <eggert@cs.ucla.edu>
parents: 18626
diff changeset
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
11937
ded3ad24a7f4 openat-safer: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
16
ded3ad24a7f4 openat-safer: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
17 /* Written by Eric Blake <ebb9@byu.net>, 2009. */
ded3ad24a7f4 openat-safer: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
18
ded3ad24a7f4 openat-safer: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
19 #include <config.h>
ded3ad24a7f4 openat-safer: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
20
ded3ad24a7f4 openat-safer: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
21 #include "fcntl--.h"
ded3ad24a7f4 openat-safer: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
22
ded3ad24a7f4 openat-safer: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
23 #include <errno.h>
ded3ad24a7f4 openat-safer: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
24 #include <stdio.h>
ded3ad24a7f4 openat-safer: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
25 #include <sys/stat.h>
ded3ad24a7f4 openat-safer: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
26 #include <unistd.h>
ded3ad24a7f4 openat-safer: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
27
ded3ad24a7f4 openat-safer: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
28 /* This test intentionally closes stderr. So, we arrange to have fd 10
ded3ad24a7f4 openat-safer: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
29 (outside the range of interesting fd's during the test) set up to
ded3ad24a7f4 openat-safer: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
30 duplicate the original stderr. */
ded3ad24a7f4 openat-safer: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
31
ded3ad24a7f4 openat-safer: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
32 #define BACKUP_STDERR_FILENO 10
12510
618282a72d9b tests: use macros.h in more places
Eric Blake <ebb9@byu.net>
parents: 12421
diff changeset
33 #define ASSERT_STREAM myerr
618282a72d9b tests: use macros.h in more places
Eric Blake <ebb9@byu.net>
parents: 12421
diff changeset
34 #include "macros.h"
11937
ded3ad24a7f4 openat-safer: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
35
12510
618282a72d9b tests: use macros.h in more places
Eric Blake <ebb9@byu.net>
parents: 12421
diff changeset
36 static FILE *myerr;
11937
ded3ad24a7f4 openat-safer: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
37
12105
edba99785b75 test-open: on GNU/Hurd, /dev/null is a directory
Eric Blake <ebb9@byu.net>
parents: 11955
diff changeset
38 #define witness "test-openat-safer.txt"
edba99785b75 test-open: on GNU/Hurd, /dev/null is a directory
Eric Blake <ebb9@byu.net>
parents: 11955
diff changeset
39
11937
ded3ad24a7f4 openat-safer: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
40 int
12197
e45d9bb2233e tests: avoid several compiler warnings
Eric Blake <ebb9@byu.net>
parents: 12107
diff changeset
41 main (void)
11937
ded3ad24a7f4 openat-safer: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
42 {
ded3ad24a7f4 openat-safer: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
43 int i;
ded3ad24a7f4 openat-safer: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
44 int j;
ded3ad24a7f4 openat-safer: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
45 int dfd;
ded3ad24a7f4 openat-safer: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
46 int fd;
ded3ad24a7f4 openat-safer: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
47 char buf[2];
ded3ad24a7f4 openat-safer: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
48
ded3ad24a7f4 openat-safer: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
49 /* We close fd 2 later, so save it in fd 10. */
ded3ad24a7f4 openat-safer: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
50 if (dup2 (STDERR_FILENO, BACKUP_STDERR_FILENO) != BACKUP_STDERR_FILENO
ded3ad24a7f4 openat-safer: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
51 || (myerr = fdopen (BACKUP_STDERR_FILENO, "w")) == NULL)
ded3ad24a7f4 openat-safer: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
52 return 2;
ded3ad24a7f4 openat-safer: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
53
ded3ad24a7f4 openat-safer: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
54 /* Create handle for future use. */
ded3ad24a7f4 openat-safer: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
55 dfd = openat (AT_FDCWD, ".", O_RDONLY);
ded3ad24a7f4 openat-safer: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
56 ASSERT (STDERR_FILENO < dfd);
ded3ad24a7f4 openat-safer: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
57
ded3ad24a7f4 openat-safer: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
58 /* Create file for later checks. */
ded3ad24a7f4 openat-safer: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
59 remove (witness);
ded3ad24a7f4 openat-safer: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
60 fd = openat (dfd, witness, O_WRONLY | O_CREAT | O_EXCL, 0600);
ded3ad24a7f4 openat-safer: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
61 ASSERT (STDERR_FILENO < fd);
ded3ad24a7f4 openat-safer: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
62 ASSERT (write (fd, "hi", 2) == 2);
ded3ad24a7f4 openat-safer: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
63 ASSERT (close (fd) == 0);
ded3ad24a7f4 openat-safer: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
64
ded3ad24a7f4 openat-safer: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
65 /* Four iterations, with progressively more standard descriptors
ded3ad24a7f4 openat-safer: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
66 closed. */
ded3ad24a7f4 openat-safer: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
67 for (i = -1; i <= STDERR_FILENO; i++)
ded3ad24a7f4 openat-safer: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
68 {
ded3ad24a7f4 openat-safer: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
69 ASSERT (fchdir (dfd) == 0);
ded3ad24a7f4 openat-safer: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
70 if (0 <= i)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12197
diff changeset
71 ASSERT (close (i) == 0);
11937
ded3ad24a7f4 openat-safer: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
72
ded3ad24a7f4 openat-safer: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
73 /* Execute once in ".", once in "..". */
ded3ad24a7f4 openat-safer: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
74 for (j = 0; j <= 1; j++)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12197
diff changeset
75 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12197
diff changeset
76 if (j)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12197
diff changeset
77 ASSERT (chdir ("..") == 0);
11937
ded3ad24a7f4 openat-safer: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
78
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12197
diff changeset
79 /* Check for error detection. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12197
diff changeset
80 errno = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12197
diff changeset
81 ASSERT (openat (AT_FDCWD, "", O_RDONLY) == -1);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12197
diff changeset
82 ASSERT (errno == ENOENT);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12197
diff changeset
83 errno = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12197
diff changeset
84 ASSERT (openat (dfd, "", O_RDONLY) == -1);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12197
diff changeset
85 ASSERT (errno == ENOENT);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12197
diff changeset
86 errno = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12197
diff changeset
87 ASSERT (openat (-1, ".", O_RDONLY) == -1);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12197
diff changeset
88 ASSERT (errno == EBADF);
11937
ded3ad24a7f4 openat-safer: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
89
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12197
diff changeset
90 /* Check for trailing slash and /dev/null handling. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12197
diff changeset
91 errno = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12197
diff changeset
92 ASSERT (openat (dfd, "nonexist.ent/", O_CREAT | O_RDONLY,
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12197
diff changeset
93 S_IRUSR | S_IWUSR) == -1);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12197
diff changeset
94 ASSERT (errno == ENOTDIR || errno == EISDIR || errno == ENOENT
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12197
diff changeset
95 || errno == EINVAL);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12197
diff changeset
96 errno = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12197
diff changeset
97 ASSERT (openat (dfd, witness "/", O_RDONLY) == -1);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12197
diff changeset
98 ASSERT (errno == ENOTDIR || errno == EISDIR || errno == EINVAL);
40030
41dc174470c2 openat-safer tests: Avoid test failure on NetBSD 8.
Bruno Haible <bruno@clisp.org>
parents: 19484
diff changeset
99 #ifdef __linux__
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12197
diff changeset
100 /* Using a bad directory is okay for absolute paths. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12197
diff changeset
101 fd = openat (-1, "/dev/null", O_WRONLY);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12197
diff changeset
102 ASSERT (STDERR_FILENO < fd);
40030
41dc174470c2 openat-safer tests: Avoid test failure on NetBSD 8.
Bruno Haible <bruno@clisp.org>
parents: 19484
diff changeset
103 #endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12197
diff changeset
104 /* Using a non-directory is wrong for relative paths. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12197
diff changeset
105 errno = 0;
40031
49522e0c0097 openat-safer tests: Avoid test failure on NetBSD 8. Part 2.
Bruno Haible <bruno@clisp.org>
parents: 40030
diff changeset
106 fd = open ("/dev/null", O_RDONLY);
49522e0c0097 openat-safer tests: Avoid test failure on NetBSD 8. Part 2.
Bruno Haible <bruno@clisp.org>
parents: 40030
diff changeset
107 ASSERT (STDERR_FILENO < fd);
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12197
diff changeset
108 ASSERT (openat (fd, ".", O_RDONLY) == -1);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12197
diff changeset
109 ASSERT (errno == EBADF || errno == ENOTDIR);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12197
diff changeset
110 ASSERT (close (fd) == 0);
11937
ded3ad24a7f4 openat-safer: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
111
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12197
diff changeset
112 /* Check for our witness file. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12197
diff changeset
113 fd = openat (dfd, witness, O_RDONLY | O_NOFOLLOW);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12197
diff changeset
114 ASSERT (STDERR_FILENO < fd);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12197
diff changeset
115 ASSERT (read (fd, buf, 2) == 2);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12197
diff changeset
116 ASSERT (buf[0] == 'h' && buf[1] == 'i');
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12197
diff changeset
117 ASSERT (close (fd) == 0);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12197
diff changeset
118 }
11937
ded3ad24a7f4 openat-safer: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
119 }
ded3ad24a7f4 openat-safer: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
120 ASSERT (fchdir (dfd) == 0);
ded3ad24a7f4 openat-safer: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
121 ASSERT (unlink (witness) == 0);
ded3ad24a7f4 openat-safer: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
122 ASSERT (close (dfd) == 0);
ded3ad24a7f4 openat-safer: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
123
ded3ad24a7f4 openat-safer: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
124 return 0;
ded3ad24a7f4 openat-safer: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
125 }