1 /* $NetBSD: t_stat.c,v 1.4 2012/03/17 08:37:08 jruoho Exp $ */
4 * Copyright (c) 2011 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_stat.c,v 1.4 2012/03/17 08:37:08 jruoho Exp $");
35 #include <sys/socket.h>
36 #include <sys/types.h>
38 #include <arpa/inet.h>
51 #include <netinet/in.h>
54 static const char *path = "stat";
56 ATF_TC_WITH_CLEANUP(stat_chflags);
57 ATF_TC_HEAD(stat_chflags, tc)
59 atf_tc_set_md_var(tc, "descr", "Test chflags(2) with stat(2)");
62 ATF_TC_BODY(stat_chflags, tc)
67 (void)memset(&sa, 0, sizeof(struct stat));
68 (void)memset(&sb, 0, sizeof(struct stat));
70 fd = open(path, O_RDONLY | O_CREAT);
72 ATF_REQUIRE(fd != -1);
73 ATF_REQUIRE(stat(path, &sa) == 0);
74 ATF_REQUIRE(chflags(path, UF_NODUMP) == 0);
75 ATF_REQUIRE(stat(path, &sb) == 0);
77 if (sa.st_flags == sb.st_flags)
78 atf_tc_fail("stat(2) did not detect chflags(2)");
80 ATF_REQUIRE(close(fd) == 0);
81 ATF_REQUIRE(unlink(path) == 0);
84 ATF_TC_CLEANUP(stat_chflags, tc)
90 ATF_TC_HEAD(stat_dir, tc)
92 atf_tc_set_md_var(tc, "descr", "Test stat(2) with directories");
95 ATF_TC_BODY(stat_dir, tc)
97 const short depth = 2;
105 argv[0] = __UNCONST("/");
110 fts = fts_open(argv, ops, NULL);
111 ATF_REQUIRE(fts != NULL);
113 while ((ftse = fts_read(fts)) != NULL) {
115 if (ftse->fts_level < 1)
118 if (ftse->fts_level > depth) {
119 (void)fts_set(fts, ftse, FTS_SKIP);
123 switch(ftse->fts_info) {
127 (void)memset(&sa, 0, sizeof(struct stat));
128 (void)memset(&sb, 0, sizeof(struct stat));
130 ATF_REQUIRE(stat(ftse->fts_parent->fts_path,&sa) == 0);
131 ATF_REQUIRE(chdir(ftse->fts_path) == 0);
132 ATF_REQUIRE(stat(".", &sb) == 0);
135 * The previous two stat(2) calls
136 * should be for the same directory.
138 if (sa.st_dev != sb.st_dev || sa.st_ino != sb.st_ino)
139 atf_tc_fail("inconsistent stat(2)");
142 * Check that fts(3)'s stat(2)
143 * call equals the manual one.
145 if (sb.st_ino != ftse->fts_statp->st_ino)
146 atf_tc_fail("stat(2) and fts(3) differ");
155 (void)fts_close(fts);
159 ATF_TC_HEAD(stat_err, tc)
161 atf_tc_set_md_var(tc, "descr", "Test errors from the stat(2) family");
164 ATF_TC_BODY(stat_err, tc)
166 char buf[NAME_MAX + 1];
169 (void)memset(buf, 'x', sizeof(buf));
172 ATF_REQUIRE_ERRNO(EBADF, fstat(-1, &st) == -1);
175 ATF_REQUIRE_ERRNO(ENAMETOOLONG, stat(buf, &st) == -1);
178 ATF_REQUIRE_ERRNO(ENAMETOOLONG, lstat(buf, &st) == -1);
181 ATF_REQUIRE_ERRNO(EFAULT, stat((void *)-1, &st) == -1);
184 ATF_REQUIRE_ERRNO(EFAULT, lstat((void *)-1, &st) == -1);
187 ATF_REQUIRE_ERRNO(EFAULT, stat("/etc/passwd", (void *)-1) == -1);
190 ATF_REQUIRE_ERRNO(EFAULT, lstat("/etc/passwd", (void *)-1) == -1);
193 ATF_REQUIRE_ERRNO(ENOENT, stat("/a/b/c/d/e/f/g/h/i/j/k", &st) == -1);
196 ATF_REQUIRE_ERRNO(ENOENT, lstat("/a/b/c/d/e/f/g/h/i/j/k", &st) == -1);
199 ATF_TC_WITH_CLEANUP(stat_mtime);
200 ATF_TC_HEAD(stat_mtime, tc)
202 atf_tc_set_md_var(tc, "descr", "Test modification times with stat(2)");
205 ATF_TC_BODY(stat_mtime, tc)
211 for (i = 0; i < __arraycount(fd); i++) {
213 (void)memset(&sa, 0, sizeof(struct stat));
214 (void)memset(&sb, 0, sizeof(struct stat));
216 fd[i] = open(path, O_WRONLY | O_CREAT);
218 ATF_REQUIRE(fd[i] != -1);
219 ATF_REQUIRE(write(fd[i], "X", 1) == 1);
220 ATF_REQUIRE(stat(path, &sa) == 0);
224 ATF_REQUIRE(write(fd[i], "X", 1) == 1);
225 ATF_REQUIRE(stat(path, &sb) == 0);
227 ATF_REQUIRE(close(fd[i]) == 0);
228 ATF_REQUIRE(unlink(path) == 0);
230 if (sa.st_mtime == sb.st_mtime)
231 atf_tc_fail("mtimes did not change");
235 ATF_TC_CLEANUP(stat_mtime, tc)
240 ATF_TC_WITH_CLEANUP(stat_perm);
241 ATF_TC_HEAD(stat_perm, tc)
243 atf_tc_set_md_var(tc, "descr", "Test permissions with stat(2)");
244 atf_tc_set_md_var(tc, "require.user", "root");
247 ATF_TC_BODY(stat_perm, tc)
254 (void)memset(&sa, 0, sizeof(struct stat));
255 (void)memset(&sb, 0, sizeof(struct stat));
260 fd = open(path, O_RDONLY | O_CREAT);
262 ATF_REQUIRE(fd != -1);
263 ATF_REQUIRE(fstat(fd, &sa) == 0);
264 ATF_REQUIRE(stat(path, &sb) == 0);
266 if (gid != sa.st_gid || sa.st_gid != sb.st_gid)
267 atf_tc_fail("invalid GID");
269 if (uid != sa.st_uid || sa.st_uid != sb.st_uid)
270 atf_tc_fail("invalid UID");
272 ATF_REQUIRE(close(fd) == 0);
273 ATF_REQUIRE(unlink(path) == 0);
276 ATF_TC_CLEANUP(stat_perm, tc)
281 ATF_TC_WITH_CLEANUP(stat_size);
282 ATF_TC_HEAD(stat_size, tc)
284 atf_tc_set_md_var(tc, "descr", "Test file sizes with stat(2)");
287 ATF_TC_BODY(stat_size, tc)
289 struct stat sa, sb, sc;
294 fd = open(path, O_WRONLY | O_CREAT);
295 ATF_REQUIRE(fd >= 0);
297 for (i = 0; i < n; i++) {
299 (void)memset(&sa, 0, sizeof(struct stat));
300 (void)memset(&sb, 0, sizeof(struct stat));
301 (void)memset(&sc, 0, sizeof(struct stat));
303 ATF_REQUIRE(fstat(fd, &sa) == 0);
304 ATF_REQUIRE(write(fd, "X", 1) == 1);
305 ATF_REQUIRE(fstat(fd, &sb) == 0);
306 ATF_REQUIRE(stat(path, &sc) == 0);
308 if (sa.st_size + 1 != sb.st_size)
309 atf_tc_fail("invalid file size");
311 if (sb.st_size != sc.st_size)
312 atf_tc_fail("stat(2) and fstat(2) mismatch");
315 ATF_REQUIRE(close(fd) == 0);
316 ATF_REQUIRE(unlink(path) == 0);
319 ATF_TC_CLEANUP(stat_size, tc)
325 ATF_TC_HEAD(stat_socket, tc)
327 atf_tc_set_md_var(tc, "descr", "Test fstat(2) with "
328 "a socket (PR kern/46077)");
331 ATF_TC_BODY(stat_socket, tc)
333 struct sockaddr_in addr;
338 (void)memset(&st, 0, sizeof(struct stat));
339 (void)memset(&addr, 0, sizeof(struct sockaddr_in));
341 fd = socket(AF_INET, SOCK_STREAM, 0);
342 ATF_REQUIRE(fd >= 0);
344 flags = fcntl(fd, F_GETFL);
346 ATF_REQUIRE(flags != -1);
347 ATF_REQUIRE(fcntl(fd, F_SETFL, flags | O_NONBLOCK) != -1);
348 ATF_REQUIRE(inet_pton(AF_INET, "127.0.0.1", &iaddr) == 1);
350 addr.sin_port = htons(42);
351 addr.sin_family = AF_INET;
352 addr.sin_addr.s_addr = iaddr;
356 ATF_REQUIRE_ERRNO(EINPROGRESS,
357 connect(fd, (struct sockaddr *)&addr,
358 sizeof(struct sockaddr_in)) == -1);
362 if (fstat(fd, &st) != 0 || errno != 0)
363 atf_tc_fail("fstat(2) failed for a EINPROGRESS socket");
368 ATF_TC_WITH_CLEANUP(stat_symlink);
369 ATF_TC_HEAD(stat_symlink, tc)
371 atf_tc_set_md_var(tc, "descr", "Test symbolic links with stat(2)");
374 ATF_TC_BODY(stat_symlink, tc)
376 const char *pathlink = "pathlink";
380 (void)memset(&sa, 0, sizeof(struct stat));
381 (void)memset(&sb, 0, sizeof(struct stat));
383 fd = open(path, O_WRONLY | O_CREAT);
385 ATF_REQUIRE(fd >= 0);
386 ATF_REQUIRE(symlink(path, pathlink) == 0);
387 ATF_REQUIRE(stat(pathlink, &sa) == 0);
388 ATF_REQUIRE(lstat(pathlink, &sb) == 0);
390 if (S_ISLNK(sa.st_mode) != 0)
391 atf_tc_fail("stat(2) detected symbolic link");
393 if (S_ISLNK(sb.st_mode) == 0)
394 atf_tc_fail("lstat(2) did not detect symbolic link");
396 if (sa.st_mode == sb.st_mode)
397 atf_tc_fail("inconsistencies between stat(2) and lstat(2)");
399 ATF_REQUIRE(unlink(path) == 0);
400 ATF_REQUIRE(unlink(pathlink) == 0);
403 ATF_TC_CLEANUP(stat_symlink, tc)
411 ATF_TP_ADD_TC(tp, stat_chflags);
412 ATF_TP_ADD_TC(tp, stat_dir);
413 ATF_TP_ADD_TC(tp, stat_err);
414 ATF_TP_ADD_TC(tp, stat_mtime);
415 ATF_TP_ADD_TC(tp, stat_perm);
416 ATF_TP_ADD_TC(tp, stat_size);
417 ATF_TP_ADD_TC(tp, stat_socket);
418 ATF_TP_ADD_TC(tp, stat_symlink);
420 return atf_no_error();