xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/nftw/test.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker /*
2*49cdfc7eSAndroid Build Coastguard Worker  *
3*49cdfc7eSAndroid Build Coastguard Worker  *   Copyright (c) International Business Machines  Corp., 2002
4*49cdfc7eSAndroid Build Coastguard Worker  *
5*49cdfc7eSAndroid Build Coastguard Worker  *   This program is free software;  you can redistribute it and/or modify
6*49cdfc7eSAndroid Build Coastguard Worker  *   it under the terms of the GNU General Public License as published by
7*49cdfc7eSAndroid Build Coastguard Worker  *   the Free Software Foundation; either version 2 of the License, or
8*49cdfc7eSAndroid Build Coastguard Worker  *   (at your option) any later version.
9*49cdfc7eSAndroid Build Coastguard Worker  *
10*49cdfc7eSAndroid Build Coastguard Worker  *   This program is distributed in the hope that it will be useful,
11*49cdfc7eSAndroid Build Coastguard Worker  *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
12*49cdfc7eSAndroid Build Coastguard Worker  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
13*49cdfc7eSAndroid Build Coastguard Worker  *   the GNU General Public License for more details.
14*49cdfc7eSAndroid Build Coastguard Worker  *
15*49cdfc7eSAndroid Build Coastguard Worker  *   You should have received a copy of the GNU General Public License
16*49cdfc7eSAndroid Build Coastguard Worker  *   along with this program;  if not, write to the Free Software
17*49cdfc7eSAndroid Build Coastguard Worker  *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18*49cdfc7eSAndroid Build Coastguard Worker  */
19*49cdfc7eSAndroid Build Coastguard Worker 
20*49cdfc7eSAndroid Build Coastguard Worker /* 10/31/2002   Port to LTP     [email protected] */
21*49cdfc7eSAndroid Build Coastguard Worker /* 06/30/2001   Port to Linux   [email protected] */
22*49cdfc7eSAndroid Build Coastguard Worker 
23*49cdfc7eSAndroid Build Coastguard Worker /*
24*49cdfc7eSAndroid Build Coastguard Worker  * NAME
25*49cdfc7eSAndroid Build Coastguard Worker  *      test.c - Test functions for nftw()
26*49cdfc7eSAndroid Build Coastguard Worker  */
27*49cdfc7eSAndroid Build Coastguard Worker 
28*49cdfc7eSAndroid Build Coastguard Worker #include "nftw.h"
29*49cdfc7eSAndroid Build Coastguard Worker 
30*49cdfc7eSAndroid Build Coastguard Worker extern int callback(const char *path);
31*49cdfc7eSAndroid Build Coastguard Worker 
32*49cdfc7eSAndroid Build Coastguard Worker extern pathdata pathdat[];
33*49cdfc7eSAndroid Build Coastguard Worker extern struct list mnem[], badlist[];
34*49cdfc7eSAndroid Build Coastguard Worker extern char *dirlist[NDIRLISTENTS], *goodlist[];
35*49cdfc7eSAndroid Build Coastguard Worker extern int npathdats, ngoods, nbads, nmnem, visit, s2, next_fd[4];
36*49cdfc7eSAndroid Build Coastguard Worker 
37*49cdfc7eSAndroid Build Coastguard Worker extern FILE *temp;
38*49cdfc7eSAndroid Build Coastguard Worker /*
39*49cdfc7eSAndroid Build Coastguard Worker  *    void test1A()     - tests the assertion:
40*49cdfc7eSAndroid Build Coastguard Worker  *      A call to int nftw(const char *path, int (*fn)(const char *, const
41*49cdfc7eSAndroid Build Coastguard Worker  *      struct stat *, int, struct FTW *), int depth, int flags) shall
42*49cdfc7eSAndroid Build Coastguard Worker  *      recursively descend the directory hierarchy rooted in path until it
43*49cdfc7eSAndroid Build Coastguard Worker  *      has traversed the whole tree, calling the function fn for each object
44*49cdfc7eSAndroid Build Coastguard Worker  *      in the directory tree, and return 0.
45*49cdfc7eSAndroid Build Coastguard Worker  */
46*49cdfc7eSAndroid Build Coastguard Worker 
test1A(void)47*49cdfc7eSAndroid Build Coastguard Worker void test1A(void)
48*49cdfc7eSAndroid Build Coastguard Worker {
49*49cdfc7eSAndroid Build Coastguard Worker 	int i, j, ret;
50*49cdfc7eSAndroid Build Coastguard Worker 
51*49cdfc7eSAndroid Build Coastguard Worker 	temp = stderr;
52*49cdfc7eSAndroid Build Coastguard Worker #ifdef DEBUG
53*49cdfc7eSAndroid Build Coastguard Worker 	fprintf(temp, "TEST: nftw() succeeds\n");
54*49cdfc7eSAndroid Build Coastguard Worker #endif
55*49cdfc7eSAndroid Build Coastguard Worker 
56*49cdfc7eSAndroid Build Coastguard Worker 	visit = 0;
57*49cdfc7eSAndroid Build Coastguard Worker 	if ((ret = nftw("./tmp/data/dirh", test_func1, MAX_FD, 0)) == -1) {
58*49cdfc7eSAndroid Build Coastguard Worker 		perror("ERROR: nftw failed");
59*49cdfc7eSAndroid Build Coastguard Worker 		cleanup_function();
60*49cdfc7eSAndroid Build Coastguard Worker 		fail_exit();
61*49cdfc7eSAndroid Build Coastguard Worker 	}
62*49cdfc7eSAndroid Build Coastguard Worker 
63*49cdfc7eSAndroid Build Coastguard Worker 	if (ret == 999) {
64*49cdfc7eSAndroid Build Coastguard Worker 		cleanup_function();
65*49cdfc7eSAndroid Build Coastguard Worker 		fail_exit();
66*49cdfc7eSAndroid Build Coastguard Worker 	}
67*49cdfc7eSAndroid Build Coastguard Worker #ifdef DEBUG
68*49cdfc7eSAndroid Build Coastguard Worker 	fprintf(temp, "TEST: Whole tree traversed\n");
69*49cdfc7eSAndroid Build Coastguard Worker #endif
70*49cdfc7eSAndroid Build Coastguard Worker 
71*49cdfc7eSAndroid Build Coastguard Worker 	if (visit != ngoods) {
72*49cdfc7eSAndroid Build Coastguard Worker 		fprintf(temp, "ERROR: Count of objects visited incorrect\n");
73*49cdfc7eSAndroid Build Coastguard Worker 		fprintf(temp, "       Expected %d, Received %d\n", ngoods,
74*49cdfc7eSAndroid Build Coastguard Worker 			visit);
75*49cdfc7eSAndroid Build Coastguard Worker 		cleanup_function();
76*49cdfc7eSAndroid Build Coastguard Worker 		fail_exit();
77*49cdfc7eSAndroid Build Coastguard Worker 	}
78*49cdfc7eSAndroid Build Coastguard Worker 
79*49cdfc7eSAndroid Build Coastguard Worker 	for (i = 0; i < visit; i++) {
80*49cdfc7eSAndroid Build Coastguard Worker 		for (j = 0; j < ngoods; j++) {
81*49cdfc7eSAndroid Build Coastguard Worker 			if (strcmp(dirlist[i], goodlist[j]) == 0) {
82*49cdfc7eSAndroid Build Coastguard Worker 				free(dirlist[i]);
83*49cdfc7eSAndroid Build Coastguard Worker 				dirlist[i] = NULL;
84*49cdfc7eSAndroid Build Coastguard Worker 				break;
85*49cdfc7eSAndroid Build Coastguard Worker 			}
86*49cdfc7eSAndroid Build Coastguard Worker 		}
87*49cdfc7eSAndroid Build Coastguard Worker 	}
88*49cdfc7eSAndroid Build Coastguard Worker 
89*49cdfc7eSAndroid Build Coastguard Worker 	for (i = 0; i < visit; i++) {
90*49cdfc7eSAndroid Build Coastguard Worker 		if (dirlist[i] != NULL) {
91*49cdfc7eSAndroid Build Coastguard Worker 			free(dirlist[i]);
92*49cdfc7eSAndroid Build Coastguard Worker 			fprintf(temp, "ERROR: Unexpected visit to %s\n",
93*49cdfc7eSAndroid Build Coastguard Worker 				dirlist[i]);
94*49cdfc7eSAndroid Build Coastguard Worker 			cleanup_function();
95*49cdfc7eSAndroid Build Coastguard Worker 			fail_exit();
96*49cdfc7eSAndroid Build Coastguard Worker 		}
97*49cdfc7eSAndroid Build Coastguard Worker 	}
98*49cdfc7eSAndroid Build Coastguard Worker }
99*49cdfc7eSAndroid Build Coastguard Worker 
100*49cdfc7eSAndroid Build Coastguard Worker /*
101*49cdfc7eSAndroid Build Coastguard Worker  *    void test2A()     - tests the assertion:
102*49cdfc7eSAndroid Build Coastguard Worker  *      A call to int nftw(const char *path, int (*fn)(const char *, const
103*49cdfc7eSAndroid Build Coastguard Worker  *      struct stat *, int, struct FTW *), int depth, int flags) when flags
104*49cdfc7eSAndroid Build Coastguard Worker  *      contains FTW_PHYS shall not traverse symbolic links.
105*49cdfc7eSAndroid Build Coastguard Worker  */
106*49cdfc7eSAndroid Build Coastguard Worker 
test2A(void)107*49cdfc7eSAndroid Build Coastguard Worker void test2A(void)
108*49cdfc7eSAndroid Build Coastguard Worker {
109*49cdfc7eSAndroid Build Coastguard Worker 	int i, ret;
110*49cdfc7eSAndroid Build Coastguard Worker 
111*49cdfc7eSAndroid Build Coastguard Worker 	temp = stderr;
112*49cdfc7eSAndroid Build Coastguard Worker #ifdef DEBUG
113*49cdfc7eSAndroid Build Coastguard Worker 	fprintf(temp,
114*49cdfc7eSAndroid Build Coastguard Worker 		"TEST: nftw with FTW_PHYS does not follow symbolic links\n");
115*49cdfc7eSAndroid Build Coastguard Worker #endif
116*49cdfc7eSAndroid Build Coastguard Worker 
117*49cdfc7eSAndroid Build Coastguard Worker 	visit = 0;
118*49cdfc7eSAndroid Build Coastguard Worker 	if ((ret = nftw("./tmp/data/dirl", test_func1, MAX_FD, FTW_PHYS))
119*49cdfc7eSAndroid Build Coastguard Worker 	    == -1) {
120*49cdfc7eSAndroid Build Coastguard Worker 		perror("nftw");
121*49cdfc7eSAndroid Build Coastguard Worker 		cleanup_function();
122*49cdfc7eSAndroid Build Coastguard Worker 		fail_exit();
123*49cdfc7eSAndroid Build Coastguard Worker 	}
124*49cdfc7eSAndroid Build Coastguard Worker 
125*49cdfc7eSAndroid Build Coastguard Worker 	if (ret == 999) {
126*49cdfc7eSAndroid Build Coastguard Worker 		cleanup_function();
127*49cdfc7eSAndroid Build Coastguard Worker 		fail_exit();
128*49cdfc7eSAndroid Build Coastguard Worker 	}
129*49cdfc7eSAndroid Build Coastguard Worker 
130*49cdfc7eSAndroid Build Coastguard Worker 	if (visit != NO_LINK_CNT) {
131*49cdfc7eSAndroid Build Coastguard Worker 		fprintf(temp,
132*49cdfc7eSAndroid Build Coastguard Worker 			"ERROR: Expected %d files to be visited.  nftw() visited %d\n",
133*49cdfc7eSAndroid Build Coastguard Worker 			NO_LINK_CNT, visit);
134*49cdfc7eSAndroid Build Coastguard Worker 		cleanup_function();
135*49cdfc7eSAndroid Build Coastguard Worker 		fail_exit();
136*49cdfc7eSAndroid Build Coastguard Worker 	}
137*49cdfc7eSAndroid Build Coastguard Worker 
138*49cdfc7eSAndroid Build Coastguard Worker 	for (i = 0; i < visit; i++) {
139*49cdfc7eSAndroid Build Coastguard Worker 		if (dirlist[i] != NULL)
140*49cdfc7eSAndroid Build Coastguard Worker 			free(dirlist[i]);
141*49cdfc7eSAndroid Build Coastguard Worker 	}
142*49cdfc7eSAndroid Build Coastguard Worker }
143*49cdfc7eSAndroid Build Coastguard Worker 
144*49cdfc7eSAndroid Build Coastguard Worker /*
145*49cdfc7eSAndroid Build Coastguard Worker  *    void test3A()     - tests the assertion:
146*49cdfc7eSAndroid Build Coastguard Worker  *      A call to int nftw(const char *path, int (*fn)(const char *, const
147*49cdfc7eSAndroid Build Coastguard Worker  *      struct stat *, int, struct FTW *), int depth, int flags) when flags
148*49cdfc7eSAndroid Build Coastguard Worker  *      does not contain FTW_PHYS shall follow links instead of reporting
149*49cdfc7eSAndroid Build Coastguard Worker  *      them and shall not report the same file twice.
150*49cdfc7eSAndroid Build Coastguard Worker  */
151*49cdfc7eSAndroid Build Coastguard Worker 
test3A(void)152*49cdfc7eSAndroid Build Coastguard Worker void test3A(void)
153*49cdfc7eSAndroid Build Coastguard Worker {
154*49cdfc7eSAndroid Build Coastguard Worker 	int ret;
155*49cdfc7eSAndroid Build Coastguard Worker 
156*49cdfc7eSAndroid Build Coastguard Worker #ifdef DEBUG
157*49cdfc7eSAndroid Build Coastguard Worker 	fprintf(temp, "TEST: nftw without FTW_PHYS follows symbolic links\n");
158*49cdfc7eSAndroid Build Coastguard Worker #endif
159*49cdfc7eSAndroid Build Coastguard Worker 
160*49cdfc7eSAndroid Build Coastguard Worker 	visit = 0;
161*49cdfc7eSAndroid Build Coastguard Worker 
162*49cdfc7eSAndroid Build Coastguard Worker 	if ((ret = nftw("./tmp/data/dirl", test_func3, MAX_FD, 0)) == -1) {
163*49cdfc7eSAndroid Build Coastguard Worker 		perror("nftw");
164*49cdfc7eSAndroid Build Coastguard Worker 		cleanup_function();
165*49cdfc7eSAndroid Build Coastguard Worker 		fail_exit();
166*49cdfc7eSAndroid Build Coastguard Worker 	}
167*49cdfc7eSAndroid Build Coastguard Worker 	if (ret == 999) {
168*49cdfc7eSAndroid Build Coastguard Worker 		cleanup_function();
169*49cdfc7eSAndroid Build Coastguard Worker 		fail_exit();
170*49cdfc7eSAndroid Build Coastguard Worker 	}
171*49cdfc7eSAndroid Build Coastguard Worker 
172*49cdfc7eSAndroid Build Coastguard Worker 	if (visit != LINK_CNT - 1) {
173*49cdfc7eSAndroid Build Coastguard Worker 		fprintf(temp,
174*49cdfc7eSAndroid Build Coastguard Worker 			"ERROR: Expected %d files to be visited.  nftw() visited %d\n",
175*49cdfc7eSAndroid Build Coastguard Worker 			LINK_CNT - 1, visit);
176*49cdfc7eSAndroid Build Coastguard Worker 		cleanup_function();
177*49cdfc7eSAndroid Build Coastguard Worker 		fail_exit();
178*49cdfc7eSAndroid Build Coastguard Worker 	}
179*49cdfc7eSAndroid Build Coastguard Worker }
180*49cdfc7eSAndroid Build Coastguard Worker 
181*49cdfc7eSAndroid Build Coastguard Worker /*
182*49cdfc7eSAndroid Build Coastguard Worker  *    void test4A()     - tests the assertion:
183*49cdfc7eSAndroid Build Coastguard Worker  *      A call to int nftw(const char *path, int (*fn)(const char *, const
184*49cdfc7eSAndroid Build Coastguard Worker  *      struct stat *, int, struct FTW *), int depth, int flags) when flags
185*49cdfc7eSAndroid Build Coastguard Worker  *      contains FTW_DEPTH shall report all files in a directory before
186*49cdfc7eSAndroid Build Coastguard Worker  *      reporting the directory.
187*49cdfc7eSAndroid Build Coastguard Worker  */
188*49cdfc7eSAndroid Build Coastguard Worker 
test4A(void)189*49cdfc7eSAndroid Build Coastguard Worker void test4A(void)
190*49cdfc7eSAndroid Build Coastguard Worker {
191*49cdfc7eSAndroid Build Coastguard Worker 	char path[] = "./tmp/data/d777";
192*49cdfc7eSAndroid Build Coastguard Worker 	int ret_val;
193*49cdfc7eSAndroid Build Coastguard Worker 
194*49cdfc7eSAndroid Build Coastguard Worker #ifdef DEBUG
195*49cdfc7eSAndroid Build Coastguard Worker 	fprintf(temp, "TEST: Verify traversal with FTW_DEPTH set\n");
196*49cdfc7eSAndroid Build Coastguard Worker #endif
197*49cdfc7eSAndroid Build Coastguard Worker 
198*49cdfc7eSAndroid Build Coastguard Worker 	visit = 0;
199*49cdfc7eSAndroid Build Coastguard Worker 	if ((ret_val = nftw(path, test_func4, MAX_FD, FTW_DEPTH)) == -1) {
200*49cdfc7eSAndroid Build Coastguard Worker 		perror("nftw");
201*49cdfc7eSAndroid Build Coastguard Worker 		cleanup_function();
202*49cdfc7eSAndroid Build Coastguard Worker 		fail_exit();
203*49cdfc7eSAndroid Build Coastguard Worker 	}
204*49cdfc7eSAndroid Build Coastguard Worker 	if (ret_val != 999) {
205*49cdfc7eSAndroid Build Coastguard Worker 		fprintf(temp, "ERROR: %s never visited\n", path);
206*49cdfc7eSAndroid Build Coastguard Worker 		cleanup_function();
207*49cdfc7eSAndroid Build Coastguard Worker 		fail_exit();
208*49cdfc7eSAndroid Build Coastguard Worker 	}
209*49cdfc7eSAndroid Build Coastguard Worker 
210*49cdfc7eSAndroid Build Coastguard Worker 	if (visit != 2) {
211*49cdfc7eSAndroid Build Coastguard Worker 		fprintf(temp, "ERROR: Visited directory before contents\n");
212*49cdfc7eSAndroid Build Coastguard Worker 		cleanup_function();
213*49cdfc7eSAndroid Build Coastguard Worker 		fail_exit();
214*49cdfc7eSAndroid Build Coastguard Worker 	}
215*49cdfc7eSAndroid Build Coastguard Worker }
216*49cdfc7eSAndroid Build Coastguard Worker 
217*49cdfc7eSAndroid Build Coastguard Worker /*
218*49cdfc7eSAndroid Build Coastguard Worker  *    void test5A()     - tests the assertion:
219*49cdfc7eSAndroid Build Coastguard Worker  *      A call to int nftw(const char *path, int (*fn)(const char *, const
220*49cdfc7eSAndroid Build Coastguard Worker  *      struct stat *, int, struct FTW *), int depth, int flags) when flags
221*49cdfc7eSAndroid Build Coastguard Worker  *      does not contain FTW_DEPTH shall report a directory before reporting
222*49cdfc7eSAndroid Build Coastguard Worker  *      the files in that directory.
223*49cdfc7eSAndroid Build Coastguard Worker  */
224*49cdfc7eSAndroid Build Coastguard Worker 
test5A(void)225*49cdfc7eSAndroid Build Coastguard Worker void test5A(void)
226*49cdfc7eSAndroid Build Coastguard Worker {
227*49cdfc7eSAndroid Build Coastguard Worker 	char path[] = "./tmp/data/d777";
228*49cdfc7eSAndroid Build Coastguard Worker 	int ret_val;
229*49cdfc7eSAndroid Build Coastguard Worker 
230*49cdfc7eSAndroid Build Coastguard Worker #ifdef DEBUG
231*49cdfc7eSAndroid Build Coastguard Worker 	fprintf(temp, "TEST: Verify traversal without FTW_DEPTH set\n");
232*49cdfc7eSAndroid Build Coastguard Worker #endif
233*49cdfc7eSAndroid Build Coastguard Worker 
234*49cdfc7eSAndroid Build Coastguard Worker 	visit = 0;
235*49cdfc7eSAndroid Build Coastguard Worker 	if ((ret_val = nftw(path, test_func4, MAX_FD, 0)) == -1) {
236*49cdfc7eSAndroid Build Coastguard Worker 		perror("nftw");
237*49cdfc7eSAndroid Build Coastguard Worker 		cleanup_function();
238*49cdfc7eSAndroid Build Coastguard Worker 		fail_exit();
239*49cdfc7eSAndroid Build Coastguard Worker 	}
240*49cdfc7eSAndroid Build Coastguard Worker 	if (ret_val != 999) {
241*49cdfc7eSAndroid Build Coastguard Worker 		fprintf(temp, "ERROR: %s never visited\n", path);
242*49cdfc7eSAndroid Build Coastguard Worker 		cleanup_function();
243*49cdfc7eSAndroid Build Coastguard Worker 		fail_exit();
244*49cdfc7eSAndroid Build Coastguard Worker 	}
245*49cdfc7eSAndroid Build Coastguard Worker 
246*49cdfc7eSAndroid Build Coastguard Worker 	if (visit != 1) {
247*49cdfc7eSAndroid Build Coastguard Worker 		fprintf(temp, "ERROR: Visited contents before directory\n");
248*49cdfc7eSAndroid Build Coastguard Worker 		cleanup_function();
249*49cdfc7eSAndroid Build Coastguard Worker 		fail_exit();
250*49cdfc7eSAndroid Build Coastguard Worker 	}
251*49cdfc7eSAndroid Build Coastguard Worker }
252*49cdfc7eSAndroid Build Coastguard Worker 
253*49cdfc7eSAndroid Build Coastguard Worker /*
254*49cdfc7eSAndroid Build Coastguard Worker  *    void test6A()     - tests the assertion:
255*49cdfc7eSAndroid Build Coastguard Worker  *      A call to int nftw(const char *path, int (*fn)(const char *, const
256*49cdfc7eSAndroid Build Coastguard Worker  *      struct stat *, int, struct FTW *), int depth, int flags) when flags
257*49cdfc7eSAndroid Build Coastguard Worker  *      contains FTW_CHDIR shall change the current working directory to each
258*49cdfc7eSAndroid Build Coastguard Worker  *      directory as it reports files in that directory.
259*49cdfc7eSAndroid Build Coastguard Worker  */
260*49cdfc7eSAndroid Build Coastguard Worker 
test6A(void)261*49cdfc7eSAndroid Build Coastguard Worker void test6A(void)
262*49cdfc7eSAndroid Build Coastguard Worker {
263*49cdfc7eSAndroid Build Coastguard Worker 	char path[PATH_MAX + NAME_MAX];
264*49cdfc7eSAndroid Build Coastguard Worker 	int ret_val;
265*49cdfc7eSAndroid Build Coastguard Worker 
266*49cdfc7eSAndroid Build Coastguard Worker 	if (getcwd(path, sizeof(path)) == NULL) {
267*49cdfc7eSAndroid Build Coastguard Worker 		perror("getcwd");
268*49cdfc7eSAndroid Build Coastguard Worker 		cleanup_function();
269*49cdfc7eSAndroid Build Coastguard Worker 		fail_exit();
270*49cdfc7eSAndroid Build Coastguard Worker 	}
271*49cdfc7eSAndroid Build Coastguard Worker 	(void)strcat(path, "/tmp/data/dirh");
272*49cdfc7eSAndroid Build Coastguard Worker 
273*49cdfc7eSAndroid Build Coastguard Worker #ifdef DEBUG
274*49cdfc7eSAndroid Build Coastguard Worker 	fprintf(temp,
275*49cdfc7eSAndroid Build Coastguard Worker 		"TEST: nftw with FTW_CHDIR changes to each dir before reporting files in it\n");
276*49cdfc7eSAndroid Build Coastguard Worker #endif
277*49cdfc7eSAndroid Build Coastguard Worker 
278*49cdfc7eSAndroid Build Coastguard Worker 	ret_val = nftw(path, test_func5, MAX_FD, FTW_CHDIR);
279*49cdfc7eSAndroid Build Coastguard Worker 	if (ret_val == -1) {
280*49cdfc7eSAndroid Build Coastguard Worker 		perror("nftw");
281*49cdfc7eSAndroid Build Coastguard Worker 		cleanup_function();
282*49cdfc7eSAndroid Build Coastguard Worker 		fail_exit();
283*49cdfc7eSAndroid Build Coastguard Worker 	}
284*49cdfc7eSAndroid Build Coastguard Worker 	if ((ret_val == 998) || (ret_val == 999)) {
285*49cdfc7eSAndroid Build Coastguard Worker 		cleanup_function();
286*49cdfc7eSAndroid Build Coastguard Worker 		fail_exit();
287*49cdfc7eSAndroid Build Coastguard Worker 	}
288*49cdfc7eSAndroid Build Coastguard Worker }
289*49cdfc7eSAndroid Build Coastguard Worker 
290*49cdfc7eSAndroid Build Coastguard Worker /*
291*49cdfc7eSAndroid Build Coastguard Worker  *    void test7A()     - tests the assertion:
292*49cdfc7eSAndroid Build Coastguard Worker  *      A call to int nftw(const char *path, int (*fn)(const char *, const
293*49cdfc7eSAndroid Build Coastguard Worker  *      struct stat *, int, struct FTW *), int depth, int flags) shall pass
294*49cdfc7eSAndroid Build Coastguard Worker  *      the path-name of the current object as the first argument of the
295*49cdfc7eSAndroid Build Coastguard Worker  *      function fn.
296*49cdfc7eSAndroid Build Coastguard Worker  */
297*49cdfc7eSAndroid Build Coastguard Worker 
test7A(void)298*49cdfc7eSAndroid Build Coastguard Worker void test7A(void)
299*49cdfc7eSAndroid Build Coastguard Worker {
300*49cdfc7eSAndroid Build Coastguard Worker 	int ret;
301*49cdfc7eSAndroid Build Coastguard Worker 
302*49cdfc7eSAndroid Build Coastguard Worker #ifdef DEBUG
303*49cdfc7eSAndroid Build Coastguard Worker 	fprintf(temp, "TEST: nftw passes pathname as first argument to fn()\n");
304*49cdfc7eSAndroid Build Coastguard Worker #endif
305*49cdfc7eSAndroid Build Coastguard Worker 
306*49cdfc7eSAndroid Build Coastguard Worker 	if ((ret = nftw("./tmp/data/dirg", test_func7, MAX_FD, 0)) == -1) {
307*49cdfc7eSAndroid Build Coastguard Worker 		perror("nftw");
308*49cdfc7eSAndroid Build Coastguard Worker 		cleanup_function();
309*49cdfc7eSAndroid Build Coastguard Worker 		fail_exit();
310*49cdfc7eSAndroid Build Coastguard Worker 	}
311*49cdfc7eSAndroid Build Coastguard Worker 
312*49cdfc7eSAndroid Build Coastguard Worker 	if (ret == 999) {
313*49cdfc7eSAndroid Build Coastguard Worker 		cleanup_function();
314*49cdfc7eSAndroid Build Coastguard Worker 		fail_exit();
315*49cdfc7eSAndroid Build Coastguard Worker 	}
316*49cdfc7eSAndroid Build Coastguard Worker }
317*49cdfc7eSAndroid Build Coastguard Worker 
318*49cdfc7eSAndroid Build Coastguard Worker /*
319*49cdfc7eSAndroid Build Coastguard Worker  *    void test8A()    - tests the assertion:
320*49cdfc7eSAndroid Build Coastguard Worker  *      A call to int nftw(const char *path, int (*fn)(const char *, const
321*49cdfc7eSAndroid Build Coastguard Worker  *      struct stat *, int, struct FTW *), int depth, int flags) shall pass a
322*49cdfc7eSAndroid Build Coastguard Worker  *      pointer to a stat structure containing information about the current
323*49cdfc7eSAndroid Build Coastguard Worker  *      object as the second argument to fn.
324*49cdfc7eSAndroid Build Coastguard Worker  */
325*49cdfc7eSAndroid Build Coastguard Worker 
test8A(void)326*49cdfc7eSAndroid Build Coastguard Worker void test8A(void)
327*49cdfc7eSAndroid Build Coastguard Worker {
328*49cdfc7eSAndroid Build Coastguard Worker 	int ret;
329*49cdfc7eSAndroid Build Coastguard Worker 
330*49cdfc7eSAndroid Build Coastguard Worker #ifdef DEBUG
331*49cdfc7eSAndroid Build Coastguard Worker 	fprintf(temp,
332*49cdfc7eSAndroid Build Coastguard Worker 		"TEST: nftw passes stat struct as second argument to fn()\n");
333*49cdfc7eSAndroid Build Coastguard Worker #endif
334*49cdfc7eSAndroid Build Coastguard Worker 
335*49cdfc7eSAndroid Build Coastguard Worker 	if ((ret = nftw("./tmp/data/dirg", test_func8, MAX_FD, 0)) == -1) {
336*49cdfc7eSAndroid Build Coastguard Worker 		perror("nftw");
337*49cdfc7eSAndroid Build Coastguard Worker 		cleanup_function();
338*49cdfc7eSAndroid Build Coastguard Worker 		fail_exit();
339*49cdfc7eSAndroid Build Coastguard Worker 	}
340*49cdfc7eSAndroid Build Coastguard Worker 
341*49cdfc7eSAndroid Build Coastguard Worker 	if (ret == 999) {
342*49cdfc7eSAndroid Build Coastguard Worker 		cleanup_function();
343*49cdfc7eSAndroid Build Coastguard Worker 		fail_exit();
344*49cdfc7eSAndroid Build Coastguard Worker 	}
345*49cdfc7eSAndroid Build Coastguard Worker }
346*49cdfc7eSAndroid Build Coastguard Worker 
347*49cdfc7eSAndroid Build Coastguard Worker /*
348*49cdfc7eSAndroid Build Coastguard Worker  *    void test9A()    - tests the assertion:
349*49cdfc7eSAndroid Build Coastguard Worker  *      A call to int nftw(const char *path, int (*fn)(const char *, const
350*49cdfc7eSAndroid Build Coastguard Worker  *      struct stat *, int, struct FTW *), int depth, int flags) shall pass
351*49cdfc7eSAndroid Build Coastguard Worker  *      FTW_F as the third argument of the function fn when the object is a
352*49cdfc7eSAndroid Build Coastguard Worker  *      file
353*49cdfc7eSAndroid Build Coastguard Worker  */
354*49cdfc7eSAndroid Build Coastguard Worker 
test9A(void)355*49cdfc7eSAndroid Build Coastguard Worker void test9A(void)
356*49cdfc7eSAndroid Build Coastguard Worker {
357*49cdfc7eSAndroid Build Coastguard Worker 	int ret;
358*49cdfc7eSAndroid Build Coastguard Worker 
359*49cdfc7eSAndroid Build Coastguard Worker #ifdef DEBUG
360*49cdfc7eSAndroid Build Coastguard Worker 	fprintf(temp,
361*49cdfc7eSAndroid Build Coastguard Worker 		"TEST: nftw passes FTW_F as third arg to fn() for files\n");
362*49cdfc7eSAndroid Build Coastguard Worker #endif
363*49cdfc7eSAndroid Build Coastguard Worker 
364*49cdfc7eSAndroid Build Coastguard Worker 	if ((ret = nftw("./tmp/data/dirg", test_func9, MAX_FD, FTW_PHYS)) == -1) {
365*49cdfc7eSAndroid Build Coastguard Worker 		perror("nftw");
366*49cdfc7eSAndroid Build Coastguard Worker 		cleanup_function();
367*49cdfc7eSAndroid Build Coastguard Worker 		fail_exit();
368*49cdfc7eSAndroid Build Coastguard Worker 	}
369*49cdfc7eSAndroid Build Coastguard Worker 
370*49cdfc7eSAndroid Build Coastguard Worker 	if (ret == 999) {
371*49cdfc7eSAndroid Build Coastguard Worker 		cleanup_function();
372*49cdfc7eSAndroid Build Coastguard Worker 		fail_exit();
373*49cdfc7eSAndroid Build Coastguard Worker 	}
374*49cdfc7eSAndroid Build Coastguard Worker }
375*49cdfc7eSAndroid Build Coastguard Worker 
376*49cdfc7eSAndroid Build Coastguard Worker /*
377*49cdfc7eSAndroid Build Coastguard Worker  *    void test10A()    - tests the assertion:
378*49cdfc7eSAndroid Build Coastguard Worker  *      A call to int nftw(const char *path, int (*fn)(const char *, const
379*49cdfc7eSAndroid Build Coastguard Worker  *      struct stat *, int, struct FTW *), int depth, int flags) shall pass
380*49cdfc7eSAndroid Build Coastguard Worker  *      FTW_D as the third argument of the function fn when the object is a
381*49cdfc7eSAndroid Build Coastguard Worker  *      directory.
382*49cdfc7eSAndroid Build Coastguard Worker  */
383*49cdfc7eSAndroid Build Coastguard Worker 
test10A(void)384*49cdfc7eSAndroid Build Coastguard Worker void test10A(void)
385*49cdfc7eSAndroid Build Coastguard Worker {
386*49cdfc7eSAndroid Build Coastguard Worker 	int ret;
387*49cdfc7eSAndroid Build Coastguard Worker 
388*49cdfc7eSAndroid Build Coastguard Worker #ifdef DEBUG
389*49cdfc7eSAndroid Build Coastguard Worker 	fprintf(temp,
390*49cdfc7eSAndroid Build Coastguard Worker 		"TEST: nftw passes FTW_D as third arg to fn() when file is directory\n");
391*49cdfc7eSAndroid Build Coastguard Worker #endif
392*49cdfc7eSAndroid Build Coastguard Worker 
393*49cdfc7eSAndroid Build Coastguard Worker 	if ((ret = nftw("./tmp/data/dirg", test_func10, MAX_FD,
394*49cdfc7eSAndroid Build Coastguard Worker 			FTW_PHYS)) == -1) {
395*49cdfc7eSAndroid Build Coastguard Worker 		perror("nftw");
396*49cdfc7eSAndroid Build Coastguard Worker 		cleanup_function();
397*49cdfc7eSAndroid Build Coastguard Worker 		fail_exit();
398*49cdfc7eSAndroid Build Coastguard Worker 	}
399*49cdfc7eSAndroid Build Coastguard Worker 
400*49cdfc7eSAndroid Build Coastguard Worker 	if (ret == 999) {
401*49cdfc7eSAndroid Build Coastguard Worker 		cleanup_function();
402*49cdfc7eSAndroid Build Coastguard Worker 		fail_exit();
403*49cdfc7eSAndroid Build Coastguard Worker 	}
404*49cdfc7eSAndroid Build Coastguard Worker }
405*49cdfc7eSAndroid Build Coastguard Worker 
406*49cdfc7eSAndroid Build Coastguard Worker /*
407*49cdfc7eSAndroid Build Coastguard Worker  *    void test11A()    - tests the assertion:
408*49cdfc7eSAndroid Build Coastguard Worker  *      A call to int nftw(const char *path, int (*fn)(const char *, const
409*49cdfc7eSAndroid Build Coastguard Worker  *      struct stat *, int, struct FTW *), int depth, int flags) shall pass
410*49cdfc7eSAndroid Build Coastguard Worker  *      FTW_DP as the third argument of the function fn when the object is a
411*49cdfc7eSAndroid Build Coastguard Worker  *      directory and subdirectories have been visited.
412*49cdfc7eSAndroid Build Coastguard Worker  */
413*49cdfc7eSAndroid Build Coastguard Worker 
test11A(void)414*49cdfc7eSAndroid Build Coastguard Worker void test11A(void)
415*49cdfc7eSAndroid Build Coastguard Worker {
416*49cdfc7eSAndroid Build Coastguard Worker 	int i, ret;
417*49cdfc7eSAndroid Build Coastguard Worker 
418*49cdfc7eSAndroid Build Coastguard Worker 	for (i = 0; i < nbads; i++)
419*49cdfc7eSAndroid Build Coastguard Worker 		if (badlist[i].i == FTW_D)
420*49cdfc7eSAndroid Build Coastguard Worker 			badlist[i].i = FTW_DP;
421*49cdfc7eSAndroid Build Coastguard Worker 
422*49cdfc7eSAndroid Build Coastguard Worker #ifdef DEBUG
423*49cdfc7eSAndroid Build Coastguard Worker 	fprintf(temp,
424*49cdfc7eSAndroid Build Coastguard Worker 		"TEST: nftw passes FTW_DP when file is directory and subdirs already visited\n");
425*49cdfc7eSAndroid Build Coastguard Worker #endif
426*49cdfc7eSAndroid Build Coastguard Worker 
427*49cdfc7eSAndroid Build Coastguard Worker 	if ((ret = nftw("./tmp/data/dirg", test_func11, MAX_FD, FTW_DEPTH |
428*49cdfc7eSAndroid Build Coastguard Worker 			FTW_PHYS)) == -1) {
429*49cdfc7eSAndroid Build Coastguard Worker 		perror("nftw");
430*49cdfc7eSAndroid Build Coastguard Worker 		cleanup_function();
431*49cdfc7eSAndroid Build Coastguard Worker 		fail_exit();
432*49cdfc7eSAndroid Build Coastguard Worker 	}
433*49cdfc7eSAndroid Build Coastguard Worker 
434*49cdfc7eSAndroid Build Coastguard Worker 	if (ret == 999) {
435*49cdfc7eSAndroid Build Coastguard Worker 		cleanup_function();
436*49cdfc7eSAndroid Build Coastguard Worker 		fail_exit();
437*49cdfc7eSAndroid Build Coastguard Worker 	}
438*49cdfc7eSAndroid Build Coastguard Worker }
439*49cdfc7eSAndroid Build Coastguard Worker 
440*49cdfc7eSAndroid Build Coastguard Worker /*
441*49cdfc7eSAndroid Build Coastguard Worker  *    void test12A()    - tests the assertion:
442*49cdfc7eSAndroid Build Coastguard Worker  *      A call to int nftw(const char *path, int (*fn)(const char *, const
443*49cdfc7eSAndroid Build Coastguard Worker  *      struct stat *, int, struct FTW *), int depth, int flags) shall pass
444*49cdfc7eSAndroid Build Coastguard Worker  *      FTW_SL as the third argument of the function fn when the object is a
445*49cdfc7eSAndroid Build Coastguard Worker  *      symbolic link.
446*49cdfc7eSAndroid Build Coastguard Worker  */
447*49cdfc7eSAndroid Build Coastguard Worker 
test12A(void)448*49cdfc7eSAndroid Build Coastguard Worker void test12A(void)
449*49cdfc7eSAndroid Build Coastguard Worker {
450*49cdfc7eSAndroid Build Coastguard Worker 	int ret;
451*49cdfc7eSAndroid Build Coastguard Worker 
452*49cdfc7eSAndroid Build Coastguard Worker #ifdef DEBUG
453*49cdfc7eSAndroid Build Coastguard Worker 	fprintf(temp,
454*49cdfc7eSAndroid Build Coastguard Worker 		"TEST: nftw wth FTW_PHYS passes FTW_SL when file is symlink\n");
455*49cdfc7eSAndroid Build Coastguard Worker #endif
456*49cdfc7eSAndroid Build Coastguard Worker 
457*49cdfc7eSAndroid Build Coastguard Worker 	if ((ret = nftw("./tmp/data/dirg", test_func12, MAX_FD,
458*49cdfc7eSAndroid Build Coastguard Worker 			FTW_PHYS)) == -1) {
459*49cdfc7eSAndroid Build Coastguard Worker 		perror("nftw");
460*49cdfc7eSAndroid Build Coastguard Worker 		cleanup_function();
461*49cdfc7eSAndroid Build Coastguard Worker 		fail_exit();
462*49cdfc7eSAndroid Build Coastguard Worker 	}
463*49cdfc7eSAndroid Build Coastguard Worker 
464*49cdfc7eSAndroid Build Coastguard Worker 	if (ret == 999) {
465*49cdfc7eSAndroid Build Coastguard Worker 		cleanup_function();
466*49cdfc7eSAndroid Build Coastguard Worker 		fail_exit();
467*49cdfc7eSAndroid Build Coastguard Worker 	}
468*49cdfc7eSAndroid Build Coastguard Worker }
469*49cdfc7eSAndroid Build Coastguard Worker 
470*49cdfc7eSAndroid Build Coastguard Worker /*
471*49cdfc7eSAndroid Build Coastguard Worker  *    void test13A()    - tests the assertion:
472*49cdfc7eSAndroid Build Coastguard Worker  *      A call to int nftw(const char *path, int (*fn)(const char *, const
473*49cdfc7eSAndroid Build Coastguard Worker  *      struct stat *, int, struct FTW *), int depth, int flags) shall pass
474*49cdfc7eSAndroid Build Coastguard Worker  *      FTW_SLN as the third argument of the function fn when the object is a
475*49cdfc7eSAndroid Build Coastguard Worker  *      symbolic link that does not name an existing file.
476*49cdfc7eSAndroid Build Coastguard Worker  */
477*49cdfc7eSAndroid Build Coastguard Worker 
test13A(void)478*49cdfc7eSAndroid Build Coastguard Worker void test13A(void)
479*49cdfc7eSAndroid Build Coastguard Worker {
480*49cdfc7eSAndroid Build Coastguard Worker 	int i, ret;
481*49cdfc7eSAndroid Build Coastguard Worker 
482*49cdfc7eSAndroid Build Coastguard Worker 	if (unlink("./tmp/byebye") == -1) {
483*49cdfc7eSAndroid Build Coastguard Worker 		perror("unlink");
484*49cdfc7eSAndroid Build Coastguard Worker 		cleanup_function();
485*49cdfc7eSAndroid Build Coastguard Worker 		fail_exit();
486*49cdfc7eSAndroid Build Coastguard Worker 	}
487*49cdfc7eSAndroid Build Coastguard Worker 
488*49cdfc7eSAndroid Build Coastguard Worker 	for (i = 0; i < nbads; i++)
489*49cdfc7eSAndroid Build Coastguard Worker 		if (badlist[i].i == FTW_SL)
490*49cdfc7eSAndroid Build Coastguard Worker 			badlist[i].i = FTW_SLN;
491*49cdfc7eSAndroid Build Coastguard Worker 
492*49cdfc7eSAndroid Build Coastguard Worker #ifdef DEBUG
493*49cdfc7eSAndroid Build Coastguard Worker 	fprintf(temp, "TEST: nftw with FTW_PHYS passes FTW_SLN when file");
494*49cdfc7eSAndroid Build Coastguard Worker 	fprintf(temp, " is symlink pointing \n to non-existent file\n");
495*49cdfc7eSAndroid Build Coastguard Worker #endif
496*49cdfc7eSAndroid Build Coastguard Worker 
497*49cdfc7eSAndroid Build Coastguard Worker 	if ((ret = nftw("./tmp/data/dirg", test_func13, MAX_FD,
498*49cdfc7eSAndroid Build Coastguard Worker 			FTW_PHYS)) == -1) {
499*49cdfc7eSAndroid Build Coastguard Worker 		perror("nftw");
500*49cdfc7eSAndroid Build Coastguard Worker 		cleanup_function();
501*49cdfc7eSAndroid Build Coastguard Worker 		fail_exit();
502*49cdfc7eSAndroid Build Coastguard Worker 	}
503*49cdfc7eSAndroid Build Coastguard Worker 
504*49cdfc7eSAndroid Build Coastguard Worker 	if (ret == 999) {
505*49cdfc7eSAndroid Build Coastguard Worker 		cleanup_function();
506*49cdfc7eSAndroid Build Coastguard Worker 		fail_exit();
507*49cdfc7eSAndroid Build Coastguard Worker 	}
508*49cdfc7eSAndroid Build Coastguard Worker }
509*49cdfc7eSAndroid Build Coastguard Worker 
510*49cdfc7eSAndroid Build Coastguard Worker /*
511*49cdfc7eSAndroid Build Coastguard Worker  *    void test14A()    - tests the assertion:
512*49cdfc7eSAndroid Build Coastguard Worker  *      A call to int nftw(const char *path, int (*fn)(const char *, const
513*49cdfc7eSAndroid Build Coastguard Worker  *      struct stat *, int, struct FTW *), int depth, int flags) shall pass
514*49cdfc7eSAndroid Build Coastguard Worker  *      FTW_DNR as the third argument of the function fn when the object is a
515*49cdfc7eSAndroid Build Coastguard Worker  *      directory that cannot be read.
516*49cdfc7eSAndroid Build Coastguard Worker  */
517*49cdfc7eSAndroid Build Coastguard Worker 
test14A(void)518*49cdfc7eSAndroid Build Coastguard Worker void test14A(void)
519*49cdfc7eSAndroid Build Coastguard Worker {
520*49cdfc7eSAndroid Build Coastguard Worker 	int ret;
521*49cdfc7eSAndroid Build Coastguard Worker 
522*49cdfc7eSAndroid Build Coastguard Worker #ifdef DEBUG
523*49cdfc7eSAndroid Build Coastguard Worker 	fprintf(temp,
524*49cdfc7eSAndroid Build Coastguard Worker 		"TEST: nftw passes FTW_DNR when file is directory that cannot be read\n");
525*49cdfc7eSAndroid Build Coastguard Worker #endif
526*49cdfc7eSAndroid Build Coastguard Worker 
527*49cdfc7eSAndroid Build Coastguard Worker 	if ((ret = nftw("./tmp/data/d333", test_func14, MAX_FD, 0)) == -1) {
528*49cdfc7eSAndroid Build Coastguard Worker 		perror("nftw");
529*49cdfc7eSAndroid Build Coastguard Worker 		cleanup_function();
530*49cdfc7eSAndroid Build Coastguard Worker 		fail_exit();
531*49cdfc7eSAndroid Build Coastguard Worker 	}
532*49cdfc7eSAndroid Build Coastguard Worker 
533*49cdfc7eSAndroid Build Coastguard Worker 	if (ret == 999) {
534*49cdfc7eSAndroid Build Coastguard Worker 		cleanup_function();
535*49cdfc7eSAndroid Build Coastguard Worker 		fail_exit();
536*49cdfc7eSAndroid Build Coastguard Worker 	}
537*49cdfc7eSAndroid Build Coastguard Worker }
538*49cdfc7eSAndroid Build Coastguard Worker 
539*49cdfc7eSAndroid Build Coastguard Worker /*
540*49cdfc7eSAndroid Build Coastguard Worker  *    void test15A()    - tests the assertion:
541*49cdfc7eSAndroid Build Coastguard Worker  *      A call to int nftw(const char *path, int (*fn)(const char *, const
542*49cdfc7eSAndroid Build Coastguard Worker  *      struct stat *, int, struct FTW *), int depth, int flags) shall pass
543*49cdfc7eSAndroid Build Coastguard Worker  *      FTW_NS as the third argument of the function fn when stat() failed on
544*49cdfc7eSAndroid Build Coastguard Worker  *      the object because of lack of appropriate permission.
545*49cdfc7eSAndroid Build Coastguard Worker  */
546*49cdfc7eSAndroid Build Coastguard Worker 
test15A(void)547*49cdfc7eSAndroid Build Coastguard Worker void test15A(void)
548*49cdfc7eSAndroid Build Coastguard Worker {
549*49cdfc7eSAndroid Build Coastguard Worker 	int ret;
550*49cdfc7eSAndroid Build Coastguard Worker 
551*49cdfc7eSAndroid Build Coastguard Worker #ifdef DEBUG
552*49cdfc7eSAndroid Build Coastguard Worker 	fprintf(temp,
553*49cdfc7eSAndroid Build Coastguard Worker 		"TEST: nftw(path, fn, depth, FTW_PHYS) passes FTW_NS when dir unsearchable\n");
554*49cdfc7eSAndroid Build Coastguard Worker #endif
555*49cdfc7eSAndroid Build Coastguard Worker 
556*49cdfc7eSAndroid Build Coastguard Worker 	if ((ret =
557*49cdfc7eSAndroid Build Coastguard Worker 	     nftw("./tmp/data/d666", test_func15, MAX_FD, FTW_PHYS)) == -1) {
558*49cdfc7eSAndroid Build Coastguard Worker 		perror("nftw");
559*49cdfc7eSAndroid Build Coastguard Worker 		cleanup_function();
560*49cdfc7eSAndroid Build Coastguard Worker 		fail_exit();
561*49cdfc7eSAndroid Build Coastguard Worker 	}
562*49cdfc7eSAndroid Build Coastguard Worker 
563*49cdfc7eSAndroid Build Coastguard Worker 	if (ret == 999) {
564*49cdfc7eSAndroid Build Coastguard Worker 		cleanup_function();
565*49cdfc7eSAndroid Build Coastguard Worker 		fail_exit();
566*49cdfc7eSAndroid Build Coastguard Worker 	}
567*49cdfc7eSAndroid Build Coastguard Worker }
568*49cdfc7eSAndroid Build Coastguard Worker 
569*49cdfc7eSAndroid Build Coastguard Worker /*
570*49cdfc7eSAndroid Build Coastguard Worker  *    void test16A()    - tests the assertion:
571*49cdfc7eSAndroid Build Coastguard Worker  *      A call to int nftw(const char *path, int (*fn)(const char *, const
572*49cdfc7eSAndroid Build Coastguard Worker  *      struct stat *, int, struct FTW *), int depth, int flags) shall pass a
573*49cdfc7eSAndroid Build Coastguard Worker  *      structure which contains the offset into the pathname of the object
574*49cdfc7eSAndroid Build Coastguard Worker  *      and the depth relative to the root of the walk starting from 0 as the
575*49cdfc7eSAndroid Build Coastguard Worker  *      fourth argument of the function fn.
576*49cdfc7eSAndroid Build Coastguard Worker  */
577*49cdfc7eSAndroid Build Coastguard Worker 
test16A(void)578*49cdfc7eSAndroid Build Coastguard Worker void test16A(void)
579*49cdfc7eSAndroid Build Coastguard Worker {
580*49cdfc7eSAndroid Build Coastguard Worker 	char path[PATH_MAX + NAME_MAX];
581*49cdfc7eSAndroid Build Coastguard Worker 	char orig[PATH_MAX];
582*49cdfc7eSAndroid Build Coastguard Worker 
583*49cdfc7eSAndroid Build Coastguard Worker 	if (getcwd(orig, sizeof(orig)) == NULL) {
584*49cdfc7eSAndroid Build Coastguard Worker 		perror("getcwd on original wd");
585*49cdfc7eSAndroid Build Coastguard Worker 		cleanup_function();
586*49cdfc7eSAndroid Build Coastguard Worker 		fail_exit();
587*49cdfc7eSAndroid Build Coastguard Worker 	}
588*49cdfc7eSAndroid Build Coastguard Worker 
589*49cdfc7eSAndroid Build Coastguard Worker 	strcpy(path, orig);
590*49cdfc7eSAndroid Build Coastguard Worker 	(void)strcat(path, "/tmp/data/dirg");
591*49cdfc7eSAndroid Build Coastguard Worker 
592*49cdfc7eSAndroid Build Coastguard Worker #ifdef DEBUG
593*49cdfc7eSAndroid Build Coastguard Worker 	fprintf(temp, "TEST: nftw with absolute pathname %s\n", path);
594*49cdfc7eSAndroid Build Coastguard Worker #endif
595*49cdfc7eSAndroid Build Coastguard Worker 
596*49cdfc7eSAndroid Build Coastguard Worker 	if ((s2 = nftw(path, test_func16, MAX_FD, 0)) == -1) {
597*49cdfc7eSAndroid Build Coastguard Worker 		perror("nftw");
598*49cdfc7eSAndroid Build Coastguard Worker 		cleanup_function();
599*49cdfc7eSAndroid Build Coastguard Worker 		fail_exit();
600*49cdfc7eSAndroid Build Coastguard Worker 	}
601*49cdfc7eSAndroid Build Coastguard Worker 	if (s2 == 999) {
602*49cdfc7eSAndroid Build Coastguard Worker 		cleanup_function();
603*49cdfc7eSAndroid Build Coastguard Worker 		fail_exit();
604*49cdfc7eSAndroid Build Coastguard Worker 	}
605*49cdfc7eSAndroid Build Coastguard Worker 
606*49cdfc7eSAndroid Build Coastguard Worker 	(void)strcpy(path, "./tmp/data/dirg");
607*49cdfc7eSAndroid Build Coastguard Worker 
608*49cdfc7eSAndroid Build Coastguard Worker #ifdef DEBUG
609*49cdfc7eSAndroid Build Coastguard Worker 	fprintf(temp, "TEST: nftw with relative pathname %s\n", path);
610*49cdfc7eSAndroid Build Coastguard Worker #endif
611*49cdfc7eSAndroid Build Coastguard Worker 
612*49cdfc7eSAndroid Build Coastguard Worker 	if ((s2 = nftw(path, test_func16, MAX_FD, 0)) == -1) {
613*49cdfc7eSAndroid Build Coastguard Worker 		perror("nftw");
614*49cdfc7eSAndroid Build Coastguard Worker 		cleanup_function();
615*49cdfc7eSAndroid Build Coastguard Worker 		fail_exit();
616*49cdfc7eSAndroid Build Coastguard Worker 	}
617*49cdfc7eSAndroid Build Coastguard Worker 
618*49cdfc7eSAndroid Build Coastguard Worker 	if (s2 == 999) {
619*49cdfc7eSAndroid Build Coastguard Worker 		cleanup_function();
620*49cdfc7eSAndroid Build Coastguard Worker 		fail_exit();
621*49cdfc7eSAndroid Build Coastguard Worker 	}
622*49cdfc7eSAndroid Build Coastguard Worker }
623*49cdfc7eSAndroid Build Coastguard Worker 
624*49cdfc7eSAndroid Build Coastguard Worker /*
625*49cdfc7eSAndroid Build Coastguard Worker  *    void test17A()    - tests the assertion:
626*49cdfc7eSAndroid Build Coastguard Worker  *      A call to int nftw(const char *path, int (*fn)(const char *, const
627*49cdfc7eSAndroid Build Coastguard Worker  *      struct stat *, int, struct FTW *), int depth, int flags) shall pass
628*49cdfc7eSAndroid Build Coastguard Worker  *      FTW_SL as the third argument to the function fn if and only if the
629*49cdfc7eSAndroid Build Coastguard Worker  *      FTW_PHYS flag is included in flags.
630*49cdfc7eSAndroid Build Coastguard Worker  */
631*49cdfc7eSAndroid Build Coastguard Worker 
test17A(void)632*49cdfc7eSAndroid Build Coastguard Worker void test17A(void)
633*49cdfc7eSAndroid Build Coastguard Worker {
634*49cdfc7eSAndroid Build Coastguard Worker 	int ret;
635*49cdfc7eSAndroid Build Coastguard Worker 
636*49cdfc7eSAndroid Build Coastguard Worker 	visit = 0;
637*49cdfc7eSAndroid Build Coastguard Worker 
638*49cdfc7eSAndroid Build Coastguard Worker #ifdef DEBUG
639*49cdfc7eSAndroid Build Coastguard Worker 	fprintf(temp, "TEST: nftw with FTW_PHYS passes FTW_SL for symlink\n");
640*49cdfc7eSAndroid Build Coastguard Worker #endif
641*49cdfc7eSAndroid Build Coastguard Worker 
642*49cdfc7eSAndroid Build Coastguard Worker 	if ((ret =
643*49cdfc7eSAndroid Build Coastguard Worker 	     nftw("./tmp/data/dirl", test_func17, MAX_FD, FTW_PHYS)) == -1) {
644*49cdfc7eSAndroid Build Coastguard Worker 		perror("nftw");
645*49cdfc7eSAndroid Build Coastguard Worker 		cleanup_function();
646*49cdfc7eSAndroid Build Coastguard Worker 		fail_exit();
647*49cdfc7eSAndroid Build Coastguard Worker 	}
648*49cdfc7eSAndroid Build Coastguard Worker 	if (ret != 999) {
649*49cdfc7eSAndroid Build Coastguard Worker 		fprintf(temp, "ERROR: nftw() failed to find symbolic link\n");
650*49cdfc7eSAndroid Build Coastguard Worker 		cleanup_function();
651*49cdfc7eSAndroid Build Coastguard Worker 		fail_exit();
652*49cdfc7eSAndroid Build Coastguard Worker 	}
653*49cdfc7eSAndroid Build Coastguard Worker 
654*49cdfc7eSAndroid Build Coastguard Worker 	visit = 0;
655*49cdfc7eSAndroid Build Coastguard Worker 
656*49cdfc7eSAndroid Build Coastguard Worker #ifdef DEBUG
657*49cdfc7eSAndroid Build Coastguard Worker 	fprintf(temp,
658*49cdfc7eSAndroid Build Coastguard Worker 		"TEST: nftw without FTW_PHYS does not pass FTW_SL for symlink\n");
659*49cdfc7eSAndroid Build Coastguard Worker #endif
660*49cdfc7eSAndroid Build Coastguard Worker 
661*49cdfc7eSAndroid Build Coastguard Worker 	if ((ret = nftw("./tmp/data/dirl", test_func17, MAX_FD, 0)) == -1) {
662*49cdfc7eSAndroid Build Coastguard Worker 		perror("nftw");
663*49cdfc7eSAndroid Build Coastguard Worker 		cleanup_function();
664*49cdfc7eSAndroid Build Coastguard Worker 		fail_exit();
665*49cdfc7eSAndroid Build Coastguard Worker 	}
666*49cdfc7eSAndroid Build Coastguard Worker 	if (ret == 999) {
667*49cdfc7eSAndroid Build Coastguard Worker 		fprintf(temp, "ERROR: nftw() found symbolic link\n");
668*49cdfc7eSAndroid Build Coastguard Worker 		cleanup_function();
669*49cdfc7eSAndroid Build Coastguard Worker 		fail_exit();
670*49cdfc7eSAndroid Build Coastguard Worker 	}
671*49cdfc7eSAndroid Build Coastguard Worker }
672*49cdfc7eSAndroid Build Coastguard Worker 
673*49cdfc7eSAndroid Build Coastguard Worker /*
674*49cdfc7eSAndroid Build Coastguard Worker  *    void test18A()    - tests the assertion:
675*49cdfc7eSAndroid Build Coastguard Worker  *      A call to int nftw(const char *path, int (*fn)(const char *, const
676*49cdfc7eSAndroid Build Coastguard Worker  *      struct stat *, int, struct FTW *), int depth, int flags) shall pass
677*49cdfc7eSAndroid Build Coastguard Worker  *      FTW_SLN as the third argument to the function fn if and only if the
678*49cdfc7eSAndroid Build Coastguard Worker  *      FTW_PHYS flag is not included in flags.
679*49cdfc7eSAndroid Build Coastguard Worker  */
680*49cdfc7eSAndroid Build Coastguard Worker 
test18A(void)681*49cdfc7eSAndroid Build Coastguard Worker void test18A(void)
682*49cdfc7eSAndroid Build Coastguard Worker {
683*49cdfc7eSAndroid Build Coastguard Worker 	int ret;
684*49cdfc7eSAndroid Build Coastguard Worker 
685*49cdfc7eSAndroid Build Coastguard Worker 	unlink("./tmp/byebye");
686*49cdfc7eSAndroid Build Coastguard Worker 
687*49cdfc7eSAndroid Build Coastguard Worker 	visit = 0;
688*49cdfc7eSAndroid Build Coastguard Worker 
689*49cdfc7eSAndroid Build Coastguard Worker #ifdef DEBUG
690*49cdfc7eSAndroid Build Coastguard Worker 	fprintf(temp, "TEST: nftw with FTW_PHYS does not pass FTW_SLN\n");
691*49cdfc7eSAndroid Build Coastguard Worker #endif
692*49cdfc7eSAndroid Build Coastguard Worker 
693*49cdfc7eSAndroid Build Coastguard Worker 	if ((ret = nftw("./tmp/data/dirg", test_func18, MAX_FD,
694*49cdfc7eSAndroid Build Coastguard Worker 			FTW_PHYS)) == -1) {
695*49cdfc7eSAndroid Build Coastguard Worker 		perror("nftw");
696*49cdfc7eSAndroid Build Coastguard Worker 		cleanup_function();
697*49cdfc7eSAndroid Build Coastguard Worker 		fail_exit();
698*49cdfc7eSAndroid Build Coastguard Worker 	}
699*49cdfc7eSAndroid Build Coastguard Worker 	if (ret == 999) {
700*49cdfc7eSAndroid Build Coastguard Worker 		fprintf(temp, "ERROR: nftw() passed FTW_SLN\n");
701*49cdfc7eSAndroid Build Coastguard Worker 		cleanup_function();
702*49cdfc7eSAndroid Build Coastguard Worker 		fail_exit();
703*49cdfc7eSAndroid Build Coastguard Worker 	}
704*49cdfc7eSAndroid Build Coastguard Worker 
705*49cdfc7eSAndroid Build Coastguard Worker 	visit = 0;
706*49cdfc7eSAndroid Build Coastguard Worker 
707*49cdfc7eSAndroid Build Coastguard Worker #ifdef DEBUG
708*49cdfc7eSAndroid Build Coastguard Worker 	fprintf(temp, "TEST: nftw without FTW_PHYS passes FTW_SLN\n");
709*49cdfc7eSAndroid Build Coastguard Worker #endif
710*49cdfc7eSAndroid Build Coastguard Worker 
711*49cdfc7eSAndroid Build Coastguard Worker 	if ((ret = nftw("./tmp/data/dirg", test_func18, MAX_FD, 0)) == -1) {
712*49cdfc7eSAndroid Build Coastguard Worker 		perror("nftw");
713*49cdfc7eSAndroid Build Coastguard Worker 		cleanup_function();
714*49cdfc7eSAndroid Build Coastguard Worker 		fail_exit();
715*49cdfc7eSAndroid Build Coastguard Worker 	}
716*49cdfc7eSAndroid Build Coastguard Worker 
717*49cdfc7eSAndroid Build Coastguard Worker 	if (visit == 1) {
718*49cdfc7eSAndroid Build Coastguard Worker 		if (ret == 999) {
719*49cdfc7eSAndroid Build Coastguard Worker 			/* Test is passed */
720*49cdfc7eSAndroid Build Coastguard Worker 			return;
721*49cdfc7eSAndroid Build Coastguard Worker 		} else {
722*49cdfc7eSAndroid Build Coastguard Worker 			fprintf(temp, "ERROR: nftw passed FTW_SLN but did");
723*49cdfc7eSAndroid Build Coastguard Worker 			fprintf(temp, "not return value returned by fn()\n");
724*49cdfc7eSAndroid Build Coastguard Worker 			cleanup_function();
725*49cdfc7eSAndroid Build Coastguard Worker 			fail_exit();
726*49cdfc7eSAndroid Build Coastguard Worker 		}
727*49cdfc7eSAndroid Build Coastguard Worker 	} else {
728*49cdfc7eSAndroid Build Coastguard Worker 		fprintf(temp, "ERROR: nftw() did not pass FTW_SLN\n");
729*49cdfc7eSAndroid Build Coastguard Worker 		cleanup_function();
730*49cdfc7eSAndroid Build Coastguard Worker 		fail_exit();
731*49cdfc7eSAndroid Build Coastguard Worker 	}
732*49cdfc7eSAndroid Build Coastguard Worker }
733*49cdfc7eSAndroid Build Coastguard Worker 
734*49cdfc7eSAndroid Build Coastguard Worker /*
735*49cdfc7eSAndroid Build Coastguard Worker  *    void test19A()    - tests the assertion:
736*49cdfc7eSAndroid Build Coastguard Worker  *      On a call to int nftw(const char *path, int (*fn)(const char *, const
737*49cdfc7eSAndroid Build Coastguard Worker  *      struct stat *, int, struct FTW *), int depth, int flags) when the
738*49cdfc7eSAndroid Build Coastguard Worker  *      third argument passed to the function fn is FTW_DNR then the
739*49cdfc7eSAndroid Build Coastguard Worker  *      descendants of the directory shall not be processed.
740*49cdfc7eSAndroid Build Coastguard Worker  */
741*49cdfc7eSAndroid Build Coastguard Worker 
test19A(void)742*49cdfc7eSAndroid Build Coastguard Worker void test19A(void)
743*49cdfc7eSAndroid Build Coastguard Worker {
744*49cdfc7eSAndroid Build Coastguard Worker 	int ret_val;
745*49cdfc7eSAndroid Build Coastguard Worker 
746*49cdfc7eSAndroid Build Coastguard Worker #ifdef DEBUG
747*49cdfc7eSAndroid Build Coastguard Worker 	fprintf(temp,
748*49cdfc7eSAndroid Build Coastguard Worker 		"TEST: Can not traverse directory with no read permission\n");
749*49cdfc7eSAndroid Build Coastguard Worker #endif
750*49cdfc7eSAndroid Build Coastguard Worker 
751*49cdfc7eSAndroid Build Coastguard Worker 	visit = 0;
752*49cdfc7eSAndroid Build Coastguard Worker 
753*49cdfc7eSAndroid Build Coastguard Worker 	ret_val = nftw("./tmp/data/d333", test_func19, MAX_FD, 0);
754*49cdfc7eSAndroid Build Coastguard Worker 	if (ret_val == -1) {
755*49cdfc7eSAndroid Build Coastguard Worker 		perror("nftw");
756*49cdfc7eSAndroid Build Coastguard Worker 		cleanup_function();
757*49cdfc7eSAndroid Build Coastguard Worker 		fail_exit();
758*49cdfc7eSAndroid Build Coastguard Worker 	}
759*49cdfc7eSAndroid Build Coastguard Worker 
760*49cdfc7eSAndroid Build Coastguard Worker 	if (ret_val == 999) {
761*49cdfc7eSAndroid Build Coastguard Worker 		cleanup_function();
762*49cdfc7eSAndroid Build Coastguard Worker 		fail_exit();
763*49cdfc7eSAndroid Build Coastguard Worker 	}
764*49cdfc7eSAndroid Build Coastguard Worker #ifdef DEBUG
765*49cdfc7eSAndroid Build Coastguard Worker 	fprintf(temp, "TEST: fn only be called once\n");
766*49cdfc7eSAndroid Build Coastguard Worker #endif
767*49cdfc7eSAndroid Build Coastguard Worker 
768*49cdfc7eSAndroid Build Coastguard Worker 	if (visit != 1) {
769*49cdfc7eSAndroid Build Coastguard Worker 		fprintf(temp, "ERROR: %s",
770*49cdfc7eSAndroid Build Coastguard Worker 			"Directory without read permission allows traversing\n");
771*49cdfc7eSAndroid Build Coastguard Worker 		fprintf(temp, "       Visited %d files\n", visit);
772*49cdfc7eSAndroid Build Coastguard Worker 		cleanup_function();
773*49cdfc7eSAndroid Build Coastguard Worker 		fail_exit();
774*49cdfc7eSAndroid Build Coastguard Worker 	}
775*49cdfc7eSAndroid Build Coastguard Worker }
776*49cdfc7eSAndroid Build Coastguard Worker 
777*49cdfc7eSAndroid Build Coastguard Worker /*
778*49cdfc7eSAndroid Build Coastguard Worker  *    void test20A()    - tests the assertion:
779*49cdfc7eSAndroid Build Coastguard Worker  *      A call to int nftw(const char *path, int (*fn)(const char *, const
780*49cdfc7eSAndroid Build Coastguard Worker  *      struct stat *, int, struct FTW *), int depth, int flags) shall close
781*49cdfc7eSAndroid Build Coastguard Worker  *      any file descriptors or directory streams used to traverse the
782*49cdfc7eSAndroid Build Coastguard Worker  *      directory tree.
783*49cdfc7eSAndroid Build Coastguard Worker  */
784*49cdfc7eSAndroid Build Coastguard Worker 
test20A(void)785*49cdfc7eSAndroid Build Coastguard Worker void test20A(void)
786*49cdfc7eSAndroid Build Coastguard Worker {
787*49cdfc7eSAndroid Build Coastguard Worker 	int fd, nfd;
788*49cdfc7eSAndroid Build Coastguard Worker 
789*49cdfc7eSAndroid Build Coastguard Worker #ifdef DEBUG
790*49cdfc7eSAndroid Build Coastguard Worker 	fprintf(temp, "TEST: File descriptors used in traversal are closed\n");
791*49cdfc7eSAndroid Build Coastguard Worker #endif
792*49cdfc7eSAndroid Build Coastguard Worker 
793*49cdfc7eSAndroid Build Coastguard Worker 	if ((fd = open("./tmp/data/dirh", O_RDONLY)) == -1) {
794*49cdfc7eSAndroid Build Coastguard Worker 		perror("close");
795*49cdfc7eSAndroid Build Coastguard Worker 		cleanup_function();
796*49cdfc7eSAndroid Build Coastguard Worker 		fail_exit();
797*49cdfc7eSAndroid Build Coastguard Worker 	}
798*49cdfc7eSAndroid Build Coastguard Worker 
799*49cdfc7eSAndroid Build Coastguard Worker 	if (close(fd) == -1) {
800*49cdfc7eSAndroid Build Coastguard Worker 		perror("close");
801*49cdfc7eSAndroid Build Coastguard Worker 		cleanup_function();
802*49cdfc7eSAndroid Build Coastguard Worker 		fail_exit();
803*49cdfc7eSAndroid Build Coastguard Worker 	}
804*49cdfc7eSAndroid Build Coastguard Worker 
805*49cdfc7eSAndroid Build Coastguard Worker 	if (nftw("./tmp/data/dirh", test_func20, 1, 0) == -1) {
806*49cdfc7eSAndroid Build Coastguard Worker 		perror("nftw");
807*49cdfc7eSAndroid Build Coastguard Worker 		cleanup_function();
808*49cdfc7eSAndroid Build Coastguard Worker 		fail_exit();
809*49cdfc7eSAndroid Build Coastguard Worker 	}
810*49cdfc7eSAndroid Build Coastguard Worker 
811*49cdfc7eSAndroid Build Coastguard Worker 	if ((nfd = open("./tmp/data/dirh", O_RDONLY)) == -1) {
812*49cdfc7eSAndroid Build Coastguard Worker 		perror("open");
813*49cdfc7eSAndroid Build Coastguard Worker 		cleanup_function();
814*49cdfc7eSAndroid Build Coastguard Worker 		fail_exit();
815*49cdfc7eSAndroid Build Coastguard Worker 	}
816*49cdfc7eSAndroid Build Coastguard Worker 
817*49cdfc7eSAndroid Build Coastguard Worker 	if (nfd != fd) {
818*49cdfc7eSAndroid Build Coastguard Worker 		fprintf(temp, "ERROR: %s,fd == %d ofd = %d",
819*49cdfc7eSAndroid Build Coastguard Worker 			"nftw did not close all file descriptors used in traversal\n",
820*49cdfc7eSAndroid Build Coastguard Worker 			nfd, fd);
821*49cdfc7eSAndroid Build Coastguard Worker 		cleanup_function();
822*49cdfc7eSAndroid Build Coastguard Worker 		fail_exit();
823*49cdfc7eSAndroid Build Coastguard Worker 	}
824*49cdfc7eSAndroid Build Coastguard Worker 
825*49cdfc7eSAndroid Build Coastguard Worker 	if (close(nfd) == -1) {
826*49cdfc7eSAndroid Build Coastguard Worker 		perror("close");
827*49cdfc7eSAndroid Build Coastguard Worker 		cleanup_function();
828*49cdfc7eSAndroid Build Coastguard Worker 		fail_exit();
829*49cdfc7eSAndroid Build Coastguard Worker 	}
830*49cdfc7eSAndroid Build Coastguard Worker }
831*49cdfc7eSAndroid Build Coastguard Worker 
832*49cdfc7eSAndroid Build Coastguard Worker /*
833*49cdfc7eSAndroid Build Coastguard Worker  *    void test21A()    - tests the assertion:
834*49cdfc7eSAndroid Build Coastguard Worker  *      On a call to int nftw(const char *path, int (*fn)(const char *, const
835*49cdfc7eSAndroid Build Coastguard Worker  *      struct stat *, int, struct FTW *), int depth, int flags) shall
836*49cdfc7eSAndroid Build Coastguard Worker  *      be the maximum number of file descriptors used for the search.
837*49cdfc7eSAndroid Build Coastguard Worker  */
838*49cdfc7eSAndroid Build Coastguard Worker 
test21A(void)839*49cdfc7eSAndroid Build Coastguard Worker void test21A(void)
840*49cdfc7eSAndroid Build Coastguard Worker {
841*49cdfc7eSAndroid Build Coastguard Worker 	char path[] = "./tmp/data/dirh";
842*49cdfc7eSAndroid Build Coastguard Worker 	int ret_val;
843*49cdfc7eSAndroid Build Coastguard Worker 
844*49cdfc7eSAndroid Build Coastguard Worker #ifdef DEBUG
845*49cdfc7eSAndroid Build Coastguard Worker 	fprintf(temp,
846*49cdfc7eSAndroid Build Coastguard Worker 		"TEST: No more than depth file descriptors used in traversal\n");
847*49cdfc7eSAndroid Build Coastguard Worker #endif
848*49cdfc7eSAndroid Build Coastguard Worker 
849*49cdfc7eSAndroid Build Coastguard Worker 	/*this is the fd we expect if 0 are used */
850*49cdfc7eSAndroid Build Coastguard Worker 	if ((next_fd[0] = open(path, O_RDONLY)) == -1) {
851*49cdfc7eSAndroid Build Coastguard Worker 		perror("open next_fd[0]");
852*49cdfc7eSAndroid Build Coastguard Worker 		cleanup_function();
853*49cdfc7eSAndroid Build Coastguard Worker 		fail_exit();
854*49cdfc7eSAndroid Build Coastguard Worker 	}
855*49cdfc7eSAndroid Build Coastguard Worker 
856*49cdfc7eSAndroid Build Coastguard Worker 	/*this is the fd we expect if 1 is used */
857*49cdfc7eSAndroid Build Coastguard Worker 	if ((next_fd[1] = open(path, O_RDONLY)) == -1) {
858*49cdfc7eSAndroid Build Coastguard Worker 		perror("open next_fd[1]");
859*49cdfc7eSAndroid Build Coastguard Worker 		cleanup_function();
860*49cdfc7eSAndroid Build Coastguard Worker 		fail_exit();
861*49cdfc7eSAndroid Build Coastguard Worker 	}
862*49cdfc7eSAndroid Build Coastguard Worker 
863*49cdfc7eSAndroid Build Coastguard Worker 	if (close(next_fd[0]) == -1) {
864*49cdfc7eSAndroid Build Coastguard Worker 		perror("close next_fd[0]");
865*49cdfc7eSAndroid Build Coastguard Worker 		cleanup_function();
866*49cdfc7eSAndroid Build Coastguard Worker 		fail_exit();
867*49cdfc7eSAndroid Build Coastguard Worker 	}
868*49cdfc7eSAndroid Build Coastguard Worker 
869*49cdfc7eSAndroid Build Coastguard Worker 	if (close(next_fd[1]) == -1) {
870*49cdfc7eSAndroid Build Coastguard Worker 		perror("close next_fd[1]");
871*49cdfc7eSAndroid Build Coastguard Worker 		cleanup_function();
872*49cdfc7eSAndroid Build Coastguard Worker 		fail_exit();
873*49cdfc7eSAndroid Build Coastguard Worker 	}
874*49cdfc7eSAndroid Build Coastguard Worker 
875*49cdfc7eSAndroid Build Coastguard Worker 	visit = 0;
876*49cdfc7eSAndroid Build Coastguard Worker 	ret_val = nftw(path, test_func21, 1, 0);
877*49cdfc7eSAndroid Build Coastguard Worker 	if (ret_val == -1) {
878*49cdfc7eSAndroid Build Coastguard Worker 		perror("nftw");
879*49cdfc7eSAndroid Build Coastguard Worker 		cleanup_function();
880*49cdfc7eSAndroid Build Coastguard Worker 		fail_exit();
881*49cdfc7eSAndroid Build Coastguard Worker 	}
882*49cdfc7eSAndroid Build Coastguard Worker 
883*49cdfc7eSAndroid Build Coastguard Worker 	if (ret_val == 999) {
884*49cdfc7eSAndroid Build Coastguard Worker 		cleanup_function();
885*49cdfc7eSAndroid Build Coastguard Worker 		fail_exit();
886*49cdfc7eSAndroid Build Coastguard Worker 	}
887*49cdfc7eSAndroid Build Coastguard Worker }
888*49cdfc7eSAndroid Build Coastguard Worker 
889*49cdfc7eSAndroid Build Coastguard Worker /*
890*49cdfc7eSAndroid Build Coastguard Worker  *    void test22A()    - tests the assertion:
891*49cdfc7eSAndroid Build Coastguard Worker  *      A call to int nftw(const char *path, int (*fn)(const char *, const
892*49cdfc7eSAndroid Build Coastguard Worker  *      struct stat *, int, struct FTW *), int depth, int flags) shall use at
893*49cdfc7eSAndroid Build Coastguard Worker  *      most one file descriptor for each directory level.
894*49cdfc7eSAndroid Build Coastguard Worker  */
895*49cdfc7eSAndroid Build Coastguard Worker 
test22A(void)896*49cdfc7eSAndroid Build Coastguard Worker void test22A(void)
897*49cdfc7eSAndroid Build Coastguard Worker {
898*49cdfc7eSAndroid Build Coastguard Worker 	char path[] = "./tmp/data/dirh";
899*49cdfc7eSAndroid Build Coastguard Worker 	int ret_val, i;
900*49cdfc7eSAndroid Build Coastguard Worker 
901*49cdfc7eSAndroid Build Coastguard Worker 	for (i = 0; i < 4; i++) {
902*49cdfc7eSAndroid Build Coastguard Worker 		if ((next_fd[i] = open(path, O_RDONLY)) == -1) {
903*49cdfc7eSAndroid Build Coastguard Worker 			perror("open");
904*49cdfc7eSAndroid Build Coastguard Worker 			cleanup_function();
905*49cdfc7eSAndroid Build Coastguard Worker 			fail_exit();
906*49cdfc7eSAndroid Build Coastguard Worker 		}
907*49cdfc7eSAndroid Build Coastguard Worker 	}
908*49cdfc7eSAndroid Build Coastguard Worker 
909*49cdfc7eSAndroid Build Coastguard Worker 	for (i = 0; i < 4; i++) {
910*49cdfc7eSAndroid Build Coastguard Worker 		if (close(next_fd[i]) == -1) {
911*49cdfc7eSAndroid Build Coastguard Worker 			perror("close");
912*49cdfc7eSAndroid Build Coastguard Worker 			cleanup_function();
913*49cdfc7eSAndroid Build Coastguard Worker 			fail_exit();
914*49cdfc7eSAndroid Build Coastguard Worker 		}
915*49cdfc7eSAndroid Build Coastguard Worker 	}
916*49cdfc7eSAndroid Build Coastguard Worker 
917*49cdfc7eSAndroid Build Coastguard Worker 	visit = 0;
918*49cdfc7eSAndroid Build Coastguard Worker 
919*49cdfc7eSAndroid Build Coastguard Worker #ifdef DEBUG
920*49cdfc7eSAndroid Build Coastguard Worker 	fprintf(temp,
921*49cdfc7eSAndroid Build Coastguard Worker 		"TEST: No more than 1 fd per level is used in traversal\n");
922*49cdfc7eSAndroid Build Coastguard Worker #endif
923*49cdfc7eSAndroid Build Coastguard Worker 
924*49cdfc7eSAndroid Build Coastguard Worker 	ret_val = nftw(path, test_func22, MAX_FD, 0);
925*49cdfc7eSAndroid Build Coastguard Worker 
926*49cdfc7eSAndroid Build Coastguard Worker 	if (ret_val == -1) {
927*49cdfc7eSAndroid Build Coastguard Worker 		perror("nftw");
928*49cdfc7eSAndroid Build Coastguard Worker 		cleanup_function();
929*49cdfc7eSAndroid Build Coastguard Worker 		fail_exit();
930*49cdfc7eSAndroid Build Coastguard Worker 	}
931*49cdfc7eSAndroid Build Coastguard Worker 
932*49cdfc7eSAndroid Build Coastguard Worker 	if (ret_val == 999) {
933*49cdfc7eSAndroid Build Coastguard Worker 		cleanup_function();
934*49cdfc7eSAndroid Build Coastguard Worker 		fail_exit();
935*49cdfc7eSAndroid Build Coastguard Worker 	}
936*49cdfc7eSAndroid Build Coastguard Worker }
937*49cdfc7eSAndroid Build Coastguard Worker 
938*49cdfc7eSAndroid Build Coastguard Worker /*
939*49cdfc7eSAndroid Build Coastguard Worker  *    void test23A()    - tests the assertion:
940*49cdfc7eSAndroid Build Coastguard Worker  *      A call to int nftw(const char *path, int (*fn)(const char *, const
941*49cdfc7eSAndroid Build Coastguard Worker  *      struct stat *, int, struct FTW *), int depth, int flags) when the
942*49cdfc7eSAndroid Build Coastguard Worker  *      function fn returns a non-zero value shall stop and return the value
943*49cdfc7eSAndroid Build Coastguard Worker  *      returned by fn.
944*49cdfc7eSAndroid Build Coastguard Worker  */
945*49cdfc7eSAndroid Build Coastguard Worker 
test23A(void)946*49cdfc7eSAndroid Build Coastguard Worker void test23A(void)
947*49cdfc7eSAndroid Build Coastguard Worker {
948*49cdfc7eSAndroid Build Coastguard Worker 	int ret;
949*49cdfc7eSAndroid Build Coastguard Worker 
950*49cdfc7eSAndroid Build Coastguard Worker 	visit = 0;
951*49cdfc7eSAndroid Build Coastguard Worker 
952*49cdfc7eSAndroid Build Coastguard Worker #ifdef DEBUG
953*49cdfc7eSAndroid Build Coastguard Worker 	fprintf(temp,
954*49cdfc7eSAndroid Build Coastguard Worker 		"TEST: The function nftw should return with value set by fn\n");
955*49cdfc7eSAndroid Build Coastguard Worker #endif
956*49cdfc7eSAndroid Build Coastguard Worker 
957*49cdfc7eSAndroid Build Coastguard Worker 	if ((ret =
958*49cdfc7eSAndroid Build Coastguard Worker 	     nftw("./tmp/data/dirh", test_func23, MAX_FD, FTW_PHYS)) == -1) {
959*49cdfc7eSAndroid Build Coastguard Worker 		perror("nftw");
960*49cdfc7eSAndroid Build Coastguard Worker 		cleanup_function();
961*49cdfc7eSAndroid Build Coastguard Worker 		fail_exit();
962*49cdfc7eSAndroid Build Coastguard Worker 	}
963*49cdfc7eSAndroid Build Coastguard Worker 
964*49cdfc7eSAndroid Build Coastguard Worker 	if (ret != 999) {
965*49cdfc7eSAndroid Build Coastguard Worker 		fprintf(temp,
966*49cdfc7eSAndroid Build Coastguard Worker 			"ERROR: nftw did not return value returned by fn()\n");
967*49cdfc7eSAndroid Build Coastguard Worker 		cleanup_function();
968*49cdfc7eSAndroid Build Coastguard Worker 		fail_exit();
969*49cdfc7eSAndroid Build Coastguard Worker 	}
970*49cdfc7eSAndroid Build Coastguard Worker 	if (visit != 4) {
971*49cdfc7eSAndroid Build Coastguard Worker 		fprintf(temp,
972*49cdfc7eSAndroid Build Coastguard Worker 			"ERROR: nftw() did not return immediately on non-zero fn() return\n");
973*49cdfc7eSAndroid Build Coastguard Worker 		cleanup_function();
974*49cdfc7eSAndroid Build Coastguard Worker 		fail_exit();
975*49cdfc7eSAndroid Build Coastguard Worker 	}
976*49cdfc7eSAndroid Build Coastguard Worker }
977*49cdfc7eSAndroid Build Coastguard Worker 
978*49cdfc7eSAndroid Build Coastguard Worker /*
979*49cdfc7eSAndroid Build Coastguard Worker  *    void test24A()    - tests the assertion:
980*49cdfc7eSAndroid Build Coastguard Worker  *      ENAMETOOLONG in errno and return -1 on a call to int nftw(const char
981*49cdfc7eSAndroid Build Coastguard Worker  *      *path, int (*fn)(const char *, const struct stat *, int, struct FTW
982*49cdfc7eSAndroid Build Coastguard Worker  *      *), int depth, int flags) when the length of path exceeds PATH_MAX.
983*49cdfc7eSAndroid Build Coastguard Worker  */
984*49cdfc7eSAndroid Build Coastguard Worker 
test24A(void)985*49cdfc7eSAndroid Build Coastguard Worker void test24A(void)
986*49cdfc7eSAndroid Build Coastguard Worker {
987*49cdfc7eSAndroid Build Coastguard Worker 	test_ENAMETOOLONG_path("nftw", callback, -1);
988*49cdfc7eSAndroid Build Coastguard Worker }
989*49cdfc7eSAndroid Build Coastguard Worker 
990*49cdfc7eSAndroid Build Coastguard Worker /*
991*49cdfc7eSAndroid Build Coastguard Worker  *    void test25A()    - tests the assertion:
992*49cdfc7eSAndroid Build Coastguard Worker  *      ENAMETOOLONG in errno and return -1 on a call to int nftw(const char
993*49cdfc7eSAndroid Build Coastguard Worker  *      *path, int (*fn)(const char *, const struct stat *, int, struct FTW
994*49cdfc7eSAndroid Build Coastguard Worker  *      *), int depth, int flags) when a component of path exceeds NAME_MAX.
995*49cdfc7eSAndroid Build Coastguard Worker  */
996*49cdfc7eSAndroid Build Coastguard Worker 
test25A(void)997*49cdfc7eSAndroid Build Coastguard Worker void test25A(void)
998*49cdfc7eSAndroid Build Coastguard Worker {
999*49cdfc7eSAndroid Build Coastguard Worker 	test_ENAMETOOLONG_name("nftw", callback, -1);
1000*49cdfc7eSAndroid Build Coastguard Worker }
1001*49cdfc7eSAndroid Build Coastguard Worker 
1002*49cdfc7eSAndroid Build Coastguard Worker /*
1003*49cdfc7eSAndroid Build Coastguard Worker  *    void test26A()    - tests the assertion:
1004*49cdfc7eSAndroid Build Coastguard Worker  *      ENOENT in errno and return -1 on a call to int nftw(const char *path,
1005*49cdfc7eSAndroid Build Coastguard Worker  *      int (*fn)(const char *, const struct stat *, int, struct FTW *), int
1006*49cdfc7eSAndroid Build Coastguard Worker  *      depth, int flags) when path points to a file which does not exist.
1007*49cdfc7eSAndroid Build Coastguard Worker  */
1008*49cdfc7eSAndroid Build Coastguard Worker 
test26A(void)1009*49cdfc7eSAndroid Build Coastguard Worker void test26A(void)
1010*49cdfc7eSAndroid Build Coastguard Worker {
1011*49cdfc7eSAndroid Build Coastguard Worker #ifdef DEBUG
1012*49cdfc7eSAndroid Build Coastguard Worker 	fprintf(temp, "TEST: [ENOENT] && -1 returned by nftw\n");
1013*49cdfc7eSAndroid Build Coastguard Worker #endif
1014*49cdfc7eSAndroid Build Coastguard Worker 
1015*49cdfc7eSAndroid Build Coastguard Worker 	test_ENOENT_nofile("nftw", callback, -1);
1016*49cdfc7eSAndroid Build Coastguard Worker }
1017*49cdfc7eSAndroid Build Coastguard Worker 
1018*49cdfc7eSAndroid Build Coastguard Worker /*
1019*49cdfc7eSAndroid Build Coastguard Worker  *    void test27A()    - tests the assertion:
1020*49cdfc7eSAndroid Build Coastguard Worker  *      ENOENT in errno and return -1 on a call to int nftw(const char *path,
1021*49cdfc7eSAndroid Build Coastguard Worker  *      int (*fn)(const char *, const struct stat *, int, struct FTW *), int
1022*49cdfc7eSAndroid Build Coastguard Worker  *      depth, int flags) when path points to an empty string.
1023*49cdfc7eSAndroid Build Coastguard Worker  */
1024*49cdfc7eSAndroid Build Coastguard Worker 
test27A(void)1025*49cdfc7eSAndroid Build Coastguard Worker void test27A(void)
1026*49cdfc7eSAndroid Build Coastguard Worker {
1027*49cdfc7eSAndroid Build Coastguard Worker #ifdef DEBUG
1028*49cdfc7eSAndroid Build Coastguard Worker 	fprintf(temp, "TEST: The function nftw should return with a -1\n");
1029*49cdfc7eSAndroid Build Coastguard Worker #endif
1030*49cdfc7eSAndroid Build Coastguard Worker 
1031*49cdfc7eSAndroid Build Coastguard Worker 	test_ENOENT_empty("nftw", callback, -1);
1032*49cdfc7eSAndroid Build Coastguard Worker }
1033*49cdfc7eSAndroid Build Coastguard Worker 
1034*49cdfc7eSAndroid Build Coastguard Worker /*
1035*49cdfc7eSAndroid Build Coastguard Worker  *    void test28A()    - tests the assertion:
1036*49cdfc7eSAndroid Build Coastguard Worker  *      ENOTDIR in errno and return -1 on a call to int nftw(const char
1037*49cdfc7eSAndroid Build Coastguard Worker  *      *path, int (*fn)(const char *, const struct stat *, int, struct FTW
1038*49cdfc7eSAndroid Build Coastguard Worker  *      *), int depth, int flags) when path is not a directory.
1039*49cdfc7eSAndroid Build Coastguard Worker  */
1040*49cdfc7eSAndroid Build Coastguard Worker 
test28A(void)1041*49cdfc7eSAndroid Build Coastguard Worker void test28A(void)
1042*49cdfc7eSAndroid Build Coastguard Worker {
1043*49cdfc7eSAndroid Build Coastguard Worker #ifdef DEBUG
1044*49cdfc7eSAndroid Build Coastguard Worker 	fprintf(temp, "TEST: [ENOTDIR] && -1 returned by nftw\n");
1045*49cdfc7eSAndroid Build Coastguard Worker #endif
1046*49cdfc7eSAndroid Build Coastguard Worker 
1047*49cdfc7eSAndroid Build Coastguard Worker 	test_ENOTDIR("nftw", callback, -1);
1048*49cdfc7eSAndroid Build Coastguard Worker }
1049*49cdfc7eSAndroid Build Coastguard Worker 
1050*49cdfc7eSAndroid Build Coastguard Worker /*
1051*49cdfc7eSAndroid Build Coastguard Worker  *    void test29A()    - tests the assertion:
1052*49cdfc7eSAndroid Build Coastguard Worker  *      EACCES in errno and return -1 on a call to int nftw(const char *path,
1053*49cdfc7eSAndroid Build Coastguard Worker  *      int (*fn)(const char *, const struct stat *, int, struct FTW *), int
1054*49cdfc7eSAndroid Build Coastguard Worker  *      depth, int flags) when search permission is denied for any component
1055*49cdfc7eSAndroid Build Coastguard Worker  *      of path.
1056*49cdfc7eSAndroid Build Coastguard Worker  */
1057*49cdfc7eSAndroid Build Coastguard Worker 
test29A(void)1058*49cdfc7eSAndroid Build Coastguard Worker void test29A(void)
1059*49cdfc7eSAndroid Build Coastguard Worker {
1060*49cdfc7eSAndroid Build Coastguard Worker 	if (chmod("./tmp/data/d333", (mode_t) S_IRUSR) == -1) {
1061*49cdfc7eSAndroid Build Coastguard Worker 		perror("chmod");
1062*49cdfc7eSAndroid Build Coastguard Worker 		cleanup_function();
1063*49cdfc7eSAndroid Build Coastguard Worker 		fail_exit();
1064*49cdfc7eSAndroid Build Coastguard Worker 	}
1065*49cdfc7eSAndroid Build Coastguard Worker #ifdef DEBUG
1066*49cdfc7eSAndroid Build Coastguard Worker 	fprintf(temp, "TEST: [EACCES] && -1 returned by nftw\n");
1067*49cdfc7eSAndroid Build Coastguard Worker #endif
1068*49cdfc7eSAndroid Build Coastguard Worker 
1069*49cdfc7eSAndroid Build Coastguard Worker 	test_ENOTDIR("nftw", callback, -1);
1070*49cdfc7eSAndroid Build Coastguard Worker }
1071*49cdfc7eSAndroid Build Coastguard Worker 
1072*49cdfc7eSAndroid Build Coastguard Worker /*
1073*49cdfc7eSAndroid Build Coastguard Worker  *    void test30A()    - tests the assertion:
1074*49cdfc7eSAndroid Build Coastguard Worker  *      EACCES in errno and return -1 on a call to int nftw(const char *path,
1075*49cdfc7eSAndroid Build Coastguard Worker  *      int (*fn)(const char *, const struct stat *, int, struct FTW *), int
1076*49cdfc7eSAndroid Build Coastguard Worker  *      depth, int flags) when read permission is denied for path.
1077*49cdfc7eSAndroid Build Coastguard Worker  */
1078*49cdfc7eSAndroid Build Coastguard Worker 
test30A(void)1079*49cdfc7eSAndroid Build Coastguard Worker void test30A(void)
1080*49cdfc7eSAndroid Build Coastguard Worker {
1081*49cdfc7eSAndroid Build Coastguard Worker 	if (chmod("./tmp/data/d333", (mode_t) S_IXUSR) == -1) {
1082*49cdfc7eSAndroid Build Coastguard Worker 		perror("chmod");
1083*49cdfc7eSAndroid Build Coastguard Worker 		cleanup_function();
1084*49cdfc7eSAndroid Build Coastguard Worker 		fail_exit();
1085*49cdfc7eSAndroid Build Coastguard Worker 	}
1086*49cdfc7eSAndroid Build Coastguard Worker #ifdef DEBUG
1087*49cdfc7eSAndroid Build Coastguard Worker 	fprintf(temp, "TEST: [EACCES] && -1 returned by nftw\n");
1088*49cdfc7eSAndroid Build Coastguard Worker #endif
1089*49cdfc7eSAndroid Build Coastguard Worker 	test_ENOTDIR("nftw", callback, -1);
1090*49cdfc7eSAndroid Build Coastguard Worker }
1091