test-open: support mingw errno values
[gnulib.git] / tests / test-openat-safer.c
1 /* Test that openat_safer leave 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 "fcntl--.h"
22
23 #include <errno.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <sys/stat.h>
27 #include <unistd.h>
28
29 /* This test intentionally closes stderr.  So, we arrange to have fd 10
30    (outside the range of interesting fd's during the test) set up to
31    duplicate the original stderr.  */
32
33 #define BACKUP_STDERR_FILENO 10
34 static FILE *myerr;
35
36 #define ASSERT(expr) \
37   do                                                                         \
38     {                                                                        \
39       if (!(expr))                                                           \
40         {                                                                    \
41           fprintf (myerr, "%s:%d: assertion failed\n", __FILE__, __LINE__);  \
42           fflush (myerr);                                                    \
43           abort ();                                                          \
44         }                                                                    \
45     }                                                                        \
46   while (0)
47
48 #define witness "test-openat-safer.txt"
49
50 int
51 main ()
52 {
53   int i;
54   int j;
55   int dfd;
56   int fd;
57   char buf[2];
58
59   /* We close fd 2 later, so save it in fd 10.  */
60   if (dup2 (STDERR_FILENO, BACKUP_STDERR_FILENO) != BACKUP_STDERR_FILENO
61       || (myerr = fdopen (BACKUP_STDERR_FILENO, "w")) == NULL)
62     return 2;
63
64   /* Create handle for future use.  */
65   dfd = openat (AT_FDCWD, ".", O_RDONLY);
66   ASSERT (STDERR_FILENO < dfd);
67
68   /* Create file for later checks.  */
69   remove (witness);
70   fd = openat (dfd, witness, O_WRONLY | O_CREAT | O_EXCL, 0600);
71   ASSERT (STDERR_FILENO < fd);
72   ASSERT (write (fd, "hi", 2) == 2);
73   ASSERT (close (fd) == 0);
74
75   /* Four iterations, with progressively more standard descriptors
76      closed.  */
77   for (i = -1; i <= STDERR_FILENO; i++)
78     {
79       ASSERT (fchdir (dfd) == 0);
80       if (0 <= i)
81         ASSERT (close (i) == 0);
82
83       /* Execute once in ".", once in "..".  */
84       for (j = 0; j <= 1; j++)
85         {
86           if (j)
87             ASSERT (chdir ("..") == 0);
88
89           /* Check for error detection.  */
90           errno = 0;
91           ASSERT (openat (AT_FDCWD, "", O_RDONLY) == -1);
92           ASSERT (errno == ENOENT);
93           errno = 0;
94           ASSERT (openat (dfd, "", O_RDONLY) == -1);
95           ASSERT (errno == ENOENT);
96           errno = 0;
97           ASSERT (openat (-1, ".", O_RDONLY) == -1);
98           ASSERT (errno == EBADF);
99
100           /* Check for trailing slash and /dev/null handling.  */
101           errno = 0;
102           ASSERT (openat (dfd, "nonexist.ent/", O_CREAT | O_RDONLY,
103                           S_IRUSR | S_IWUSR) == -1);
104           ASSERT (errno == ENOTDIR || errno == EISDIR || errno == ENOENT
105                   || errno == EINVAL);
106           errno = 0;
107           ASSERT (openat (dfd, witness "/", O_RDONLY) == -1);
108           ASSERT (errno == ENOTDIR || errno == EISDIR || errno == EINVAL);
109           /* Using a bad directory is okay for absolute paths.  */
110           fd = openat (-1, "/dev/null", O_WRONLY);
111           ASSERT (STDERR_FILENO < fd);
112           /* Using a non-directory is wrong for relative paths.  */
113           errno = 0;
114           ASSERT (openat (fd, ".", O_RDONLY) == -1);
115           ASSERT (errno == EBADF || errno == ENOTDIR);
116           ASSERT (close (fd) == 0);
117
118           /* Check for our witness file.  */
119           fd = openat (dfd, witness, O_RDONLY | O_NOFOLLOW);
120           ASSERT (STDERR_FILENO < fd);
121           ASSERT (read (fd, buf, 2) == 2);
122           ASSERT (buf[0] == 'h' && buf[1] == 'i');
123           ASSERT (close (fd) == 0);
124         }
125     }
126   ASSERT (fchdir (dfd) == 0);
127   ASSERT (unlink (witness) == 0);
128   ASSERT (close (dfd) == 0);
129
130   return 0;
131 }