xref: /freebsd/contrib/netbsd-tests/lib/libc/sys/t_access.c (revision 6186fd1857626de0f7cb1a9e4dff19082f9ebb11)
1 /* $NetBSD: t_access.c,v 1.1 2011/07/07 06:57:53 jruoho 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.1 2011/07/07 06:57:53 jruoho Exp $");
33 
34 #include <errno.h>
35 #include <fcntl.h>
36 #include <limits.h>
37 #include <stdint.h>
38 #include <stdlib.h>
39 #include <unistd.h>
40 
41 #include <atf-c.h>
42 
43 static const char path[] = "access";
44 static const int mode[4] = { R_OK, W_OK, X_OK, F_OK };
45 
46 ATF_TC_WITH_CLEANUP(access_access);
47 ATF_TC_HEAD(access_access, tc)
48 {
49 	atf_tc_set_md_var(tc, "descr", "Test access(2) for EACCES");
50 	atf_tc_set_md_var(tc, "require.user", "unprivileged");
51 }
52 
53 ATF_TC_BODY(access_access, tc)
54 {
55 	const int perm[3] = { 0200, 0400, 0000 };
56 	size_t i;
57 	int fd;
58 
59 	fd = open(path, O_RDONLY | O_CREAT);
60 
61 	if (fd < 0)
62 		return;
63 
64 	for (i = 0; i < __arraycount(mode) - 1; i++) {
65 
66 		ATF_REQUIRE(fchmod(fd, perm[i]) == 0);
67 
68 		errno = 0;
69 
70 		ATF_REQUIRE(access(path, mode[i]) != 0);
71 		ATF_REQUIRE(errno == EACCES);
72 	}
73 
74 	ATF_REQUIRE(close(fd) == 0);
75 }
76 
77 ATF_TC_CLEANUP(access_access, tc)
78 {
79 	(void)unlink(path);
80 }
81 
82 ATF_TC(access_fault);
83 ATF_TC_HEAD(access_fault, tc)
84 {
85 	atf_tc_set_md_var(tc, "descr", "Test access(2) for EFAULT");
86 }
87 
88 ATF_TC_BODY(access_fault, tc)
89 {
90 	size_t i;
91 
92 	for (i = 0; i < __arraycount(mode); i++) {
93 
94 		errno = 0;
95 
96 		ATF_REQUIRE(access(NULL, mode[i]) != 0);
97 		ATF_REQUIRE(errno == EFAULT);
98 
99 		errno = 0;
100 
101 		ATF_REQUIRE(access((char *)-1, mode[i]) != 0);
102 		ATF_REQUIRE(errno == EFAULT);
103 	}
104 }
105 
106 ATF_TC(access_inval);
107 ATF_TC_HEAD(access_inval, tc)
108 {
109 	atf_tc_set_md_var(tc, "descr", "Test access(2) for EINVAL");
110 }
111 
112 ATF_TC_BODY(access_inval, tc)
113 {
114 
115 	errno = 0;
116 
117 	ATF_REQUIRE(access("/usr", -1) != 0);
118 	ATF_REQUIRE(errno == EINVAL);
119 }
120 
121 ATF_TC(access_notdir);
122 ATF_TC_HEAD(access_notdir, tc)
123 {
124 	atf_tc_set_md_var(tc, "descr", "Test access(2) for ENOTDIR");
125 }
126 
127 ATF_TC_BODY(access_notdir, tc)
128 {
129 	size_t i;
130 
131 	for (i = 0; i < __arraycount(mode); i++) {
132 
133 		errno = 0;
134 
135 		/*
136 		 *  IEEE Std 1003.1-2008 about ENOTDIR:
137 		 *
138 		 *  "A component of the path prefix is not a directory,
139 		 *   or the path argument contains at least one non-<slash>
140 		 *   character and ends with one or more trailing <slash>
141 		 *   characters and the last pathname component names an
142 		 *   existing file that is neither a directory nor a symbolic
143 		 *   link to a directory."
144 		 */
145 		ATF_REQUIRE(access("/etc/passwd//", mode[i]) != 0);
146 		ATF_REQUIRE(errno == ENOTDIR);
147 	}
148 }
149 
150 ATF_TC(access_notexist);
151 ATF_TC_HEAD(access_notexist, tc)
152 {
153 	atf_tc_set_md_var(tc, "descr", "Test access(2) for ENOENT");
154 }
155 
156 ATF_TC_BODY(access_notexist, tc)
157 {
158 	size_t i;
159 
160 	for (i = 0; i < __arraycount(mode); i++) {
161 
162 		errno = 0;
163 
164 		ATF_REQUIRE(access("", mode[i]) != 0);
165 		ATF_REQUIRE(errno == ENOENT);
166 	}
167 }
168 
169 ATF_TC(access_toolong);
170 ATF_TC_HEAD(access_toolong, tc)
171 {
172 	atf_tc_set_md_var(tc, "descr", "Test access(2) for ENAMETOOLONG");
173 }
174 
175 ATF_TC_BODY(access_toolong, tc)
176 {
177 	char *buf;
178 	size_t i;
179 
180 	buf = malloc(PATH_MAX);
181 
182 	if (buf == NULL)
183 		return;
184 
185 	for (i = 0; i < PATH_MAX; i++)
186 		buf[i] = 'x';
187 
188 	for (i = 0; i < __arraycount(mode); i++) {
189 
190 		errno = 0;
191 
192 		ATF_REQUIRE(access(buf, mode[i]) != 0);
193 		ATF_REQUIRE(errno == ENAMETOOLONG);
194 	}
195 
196 	free(buf);
197 }
198 
199 ATF_TP_ADD_TCS(tp)
200 {
201 
202 	ATF_TP_ADD_TC(tp, access_access);
203 	ATF_TP_ADD_TC(tp, access_fault);
204 	ATF_TP_ADD_TC(tp, access_inval);
205 	ATF_TP_ADD_TC(tp, access_notdir);
206 	ATF_TP_ADD_TC(tp, access_notexist);
207 	ATF_TP_ADD_TC(tp, access_toolong);
208 
209 	return atf_no_error();
210 }
211