s3: smbd: rename_internals_fsp() has to reopen the parent directory of the target...
[Samba.git] / lib / replace / tests / testsuite.c
blobc0dcda55bd2d106c8515d46280aac6ed02f62375
1 /*
2 Unix SMB/CIFS implementation.
4 libreplace tests
6 Copyright (C) Jelmer Vernooij 2006
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
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.
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.
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/>.
26 #include "replace.h"
27 #include "replace-test.h"
28 #include "replace-testsuite.h"
31 we include all the system/ include files here so that libreplace tests
32 them in the build farm
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"
50 #define TESTFILE "testfile.dat"
54 test ftruncate() function
56 static int test_ftruncate(void)
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;
69 if (ftruncate(fd, size) != 0) {
70 printf("failure: ftruncate [\n%s\n]\n", strerror(errno));
71 close(fd);
72 return false;
74 if (fstat(fd, &st) != 0) {
75 printf("failure: ftruncate [\nfstat failed - %s\n]\n", strerror(errno));
76 close(fd);
77 return false;
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;
85 unlink(TESTFILE);
86 printf("success: ftruncate\n");
87 close(fd);
88 return true;
92 test strlcpy() function.
93 see http://www.gratisoft.us/todd/papers/strlcpy.html
95 static int test_strlcpy(void)
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 }
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;
116 printf("success: strlcpy\n");
117 return true;
120 static int test_strlcat(void)
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;
129 if (strcmp(tmp, "bl") != 0) {
130 printf("failure: strlcat [\nexpected \"bl\", got \"%s\"\n]\n",
131 tmp);
132 return false;
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;
142 printf("success: strlcat\n");
143 return true;
146 static int test_mktime(void)
148 /* FIXME */
149 return true;
152 static int test_initgroups(void)
154 /* FIXME */
155 return true;
158 static int test_memmove(void)
160 /* FIXME */
161 return true;
164 static int test_strdup(void)
166 char *x;
167 int cmp;
169 printf("test: strdup\n");
170 x = strdup("bla");
172 cmp = strcmp("bla", x);
173 if (cmp != 0) {
174 printf("failure: strdup [\nfailed: expected \"bla\", got \"%s\"\n]\n",
176 free(x);
177 return false;
179 free(x);
180 printf("success: strdup\n");
181 return true;
184 static int test_setlinebuf(void)
186 printf("test: setlinebuf\n");
187 setlinebuf(stdout);
188 printf("success: setlinebuf\n");
189 return true;
192 static int test_vsyslog(void)
194 /* FIXME */
195 return true;
198 static int test_timegm(void)
200 /* FIXME */
201 return true;
204 static int test_setenv(void)
206 #define TEST_SETENV(key, value, overwrite, result) do { \
207 int _ret; \
208 char *_v; \
209 _ret = setenv(key, value, overwrite); \
210 if (_ret != 0) { \
211 printf("failure: setenv [\n" \
212 "setenv(%s, %s, %d) failed\n" \
213 "]\n", \
214 key, value, overwrite); \
215 return false; \
217 _v=getenv(key); \
218 if (!_v) { \
219 printf("failure: setenv [\n" \
220 "getenv(%s) returned NULL\n" \
221 "]\n", \
222 key); \
223 return false; \
225 if (strcmp(result, _v) != 0) { \
226 printf("failure: setenv [\n" \
227 "getenv(%s): '%s' != '%s'\n" \
228 "]\n", \
229 key, result, _v); \
230 return false; \
232 } while(0)
234 #define TEST_UNSETENV(key) do { \
235 char *_v; \
236 unsetenv(key); \
237 _v=getenv(key); \
238 if (_v) { \
239 printf("failure: setenv [\n" \
240 "getenv(%s): NULL != '%s'\n" \
241 "]\n", \
242 SETENVTEST_KEY, _v); \
243 return false; \
245 } while (0)
247 #define SETENVTEST_KEY "SETENVTESTKEY"
248 #define SETENVTEST_VAL "SETENVTESTVAL"
250 printf("test: setenv\n");
251 TEST_SETENV(SETENVTEST_KEY, SETENVTEST_VAL"1", 0, SETENVTEST_VAL"1");
252 TEST_SETENV(SETENVTEST_KEY, SETENVTEST_VAL"2", 0, SETENVTEST_VAL"1");
253 TEST_SETENV(SETENVTEST_KEY, SETENVTEST_VAL"3", 1, SETENVTEST_VAL"3");
254 TEST_SETENV(SETENVTEST_KEY, SETENVTEST_VAL"4", 1, SETENVTEST_VAL"4");
255 TEST_UNSETENV(SETENVTEST_KEY);
256 TEST_UNSETENV(SETENVTEST_KEY);
257 TEST_SETENV(SETENVTEST_KEY, SETENVTEST_VAL"5", 0, SETENVTEST_VAL"5");
258 TEST_UNSETENV(SETENVTEST_KEY);
259 TEST_UNSETENV(SETENVTEST_KEY);
260 printf("success: setenv\n");
261 return true;
264 static int test_strndup(void)
266 char *x;
267 int cmp;
269 printf("test: strndup\n");
270 x = strndup("bla", 0);
271 cmp = strcmp(x, "");
272 free(x);
273 if (cmp != 0) {
274 printf("failure: strndup [\ninvalid\n]\n");
275 return false;
278 x = strndup("bla", 2);
279 cmp = strcmp(x, "bl");
280 free(x);
281 if (cmp != 0) {
282 printf("failure: strndup [\ninvalid\n]\n");
283 return false;
286 #ifdef __GNUC__
287 # if __GNUC__ < 11
289 * This code will not compile with gcc11 -O3 anymore.
291 * error: ‘strndup’ specified bound 10 exceeds source size 4 [-Werror=stringop-overread]
292 * x = strndup("bla", 10);
293 * ^~~~~~~~~~~~~~~~~~
295 x = strndup("bla", 10);
296 cmp = strcmp(x, "bla");
297 free(x);
298 if (cmp != 0) {
299 printf("failure: strndup [\ninvalid\n]\n");
300 return false;
302 # endif
303 #endif /* __GNUC__ */
305 printf("success: strndup\n");
306 return true;
309 static int test_strnlen(void)
311 char longlen[20] = { 0 };
313 printf("test: strnlen\n");
314 if (strnlen("bla", 2) != 2) {
315 printf("failure: strnlen [\nunexpected length\n]\n");
316 return false;
319 if (strnlen("some text\n", 0) != 0) {
320 printf("failure: strnlen [\nunexpected length\n]\n");
321 return false;
324 memcpy(longlen, "some text", 10);
326 if (strnlen(longlen, 20) != 9) {
327 printf("failure: strnlen [\nunexpected length\n]\n");
328 return false;
331 printf("success: strnlen\n");
332 return true;
335 static int test_waitpid(void)
337 /* FIXME */
338 return true;
341 static int test_seteuid(void)
343 /* FIXME */
344 return true;
347 static int test_setegid(void)
349 /* FIXME */
350 return true;
353 static int test_asprintf(void)
355 char *x = NULL;
357 printf("test: asprintf\n");
358 if (asprintf(&x, "%d", 9) != 1) {
359 printf("failure: asprintf [\ngenerate asprintf\n]\n");
360 free(x);
361 return false;
363 if (strcmp(x, "9") != 0) {
364 printf("failure: asprintf [\ngenerate asprintf\n]\n");
365 free(x);
366 return false;
368 if (asprintf(&x, "dat%s", "a") != 4) {
369 printf("failure: asprintf [\ngenerate asprintf\n]\n");
370 free(x);
371 return false;
373 if (strcmp(x, "data") != 0) {
374 printf("failure: asprintf [\ngenerate asprintf\n]\n");
375 free(x);
376 return false;
378 free(x);
379 printf("success: asprintf\n");
380 return true;
383 static int test_snprintf(void)
385 char tmp[10];
386 printf("test: snprintf\n");
387 if (snprintf(tmp, 3, "foo%d", 9) != 4) {
388 printf("failure: snprintf [\nsnprintf return code failed\n]\n");
389 return false;
392 if (strcmp(tmp, "fo") != 0) {
393 printf("failure: snprintf [\nsnprintf failed\n]\n");
394 return false;
397 printf("success: snprintf\n");
398 return true;
401 static int test_vasprintf(void)
403 /* FIXME */
404 return true;
407 static int test_vsnprintf(void)
409 /* FIXME */
410 return true;
413 static int test_opendir(void)
415 /* FIXME */
416 return true;
419 static int test_readdir(void)
421 printf("test: readdir\n");
422 if (test_readdir_os2_delete() != 0) {
423 return false;
425 printf("success: readdir\n");
426 return true;
429 static int test_telldir(void)
431 /* FIXME */
432 return true;
435 static int test_seekdir(void)
437 /* FIXME */
438 return true;
441 static int test_dlopen(void)
443 /* FIXME: test dlopen, dlsym, dlclose, dlerror */
444 return true;
448 static int test_chroot(void)
450 /* FIXME: chroot() */
451 return true;
454 static int test_bzero(void)
456 /* FIXME: bzero */
457 return true;
460 static int test_strerror(void)
462 /* FIXME */
463 return true;
466 static int test_errno(void)
468 printf("test: errno\n");
469 errno = 3;
470 if (errno != 3) {
471 printf("failure: errno [\nerrno failed\n]\n");
472 return false;
475 printf("success: errno\n");
476 return true;
479 static int test_mkdtemp(void)
481 /* FIXME */
482 return true;
485 static int test_mkstemp(void)
487 /* FIXME */
488 return true;
491 static int test_pread(void)
493 /* FIXME */
494 return true;
497 static int test_pwrite(void)
499 /* FIXME */
500 return true;
503 static int test_inet_ntoa(void)
505 /* FIXME */
506 return true;
509 #define TEST_STRTO_X(type,fmt,func,str,base,res,diff,rrnoo) do {\
510 type _v; \
511 char _s[64]; \
512 char *_p = NULL;\
513 char *_ep = NULL; \
514 strlcpy(_s, str, sizeof(_s));\
515 if (diff >= 0) { \
516 _ep = &_s[diff]; \
518 errno = 0; \
519 _v = func(_s, &_p, base); \
520 if (errno != rrnoo) { \
521 printf("failure: %s [\n" \
522 "\t%s\n" \
523 "\t%s(\"%s\",%d,%d): " fmt " (=/!)= " fmt "\n" \
524 "\terrno: %d != %d\n" \
525 "]\n", \
526 __STRING(func), __location__, __STRING(func), \
527 str, diff, base, res, _v, rrnoo, errno); \
528 return false; \
529 } else if (_v != res) { \
530 printf("failure: %s [\n" \
531 "\t%s\n" \
532 "\t%s(\"%s\",%d,%d): " fmt " != " fmt "\n" \
533 "]\n", \
534 __STRING(func), __location__, __STRING(func), \
535 str, diff, base, res, _v); \
536 return false; \
537 } else if (_p != _ep) { \
538 printf("failure: %s [\n" \
539 "\t%s\n" \
540 "\t%s(\"%s\",%d,%d): " fmt " (=/!)= " fmt "\n" \
541 "\tptr: %p - %p = %d != %d\n" \
542 "]\n", \
543 __STRING(func), __location__, __STRING(func), \
544 str, diff, base, res, _v, _ep, _p, (int)(diff - (_ep - _p)), diff); \
545 return false; \
547 } while (0)
549 static int test_strtoll(void)
551 printf("test: strtoll\n");
553 #define TEST_STRTOLL(str,base,res,diff,errnoo) TEST_STRTO_X(long long int, "%lld", strtoll,str,base,res,diff,errnoo)
555 TEST_STRTOLL("15", 10, 15LL, 2, 0);
556 TEST_STRTOLL(" 15", 10, 15LL, 4, 0);
557 TEST_STRTOLL("15", 0, 15LL, 2, 0);
558 TEST_STRTOLL(" 15 ", 0, 15LL, 3, 0);
559 TEST_STRTOLL("+15", 10, 15LL, 3, 0);
560 TEST_STRTOLL(" +15", 10, 15LL, 5, 0);
561 TEST_STRTOLL("+15", 0, 15LL, 3, 0);
562 TEST_STRTOLL(" +15 ", 0, 15LL, 4, 0);
563 TEST_STRTOLL("-15", 10, -15LL, 3, 0);
564 TEST_STRTOLL(" -15", 10, -15LL, 5, 0);
565 TEST_STRTOLL("-15", 0, -15LL, 3, 0);
566 TEST_STRTOLL(" -15 ", 0, -15LL, 4, 0);
567 TEST_STRTOLL("015", 10, 15LL, 3, 0);
568 TEST_STRTOLL(" 015", 10, 15LL, 5, 0);
569 TEST_STRTOLL("015", 0, 13LL, 3, 0);
570 TEST_STRTOLL(" 015", 0, 13LL, 5, 0);
571 TEST_STRTOLL("0x15", 10, 0LL, 1, 0);
572 TEST_STRTOLL(" 0x15", 10, 0LL, 3, 0);
573 TEST_STRTOLL("0x15", 0, 21LL, 4, 0);
574 TEST_STRTOLL(" 0x15", 0, 21LL, 6, 0);
576 TEST_STRTOLL("10", 16, 16LL, 2, 0);
577 TEST_STRTOLL(" 10 ", 16, 16LL, 4, 0);
578 TEST_STRTOLL("0x10", 16, 16LL, 4, 0);
579 TEST_STRTOLL("0x10", 0, 16LL, 4, 0);
580 TEST_STRTOLL(" 0x10 ", 0, 16LL, 5, 0);
581 TEST_STRTOLL("+10", 16, 16LL, 3, 0);
582 TEST_STRTOLL(" +10 ", 16, 16LL, 5, 0);
583 TEST_STRTOLL("+0x10", 16, 16LL, 5, 0);
584 TEST_STRTOLL("+0x10", 0, 16LL, 5, 0);
585 TEST_STRTOLL(" +0x10 ", 0, 16LL, 6, 0);
586 TEST_STRTOLL("-10", 16, -16LL, 3, 0);
587 TEST_STRTOLL(" -10 ", 16, -16LL, 5, 0);
588 TEST_STRTOLL("-0x10", 16, -16LL, 5, 0);
589 TEST_STRTOLL("-0x10", 0, -16LL, 5, 0);
590 TEST_STRTOLL(" -0x10 ", 0, -16LL, 6, 0);
591 TEST_STRTOLL("010", 16, 16LL, 3, 0);
592 TEST_STRTOLL(" 010 ", 16, 16LL, 5, 0);
593 TEST_STRTOLL("-010", 16, -16LL, 4, 0);
595 TEST_STRTOLL("11", 8, 9LL, 2, 0);
596 TEST_STRTOLL("011", 8, 9LL, 3, 0);
597 TEST_STRTOLL("011", 0, 9LL, 3, 0);
598 TEST_STRTOLL("-11", 8, -9LL, 3, 0);
599 TEST_STRTOLL("-011", 8, -9LL, 4, 0);
600 TEST_STRTOLL("-011", 0, -9LL, 4, 0);
602 TEST_STRTOLL("011", 8, 9LL, 3, 0);
603 TEST_STRTOLL("011", 0, 9LL, 3, 0);
604 TEST_STRTOLL("-11", 8, -9LL, 3, 0);
605 TEST_STRTOLL("-011", 8, -9LL, 4, 0);
606 TEST_STRTOLL("-011", 0, -9LL, 4, 0);
608 TEST_STRTOLL("Text", 0, 0LL, 0, 0);
610 TEST_STRTOLL("9223372036854775807", 10, 9223372036854775807LL, 19, 0);
611 TEST_STRTOLL("9223372036854775807", 0, 9223372036854775807LL, 19, 0);
612 TEST_STRTOLL("9223372036854775808", 0, 9223372036854775807LL, 19, ERANGE);
613 TEST_STRTOLL("9223372036854775808", 10, 9223372036854775807LL, 19, ERANGE);
614 TEST_STRTOLL("0x7FFFFFFFFFFFFFFF", 0, 9223372036854775807LL, 18, 0);
615 TEST_STRTOLL("0x7FFFFFFFFFFFFFFF", 16, 9223372036854775807LL, 18, 0);
616 TEST_STRTOLL("7FFFFFFFFFFFFFFF", 16, 9223372036854775807LL, 16, 0);
617 TEST_STRTOLL("0x8000000000000000", 0, 9223372036854775807LL, 18, ERANGE);
618 TEST_STRTOLL("0x8000000000000000", 16, 9223372036854775807LL, 18, ERANGE);
619 TEST_STRTOLL("80000000000000000", 16, 9223372036854775807LL, 17, ERANGE);
620 TEST_STRTOLL("0777777777777777777777", 0, 9223372036854775807LL, 22, 0);
621 TEST_STRTOLL("0777777777777777777777", 8, 9223372036854775807LL, 22, 0);
622 TEST_STRTOLL("777777777777777777777", 8, 9223372036854775807LL, 21, 0);
623 TEST_STRTOLL("01000000000000000000000", 0, 9223372036854775807LL, 23, ERANGE);
624 TEST_STRTOLL("01000000000000000000000", 8, 9223372036854775807LL, 23, ERANGE);
625 TEST_STRTOLL("1000000000000000000000", 8, 9223372036854775807LL, 22, ERANGE);
627 TEST_STRTOLL("-9223372036854775808", 10, -9223372036854775807LL -1, 20, 0);
628 TEST_STRTOLL("-9223372036854775808", 0, -9223372036854775807LL -1, 20, 0);
629 TEST_STRTOLL("-9223372036854775809", 0, -9223372036854775807LL -1, 20, ERANGE);
630 TEST_STRTOLL("-9223372036854775809", 10, -9223372036854775807LL -1, 20, ERANGE);
631 TEST_STRTOLL("-0x8000000000000000", 0, -9223372036854775807LL -1, 19, 0);
632 TEST_STRTOLL("-0x8000000000000000", 16, -9223372036854775807LL -1, 19, 0);
633 TEST_STRTOLL("-8000000000000000", 16, -9223372036854775807LL -1, 17, 0);
634 TEST_STRTOLL("-0x8000000000000001", 0, -9223372036854775807LL -1, 19, ERANGE);
635 TEST_STRTOLL("-0x8000000000000001", 16, -9223372036854775807LL -1, 19, ERANGE);
636 TEST_STRTOLL("-80000000000000001", 16, -9223372036854775807LL -1, 18, ERANGE);
637 TEST_STRTOLL("-01000000000000000000000",0, -9223372036854775807LL -1, 24, 0);
638 TEST_STRTOLL("-01000000000000000000000",8, -9223372036854775807LL -1, 24, 0);
639 TEST_STRTOLL("-1000000000000000000000", 8, -9223372036854775807LL -1, 23, 0);
640 TEST_STRTOLL("-01000000000000000000001",0, -9223372036854775807LL -1, 24, ERANGE);
641 TEST_STRTOLL("-01000000000000000000001",8, -9223372036854775807LL -1, 24, ERANGE);
642 TEST_STRTOLL("-1000000000000000000001", 8, -9223372036854775807LL -1, 23, ERANGE);
644 printf("success: strtoll\n");
645 return true;
648 static int test_strtoull(void)
650 printf("test: strtoull\n");
652 #define TEST_STRTOULL(str,base,res,diff,errnoo) TEST_STRTO_X(long long unsigned int,"%llu",strtoull,str,base,res,diff,errnoo)
654 TEST_STRTOULL("15", 10, 15LLU, 2, 0);
655 TEST_STRTOULL(" 15", 10, 15LLU, 4, 0);
656 TEST_STRTOULL("15", 0, 15LLU, 2, 0);
657 TEST_STRTOULL(" 15 ", 0, 15LLU, 3, 0);
658 TEST_STRTOULL("+15", 10, 15LLU, 3, 0);
659 TEST_STRTOULL(" +15", 10, 15LLU, 5, 0);
660 TEST_STRTOULL("+15", 0, 15LLU, 3, 0);
661 TEST_STRTOULL(" +15 ", 0, 15LLU, 4, 0);
662 TEST_STRTOULL("-15", 10, 18446744073709551601LLU, 3, 0);
663 TEST_STRTOULL(" -15", 10, 18446744073709551601LLU, 5, 0);
664 TEST_STRTOULL("-15", 0, 18446744073709551601LLU, 3, 0);
665 TEST_STRTOULL(" -15 ", 0, 18446744073709551601LLU, 4, 0);
666 TEST_STRTOULL("015", 10, 15LLU, 3, 0);
667 TEST_STRTOULL(" 015", 10, 15LLU, 5, 0);
668 TEST_STRTOULL("015", 0, 13LLU, 3, 0);
669 TEST_STRTOULL(" 015", 0, 13LLU, 5, 0);
670 TEST_STRTOULL("0x15", 10, 0LLU, 1, 0);
671 TEST_STRTOULL(" 0x15", 10, 0LLU, 3, 0);
672 TEST_STRTOULL("0x15", 0, 21LLU, 4, 0);
673 TEST_STRTOULL(" 0x15", 0, 21LLU, 6, 0);
675 TEST_STRTOULL("10", 16, 16LLU, 2, 0);
676 TEST_STRTOULL(" 10 ", 16, 16LLU, 4, 0);
677 TEST_STRTOULL("0x10", 16, 16LLU, 4, 0);
678 TEST_STRTOULL("0x10", 0, 16LLU, 4, 0);
679 TEST_STRTOULL(" 0x10 ", 0, 16LLU, 5, 0);
680 TEST_STRTOULL("+10", 16, 16LLU, 3, 0);
681 TEST_STRTOULL(" +10 ", 16, 16LLU, 5, 0);
682 TEST_STRTOULL("+0x10", 16, 16LLU, 5, 0);
683 TEST_STRTOULL("+0x10", 0, 16LLU, 5, 0);
684 TEST_STRTOULL(" +0x10 ", 0, 16LLU, 6, 0);
685 TEST_STRTOULL("-10", 16, -16LLU, 3, 0);
686 TEST_STRTOULL(" -10 ", 16, -16LLU, 5, 0);
687 TEST_STRTOULL("-0x10", 16, -16LLU, 5, 0);
688 TEST_STRTOULL("-0x10", 0, -16LLU, 5, 0);
689 TEST_STRTOULL(" -0x10 ", 0, -16LLU, 6, 0);
690 TEST_STRTOULL("010", 16, 16LLU, 3, 0);
691 TEST_STRTOULL(" 010 ", 16, 16LLU, 5, 0);
692 TEST_STRTOULL("-010", 16, -16LLU, 4, 0);
694 TEST_STRTOULL("11", 8, 9LLU, 2, 0);
695 TEST_STRTOULL("011", 8, 9LLU, 3, 0);
696 TEST_STRTOULL("011", 0, 9LLU, 3, 0);
697 TEST_STRTOULL("-11", 8, -9LLU, 3, 0);
698 TEST_STRTOULL("-011", 8, -9LLU, 4, 0);
699 TEST_STRTOULL("-011", 0, -9LLU, 4, 0);
701 TEST_STRTOULL("011", 8, 9LLU, 3, 0);
702 TEST_STRTOULL("011", 0, 9LLU, 3, 0);
703 TEST_STRTOULL("-11", 8, -9LLU, 3, 0);
704 TEST_STRTOULL("-011", 8, -9LLU, 4, 0);
705 TEST_STRTOULL("-011", 0, -9LLU, 4, 0);
707 TEST_STRTOULL("Text", 0, 0LLU, 0, 0);
709 TEST_STRTOULL("9223372036854775807", 10, 9223372036854775807LLU, 19, 0);
710 TEST_STRTOULL("9223372036854775807", 0, 9223372036854775807LLU, 19, 0);
711 TEST_STRTOULL("9223372036854775808", 0, 9223372036854775808LLU, 19, 0);
712 TEST_STRTOULL("9223372036854775808", 10, 9223372036854775808LLU, 19, 0);
713 TEST_STRTOULL("0x7FFFFFFFFFFFFFFF", 0, 9223372036854775807LLU, 18, 0);
714 TEST_STRTOULL("0x7FFFFFFFFFFFFFFF", 16, 9223372036854775807LLU, 18, 0);
715 TEST_STRTOULL("7FFFFFFFFFFFFFFF", 16, 9223372036854775807LLU, 16, 0);
716 TEST_STRTOULL("0x8000000000000000", 0, 9223372036854775808LLU, 18, 0);
717 TEST_STRTOULL("0x8000000000000000", 16, 9223372036854775808LLU, 18, 0);
718 TEST_STRTOULL("8000000000000000", 16, 9223372036854775808LLU, 16, 0);
719 TEST_STRTOULL("0777777777777777777777", 0, 9223372036854775807LLU, 22, 0);
720 TEST_STRTOULL("0777777777777777777777", 8, 9223372036854775807LLU, 22, 0);
721 TEST_STRTOULL("777777777777777777777", 8, 9223372036854775807LLU, 21, 0);
722 TEST_STRTOULL("01000000000000000000000",0, 9223372036854775808LLU, 23, 0);
723 TEST_STRTOULL("01000000000000000000000",8, 9223372036854775808LLU, 23, 0);
724 TEST_STRTOULL("1000000000000000000000", 8, 9223372036854775808LLU, 22, 0);
726 TEST_STRTOULL("-9223372036854775808", 10, 9223372036854775808LLU, 20, 0);
727 TEST_STRTOULL("-9223372036854775808", 0, 9223372036854775808LLU, 20, 0);
728 TEST_STRTOULL("-9223372036854775809", 0, 9223372036854775807LLU, 20, 0);
729 TEST_STRTOULL("-9223372036854775809", 10, 9223372036854775807LLU, 20, 0);
730 TEST_STRTOULL("-0x8000000000000000", 0, 9223372036854775808LLU, 19, 0);
731 TEST_STRTOULL("-0x8000000000000000", 16, 9223372036854775808LLU, 19, 0);
732 TEST_STRTOULL("-8000000000000000", 16, 9223372036854775808LLU, 17, 0);
733 TEST_STRTOULL("-0x8000000000000001", 0, 9223372036854775807LLU, 19, 0);
734 TEST_STRTOULL("-0x8000000000000001", 16, 9223372036854775807LLU, 19, 0);
735 TEST_STRTOULL("-8000000000000001", 16, 9223372036854775807LLU, 17, 0);
736 TEST_STRTOULL("-01000000000000000000000",0, 9223372036854775808LLU, 24, 0);
737 TEST_STRTOULL("-01000000000000000000000",8, 9223372036854775808LLU, 24, 0);
738 TEST_STRTOULL("-1000000000000000000000",8, 9223372036854775808LLU, 23, 0);
739 TEST_STRTOULL("-01000000000000000000001",0, 9223372036854775807LLU, 24, 0);
740 TEST_STRTOULL("-01000000000000000000001",8, 9223372036854775807LLU, 24, 0);
741 TEST_STRTOULL("-1000000000000000000001",8, 9223372036854775807LLU, 23, 0);
743 TEST_STRTOULL("18446744073709551615", 0, 18446744073709551615LLU, 20, 0);
744 TEST_STRTOULL("18446744073709551615", 10, 18446744073709551615LLU, 20, 0);
745 TEST_STRTOULL("18446744073709551616", 0, 18446744073709551615LLU, 20, ERANGE);
746 TEST_STRTOULL("18446744073709551616", 10, 18446744073709551615LLU, 20, ERANGE);
747 TEST_STRTOULL("0xFFFFFFFFFFFFFFFF", 0, 18446744073709551615LLU, 18, 0);
748 TEST_STRTOULL("0xFFFFFFFFFFFFFFFF", 16, 18446744073709551615LLU, 18, 0);
749 TEST_STRTOULL("FFFFFFFFFFFFFFFF", 16, 18446744073709551615LLU, 16, 0);
750 TEST_STRTOULL("0x10000000000000000", 0, 18446744073709551615LLU, 19, ERANGE);
751 TEST_STRTOULL("0x10000000000000000", 16, 18446744073709551615LLU, 19, ERANGE);
752 TEST_STRTOULL("10000000000000000", 16, 18446744073709551615LLU, 17, ERANGE);
753 TEST_STRTOULL("01777777777777777777777",0, 18446744073709551615LLU, 23, 0);
754 TEST_STRTOULL("01777777777777777777777",8, 18446744073709551615LLU, 23, 0);
755 TEST_STRTOULL("1777777777777777777777", 8, 18446744073709551615LLU, 22, 0);
756 TEST_STRTOULL("02000000000000000000000",0, 18446744073709551615LLU, 23, ERANGE);
757 TEST_STRTOULL("02000000000000000000000",8, 18446744073709551615LLU, 23, ERANGE);
758 TEST_STRTOULL("2000000000000000000000", 8, 18446744073709551615LLU, 22, ERANGE);
760 TEST_STRTOULL("-18446744073709551615", 0, 1LLU, 21, 0);
761 TEST_STRTOULL("-18446744073709551615", 10, 1LLU, 21, 0);
762 TEST_STRTOULL("-18446744073709551616", 0, 18446744073709551615LLU, 21, ERANGE);
763 TEST_STRTOULL("-18446744073709551616", 10, 18446744073709551615LLU, 21, ERANGE);
764 TEST_STRTOULL("-0xFFFFFFFFFFFFFFFF", 0, 1LLU, 19, 0);
765 TEST_STRTOULL("-0xFFFFFFFFFFFFFFFF", 16, 1LLU, 19, 0);
766 TEST_STRTOULL("-FFFFFFFFFFFFFFFF", 16, 1LLU, 17, 0);
767 TEST_STRTOULL("-0x10000000000000000", 0, 18446744073709551615LLU, 20, ERANGE);
768 TEST_STRTOULL("-0x10000000000000000", 16, 18446744073709551615LLU, 20, ERANGE);
769 TEST_STRTOULL("-10000000000000000", 16, 18446744073709551615LLU, 18, ERANGE);
770 TEST_STRTOULL("-01777777777777777777777",0, 1LLU, 24, 0);
771 TEST_STRTOULL("-01777777777777777777777",8, 1LLU, 24, 0);
772 TEST_STRTOULL("-1777777777777777777777",8, 1LLU, 23, 0);
773 TEST_STRTOULL("-02000000000000000000000",0, 18446744073709551615LLU, 24, ERANGE);
774 TEST_STRTOULL("-02000000000000000000000",8, 18446744073709551615LLU, 24, ERANGE);
775 TEST_STRTOULL("-2000000000000000000000",8, 18446744073709551615LLU, 23, ERANGE);
777 printf("success: strtoull\n");
778 return true;
782 FIXME:
783 Types:
784 bool
785 socklen_t
786 uint{8,16,32,64}_t
787 int{8,16,32,64}_t
788 intptr_t
790 Constants:
791 PATH_NAME_MAX
792 UINT{16,32,64}_MAX
793 INT32_MAX
796 static int test_va_copy(void)
798 /* FIXME */
799 return true;
802 static int test_FUNCTION(void)
804 printf("test: FUNCTION\n");
805 if (strcmp(__FUNCTION__, "test_FUNCTION") != 0) {
806 printf("failure: FUNCTION [\nFUNCTION invalid\n]\n");
807 return false;
809 printf("success: FUNCTION\n");
810 return true;
813 static int test_MIN(void)
815 printf("test: MIN\n");
816 if (MIN(20, 1) != 1) {
817 printf("failure: MIN [\nMIN invalid\n]\n");
818 return false;
820 if (MIN(1, 20) != 1) {
821 printf("failure: MIN [\nMIN invalid\n]\n");
822 return false;
824 printf("success: MIN\n");
825 return true;
828 static int test_MAX(void)
830 printf("test: MAX\n");
831 if (MAX(20, 1) != 20) {
832 printf("failure: MAX [\nMAX invalid\n]\n");
833 return false;
835 if (MAX(1, 20) != 20) {
836 printf("failure: MAX [\nMAX invalid\n]\n");
837 return false;
839 printf("success: MAX\n");
840 return true;
843 static int test_socketpair(void)
845 int sock[2];
846 char buf[20];
848 printf("test: socketpair\n");
850 if (socketpair(AF_UNIX, SOCK_STREAM, 0, sock) == -1) {
851 printf("failure: socketpair [\n"
852 "socketpair() failed\n"
853 "]\n");
854 return false;
857 if (write(sock[1], "automatisch", 12) == -1) {
858 printf("failure: socketpair [\n"
859 "write() failed: %s\n"
860 "]\n", strerror(errno));
861 return false;
864 if (read(sock[0], buf, 12) == -1) {
865 printf("failure: socketpair [\n"
866 "read() failed: %s\n"
867 "]\n", strerror(errno));
868 return false;
871 if (strcmp(buf, "automatisch") != 0) {
872 printf("failure: socketpair [\n"
873 "expected: automatisch, got: %s\n"
874 "]\n", buf);
875 return false;
878 printf("success: socketpair\n");
880 return true;
883 extern int libreplace_test_strptime(void);
885 static int test_strptime(void)
887 return libreplace_test_strptime();
890 extern int getifaddrs_test(void);
892 static int test_getifaddrs(void)
895 printf("test: getifaddrs\n");
897 if (getifaddrs_test() != 0) {
898 printf("failure: getifaddrs\n");
899 return false;
902 printf("success: getifaddrs\n");
903 return true;
906 static int test_utime(void)
908 struct utimbuf u;
909 struct stat st1, st2, st3;
910 int fd;
912 printf("test: utime\n");
913 unlink(TESTFILE);
915 fd = open(TESTFILE, O_RDWR|O_CREAT, 0600);
916 if (fd == -1) {
917 printf("failure: utime [\n"
918 "creating '%s' failed - %s\n]\n",
919 TESTFILE, strerror(errno));
920 return false;
923 if (fstat(fd, &st1) != 0) {
924 printf("failure: utime [\n"
925 "fstat (1) failed - %s\n]\n",
926 strerror(errno));
927 close(fd);
928 return false;
931 u.actime = st1.st_atime + 300;
932 u.modtime = st1.st_mtime - 300;
933 if (utime(TESTFILE, &u) != 0) {
934 printf("failure: utime [\n"
935 "utime(&u) failed - %s\n]\n",
936 strerror(errno));
937 close(fd);
938 return false;
941 if (fstat(fd, &st2) != 0) {
942 printf("failure: utime [\n"
943 "fstat (2) failed - %s\n]\n",
944 strerror(errno));
945 close(fd);
946 return false;
949 if (utime(TESTFILE, NULL) != 0) {
950 printf("failure: utime [\n"
951 "utime(NULL) failed - %s\n]\n",
952 strerror(errno));
953 close(fd);
954 return false;
957 if (fstat(fd, &st3) != 0) {
958 printf("failure: utime [\n"
959 "fstat (3) failed - %s\n]\n",
960 strerror(errno));
961 close(fd);
962 return false;
965 #define CMP_VAL(a,c,b) do { \
966 if (a c b) { \
967 printf("failure: utime [\n" \
968 "%s: %s(%d) %s %s(%d)\n]\n", \
969 __location__, \
970 #a, (int)a, #c, #b, (int)b); \
971 close(fd); \
972 return false; \
974 } while(0)
975 #define EQUAL_VAL(a,b) CMP_VAL(a,!=,b)
976 #define GREATER_VAL(a,b) CMP_VAL(a,<=,b)
977 #define LESSER_VAL(a,b) CMP_VAL(a,>=,b)
979 EQUAL_VAL(st2.st_atime, st1.st_atime + 300);
980 EQUAL_VAL(st2.st_mtime, st1.st_mtime - 300);
981 LESSER_VAL(st3.st_atime, st2.st_atime);
982 GREATER_VAL(st3.st_mtime, st2.st_mtime);
984 #undef CMP_VAL
985 #undef EQUAL_VAL
986 #undef GREATER_VAL
987 #undef LESSER_VAL
989 unlink(TESTFILE);
990 printf("success: utime\n");
991 close(fd);
992 return true;
995 static int test_utimes(void)
997 struct timeval tv[2];
998 struct stat st1, st2;
999 int fd;
1001 printf("test: utimes\n");
1002 unlink(TESTFILE);
1004 fd = open(TESTFILE, O_RDWR|O_CREAT, 0600);
1005 if (fd == -1) {
1006 printf("failure: utimes [\n"
1007 "creating '%s' failed - %s\n]\n",
1008 TESTFILE, strerror(errno));
1009 return false;
1012 if (fstat(fd, &st1) != 0) {
1013 printf("failure: utimes [\n"
1014 "fstat (1) failed - %s\n]\n",
1015 strerror(errno));
1016 close(fd);
1017 return false;
1020 ZERO_STRUCT(tv);
1021 tv[0].tv_sec = st1.st_atime + 300;
1022 tv[1].tv_sec = st1.st_mtime - 300;
1023 if (utimes(TESTFILE, tv) != 0) {
1024 printf("failure: utimes [\n"
1025 "utimes(tv) failed - %s\n]\n",
1026 strerror(errno));
1027 close(fd);
1028 return false;
1031 if (fstat(fd, &st2) != 0) {
1032 printf("failure: utimes [\n"
1033 "fstat (2) failed - %s\n]\n",
1034 strerror(errno));
1035 close(fd);
1036 return false;
1039 #define EQUAL_VAL(a,b) do { \
1040 if (a != b) { \
1041 printf("failure: utimes [\n" \
1042 "%s: %s(%d) != %s(%d)\n]\n", \
1043 __location__, \
1044 #a, (int)a, #b, (int)b); \
1045 close(fd); \
1046 return false; \
1048 } while(0)
1050 EQUAL_VAL(st2.st_atime, st1.st_atime + 300);
1051 EQUAL_VAL(st2.st_mtime, st1.st_mtime - 300);
1053 #undef EQUAL_VAL
1055 unlink(TESTFILE);
1056 printf("success: utimes\n");
1057 close(fd);
1058 return true;
1061 static int test_memmem(void)
1063 char *s;
1065 printf("test: memmem\n");
1067 s = (char *)memmem("foo", 3, "fo", 2);
1068 if (strcmp(s, "foo") != 0) {
1069 printf(__location__ ": Failed memmem\n");
1070 return false;
1073 s = (char *)memmem("foo", 3, "", 0);
1074 /* it is allowable for this to return NULL (as happens on
1075 FreeBSD) */
1076 if (s && strcmp(s, "foo") != 0) {
1077 printf(__location__ ": Failed memmem\n");
1078 return false;
1081 s = (char *)memmem("foo", 4, "o", 1);
1082 if (strcmp(s, "oo") != 0) {
1083 printf(__location__ ": Failed memmem\n");
1084 return false;
1087 s = (char *)memmem("foobarfodx", 11, "fod", 3);
1088 if (strcmp(s, "fodx") != 0) {
1089 printf(__location__ ": Failed memmem\n");
1090 return false;
1093 printf("success: memmem\n");
1095 return true;
1098 static bool test_closefrom(void)
1100 int i, fd;
1102 for (i=0; i<100; i++) {
1103 fd = dup(0);
1104 if (fd == -1) {
1105 perror("dup failed");
1106 closefrom(3);
1107 return false;
1110 /* 1000 is just an arbitrarily chosen upper bound */
1112 if (fd >= 1000) {
1113 printf("fd=%d\n", fd);
1114 closefrom(3);
1115 return false;
1119 closefrom(3);
1121 for (i=3; i<=fd; i++) {
1122 off_t off;
1123 off = lseek(i, 0, SEEK_CUR);
1124 if ((off != (off_t)-1) || (errno != EBADF)) {
1125 printf("fd %d not closed\n", i);
1126 return false;
1130 return true;
1133 static bool test_array_del_element(void)
1135 int a[] = { 1,2,3,4,5 };
1137 printf("test: array_del_element\n");
1139 ARRAY_DEL_ELEMENT(a, 4, ARRAY_SIZE(a));
1141 if ((a[0] != 1) ||
1142 (a[1] != 2) ||
1143 (a[2] != 3) ||
1144 (a[3] != 4) ||
1145 (a[4] != 5)) {
1146 return false;
1149 ARRAY_DEL_ELEMENT(a, 0, ARRAY_SIZE(a));
1151 if ((a[0] != 2) ||
1152 (a[1] != 3) ||
1153 (a[2] != 4) ||
1154 (a[3] != 5) ||
1155 (a[4] != 5)) {
1156 return false;
1159 ARRAY_DEL_ELEMENT(a, 2, ARRAY_SIZE(a));
1161 if ((a[0] != 2) ||
1162 (a[1] != 3) ||
1163 (a[2] != 5) ||
1164 (a[3] != 5) ||
1165 (a[4] != 5)) {
1166 return false;
1169 printf("success: array_del_element\n");
1171 return true;
1174 bool torture_local_replace(struct torture_context *ctx)
1176 bool ret = true;
1177 ret &= test_ftruncate();
1178 ret &= test_strlcpy();
1179 ret &= test_strlcat();
1180 ret &= test_mktime();
1181 ret &= test_initgroups();
1182 ret &= test_memmove();
1183 ret &= test_strdup();
1184 ret &= test_setlinebuf();
1185 ret &= test_vsyslog();
1186 ret &= test_timegm();
1187 ret &= test_setenv();
1188 ret &= test_strndup();
1189 ret &= test_strnlen();
1190 ret &= test_waitpid();
1191 ret &= test_seteuid();
1192 ret &= test_setegid();
1193 ret &= test_asprintf();
1194 ret &= test_snprintf();
1195 ret &= test_vasprintf();
1196 ret &= test_vsnprintf();
1197 ret &= test_opendir();
1198 ret &= test_readdir();
1199 ret &= test_telldir();
1200 ret &= test_seekdir();
1201 ret &= test_dlopen();
1202 ret &= test_chroot();
1203 ret &= test_bzero();
1204 ret &= test_strerror();
1205 ret &= test_errno();
1206 ret &= test_mkdtemp();
1207 ret &= test_mkstemp();
1208 ret &= test_pread();
1209 ret &= test_pwrite();
1210 ret &= test_inet_ntoa();
1211 ret &= test_strtoll();
1212 ret &= test_strtoull();
1213 ret &= test_va_copy();
1214 ret &= test_FUNCTION();
1215 ret &= test_MIN();
1216 ret &= test_MAX();
1217 ret &= test_socketpair();
1218 ret &= test_strptime();
1219 ret &= test_getifaddrs();
1220 ret &= test_utime();
1221 ret &= test_utimes();
1222 ret &= test_memmem();
1223 ret &= test_closefrom();
1224 ret &= test_array_del_element();
1226 return ret;