Merge branch 'upstream'
[gnulib.git] / tests / test-mkfifoat.c
1 /* Tests of mkfifoat and mknodat.
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 <sys/stat.h>
22
23 #include <fcntl.h>
24 #include <errno.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <unistd.h>
29
30 #define ASSERT(expr) \
31   do                                                                         \
32     {                                                                        \
33       if (!(expr))                                                           \
34         {                                                                    \
35           fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__);  \
36           fflush (stderr);                                                   \
37           abort ();                                                          \
38         }                                                                    \
39     }                                                                        \
40   while (0)
41
42 typedef int (*test_func) (int, char const *, mode_t);
43
44 /* Wrapper to make testing mknodat easier.  */
45 static int
46 test_mknodat (int fd, char const *name, mode_t mode)
47 {
48   /* This is the only portable use of mknodat, per POSIX.  */
49   return mknodat (fd, name, mode | S_IFIFO, 0);
50 }
51
52 int
53 main ()
54 {
55   int i;
56   test_func funcs[2] = { mkfifoat, test_mknodat };
57   const char *fifo = "test-mkfifoat.fifo";
58
59   /* Create handle for future use.  */
60   int dfd = openat (AT_FDCWD, ".", O_RDONLY);
61   ASSERT (0 <= dfd);
62
63 #if !HAVE_MKFIFO
64   fputs ("skipping test: no support for named fifos\n", stderr);
65   return 77;
66 #endif
67
68   /* Clean up anything from previous incomplete test.  */
69   remove (fifo);
70
71   /* Test both functions.  */
72   for (i = 0; i < 2; i++)
73     {
74       struct stat st;
75       test_func func = funcs[i];
76
77       /* Sanity checks of failures.  */
78       errno = 0;
79       ASSERT (func (AT_FDCWD, "", 0600) == -1);
80       ASSERT (errno == ENOENT);
81       errno = 0;
82       ASSERT (func (dfd, "", S_IRUSR | S_IWUSR) == -1);
83       ASSERT (errno == ENOENT);
84       errno = 0;
85       ASSERT (func (AT_FDCWD, ".", 0600) == -1);
86       /* POSIX requires EEXIST, but Solaris gives EINVAL.  */
87       ASSERT (errno == EEXIST || errno == EINVAL);
88       errno = 0;
89       ASSERT (func (dfd, ".", 0600) == -1);
90       ASSERT (errno == EEXIST || errno == EINVAL);
91
92       /* Create fifo while cwd is '.', then stat it from '..'.  */
93       ASSERT (func (AT_FDCWD, fifo, 0600) == 0);
94       errno = 0;
95       ASSERT (func (dfd, fifo, 0600) == -1);
96       ASSERT (errno == EEXIST);
97       ASSERT (chdir ("..") == 0);
98       errno = 0;
99       ASSERT (fstatat (AT_FDCWD, fifo, &st, 0) == -1);
100       ASSERT (errno == ENOENT);
101       memset (&st, 0, sizeof st);
102       ASSERT (fstatat (dfd, fifo, &st, 0) == 0);
103       ASSERT (S_ISFIFO (st.st_mode));
104       ASSERT (unlinkat (dfd, fifo, 0) == 0);
105
106       /* Create fifo while cwd is '..', then stat it from '.'.  */
107       ASSERT (func (dfd, fifo, 0600) == 0);
108       ASSERT (fchdir (dfd) == 0);
109       errno = 0;
110       ASSERT (func (AT_FDCWD, fifo, 0600) == -1);
111       ASSERT (errno == EEXIST);
112       memset (&st, 0, sizeof st);
113       ASSERT (fstatat (AT_FDCWD, fifo, &st, AT_SYMLINK_NOFOLLOW) == 0);
114       ASSERT (S_ISFIFO (st.st_mode));
115       memset (&st, 0, sizeof st);
116       ASSERT (fstatat (dfd, fifo, &st, AT_SYMLINK_NOFOLLOW) == 0);
117       ASSERT (S_ISFIFO (st.st_mode));
118       ASSERT (unlink (fifo) == 0);
119     }
120
121   ASSERT (close (dfd) == 0);
122
123   return 0;
124 }