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