1 /* $NetBSD: t_fchmodat.c,v 1.2 2013/03/17 04:46:06 jmmv Exp $ */
4 * Copyright (c) 2012 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_fchmodat.c,v 1.2 2013/03/17 04:46:06 jmmv Exp $");
42 #include <sys/param.h>
48 #define FILE "dir/fchmodat"
49 #define BASEFILE "fchmodat"
50 #define LINK "dir/symlink"
51 #define BASELINK "symlink"
52 #define FILEERR "dir/fchmodaterr"
55 ATF_TC_HEAD(fchmodat_fd, tc)
57 atf_tc_set_md_var(tc, "descr", "See that fchmodat works with fd");
59 ATF_TC_BODY(fchmodat_fd, tc)
65 ATF_REQUIRE(mkdir(DIR, 0755) == 0);
66 ATF_REQUIRE((fd = open(FILE, O_CREAT|O_RDWR, 0644)) != -1);
67 ATF_REQUIRE(close(fd) == 0);
69 ATF_REQUIRE((dfd = open(DIR, O_RDONLY, 0)) != -1);
70 ATF_REQUIRE(fchmodat(dfd, BASEFILE, 0600, 0) == 0);
71 ATF_REQUIRE(close(dfd) == 0);
73 ATF_REQUIRE(stat(FILE, &st) == 0);
74 ATF_REQUIRE(st.st_mode = 0600);
77 ATF_TC(fchmodat_fdcwd);
78 ATF_TC_HEAD(fchmodat_fdcwd, tc)
80 atf_tc_set_md_var(tc, "descr",
81 "See that fchmodat works with fd as AT_FDCWD");
83 ATF_TC_BODY(fchmodat_fdcwd, tc)
88 ATF_REQUIRE(mkdir(DIR, 0755) == 0);
89 ATF_REQUIRE((fd = open(FILE, O_CREAT|O_RDWR, 0644)) != -1);
90 ATF_REQUIRE(close(fd) == 0);
92 ATF_REQUIRE(chdir(DIR) == 0);
93 ATF_REQUIRE(fchmodat(AT_FDCWD, BASEFILE, 0600, 0) == 0);
95 ATF_REQUIRE(stat(BASEFILE, &st) == 0);
96 ATF_REQUIRE(st.st_mode = 0600);
99 ATF_TC(fchmodat_fdcwderr);
100 ATF_TC_HEAD(fchmodat_fdcwderr, tc)
102 atf_tc_set_md_var(tc, "descr",
103 "See that fchmodat fails with fd as AT_FDCWD and bad path");
105 ATF_TC_BODY(fchmodat_fdcwderr, tc)
107 ATF_REQUIRE(mkdir(DIR, 0755) == 0);
108 ATF_REQUIRE(fchmodat(AT_FDCWD, FILEERR, 0600, 0) == -1);
111 ATF_TC(fchmodat_fderr1);
112 ATF_TC_HEAD(fchmodat_fderr1, tc)
114 atf_tc_set_md_var(tc, "descr", "See that fchmodat fail with bad path");
116 ATF_TC_BODY(fchmodat_fderr1, tc)
120 ATF_REQUIRE(mkdir(DIR, 0755) == 0);
121 ATF_REQUIRE((dfd = open(DIR, O_RDONLY, 0)) != -1);
122 ATF_REQUIRE(fchmodat(dfd, FILEERR, 0600, 0) == -1);
123 ATF_REQUIRE(close(dfd) == 0);
126 ATF_TC(fchmodat_fderr2);
127 ATF_TC_HEAD(fchmodat_fderr2, tc)
129 atf_tc_set_md_var(tc, "descr", "See that fchmodat fails with bad fdat");
131 ATF_TC_BODY(fchmodat_fderr2, tc)
135 char cwd[MAXPATHLEN];
137 ATF_REQUIRE(mkdir(DIR, 0755) == 0);
138 ATF_REQUIRE((fd = open(FILE, O_CREAT|O_RDWR, 0644)) != -1);
139 ATF_REQUIRE(close(fd) == 0);
141 ATF_REQUIRE((dfd = open(getcwd(cwd, MAXPATHLEN), O_RDONLY, 0)) != -1);
142 ATF_REQUIRE(fchmodat(dfd, BASEFILE, 0600, 0) == -1);
143 ATF_REQUIRE(close(dfd) == 0);
146 ATF_TC(fchmodat_fderr3);
147 ATF_TC_HEAD(fchmodat_fderr3, tc)
149 atf_tc_set_md_var(tc, "descr", "See that fchmodat fails with fd as -1");
151 ATF_TC_BODY(fchmodat_fderr3, tc)
155 ATF_REQUIRE(mkdir(DIR, 0755) == 0);
156 ATF_REQUIRE((fd = open(FILE, O_CREAT|O_RDWR, 0644)) != -1);
157 ATF_REQUIRE(close(fd) == 0);
159 ATF_REQUIRE(fchmodat(-1, FILE, 0600, 0) == -1);
162 ATF_TC(fchmodat_fdlink);
163 ATF_TC_HEAD(fchmodat_fdlink, tc)
165 atf_tc_set_md_var(tc, "descr", "See that fchmodat works on symlink");
167 ATF_TC_BODY(fchmodat_fdlink, tc)
172 ATF_REQUIRE(mkdir(DIR, 0755) == 0);
173 ATF_REQUIRE(symlink(FILE, LINK) == 0);
175 ATF_REQUIRE((dfdlink = open(DIR, O_RDONLY, 0)) != -1);
177 ATF_REQUIRE(fchmodat(dfdlink, BASELINK, 0600, 0) == -1);
178 ATF_REQUIRE(errno = ENOENT);
180 ATF_REQUIRE(fchmodat(dfdlink, BASELINK, 0600, AT_SYMLINK_NOFOLLOW) == 0);
182 ATF_REQUIRE(close(dfdlink) == 0);
184 ATF_REQUIRE(lstat(LINK, &st) == 0);
185 ATF_REQUIRE(st.st_mode = 0600);
191 ATF_TP_ADD_TC(tp, fchmodat_fd);
192 ATF_TP_ADD_TC(tp, fchmodat_fdcwd);
193 ATF_TP_ADD_TC(tp, fchmodat_fdcwderr);
194 ATF_TP_ADD_TC(tp, fchmodat_fderr1);
195 ATF_TP_ADD_TC(tp, fchmodat_fderr2);
196 ATF_TP_ADD_TC(tp, fchmodat_fderr3);
197 ATF_TP_ADD_TC(tp, fchmodat_fdlink);
199 return atf_no_error();