Mercurial > hg > octave-kai > gnulib-hg
annotate tests/test-dup2.c @ 12399:7b2940180b02
unistd-safer: add unit test
Add more unit tests. Meanwhile, fix compilation error on mingw when
testing unistd-safer and fchdir together; and avoid gcc warning on
platforms without setmode.
* modules/unistd-safer-tests: New file.
* tests/test-dup-safer.c: Likewise.
* tests/test-cloexec.c (setmode): Avoid compiler warning.
* tests/test-dup2.c (setmode): Likewise.
* lib/cloexec.c (dup_cloexec): Fix mingw compile error.
Signed-off-by: Eric Blake <ebb9@byu.net>
author | Eric Blake <ebb9@byu.net> |
---|---|
date | Mon, 07 Dec 2009 10:17:07 -0700 |
parents | 8700bea78e67 |
children | 33ab12a7cea2 |
rev | line source |
---|---|
11727 | 1 /* Test duplicating file descriptors. |
2 Copyright (C) 2009 Free Software Foundation, Inc. | |
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 | |
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */ | |
16 | |
17 /* Written by Eric Blake <ebb9@byu.net>, 2009. */ | |
18 | |
19 #include <config.h> | |
20 | |
21 #include <unistd.h> | |
22 | |
23 #include <errno.h> | |
24 #include <fcntl.h> | |
25 #include <stdio.h> | |
26 #include <stdlib.h> | |
27 | |
12398
8700bea78e67
cloexec: preserve text vs. binary across dup_cloexec
Eric Blake <ebb9@byu.net>
parents:
12391
diff
changeset
|
28 #include "binary-io.h" |
12391 | 29 #include "cloexec.h" |
30 | |
11727 | 31 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__ |
32 /* Get declarations of the Win32 API functions. */ | |
33 # define WIN32_LEAN_AND_MEAN | |
34 # include <windows.h> | |
35 #endif | |
36 | |
37 #define ASSERT(expr) \ | |
12391 | 38 do \ |
39 { \ | |
40 if (!(expr)) \ | |
41 { \ | |
11727 | 42 fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \ |
12391 | 43 fflush (stderr); \ |
44 abort (); \ | |
45 } \ | |
46 } \ | |
11727 | 47 while (0) |
48 | |
49 /* Return non-zero if FD is open. */ | |
50 static int | |
51 is_open (int fd) | |
52 { | |
53 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__ | |
54 /* On Win32, the initial state of unassigned standard file | |
55 descriptors is that they are open but point to an | |
56 INVALID_HANDLE_VALUE, and there is no fcntl. */ | |
57 return (HANDLE) _get_osfhandle (fd) != INVALID_HANDLE_VALUE; | |
58 #else | |
59 # ifndef F_GETFL | |
60 # error Please port fcntl to your platform | |
61 # endif | |
62 return 0 <= fcntl (fd, F_GETFL); | |
63 #endif | |
64 } | |
65 | |
12391 | 66 /* Return non-zero if FD is open and inheritable across exec/spawn. */ |
67 static int | |
68 is_inheritable (int fd) | |
69 { | |
70 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__ | |
71 /* On Win32, the initial state of unassigned standard file | |
72 descriptors is that they are open but point to an | |
73 INVALID_HANDLE_VALUE, and there is no fcntl. */ | |
74 HANDLE h = (HANDLE) _get_osfhandle (fd); | |
75 DWORD flags; | |
76 if (h == INVALID_HANDLE_VALUE || GetHandleInformation (h, &flags) == 0) | |
77 return 0; | |
78 return (flags & HANDLE_FLAG_INHERIT) != 0; | |
79 #else | |
80 # ifndef F_GETFD | |
81 # error Please port fcntl to your platform | |
82 # endif | |
83 int i = fcntl (fd, F_GETFD); | |
84 return 0 <= i && (i & FD_CLOEXEC) == 0; | |
85 #endif | |
86 } | |
87 | |
12398
8700bea78e67
cloexec: preserve text vs. binary across dup_cloexec
Eric Blake <ebb9@byu.net>
parents:
12391
diff
changeset
|
88 #if !O_BINARY |
12399 | 89 # define setmode(f,m) zero () |
90 static int zero (void) { return 0; } | |
12398
8700bea78e67
cloexec: preserve text vs. binary across dup_cloexec
Eric Blake <ebb9@byu.net>
parents:
12391
diff
changeset
|
91 #endif |
8700bea78e67
cloexec: preserve text vs. binary across dup_cloexec
Eric Blake <ebb9@byu.net>
parents:
12391
diff
changeset
|
92 |
8700bea78e67
cloexec: preserve text vs. binary across dup_cloexec
Eric Blake <ebb9@byu.net>
parents:
12391
diff
changeset
|
93 /* Return non-zero if FD is open in the given MODE, which is either |
8700bea78e67
cloexec: preserve text vs. binary across dup_cloexec
Eric Blake <ebb9@byu.net>
parents:
12391
diff
changeset
|
94 O_TEXT or O_BINARY. */ |
8700bea78e67
cloexec: preserve text vs. binary across dup_cloexec
Eric Blake <ebb9@byu.net>
parents:
12391
diff
changeset
|
95 static int |
8700bea78e67
cloexec: preserve text vs. binary across dup_cloexec
Eric Blake <ebb9@byu.net>
parents:
12391
diff
changeset
|
96 is_mode (int fd, int mode) |
8700bea78e67
cloexec: preserve text vs. binary across dup_cloexec
Eric Blake <ebb9@byu.net>
parents:
12391
diff
changeset
|
97 { |
8700bea78e67
cloexec: preserve text vs. binary across dup_cloexec
Eric Blake <ebb9@byu.net>
parents:
12391
diff
changeset
|
98 int value = setmode (fd, O_BINARY); |
8700bea78e67
cloexec: preserve text vs. binary across dup_cloexec
Eric Blake <ebb9@byu.net>
parents:
12391
diff
changeset
|
99 setmode (fd, value); |
8700bea78e67
cloexec: preserve text vs. binary across dup_cloexec
Eric Blake <ebb9@byu.net>
parents:
12391
diff
changeset
|
100 return mode == value; |
8700bea78e67
cloexec: preserve text vs. binary across dup_cloexec
Eric Blake <ebb9@byu.net>
parents:
12391
diff
changeset
|
101 } |
8700bea78e67
cloexec: preserve text vs. binary across dup_cloexec
Eric Blake <ebb9@byu.net>
parents:
12391
diff
changeset
|
102 |
11727 | 103 int |
12197
e45d9bb2233e
tests: avoid several compiler warnings
Eric Blake <ebb9@byu.net>
parents:
12154
diff
changeset
|
104 main (void) |
11727 | 105 { |
106 const char *file = "test-dup2.tmp"; | |
107 char buffer[1]; | |
11885 | 108 int fd = open (file, O_CREAT | O_TRUNC | O_RDWR, 0600); |
11727 | 109 |
11825 | 110 /* Assume std descriptors were provided by invoker. */ |
111 ASSERT (STDERR_FILENO < fd); | |
11727 | 112 ASSERT (is_open (fd)); |
11825 | 113 /* Ignore any other fd's leaked into this process. */ |
114 close (fd + 1); | |
115 close (fd + 2); | |
11727 | 116 ASSERT (!is_open (fd + 1)); |
117 ASSERT (!is_open (fd + 2)); | |
118 | |
119 /* Assigning to self must be a no-op. */ | |
120 ASSERT (dup2 (fd, fd) == fd); | |
121 ASSERT (is_open (fd)); | |
122 | |
12154 | 123 /* The source must be valid. */ |
124 errno = 0; | |
125 ASSERT (dup2 (-1, fd) == -1); | |
126 ASSERT (errno == EBADF); | |
127 errno = 0; | |
128 ASSERT (dup2 (AT_FDCWD, fd) == -1); | |
129 ASSERT (errno == EBADF); | |
130 ASSERT (is_open (fd)); | |
131 | |
11727 | 132 /* If the source is not open, then the destination is unaffected. */ |
133 errno = 0; | |
134 ASSERT (dup2 (fd + 1, fd + 1) == -1); | |
135 ASSERT (errno == EBADF); | |
136 ASSERT (!is_open (fd + 1)); | |
137 errno = 0; | |
138 ASSERT (dup2 (fd + 1, fd) == -1); | |
139 ASSERT (errno == EBADF); | |
140 ASSERT (is_open (fd)); | |
141 | |
142 /* The destination must be valid. */ | |
143 errno = 0; | |
144 ASSERT (dup2 (fd, -2) == -1); | |
145 ASSERT (errno == EBADF); | |
11885 | 146 errno = 0; |
147 ASSERT (dup2 (fd, 10000000) == -1); | |
148 ASSERT (errno == EBADF); | |
11727 | 149 |
150 /* Using dup2 can skip fds. */ | |
151 ASSERT (dup2 (fd, fd + 2) == fd + 2); | |
152 ASSERT (is_open (fd)); | |
153 ASSERT (!is_open (fd + 1)); | |
154 ASSERT (is_open (fd + 2)); | |
155 | |
11885 | 156 /* Verify that dup2 closes the previous occupant of a fd. */ |
11727 | 157 ASSERT (open ("/dev/null", O_WRONLY, 0600) == fd + 1); |
158 ASSERT (dup2 (fd + 1, fd) == fd); | |
159 ASSERT (close (fd + 1) == 0); | |
160 ASSERT (write (fd, "1", 1) == 1); | |
161 ASSERT (dup2 (fd + 2, fd) == fd); | |
11885 | 162 ASSERT (lseek (fd, 0, SEEK_END) == 0); |
11727 | 163 ASSERT (write (fd + 2, "2", 1) == 1); |
11885 | 164 ASSERT (lseek (fd, 0, SEEK_SET) == 0); |
11727 | 165 ASSERT (read (fd, buffer, 1) == 1); |
166 ASSERT (*buffer == '2'); | |
167 | |
12391 | 168 /* Any new fd created by dup2 must not be cloexec. */ |
169 ASSERT (close (fd + 2) == 0); | |
170 ASSERT (dup_cloexec (fd) == fd + 1); | |
171 ASSERT (!is_inheritable (fd + 1)); | |
172 ASSERT (dup2 (fd + 1, fd + 1) == fd + 1); | |
173 ASSERT (!is_inheritable (fd + 1)); | |
174 ASSERT (dup2 (fd + 1, fd + 2) == fd + 2); | |
175 ASSERT (is_inheritable (fd + 2)); | |
176 | |
12398
8700bea78e67
cloexec: preserve text vs. binary across dup_cloexec
Eric Blake <ebb9@byu.net>
parents:
12391
diff
changeset
|
177 /* On systems that distinguish between text and binary mode, dup2 |
8700bea78e67
cloexec: preserve text vs. binary across dup_cloexec
Eric Blake <ebb9@byu.net>
parents:
12391
diff
changeset
|
178 reuses the mode of the source. */ |
8700bea78e67
cloexec: preserve text vs. binary across dup_cloexec
Eric Blake <ebb9@byu.net>
parents:
12391
diff
changeset
|
179 setmode (fd, O_BINARY); |
8700bea78e67
cloexec: preserve text vs. binary across dup_cloexec
Eric Blake <ebb9@byu.net>
parents:
12391
diff
changeset
|
180 ASSERT (is_mode (fd, O_BINARY)); |
8700bea78e67
cloexec: preserve text vs. binary across dup_cloexec
Eric Blake <ebb9@byu.net>
parents:
12391
diff
changeset
|
181 ASSERT (dup2 (fd, fd + 1) == fd + 1); |
8700bea78e67
cloexec: preserve text vs. binary across dup_cloexec
Eric Blake <ebb9@byu.net>
parents:
12391
diff
changeset
|
182 ASSERT (is_mode (fd + 1, O_BINARY)); |
8700bea78e67
cloexec: preserve text vs. binary across dup_cloexec
Eric Blake <ebb9@byu.net>
parents:
12391
diff
changeset
|
183 setmode (fd, O_TEXT); |
8700bea78e67
cloexec: preserve text vs. binary across dup_cloexec
Eric Blake <ebb9@byu.net>
parents:
12391
diff
changeset
|
184 ASSERT (is_mode (fd, O_TEXT)); |
8700bea78e67
cloexec: preserve text vs. binary across dup_cloexec
Eric Blake <ebb9@byu.net>
parents:
12391
diff
changeset
|
185 ASSERT (dup2 (fd, fd + 1) == fd + 1); |
8700bea78e67
cloexec: preserve text vs. binary across dup_cloexec
Eric Blake <ebb9@byu.net>
parents:
12391
diff
changeset
|
186 ASSERT (is_mode (fd + 1, O_TEXT)); |
8700bea78e67
cloexec: preserve text vs. binary across dup_cloexec
Eric Blake <ebb9@byu.net>
parents:
12391
diff
changeset
|
187 |
11727 | 188 /* Clean up. */ |
189 ASSERT (close (fd + 2) == 0); | |
12391 | 190 ASSERT (close (fd + 1) == 0); |
11727 | 191 ASSERT (close (fd) == 0); |
192 ASSERT (unlink (file) == 0); | |
193 | |
194 return 0; | |
195 } |