tests: add signature checks
[gnulib.git] / tests / test-fchdir.c
1 /* Test changing to a directory named by a file descriptor.
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 "signature.h"
24 SIGNATURE_CHECK (fchdir, int, (int));
25
26 #include <errno.h>
27 #include <fcntl.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31
32 #include "cloexec.h"
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 int
47 main (void)
48 {
49   char *cwd = getcwd (NULL, 0);
50   int fd = open (".", O_RDONLY);
51   int i;
52
53   ASSERT (cwd);
54   ASSERT (0 <= fd);
55
56   /* Check for failure cases.  */
57   {
58     int bad_fd = open ("/dev/null", O_RDONLY);
59     ASSERT (0 <= bad_fd);
60     errno = 0;
61     ASSERT (fchdir (bad_fd) == -1);
62     ASSERT (errno == ENOTDIR);
63     ASSERT (close (bad_fd) == 0);
64     errno = 0;
65     ASSERT (fchdir (-1) == -1);
66     ASSERT (errno == EBADF);
67   }
68
69   /* Repeat test twice, once in '.' and once in '..'.  */
70   for (i = 0; i < 2; i++)
71     {
72       ASSERT (chdir (".." + 1 - i) == 0);
73       ASSERT (fchdir (fd) == 0);
74       {
75         size_t len = strlen (cwd) + 1;
76         char *new_dir = malloc (len);
77         ASSERT (new_dir);
78         ASSERT (getcwd (new_dir, len) == new_dir);
79         ASSERT (strcmp (cwd, new_dir) == 0);
80         free (new_dir);
81       }
82
83       /* For second iteration, use a cloned fd, to ensure that dup
84          remembers whether an fd was associated with a directory.  */
85       if (!i)
86         {
87           int new_fd = dup (fd);
88           ASSERT (0 <= new_fd);
89           ASSERT (close (fd) == 0);
90           ASSERT (dup2 (new_fd, fd) == fd);
91           ASSERT (close (new_fd) == 0);
92           ASSERT (dup_cloexec (fd) == new_fd);
93           ASSERT (dup2 (new_fd, fd) == fd);
94           ASSERT (close (new_fd) == 0);
95           ASSERT (fcntl (fd, F_DUPFD_CLOEXEC, new_fd) == new_fd);
96           ASSERT (close (fd) == 0);
97           ASSERT (fcntl (new_fd, F_DUPFD, fd) == fd);
98           ASSERT (close (new_fd) == 0);
99 #if GNULIB_DUP3
100           ASSERT (dup3 (fd, new_fd, 0) == new_fd);
101           ASSERT (dup3 (new_fd, fd, 0) == fd);
102           ASSERT (close (new_fd) == 0);
103 #endif
104         }
105     }
106
107   free (cwd);
108   return 0;
109 }