1 /* Tests of mkfifoat and mknodat.
2 Copyright (C) 2009 Free Software Foundation, Inc.
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.
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.
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/>. */
17 /* Written by Eric Blake <ebb9@byu.net>, 2009. */
31 #define ASSERT(expr) \
36 fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
43 #define BASE "test-mkfifoat.t"
45 #include "test-mkfifo.h"
47 typedef int (*test_func) (int, char const *, mode_t);
49 static int dfd = AT_FDCWD;
51 /* Wrapper to test mknodat like mkfifoat. */
53 test_mknodat (int fd, char const *name, mode_t mode)
55 /* This is the only portable use of mknodat, per POSIX. */
56 return mknodat (fd, name, mode | S_IFIFO, 0);
59 /* Wrapper to test mkfifoat like mkfifo. */
61 do_mkfifoat (char const *name, mode_t mode)
63 return mkfifoat (dfd, name, mode);
66 /* Wrapper to test mknodat like mkfifo. */
68 do_mknodat (char const *name, mode_t mode)
70 return mknodat (dfd, name, mode | S_IFIFO, 0);
77 test_func funcs[2] = { mkfifoat, test_mknodat };
80 /* Remove any leftovers from a previous partial run. */
81 ASSERT (system ("rm -rf " BASE "*") == 0);
84 result = test_mkfifo (do_mkfifoat, true);
85 ASSERT (test_mkfifo (do_mknodat, false) == result);
86 dfd = open (".", O_RDONLY);
88 ASSERT (test_mkfifo (do_mkfifoat, false) == result);
89 ASSERT (test_mkfifo (do_mknodat, false) == result);
91 /* Test directory-relative handling of both functions. */
92 for (i = 0; i < 2; i++)
95 test_func func = funcs[i];
97 /* Create fifo while cwd is '.', then stat it from '..'. */
98 ASSERT (func (AT_FDCWD, BASE "fifo", 0600) == 0);
100 ASSERT (func (dfd, BASE "fifo", 0600) == -1);
101 ASSERT (errno == EEXIST);
102 ASSERT (chdir ("..") == 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);
111 /* Create fifo while cwd is '..', then stat it from '.'. */
112 ASSERT (func (dfd, BASE "fifo", 0600) == 0);
113 ASSERT (fchdir (dfd) == 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);
126 ASSERT (close (dfd) == 0);