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.
47 static struct timeval timeval_current(void)
50 gettimeofday(&tv, NULL);
54 static double timeval_elapsed(struct timeval *tv)
56 struct timeval tv2 = timeval_current();
57 return (tv2.tv_sec - tv->tv_sec) +
58 (tv2.tv_usec - tv->tv_usec)*1.0e-6;
61 #if SAMBA_VERSION_MAJOR<4
70 #define CHECK_SIZE(ptr, tsize) do { \
71 if (talloc_total_size(ptr) != (tsize)) { \
72 printf(__location__ " failed: wrong '%s' tree size: got %u expected %u\n", \
74 (unsigned)talloc_total_size(ptr), \
76 talloc_report_full(ptr, stdout); \
81 #define CHECK_BLOCKS(ptr, tblocks) do { \
82 if (talloc_total_blocks(ptr) != (tblocks)) { \
83 printf(__location__ " failed: wrong '%s' tree blocks: got %u expected %u\n", \
85 (unsigned)talloc_total_blocks(ptr), \
87 talloc_report_full(ptr, stdout); \
96 static BOOL test_ref1(void)
98 void *root, *p1, *p2, *ref, *r1;
100 printf("TESTING SINGLE REFERENCE FREE\n");
102 root = talloc_named_const(NULL, 0, "root");
103 p1 = talloc_named_const(root, 1, "p1");
104 p2 = talloc_named_const(p1, 1, "p2");
105 talloc_named_const(p1, 1, "x1");
106 talloc_named_const(p1, 2, "x2");
107 talloc_named_const(p1, 3, "x3");
109 r1 = talloc_named_const(root, 1, "r1");
110 ref = talloc_reference(r1, p2);
111 talloc_report_full(root, stdout);
117 printf("Freeing p2\n");
119 talloc_report_full(root, stdout);
125 printf("Freeing p1\n");
127 talloc_report_full(root, stdout);
131 printf("Freeing r1\n");
133 talloc_report_full(NULL, stdout);
135 printf("Testing NULL\n");
136 if (talloc_reference(root, NULL)) {
140 CHECK_BLOCKS(root, 1);
152 static BOOL test_ref2(void)
154 void *root, *p1, *p2, *ref, *r1;
156 printf("TESTING DOUBLE REFERENCE FREE\n");
158 root = talloc_named_const(NULL, 0, "root");
159 p1 = talloc_named_const(root, 1, "p1");
160 talloc_named_const(p1, 1, "x1");
161 talloc_named_const(p1, 1, "x2");
162 talloc_named_const(p1, 1, "x3");
163 p2 = talloc_named_const(p1, 1, "p2");
165 r1 = talloc_named_const(root, 1, "r1");
166 ref = talloc_reference(r1, p2);
167 talloc_report_full(root, stdout);
173 printf("Freeing ref\n");
175 talloc_report_full(root, stdout);
181 printf("Freeing p2\n");
183 talloc_report_full(root, stdout);
188 printf("Freeing p1\n");
190 talloc_report_full(root, stdout);
194 printf("Freeing r1\n");
196 talloc_report_full(root, stdout);
208 static BOOL test_ref3(void)
210 void *root, *p1, *p2, *ref, *r1;
212 printf("TESTING PARENT REFERENCE FREE\n");
214 root = talloc_named_const(NULL, 0, "root");
215 p1 = talloc_named_const(root, 1, "p1");
216 p2 = talloc_named_const(root, 1, "p2");
217 r1 = talloc_named_const(p1, 1, "r1");
218 ref = talloc_reference(p2, r1);
219 talloc_report_full(root, stdout);
225 printf("Freeing p1\n");
227 talloc_report_full(root, stdout);
232 printf("Freeing p2\n");
234 talloc_report_full(root, stdout);
246 static BOOL test_ref4(void)
248 void *root, *p1, *p2, *ref, *r1;
250 printf("TESTING REFERRER REFERENCE FREE\n");
252 root = talloc_named_const(NULL, 0, "root");
253 p1 = talloc_named_const(root, 1, "p1");
254 talloc_named_const(p1, 1, "x1");
255 talloc_named_const(p1, 1, "x2");
256 talloc_named_const(p1, 1, "x3");
257 p2 = talloc_named_const(p1, 1, "p2");
259 r1 = talloc_named_const(root, 1, "r1");
260 ref = talloc_reference(r1, p2);
261 talloc_report_full(root, stdout);
267 printf("Freeing r1\n");
269 talloc_report_full(root, stdout);
274 printf("Freeing p2\n");
276 talloc_report_full(root, stdout);
280 printf("Freeing p1\n");
282 talloc_report_full(root, stdout);
295 static BOOL test_unlink1(void)
297 void *root, *p1, *p2, *ref, *r1;
299 printf("TESTING UNLINK\n");
301 root = talloc_named_const(NULL, 0, "root");
302 p1 = talloc_named_const(root, 1, "p1");
303 talloc_named_const(p1, 1, "x1");
304 talloc_named_const(p1, 1, "x2");
305 talloc_named_const(p1, 1, "x3");
306 p2 = talloc_named_const(p1, 1, "p2");
308 r1 = talloc_named_const(p1, 1, "r1");
309 ref = talloc_reference(r1, p2);
310 talloc_report_full(root, stdout);
316 printf("Unreferencing r1\n");
317 talloc_unlink(r1, p2);
318 talloc_report_full(root, stdout);
324 printf("Freeing p1\n");
326 talloc_report_full(root, stdout);
335 static int fail_destructor(void *ptr)
341 miscellaneous tests to try to get a higher test coverage percentage
343 static BOOL test_misc(void)
349 printf("TESTING MISCELLANEOUS\n");
351 root = talloc_new(NULL);
353 p1 = talloc_size(root, 0x7fffffff);
355 printf("failed: large talloc allowed\n");
359 p1 = talloc_strdup(root, "foo");
360 talloc_increase_ref_count(p1);
361 talloc_increase_ref_count(p1);
362 talloc_increase_ref_count(p1);
364 CHECK_BLOCKS(root, 2);
367 CHECK_BLOCKS(root, 2);
368 talloc_unlink(NULL, p1);
370 CHECK_BLOCKS(root, 2);
371 p2 = talloc_strdup(p1, "foo");
372 if (talloc_unlink(root, p2) != -1) {
373 printf("failed: talloc_unlink() of non-reference context should return -1\n");
376 if (talloc_unlink(p1, p2) != 0) {
377 printf("failed: talloc_unlink() of parent should succeed\n");
382 CHECK_BLOCKS(root, 2);
384 talloc_set_name(p1, "my name is %s", "foo");
385 if (strcmp(talloc_get_name(p1), "my name is foo") != 0) {
386 printf("failed: wrong name after talloc_set_name\n");
390 CHECK_BLOCKS(root, 3);
392 talloc_set_name_const(p1, NULL);
393 if (strcmp(talloc_get_name(p1), "UNNAMED") != 0) {
394 printf("failed: wrong name after talloc_set_name(NULL)\n");
398 CHECK_BLOCKS(root, 3);
401 if (talloc_free(NULL) != -1) {
402 printf("talloc_free(NULL) should give -1\n");
406 talloc_set_destructor(p1, fail_destructor);
407 if (talloc_free(p1) != -1) {
408 printf("Failed destructor should cause talloc_free to fail\n");
411 talloc_set_destructor(p1, NULL);
413 talloc_report(root, stdout);
416 p2 = talloc_zero_size(p1, 20);
418 printf("Failed to give zero memory\n");
423 if (talloc_strdup(root, NULL) != NULL) {
424 printf("failed: strdup on NULL should give NULL\n");
428 p2 = talloc_strndup(p1, "foo", 2);
429 if (strcmp("fo", p2) != 0) {
430 printf("failed: strndup doesn't work\n");
433 p2 = talloc_asprintf_append(p2, "o%c", 'd');
434 if (strcmp("food", p2) != 0) {
435 printf("failed: talloc_asprintf_append doesn't work\n");
441 p2 = talloc_asprintf_append(NULL, "hello %s", "world");
442 if (strcmp("hello world", p2) != 0) {
443 printf("failed: talloc_asprintf_append doesn't work\n");
450 d = talloc_array(p1, double, 0x20000000);
452 printf("failed: integer overflow not detected\n");
456 d = talloc_realloc(p1, d, double, 0x20000000);
458 printf("failed: integer overflow not detected\n");
463 CHECK_BLOCKS(root, 1);
465 p1 = talloc_named(root, 100, "%d bytes", 100);
467 CHECK_BLOCKS(root, 3);
468 talloc_unlink(root, p1);
470 p1 = talloc_init("%d bytes", 200);
471 p2 = talloc_asprintf(p1, "my test '%s'", "string");
474 CHECK_BLOCKS(root, 1);
475 talloc_unlink(NULL, p1);
477 p1 = talloc_named_const(root, 10, "p1");
478 p2 = talloc_named_const(root, 20, "p2");
479 talloc_reference(p1, p2);
480 talloc_report_full(root, stdout);
481 talloc_unlink(root, p2);
482 talloc_report_full(root, stdout);
485 CHECK_BLOCKS(root, 3);
486 talloc_unlink(p1, p2);
487 talloc_unlink(root, p1);
489 p1 = talloc_named_const(root, 10, "p1");
490 p2 = talloc_named_const(root, 20, "p2");
491 talloc_reference(NULL, p2);
492 talloc_report_full(root, stdout);
493 talloc_unlink(root, p2);
494 talloc_report_full(root, stdout);
497 CHECK_BLOCKS(root, 2);
498 talloc_unlink(NULL, p2);
499 talloc_unlink(root, p1);
501 /* Test that talloc_unlink is a no-op */
503 if (talloc_unlink(root, NULL) != -1) {
504 printf("failed: talloc_unlink(root, NULL) == -1\n");
508 talloc_report(root, stdout);
509 talloc_report(NULL, stdout);
517 talloc_enable_leak_report();
518 talloc_enable_leak_report_full();
527 static BOOL test_realloc(void)
529 void *root, *p1, *p2;
531 printf("TESTING REALLOC\n");
533 root = talloc_new(NULL);
535 p1 = talloc_size(root, 10);
538 p1 = talloc_realloc_size(NULL, p1, 20);
543 p2 = talloc_realloc_size(p1, NULL, 30);
547 p2 = talloc_realloc_size(p1, p2, 40);
550 CHECK_SIZE(root, 60);
553 p1 = talloc_realloc_size(NULL, p1, 20);
556 talloc_increase_ref_count(p2);
557 if (talloc_realloc_size(NULL, p2, 5) != NULL) {
558 printf("failed: talloc_realloc() on a referenced pointer should fail\n");
563 talloc_realloc_size(NULL, p2, 0);
564 talloc_realloc_size(NULL, p2, 0);
567 if (talloc_realloc_size(NULL, p1, 0x7fffffff) != NULL) {
568 printf("failed: oversize talloc should fail\n");
572 talloc_realloc_size(NULL, p1, 0);
574 CHECK_BLOCKS(root, 1);
584 test realloc with a child
586 static BOOL test_realloc_child(void)
593 } **list, **list2, **list3;
597 printf("TESTING REALLOC WITH CHILD\n");
599 root = talloc_new(NULL);
601 el1 = talloc(root, struct el1);
602 el1->list = talloc(el1, struct el2 *);
603 el1->list[0] = talloc(el1->list, struct el2);
604 el1->list[0]->name = talloc_strdup(el1->list[0], "testing");
606 el1->list2 = talloc(el1, struct el2 *);
607 el1->list2[0] = talloc(el1->list2, struct el2);
608 el1->list2[0]->name = talloc_strdup(el1->list2[0], "testing2");
610 el1->list3 = talloc(el1, struct el2 *);
611 el1->list3[0] = talloc(el1->list3, struct el2);
612 el1->list3[0]->name = talloc_strdup(el1->list3[0], "testing2");
614 el2 = talloc(el1->list, struct el2);
615 el2 = talloc(el1->list2, struct el2);
616 el2 = talloc(el1->list3, struct el2);
618 el1->list = talloc_realloc(el1, el1->list, struct el2 *, 100);
619 el1->list2 = talloc_realloc(el1, el1->list2, struct el2 *, 200);
620 el1->list3 = talloc_realloc(el1, el1->list3, struct el2 *, 300);
631 static BOOL test_type(void)
642 printf("TESTING talloc type checking\n");
644 root = talloc_new(NULL);
646 el1 = talloc(root, struct el1);
650 if (talloc_get_type(el1, struct el1) != el1) {
651 printf("type check failed on el1\n");
654 if (talloc_get_type(el1, struct el2) != NULL) {
655 printf("type check failed on el1 with el2\n");
658 talloc_set_type(el1, struct el2);
659 if (talloc_get_type(el1, struct el2) != (struct el2 *)el1) {
660 printf("type set failed on el1 with el2\n");
672 static BOOL test_steal(void)
674 void *root, *p1, *p2;
676 printf("TESTING STEAL\n");
678 root = talloc_new(NULL);
680 p1 = talloc_array(root, char, 10);
683 p2 = talloc_realloc(root, NULL, char, 20);
685 CHECK_SIZE(root, 30);
687 if (talloc_steal(p1, NULL) != NULL) {
688 printf("failed: stealing NULL should give NULL\n");
692 if (talloc_steal(p1, p1) != p1) {
693 printf("failed: stealing to ourselves is a nop\n");
696 CHECK_BLOCKS(root, 3);
697 CHECK_SIZE(root, 30);
699 talloc_steal(NULL, p1);
700 talloc_steal(NULL, p2);
701 CHECK_BLOCKS(root, 1);
705 talloc_steal(root, p2);
706 CHECK_BLOCKS(root, 2);
707 CHECK_SIZE(root, 20);
711 CHECK_BLOCKS(root, 1);
716 p1 = talloc_size(NULL, 3);
717 talloc_report_full(NULL, stdout);
725 test talloc_realloc_fn
727 static BOOL test_realloc_fn(void)
731 printf("TESTING talloc_realloc_fn\n");
733 root = talloc_new(NULL);
735 p1 = talloc_realloc_fn(root, NULL, 10);
736 CHECK_BLOCKS(root, 2);
737 CHECK_SIZE(root, 10);
738 p1 = talloc_realloc_fn(root, p1, 20);
739 CHECK_BLOCKS(root, 2);
740 CHECK_SIZE(root, 20);
741 p1 = talloc_realloc_fn(root, p1, 0);
742 CHECK_BLOCKS(root, 1);
752 static BOOL test_unref_reparent(void)
754 void *root, *p1, *p2, *c1;
756 printf("TESTING UNREFERENCE AFTER PARENT FREED\n");
758 root = talloc_named_const(NULL, 0, "root");
759 p1 = talloc_named_const(root, 1, "orig parent");
760 p2 = talloc_named_const(root, 1, "parent by reference");
762 c1 = talloc_named_const(p1, 1, "child");
763 talloc_reference(p2, c1);
766 talloc_unlink(p2, c1);
777 measure the speed of talloc versus malloc
779 static BOOL test_speed(void)
781 void *ctx = talloc_new(NULL);
785 printf("MEASURING TALLOC VS MALLOC SPEED\n");
787 tv = timeval_current();
791 p1 = talloc_size(ctx, count);
792 p2 = talloc_strdup(p1, "foo bar");
793 p3 = talloc_size(p1, 300);
796 } while (timeval_elapsed(&tv) < 5.0);
798 printf("talloc: %.0f ops/sec\n", count/timeval_elapsed(&tv));
802 tv = timeval_current();
807 p2 = strdup("foo bar");
813 } while (timeval_elapsed(&tv) < 5.0);
815 printf("malloc: %.0f ops/sec\n", count/timeval_elapsed(&tv));
821 BOOL test_lifeless(void)
823 char *top = talloc_new(NULL);
824 char *parent, *child;
825 char *child_owner = talloc_new(NULL);
827 printf("TESTING TALLOC_UNLINK LOOP\n");
829 parent = talloc_strdup(top, "parent");
830 child = talloc_strdup(parent, "child");
831 talloc_reference(child, parent);
832 talloc_reference(child_owner, child);
833 talloc_unlink(top, parent);
835 talloc_report_full(top, stdout);
841 BOOL torture_local_talloc(void)
849 ret &= test_unlink1();
851 ret &= test_realloc();
852 ret &= test_realloc_child();
854 ret &= test_unref_reparent();
855 ret &= test_realloc_fn();
857 ret &= test_lifeless();
867 #if !defined(_SAMBA_BUILD_) || ((SAMBA_VERSION_MAJOR==3)&&(SAMBA_VERSION_MINOR<9))
870 if (!torture_local_talloc()) {
871 printf("ERROR: TESTSUIE FAILED\n");