1 /* Test checking user's permissions for a file.
2 Copyright (C) 2011-2025 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 <https://www.gnu.org/licenses/>. */
21 #include "signature.h"
22 SIGNATURE_CHECK (faccessat
, int, (int, const char *, int, int));
29 #define BASE "test-faccessat.t"
34 /* Test behaviour for invalid file descriptors. */
37 ASSERT (faccessat (-1, "foo", F_OK
, 0) == -1);
38 ASSERT (errno
== EBADF
);
43 ASSERT (faccessat (99, "foo", F_OK
, 0) == -1);
44 ASSERT (errno
== EBADF
);
47 /* Test behavior with trailing slash. */
50 ASSERT (faccessat (AT_FDCWD
, ".", X_OK
, 0) == 0);
51 ASSERT (faccessat (AT_FDCWD
, "./", X_OK
, 0) == 0);
52 ASSERT (close (open (BASE
"file", O_CREAT
| O_WRONLY
, 0)) == 0);
53 ASSERT (faccessat (AT_FDCWD
, BASE
"file", F_OK
, 0) == 0);
55 ASSERT (faccessat (AT_FDCWD
, BASE
"file/", F_OK
, 0) != 0);
56 ASSERT (errno
== ENOTDIR
);
57 ASSERT (faccessat (AT_FDCWD
, BASE
"file/", R_OK
, 0) != 0);
58 ASSERT (errno
== ENOTDIR
);
59 ASSERT (faccessat (AT_FDCWD
, BASE
"file/", W_OK
, 0) != 0);
60 ASSERT (errno
== ENOTDIR
);
61 ASSERT (faccessat (AT_FDCWD
, BASE
"file/", X_OK
, 0) != 0);
62 ASSERT (errno
== ENOTDIR
);
64 unlink (BASE
"link1");
65 if (symlink (".", BASE
"link1") == 0)
67 ASSERT (faccessat (AT_FDCWD
, BASE
"link1", X_OK
, 0) == 0);
68 ASSERT (faccessat (AT_FDCWD
, BASE
"link1/", X_OK
, 0) == 0);
70 unlink (BASE
"link2");
71 ASSERT (symlink (BASE
"file", BASE
"link2") == 0);
72 ASSERT (faccessat (AT_FDCWD
, BASE
"link2", F_OK
, 0) == 0);
73 ASSERT (faccessat (AT_FDCWD
, BASE
"link2/", F_OK
, 0) != 0);
74 ASSERT (errno
== ENOTDIR
);
75 ASSERT (faccessat (AT_FDCWD
, BASE
"link2/", R_OK
, 0) != 0);
76 ASSERT (errno
== ENOTDIR
|| errno
== EACCES
);
77 ASSERT (faccessat (AT_FDCWD
, BASE
"link2/", W_OK
, 0) != 0);
78 ASSERT (errno
== ENOTDIR
|| errno
== EACCES
);
79 ASSERT (faccessat (AT_FDCWD
, BASE
"link2/", X_OK
, 0) != 0);
80 ASSERT (errno
== ENOTDIR
|| errno
== EACCES
);
81 unlink (BASE
"link2");
83 unlink (BASE
"link3");
84 ASSERT (symlink (BASE
"no-such-file", BASE
"link3") == 0);
85 ASSERT (faccessat (AT_FDCWD
, BASE
"link3", F_OK
, 0) != 0);
86 ASSERT (errno
== ENOENT
);
87 ASSERT (faccessat (AT_FDCWD
, BASE
"link3/", F_OK
, 0) != 0);
88 ASSERT (errno
== ENOENT
);
89 ASSERT (faccessat (AT_FDCWD
, BASE
"link3/", R_OK
, 0) != 0);
90 ASSERT (errno
== ENOENT
);
91 ASSERT (faccessat (AT_FDCWD
, BASE
"link3/", W_OK
, 0) != 0);
92 ASSERT (errno
== ENOENT
);
93 ASSERT (faccessat (AT_FDCWD
, BASE
"link3/", X_OK
, 0) != 0);
94 ASSERT (errno
== ENOENT
);
95 unlink (BASE
"link3");
97 unlink (BASE
"link1");
100 return test_exit_status
;