lib:replace: Fix a memleak in test_strdup()
[kseeger/samba-autobuild/.git] / lib / replace / tests / testsuite.c
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  */
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  */
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
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
146 static int test_mktime(void)
147 {
148         /* FIXME */
149         return true;
150 }
151
152 static int test_initgroups(void)
153 {
154         /* FIXME */
155         return true;
156 }
157
158 static int test_memmove(void)
159 {
160         /* FIXME */
161         return true;
162 }
163
164 static int test_strdup(void)
165 {
166         char *x;
167         int cmp;
168
169         printf("test: strdup\n");
170         x = strdup("bla");
171
172         cmp = strcmp("bla", x);
173         if (cmp != 0) {
174                 printf("failure: strdup [\nfailed: expected \"bla\", got \"%s\"\n]\n",
175                            x);
176                 free(x);
177                 return false;
178         }
179         free(x);
180         printf("success: strdup\n");
181         return true;
182 }       
183
184 static int test_setlinebuf(void)
185 {
186         printf("test: setlinebuf\n");
187         setlinebuf(stdout);
188         printf("success: setlinebuf\n");
189         return true;
190 }
191
192 static int test_vsyslog(void)
193 {
194         /* FIXME */
195         return true;
196 }
197
198 static int test_timegm(void)
199 {
200         /* FIXME */
201         return true;
202 }
203
204 static int test_setenv(void)
205 {
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; \
216         } \
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; \
224         } \
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; \
231         } \
232 } while(0)
233
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; \
244         } \
245 } while (0)
246
247 #define SETENVTEST_KEY "SETENVTESTKEY"
248 #define SETENVTEST_VAL "SETENVTESTVAL"
249
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;
262 }
263
264 static int test_strndup(void)
265 {
266         char *x;
267         printf("test: strndup\n");
268         x = strndup("bla", 0);
269         if (strcmp(x, "") != 0) {
270                 printf("failure: strndup [\ninvalid\n]\n");
271                 return false;
272         }
273         free(x);
274         x = strndup("bla", 2);
275         if (strcmp(x, "bl") != 0) {
276                 printf("failure: strndup [\ninvalid\n]\n");
277                 return false;
278         }
279         free(x);
280         x = strndup("bla", 10);
281         if (strcmp(x, "bla") != 0) {
282                 printf("failure: strndup [\ninvalid\n]\n");
283                 free(x);
284                 return false;
285         }
286         free(x);
287         printf("success: strndup\n");
288         return true;
289 }
290
291 static int test_strnlen(void)
292 {
293         printf("test: strnlen\n");
294         if (strnlen("bla", 2) != 2) {
295                 printf("failure: strnlen [\nunexpected length\n]\n");
296                 return false;
297         }
298
299         if (strnlen("some text\n", 0) != 0) {
300                 printf("failure: strnlen [\nunexpected length\n]\n");
301                 return false;
302         }
303
304         if (strnlen("some text", 20) != 9) {
305                 printf("failure: strnlen [\nunexpected length\n]\n");
306                 return false;
307         }
308
309         printf("success: strnlen\n");
310         return true;
311 }
312
313 static int test_waitpid(void)
314 {
315         /* FIXME */
316         return true;
317 }
318
319 static int test_seteuid(void)
320 {
321         /* FIXME */
322         return true;
323 }
324
325 static int test_setegid(void)
326 {
327         /* FIXME */
328         return true;
329 }
330
331 static int test_asprintf(void)
332 {
333         char *x;
334         printf("test: asprintf\n");
335         if (asprintf(&x, "%d", 9) != 1) {
336                 printf("failure: asprintf [\ngenerate asprintf\n]\n");
337                 return false;
338         }
339         if (strcmp(x, "9") != 0) {
340                 printf("failure: asprintf [\ngenerate asprintf\n]\n");
341                 return false;
342         }
343         if (asprintf(&x, "dat%s", "a") != 4) {
344                 printf("failure: asprintf [\ngenerate asprintf\n]\n");
345                 return false;
346         }
347         if (strcmp(x, "data") != 0) {
348                 printf("failure: asprintf [\ngenerate asprintf\n]\n");
349                 return false;
350         }
351         printf("success: asprintf\n");
352         return true;
353 }
354
355 static int test_snprintf(void)
356 {
357         char tmp[10];
358         printf("test: snprintf\n");
359         if (snprintf(tmp, 3, "foo%d", 9) != 4) {
360                 printf("failure: snprintf [\nsnprintf return code failed\n]\n");
361                 return false;
362         }
363
364         if (strcmp(tmp, "fo") != 0) {
365                 printf("failure: snprintf [\nsnprintf failed\n]\n");
366                 return false;
367         }
368
369         printf("success: snprintf\n");
370         return true;
371 }
372
373 static int test_vasprintf(void)
374 {
375         /* FIXME */
376         return true;
377 }
378
379 static int test_vsnprintf(void)
380 {
381         /* FIXME */
382         return true;
383 }
384
385 static int test_opendir(void)
386 {
387         /* FIXME */
388         return true;
389 }
390
391 static int test_readdir(void)
392 {
393         printf("test: readdir\n");
394         if (test_readdir_os2_delete() != 0) {
395                 return false;
396         }
397         printf("success: readdir\n");
398         return true;
399 }
400
401 static int test_telldir(void)
402 {
403         /* FIXME */
404         return true;
405 }
406
407 static int test_seekdir(void)
408 {
409         /* FIXME */
410         return true;
411 }
412
413 static int test_dlopen(void)
414 {
415         /* FIXME: test dlopen, dlsym, dlclose, dlerror */
416         return true;
417 }
418
419
420 static int test_chroot(void)
421 {
422         /* FIXME: chroot() */
423         return true;
424 }
425
426 static int test_bzero(void)
427 {
428         /* FIXME: bzero */
429         return true;
430 }
431
432 static int test_strerror(void)
433 {
434         /* FIXME */
435         return true;
436 }
437
438 static int test_errno(void)
439 {
440         printf("test: errno\n");
441         errno = 3;
442         if (errno != 3) {
443                 printf("failure: errno [\nerrno failed\n]\n");
444                 return false;
445         }
446
447         printf("success: errno\n");
448         return true;
449 }
450
451 static int test_mkdtemp(void)
452 {
453         /* FIXME */
454         return true;
455 }
456
457 static int test_mkstemp(void)
458 {
459         /* FIXME */
460         return true;
461 }
462
463 static int test_pread(void)
464 {
465         /* FIXME */
466         return true;
467 }
468
469 static int test_pwrite(void)
470 {
471         /* FIXME */
472         return true;
473 }
474
475 static int test_inet_ntoa(void)
476 {
477         /* FIXME */
478         return true;
479 }
480
481 #define TEST_STRTO_X(type,fmt,func,str,base,res,diff,rrnoo) do {\
482         type _v; \
483         char _s[64]; \
484         char *_p = NULL;\
485         char *_ep = NULL; \
486         strlcpy(_s, str, sizeof(_s));\
487         if (diff >= 0) { \
488                 _ep = &_s[diff]; \
489         } \
490         errno = 0; \
491         _v = func(_s, &_p, base); \
492         if (errno != rrnoo) { \
493                 printf("failure: %s [\n" \
494                        "\t%s\n" \
495                        "\t%s(\"%s\",%d,%d): " fmt " (=/!)= " fmt "\n" \
496                        "\terrno: %d != %d\n" \
497                        "]\n", \
498                         __STRING(func), __location__, __STRING(func), \
499                        str, diff, base, res, _v, rrnoo, errno); \
500                 return false; \
501         } else if (_v != res) { \
502                 printf("failure: %s [\n" \
503                        "\t%s\n" \
504                        "\t%s(\"%s\",%d,%d): " fmt " != " fmt "\n" \
505                        "]\n", \
506                        __STRING(func), __location__, __STRING(func), \
507                        str, diff, base, res, _v); \
508                 return false; \
509         } else if (_p != _ep) { \
510                 printf("failure: %s [\n" \
511                        "\t%s\n" \
512                        "\t%s(\"%s\",%d,%d): " fmt " (=/!)= " fmt "\n" \
513                        "\tptr: %p - %p = %d != %d\n" \
514                        "]\n", \
515                        __STRING(func), __location__, __STRING(func), \
516                        str, diff, base, res, _v, _ep, _p, (int)(diff - (_ep - _p)), diff); \
517                 return false; \
518         } \
519 } while (0)
520
521 static int test_strtoll(void)
522 {
523         printf("test: strtoll\n");
524
525 #define TEST_STRTOLL(str,base,res,diff,errnoo) TEST_STRTO_X(long long int, "%lld", strtoll,str,base,res,diff,errnoo)
526
527         TEST_STRTOLL("15",      10,     15LL,   2, 0);
528         TEST_STRTOLL("  15",    10,     15LL,   4, 0);
529         TEST_STRTOLL("15",      0,      15LL,   2, 0);
530         TEST_STRTOLL(" 15 ",    0,      15LL,   3, 0);
531         TEST_STRTOLL("+15",     10,     15LL,   3, 0);
532         TEST_STRTOLL("  +15",   10,     15LL,   5, 0);
533         TEST_STRTOLL("+15",     0,      15LL,   3, 0);
534         TEST_STRTOLL(" +15 ",   0,      15LL,   4, 0);
535         TEST_STRTOLL("-15",     10,     -15LL,  3, 0);
536         TEST_STRTOLL("  -15",   10,     -15LL,  5, 0);
537         TEST_STRTOLL("-15",     0,      -15LL,  3, 0);
538         TEST_STRTOLL(" -15 ",   0,      -15LL,  4, 0);
539         TEST_STRTOLL("015",     10,     15LL,   3, 0);
540         TEST_STRTOLL("  015",   10,     15LL,   5, 0);
541         TEST_STRTOLL("015",     0,      13LL,   3, 0);
542         TEST_STRTOLL("  015",   0,      13LL,   5, 0);
543         TEST_STRTOLL("0x15",    10,     0LL,    1, 0);
544         TEST_STRTOLL("  0x15",  10,     0LL,    3, 0);
545         TEST_STRTOLL("0x15",    0,      21LL,   4, 0);
546         TEST_STRTOLL("  0x15",  0,      21LL,   6, 0);
547
548         TEST_STRTOLL("10",      16,     16LL,   2, 0);
549         TEST_STRTOLL("  10 ",   16,     16LL,   4, 0);
550         TEST_STRTOLL("0x10",    16,     16LL,   4, 0);
551         TEST_STRTOLL("0x10",    0,      16LL,   4, 0);
552         TEST_STRTOLL(" 0x10 ",  0,      16LL,   5, 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("-10",     16,     -16LL,  3, 0);
559         TEST_STRTOLL("  -10 ",  16,     -16LL,  5, 0);
560         TEST_STRTOLL("-0x10",   16,     -16LL,  5, 0);
561         TEST_STRTOLL("-0x10",   0,      -16LL,  5, 0);
562         TEST_STRTOLL(" -0x10 ", 0,      -16LL,  6, 0);
563         TEST_STRTOLL("010",     16,     16LL,   3, 0);
564         TEST_STRTOLL("  010 ",  16,     16LL,   5, 0);
565         TEST_STRTOLL("-010",    16,     -16LL,  4, 0);
566
567         TEST_STRTOLL("11",      8,      9LL,    2, 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);
573
574         TEST_STRTOLL("011",     8,      9LL,    3, 0);
575         TEST_STRTOLL("011",     0,      9LL,    3, 0);
576         TEST_STRTOLL("-11",     8,      -9LL,   3, 0);
577         TEST_STRTOLL("-011",    8,      -9LL,   4, 0);
578         TEST_STRTOLL("-011",    0,      -9LL,   4, 0);
579
580         TEST_STRTOLL("Text",    0,      0LL,    0, 0);
581
582         TEST_STRTOLL("9223372036854775807",     10,     9223372036854775807LL,  19, 0);
583         TEST_STRTOLL("9223372036854775807",     0,      9223372036854775807LL,  19, 0);
584         TEST_STRTOLL("9223372036854775808",     0,      9223372036854775807LL,  19, ERANGE);
585         TEST_STRTOLL("9223372036854775808",     10,     9223372036854775807LL,  19, ERANGE);
586         TEST_STRTOLL("0x7FFFFFFFFFFFFFFF",      0,      9223372036854775807LL,  18, 0);
587         TEST_STRTOLL("0x7FFFFFFFFFFFFFFF",      16,     9223372036854775807LL,  18, 0);
588         TEST_STRTOLL("7FFFFFFFFFFFFFFF",        16,     9223372036854775807LL,  16, 0);
589         TEST_STRTOLL("0x8000000000000000",      0,      9223372036854775807LL,  18, ERANGE);
590         TEST_STRTOLL("0x8000000000000000",      16,     9223372036854775807LL,  18, ERANGE);
591         TEST_STRTOLL("80000000000000000",       16,     9223372036854775807LL,  17, ERANGE);
592         TEST_STRTOLL("0777777777777777777777",  0,      9223372036854775807LL,  22, 0);
593         TEST_STRTOLL("0777777777777777777777",  8,      9223372036854775807LL,  22, 0);
594         TEST_STRTOLL("777777777777777777777",   8,      9223372036854775807LL,  21, 0);
595         TEST_STRTOLL("01000000000000000000000", 0,      9223372036854775807LL,  23, ERANGE);
596         TEST_STRTOLL("01000000000000000000000", 8,      9223372036854775807LL,  23, ERANGE);
597         TEST_STRTOLL("1000000000000000000000",  8,      9223372036854775807LL,  22, ERANGE);
598
599         TEST_STRTOLL("-9223372036854775808",    10,     -9223372036854775807LL -1,      20, 0);
600         TEST_STRTOLL("-9223372036854775808",    0,      -9223372036854775807LL -1,      20, 0);
601         TEST_STRTOLL("-9223372036854775809",    0,      -9223372036854775807LL -1,      20, ERANGE);
602         TEST_STRTOLL("-9223372036854775809",    10,     -9223372036854775807LL -1,      20, ERANGE);
603         TEST_STRTOLL("-0x8000000000000000",     0,      -9223372036854775807LL -1,      19, 0);
604         TEST_STRTOLL("-0x8000000000000000",     16,     -9223372036854775807LL -1,      19, 0);
605         TEST_STRTOLL("-8000000000000000",       16,     -9223372036854775807LL -1,      17, 0);
606         TEST_STRTOLL("-0x8000000000000001",     0,      -9223372036854775807LL -1,      19, ERANGE);
607         TEST_STRTOLL("-0x8000000000000001",     16,     -9223372036854775807LL -1,      19, ERANGE);
608         TEST_STRTOLL("-80000000000000001",      16,     -9223372036854775807LL -1,      18, ERANGE);
609         TEST_STRTOLL("-01000000000000000000000",0,      -9223372036854775807LL -1,      24, 0);
610         TEST_STRTOLL("-01000000000000000000000",8,      -9223372036854775807LL -1,      24, 0);
611         TEST_STRTOLL("-1000000000000000000000", 8,      -9223372036854775807LL -1,      23, 0);
612         TEST_STRTOLL("-01000000000000000000001",0,      -9223372036854775807LL -1,      24, ERANGE);
613         TEST_STRTOLL("-01000000000000000000001",8,      -9223372036854775807LL -1,      24, ERANGE);
614         TEST_STRTOLL("-1000000000000000000001", 8,      -9223372036854775807LL -1,      23, ERANGE);
615
616         printf("success: strtoll\n");
617         return true;
618 }
619
620 static int test_strtoull(void)
621 {
622         printf("test: strtoull\n");
623
624 #define TEST_STRTOULL(str,base,res,diff,errnoo) TEST_STRTO_X(long long unsigned int,"%llu",strtoull,str,base,res,diff,errnoo)
625
626         TEST_STRTOULL("15",     10,     15LLU,  2, 0);
627         TEST_STRTOULL("  15",   10,     15LLU,  4, 0);
628         TEST_STRTOULL("15",     0,      15LLU,  2, 0);
629         TEST_STRTOULL(" 15 ",   0,      15LLU,  3, 0);
630         TEST_STRTOULL("+15",    10,     15LLU,  3, 0);
631         TEST_STRTOULL("  +15",  10,     15LLU,  5, 0);
632         TEST_STRTOULL("+15",    0,      15LLU,  3, 0);
633         TEST_STRTOULL(" +15 ",  0,      15LLU,  4, 0);
634         TEST_STRTOULL("-15",    10,     18446744073709551601LLU,        3, 0);
635         TEST_STRTOULL("  -15",  10,     18446744073709551601LLU,        5, 0);
636         TEST_STRTOULL("-15",    0,      18446744073709551601LLU,        3, 0);
637         TEST_STRTOULL(" -15 ",  0,      18446744073709551601LLU,        4, 0);
638         TEST_STRTOULL("015",    10,     15LLU,  3, 0);
639         TEST_STRTOULL("  015",  10,     15LLU,  5, 0);
640         TEST_STRTOULL("015",    0,      13LLU,  3, 0);
641         TEST_STRTOULL("  015",  0,      13LLU,  5, 0);
642         TEST_STRTOULL("0x15",   10,     0LLU,   1, 0);
643         TEST_STRTOULL("  0x15", 10,     0LLU,   3, 0);
644         TEST_STRTOULL("0x15",   0,      21LLU,  4, 0);
645         TEST_STRTOULL("  0x15", 0,      21LLU,  6, 0);
646
647         TEST_STRTOULL("10",     16,     16LLU,  2, 0);
648         TEST_STRTOULL("  10 ",  16,     16LLU,  4, 0);
649         TEST_STRTOULL("0x10",   16,     16LLU,  4, 0);
650         TEST_STRTOULL("0x10",   0,      16LLU,  4, 0);
651         TEST_STRTOULL(" 0x10 ", 0,      16LLU,  5, 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("-10",    16,     -16LLU, 3, 0);
658         TEST_STRTOULL("  -10 ", 16,     -16LLU, 5, 0);
659         TEST_STRTOULL("-0x10",  16,     -16LLU, 5, 0);
660         TEST_STRTOULL("-0x10",  0,      -16LLU, 5, 0);
661         TEST_STRTOULL(" -0x10 ",        0,      -16LLU, 6, 0);
662         TEST_STRTOULL("010",    16,     16LLU,  3, 0);
663         TEST_STRTOULL("  010 ", 16,     16LLU,  5, 0);
664         TEST_STRTOULL("-010",   16,     -16LLU, 4, 0);
665
666         TEST_STRTOULL("11",     8,      9LLU,   2, 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);
672
673         TEST_STRTOULL("011",    8,      9LLU,   3, 0);
674         TEST_STRTOULL("011",    0,      9LLU,   3, 0);
675         TEST_STRTOULL("-11",    8,      -9LLU,  3, 0);
676         TEST_STRTOULL("-011",   8,      -9LLU,  4, 0);
677         TEST_STRTOULL("-011",   0,      -9LLU,  4, 0);
678
679         TEST_STRTOULL("Text",   0,      0LLU,   0, 0);
680
681         TEST_STRTOULL("9223372036854775807",    10,     9223372036854775807LLU, 19, 0);
682         TEST_STRTOULL("9223372036854775807",    0,      9223372036854775807LLU, 19, 0);
683         TEST_STRTOULL("9223372036854775808",    0,      9223372036854775808LLU, 19, 0);
684         TEST_STRTOULL("9223372036854775808",    10,     9223372036854775808LLU, 19, 0);
685         TEST_STRTOULL("0x7FFFFFFFFFFFFFFF",     0,      9223372036854775807LLU, 18, 0);
686         TEST_STRTOULL("0x7FFFFFFFFFFFFFFF",     16,     9223372036854775807LLU, 18, 0);
687         TEST_STRTOULL("7FFFFFFFFFFFFFFF",       16,     9223372036854775807LLU, 16, 0);
688         TEST_STRTOULL("0x8000000000000000",     0,      9223372036854775808LLU, 18, 0);
689         TEST_STRTOULL("0x8000000000000000",     16,     9223372036854775808LLU, 18, 0);
690         TEST_STRTOULL("8000000000000000",       16,     9223372036854775808LLU, 16, 0);
691         TEST_STRTOULL("0777777777777777777777", 0,      9223372036854775807LLU, 22, 0);
692         TEST_STRTOULL("0777777777777777777777", 8,      9223372036854775807LLU, 22, 0);
693         TEST_STRTOULL("777777777777777777777",  8,      9223372036854775807LLU, 21, 0);
694         TEST_STRTOULL("01000000000000000000000",0,      9223372036854775808LLU, 23, 0);
695         TEST_STRTOULL("01000000000000000000000",8,      9223372036854775808LLU, 23, 0);
696         TEST_STRTOULL("1000000000000000000000", 8,      9223372036854775808LLU, 22, 0);
697
698         TEST_STRTOULL("-9223372036854775808",   10,     9223372036854775808LLU, 20, 0);
699         TEST_STRTOULL("-9223372036854775808",   0,      9223372036854775808LLU, 20, 0);
700         TEST_STRTOULL("-9223372036854775809",   0,      9223372036854775807LLU, 20, 0);
701         TEST_STRTOULL("-9223372036854775809",   10,     9223372036854775807LLU, 20, 0);
702         TEST_STRTOULL("-0x8000000000000000",    0,      9223372036854775808LLU, 19, 0);
703         TEST_STRTOULL("-0x8000000000000000",    16,     9223372036854775808LLU, 19, 0);
704         TEST_STRTOULL("-8000000000000000",      16,     9223372036854775808LLU, 17, 0);
705         TEST_STRTOULL("-0x8000000000000001",    0,      9223372036854775807LLU, 19, 0);
706         TEST_STRTOULL("-0x8000000000000001",    16,     9223372036854775807LLU, 19, 0);
707         TEST_STRTOULL("-8000000000000001",      16,     9223372036854775807LLU, 17, 0);
708         TEST_STRTOULL("-01000000000000000000000",0,     9223372036854775808LLU, 24, 0);
709         TEST_STRTOULL("-01000000000000000000000",8,     9223372036854775808LLU, 24, 0);
710         TEST_STRTOULL("-1000000000000000000000",8,      9223372036854775808LLU, 23, 0);
711         TEST_STRTOULL("-01000000000000000000001",0,     9223372036854775807LLU, 24, 0);
712         TEST_STRTOULL("-01000000000000000000001",8,     9223372036854775807LLU, 24, 0);
713         TEST_STRTOULL("-1000000000000000000001",8,      9223372036854775807LLU, 23, 0);
714
715         TEST_STRTOULL("18446744073709551615",   0,      18446744073709551615LLU,        20, 0);
716         TEST_STRTOULL("18446744073709551615",   10,     18446744073709551615LLU,        20, 0);
717         TEST_STRTOULL("18446744073709551616",   0,      18446744073709551615LLU,        20, ERANGE);
718         TEST_STRTOULL("18446744073709551616",   10,     18446744073709551615LLU,        20, ERANGE);
719         TEST_STRTOULL("0xFFFFFFFFFFFFFFFF",     0,      18446744073709551615LLU,        18, 0);
720         TEST_STRTOULL("0xFFFFFFFFFFFFFFFF",     16,     18446744073709551615LLU,        18, 0);
721         TEST_STRTOULL("FFFFFFFFFFFFFFFF",       16,     18446744073709551615LLU,        16, 0);
722         TEST_STRTOULL("0x10000000000000000",    0,      18446744073709551615LLU,        19, ERANGE);
723         TEST_STRTOULL("0x10000000000000000",    16,     18446744073709551615LLU,        19, ERANGE);
724         TEST_STRTOULL("10000000000000000",      16,     18446744073709551615LLU,        17, ERANGE);
725         TEST_STRTOULL("01777777777777777777777",0,      18446744073709551615LLU,        23, 0);
726         TEST_STRTOULL("01777777777777777777777",8,      18446744073709551615LLU,        23, 0);
727         TEST_STRTOULL("1777777777777777777777", 8,      18446744073709551615LLU,        22, 0);
728         TEST_STRTOULL("02000000000000000000000",0,      18446744073709551615LLU,        23, ERANGE);
729         TEST_STRTOULL("02000000000000000000000",8,      18446744073709551615LLU,        23, ERANGE);
730         TEST_STRTOULL("2000000000000000000000", 8,      18446744073709551615LLU,        22, ERANGE);
731
732         TEST_STRTOULL("-18446744073709551615",  0,      1LLU,                           21, 0);
733         TEST_STRTOULL("-18446744073709551615",  10,     1LLU,                           21, 0);
734         TEST_STRTOULL("-18446744073709551616",  0,      18446744073709551615LLU,        21, ERANGE);
735         TEST_STRTOULL("-18446744073709551616",  10,     18446744073709551615LLU,        21, ERANGE);
736         TEST_STRTOULL("-0xFFFFFFFFFFFFFFFF",    0,      1LLU,                           19, 0);
737         TEST_STRTOULL("-0xFFFFFFFFFFFFFFFF",    16,     1LLU,                           19, 0);
738         TEST_STRTOULL("-FFFFFFFFFFFFFFFF",      16,     1LLU,                           17, 0);
739         TEST_STRTOULL("-0x10000000000000000",   0,      18446744073709551615LLU,        20, ERANGE);
740         TEST_STRTOULL("-0x10000000000000000",   16,     18446744073709551615LLU,        20, ERANGE);
741         TEST_STRTOULL("-10000000000000000",     16,     18446744073709551615LLU,        18, ERANGE);
742         TEST_STRTOULL("-01777777777777777777777",0,     1LLU,                           24, 0);
743         TEST_STRTOULL("-01777777777777777777777",8,     1LLU,                           24, 0);
744         TEST_STRTOULL("-1777777777777777777777",8,      1LLU,                           23, 0);
745         TEST_STRTOULL("-02000000000000000000000",0,     18446744073709551615LLU,        24, ERANGE);
746         TEST_STRTOULL("-02000000000000000000000",8,     18446744073709551615LLU,        24, ERANGE);
747         TEST_STRTOULL("-2000000000000000000000",8,      18446744073709551615LLU,        23, ERANGE);
748
749         printf("success: strtoull\n");
750         return true;
751 }
752
753 /* 
754 FIXME:
755 Types:
756 bool
757 socklen_t
758 uint{8,16,32,64}_t
759 int{8,16,32,64}_t
760 intptr_t
761
762 Constants:
763 PATH_NAME_MAX
764 UINT{16,32,64}_MAX
765 INT32_MAX
766 */
767
768 static int test_va_copy(void)
769 {
770         /* FIXME */
771         return true;
772 }
773
774 static int test_FUNCTION(void)
775 {
776         printf("test: FUNCTION\n");
777         if (strcmp(__FUNCTION__, "test_FUNCTION") != 0) {
778                 printf("failure: FUNCTION [\nFUNCTION invalid\n]\n");
779                 return false;
780         }
781         printf("success: FUNCTION\n");
782         return true;
783 }
784
785 static int test_MIN(void)
786 {
787         printf("test: MIN\n");
788         if (MIN(20, 1) != 1) {
789                 printf("failure: MIN [\nMIN invalid\n]\n");
790                 return false;
791         }
792         if (MIN(1, 20) != 1) {
793                 printf("failure: MIN [\nMIN invalid\n]\n");
794                 return false;
795         }
796         printf("success: MIN\n");
797         return true;
798 }
799
800 static int test_MAX(void)
801 {
802         printf("test: MAX\n");
803         if (MAX(20, 1) != 20) {
804                 printf("failure: MAX [\nMAX invalid\n]\n");
805                 return false;
806         }
807         if (MAX(1, 20) != 20) {
808                 printf("failure: MAX [\nMAX invalid\n]\n");
809                 return false;
810         }
811         printf("success: MAX\n");
812         return true;
813 }
814
815 static int test_socketpair(void)
816 {
817         int sock[2];
818         char buf[20];
819
820         printf("test: socketpair\n");
821
822         if (socketpair(AF_UNIX, SOCK_STREAM, 0, sock) == -1) {
823                 printf("failure: socketpair [\n"
824                            "socketpair() failed\n"
825                            "]\n");
826                 return false;
827         }
828
829         if (write(sock[1], "automatisch", 12) == -1) {
830                 printf("failure: socketpair [\n"
831                            "write() failed: %s\n"
832                            "]\n", strerror(errno));
833                 return false;
834         }
835
836         if (read(sock[0], buf, 12) == -1) {
837                 printf("failure: socketpair [\n"
838                            "read() failed: %s\n"
839                            "]\n", strerror(errno));
840                 return false;
841         }
842
843         if (strcmp(buf, "automatisch") != 0) {
844                 printf("failure: socketpair [\n"
845                            "expected: automatisch, got: %s\n"
846                            "]\n", buf);
847                 return false;
848         }
849
850         printf("success: socketpair\n");
851
852         return true;
853 }
854
855 extern int libreplace_test_strptime(void);
856
857 static int test_strptime(void)
858 {
859         return libreplace_test_strptime();
860 }
861
862 extern int getifaddrs_test(void);
863
864 static int test_getifaddrs(void)
865 {
866
867         printf("test: getifaddrs\n");
868
869         if (getifaddrs_test() != 0) {
870                 printf("failure: getifaddrs\n");
871                 return false;
872         }
873
874         printf("success: getifaddrs\n");
875         return true;
876 }
877
878 static int test_utime(void)
879 {
880         struct utimbuf u;
881         struct stat st1, st2, st3;
882         int fd;
883
884         printf("test: utime\n");
885         unlink(TESTFILE);
886
887         fd = open(TESTFILE, O_RDWR|O_CREAT, 0600);
888         if (fd == -1) {
889                 printf("failure: utime [\n"
890                        "creating '%s' failed - %s\n]\n",
891                        TESTFILE, strerror(errno));
892                 return false;
893         }
894
895         if (fstat(fd, &st1) != 0) {
896                 printf("failure: utime [\n"
897                        "fstat (1) failed - %s\n]\n",
898                        strerror(errno));
899                 close(fd);
900                 return false;
901         }
902
903         u.actime = st1.st_atime + 300;
904         u.modtime = st1.st_mtime - 300;
905         if (utime(TESTFILE, &u) != 0) {
906                 printf("failure: utime [\n"
907                        "utime(&u) failed - %s\n]\n",
908                        strerror(errno));
909                 close(fd);
910                 return false;
911         }
912
913         if (fstat(fd, &st2) != 0) {
914                 printf("failure: utime [\n"
915                        "fstat (2) failed - %s\n]\n",
916                        strerror(errno));
917                 close(fd);
918                 return false;
919         }
920
921         if (utime(TESTFILE, NULL) != 0) {
922                 printf("failure: utime [\n"
923                        "utime(NULL) failed - %s\n]\n",
924                        strerror(errno));
925                 close(fd);
926                 return false;
927         }
928
929         if (fstat(fd, &st3) != 0) {
930                 printf("failure: utime [\n"
931                        "fstat (3) failed - %s\n]\n",
932                        strerror(errno));
933                 close(fd);
934                 return false;
935         }
936
937 #define CMP_VAL(a,c,b) do { \
938         if (a c b) { \
939                 printf("failure: utime [\n" \
940                        "%s: %s(%d) %s %s(%d)\n]\n", \
941                        __location__, \
942                        #a, (int)a, #c, #b, (int)b); \
943                 close(fd); \
944                 return false; \
945         } \
946 } while(0)
947 #define EQUAL_VAL(a,b) CMP_VAL(a,!=,b)
948 #define GREATER_VAL(a,b) CMP_VAL(a,<=,b)
949 #define LESSER_VAL(a,b) CMP_VAL(a,>=,b)
950
951         EQUAL_VAL(st2.st_atime, st1.st_atime + 300);
952         EQUAL_VAL(st2.st_mtime, st1.st_mtime - 300);
953         LESSER_VAL(st3.st_atime, st2.st_atime);
954         GREATER_VAL(st3.st_mtime, st2.st_mtime);
955
956 #undef CMP_VAL
957 #undef EQUAL_VAL
958 #undef GREATER_VAL
959 #undef LESSER_VAL
960
961         unlink(TESTFILE);
962         printf("success: utime\n");
963         close(fd);
964         return true;
965 }
966
967 static int test_utimes(void)
968 {
969         struct timeval tv[2];
970         struct stat st1, st2;
971         int fd;
972
973         printf("test: utimes\n");
974         unlink(TESTFILE);
975
976         fd = open(TESTFILE, O_RDWR|O_CREAT, 0600);
977         if (fd == -1) {
978                 printf("failure: utimes [\n"
979                        "creating '%s' failed - %s\n]\n",
980                        TESTFILE, strerror(errno));
981                 return false;
982         }
983
984         if (fstat(fd, &st1) != 0) {
985                 printf("failure: utimes [\n"
986                        "fstat (1) failed - %s\n]\n",
987                        strerror(errno));
988                 close(fd);
989                 return false;
990         }
991
992         ZERO_STRUCT(tv);
993         tv[0].tv_sec = st1.st_atime + 300;
994         tv[1].tv_sec = st1.st_mtime - 300;
995         if (utimes(TESTFILE, tv) != 0) {
996                 printf("failure: utimes [\n"
997                        "utimes(tv) failed - %s\n]\n",
998                        strerror(errno));
999                 close(fd);
1000                 return false;
1001         }
1002
1003         if (fstat(fd, &st2) != 0) {
1004                 printf("failure: utimes [\n"
1005                        "fstat (2) failed - %s\n]\n",
1006                        strerror(errno));
1007                 close(fd);
1008                 return false;
1009         }
1010
1011 #define EQUAL_VAL(a,b) do { \
1012         if (a != b) { \
1013                 printf("failure: utimes [\n" \
1014                        "%s: %s(%d) != %s(%d)\n]\n", \
1015                        __location__, \
1016                        #a, (int)a, #b, (int)b); \
1017                 close(fd); \
1018                 return false; \
1019         } \
1020 } while(0)
1021
1022         EQUAL_VAL(st2.st_atime, st1.st_atime + 300);
1023         EQUAL_VAL(st2.st_mtime, st1.st_mtime - 300);
1024
1025 #undef EQUAL_VAL
1026
1027         unlink(TESTFILE);
1028         printf("success: utimes\n");
1029         close(fd);
1030         return true;
1031 }
1032
1033 static int test_memmem(void)
1034 {
1035         char *s;
1036
1037         printf("test: memmem\n");
1038
1039         s = (char *)memmem("foo", 3, "fo", 2);
1040         if (strcmp(s, "foo") != 0) {
1041                 printf(__location__ ": Failed memmem\n");
1042                 return false;
1043         }
1044
1045         s = (char *)memmem("foo", 3, "", 0);
1046         /* it is allowable for this to return NULL (as happens on
1047            FreeBSD) */
1048         if (s && strcmp(s, "foo") != 0) {
1049                 printf(__location__ ": Failed memmem\n");
1050                 return false;
1051         }
1052
1053         s = (char *)memmem("foo", 4, "o", 1);
1054         if (strcmp(s, "oo") != 0) {
1055                 printf(__location__ ": Failed memmem\n");
1056                 return false;
1057         }
1058
1059         s = (char *)memmem("foobarfodx", 11, "fod", 3);
1060         if (strcmp(s, "fodx") != 0) {
1061                 printf(__location__ ": Failed memmem\n");
1062                 return false;
1063         }
1064
1065         printf("success: memmem\n");
1066
1067         return true;
1068 }
1069
1070 static bool test_closefrom(void)
1071 {
1072         int i, fd;
1073
1074         for (i=0; i<100; i++) {
1075                 fd = dup(0);
1076                 if (fd == -1) {
1077                         perror("dup failed");
1078                         return false;
1079                 }
1080
1081                 /* 1000 is just an arbitrarily chosen upper bound */
1082
1083                 if (fd >= 1000) {
1084                         printf("fd=%d\n", fd);
1085                         return false;
1086                 }
1087         }
1088
1089         closefrom(3);
1090
1091         for (i=3; i<=fd; i++) {
1092                 off_t off;
1093                 off = lseek(i, 0, SEEK_CUR);
1094                 if ((off != (off_t)-1) || (errno != EBADF)) {
1095                         printf("fd %d not closed\n", i);
1096                         return false;
1097                 }
1098         }
1099
1100         return true;
1101 }
1102
1103 static bool test_array_del_element(void)
1104 {
1105         int a[] = { 1,2,3,4,5 };
1106
1107         printf("test: array_del_element\n");
1108
1109         ARRAY_DEL_ELEMENT(a, 4, ARRAY_SIZE(a));
1110
1111         if ((a[0] != 1) ||
1112             (a[1] != 2) ||
1113             (a[2] != 3) ||
1114             (a[3] != 4) ||
1115             (a[4] != 5)) {
1116                 return false;
1117         }
1118
1119         ARRAY_DEL_ELEMENT(a, 0, ARRAY_SIZE(a));
1120
1121         if ((a[0] != 2) ||
1122             (a[1] != 3) ||
1123             (a[2] != 4) ||
1124             (a[3] != 5) ||
1125             (a[4] != 5)) {
1126                 return false;
1127         }
1128
1129         ARRAY_DEL_ELEMENT(a, 2, ARRAY_SIZE(a));
1130
1131         if ((a[0] != 2) ||
1132             (a[1] != 3) ||
1133             (a[2] != 5) ||
1134             (a[3] != 5) ||
1135             (a[4] != 5)) {
1136                 return false;
1137         }
1138
1139         printf("success: array_del_element\n");
1140
1141         return true;
1142 }
1143
1144 bool torture_local_replace(struct torture_context *ctx)
1145 {
1146         bool ret = true;
1147         ret &= test_ftruncate();
1148         ret &= test_strlcpy();
1149         ret &= test_strlcat();
1150         ret &= test_mktime();
1151         ret &= test_initgroups();
1152         ret &= test_memmove();
1153         ret &= test_strdup();
1154         ret &= test_setlinebuf();
1155         ret &= test_vsyslog();
1156         ret &= test_timegm();
1157         ret &= test_setenv();
1158         ret &= test_strndup();
1159         ret &= test_strnlen();
1160         ret &= test_waitpid();
1161         ret &= test_seteuid();
1162         ret &= test_setegid();
1163         ret &= test_asprintf();
1164         ret &= test_snprintf();
1165         ret &= test_vasprintf();
1166         ret &= test_vsnprintf();
1167         ret &= test_opendir();
1168         ret &= test_readdir();
1169         ret &= test_telldir();
1170         ret &= test_seekdir();
1171         ret &= test_dlopen();
1172         ret &= test_chroot();
1173         ret &= test_bzero();
1174         ret &= test_strerror();
1175         ret &= test_errno();
1176         ret &= test_mkdtemp();
1177         ret &= test_mkstemp();
1178         ret &= test_pread();
1179         ret &= test_pwrite();
1180         ret &= test_inet_ntoa();
1181         ret &= test_strtoll();
1182         ret &= test_strtoull();
1183         ret &= test_va_copy();
1184         ret &= test_FUNCTION();
1185         ret &= test_MIN();
1186         ret &= test_MAX();
1187         ret &= test_socketpair();
1188         ret &= test_strptime();
1189         ret &= test_getifaddrs();
1190         ret &= test_utime();
1191         ret &= test_utimes();
1192         ret &= test_memmem();
1193         ret &= test_closefrom();
1194         ret &= test_array_del_element();
1195
1196         return ret;
1197 }