2 Unix SMB/CIFS implementation.
4 local testing of talloc routines.
6 Copyright (C) Andrew Tridgell 2004
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
29 #endif /* _SAMBA_BUILD_ */
55 struct torture_context;
57 static struct timeval timeval_current(void)
60 gettimeofday(&tv, NULL);
64 static double timeval_elapsed(struct timeval *tv)
66 struct timeval tv2 = timeval_current();
67 return (tv2.tv_sec - tv->tv_sec) +
68 (tv2.tv_usec - tv->tv_usec)*1.0e-6;
71 #if SAMBA_VERSION_MAJOR==3
80 #define CHECK_SIZE(ptr, tsize) do { \
81 if (talloc_total_size(ptr) != (tsize)) { \
82 printf(__location__ " failed: wrong '%s' tree size: got %u expected %u\n", \
84 (unsigned)talloc_total_size(ptr), \
86 talloc_report_full(ptr, stdout); \
91 #define CHECK_BLOCKS(ptr, tblocks) do { \
92 if (talloc_total_blocks(ptr) != (tblocks)) { \
93 printf(__location__ " failed: wrong '%s' tree blocks: got %u expected %u\n", \
95 (unsigned)talloc_total_blocks(ptr), \
97 talloc_report_full(ptr, stdout); \
102 #define CHECK_PARENT(ptr, parent) do { \
103 if (talloc_parent(ptr) != (parent)) { \
104 printf(__location__ " failed: '%s' has wrong parent: got %p expected %p\n", \
106 talloc_parent(ptr), \
108 talloc_report_full(ptr, stdout); \
109 talloc_report_full(parent, stdout); \
110 talloc_report_full(NULL, stdout); \
119 static BOOL test_ref1(void)
121 void *root, *p1, *p2, *ref, *r1;
123 printf("TESTING SINGLE REFERENCE FREE\n");
125 root = talloc_named_const(NULL, 0, "root");
126 p1 = talloc_named_const(root, 1, "p1");
127 p2 = talloc_named_const(p1, 1, "p2");
128 talloc_named_const(p1, 1, "x1");
129 talloc_named_const(p1, 2, "x2");
130 talloc_named_const(p1, 3, "x3");
132 r1 = talloc_named_const(root, 1, "r1");
133 ref = talloc_reference(r1, p2);
134 talloc_report_full(root, stdout);
140 printf("Freeing p2\n");
142 talloc_report_full(root, stdout);
148 printf("Freeing p1\n");
150 talloc_report_full(root, stdout);
154 printf("Freeing r1\n");
156 talloc_report_full(NULL, stdout);
158 printf("Testing NULL\n");
159 if (talloc_reference(root, NULL)) {
163 CHECK_BLOCKS(root, 1);
175 static BOOL test_ref2(void)
177 void *root, *p1, *p2, *ref, *r1;
179 printf("TESTING DOUBLE REFERENCE FREE\n");
181 root = talloc_named_const(NULL, 0, "root");
182 p1 = talloc_named_const(root, 1, "p1");
183 talloc_named_const(p1, 1, "x1");
184 talloc_named_const(p1, 1, "x2");
185 talloc_named_const(p1, 1, "x3");
186 p2 = talloc_named_const(p1, 1, "p2");
188 r1 = talloc_named_const(root, 1, "r1");
189 ref = talloc_reference(r1, p2);
190 talloc_report_full(root, stdout);
196 printf("Freeing ref\n");
198 talloc_report_full(root, stdout);
204 printf("Freeing p2\n");
206 talloc_report_full(root, stdout);
211 printf("Freeing p1\n");
213 talloc_report_full(root, stdout);
217 printf("Freeing r1\n");
219 talloc_report_full(root, stdout);
231 static BOOL test_ref3(void)
233 void *root, *p1, *p2, *ref, *r1;
235 printf("TESTING PARENT REFERENCE FREE\n");
237 root = talloc_named_const(NULL, 0, "root");
238 p1 = talloc_named_const(root, 1, "p1");
239 p2 = talloc_named_const(root, 1, "p2");
240 r1 = talloc_named_const(p1, 1, "r1");
241 ref = talloc_reference(p2, r1);
242 talloc_report_full(root, stdout);
248 printf("Freeing p1\n");
250 talloc_report_full(root, stdout);
255 printf("Freeing p2\n");
257 talloc_report_full(root, stdout);
269 static BOOL test_ref4(void)
271 void *root, *p1, *p2, *ref, *r1;
273 printf("TESTING REFERRER REFERENCE FREE\n");
275 root = talloc_named_const(NULL, 0, "root");
276 p1 = talloc_named_const(root, 1, "p1");
277 talloc_named_const(p1, 1, "x1");
278 talloc_named_const(p1, 1, "x2");
279 talloc_named_const(p1, 1, "x3");
280 p2 = talloc_named_const(p1, 1, "p2");
282 r1 = talloc_named_const(root, 1, "r1");
283 ref = talloc_reference(r1, p2);
284 talloc_report_full(root, stdout);
290 printf("Freeing r1\n");
292 talloc_report_full(root, stdout);
297 printf("Freeing p2\n");
299 talloc_report_full(root, stdout);
303 printf("Freeing p1\n");
305 talloc_report_full(root, stdout);
318 static BOOL test_unlink1(void)
320 void *root, *p1, *p2, *ref, *r1;
322 printf("TESTING UNLINK\n");
324 root = talloc_named_const(NULL, 0, "root");
325 p1 = talloc_named_const(root, 1, "p1");
326 talloc_named_const(p1, 1, "x1");
327 talloc_named_const(p1, 1, "x2");
328 talloc_named_const(p1, 1, "x3");
329 p2 = talloc_named_const(p1, 1, "p2");
331 r1 = talloc_named_const(p1, 1, "r1");
332 ref = talloc_reference(r1, p2);
333 talloc_report_full(root, stdout);
339 printf("Unreferencing r1\n");
340 talloc_unlink(r1, p2);
341 talloc_report_full(root, stdout);
347 printf("Freeing p1\n");
349 talloc_report_full(root, stdout);
358 static int fail_destructor(void *ptr)
364 miscellaneous tests to try to get a higher test coverage percentage
366 static BOOL test_misc(void)
373 printf("TESTING MISCELLANEOUS\n");
375 root = talloc_new(NULL);
377 p1 = talloc_size(root, 0x7fffffff);
379 printf("failed: large talloc allowed\n");
383 p1 = talloc_strdup(root, "foo");
384 talloc_increase_ref_count(p1);
385 talloc_increase_ref_count(p1);
386 talloc_increase_ref_count(p1);
388 CHECK_BLOCKS(root, 2);
391 CHECK_BLOCKS(root, 2);
392 talloc_unlink(NULL, p1);
394 CHECK_BLOCKS(root, 2);
395 p2 = talloc_strdup(p1, "foo");
396 if (talloc_unlink(root, p2) != -1) {
397 printf("failed: talloc_unlink() of non-reference context should return -1\n");
400 if (talloc_unlink(p1, p2) != 0) {
401 printf("failed: talloc_unlink() of parent should succeed\n");
406 CHECK_BLOCKS(root, 2);
408 name = talloc_set_name(p1, "my name is %s", "foo");
409 if (strcmp(talloc_get_name(p1), "my name is foo") != 0) {
410 printf("failed: wrong name after talloc_set_name(my name is foo) - '%s'=>'%s'\n",
411 (name?name:"NULL"), talloc_get_name(p1));
415 CHECK_BLOCKS(root, 3);
417 talloc_set_name_const(p1, NULL);
418 if (strcmp(talloc_get_name(p1), "UNNAMED") != 0) {
419 printf("failed: wrong name after talloc_set_name(NULL) - '%s'\n",
420 talloc_get_name(p1));
424 CHECK_BLOCKS(root, 3);
427 if (talloc_free(NULL) != -1) {
428 printf("talloc_free(NULL) should give -1\n");
432 talloc_set_destructor(p1, fail_destructor);
433 if (talloc_free(p1) != -1) {
434 printf("Failed destructor should cause talloc_free to fail\n");
437 talloc_set_destructor(p1, NULL);
439 talloc_report(root, stdout);
442 p2 = (char *)talloc_zero_size(p1, 20);
444 printf("Failed to give zero memory\n");
449 if (talloc_strdup(root, NULL) != NULL) {
450 printf("failed: strdup on NULL should give NULL\n");
454 p2 = talloc_strndup(p1, "foo", 2);
455 if (strcmp("fo", p2) != 0) {
456 printf("failed: strndup doesn't work\n");
459 p2 = talloc_asprintf_append(p2, "o%c", 'd');
460 if (strcmp("food", p2) != 0) {
461 printf("failed: talloc_asprintf_append doesn't work\n");
467 p2 = talloc_asprintf_append(NULL, "hello %s", "world");
468 if (strcmp("hello world", p2) != 0) {
469 printf("failed: talloc_asprintf_append doesn't work\n");
476 d = talloc_array(p1, double, 0x20000000);
478 printf("failed: integer overflow not detected\n");
482 d = talloc_realloc(p1, d, double, 0x20000000);
484 printf("failed: integer overflow not detected\n");
489 CHECK_BLOCKS(root, 1);
491 p1 = talloc_named(root, 100, "%d bytes", 100);
493 CHECK_BLOCKS(root, 3);
494 talloc_unlink(root, p1);
496 p1 = talloc_init("%d bytes", 200);
497 p2 = talloc_asprintf(p1, "my test '%s'", "string");
498 if (strcmp(p2, "my test 'string'") != 0) {
499 printf("failed: talloc_asprintf(\"my test '%%s'\", \"string\") gave: \"%s\"\n", p2);
504 CHECK_BLOCKS(root, 1);
505 talloc_unlink(NULL, p1);
507 p1 = talloc_named_const(root, 10, "p1");
508 p2 = (char *)talloc_named_const(root, 20, "p2");
509 (void)talloc_reference(p1, p2);
510 talloc_report_full(root, stdout);
511 talloc_unlink(root, p2);
512 talloc_report_full(root, stdout);
515 CHECK_BLOCKS(root, 3);
516 talloc_unlink(p1, p2);
517 talloc_unlink(root, p1);
519 p1 = talloc_named_const(root, 10, "p1");
520 p2 = (char *)talloc_named_const(root, 20, "p2");
521 (void)talloc_reference(NULL, p2);
522 talloc_report_full(root, stdout);
523 talloc_unlink(root, p2);
524 talloc_report_full(root, stdout);
527 CHECK_BLOCKS(root, 2);
528 talloc_unlink(NULL, p2);
529 talloc_unlink(root, p1);
531 /* Test that talloc_unlink is a no-op */
533 if (talloc_unlink(root, NULL) != -1) {
534 printf("failed: talloc_unlink(root, NULL) == -1\n");
538 talloc_report(root, stdout);
539 talloc_report(NULL, stdout);
547 talloc_enable_leak_report();
548 talloc_enable_leak_report_full();
557 static BOOL test_realloc(void)
559 void *root, *p1, *p2;
561 printf("TESTING REALLOC\n");
563 root = talloc_new(NULL);
565 p1 = talloc_size(root, 10);
568 p1 = talloc_realloc_size(NULL, p1, 20);
573 p2 = talloc_realloc_size(p1, NULL, 30);
577 p2 = talloc_realloc_size(p1, p2, 40);
580 CHECK_SIZE(root, 60);
583 p1 = talloc_realloc_size(NULL, p1, 20);
586 talloc_increase_ref_count(p2);
587 if (talloc_realloc_size(NULL, p2, 5) != NULL) {
588 printf("failed: talloc_realloc() on a referenced pointer should fail\n");
593 talloc_realloc_size(NULL, p2, 0);
594 talloc_realloc_size(NULL, p2, 0);
597 if (talloc_realloc_size(NULL, p1, 0x7fffffff) != NULL) {
598 printf("failed: oversize talloc should fail\n");
602 talloc_realloc_size(NULL, p1, 0);
604 CHECK_BLOCKS(root, 1);
614 test realloc with a child
616 static BOOL test_realloc_child(void)
623 } **list, **list2, **list3;
627 printf("TESTING REALLOC WITH CHILD\n");
629 root = talloc_new(NULL);
631 el1 = talloc(root, struct el1);
632 el1->list = talloc(el1, struct el2 *);
633 el1->list[0] = talloc(el1->list, struct el2);
634 el1->list[0]->name = talloc_strdup(el1->list[0], "testing");
636 el1->list2 = talloc(el1, struct el2 *);
637 el1->list2[0] = talloc(el1->list2, struct el2);
638 el1->list2[0]->name = talloc_strdup(el1->list2[0], "testing2");
640 el1->list3 = talloc(el1, struct el2 *);
641 el1->list3[0] = talloc(el1->list3, struct el2);
642 el1->list3[0]->name = talloc_strdup(el1->list3[0], "testing2");
644 el2 = talloc(el1->list, struct el2);
645 el2 = talloc(el1->list2, struct el2);
646 el2 = talloc(el1->list3, struct el2);
648 el1->list = talloc_realloc(el1, el1->list, struct el2 *, 100);
649 el1->list2 = talloc_realloc(el1, el1->list2, struct el2 *, 200);
650 el1->list3 = talloc_realloc(el1, el1->list3, struct el2 *, 300);
661 static BOOL test_type(void)
672 printf("TESTING talloc type checking\n");
674 root = talloc_new(NULL);
676 el1 = talloc(root, struct el1);
680 if (talloc_get_type(el1, struct el1) != el1) {
681 printf("type check failed on el1\n");
684 if (talloc_get_type(el1, struct el2) != NULL) {
685 printf("type check failed on el1 with el2\n");
688 talloc_set_type(el1, struct el2);
689 if (talloc_get_type(el1, struct el2) != (struct el2 *)el1) {
690 printf("type set failed on el1 with el2\n");
702 static BOOL test_steal(void)
704 void *root, *p1, *p2;
706 printf("TESTING STEAL\n");
708 root = talloc_new(NULL);
710 p1 = talloc_array(root, char, 10);
713 p2 = talloc_realloc(root, NULL, char, 20);
715 CHECK_SIZE(root, 30);
717 if (talloc_steal(p1, NULL) != NULL) {
718 printf("failed: stealing NULL should give NULL\n");
722 if (talloc_steal(p1, p1) != p1) {
723 printf("failed: stealing to ourselves is a nop\n");
726 CHECK_BLOCKS(root, 3);
727 CHECK_SIZE(root, 30);
729 talloc_steal(NULL, p1);
730 talloc_steal(NULL, p2);
731 CHECK_BLOCKS(root, 1);
735 talloc_steal(root, p2);
736 CHECK_BLOCKS(root, 2);
737 CHECK_SIZE(root, 20);
741 CHECK_BLOCKS(root, 1);
746 p1 = talloc_size(NULL, 3);
747 talloc_report_full(NULL, stdout);
755 test talloc_realloc_fn
757 static BOOL test_realloc_fn(void)
761 printf("TESTING talloc_realloc_fn\n");
763 root = talloc_new(NULL);
765 p1 = talloc_realloc_fn(root, NULL, 10);
766 CHECK_BLOCKS(root, 2);
767 CHECK_SIZE(root, 10);
768 p1 = talloc_realloc_fn(root, p1, 20);
769 CHECK_BLOCKS(root, 2);
770 CHECK_SIZE(root, 20);
771 p1 = talloc_realloc_fn(root, p1, 0);
772 CHECK_BLOCKS(root, 1);
782 static BOOL test_unref_reparent(void)
784 void *root, *p1, *p2, *c1;
786 printf("TESTING UNREFERENCE AFTER PARENT FREED\n");
788 root = talloc_named_const(NULL, 0, "root");
789 p1 = talloc_named_const(root, 1, "orig parent");
790 p2 = talloc_named_const(root, 1, "parent by reference");
792 c1 = talloc_named_const(p1, 1, "child");
793 talloc_reference(p2, c1);
795 CHECK_PARENT(c1, p1);
799 CHECK_PARENT(c1, p2);
801 talloc_unlink(p2, c1);
812 measure the speed of talloc versus malloc
814 static BOOL test_speed(void)
816 void *ctx = talloc_new(NULL);
820 printf("MEASURING TALLOC VS MALLOC SPEED\n");
822 tv = timeval_current();
826 p1 = talloc_size(ctx, count);
827 p2 = talloc_strdup(p1, "foo bar");
828 p3 = talloc_size(p1, 300);
831 } while (timeval_elapsed(&tv) < 5.0);
833 printf("talloc: %.0f ops/sec\n", count/timeval_elapsed(&tv));
837 tv = timeval_current();
842 p2 = strdup("foo bar");
848 } while (timeval_elapsed(&tv) < 5.0);
850 printf("malloc: %.0f ops/sec\n", count/timeval_elapsed(&tv));
856 static BOOL test_lifeless(void)
858 void *top = talloc_new(NULL);
859 char *parent, *child;
860 void *child_owner = talloc_new(NULL);
862 printf("TESTING TALLOC_UNLINK LOOP\n");
864 parent = talloc_strdup(top, "parent");
865 child = talloc_strdup(parent, "child");
866 (void)talloc_reference(child, parent);
867 (void)talloc_reference(child_owner, child);
868 talloc_report_full(top, stdout);
869 talloc_unlink(top, parent);
871 talloc_report_full(top, stdout);
873 talloc_free(child_owner);
879 static int loop_destructor_count;
881 static int test_loop_destructor(char *ptr)
883 printf("loop destructor\n");
884 loop_destructor_count++;
888 static BOOL test_loop(void)
890 void *top = talloc_new(NULL);
896 printf("TESTING TALLOC LOOP DESTRUCTION\n");
897 parent = talloc_strdup(top, "parent");
898 req1 = talloc(parent, struct req1);
899 req1->req2 = talloc_strdup(req1, "req2");
900 talloc_set_destructor(req1->req2, test_loop_destructor);
901 req1->req3 = talloc_strdup(req1, "req3");
902 (void)talloc_reference(req1->req3, req1);
903 talloc_report_full(top, stdout);
905 talloc_report_full(top, stdout);
906 talloc_report_full(NULL, stdout);
909 if (loop_destructor_count != 1) {
910 printf("FAILED TO FIRE LOOP DESTRUCTOR\n");
913 loop_destructor_count = 0;
918 static int fail_destructor_str(char *ptr)
923 static BOOL test_free_parent_deny_child(void)
925 void *top = talloc_new(NULL);
930 printf("TESTING TALLOC FREE PARENT DENY CHILD\n");
931 level1 = talloc_strdup(top, "level1");
932 level2 = talloc_strdup(level1, "level2");
933 level3 = talloc_strdup(level2, "level3");
935 talloc_set_destructor(level3, fail_destructor_str);
937 talloc_set_destructor(level3, NULL);
939 CHECK_PARENT(level3, top);
946 static BOOL test_talloc_ptrtype(void)
949 void *top = talloc_new(NULL);
953 } *s1, *s2, **s3, ***s4;
954 const char *location1;
955 const char *location2;
956 const char *location3;
957 const char *location4;
959 printf("TESTING TALLOC PTRTYPE\n");
960 s1 = talloc_ptrtype(top, s1);location1 = __location__;
962 if (talloc_get_size(s1) != sizeof(struct struct1)) {
963 printf("%s: talloc_ptrtype() allocated the wrong size %u (should be %u)\n",
964 __location__, talloc_get_size(s1), sizeof(struct struct1));
968 if (strcmp(location1, talloc_get_name(s1)) != 0) {
969 printf("%s: talloc_ptrtype() sets the wrong name '%s' (should be '%s')\n",
970 __location__, talloc_get_name(s1), location1);
974 s2 = talloc_array_ptrtype(top, s2, 10);location2 = __location__;
976 if (talloc_get_size(s2) != (sizeof(struct struct1) * 10)) {
977 printf("%s: talloc_array_ptrtype() allocated the wrong size %u (should be %u)\n",
978 __location__, talloc_get_size(s2), (sizeof(struct struct1)*10));
982 if (strcmp(location2, talloc_get_name(s2)) != 0) {
983 printf("%s: talloc_array_ptrtype() sets the wrong name '%s' (should be '%s')\n",
984 __location__, talloc_get_name(s2), location2);
988 s3 = talloc_array_ptrtype(top, s3, 10);location3 = __location__;
990 if (talloc_get_size(s3) != (sizeof(struct struct1 *) * 10)) {
991 printf("%s: talloc_array_ptrtype() allocated the wrong size %u (should be %u)\n",
992 __location__, talloc_get_size(s3), (sizeof(struct struct1 *)*10));
996 if (strcmp(location3, talloc_get_name(s3)) != 0) {
997 printf("%s: talloc_array_ptrtype() sets the wrong name '%s' (should be '%s')\n",
998 __location__, talloc_get_name(s3), location3);
1002 s4 = talloc_array_ptrtype(top, s4, 10);location4 = __location__;
1004 if (talloc_get_size(s4) != (sizeof(struct struct1 **) * 10)) {
1005 printf("%s: talloc_array_ptrtype() allocated the wrong size %u (should be %u)\n",
1006 __location__, talloc_get_size(s4), (sizeof(struct struct1 **)*10));
1010 if (strcmp(location4, talloc_get_name(s4)) != 0) {
1011 printf("%s: talloc_array_ptrtype() sets the wrong name '%s' (should be '%s')\n",
1012 __location__, talloc_get_name(s4), location4);
1021 BOOL torture_local_talloc(struct torture_context *torture)
1025 talloc_disable_null_tracking();
1026 talloc_enable_null_tracking();
1032 ret &= test_unlink1();
1034 ret &= test_realloc();
1035 ret &= test_realloc_child();
1036 ret &= test_steal();
1037 ret &= test_unref_reparent();
1038 ret &= test_realloc_fn();
1040 ret &= test_lifeless();
1042 ret &= test_free_parent_deny_child();
1043 ret &= test_talloc_ptrtype();
1045 ret &= test_speed();
1053 #if !defined(_SAMBA_BUILD_) || ((SAMBA_VERSION_MAJOR==3)&&(SAMBA_VERSION_MINOR<9))
1056 if (!torture_local_talloc(NULL)) {
1057 printf("ERROR: TESTSUITE FAILED\n");