1 /*- 2 * Copyright (c) 2012 Jilles Tjoelker 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27 /* 28 * Limited test program for nftw() as specified by IEEE Std. 1003.1-2008. 29 */ 30 31 #include <sys/cdefs.h> 32 __FBSDID("$FreeBSD$"); 33 34 #include <sys/wait.h> 35 #include <err.h> 36 #include <errno.h> 37 #include <fcntl.h> 38 #include <ftw.h> 39 #include <limits.h> 40 #include <stdio.h> 41 #include <stdlib.h> 42 #include <string.h> 43 #include <spawn.h> 44 #include <unistd.h> 45 46 #include <atf-c.h> 47 48 extern char **environ; 49 50 static char template[] = "testftw.XXXXXXXXXX"; 51 static char dir[PATH_MAX]; 52 static int failures; 53 static int ftwflags; 54 55 static int 56 cb(const char *path, const struct stat *st, int type, struct FTW *f) 57 { 58 59 switch (type) { 60 case FTW_D: 61 if ((ftwflags & FTW_DEPTH) == 0) 62 return (0); 63 break; 64 case FTW_DP: 65 if ((ftwflags & FTW_DEPTH) != 0) 66 return (0); 67 break; 68 case FTW_SL: 69 if ((ftwflags & FTW_PHYS) != 0) 70 return (0); 71 break; 72 } 73 ATF_CHECK_MSG(false, 74 "unexpected path=%s type=%d f.level=%d\n", 75 path, type, f->level); 76 return (0); 77 } 78 79 ATF_TC_WITHOUT_HEAD(ftw_test); 80 ATF_TC_BODY(ftw_test, tc) 81 { 82 int fd; 83 84 ATF_REQUIRE_MSG(mkdtemp(template) != NULL, "mkdtemp failed"); 85 86 /* XXX: the path needs to be absolute for the 0/FTW_DEPTH testcases */ 87 ATF_REQUIRE_MSG(realpath(template, dir) != NULL, 88 "realpath failed; errno=%d", errno); 89 90 fd = open(dir, O_DIRECTORY|O_RDONLY); 91 ATF_REQUIRE_MSG(fd != -1, "open failed; errno=%d", errno); 92 93 ATF_REQUIRE_MSG(mkdirat(fd, "d1", 0777) == 0, 94 "mkdirat failed; errno=%d", errno); 95 96 ATF_REQUIRE_MSG(symlinkat(dir, fd, "d1/looper") == 0, 97 "symlinkat failed; errno=%d", errno); 98 99 printf("ftwflags=FTW_PHYS\n"); 100 ftwflags = FTW_PHYS; 101 ATF_REQUIRE_MSG(nftw(dir, cb, 10, ftwflags) != -1, 102 "nftw FTW_PHYS failed; errno=%d", errno); 103 104 printf("ftwflags=FTW_PHYS|FTW_DEPTH\n"); 105 ftwflags = FTW_PHYS|FTW_DEPTH; 106 ATF_REQUIRE_MSG(nftw(dir, cb, 10, ftwflags) != -1, 107 "nftw FTW_PHYS|FTW_DEPTH failed; errno=%d", errno); 108 109 printf("ftwflags=0\n"); 110 ftwflags = 0; 111 ATF_REQUIRE_MSG(nftw(dir, cb, 10, ftwflags) != -1, 112 "nftw 0 failed; errno=%d", errno); 113 114 printf("ftwflags=FTW_DEPTH\n"); 115 ftwflags = FTW_DEPTH; 116 ATF_REQUIRE_MSG(nftw(dir, cb, 10, ftwflags) != -1, 117 "nftw FTW_DEPTH failed; errno=%d", errno); 118 119 close(fd); 120 } 121 122 ATF_TP_ADD_TCS(tp) 123 { 124 125 ATF_TP_ADD_TC(tp, ftw_test); 126 127 return (atf_no_error()); 128 } 129