xref: /netbsd-src/tests/lib/libc/c063/t_fchmodat.c (revision ad0d3005e406851cbc568a9348b7f099eef5aa90)
1 /*	$NetBSD: t_fchmodat.c,v 1.7 2024/07/10 20:44:06 rillig 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.7 2024/07/10 20:44:06 rillig Exp $");
33 
34 #include <sys/param.h>
35 #include <sys/stat.h>
36 #include <atf-c.h>
37 #include <errno.h>
38 #include <fcntl.h>
39 #include <limits.h>
40 #include <paths.h>
41 #include <stdio.h>
42 #include <string.h>
43 #include <unistd.h>
44 
45 #define DIR "dir"
46 #define FILE "dir/fchmodat"
47 #define BASEFILE "fchmodat"
48 #define LINK "dir/symlink"
49 #define BASELINK "symlink"
50 #define FILEERR "dir/fchmodaterr"
51 
52 #define modecheck(a, b) \
53 	ATF_REQUIRE_MSG(((a) & ALLPERMS) == (b), \
54 	    "Incorrect mode found %#o != expected %#o", \
55 	    ((a) & ALLPERMS), (b));
56 
57 ATF_TC(fchmodat_fd);
ATF_TC_HEAD(fchmodat_fd,tc)58 ATF_TC_HEAD(fchmodat_fd, tc)
59 {
60 	atf_tc_set_md_var(tc, "descr", "See that fchmodat works with fd");
61 }
ATF_TC_BODY(fchmodat_fd,tc)62 ATF_TC_BODY(fchmodat_fd, tc)
63 {
64 	int dfd;
65 	int fd;
66 	struct stat st;
67 
68 	ATF_REQUIRE(mkdir(DIR, 0755) == 0);
69 	ATF_REQUIRE((fd = open(FILE, O_CREAT|O_RDWR, 0644)) != -1);
70 	ATF_REQUIRE(close(fd) == 0);
71 
72 	ATF_REQUIRE((dfd = open(DIR, O_RDONLY, 0)) != -1);
73 	ATF_REQUIRE(fchmodat(dfd, BASEFILE, 0600, 0) == 0);
74 	ATF_REQUIRE(close(dfd) == 0);
75 
76 	ATF_REQUIRE(stat(FILE, &st) == 0);
77 	modecheck(st.st_mode, 0600);
78 }
79 
80 ATF_TC(fchmodat_fdcwd);
ATF_TC_HEAD(fchmodat_fdcwd,tc)81 ATF_TC_HEAD(fchmodat_fdcwd, tc)
82 {
83 	atf_tc_set_md_var(tc, "descr",
84 			  "See that fchmodat works with fd as AT_FDCWD");
85 }
ATF_TC_BODY(fchmodat_fdcwd,tc)86 ATF_TC_BODY(fchmodat_fdcwd, tc)
87 {
88 	int fd;
89 	struct stat st;
90 
91 	ATF_REQUIRE(mkdir(DIR, 0755) == 0);
92 	ATF_REQUIRE((fd = open(FILE, O_CREAT|O_RDWR, 0644)) != -1);
93 	ATF_REQUIRE(close(fd) == 0);
94 
95 	ATF_REQUIRE(chdir(DIR) == 0);
96 	ATF_REQUIRE(fchmodat(AT_FDCWD, BASEFILE, 0600, 0) == 0);
97 
98 	ATF_REQUIRE(stat(BASEFILE, &st) == 0);
99 	modecheck(st.st_mode, 0600);
100 }
101 
102 ATF_TC(fchmodat_fdcwderr);
ATF_TC_HEAD(fchmodat_fdcwderr,tc)103 ATF_TC_HEAD(fchmodat_fdcwderr, tc)
104 {
105 	atf_tc_set_md_var(tc, "descr",
106 		  "See that fchmodat fails with fd as AT_FDCWD and bad path");
107 }
ATF_TC_BODY(fchmodat_fdcwderr,tc)108 ATF_TC_BODY(fchmodat_fdcwderr, tc)
109 {
110 	ATF_REQUIRE(mkdir(DIR, 0755) == 0);
111 	ATF_REQUIRE(fchmodat(AT_FDCWD, FILEERR, 0600, 0) == -1);
112 }
113 
114 ATF_TC(fchmodat_fderr1);
ATF_TC_HEAD(fchmodat_fderr1,tc)115 ATF_TC_HEAD(fchmodat_fderr1, tc)
116 {
117 	atf_tc_set_md_var(tc, "descr", "See that fchmodat fail with bad path");
118 }
ATF_TC_BODY(fchmodat_fderr1,tc)119 ATF_TC_BODY(fchmodat_fderr1, tc)
120 {
121 	int dfd;
122 
123 	ATF_REQUIRE(mkdir(DIR, 0755) == 0);
124 	ATF_REQUIRE((dfd = open(DIR, O_RDONLY, 0)) != -1);
125 	ATF_REQUIRE(fchmodat(dfd, FILEERR, 0600, 0) == -1);
126 	ATF_REQUIRE(close(dfd) == 0);
127 }
128 
129 ATF_TC(fchmodat_fderr2);
ATF_TC_HEAD(fchmodat_fderr2,tc)130 ATF_TC_HEAD(fchmodat_fderr2, tc)
131 {
132 	atf_tc_set_md_var(tc, "descr", "See that fchmodat fails with bad fdat");
133 }
ATF_TC_BODY(fchmodat_fderr2,tc)134 ATF_TC_BODY(fchmodat_fderr2, tc)
135 {
136 	int dfd;
137 	int fd;
138 	char cwd[MAXPATHLEN];
139 
140 	ATF_REQUIRE(mkdir(DIR, 0755) == 0);
141 	ATF_REQUIRE((fd = open(FILE, O_CREAT|O_RDWR, 0644)) != -1);
142 	ATF_REQUIRE(close(fd) == 0);
143 
144 	ATF_REQUIRE((dfd = open(getcwd(cwd, MAXPATHLEN), O_RDONLY, 0)) != -1);
145 	ATF_REQUIRE(fchmodat(dfd, BASEFILE, 0600, 0) == -1);
146 	ATF_REQUIRE(close(dfd) == 0);
147 }
148 
149 ATF_TC(fchmodat_fderr3);
ATF_TC_HEAD(fchmodat_fderr3,tc)150 ATF_TC_HEAD(fchmodat_fderr3, tc)
151 {
152 	atf_tc_set_md_var(tc, "descr", "See that fchmodat fails with fd as -1");
153 }
ATF_TC_BODY(fchmodat_fderr3,tc)154 ATF_TC_BODY(fchmodat_fderr3, tc)
155 {
156 	int fd;
157 
158 	ATF_REQUIRE(mkdir(DIR, 0755) == 0);
159 	ATF_REQUIRE((fd = open(FILE, O_CREAT|O_RDWR, 0644)) != -1);
160 	ATF_REQUIRE(close(fd) == 0);
161 
162 	ATF_REQUIRE(fchmodat(-1, FILE, 0600, 0) == -1);
163 }
164 
165 ATF_TC(fchmodat_fdlink);
ATF_TC_HEAD(fchmodat_fdlink,tc)166 ATF_TC_HEAD(fchmodat_fdlink, tc)
167 {
168 	atf_tc_set_md_var(tc, "descr", "See that fchmodat works on symlink");
169 }
ATF_TC_BODY(fchmodat_fdlink,tc)170 ATF_TC_BODY(fchmodat_fdlink, tc)
171 {
172 	int dfdlink;
173 	struct stat st;
174 
175 	ATF_REQUIRE(mkdir(DIR, 0755) == 0);
176 	ATF_REQUIRE(symlink(FILE, LINK) == 0);
177 
178 	ATF_REQUIRE((dfdlink = open(DIR, O_RDONLY, 0)) != -1);
179 
180 	ATF_REQUIRE(fchmodat(dfdlink, BASELINK, 0600, 0) == -1);
181 	ATF_REQUIRE(errno == ENOENT);
182 
183 	ATF_REQUIRE(fchmodat(dfdlink, BASELINK, 0600, AT_SYMLINK_NOFOLLOW) == 0);
184 
185 	ATF_REQUIRE(close(dfdlink) == 0);
186 
187 	ATF_REQUIRE(lstat(LINK, &st) == 0);
188 	modecheck(st.st_mode, 0600);
189 }
190 
ATF_TP_ADD_TCS(tp)191 ATF_TP_ADD_TCS(tp)
192 {
193 
194 	ATF_TP_ADD_TC(tp, fchmodat_fd);
195 	ATF_TP_ADD_TC(tp, fchmodat_fdcwd);
196 	ATF_TP_ADD_TC(tp, fchmodat_fdcwderr);
197 	ATF_TP_ADD_TC(tp, fchmodat_fderr1);
198 	ATF_TP_ADD_TC(tp, fchmodat_fderr2);
199 	ATF_TP_ADD_TC(tp, fchmodat_fderr3);
200 	ATF_TP_ADD_TC(tp, fchmodat_fdlink);
201 
202 	return atf_no_error();
203 }
204