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