openat-safer: new module
[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 int
49 main ()
50 {
51   int i;
52   int j;
53   int dfd;
54   int fd;
55   char buf[2];
56   const char *witness = "test-openat-safer.txt";
57
58   /* We close fd 2 later, so save it in fd 10.  */
59   if (dup2 (STDERR_FILENO, BACKUP_STDERR_FILENO) != BACKUP_STDERR_FILENO
60       || (myerr = fdopen (BACKUP_STDERR_FILENO, "w")) == NULL)
61     return 2;
62
63   /* Create handle for future use.  */
64   dfd = openat (AT_FDCWD, ".", O_RDONLY);
65   ASSERT (STDERR_FILENO < dfd);
66
67   /* Create file for later checks.  */
68   remove (witness);
69   fd = openat (dfd, witness, O_WRONLY | O_CREAT | O_EXCL, 0600);
70   ASSERT (STDERR_FILENO < fd);
71   ASSERT (write (fd, "hi", 2) == 2);
72   ASSERT (close (fd) == 0);
73
74   /* Four iterations, with progressively more standard descriptors
75      closed.  */
76   for (i = -1; i <= STDERR_FILENO; i++)
77     {
78       ASSERT (fchdir (dfd) == 0);
79       if (0 <= i)
80         ASSERT (close (i) == 0);
81
82       /* Execute once in ".", once in "..".  */
83       for (j = 0; j <= 1; j++)
84         {
85           if (j)
86             ASSERT (chdir ("..") == 0);
87
88           /* Check for error detection.  */
89           errno = 0;
90           ASSERT (openat (AT_FDCWD, "", O_RDONLY) == -1);
91           ASSERT (errno == ENOENT);
92           errno = 0;
93           ASSERT (openat (dfd, "", O_RDONLY) == -1);
94           ASSERT (errno == ENOENT);
95
96           /* Check for trailing slash and /dev/null handling; the
97              particular errno might be ambiguous.  */
98           errno = 0;
99           ASSERT (openat (dfd, "nonexist.ent/", O_CREAT | O_RDONLY,
100                           S_IRUSR | S_IWUSR) == -1);
101           /* ASSERT (errno == ENOTDIR); */
102           errno = 0;
103           ASSERT (openat (dfd, "/dev/null/", O_RDONLY) == -1);
104           /* ASSERT (errno == ENOTDIR); */
105           fd = openat (dfd, "/dev/null", O_RDONLY);
106           ASSERT (STDERR_FILENO < fd);
107           ASSERT (close (fd) == 0);
108
109           /* Check for our witness file.  */
110           fd = openat (dfd, witness, O_RDONLY | O_NOFOLLOW);
111           ASSERT (STDERR_FILENO < fd);
112           ASSERT (read (fd, buf, 2) == 2);
113           ASSERT (buf[0] == 'h' && buf[1] == 'i');
114           ASSERT (close (fd) == 0);
115         }
116     }
117   ASSERT (fchdir (dfd) == 0);
118   ASSERT (unlink (witness) == 0);
119   ASSERT (close (dfd) == 0);
120
121   return 0;
122 }