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