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