diff options
Diffstat (limited to 'src/regress/lib/libc/sys/t_access.c')
-rw-r--r-- | src/regress/lib/libc/sys/t_access.c | 216 |
1 files changed, 216 insertions, 0 deletions
diff --git a/src/regress/lib/libc/sys/t_access.c b/src/regress/lib/libc/sys/t_access.c new file mode 100644 index 0000000000..33888c7039 --- /dev/null +++ b/src/regress/lib/libc/sys/t_access.c | |||
@@ -0,0 +1,216 @@ | |||
1 | /* $OpenBSD: t_access.c,v 1.1.1.1 2019/11/19 19:57:03 bluhm Exp $ */ | ||
2 | /* $NetBSD: t_access.c,v 1.2 2017/01/10 22:36:29 christos Exp $ */ | ||
3 | |||
4 | /*- | ||
5 | * Copyright (c) 2011 The NetBSD Foundation, Inc. | ||
6 | * All rights reserved. | ||
7 | * | ||
8 | * This code is derived from software contributed to The NetBSD Foundation | ||
9 | * by Jukka Ruohonen. | ||
10 | * | ||
11 | * Redistribution and use in source and binary forms, with or without | ||
12 | * modification, are permitted provided that the following conditions | ||
13 | * are met: | ||
14 | * 1. Redistributions of source code must retain the above copyright | ||
15 | * notice, this list of conditions and the following disclaimer. | ||
16 | * 2. Redistributions in binary form must reproduce the above copyright | ||
17 | * notice, this list of conditions and the following disclaimer in the | ||
18 | * documentation and/or other materials provided with the distribution. | ||
19 | * | ||
20 | * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS | ||
21 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED | ||
22 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | ||
23 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS | ||
24 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
25 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
26 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
27 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
28 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
29 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
30 | * POSSIBILITY OF SUCH DAMAGE. | ||
31 | */ | ||
32 | |||
33 | #include "macros.h" | ||
34 | |||
35 | #include <sys/cdefs.h> | ||
36 | __RCSID("$NetBSD: t_access.c,v 1.2 2017/01/10 22:36:29 christos Exp $"); | ||
37 | |||
38 | #include "atf-c.h" | ||
39 | |||
40 | #include <sys/stat.h> | ||
41 | |||
42 | #include <errno.h> | ||
43 | #include <fcntl.h> | ||
44 | #include <limits.h> | ||
45 | #include <stdint.h> | ||
46 | #include <stdlib.h> | ||
47 | #include <unistd.h> | ||
48 | |||
49 | static const char path[] = "access"; | ||
50 | static const int mode[4] = { R_OK, W_OK, X_OK, F_OK }; | ||
51 | |||
52 | ATF_TC_WITH_CLEANUP(access_access); | ||
53 | ATF_TC_HEAD(access_access, tc) | ||
54 | { | ||
55 | atf_tc_set_md_var(tc, "descr", "Test access(2) for EACCES"); | ||
56 | atf_tc_set_md_var(tc, "require.user", "unprivileged"); | ||
57 | } | ||
58 | |||
59 | ATF_TC_BODY(access_access, tc) | ||
60 | { | ||
61 | const int perm[3] = { 0200, 0400, 0000 }; | ||
62 | size_t i; | ||
63 | int fd; | ||
64 | |||
65 | fd = open(path, O_RDONLY | O_CREAT); | ||
66 | |||
67 | if (fd < 0) | ||
68 | return; | ||
69 | |||
70 | for (i = 0; i < __arraycount(mode) - 1; i++) { | ||
71 | |||
72 | ATF_REQUIRE(fchmod(fd, perm[i]) == 0); | ||
73 | |||
74 | errno = 0; | ||
75 | |||
76 | ATF_REQUIRE(access(path, mode[i]) != 0); | ||
77 | ATF_REQUIRE(errno == EACCES); | ||
78 | } | ||
79 | |||
80 | ATF_REQUIRE(close(fd) == 0); | ||
81 | } | ||
82 | |||
83 | ATF_TC_CLEANUP(access_access, tc) | ||
84 | { | ||
85 | (void)unlink(path); | ||
86 | } | ||
87 | |||
88 | ATF_TC(access_fault); | ||
89 | ATF_TC_HEAD(access_fault, tc) | ||
90 | { | ||
91 | atf_tc_set_md_var(tc, "descr", "Test access(2) for EFAULT"); | ||
92 | } | ||
93 | |||
94 | ATF_TC_BODY(access_fault, tc) | ||
95 | { | ||
96 | size_t i; | ||
97 | |||
98 | for (i = 0; i < __arraycount(mode); i++) { | ||
99 | |||
100 | errno = 0; | ||
101 | |||
102 | ATF_REQUIRE(access(NULL, mode[i]) != 0); | ||
103 | ATF_REQUIRE(errno == EFAULT); | ||
104 | |||
105 | errno = 0; | ||
106 | |||
107 | ATF_REQUIRE(access((char *)-1, mode[i]) != 0); | ||
108 | ATF_REQUIRE(errno == EFAULT); | ||
109 | } | ||
110 | } | ||
111 | |||
112 | ATF_TC(access_inval); | ||
113 | ATF_TC_HEAD(access_inval, tc) | ||
114 | { | ||
115 | atf_tc_set_md_var(tc, "descr", "Test access(2) for EINVAL"); | ||
116 | } | ||
117 | |||
118 | ATF_TC_BODY(access_inval, tc) | ||
119 | { | ||
120 | |||
121 | errno = 0; | ||
122 | |||
123 | ATF_REQUIRE(access("/usr", -1) != 0); | ||
124 | ATF_REQUIRE(errno == EINVAL); | ||
125 | } | ||
126 | |||
127 | ATF_TC(access_notdir); | ||
128 | ATF_TC_HEAD(access_notdir, tc) | ||
129 | { | ||
130 | atf_tc_set_md_var(tc, "descr", "Test access(2) for ENOTDIR"); | ||
131 | } | ||
132 | |||
133 | ATF_TC_BODY(access_notdir, tc) | ||
134 | { | ||
135 | size_t i; | ||
136 | |||
137 | for (i = 0; i < __arraycount(mode); i++) { | ||
138 | |||
139 | errno = 0; | ||
140 | |||
141 | /* | ||
142 | * IEEE Std 1003.1-2008 about ENOTDIR: | ||
143 | * | ||
144 | * "A component of the path prefix is not a directory, | ||
145 | * or the path argument contains at least one non-<slash> | ||
146 | * character and ends with one or more trailing <slash> | ||
147 | * characters and the last pathname component names an | ||
148 | * existing file that is neither a directory nor a symbolic | ||
149 | * link to a directory." | ||
150 | */ | ||
151 | ATF_REQUIRE(access("/etc/passwd//", mode[i]) != 0); | ||
152 | ATF_REQUIRE(errno == ENOTDIR); | ||
153 | } | ||
154 | } | ||
155 | |||
156 | ATF_TC(access_notexist); | ||
157 | ATF_TC_HEAD(access_notexist, tc) | ||
158 | { | ||
159 | atf_tc_set_md_var(tc, "descr", "Test access(2) for ENOENT"); | ||
160 | } | ||
161 | |||
162 | ATF_TC_BODY(access_notexist, tc) | ||
163 | { | ||
164 | size_t i; | ||
165 | |||
166 | for (i = 0; i < __arraycount(mode); i++) { | ||
167 | |||
168 | errno = 0; | ||
169 | |||
170 | ATF_REQUIRE(access("", mode[i]) != 0); | ||
171 | ATF_REQUIRE(errno == ENOENT); | ||
172 | } | ||
173 | } | ||
174 | |||
175 | ATF_TC(access_toolong); | ||
176 | ATF_TC_HEAD(access_toolong, tc) | ||
177 | { | ||
178 | atf_tc_set_md_var(tc, "descr", "Test access(2) for ENAMETOOLONG"); | ||
179 | } | ||
180 | |||
181 | ATF_TC_BODY(access_toolong, tc) | ||
182 | { | ||
183 | char *buf; | ||
184 | size_t i; | ||
185 | |||
186 | buf = malloc(PATH_MAX); | ||
187 | |||
188 | if (buf == NULL) | ||
189 | return; | ||
190 | |||
191 | for (i = 0; i < PATH_MAX; i++) | ||
192 | buf[i] = 'x'; | ||
193 | |||
194 | for (i = 0; i < __arraycount(mode); i++) { | ||
195 | |||
196 | errno = 0; | ||
197 | |||
198 | ATF_REQUIRE(access(buf, mode[i]) != 0); | ||
199 | ATF_REQUIRE(errno == ENAMETOOLONG); | ||
200 | } | ||
201 | |||
202 | free(buf); | ||
203 | } | ||
204 | |||
205 | ATF_TP_ADD_TCS(tp) | ||
206 | { | ||
207 | |||
208 | ATF_TP_ADD_TC(tp, access_access); | ||
209 | ATF_TP_ADD_TC(tp, access_fault); | ||
210 | ATF_TP_ADD_TC(tp, access_inval); | ||
211 | ATF_TP_ADD_TC(tp, access_notdir); | ||
212 | ATF_TP_ADD_TC(tp, access_notexist); | ||
213 | ATF_TP_ADD_TC(tp, access_toolong); | ||
214 | |||
215 | return atf_no_error(); | ||
216 | } | ||