Deleted Added
full compact
t_fchmodat.c (276478) t_fchmodat.c (277443)
1/* $NetBSD: t_fchmodat.c,v 1.2 2013/03/17 04:46:06 jmmv 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.2 2013/03/17 04:46:06 jmmv 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>
1/* $NetBSD: t_fchmodat.c,v 1.2 2013/03/17 04:46:06 jmmv 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.2 2013/03/17 04:46:06 jmmv 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#ifdef __FreeBSD__
44#include <sys/stat.h>
45#endif
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
51ATF_TC(fchmodat_fd);
52ATF_TC_HEAD(fchmodat_fd, tc)
53{
54 atf_tc_set_md_var(tc, "descr", "See that fchmodat works with fd");
55}
56ATF_TC_BODY(fchmodat_fd, tc)
57{
58 int dfd;
59 int fd;
60 struct stat st;
61
62 ATF_REQUIRE(mkdir(DIR, 0755) == 0);
63 ATF_REQUIRE((fd = open(FILE, O_CREAT|O_RDWR, 0644)) != -1);
64 ATF_REQUIRE(close(fd) == 0);
65
66 ATF_REQUIRE((dfd = open(DIR, O_RDONLY, 0)) != -1);
67 ATF_REQUIRE(fchmodat(dfd, BASEFILE, 0600, 0) == 0);
68 ATF_REQUIRE(close(dfd) == 0);
69
70 ATF_REQUIRE(stat(FILE, &st) == 0);
71 ATF_REQUIRE(st.st_mode = 0600);
72}
73
74ATF_TC(fchmodat_fdcwd);
75ATF_TC_HEAD(fchmodat_fdcwd, tc)
76{
77 atf_tc_set_md_var(tc, "descr",
78 "See that fchmodat works with fd as AT_FDCWD");
79}
80ATF_TC_BODY(fchmodat_fdcwd, tc)
81{
82 int fd;
83 struct stat st;
84
85 ATF_REQUIRE(mkdir(DIR, 0755) == 0);
86 ATF_REQUIRE((fd = open(FILE, O_CREAT|O_RDWR, 0644)) != -1);
87 ATF_REQUIRE(close(fd) == 0);
88
89 ATF_REQUIRE(chdir(DIR) == 0);
90 ATF_REQUIRE(fchmodat(AT_FDCWD, BASEFILE, 0600, 0) == 0);
91
92 ATF_REQUIRE(stat(BASEFILE, &st) == 0);
93 ATF_REQUIRE(st.st_mode = 0600);
94}
95
96ATF_TC(fchmodat_fdcwderr);
97ATF_TC_HEAD(fchmodat_fdcwderr, tc)
98{
99 atf_tc_set_md_var(tc, "descr",
100 "See that fchmodat fails with fd as AT_FDCWD and bad path");
101}
102ATF_TC_BODY(fchmodat_fdcwderr, tc)
103{
104 ATF_REQUIRE(mkdir(DIR, 0755) == 0);
105 ATF_REQUIRE(fchmodat(AT_FDCWD, FILEERR, 0600, 0) == -1);
106}
107
108ATF_TC(fchmodat_fderr1);
109ATF_TC_HEAD(fchmodat_fderr1, tc)
110{
111 atf_tc_set_md_var(tc, "descr", "See that fchmodat fail with bad path");
112}
113ATF_TC_BODY(fchmodat_fderr1, tc)
114{
115 int dfd;
116
117 ATF_REQUIRE(mkdir(DIR, 0755) == 0);
118 ATF_REQUIRE((dfd = open(DIR, O_RDONLY, 0)) != -1);
119 ATF_REQUIRE(fchmodat(dfd, FILEERR, 0600, 0) == -1);
120 ATF_REQUIRE(close(dfd) == 0);
121}
122
123ATF_TC(fchmodat_fderr2);
124ATF_TC_HEAD(fchmodat_fderr2, tc)
125{
126 atf_tc_set_md_var(tc, "descr", "See that fchmodat fails with bad fdat");
127}
128ATF_TC_BODY(fchmodat_fderr2, tc)
129{
130 int dfd;
131 int fd;
132 char cwd[MAXPATHLEN];
133
134 ATF_REQUIRE(mkdir(DIR, 0755) == 0);
135 ATF_REQUIRE((fd = open(FILE, O_CREAT|O_RDWR, 0644)) != -1);
136 ATF_REQUIRE(close(fd) == 0);
137
138 ATF_REQUIRE((dfd = open(getcwd(cwd, MAXPATHLEN), O_RDONLY, 0)) != -1);
139 ATF_REQUIRE(fchmodat(dfd, BASEFILE, 0600, 0) == -1);
140 ATF_REQUIRE(close(dfd) == 0);
141}
142
143ATF_TC(fchmodat_fderr3);
144ATF_TC_HEAD(fchmodat_fderr3, tc)
145{
146 atf_tc_set_md_var(tc, "descr", "See that fchmodat fails with fd as -1");
147}
148ATF_TC_BODY(fchmodat_fderr3, tc)
149{
150 int fd;
151
152 ATF_REQUIRE(mkdir(DIR, 0755) == 0);
153 ATF_REQUIRE((fd = open(FILE, O_CREAT|O_RDWR, 0644)) != -1);
154 ATF_REQUIRE(close(fd) == 0);
155
156 ATF_REQUIRE(fchmodat(-1, FILE, 0600, 0) == -1);
157}
158
159ATF_TC(fchmodat_fdlink);
160ATF_TC_HEAD(fchmodat_fdlink, tc)
161{
162 atf_tc_set_md_var(tc, "descr", "See that fchmodat works on symlink");
163}
164ATF_TC_BODY(fchmodat_fdlink, tc)
165{
166 int dfdlink;
167 struct stat st;
168
169 ATF_REQUIRE(mkdir(DIR, 0755) == 0);
170 ATF_REQUIRE(symlink(FILE, LINK) == 0);
171
172 ATF_REQUIRE((dfdlink = open(DIR, O_RDONLY, 0)) != -1);
173
174 ATF_REQUIRE(fchmodat(dfdlink, BASELINK, 0600, 0) == -1);
175 ATF_REQUIRE(errno = ENOENT);
176
177 ATF_REQUIRE(fchmodat(dfdlink, BASELINK, 0600, AT_SYMLINK_NOFOLLOW) == 0);
178
179 ATF_REQUIRE(close(dfdlink) == 0);
180
181 ATF_REQUIRE(lstat(LINK, &st) == 0);
182 ATF_REQUIRE(st.st_mode = 0600);
183}
184
185ATF_TP_ADD_TCS(tp)
186{
187
188 ATF_TP_ADD_TC(tp, fchmodat_fd);
189 ATF_TP_ADD_TC(tp, fchmodat_fdcwd);
190 ATF_TP_ADD_TC(tp, fchmodat_fdcwderr);
191 ATF_TP_ADD_TC(tp, fchmodat_fderr1);
192 ATF_TP_ADD_TC(tp, fchmodat_fderr2);
193 ATF_TP_ADD_TC(tp, fchmodat_fderr3);
194 ATF_TP_ADD_TC(tp, fchmodat_fdlink);
195
196 return atf_no_error();
197}
46
47#define DIR "dir"
48#define FILE "dir/fchmodat"
49#define BASEFILE "fchmodat"
50#define LINK "dir/symlink"
51#define BASELINK "symlink"
52#define FILEERR "dir/fchmodaterr"
53
54ATF_TC(fchmodat_fd);
55ATF_TC_HEAD(fchmodat_fd, tc)
56{
57 atf_tc_set_md_var(tc, "descr", "See that fchmodat works with fd");
58}
59ATF_TC_BODY(fchmodat_fd, tc)
60{
61 int dfd;
62 int fd;
63 struct stat st;
64
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);
68
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);
72
73 ATF_REQUIRE(stat(FILE, &st) == 0);
74 ATF_REQUIRE(st.st_mode = 0600);
75}
76
77ATF_TC(fchmodat_fdcwd);
78ATF_TC_HEAD(fchmodat_fdcwd, tc)
79{
80 atf_tc_set_md_var(tc, "descr",
81 "See that fchmodat works with fd as AT_FDCWD");
82}
83ATF_TC_BODY(fchmodat_fdcwd, tc)
84{
85 int fd;
86 struct stat st;
87
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);
91
92 ATF_REQUIRE(chdir(DIR) == 0);
93 ATF_REQUIRE(fchmodat(AT_FDCWD, BASEFILE, 0600, 0) == 0);
94
95 ATF_REQUIRE(stat(BASEFILE, &st) == 0);
96 ATF_REQUIRE(st.st_mode = 0600);
97}
98
99ATF_TC(fchmodat_fdcwderr);
100ATF_TC_HEAD(fchmodat_fdcwderr, tc)
101{
102 atf_tc_set_md_var(tc, "descr",
103 "See that fchmodat fails with fd as AT_FDCWD and bad path");
104}
105ATF_TC_BODY(fchmodat_fdcwderr, tc)
106{
107 ATF_REQUIRE(mkdir(DIR, 0755) == 0);
108 ATF_REQUIRE(fchmodat(AT_FDCWD, FILEERR, 0600, 0) == -1);
109}
110
111ATF_TC(fchmodat_fderr1);
112ATF_TC_HEAD(fchmodat_fderr1, tc)
113{
114 atf_tc_set_md_var(tc, "descr", "See that fchmodat fail with bad path");
115}
116ATF_TC_BODY(fchmodat_fderr1, tc)
117{
118 int dfd;
119
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);
124}
125
126ATF_TC(fchmodat_fderr2);
127ATF_TC_HEAD(fchmodat_fderr2, tc)
128{
129 atf_tc_set_md_var(tc, "descr", "See that fchmodat fails with bad fdat");
130}
131ATF_TC_BODY(fchmodat_fderr2, tc)
132{
133 int dfd;
134 int fd;
135 char cwd[MAXPATHLEN];
136
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);
140
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);
144}
145
146ATF_TC(fchmodat_fderr3);
147ATF_TC_HEAD(fchmodat_fderr3, tc)
148{
149 atf_tc_set_md_var(tc, "descr", "See that fchmodat fails with fd as -1");
150}
151ATF_TC_BODY(fchmodat_fderr3, tc)
152{
153 int fd;
154
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);
158
159 ATF_REQUIRE(fchmodat(-1, FILE, 0600, 0) == -1);
160}
161
162ATF_TC(fchmodat_fdlink);
163ATF_TC_HEAD(fchmodat_fdlink, tc)
164{
165 atf_tc_set_md_var(tc, "descr", "See that fchmodat works on symlink");
166}
167ATF_TC_BODY(fchmodat_fdlink, tc)
168{
169 int dfdlink;
170 struct stat st;
171
172 ATF_REQUIRE(mkdir(DIR, 0755) == 0);
173 ATF_REQUIRE(symlink(FILE, LINK) == 0);
174
175 ATF_REQUIRE((dfdlink = open(DIR, O_RDONLY, 0)) != -1);
176
177 ATF_REQUIRE(fchmodat(dfdlink, BASELINK, 0600, 0) == -1);
178 ATF_REQUIRE(errno = ENOENT);
179
180 ATF_REQUIRE(fchmodat(dfdlink, BASELINK, 0600, AT_SYMLINK_NOFOLLOW) == 0);
181
182 ATF_REQUIRE(close(dfdlink) == 0);
183
184 ATF_REQUIRE(lstat(LINK, &st) == 0);
185 ATF_REQUIRE(st.st_mode = 0600);
186}
187
188ATF_TP_ADD_TCS(tp)
189{
190
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);
198
199 return atf_no_error();
200}