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 #include "talloc_testsuite.h"
32 static struct timeval
timeval_current(void)
35 gettimeofday(&tv
, NULL
);
39 static double timeval_elapsed(struct timeval
*tv
)
41 struct timeval tv2
= timeval_current();
42 return (tv2
.tv_sec
- tv
->tv_sec
) +
43 (tv2
.tv_usec
- tv
->tv_usec
)*1.0e-6;
46 #define torture_assert(test, expr, str) if (!(expr)) { \
47 printf("failure: %s [\n%s: Expression %s failed: %s\n]\n", \
48 test, __location__, #expr, str); \
52 #define torture_assert_str_equal(test, arg1, arg2, desc) \
53 if (arg1 == NULL && arg2 == NULL) { \
54 } else if (strcmp(arg1, arg2)) { \
55 printf("failure: %s [\n%s: Expected %s, got %s: %s\n]\n", \
56 test, __location__, arg1, arg2, desc); \
69 #define CHECK_SIZE(test, ptr, tsize) do { \
70 if (talloc_total_size(ptr) != (tsize)) { \
71 printf("failed: %s [\n%s: wrong '%s' tree size: got %u expected %u\n]\n", \
72 test, __location__, #ptr, \
73 (unsigned)talloc_total_size(ptr), \
75 talloc_report_full(ptr, stdout); \
80 #define CHECK_BLOCKS(test, ptr, tblocks) do { \
81 if (talloc_total_blocks(ptr) != (tblocks)) { \
82 printf("failed: %s [\n%s: wrong '%s' tree blocks: got %u expected %u\n]\n", \
83 test, __location__, #ptr, \
84 (unsigned)talloc_total_blocks(ptr), \
86 talloc_report_full(ptr, stdout); \
91 #define CHECK_PARENT(test, ptr, parent) do { \
92 if (talloc_parent(ptr) != (parent)) { \
93 printf("failed: %s [\n%s: '%s' has wrong parent: got %p expected %p\n]\n", \
94 test, __location__, #ptr, \
97 talloc_report_full(ptr, stdout); \
98 talloc_report_full(parent, stdout); \
99 talloc_report_full(NULL, stdout); \
104 static unsigned int test_abort_count
;
107 static void test_abort_fn(const char *reason
)
109 printf("# test_abort_fn(%s)\n", reason
);
113 static void test_abort_start(void)
115 test_abort_count
= 0;
116 talloc_set_abort_fn(test_abort_fn
);
120 static void test_abort_stop(void)
122 test_abort_count
= 0;
123 talloc_set_abort_fn(NULL
);
126 static void test_log_stdout(const char *message
)
128 fprintf(stdout
, "%s", message
);
134 static bool test_ref1(void)
136 void *root
, *p1
, *p2
, *ref
, *r1
;
138 printf("test: ref1\n# SINGLE REFERENCE FREE\n");
140 root
= talloc_named_const(NULL
, 0, "root");
141 p1
= talloc_named_const(root
, 1, "p1");
142 p2
= talloc_named_const(p1
, 1, "p2");
143 talloc_named_const(p1
, 1, "x1");
144 talloc_named_const(p1
, 2, "x2");
145 talloc_named_const(p1
, 3, "x3");
147 r1
= talloc_named_const(root
, 1, "r1");
148 ref
= talloc_reference(r1
, p2
);
149 talloc_report_full(root
, stderr
);
151 CHECK_BLOCKS("ref1", p1
, 5);
152 CHECK_BLOCKS("ref1", p2
, 1);
153 CHECK_BLOCKS("ref1", r1
, 2);
155 fprintf(stderr
, "Freeing p2\n");
156 talloc_unlink(r1
, p2
);
157 talloc_report_full(root
, stderr
);
159 CHECK_BLOCKS("ref1", p1
, 5);
160 CHECK_BLOCKS("ref1", p2
, 1);
161 CHECK_BLOCKS("ref1", r1
, 1);
163 fprintf(stderr
, "Freeing p1\n");
165 talloc_report_full(root
, stderr
);
167 CHECK_BLOCKS("ref1", r1
, 1);
169 fprintf(stderr
, "Freeing r1\n");
171 talloc_report_full(NULL
, stderr
);
173 fprintf(stderr
, "Testing NULL\n");
174 if (talloc_reference(root
, NULL
)) {
178 CHECK_BLOCKS("ref1", root
, 1);
180 CHECK_SIZE("ref1", root
, 0);
183 printf("success: ref1\n");
190 static bool test_ref2(void)
192 void *root
, *p1
, *p2
, *ref
, *r1
;
194 printf("test: ref2\n# DOUBLE REFERENCE FREE\n");
195 root
= talloc_named_const(NULL
, 0, "root");
196 p1
= talloc_named_const(root
, 1, "p1");
197 talloc_named_const(p1
, 1, "x1");
198 talloc_named_const(p1
, 1, "x2");
199 talloc_named_const(p1
, 1, "x3");
200 p2
= talloc_named_const(p1
, 1, "p2");
202 r1
= talloc_named_const(root
, 1, "r1");
203 ref
= talloc_reference(r1
, p2
);
204 talloc_report_full(root
, stderr
);
206 CHECK_BLOCKS("ref2", p1
, 5);
207 CHECK_BLOCKS("ref2", p2
, 1);
208 CHECK_BLOCKS("ref2", r1
, 2);
210 fprintf(stderr
, "Freeing ref\n");
211 talloc_unlink(r1
, ref
);
212 talloc_report_full(root
, stderr
);
214 CHECK_BLOCKS("ref2", p1
, 5);
215 CHECK_BLOCKS("ref2", p2
, 1);
216 CHECK_BLOCKS("ref2", r1
, 1);
218 fprintf(stderr
, "Freeing p2\n");
220 talloc_report_full(root
, stderr
);
222 CHECK_BLOCKS("ref2", p1
, 4);
223 CHECK_BLOCKS("ref2", r1
, 1);
225 fprintf(stderr
, "Freeing p1\n");
227 talloc_report_full(root
, stderr
);
229 CHECK_BLOCKS("ref2", r1
, 1);
231 fprintf(stderr
, "Freeing r1\n");
233 talloc_report_full(root
, stderr
);
235 CHECK_SIZE("ref2", root
, 0);
238 printf("success: ref2\n");
245 static bool test_ref3(void)
247 void *root
, *p1
, *p2
, *ref
, *r1
;
249 printf("test: ref3\n# PARENT REFERENCE FREE\n");
251 root
= talloc_named_const(NULL
, 0, "root");
252 p1
= talloc_named_const(root
, 1, "p1");
253 p2
= talloc_named_const(root
, 1, "p2");
254 r1
= talloc_named_const(p1
, 1, "r1");
255 ref
= talloc_reference(p2
, r1
);
256 talloc_report_full(root
, stderr
);
258 CHECK_BLOCKS("ref3", p1
, 2);
259 CHECK_BLOCKS("ref3", p2
, 2);
260 CHECK_BLOCKS("ref3", r1
, 1);
262 fprintf(stderr
, "Freeing p1\n");
264 talloc_report_full(root
, stderr
);
266 CHECK_BLOCKS("ref3", p2
, 2);
267 CHECK_BLOCKS("ref3", r1
, 1);
269 fprintf(stderr
, "Freeing p2\n");
271 talloc_report_full(root
, stderr
);
273 CHECK_SIZE("ref3", root
, 0);
277 printf("success: ref3\n");
284 static bool test_ref4(void)
286 void *root
, *p1
, *p2
, *ref
, *r1
;
288 printf("test: ref4\n# REFERRER REFERENCE FREE\n");
290 root
= talloc_named_const(NULL
, 0, "root");
291 p1
= talloc_named_const(root
, 1, "p1");
292 talloc_named_const(p1
, 1, "x1");
293 talloc_named_const(p1
, 1, "x2");
294 talloc_named_const(p1
, 1, "x3");
295 p2
= talloc_named_const(p1
, 1, "p2");
297 r1
= talloc_named_const(root
, 1, "r1");
298 ref
= talloc_reference(r1
, p2
);
299 talloc_report_full(root
, stderr
);
301 CHECK_BLOCKS("ref4", p1
, 5);
302 CHECK_BLOCKS("ref4", p2
, 1);
303 CHECK_BLOCKS("ref4", r1
, 2);
305 fprintf(stderr
, "Freeing r1\n");
307 talloc_report_full(root
, stderr
);
309 CHECK_BLOCKS("ref4", p1
, 5);
310 CHECK_BLOCKS("ref4", p2
, 1);
312 fprintf(stderr
, "Freeing p2\n");
314 talloc_report_full(root
, stderr
);
316 CHECK_BLOCKS("ref4", p1
, 4);
318 fprintf(stderr
, "Freeing p1\n");
320 talloc_report_full(root
, stderr
);
322 CHECK_SIZE("ref4", root
, 0);
326 printf("success: ref4\n");
334 static bool test_unlink1(void)
336 void *root
, *p1
, *p2
, *ref
, *r1
;
338 printf("test: unlink\n# UNLINK\n");
340 root
= talloc_named_const(NULL
, 0, "root");
341 p1
= talloc_named_const(root
, 1, "p1");
342 talloc_named_const(p1
, 1, "x1");
343 talloc_named_const(p1
, 1, "x2");
344 talloc_named_const(p1
, 1, "x3");
345 p2
= talloc_named_const(p1
, 1, "p2");
347 r1
= talloc_named_const(p1
, 1, "r1");
348 ref
= talloc_reference(r1
, p2
);
349 talloc_report_full(root
, stderr
);
351 CHECK_BLOCKS("unlink", p1
, 7);
352 CHECK_BLOCKS("unlink", p2
, 1);
353 CHECK_BLOCKS("unlink", r1
, 2);
355 fprintf(stderr
, "Unreferencing r1\n");
356 talloc_unlink(r1
, p2
);
357 talloc_report_full(root
, stderr
);
359 CHECK_BLOCKS("unlink", p1
, 6);
360 CHECK_BLOCKS("unlink", p2
, 1);
361 CHECK_BLOCKS("unlink", r1
, 1);
363 fprintf(stderr
, "Freeing p1\n");
365 talloc_report_full(root
, stderr
);
367 CHECK_SIZE("unlink", root
, 0);
371 printf("success: unlink\n");
375 static int fail_destructor(void *ptr
)
381 miscellaneous tests to try to get a higher test coverage percentage
383 static bool test_misc(void)
390 printf("test: misc\n# MISCELLANEOUS\n");
392 root
= talloc_new(NULL
);
394 p1
= talloc_size(root
, 0x7fffffff);
395 torture_assert("misc", !p1
, "failed: large talloc allowed\n");
397 p1
= talloc_strdup(root
, "foo");
398 talloc_increase_ref_count(p1
);
399 talloc_increase_ref_count(p1
);
400 talloc_increase_ref_count(p1
);
401 CHECK_BLOCKS("misc", p1
, 1);
402 CHECK_BLOCKS("misc", root
, 2);
403 talloc_unlink(NULL
, p1
);
404 CHECK_BLOCKS("misc", p1
, 1);
405 CHECK_BLOCKS("misc", root
, 2);
406 talloc_unlink(NULL
, p1
);
407 CHECK_BLOCKS("misc", p1
, 1);
408 CHECK_BLOCKS("misc", root
, 2);
409 p2
= talloc_strdup(p1
, "foo");
410 torture_assert("misc", talloc_unlink(root
, p2
) == -1,
411 "failed: talloc_unlink() of non-reference context should return -1\n");
412 torture_assert("misc", talloc_unlink(p1
, p2
) == 0,
413 "failed: talloc_unlink() of parent should succeed\n");
414 talloc_unlink(NULL
, p1
);
415 CHECK_BLOCKS("misc", p1
, 1);
416 CHECK_BLOCKS("misc", root
, 2);
418 name
= talloc_set_name(p1
, "my name is %s", "foo");
419 torture_assert_str_equal("misc", talloc_get_name(p1
), "my name is foo",
420 "failed: wrong name after talloc_set_name(my name is foo)");
421 CHECK_BLOCKS("misc", p1
, 2);
422 CHECK_BLOCKS("misc", root
, 3);
424 talloc_set_name_const(p1
, NULL
);
425 torture_assert_str_equal ("misc", talloc_get_name(p1
), "UNNAMED",
426 "failed: wrong name after talloc_set_name(NULL)");
427 CHECK_BLOCKS("misc", p1
, 2);
428 CHECK_BLOCKS("misc", root
, 3);
430 torture_assert("misc", talloc_free(NULL
) == -1,
431 "talloc_free(NULL) should give -1\n");
433 talloc_set_destructor(p1
, fail_destructor
);
434 torture_assert("misc", talloc_free(p1
) == -1,
435 "Failed destructor should cause talloc_free to fail\n");
436 talloc_set_destructor(p1
, NULL
);
438 talloc_report(root
, stderr
);
441 p2
= (char *)talloc_zero_size(p1
, 20);
442 torture_assert("misc", p2
[19] == 0, "Failed to give zero memory\n");
445 torture_assert("misc", talloc_strdup(root
, NULL
) == NULL
,
446 "failed: strdup on NULL should give NULL\n");
448 p2
= talloc_strndup(p1
, "foo", 2);
449 torture_assert("misc", strcmp("fo", p2
) == 0,
450 "strndup doesn't work\n");
451 p2
= talloc_asprintf_append_buffer(p2
, "o%c", 'd');
452 torture_assert("misc", strcmp("food", p2
) == 0,
453 "talloc_asprintf_append_buffer doesn't work\n");
454 CHECK_BLOCKS("misc", p2
, 1);
455 CHECK_BLOCKS("misc", p1
, 3);
457 p2
= talloc_asprintf_append_buffer(NULL
, "hello %s", "world");
458 torture_assert("misc", strcmp("hello world", p2
) == 0,
459 "talloc_asprintf_append_buffer doesn't work\n");
460 CHECK_BLOCKS("misc", p2
, 1);
461 CHECK_BLOCKS("misc", p1
, 3);
464 d
= talloc_array(p1
, double, 0x20000000);
465 torture_assert("misc", !d
, "failed: integer overflow not detected\n");
467 d
= talloc_realloc(p1
, d
, double, 0x20000000);
468 torture_assert("misc", !d
, "failed: integer overflow not detected\n");
471 CHECK_BLOCKS("misc", root
, 1);
473 p1
= talloc_named(root
, 100, "%d bytes", 100);
474 CHECK_BLOCKS("misc", p1
, 2);
475 CHECK_BLOCKS("misc", root
, 3);
476 talloc_unlink(root
, p1
);
478 p1
= talloc_init("%d bytes", 200);
479 p2
= talloc_asprintf(p1
, "my test '%s'", "string");
480 torture_assert_str_equal("misc", p2
, "my test 'string'",
481 "failed: talloc_asprintf(\"my test '%%s'\", \"string\") gave: \"%s\"");
482 CHECK_BLOCKS("misc", p1
, 3);
483 CHECK_SIZE("misc", p2
, 17);
484 CHECK_BLOCKS("misc", root
, 1);
485 talloc_unlink(NULL
, p1
);
487 p1
= talloc_named_const(root
, 10, "p1");
488 p2
= (char *)talloc_named_const(root
, 20, "p2");
489 (void)talloc_reference(p1
, p2
);
490 talloc_report_full(root
, stderr
);
491 talloc_unlink(root
, p2
);
492 talloc_report_full(root
, stderr
);
493 CHECK_BLOCKS("misc", p2
, 1);
494 CHECK_BLOCKS("misc", p1
, 2);
495 CHECK_BLOCKS("misc", root
, 3);
496 talloc_unlink(p1
, p2
);
497 talloc_unlink(root
, p1
);
499 p1
= talloc_named_const(root
, 10, "p1");
500 p2
= (char *)talloc_named_const(root
, 20, "p2");
501 (void)talloc_reference(NULL
, p2
);
502 talloc_report_full(root
, stderr
);
503 talloc_unlink(root
, p2
);
504 talloc_report_full(root
, stderr
);
505 CHECK_BLOCKS("misc", p2
, 1);
506 CHECK_BLOCKS("misc", p1
, 1);
507 CHECK_BLOCKS("misc", root
, 2);
508 talloc_unlink(NULL
, p2
);
509 talloc_unlink(root
, p1
);
511 /* Test that talloc_unlink is a no-op */
513 torture_assert("misc", talloc_unlink(root
, NULL
) == -1,
514 "failed: talloc_unlink(root, NULL) == -1\n");
516 talloc_report(root
, stderr
);
517 talloc_report(NULL
, stderr
);
519 CHECK_SIZE("misc", root
, 0);
523 CHECK_SIZE("misc", NULL
, 0);
525 talloc_enable_null_tracking_no_autofree();
526 talloc_enable_leak_report();
527 talloc_enable_leak_report_full();
529 printf("success: misc\n");
538 static bool test_realloc(void)
540 void *root
, *p1
, *p2
;
542 printf("test: realloc\n# REALLOC\n");
544 root
= talloc_new(NULL
);
546 p1
= talloc_size(root
, 10);
547 CHECK_SIZE("realloc", p1
, 10);
549 p1
= talloc_realloc_size(NULL
, p1
, 20);
550 CHECK_SIZE("realloc", p1
, 20);
554 p2
= talloc_realloc_size(p1
, NULL
, 30);
558 p2
= talloc_realloc_size(p1
, p2
, 40);
560 CHECK_SIZE("realloc", p2
, 40);
561 CHECK_SIZE("realloc", root
, 60);
562 CHECK_BLOCKS("realloc", p1
, 4);
564 p1
= talloc_realloc_size(NULL
, p1
, 20);
565 CHECK_SIZE("realloc", p1
, 60);
567 talloc_increase_ref_count(p2
);
568 torture_assert("realloc", talloc_realloc_size(NULL
, p2
, 5) == NULL
,
569 "failed: talloc_realloc() on a referenced pointer should fail\n");
570 CHECK_BLOCKS("realloc", p1
, 4);
572 talloc_realloc_size(NULL
, p2
, 0);
573 talloc_realloc_size(NULL
, p2
, 0);
574 CHECK_BLOCKS("realloc", p1
, 4);
575 talloc_realloc_size(p1
, p2
, 0);
576 CHECK_BLOCKS("realloc", p1
, 3);
578 torture_assert("realloc", talloc_realloc_size(NULL
, p1
, 0x7fffffff) == NULL
,
579 "failed: oversize talloc should fail\n");
581 talloc_realloc_size(NULL
, p1
, 0);
582 CHECK_BLOCKS("realloc", root
, 4);
583 talloc_realloc_size(root
, p1
, 0);
584 CHECK_BLOCKS("realloc", root
, 1);
586 CHECK_SIZE("realloc", root
, 0);
590 printf("success: realloc\n");
596 test realloc with a child
598 static bool test_realloc_child(void)
606 struct el2
**list
, **list2
, **list3
;
609 printf("test: REALLOC WITH CHILD\n");
611 root
= talloc_new(NULL
);
613 el1
= talloc(root
, struct el1
);
614 el1
->list
= talloc(el1
, struct el2
*);
615 el1
->list
[0] = talloc(el1
->list
, struct el2
);
616 el1
->list
[0]->name
= talloc_strdup(el1
->list
[0], "testing");
618 el1
->list2
= talloc(el1
, struct el2
*);
619 el1
->list2
[0] = talloc(el1
->list2
, struct el2
);
620 el1
->list2
[0]->name
= talloc_strdup(el1
->list2
[0], "testing2");
622 el1
->list3
= talloc(el1
, struct el2
*);
623 el1
->list3
[0] = talloc(el1
->list3
, struct el2
);
624 el1
->list3
[0]->name
= talloc_strdup(el1
->list3
[0], "testing2");
626 el2
= talloc(el1
->list
, struct el2
);
627 el2
= talloc(el1
->list2
, struct el2
);
628 el2
= talloc(el1
->list3
, struct el2
);
630 el1
->list
= talloc_realloc(el1
, el1
->list
, struct el2
*, 100);
631 el1
->list2
= talloc_realloc(el1
, el1
->list2
, struct el2
*, 200);
632 el1
->list3
= talloc_realloc(el1
, el1
->list3
, struct el2
*, 300);
636 printf("success: REALLOC WITH CHILD\n");
643 static bool test_type(void)
654 printf("test: type\n# talloc type checking\n");
656 root
= talloc_new(NULL
);
658 el1
= talloc(root
, struct el1
);
662 torture_assert("type", talloc_get_type(el1
, struct el1
) == el1
,
663 "type check failed on el1\n");
664 torture_assert("type", talloc_get_type(el1
, struct el2
) == NULL
,
665 "type check failed on el1 with el2\n");
666 talloc_set_type(el1
, struct el2
);
667 torture_assert("type", talloc_get_type(el1
, struct el2
) == (struct el2
*)el1
,
668 "type set failed on el1 with el2\n");
672 printf("success: type\n");
679 static bool test_steal(void)
681 void *root
, *p1
, *p2
;
683 printf("test: steal\n# STEAL\n");
685 root
= talloc_new(NULL
);
687 p1
= talloc_array(root
, char, 10);
688 CHECK_SIZE("steal", p1
, 10);
690 p2
= talloc_realloc(root
, NULL
, char, 20);
691 CHECK_SIZE("steal", p1
, 10);
692 CHECK_SIZE("steal", root
, 30);
694 torture_assert("steal", talloc_steal(p1
, NULL
) == NULL
,
695 "failed: stealing NULL should give NULL\n");
697 torture_assert("steal", talloc_steal(p1
, p1
) == p1
,
698 "failed: stealing to ourselves is a nop\n");
699 CHECK_BLOCKS("steal", root
, 3);
700 CHECK_SIZE("steal", root
, 30);
702 talloc_steal(NULL
, p1
);
703 talloc_steal(NULL
, p2
);
704 CHECK_BLOCKS("steal", root
, 1);
705 CHECK_SIZE("steal", root
, 0);
708 talloc_steal(root
, p2
);
709 CHECK_BLOCKS("steal", root
, 2);
710 CHECK_SIZE("steal", root
, 20);
714 CHECK_BLOCKS("steal", root
, 1);
715 CHECK_SIZE("steal", root
, 0);
719 p1
= talloc_size(NULL
, 3);
720 talloc_report_full(NULL
, stderr
);
721 CHECK_SIZE("steal", NULL
, 3);
724 printf("success: steal\n");
731 static bool test_move(void)
739 printf("test: move\n# MOVE\n");
741 root
= talloc_new(NULL
);
743 t1
= talloc(root
, struct t_move
);
744 t2
= talloc(root
, struct t_move
);
745 t1
->p
= talloc_strdup(t1
, "foo");
746 t1
->x
= talloc(t1
, int);
749 t2
->p
= talloc_move(t2
, &t1
->p
);
750 t2
->x
= talloc_move(t2
, &t1
->x
);
751 torture_assert("move", t1
->p
== NULL
&& t1
->x
== NULL
&&
752 strcmp(t2
->p
, "foo") == 0 && *t2
->x
== 42,
753 "talloc move failed");
757 printf("success: move\n");
763 test talloc_realloc_fn
765 static bool test_realloc_fn(void)
769 printf("test: realloc_fn\n# talloc_realloc_fn\n");
771 root
= talloc_new(NULL
);
773 p1
= talloc_realloc_fn(root
, NULL
, 10);
774 CHECK_BLOCKS("realloc_fn", root
, 2);
775 CHECK_SIZE("realloc_fn", root
, 10);
776 p1
= talloc_realloc_fn(root
, p1
, 20);
777 CHECK_BLOCKS("realloc_fn", root
, 2);
778 CHECK_SIZE("realloc_fn", root
, 20);
779 p1
= talloc_realloc_fn(root
, p1
, 0);
780 CHECK_BLOCKS("realloc_fn", root
, 1);
781 CHECK_SIZE("realloc_fn", root
, 0);
785 printf("success: realloc_fn\n");
790 static bool test_unref_reparent(void)
792 void *root
, *p1
, *p2
, *c1
;
794 printf("test: unref_reparent\n# UNREFERENCE AFTER PARENT FREED\n");
796 root
= talloc_named_const(NULL
, 0, "root");
797 p1
= talloc_named_const(root
, 1, "orig parent");
798 p2
= talloc_named_const(root
, 1, "parent by reference");
800 c1
= talloc_named_const(p1
, 1, "child");
801 talloc_reference(p2
, c1
);
803 CHECK_PARENT("unref_reparent", c1
, p1
);
807 CHECK_PARENT("unref_reparent", c1
, p2
);
809 talloc_unlink(p2
, c1
);
811 CHECK_SIZE("unref_reparent", root
, 1);
816 printf("success: unref_reparent\n");
821 measure the speed of talloc versus malloc
823 static bool test_speed(void)
825 void *ctx
= talloc_new(NULL
);
827 const int loop
= 1000;
831 printf("test: speed\n# TALLOC VS MALLOC SPEED\n");
833 tv
= timeval_current();
837 for (i
=0;i
<loop
;i
++) {
838 p1
= talloc_size(ctx
, loop
% 100);
839 p2
= talloc_strdup(p1
, "foo bar");
840 p3
= talloc_size(p1
, 300);
844 } while (timeval_elapsed(&tv
) < 5.0);
846 fprintf(stderr
, "talloc: %.0f ops/sec\n", count
/timeval_elapsed(&tv
));
850 ctx
= talloc_pool(NULL
, 1024);
852 tv
= timeval_current();
856 for (i
=0;i
<loop
;i
++) {
857 p1
= talloc_size(ctx
, loop
% 100);
858 p2
= talloc_strdup(p1
, "foo bar");
859 p3
= talloc_size(p1
, 300);
860 talloc_free_children(ctx
);
863 } while (timeval_elapsed(&tv
) < 5.0);
867 fprintf(stderr
, "talloc_pool: %.0f ops/sec\n", count
/timeval_elapsed(&tv
));
869 tv
= timeval_current();
873 for (i
=0;i
<loop
;i
++) {
874 p1
= malloc(loop
% 100);
875 p2
= strdup("foo bar");
882 } while (timeval_elapsed(&tv
) < 5.0);
883 fprintf(stderr
, "malloc: %.0f ops/sec\n", count
/timeval_elapsed(&tv
));
885 printf("success: speed\n");
890 static bool test_lifeless(void)
892 void *top
= talloc_new(NULL
);
893 char *parent
, *child
;
894 void *child_owner
= talloc_new(NULL
);
896 printf("test: lifeless\n# TALLOC_UNLINK LOOP\n");
898 parent
= talloc_strdup(top
, "parent");
899 child
= talloc_strdup(parent
, "child");
900 (void)talloc_reference(child
, parent
);
901 (void)talloc_reference(child_owner
, child
);
902 talloc_report_full(top
, stderr
);
903 talloc_unlink(top
, parent
);
904 talloc_unlink(top
, child
);
905 talloc_report_full(top
, stderr
);
907 talloc_free(child_owner
);
910 printf("success: lifeless\n");
914 static int loop_destructor_count
;
916 static int test_loop_destructor(char *ptr
)
918 loop_destructor_count
++;
922 static bool test_loop(void)
924 void *top
= talloc_new(NULL
);
930 printf("test: loop\n# TALLOC LOOP DESTRUCTION\n");
932 parent
= talloc_strdup(top
, "parent");
933 req1
= talloc(parent
, struct req1
);
934 req1
->req2
= talloc_strdup(req1
, "req2");
935 talloc_set_destructor(req1
->req2
, test_loop_destructor
);
936 req1
->req3
= talloc_strdup(req1
, "req3");
937 (void)talloc_reference(req1
->req3
, req1
);
938 talloc_report_full(top
, stderr
);
940 talloc_report_full(top
, stderr
);
941 talloc_report_full(NULL
, stderr
);
944 torture_assert("loop", loop_destructor_count
== 1,
945 "FAILED TO FIRE LOOP DESTRUCTOR\n");
946 loop_destructor_count
= 0;
948 printf("success: loop\n");
952 static int fail_destructor_str(char *ptr
)
957 static bool test_free_parent_deny_child(void)
959 void *top
= talloc_new(NULL
);
964 printf("test: free_parent_deny_child\n# TALLOC FREE PARENT DENY CHILD\n");
966 level1
= talloc_strdup(top
, "level1");
967 level2
= talloc_strdup(level1
, "level2");
968 level3
= talloc_strdup(level2
, "level3");
970 talloc_set_destructor(level3
, fail_destructor_str
);
972 talloc_set_destructor(level3
, NULL
);
974 CHECK_PARENT("free_parent_deny_child", level3
, top
);
978 printf("success: free_parent_deny_child\n");
982 static bool test_talloc_ptrtype(void)
984 void *top
= talloc_new(NULL
);
988 } *s1
, *s2
, **s3
, ***s4
;
989 const char *location1
;
990 const char *location2
;
991 const char *location3
;
992 const char *location4
;
994 printf("test: ptrtype\n# TALLOC PTRTYPE\n");
996 s1
= talloc_ptrtype(top
, s1
);location1
= __location__
;
998 if (talloc_get_size(s1
) != sizeof(struct struct1
)) {
999 printf("failure: ptrtype [\n"
1000 "talloc_ptrtype() allocated the wrong size %lu (should be %lu)\n"
1001 "]\n", (unsigned long)talloc_get_size(s1
),
1002 (unsigned long)sizeof(struct struct1
));
1006 if (strcmp(location1
, talloc_get_name(s1
)) != 0) {
1007 printf("failure: ptrtype [\n"
1008 "talloc_ptrtype() sets the wrong name '%s' (should be '%s')\n]\n",
1009 talloc_get_name(s1
), location1
);
1013 s2
= talloc_array_ptrtype(top
, s2
, 10);location2
= __location__
;
1015 if (talloc_get_size(s2
) != (sizeof(struct struct1
) * 10)) {
1016 printf("failure: ptrtype [\n"
1017 "talloc_array_ptrtype() allocated the wrong size "
1018 "%lu (should be %lu)\n]\n",
1019 (unsigned long)talloc_get_size(s2
),
1020 (unsigned long)(sizeof(struct struct1
)*10));
1024 if (strcmp(location2
, talloc_get_name(s2
)) != 0) {
1025 printf("failure: ptrtype [\n"
1026 "talloc_array_ptrtype() sets the wrong name '%s' (should be '%s')\n]\n",
1027 talloc_get_name(s2
), location2
);
1031 s3
= talloc_array_ptrtype(top
, s3
, 10);location3
= __location__
;
1033 if (talloc_get_size(s3
) != (sizeof(struct struct1
*) * 10)) {
1034 printf("failure: ptrtype [\n"
1035 "talloc_array_ptrtype() allocated the wrong size "
1036 "%lu (should be %lu)\n]\n",
1037 (unsigned long)talloc_get_size(s3
),
1038 (unsigned long)(sizeof(struct struct1
*)*10));
1042 torture_assert_str_equal("ptrtype", location3
, talloc_get_name(s3
),
1043 "talloc_array_ptrtype() sets the wrong name");
1045 s4
= talloc_array_ptrtype(top
, s4
, 10);location4
= __location__
;
1047 if (talloc_get_size(s4
) != (sizeof(struct struct1
**) * 10)) {
1048 printf("failure: ptrtype [\n"
1049 "talloc_array_ptrtype() allocated the wrong size "
1050 "%lu (should be %lu)\n]\n",
1051 (unsigned long)talloc_get_size(s4
),
1052 (unsigned long)(sizeof(struct struct1
**)*10));
1056 torture_assert_str_equal("ptrtype", location4
, talloc_get_name(s4
),
1057 "talloc_array_ptrtype() sets the wrong name");
1061 printf("success: ptrtype\n");
1065 static int _test_talloc_free_in_destructor(void **ptr
)
1071 static bool test_talloc_free_in_destructor(void)
1080 printf("test: free_in_destructor\n# TALLOC FREE IN DESTRUCTOR\n");
1082 level0
= talloc_new(NULL
);
1083 level1
= talloc_new(level0
);
1084 level2
= talloc_new(level1
);
1085 level3
= talloc_new(level2
);
1086 level4
= talloc_new(level3
);
1087 level5
= talloc(level4
, void *);
1090 (void)talloc_reference(level0
, level3
);
1091 (void)talloc_reference(level3
, level3
);
1092 (void)talloc_reference(level5
, level3
);
1094 talloc_set_destructor(level5
, _test_talloc_free_in_destructor
);
1096 talloc_free(level1
);
1098 talloc_free(level0
);
1100 printf("success: free_in_destructor\n");
1104 static bool test_autofree(void)
1106 #if _SAMBA_BUILD_ < 4
1107 /* autofree test would kill smbtorture */
1109 printf("test: autofree\n# TALLOC AUTOFREE CONTEXT\n");
1111 p
= talloc_autofree_context();
1114 p
= talloc_autofree_context();
1117 printf("success: autofree\n");
1122 static bool test_pool(void)
1125 void *p1
, *p2
, *p3
, *p4
;
1127 pool
= talloc_pool(NULL
, 1024);
1129 p1
= talloc_size(pool
, 80);
1130 p2
= talloc_size(pool
, 20);
1131 p3
= talloc_size(p1
, 50);
1132 p4
= talloc_size(p3
, 1000);
1140 static bool test_free_ref_null_context(void)
1145 talloc_disable_null_tracking();
1146 p1
= talloc_new(NULL
);
1147 p2
= talloc_new(NULL
);
1149 p3
= talloc_reference(p2
, p1
);
1150 torture_assert("reference", p3
== p1
, "failed: reference on null");
1152 ret
= talloc_free(p1
);
1153 torture_assert("ref free with null parent", ret
== 0, "failed: free with null parent");
1156 talloc_enable_null_tracking_no_autofree();
1157 p1
= talloc_new(NULL
);
1158 p2
= talloc_new(NULL
);
1160 p3
= talloc_reference(p2
, p1
);
1161 torture_assert("reference", p3
== p1
, "failed: reference on null");
1163 ret
= talloc_free(p1
);
1164 torture_assert("ref free with null tracked parent", ret
== 0, "failed: free with null parent");
1170 static bool test_rusty(void)
1175 talloc_enable_null_tracking();
1176 root
= talloc_new(NULL
);
1177 p1
= talloc_strdup(root
, "foo");
1178 talloc_increase_ref_count(p1
);
1179 talloc_report_full(root
, stdout
);
1185 static void test_reset(void)
1187 talloc_set_log_fn(test_log_stdout
);
1189 talloc_disable_null_tracking();
1190 talloc_enable_null_tracking_no_autofree();
1193 bool torture_local_talloc(struct torture_context
*tctx
)
1208 ret
&= test_unlink1();
1212 ret
&= test_realloc();
1214 ret
&= test_realloc_child();
1216 ret
&= test_steal();
1220 ret
&= test_unref_reparent();
1222 ret
&= test_realloc_fn();
1226 ret
&= test_lifeless();
1230 ret
&= test_free_parent_deny_child();
1232 ret
&= test_talloc_ptrtype();
1234 ret
&= test_talloc_free_in_destructor();
1238 ret
&= test_free_ref_null_context();
1240 ret
&= test_rusty();
1244 ret
&= test_speed();
1247 ret
&= test_autofree();