xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/nftw/lib.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  *      lib.c - This file contains code for common failure conditions
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 s2;
31*49cdfc7eSAndroid Build Coastguard Worker 
32*49cdfc7eSAndroid Build Coastguard Worker static char *tmp_path = "./tmp";
33*49cdfc7eSAndroid Build Coastguard Worker static const char *no_file = "./tmp/no_such_file";
34*49cdfc7eSAndroid Build Coastguard Worker static const char *is_a_file = "./tmp/is_a_file";
35*49cdfc7eSAndroid Build Coastguard Worker 
36*49cdfc7eSAndroid Build Coastguard Worker extern FILE *temp;
37*49cdfc7eSAndroid Build Coastguard Worker /*
38*49cdfc7eSAndroid Build Coastguard Worker  * Cleanup the ./tmp
39*49cdfc7eSAndroid Build Coastguard Worker  */
remove_test_ENOTDIR_files(void)40*49cdfc7eSAndroid Build Coastguard Worker void remove_test_ENOTDIR_files(void)
41*49cdfc7eSAndroid Build Coastguard Worker {
42*49cdfc7eSAndroid Build Coastguard Worker 	(void)unlink(is_a_file);
43*49cdfc7eSAndroid Build Coastguard Worker }
44*49cdfc7eSAndroid Build Coastguard Worker 
45*49cdfc7eSAndroid Build Coastguard Worker /*
46*49cdfc7eSAndroid Build Coastguard Worker  * Cleanup the ./tmp
47*49cdfc7eSAndroid Build Coastguard Worker  */
remove_test_ENOENT_files(void)48*49cdfc7eSAndroid Build Coastguard Worker void remove_test_ENOENT_files(void)
49*49cdfc7eSAndroid Build Coastguard Worker {
50*49cdfc7eSAndroid Build Coastguard Worker 	(void)unlink(no_file);
51*49cdfc7eSAndroid Build Coastguard Worker }
52*49cdfc7eSAndroid Build Coastguard Worker 
53*49cdfc7eSAndroid Build Coastguard Worker /*
54*49cdfc7eSAndroid Build Coastguard Worker  *  get_long_name_buffer()
55*49cdfc7eSAndroid Build Coastguard Worker  * 	Create a path containing a component of length pathconf(NAME_MAX) + 1
56*49cdfc7eSAndroid Build Coastguard Worker  *      characters.
57*49cdfc7eSAndroid Build Coastguard Worker  */
58*49cdfc7eSAndroid Build Coastguard Worker 
get_long_name_buffer(size_t * length,size_t extra)59*49cdfc7eSAndroid Build Coastguard Worker static char *get_long_name_buffer(size_t * length, size_t extra)
60*49cdfc7eSAndroid Build Coastguard Worker {
61*49cdfc7eSAndroid Build Coastguard Worker 	char *buffer;
62*49cdfc7eSAndroid Build Coastguard Worker 	size_t path_length, name_length;
63*49cdfc7eSAndroid Build Coastguard Worker 
64*49cdfc7eSAndroid Build Coastguard Worker 	temp = stderr;
65*49cdfc7eSAndroid Build Coastguard Worker 	if ((path_length = pathconf(tmp_path, _PC_PATH_MAX)) == -1) {
66*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TFAIL, "pathconf(_PC_PATH_MAX) failed: %s",
67*49cdfc7eSAndroid Build Coastguard Worker 			 strerror(errno));
68*49cdfc7eSAndroid Build Coastguard Worker 		cleanup_function();
69*49cdfc7eSAndroid Build Coastguard Worker 		fail_exit();
70*49cdfc7eSAndroid Build Coastguard Worker 	}
71*49cdfc7eSAndroid Build Coastguard Worker 
72*49cdfc7eSAndroid Build Coastguard Worker 	if ((name_length = pathconf(tmp_path, _PC_NAME_MAX)) == -1) {
73*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TFAIL, "pathconf(_PC_NAME_MAX) failed: %s",
74*49cdfc7eSAndroid Build Coastguard Worker 			 strerror(errno));
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 	if ((strlen(tmp_path) + name_length + extra) > path_length) {
80*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TFAIL,
81*49cdfc7eSAndroid Build Coastguard Worker 			 "pathconf(_PC_NAME_MAX)[=%zi] too large relative to pathconf(_PC_PATH_MAX)[=%zi]",
82*49cdfc7eSAndroid Build Coastguard Worker 			 name_length, path_length);
83*49cdfc7eSAndroid Build Coastguard Worker 		cleanup_function();
84*49cdfc7eSAndroid Build Coastguard Worker 		fail_exit();
85*49cdfc7eSAndroid Build Coastguard Worker 	}
86*49cdfc7eSAndroid Build Coastguard Worker 
87*49cdfc7eSAndroid Build Coastguard Worker 	if ((buffer = malloc(path_length + extra)) == NULL) {
88*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TFAIL, "malloc(%zi) failed: %s", path_length + extra,
89*49cdfc7eSAndroid Build Coastguard Worker 			 strerror(errno));
90*49cdfc7eSAndroid Build Coastguard Worker 		cleanup_function();
91*49cdfc7eSAndroid Build Coastguard Worker 		fail_exit();
92*49cdfc7eSAndroid Build Coastguard Worker 	}
93*49cdfc7eSAndroid Build Coastguard Worker 	*length = name_length;
94*49cdfc7eSAndroid Build Coastguard Worker 	return buffer;
95*49cdfc7eSAndroid Build Coastguard Worker }
96*49cdfc7eSAndroid Build Coastguard Worker 
97*49cdfc7eSAndroid Build Coastguard Worker static void
execute_function(char * name,int (* callback)(const char *),char * buffer,int expected)98*49cdfc7eSAndroid Build Coastguard Worker execute_function(char *name, int (*callback) (const char *), char *buffer,
99*49cdfc7eSAndroid Build Coastguard Worker 		 int expected)
100*49cdfc7eSAndroid Build Coastguard Worker {
101*49cdfc7eSAndroid Build Coastguard Worker 	int result;
102*49cdfc7eSAndroid Build Coastguard Worker 	temp = stderr;
103*49cdfc7eSAndroid Build Coastguard Worker 
104*49cdfc7eSAndroid Build Coastguard Worker #ifdef DEBUG
105*49cdfc7eSAndroid Build Coastguard Worker 	fprintf(temp, "TEST: %s fails with ENAMETOOLONG\n", name);
106*49cdfc7eSAndroid Build Coastguard Worker #endif
107*49cdfc7eSAndroid Build Coastguard Worker 
108*49cdfc7eSAndroid Build Coastguard Worker 	errno = 0;
109*49cdfc7eSAndroid Build Coastguard Worker 	result = (*callback) (buffer);
110*49cdfc7eSAndroid Build Coastguard Worker 
111*49cdfc7eSAndroid Build Coastguard Worker 	/*callback found an error, fail */
112*49cdfc7eSAndroid Build Coastguard Worker 	if (result == -752) {
113*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TFAIL, "%s callback did not work correctly", name);
114*49cdfc7eSAndroid Build Coastguard Worker 		cleanup_function();
115*49cdfc7eSAndroid Build Coastguard Worker 		fail_exit();
116*49cdfc7eSAndroid Build Coastguard Worker 	}
117*49cdfc7eSAndroid Build Coastguard Worker 	if (result != expected) {
118*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TFAIL,
119*49cdfc7eSAndroid Build Coastguard Worker 			 "%s did not return value as expected; Expected=%d Received=%d",
120*49cdfc7eSAndroid Build Coastguard Worker 			 name, expected, result);
121*49cdfc7eSAndroid Build Coastguard Worker 		cleanup_function();
122*49cdfc7eSAndroid Build Coastguard Worker 		fail_exit();
123*49cdfc7eSAndroid Build Coastguard Worker 	}
124*49cdfc7eSAndroid Build Coastguard Worker 	if (errno != ENAMETOOLONG) {
125*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TFAIL, "%s failed: errno should be %i but is %i", name,
126*49cdfc7eSAndroid Build Coastguard Worker 			 ENAMETOOLONG, errno);
127*49cdfc7eSAndroid Build Coastguard Worker 		cleanup_function();
128*49cdfc7eSAndroid Build Coastguard Worker 		fail_exit();
129*49cdfc7eSAndroid Build Coastguard Worker 	}
130*49cdfc7eSAndroid Build Coastguard Worker }
131*49cdfc7eSAndroid Build Coastguard Worker 
132*49cdfc7eSAndroid Build Coastguard Worker static void
test_long_file_name(char * name,int (* callback)(const char *),int expected)133*49cdfc7eSAndroid Build Coastguard Worker test_long_file_name(char *name, int (*callback) (const char *), int expected)
134*49cdfc7eSAndroid Build Coastguard Worker {
135*49cdfc7eSAndroid Build Coastguard Worker 	char *ptr, *ptr_end, *buffer;
136*49cdfc7eSAndroid Build Coastguard Worker 	size_t name_length;
137*49cdfc7eSAndroid Build Coastguard Worker 
138*49cdfc7eSAndroid Build Coastguard Worker 	buffer = get_long_name_buffer(&name_length, 1);
139*49cdfc7eSAndroid Build Coastguard Worker 
140*49cdfc7eSAndroid Build Coastguard Worker 	strcpy(buffer, tmp_path);
141*49cdfc7eSAndroid Build Coastguard Worker 	ptr = buffer + strlen(buffer);
142*49cdfc7eSAndroid Build Coastguard Worker 	ptr_end = ptr + name_length + 1;
143*49cdfc7eSAndroid Build Coastguard Worker 
144*49cdfc7eSAndroid Build Coastguard Worker 	*(ptr++) = '/';
145*49cdfc7eSAndroid Build Coastguard Worker 	while (ptr <= ptr_end)
146*49cdfc7eSAndroid Build Coastguard Worker 		*(ptr++) = 'E';
147*49cdfc7eSAndroid Build Coastguard Worker 	*ptr = '\000';
148*49cdfc7eSAndroid Build Coastguard Worker 
149*49cdfc7eSAndroid Build Coastguard Worker 	execute_function(name, callback, buffer, expected);
150*49cdfc7eSAndroid Build Coastguard Worker 	free(buffer);
151*49cdfc7eSAndroid Build Coastguard Worker }
152*49cdfc7eSAndroid Build Coastguard Worker 
153*49cdfc7eSAndroid Build Coastguard Worker static void
test_long_component_name(char * name,int (* callback)(const char *),int expected)154*49cdfc7eSAndroid Build Coastguard Worker test_long_component_name(char *name, int (*callback) (const char *),
155*49cdfc7eSAndroid Build Coastguard Worker 			 int expected)
156*49cdfc7eSAndroid Build Coastguard Worker {
157*49cdfc7eSAndroid Build Coastguard Worker 	char *ptr, *ptr_end, *buffer;
158*49cdfc7eSAndroid Build Coastguard Worker 	size_t name_length;
159*49cdfc7eSAndroid Build Coastguard Worker 
160*49cdfc7eSAndroid Build Coastguard Worker 	buffer = get_long_name_buffer(&name_length, 3);
161*49cdfc7eSAndroid Build Coastguard Worker 
162*49cdfc7eSAndroid Build Coastguard Worker 	strcpy(buffer, tmp_path);
163*49cdfc7eSAndroid Build Coastguard Worker 	ptr = buffer + strlen(buffer);
164*49cdfc7eSAndroid Build Coastguard Worker 	ptr_end = ptr + name_length + 2;
165*49cdfc7eSAndroid Build Coastguard Worker 	*(ptr++) = '/';
166*49cdfc7eSAndroid Build Coastguard Worker 	for (; ptr < ptr_end; ptr++)
167*49cdfc7eSAndroid Build Coastguard Worker 		*ptr = 'E';
168*49cdfc7eSAndroid Build Coastguard Worker 	*(ptr++) = '/';
169*49cdfc7eSAndroid Build Coastguard Worker 	*(ptr++) = 'F';
170*49cdfc7eSAndroid Build Coastguard Worker 	*ptr = '\000';
171*49cdfc7eSAndroid Build Coastguard Worker 	execute_function(name, callback, buffer, expected);
172*49cdfc7eSAndroid Build Coastguard Worker 	free(buffer);
173*49cdfc7eSAndroid Build Coastguard Worker }
174*49cdfc7eSAndroid Build Coastguard Worker 
175*49cdfc7eSAndroid Build Coastguard Worker void
test_ENAMETOOLONG_path(char * name,int (* callback)(const char *),int expected)176*49cdfc7eSAndroid Build Coastguard Worker test_ENAMETOOLONG_path(char *name, int (*callback) (const char *), int expected)
177*49cdfc7eSAndroid Build Coastguard Worker {
178*49cdfc7eSAndroid Build Coastguard Worker 	size_t pcPathMax;
179*49cdfc7eSAndroid Build Coastguard Worker 	char *path, *tmpPtr;
180*49cdfc7eSAndroid Build Coastguard Worker 	int pathLength, tempPathLength;
181*49cdfc7eSAndroid Build Coastguard Worker 
182*49cdfc7eSAndroid Build Coastguard Worker 	temp = stderr;
183*49cdfc7eSAndroid Build Coastguard Worker 	if ((pcPathMax = pathconf(tmp_path, _PC_PATH_MAX)) == -1) {
184*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TFAIL, "pathconf(_PC_PATH_MAX) failed: %s",
185*49cdfc7eSAndroid Build Coastguard Worker 			 strerror(errno));
186*49cdfc7eSAndroid Build Coastguard Worker 		cleanup_function();
187*49cdfc7eSAndroid Build Coastguard Worker 		fail_exit();
188*49cdfc7eSAndroid Build Coastguard Worker 	}
189*49cdfc7eSAndroid Build Coastguard Worker #ifdef DEBUG
190*49cdfc7eSAndroid Build Coastguard Worker 	fprintf(temp, "INFO: pathconf(_PC_PATH_MAX) for %s is %lu\n",
191*49cdfc7eSAndroid Build Coastguard Worker 		tmp_path, pcPathMax);
192*49cdfc7eSAndroid Build Coastguard Worker #endif
193*49cdfc7eSAndroid Build Coastguard Worker 
194*49cdfc7eSAndroid Build Coastguard Worker 	if ((path = malloc(pcPathMax + 2)) == NULL) {
195*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TFAIL, "malloc(%zu) for path failed: %s",
196*49cdfc7eSAndroid Build Coastguard Worker 			 pcPathMax + 2, strerror(errno));
197*49cdfc7eSAndroid Build Coastguard Worker 		cleanup_function();
198*49cdfc7eSAndroid Build Coastguard Worker 		fail_exit();
199*49cdfc7eSAndroid Build Coastguard Worker 	}
200*49cdfc7eSAndroid Build Coastguard Worker 	path = strcpy(path, tmp_path);
201*49cdfc7eSAndroid Build Coastguard Worker 	pathLength = (int)strlen(path);
202*49cdfc7eSAndroid Build Coastguard Worker 	tempPathLength = pathLength + 1;
203*49cdfc7eSAndroid Build Coastguard Worker 
204*49cdfc7eSAndroid Build Coastguard Worker 	/* leave some chars for element that pushes path over PC_PATH_MAX */
205*49cdfc7eSAndroid Build Coastguard Worker 	pcPathMax = pcPathMax - tempPathLength - 5;
206*49cdfc7eSAndroid Build Coastguard Worker 
207*49cdfc7eSAndroid Build Coastguard Worker 	tmpPtr = path + strlen(path);
208*49cdfc7eSAndroid Build Coastguard Worker 	while (pathLength < pcPathMax) {
209*49cdfc7eSAndroid Build Coastguard Worker 		tmpPtr += sprintf(tmpPtr, "/%s", tmp_path);
210*49cdfc7eSAndroid Build Coastguard Worker 		pathLength += tempPathLength;
211*49cdfc7eSAndroid Build Coastguard Worker 	}
212*49cdfc7eSAndroid Build Coastguard Worker 
213*49cdfc7eSAndroid Build Coastguard Worker 	/* reinstate pcPathMax correct value */
214*49cdfc7eSAndroid Build Coastguard Worker 	pcPathMax = pcPathMax + tempPathLength + 5;
215*49cdfc7eSAndroid Build Coastguard Worker 
216*49cdfc7eSAndroid Build Coastguard Worker 	tmpPtr = path + pathLength;
217*49cdfc7eSAndroid Build Coastguard Worker 	*tmpPtr++ = '/';
218*49cdfc7eSAndroid Build Coastguard Worker 	pathLength++;
219*49cdfc7eSAndroid Build Coastguard Worker 	while (pathLength <= pcPathMax) {
220*49cdfc7eSAndroid Build Coastguard Worker 		*tmpPtr++ = 'z';
221*49cdfc7eSAndroid Build Coastguard Worker 		pathLength++;
222*49cdfc7eSAndroid Build Coastguard Worker 	}
223*49cdfc7eSAndroid Build Coastguard Worker 	*tmpPtr = '\0';
224*49cdfc7eSAndroid Build Coastguard Worker 
225*49cdfc7eSAndroid Build Coastguard Worker 	pathLength = (int)strlen(path);
226*49cdfc7eSAndroid Build Coastguard Worker 	if (pathLength != pcPathMax + 1) {
227*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TFAIL,
228*49cdfc7eSAndroid Build Coastguard Worker 			 "test logic failure, path length is %d, should be %lu",
229*49cdfc7eSAndroid Build Coastguard Worker 			 pathLength, (long unsigned int)pcPathMax + 1);
230*49cdfc7eSAndroid Build Coastguard Worker 		free(path);
231*49cdfc7eSAndroid Build Coastguard Worker 		cleanup_function();
232*49cdfc7eSAndroid Build Coastguard Worker 		fail_exit();
233*49cdfc7eSAndroid Build Coastguard Worker 	}
234*49cdfc7eSAndroid Build Coastguard Worker 	execute_function(name, callback, path, expected);
235*49cdfc7eSAndroid Build Coastguard Worker 	free(path);
236*49cdfc7eSAndroid Build Coastguard Worker }
237*49cdfc7eSAndroid Build Coastguard Worker 
238*49cdfc7eSAndroid Build Coastguard Worker void
test_ENAMETOOLONG_name(char * name,int (* callback)(const char *),int expected)239*49cdfc7eSAndroid Build Coastguard Worker test_ENAMETOOLONG_name(char *name, int (*callback) (const char *), int expected)
240*49cdfc7eSAndroid Build Coastguard Worker {
241*49cdfc7eSAndroid Build Coastguard Worker 	test_long_file_name(name, callback, expected);
242*49cdfc7eSAndroid Build Coastguard Worker 	test_long_component_name(name, callback, expected);
243*49cdfc7eSAndroid Build Coastguard Worker }
244*49cdfc7eSAndroid Build Coastguard Worker 
test_ENOENT_empty(char * name,int (* callback)(const char *),int expected)245*49cdfc7eSAndroid Build Coastguard Worker void test_ENOENT_empty(char *name, int (*callback) (const char *), int expected)
246*49cdfc7eSAndroid Build Coastguard Worker {
247*49cdfc7eSAndroid Build Coastguard Worker 	char *empty_string;
248*49cdfc7eSAndroid Build Coastguard Worker 
249*49cdfc7eSAndroid Build Coastguard Worker 	empty_string = "";
250*49cdfc7eSAndroid Build Coastguard Worker 
251*49cdfc7eSAndroid Build Coastguard Worker 	errno = 0;
252*49cdfc7eSAndroid Build Coastguard Worker 	temp = stderr;
253*49cdfc7eSAndroid Build Coastguard Worker 
254*49cdfc7eSAndroid Build Coastguard Worker #ifdef DEBUG
255*49cdfc7eSAndroid Build Coastguard Worker 	fprintf(temp, "TEST: ENOENT when empty string is passed\n");
256*49cdfc7eSAndroid Build Coastguard Worker #endif
257*49cdfc7eSAndroid Build Coastguard Worker 
258*49cdfc7eSAndroid Build Coastguard Worker 	if ((s2 = (*callback) (empty_string)) == expected) {
259*49cdfc7eSAndroid Build Coastguard Worker 		if (errno != ENOENT) {
260*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TFAIL,
261*49cdfc7eSAndroid Build Coastguard Worker 				 "%s failed: errno should be %i but is %i",
262*49cdfc7eSAndroid Build Coastguard Worker 				 name, ENOENT, errno);
263*49cdfc7eSAndroid Build Coastguard Worker 			cleanup_function();
264*49cdfc7eSAndroid Build Coastguard Worker 			fail_exit();
265*49cdfc7eSAndroid Build Coastguard Worker 		}
266*49cdfc7eSAndroid Build Coastguard Worker 	} else {
267*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TFAIL,
268*49cdfc7eSAndroid Build Coastguard Worker 			 "%s did not return correct value; Expected=%d Received=%d",
269*49cdfc7eSAndroid Build Coastguard Worker 			 name, expected, s2);
270*49cdfc7eSAndroid Build Coastguard Worker 		cleanup_function();
271*49cdfc7eSAndroid Build Coastguard Worker 		fail_exit();
272*49cdfc7eSAndroid Build Coastguard Worker 	}
273*49cdfc7eSAndroid Build Coastguard Worker }
274*49cdfc7eSAndroid Build Coastguard Worker 
test_ENOTDIR(char * name,int (* callback)(const char *),int expected)275*49cdfc7eSAndroid Build Coastguard Worker void test_ENOTDIR(char *name, int (*callback) (const char *), int expected)
276*49cdfc7eSAndroid Build Coastguard Worker {
277*49cdfc7eSAndroid Build Coastguard Worker 	int fd;
278*49cdfc7eSAndroid Build Coastguard Worker 
279*49cdfc7eSAndroid Build Coastguard Worker 	if ((fd = creat(is_a_file, (mode_t) (S_IRWXU | S_IRWXG |
280*49cdfc7eSAndroid Build Coastguard Worker 					     S_IRWXO))) == -1) {
281*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TFAIL, "creat(%s) failed: %s", is_a_file,
282*49cdfc7eSAndroid Build Coastguard Worker 			 strerror(errno));
283*49cdfc7eSAndroid Build Coastguard Worker 		cleanup_function();
284*49cdfc7eSAndroid Build Coastguard Worker 		fail_exit();
285*49cdfc7eSAndroid Build Coastguard Worker 	}
286*49cdfc7eSAndroid Build Coastguard Worker 
287*49cdfc7eSAndroid Build Coastguard Worker 	if (close(fd) == -1) {
288*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TFAIL, "close(%i) failed: %s", fd, strerror(errno));
289*49cdfc7eSAndroid Build Coastguard Worker 		remove_test_ENOTDIR_files();
290*49cdfc7eSAndroid Build Coastguard Worker 		cleanup_function();
291*49cdfc7eSAndroid Build Coastguard Worker 		fail_exit();
292*49cdfc7eSAndroid Build Coastguard Worker 	}
293*49cdfc7eSAndroid Build Coastguard Worker 
294*49cdfc7eSAndroid Build Coastguard Worker 	errno = 0;
295*49cdfc7eSAndroid Build Coastguard Worker 
296*49cdfc7eSAndroid Build Coastguard Worker 	temp = stderr;
297*49cdfc7eSAndroid Build Coastguard Worker #ifdef DEBUG
298*49cdfc7eSAndroid Build Coastguard Worker 	fprintf(temp, "TEST: ENOTDIR when a component is not a directory\n");
299*49cdfc7eSAndroid Build Coastguard Worker #endif
300*49cdfc7eSAndroid Build Coastguard Worker 
301*49cdfc7eSAndroid Build Coastguard Worker 	s2 = (*callback) ("./tmp/is_a_file/no_file");
302*49cdfc7eSAndroid Build Coastguard Worker 	/*callback found an error, bail */
303*49cdfc7eSAndroid Build Coastguard Worker 	if (s2 == -752) {
304*49cdfc7eSAndroid Build Coastguard Worker 		remove_test_ENOTDIR_files();
305*49cdfc7eSAndroid Build Coastguard Worker 		cleanup_function();
306*49cdfc7eSAndroid Build Coastguard Worker 		fail_exit();
307*49cdfc7eSAndroid Build Coastguard Worker 	}
308*49cdfc7eSAndroid Build Coastguard Worker 	if (s2 == expected) {
309*49cdfc7eSAndroid Build Coastguard Worker 		if (errno != ENOTDIR) {
310*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TFAIL,
311*49cdfc7eSAndroid Build Coastguard Worker 				 "%s failed: errno should be %i but is %i",
312*49cdfc7eSAndroid Build Coastguard Worker 				 name, ENOTDIR, errno);
313*49cdfc7eSAndroid Build Coastguard Worker 			cleanup_function();
314*49cdfc7eSAndroid Build Coastguard Worker 			fail_exit();
315*49cdfc7eSAndroid Build Coastguard Worker 		}
316*49cdfc7eSAndroid Build Coastguard Worker 	} else {
317*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TFAIL,
318*49cdfc7eSAndroid Build Coastguard Worker 			 "%s did not return correct value; Expected=%d Received=%d",
319*49cdfc7eSAndroid Build Coastguard Worker 			 name, expected, s2);
320*49cdfc7eSAndroid Build Coastguard Worker 		cleanup_function();
321*49cdfc7eSAndroid Build Coastguard Worker 		fail_exit();
322*49cdfc7eSAndroid Build Coastguard Worker 	}
323*49cdfc7eSAndroid Build Coastguard Worker 	remove_test_ENOTDIR_files();
324*49cdfc7eSAndroid Build Coastguard Worker }
325*49cdfc7eSAndroid Build Coastguard Worker 
326*49cdfc7eSAndroid Build Coastguard Worker void
test_ENOENT_nofile(char * name,int (* callback)(const char *),int expected)327*49cdfc7eSAndroid Build Coastguard Worker test_ENOENT_nofile(char *name, int (*callback) (const char *), int expected)
328*49cdfc7eSAndroid Build Coastguard Worker {
329*49cdfc7eSAndroid Build Coastguard Worker 
330*49cdfc7eSAndroid Build Coastguard Worker 	remove_test_ENOENT_files();
331*49cdfc7eSAndroid Build Coastguard Worker 	temp = stderr;
332*49cdfc7eSAndroid Build Coastguard Worker 
333*49cdfc7eSAndroid Build Coastguard Worker #ifdef DEBUG
334*49cdfc7eSAndroid Build Coastguard Worker 	fprintf(temp, "TEST: ENOENT when file does not exist\n");
335*49cdfc7eSAndroid Build Coastguard Worker #endif
336*49cdfc7eSAndroid Build Coastguard Worker 
337*49cdfc7eSAndroid Build Coastguard Worker 	if ((s2 = (*callback) (no_file)) == expected) {
338*49cdfc7eSAndroid Build Coastguard Worker 		if (errno != ENOENT) {
339*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TFAIL,
340*49cdfc7eSAndroid Build Coastguard Worker 				 "%s failed: errno should be %i but is %i",
341*49cdfc7eSAndroid Build Coastguard Worker 				 name, ENOENT, errno);
342*49cdfc7eSAndroid Build Coastguard Worker 			cleanup_function();
343*49cdfc7eSAndroid Build Coastguard Worker 			fail_exit();
344*49cdfc7eSAndroid Build Coastguard Worker 		}
345*49cdfc7eSAndroid Build Coastguard Worker 	} else {
346*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TFAIL,
347*49cdfc7eSAndroid Build Coastguard Worker 			 "%s did not return correct value; Expected=%d Received=%d",
348*49cdfc7eSAndroid Build Coastguard Worker 			 name, expected, s2);
349*49cdfc7eSAndroid Build Coastguard Worker 		cleanup_function();
350*49cdfc7eSAndroid Build Coastguard Worker 		fail_exit();
351*49cdfc7eSAndroid Build Coastguard Worker 	}
352*49cdfc7eSAndroid Build Coastguard Worker }
353