1 /* Tests of access and euidaccess.
2 Copyright (C) 2019-2024 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/>. */
17 /* mingw and MSVC 9 lack geteuid, so setup a dummy value. */
19 # define geteuid() ROOT_UID
23 test_access (int (*func
) (const char * /*file*/, int /*mode*/))
25 /* Remove anything from prior partial run. */
28 chmod (BASE
"f2", 0600);
34 ASSERT (func (BASE
"f", R_OK
) == -1);
35 ASSERT (errno
== ENOENT
);
38 ASSERT (func (BASE
"f", W_OK
) == -1);
39 ASSERT (errno
== ENOENT
);
42 ASSERT (func (BASE
"f", X_OK
) == -1);
43 ASSERT (errno
== ENOENT
);
46 ASSERT (close (creat (BASE
"f1", 0700)) == 0);
48 ASSERT (func (BASE
"f1", F_OK
) == 0);
49 ASSERT (func (BASE
"f1", R_OK
) == 0);
50 ASSERT (func (BASE
"f1", W_OK
) == 0);
51 ASSERT (func (BASE
"f1", X_OK
) == 0);
53 ASSERT (func (BASE
"f1/", F_OK
) == -1);
54 ASSERT (errno
== ENOTDIR
);
55 ASSERT (func (BASE
"f1/", R_OK
) == -1);
56 ASSERT (errno
== ENOTDIR
);
57 ASSERT (func (BASE
"f1/", W_OK
) == -1);
58 ASSERT (errno
== ENOTDIR
);
59 ASSERT (func (BASE
"f1/", X_OK
) == -1);
60 ASSERT (errno
== ENOTDIR
);
62 if (symlink (BASE
"f1", BASE
"sl") == 0)
64 ASSERT (func (BASE
"sl/", F_OK
) == -1);
65 ASSERT (errno
== ENOTDIR
);
66 ASSERT (func (BASE
"sl/", R_OK
) == -1);
67 ASSERT (errno
== ENOTDIR
);
68 ASSERT (func (BASE
"sl/", W_OK
) == -1);
69 ASSERT (errno
== ENOTDIR
);
70 ASSERT (func (BASE
"sl/", X_OK
) == -1);
71 ASSERT (errno
== ENOTDIR
);
75 ASSERT (close (creat (BASE
"f2", 0600)) == 0);
76 ASSERT (chmod (BASE
"f2", 0400) == 0);
78 ASSERT (func (BASE
"f2", R_OK
) == 0);
80 /* On Cygwin, for users that are in the 'Administrators' group,
82 #if !defined __CYGWIN__
83 if (geteuid () != ROOT_UID
)
86 ASSERT (func (BASE
"f2", W_OK
) == -1);
87 ASSERT (errno
== EACCES
);
91 #if defined _WIN32 && !defined __CYGWIN__
92 /* X_OK works like R_OK. */
93 ASSERT (func (BASE
"f2", X_OK
) == 0);
95 /* On Solaris, for the root user, X_OK is allowed. */
97 if (geteuid () != ROOT_UID
)
101 ASSERT (func (BASE
"f2", X_OK
) == -1);
102 ASSERT (errno
== EACCES
);
108 ASSERT (unlink (BASE
"f1") == 0);
109 ASSERT (chmod (BASE
"f2", 0600) == 0);
110 ASSERT (unlink (BASE
"f2") == 0);