tests: add signature checks
[gnulib.git] / tests / test-dup3.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    and Bruno Haible <bruno@clisp.org>, 2009.  */
19
20 #include <config.h>
21
22 #include <unistd.h>
23
24 #include "signature.h"
25 SIGNATURE_CHECK (dup3, int, (int, int, int));
26
27 #include <errno.h>
28 #include <fcntl.h>
29 #include <stdbool.h>
30 #include <stdio.h>
31 #include <stdlib.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 #include "binary-io.h"
40
41 #define ASSERT(expr) \
42   do                                                                         \
43     {                                                                        \
44       if (!(expr))                                                           \
45         {                                                                    \
46           fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
47           fflush (stderr);                                                   \
48           abort ();                                                          \
49         }                                                                    \
50     }                                                                        \
51   while (0)
52
53 /* Return true if FD is open.  */
54 static bool
55 is_open (int fd)
56 {
57 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
58   /* On Win32, the initial state of unassigned standard file
59      descriptors is that they are open but point to an
60      INVALID_HANDLE_VALUE, and there is no fcntl.  */
61   return (HANDLE) _get_osfhandle (fd) != INVALID_HANDLE_VALUE;
62 #else
63 # ifndef F_GETFL
64 #  error Please port fcntl to your platform
65 # endif
66   return 0 <= fcntl (fd, F_GETFL);
67 #endif
68 }
69
70 /* Return true if FD is not inherited to child processes.  */
71 static bool
72 is_cloexec (int fd)
73 {
74 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
75   HANDLE h = (HANDLE) _get_osfhandle (fd);
76   DWORD flags;
77   ASSERT (GetHandleInformation (h, &flags));
78   return (flags & HANDLE_FLAG_INHERIT) == 0;
79 #else
80   int flags;
81   ASSERT ((flags = fcntl (fd, F_GETFD)) >= 0);
82   return (flags & FD_CLOEXEC) != 0;
83 #endif
84 }
85
86 int
87 main ()
88 {
89   int use_cloexec;
90
91 #if defined O_CLOEXEC
92   for (use_cloexec = 0; use_cloexec <= 1; use_cloexec++)
93 #else
94   use_cloexec = 0;
95 #endif
96     {
97       const char *file = "test-dup3.tmp";
98       int fd = open (file, O_CREAT | O_TRUNC | O_RDWR, 0600);
99       int o_flags;
100       char buffer[1];
101
102       o_flags = 0;
103 #if defined O_CLOEXEC
104       if (use_cloexec)
105         o_flags |= O_CLOEXEC;
106 #endif
107
108       /* Assume std descriptors were provided by invoker.  */
109       ASSERT (STDERR_FILENO < fd);
110       ASSERT (is_open (fd));
111       /* Ignore any other fd's leaked into this process.  */
112       close (fd + 1);
113       close (fd + 2);
114       ASSERT (!is_open (fd + 1));
115       ASSERT (!is_open (fd + 2));
116
117       /* Assigning to self is invalid.  */
118       errno = 0;
119       ASSERT (dup3 (fd, fd, o_flags) == -1);
120       ASSERT (errno == EINVAL);
121       ASSERT (is_open (fd));
122
123       /* If the source is not open, then the destination is unaffected.  */
124       errno = 0;
125       ASSERT (dup3 (fd + 1, fd + 2, o_flags) == -1);
126       ASSERT (errno == EBADF);
127       ASSERT (!is_open (fd + 2));
128       errno = 0;
129       ASSERT (dup3 (fd + 1, fd, o_flags) == -1);
130       ASSERT (errno == EBADF);
131       ASSERT (is_open (fd));
132
133       /* The destination must be valid.  */
134       errno = 0;
135       ASSERT (dup3 (fd, -2, o_flags) == -1);
136       ASSERT (errno == EBADF);
137       errno = 0;
138       ASSERT (dup3 (fd, 10000000, o_flags) == -1);
139       ASSERT (errno == EBADF);
140
141       /* Using dup3 can skip fds.  */
142       ASSERT (dup3 (fd, fd + 2, o_flags) == fd + 2);
143       ASSERT (is_open (fd));
144       ASSERT (!is_open (fd + 1));
145       ASSERT (is_open (fd + 2));
146       if (use_cloexec)
147         ASSERT (is_cloexec (fd + 2));
148       else
149         ASSERT (!is_cloexec (fd + 2));
150
151       /* Verify that dup3 closes the previous occupant of a fd.  */
152       ASSERT (open ("/dev/null", O_WRONLY, 0600) == fd + 1);
153       ASSERT (dup3 (fd + 1, fd, o_flags) == fd);
154       ASSERT (close (fd + 1) == 0);
155       ASSERT (write (fd, "1", 1) == 1);
156       ASSERT (dup3 (fd + 2, fd, o_flags) == fd);
157       ASSERT (lseek (fd, 0, SEEK_END) == 0);
158       ASSERT (write (fd + 2, "2", 1) == 1);
159       ASSERT (lseek (fd, 0, SEEK_SET) == 0);
160       ASSERT (read (fd, buffer, 1) == 1);
161       ASSERT (*buffer == '2');
162
163       /* Clean up.  */
164       ASSERT (close (fd + 2) == 0);
165       ASSERT (close (fd) == 0);
166       ASSERT (unlink (file) == 0);
167     }
168
169   return 0;
170 }