fca3411b3e6842310c172fcbdac782406dcebc6e
[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 <stdbool.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <unistd.h>
30
31 #define ASSERT(expr) \
32   do                                                                         \
33     {                                                                        \
34       if (!(expr))                                                           \
35         {                                                                    \
36           fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__);  \
37           fflush (stderr);                                                   \
38           abort ();                                                          \
39         }                                                                    \
40     }                                                                        \
41   while (0)
42
43 #define BASE "test-mkfifoat.t"
44
45 #include "test-mkfifo.h"
46
47 typedef int (*test_func) (int, char const *, mode_t);
48
49 static int dfd = AT_FDCWD;
50
51 /* Wrapper to test mknodat like mkfifoat.  */
52 static int
53 test_mknodat (int fd, char const *name, mode_t mode)
54 {
55   /* This is the only portable use of mknodat, per POSIX.  */
56   return mknodat (fd, name, mode | S_IFIFO, 0);
57 }
58
59 /* Wrapper to test mkfifoat like mkfifo.  */
60 static int
61 do_mkfifoat (char const *name, mode_t mode)
62 {
63   return mkfifoat (dfd, name, mode);
64 }
65
66 /* Wrapper to test mknodat like mkfifo.  */
67 static int
68 do_mknodat (char const *name, mode_t mode)
69 {
70   return mknodat (dfd, name, mode | S_IFIFO, 0);
71 }
72
73 int
74 main (void)
75 {
76   int i;
77   test_func funcs[2] = { mkfifoat, test_mknodat };
78   int result;
79
80   /* Remove any leftovers from a previous partial run.  */
81   ASSERT (system ("rm -rf " BASE "*") == 0);
82
83   /* Basic tests.  */
84   result = test_mkfifo (do_mkfifoat, true);
85   ASSERT (test_mkfifo (do_mknodat, false) == result);
86   dfd = open (".", O_RDONLY);
87   ASSERT (0 <= dfd);
88   ASSERT (test_mkfifo (do_mkfifoat, false) == result);
89   ASSERT (test_mkfifo (do_mknodat, false) == result);
90
91   /* Test directory-relative handling of both functions.  */
92   for (i = 0; i < 2; i++)
93     {
94       struct stat st;
95       test_func func = funcs[i];
96
97       /* Create fifo while cwd is '.', then stat it from '..'.  */
98       ASSERT (func (AT_FDCWD, BASE "fifo", 0600) == 0);
99       errno = 0;
100       ASSERT (func (dfd, BASE "fifo", 0600) == -1);
101       ASSERT (errno == EEXIST);
102       ASSERT (chdir ("..") == 0);
103       errno = 0;
104       ASSERT (fstatat (AT_FDCWD, BASE "fifo", &st, 0) == -1);
105       ASSERT (errno == ENOENT);
106       memset (&st, 0, sizeof st);
107       ASSERT (fstatat (dfd, BASE "fifo", &st, 0) == 0);
108       ASSERT (S_ISFIFO (st.st_mode));
109       ASSERT (unlinkat (dfd, BASE "fifo", 0) == 0);
110
111       /* Create fifo while cwd is '..', then stat it from '.'.  */
112       ASSERT (func (dfd, BASE "fifo", 0600) == 0);
113       ASSERT (fchdir (dfd) == 0);
114       errno = 0;
115       ASSERT (func (AT_FDCWD, BASE "fifo", 0600) == -1);
116       ASSERT (errno == EEXIST);
117       memset (&st, 0, sizeof st);
118       ASSERT (fstatat (AT_FDCWD, BASE "fifo", &st, AT_SYMLINK_NOFOLLOW) == 0);
119       ASSERT (S_ISFIFO (st.st_mode));
120       memset (&st, 0, sizeof st);
121       ASSERT (fstatat (dfd, BASE "fifo", &st, AT_SYMLINK_NOFOLLOW) == 0);
122       ASSERT (S_ISFIFO (st.st_mode));
123       ASSERT (unlink (BASE "fifo") == 0);
124     }
125
126   ASSERT (close (dfd) == 0);
127
128   return 0;
129 }