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 (arg1 == NULL && arg2 == NULL) { \
52 } else if (strcmp(arg1, arg2)) { \
53 printf("failure: %s [\n%s: Expected %s, got %s: %s\n]\n", \
54 test, __location__, arg1, arg2, desc); \
67 #define CHECK_SIZE(test, ptr, tsize) do { \
68 if (talloc_total_size(ptr) != (tsize)) { \
69 printf("failed: %s [\n%s: wrong '%s' tree size: got %u expected %u\n]\n", \
70 test, __location__, #ptr, \
71 (unsigned)talloc_total_size(ptr), \
73 talloc_report_full(ptr, stdout); \
78 #define CHECK_BLOCKS(test, ptr, tblocks) do { \
79 if (talloc_total_blocks(ptr) != (tblocks)) { \
80 printf("failed: %s [\n%s: wrong '%s' tree blocks: got %u expected %u\n]\n", \
81 test, __location__, #ptr, \
82 (unsigned)talloc_total_blocks(ptr), \
84 talloc_report_full(ptr, stdout); \
89 #define CHECK_PARENT(test, ptr, parent) do { \
90 if (talloc_parent(ptr) != (parent)) { \
91 printf("failed: %s [\n%s: '%s' has wrong parent: got %p expected %p\n]\n", \
92 test, __location__, #ptr, \
95 talloc_report_full(ptr, stdout); \
96 talloc_report_full(parent, stdout); \
97 talloc_report_full(NULL, stdout); \
102 #define CHECK_NOT_PARENT(test, ptr, parent) do { \
103 if (talloc_parent(ptr) == (parent)) { \
104 printf("failed: %s [\n%s: '%s' has wrong parent: got %p expected a difference\n]\n", \
105 test, __location__, #ptr, \
107 talloc_report_full(ptr, stdout); \
108 talloc_report_full(parent, stdout); \
109 talloc_report_full(NULL, stdout); \
118 static bool test_ref1(void)
120 void *root, *p1, *p2, *ref, *r1;
122 printf("test: ref1\n# SINGLE REFERENCE FREE\n");
124 root = talloc_named_const(NULL, 0, "root");
125 p1 = talloc_named_const(root, 1, "p1");
126 p2 = talloc_named_const(p1, 1, "p2");
127 talloc_named_const(p1, 1, "x1");
128 talloc_named_const(p1, 2, "x2");
129 talloc_named_const(p1, 3, "x3");
131 r1 = talloc_named_const(root, 1, "r1");
132 ref = talloc_reference(r1, p2);
133 talloc_report_full(root, stderr);
135 CHECK_BLOCKS("ref1", p1, 5);
136 CHECK_BLOCKS("ref1", p2, 1);
137 CHECK_BLOCKS("ref1", r1, 2);
139 fprintf(stderr, "Freeing p2\n");
141 talloc_report_full(root, stderr);
143 CHECK_BLOCKS("ref1", p1, 4);
144 CHECK_BLOCKS("ref1", p2, 1);
145 CHECK_BLOCKS("ref1", r1, 2);
147 fprintf(stderr, "Freeing p1\n");
149 talloc_report_full(root, stderr);
151 CHECK_BLOCKS("ref1", r1, 2);
153 fprintf(stderr, "Freeing r1\n");
155 talloc_report_full(NULL, stderr);
157 fprintf(stderr, "Testing NULL\n");
158 if (talloc_reference(root, NULL)) {
162 CHECK_BLOCKS("ref1", root, 1);
164 CHECK_SIZE("ref1", root, 0);
167 printf("success: ref1\n");
174 static bool test_ref2(void)
176 void *root, *p1, *p2, *ref, *r1;
178 printf("test: ref2\n# DOUBLE REFERENCE FREE\n");
179 root = talloc_named_const(NULL, 0, "root");
180 p1 = talloc_named_const(root, 1, "p1");
181 talloc_named_const(p1, 1, "x1");
182 talloc_named_const(p1, 1, "x2");
183 talloc_named_const(p1, 1, "x3");
184 p2 = talloc_named_const(p1, 1, "p2");
186 r1 = talloc_named_const(root, 1, "r1");
187 ref = talloc_reference(r1, p2);
188 talloc_report_full(root, stderr);
190 CHECK_BLOCKS("ref2", p1, 5);
191 CHECK_BLOCKS("ref2", p2, 1);
192 CHECK_BLOCKS("ref2", r1, 2);
194 fprintf(stderr, "Freeing ref\n");
195 talloc_unlink(r1, ref);
196 talloc_report_full(root, stderr);
198 CHECK_BLOCKS("ref2", p1, 5);
199 CHECK_BLOCKS("ref2", p2, 1);
200 CHECK_BLOCKS("ref2", r1, 1);
202 fprintf(stderr, "Freeing p2\n");
204 talloc_report_full(root, stderr);
206 CHECK_BLOCKS("ref2", p1, 4);
207 CHECK_BLOCKS("ref2", r1, 1);
209 fprintf(stderr, "Freeing p1\n");
211 talloc_report_full(root, stderr);
213 CHECK_BLOCKS("ref2", r1, 1);
215 fprintf(stderr, "Freeing r1\n");
217 talloc_report_full(root, stderr);
219 CHECK_SIZE("ref2", root, 0);
222 printf("success: ref2\n");
229 static bool test_ref3(void)
231 void *root, *p1, *p2, *ref, *r1;
233 printf("test: ref3\n# PARENT REFERENCE FREE\n");
235 root = talloc_named_const(NULL, 0, "root");
236 p1 = talloc_named_const(root, 1, "p1");
237 p2 = talloc_named_const(root, 1, "p2");
238 r1 = talloc_named_const(p1, 1, "r1");
239 ref = talloc_reference(p2, r1);
240 talloc_report_full(root, stderr);
242 CHECK_BLOCKS("ref3", p1, 2);
243 CHECK_BLOCKS("ref3", p2, 2);
244 CHECK_BLOCKS("ref3", r1, 1);
246 fprintf(stderr, "Freeing p1\n");
248 talloc_report_full(root, stderr);
250 CHECK_BLOCKS("ref3", p2, 2);
251 CHECK_BLOCKS("ref3", r1, 1);
253 fprintf(stderr, "Freeing p2\n");
255 talloc_report_full(root, stderr);
257 CHECK_SIZE("ref3", root, 0);
261 printf("success: ref3\n");
268 static bool test_ref4(void)
270 void *root, *p1, *p2, *ref, *r1;
272 printf("test: ref4\n# REFERRER REFERENCE FREE\n");
274 root = talloc_named_const(NULL, 0, "root");
275 p1 = talloc_named_const(root, 1, "p1");
276 talloc_named_const(p1, 1, "x1");
277 talloc_named_const(p1, 1, "x2");
278 talloc_named_const(p1, 1, "x3");
279 p2 = talloc_named_const(p1, 1, "p2");
281 r1 = talloc_named_const(root, 1, "r1");
282 ref = talloc_reference(r1, p2);
283 talloc_report_full(root, stderr);
285 CHECK_BLOCKS("ref4", p1, 5);
286 CHECK_BLOCKS("ref4", p2, 1);
287 CHECK_BLOCKS("ref4", r1, 2);
289 fprintf(stderr, "Freeing r1\n");
291 talloc_report_full(root, stderr);
293 CHECK_BLOCKS("ref4", p1, 5);
294 CHECK_BLOCKS("ref4", p2, 1);
296 fprintf(stderr, "Freeing p2\n");
298 talloc_report_full(root, stderr);
300 CHECK_BLOCKS("ref4", p1, 4);
302 fprintf(stderr, "Freeing p1\n");
304 talloc_report_full(root, stderr);
306 CHECK_SIZE("ref4", root, 0);
310 printf("success: ref4\n");
318 static bool test_unlink1(void)
320 void *root, *p1, *p2, *ref, *r1;
322 printf("test: unlink\n# 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, stderr);
335 CHECK_BLOCKS("unlink", p1, 7);
336 CHECK_BLOCKS("unlink", p2, 1);
337 CHECK_BLOCKS("unlink", r1, 2);
339 fprintf(stderr, "Unreferencing r1\n");
340 talloc_unlink(r1, p2);
341 talloc_report_full(root, stderr);
343 CHECK_BLOCKS("unlink", p1, 6);
344 CHECK_BLOCKS("unlink", p2, 1);
345 CHECK_BLOCKS("unlink", r1, 1);
347 fprintf(stderr, "Freeing p1\n");
349 talloc_report_full(root, stderr);
351 CHECK_SIZE("unlink", root, 0);
355 printf("success: unlink\n");
359 static int fail_destructor(void *ptr)
365 miscellaneous tests to try to get a higher test coverage percentage
367 static bool test_misc(void)
374 printf("test: misc\n# MISCELLANEOUS\n");
376 root = talloc_new(NULL);
378 p1 = talloc_size(root, 0x7fffffff);
379 torture_assert("misc", !p1, "failed: large talloc allowed\n");
381 p1 = talloc_strdup(root, "foo");
382 talloc_increase_ref_count(p1);
383 talloc_increase_ref_count(p1);
384 talloc_increase_ref_count(p1);
385 CHECK_BLOCKS("misc", NULL, 7);
386 CHECK_BLOCKS("misc", p1, 1);
387 CHECK_BLOCKS("misc", root, 2);
388 talloc_unlink(NULL, p1);
389 CHECK_BLOCKS("misc", NULL, 6);
390 CHECK_BLOCKS("misc", p1, 1);
391 CHECK_BLOCKS("misc", root, 2);
392 p2 = talloc_strdup(p1, "foo");
393 torture_assert("misc", talloc_unlink(root, p2) == -1,
394 "failed: talloc_unlink() of non-reference context should return -1\n");
395 torture_assert("misc", talloc_unlink(p1, p2) == 0,
396 "failed: talloc_unlink() of parent should succeed\n");
398 CHECK_BLOCKS("misc", NULL, 6);
399 CHECK_BLOCKS("misc", p1, 1);
400 CHECK_BLOCKS("misc", root, 1);
401 talloc_unlink(NULL, p1);
402 CHECK_BLOCKS("misc", NULL, 5);
403 CHECK_BLOCKS("misc", p1, 1);
404 CHECK_BLOCKS("misc", root, 1);
406 name = talloc_set_name(p1, "my name is %s", "foo");
407 torture_assert_str_equal("misc", talloc_get_name(p1), "my name is foo",
408 "failed: wrong name after talloc_set_name(my name is foo)");
409 CHECK_BLOCKS("misc", NULL, 6);
410 CHECK_BLOCKS("misc", p1, 2);
411 CHECK_BLOCKS("misc", root, 1);
413 talloc_set_name_const(p1, NULL);
414 torture_assert_str_equal ("misc", talloc_get_name(p1), "UNNAMED",
415 "failed: wrong name after talloc_set_name(NULL)");
416 CHECK_BLOCKS("misc", NULL, 6);
417 CHECK_BLOCKS("misc", p1, 2);
418 CHECK_BLOCKS("misc", root, 1);
420 torture_assert("misc", talloc_free(NULL) == -1,
421 "talloc_free(NULL) should give -1\n");
423 talloc_set_destructor(p1, fail_destructor);
424 torture_assert("misc", talloc_unlink(NULL, p1) == -1,
425 "Failed destructor should cause talloc_unlink to fail\n");
426 talloc_set_destructor(p1, NULL);
427 CHECK_BLOCKS("misc", NULL, 6);
428 CHECK_BLOCKS("misc", p1, 2);
429 CHECK_BLOCKS("misc", root, 1);
431 talloc_report(root, stderr);
434 p2 = (char *)talloc_zero_size(p1, 20);
435 torture_assert("misc", p2[19] == 0, "Failed to give zero memory\n");
438 torture_assert("misc", talloc_strdup(root, NULL) == NULL,
439 "failed: strdup on NULL should give NULL\n");
441 p2 = talloc_strndup(p1, "foo", 2);
442 torture_assert("misc", strcmp("fo", p2) == 0,
443 "strndup doesn't work\n");
444 p2 = talloc_asprintf_append_buffer(p2, "o%c", 'd');
445 torture_assert("misc", strcmp("food", p2) == 0,
446 "talloc_asprintf_append_buffer doesn't work\n");
447 CHECK_BLOCKS("misc", p2, 1);
448 CHECK_BLOCKS("misc", p1, 3);
450 p2 = talloc_asprintf_append_buffer(NULL, "hello %s", "world");
451 torture_assert("misc", strcmp("hello world", p2) == 0,
452 "talloc_asprintf_append_buffer doesn't work\n");
453 CHECK_BLOCKS("misc", p2, 1);
454 CHECK_BLOCKS("misc", p1, 3);
457 d = talloc_array(p1, double, 0x20000000);
458 torture_assert("misc", !d, "failed: integer overflow not detected\n");
460 d = talloc_realloc(p1, d, double, 0x20000000);
461 torture_assert("misc", !d, "failed: integer overflow not detected\n");
463 CHECK_BLOCKS("misc", NULL, 7);
464 talloc_unlink(NULL, p1);
465 CHECK_BLOCKS("misc", NULL, 3);
466 CHECK_BLOCKS("misc", root, 1);
468 p1 = talloc_named(root, 100, "%d bytes", 100);
469 CHECK_BLOCKS("misc", NULL, 5);
470 CHECK_BLOCKS("misc", p1, 2);
471 CHECK_BLOCKS("misc", root, 3);
472 talloc_unlink(root, p1);
473 CHECK_BLOCKS("misc", NULL, 3);
474 CHECK_BLOCKS("misc", root, 1);
476 p1 = talloc_init("%d bytes", 200);
477 p2 = talloc_asprintf(p1, "my test '%s'", "string");
478 torture_assert_str_equal("misc", p2, "my test 'string'",
479 "failed: talloc_asprintf(\"my test '%%s'\", \"string\") gave: \"%s\"");
480 CHECK_BLOCKS("misc", p1, 3);
481 CHECK_SIZE("misc", p2, 17);
482 CHECK_BLOCKS("misc", root, 1);
483 talloc_unlink(NULL, p1);
485 p1 = talloc_named_const(root, 10, "p1");
486 p2 = (char *)talloc_named_const(root, 20, "p2");
487 (void)talloc_reference(p1, p2);
488 talloc_report_full(root, stderr);
489 talloc_unlink(root, p2);
490 talloc_report_full(root, stderr);
491 CHECK_BLOCKS("misc", p2, 1);
492 CHECK_BLOCKS("misc", p1, 2);
493 CHECK_BLOCKS("misc", root, 3);
494 talloc_unlink(p1, p2);
495 talloc_unlink(root, p1);
497 p1 = talloc_named_const(root, 10, "p1");
498 p2 = (char *)talloc_named_const(root, 20, "p2");
499 (void)talloc_reference(NULL, p2);
500 talloc_report_full(root, stderr);
501 talloc_unlink(root, p2);
502 talloc_report_full(root, stderr);
503 CHECK_BLOCKS("misc", p2, 1);
504 CHECK_BLOCKS("misc", p1, 1);
505 CHECK_BLOCKS("misc", root, 2);
506 talloc_unlink(NULL, p2);
507 talloc_unlink(root, p1);
509 /* Test that talloc_unlink is a no-op */
511 torture_assert("misc", talloc_unlink(root, NULL) == -1,
512 "failed: talloc_unlink(root, NULL) == -1\n");
514 talloc_report(root, stderr);
515 talloc_report(NULL, stderr);
517 CHECK_SIZE("misc", root, 0);
521 CHECK_SIZE("misc", NULL, 0);
523 talloc_enable_leak_report();
524 talloc_enable_leak_report_full();
526 printf("success: misc\n");
535 static bool test_realloc(void)
537 void *root, *p1, *p2;
539 printf("test: realloc\n# REALLOC\n");
541 root = talloc_new(NULL);
543 p1 = talloc_size(root, 10);
544 CHECK_SIZE("realloc", p1, 10);
546 p1 = talloc_realloc_size(NULL, p1, 20);
547 CHECK_SIZE("realloc", p1, 20);
551 p2 = talloc_realloc_size(p1, NULL, 30);
555 p2 = talloc_realloc_size(p1, p2, 40);
557 CHECK_SIZE("realloc", p2, 40);
558 CHECK_SIZE("realloc", root, 60);
559 CHECK_BLOCKS("realloc", p1, 4);
561 p1 = talloc_realloc_size(NULL, p1, 20);
562 CHECK_SIZE("realloc", p1, 60);
564 talloc_increase_ref_count(p2);
565 torture_assert("realloc", talloc_realloc_size(NULL, p2, 5) == NULL,
566 "failed: talloc_realloc() on a referenced pointer should fail\n");
567 CHECK_BLOCKS("realloc", p1, 4);
569 talloc_realloc_size(NULL, p2, 0);
570 talloc_realloc_size(NULL, p2, 0);
571 CHECK_BLOCKS("realloc", p1, 3);
573 torture_assert("realloc", talloc_realloc_size(NULL, p1, 0x7fffffff) == NULL,
574 "failed: oversize talloc should fail\n");
576 talloc_realloc_size(NULL, p1, 0);
578 CHECK_BLOCKS("realloc", root, 1);
579 CHECK_SIZE("realloc", root, 0);
583 printf("success: realloc\n");
589 test realloc with a child
591 static bool test_realloc_child(void)
599 struct el2 **list, **list2, **list3;
602 printf("test: REALLOC WITH CHILD\n");
604 root = talloc_new(NULL);
606 el1 = talloc(root, struct el1);
607 el1->list = talloc(el1, struct el2 *);
608 el1->list[0] = talloc(el1->list, struct el2);
609 el1->list[0]->name = talloc_strdup(el1->list[0], "testing");
611 el1->list2 = talloc(el1, struct el2 *);
612 el1->list2[0] = talloc(el1->list2, struct el2);
613 el1->list2[0]->name = talloc_strdup(el1->list2[0], "testing2");
615 el1->list3 = talloc(el1, struct el2 *);
616 el1->list3[0] = talloc(el1->list3, struct el2);
617 el1->list3[0]->name = talloc_strdup(el1->list3[0], "testing2");
619 el2 = talloc(el1->list, struct el2);
620 el2 = talloc(el1->list2, struct el2);
621 el2 = talloc(el1->list3, struct el2);
623 el1->list = talloc_realloc(el1, el1->list, struct el2 *, 100);
624 el1->list2 = talloc_realloc(el1, el1->list2, struct el2 *, 200);
625 el1->list3 = talloc_realloc(el1, el1->list3, struct el2 *, 300);
629 printf("success: REALLOC WITH CHILD\n");
636 static bool test_type(void)
647 printf("test: type\n# talloc type checking\n");
649 root = talloc_new(NULL);
651 el1 = talloc(root, struct el1);
655 torture_assert("type", talloc_get_type(el1, struct el1) == el1,
656 "type check failed on el1\n");
657 torture_assert("type", talloc_get_type(el1, struct el2) == NULL,
658 "type check failed on el1 with el2\n");
659 talloc_set_type(el1, struct el2);
660 torture_assert("type", talloc_get_type(el1, struct el2) == (struct el2 *)el1,
661 "type set failed on el1 with el2\n");
665 printf("success: type\n");
672 static bool test_steal(void)
674 void *root, *p1, *p2;
676 printf("test: steal\n# STEAL\n");
678 root = talloc_new(NULL);
680 p1 = talloc_array(root, char, 10);
681 CHECK_SIZE("steal", p1, 10);
683 p2 = talloc_realloc(root, NULL, char, 20);
684 CHECK_SIZE("steal", p1, 10);
685 CHECK_SIZE("steal", root, 30);
687 torture_assert("steal", talloc_steal(p1, NULL) == NULL,
688 "failed: stealing NULL should give NULL\n");
690 torture_assert("steal", talloc_steal(p1, p1) == p1,
691 "failed: stealing to ourselves is a nop\n");
692 CHECK_BLOCKS("steal", root, 3);
693 CHECK_SIZE("steal", root, 30);
695 talloc_steal(NULL, p1);
696 talloc_steal(NULL, p2);
697 CHECK_BLOCKS("steal", root, 1);
698 CHECK_SIZE("steal", root, 0);
701 talloc_steal(root, p2);
702 CHECK_BLOCKS("steal", root, 2);
703 CHECK_SIZE("steal", root, 20);
707 CHECK_BLOCKS("steal", root, 1);
708 CHECK_SIZE("steal", root, 0);
712 p1 = talloc_size(NULL, 3);
713 talloc_report_full(NULL, stderr);
714 CHECK_SIZE("steal", NULL, 3);
717 printf("success: steal\n");
724 static bool test_move(void)
732 printf("test: move\n# MOVE\n");
734 root = talloc_new(NULL);
736 t1 = talloc(root, struct t_move);
737 t2 = talloc(root, struct t_move);
738 t1->p = talloc_strdup(t1, "foo");
739 t1->x = talloc(t1, int);
742 t2->p = talloc_move(t2, &t1->p);
743 t2->x = talloc_move(t2, &t1->x);
744 torture_assert("move", t1->p == NULL && t1->x == NULL &&
745 strcmp(t2->p, "foo") == 0 && *t2->x == 42,
746 "talloc move failed");
750 printf("success: move\n");
756 test talloc_realloc_fn
758 static bool test_realloc_fn(void)
762 printf("test: realloc_fn\n# talloc_realloc_fn\n");
764 root = talloc_new(NULL);
766 p1 = talloc_realloc_fn(root, NULL, 10);
767 CHECK_BLOCKS("realloc_fn", root, 2);
768 CHECK_SIZE("realloc_fn", root, 10);
769 p1 = talloc_realloc_fn(root, p1, 20);
770 CHECK_BLOCKS("realloc_fn", root, 2);
771 CHECK_SIZE("realloc_fn", root, 20);
772 p1 = talloc_realloc_fn(root, p1, 0);
773 CHECK_BLOCKS("realloc_fn", root, 1);
774 CHECK_SIZE("realloc_fn", root, 0);
778 printf("success: realloc_fn\n");
783 static bool test_unref_reparent(void)
785 void *root, *p1, *p2, *c1;
787 printf("test: unref_reparent\n# UNREFERENCE AFTER PARENT FREED\n");
789 root = talloc_named_const(NULL, 0, "root");
790 p1 = talloc_named_const(root, 1, "orig parent");
791 p2 = talloc_named_const(root, 1, "parent by reference");
793 c1 = talloc_named_const(p1, 1, "child");
794 CHECK_BLOCKS("unref_reparent", root, 4);
795 CHECK_SIZE("unref_reparent", root, 3);
796 talloc_reference(p2, c1);
797 CHECK_BLOCKS("unref_reparent", root, 5);
798 CHECK_SIZE("unref_reparent", root, 3);
800 CHECK_BLOCKS("unref_reparent", root, 5);
801 CHECK_SIZE("unref_reparent", root, 3);
802 CHECK_PARENT("unref_reparent", c1, p1);
806 CHECK_BLOCKS("unref_reparent", root, 3);
807 CHECK_SIZE("unref_reparent", root, 1);
808 CHECK_NOT_PARENT("unref_reparent", c1, p2);
810 talloc_unlink(p2, c1);
812 CHECK_BLOCKS("unref_reparent", root, 2);
813 CHECK_SIZE("unref_reparent", root, 1);
818 printf("success: unref_reparent\n");
823 measure the speed of talloc versus malloc
825 static bool test_speed(void)
827 void *ctx = talloc_new(NULL);
829 const int loop = 1000;
833 printf("test: speed\n# TALLOC VS MALLOC SPEED\n");
835 tv = timeval_current();
839 for (i=0;i<loop;i++) {
840 p1 = talloc_size(ctx, loop % 100);
841 p2 = talloc_strdup(p1, "foo bar");
842 p3 = talloc_size(p1, 300);
846 } while (timeval_elapsed(&tv) < 5.0);
848 fprintf(stderr, "talloc: %.0f ops/sec\n", count/timeval_elapsed(&tv));
852 ctx = talloc_pool(NULL, 1024);
854 tv = timeval_current();
858 for (i=0;i<loop;i++) {
859 p1 = talloc_size(ctx, loop % 100);
860 p2 = talloc_strdup(p1, "foo bar");
861 p3 = talloc_size(p1, 300);
862 talloc_free_children(ctx);
865 } while (timeval_elapsed(&tv) < 5.0);
869 fprintf(stderr, "talloc_pool: %.0f ops/sec\n", count/timeval_elapsed(&tv));
871 tv = timeval_current();
875 for (i=0;i<loop;i++) {
876 p1 = malloc(loop % 100);
877 p2 = strdup("foo bar");
884 } while (timeval_elapsed(&tv) < 5.0);
885 fprintf(stderr, "malloc: %.0f ops/sec\n", count/timeval_elapsed(&tv));
887 printf("success: speed\n");
892 static bool test_lifeless(void)
894 void *top = talloc_named_const(NULL, 0, "top");
895 char *parent, *child;
896 void *child_owner = talloc_named_const(NULL, 0, "child_owner");
898 printf("test: lifeless\n# TALLOC_UNLINK LOOP\n");
900 parent = talloc_strdup(top, "parent");
901 CHECK_BLOCKS("lifeless", NULL, 5);
902 child = talloc_strdup(parent, "child");
903 CHECK_BLOCKS("lifeless", NULL, 6);
904 (void)talloc_reference(child, parent);
905 CHECK_BLOCKS("lifeless", NULL, 7);
906 (void)talloc_reference(child_owner, child);
907 CHECK_BLOCKS("lifeless", NULL, 8);
908 CHECK_BLOCKS("lifeless", top, 4);
909 CHECK_BLOCKS("lifeless", child_owner, 2);
910 talloc_unlink(top, parent);
911 CHECK_BLOCKS("lifeless", NULL, 8);
912 CHECK_BLOCKS("lifeless", top, 1);
913 CHECK_BLOCKS("lifeless", child_owner, 2);
914 CHECK_BLOCKS("lifeless", child, 2);
916 CHECK_BLOCKS("lifeless", NULL, 8);
917 CHECK_BLOCKS("lifeless", top, 1);
918 CHECK_BLOCKS("lifeless", child_owner, 2);
919 CHECK_BLOCKS("lifeless", child, 2);
921 CHECK_BLOCKS("lifeless", NULL, 7);
922 CHECK_BLOCKS("lifeless", child_owner, 2);
923 CHECK_BLOCKS("lifeless", child, 2);
924 talloc_free(child_owner);
925 CHECK_BLOCKS("lifeless", NULL, 2);
927 printf("success: lifeless\n");
931 static int loop_destructor_count;
933 static int test_loop_destructor(char *ptr)
935 loop_destructor_count++;
939 static bool test_loop(void)
941 void *top = talloc_new(NULL);
947 printf("test: loop\n# TALLOC LOOP DESTRUCTION\n");
949 parent = talloc_strdup(top, "parent");
950 req1 = talloc(parent, struct req1);
951 req1->req2 = talloc_strdup(req1, "req2");
952 talloc_set_destructor(req1->req2, test_loop_destructor);
953 req1->req3 = talloc_strdup(req1, "req3");
954 (void)talloc_reference(req1->req3, req1);
955 talloc_report_full(top, stderr);
957 talloc_report_full(top, stderr);
958 talloc_report_full(NULL, stderr);
961 torture_assert("loop", loop_destructor_count == 1,
962 "FAILED TO FIRE LOOP DESTRUCTOR\n");
963 loop_destructor_count = 0;
965 printf("success: loop\n");
969 static int fail_destructor_str(char *ptr)
974 static bool test_free_parent_deny_child(void)
976 void *top = talloc_new(NULL);
981 printf("test: free_parent_deny_child\n# TALLOC FREE PARENT DENY CHILD\n");
983 level1 = talloc_strdup(top, "level1");
984 level2 = talloc_strdup(level1, "level2");
985 level3 = talloc_strdup(level2, "level3");
987 talloc_set_destructor(level3, fail_destructor_str);
989 talloc_set_destructor(level3, NULL);
991 CHECK_PARENT("free_parent_deny_child", level3, top);
995 printf("success: free_parent_deny_child\n");
999 static bool test_talloc_ptrtype(void)
1001 void *top = talloc_new(NULL);
1005 } *s1, *s2, **s3, ***s4;
1006 const char *location1;
1007 const char *location2;
1008 const char *location3;
1009 const char *location4;
1011 printf("test: ptrtype\n# TALLOC PTRTYPE\n");
1013 s1 = talloc_ptrtype(top, s1);location1 = __location__;
1015 if (talloc_get_size(s1) != sizeof(struct struct1)) {
1016 printf("failure: ptrtype [\n"
1017 "talloc_ptrtype() allocated the wrong size %lu (should be %lu)\n"
1018 "]\n", (unsigned long)talloc_get_size(s1),
1019 (unsigned long)sizeof(struct struct1));
1023 if (strcmp(location1, talloc_get_name(s1)) != 0) {
1024 printf("failure: ptrtype [\n"
1025 "talloc_ptrtype() sets the wrong name '%s' (should be '%s')\n]\n",
1026 talloc_get_name(s1), location1);
1030 s2 = talloc_array_ptrtype(top, s2, 10);location2 = __location__;
1032 if (talloc_get_size(s2) != (sizeof(struct struct1) * 10)) {
1033 printf("failure: ptrtype [\n"
1034 "talloc_array_ptrtype() allocated the wrong size "
1035 "%lu (should be %lu)\n]\n",
1036 (unsigned long)talloc_get_size(s2),
1037 (unsigned long)(sizeof(struct struct1)*10));
1041 if (strcmp(location2, talloc_get_name(s2)) != 0) {
1042 printf("failure: ptrtype [\n"
1043 "talloc_array_ptrtype() sets the wrong name '%s' (should be '%s')\n]\n",
1044 talloc_get_name(s2), location2);
1048 s3 = talloc_array_ptrtype(top, s3, 10);location3 = __location__;
1050 if (talloc_get_size(s3) != (sizeof(struct struct1 *) * 10)) {
1051 printf("failure: ptrtype [\n"
1052 "talloc_array_ptrtype() allocated the wrong size "
1053 "%lu (should be %lu)\n]\n",
1054 (unsigned long)talloc_get_size(s3),
1055 (unsigned long)(sizeof(struct struct1 *)*10));
1059 torture_assert_str_equal("ptrtype", location3, talloc_get_name(s3),
1060 "talloc_array_ptrtype() sets the wrong name");
1062 s4 = talloc_array_ptrtype(top, s4, 10);location4 = __location__;
1064 if (talloc_get_size(s4) != (sizeof(struct struct1 **) * 10)) {
1065 printf("failure: ptrtype [\n"
1066 "talloc_array_ptrtype() allocated the wrong size "
1067 "%lu (should be %lu)\n]\n",
1068 (unsigned long)talloc_get_size(s4),
1069 (unsigned long)(sizeof(struct struct1 **)*10));
1073 torture_assert_str_equal("ptrtype", location4, talloc_get_name(s4),
1074 "talloc_array_ptrtype() sets the wrong name");
1078 printf("success: ptrtype\n");
1082 static int _test_talloc_free_in_destructor(void **ptr)
1088 static bool test_talloc_free_in_destructor(void)
1097 printf("test: free_in_destructor\n# TALLOC FREE IN DESTRUCTOR\n");
1099 level0 = talloc_new(NULL);
1100 level1 = talloc_new(level0);
1101 level2 = talloc_new(level1);
1102 level3 = talloc_new(level2);
1103 level4 = talloc_new(level3);
1104 level5 = talloc(level4, void *);
1107 (void)talloc_reference(level0, level3);
1108 (void)talloc_reference(level3, level3);
1109 (void)talloc_reference(level5, level3);
1111 talloc_set_destructor(level5, _test_talloc_free_in_destructor);
1113 talloc_free(level1);
1115 talloc_free(level0);
1117 printf("success: free_in_destructor\n");
1121 static bool test_autofree(void)
1123 #if _SAMBA_BUILD_ < 4
1124 /* autofree test would kill smbtorture */
1126 printf("test: autofree\n# TALLOC AUTOFREE CONTEXT\n");
1128 p = talloc_autofree_context();
1131 p = talloc_autofree_context();
1134 printf("success: autofree\n");
1139 static bool test_pool(void)
1142 void *p1, *p2, *p3, *p4;
1144 pool = talloc_pool(NULL, 1024);
1146 p1 = talloc_size(pool, 80);
1147 p2 = talloc_size(pool, 20);
1148 p3 = talloc_size(p1, 50);
1149 p4 = talloc_size(p3, 1000);
1156 static void test_reset(void)
1158 talloc_disable_null_tracking();
1159 talloc_enable_null_tracking();
1162 struct torture_context;
1163 bool torture_local_talloc(struct torture_context *tctx)
1178 ret &= test_unlink1();
1182 ret &= test_realloc();
1184 ret &= test_realloc_child();
1186 ret &= test_steal();
1190 ret &= test_unref_reparent();
1192 ret &= test_realloc_fn();
1196 ret &= test_lifeless();
1200 ret &= test_free_parent_deny_child();
1202 ret &= test_talloc_ptrtype();
1204 ret &= test_talloc_free_in_destructor();
1210 ret &= test_speed();
1213 ret &= test_autofree();