sigprocmask: Fix configuration failure on Solaris 10 (regr. 2020-07-25).
[gnulib.git] / tests / test-access.c
blob76d1455b54fe9bedded4a787856b7fee7eec1b14
1 /* Tests of access.
2 Copyright (C) 2019-2020 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 #include <config.h>
19 #include <unistd.h>
21 #include "signature.h"
22 SIGNATURE_CHECK (access, int, (const char *, int));
24 #include <errno.h>
25 #include <fcntl.h>
26 #include <sys/stat.h>
27 #include "root-uid.h"
28 #include "macros.h"
30 /* mingw and MSVC 9 lack geteuid, so setup a dummy value. */
31 #if !HAVE_GETEUID
32 # define geteuid() ROOT_UID
33 #endif
35 #define BASE "test-access.t"
37 int
38 main ()
40 /* Remove anything from prior partial run. */
41 unlink (BASE "f");
42 unlink (BASE "f1");
43 unlink (BASE "f2");
46 errno = 0;
47 ASSERT (access (BASE "f", R_OK) == -1);
48 ASSERT (errno == ENOENT);
50 errno = 0;
51 ASSERT (access (BASE "f", W_OK) == -1);
52 ASSERT (errno == ENOENT);
54 errno = 0;
55 ASSERT (access (BASE "f", X_OK) == -1);
56 ASSERT (errno == ENOENT);
59 ASSERT (close (creat (BASE "f1", 0700)) == 0);
61 ASSERT (access (BASE "f1", R_OK) == 0);
62 ASSERT (access (BASE "f1", W_OK) == 0);
63 ASSERT (access (BASE "f1", X_OK) == 0);
66 ASSERT (close (creat (BASE "f2", 0600)) == 0);
67 ASSERT (chmod (BASE "f2", 0400) == 0);
69 ASSERT (access (BASE "f2", R_OK) == 0);
71 if (geteuid () != ROOT_UID)
73 errno = 0;
74 ASSERT (access (BASE "f2", W_OK) == -1);
75 ASSERT (errno == EACCES);
78 #if defined _WIN32 && !defined __CYGWIN__
79 /* X_OK works like R_OK. */
80 ASSERT (access (BASE "f2", X_OK) == 0);
81 #else
82 errno = 0;
83 ASSERT (access (BASE "f2", X_OK) == -1);
84 ASSERT (errno == EACCES);
85 #endif
88 /* Cleanup. */
89 unlink (BASE "f1");
90 unlink (BASE "f2");
92 return 0;