1 /*
2    Unix SMB/CIFS implementation.
3 
4    libreplace tests
5 
6    Copyright (C) Jelmer Vernooij 2006
7 
8      ** NOTE! The following LGPL license applies to the talloc
9      ** library. This does NOT imply that all of Samba is released
10      ** under the LGPL
11 
12    This library is free software; you can redistribute it and/or
13    modify it under the terms of the GNU Lesser General Public
14    License as published by the Free Software Foundation; either
15    version 3 of the License, or (at your option) any later version.
16 
17    This library is distributed in the hope that it will be useful,
18    but WITHOUT ANY WARRANTY; without even the implied warranty of
19    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20    Lesser General Public License for more details.
21 
22    You should have received a copy of the GNU Lesser General Public
23    License along with this library; if not, see <http://www.gnu.org/licenses/>.
24 */
25 
26 #include "replace.h"
27 #include "replace-test.h"
28 #include "replace-testsuite.h"
29 
30 /*
31   we include all the system/ include files here so that libreplace tests
32   them in the build farm
33 */
34 #include "system/capability.h"
35 #include "system/dir.h"
36 #include "system/filesys.h"
37 #include "system/glob.h"
38 #include "system/iconv.h"
39 #include "system/locale.h"
40 #include "system/network.h"
41 #include "system/passwd.h"
42 #include "system/readline.h"
43 #include "system/select.h"
44 #include "system/shmem.h"
45 #include "system/syslog.h"
46 #include "system/terminal.h"
47 #include "system/time.h"
48 #include "system/wait.h"
49 
50 #define TESTFILE "testfile.dat"
51 
52 
53 /*
54   test ftruncate() function
55  */
test_ftruncate(void)56 static int test_ftruncate(void)
57 {
58 	struct stat st;
59 	int fd;
60 	const int size = 1234;
61 	printf("test: ftruncate\n");
62 	unlink(TESTFILE);
63 	fd = open(TESTFILE, O_RDWR|O_CREAT, 0600);
64 	if (fd == -1) {
65 		printf("failure: ftruncate [\n"
66 			   "creating '%s' failed - %s\n]\n", TESTFILE, strerror(errno));
67 		return false;
68 	}
69 	if (ftruncate(fd, size) != 0) {
70 		printf("failure: ftruncate [\n%s\n]\n", strerror(errno));
71 		close(fd);
72 		return false;
73 	}
74 	if (fstat(fd, &st) != 0) {
75 		printf("failure: ftruncate [\nfstat failed - %s\n]\n", strerror(errno));
76 		close(fd);
77 		return false;
78 	}
79 	if (st.st_size != size) {
80 		printf("failure: ftruncate [\ngave wrong size %d - expected %d\n]\n",
81 		       (int)st.st_size, size);
82 		close(fd);
83 		return false;
84 	}
85 	unlink(TESTFILE);
86 	printf("success: ftruncate\n");
87 	close(fd);
88 	return true;
89 }
90 
91 /*
92   test strlcpy() function.
93   see http://www.gratisoft.us/todd/papers/strlcpy.html
94  */
test_strlcpy(void)95 static int test_strlcpy(void)
96 {
97 	char buf[4];
98 	const struct {
99 		const char *src;
100 		size_t result;
101 	} tests[] = {
102 		{ "abc", 3 },
103 		{ "abcdef", 6 },
104 		{ "abcd", 4 },
105 		{ "", 0 },
106 		{ NULL, 0 }
107 	};
108 	int i;
109 	printf("test: strlcpy\n");
110 	for (i=0;tests[i].src;i++) {
111 		if (strlcpy(buf, tests[i].src, sizeof(buf)) != tests[i].result) {
112 			printf("failure: strlcpy [\ntest %d failed\n]\n", i);
113 			return false;
114 		}
115 	}
116 	printf("success: strlcpy\n");
117 	return true;
118 }
119 
test_strlcat(void)120 static int test_strlcat(void)
121 {
122 	char tmp[10];
123 	printf("test: strlcat\n");
124 	strlcpy(tmp, "", sizeof(tmp));
125 	if (strlcat(tmp, "bla", 3) != 3) {
126 		printf("failure: strlcat [\ninvalid return code\n]\n");
127 		return false;
128 	}
129 	if (strcmp(tmp, "bl") != 0) {
130 		printf("failure: strlcat [\nexpected \"bl\", got \"%s\"\n]\n",
131 			   tmp);
132 		return false;
133 	}
134 
135 	strlcpy(tmp, "da", sizeof(tmp));
136 	if (strlcat(tmp, "me", 4) != 4) {
137 		printf("failure: strlcat [\nexpected \"dam\", got \"%s\"\n]\n",
138 			   tmp);
139 		return false;
140 	}
141 
142 	printf("success: strlcat\n");
143 	return true;
144 }
145 
test_mktime(void)146 static int test_mktime(void)
147 {
148 	/* FIXME */
149 	return true;
150 }
151 
test_initgroups(void)152 static int test_initgroups(void)
153 {
154 	/* FIXME */
155 	return true;
156 }
157 
test_memmove(void)158 static int test_memmove(void)
159 {
160 	/* FIXME */
161 	return true;
162 }
163 
test_strdup(void)164 static int test_strdup(void)
165 {
166 	char *x;
167 	printf("test: strdup\n");
168 	x = strdup("bla");
169 	if (strcmp("bla", x) != 0) {
170 		printf("failure: strdup [\nfailed: expected \"bla\", got \"%s\"\n]\n",
171 			   x);
172 		return false;
173 	}
174 	free(x);
175 	printf("success: strdup\n");
176 	return true;
177 }
178 
test_setlinebuf(void)179 static int test_setlinebuf(void)
180 {
181 	printf("test: setlinebuf\n");
182 	setlinebuf(stdout);
183 	printf("success: setlinebuf\n");
184 	return true;
185 }
186 
test_vsyslog(void)187 static int test_vsyslog(void)
188 {
189 	/* FIXME */
190 	return true;
191 }
192 
test_timegm(void)193 static int test_timegm(void)
194 {
195 	/* FIXME */
196 	return true;
197 }
198 
test_setenv(void)199 static int test_setenv(void)
200 {
201 #define TEST_SETENV(key, value, overwrite, result) do { \
202 	int _ret; \
203 	char *_v; \
204 	_ret = setenv(key, value, overwrite); \
205 	if (_ret != 0) { \
206 		printf("failure: setenv [\n" \
207 			"setenv(%s, %s, %d) failed\n" \
208 			"]\n", \
209 			key, value, overwrite); \
210 		return false; \
211 	} \
212 	_v=getenv(key); \
213 	if (!_v) { \
214 		printf("failure: setenv [\n" \
215 			"getenv(%s) returned NULL\n" \
216 			"]\n", \
217 			key); \
218 		return false; \
219 	} \
220 	if (strcmp(result, _v) != 0) { \
221 		printf("failure: setenv [\n" \
222 			"getenv(%s): '%s' != '%s'\n" \
223 			"]\n", \
224 			key, result, _v); \
225 		return false; \
226 	} \
227 } while(0)
228 
229 #define TEST_UNSETENV(key) do { \
230 	char *_v; \
231 	unsetenv(key); \
232 	_v=getenv(key); \
233 	if (_v) { \
234 		printf("failure: setenv [\n" \
235 			"getenv(%s): NULL != '%s'\n" \
236 			"]\n", \
237 			SETENVTEST_KEY, _v); \
238 		return false; \
239 	} \
240 } while (0)
241 
242 #define SETENVTEST_KEY "SETENVTESTKEY"
243 #define SETENVTEST_VAL "SETENVTESTVAL"
244 
245 	printf("test: setenv\n");
246 	TEST_SETENV(SETENVTEST_KEY, SETENVTEST_VAL"1", 0, SETENVTEST_VAL"1");
247 	TEST_SETENV(SETENVTEST_KEY, SETENVTEST_VAL"2", 0, SETENVTEST_VAL"1");
248 	TEST_SETENV(SETENVTEST_KEY, SETENVTEST_VAL"3", 1, SETENVTEST_VAL"3");
249 	TEST_SETENV(SETENVTEST_KEY, SETENVTEST_VAL"4", 1, SETENVTEST_VAL"4");
250 	TEST_UNSETENV(SETENVTEST_KEY);
251 	TEST_UNSETENV(SETENVTEST_KEY);
252 	TEST_SETENV(SETENVTEST_KEY, SETENVTEST_VAL"5", 0, SETENVTEST_VAL"5");
253 	TEST_UNSETENV(SETENVTEST_KEY);
254 	TEST_UNSETENV(SETENVTEST_KEY);
255 	printf("success: setenv\n");
256 	return true;
257 }
258 
test_strndup(void)259 static int test_strndup(void)
260 {
261 	char *x;
262 	printf("test: strndup\n");
263 	x = strndup("bla", 0);
264 	if (strcmp(x, "") != 0) {
265 		printf("failure: strndup [\ninvalid\n]\n");
266 		return false;
267 	}
268 	free(x);
269 	x = strndup("bla", 2);
270 	if (strcmp(x, "bl") != 0) {
271 		printf("failure: strndup [\ninvalid\n]\n");
272 		return false;
273 	}
274 	free(x);
275 	x = strndup("bla", 10);
276 	if (strcmp(x, "bla") != 0) {
277 		printf("failure: strndup [\ninvalid\n]\n");
278 		free(x);
279 		return false;
280 	}
281 	free(x);
282 	printf("success: strndup\n");
283 	return true;
284 }
285 
test_strnlen(void)286 static int test_strnlen(void)
287 {
288 	printf("test: strnlen\n");
289 	if (strnlen("bla", 2) != 2) {
290 		printf("failure: strnlen [\nunexpected length\n]\n");
291 		return false;
292 	}
293 
294 	if (strnlen("some text\n", 0) != 0) {
295 		printf("failure: strnlen [\nunexpected length\n]\n");
296 		return false;
297 	}
298 
299 	if (strnlen("some text", 20) != 9) {
300 		printf("failure: strnlen [\nunexpected length\n]\n");
301 		return false;
302 	}
303 
304 	printf("success: strnlen\n");
305 	return true;
306 }
307 
test_waitpid(void)308 static int test_waitpid(void)
309 {
310 	/* FIXME */
311 	return true;
312 }
313 
test_seteuid(void)314 static int test_seteuid(void)
315 {
316 	/* FIXME */
317 	return true;
318 }
319 
test_setegid(void)320 static int test_setegid(void)
321 {
322 	/* FIXME */
323 	return true;
324 }
325 
test_asprintf(void)326 static int test_asprintf(void)
327 {
328 	char *x;
329 	printf("test: asprintf\n");
330 	if (asprintf(&x, "%d", 9) != 1) {
331 		printf("failure: asprintf [\ngenerate asprintf\n]\n");
332 		return false;
333 	}
334 	if (strcmp(x, "9") != 0) {
335 		printf("failure: asprintf [\ngenerate asprintf\n]\n");
336 		return false;
337 	}
338 	if (asprintf(&x, "dat%s", "a") != 4) {
339 		printf("failure: asprintf [\ngenerate asprintf\n]\n");
340 		return false;
341 	}
342 	if (strcmp(x, "data") != 0) {
343 		printf("failure: asprintf [\ngenerate asprintf\n]\n");
344 		return false;
345 	}
346 	printf("success: asprintf\n");
347 	return true;
348 }
349 
test_snprintf(void)350 static int test_snprintf(void)
351 {
352 	char tmp[10];
353 	printf("test: snprintf\n");
354 	if (snprintf(tmp, 3, "foo%d", 9) != 4) {
355 		printf("failure: snprintf [\nsnprintf return code failed\n]\n");
356 		return false;
357 	}
358 
359 	if (strcmp(tmp, "fo") != 0) {
360 		printf("failure: snprintf [\nsnprintf failed\n]\n");
361 		return false;
362 	}
363 
364 	printf("success: snprintf\n");
365 	return true;
366 }
367 
test_vasprintf(void)368 static int test_vasprintf(void)
369 {
370 	/* FIXME */
371 	return true;
372 }
373 
test_vsnprintf(void)374 static int test_vsnprintf(void)
375 {
376 	/* FIXME */
377 	return true;
378 }
379 
test_opendir(void)380 static int test_opendir(void)
381 {
382 	/* FIXME */
383 	return true;
384 }
385 
test_readdir(void)386 static int test_readdir(void)
387 {
388 	printf("test: readdir\n");
389 	if (test_readdir_os2_delete() != 0) {
390 		return false;
391 	}
392 	printf("success: readdir\n");
393 	return true;
394 }
395 
test_telldir(void)396 static int test_telldir(void)
397 {
398 	/* FIXME */
399 	return true;
400 }
401 
test_seekdir(void)402 static int test_seekdir(void)
403 {
404 	/* FIXME */
405 	return true;
406 }
407 
test_dlopen(void)408 static int test_dlopen(void)
409 {
410 	/* FIXME: test dlopen, dlsym, dlclose, dlerror */
411 	return true;
412 }
413 
414 
test_chroot(void)415 static int test_chroot(void)
416 {
417 	/* FIXME: chroot() */
418 	return true;
419 }
420 
test_bzero(void)421 static int test_bzero(void)
422 {
423 	/* FIXME: bzero */
424 	return true;
425 }
426 
test_strerror(void)427 static int test_strerror(void)
428 {
429 	/* FIXME */
430 	return true;
431 }
432 
test_errno(void)433 static int test_errno(void)
434 {
435 	printf("test: errno\n");
436 	errno = 3;
437 	if (errno != 3) {
438 		printf("failure: errno [\nerrno failed\n]\n");
439 		return false;
440 	}
441 
442 	printf("success: errno\n");
443 	return true;
444 }
445 
test_mkdtemp(void)446 static int test_mkdtemp(void)
447 {
448 	/* FIXME */
449 	return true;
450 }
451 
test_mkstemp(void)452 static int test_mkstemp(void)
453 {
454 	/* FIXME */
455 	return true;
456 }
457 
test_pread(void)458 static int test_pread(void)
459 {
460 	/* FIXME */
461 	return true;
462 }
463 
test_pwrite(void)464 static int test_pwrite(void)
465 {
466 	/* FIXME */
467 	return true;
468 }
469 
test_inet_ntoa(void)470 static int test_inet_ntoa(void)
471 {
472 	/* FIXME */
473 	return true;
474 }
475 
476 #define TEST_STRTO_X(type,fmt,func,str,base,res,diff,rrnoo) do {\
477 	type _v; \
478 	char _s[64]; \
479 	char *_p = NULL;\
480 	char *_ep = NULL; \
481 	strlcpy(_s, str, sizeof(_s));\
482 	if (diff >= 0) { \
483 		_ep = &_s[diff]; \
484 	} \
485 	errno = 0; \
486 	_v = func(_s, &_p, base); \
487 	if (errno != rrnoo) { \
488 		printf("failure: %s [\n" \
489 		       "\t%s\n" \
490 		       "\t%s(\"%s\",%d,%d): " fmt " (=/!)= " fmt "\n" \
491 		       "\terrno: %d != %d\n" \
492 		       "]\n", \
493 		        __STRING(func), __location__, __STRING(func), \
494 		       str, diff, base, res, _v, rrnoo, errno); \
495 		return false; \
496 	} else if (_v != res) { \
497 		printf("failure: %s [\n" \
498 		       "\t%s\n" \
499 		       "\t%s(\"%s\",%d,%d): " fmt " != " fmt "\n" \
500 		       "]\n", \
501 		       __STRING(func), __location__, __STRING(func), \
502 		       str, diff, base, res, _v); \
503 		return false; \
504 	} else if (_p != _ep) { \
505 		printf("failure: %s [\n" \
506 		       "\t%s\n" \
507 		       "\t%s(\"%s\",%d,%d): " fmt " (=/!)= " fmt "\n" \
508 		       "\tptr: %p - %p = %d != %d\n" \
509 		       "]\n", \
510 		       __STRING(func), __location__, __STRING(func), \
511 		       str, diff, base, res, _v, _ep, _p, (int)(diff - (_ep - _p)), diff); \
512 		return false; \
513 	} \
514 } while (0)
515 
test_strtoll(void)516 static int test_strtoll(void)
517 {
518 	printf("test: strtoll\n");
519 
520 #define TEST_STRTOLL(str,base,res,diff,errnoo) TEST_STRTO_X(long long int, "%lld", strtoll,str,base,res,diff,errnoo)
521 
522 	TEST_STRTOLL("15",	10,	15LL,	2, 0);
523 	TEST_STRTOLL("  15",	10,	15LL,	4, 0);
524 	TEST_STRTOLL("15",	0,	15LL,	2, 0);
525 	TEST_STRTOLL(" 15 ",	0,	15LL,	3, 0);
526 	TEST_STRTOLL("+15",	10,	15LL,	3, 0);
527 	TEST_STRTOLL("  +15",	10,	15LL,	5, 0);
528 	TEST_STRTOLL("+15",	0,	15LL,	3, 0);
529 	TEST_STRTOLL(" +15 ",	0,	15LL,	4, 0);
530 	TEST_STRTOLL("-15",	10,	-15LL,	3, 0);
531 	TEST_STRTOLL("  -15",	10,	-15LL,	5, 0);
532 	TEST_STRTOLL("-15",	0,	-15LL,	3, 0);
533 	TEST_STRTOLL(" -15 ",	0,	-15LL,	4, 0);
534 	TEST_STRTOLL("015",	10,	15LL,	3, 0);
535 	TEST_STRTOLL("  015",	10,	15LL,	5, 0);
536 	TEST_STRTOLL("015",	0,	13LL,	3, 0);
537 	TEST_STRTOLL("  015",	0,	13LL,	5, 0);
538 	TEST_STRTOLL("0x15",	10,	0LL,	1, 0);
539 	TEST_STRTOLL("  0x15",	10,	0LL,	3, 0);
540 	TEST_STRTOLL("0x15",	0,	21LL,	4, 0);
541 	TEST_STRTOLL("  0x15",	0,	21LL,	6, 0);
542 
543 	TEST_STRTOLL("10",	16,	16LL,	2, 0);
544 	TEST_STRTOLL("  10 ",	16,	16LL,	4, 0);
545 	TEST_STRTOLL("0x10",	16,	16LL,	4, 0);
546 	TEST_STRTOLL("0x10",	0,	16LL,	4, 0);
547 	TEST_STRTOLL(" 0x10 ",	0,	16LL,	5, 0);
548 	TEST_STRTOLL("+10",	16,	16LL,	3, 0);
549 	TEST_STRTOLL("  +10 ",	16,	16LL,	5, 0);
550 	TEST_STRTOLL("+0x10",	16,	16LL,	5, 0);
551 	TEST_STRTOLL("+0x10",	0,	16LL,	5, 0);
552 	TEST_STRTOLL(" +0x10 ",	0,	16LL,	6, 0);
553 	TEST_STRTOLL("-10",	16,	-16LL,	3, 0);
554 	TEST_STRTOLL("  -10 ",	16,	-16LL,	5, 0);
555 	TEST_STRTOLL("-0x10",	16,	-16LL,	5, 0);
556 	TEST_STRTOLL("-0x10",	0,	-16LL,	5, 0);
557 	TEST_STRTOLL(" -0x10 ",	0,	-16LL,	6, 0);
558 	TEST_STRTOLL("010",	16,	16LL,	3, 0);
559 	TEST_STRTOLL("  010 ",	16,	16LL,	5, 0);
560 	TEST_STRTOLL("-010",	16,	-16LL,	4, 0);
561 
562 	TEST_STRTOLL("11",	8,	9LL,	2, 0);
563 	TEST_STRTOLL("011",	8,	9LL,	3, 0);
564 	TEST_STRTOLL("011",	0,	9LL,	3, 0);
565 	TEST_STRTOLL("-11",	8,	-9LL,	3, 0);
566 	TEST_STRTOLL("-011",	8,	-9LL,	4, 0);
567 	TEST_STRTOLL("-011",	0,	-9LL,	4, 0);
568 
569 	TEST_STRTOLL("011",	8,	9LL,	3, 0);
570 	TEST_STRTOLL("011",	0,	9LL,	3, 0);
571 	TEST_STRTOLL("-11",	8,	-9LL,	3, 0);
572 	TEST_STRTOLL("-011",	8,	-9LL,	4, 0);
573 	TEST_STRTOLL("-011",	0,	-9LL,	4, 0);
574 
575 	TEST_STRTOLL("Text",	0,	0LL,	0, 0);
576 
577 	TEST_STRTOLL("9223372036854775807",	10,	9223372036854775807LL,	19, 0);
578 	TEST_STRTOLL("9223372036854775807",	0,	9223372036854775807LL,	19, 0);
579 	TEST_STRTOLL("9223372036854775808",	0,	9223372036854775807LL,	19, ERANGE);
580 	TEST_STRTOLL("9223372036854775808",	10,	9223372036854775807LL,	19, ERANGE);
581 	TEST_STRTOLL("0x7FFFFFFFFFFFFFFF",	0,	9223372036854775807LL,	18, 0);
582 	TEST_STRTOLL("0x7FFFFFFFFFFFFFFF",	16,	9223372036854775807LL,	18, 0);
583 	TEST_STRTOLL("7FFFFFFFFFFFFFFF",	16,	9223372036854775807LL,	16, 0);
584 	TEST_STRTOLL("0x8000000000000000",	0,	9223372036854775807LL,	18, ERANGE);
585 	TEST_STRTOLL("0x8000000000000000",	16,	9223372036854775807LL,	18, ERANGE);
586 	TEST_STRTOLL("80000000000000000",	16,	9223372036854775807LL,	17, ERANGE);
587 	TEST_STRTOLL("0777777777777777777777",	0,	9223372036854775807LL,	22, 0);
588 	TEST_STRTOLL("0777777777777777777777",	8,	9223372036854775807LL,	22, 0);
589 	TEST_STRTOLL("777777777777777777777",	8,	9223372036854775807LL,	21, 0);
590 	TEST_STRTOLL("01000000000000000000000",	0,	9223372036854775807LL,	23, ERANGE);
591 	TEST_STRTOLL("01000000000000000000000",	8,	9223372036854775807LL,	23, ERANGE);
592 	TEST_STRTOLL("1000000000000000000000",	8,	9223372036854775807LL,	22, ERANGE);
593 
594 	TEST_STRTOLL("-9223372036854775808",	10,	-9223372036854775807LL -1,	20, 0);
595 	TEST_STRTOLL("-9223372036854775808",	0,	-9223372036854775807LL -1,	20, 0);
596 	TEST_STRTOLL("-9223372036854775809",	0,	-9223372036854775807LL -1,	20, ERANGE);
597 	TEST_STRTOLL("-9223372036854775809",	10,	-9223372036854775807LL -1,	20, ERANGE);
598 	TEST_STRTOLL("-0x8000000000000000",	0,	-9223372036854775807LL -1,	19, 0);
599 	TEST_STRTOLL("-0x8000000000000000",	16,	-9223372036854775807LL -1,	19, 0);
600 	TEST_STRTOLL("-8000000000000000",	16,	-9223372036854775807LL -1,	17, 0);
601 	TEST_STRTOLL("-0x8000000000000001",	0,	-9223372036854775807LL -1,	19, ERANGE);
602 	TEST_STRTOLL("-0x8000000000000001",	16,	-9223372036854775807LL -1,	19, ERANGE);
603 	TEST_STRTOLL("-80000000000000001",	16,	-9223372036854775807LL -1,	18, ERANGE);
604 	TEST_STRTOLL("-01000000000000000000000",0,	-9223372036854775807LL -1,	24, 0);
605 	TEST_STRTOLL("-01000000000000000000000",8,	-9223372036854775807LL -1,	24, 0);
606 	TEST_STRTOLL("-1000000000000000000000",	8,	-9223372036854775807LL -1,	23, 0);
607 	TEST_STRTOLL("-01000000000000000000001",0,	-9223372036854775807LL -1,	24, ERANGE);
608 	TEST_STRTOLL("-01000000000000000000001",8,	-9223372036854775807LL -1,	24, ERANGE);
609 	TEST_STRTOLL("-1000000000000000000001",	8,	-9223372036854775807LL -1,	23, ERANGE);
610 
611 	printf("success: strtoll\n");
612 	return true;
613 }
614 
test_strtoull(void)615 static int test_strtoull(void)
616 {
617 	printf("test: strtoull\n");
618 
619 #define TEST_STRTOULL(str,base,res,diff,errnoo) TEST_STRTO_X(long long unsigned int,"%llu",strtoull,str,base,res,diff,errnoo)
620 
621 	TEST_STRTOULL("15",	10,	15LLU,	2, 0);
622 	TEST_STRTOULL("  15",	10,	15LLU,	4, 0);
623 	TEST_STRTOULL("15",	0,	15LLU,	2, 0);
624 	TEST_STRTOULL(" 15 ",	0,	15LLU,	3, 0);
625 	TEST_STRTOULL("+15",	10,	15LLU,	3, 0);
626 	TEST_STRTOULL("  +15",	10,	15LLU,	5, 0);
627 	TEST_STRTOULL("+15",	0,	15LLU,	3, 0);
628 	TEST_STRTOULL(" +15 ",	0,	15LLU,	4, 0);
629 	TEST_STRTOULL("-15",	10,	18446744073709551601LLU,	3, 0);
630 	TEST_STRTOULL("  -15",	10,	18446744073709551601LLU,	5, 0);
631 	TEST_STRTOULL("-15",	0,	18446744073709551601LLU,	3, 0);
632 	TEST_STRTOULL(" -15 ",	0,	18446744073709551601LLU,	4, 0);
633 	TEST_STRTOULL("015",	10,	15LLU,	3, 0);
634 	TEST_STRTOULL("  015",	10,	15LLU,	5, 0);
635 	TEST_STRTOULL("015",	0,	13LLU,	3, 0);
636 	TEST_STRTOULL("  015",	0,	13LLU,	5, 0);
637 	TEST_STRTOULL("0x15",	10,	0LLU,	1, 0);
638 	TEST_STRTOULL("  0x15",	10,	0LLU,	3, 0);
639 	TEST_STRTOULL("0x15",	0,	21LLU,	4, 0);
640 	TEST_STRTOULL("  0x15",	0,	21LLU,	6, 0);
641 
642 	TEST_STRTOULL("10",	16,	16LLU,	2, 0);
643 	TEST_STRTOULL("  10 ",	16,	16LLU,	4, 0);
644 	TEST_STRTOULL("0x10",	16,	16LLU,	4, 0);
645 	TEST_STRTOULL("0x10",	0,	16LLU,	4, 0);
646 	TEST_STRTOULL(" 0x10 ",	0,	16LLU,	5, 0);
647 	TEST_STRTOULL("+10",	16,	16LLU,	3, 0);
648 	TEST_STRTOULL("  +10 ",	16,	16LLU,	5, 0);
649 	TEST_STRTOULL("+0x10",	16,	16LLU,	5, 0);
650 	TEST_STRTOULL("+0x10",	0,	16LLU,	5, 0);
651 	TEST_STRTOULL(" +0x10 ",	0,	16LLU,	6, 0);
652 	TEST_STRTOULL("-10",	16,	-16LLU,	3, 0);
653 	TEST_STRTOULL("  -10 ",	16,	-16LLU,	5, 0);
654 	TEST_STRTOULL("-0x10",	16,	-16LLU,	5, 0);
655 	TEST_STRTOULL("-0x10",	0,	-16LLU,	5, 0);
656 	TEST_STRTOULL(" -0x10 ",	0,	-16LLU,	6, 0);
657 	TEST_STRTOULL("010",	16,	16LLU,	3, 0);
658 	TEST_STRTOULL("  010 ",	16,	16LLU,	5, 0);
659 	TEST_STRTOULL("-010",	16,	-16LLU,	4, 0);
660 
661 	TEST_STRTOULL("11",	8,	9LLU,	2, 0);
662 	TEST_STRTOULL("011",	8,	9LLU,	3, 0);
663 	TEST_STRTOULL("011",	0,	9LLU,	3, 0);
664 	TEST_STRTOULL("-11",	8,	-9LLU,	3, 0);
665 	TEST_STRTOULL("-011",	8,	-9LLU,	4, 0);
666 	TEST_STRTOULL("-011",	0,	-9LLU,	4, 0);
667 
668 	TEST_STRTOULL("011",	8,	9LLU,	3, 0);
669 	TEST_STRTOULL("011",	0,	9LLU,	3, 0);
670 	TEST_STRTOULL("-11",	8,	-9LLU,	3, 0);
671 	TEST_STRTOULL("-011",	8,	-9LLU,	4, 0);
672 	TEST_STRTOULL("-011",	0,	-9LLU,	4, 0);
673 
674 	TEST_STRTOULL("Text",	0,	0LLU,	0, 0);
675 
676 	TEST_STRTOULL("9223372036854775807",	10,	9223372036854775807LLU,	19, 0);
677 	TEST_STRTOULL("9223372036854775807",	0,	9223372036854775807LLU,	19, 0);
678 	TEST_STRTOULL("9223372036854775808",	0,	9223372036854775808LLU,	19, 0);
679 	TEST_STRTOULL("9223372036854775808",	10,	9223372036854775808LLU,	19, 0);
680 	TEST_STRTOULL("0x7FFFFFFFFFFFFFFF",	0,	9223372036854775807LLU,	18, 0);
681 	TEST_STRTOULL("0x7FFFFFFFFFFFFFFF",	16,	9223372036854775807LLU,	18, 0);
682 	TEST_STRTOULL("7FFFFFFFFFFFFFFF",	16,	9223372036854775807LLU,	16, 0);
683 	TEST_STRTOULL("0x8000000000000000",	0,	9223372036854775808LLU,	18, 0);
684 	TEST_STRTOULL("0x8000000000000000",	16,	9223372036854775808LLU,	18, 0);
685 	TEST_STRTOULL("8000000000000000",	16,	9223372036854775808LLU,	16, 0);
686 	TEST_STRTOULL("0777777777777777777777",	0,	9223372036854775807LLU,	22, 0);
687 	TEST_STRTOULL("0777777777777777777777",	8,	9223372036854775807LLU,	22, 0);
688 	TEST_STRTOULL("777777777777777777777",	8,	9223372036854775807LLU,	21, 0);
689 	TEST_STRTOULL("01000000000000000000000",0,	9223372036854775808LLU,	23, 0);
690 	TEST_STRTOULL("01000000000000000000000",8,	9223372036854775808LLU,	23, 0);
691 	TEST_STRTOULL("1000000000000000000000",	8,	9223372036854775808LLU,	22, 0);
692 
693 	TEST_STRTOULL("-9223372036854775808",	10,	9223372036854775808LLU,	20, 0);
694 	TEST_STRTOULL("-9223372036854775808",	0,	9223372036854775808LLU,	20, 0);
695 	TEST_STRTOULL("-9223372036854775809",	0,	9223372036854775807LLU,	20, 0);
696 	TEST_STRTOULL("-9223372036854775809",	10,	9223372036854775807LLU,	20, 0);
697 	TEST_STRTOULL("-0x8000000000000000",	0,	9223372036854775808LLU,	19, 0);
698 	TEST_STRTOULL("-0x8000000000000000",	16,	9223372036854775808LLU,	19, 0);
699 	TEST_STRTOULL("-8000000000000000",	16,	9223372036854775808LLU,	17, 0);
700 	TEST_STRTOULL("-0x8000000000000001",	0,	9223372036854775807LLU,	19, 0);
701 	TEST_STRTOULL("-0x8000000000000001",	16,	9223372036854775807LLU,	19, 0);
702 	TEST_STRTOULL("-8000000000000001",	16,	9223372036854775807LLU,	17, 0);
703 	TEST_STRTOULL("-01000000000000000000000",0,	9223372036854775808LLU,	24, 0);
704 	TEST_STRTOULL("-01000000000000000000000",8,	9223372036854775808LLU,	24, 0);
705 	TEST_STRTOULL("-1000000000000000000000",8,	9223372036854775808LLU,	23, 0);
706 	TEST_STRTOULL("-01000000000000000000001",0,	9223372036854775807LLU,	24, 0);
707 	TEST_STRTOULL("-01000000000000000000001",8,	9223372036854775807LLU,	24, 0);
708 	TEST_STRTOULL("-1000000000000000000001",8,	9223372036854775807LLU,	23, 0);
709 
710 	TEST_STRTOULL("18446744073709551615",	0,	18446744073709551615LLU,	20, 0);
711 	TEST_STRTOULL("18446744073709551615",	10,	18446744073709551615LLU,	20, 0);
712 	TEST_STRTOULL("18446744073709551616",	0,	18446744073709551615LLU,	20, ERANGE);
713 	TEST_STRTOULL("18446744073709551616",	10,	18446744073709551615LLU,	20, ERANGE);
714 	TEST_STRTOULL("0xFFFFFFFFFFFFFFFF",	0,	18446744073709551615LLU,	18, 0);
715 	TEST_STRTOULL("0xFFFFFFFFFFFFFFFF",	16,	18446744073709551615LLU,	18, 0);
716 	TEST_STRTOULL("FFFFFFFFFFFFFFFF",	16,	18446744073709551615LLU,	16, 0);
717 	TEST_STRTOULL("0x10000000000000000",	0,	18446744073709551615LLU,	19, ERANGE);
718 	TEST_STRTOULL("0x10000000000000000",	16,	18446744073709551615LLU,	19, ERANGE);
719 	TEST_STRTOULL("10000000000000000",	16,	18446744073709551615LLU,	17, ERANGE);
720 	TEST_STRTOULL("01777777777777777777777",0,	18446744073709551615LLU,	23, 0);
721 	TEST_STRTOULL("01777777777777777777777",8,	18446744073709551615LLU,	23, 0);
722 	TEST_STRTOULL("1777777777777777777777",	8,	18446744073709551615LLU,	22, 0);
723 	TEST_STRTOULL("02000000000000000000000",0,	18446744073709551615LLU,	23, ERANGE);
724 	TEST_STRTOULL("02000000000000000000000",8,	18446744073709551615LLU,	23, ERANGE);
725 	TEST_STRTOULL("2000000000000000000000",	8,	18446744073709551615LLU,	22, ERANGE);
726 
727 	TEST_STRTOULL("-18446744073709551615",	0,	1LLU,				21, 0);
728 	TEST_STRTOULL("-18446744073709551615",	10,	1LLU,				21, 0);
729 	TEST_STRTOULL("-18446744073709551616",	0,	18446744073709551615LLU,	21, ERANGE);
730 	TEST_STRTOULL("-18446744073709551616",	10,	18446744073709551615LLU,	21, ERANGE);
731 	TEST_STRTOULL("-0xFFFFFFFFFFFFFFFF",	0,	1LLU,				19, 0);
732 	TEST_STRTOULL("-0xFFFFFFFFFFFFFFFF",	16,	1LLU,				19, 0);
733 	TEST_STRTOULL("-FFFFFFFFFFFFFFFF",	16,	1LLU,				17, 0);
734 	TEST_STRTOULL("-0x10000000000000000",	0,	18446744073709551615LLU,	20, ERANGE);
735 	TEST_STRTOULL("-0x10000000000000000",	16,	18446744073709551615LLU,	20, ERANGE);
736 	TEST_STRTOULL("-10000000000000000",	16,	18446744073709551615LLU,	18, ERANGE);
737 	TEST_STRTOULL("-01777777777777777777777",0,	1LLU,				24, 0);
738 	TEST_STRTOULL("-01777777777777777777777",8,	1LLU,				24, 0);
739 	TEST_STRTOULL("-1777777777777777777777",8,	1LLU,				23, 0);
740 	TEST_STRTOULL("-02000000000000000000000",0,	18446744073709551615LLU,	24, ERANGE);
741 	TEST_STRTOULL("-02000000000000000000000",8,	18446744073709551615LLU,	24, ERANGE);
742 	TEST_STRTOULL("-2000000000000000000000",8,	18446744073709551615LLU,	23, ERANGE);
743 
744 	printf("success: strtoull\n");
745 	return true;
746 }
747 
748 /*
749 FIXME:
750 Types:
751 bool
752 socklen_t
753 uint{8,16,32,64}_t
754 int{8,16,32,64}_t
755 intptr_t
756 
757 Constants:
758 PATH_NAME_MAX
759 UINT{16,32,64}_MAX
760 INT32_MAX
761 */
762 
test_va_copy(void)763 static int test_va_copy(void)
764 {
765 	/* FIXME */
766 	return true;
767 }
768 
test_FUNCTION(void)769 static int test_FUNCTION(void)
770 {
771 	printf("test: FUNCTION\n");
772 	if (strcmp(__FUNCTION__, "test_FUNCTION") != 0) {
773 		printf("failure: FUNCTION [\nFUNCTION invalid\n]\n");
774 		return false;
775 	}
776 	printf("success: FUNCTION\n");
777 	return true;
778 }
779 
test_MIN(void)780 static int test_MIN(void)
781 {
782 	printf("test: MIN\n");
783 	if (MIN(20, 1) != 1) {
784 		printf("failure: MIN [\nMIN invalid\n]\n");
785 		return false;
786 	}
787 	if (MIN(1, 20) != 1) {
788 		printf("failure: MIN [\nMIN invalid\n]\n");
789 		return false;
790 	}
791 	printf("success: MIN\n");
792 	return true;
793 }
794 
test_MAX(void)795 static int test_MAX(void)
796 {
797 	printf("test: MAX\n");
798 	if (MAX(20, 1) != 20) {
799 		printf("failure: MAX [\nMAX invalid\n]\n");
800 		return false;
801 	}
802 	if (MAX(1, 20) != 20) {
803 		printf("failure: MAX [\nMAX invalid\n]\n");
804 		return false;
805 	}
806 	printf("success: MAX\n");
807 	return true;
808 }
809 
test_socketpair(void)810 static int test_socketpair(void)
811 {
812 	int sock[2];
813 	char buf[20];
814 
815 	printf("test: socketpair\n");
816 
817 	if (socketpair(AF_UNIX, SOCK_STREAM, 0, sock) == -1) {
818 		printf("failure: socketpair [\n"
819 			   "socketpair() failed\n"
820 			   "]\n");
821 		return false;
822 	}
823 
824 	if (write(sock[1], "automatisch", 12) == -1) {
825 		printf("failure: socketpair [\n"
826 			   "write() failed: %s\n"
827 			   "]\n", strerror(errno));
828 		return false;
829 	}
830 
831 	if (read(sock[0], buf, 12) == -1) {
832 		printf("failure: socketpair [\n"
833 			   "read() failed: %s\n"
834 			   "]\n", strerror(errno));
835 		return false;
836 	}
837 
838 	if (strcmp(buf, "automatisch") != 0) {
839 		printf("failure: socketpair [\n"
840 			   "expected: automatisch, got: %s\n"
841 			   "]\n", buf);
842 		return false;
843 	}
844 
845 	printf("success: socketpair\n");
846 
847 	return true;
848 }
849 
850 extern int libreplace_test_strptime(void);
851 
test_strptime(void)852 static int test_strptime(void)
853 {
854 	return libreplace_test_strptime();
855 }
856 
857 extern int getifaddrs_test(void);
858 
test_getifaddrs(void)859 static int test_getifaddrs(void)
860 {
861 
862 	printf("test: getifaddrs\n");
863 
864 	if (getifaddrs_test() != 0) {
865 		printf("failure: getifaddrs\n");
866 		return false;
867 	}
868 
869 	printf("success: getifaddrs\n");
870 	return true;
871 }
872 
test_utime(void)873 static int test_utime(void)
874 {
875 	struct utimbuf u;
876 	struct stat st1, st2, st3;
877 	int fd;
878 
879 	printf("test: utime\n");
880 	unlink(TESTFILE);
881 
882 	fd = open(TESTFILE, O_RDWR|O_CREAT, 0600);
883 	if (fd == -1) {
884 		printf("failure: utime [\n"
885 		       "creating '%s' failed - %s\n]\n",
886 		       TESTFILE, strerror(errno));
887 		return false;
888 	}
889 
890 	if (fstat(fd, &st1) != 0) {
891 		printf("failure: utime [\n"
892 		       "fstat (1) failed - %s\n]\n",
893 		       strerror(errno));
894 		close(fd);
895 		return false;
896 	}
897 
898 	u.actime = st1.st_atime + 300;
899 	u.modtime = st1.st_mtime - 300;
900 	if (utime(TESTFILE, &u) != 0) {
901 		printf("failure: utime [\n"
902 		       "utime(&u) failed - %s\n]\n",
903 		       strerror(errno));
904 		close(fd);
905 		return false;
906 	}
907 
908 	if (fstat(fd, &st2) != 0) {
909 		printf("failure: utime [\n"
910 		       "fstat (2) failed - %s\n]\n",
911 		       strerror(errno));
912 		close(fd);
913 		return false;
914 	}
915 
916 	if (utime(TESTFILE, NULL) != 0) {
917 		printf("failure: utime [\n"
918 		       "utime(NULL) failed - %s\n]\n",
919 		       strerror(errno));
920 		close(fd);
921 		return false;
922 	}
923 
924 	if (fstat(fd, &st3) != 0) {
925 		printf("failure: utime [\n"
926 		       "fstat (3) failed - %s\n]\n",
927 		       strerror(errno));
928 		close(fd);
929 		return false;
930 	}
931 
932 #define CMP_VAL(a,c,b) do { \
933 	if (a c b) { \
934 		printf("failure: utime [\n" \
935 		       "%s: %s(%d) %s %s(%d)\n]\n", \
936 		       __location__, \
937 		       #a, (int)a, #c, #b, (int)b); \
938 		close(fd); \
939 		return false; \
940 	} \
941 } while(0)
942 #define EQUAL_VAL(a,b) CMP_VAL(a,!=,b)
943 #define GREATER_VAL(a,b) CMP_VAL(a,<=,b)
944 #define LESSER_VAL(a,b) CMP_VAL(a,>=,b)
945 
946 	EQUAL_VAL(st2.st_atime, st1.st_atime + 300);
947 	EQUAL_VAL(st2.st_mtime, st1.st_mtime - 300);
948 	LESSER_VAL(st3.st_atime, st2.st_atime);
949 	GREATER_VAL(st3.st_mtime, st2.st_mtime);
950 
951 #undef CMP_VAL
952 #undef EQUAL_VAL
953 #undef GREATER_VAL
954 #undef LESSER_VAL
955 
956 	unlink(TESTFILE);
957 	printf("success: utime\n");
958 	close(fd);
959 	return true;
960 }
961 
test_utimes(void)962 static int test_utimes(void)
963 {
964 	struct timeval tv[2];
965 	struct stat st1, st2;
966 	int fd;
967 
968 	printf("test: utimes\n");
969 	unlink(TESTFILE);
970 
971 	fd = open(TESTFILE, O_RDWR|O_CREAT, 0600);
972 	if (fd == -1) {
973 		printf("failure: utimes [\n"
974 		       "creating '%s' failed - %s\n]\n",
975 		       TESTFILE, strerror(errno));
976 		return false;
977 	}
978 
979 	if (fstat(fd, &st1) != 0) {
980 		printf("failure: utimes [\n"
981 		       "fstat (1) failed - %s\n]\n",
982 		       strerror(errno));
983 		close(fd);
984 		return false;
985 	}
986 
987 	ZERO_STRUCT(tv);
988 	tv[0].tv_sec = st1.st_atime + 300;
989 	tv[1].tv_sec = st1.st_mtime - 300;
990 	if (utimes(TESTFILE, tv) != 0) {
991 		printf("failure: utimes [\n"
992 		       "utimes(tv) failed - %s\n]\n",
993 		       strerror(errno));
994 		close(fd);
995 		return false;
996 	}
997 
998 	if (fstat(fd, &st2) != 0) {
999 		printf("failure: utimes [\n"
1000 		       "fstat (2) failed - %s\n]\n",
1001 		       strerror(errno));
1002 		close(fd);
1003 		return false;
1004 	}
1005 
1006 #define EQUAL_VAL(a,b) do { \
1007 	if (a != b) { \
1008 		printf("failure: utimes [\n" \
1009 		       "%s: %s(%d) != %s(%d)\n]\n", \
1010 		       __location__, \
1011 		       #a, (int)a, #b, (int)b); \
1012 		close(fd); \
1013 		return false; \
1014 	} \
1015 } while(0)
1016 
1017 	EQUAL_VAL(st2.st_atime, st1.st_atime + 300);
1018 	EQUAL_VAL(st2.st_mtime, st1.st_mtime - 300);
1019 
1020 #undef EQUAL_VAL
1021 
1022 	unlink(TESTFILE);
1023 	printf("success: utimes\n");
1024 	close(fd);
1025 	return true;
1026 }
1027 
test_memmem(void)1028 static int test_memmem(void)
1029 {
1030 	char *s;
1031 
1032 	printf("test: memmem\n");
1033 
1034 	s = (char *)memmem("foo", 3, "fo", 2);
1035 	if (strcmp(s, "foo") != 0) {
1036 		printf(__location__ ": Failed memmem\n");
1037 		return false;
1038 	}
1039 
1040 	s = (char *)memmem("foo", 3, "", 0);
1041 	/* it is allowable for this to return NULL (as happens on
1042 	   FreeBSD) */
1043 	if (s && strcmp(s, "foo") != 0) {
1044 		printf(__location__ ": Failed memmem\n");
1045 		return false;
1046 	}
1047 
1048 	s = (char *)memmem("foo", 4, "o", 1);
1049 	if (strcmp(s, "oo") != 0) {
1050 		printf(__location__ ": Failed memmem\n");
1051 		return false;
1052 	}
1053 
1054 	s = (char *)memmem("foobarfodx", 11, "fod", 3);
1055 	if (strcmp(s, "fodx") != 0) {
1056 		printf(__location__ ": Failed memmem\n");
1057 		return false;
1058 	}
1059 
1060 	printf("success: memmem\n");
1061 
1062 	return true;
1063 }
1064 
test_closefrom(void)1065 static bool test_closefrom(void)
1066 {
1067 	int i, fd;
1068 
1069 	for (i=0; i<100; i++) {
1070 		fd = dup(0);
1071 		if (fd == -1) {
1072 			perror("dup failed");
1073 			return false;
1074 		}
1075 
1076 		/* 1000 is just an arbitrarily chosen upper bound */
1077 
1078 		if (fd >= 1000) {
1079 			printf("fd=%d\n", fd);
1080 			return false;
1081 		}
1082 	}
1083 
1084 	closefrom(3);
1085 
1086 	for (i=3; i<=fd; i++) {
1087 		off_t off;
1088 		off = lseek(i, 0, SEEK_CUR);
1089 		if ((off != (off_t)-1) || (errno != EBADF)) {
1090 			printf("fd %d not closed\n", i);
1091 			return false;
1092 		}
1093 	}
1094 
1095 	return true;
1096 }
1097 
torture_local_replace(struct torture_context * ctx)1098 bool torture_local_replace(struct torture_context *ctx)
1099 {
1100 	bool ret = true;
1101 	ret &= test_ftruncate();
1102 	ret &= test_strlcpy();
1103 	ret &= test_strlcat();
1104 	ret &= test_mktime();
1105 	ret &= test_initgroups();
1106 	ret &= test_memmove();
1107 	ret &= test_strdup();
1108 	ret &= test_setlinebuf();
1109 	ret &= test_vsyslog();
1110 	ret &= test_timegm();
1111 	ret &= test_setenv();
1112 	ret &= test_strndup();
1113 	ret &= test_strnlen();
1114 	ret &= test_waitpid();
1115 	ret &= test_seteuid();
1116 	ret &= test_setegid();
1117 	ret &= test_asprintf();
1118 	ret &= test_snprintf();
1119 	ret &= test_vasprintf();
1120 	ret &= test_vsnprintf();
1121 	ret &= test_opendir();
1122 	ret &= test_readdir();
1123 	ret &= test_telldir();
1124 	ret &= test_seekdir();
1125 	ret &= test_dlopen();
1126 	ret &= test_chroot();
1127 	ret &= test_bzero();
1128 	ret &= test_strerror();
1129 	ret &= test_errno();
1130 	ret &= test_mkdtemp();
1131 	ret &= test_mkstemp();
1132 	ret &= test_pread();
1133 	ret &= test_pwrite();
1134 	ret &= test_inet_ntoa();
1135 	ret &= test_strtoll();
1136 	ret &= test_strtoull();
1137 	ret &= test_va_copy();
1138 	ret &= test_FUNCTION();
1139 	ret &= test_MIN();
1140 	ret &= test_MAX();
1141 	ret &= test_socketpair();
1142 	ret &= test_strptime();
1143 	ret &= test_getifaddrs();
1144 	ret &= test_utime();
1145 	ret &= test_utimes();
1146 	ret &= test_memmem();
1147 	ret &= test_closefrom();
1148 
1149 	return ret;
1150 }
1151