test-dup2: enhance test
[gnulib.git] / tests / test-dup2.c
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
28 #include "cloexec.h"
29
30 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
31 /* Get declarations of the Win32 API functions.  */
32 # define WIN32_LEAN_AND_MEAN
33 # include <windows.h>
34 #endif
35
36 #define ASSERT(expr) \
37   do                                                                         \
38     {                                                                        \
39       if (!(expr))                                                           \
40         {                                                                    \
41           fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
42           fflush (stderr);                                                   \
43           abort ();                                                          \
44         }                                                                    \
45     }                                                                        \
46   while (0)
47
48 /* Return non-zero if FD is open.  */
49 static int
50 is_open (int fd)
51 {
52 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
53   /* On Win32, the initial state of unassigned standard file
54      descriptors is that they are open but point to an
55      INVALID_HANDLE_VALUE, and there is no fcntl.  */
56   return (HANDLE) _get_osfhandle (fd) != INVALID_HANDLE_VALUE;
57 #else
58 # ifndef F_GETFL
59 #  error Please port fcntl to your platform
60 # endif
61   return 0 <= fcntl (fd, F_GETFL);
62 #endif
63 }
64
65 /* Return non-zero if FD is open and inheritable across exec/spawn.  */
66 static int
67 is_inheritable (int fd)
68 {
69 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
70   /* On Win32, the initial state of unassigned standard file
71      descriptors is that they are open but point to an
72      INVALID_HANDLE_VALUE, and there is no fcntl.  */
73   HANDLE h = (HANDLE) _get_osfhandle (fd);
74   DWORD flags;
75   if (h == INVALID_HANDLE_VALUE || GetHandleInformation (h, &flags) == 0)
76     return 0;
77   return (flags & HANDLE_FLAG_INHERIT) != 0;
78 #else
79 # ifndef F_GETFD
80 #  error Please port fcntl to your platform
81 # endif
82   int i = fcntl (fd, F_GETFD);
83   return 0 <= i && (i & FD_CLOEXEC) == 0;
84 #endif
85 }
86
87 int
88 main (void)
89 {
90   const char *file = "test-dup2.tmp";
91   char buffer[1];
92   int fd = open (file, O_CREAT | O_TRUNC | O_RDWR, 0600);
93
94   /* Assume std descriptors were provided by invoker.  */
95   ASSERT (STDERR_FILENO < fd);
96   ASSERT (is_open (fd));
97   /* Ignore any other fd's leaked into this process.  */
98   close (fd + 1);
99   close (fd + 2);
100   ASSERT (!is_open (fd + 1));
101   ASSERT (!is_open (fd + 2));
102
103   /* Assigning to self must be a no-op.  */
104   ASSERT (dup2 (fd, fd) == fd);
105   ASSERT (is_open (fd));
106
107   /* The source must be valid.  */
108   errno = 0;
109   ASSERT (dup2 (-1, fd) == -1);
110   ASSERT (errno == EBADF);
111   errno = 0;
112   ASSERT (dup2 (AT_FDCWD, fd) == -1);
113   ASSERT (errno == EBADF);
114   ASSERT (is_open (fd));
115
116   /* If the source is not open, then the destination is unaffected.  */
117   errno = 0;
118   ASSERT (dup2 (fd + 1, fd + 1) == -1);
119   ASSERT (errno == EBADF);
120   ASSERT (!is_open (fd + 1));
121   errno = 0;
122   ASSERT (dup2 (fd + 1, fd) == -1);
123   ASSERT (errno == EBADF);
124   ASSERT (is_open (fd));
125
126   /* The destination must be valid.  */
127   errno = 0;
128   ASSERT (dup2 (fd, -2) == -1);
129   ASSERT (errno == EBADF);
130   errno = 0;
131   ASSERT (dup2 (fd, 10000000) == -1);
132   ASSERT (errno == EBADF);
133
134   /* Using dup2 can skip fds.  */
135   ASSERT (dup2 (fd, fd + 2) == fd + 2);
136   ASSERT (is_open (fd));
137   ASSERT (!is_open (fd + 1));
138   ASSERT (is_open (fd + 2));
139
140   /* Verify that dup2 closes the previous occupant of a fd.  */
141   ASSERT (open ("/dev/null", O_WRONLY, 0600) == fd + 1);
142   ASSERT (dup2 (fd + 1, fd) == fd);
143   ASSERT (close (fd + 1) == 0);
144   ASSERT (write (fd, "1", 1) == 1);
145   ASSERT (dup2 (fd + 2, fd) == fd);
146   ASSERT (lseek (fd, 0, SEEK_END) == 0);
147   ASSERT (write (fd + 2, "2", 1) == 1);
148   ASSERT (lseek (fd, 0, SEEK_SET) == 0);
149   ASSERT (read (fd, buffer, 1) == 1);
150   ASSERT (*buffer == '2');
151
152   /* Any new fd created by dup2 must not be cloexec.  */
153   ASSERT (close (fd + 2) == 0);
154   ASSERT (dup_cloexec (fd) == fd + 1);
155   ASSERT (!is_inheritable (fd + 1));
156   ASSERT (dup2 (fd + 1, fd + 1) == fd + 1);
157   ASSERT (!is_inheritable (fd + 1));
158   ASSERT (dup2 (fd + 1, fd + 2) == fd + 2);
159   ASSERT (is_inheritable (fd + 2));
160
161   /* Clean up.  */
162   ASSERT (close (fd + 2) == 0);
163   ASSERT (close (fd + 1) == 0);
164   ASSERT (close (fd) == 0);
165   ASSERT (unlink (file) == 0);
166
167   return 0;
168 }