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); \
60 #define CHECK_SIZE(test, ptr, tsize) do { \
61 if (talloc_total_size(ptr) != (tsize)) { \
62 printf("failed: %s [\n%s: wrong '%s' tree size: got %u expected %u\n]\n", \
63 test, __location__, #ptr, \
64 (unsigned)talloc_total_size(ptr), \
66 talloc_report_full(ptr, stdout); \
71 #define CHECK_BLOCKS(test, ptr, tblocks) do { \
72 if (talloc_total_blocks(ptr) != (tblocks)) { \
73 printf("failed: %s [\n%s: wrong '%s' tree blocks: got %u expected %u\n]\n", \
74 test, __location__, #ptr, \
75 (unsigned)talloc_total_blocks(ptr), \
77 talloc_report_full(ptr, stdout); \
82 #define CHECK_PARENT(test, ptr, parent) do { \
83 if (talloc_parent(ptr) != (parent)) { \
84 printf("failed: %s [\n%s: '%s' has wrong parent: got %p expected %p\n]\n", \
85 test, __location__, #ptr, \
88 talloc_report_full(ptr, stdout); \
89 talloc_report_full(parent, stdout); \
90 talloc_report_full(NULL, stdout); \
95 static unsigned int test_abort_count
;
98 static void test_abort_fn(const char *reason
)
100 printf("# test_abort_fn(%s)\n", reason
);
104 static void test_abort_start(void)
106 test_abort_count
= 0;
107 talloc_set_abort_fn(test_abort_fn
);
111 static void test_abort_stop(void)
113 test_abort_count
= 0;
114 talloc_set_abort_fn(NULL
);
117 static void test_log_stdout(const char *message
)
119 fprintf(stdout
, "%s", message
);
125 static bool test_ref1(void)
127 void *root
, *p1
, *p2
, *ref
, *r1
;
129 printf("test: ref1\n# SINGLE REFERENCE FREE\n");
131 root
= talloc_named_const(NULL
, 0, "root");
132 p1
= talloc_named_const(root
, 1, "p1");
133 p2
= talloc_named_const(p1
, 1, "p2");
134 talloc_named_const(p1
, 1, "x1");
135 talloc_named_const(p1
, 2, "x2");
136 talloc_named_const(p1
, 3, "x3");
138 r1
= talloc_named_const(root
, 1, "r1");
139 ref
= talloc_reference(r1
, p2
);
140 talloc_report_full(root
, stderr
);
142 CHECK_BLOCKS("ref1", p1
, 5);
143 CHECK_BLOCKS("ref1", p2
, 1);
144 CHECK_BLOCKS("ref1", r1
, 2);
146 fprintf(stderr
, "Freeing p2\n");
147 talloc_unlink(r1
, p2
);
148 talloc_report_full(root
, stderr
);
150 CHECK_BLOCKS("ref1", p1
, 5);
151 CHECK_BLOCKS("ref1", p2
, 1);
152 CHECK_BLOCKS("ref1", r1
, 1);
154 fprintf(stderr
, "Freeing p1\n");
156 talloc_report_full(root
, stderr
);
158 CHECK_BLOCKS("ref1", r1
, 1);
160 fprintf(stderr
, "Freeing r1\n");
162 talloc_report_full(NULL
, stderr
);
164 fprintf(stderr
, "Testing NULL\n");
165 if (talloc_reference(root
, NULL
)) {
169 CHECK_BLOCKS("ref1", root
, 1);
171 CHECK_SIZE("ref1", root
, 0);
174 printf("success: ref1\n");
181 static bool test_ref2(void)
183 void *root
, *p1
, *p2
, *ref
, *r1
;
185 printf("test: ref2\n# DOUBLE REFERENCE FREE\n");
186 root
= talloc_named_const(NULL
, 0, "root");
187 p1
= talloc_named_const(root
, 1, "p1");
188 talloc_named_const(p1
, 1, "x1");
189 talloc_named_const(p1
, 1, "x2");
190 talloc_named_const(p1
, 1, "x3");
191 p2
= talloc_named_const(p1
, 1, "p2");
193 r1
= talloc_named_const(root
, 1, "r1");
194 ref
= talloc_reference(r1
, p2
);
195 talloc_report_full(root
, stderr
);
197 CHECK_BLOCKS("ref2", p1
, 5);
198 CHECK_BLOCKS("ref2", p2
, 1);
199 CHECK_BLOCKS("ref2", r1
, 2);
201 fprintf(stderr
, "Freeing ref\n");
202 talloc_unlink(r1
, ref
);
203 talloc_report_full(root
, stderr
);
205 CHECK_BLOCKS("ref2", p1
, 5);
206 CHECK_BLOCKS("ref2", p2
, 1);
207 CHECK_BLOCKS("ref2", r1
, 1);
209 fprintf(stderr
, "Freeing p2\n");
211 talloc_report_full(root
, stderr
);
213 CHECK_BLOCKS("ref2", p1
, 4);
214 CHECK_BLOCKS("ref2", r1
, 1);
216 fprintf(stderr
, "Freeing p1\n");
218 talloc_report_full(root
, stderr
);
220 CHECK_BLOCKS("ref2", r1
, 1);
222 fprintf(stderr
, "Freeing r1\n");
224 talloc_report_full(root
, stderr
);
226 CHECK_SIZE("ref2", root
, 0);
229 printf("success: ref2\n");
236 static bool test_ref3(void)
238 void *root
, *p1
, *p2
, *ref
, *r1
;
240 printf("test: ref3\n# PARENT REFERENCE FREE\n");
242 root
= talloc_named_const(NULL
, 0, "root");
243 p1
= talloc_named_const(root
, 1, "p1");
244 p2
= talloc_named_const(root
, 1, "p2");
245 r1
= talloc_named_const(p1
, 1, "r1");
246 ref
= talloc_reference(p2
, r1
);
247 talloc_report_full(root
, stderr
);
249 CHECK_BLOCKS("ref3", p1
, 2);
250 CHECK_BLOCKS("ref3", p2
, 2);
251 CHECK_BLOCKS("ref3", r1
, 1);
253 fprintf(stderr
, "Freeing p1\n");
255 talloc_report_full(root
, stderr
);
257 CHECK_BLOCKS("ref3", p2
, 2);
258 CHECK_BLOCKS("ref3", r1
, 1);
260 fprintf(stderr
, "Freeing p2\n");
262 talloc_report_full(root
, stderr
);
264 CHECK_SIZE("ref3", root
, 0);
268 printf("success: ref3\n");
275 static bool test_ref4(void)
277 void *root
, *p1
, *p2
, *ref
, *r1
;
279 printf("test: ref4\n# REFERRER REFERENCE FREE\n");
281 root
= talloc_named_const(NULL
, 0, "root");
282 p1
= talloc_named_const(root
, 1, "p1");
283 talloc_named_const(p1
, 1, "x1");
284 talloc_named_const(p1
, 1, "x2");
285 talloc_named_const(p1
, 1, "x3");
286 p2
= talloc_named_const(p1
, 1, "p2");
288 r1
= talloc_named_const(root
, 1, "r1");
289 ref
= talloc_reference(r1
, p2
);
290 talloc_report_full(root
, stderr
);
292 CHECK_BLOCKS("ref4", p1
, 5);
293 CHECK_BLOCKS("ref4", p2
, 1);
294 CHECK_BLOCKS("ref4", r1
, 2);
296 fprintf(stderr
, "Freeing r1\n");
298 talloc_report_full(root
, stderr
);
300 CHECK_BLOCKS("ref4", p1
, 5);
301 CHECK_BLOCKS("ref4", p2
, 1);
303 fprintf(stderr
, "Freeing p2\n");
305 talloc_report_full(root
, stderr
);
307 CHECK_BLOCKS("ref4", p1
, 4);
309 fprintf(stderr
, "Freeing p1\n");
311 talloc_report_full(root
, stderr
);
313 CHECK_SIZE("ref4", root
, 0);
317 printf("success: ref4\n");
325 static bool test_unlink1(void)
327 void *root
, *p1
, *p2
, *ref
, *r1
;
329 printf("test: unlink\n# UNLINK\n");
331 root
= talloc_named_const(NULL
, 0, "root");
332 p1
= talloc_named_const(root
, 1, "p1");
333 talloc_named_const(p1
, 1, "x1");
334 talloc_named_const(p1
, 1, "x2");
335 talloc_named_const(p1
, 1, "x3");
336 p2
= talloc_named_const(p1
, 1, "p2");
338 r1
= talloc_named_const(p1
, 1, "r1");
339 ref
= talloc_reference(r1
, p2
);
340 talloc_report_full(root
, stderr
);
342 CHECK_BLOCKS("unlink", p1
, 7);
343 CHECK_BLOCKS("unlink", p2
, 1);
344 CHECK_BLOCKS("unlink", r1
, 2);
346 fprintf(stderr
, "Unreferencing r1\n");
347 talloc_unlink(r1
, p2
);
348 talloc_report_full(root
, stderr
);
350 CHECK_BLOCKS("unlink", p1
, 6);
351 CHECK_BLOCKS("unlink", p2
, 1);
352 CHECK_BLOCKS("unlink", r1
, 1);
354 fprintf(stderr
, "Freeing p1\n");
356 talloc_report_full(root
, stderr
);
358 CHECK_SIZE("unlink", root
, 0);
362 printf("success: unlink\n");
366 static int fail_destructor(void *ptr
)
372 miscellaneous tests to try to get a higher test coverage percentage
374 static bool test_misc(void)
381 printf("test: misc\n# MISCELLANEOUS\n");
383 root
= talloc_new(NULL
);
385 p1
= talloc_size(root
, 0x7fffffff);
386 torture_assert("misc", !p1
, "failed: large talloc allowed\n");
388 p1
= talloc_strdup(root
, "foo");
389 talloc_increase_ref_count(p1
);
390 talloc_increase_ref_count(p1
);
391 talloc_increase_ref_count(p1
);
392 CHECK_BLOCKS("misc", p1
, 1);
393 CHECK_BLOCKS("misc", root
, 2);
394 talloc_unlink(NULL
, p1
);
395 CHECK_BLOCKS("misc", p1
, 1);
396 CHECK_BLOCKS("misc", root
, 2);
397 talloc_unlink(NULL
, p1
);
398 CHECK_BLOCKS("misc", p1
, 1);
399 CHECK_BLOCKS("misc", root
, 2);
400 p2
= talloc_strdup(p1
, "foo");
401 torture_assert("misc", talloc_unlink(root
, p2
) == -1,
402 "failed: talloc_unlink() of non-reference context should return -1\n");
403 torture_assert("misc", talloc_unlink(p1
, p2
) == 0,
404 "failed: talloc_unlink() of parent should succeed\n");
405 talloc_unlink(NULL
, p1
);
406 CHECK_BLOCKS("misc", p1
, 1);
407 CHECK_BLOCKS("misc", root
, 2);
409 name
= talloc_set_name(p1
, "my name is %s", "foo");
410 torture_assert_str_equal("misc", talloc_get_name(p1
), "my name is foo",
411 "failed: wrong name after talloc_set_name(my name is foo)");
412 CHECK_BLOCKS("misc", p1
, 2);
413 CHECK_BLOCKS("misc", root
, 3);
415 talloc_set_name_const(p1
, NULL
);
416 torture_assert_str_equal ("misc", talloc_get_name(p1
), "UNNAMED",
417 "failed: wrong name after talloc_set_name(NULL)");
418 CHECK_BLOCKS("misc", p1
, 2);
419 CHECK_BLOCKS("misc", root
, 3);
421 torture_assert("misc", talloc_free(NULL
) == -1,
422 "talloc_free(NULL) should give -1\n");
424 talloc_set_destructor(p1
, fail_destructor
);
425 torture_assert("misc", talloc_free(p1
) == -1,
426 "Failed destructor should cause talloc_free to fail\n");
427 talloc_set_destructor(p1
, NULL
);
429 talloc_report(root
, stderr
);
432 p2
= (char *)talloc_zero_size(p1
, 20);
433 torture_assert("misc", p2
[19] == 0, "Failed to give zero memory\n");
436 torture_assert("misc", talloc_strdup(root
, NULL
) == NULL
,
437 "failed: strdup on NULL should give NULL\n");
439 p2
= talloc_strndup(p1
, "foo", 2);
440 torture_assert("misc", strcmp("fo", p2
) == 0,
441 "strndup doesn't work\n");
442 p2
= talloc_asprintf_append_buffer(p2
, "o%c", 'd');
443 torture_assert("misc", strcmp("food", p2
) == 0,
444 "talloc_asprintf_append_buffer doesn't work\n");
445 CHECK_BLOCKS("misc", p2
, 1);
446 CHECK_BLOCKS("misc", p1
, 3);
448 p2
= talloc_asprintf_append_buffer(NULL
, "hello %s", "world");
449 torture_assert("misc", strcmp("hello world", p2
) == 0,
450 "talloc_asprintf_append_buffer doesn't work\n");
451 CHECK_BLOCKS("misc", p2
, 1);
452 CHECK_BLOCKS("misc", p1
, 3);
455 d
= talloc_array(p1
, double, 0x20000000);
456 torture_assert("misc", !d
, "failed: integer overflow not detected\n");
458 d
= talloc_realloc(p1
, d
, double, 0x20000000);
459 torture_assert("misc", !d
, "failed: integer overflow not detected\n");
462 CHECK_BLOCKS("misc", root
, 1);
464 p1
= talloc_named(root
, 100, "%d bytes", 100);
465 CHECK_BLOCKS("misc", p1
, 2);
466 CHECK_BLOCKS("misc", root
, 3);
467 talloc_unlink(root
, p1
);
469 p1
= talloc_init("%d bytes", 200);
470 p2
= talloc_asprintf(p1
, "my test '%s'", "string");
471 torture_assert_str_equal("misc", p2
, "my test 'string'",
472 "failed: talloc_asprintf(\"my test '%%s'\", \"string\") gave: \"%s\"");
473 CHECK_BLOCKS("misc", p1
, 3);
474 CHECK_SIZE("misc", p2
, 17);
475 CHECK_BLOCKS("misc", root
, 1);
476 talloc_unlink(NULL
, p1
);
478 p1
= talloc_named_const(root
, 10, "p1");
479 p2
= (char *)talloc_named_const(root
, 20, "p2");
480 (void)talloc_reference(p1
, p2
);
481 talloc_report_full(root
, stderr
);
482 talloc_unlink(root
, p2
);
483 talloc_report_full(root
, stderr
);
484 CHECK_BLOCKS("misc", p2
, 1);
485 CHECK_BLOCKS("misc", p1
, 2);
486 CHECK_BLOCKS("misc", root
, 3);
487 talloc_unlink(p1
, p2
);
488 talloc_unlink(root
, p1
);
490 p1
= talloc_named_const(root
, 10, "p1");
491 p2
= (char *)talloc_named_const(root
, 20, "p2");
492 (void)talloc_reference(NULL
, p2
);
493 talloc_report_full(root
, stderr
);
494 talloc_unlink(root
, p2
);
495 talloc_report_full(root
, stderr
);
496 CHECK_BLOCKS("misc", p2
, 1);
497 CHECK_BLOCKS("misc", p1
, 1);
498 CHECK_BLOCKS("misc", root
, 2);
499 talloc_unlink(NULL
, p2
);
500 talloc_unlink(root
, p1
);
502 /* Test that talloc_unlink is a no-op */
504 torture_assert("misc", talloc_unlink(root
, NULL
) == -1,
505 "failed: talloc_unlink(root, NULL) == -1\n");
507 talloc_report(root
, stderr
);
508 talloc_report(NULL
, stderr
);
510 CHECK_SIZE("misc", root
, 0);
514 CHECK_SIZE("misc", NULL
, 0);
516 talloc_enable_null_tracking_no_autofree();
517 talloc_enable_leak_report();
518 talloc_enable_leak_report_full();
520 printf("success: misc\n");
529 static bool test_realloc(void)
531 void *root
, *p1
, *p2
;
533 printf("test: realloc\n# REALLOC\n");
535 root
= talloc_new(NULL
);
537 p1
= talloc_size(root
, 10);
538 CHECK_SIZE("realloc", p1
, 10);
540 p1
= talloc_realloc_size(NULL
, p1
, 20);
541 CHECK_SIZE("realloc", p1
, 20);
545 p2
= talloc_realloc_size(p1
, NULL
, 30);
549 p2
= talloc_realloc_size(p1
, p2
, 40);
551 CHECK_SIZE("realloc", p2
, 40);
552 CHECK_SIZE("realloc", root
, 60);
553 CHECK_BLOCKS("realloc", p1
, 4);
555 p1
= talloc_realloc_size(NULL
, p1
, 20);
556 CHECK_SIZE("realloc", p1
, 60);
558 talloc_increase_ref_count(p2
);
559 torture_assert("realloc", talloc_realloc_size(NULL
, p2
, 5) == NULL
,
560 "failed: talloc_realloc() on a referenced pointer should fail\n");
561 CHECK_BLOCKS("realloc", p1
, 4);
563 talloc_realloc_size(NULL
, p2
, 0);
564 talloc_realloc_size(NULL
, p2
, 0);
565 CHECK_BLOCKS("realloc", p1
, 4);
566 talloc_realloc_size(p1
, p2
, 0);
567 CHECK_BLOCKS("realloc", p1
, 3);
569 torture_assert("realloc", talloc_realloc_size(NULL
, p1
, 0x7fffffff) == NULL
,
570 "failed: oversize talloc should fail\n");
572 talloc_realloc_size(NULL
, p1
, 0);
573 CHECK_BLOCKS("realloc", root
, 4);
574 talloc_realloc_size(root
, p1
, 0);
575 CHECK_BLOCKS("realloc", root
, 1);
577 CHECK_SIZE("realloc", root
, 0);
581 printf("success: realloc\n");
587 test realloc with a child
589 static bool test_realloc_child(void)
597 struct el2
**list
, **list2
, **list3
;
600 printf("test: REALLOC WITH CHILD\n");
602 root
= talloc_new(NULL
);
604 el1
= talloc(root
, struct el1
);
605 el1
->list
= talloc(el1
, struct el2
*);
606 el1
->list
[0] = talloc(el1
->list
, struct el2
);
607 el1
->list
[0]->name
= talloc_strdup(el1
->list
[0], "testing");
609 el1
->list2
= talloc(el1
, struct el2
*);
610 el1
->list2
[0] = talloc(el1
->list2
, struct el2
);
611 el1
->list2
[0]->name
= talloc_strdup(el1
->list2
[0], "testing2");
613 el1
->list3
= talloc(el1
, struct el2
*);
614 el1
->list3
[0] = talloc(el1
->list3
, struct el2
);
615 el1
->list3
[0]->name
= talloc_strdup(el1
->list3
[0], "testing2");
617 el2
= talloc(el1
->list
, struct el2
);
618 el2
= talloc(el1
->list2
, struct el2
);
619 el2
= talloc(el1
->list3
, struct el2
);
621 el1
->list
= talloc_realloc(el1
, el1
->list
, struct el2
*, 100);
622 el1
->list2
= talloc_realloc(el1
, el1
->list2
, struct el2
*, 200);
623 el1
->list3
= talloc_realloc(el1
, el1
->list3
, struct el2
*, 300);
627 printf("success: REALLOC WITH CHILD\n");
634 static bool test_type(void)
645 printf("test: type\n# talloc type checking\n");
647 root
= talloc_new(NULL
);
649 el1
= talloc(root
, struct el1
);
653 torture_assert("type", talloc_get_type(el1
, struct el1
) == el1
,
654 "type check failed on el1\n");
655 torture_assert("type", talloc_get_type(el1
, struct el2
) == NULL
,
656 "type check failed on el1 with el2\n");
657 talloc_set_type(el1
, struct el2
);
658 torture_assert("type", talloc_get_type(el1
, struct el2
) == (struct el2
*)el1
,
659 "type set failed on el1 with el2\n");
663 printf("success: type\n");
670 static bool test_steal(void)
672 void *root
, *p1
, *p2
;
674 printf("test: steal\n# STEAL\n");
676 root
= talloc_new(NULL
);
678 p1
= talloc_array(root
, char, 10);
679 CHECK_SIZE("steal", p1
, 10);
681 p2
= talloc_realloc(root
, NULL
, char, 20);
682 CHECK_SIZE("steal", p1
, 10);
683 CHECK_SIZE("steal", root
, 30);
685 torture_assert("steal", talloc_steal(p1
, NULL
) == NULL
,
686 "failed: stealing NULL should give NULL\n");
688 torture_assert("steal", talloc_steal(p1
, p1
) == p1
,
689 "failed: stealing to ourselves is a nop\n");
690 CHECK_BLOCKS("steal", root
, 3);
691 CHECK_SIZE("steal", root
, 30);
693 talloc_steal(NULL
, p1
);
694 talloc_steal(NULL
, p2
);
695 CHECK_BLOCKS("steal", root
, 1);
696 CHECK_SIZE("steal", root
, 0);
699 talloc_steal(root
, p2
);
700 CHECK_BLOCKS("steal", root
, 2);
701 CHECK_SIZE("steal", root
, 20);
705 CHECK_BLOCKS("steal", root
, 1);
706 CHECK_SIZE("steal", root
, 0);
710 p1
= talloc_size(NULL
, 3);
711 talloc_report_full(NULL
, stderr
);
712 CHECK_SIZE("steal", NULL
, 3);
715 printf("success: steal\n");
722 static bool test_move(void)
730 printf("test: move\n# MOVE\n");
732 root
= talloc_new(NULL
);
734 t1
= talloc(root
, struct t_move
);
735 t2
= talloc(root
, struct t_move
);
736 t1
->p
= talloc_strdup(t1
, "foo");
737 t1
->x
= talloc(t1
, int);
740 t2
->p
= talloc_move(t2
, &t1
->p
);
741 t2
->x
= talloc_move(t2
, &t1
->x
);
742 torture_assert("move", t1
->p
== NULL
&& t1
->x
== NULL
&&
743 strcmp(t2
->p
, "foo") == 0 && *t2
->x
== 42,
744 "talloc move failed");
748 printf("success: move\n");
754 test talloc_realloc_fn
756 static bool test_realloc_fn(void)
760 printf("test: realloc_fn\n# talloc_realloc_fn\n");
762 root
= talloc_new(NULL
);
764 p1
= talloc_realloc_fn(root
, NULL
, 10);
765 CHECK_BLOCKS("realloc_fn", root
, 2);
766 CHECK_SIZE("realloc_fn", root
, 10);
767 p1
= talloc_realloc_fn(root
, p1
, 20);
768 CHECK_BLOCKS("realloc_fn", root
, 2);
769 CHECK_SIZE("realloc_fn", root
, 20);
770 p1
= talloc_realloc_fn(root
, p1
, 0);
771 CHECK_BLOCKS("realloc_fn", root
, 1);
772 CHECK_SIZE("realloc_fn", root
, 0);
776 printf("success: realloc_fn\n");
781 static bool test_unref_reparent(void)
783 void *root
, *p1
, *p2
, *c1
;
785 printf("test: unref_reparent\n# UNREFERENCE AFTER PARENT FREED\n");
787 root
= talloc_named_const(NULL
, 0, "root");
788 p1
= talloc_named_const(root
, 1, "orig parent");
789 p2
= talloc_named_const(root
, 1, "parent by reference");
791 c1
= talloc_named_const(p1
, 1, "child");
792 talloc_reference(p2
, c1
);
794 CHECK_PARENT("unref_reparent", c1
, p1
);
798 CHECK_PARENT("unref_reparent", c1
, p2
);
800 talloc_unlink(p2
, c1
);
802 CHECK_SIZE("unref_reparent", root
, 1);
807 printf("success: unref_reparent\n");
812 measure the speed of talloc versus malloc
814 static bool test_speed(void)
816 void *ctx
= talloc_new(NULL
);
818 const int loop
= 1000;
822 printf("test: speed\n# TALLOC VS MALLOC SPEED\n");
824 tv
= timeval_current();
828 for (i
=0;i
<loop
;i
++) {
829 p1
= talloc_size(ctx
, loop
% 100);
830 p2
= talloc_strdup(p1
, "foo bar");
831 p3
= talloc_size(p1
, 300);
835 } while (timeval_elapsed(&tv
) < 5.0);
837 fprintf(stderr
, "talloc: %.0f ops/sec\n", count
/timeval_elapsed(&tv
));
841 ctx
= talloc_pool(NULL
, 1024);
843 tv
= timeval_current();
847 for (i
=0;i
<loop
;i
++) {
848 p1
= talloc_size(ctx
, loop
% 100);
849 p2
= talloc_strdup(p1
, "foo bar");
850 p3
= talloc_size(p1
, 300);
851 talloc_free_children(ctx
);
854 } while (timeval_elapsed(&tv
) < 5.0);
858 fprintf(stderr
, "talloc_pool: %.0f ops/sec\n", count
/timeval_elapsed(&tv
));
860 tv
= timeval_current();
864 for (i
=0;i
<loop
;i
++) {
865 p1
= malloc(loop
% 100);
866 p2
= strdup("foo bar");
873 } while (timeval_elapsed(&tv
) < 5.0);
874 fprintf(stderr
, "malloc: %.0f ops/sec\n", count
/timeval_elapsed(&tv
));
876 printf("success: speed\n");
881 static bool test_lifeless(void)
883 void *top
= talloc_new(NULL
);
884 char *parent
, *child
;
885 void *child_owner
= talloc_new(NULL
);
887 printf("test: lifeless\n# TALLOC_UNLINK LOOP\n");
889 parent
= talloc_strdup(top
, "parent");
890 child
= talloc_strdup(parent
, "child");
891 (void)talloc_reference(child
, parent
);
892 (void)talloc_reference(child_owner
, child
);
893 talloc_report_full(top
, stderr
);
894 talloc_unlink(top
, parent
);
895 talloc_unlink(top
, child
);
896 talloc_report_full(top
, stderr
);
898 talloc_free(child_owner
);
901 printf("success: lifeless\n");
905 static int loop_destructor_count
;
907 static int test_loop_destructor(char *ptr
)
909 loop_destructor_count
++;
913 static bool test_loop(void)
915 void *top
= talloc_new(NULL
);
921 printf("test: loop\n# TALLOC LOOP DESTRUCTION\n");
923 parent
= talloc_strdup(top
, "parent");
924 req1
= talloc(parent
, struct req1
);
925 req1
->req2
= talloc_strdup(req1
, "req2");
926 talloc_set_destructor(req1
->req2
, test_loop_destructor
);
927 req1
->req3
= talloc_strdup(req1
, "req3");
928 (void)talloc_reference(req1
->req3
, req1
);
929 talloc_report_full(top
, stderr
);
931 talloc_report_full(top
, stderr
);
932 talloc_report_full(NULL
, stderr
);
935 torture_assert("loop", loop_destructor_count
== 1,
936 "FAILED TO FIRE LOOP DESTRUCTOR\n");
937 loop_destructor_count
= 0;
939 printf("success: loop\n");
943 static int fail_destructor_str(char *ptr
)
948 static bool test_free_parent_deny_child(void)
950 void *top
= talloc_new(NULL
);
955 printf("test: free_parent_deny_child\n# TALLOC FREE PARENT DENY CHILD\n");
957 level1
= talloc_strdup(top
, "level1");
958 level2
= talloc_strdup(level1
, "level2");
959 level3
= talloc_strdup(level2
, "level3");
961 talloc_set_destructor(level3
, fail_destructor_str
);
963 talloc_set_destructor(level3
, NULL
);
965 CHECK_PARENT("free_parent_deny_child", level3
, top
);
969 printf("success: free_parent_deny_child\n");
973 static bool test_talloc_ptrtype(void)
975 void *top
= talloc_new(NULL
);
979 } *s1
, *s2
, **s3
, ***s4
;
980 const char *location1
;
981 const char *location2
;
982 const char *location3
;
983 const char *location4
;
985 printf("test: ptrtype\n# TALLOC PTRTYPE\n");
987 s1
= talloc_ptrtype(top
, s1
);location1
= __location__
;
989 if (talloc_get_size(s1
) != sizeof(struct struct1
)) {
990 printf("failure: ptrtype [\n"
991 "talloc_ptrtype() allocated the wrong size %lu (should be %lu)\n"
992 "]\n", (unsigned long)talloc_get_size(s1
),
993 (unsigned long)sizeof(struct struct1
));
997 if (strcmp(location1
, talloc_get_name(s1
)) != 0) {
998 printf("failure: ptrtype [\n"
999 "talloc_ptrtype() sets the wrong name '%s' (should be '%s')\n]\n",
1000 talloc_get_name(s1
), location1
);
1004 s2
= talloc_array_ptrtype(top
, s2
, 10);location2
= __location__
;
1006 if (talloc_get_size(s2
) != (sizeof(struct struct1
) * 10)) {
1007 printf("failure: ptrtype [\n"
1008 "talloc_array_ptrtype() allocated the wrong size "
1009 "%lu (should be %lu)\n]\n",
1010 (unsigned long)talloc_get_size(s2
),
1011 (unsigned long)(sizeof(struct struct1
)*10));
1015 if (strcmp(location2
, talloc_get_name(s2
)) != 0) {
1016 printf("failure: ptrtype [\n"
1017 "talloc_array_ptrtype() sets the wrong name '%s' (should be '%s')\n]\n",
1018 talloc_get_name(s2
), location2
);
1022 s3
= talloc_array_ptrtype(top
, s3
, 10);location3
= __location__
;
1024 if (talloc_get_size(s3
) != (sizeof(struct struct1
*) * 10)) {
1025 printf("failure: ptrtype [\n"
1026 "talloc_array_ptrtype() allocated the wrong size "
1027 "%lu (should be %lu)\n]\n",
1028 (unsigned long)talloc_get_size(s3
),
1029 (unsigned long)(sizeof(struct struct1
*)*10));
1033 torture_assert_str_equal("ptrtype", location3
, talloc_get_name(s3
),
1034 "talloc_array_ptrtype() sets the wrong name");
1036 s4
= talloc_array_ptrtype(top
, s4
, 10);location4
= __location__
;
1038 if (talloc_get_size(s4
) != (sizeof(struct struct1
**) * 10)) {
1039 printf("failure: ptrtype [\n"
1040 "talloc_array_ptrtype() allocated the wrong size "
1041 "%lu (should be %lu)\n]\n",
1042 (unsigned long)talloc_get_size(s4
),
1043 (unsigned long)(sizeof(struct struct1
**)*10));
1047 torture_assert_str_equal("ptrtype", location4
, talloc_get_name(s4
),
1048 "talloc_array_ptrtype() sets the wrong name");
1052 printf("success: ptrtype\n");
1056 static int _test_talloc_free_in_destructor(void **ptr
)
1062 static bool test_talloc_free_in_destructor(void)
1071 printf("test: free_in_destructor\n# TALLOC FREE IN DESTRUCTOR\n");
1073 level0
= talloc_new(NULL
);
1074 level1
= talloc_new(level0
);
1075 level2
= talloc_new(level1
);
1076 level3
= talloc_new(level2
);
1077 level4
= talloc_new(level3
);
1078 level5
= talloc(level4
, void *);
1081 (void)talloc_reference(level0
, level3
);
1082 (void)talloc_reference(level3
, level3
);
1083 (void)talloc_reference(level5
, level3
);
1085 talloc_set_destructor(level5
, _test_talloc_free_in_destructor
);
1087 talloc_free(level1
);
1089 talloc_free(level0
);
1091 printf("success: free_in_destructor\n");
1095 static bool test_autofree(void)
1097 #if _SAMBA_BUILD_ < 4
1098 /* autofree test would kill smbtorture */
1100 printf("test: autofree\n# TALLOC AUTOFREE CONTEXT\n");
1102 p
= talloc_autofree_context();
1105 p
= talloc_autofree_context();
1108 printf("success: autofree\n");
1113 static bool test_pool(void)
1116 void *p1
, *p2
, *p3
, *p4
;
1119 pool
= talloc_pool(NULL
, 1024);
1121 p1
= talloc_size(pool
, 80);
1122 memset(p1
, 0x11, talloc_get_size(p1
));
1123 p2
= talloc_size(pool
, 20);
1124 memset(p2
, 0x11, talloc_get_size(p2
));
1125 p3
= talloc_size(p1
, 50);
1126 memset(p3
, 0x11, talloc_get_size(p3
));
1127 p4
= talloc_size(p3
, 1000);
1128 memset(p4
, 0x11, talloc_get_size(p4
));
1130 #if 1 /* this relies on ALWAYS_REALLOC == 0 in talloc.c */
1131 p2_2
= talloc_realloc_size(pool
, p2
, 20+1);
1132 torture_assert("pool realloc 20+1", p2_2
== p2
, "failed: pointer changed");
1133 memset(p2
, 0x11, talloc_get_size(p2
));
1134 p2_2
= talloc_realloc_size(pool
, p2
, 20-1);
1135 torture_assert("pool realloc 20-1", p2_2
== p2
, "failed: pointer changed");
1136 memset(p2
, 0x11, talloc_get_size(p2
));
1137 p2_2
= talloc_realloc_size(pool
, p2
, 20-1);
1138 torture_assert("pool realloc 20-1", p2_2
== p2
, "failed: pointer changed");
1139 memset(p2
, 0x11, talloc_get_size(p2
));
1143 /* this should reclaim the memory of p4 and p3 */
1144 p2_2
= talloc_realloc_size(pool
, p2
, 400);
1145 torture_assert("pool realloc 400", p2_2
== p2
, "failed: pointer changed");
1146 memset(p2
, 0x11, talloc_get_size(p2
));
1150 /* this should reclaim the memory of p1 */
1151 p2_2
= talloc_realloc_size(pool
, p2
, 800);
1152 torture_assert("pool realloc 800", p2_2
== p1
, "failed: pointer not changed");
1154 memset(p2
, 0x11, talloc_get_size(p2
));
1156 /* this should do a malloc */
1157 p2_2
= talloc_realloc_size(pool
, p2
, 1800);
1158 torture_assert("pool realloc 1800", p2_2
!= p2
, "failed: pointer not changed");
1160 memset(p2
, 0x11, talloc_get_size(p2
));
1162 /* this should reclaim the memory from the pool */
1163 p3
= talloc_size(pool
, 80);
1164 torture_assert("pool alloc 80", p3
== p1
, "failed: pointer changed");
1165 memset(p3
, 0x11, talloc_get_size(p3
));
1170 p1
= talloc_size(pool
, 80);
1171 memset(p1
, 0x11, talloc_get_size(p1
));
1172 p2
= talloc_size(pool
, 20);
1173 memset(p2
, 0x11, talloc_get_size(p2
));
1177 p2_2
= talloc_realloc_size(pool
, p2
, 20-1);
1178 torture_assert("pool realloc 20-1", p2_2
== p2
, "failed: pointer changed");
1179 memset(p2
, 0x11, talloc_get_size(p2
));
1180 p2_2
= talloc_realloc_size(pool
, p2
, 20-1);
1181 torture_assert("pool realloc 20-1", p2_2
== p2
, "failed: pointer changed");
1182 memset(p2
, 0x11, talloc_get_size(p2
));
1184 /* this should do a malloc */
1185 p2_2
= talloc_realloc_size(pool
, p2
, 1800);
1186 torture_assert("pool realloc 1800", p2_2
!= p2
, "failed: pointer not changed");
1188 memset(p2
, 0x11, talloc_get_size(p2
));
1190 /* this should reclaim the memory from the pool */
1191 p3
= talloc_size(pool
, 800);
1192 torture_assert("pool alloc 800", p3
== p1
, "failed: pointer changed");
1193 memset(p3
, 0x11, talloc_get_size(p3
));
1195 #endif /* this relies on ALWAYS_REALLOC == 0 in talloc.c */
1202 static bool test_pool_steal(void)
1211 root
= talloc_new(NULL
);
1212 pool
= talloc_pool(root
, 1024);
1214 p1
= talloc_size(pool
, 4 * 16);
1215 torture_assert("pool allocate 4 * 16", p1
!= NULL
, "failed ");
1216 memset(p1
, 0x11, talloc_get_size(p1
));
1217 p2
= talloc_size(pool
, 4 * 16);
1218 torture_assert("pool allocate 4 * 16", p2
> p1
, "failed: !(p2 > p1) ");
1219 memset(p2
, 0x11, talloc_get_size(p2
));
1221 ofs1
= PTR_DIFF(p2
, p1
);
1222 hdr
= ofs1
- talloc_get_size(p1
);
1224 talloc_steal(root
, p1
);
1225 talloc_steal(root
, p2
);
1231 #if 1 /* this relies on ALWAYS_REALLOC == 0 in talloc.c */
1232 p1_2
= talloc_realloc_size(root
, p1
, 5 * 16);
1233 torture_assert("pool realloc 5 * 16", p1_2
> p2
, "failed: pointer not changed");
1234 memset(p1_2
, 0x11, talloc_get_size(p1_2
));
1235 ofs1
= PTR_DIFF(p1_2
, p2
);
1236 ofs2
= talloc_get_size(p2
) + hdr
;
1238 torture_assert("pool realloc ", ofs1
== ofs2
, "failed: pointer offset unexpected");
1240 p2_2
= talloc_realloc_size(root
, p2
, 3 * 16);
1241 torture_assert("pool realloc 5 * 16", p2_2
== p2
, "failed: pointer changed");
1242 memset(p2_2
, 0x11, talloc_get_size(p2_2
));
1243 #endif /* this relies on ALWAYS_REALLOC == 0 in talloc.c */
1249 #if 1 /* this relies on ALWAYS_REALLOC == 0 in talloc.c */
1250 /* now we should reclaim the full pool */
1251 p2_2
= talloc_realloc_size(root
, p2
, 8 * 16);
1252 torture_assert("pool realloc 8 * 16", p2_2
== p1
, "failed: pointer not expected");
1254 memset(p2_2
, 0x11, talloc_get_size(p2_2
));
1256 /* now we malloc and free the full pool space */
1257 p2_2
= talloc_realloc_size(root
, p2
, 2 * 1024);
1258 torture_assert("pool realloc 2 * 1024", p2_2
!= p1
, "failed: pointer not expected");
1259 memset(p2_2
, 0x11, talloc_get_size(p2_2
));
1261 #endif /* this relies on ALWAYS_REALLOC == 0 in talloc.c */
1270 static bool test_free_ref_null_context(void)
1275 talloc_disable_null_tracking();
1276 p1
= talloc_new(NULL
);
1277 p2
= talloc_new(NULL
);
1279 p3
= talloc_reference(p2
, p1
);
1280 torture_assert("reference", p3
== p1
, "failed: reference on null");
1282 ret
= talloc_free(p1
);
1283 torture_assert("ref free with null parent", ret
== 0, "failed: free with null parent");
1286 talloc_enable_null_tracking_no_autofree();
1287 p1
= talloc_new(NULL
);
1288 p2
= talloc_new(NULL
);
1290 p3
= talloc_reference(p2
, p1
);
1291 torture_assert("reference", p3
== p1
, "failed: reference on null");
1293 ret
= talloc_free(p1
);
1294 torture_assert("ref free with null tracked parent", ret
== 0, "failed: free with null parent");
1300 static bool test_rusty(void)
1305 talloc_enable_null_tracking();
1306 root
= talloc_new(NULL
);
1307 p1
= talloc_strdup(root
, "foo");
1308 talloc_increase_ref_count(p1
);
1309 talloc_report_full(root
, stdout
);
1311 CHECK_BLOCKS("null_context", NULL
, 2);
1315 static bool test_free_children(void)
1319 const char *name
, *name2
;
1321 talloc_enable_null_tracking();
1322 root
= talloc_new(NULL
);
1323 p1
= talloc_strdup(root
, "foo1");
1324 p2
= talloc_strdup(p1
, "foo2");
1326 talloc_set_name(p1
, "%s", "testname");
1327 talloc_free_children(p1
);
1328 /* check its still a valid talloc ptr */
1329 talloc_get_size(talloc_get_name(p1
));
1330 if (strcmp(talloc_get_name(p1
), "testname") != 0) {
1334 talloc_set_name(p1
, "%s", "testname");
1335 name
= talloc_get_name(p1
);
1336 talloc_free_children(p1
);
1337 /* check its still a valid talloc ptr */
1338 talloc_get_size(talloc_get_name(p1
));
1339 torture_assert("name", name
== talloc_get_name(p1
), "name ptr changed");
1340 torture_assert("namecheck", strcmp(talloc_get_name(p1
), "testname") == 0,
1342 CHECK_BLOCKS("name1", p1
, 2);
1344 /* note that this does not free the old child name */
1345 talloc_set_name_const(p1
, "testname2");
1346 name2
= talloc_get_name(p1
);
1348 talloc_free_children(p1
);
1349 torture_assert("namecheck", strcmp(talloc_get_name(p1
), "testname2") == 0,
1351 CHECK_BLOCKS("name1", p1
, 1);
1353 talloc_report_full(root
, stdout
);
1359 static void test_reset(void)
1361 talloc_set_log_fn(test_log_stdout
);
1363 talloc_disable_null_tracking();
1364 talloc_enable_null_tracking_no_autofree();
1367 bool torture_local_talloc(struct torture_context
*tctx
)
1382 ret
&= test_unlink1();
1386 ret
&= test_realloc();
1388 ret
&= test_realloc_child();
1390 ret
&= test_steal();
1394 ret
&= test_unref_reparent();
1396 ret
&= test_realloc_fn();
1400 ret
&= test_lifeless();
1404 ret
&= test_free_parent_deny_child();
1406 ret
&= test_talloc_ptrtype();
1408 ret
&= test_talloc_free_in_destructor();
1412 ret
&= test_pool_steal();
1414 ret
&= test_free_ref_null_context();
1416 ret
&= test_rusty();
1418 ret
&= test_free_children();
1422 ret
&= test_speed();
1425 ret
&= test_autofree();
1428 talloc_disable_null_tracking();