xref: /freebsd/lib/libc/tests/gen/ftw_test.c (revision 5e3934b15a2741b2de6b217e77dc9d798d740804)
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/wait.h>
32 #include <err.h>
33 #include <errno.h>
34 #include <fcntl.h>
35 #include <ftw.h>
36 #include <limits.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <spawn.h>
41 #include <unistd.h>
42 
43 #include <atf-c.h>
44 
45 extern char **environ;
46 
47 static char template[] = "testftw.XXXXXXXXXX";
48 static char dir[PATH_MAX];
49 static int ftwflags;
50 
51 static int
52 cb(const char *path, const struct stat *st, int type, struct FTW *f)
53 {
54 
55 	switch (type) {
56 	case FTW_D:
57 		if ((ftwflags & FTW_DEPTH) == 0)
58 			return (0);
59 		break;
60 	case FTW_DP:
61 		if ((ftwflags & FTW_DEPTH) != 0)
62 			return (0);
63 		break;
64 	case FTW_SL:
65 		if ((ftwflags & FTW_PHYS) != 0)
66 			return (0);
67 		break;
68 	}
69 	ATF_CHECK_MSG(false,
70 	    "unexpected path=%s type=%d f.level=%d\n",
71 	    path, type, f->level);
72 	return (0);
73 }
74 
75 ATF_TC_WITHOUT_HEAD(ftw_test);
76 ATF_TC_BODY(ftw_test, tc)
77 {
78 	int fd;
79 
80 	ATF_REQUIRE_MSG(mkdtemp(template) != NULL, "mkdtemp failed");
81 
82 	/* XXX: the path needs to be absolute for the 0/FTW_DEPTH testcases */
83 	ATF_REQUIRE_MSG(realpath(template, dir) != NULL,
84 	    "realpath failed; errno=%d", errno);
85 
86 	fd = open(dir, O_DIRECTORY|O_RDONLY);
87 	ATF_REQUIRE_MSG(fd != -1, "open failed; errno=%d", errno);
88 
89 	ATF_REQUIRE_MSG(mkdirat(fd, "d1", 0777) == 0,
90 	    "mkdirat failed; errno=%d", errno);
91 
92 	ATF_REQUIRE_MSG(symlinkat(dir, fd, "d1/looper") == 0,
93 	    "symlinkat failed; errno=%d", errno);
94 
95 	printf("ftwflags=FTW_PHYS\n");
96 	ftwflags = FTW_PHYS;
97 	ATF_REQUIRE_MSG(nftw(dir, cb, 10, ftwflags) != -1,
98 	    "nftw FTW_PHYS failed; errno=%d", errno);
99 
100 	printf("ftwflags=FTW_PHYS|FTW_DEPTH\n");
101 	ftwflags = FTW_PHYS|FTW_DEPTH;
102 	ATF_REQUIRE_MSG(nftw(dir, cb, 10, ftwflags) != -1,
103 	    "nftw FTW_PHYS|FTW_DEPTH failed; errno=%d", errno);
104 
105 	printf("ftwflags=0\n");
106 	ftwflags = 0;
107 	ATF_REQUIRE_MSG(nftw(dir, cb, 10, ftwflags) != -1,
108 	    "nftw 0 failed; errno=%d", errno);
109 
110 	printf("ftwflags=FTW_DEPTH\n");
111 	ftwflags = FTW_DEPTH;
112 	ATF_REQUIRE_MSG(nftw(dir, cb, 10, ftwflags) != -1,
113 	    "nftw FTW_DEPTH failed; errno=%d", errno);
114 
115 	close(fd);
116 }
117 
118 ATF_TP_ADD_TCS(tp)
119 {
120 
121 	ATF_TP_ADD_TC(tp, ftw_test);
122 
123 	return (atf_no_error());
124 }
125