2 Unix SMB/CIFS implementation.
6 Copyright (C) Jelmer Vernooij 2006
8 ** NOTE! The following LGPL license applies to the talloc
9 ** library. This does NOT imply that all of Samba is released
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.
17 This library is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 Lesser General Public License for more details.
22 You should have received a copy of the GNU Lesser General Public
23 License along with this library; if not, write to the Free Software
24 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 we include all the system/ include files here so that libreplace tests
31 them in the build farm
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"
51 #define TESTFILE "testfile.dat"
54 test ftruncate() function
56 static int test_ftruncate(void)
60 const int size = 1234;
61 printf("test: ftruncate\n");
63 fd = open(TESTFILE, O_RDWR|O_CREAT, 0600);
65 printf("failure: ftruncate [\n"
66 "creating '%s' failed - %s\n]\n", TESTFILE, strerror(errno));
69 if (ftruncate(fd, size) != 0) {
70 printf("failure: ftruncate [\n%s\n]\n", strerror(errno));
73 if (fstat(fd, &st) != 0) {
74 printf("failure: ftruncate [\nfstat failed - %s\n]\n", strerror(errno));
77 if (st.st_size != size) {
78 printf("failure: ftruncate [\ngave wrong size %d - expected %d\n]\n",
79 (int)st.st_size, size);
83 printf("success: ftruncate\n");
88 test strlcpy() function.
89 see http://www.gratisoft.us/todd/papers/strlcpy.html
91 static int test_strlcpy(void)
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);
112 printf("success: strlcpy\n");
116 static int test_strlcat(void)
119 printf("test: strlcat\n");
121 if (strlcat(tmp, "bla", 3) != 3) {
122 printf("failure: strlcat [\ninvalid return code\n]\n");
125 if (strcmp(tmp, "bl") != 0) {
126 printf("failure: strlcat [\nexpected \"bl\", got \"%s\"\n]\n",
132 if (strlcat(tmp, "me", 4) != 4) {
133 printf("failure: strlcat [\nexpected \"dam\", got \"%s\"\n]\n",
138 printf("success: strlcat\n");
142 static int test_mktime(void)
148 static int test_initgroups(void)
154 static int test_memmove(void)
160 static int test_strdup(void)
163 printf("test: strdup\n");
165 if (strcmp("bla", x) != 0) {
166 printf("failure: strdup [\nfailed: expected \"bla\", got \"%s\"\n]\n",
171 printf("success: strdup\n");
175 static int test_setlinebuf(void)
177 printf("test: setlinebuf\n");
179 printf("success: setlinebuf\n");
183 static int test_vsyslog(void)
189 static int test_timegm(void)
195 static int test_setenv(void)
197 #define TEST_SETENV(key, value, overwrite, result) do { \
200 _ret = setenv(key, value, overwrite); \
202 printf("failure: setenv [\n" \
203 "setenv(%s, %s, %d) failed\n" \
205 key, value, overwrite); \
210 printf("failure: setenv [\n" \
211 "getenv(%s) returned NULL\n" \
216 if (strcmp(result, _v) != 0) { \
217 printf("failure: setenv [\n" \
218 "getenv(%s): '%s' != '%s'\n" \
225 #define TEST_UNSETENV(key) do { \
230 printf("failure: setenv [\n" \
231 "getenv(%s): NULL != '%s'\n" \
233 SETENVTEST_KEY, _v); \
238 #define SETENVTEST_KEY "SETENVTESTKEY"
239 #define SETENVTEST_VAL "SETENVTESTVAL"
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");
255 static int test_strndup(void)
258 printf("test: strndup\n");
259 x = strndup("bla", 0);
260 if (strcmp(x, "") != 0) {
261 printf("failure: strndup [\ninvalid\n]\n");
265 x = strndup("bla", 2);
266 if (strcmp(x, "bl") != 0) {
267 printf("failure: strndup [\ninvalid\n]\n");
271 x = strndup("bla", 10);
272 if (strcmp(x, "bla") != 0) {
273 printf("failure: strndup [\ninvalid\n]\n");
277 printf("success: strndup\n");
281 static int test_strnlen(void)
283 printf("test: strnlen\n");
284 if (strnlen("bla", 2) != 2) {
285 printf("failure: strnlen [\nunexpected length\n]\n");
289 if (strnlen("some text\n", 0) != 0) {
290 printf("failure: strnlen [\nunexpected length\n]\n");
294 if (strnlen("some text", 20) != 9) {
295 printf("failure: strnlen [\nunexpected length\n]\n");
299 printf("success: strnlen\n");
303 static int test_waitpid(void)
309 static int test_seteuid(void)
315 static int test_setegid(void)
321 static int test_asprintf(void)
324 printf("test: asprintf\n");
325 if (asprintf(&x, "%d", 9) != 1) {
326 printf("failure: asprintf [\ngenerate asprintf\n]\n");
329 if (strcmp(x, "9") != 0) {
330 printf("failure: asprintf [\ngenerate asprintf\n]\n");
333 if (asprintf(&x, "dat%s", "a") != 4) {
334 printf("failure: asprintf [\ngenerate asprintf\n]\n");
337 if (strcmp(x, "data") != 0) {
338 printf("failure: asprintf [\ngenerate asprintf\n]\n");
341 printf("success: asprintf\n");
345 static int test_snprintf(void)
348 printf("test: snprintf\n");
349 if (snprintf(tmp, 3, "foo%d", 9) != 4) {
350 printf("failure: snprintf [\nsnprintf return code failed\n]\n");
354 if (strcmp(tmp, "fo") != 0) {
355 printf("failure: snprintf [\nsnprintf failed\n]\n");
359 printf("success: snprintf\n");
363 static int test_vasprintf(void)
369 static int test_vsnprintf(void)
375 static int test_opendir(void)
381 extern int test_readdir_os2_delete(void);
383 static int test_readdir(void)
385 printf("test: readdir\n");
386 if (test_readdir_os2_delete() != 0) {
389 printf("success: readdir\n");
393 static int test_telldir(void)
399 static int test_seekdir(void)
405 static int test_dlopen(void)
407 /* FIXME: test dlopen, dlsym, dlclose, dlerror */
412 static int test_chroot(void)
414 /* FIXME: chroot() */
418 static int test_bzero(void)
424 static int test_strerror(void)
426 printf("test: strerror\n");
428 printf("failure: sterror\n");
432 static int test_errno(void)
434 printf("test: errno\n");
437 printf("failure: errno [\nerrno failed\n]\n");
441 printf("success: errno\n");
445 static int test_mkdtemp(void)
451 static int test_mkstemp(void)
457 static int test_pread(void)
463 static int test_pwrite(void)
469 static int test_getpass(void)
475 static int test_inet_ntoa(void)
481 static int test_strtoll(void)
483 printf("test: strtoll\n");
484 if (strtoll("15", NULL, 10) != 15) {
485 printf("failure: strtoll [\nstrtoll failed\n]\n");
488 if (strtoll("10", NULL, 16) != 16) {
489 printf("failure: strtoll [\nstrtoll hex failed\n]\n");
492 if (strtoll("11", NULL, 2) != 3) {
493 printf("failure: strtoll [\nstrtoll binary failed\n]\n");
496 printf("success: strtoll\n");
500 static int test_strtoull(void)
522 static int test_va_copy(void)
528 static int test_FUNCTION(void)
530 printf("test: FUNCTION\n");
531 if (strcmp(__FUNCTION__, "test_FUNCTION") != 0) {
532 printf("failure: FAILURE [\nFAILURE invalid\n]\n");
535 printf("success: FUNCTION\n");
539 static int test_MIN(void)
541 printf("test: MIN\n");
542 if (MIN(20, 1) != 1) {
543 printf("failure: MIN [\nMIN invalid\n]\n");
546 if (MIN(1, 20) != 1) {
547 printf("failure: MIN [\nMIN invalid\n]\n");
550 printf("success: MIN\n");
554 static int test_MAX(void)
556 printf("test: MAX\n");
557 if (MAX(20, 1) != 20) {
558 printf("failure: MAX [\nMAX invalid\n]\n");
561 if (MAX(1, 20) != 20) {
562 printf("failure: MAX [\nMAX invalid\n]\n");
565 printf("success: MAX\n");
569 static int test_socketpair(void)
574 printf("test: socketpair\n");
576 if (socketpair(AF_UNIX, SOCK_STREAM, 0, sock) == -1) {
577 printf("failure: socketpair [\n"
578 "socketpair() failed\n"
583 if (write(sock[1], "automatisch", 12) == -1) {
584 printf("failure: socketpair [\n"
585 "write() failed: %s\n"
586 "]\n", strerror(errno));
590 if (read(sock[0], buf, 12) == -1) {
591 printf("failure: socketpair [\n"
592 "read() failed: %s\n"
593 "]\n", strerror(errno));
597 if (strcmp(buf, "automatisch") != 0) {
598 printf("failure: socketpair [\n"
599 "expected: automatisch, got: %s\n"
604 printf("success: socketpair\n");
609 extern int libreplace_test_strptime(void);
611 static int test_strptime(void)
613 return libreplace_test_strptime();
616 struct torture_context;
617 bool torture_local_replace(struct torture_context *ctx)
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();
647 ret &= test_strerror();
649 ret &= test_mkdtemp();
650 ret &= test_mkstemp();
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();
662 ret &= test_socketpair();
663 ret &= test_strptime();
671 bool ret = torture_local_replace(NULL);