xref: /netbsd-src/tests/lib/libc/sys/t_stat.c (revision 9aa0541bdf64142d9a27c2cf274394d60182818f)
1 /* $NetBSD: t_stat.c,v 1.1 2011/07/07 06:57:54 jruoho 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_stat.c,v 1.1 2011/07/07 06:57:54 jruoho Exp $");
33 
34 #include <sys/stat.h>
35 #include <sys/types.h>
36 
37 #include <atf-c.h>
38 #include <errno.h>
39 #include <fcntl.h>
40 #include <fts.h>
41 #include <limits.h>
42 #include <string.h>
43 #include <unistd.h>
44 
45 #include <stdio.h>
46 
47 static const char *path = "stat";
48 
49 ATF_TC_WITH_CLEANUP(stat_chflags);
50 ATF_TC_HEAD(stat_chflags, tc)
51 {
52 	atf_tc_set_md_var(tc, "descr", "Test chflags(2) with stat(2)");
53 }
54 
55 ATF_TC_BODY(stat_chflags, tc)
56 {
57 	struct stat sa, sb;
58 	int fd;
59 
60 	(void)memset(&sa, 0, sizeof(struct stat));
61 	(void)memset(&sb, 0, sizeof(struct stat));
62 
63 	fd = open(path, O_RDONLY | O_CREAT);
64 
65 	ATF_REQUIRE(fd != -1);
66 	ATF_REQUIRE(stat(path, &sa) == 0);
67 	ATF_REQUIRE(chflags(path, UF_NODUMP) == 0);
68 	ATF_REQUIRE(stat(path, &sb) == 0);
69 
70 	if (sa.st_flags == sb.st_flags)
71 		atf_tc_fail("stat(2) did not detect chflags(2)");
72 
73 	ATF_REQUIRE(close(fd) == 0);
74 	ATF_REQUIRE(unlink(path) == 0);
75 }
76 
77 ATF_TC_CLEANUP(stat_chflags, tc)
78 {
79 	(void)unlink(path);
80 }
81 
82 ATF_TC(stat_dir);
83 ATF_TC_HEAD(stat_dir, tc)
84 {
85 	atf_tc_set_md_var(tc, "descr", "Test stat(2) with directories");
86 }
87 
88 ATF_TC_BODY(stat_dir, tc)
89 {
90 	const short depth = 3;
91 	struct stat sa, sb;
92 	char *argv[2];
93 	FTSENT *ftse;
94 	FTS *fts;
95 	int ops;
96 
97 	/*
98 	 * XXX: This is verified to panic at least a qemu/i386 guest.
99 	 */
100 	atf_tc_skip("the test may cause a panic");
101 
102 	argv[1] = NULL;
103 	argv[0] = __UNCONST("/");
104 
105 	ops = FTS_NOCHDIR;
106 	ops |= FTS_PHYSICAL;
107 
108 	fts = fts_open(argv, ops, NULL);
109 	ATF_REQUIRE(fts != NULL);
110 
111 	while ((ftse = fts_read(fts)) != NULL) {
112 
113 		if (ftse->fts_level < 1)
114 			continue;
115 
116 		if (ftse->fts_level > depth) {
117 			(void)fts_set(fts, ftse, FTS_SKIP);
118 			continue;
119 		}
120 
121 		switch(ftse->fts_info) {
122 
123 		case FTS_DP:
124 
125 			(void)memset(&sa, 0, sizeof(struct stat));
126 			(void)memset(&sb, 0, sizeof(struct stat));
127 
128 			ATF_REQUIRE(stat(ftse->fts_parent->fts_path,&sa) == 0);
129 			ATF_REQUIRE(chdir(ftse->fts_path) == 0);
130 			ATF_REQUIRE(stat(".", &sb) == 0);
131 
132 			/*
133 			 * The previous two stat(2) calls
134 			 * should be for the same directory.
135 			 */
136 			if (sa.st_dev != sb.st_dev || sa.st_ino != sb.st_ino)
137 				atf_tc_fail("inconsistent stat(2)");
138 
139 			/*
140 			 * Check that fts(3)'s stat(2)
141 			 * call equals the manual one.
142 			 */
143 			if (sb.st_ino != ftse->fts_statp->st_ino)
144 				atf_tc_fail("stat(2) and fts(3) differ");
145 
146 			break;
147 
148 		default:
149 			break;
150 		}
151 	}
152 
153 	(void)fts_close(fts);
154 }
155 
156 ATF_TC(stat_err);
157 ATF_TC_HEAD(stat_err, tc)
158 {
159 	atf_tc_set_md_var(tc, "descr", "Test errors from the stat(2) family");
160 }
161 
162 ATF_TC_BODY(stat_err, tc)
163 {
164 	char buf[NAME_MAX + 1];
165 	struct stat st;
166 
167 	(void)memset(buf, 'x', sizeof(buf));
168 
169 	errno = 0;
170 	ATF_REQUIRE_ERRNO(EBADF, fstat(-1, &st) == -1);
171 
172 	errno = 0;
173 	ATF_REQUIRE_ERRNO(ENAMETOOLONG, stat(buf, &st) == -1);
174 
175 	errno = 0;
176 	ATF_REQUIRE_ERRNO(ENAMETOOLONG, lstat(buf, &st) == -1);
177 
178 	errno = 0;
179 	ATF_REQUIRE_ERRNO(EFAULT, stat((void *)-1, &st) == -1);
180 
181 	errno = 0;
182 	ATF_REQUIRE_ERRNO(EFAULT, lstat((void *)-1, &st) == -1);
183 
184 	errno = 0;
185 	ATF_REQUIRE_ERRNO(EFAULT, stat("/etc/passwd", (void *)-1) == -1);
186 
187 	errno = 0;
188 	ATF_REQUIRE_ERRNO(EFAULT, lstat("/etc/passwd", (void *)-1) == -1);
189 
190 	errno = 0;
191 	ATF_REQUIRE_ERRNO(ENOENT, stat("/a/b/c/d/e/f/g/h/i/j/k", &st) == -1);
192 
193 	errno = 0;
194 	ATF_REQUIRE_ERRNO(ENOENT, lstat("/a/b/c/d/e/f/g/h/i/j/k", &st) == -1);
195 }
196 
197 ATF_TC_WITH_CLEANUP(stat_mtime);
198 ATF_TC_HEAD(stat_mtime, tc)
199 {
200 	atf_tc_set_md_var(tc, "descr", "Test modification times with stat(2)");
201 }
202 
203 ATF_TC_BODY(stat_mtime, tc)
204 {
205 	struct stat sa, sb;
206 	int fd[3];
207 	size_t i;
208 
209 	for (i = 0; i < __arraycount(fd); i++) {
210 
211 		(void)memset(&sa, 0, sizeof(struct stat));
212 		(void)memset(&sb, 0, sizeof(struct stat));
213 
214 		fd[i] = open(path, O_WRONLY | O_CREAT);
215 
216 		ATF_REQUIRE(fd[i] != -1);
217 		ATF_REQUIRE(write(fd[i], "X", 1) == 1);
218 		ATF_REQUIRE(stat(path, &sa) == 0);
219 
220 		(void)sleep(1);
221 
222 		ATF_REQUIRE(write(fd[i], "X", 1) == 1);
223 		ATF_REQUIRE(stat(path, &sb) == 0);
224 
225 		ATF_REQUIRE(close(fd[i]) == 0);
226 		ATF_REQUIRE(unlink(path) == 0);
227 
228 		if (sa.st_mtime == sb.st_mtime)
229 			atf_tc_fail("mtimes did not change");
230 	}
231 }
232 
233 ATF_TC_CLEANUP(stat_mtime, tc)
234 {
235 	(void)unlink(path);
236 }
237 
238 ATF_TC_WITH_CLEANUP(stat_perm);
239 ATF_TC_HEAD(stat_perm, tc)
240 {
241 	atf_tc_set_md_var(tc, "descr", "Test permissions with stat(2)");
242 	atf_tc_set_md_var(tc, "require.user", "root");
243 }
244 
245 ATF_TC_BODY(stat_perm, tc)
246 {
247 	struct stat sa, sb;
248 	gid_t gid;
249 	uid_t uid;
250 	int fd;
251 
252 	(void)memset(&sa, 0, sizeof(struct stat));
253 	(void)memset(&sb, 0, sizeof(struct stat));
254 
255 	uid = getuid();
256 	gid = getgid();
257 
258 	fd = open(path, O_RDONLY | O_CREAT);
259 
260 	ATF_REQUIRE(fd != -1);
261 	ATF_REQUIRE(fstat(fd, &sa) == 0);
262 	ATF_REQUIRE(stat(path, &sb) == 0);
263 
264 	if (gid != sa.st_gid || sa.st_gid != sb.st_gid)
265 		atf_tc_fail("invalid GID");
266 
267 	if (uid != sa.st_uid || sa.st_uid != sb.st_uid)
268 		atf_tc_fail("invalid UID");
269 
270 	ATF_REQUIRE(close(fd) == 0);
271 	ATF_REQUIRE(unlink(path) == 0);
272 }
273 
274 ATF_TC_CLEANUP(stat_perm, tc)
275 {
276 	(void)unlink(path);
277 }
278 
279 ATF_TC_WITH_CLEANUP(stat_size);
280 ATF_TC_HEAD(stat_size, tc)
281 {
282 	atf_tc_set_md_var(tc, "descr", "Test file sizes with stat(2)");
283 }
284 
285 ATF_TC_BODY(stat_size, tc)
286 {
287 	struct stat sa, sb, sc;
288 	const size_t n = 10;
289 	size_t i;
290 	int fd;
291 
292 	fd = open(path, O_WRONLY | O_CREAT);
293 	ATF_REQUIRE(fd >= 0);
294 
295 	for (i = 0; i < n; i++) {
296 
297 		(void)memset(&sa, 0, sizeof(struct stat));
298 		(void)memset(&sb, 0, sizeof(struct stat));
299 		(void)memset(&sc, 0, sizeof(struct stat));
300 
301 		ATF_REQUIRE(fstat(fd, &sa) == 0);
302 		ATF_REQUIRE(write(fd, "X", 1) == 1);
303 		ATF_REQUIRE(fstat(fd, &sb) == 0);
304 		ATF_REQUIRE(stat(path, &sc) == 0);
305 
306 		if (sa.st_size + 1 != sb.st_size)
307 			atf_tc_fail("invalid file size");
308 
309 		if (sb.st_size != sc.st_size)
310 			atf_tc_fail("stat(2) and fstat(2) mismatch");
311 	}
312 
313 	ATF_REQUIRE(close(fd) == 0);
314 	ATF_REQUIRE(unlink(path) == 0);
315 }
316 
317 ATF_TC_CLEANUP(stat_size, tc)
318 {
319 	(void)unlink(path);
320 }
321 
322 ATF_TC_WITH_CLEANUP(stat_symlink);
323 ATF_TC_HEAD(stat_symlink, tc)
324 {
325 	atf_tc_set_md_var(tc, "descr", "Test symbolic links with stat(2)");
326 }
327 
328 ATF_TC_BODY(stat_symlink, tc)
329 {
330 	const char *pathlink = "pathlink";
331 	struct stat sa, sb;
332 	int fd;
333 
334 	(void)memset(&sa, 0, sizeof(struct stat));
335 	(void)memset(&sb, 0, sizeof(struct stat));
336 
337 	fd = open(path, O_WRONLY | O_CREAT);
338 
339 	ATF_REQUIRE(fd >= 0);
340 	ATF_REQUIRE(symlink(path, pathlink) == 0);
341 	ATF_REQUIRE(stat(pathlink, &sa) == 0);
342 	ATF_REQUIRE(lstat(pathlink, &sb) == 0);
343 
344 	if (S_ISLNK(sa.st_mode) != 0)
345 		atf_tc_fail("stat(2) detected symbolic link");
346 
347 	if (S_ISLNK(sb.st_mode) == 0)
348 		atf_tc_fail("lstat(2) did not detect symbolic link");
349 
350 	if (sa.st_mode == sb.st_mode)
351 		atf_tc_fail("inconsistencies between stat(2) and lstat(2)");
352 
353 	ATF_REQUIRE(unlink(path) == 0);
354 	ATF_REQUIRE(unlink(pathlink) == 0);
355 }
356 
357 ATF_TC_CLEANUP(stat_symlink, tc)
358 {
359 	(void)unlink(path);
360 }
361 
362 ATF_TP_ADD_TCS(tp)
363 {
364 
365 	ATF_TP_ADD_TC(tp, stat_chflags);
366 	ATF_TP_ADD_TC(tp, stat_dir);
367 	ATF_TP_ADD_TC(tp, stat_err);
368 	ATF_TP_ADD_TC(tp, stat_mtime);
369 	ATF_TP_ADD_TC(tp, stat_perm);
370 	ATF_TP_ADD_TC(tp, stat_size);
371 	ATF_TP_ADD_TC(tp, stat_symlink);
372 
373 	return atf_no_error();
374 }
375