1 /*
2  *
3  *   Copyright (c) International Business Machines  Corp., 2002
4  *
5  *   This program is free software;  you can redistribute it and/or modify
6  *   it under the terms of the GNU General Public License as published by
7  *   the Free Software Foundation; either version 2 of the License, or
8  *   (at your option) any later version.
9  *
10  *   This program is distributed in the hope that it will be useful,
11  *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
13  *   the GNU General Public License for more details.
14  *
15  *   You should have received a copy of the GNU General Public License
16  *   along with this program;  if not, write to the Free Software
17  *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  */
19 
20 /* 10/31/2002   Port to LTP     robbiew@us.ibm.com */
21 /* 06/30/2001   Port to Linux   nsharoff@us.ibm.com */
22 
23 /*
24  * NAME
25  *      lib.c - This file contains code for common failure conditions
26  */
27 
28 #include "nftw.h"
29 
30 extern int s2;
31 
32 static char *tmp_path = "./tmp";
33 static const char *no_file = "./tmp/no_such_file";
34 static const char *is_a_file = "./tmp/is_a_file";
35 
36 extern FILE *temp;
37 /*
38  * Cleanup the ./tmp
39  */
remove_test_ENOTDIR_files(void)40 void remove_test_ENOTDIR_files(void)
41 {
42 	(void)unlink(is_a_file);
43 }
44 
45 /*
46  * Cleanup the ./tmp
47  */
remove_test_ENOENT_files(void)48 void remove_test_ENOENT_files(void)
49 {
50 	(void)unlink(no_file);
51 }
52 
53 /*
54  *  get_long_name_buffer()
55  * 	Create a path containing a component of length pathconf(NAME_MAX) + 1
56  *      characters.
57  */
58 
get_long_name_buffer(size_t * length,size_t extra)59 static char *get_long_name_buffer(size_t * length, size_t extra)
60 {
61 	char *buffer;
62 	size_t path_length, name_length;
63 
64 	temp = stderr;
65 	if ((path_length = pathconf(tmp_path, _PC_PATH_MAX)) == -1) {
66 		tst_resm(TFAIL, "pathconf(_PC_PATH_MAX) failed: %s",
67 			 strerror(errno));
68 		cleanup_function();
69 		fail_exit();
70 	}
71 
72 	if ((name_length = pathconf(tmp_path, _PC_NAME_MAX)) == -1) {
73 		tst_resm(TFAIL, "pathconf(_PC_NAME_MAX) failed: %s",
74 			 strerror(errno));
75 		cleanup_function();
76 		fail_exit();
77 	}
78 
79 	if ((strlen(tmp_path) + name_length + extra) > path_length) {
80 		tst_resm(TFAIL,
81 			 "pathconf(_PC_NAME_MAX)[=%zi] too large relative to pathconf(_PC_PATH_MAX)[=%zi]",
82 			 name_length, path_length);
83 		cleanup_function();
84 		fail_exit();
85 	}
86 
87 	if ((buffer = malloc(path_length + extra)) == NULL) {
88 		tst_resm(TFAIL, "malloc(%zi) failed: %s", path_length + extra,
89 			 strerror(errno));
90 		cleanup_function();
91 		fail_exit();
92 	}
93 	*length = name_length;
94 	return buffer;
95 }
96 
97 static void
execute_function(char * name,int (* callback)(const char *),char * buffer,int expected)98 execute_function(char *name, int (*callback) (const char *), char *buffer,
99 		 int expected)
100 {
101 	int result;
102 	temp = stderr;
103 
104 #ifdef DEBUG
105 	fprintf(temp, "TEST: %s fails with ENAMETOOLONG\n", name);
106 #endif
107 
108 	errno = 0;
109 	result = (*callback) (buffer);
110 
111 	/*callback found an error, fail */
112 	if (result == -752) {
113 		tst_resm(TFAIL, "%s callback did not work correctly", name);
114 		cleanup_function();
115 		fail_exit();
116 	}
117 	if (result != expected) {
118 		tst_resm(TFAIL,
119 			 "%s did not return value as expected; Expected=%d Received=%d",
120 			 name, expected, result);
121 		cleanup_function();
122 		fail_exit();
123 	}
124 	if (errno != ENAMETOOLONG) {
125 		tst_resm(TFAIL, "%s failed: errno should be %i but is %i", name,
126 			 ENAMETOOLONG, errno);
127 		cleanup_function();
128 		fail_exit();
129 	}
130 }
131 
132 static void
test_long_file_name(char * name,int (* callback)(const char *),int expected)133 test_long_file_name(char *name, int (*callback) (const char *), int expected)
134 {
135 	char *ptr, *ptr_end, *buffer;
136 	size_t name_length;
137 
138 	buffer = get_long_name_buffer(&name_length, 1);
139 
140 	strcpy(buffer, tmp_path);
141 	ptr = buffer + strlen(buffer);
142 	ptr_end = ptr + name_length + 1;
143 
144 	*(ptr++) = '/';
145 	while (ptr <= ptr_end)
146 		*(ptr++) = 'E';
147 	*ptr = '\000';
148 
149 	execute_function(name, callback, buffer, expected);
150 	free(buffer);
151 }
152 
153 static void
test_long_component_name(char * name,int (* callback)(const char *),int expected)154 test_long_component_name(char *name, int (*callback) (const char *),
155 			 int expected)
156 {
157 	char *ptr, *ptr_end, *buffer;
158 	size_t name_length;
159 
160 	buffer = get_long_name_buffer(&name_length, 3);
161 
162 	strcpy(buffer, tmp_path);
163 	ptr = buffer + strlen(buffer);
164 	ptr_end = ptr + name_length + 2;
165 	*(ptr++) = '/';
166 	for (; ptr < ptr_end; ptr++)
167 		*ptr = 'E';
168 	*(ptr++) = '/';
169 	*(ptr++) = 'F';
170 	*ptr = '\000';
171 	execute_function(name, callback, buffer, expected);
172 	free(buffer);
173 }
174 
175 void
test_ENAMETOOLONG_path(char * name,int (* callback)(const char *),int expected)176 test_ENAMETOOLONG_path(char *name, int (*callback) (const char *), int expected)
177 {
178 	size_t pcPathMax;
179 	char *path, *tmpPtr;
180 	int pathLength, tempPathLength;
181 
182 	temp = stderr;
183 	if ((pcPathMax = pathconf(tmp_path, _PC_PATH_MAX)) == -1) {
184 		tst_resm(TFAIL, "pathconf(_PC_PATH_MAX) failed: %s",
185 			 strerror(errno));
186 		cleanup_function();
187 		fail_exit();
188 	}
189 #ifdef DEBUG
190 	fprintf(temp, "INFO: pathconf(_PC_PATH_MAX) for %s is %lu\n",
191 		tmp_path, pcPathMax);
192 #endif
193 
194 	if ((path = malloc(pcPathMax + 2)) == NULL) {
195 		tst_resm(TFAIL, "malloc(%zu) for path failed: %s",
196 			 pcPathMax + 2, strerror(errno));
197 		cleanup_function();
198 		fail_exit();
199 	}
200 	path = strcpy(path, tmp_path);
201 	pathLength = (int)strlen(path);
202 	tempPathLength = pathLength + 1;
203 
204 	/* leave some chars for element that pushes path over PC_PATH_MAX */
205 	pcPathMax = pcPathMax - tempPathLength - 5;
206 
207 	tmpPtr = path + strlen(path);
208 	while (pathLength < pcPathMax) {
209 		tmpPtr += sprintf(tmpPtr, "/%s", tmp_path);
210 		pathLength += tempPathLength;
211 	}
212 
213 	/* reinstate pcPathMax correct value */
214 	pcPathMax = pcPathMax + tempPathLength + 5;
215 
216 	tmpPtr = path + pathLength;
217 	*tmpPtr++ = '/';
218 	pathLength++;
219 	while (pathLength <= pcPathMax) {
220 		*tmpPtr++ = 'z';
221 		pathLength++;
222 	}
223 	*tmpPtr = '\0';
224 
225 	pathLength = (int)strlen(path);
226 	if (pathLength != pcPathMax + 1) {
227 		tst_resm(TFAIL,
228 			 "test logic failure, path length is %d, should be %lu",
229 			 pathLength, (long unsigned int)pcPathMax + 1);
230 		free(path);
231 		cleanup_function();
232 		fail_exit();
233 	}
234 	execute_function(name, callback, path, expected);
235 	free(path);
236 }
237 
238 void
test_ENAMETOOLONG_name(char * name,int (* callback)(const char *),int expected)239 test_ENAMETOOLONG_name(char *name, int (*callback) (const char *), int expected)
240 {
241 	test_long_file_name(name, callback, expected);
242 	test_long_component_name(name, callback, expected);
243 }
244 
test_ENOENT_empty(char * name,int (* callback)(const char *),int expected)245 void test_ENOENT_empty(char *name, int (*callback) (const char *), int expected)
246 {
247 	char *empty_string;
248 
249 	empty_string = "";
250 
251 	errno = 0;
252 	temp = stderr;
253 
254 #ifdef DEBUG
255 	fprintf(temp, "TEST: ENOENT when empty string is passed\n");
256 #endif
257 
258 	if ((s2 = (*callback) (empty_string)) == expected) {
259 		if (errno != ENOENT) {
260 			tst_resm(TFAIL,
261 				 "%s failed: errno should be %i but is %i",
262 				 name, ENOENT, errno);
263 			cleanup_function();
264 			fail_exit();
265 		}
266 	} else {
267 		tst_resm(TFAIL,
268 			 "%s did not return correct value; Expected=%d Received=%d",
269 			 name, expected, s2);
270 		cleanup_function();
271 		fail_exit();
272 	}
273 }
274 
test_ENOTDIR(char * name,int (* callback)(const char *),int expected)275 void test_ENOTDIR(char *name, int (*callback) (const char *), int expected)
276 {
277 	int fd;
278 
279 	if ((fd = creat(is_a_file, (mode_t) (S_IRWXU | S_IRWXG |
280 					     S_IRWXO))) == -1) {
281 		tst_resm(TFAIL, "creat(%s) failed: %s", is_a_file,
282 			 strerror(errno));
283 		cleanup_function();
284 		fail_exit();
285 	}
286 
287 	if (close(fd) == -1) {
288 		tst_resm(TFAIL, "close(%i) failed: %s", fd, strerror(errno));
289 		remove_test_ENOTDIR_files();
290 		cleanup_function();
291 		fail_exit();
292 	}
293 
294 	errno = 0;
295 
296 	temp = stderr;
297 #ifdef DEBUG
298 	fprintf(temp, "TEST: ENOTDIR when a component is not a directory\n");
299 #endif
300 
301 	s2 = (*callback) ("./tmp/is_a_file/no_file");
302 	/*callback found an error, bail */
303 	if (s2 == -752) {
304 		remove_test_ENOTDIR_files();
305 		cleanup_function();
306 		fail_exit();
307 	}
308 	if (s2 == expected) {
309 		if (errno != ENOTDIR) {
310 			tst_resm(TFAIL,
311 				 "%s failed: errno should be %i but is %i",
312 				 name, ENOTDIR, errno);
313 			cleanup_function();
314 			fail_exit();
315 		}
316 	} else {
317 		tst_resm(TFAIL,
318 			 "%s did not return correct value; Expected=%d Received=%d",
319 			 name, expected, s2);
320 		cleanup_function();
321 		fail_exit();
322 	}
323 	remove_test_ENOTDIR_files();
324 }
325 
326 void
test_ENOENT_nofile(char * name,int (* callback)(const char *),int expected)327 test_ENOENT_nofile(char *name, int (*callback) (const char *), int expected)
328 {
329 
330 	remove_test_ENOENT_files();
331 	temp = stderr;
332 
333 #ifdef DEBUG
334 	fprintf(temp, "TEST: ENOENT when file does not exist\n");
335 #endif
336 
337 	if ((s2 = (*callback) (no_file)) == expected) {
338 		if (errno != ENOENT) {
339 			tst_resm(TFAIL,
340 				 "%s failed: errno should be %i but is %i",
341 				 name, ENOENT, errno);
342 			cleanup_function();
343 			fail_exit();
344 		}
345 	} else {
346 		tst_resm(TFAIL,
347 			 "%s did not return correct value; Expected=%d Received=%d",
348 			 name, expected, s2);
349 		cleanup_function();
350 		fail_exit();
351 	}
352 }
353