t_fchmodat.c revision 1.1 1 /* $NetBSD: t_fchmodat.c,v 1.1 2012/11/18 17:41:54 manu Exp $ */
2
3 /*-
4 * Copyright (c) 2012 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Emmanuel Dreyfus.
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_fchmodat.c,v 1.1 2012/11/18 17:41:54 manu Exp $");
33
34 #include <atf-c.h>
35 #include <errno.h>
36 #include <fcntl.h>
37 #include <limits.h>
38 #include <paths.h>
39 #include <stdio.h>
40 #include <string.h>
41 #include <unistd.h>
42 #include <sys/param.h>
43
44 #define DIR "dir"
45 #define FILE "dir/fchmodat"
46 #define BASEFILE "fchmodat"
47 #define LINK "dir/symlink"
48 #define BASELINK "symlink"
49 #define FILEERR "dir/fchmodaterr"
50
51 ATF_TC_WITH_CLEANUP(fchmodat_fd);
52 ATF_TC_HEAD(fchmodat_fd, tc)
53 {
54 atf_tc_set_md_var(tc, "descr", "See that fchmodat works with fd");
55 }
56
57 ATF_TC_BODY(fchmodat_fd, tc)
58 {
59 int dfd;
60 int fd;
61 struct stat st;
62
63 ATF_REQUIRE(mkdir(DIR, 0755) == 0);
64 ATF_REQUIRE((fd = open(FILE, O_CREAT|O_RDWR, 0644)) != -1);
65 ATF_REQUIRE(close(fd) == 0);
66
67 ATF_REQUIRE((dfd = open(DIR, O_RDONLY, 0)) != -1);
68 ATF_REQUIRE(fchmodat(dfd, BASEFILE, 0600, 0) == 0);
69 ATF_REQUIRE(close(dfd) == 0);
70
71 ATF_REQUIRE(stat(FILE, &st) == 0);
72 ATF_REQUIRE(st.st_mode = 0600);
73 }
74
75 ATF_TC_CLEANUP(fchmodat_fd, tc)
76 {
77 (void)unlink(FILE);
78 (void)unlink(FILEERR);
79 (void)rmdir(DIR);
80 }
81
82 ATF_TC_WITH_CLEANUP(fchmodat_fdcwd);
83 ATF_TC_HEAD(fchmodat_fdcwd, tc)
84 {
85 atf_tc_set_md_var(tc, "descr",
86 "See that fchmodat works with fd as AT_FDCWD");
87 }
88
89 ATF_TC_BODY(fchmodat_fdcwd, tc)
90 {
91 int fd;
92 struct stat st;
93
94 ATF_REQUIRE(mkdir(DIR, 0755) == 0);
95 ATF_REQUIRE((fd = open(FILE, O_CREAT|O_RDWR, 0644)) != -1);
96 ATF_REQUIRE(close(fd) == 0);
97
98 ATF_REQUIRE(chdir(DIR) == 0);
99 ATF_REQUIRE(fchmodat(AT_FDCWD, BASEFILE, 0600, 0) == 0);
100
101 ATF_REQUIRE(stat(BASEFILE, &st) == 0);
102 ATF_REQUIRE(st.st_mode = 0600);
103 }
104
105 ATF_TC_CLEANUP(fchmodat_fdcwd, tc)
106 {
107 (void)unlink(FILE);
108 (void)unlink(FILEERR);
109 (void)rmdir(DIR);
110 }
111
112 ATF_TC_WITH_CLEANUP(fchmodat_fdcwderr);
113 ATF_TC_HEAD(fchmodat_fdcwderr, tc)
114 {
115 atf_tc_set_md_var(tc, "descr",
116 "See that fchmodat fails with fd as AT_FDCWD and bad path");
117 }
118
119 ATF_TC_BODY(fchmodat_fdcwderr, tc)
120 {
121 ATF_REQUIRE(mkdir(DIR, 0755) == 0);
122 ATF_REQUIRE(fchmodat(AT_FDCWD, FILEERR, 0600, 0) == -1);
123 }
124
125 ATF_TC_CLEANUP(fchmodat_fdcwderr, tc)
126 {
127 (void)unlink(FILE);
128 (void)unlink(FILEERR);
129 (void)rmdir(DIR);
130 }
131
132 ATF_TC_WITH_CLEANUP(fchmodat_fderr1);
133 ATF_TC_HEAD(fchmodat_fderr1, tc)
134 {
135 atf_tc_set_md_var(tc, "descr", "See that fchmodat fail with bad path");
136 }
137
138 ATF_TC_BODY(fchmodat_fderr1, tc)
139 {
140 int dfd;
141
142 ATF_REQUIRE(mkdir(DIR, 0755) == 0);
143 ATF_REQUIRE((dfd = open(DIR, O_RDONLY, 0)) != -1);
144 ATF_REQUIRE(fchmodat(dfd, FILEERR, 0600, 0) == -1);
145 ATF_REQUIRE(close(dfd) == 0);
146 }
147
148 ATF_TC_CLEANUP(fchmodat_fderr1, tc)
149 {
150 (void)unlink(FILE);
151 (void)unlink(FILEERR);
152 (void)rmdir(DIR);
153 }
154
155 ATF_TC_WITH_CLEANUP(fchmodat_fderr2);
156 ATF_TC_HEAD(fchmodat_fderr2, tc)
157 {
158 atf_tc_set_md_var(tc, "descr", "See that fchmodat fails with bad fdat");
159 }
160
161 ATF_TC_BODY(fchmodat_fderr2, tc)
162 {
163 int dfd;
164 int fd;
165 char cwd[MAXPATHLEN];
166
167 ATF_REQUIRE(mkdir(DIR, 0755) == 0);
168 ATF_REQUIRE((fd = open(FILE, O_CREAT|O_RDWR, 0644)) != -1);
169 ATF_REQUIRE(close(fd) == 0);
170
171 ATF_REQUIRE((dfd = open(getcwd(cwd, MAXPATHLEN), O_RDONLY, 0)) != -1);
172 ATF_REQUIRE(fchmodat(dfd, BASEFILE, 0600, 0) == -1);
173 ATF_REQUIRE(close(dfd) == 0);
174 }
175
176 ATF_TC_CLEANUP(fchmodat_fderr2, tc)
177 {
178 (void)unlink(FILE);
179 (void)unlink(FILEERR);
180 (void)rmdir(DIR);
181 }
182
183 ATF_TC_WITH_CLEANUP(fchmodat_fderr3);
184 ATF_TC_HEAD(fchmodat_fderr3, tc)
185 {
186 atf_tc_set_md_var(tc, "descr", "See that fchmodat fails with fd as -1");
187 }
188
189 ATF_TC_BODY(fchmodat_fderr3, tc)
190 {
191 int fd;
192
193 ATF_REQUIRE(mkdir(DIR, 0755) == 0);
194 ATF_REQUIRE((fd = open(FILE, O_CREAT|O_RDWR, 0644)) != -1);
195 ATF_REQUIRE(close(fd) == 0);
196
197 ATF_REQUIRE(fchmodat(-1, FILE, 0600, 0) == -1);
198 }
199
200 ATF_TC_CLEANUP(fchmodat_fderr3, tc)
201 {
202 (void)unlink(FILE);
203 (void)unlink(FILEERR);
204 (void)rmdir(DIR);
205 }
206
207
208 ATF_TC_WITH_CLEANUP(fchmodat_fdlink);
209 ATF_TC_HEAD(fchmodat_fdlink, tc)
210 {
211 atf_tc_set_md_var(tc, "descr", "See that fchmodat works on symlink");
212 }
213
214 ATF_TC_BODY(fchmodat_fdlink, tc)
215 {
216 int dfdlink;
217 struct stat st;
218
219 ATF_REQUIRE(mkdir(DIR, 0755) == 0);
220 ATF_REQUIRE(symlink(FILE, LINK) == 0);
221
222 ATF_REQUIRE((dfdlink = open(DIR, O_RDONLY, 0)) != -1);
223
224 ATF_REQUIRE(fchmodat(dfdlink, BASELINK, 0600, 0) == -1);
225 ATF_REQUIRE(errno = ENOENT);
226
227 ATF_REQUIRE(fchmodat(dfdlink, BASELINK, 0600, AT_SYMLINK_NOFOLLOW) == 0);
228
229 ATF_REQUIRE(close(dfdlink) == 0);
230
231 ATF_REQUIRE(lstat(LINK, &st) == 0);
232 ATF_REQUIRE(st.st_mode = 0600);
233 }
234
235 ATF_TC_CLEANUP(fchmodat_fdlink, tc)
236 {
237 (void)unlink(LINK);
238 (void)rmdir(DIR);
239 }
240
241 ATF_TP_ADD_TCS(tp)
242 {
243
244 ATF_TP_ADD_TC(tp, fchmodat_fd);
245 ATF_TP_ADD_TC(tp, fchmodat_fdcwd);
246 ATF_TP_ADD_TC(tp, fchmodat_fdcwderr);
247 ATF_TP_ADD_TC(tp, fchmodat_fderr1);
248 ATF_TP_ADD_TC(tp, fchmodat_fderr2);
249 ATF_TP_ADD_TC(tp, fchmodat_fderr3);
250 ATF_TP_ADD_TC(tp, fchmodat_fdlink);
251
252 return atf_no_error();
253 }
254