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