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 3 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, see <http://www.gnu.org/licenses/>.
27 #include "system/time.h"
30 static struct timeval timeval_current(void)
33 gettimeofday(&tv, NULL);
37 static double timeval_elapsed(struct timeval *tv)
39 struct timeval tv2 = timeval_current();
40 return (tv2.tv_sec - tv->tv_sec) +
41 (tv2.tv_usec - tv->tv_usec)*1.0e-6;
44 #define torture_assert(test, expr, str) if (!(expr)) { \
45 printf("failure: %s [\n%s: Expression %s failed: %s\n]\n", \
46 test, __location__, #expr, str); \
50 #define torture_assert_str_equal(test, arg1, arg2, desc) \
51 if (strcmp(arg1, arg2)) { \
52 printf("failure: %s [\n%s: Expected %s, got %s: %s\n]\n", \
53 test, __location__, arg1, arg2, desc); \
66 #define CHECK_SIZE(test, ptr, tsize) do { \
67 if (talloc_total_size(ptr) != (tsize)) { \
68 printf("failed: %s [\nwrong '%s' tree size: got %u expected %u\n]\n", \
70 (unsigned)talloc_total_size(ptr), \
72 talloc_report_full(ptr, stdout); \
77 #define CHECK_BLOCKS(test, ptr, tblocks) do { \
78 if (talloc_total_blocks(ptr) != (tblocks)) { \
79 printf("failed: %s [\nwrong '%s' tree blocks: got %u expected %u\n]\n", \
81 (unsigned)talloc_total_blocks(ptr), \
83 talloc_report_full(ptr, stdout); \
88 #define CHECK_PARENT(test, ptr, parent) do { \
89 if (talloc_parent(ptr) != (parent)) { \
90 printf("failed: %s [\n'%s' has wrong parent: got %p expected %p\n]\n", \
94 talloc_report_full(ptr, stdout); \
95 talloc_report_full(parent, stdout); \
96 talloc_report_full(NULL, stdout); \
105 static bool test_ref1(void)
107 void *root, *p1, *p2, *ref, *r1;
109 printf("test: ref1\n# SINGLE REFERENCE FREE\n");
111 root = talloc_named_const(NULL, 0, "root");
112 p1 = talloc_named_const(root, 1, "p1");
113 p2 = talloc_named_const(p1, 1, "p2");
114 talloc_named_const(p1, 1, "x1");
115 talloc_named_const(p1, 2, "x2");
116 talloc_named_const(p1, 3, "x3");
118 r1 = talloc_named_const(root, 1, "r1");
119 ref = talloc_reference(r1, p2);
120 talloc_report_full(root, stderr);
122 CHECK_BLOCKS("ref1", p1, 5);
123 CHECK_BLOCKS("ref1", p2, 1);
124 CHECK_BLOCKS("ref1", r1, 2);
126 fprintf(stderr, "Freeing p2\n");
128 talloc_report_full(root, stderr);
130 CHECK_BLOCKS("ref1", p1, 5);
131 CHECK_BLOCKS("ref1", p2, 1);
132 CHECK_BLOCKS("ref1", r1, 1);
134 fprintf(stderr, "Freeing p1\n");
136 talloc_report_full(root, stderr);
138 CHECK_BLOCKS("ref1", r1, 1);
140 fprintf(stderr, "Freeing r1\n");
142 talloc_report_full(NULL, stderr);
144 fprintf(stderr, "Testing NULL\n");
145 if (talloc_reference(root, NULL)) {
149 CHECK_BLOCKS("ref1", root, 1);
151 CHECK_SIZE("ref1", root, 0);
154 printf("success: ref1\n");
161 static bool test_ref2(void)
163 void *root, *p1, *p2, *ref, *r1;
165 printf("test: ref2\n# DOUBLE REFERENCE FREE\n");
166 root = talloc_named_const(NULL, 0, "root");
167 p1 = talloc_named_const(root, 1, "p1");
168 talloc_named_const(p1, 1, "x1");
169 talloc_named_const(p1, 1, "x2");
170 talloc_named_const(p1, 1, "x3");
171 p2 = talloc_named_const(p1, 1, "p2");
173 r1 = talloc_named_const(root, 1, "r1");
174 ref = talloc_reference(r1, p2);
175 talloc_report_full(root, stderr);
177 CHECK_BLOCKS("ref2", p1, 5);
178 CHECK_BLOCKS("ref2", p2, 1);
179 CHECK_BLOCKS("ref2", r1, 2);
181 fprintf(stderr, "Freeing ref\n");
183 talloc_report_full(root, stderr);
185 CHECK_BLOCKS("ref2", p1, 5);
186 CHECK_BLOCKS("ref2", p2, 1);
187 CHECK_BLOCKS("ref2", r1, 1);
189 fprintf(stderr, "Freeing p2\n");
191 talloc_report_full(root, stderr);
193 CHECK_BLOCKS("ref2", p1, 4);
194 CHECK_BLOCKS("ref2", r1, 1);
196 fprintf(stderr, "Freeing p1\n");
198 talloc_report_full(root, stderr);
200 CHECK_BLOCKS("ref2", r1, 1);
202 fprintf(stderr, "Freeing r1\n");
204 talloc_report_full(root, stderr);
206 CHECK_SIZE("ref2", root, 0);
209 printf("success: ref2\n");
216 static bool test_ref3(void)
218 void *root, *p1, *p2, *ref, *r1;
220 printf("test: ref3\n# PARENT REFERENCE FREE\n");
222 root = talloc_named_const(NULL, 0, "root");
223 p1 = talloc_named_const(root, 1, "p1");
224 p2 = talloc_named_const(root, 1, "p2");
225 r1 = talloc_named_const(p1, 1, "r1");
226 ref = talloc_reference(p2, r1);
227 talloc_report_full(root, stderr);
229 CHECK_BLOCKS("ref3", p1, 2);
230 CHECK_BLOCKS("ref3", p2, 2);
231 CHECK_BLOCKS("ref3", r1, 1);
233 fprintf(stderr, "Freeing p1\n");
235 talloc_report_full(root, stderr);
237 CHECK_BLOCKS("ref3", p2, 2);
238 CHECK_BLOCKS("ref3", r1, 1);
240 fprintf(stderr, "Freeing p2\n");
242 talloc_report_full(root, stderr);
244 CHECK_SIZE("ref3", root, 0);
248 printf("success: ref3\n");
255 static bool test_ref4(void)
257 void *root, *p1, *p2, *ref, *r1;
259 printf("test: ref4\n# REFERRER REFERENCE FREE\n");
261 root = talloc_named_const(NULL, 0, "root");
262 p1 = talloc_named_const(root, 1, "p1");
263 talloc_named_const(p1, 1, "x1");
264 talloc_named_const(p1, 1, "x2");
265 talloc_named_const(p1, 1, "x3");
266 p2 = talloc_named_const(p1, 1, "p2");
268 r1 = talloc_named_const(root, 1, "r1");
269 ref = talloc_reference(r1, p2);
270 talloc_report_full(root, stderr);
272 CHECK_BLOCKS("ref4", p1, 5);
273 CHECK_BLOCKS("ref4", p2, 1);
274 CHECK_BLOCKS("ref4", r1, 2);
276 fprintf(stderr, "Freeing r1\n");
278 talloc_report_full(root, stderr);
280 CHECK_BLOCKS("ref4", p1, 5);
281 CHECK_BLOCKS("ref4", p2, 1);
283 fprintf(stderr, "Freeing p2\n");
285 talloc_report_full(root, stderr);
287 CHECK_BLOCKS("ref4", p1, 4);
289 fprintf(stderr, "Freeing p1\n");
291 talloc_report_full(root, stderr);
293 CHECK_SIZE("ref4", root, 0);
297 printf("success: ref4\n");
305 static bool test_unlink1(void)
307 void *root, *p1, *p2, *ref, *r1;
309 printf("test: unlink\n# UNLINK\n");
311 root = talloc_named_const(NULL, 0, "root");
312 p1 = talloc_named_const(root, 1, "p1");
313 talloc_named_const(p1, 1, "x1");
314 talloc_named_const(p1, 1, "x2");
315 talloc_named_const(p1, 1, "x3");
316 p2 = talloc_named_const(p1, 1, "p2");
318 r1 = talloc_named_const(p1, 1, "r1");
319 ref = talloc_reference(r1, p2);
320 talloc_report_full(root, stderr);
322 CHECK_BLOCKS("unlink", p1, 7);
323 CHECK_BLOCKS("unlink", p2, 1);
324 CHECK_BLOCKS("unlink", r1, 2);
326 fprintf(stderr, "Unreferencing r1\n");
327 talloc_unlink(r1, p2);
328 talloc_report_full(root, stderr);
330 CHECK_BLOCKS("unlink", p1, 6);
331 CHECK_BLOCKS("unlink", p2, 1);
332 CHECK_BLOCKS("unlink", r1, 1);
334 fprintf(stderr, "Freeing p1\n");
336 talloc_report_full(root, stderr);
338 CHECK_SIZE("unlink", root, 0);
342 printf("success: unlink\n");
346 static int fail_destructor(void *ptr)
352 miscellaneous tests to try to get a higher test coverage percentage
354 static bool test_misc(void)
361 printf("test: misc\n# MISCELLANEOUS\n");
363 root = talloc_new(NULL);
365 p1 = talloc_size(root, 0x7fffffff);
366 torture_assert("misc", !p1, "failed: large talloc allowed\n");
368 p1 = talloc_strdup(root, "foo");
369 talloc_increase_ref_count(p1);
370 talloc_increase_ref_count(p1);
371 talloc_increase_ref_count(p1);
372 CHECK_BLOCKS("misc", p1, 1);
373 CHECK_BLOCKS("misc", root, 2);
375 CHECK_BLOCKS("misc", p1, 1);
376 CHECK_BLOCKS("misc", root, 2);
377 talloc_unlink(NULL, p1);
378 CHECK_BLOCKS("misc", p1, 1);
379 CHECK_BLOCKS("misc", root, 2);
380 p2 = talloc_strdup(p1, "foo");
381 torture_assert("misc", talloc_unlink(root, p2) == -1,
382 "failed: talloc_unlink() of non-reference context should return -1\n");
383 torture_assert("misc", talloc_unlink(p1, p2) == 0,
384 "failed: talloc_unlink() of parent should succeed\n");
386 CHECK_BLOCKS("misc", p1, 1);
387 CHECK_BLOCKS("misc", root, 2);
389 name = talloc_set_name(p1, "my name is %s", "foo");
390 torture_assert_str_equal("misc", talloc_get_name(p1), "my name is foo",
391 "failed: wrong name after talloc_set_name(my name is foo)");
392 CHECK_BLOCKS("misc", p1, 2);
393 CHECK_BLOCKS("misc", root, 3);
395 talloc_set_name_const(p1, NULL);
396 torture_assert_str_equal ("misc", talloc_get_name(p1), "UNNAMED",
397 "failed: wrong name after talloc_set_name(NULL)");
398 CHECK_BLOCKS("misc", p1, 2);
399 CHECK_BLOCKS("misc", root, 3);
401 torture_assert("misc", talloc_free(NULL) == -1,
402 "talloc_free(NULL) should give -1\n");
404 talloc_set_destructor(p1, fail_destructor);
405 torture_assert("misc", talloc_free(p1) == -1,
406 "Failed destructor should cause talloc_free to fail\n");
407 talloc_set_destructor(p1, NULL);
409 talloc_report(root, stderr);
412 p2 = (char *)talloc_zero_size(p1, 20);
413 torture_assert("misc", p2[19] == 0, "Failed to give zero memory\n");
416 torture_assert("misc", talloc_strdup(root, NULL) == NULL,
417 "failed: strdup on NULL should give NULL\n");
419 p2 = talloc_strndup(p1, "foo", 2);
420 torture_assert("misc", strcmp("fo", p2) == 0,
421 "strndup doesn't work\n");
422 p2 = talloc_asprintf_append_buffer(p2, "o%c", 'd');
423 torture_assert("misc", strcmp("food", p2) == 0,
424 "talloc_asprintf_append_buffer doesn't work\n");
425 CHECK_BLOCKS("misc", p2, 1);
426 CHECK_BLOCKS("misc", p1, 3);
428 p2 = talloc_asprintf_append_buffer(NULL, "hello %s", "world");
429 torture_assert("misc", strcmp("hello world", p2) == 0,
430 "talloc_asprintf_append_buffer doesn't work\n");
431 CHECK_BLOCKS("misc", p2, 1);
432 CHECK_BLOCKS("misc", p1, 3);
435 d = talloc_array(p1, double, 0x20000000);
436 torture_assert("misc", !d, "failed: integer overflow not detected\n");
438 d = talloc_realloc(p1, d, double, 0x20000000);
439 torture_assert("misc", !d, "failed: integer overflow not detected\n");
442 CHECK_BLOCKS("misc", root, 1);
444 p1 = talloc_named(root, 100, "%d bytes", 100);
445 CHECK_BLOCKS("misc", p1, 2);
446 CHECK_BLOCKS("misc", root, 3);
447 talloc_unlink(root, p1);
449 p1 = talloc_init("%d bytes", 200);
450 p2 = talloc_asprintf(p1, "my test '%s'", "string");
451 torture_assert_str_equal("misc", p2, "my test 'string'",
452 "failed: talloc_asprintf(\"my test '%%s'\", \"string\") gave: \"%s\"");
453 CHECK_BLOCKS("misc", p1, 3);
454 CHECK_SIZE("misc", p2, 17);
455 CHECK_BLOCKS("misc", root, 1);
456 talloc_unlink(NULL, p1);
458 p1 = talloc_named_const(root, 10, "p1");
459 p2 = (char *)talloc_named_const(root, 20, "p2");
460 (void)talloc_reference(p1, p2);
461 talloc_report_full(root, stderr);
462 talloc_unlink(root, p2);
463 talloc_report_full(root, stderr);
464 CHECK_BLOCKS("misc", p2, 1);
465 CHECK_BLOCKS("misc", p1, 2);
466 CHECK_BLOCKS("misc", root, 3);
467 talloc_unlink(p1, p2);
468 talloc_unlink(root, p1);
470 p1 = talloc_named_const(root, 10, "p1");
471 p2 = (char *)talloc_named_const(root, 20, "p2");
472 (void)talloc_reference(NULL, p2);
473 talloc_report_full(root, stderr);
474 talloc_unlink(root, p2);
475 talloc_report_full(root, stderr);
476 CHECK_BLOCKS("misc", p2, 1);
477 CHECK_BLOCKS("misc", p1, 1);
478 CHECK_BLOCKS("misc", root, 2);
479 talloc_unlink(NULL, p2);
480 talloc_unlink(root, p1);
482 /* Test that talloc_unlink is a no-op */
484 torture_assert("misc", talloc_unlink(root, NULL) == -1,
485 "failed: talloc_unlink(root, NULL) == -1\n");
487 talloc_report(root, stderr);
488 talloc_report(NULL, stderr);
490 CHECK_SIZE("misc", root, 0);
494 CHECK_SIZE("misc", NULL, 0);
496 talloc_enable_leak_report();
497 talloc_enable_leak_report_full();
499 printf("success: misc\n");
508 static bool test_realloc(void)
510 void *root, *p1, *p2;
512 printf("test: realloc\n# REALLOC\n");
514 root = talloc_new(NULL);
516 p1 = talloc_size(root, 10);
517 CHECK_SIZE("realloc", p1, 10);
519 p1 = talloc_realloc_size(NULL, p1, 20);
520 CHECK_SIZE("realloc", p1, 20);
524 p2 = talloc_realloc_size(p1, NULL, 30);
528 p2 = talloc_realloc_size(p1, p2, 40);
530 CHECK_SIZE("realloc", p2, 40);
531 CHECK_SIZE("realloc", root, 60);
532 CHECK_BLOCKS("realloc", p1, 4);
534 p1 = talloc_realloc_size(NULL, p1, 20);
535 CHECK_SIZE("realloc", p1, 60);
537 talloc_increase_ref_count(p2);
538 torture_assert("realloc", talloc_realloc_size(NULL, p2, 5) == NULL,
539 "failed: talloc_realloc() on a referenced pointer should fail\n");
540 CHECK_BLOCKS("realloc", p1, 4);
542 talloc_realloc_size(NULL, p2, 0);
543 talloc_realloc_size(NULL, p2, 0);
544 CHECK_BLOCKS("realloc", p1, 3);
546 torture_assert("realloc", talloc_realloc_size(NULL, p1, 0x7fffffff) == NULL,
547 "failed: oversize talloc should fail\n");
549 talloc_realloc_size(NULL, p1, 0);
551 CHECK_BLOCKS("realloc", root, 1);
552 CHECK_SIZE("realloc", root, 0);
556 printf("success: realloc\n");
562 test realloc with a child
564 static bool test_realloc_child(void)
572 struct el2 **list, **list2, **list3;
575 printf("test: REALLOC WITH CHILD\n");
577 root = talloc_new(NULL);
579 el1 = talloc(root, struct el1);
580 el1->list = talloc(el1, struct el2 *);
581 el1->list[0] = talloc(el1->list, struct el2);
582 el1->list[0]->name = talloc_strdup(el1->list[0], "testing");
584 el1->list2 = talloc(el1, struct el2 *);
585 el1->list2[0] = talloc(el1->list2, struct el2);
586 el1->list2[0]->name = talloc_strdup(el1->list2[0], "testing2");
588 el1->list3 = talloc(el1, struct el2 *);
589 el1->list3[0] = talloc(el1->list3, struct el2);
590 el1->list3[0]->name = talloc_strdup(el1->list3[0], "testing2");
592 el2 = talloc(el1->list, struct el2);
593 el2 = talloc(el1->list2, struct el2);
594 el2 = talloc(el1->list3, struct el2);
596 el1->list = talloc_realloc(el1, el1->list, struct el2 *, 100);
597 el1->list2 = talloc_realloc(el1, el1->list2, struct el2 *, 200);
598 el1->list3 = talloc_realloc(el1, el1->list3, struct el2 *, 300);
602 printf("success: REALLOC WITH CHILD\n");
609 static bool test_type(void)
620 printf("test: type\n# talloc type checking\n");
622 root = talloc_new(NULL);
624 el1 = talloc(root, struct el1);
628 torture_assert("type", talloc_get_type(el1, struct el1) == el1,
629 "type check failed on el1\n");
630 torture_assert("type", talloc_get_type(el1, struct el2) == NULL,
631 "type check failed on el1 with el2\n");
632 talloc_set_type(el1, struct el2);
633 torture_assert("type", talloc_get_type(el1, struct el2) == (struct el2 *)el1,
634 "type set failed on el1 with el2\n");
638 printf("success: type\n");
645 static bool test_steal(void)
647 void *root, *p1, *p2;
649 printf("test: steal\n# STEAL\n");
651 root = talloc_new(NULL);
653 p1 = talloc_array(root, char, 10);
654 CHECK_SIZE("steal", p1, 10);
656 p2 = talloc_realloc(root, NULL, char, 20);
657 CHECK_SIZE("steal", p1, 10);
658 CHECK_SIZE("steal", root, 30);
660 torture_assert("steal", talloc_steal(p1, NULL) == NULL,
661 "failed: stealing NULL should give NULL\n");
663 torture_assert("steal", talloc_steal(p1, p1) == p1,
664 "failed: stealing to ourselves is a nop\n");
665 CHECK_BLOCKS("steal", root, 3);
666 CHECK_SIZE("steal", root, 30);
668 talloc_steal(NULL, p1);
669 talloc_steal(NULL, p2);
670 CHECK_BLOCKS("steal", root, 1);
671 CHECK_SIZE("steal", root, 0);
674 talloc_steal(root, p2);
675 CHECK_BLOCKS("steal", root, 2);
676 CHECK_SIZE("steal", root, 20);
680 CHECK_BLOCKS("steal", root, 1);
681 CHECK_SIZE("steal", root, 0);
685 p1 = talloc_size(NULL, 3);
686 talloc_report_full(NULL, stderr);
687 CHECK_SIZE("steal", NULL, 3);
690 printf("success: steal\n");
697 static bool test_move(void)
705 printf("test: move\n# MOVE\n");
707 root = talloc_new(NULL);
709 t1 = talloc(root, struct t_move);
710 t2 = talloc(root, struct t_move);
711 t1->p = talloc_strdup(t1, "foo");
712 t1->x = talloc(t1, int);
715 t2->p = talloc_move(t2, &t1->p);
716 t2->x = talloc_move(t2, &t1->x);
717 torture_assert("move", t1->p == NULL && t1->x == NULL &&
718 strcmp(t2->p, "foo") == 0 && *t2->x == 42,
719 "talloc move failed");
723 printf("success: move\n");
729 test talloc_realloc_fn
731 static bool test_realloc_fn(void)
735 printf("test: realloc_fn\n# talloc_realloc_fn\n");
737 root = talloc_new(NULL);
739 p1 = talloc_realloc_fn(root, NULL, 10);
740 CHECK_BLOCKS("realloc_fn", root, 2);
741 CHECK_SIZE("realloc_fn", root, 10);
742 p1 = talloc_realloc_fn(root, p1, 20);
743 CHECK_BLOCKS("realloc_fn", root, 2);
744 CHECK_SIZE("realloc_fn", root, 20);
745 p1 = talloc_realloc_fn(root, p1, 0);
746 CHECK_BLOCKS("realloc_fn", root, 1);
747 CHECK_SIZE("realloc_fn", root, 0);
751 printf("success: realloc_fn\n");
756 static bool test_unref_reparent(void)
758 void *root, *p1, *p2, *c1;
760 printf("test: unref_reparent\n# UNREFERENCE AFTER PARENT FREED\n");
762 root = talloc_named_const(NULL, 0, "root");
763 p1 = talloc_named_const(root, 1, "orig parent");
764 p2 = talloc_named_const(root, 1, "parent by reference");
766 c1 = talloc_named_const(p1, 1, "child");
767 talloc_reference(p2, c1);
769 CHECK_PARENT("unref_reparent", c1, p1);
773 CHECK_PARENT("unref_reparent", c1, p2);
775 talloc_unlink(p2, c1);
777 CHECK_SIZE("unref_reparent", root, 1);
782 printf("success: unref_reparent\n");
787 measure the speed of talloc versus malloc
789 static bool test_speed(void)
791 void *ctx = talloc_new(NULL);
793 const int loop = 1000;
797 printf("test: speed\n# TALLOC VS MALLOC SPEED\n");
799 tv = timeval_current();
803 for (i=0;i<loop;i++) {
804 p1 = talloc_size(ctx, loop % 100);
805 p2 = talloc_strdup(p1, "foo bar");
806 p3 = talloc_size(p1, 300);
810 } while (timeval_elapsed(&tv) < 5.0);
812 fprintf(stderr, "talloc: %.0f ops/sec\n", count/timeval_elapsed(&tv));
816 ctx = talloc_pool(NULL, 1024);
818 tv = timeval_current();
822 for (i=0;i<loop;i++) {
823 p1 = talloc_size(ctx, loop % 100);
824 p2 = talloc_strdup(p1, "foo bar");
825 p3 = talloc_size(p1, 300);
826 talloc_free_children(ctx);
829 } while (timeval_elapsed(&tv) < 5.0);
833 fprintf(stderr, "talloc_pool: %.0f ops/sec\n", count/timeval_elapsed(&tv));
835 tv = timeval_current();
839 for (i=0;i<loop;i++) {
840 p1 = malloc(loop % 100);
841 p2 = strdup("foo bar");
848 } while (timeval_elapsed(&tv) < 5.0);
849 fprintf(stderr, "malloc: %.0f ops/sec\n", count/timeval_elapsed(&tv));
851 printf("success: speed\n");
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("test: lifeless\n# 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, stderr);
869 talloc_unlink(top, parent);
871 talloc_report_full(top, stderr);
873 talloc_free(child_owner);
876 printf("success: lifeless\n");
880 static int loop_destructor_count;
882 static int test_loop_destructor(char *ptr)
884 loop_destructor_count++;
888 static bool test_loop(void)
890 void *top = talloc_new(NULL);
896 printf("test: loop\n# TALLOC LOOP DESTRUCTION\n");
898 parent = talloc_strdup(top, "parent");
899 req1 = talloc(parent, struct req1);
900 req1->req2 = talloc_strdup(req1, "req2");
901 talloc_set_destructor(req1->req2, test_loop_destructor);
902 req1->req3 = talloc_strdup(req1, "req3");
903 (void)talloc_reference(req1->req3, req1);
904 talloc_report_full(top, stderr);
906 talloc_report_full(top, stderr);
907 talloc_report_full(NULL, stderr);
910 torture_assert("loop", loop_destructor_count == 1,
911 "FAILED TO FIRE LOOP DESTRUCTOR\n");
912 loop_destructor_count = 0;
914 printf("success: loop\n");
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("test: free_parent_deny_child\n# TALLOC FREE PARENT DENY CHILD\n");
932 level1 = talloc_strdup(top, "level1");
933 level2 = talloc_strdup(level1, "level2");
934 level3 = talloc_strdup(level2, "level3");
936 talloc_set_destructor(level3, fail_destructor_str);
938 talloc_set_destructor(level3, NULL);
940 CHECK_PARENT("free_parent_deny_child", level3, top);
944 printf("success: free_parent_deny_child\n");
948 static bool test_talloc_ptrtype(void)
950 void *top = talloc_new(NULL);
954 } *s1, *s2, **s3, ***s4;
955 const char *location1;
956 const char *location2;
957 const char *location3;
958 const char *location4;
960 printf("test: ptrtype\n# TALLOC PTRTYPE\n");
962 s1 = talloc_ptrtype(top, s1);location1 = __location__;
964 if (talloc_get_size(s1) != sizeof(struct struct1)) {
965 printf("failure: ptrtype [\n"
966 "talloc_ptrtype() allocated the wrong size %lu (should be %lu)\n"
967 "]\n", (unsigned long)talloc_get_size(s1),
968 (unsigned long)sizeof(struct struct1));
972 if (strcmp(location1, talloc_get_name(s1)) != 0) {
973 printf("failure: ptrtype [\n"
974 "talloc_ptrtype() sets the wrong name '%s' (should be '%s')\n]\n",
975 talloc_get_name(s1), location1);
979 s2 = talloc_array_ptrtype(top, s2, 10);location2 = __location__;
981 if (talloc_get_size(s2) != (sizeof(struct struct1) * 10)) {
982 printf("failure: ptrtype [\n"
983 "talloc_array_ptrtype() allocated the wrong size "
984 "%lu (should be %lu)\n]\n",
985 (unsigned long)talloc_get_size(s2),
986 (unsigned long)(sizeof(struct struct1)*10));
990 if (strcmp(location2, talloc_get_name(s2)) != 0) {
991 printf("failure: ptrtype [\n"
992 "talloc_array_ptrtype() sets the wrong name '%s' (should be '%s')\n]\n",
993 talloc_get_name(s2), location2);
997 s3 = talloc_array_ptrtype(top, s3, 10);location3 = __location__;
999 if (talloc_get_size(s3) != (sizeof(struct struct1 *) * 10)) {
1000 printf("failure: ptrtype [\n"
1001 "talloc_array_ptrtype() allocated the wrong size "
1002 "%lu (should be %lu)\n]\n",
1003 (unsigned long)talloc_get_size(s3),
1004 (unsigned long)(sizeof(struct struct1 *)*10));
1008 torture_assert_str_equal("ptrtype", location3, talloc_get_name(s3),
1009 "talloc_array_ptrtype() sets the wrong name");
1011 s4 = talloc_array_ptrtype(top, s4, 10);location4 = __location__;
1013 if (talloc_get_size(s4) != (sizeof(struct struct1 **) * 10)) {
1014 printf("failure: ptrtype [\n"
1015 "talloc_array_ptrtype() allocated the wrong size "
1016 "%lu (should be %lu)\n]\n",
1017 (unsigned long)talloc_get_size(s4),
1018 (unsigned long)(sizeof(struct struct1 **)*10));
1022 torture_assert_str_equal("ptrtype", location4, talloc_get_name(s4),
1023 "talloc_array_ptrtype() sets the wrong name");
1027 printf("success: ptrtype\n");
1031 static int _test_talloc_free_in_destructor(void **ptr)
1037 static bool test_talloc_free_in_destructor(void)
1046 printf("test: free_in_destructor\n# TALLOC FREE IN DESTRUCTOR\n");
1048 level0 = talloc_new(NULL);
1049 level1 = talloc_new(level0);
1050 level2 = talloc_new(level1);
1051 level3 = talloc_new(level2);
1052 level4 = talloc_new(level3);
1053 level5 = talloc(level4, void *);
1056 (void)talloc_reference(level0, level3);
1057 (void)talloc_reference(level3, level3);
1058 (void)talloc_reference(level5, level3);
1060 talloc_set_destructor(level5, _test_talloc_free_in_destructor);
1062 talloc_free(level1);
1064 talloc_free(level0);
1066 printf("success: free_in_destructor\n");
1070 static bool test_autofree(void)
1072 #if _SAMBA_BUILD_ < 4
1073 /* autofree test would kill smbtorture */
1075 printf("test: autofree\n# TALLOC AUTOFREE CONTEXT\n");
1077 p = talloc_autofree_context();
1080 p = talloc_autofree_context();
1083 printf("success: autofree\n");
1088 static bool test_pool(void)
1091 void *p1, *p2, *p3, *p4;
1093 pool = talloc_pool(NULL, 1024);
1095 p1 = talloc_size(pool, 80);
1096 p2 = talloc_size(pool, 20);
1097 p3 = talloc_size(p1, 50);
1098 p4 = talloc_size(p3, 1000);
1105 struct torture_context;
1106 bool torture_local_talloc(struct torture_context *tctx)
1112 talloc_disable_null_tracking();
1113 talloc_enable_null_tracking();
1119 ret &= test_unlink1();
1121 ret &= test_realloc();
1122 ret &= test_realloc_child();
1123 ret &= test_steal();
1125 ret &= test_unref_reparent();
1126 ret &= test_realloc_fn();
1128 ret &= test_lifeless();
1130 ret &= test_free_parent_deny_child();
1131 ret &= test_talloc_ptrtype();
1132 ret &= test_talloc_free_in_destructor();
1136 ret &= test_speed();
1138 ret &= test_autofree();
1143 #if _SAMBA_BUILD_ < 4
1146 bool ret = torture_local_talloc(NULL);