Fix CID 1138340 Resource leak
[Samba.git] / ctdb / lib / replace / test / testsuite.c
blobdf8b71f7d6eff5da421b65af3bbfee6635fdf86e
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"
49 #include "system/aio.h"
51 #define TESTFILE "testfile.dat"
55 test ftruncate() function
57 static int test_ftruncate(void)
59 struct stat st;
60 int fd;
61 const int size = 1234;
62 printf("test: ftruncate\n");
63 unlink(TESTFILE);
64 fd = open(TESTFILE, O_RDWR|O_CREAT, 0600);
65 if (fd == -1) {
66 printf("failure: ftruncate [\n"
67 "creating '%s' failed - %s\n]\n", TESTFILE, strerror(errno));
68 return false;
70 if (ftruncate(fd, size) != 0) {
71 printf("failure: ftruncate [\n%s\n]\n", strerror(errno));
72 return false;
74 if (fstat(fd, &st) != 0) {
75 printf("failure: ftruncate [\nfstat failed - %s\n]\n", strerror(errno));
76 return false;
78 if (st.st_size != size) {
79 printf("failure: ftruncate [\ngave wrong size %d - expected %d\n]\n",
80 (int)st.st_size, size);
81 return false;
83 unlink(TESTFILE);
84 printf("success: ftruncate\n");
85 return true;
89 test strlcpy() function.
90 see http://www.gratisoft.us/todd/papers/strlcpy.html
92 static int test_strlcpy(void)
94 char buf[4];
95 const struct {
96 const char *src;
97 size_t result;
98 } tests[] = {
99 { "abc", 3 },
100 { "abcdef", 6 },
101 { "abcd", 4 },
102 { "", 0 },
103 { NULL, 0 }
105 int i;
106 printf("test: strlcpy\n");
107 for (i=0;tests[i].src;i++) {
108 if (strlcpy(buf, tests[i].src, sizeof(buf)) != tests[i].result) {
109 printf("failure: strlcpy [\ntest %d failed\n]\n", i);
110 return false;
113 printf("success: strlcpy\n");
114 return true;
117 static int test_strlcat(void)
119 char tmp[10];
120 printf("test: strlcat\n");
121 strlcpy(tmp, "", sizeof(tmp));
122 if (strlcat(tmp, "bla", 3) != 3) {
123 printf("failure: strlcat [\ninvalid return code\n]\n");
124 return false;
126 if (strcmp(tmp, "bl") != 0) {
127 printf("failure: strlcat [\nexpected \"bl\", got \"%s\"\n]\n",
128 tmp);
129 return false;
132 strlcpy(tmp, "da", sizeof(tmp));
133 if (strlcat(tmp, "me", 4) != 4) {
134 printf("failure: strlcat [\nexpected \"dam\", got \"%s\"\n]\n",
135 tmp);
136 return false;
139 printf("success: strlcat\n");
140 return true;
143 static int test_mktime(void)
145 /* FIXME */
146 return true;
149 static int test_initgroups(void)
151 /* FIXME */
152 return true;
155 static int test_memmove(void)
157 /* FIXME */
158 return true;
161 static int test_strdup(void)
163 char *x;
164 printf("test: strdup\n");
165 x = strdup("bla");
166 if (strcmp("bla", x) != 0) {
167 printf("failure: strdup [\nfailed: expected \"bla\", got \"%s\"\n]\n",
169 free(x);
170 return false;
172 free(x);
173 printf("success: strdup\n");
174 return true;
177 static int test_setlinebuf(void)
179 printf("test: setlinebuf\n");
180 setlinebuf(stdout);
181 printf("success: setlinebuf\n");
182 return true;
185 static int test_vsyslog(void)
187 /* FIXME */
188 return true;
191 static int test_timegm(void)
193 /* FIXME */
194 return true;
197 static int test_setenv(void)
199 #define TEST_SETENV(key, value, overwrite, result) do { \
200 int _ret; \
201 char *_v; \
202 _ret = setenv(key, value, overwrite); \
203 if (_ret != 0) { \
204 printf("failure: setenv [\n" \
205 "setenv(%s, %s, %d) failed\n" \
206 "]\n", \
207 key, value, overwrite); \
208 return false; \
210 _v=getenv(key); \
211 if (!_v) { \
212 printf("failure: setenv [\n" \
213 "getenv(%s) returned NULL\n" \
214 "]\n", \
215 key); \
216 return false; \
218 if (strcmp(result, _v) != 0) { \
219 printf("failure: setenv [\n" \
220 "getenv(%s): '%s' != '%s'\n" \
221 "]\n", \
222 key, result, _v); \
223 return false; \
225 } while(0)
227 #define TEST_UNSETENV(key) do { \
228 char *_v; \
229 unsetenv(key); \
230 _v=getenv(key); \
231 if (_v) { \
232 printf("failure: setenv [\n" \
233 "getenv(%s): NULL != '%s'\n" \
234 "]\n", \
235 SETENVTEST_KEY, _v); \
236 return false; \
238 } while (0)
240 #define SETENVTEST_KEY "SETENVTESTKEY"
241 #define SETENVTEST_VAL "SETENVTESTVAL"
243 printf("test: setenv\n");
244 TEST_SETENV(SETENVTEST_KEY, SETENVTEST_VAL"1", 0, SETENVTEST_VAL"1");
245 TEST_SETENV(SETENVTEST_KEY, SETENVTEST_VAL"2", 0, SETENVTEST_VAL"1");
246 TEST_SETENV(SETENVTEST_KEY, SETENVTEST_VAL"3", 1, SETENVTEST_VAL"3");
247 TEST_SETENV(SETENVTEST_KEY, SETENVTEST_VAL"4", 1, SETENVTEST_VAL"4");
248 TEST_UNSETENV(SETENVTEST_KEY);
249 TEST_UNSETENV(SETENVTEST_KEY);
250 TEST_SETENV(SETENVTEST_KEY, SETENVTEST_VAL"5", 0, SETENVTEST_VAL"5");
251 TEST_UNSETENV(SETENVTEST_KEY);
252 TEST_UNSETENV(SETENVTEST_KEY);
253 printf("success: setenv\n");
254 return true;
257 static int test_strndup(void)
259 char *x;
260 printf("test: strndup\n");
261 x = strndup("bla", 0);
262 if (strcmp(x, "") != 0) {
263 printf("failure: strndup [\ninvalid\n]\n");
264 return false;
266 free(x);
267 x = strndup("bla", 2);
268 if (strcmp(x, "bl") != 0) {
269 printf("failure: strndup [\ninvalid\n]\n");
270 free(x);
271 return false;
273 free(x);
274 x = strndup("bla", 10);
275 if (strcmp(x, "bla") != 0) {
276 printf("failure: strndup [\ninvalid\n]\n");
277 free(x);
278 return false;
280 free(x);
281 printf("success: strndup\n");
282 return true;
285 static int test_strnlen(void)
287 printf("test: strnlen\n");
288 if (strnlen("bla", 2) != 2) {
289 printf("failure: strnlen [\nunexpected length\n]\n");
290 return false;
293 if (strnlen("some text\n", 0) != 0) {
294 printf("failure: strnlen [\nunexpected length\n]\n");
295 return false;
298 if (strnlen("some text", 20) != 9) {
299 printf("failure: strnlen [\nunexpected length\n]\n");
300 return false;
303 printf("success: strnlen\n");
304 return true;
307 static int test_waitpid(void)
309 /* FIXME */
310 return true;
313 static int test_seteuid(void)
315 /* FIXME */
316 return true;
319 static int test_setegid(void)
321 /* FIXME */
322 return true;
325 static int test_asprintf(void)
327 char *x;
328 printf("test: asprintf\n");
329 if (asprintf(&x, "%d", 9) != 1) {
330 printf("failure: asprintf [\ngenerate asprintf\n]\n");
331 return false;
333 if (strcmp(x, "9") != 0) {
334 printf("failure: asprintf [\ngenerate asprintf\n]\n");
335 return false;
337 if (asprintf(&x, "dat%s", "a") != 4) {
338 printf("failure: asprintf [\ngenerate asprintf\n]\n");
339 return false;
341 if (strcmp(x, "data") != 0) {
342 printf("failure: asprintf [\ngenerate asprintf\n]\n");
343 return false;
345 printf("success: asprintf\n");
346 return true;
349 static int test_snprintf(void)
351 char tmp[10];
352 printf("test: snprintf\n");
353 if (snprintf(tmp, 3, "foo%d", 9) != 4) {
354 printf("failure: snprintf [\nsnprintf return code failed\n]\n");
355 return false;
358 if (strcmp(tmp, "fo") != 0) {
359 printf("failure: snprintf [\nsnprintf failed\n]\n");
360 return false;
363 printf("success: snprintf\n");
364 return true;
367 static int test_vasprintf(void)
369 /* FIXME */
370 return true;
373 static int test_vsnprintf(void)
375 /* FIXME */
376 return true;
379 static int test_opendir(void)
381 /* FIXME */
382 return true;
385 static int test_readdir(void)
387 printf("test: readdir\n");
388 if (test_readdir_os2_delete() != 0) {
389 return false;
391 printf("success: readdir\n");
392 return true;
395 static int test_telldir(void)
397 /* FIXME */
398 return true;
401 static int test_seekdir(void)
403 /* FIXME */
404 return true;
407 static int test_dlopen(void)
409 /* FIXME: test dlopen, dlsym, dlclose, dlerror */
410 return true;
414 static int test_chroot(void)
416 /* FIXME: chroot() */
417 return true;
420 static int test_bzero(void)
422 /* FIXME: bzero */
423 return true;
426 static int test_strerror(void)
428 /* FIXME */
429 return true;
432 static int test_errno(void)
434 printf("test: errno\n");
435 errno = 3;
436 if (errno != 3) {
437 printf("failure: errno [\nerrno failed\n]\n");
438 return false;
441 printf("success: errno\n");
442 return true;
445 static int test_mkdtemp(void)
447 /* FIXME */
448 return true;
451 static int test_mkstemp(void)
453 /* FIXME */
454 return true;
457 static int test_pread(void)
459 /* FIXME */
460 return true;
463 static int test_pwrite(void)
465 /* FIXME */
466 return true;
469 static int test_inet_ntoa(void)
471 /* FIXME */
472 return true;
475 #define TEST_STRTO_X(type,fmt,func,str,base,res,diff,rrnoo) do {\
476 type _v; \
477 char _s[64]; \
478 char *_p = NULL;\
479 char *_ep = NULL; \
480 strlcpy(_s, str, sizeof(_s));\
481 if (diff >= 0) { \
482 _ep = &_s[diff]; \
484 errno = 0; \
485 _v = func(_s, &_p, base); \
486 if (errno != rrnoo) { \
487 printf("failure: %s [\n" \
488 "\t%s\n" \
489 "\t%s(\"%s\",%d,%d): " fmt " (=/!)= " fmt "\n" \
490 "\terrno: %d != %d\n" \
491 "]\n", \
492 __STRING(func), __location__, __STRING(func), \
493 str, diff, base, res, _v, rrnoo, errno); \
494 return false; \
495 } else if (_v != res) { \
496 printf("failure: %s [\n" \
497 "\t%s\n" \
498 "\t%s(\"%s\",%d,%d): " fmt " != " fmt "\n" \
499 "]\n", \
500 __STRING(func), __location__, __STRING(func), \
501 str, diff, base, res, _v); \
502 return false; \
503 } else if (_p != _ep) { \
504 printf("failure: %s [\n" \
505 "\t%s\n" \
506 "\t%s(\"%s\",%d,%d): " fmt " (=/!)= " fmt "\n" \
507 "\tptr: %p - %p = %d != %d\n" \
508 "]\n", \
509 __STRING(func), __location__, __STRING(func), \
510 str, diff, base, res, _v, _ep, _p, (int)(diff - (_ep - _p)), diff); \
511 return false; \
513 } while (0)
515 static int test_strtoll(void)
517 printf("test: strtoll\n");
519 #define TEST_STRTOLL(str,base,res,diff,errnoo) TEST_STRTO_X(long long int, "%lld", strtoll,str,base,res,diff,errnoo)
521 TEST_STRTOLL("15", 10, 15LL, 2, 0);
522 TEST_STRTOLL(" 15", 10, 15LL, 4, 0);
523 TEST_STRTOLL("15", 0, 15LL, 2, 0);
524 TEST_STRTOLL(" 15 ", 0, 15LL, 3, 0);
525 TEST_STRTOLL("+15", 10, 15LL, 3, 0);
526 TEST_STRTOLL(" +15", 10, 15LL, 5, 0);
527 TEST_STRTOLL("+15", 0, 15LL, 3, 0);
528 TEST_STRTOLL(" +15 ", 0, 15LL, 4, 0);
529 TEST_STRTOLL("-15", 10, -15LL, 3, 0);
530 TEST_STRTOLL(" -15", 10, -15LL, 5, 0);
531 TEST_STRTOLL("-15", 0, -15LL, 3, 0);
532 TEST_STRTOLL(" -15 ", 0, -15LL, 4, 0);
533 TEST_STRTOLL("015", 10, 15LL, 3, 0);
534 TEST_STRTOLL(" 015", 10, 15LL, 5, 0);
535 TEST_STRTOLL("015", 0, 13LL, 3, 0);
536 TEST_STRTOLL(" 015", 0, 13LL, 5, 0);
537 TEST_STRTOLL("0x15", 10, 0LL, 1, 0);
538 TEST_STRTOLL(" 0x15", 10, 0LL, 3, 0);
539 TEST_STRTOLL("0x15", 0, 21LL, 4, 0);
540 TEST_STRTOLL(" 0x15", 0, 21LL, 6, 0);
542 TEST_STRTOLL("10", 16, 16LL, 2, 0);
543 TEST_STRTOLL(" 10 ", 16, 16LL, 4, 0);
544 TEST_STRTOLL("0x10", 16, 16LL, 4, 0);
545 TEST_STRTOLL("0x10", 0, 16LL, 4, 0);
546 TEST_STRTOLL(" 0x10 ", 0, 16LL, 5, 0);
547 TEST_STRTOLL("+10", 16, 16LL, 3, 0);
548 TEST_STRTOLL(" +10 ", 16, 16LL, 5, 0);
549 TEST_STRTOLL("+0x10", 16, 16LL, 5, 0);
550 TEST_STRTOLL("+0x10", 0, 16LL, 5, 0);
551 TEST_STRTOLL(" +0x10 ", 0, 16LL, 6, 0);
552 TEST_STRTOLL("-10", 16, -16LL, 3, 0);
553 TEST_STRTOLL(" -10 ", 16, -16LL, 5, 0);
554 TEST_STRTOLL("-0x10", 16, -16LL, 5, 0);
555 TEST_STRTOLL("-0x10", 0, -16LL, 5, 0);
556 TEST_STRTOLL(" -0x10 ", 0, -16LL, 6, 0);
557 TEST_STRTOLL("010", 16, 16LL, 3, 0);
558 TEST_STRTOLL(" 010 ", 16, 16LL, 5, 0);
559 TEST_STRTOLL("-010", 16, -16LL, 4, 0);
561 TEST_STRTOLL("11", 8, 9LL, 2, 0);
562 TEST_STRTOLL("011", 8, 9LL, 3, 0);
563 TEST_STRTOLL("011", 0, 9LL, 3, 0);
564 TEST_STRTOLL("-11", 8, -9LL, 3, 0);
565 TEST_STRTOLL("-011", 8, -9LL, 4, 0);
566 TEST_STRTOLL("-011", 0, -9LL, 4, 0);
568 TEST_STRTOLL("011", 8, 9LL, 3, 0);
569 TEST_STRTOLL("011", 0, 9LL, 3, 0);
570 TEST_STRTOLL("-11", 8, -9LL, 3, 0);
571 TEST_STRTOLL("-011", 8, -9LL, 4, 0);
572 TEST_STRTOLL("-011", 0, -9LL, 4, 0);
574 TEST_STRTOLL("Text", 0, 0LL, 0, 0);
576 TEST_STRTOLL("9223372036854775807", 10, 9223372036854775807LL, 19, 0);
577 TEST_STRTOLL("9223372036854775807", 0, 9223372036854775807LL, 19, 0);
578 TEST_STRTOLL("9223372036854775808", 0, 9223372036854775807LL, 19, ERANGE);
579 TEST_STRTOLL("9223372036854775808", 10, 9223372036854775807LL, 19, ERANGE);
580 TEST_STRTOLL("0x7FFFFFFFFFFFFFFF", 0, 9223372036854775807LL, 18, 0);
581 TEST_STRTOLL("0x7FFFFFFFFFFFFFFF", 16, 9223372036854775807LL, 18, 0);
582 TEST_STRTOLL("7FFFFFFFFFFFFFFF", 16, 9223372036854775807LL, 16, 0);
583 TEST_STRTOLL("0x8000000000000000", 0, 9223372036854775807LL, 18, ERANGE);
584 TEST_STRTOLL("0x8000000000000000", 16, 9223372036854775807LL, 18, ERANGE);
585 TEST_STRTOLL("80000000000000000", 16, 9223372036854775807LL, 17, ERANGE);
586 TEST_STRTOLL("0777777777777777777777", 0, 9223372036854775807LL, 22, 0);
587 TEST_STRTOLL("0777777777777777777777", 8, 9223372036854775807LL, 22, 0);
588 TEST_STRTOLL("777777777777777777777", 8, 9223372036854775807LL, 21, 0);
589 TEST_STRTOLL("01000000000000000000000", 0, 9223372036854775807LL, 23, ERANGE);
590 TEST_STRTOLL("01000000000000000000000", 8, 9223372036854775807LL, 23, ERANGE);
591 TEST_STRTOLL("1000000000000000000000", 8, 9223372036854775807LL, 22, ERANGE);
593 TEST_STRTOLL("-9223372036854775808", 10, -9223372036854775807LL -1, 20, 0);
594 TEST_STRTOLL("-9223372036854775808", 0, -9223372036854775807LL -1, 20, 0);
595 TEST_STRTOLL("-9223372036854775809", 0, -9223372036854775807LL -1, 20, ERANGE);
596 TEST_STRTOLL("-9223372036854775809", 10, -9223372036854775807LL -1, 20, ERANGE);
597 TEST_STRTOLL("-0x8000000000000000", 0, -9223372036854775807LL -1, 19, 0);
598 TEST_STRTOLL("-0x8000000000000000", 16, -9223372036854775807LL -1, 19, 0);
599 TEST_STRTOLL("-8000000000000000", 16, -9223372036854775807LL -1, 17, 0);
600 TEST_STRTOLL("-0x8000000000000001", 0, -9223372036854775807LL -1, 19, ERANGE);
601 TEST_STRTOLL("-0x8000000000000001", 16, -9223372036854775807LL -1, 19, ERANGE);
602 TEST_STRTOLL("-80000000000000001", 16, -9223372036854775807LL -1, 18, ERANGE);
603 TEST_STRTOLL("-01000000000000000000000",0, -9223372036854775807LL -1, 24, 0);
604 TEST_STRTOLL("-01000000000000000000000",8, -9223372036854775807LL -1, 24, 0);
605 TEST_STRTOLL("-1000000000000000000000", 8, -9223372036854775807LL -1, 23, 0);
606 TEST_STRTOLL("-01000000000000000000001",0, -9223372036854775807LL -1, 24, ERANGE);
607 TEST_STRTOLL("-01000000000000000000001",8, -9223372036854775807LL -1, 24, ERANGE);
608 TEST_STRTOLL("-1000000000000000000001", 8, -9223372036854775807LL -1, 23, ERANGE);
610 printf("success: strtoll\n");
611 return true;
614 static int test_strtoull(void)
616 printf("test: strtoull\n");
618 #define TEST_STRTOULL(str,base,res,diff,errnoo) TEST_STRTO_X(long long unsigned int,"%llu",strtoull,str,base,res,diff,errnoo)
620 TEST_STRTOULL("15", 10, 15LLU, 2, 0);
621 TEST_STRTOULL(" 15", 10, 15LLU, 4, 0);
622 TEST_STRTOULL("15", 0, 15LLU, 2, 0);
623 TEST_STRTOULL(" 15 ", 0, 15LLU, 3, 0);
624 TEST_STRTOULL("+15", 10, 15LLU, 3, 0);
625 TEST_STRTOULL(" +15", 10, 15LLU, 5, 0);
626 TEST_STRTOULL("+15", 0, 15LLU, 3, 0);
627 TEST_STRTOULL(" +15 ", 0, 15LLU, 4, 0);
628 TEST_STRTOULL("-15", 10, 18446744073709551601LLU, 3, 0);
629 TEST_STRTOULL(" -15", 10, 18446744073709551601LLU, 5, 0);
630 TEST_STRTOULL("-15", 0, 18446744073709551601LLU, 3, 0);
631 TEST_STRTOULL(" -15 ", 0, 18446744073709551601LLU, 4, 0);
632 TEST_STRTOULL("015", 10, 15LLU, 3, 0);
633 TEST_STRTOULL(" 015", 10, 15LLU, 5, 0);
634 TEST_STRTOULL("015", 0, 13LLU, 3, 0);
635 TEST_STRTOULL(" 015", 0, 13LLU, 5, 0);
636 TEST_STRTOULL("0x15", 10, 0LLU, 1, 0);
637 TEST_STRTOULL(" 0x15", 10, 0LLU, 3, 0);
638 TEST_STRTOULL("0x15", 0, 21LLU, 4, 0);
639 TEST_STRTOULL(" 0x15", 0, 21LLU, 6, 0);
641 TEST_STRTOULL("10", 16, 16LLU, 2, 0);
642 TEST_STRTOULL(" 10 ", 16, 16LLU, 4, 0);
643 TEST_STRTOULL("0x10", 16, 16LLU, 4, 0);
644 TEST_STRTOULL("0x10", 0, 16LLU, 4, 0);
645 TEST_STRTOULL(" 0x10 ", 0, 16LLU, 5, 0);
646 TEST_STRTOULL("+10", 16, 16LLU, 3, 0);
647 TEST_STRTOULL(" +10 ", 16, 16LLU, 5, 0);
648 TEST_STRTOULL("+0x10", 16, 16LLU, 5, 0);
649 TEST_STRTOULL("+0x10", 0, 16LLU, 5, 0);
650 TEST_STRTOULL(" +0x10 ", 0, 16LLU, 6, 0);
651 TEST_STRTOULL("-10", 16, -16LLU, 3, 0);
652 TEST_STRTOULL(" -10 ", 16, -16LLU, 5, 0);
653 TEST_STRTOULL("-0x10", 16, -16LLU, 5, 0);
654 TEST_STRTOULL("-0x10", 0, -16LLU, 5, 0);
655 TEST_STRTOULL(" -0x10 ", 0, -16LLU, 6, 0);
656 TEST_STRTOULL("010", 16, 16LLU, 3, 0);
657 TEST_STRTOULL(" 010 ", 16, 16LLU, 5, 0);
658 TEST_STRTOULL("-010", 16, -16LLU, 4, 0);
660 TEST_STRTOULL("11", 8, 9LLU, 2, 0);
661 TEST_STRTOULL("011", 8, 9LLU, 3, 0);
662 TEST_STRTOULL("011", 0, 9LLU, 3, 0);
663 TEST_STRTOULL("-11", 8, -9LLU, 3, 0);
664 TEST_STRTOULL("-011", 8, -9LLU, 4, 0);
665 TEST_STRTOULL("-011", 0, -9LLU, 4, 0);
667 TEST_STRTOULL("011", 8, 9LLU, 3, 0);
668 TEST_STRTOULL("011", 0, 9LLU, 3, 0);
669 TEST_STRTOULL("-11", 8, -9LLU, 3, 0);
670 TEST_STRTOULL("-011", 8, -9LLU, 4, 0);
671 TEST_STRTOULL("-011", 0, -9LLU, 4, 0);
673 TEST_STRTOULL("Text", 0, 0LLU, 0, 0);
675 TEST_STRTOULL("9223372036854775807", 10, 9223372036854775807LLU, 19, 0);
676 TEST_STRTOULL("9223372036854775807", 0, 9223372036854775807LLU, 19, 0);
677 TEST_STRTOULL("9223372036854775808", 0, 9223372036854775808LLU, 19, 0);
678 TEST_STRTOULL("9223372036854775808", 10, 9223372036854775808LLU, 19, 0);
679 TEST_STRTOULL("0x7FFFFFFFFFFFFFFF", 0, 9223372036854775807LLU, 18, 0);
680 TEST_STRTOULL("0x7FFFFFFFFFFFFFFF", 16, 9223372036854775807LLU, 18, 0);
681 TEST_STRTOULL("7FFFFFFFFFFFFFFF", 16, 9223372036854775807LLU, 16, 0);
682 TEST_STRTOULL("0x8000000000000000", 0, 9223372036854775808LLU, 18, 0);
683 TEST_STRTOULL("0x8000000000000000", 16, 9223372036854775808LLU, 18, 0);
684 TEST_STRTOULL("8000000000000000", 16, 9223372036854775808LLU, 16, 0);
685 TEST_STRTOULL("0777777777777777777777", 0, 9223372036854775807LLU, 22, 0);
686 TEST_STRTOULL("0777777777777777777777", 8, 9223372036854775807LLU, 22, 0);
687 TEST_STRTOULL("777777777777777777777", 8, 9223372036854775807LLU, 21, 0);
688 TEST_STRTOULL("01000000000000000000000",0, 9223372036854775808LLU, 23, 0);
689 TEST_STRTOULL("01000000000000000000000",8, 9223372036854775808LLU, 23, 0);
690 TEST_STRTOULL("1000000000000000000000", 8, 9223372036854775808LLU, 22, 0);
692 TEST_STRTOULL("-9223372036854775808", 10, 9223372036854775808LLU, 20, 0);
693 TEST_STRTOULL("-9223372036854775808", 0, 9223372036854775808LLU, 20, 0);
694 TEST_STRTOULL("-9223372036854775809", 0, 9223372036854775807LLU, 20, 0);
695 TEST_STRTOULL("-9223372036854775809", 10, 9223372036854775807LLU, 20, 0);
696 TEST_STRTOULL("-0x8000000000000000", 0, 9223372036854775808LLU, 19, 0);
697 TEST_STRTOULL("-0x8000000000000000", 16, 9223372036854775808LLU, 19, 0);
698 TEST_STRTOULL("-8000000000000000", 16, 9223372036854775808LLU, 17, 0);
699 TEST_STRTOULL("-0x8000000000000001", 0, 9223372036854775807LLU, 19, 0);
700 TEST_STRTOULL("-0x8000000000000001", 16, 9223372036854775807LLU, 19, 0);
701 TEST_STRTOULL("-8000000000000001", 16, 9223372036854775807LLU, 17, 0);
702 TEST_STRTOULL("-01000000000000000000000",0, 9223372036854775808LLU, 24, 0);
703 TEST_STRTOULL("-01000000000000000000000",8, 9223372036854775808LLU, 24, 0);
704 TEST_STRTOULL("-1000000000000000000000",8, 9223372036854775808LLU, 23, 0);
705 TEST_STRTOULL("-01000000000000000000001",0, 9223372036854775807LLU, 24, 0);
706 TEST_STRTOULL("-01000000000000000000001",8, 9223372036854775807LLU, 24, 0);
707 TEST_STRTOULL("-1000000000000000000001",8, 9223372036854775807LLU, 23, 0);
709 TEST_STRTOULL("18446744073709551615", 0, 18446744073709551615LLU, 20, 0);
710 TEST_STRTOULL("18446744073709551615", 10, 18446744073709551615LLU, 20, 0);
711 TEST_STRTOULL("18446744073709551616", 0, 18446744073709551615LLU, 20, ERANGE);
712 TEST_STRTOULL("18446744073709551616", 10, 18446744073709551615LLU, 20, ERANGE);
713 TEST_STRTOULL("0xFFFFFFFFFFFFFFFF", 0, 18446744073709551615LLU, 18, 0);
714 TEST_STRTOULL("0xFFFFFFFFFFFFFFFF", 16, 18446744073709551615LLU, 18, 0);
715 TEST_STRTOULL("FFFFFFFFFFFFFFFF", 16, 18446744073709551615LLU, 16, 0);
716 TEST_STRTOULL("0x10000000000000000", 0, 18446744073709551615LLU, 19, ERANGE);
717 TEST_STRTOULL("0x10000000000000000", 16, 18446744073709551615LLU, 19, ERANGE);
718 TEST_STRTOULL("10000000000000000", 16, 18446744073709551615LLU, 17, ERANGE);
719 TEST_STRTOULL("01777777777777777777777",0, 18446744073709551615LLU, 23, 0);
720 TEST_STRTOULL("01777777777777777777777",8, 18446744073709551615LLU, 23, 0);
721 TEST_STRTOULL("1777777777777777777777", 8, 18446744073709551615LLU, 22, 0);
722 TEST_STRTOULL("02000000000000000000000",0, 18446744073709551615LLU, 23, ERANGE);
723 TEST_STRTOULL("02000000000000000000000",8, 18446744073709551615LLU, 23, ERANGE);
724 TEST_STRTOULL("2000000000000000000000", 8, 18446744073709551615LLU, 22, ERANGE);
726 TEST_STRTOULL("-18446744073709551615", 0, 1LLU, 21, 0);
727 TEST_STRTOULL("-18446744073709551615", 10, 1LLU, 21, 0);
728 TEST_STRTOULL("-18446744073709551616", 0, 18446744073709551615LLU, 21, ERANGE);
729 TEST_STRTOULL("-18446744073709551616", 10, 18446744073709551615LLU, 21, ERANGE);
730 TEST_STRTOULL("-0xFFFFFFFFFFFFFFFF", 0, 1LLU, 19, 0);
731 TEST_STRTOULL("-0xFFFFFFFFFFFFFFFF", 16, 1LLU, 19, 0);
732 TEST_STRTOULL("-FFFFFFFFFFFFFFFF", 16, 1LLU, 17, 0);
733 TEST_STRTOULL("-0x10000000000000000", 0, 18446744073709551615LLU, 20, ERANGE);
734 TEST_STRTOULL("-0x10000000000000000", 16, 18446744073709551615LLU, 20, ERANGE);
735 TEST_STRTOULL("-10000000000000000", 16, 18446744073709551615LLU, 18, ERANGE);
736 TEST_STRTOULL("-01777777777777777777777",0, 1LLU, 24, 0);
737 TEST_STRTOULL("-01777777777777777777777",8, 1LLU, 24, 0);
738 TEST_STRTOULL("-1777777777777777777777",8, 1LLU, 23, 0);
739 TEST_STRTOULL("-02000000000000000000000",0, 18446744073709551615LLU, 24, ERANGE);
740 TEST_STRTOULL("-02000000000000000000000",8, 18446744073709551615LLU, 24, ERANGE);
741 TEST_STRTOULL("-2000000000000000000000",8, 18446744073709551615LLU, 23, ERANGE);
743 printf("success: strtoull\n");
744 return true;
748 FIXME:
749 Types:
750 bool
751 socklen_t
752 uint{8,16,32,64}_t
753 int{8,16,32,64}_t
754 intptr_t
756 Constants:
757 PATH_NAME_MAX
758 UINT{16,32,64}_MAX
759 INT32_MAX
762 static int test_va_copy(void)
764 /* FIXME */
765 return true;
768 static int test_FUNCTION(void)
770 printf("test: FUNCTION\n");
771 if (strcmp(__FUNCTION__, "test_FUNCTION") != 0) {
772 printf("failure: FUNCTION [\nFUNCTION invalid\n]\n");
773 return false;
775 printf("success: FUNCTION\n");
776 return true;
779 static int test_MIN(void)
781 printf("test: MIN\n");
782 if (MIN(20, 1) != 1) {
783 printf("failure: MIN [\nMIN invalid\n]\n");
784 return false;
786 if (MIN(1, 20) != 1) {
787 printf("failure: MIN [\nMIN invalid\n]\n");
788 return false;
790 printf("success: MIN\n");
791 return true;
794 static int test_MAX(void)
796 printf("test: MAX\n");
797 if (MAX(20, 1) != 20) {
798 printf("failure: MAX [\nMAX invalid\n]\n");
799 return false;
801 if (MAX(1, 20) != 20) {
802 printf("failure: MAX [\nMAX invalid\n]\n");
803 return false;
805 printf("success: MAX\n");
806 return true;
809 static int test_socketpair(void)
811 int sock[2];
812 char buf[20];
814 printf("test: socketpair\n");
816 if (socketpair(AF_UNIX, SOCK_STREAM, 0, sock) == -1) {
817 printf("failure: socketpair [\n"
818 "socketpair() failed\n"
819 "]\n");
820 return false;
823 if (write(sock[1], "automatisch", 12) == -1) {
824 printf("failure: socketpair [\n"
825 "write() failed: %s\n"
826 "]\n", strerror(errno));
827 return false;
830 if (read(sock[0], buf, 12) == -1) {
831 printf("failure: socketpair [\n"
832 "read() failed: %s\n"
833 "]\n", strerror(errno));
834 return false;
837 if (strcmp(buf, "automatisch") != 0) {
838 printf("failure: socketpair [\n"
839 "expected: automatisch, got: %s\n"
840 "]\n", buf);
841 return false;
844 printf("success: socketpair\n");
846 return true;
849 extern int libreplace_test_strptime(void);
851 static int test_strptime(void)
853 return libreplace_test_strptime();
856 extern int getifaddrs_test(void);
858 static int test_getifaddrs(void)
861 printf("test: getifaddrs\n");
863 if (getifaddrs_test() != 0) {
864 printf("failure: getifaddrs\n");
865 return false;
868 printf("success: getifaddrs\n");
869 return true;
872 static int test_utime(void)
874 struct utimbuf u;
875 struct stat st1, st2, st3;
876 int fd;
878 printf("test: utime\n");
879 unlink(TESTFILE);
881 fd = open(TESTFILE, O_RDWR|O_CREAT, 0600);
882 if (fd == -1) {
883 printf("failure: utime [\n"
884 "creating '%s' failed - %s\n]\n",
885 TESTFILE, strerror(errno));
886 return false;
889 if (fstat(fd, &st1) != 0) {
890 printf("failure: utime [\n"
891 "fstat (1) failed - %s\n]\n",
892 strerror(errno));
893 return false;
896 u.actime = st1.st_atime + 300;
897 u.modtime = st1.st_mtime - 300;
898 if (utime(TESTFILE, &u) != 0) {
899 printf("failure: utime [\n"
900 "utime(&u) failed - %s\n]\n",
901 strerror(errno));
902 return false;
905 if (fstat(fd, &st2) != 0) {
906 printf("failure: utime [\n"
907 "fstat (2) failed - %s\n]\n",
908 strerror(errno));
909 return false;
912 if (utime(TESTFILE, NULL) != 0) {
913 printf("failure: utime [\n"
914 "utime(NULL) failed - %s\n]\n",
915 strerror(errno));
916 return false;
919 if (fstat(fd, &st3) != 0) {
920 printf("failure: utime [\n"
921 "fstat (3) failed - %s\n]\n",
922 strerror(errno));
923 return false;
926 #define CMP_VAL(a,c,b) do { \
927 if (a c b) { \
928 printf("failure: utime [\n" \
929 "%s: %s(%d) %s %s(%d)\n]\n", \
930 __location__, \
931 #a, (int)a, #c, #b, (int)b); \
932 return false; \
934 } while(0)
935 #define EQUAL_VAL(a,b) CMP_VAL(a,!=,b)
936 #define GREATER_VAL(a,b) CMP_VAL(a,<=,b)
937 #define LESSER_VAL(a,b) CMP_VAL(a,>=,b)
939 EQUAL_VAL(st2.st_atime, st1.st_atime + 300);
940 EQUAL_VAL(st2.st_mtime, st1.st_mtime - 300);
941 LESSER_VAL(st3.st_atime, st2.st_atime);
942 GREATER_VAL(st3.st_mtime, st2.st_mtime);
944 #undef CMP_VAL
945 #undef EQUAL_VAL
946 #undef GREATER_VAL
947 #undef LESSER_VAL
949 unlink(TESTFILE);
950 printf("success: utime\n");
951 return true;
954 static int test_utimes(void)
956 struct timeval tv[2];
957 struct stat st1, st2;
958 int fd;
960 printf("test: utimes\n");
961 unlink(TESTFILE);
963 fd = open(TESTFILE, O_RDWR|O_CREAT, 0600);
964 if (fd == -1) {
965 printf("failure: utimes [\n"
966 "creating '%s' failed - %s\n]\n",
967 TESTFILE, strerror(errno));
968 return false;
971 if (fstat(fd, &st1) != 0) {
972 printf("failure: utimes [\n"
973 "fstat (1) failed - %s\n]\n",
974 strerror(errno));
975 return false;
978 ZERO_STRUCT(tv);
979 tv[0].tv_sec = st1.st_atime + 300;
980 tv[1].tv_sec = st1.st_mtime - 300;
981 if (utimes(TESTFILE, tv) != 0) {
982 printf("failure: utimes [\n"
983 "utimes(tv) failed - %s\n]\n",
984 strerror(errno));
985 return false;
988 if (fstat(fd, &st2) != 0) {
989 printf("failure: utimes [\n"
990 "fstat (2) failed - %s\n]\n",
991 strerror(errno));
992 return false;
995 #define EQUAL_VAL(a,b) do { \
996 if (a != b) { \
997 printf("failure: utimes [\n" \
998 "%s: %s(%d) != %s(%d)\n]\n", \
999 __location__, \
1000 #a, (int)a, #b, (int)b); \
1001 return false; \
1003 } while(0)
1005 EQUAL_VAL(st2.st_atime, st1.st_atime + 300);
1006 EQUAL_VAL(st2.st_mtime, st1.st_mtime - 300);
1008 #undef EQUAL_VAL
1010 unlink(TESTFILE);
1011 printf("success: utimes\n");
1012 return true;
1015 static int test_memmem(void)
1017 char *s;
1019 printf("test: memmem\n");
1021 s = (char *)memmem("foo", 3, "fo", 2);
1022 if (strcmp(s, "foo") != 0) {
1023 printf(__location__ ": Failed memmem\n");
1024 return false;
1027 s = (char *)memmem("foo", 3, "", 0);
1028 /* it is allowable for this to return NULL (as happens on
1029 FreeBSD) */
1030 if (s && strcmp(s, "foo") != 0) {
1031 printf(__location__ ": Failed memmem\n");
1032 return false;
1035 s = (char *)memmem("foo", 4, "o", 1);
1036 if (strcmp(s, "oo") != 0) {
1037 printf(__location__ ": Failed memmem\n");
1038 return false;
1041 s = (char *)memmem("foobarfodx", 11, "fod", 3);
1042 if (strcmp(s, "fodx") != 0) {
1043 printf(__location__ ": Failed memmem\n");
1044 return false;
1047 printf("success: memmem\n");
1049 return true;
1053 bool torture_local_replace(struct torture_context *ctx)
1055 bool ret = true;
1056 ret &= test_ftruncate();
1057 ret &= test_strlcpy();
1058 ret &= test_strlcat();
1059 ret &= test_mktime();
1060 ret &= test_initgroups();
1061 ret &= test_memmove();
1062 ret &= test_strdup();
1063 ret &= test_setlinebuf();
1064 ret &= test_vsyslog();
1065 ret &= test_timegm();
1066 ret &= test_setenv();
1067 ret &= test_strndup();
1068 ret &= test_strnlen();
1069 ret &= test_waitpid();
1070 ret &= test_seteuid();
1071 ret &= test_setegid();
1072 ret &= test_asprintf();
1073 ret &= test_snprintf();
1074 ret &= test_vasprintf();
1075 ret &= test_vsnprintf();
1076 ret &= test_opendir();
1077 ret &= test_readdir();
1078 ret &= test_telldir();
1079 ret &= test_seekdir();
1080 ret &= test_dlopen();
1081 ret &= test_chroot();
1082 ret &= test_bzero();
1083 ret &= test_strerror();
1084 ret &= test_errno();
1085 ret &= test_mkdtemp();
1086 ret &= test_mkstemp();
1087 ret &= test_pread();
1088 ret &= test_pwrite();
1089 ret &= test_inet_ntoa();
1090 ret &= test_strtoll();
1091 ret &= test_strtoull();
1092 ret &= test_va_copy();
1093 ret &= test_FUNCTION();
1094 ret &= test_MIN();
1095 ret &= test_MAX();
1096 ret &= test_socketpair();
1097 ret &= test_strptime();
1098 ret &= test_getifaddrs();
1099 ret &= test_utime();
1100 ret &= test_utimes();
1101 ret &= test_memmem();
1103 return ret;