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