unistd-safer: add unit test
[gnulib.git] / tests / test-dup-safer.c
1 /* Test that dup_safer leaves standard fds alone.
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 <fcntl.h>
24 #include <errno.h>
25 #include <stdbool.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <sys/stat.h>
29
30 #include "binary-io.h"
31 #include "cloexec.h"
32
33 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
34 /* Get declarations of the Win32 API functions.  */
35 # define WIN32_LEAN_AND_MEAN
36 # include <windows.h>
37 #endif
38
39 #if !O_BINARY
40 # define setmode(f,m) zero ()
41 static int zero (void) { return 0; }
42 #endif
43 #ifndef O_CLOEXEC
44 # define O_CLOEXEC 0
45 #endif
46
47 /* This test intentionally closes stderr.  So, we arrange to have fd 10
48    (outside the range of interesting fd's during the test) set up to
49    duplicate the original stderr.  */
50
51 #define BACKUP_STDERR_FILENO 10
52 static FILE *myerr;
53
54 #define ASSERT(expr) \
55   do                                                                         \
56     {                                                                        \
57       if (!(expr))                                                           \
58         {                                                                    \
59           fprintf (myerr, "%s:%d: assertion failed\n", __FILE__, __LINE__);  \
60           fflush (myerr);                                                    \
61           abort ();                                                          \
62         }                                                                    \
63     }                                                                        \
64   while (0)
65
66 /* Return true if FD is open.  */
67 static bool
68 is_open (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   return (HANDLE) _get_osfhandle (fd) != INVALID_HANDLE_VALUE;
75 #else
76 # ifndef F_GETFL
77 #  error Please port fcntl to your platform
78 # endif
79   return 0 <= fcntl (fd, F_GETFL);
80 #endif
81 }
82
83 /* Return true if FD is open and inheritable across exec/spawn.  */
84 static bool
85 is_inheritable (int fd)
86 {
87 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
88   /* On Win32, the initial state of unassigned standard file
89      descriptors is that they are open but point to an
90      INVALID_HANDLE_VALUE, and there is no fcntl.  */
91   HANDLE h = (HANDLE) _get_osfhandle (fd);
92   DWORD flags;
93   if (h == INVALID_HANDLE_VALUE || GetHandleInformation (h, &flags) == 0)
94     return 0;
95   return (flags & HANDLE_FLAG_INHERIT) != 0;
96 #else
97 # ifndef F_GETFD
98 #  error Please port fcntl to your platform
99 # endif
100   int i = fcntl (fd, F_GETFD);
101   return 0 <= i && (i & FD_CLOEXEC) == 0;
102 #endif
103 }
104
105 /* Return true if FD is open in the given MODE, which is either
106    O_TEXT or O_BINARY.  */
107 static bool
108 is_mode (int fd, int mode)
109 {
110   int value = setmode (fd, O_BINARY);
111   setmode (fd, value);
112   return mode == value;
113 }
114
115 #define witness "test-dup-safer.txt"
116
117 int
118 main (void)
119 {
120   int i;
121   int fd;
122
123   /* We close fd 2 later, so save it in fd 10.  */
124   if (dup2 (STDERR_FILENO, BACKUP_STDERR_FILENO) != BACKUP_STDERR_FILENO
125       || (myerr = fdopen (BACKUP_STDERR_FILENO, "w")) == NULL)
126     return 2;
127
128   /* Create file for later checks.  */
129   fd = creat (witness, 0600);
130   ASSERT (STDERR_FILENO < fd);
131
132   /* Four iterations, with progressively more standard descriptors
133      closed.  */
134   for (i = -1; i <= STDERR_FILENO; i++)
135     {
136       if (0 <= i)
137         ASSERT (close (i) == 0);
138
139       /* Detect errors.  */
140       errno = 0;
141       ASSERT (dup (-1) == -1);
142       ASSERT (errno == EBADF);
143       errno = 0;
144       ASSERT (dup (10000000) == -1);
145       ASSERT (errno == EBADF);
146       close (fd + 1);
147       errno = 0;
148       ASSERT (dup (fd + 1) == -1);
149       ASSERT (errno == EBADF);
150
151       /* Preserve text vs. binary.  */
152       setmode (fd, O_BINARY);
153       ASSERT (dup (fd) == fd + 1);
154       ASSERT (is_open (fd + 1));
155       ASSERT (is_inheritable (fd + 1));
156       ASSERT (is_mode (fd + 1, O_BINARY));
157
158       ASSERT (close (fd + 1) == 0);
159       setmode (fd, O_TEXT);
160       ASSERT (dup (fd) == fd + 1);
161       ASSERT (is_open (fd + 1));
162       ASSERT (is_inheritable (fd + 1));
163       ASSERT (is_mode (fd + 1, O_TEXT));
164
165       /* Create cloexec copy.  */
166       ASSERT (close (fd + 1) == 0);
167       ASSERT (fd_safer_flag (dup_cloexec (fd), O_CLOEXEC) == fd + 1);
168       ASSERT (set_cloexec_flag (fd + 1, true) == 0);
169       ASSERT (is_open (fd + 1));
170       ASSERT (!is_inheritable (fd + 1));
171       ASSERT (close (fd) == 0);
172
173       /* dup always creates inheritable copies.  Also, check that
174          earliest slot past std fds is used.  */
175       ASSERT (dup (fd + 1) == fd);
176       ASSERT (is_open (fd));
177       ASSERT (is_inheritable (fd));
178       ASSERT (close (fd + 1) == 0);
179     }
180
181   /* Cleanup.  */
182   ASSERT (close (fd) == 0);
183   ASSERT (unlink (witness) == 0);
184
185   return 0;
186 }