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. */
23 #include "signature.h"
24 SIGNATURE_CHECK (mkfifoat, int, (int, char const *, mode_t));
25 SIGNATURE_CHECK (mknodat, int, (int, char const *, mode_t, dev_t));
37 #define BASE "test-mkfifoat.t"
39 #include "test-mkfifo.h"
41 typedef int (*test_func) (int, char const *, mode_t);
43 static int dfd = AT_FDCWD;
45 /* Wrapper to test mknodat like mkfifoat. */
47 test_mknodat (int fd, char const *name, mode_t mode)
49 /* This is the only portable use of mknodat, per POSIX. */
50 return mknodat (fd, name, mode | S_IFIFO, 0);
53 /* Wrapper to test mkfifoat like mkfifo. */
55 do_mkfifoat (char const *name, mode_t mode)
57 return mkfifoat (dfd, name, mode);
60 /* Wrapper to test mknodat like mkfifo. */
62 do_mknodat (char const *name, mode_t mode)
64 return mknodat (dfd, name, mode | S_IFIFO, 0);
71 test_func funcs[2] = { mkfifoat, test_mknodat };
74 /* Remove any leftovers from a previous partial run. */
75 ASSERT (system ("rm -rf " BASE "*") == 0);
78 result = test_mkfifo (do_mkfifoat, true);
79 ASSERT (test_mkfifo (do_mknodat, false) == result);
80 dfd = open (".", O_RDONLY);
82 ASSERT (test_mkfifo (do_mkfifoat, false) == result);
83 ASSERT (test_mkfifo (do_mknodat, false) == result);
85 /* Test directory-relative handling of both functions. */
86 for (i = 0; i < 2; i++)
89 test_func func = funcs[i];
91 /* Create fifo while cwd is '.', then stat it from '..'. */
92 ASSERT (func (AT_FDCWD, BASE "fifo", 0600) == 0);
94 ASSERT (func (dfd, BASE "fifo", 0600) == -1);
95 ASSERT (errno == EEXIST);
96 ASSERT (chdir ("..") == 0);
98 ASSERT (fstatat (AT_FDCWD, BASE "fifo", &st, 0) == -1);
99 ASSERT (errno == ENOENT);
100 memset (&st, 0, sizeof st);
101 ASSERT (fstatat (dfd, BASE "fifo", &st, 0) == 0);
102 ASSERT (S_ISFIFO (st.st_mode));
103 ASSERT (unlinkat (dfd, BASE "fifo", 0) == 0);
105 /* Create fifo while cwd is '..', then stat it from '.'. */
106 ASSERT (func (dfd, BASE "fifo", 0600) == 0);
107 ASSERT (fchdir (dfd) == 0);
109 ASSERT (func (AT_FDCWD, BASE "fifo", 0600) == -1);
110 ASSERT (errno == EEXIST);
111 memset (&st, 0, sizeof st);
112 ASSERT (fstatat (AT_FDCWD, BASE "fifo", &st, AT_SYMLINK_NOFOLLOW) == 0);
113 ASSERT (S_ISFIFO (st.st_mode));
114 memset (&st, 0, sizeof st);
115 ASSERT (fstatat (dfd, BASE "fifo", &st, AT_SYMLINK_NOFOLLOW) == 0);
116 ASSERT (S_ISFIFO (st.st_mode));
117 ASSERT (unlink (BASE "fifo") == 0);
120 ASSERT (close (dfd) == 0);