1 /* $NetBSD: t_access.c,v 1.1 2011/07/07 06:57:53 jruoho Exp $ */
4 * Copyright (c) 2011 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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.
31 #include <sys/cdefs.h>
32 __RCSID("$NetBSD: t_access.c,v 1.1 2011/07/07 06:57:53 jruoho Exp $");
44 #include <sys/param.h>
48 static const char path[] = "access";
49 static const int mode[4] = { R_OK, W_OK, X_OK, F_OK };
51 ATF_TC_WITH_CLEANUP(access_access);
52 ATF_TC_HEAD(access_access, tc)
54 atf_tc_set_md_var(tc, "descr", "Test access(2) for EACCES");
55 atf_tc_set_md_var(tc, "require.user", "unprivileged");
58 ATF_TC_BODY(access_access, tc)
60 const int perm[3] = { 0200, 0400, 0000 };
64 fd = open(path, O_RDONLY | O_CREAT);
69 for (i = 0; i < __arraycount(mode) - 1; i++) {
71 ATF_REQUIRE(fchmod(fd, perm[i]) == 0);
75 ATF_REQUIRE(access(path, mode[i]) != 0);
76 ATF_REQUIRE(errno == EACCES);
79 ATF_REQUIRE(close(fd) == 0);
82 ATF_TC_CLEANUP(access_access, tc)
88 ATF_TC_HEAD(access_fault, tc)
90 atf_tc_set_md_var(tc, "descr", "Test access(2) for EFAULT");
93 ATF_TC_BODY(access_fault, tc)
97 for (i = 0; i < __arraycount(mode); i++) {
101 ATF_REQUIRE(access(NULL, mode[i]) != 0);
102 ATF_REQUIRE(errno == EFAULT);
106 ATF_REQUIRE(access((char *)-1, mode[i]) != 0);
107 ATF_REQUIRE(errno == EFAULT);
111 ATF_TC(access_inval);
112 ATF_TC_HEAD(access_inval, tc)
114 atf_tc_set_md_var(tc, "descr", "Test access(2) for EINVAL");
117 ATF_TC_BODY(access_inval, tc)
120 #if defined(__FreeBSD__) && __FreeBSD_version < 1100033
121 atf_tc_expect_fail("arguments to access aren't validated; see "
122 "bug # 181155 for more details");
126 ATF_REQUIRE(access("/usr", -1) != 0);
127 ATF_REQUIRE(errno == EINVAL);
130 ATF_TC(access_notdir);
131 ATF_TC_HEAD(access_notdir, tc)
133 atf_tc_set_md_var(tc, "descr", "Test access(2) for ENOTDIR");
136 ATF_TC_BODY(access_notdir, tc)
140 for (i = 0; i < __arraycount(mode); i++) {
145 * IEEE Std 1003.1-2008 about ENOTDIR:
147 * "A component of the path prefix is not a directory,
148 * or the path argument contains at least one non-<slash>
149 * character and ends with one or more trailing <slash>
150 * characters and the last pathname component names an
151 * existing file that is neither a directory nor a symbolic
152 * link to a directory."
154 ATF_REQUIRE(access("/etc/passwd//", mode[i]) != 0);
155 ATF_REQUIRE(errno == ENOTDIR);
159 ATF_TC(access_notexist);
160 ATF_TC_HEAD(access_notexist, tc)
162 atf_tc_set_md_var(tc, "descr", "Test access(2) for ENOENT");
165 ATF_TC_BODY(access_notexist, tc)
169 for (i = 0; i < __arraycount(mode); i++) {
173 ATF_REQUIRE(access("", mode[i]) != 0);
174 ATF_REQUIRE(errno == ENOENT);
178 ATF_TC(access_toolong);
179 ATF_TC_HEAD(access_toolong, tc)
181 atf_tc_set_md_var(tc, "descr", "Test access(2) for ENAMETOOLONG");
184 ATF_TC_BODY(access_toolong, tc)
189 buf = malloc(PATH_MAX);
194 for (i = 0; i < PATH_MAX; i++)
197 for (i = 0; i < __arraycount(mode); i++) {
201 ATF_REQUIRE(access(buf, mode[i]) != 0);
202 ATF_REQUIRE(errno == ENAMETOOLONG);
211 ATF_TP_ADD_TC(tp, access_access);
212 ATF_TP_ADD_TC(tp, access_fault);
213 ATF_TP_ADD_TC(tp, access_inval);
214 ATF_TP_ADD_TC(tp, access_notdir);
215 ATF_TP_ADD_TC(tp, access_notexist);
216 ATF_TP_ADD_TC(tp, access_toolong);
218 return atf_no_error();