talloc/testsuite: reset the globals after each subtest
[Samba/nivanova.git] / lib / talloc / testsuite.c
blob262228760ac9cf235633bb258770eb0540683109
1 /*
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
10 ** under the LGPL
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/>.
26 #include "replace.h"
27 #include "system/time.h"
28 #include "talloc.h"
30 static struct timeval timeval_current(void)
32 struct timeval tv;
33 gettimeofday(&tv, NULL);
34 return tv;
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); \
47 return false; \
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); \
55 return false; \
58 #if _SAMBA_BUILD_==3
59 #ifdef malloc
60 #undef malloc
61 #endif
62 #ifdef strdup
63 #undef strdup
64 #endif
65 #endif
67 #define CHECK_SIZE(test, ptr, tsize) do { \
68 if (talloc_total_size(ptr) != (tsize)) { \
69 printf("failed: %s [\nwrong '%s' tree size: got %u expected %u\n]\n", \
70 test, #ptr, \
71 (unsigned)talloc_total_size(ptr), \
72 (unsigned)tsize); \
73 talloc_report_full(ptr, stdout); \
74 return false; \
75 } \
76 } while (0)
78 #define CHECK_BLOCKS(test, ptr, tblocks) do { \
79 if (talloc_total_blocks(ptr) != (tblocks)) { \
80 printf("failed: %s [\nwrong '%s' tree blocks: got %u expected %u\n]\n", \
81 test, #ptr, \
82 (unsigned)talloc_total_blocks(ptr), \
83 (unsigned)tblocks); \
84 talloc_report_full(ptr, stdout); \
85 return false; \
86 } \
87 } while (0)
89 #define CHECK_PARENT(test, ptr, parent) do { \
90 if (talloc_parent(ptr) != (parent)) { \
91 printf("failed: %s [\n'%s' has wrong parent: got %p expected %p\n]\n", \
92 test, #ptr, \
93 talloc_parent(ptr), \
94 (parent)); \
95 talloc_report_full(ptr, stdout); \
96 talloc_report_full(parent, stdout); \
97 talloc_report_full(NULL, stdout); \
98 return false; \
99 } \
100 } while (0)
104 test references
106 static bool test_ref1(void)
108 void *root, *p1, *p2, *ref, *r1;
110 printf("test: ref1\n# SINGLE REFERENCE FREE\n");
112 root = talloc_named_const(NULL, 0, "root");
113 p1 = talloc_named_const(root, 1, "p1");
114 p2 = talloc_named_const(p1, 1, "p2");
115 talloc_named_const(p1, 1, "x1");
116 talloc_named_const(p1, 2, "x2");
117 talloc_named_const(p1, 3, "x3");
119 r1 = talloc_named_const(root, 1, "r1");
120 ref = talloc_reference(r1, p2);
121 talloc_report_full(root, stderr);
123 CHECK_BLOCKS("ref1", p1, 5);
124 CHECK_BLOCKS("ref1", p2, 1);
125 CHECK_BLOCKS("ref1", r1, 2);
127 fprintf(stderr, "Freeing p2\n");
128 talloc_unlink(r1, p2);
129 talloc_report_full(root, stderr);
131 CHECK_BLOCKS("ref1", p1, 5);
132 CHECK_BLOCKS("ref1", p2, 1);
133 CHECK_BLOCKS("ref1", r1, 1);
135 fprintf(stderr, "Freeing p1\n");
136 talloc_free(p1);
137 talloc_report_full(root, stderr);
139 CHECK_BLOCKS("ref1", r1, 1);
141 fprintf(stderr, "Freeing r1\n");
142 talloc_free(r1);
143 talloc_report_full(NULL, stderr);
145 fprintf(stderr, "Testing NULL\n");
146 if (talloc_reference(root, NULL)) {
147 return false;
150 CHECK_BLOCKS("ref1", root, 1);
152 CHECK_SIZE("ref1", root, 0);
154 talloc_free(root);
155 printf("success: ref1\n");
156 return true;
160 test references
162 static bool test_ref2(void)
164 void *root, *p1, *p2, *ref, *r1;
166 printf("test: ref2\n# DOUBLE REFERENCE FREE\n");
167 root = talloc_named_const(NULL, 0, "root");
168 p1 = talloc_named_const(root, 1, "p1");
169 talloc_named_const(p1, 1, "x1");
170 talloc_named_const(p1, 1, "x2");
171 talloc_named_const(p1, 1, "x3");
172 p2 = talloc_named_const(p1, 1, "p2");
174 r1 = talloc_named_const(root, 1, "r1");
175 ref = talloc_reference(r1, p2);
176 talloc_report_full(root, stderr);
178 CHECK_BLOCKS("ref2", p1, 5);
179 CHECK_BLOCKS("ref2", p2, 1);
180 CHECK_BLOCKS("ref2", r1, 2);
182 fprintf(stderr, "Freeing ref\n");
183 talloc_unlink(r1, ref);
184 talloc_report_full(root, stderr);
186 CHECK_BLOCKS("ref2", p1, 5);
187 CHECK_BLOCKS("ref2", p2, 1);
188 CHECK_BLOCKS("ref2", r1, 1);
190 fprintf(stderr, "Freeing p2\n");
191 talloc_free(p2);
192 talloc_report_full(root, stderr);
194 CHECK_BLOCKS("ref2", p1, 4);
195 CHECK_BLOCKS("ref2", r1, 1);
197 fprintf(stderr, "Freeing p1\n");
198 talloc_free(p1);
199 talloc_report_full(root, stderr);
201 CHECK_BLOCKS("ref2", r1, 1);
203 fprintf(stderr, "Freeing r1\n");
204 talloc_free(r1);
205 talloc_report_full(root, stderr);
207 CHECK_SIZE("ref2", root, 0);
209 talloc_free(root);
210 printf("success: ref2\n");
211 return true;
215 test references
217 static bool test_ref3(void)
219 void *root, *p1, *p2, *ref, *r1;
221 printf("test: ref3\n# PARENT REFERENCE FREE\n");
223 root = talloc_named_const(NULL, 0, "root");
224 p1 = talloc_named_const(root, 1, "p1");
225 p2 = talloc_named_const(root, 1, "p2");
226 r1 = talloc_named_const(p1, 1, "r1");
227 ref = talloc_reference(p2, r1);
228 talloc_report_full(root, stderr);
230 CHECK_BLOCKS("ref3", p1, 2);
231 CHECK_BLOCKS("ref3", p2, 2);
232 CHECK_BLOCKS("ref3", r1, 1);
234 fprintf(stderr, "Freeing p1\n");
235 talloc_free(p1);
236 talloc_report_full(root, stderr);
238 CHECK_BLOCKS("ref3", p2, 2);
239 CHECK_BLOCKS("ref3", r1, 1);
241 fprintf(stderr, "Freeing p2\n");
242 talloc_free(p2);
243 talloc_report_full(root, stderr);
245 CHECK_SIZE("ref3", root, 0);
247 talloc_free(root);
249 printf("success: ref3\n");
250 return true;
254 test references
256 static bool test_ref4(void)
258 void *root, *p1, *p2, *ref, *r1;
260 printf("test: ref4\n# REFERRER REFERENCE FREE\n");
262 root = talloc_named_const(NULL, 0, "root");
263 p1 = talloc_named_const(root, 1, "p1");
264 talloc_named_const(p1, 1, "x1");
265 talloc_named_const(p1, 1, "x2");
266 talloc_named_const(p1, 1, "x3");
267 p2 = talloc_named_const(p1, 1, "p2");
269 r1 = talloc_named_const(root, 1, "r1");
270 ref = talloc_reference(r1, p2);
271 talloc_report_full(root, stderr);
273 CHECK_BLOCKS("ref4", p1, 5);
274 CHECK_BLOCKS("ref4", p2, 1);
275 CHECK_BLOCKS("ref4", r1, 2);
277 fprintf(stderr, "Freeing r1\n");
278 talloc_free(r1);
279 talloc_report_full(root, stderr);
281 CHECK_BLOCKS("ref4", p1, 5);
282 CHECK_BLOCKS("ref4", p2, 1);
284 fprintf(stderr, "Freeing p2\n");
285 talloc_free(p2);
286 talloc_report_full(root, stderr);
288 CHECK_BLOCKS("ref4", p1, 4);
290 fprintf(stderr, "Freeing p1\n");
291 talloc_free(p1);
292 talloc_report_full(root, stderr);
294 CHECK_SIZE("ref4", root, 0);
296 talloc_free(root);
298 printf("success: ref4\n");
299 return true;
304 test references
306 static bool test_unlink1(void)
308 void *root, *p1, *p2, *ref, *r1;
310 printf("test: unlink\n# UNLINK\n");
312 root = talloc_named_const(NULL, 0, "root");
313 p1 = talloc_named_const(root, 1, "p1");
314 talloc_named_const(p1, 1, "x1");
315 talloc_named_const(p1, 1, "x2");
316 talloc_named_const(p1, 1, "x3");
317 p2 = talloc_named_const(p1, 1, "p2");
319 r1 = talloc_named_const(p1, 1, "r1");
320 ref = talloc_reference(r1, p2);
321 talloc_report_full(root, stderr);
323 CHECK_BLOCKS("unlink", p1, 7);
324 CHECK_BLOCKS("unlink", p2, 1);
325 CHECK_BLOCKS("unlink", r1, 2);
327 fprintf(stderr, "Unreferencing r1\n");
328 talloc_unlink(r1, p2);
329 talloc_report_full(root, stderr);
331 CHECK_BLOCKS("unlink", p1, 6);
332 CHECK_BLOCKS("unlink", p2, 1);
333 CHECK_BLOCKS("unlink", r1, 1);
335 fprintf(stderr, "Freeing p1\n");
336 talloc_free(p1);
337 talloc_report_full(root, stderr);
339 CHECK_SIZE("unlink", root, 0);
341 talloc_free(root);
343 printf("success: unlink\n");
344 return true;
347 static int fail_destructor(void *ptr)
349 return -1;
353 miscellaneous tests to try to get a higher test coverage percentage
355 static bool test_misc(void)
357 void *root, *p1;
358 char *p2;
359 double *d;
360 const char *name;
362 printf("test: misc\n# MISCELLANEOUS\n");
364 root = talloc_new(NULL);
366 p1 = talloc_size(root, 0x7fffffff);
367 torture_assert("misc", !p1, "failed: large talloc allowed\n");
369 p1 = talloc_strdup(root, "foo");
370 talloc_increase_ref_count(p1);
371 talloc_increase_ref_count(p1);
372 talloc_increase_ref_count(p1);
373 CHECK_BLOCKS("misc", p1, 1);
374 CHECK_BLOCKS("misc", root, 2);
375 talloc_unlink(NULL, p1);
376 CHECK_BLOCKS("misc", p1, 1);
377 CHECK_BLOCKS("misc", root, 2);
378 talloc_unlink(NULL, p1);
379 CHECK_BLOCKS("misc", p1, 1);
380 CHECK_BLOCKS("misc", root, 2);
381 p2 = talloc_strdup(p1, "foo");
382 torture_assert("misc", talloc_unlink(root, p2) == -1,
383 "failed: talloc_unlink() of non-reference context should return -1\n");
384 torture_assert("misc", talloc_unlink(p1, p2) == 0,
385 "failed: talloc_unlink() of parent should succeed\n");
386 talloc_unlink(NULL, p1);
387 CHECK_BLOCKS("misc", p1, 1);
388 CHECK_BLOCKS("misc", root, 2);
390 name = talloc_set_name(p1, "my name is %s", "foo");
391 torture_assert_str_equal("misc", talloc_get_name(p1), "my name is foo",
392 "failed: wrong name after talloc_set_name(my name is foo)");
393 CHECK_BLOCKS("misc", p1, 2);
394 CHECK_BLOCKS("misc", root, 3);
396 talloc_set_name_const(p1, NULL);
397 torture_assert_str_equal ("misc", talloc_get_name(p1), "UNNAMED",
398 "failed: wrong name after talloc_set_name(NULL)");
399 CHECK_BLOCKS("misc", p1, 2);
400 CHECK_BLOCKS("misc", root, 3);
402 torture_assert("misc", talloc_free(NULL) == -1,
403 "talloc_free(NULL) should give -1\n");
405 talloc_set_destructor(p1, fail_destructor);
406 torture_assert("misc", talloc_free(p1) == -1,
407 "Failed destructor should cause talloc_free to fail\n");
408 talloc_set_destructor(p1, NULL);
410 talloc_report(root, stderr);
413 p2 = (char *)talloc_zero_size(p1, 20);
414 torture_assert("misc", p2[19] == 0, "Failed to give zero memory\n");
415 talloc_free(p2);
417 torture_assert("misc", talloc_strdup(root, NULL) == NULL,
418 "failed: strdup on NULL should give NULL\n");
420 p2 = talloc_strndup(p1, "foo", 2);
421 torture_assert("misc", strcmp("fo", p2) == 0,
422 "strndup doesn't work\n");
423 p2 = talloc_asprintf_append_buffer(p2, "o%c", 'd');
424 torture_assert("misc", strcmp("food", p2) == 0,
425 "talloc_asprintf_append_buffer doesn't work\n");
426 CHECK_BLOCKS("misc", p2, 1);
427 CHECK_BLOCKS("misc", p1, 3);
429 p2 = talloc_asprintf_append_buffer(NULL, "hello %s", "world");
430 torture_assert("misc", strcmp("hello world", p2) == 0,
431 "talloc_asprintf_append_buffer doesn't work\n");
432 CHECK_BLOCKS("misc", p2, 1);
433 CHECK_BLOCKS("misc", p1, 3);
434 talloc_free(p2);
436 d = talloc_array(p1, double, 0x20000000);
437 torture_assert("misc", !d, "failed: integer overflow not detected\n");
439 d = talloc_realloc(p1, d, double, 0x20000000);
440 torture_assert("misc", !d, "failed: integer overflow not detected\n");
442 talloc_free(p1);
443 CHECK_BLOCKS("misc", root, 1);
445 p1 = talloc_named(root, 100, "%d bytes", 100);
446 CHECK_BLOCKS("misc", p1, 2);
447 CHECK_BLOCKS("misc", root, 3);
448 talloc_unlink(root, p1);
450 p1 = talloc_init("%d bytes", 200);
451 p2 = talloc_asprintf(p1, "my test '%s'", "string");
452 torture_assert_str_equal("misc", p2, "my test 'string'",
453 "failed: talloc_asprintf(\"my test '%%s'\", \"string\") gave: \"%s\"");
454 CHECK_BLOCKS("misc", p1, 3);
455 CHECK_SIZE("misc", p2, 17);
456 CHECK_BLOCKS("misc", root, 1);
457 talloc_unlink(NULL, p1);
459 p1 = talloc_named_const(root, 10, "p1");
460 p2 = (char *)talloc_named_const(root, 20, "p2");
461 (void)talloc_reference(p1, p2);
462 talloc_report_full(root, stderr);
463 talloc_unlink(root, p2);
464 talloc_report_full(root, stderr);
465 CHECK_BLOCKS("misc", p2, 1);
466 CHECK_BLOCKS("misc", p1, 2);
467 CHECK_BLOCKS("misc", root, 3);
468 talloc_unlink(p1, p2);
469 talloc_unlink(root, p1);
471 p1 = talloc_named_const(root, 10, "p1");
472 p2 = (char *)talloc_named_const(root, 20, "p2");
473 (void)talloc_reference(NULL, p2);
474 talloc_report_full(root, stderr);
475 talloc_unlink(root, p2);
476 talloc_report_full(root, stderr);
477 CHECK_BLOCKS("misc", p2, 1);
478 CHECK_BLOCKS("misc", p1, 1);
479 CHECK_BLOCKS("misc", root, 2);
480 talloc_unlink(NULL, p2);
481 talloc_unlink(root, p1);
483 /* Test that talloc_unlink is a no-op */
485 torture_assert("misc", talloc_unlink(root, NULL) == -1,
486 "failed: talloc_unlink(root, NULL) == -1\n");
488 talloc_report(root, stderr);
489 talloc_report(NULL, stderr);
491 CHECK_SIZE("misc", root, 0);
493 talloc_free(root);
495 CHECK_SIZE("misc", NULL, 0);
497 talloc_enable_leak_report();
498 talloc_enable_leak_report_full();
500 printf("success: misc\n");
502 return true;
507 test realloc
509 static bool test_realloc(void)
511 void *root, *p1, *p2;
513 printf("test: realloc\n# REALLOC\n");
515 root = talloc_new(NULL);
517 p1 = talloc_size(root, 10);
518 CHECK_SIZE("realloc", p1, 10);
520 p1 = talloc_realloc_size(NULL, p1, 20);
521 CHECK_SIZE("realloc", p1, 20);
523 talloc_new(p1);
525 p2 = talloc_realloc_size(p1, NULL, 30);
527 talloc_new(p1);
529 p2 = talloc_realloc_size(p1, p2, 40);
531 CHECK_SIZE("realloc", p2, 40);
532 CHECK_SIZE("realloc", root, 60);
533 CHECK_BLOCKS("realloc", p1, 4);
535 p1 = talloc_realloc_size(NULL, p1, 20);
536 CHECK_SIZE("realloc", p1, 60);
538 talloc_increase_ref_count(p2);
539 torture_assert("realloc", talloc_realloc_size(NULL, p2, 5) == NULL,
540 "failed: talloc_realloc() on a referenced pointer should fail\n");
541 CHECK_BLOCKS("realloc", p1, 4);
543 talloc_realloc_size(NULL, p2, 0);
544 talloc_realloc_size(NULL, p2, 0);
545 CHECK_BLOCKS("realloc", p1, 4);
546 talloc_realloc_size(p1, p2, 0);
547 CHECK_BLOCKS("realloc", p1, 3);
549 torture_assert("realloc", talloc_realloc_size(NULL, p1, 0x7fffffff) == NULL,
550 "failed: oversize talloc should fail\n");
552 talloc_realloc_size(NULL, p1, 0);
553 CHECK_BLOCKS("realloc", root, 4);
554 talloc_realloc_size(root, p1, 0);
555 CHECK_BLOCKS("realloc", root, 1);
557 CHECK_SIZE("realloc", root, 0);
559 talloc_free(root);
561 printf("success: realloc\n");
563 return true;
567 test realloc with a child
569 static bool test_realloc_child(void)
571 void *root;
572 struct el2 {
573 const char *name;
574 } *el2;
575 struct el1 {
576 int count;
577 struct el2 **list, **list2, **list3;
578 } *el1;
580 printf("test: REALLOC WITH CHILD\n");
582 root = talloc_new(NULL);
584 el1 = talloc(root, struct el1);
585 el1->list = talloc(el1, struct el2 *);
586 el1->list[0] = talloc(el1->list, struct el2);
587 el1->list[0]->name = talloc_strdup(el1->list[0], "testing");
589 el1->list2 = talloc(el1, struct el2 *);
590 el1->list2[0] = talloc(el1->list2, struct el2);
591 el1->list2[0]->name = talloc_strdup(el1->list2[0], "testing2");
593 el1->list3 = talloc(el1, struct el2 *);
594 el1->list3[0] = talloc(el1->list3, struct el2);
595 el1->list3[0]->name = talloc_strdup(el1->list3[0], "testing2");
597 el2 = talloc(el1->list, struct el2);
598 el2 = talloc(el1->list2, struct el2);
599 el2 = talloc(el1->list3, struct el2);
601 el1->list = talloc_realloc(el1, el1->list, struct el2 *, 100);
602 el1->list2 = talloc_realloc(el1, el1->list2, struct el2 *, 200);
603 el1->list3 = talloc_realloc(el1, el1->list3, struct el2 *, 300);
605 talloc_free(root);
607 printf("success: REALLOC WITH CHILD\n");
608 return true;
612 test type checking
614 static bool test_type(void)
616 void *root;
617 struct el1 {
618 int count;
620 struct el2 {
621 int count;
623 struct el1 *el1;
625 printf("test: type\n# talloc type checking\n");
627 root = talloc_new(NULL);
629 el1 = talloc(root, struct el1);
631 el1->count = 1;
633 torture_assert("type", talloc_get_type(el1, struct el1) == el1,
634 "type check failed on el1\n");
635 torture_assert("type", talloc_get_type(el1, struct el2) == NULL,
636 "type check failed on el1 with el2\n");
637 talloc_set_type(el1, struct el2);
638 torture_assert("type", talloc_get_type(el1, struct el2) == (struct el2 *)el1,
639 "type set failed on el1 with el2\n");
641 talloc_free(root);
643 printf("success: type\n");
644 return true;
648 test steal
650 static bool test_steal(void)
652 void *root, *p1, *p2;
654 printf("test: steal\n# STEAL\n");
656 root = talloc_new(NULL);
658 p1 = talloc_array(root, char, 10);
659 CHECK_SIZE("steal", p1, 10);
661 p2 = talloc_realloc(root, NULL, char, 20);
662 CHECK_SIZE("steal", p1, 10);
663 CHECK_SIZE("steal", root, 30);
665 torture_assert("steal", talloc_steal(p1, NULL) == NULL,
666 "failed: stealing NULL should give NULL\n");
668 torture_assert("steal", talloc_steal(p1, p1) == p1,
669 "failed: stealing to ourselves is a nop\n");
670 CHECK_BLOCKS("steal", root, 3);
671 CHECK_SIZE("steal", root, 30);
673 talloc_steal(NULL, p1);
674 talloc_steal(NULL, p2);
675 CHECK_BLOCKS("steal", root, 1);
676 CHECK_SIZE("steal", root, 0);
678 talloc_free(p1);
679 talloc_steal(root, p2);
680 CHECK_BLOCKS("steal", root, 2);
681 CHECK_SIZE("steal", root, 20);
683 talloc_free(p2);
685 CHECK_BLOCKS("steal", root, 1);
686 CHECK_SIZE("steal", root, 0);
688 talloc_free(root);
690 p1 = talloc_size(NULL, 3);
691 talloc_report_full(NULL, stderr);
692 CHECK_SIZE("steal", NULL, 3);
693 talloc_free(p1);
695 printf("success: steal\n");
696 return true;
700 test move
702 static bool test_move(void)
704 void *root;
705 struct t_move {
706 char *p;
707 int *x;
708 } *t1, *t2;
710 printf("test: move\n# MOVE\n");
712 root = talloc_new(NULL);
714 t1 = talloc(root, struct t_move);
715 t2 = talloc(root, struct t_move);
716 t1->p = talloc_strdup(t1, "foo");
717 t1->x = talloc(t1, int);
718 *t1->x = 42;
720 t2->p = talloc_move(t2, &t1->p);
721 t2->x = talloc_move(t2, &t1->x);
722 torture_assert("move", t1->p == NULL && t1->x == NULL &&
723 strcmp(t2->p, "foo") == 0 && *t2->x == 42,
724 "talloc move failed");
726 talloc_free(root);
728 printf("success: move\n");
730 return true;
734 test talloc_realloc_fn
736 static bool test_realloc_fn(void)
738 void *root, *p1;
740 printf("test: realloc_fn\n# talloc_realloc_fn\n");
742 root = talloc_new(NULL);
744 p1 = talloc_realloc_fn(root, NULL, 10);
745 CHECK_BLOCKS("realloc_fn", root, 2);
746 CHECK_SIZE("realloc_fn", root, 10);
747 p1 = talloc_realloc_fn(root, p1, 20);
748 CHECK_BLOCKS("realloc_fn", root, 2);
749 CHECK_SIZE("realloc_fn", root, 20);
750 p1 = talloc_realloc_fn(root, p1, 0);
751 CHECK_BLOCKS("realloc_fn", root, 1);
752 CHECK_SIZE("realloc_fn", root, 0);
754 talloc_free(root);
756 printf("success: realloc_fn\n");
757 return true;
761 static bool test_unref_reparent(void)
763 void *root, *p1, *p2, *c1;
765 printf("test: unref_reparent\n# UNREFERENCE AFTER PARENT FREED\n");
767 root = talloc_named_const(NULL, 0, "root");
768 p1 = talloc_named_const(root, 1, "orig parent");
769 p2 = talloc_named_const(root, 1, "parent by reference");
771 c1 = talloc_named_const(p1, 1, "child");
772 talloc_reference(p2, c1);
774 CHECK_PARENT("unref_reparent", c1, p1);
776 talloc_free(p1);
778 CHECK_PARENT("unref_reparent", c1, p2);
780 talloc_unlink(p2, c1);
782 CHECK_SIZE("unref_reparent", root, 1);
784 talloc_free(p2);
785 talloc_free(root);
787 printf("success: unref_reparent\n");
788 return true;
792 measure the speed of talloc versus malloc
794 static bool test_speed(void)
796 void *ctx = talloc_new(NULL);
797 unsigned count;
798 const int loop = 1000;
799 int i;
800 struct timeval tv;
802 printf("test: speed\n# TALLOC VS MALLOC SPEED\n");
804 tv = timeval_current();
805 count = 0;
806 do {
807 void *p1, *p2, *p3;
808 for (i=0;i<loop;i++) {
809 p1 = talloc_size(ctx, loop % 100);
810 p2 = talloc_strdup(p1, "foo bar");
811 p3 = talloc_size(p1, 300);
812 talloc_free(p1);
814 count += 3 * loop;
815 } while (timeval_elapsed(&tv) < 5.0);
817 fprintf(stderr, "talloc: %.0f ops/sec\n", count/timeval_elapsed(&tv));
819 talloc_free(ctx);
821 ctx = talloc_pool(NULL, 1024);
823 tv = timeval_current();
824 count = 0;
825 do {
826 void *p1, *p2, *p3;
827 for (i=0;i<loop;i++) {
828 p1 = talloc_size(ctx, loop % 100);
829 p2 = talloc_strdup(p1, "foo bar");
830 p3 = talloc_size(p1, 300);
831 talloc_free_children(ctx);
833 count += 3 * loop;
834 } while (timeval_elapsed(&tv) < 5.0);
836 talloc_free(ctx);
838 fprintf(stderr, "talloc_pool: %.0f ops/sec\n", count/timeval_elapsed(&tv));
840 tv = timeval_current();
841 count = 0;
842 do {
843 void *p1, *p2, *p3;
844 for (i=0;i<loop;i++) {
845 p1 = malloc(loop % 100);
846 p2 = strdup("foo bar");
847 p3 = malloc(300);
848 free(p1);
849 free(p2);
850 free(p3);
852 count += 3 * loop;
853 } while (timeval_elapsed(&tv) < 5.0);
854 fprintf(stderr, "malloc: %.0f ops/sec\n", count/timeval_elapsed(&tv));
856 printf("success: speed\n");
858 return true;
861 static bool test_lifeless(void)
863 void *top = talloc_new(NULL);
864 char *parent, *child;
865 void *child_owner = talloc_new(NULL);
867 printf("test: lifeless\n# TALLOC_UNLINK LOOP\n");
869 parent = talloc_strdup(top, "parent");
870 child = talloc_strdup(parent, "child");
871 (void)talloc_reference(child, parent);
872 (void)talloc_reference(child_owner, child);
873 talloc_report_full(top, stderr);
874 talloc_unlink(top, parent);
875 talloc_unlink(top, child);
876 talloc_report_full(top, stderr);
877 talloc_free(top);
878 talloc_free(child_owner);
879 talloc_free(child);
881 printf("success: lifeless\n");
882 return true;
885 static int loop_destructor_count;
887 static int test_loop_destructor(char *ptr)
889 loop_destructor_count++;
890 return 0;
893 static bool test_loop(void)
895 void *top = talloc_new(NULL);
896 char *parent;
897 struct req1 {
898 char *req2, *req3;
899 } *req1;
901 printf("test: loop\n# TALLOC LOOP DESTRUCTION\n");
903 parent = talloc_strdup(top, "parent");
904 req1 = talloc(parent, struct req1);
905 req1->req2 = talloc_strdup(req1, "req2");
906 talloc_set_destructor(req1->req2, test_loop_destructor);
907 req1->req3 = talloc_strdup(req1, "req3");
908 (void)talloc_reference(req1->req3, req1);
909 talloc_report_full(top, stderr);
910 talloc_free(parent);
911 talloc_report_full(top, stderr);
912 talloc_report_full(NULL, stderr);
913 talloc_free(top);
915 torture_assert("loop", loop_destructor_count == 1,
916 "FAILED TO FIRE LOOP DESTRUCTOR\n");
917 loop_destructor_count = 0;
919 printf("success: loop\n");
920 return true;
923 static int fail_destructor_str(char *ptr)
925 return -1;
928 static bool test_free_parent_deny_child(void)
930 void *top = talloc_new(NULL);
931 char *level1;
932 char *level2;
933 char *level3;
935 printf("test: free_parent_deny_child\n# TALLOC FREE PARENT DENY CHILD\n");
937 level1 = talloc_strdup(top, "level1");
938 level2 = talloc_strdup(level1, "level2");
939 level3 = talloc_strdup(level2, "level3");
941 talloc_set_destructor(level3, fail_destructor_str);
942 talloc_free(level1);
943 talloc_set_destructor(level3, NULL);
945 CHECK_PARENT("free_parent_deny_child", level3, top);
947 talloc_free(top);
949 printf("success: free_parent_deny_child\n");
950 return true;
953 static bool test_talloc_ptrtype(void)
955 void *top = talloc_new(NULL);
956 struct struct1 {
957 int foo;
958 int bar;
959 } *s1, *s2, **s3, ***s4;
960 const char *location1;
961 const char *location2;
962 const char *location3;
963 const char *location4;
965 printf("test: ptrtype\n# TALLOC PTRTYPE\n");
967 s1 = talloc_ptrtype(top, s1);location1 = __location__;
969 if (talloc_get_size(s1) != sizeof(struct struct1)) {
970 printf("failure: ptrtype [\n"
971 "talloc_ptrtype() allocated the wrong size %lu (should be %lu)\n"
972 "]\n", (unsigned long)talloc_get_size(s1),
973 (unsigned long)sizeof(struct struct1));
974 return false;
977 if (strcmp(location1, talloc_get_name(s1)) != 0) {
978 printf("failure: ptrtype [\n"
979 "talloc_ptrtype() sets the wrong name '%s' (should be '%s')\n]\n",
980 talloc_get_name(s1), location1);
981 return false;
984 s2 = talloc_array_ptrtype(top, s2, 10);location2 = __location__;
986 if (talloc_get_size(s2) != (sizeof(struct struct1) * 10)) {
987 printf("failure: ptrtype [\n"
988 "talloc_array_ptrtype() allocated the wrong size "
989 "%lu (should be %lu)\n]\n",
990 (unsigned long)talloc_get_size(s2),
991 (unsigned long)(sizeof(struct struct1)*10));
992 return false;
995 if (strcmp(location2, talloc_get_name(s2)) != 0) {
996 printf("failure: ptrtype [\n"
997 "talloc_array_ptrtype() sets the wrong name '%s' (should be '%s')\n]\n",
998 talloc_get_name(s2), location2);
999 return false;
1002 s3 = talloc_array_ptrtype(top, s3, 10);location3 = __location__;
1004 if (talloc_get_size(s3) != (sizeof(struct struct1 *) * 10)) {
1005 printf("failure: ptrtype [\n"
1006 "talloc_array_ptrtype() allocated the wrong size "
1007 "%lu (should be %lu)\n]\n",
1008 (unsigned long)talloc_get_size(s3),
1009 (unsigned long)(sizeof(struct struct1 *)*10));
1010 return false;
1013 torture_assert_str_equal("ptrtype", location3, talloc_get_name(s3),
1014 "talloc_array_ptrtype() sets the wrong name");
1016 s4 = talloc_array_ptrtype(top, s4, 10);location4 = __location__;
1018 if (talloc_get_size(s4) != (sizeof(struct struct1 **) * 10)) {
1019 printf("failure: ptrtype [\n"
1020 "talloc_array_ptrtype() allocated the wrong size "
1021 "%lu (should be %lu)\n]\n",
1022 (unsigned long)talloc_get_size(s4),
1023 (unsigned long)(sizeof(struct struct1 **)*10));
1024 return false;
1027 torture_assert_str_equal("ptrtype", location4, talloc_get_name(s4),
1028 "talloc_array_ptrtype() sets the wrong name");
1030 talloc_free(top);
1032 printf("success: ptrtype\n");
1033 return true;
1036 static int _test_talloc_free_in_destructor(void **ptr)
1038 talloc_free(*ptr);
1039 return 0;
1042 static bool test_talloc_free_in_destructor(void)
1044 void *level0;
1045 void *level1;
1046 void *level2;
1047 void *level3;
1048 void *level4;
1049 void **level5;
1051 printf("test: free_in_destructor\n# TALLOC FREE IN DESTRUCTOR\n");
1053 level0 = talloc_new(NULL);
1054 level1 = talloc_new(level0);
1055 level2 = talloc_new(level1);
1056 level3 = talloc_new(level2);
1057 level4 = talloc_new(level3);
1058 level5 = talloc(level4, void *);
1060 *level5 = level3;
1061 (void)talloc_reference(level0, level3);
1062 (void)talloc_reference(level3, level3);
1063 (void)talloc_reference(level5, level3);
1065 talloc_set_destructor(level5, _test_talloc_free_in_destructor);
1067 talloc_free(level1);
1069 talloc_free(level0);
1071 printf("success: free_in_destructor\n");
1072 return true;
1075 static bool test_autofree(void)
1077 #if _SAMBA_BUILD_ < 4
1078 /* autofree test would kill smbtorture */
1079 void *p;
1080 printf("test: autofree\n# TALLOC AUTOFREE CONTEXT\n");
1082 p = talloc_autofree_context();
1083 talloc_free(p);
1085 p = talloc_autofree_context();
1086 talloc_free(p);
1088 printf("success: autofree\n");
1089 #endif
1090 return true;
1093 static bool test_pool(void)
1095 void *pool;
1096 void *p1, *p2, *p3, *p4;
1098 pool = talloc_pool(NULL, 1024);
1100 p1 = talloc_size(pool, 80);
1101 p2 = talloc_size(pool, 20);
1102 p3 = talloc_size(p1, 50);
1103 p4 = talloc_size(p3, 1000);
1105 talloc_free(pool);
1107 return true;
1110 static void test_reset(void)
1112 talloc_disable_null_tracking();
1113 talloc_enable_null_tracking();
1116 struct torture_context;
1117 bool torture_local_talloc(struct torture_context *tctx)
1119 bool ret = true;
1121 setlinebuf(stdout);
1123 test_reset();
1124 ret &= test_ref1();
1125 test_reset();
1126 ret &= test_ref2();
1127 test_reset();
1128 ret &= test_ref3();
1129 test_reset();
1130 ret &= test_ref4();
1131 test_reset();
1132 ret &= test_unlink1();
1133 test_reset();
1134 ret &= test_misc();
1135 test_reset();
1136 ret &= test_realloc();
1137 test_reset();
1138 ret &= test_realloc_child();
1139 test_reset();
1140 ret &= test_steal();
1141 test_reset();
1142 ret &= test_move();
1143 test_reset();
1144 ret &= test_unref_reparent();
1145 test_reset();
1146 ret &= test_realloc_fn();
1147 test_reset();
1148 ret &= test_type();
1149 test_reset();
1150 ret &= test_lifeless();
1151 test_reset();
1152 ret &= test_loop();
1153 test_reset();
1154 ret &= test_free_parent_deny_child();
1155 test_reset();
1156 ret &= test_talloc_ptrtype();
1157 test_reset();
1158 ret &= test_talloc_free_in_destructor();
1159 test_reset();
1160 ret &= test_pool();
1162 if (ret) {
1163 test_reset();
1164 ret &= test_speed();
1166 test_reset();
1167 ret &= test_autofree();
1169 test_reset();
1171 return ret;