r22221: merge from samba4:
[sfrench/samba-autobuild/.git] / source / 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 2 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, write to the Free Software
24    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
25 */
26
27 #include "replace.h"
28
29 /*
30   we include all the system/ include files here so that libreplace tests
31   them in the build farm
32 */
33 #include "system/capability.h"
34 #include "system/dir.h"
35 #include "system/filesys.h"
36 #include "system/glob.h"
37 #include "system/iconv.h"
38 #include "system/locale.h"
39 #include "system/network.h"
40 #include "system/passwd.h"
41 #include "system/printing.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   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                 return false;
72         }
73         if (fstat(fd, &st) != 0) {
74                 printf("failure: ftruncate [\nfstat failed - %s\n]\n", strerror(errno));
75                 return false;
76         }
77         if (st.st_size != size) {
78                 printf("failure: ftruncate [\ngave wrong size %d - expected %d\n]\n",
79                        (int)st.st_size, size);
80                 return false;
81         }
82         unlink(TESTFILE);
83         printf("success: ftruncate\n");
84         return true;
85 }
86
87 /*
88   test strlcpy() function.
89   see http://www.gratisoft.us/todd/papers/strlcpy.html
90  */
91 static int test_strlcpy(void)
92 {
93         char buf[4];
94         const struct {
95                 const char *src;
96                 size_t result;
97         } tests[] = {
98                 { "abc", 3 },
99                 { "abcdef", 6 },
100                 { "abcd", 4 },
101                 { "", 0 },
102                 { NULL, 0 }
103         };
104         int i;
105         printf("test: strlcpy\n");
106         for (i=0;tests[i].src;i++) {
107                 if (strlcpy(buf, tests[i].src, sizeof(buf)) != tests[i].result) {
108                         printf("failure: strlcpy [\ntest %d failed\n]\n", i);
109                         return false;
110                 }
111         }
112         printf("success: strlcpy\n");
113         return true;
114 }
115
116 static int test_strlcat(void)
117 {
118         char tmp[10];
119         printf("test: strlcat\n");
120         strcpy(tmp, "");
121         if (strlcat(tmp, "bla", 3) != 3) {
122                 printf("failure: strlcat [\ninvalid return code\n]\n");
123                 return false;
124         }
125         if (strcmp(tmp, "bl") != 0) {
126                 printf("failure: strlcat [\nexpected \"bl\", got \"%s\"\n]\n", 
127                            tmp);
128                 return false;
129         }
130
131         strcpy(tmp, "da");
132         if (strlcat(tmp, "me", 4) != 4) {
133                 printf("failure: strlcat [\nexpected \"dam\", got \"%s\"\n]\n",
134                            tmp);
135                 return false;
136         }
137
138         printf("success: strlcat\n");
139         return true;
140 }
141
142 static int test_mktime(void)
143 {
144         /* FIXME */
145         return true;
146 }
147
148 static int test_initgroups(void)
149 {
150         /* FIXME */
151         return true;
152 }
153
154 static int test_memmove(void)
155 {
156         /* FIXME */
157         return true;
158 }
159
160 static int test_strdup(void)
161 {
162         char *x;
163         printf("test: strdup\n");
164         x = strdup("bla");
165         if (strcmp("bla", x) != 0) {
166                 printf("failure: strdup [\nfailed: expected \"bla\", got \"%s\"\n]\n",
167                            x);
168                 return false;
169         }
170         free(x);
171         printf("success: strdup\n");
172         return true;
173 }       
174
175 static int test_setlinebuf(void)
176 {
177         printf("test: setlinebuf\n");
178         setlinebuf(stdout);
179         printf("success: setlinebuf\n");
180         return true;
181 }
182
183 static int test_vsyslog(void)
184 {
185         /* FIXME */
186         return true;
187 }
188
189 static int test_timegm(void)
190 {
191         /* FIXME */
192         return true;
193 }
194
195 static int test_setenv(void)
196 {
197 #define TEST_SETENV(key, value, overwrite, result) do { \
198         int _ret; \
199         char *_v; \
200         _ret = setenv(key, value, overwrite); \
201         if (_ret != 0) { \
202                 printf("failure: setenv [\n" \
203                         "setenv(%s, %s, %d) failed\n" \
204                         "]\n", \
205                         key, value, overwrite); \
206                 return false; \
207         } \
208         _v=getenv(key); \
209         if (!_v) { \
210                 printf("failure: setenv [\n" \
211                         "getenv(%s) returned NULL\n" \
212                         "]\n", \
213                         key); \
214                 return false; \
215         } \
216         if (strcmp(result, _v) != 0) { \
217                 printf("failure: setenv [\n" \
218                         "getenv(%s): '%s' != '%s'\n" \
219                         "]\n", \
220                         key, result, _v); \
221                 return false; \
222         } \
223 } while(0)
224
225 #define TEST_UNSETENV(key) do { \
226         char *_v; \
227         unsetenv(key); \
228         _v=getenv(key); \
229         if (_v) { \
230                 printf("failure: setenv [\n" \
231                         "getenv(%s): NULL != '%s'\n" \
232                         "]\n", \
233                         SETENVTEST_KEY, _v); \
234                 return false; \
235         } \
236 } while (0)
237
238 #define SETENVTEST_KEY "SETENVTESTKEY"
239 #define SETENVTEST_VAL "SETENVTESTVAL"
240
241         printf("test: setenv\n");
242         TEST_SETENV(SETENVTEST_KEY, SETENVTEST_VAL"1", 0, SETENVTEST_VAL"1");
243         TEST_SETENV(SETENVTEST_KEY, SETENVTEST_VAL"2", 0, SETENVTEST_VAL"1");
244         TEST_SETENV(SETENVTEST_KEY, SETENVTEST_VAL"3", 1, SETENVTEST_VAL"3");
245         TEST_SETENV(SETENVTEST_KEY, SETENVTEST_VAL"4", 1, SETENVTEST_VAL"4");
246         TEST_UNSETENV(SETENVTEST_KEY);
247         TEST_UNSETENV(SETENVTEST_KEY);
248         TEST_SETENV(SETENVTEST_KEY, SETENVTEST_VAL"5", 0, SETENVTEST_VAL"5");
249         TEST_UNSETENV(SETENVTEST_KEY);
250         TEST_UNSETENV(SETENVTEST_KEY);
251         printf("success: setenv\n");
252         return true;
253 }
254
255 static int test_strndup(void)
256 {
257         char *x;
258         printf("test: strndup\n");
259         x = strndup("bla", 0);
260         if (strcmp(x, "") != 0) {
261                 printf("failure: strndup [\ninvalid\n]\n");
262                 return false;
263         }
264         free(x);
265         x = strndup("bla", 2);
266         if (strcmp(x, "bl") != 0) {
267                 printf("failure: strndup [\ninvalid\n]\n");
268                 return false;
269         }
270         free(x);
271         x = strndup("bla", 10);
272         if (strcmp(x, "bla") != 0) {
273                 printf("failure: strndup [\ninvalid\n]\n");
274                 return false;
275         }
276         free(x);
277         printf("success: strndup\n");
278         return true;
279 }
280
281 static int test_strnlen(void)
282 {
283         printf("test: strnlen\n");
284         if (strnlen("bla", 2) != 2) {
285                 printf("failure: strnlen [\nunexpected length\n]\n");
286                 return false;
287         }
288
289         if (strnlen("some text\n", 0) != 0) {
290                 printf("failure: strnlen [\nunexpected length\n]\n");
291                 return false;
292         }
293
294         if (strnlen("some text", 20) != 9) {
295                 printf("failure: strnlen [\nunexpected length\n]\n");
296                 return false;
297         }
298
299         printf("success: strnlen\n");
300         return true;
301 }
302
303 static int test_waitpid(void)
304 {
305         /* FIXME */
306         return true;
307 }
308
309 static int test_seteuid(void)
310 {
311         /* FIXME */
312         return true;
313 }
314
315 static int test_setegid(void)
316 {
317         /* FIXME */
318         return true;
319 }
320
321 static int test_asprintf(void)
322 {
323         char *x;
324         printf("test: asprintf\n");
325         if (asprintf(&x, "%d", 9) != 1) {
326                 printf("failure: asprintf [\ngenerate asprintf\n]\n");
327                 return false;
328         }
329         if (strcmp(x, "9") != 0) {
330                 printf("failure: asprintf [\ngenerate asprintf\n]\n");
331                 return false;
332         }
333         if (asprintf(&x, "dat%s", "a") != 4) {
334                 printf("failure: asprintf [\ngenerate asprintf\n]\n");
335                 return false;
336         }
337         if (strcmp(x, "data") != 0) {
338                 printf("failure: asprintf [\ngenerate asprintf\n]\n");
339                 return false;
340         }
341         printf("success: asprintf\n");
342         return true;
343 }
344
345 static int test_snprintf(void)
346 {
347         char tmp[10];
348         printf("test: snprintf\n");
349         if (snprintf(tmp, 3, "foo%d", 9) != 4) {
350                 printf("failure: snprintf [\nsnprintf return code failed\n]\n");
351                 return false;
352         }
353
354         if (strcmp(tmp, "fo") != 0) {
355                 printf("failure: snprintf [\nsnprintf failed\n]\n");
356                 return false;
357         }
358
359         printf("success: snprintf\n");
360         return true;
361 }
362
363 static int test_vasprintf(void)
364 {
365         /* FIXME */
366         return true;
367 }
368
369 static int test_vsnprintf(void)
370 {
371         /* FIXME */
372         return true;
373 }
374
375 static int test_opendir(void)
376 {
377         /* FIXME */
378         return true;
379 }
380
381 extern int test_readdir_os2_delete(void);
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         printf("test: strerror\n");
427         /* FIXME */
428         printf("failure: sterror\n");
429         return true;
430 }
431
432 static int test_errno(void)
433 {
434         printf("test: errno\n");
435         errno = 3;
436         if (errno != 3) {
437                 printf("failure: errno [\nerrno failed\n]\n");
438                 return false;
439         }
440
441         printf("success: errno\n");
442         return true;
443 }
444
445 static int test_mkdtemp(void)
446 {
447         /* FIXME */
448         return true;
449 }
450
451 static int test_mkstemp(void)
452 {
453         /* FIXME */
454         return true;
455 }
456
457 static int test_pread(void)
458 {
459         /* FIXME */
460         return true;
461 }
462
463 static int test_pwrite(void)
464 {
465         /* FIXME */
466         return true;
467 }
468
469 static int test_getpass(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 static int test_strtoll(void)
482 {
483         printf("test: strtoll\n");
484         if (strtoll("15", NULL, 10) != 15) {
485                 printf("failure: strtoll [\nstrtoll failed\n]\n");
486                 return false;
487         }
488         if (strtoll("10", NULL, 16) != 16) {
489                 printf("failure: strtoll [\nstrtoll hex failed\n]\n");
490                 return false;
491         }
492         if (strtoll("11", NULL, 2) != 3) {
493                 printf("failure: strtoll [\nstrtoll binary failed\n]\n");
494                 return false;
495         }
496         printf("success: strtoll\n");
497         return true;
498 }
499
500 static int test_strtoull(void)
501 {
502         /* FIXME */
503         return true;
504 }
505
506 /* 
507 FIXME:
508 Types:
509 bool
510 socklen_t
511 uint_t
512 uint{8,16,32,64}_t
513 int{8,16,32,64}_t
514 intptr_t
515
516 Constants:
517 PATH_NAME_MAX
518 UINT{16,32,64}_MAX
519 INT32_MAX
520 */
521
522 static int test_va_copy(void)
523 {
524         /* FIXME */
525         return true;
526 }
527
528 static int test_FUNCTION(void)
529 {
530         printf("test: FUNCTION\n");
531         if (strcmp(__FUNCTION__, "test_FUNCTION") != 0) {
532                 printf("failure: FAILURE [\nFAILURE invalid\n]\n");
533                 return false;
534         }
535         printf("success: FUNCTION\n");
536         return true;
537 }
538
539 static int test_MIN(void)
540 {
541         printf("test: MIN\n");
542         if (MIN(20, 1) != 1) {
543                 printf("failure: MIN [\nMIN invalid\n]\n");
544                 return false;
545         }
546         if (MIN(1, 20) != 1) {
547                 printf("failure: MIN [\nMIN invalid\n]\n");
548                 return false;
549         }
550         printf("success: MIN\n");
551         return true;
552 }
553
554 static int test_MAX(void)
555 {
556         printf("test: MAX\n");
557         if (MAX(20, 1) != 20) {
558                 printf("failure: MAX [\nMAX invalid\n]\n");
559                 return false;
560         }
561         if (MAX(1, 20) != 20) {
562                 printf("failure: MAX [\nMAX invalid\n]\n");
563                 return false;
564         }
565         printf("success: MAX\n");
566         return true;
567 }
568
569 static int test_socketpair(void)
570 {
571         int sock[2];
572         char buf[20];
573
574         printf("test: socketpair\n");
575
576         if (socketpair(AF_UNIX, SOCK_STREAM, 0, sock) == -1) {
577                 printf("failure: socketpair [\n"
578                            "socketpair() failed\n"
579                            "]\n");
580                 return false;
581         }
582
583         if (write(sock[1], "automatisch", 12) == -1) {
584                 printf("failure: socketpair [\n"
585                            "write() failed: %s\n"
586                            "]\n", strerror(errno));
587                 return false;
588         }
589
590         if (read(sock[0], buf, 12) == -1) {
591                 printf("failure: socketpair [\n"
592                            "read() failed: %s\n"
593                            "]\n", strerror(errno));
594                 return false;
595         }
596
597         if (strcmp(buf, "automatisch") != 0) {
598                 printf("failure: socketpair [\n"
599                            "expected: automatisch, got: %s\n"
600                            "]\n", buf);
601                 return false;
602         }
603
604         printf("success: socketpair\n");
605
606         return true;
607 }
608
609 extern int libreplace_test_strptime(void);
610
611 static int test_strptime(void)
612 {
613         return libreplace_test_strptime();
614 }
615
616 struct torture_context;
617 bool torture_local_replace(struct torture_context *ctx)
618 {
619         bool ret = true;
620         ret &= test_ftruncate();
621         ret &= test_strlcpy();
622         ret &= test_strlcat();
623         ret &= test_mktime();
624         ret &= test_initgroups();
625         ret &= test_memmove();
626         ret &= test_strdup();
627         ret &= test_setlinebuf();
628         ret &= test_vsyslog();
629         ret &= test_timegm();
630         ret &= test_setenv();
631         ret &= test_strndup();
632         ret &= test_strnlen();
633         ret &= test_waitpid();
634         ret &= test_seteuid();
635         ret &= test_setegid();
636         ret &= test_asprintf();
637         ret &= test_snprintf();
638         ret &= test_vasprintf();
639         ret &= test_vsnprintf();
640         ret &= test_opendir();
641         ret &= test_readdir();
642         ret &= test_telldir();
643         ret &= test_seekdir();
644         ret &= test_dlopen();
645         ret &= test_chroot();
646         ret &= test_bzero();
647         ret &= test_strerror();
648         ret &= test_errno();
649         ret &= test_mkdtemp();
650         ret &= test_mkstemp();
651         ret &= test_pread();
652         ret &= test_pwrite();
653         ret &= test_getpass();
654         ret &= test_inet_ntoa();
655         ret &= test_strtoll();
656         ret &= test_strtoll();
657         ret &= test_strtoull();
658         ret &= test_va_copy();
659         ret &= test_FUNCTION();
660         ret &= test_MIN();
661         ret &= test_MAX();
662         ret &= test_socketpair();
663         ret &= test_strptime();
664
665         return ret;
666 }
667
668 #if _SAMBA_BUILD_<4
669 int main(void)
670 {
671         bool ret = torture_local_replace(NULL);
672         if (ret) 
673                 return 0;
674         return -1;
675 }
676 #endif