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