Mercurial > gnulib
annotate tests/test-openat-safer.c @ 40231:9b3c79fdfe0b
strtod: fix clash with strtold
Problem reported for RHEL 5 by Jesse Caldwell (Bug#34817).
* lib/strtod.c (compute_minus_zero, minus_zero):
Simplify by remving the macro / external variable,
and having just a function. User changed. This avoids
the need for an external variable that might clash.
author | Paul Eggert <eggert@cs.ucla.edu> |
---|---|
date | Mon, 11 Mar 2019 16:40:29 -0700 |
parents | 7972ff22b3cf |
children |
rev | line source |
---|---|
11937 | 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 | 3 |
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. | |
8 | |
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. | |
13 | |
14 You should have received a copy of the GNU General Public License | |
19190 | 15 along with this program. If not, see <https://www.gnu.org/licenses/>. */ |
11937 | 16 |
17 /* Written by Eric Blake <ebb9@byu.net>, 2009. */ | |
18 | |
19 #include <config.h> | |
20 | |
21 #include "fcntl--.h" | |
22 | |
23 #include <errno.h> | |
24 #include <stdio.h> | |
25 #include <sys/stat.h> | |
26 #include <unistd.h> | |
27 | |
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. */ | |
31 | |
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 | 35 |
12510
618282a72d9b
tests: use macros.h in more places
Eric Blake <ebb9@byu.net>
parents:
12421
diff
changeset
|
36 static FILE *myerr; |
11937 | 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 | 40 int |
12197
e45d9bb2233e
tests: avoid several compiler warnings
Eric Blake <ebb9@byu.net>
parents:
12107
diff
changeset
|
41 main (void) |
11937 | 42 { |
43 int i; | |
44 int j; | |
45 int dfd; | |
46 int fd; | |
47 char buf[2]; | |
48 | |
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) | |
52 return 2; | |
53 | |
54 /* Create handle for future use. */ | |
55 dfd = openat (AT_FDCWD, ".", O_RDONLY); | |
56 ASSERT (STDERR_FILENO < dfd); | |
57 | |
58 /* Create file for later checks. */ | |
59 remove (witness); | |
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); | |
64 | |
65 /* Four iterations, with progressively more standard descriptors | |
66 closed. */ | |
67 for (i = -1; i <= STDERR_FILENO; i++) | |
68 { | |
69 ASSERT (fchdir (dfd) == 0); | |
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 | 72 |
73 /* Execute once in ".", once in "..". */ | |
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 | 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 | 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); |
40152
7972ff22b3cf
tests: Enable Linux specific tests on Android.
Bruno Haible <bruno@clisp.org>
parents:
40057
diff
changeset
|
99 #if defined __linux__ || defined __ANDROID__ |
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 | 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 | 119 } |
120 ASSERT (fchdir (dfd) == 0); | |
121 ASSERT (unlink (witness) == 0); | |
122 ASSERT (close (dfd) == 0); | |
123 | |
124 return 0; | |
125 } |