2 Unix SMB/CIFS implementation.
4 local testing of talloc routines.
6 Copyright (C) Andrew Tridgell 2004
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
37 typedef enum {False=0,True=1} BOOL;
39 static struct timeval timeval_current(void)
42 gettimeofday(&tv, NULL);
46 static double timeval_elapsed(struct timeval *tv)
48 struct timeval tv2 = timeval_current();
49 return (tv2.tv_sec - tv->tv_sec) +
50 (tv2.tv_usec - tv->tv_usec)*1.0e-6;
53 #if SAMBA_VERSION_MAJOR<4
62 #define CHECK_SIZE(ptr, tsize) do { \
63 if (talloc_total_size(ptr) != (tsize)) { \
64 printf(__location__ " failed: wrong '%s' tree size: got %u expected %u\n", \
66 (unsigned)talloc_total_size(ptr), \
68 talloc_report_full(ptr, stdout); \
73 #define CHECK_BLOCKS(ptr, tblocks) do { \
74 if (talloc_total_blocks(ptr) != (tblocks)) { \
75 printf(__location__ " failed: wrong '%s' tree blocks: got %u expected %u\n", \
77 (unsigned)talloc_total_blocks(ptr), \
79 talloc_report_full(ptr, stdout); \
88 static BOOL test_ref1(void)
90 void *root, *p1, *p2, *ref, *r1;
92 printf("TESTING SINGLE REFERENCE FREE\n");
94 root = talloc_named_const(NULL, 0, "root");
95 p1 = talloc_named_const(root, 1, "p1");
96 p2 = talloc_named_const(p1, 1, "p2");
97 talloc_named_const(p1, 1, "x1");
98 talloc_named_const(p1, 2, "x2");
99 talloc_named_const(p1, 3, "x3");
101 r1 = talloc_named_const(root, 1, "r1");
102 ref = talloc_reference(r1, p2);
103 talloc_report_full(root, stdout);
109 printf("Freeing p2\n");
111 talloc_report_full(root, stdout);
117 printf("Freeing p1\n");
119 talloc_report_full(root, stdout);
123 printf("Freeing r1\n");
125 talloc_report_full(NULL, stdout);
127 printf("Testing NULL\n");
128 if (talloc_reference(root, NULL)) {
132 CHECK_BLOCKS(root, 1);
144 static BOOL test_ref2(void)
146 void *root, *p1, *p2, *ref, *r1;
148 printf("TESTING DOUBLE REFERENCE FREE\n");
150 root = talloc_named_const(NULL, 0, "root");
151 p1 = talloc_named_const(root, 1, "p1");
152 talloc_named_const(p1, 1, "x1");
153 talloc_named_const(p1, 1, "x2");
154 talloc_named_const(p1, 1, "x3");
155 p2 = talloc_named_const(p1, 1, "p2");
157 r1 = talloc_named_const(root, 1, "r1");
158 ref = talloc_reference(r1, p2);
159 talloc_report_full(root, stdout);
165 printf("Freeing ref\n");
167 talloc_report_full(root, stdout);
173 printf("Freeing p2\n");
175 talloc_report_full(root, stdout);
180 printf("Freeing p1\n");
182 talloc_report_full(root, stdout);
186 printf("Freeing r1\n");
188 talloc_report_full(root, stdout);
200 static BOOL test_ref3(void)
202 void *root, *p1, *p2, *ref, *r1;
204 printf("TESTING PARENT REFERENCE FREE\n");
206 root = talloc_named_const(NULL, 0, "root");
207 p1 = talloc_named_const(root, 1, "p1");
208 p2 = talloc_named_const(root, 1, "p2");
209 r1 = talloc_named_const(p1, 1, "r1");
210 ref = talloc_reference(p2, r1);
211 talloc_report_full(root, stdout);
217 printf("Freeing p1\n");
219 talloc_report_full(root, stdout);
224 printf("Freeing p2\n");
226 talloc_report_full(root, stdout);
238 static BOOL test_ref4(void)
240 void *root, *p1, *p2, *ref, *r1;
242 printf("TESTING REFERRER REFERENCE FREE\n");
244 root = talloc_named_const(NULL, 0, "root");
245 p1 = talloc_named_const(root, 1, "p1");
246 talloc_named_const(p1, 1, "x1");
247 talloc_named_const(p1, 1, "x2");
248 talloc_named_const(p1, 1, "x3");
249 p2 = talloc_named_const(p1, 1, "p2");
251 r1 = talloc_named_const(root, 1, "r1");
252 ref = talloc_reference(r1, p2);
253 talloc_report_full(root, stdout);
259 printf("Freeing r1\n");
261 talloc_report_full(root, stdout);
266 printf("Freeing p2\n");
268 talloc_report_full(root, stdout);
272 printf("Freeing p1\n");
274 talloc_report_full(root, stdout);
287 static BOOL test_unlink1(void)
289 void *root, *p1, *p2, *ref, *r1;
291 printf("TESTING UNLINK\n");
293 root = talloc_named_const(NULL, 0, "root");
294 p1 = talloc_named_const(root, 1, "p1");
295 talloc_named_const(p1, 1, "x1");
296 talloc_named_const(p1, 1, "x2");
297 talloc_named_const(p1, 1, "x3");
298 p2 = talloc_named_const(p1, 1, "p2");
300 r1 = talloc_named_const(p1, 1, "r1");
301 ref = talloc_reference(r1, p2);
302 talloc_report_full(root, stdout);
308 printf("Unreferencing r1\n");
309 talloc_unlink(r1, p2);
310 talloc_report_full(root, stdout);
316 printf("Freeing p1\n");
318 talloc_report_full(root, stdout);
327 static int fail_destructor(void *ptr)
333 miscellaneous tests to try to get a higher test coverage percentage
335 static BOOL test_misc(void)
341 printf("TESTING MISCELLANEOUS\n");
343 root = talloc_new(NULL);
345 p1 = talloc_size(root, 0x7fffffff);
347 printf("failed: large talloc allowed\n");
351 p1 = talloc_strdup(root, "foo");
352 talloc_increase_ref_count(p1);
353 talloc_increase_ref_count(p1);
354 talloc_increase_ref_count(p1);
356 CHECK_BLOCKS(root, 2);
359 CHECK_BLOCKS(root, 2);
360 talloc_unlink(NULL, p1);
362 CHECK_BLOCKS(root, 2);
363 p2 = talloc_strdup(p1, "foo");
364 if (talloc_unlink(root, p2) != -1) {
365 printf("failed: talloc_unlink() of non-reference context should return -1\n");
368 if (talloc_unlink(p1, p2) != 0) {
369 printf("failed: talloc_unlink() of parent should succeed\n");
374 CHECK_BLOCKS(root, 2);
376 talloc_set_name(p1, "my name is %s", "foo");
377 if (strcmp(talloc_get_name(p1), "my name is foo") != 0) {
378 printf("failed: wrong name after talloc_set_name\n");
382 CHECK_BLOCKS(root, 3);
384 talloc_set_name_const(p1, NULL);
385 if (strcmp(talloc_get_name(p1), "UNNAMED") != 0) {
386 printf("failed: wrong name after talloc_set_name(NULL)\n");
390 CHECK_BLOCKS(root, 3);
393 if (talloc_free(NULL) != -1) {
394 printf("talloc_free(NULL) should give -1\n");
398 talloc_set_destructor(p1, fail_destructor);
399 if (talloc_free(p1) != -1) {
400 printf("Failed destructor should cause talloc_free to fail\n");
403 talloc_set_destructor(p1, NULL);
405 talloc_report(root, stdout);
408 p2 = talloc_zero_size(p1, 20);
410 printf("Failed to give zero memory\n");
415 if (talloc_strdup(root, NULL) != NULL) {
416 printf("failed: strdup on NULL should give NULL\n");
420 p2 = talloc_strndup(p1, "foo", 2);
421 if (strcmp("fo", p2) != 0) {
422 printf("failed: strndup doesn't work\n");
425 p2 = talloc_asprintf_append(p2, "o%c", 'd');
426 if (strcmp("food", p2) != 0) {
427 printf("failed: talloc_asprintf_append doesn't work\n");
433 p2 = talloc_asprintf_append(NULL, "hello %s", "world");
434 if (strcmp("hello world", p2) != 0) {
435 printf("failed: talloc_asprintf_append doesn't work\n");
442 d = talloc_array(p1, double, 0x20000000);
444 printf("failed: integer overflow not detected\n");
448 d = talloc_realloc(p1, d, double, 0x20000000);
450 printf("failed: integer overflow not detected\n");
455 CHECK_BLOCKS(root, 1);
457 p1 = talloc_named(root, 100, "%d bytes", 100);
459 CHECK_BLOCKS(root, 3);
460 talloc_unlink(root, p1);
462 p1 = talloc_init("%d bytes", 200);
463 p2 = talloc_asprintf(p1, "my test '%s'", "string");
466 CHECK_BLOCKS(root, 1);
467 talloc_unlink(NULL, p1);
469 p1 = talloc_named_const(root, 10, "p1");
470 p2 = talloc_named_const(root, 20, "p2");
471 talloc_reference(p1, p2);
472 talloc_report_full(root, stdout);
473 talloc_unlink(root, p2);
474 talloc_report_full(root, stdout);
477 CHECK_BLOCKS(root, 3);
478 talloc_unlink(p1, p2);
479 talloc_unlink(root, p1);
481 p1 = talloc_named_const(root, 10, "p1");
482 p2 = talloc_named_const(root, 20, "p2");
483 talloc_reference(NULL, p2);
484 talloc_report_full(root, stdout);
485 talloc_unlink(root, p2);
486 talloc_report_full(root, stdout);
489 CHECK_BLOCKS(root, 2);
490 talloc_unlink(NULL, p2);
491 talloc_unlink(root, p1);
493 /* Test that talloc_unlink is a no-op */
495 if (talloc_unlink(root, NULL) != -1) {
496 printf("failed: talloc_unlink(root, NULL) == -1\n");
500 talloc_report(root, stdout);
501 talloc_report(NULL, stdout);
509 talloc_enable_leak_report();
510 talloc_enable_leak_report_full();
519 static BOOL test_realloc(void)
521 void *root, *p1, *p2;
523 printf("TESTING REALLOC\n");
525 root = talloc_new(NULL);
527 p1 = talloc_size(root, 10);
530 p1 = talloc_realloc_size(NULL, p1, 20);
535 p2 = talloc_realloc_size(p1, NULL, 30);
539 p2 = talloc_realloc_size(p1, p2, 40);
542 CHECK_SIZE(root, 60);
545 p1 = talloc_realloc_size(NULL, p1, 20);
548 talloc_increase_ref_count(p2);
549 if (talloc_realloc_size(NULL, p2, 5) != NULL) {
550 printf("failed: talloc_realloc() on a referenced pointer should fail\n");
555 talloc_realloc_size(NULL, p2, 0);
556 talloc_realloc_size(NULL, p2, 0);
559 if (talloc_realloc_size(NULL, p1, 0x7fffffff) != NULL) {
560 printf("failed: oversize talloc should fail\n");
564 talloc_realloc_size(NULL, p1, 0);
566 CHECK_BLOCKS(root, 1);
576 test realloc with a child
578 static BOOL test_realloc_child(void)
585 } **list, **list2, **list3;
589 printf("TESTING REALLOC WITH CHILD\n");
591 root = talloc_new(NULL);
593 el1 = talloc(root, struct el1);
594 el1->list = talloc(el1, struct el2 *);
595 el1->list[0] = talloc(el1->list, struct el2);
596 el1->list[0]->name = talloc_strdup(el1->list[0], "testing");
598 el1->list2 = talloc(el1, struct el2 *);
599 el1->list2[0] = talloc(el1->list2, struct el2);
600 el1->list2[0]->name = talloc_strdup(el1->list2[0], "testing2");
602 el1->list3 = talloc(el1, struct el2 *);
603 el1->list3[0] = talloc(el1->list3, struct el2);
604 el1->list3[0]->name = talloc_strdup(el1->list3[0], "testing2");
606 el2 = talloc(el1->list, struct el2);
607 el2 = talloc(el1->list2, struct el2);
608 el2 = talloc(el1->list3, struct el2);
610 el1->list = talloc_realloc(el1, el1->list, struct el2 *, 100);
611 el1->list2 = talloc_realloc(el1, el1->list2, struct el2 *, 200);
612 el1->list3 = talloc_realloc(el1, el1->list3, struct el2 *, 300);
623 static BOOL test_type(void)
634 printf("TESTING talloc type checking\n");
636 root = talloc_new(NULL);
638 el1 = talloc(root, struct el1);
642 if (talloc_get_type(el1, struct el1) != el1) {
643 printf("type check failed on el1\n");
646 if (talloc_get_type(el1, struct el2) != NULL) {
647 printf("type check failed on el1 with el2\n");
650 talloc_set_type(el1, struct el2);
651 if (talloc_get_type(el1, struct el2) != (struct el2 *)el1) {
652 printf("type set failed on el1 with el2\n");
664 static BOOL test_steal(void)
666 void *root, *p1, *p2;
668 printf("TESTING STEAL\n");
670 root = talloc_new(NULL);
672 p1 = talloc_array(root, char, 10);
675 p2 = talloc_realloc(root, NULL, char, 20);
677 CHECK_SIZE(root, 30);
679 if (talloc_steal(p1, NULL) != NULL) {
680 printf("failed: stealing NULL should give NULL\n");
684 if (talloc_steal(p1, p1) != p1) {
685 printf("failed: stealing to ourselves is a nop\n");
688 CHECK_BLOCKS(root, 3);
689 CHECK_SIZE(root, 30);
691 talloc_steal(NULL, p1);
692 talloc_steal(NULL, p2);
693 CHECK_BLOCKS(root, 1);
697 talloc_steal(root, p2);
698 CHECK_BLOCKS(root, 2);
699 CHECK_SIZE(root, 20);
703 CHECK_BLOCKS(root, 1);
708 p1 = talloc_size(NULL, 3);
716 test talloc_realloc_fn
718 static BOOL test_realloc_fn(void)
722 printf("TESTING talloc_realloc_fn\n");
724 root = talloc_new(NULL);
726 p1 = talloc_realloc_fn(root, NULL, 10);
727 CHECK_BLOCKS(root, 2);
728 CHECK_SIZE(root, 10);
729 p1 = talloc_realloc_fn(root, p1, 20);
730 CHECK_BLOCKS(root, 2);
731 CHECK_SIZE(root, 20);
732 p1 = talloc_realloc_fn(root, p1, 0);
733 CHECK_BLOCKS(root, 1);
743 static BOOL test_unref_reparent(void)
745 void *root, *p1, *p2, *c1;
747 printf("TESTING UNREFERENCE AFTER PARENT FREED\n");
749 root = talloc_named_const(NULL, 0, "root");
750 p1 = talloc_named_const(root, 1, "orig parent");
751 p2 = talloc_named_const(root, 1, "parent by reference");
753 c1 = talloc_named_const(p1, 1, "child");
754 talloc_reference(p2, c1);
757 talloc_unlink(p2, c1);
768 measure the speed of talloc versus malloc
770 static BOOL test_speed(void)
772 void *ctx = talloc_new(NULL);
776 printf("MEASURING TALLOC VS MALLOC SPEED\n");
778 tv = timeval_current();
782 p1 = talloc_size(ctx, count);
783 p2 = talloc_strdup(p1, "foo bar");
784 p3 = talloc_size(p1, 300);
787 } while (timeval_elapsed(&tv) < 5.0);
789 printf("talloc: %.0f ops/sec\n", count/timeval_elapsed(&tv));
793 tv = timeval_current();
798 p2 = strdup("foo bar");
804 } while (timeval_elapsed(&tv) < 5.0);
806 printf("malloc: %.0f ops/sec\n", count/timeval_elapsed(&tv));
812 BOOL torture_local_talloc(void)
820 ret &= test_unlink1();
822 ret &= test_realloc();
823 ret &= test_realloc_child();
825 ret &= test_unref_reparent();
826 ret &= test_realloc_fn();
837 #if !defined(_SAMBA_BUILD_) || ((SAMBA_VERSION_MAJOR==3)&&(SAMBA_VERSION_MINOR<9))
840 if (!torture_local_talloc()) {
841 printf("ERROR: TESTSUIE FAILED\n");