talloc: Test the pooled object
[Samba/wip.git] / lib / talloc / testsuite.c
blob888d260cbc9acc1e2f6f2ef0ab848d994eee6a66
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 #include "talloc_testsuite.h"
32 static struct timeval timeval_current(void)
34 struct timeval tv;
35 gettimeofday(&tv, NULL);
36 return tv;
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); \
49 return false; \
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); \
57 return false; \
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), \
65 (unsigned)tsize); \
66 talloc_report_full(ptr, stdout); \
67 return false; \
68 } \
69 } while (0)
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), \
76 (unsigned)tblocks); \
77 talloc_report_full(ptr, stdout); \
78 return false; \
79 } \
80 } while (0)
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, \
86 talloc_parent(ptr), \
87 (parent)); \
88 talloc_report_full(ptr, stdout); \
89 talloc_report_full(parent, stdout); \
90 talloc_report_full(NULL, stdout); \
91 return false; \
92 } \
93 } while (0)
95 static unsigned int test_abort_count;
97 #if 0
98 static void test_abort_fn(const char *reason)
100 printf("# test_abort_fn(%s)\n", reason);
101 test_abort_count++;
104 static void test_abort_start(void)
106 test_abort_count = 0;
107 talloc_set_abort_fn(test_abort_fn);
109 #endif
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);
123 test references
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");
155 talloc_free(p1);
156 talloc_report_full(root, stderr);
158 CHECK_BLOCKS("ref1", r1, 1);
160 fprintf(stderr, "Freeing r1\n");
161 talloc_free(r1);
162 talloc_report_full(NULL, stderr);
164 fprintf(stderr, "Testing NULL\n");
165 if (talloc_reference(root, NULL)) {
166 return false;
169 CHECK_BLOCKS("ref1", root, 1);
171 CHECK_SIZE("ref1", root, 0);
173 talloc_free(root);
174 printf("success: ref1\n");
175 return true;
179 test references
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");
210 talloc_free(p2);
211 talloc_report_full(root, stderr);
213 CHECK_BLOCKS("ref2", p1, 4);
214 CHECK_BLOCKS("ref2", r1, 1);
216 fprintf(stderr, "Freeing p1\n");
217 talloc_free(p1);
218 talloc_report_full(root, stderr);
220 CHECK_BLOCKS("ref2", r1, 1);
222 fprintf(stderr, "Freeing r1\n");
223 talloc_free(r1);
224 talloc_report_full(root, stderr);
226 CHECK_SIZE("ref2", root, 0);
228 talloc_free(root);
229 printf("success: ref2\n");
230 return true;
234 test references
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");
254 talloc_free(p1);
255 talloc_report_full(root, stderr);
257 CHECK_BLOCKS("ref3", p2, 2);
258 CHECK_BLOCKS("ref3", r1, 1);
260 fprintf(stderr, "Freeing p2\n");
261 talloc_free(p2);
262 talloc_report_full(root, stderr);
264 CHECK_SIZE("ref3", root, 0);
266 talloc_free(root);
268 printf("success: ref3\n");
269 return true;
273 test references
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");
297 talloc_free(r1);
298 talloc_report_full(root, stderr);
300 CHECK_BLOCKS("ref4", p1, 5);
301 CHECK_BLOCKS("ref4", p2, 1);
303 fprintf(stderr, "Freeing p2\n");
304 talloc_free(p2);
305 talloc_report_full(root, stderr);
307 CHECK_BLOCKS("ref4", p1, 4);
309 fprintf(stderr, "Freeing p1\n");
310 talloc_free(p1);
311 talloc_report_full(root, stderr);
313 CHECK_SIZE("ref4", root, 0);
315 talloc_free(root);
317 printf("success: ref4\n");
318 return true;
323 test references
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");
355 talloc_free(p1);
356 talloc_report_full(root, stderr);
358 CHECK_SIZE("unlink", root, 0);
360 talloc_free(root);
362 printf("success: unlink\n");
363 return true;
366 static int fail_destructor(void *ptr)
368 return -1;
372 miscellaneous tests to try to get a higher test coverage percentage
374 static bool test_misc(void)
376 void *root, *p1;
377 char *p2;
378 double *d;
379 const char *name;
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");
434 talloc_free(p2);
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);
453 talloc_free(p2);
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");
461 talloc_free(p1);
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);
512 talloc_free(root);
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");
522 return true;
527 test realloc
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);
543 talloc_new(p1);
545 p2 = talloc_realloc_size(p1, NULL, 30);
547 talloc_new(p1);
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);
579 talloc_free(root);
581 printf("success: realloc\n");
583 return true;
587 test realloc with a child
589 static bool test_realloc_child(void)
591 void *root;
592 struct el2 {
593 const char *name;
594 } *el2;
595 struct el1 {
596 int count;
597 struct el2 **list, **list2, **list3;
598 } *el1;
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);
625 talloc_free(root);
627 printf("success: REALLOC WITH CHILD\n");
628 return true;
632 test type checking
634 static bool test_type(void)
636 void *root;
637 struct el1 {
638 int count;
640 struct el2 {
641 int count;
643 struct el1 *el1;
645 printf("test: type\n# talloc type checking\n");
647 root = talloc_new(NULL);
649 el1 = talloc(root, struct el1);
651 el1->count = 1;
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");
661 talloc_free(root);
663 printf("success: type\n");
664 return true;
668 test steal
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);
698 talloc_free(p1);
699 talloc_steal(root, p2);
700 CHECK_BLOCKS("steal", root, 2);
701 CHECK_SIZE("steal", root, 20);
703 talloc_free(p2);
705 CHECK_BLOCKS("steal", root, 1);
706 CHECK_SIZE("steal", root, 0);
708 talloc_free(root);
710 p1 = talloc_size(NULL, 3);
711 talloc_report_full(NULL, stderr);
712 CHECK_SIZE("steal", NULL, 3);
713 talloc_free(p1);
715 printf("success: steal\n");
716 return true;
720 test move
722 static bool test_move(void)
724 void *root;
725 struct t_move {
726 char *p;
727 int *x;
728 } *t1, *t2;
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);
738 *t1->x = 42;
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");
746 talloc_free(root);
748 printf("success: move\n");
750 return true;
754 test talloc_realloc_fn
756 static bool test_realloc_fn(void)
758 void *root, *p1;
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);
774 talloc_free(root);
776 printf("success: realloc_fn\n");
777 return true;
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);
796 talloc_free(p1);
798 CHECK_PARENT("unref_reparent", c1, p2);
800 talloc_unlink(p2, c1);
802 CHECK_SIZE("unref_reparent", root, 1);
804 talloc_free(p2);
805 talloc_free(root);
807 printf("success: unref_reparent\n");
808 return true;
812 measure the speed of talloc versus malloc
814 static bool test_speed(void)
816 void *ctx = talloc_new(NULL);
817 unsigned count;
818 const int loop = 1000;
819 int i;
820 struct timeval tv;
822 printf("test: speed\n# TALLOC VS MALLOC SPEED\n");
824 tv = timeval_current();
825 count = 0;
826 do {
827 void *p1, *p2, *p3;
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);
832 talloc_free(p1);
834 count += 3 * loop;
835 } while (timeval_elapsed(&tv) < 5.0);
837 fprintf(stderr, "talloc: %.0f ops/sec\n", count/timeval_elapsed(&tv));
839 talloc_free(ctx);
841 ctx = talloc_pool(NULL, 1024);
843 tv = timeval_current();
844 count = 0;
845 do {
846 void *p1, *p2, *p3;
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(p1);
853 count += 3 * loop;
854 } while (timeval_elapsed(&tv) < 5.0);
856 talloc_free(ctx);
858 fprintf(stderr, "talloc_pool: %.0f ops/sec\n", count/timeval_elapsed(&tv));
860 tv = timeval_current();
861 count = 0;
862 do {
863 void *p1, *p2, *p3;
864 for (i=0;i<loop;i++) {
865 p1 = malloc(loop % 100);
866 p2 = strdup("foo bar");
867 p3 = malloc(300);
868 free(p1);
869 free(p2);
870 free(p3);
872 count += 3 * loop;
873 } while (timeval_elapsed(&tv) < 5.0);
874 fprintf(stderr, "malloc: %.0f ops/sec\n", count/timeval_elapsed(&tv));
876 printf("success: speed\n");
878 return true;
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);
897 talloc_free(top);
898 talloc_free(child_owner);
899 talloc_free(child);
901 printf("success: lifeless\n");
902 return true;
905 static int loop_destructor_count;
907 static int test_loop_destructor(char *ptr)
909 loop_destructor_count++;
910 return 0;
913 static bool test_loop(void)
915 void *top = talloc_new(NULL);
916 char *parent;
917 struct req1 {
918 char *req2, *req3;
919 } *req1;
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);
930 talloc_free(parent);
931 talloc_report_full(top, stderr);
932 talloc_report_full(NULL, stderr);
933 talloc_free(top);
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");
940 return true;
943 static int fail_destructor_str(char *ptr)
945 return -1;
948 static bool test_free_parent_deny_child(void)
950 void *top = talloc_new(NULL);
951 char *level1;
952 char *level2;
953 char *level3;
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);
962 talloc_free(level1);
963 talloc_set_destructor(level3, NULL);
965 CHECK_PARENT("free_parent_deny_child", level3, top);
967 talloc_free(top);
969 printf("success: free_parent_deny_child\n");
970 return true;
973 static bool test_talloc_ptrtype(void)
975 void *top = talloc_new(NULL);
976 struct struct1 {
977 int foo;
978 int bar;
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));
994 return false;
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);
1001 return false;
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));
1012 return false;
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);
1019 return false;
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));
1030 return false;
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));
1044 return false;
1047 torture_assert_str_equal("ptrtype", location4, talloc_get_name(s4),
1048 "talloc_array_ptrtype() sets the wrong name");
1050 talloc_free(top);
1052 printf("success: ptrtype\n");
1053 return true;
1056 static int _test_talloc_free_in_destructor(void **ptr)
1058 talloc_free(*ptr);
1059 return 0;
1062 static bool test_talloc_free_in_destructor(void)
1064 void *level0;
1065 void *level1;
1066 void *level2;
1067 void *level3;
1068 void *level4;
1069 void **level5;
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 *);
1080 *level5 = level3;
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");
1092 return true;
1095 static bool test_autofree(void)
1097 #if _SAMBA_BUILD_ < 4
1098 /* autofree test would kill smbtorture */
1099 void *p;
1100 printf("test: autofree\n# TALLOC AUTOFREE CONTEXT\n");
1102 p = talloc_autofree_context();
1103 talloc_free(p);
1105 p = talloc_autofree_context();
1106 talloc_free(p);
1108 printf("success: autofree\n");
1109 #endif
1110 return true;
1113 static bool test_pool(void)
1115 void *pool;
1116 void *p1, *p2, *p3, *p4;
1117 void *p2_2;
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));
1141 talloc_free(p3);
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));
1148 talloc_free(p1);
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");
1153 p2 = p2_2;
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");
1159 p2 = p2_2;
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));
1167 talloc_free(p2);
1168 talloc_free(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));
1175 talloc_free(p1);
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");
1187 p2 = p2_2;
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 */
1197 talloc_free(pool);
1199 return true;
1202 static bool test_pool_steal(void)
1204 void *root;
1205 void *pool;
1206 void *p1, *p2;
1207 void *p1_2, *p2_2;
1208 size_t hdr;
1209 size_t ofs1, ofs2;
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);
1227 talloc_free(pool);
1229 p1_2 = p1;
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 */
1245 talloc_free(p1_2);
1247 p2_2 = p2;
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");
1253 p2 = p2_2;
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 */
1263 talloc_free(p2_2);
1265 talloc_free(root);
1267 return true;
1270 static bool test_pool_nest(void)
1272 void *p1, *p2, *p3;
1273 void *e = talloc_new(NULL);
1275 p1 = talloc_pool(NULL, 1024);
1276 torture_assert("talloc_pool", p1 != NULL, "failed");
1278 p2 = talloc_pool(p1, 500);
1279 torture_assert("talloc_pool", p2 != NULL, "failed");
1281 p3 = talloc_size(p2, 10);
1283 talloc_steal(e, p3);
1285 talloc_free(p2);
1287 talloc_free(p3);
1289 talloc_free(p1);
1291 return true;
1294 struct pooled {
1295 char *s1;
1296 char *s2;
1297 char *s3;
1300 static bool test_pooled_object(void)
1302 struct pooled *p;
1303 const char *s1 = "hello";
1304 const char *s2 = "world";
1305 const char *s3 = "";
1307 p = talloc_pooled_object(NULL, struct pooled, 3,
1308 strlen(s1)+strlen(s2)+strlen(s3)+3);
1310 if (talloc_get_size(p) != sizeof(struct pooled)) {
1311 return false;
1314 p->s1 = talloc_strdup(p, s1);
1316 TALLOC_FREE(p->s1);
1317 p->s1 = talloc_strdup(p, s2);
1318 TALLOC_FREE(p->s1);
1320 p->s1 = talloc_strdup(p, s1);
1321 p->s2 = talloc_strdup(p, s2);
1322 p->s3 = talloc_strdup(p, s3);
1324 TALLOC_FREE(p);
1325 return true;
1328 static bool test_free_ref_null_context(void)
1330 void *p1, *p2, *p3;
1331 int ret;
1333 talloc_disable_null_tracking();
1334 p1 = talloc_new(NULL);
1335 p2 = talloc_new(NULL);
1337 p3 = talloc_reference(p2, p1);
1338 torture_assert("reference", p3 == p1, "failed: reference on null");
1340 ret = talloc_free(p1);
1341 torture_assert("ref free with null parent", ret == 0, "failed: free with null parent");
1342 talloc_free(p2);
1344 talloc_enable_null_tracking_no_autofree();
1345 p1 = talloc_new(NULL);
1346 p2 = talloc_new(NULL);
1348 p3 = talloc_reference(p2, p1);
1349 torture_assert("reference", p3 == p1, "failed: reference on null");
1351 ret = talloc_free(p1);
1352 torture_assert("ref free with null tracked parent", ret == 0, "failed: free with null parent");
1353 talloc_free(p2);
1355 return true;
1358 static bool test_rusty(void)
1360 void *root;
1361 const char *p1;
1363 talloc_enable_null_tracking();
1364 root = talloc_new(NULL);
1365 p1 = talloc_strdup(root, "foo");
1366 talloc_increase_ref_count(p1);
1367 talloc_report_full(root, stdout);
1368 talloc_free(root);
1369 CHECK_BLOCKS("null_context", NULL, 2);
1370 return true;
1373 static bool test_free_children(void)
1375 void *root;
1376 char *p1, *p2;
1377 const char *name, *name2;
1379 talloc_enable_null_tracking();
1380 root = talloc_new(NULL);
1381 p1 = talloc_strdup(root, "foo1");
1382 p2 = talloc_strdup(p1, "foo2");
1384 talloc_set_name(p1, "%s", "testname");
1385 talloc_free_children(p1);
1386 /* check its still a valid talloc ptr */
1387 talloc_get_size(talloc_get_name(p1));
1388 if (strcmp(talloc_get_name(p1), "testname") != 0) {
1389 return false;
1392 talloc_set_name(p1, "%s", "testname");
1393 name = talloc_get_name(p1);
1394 talloc_free_children(p1);
1395 /* check its still a valid talloc ptr */
1396 talloc_get_size(talloc_get_name(p1));
1397 torture_assert("name", name == talloc_get_name(p1), "name ptr changed");
1398 torture_assert("namecheck", strcmp(talloc_get_name(p1), "testname") == 0,
1399 "wrong name");
1400 CHECK_BLOCKS("name1", p1, 2);
1402 /* note that this does not free the old child name */
1403 talloc_set_name_const(p1, "testname2");
1404 name2 = talloc_get_name(p1);
1405 /* but this does */
1406 talloc_free_children(p1);
1407 torture_assert("namecheck", strcmp(talloc_get_name(p1), "testname2") == 0,
1408 "wrong name");
1409 CHECK_BLOCKS("name1", p1, 1);
1411 talloc_report_full(root, stdout);
1412 talloc_free(root);
1413 return true;
1416 static bool test_memlimit(void)
1418 void *root;
1419 char *l1, *l2, *l3, *l4, *l5, *t;
1420 char *pool;
1421 int i;
1423 printf("test: memlimit\n# MEMORY LIMITS\n");
1425 printf("==== talloc_new(NULL)\n");
1426 root = talloc_new(NULL);
1428 talloc_report_full(root, stdout);
1430 printf("==== talloc_size(root, 2048)\n");
1431 l1 = talloc_size(root, 2048);
1432 torture_assert("memlimit", l1 != NULL,
1433 "failed: alloc should not fail due to memory limit\n");
1435 talloc_report_full(root, stdout);
1437 printf("==== talloc_free(l1)\n");
1438 talloc_free(l1);
1440 talloc_report_full(root, stdout);
1442 printf("==== talloc_strdup(root, level 1)\n");
1443 l1 = talloc_strdup(root, "level 1");
1444 torture_assert("memlimit", l1 != NULL,
1445 "failed: alloc should not fail due to memory limit\n");
1447 talloc_report_full(root, stdout);
1449 printf("==== talloc_set_memlimit(l1, 2048)\n");
1450 torture_assert("memlimit", talloc_set_memlimit(l1, 2048) == 0,
1451 "failed: setting memlimit should never fail\n");
1453 talloc_report_full(root, stdout);
1455 printf("==== talloc_size(root, 2048)\n");
1456 l2 = talloc_size(l1, 2048);
1457 torture_assert("memlimit", l2 == NULL,
1458 "failed: alloc should fail due to memory limit\n");
1460 talloc_report_full(root, stdout);
1462 printf("==== talloc_strdup(l1, level 2)\n");
1463 l2 = talloc_strdup(l1, "level 2");
1464 torture_assert("memlimit", l2 != NULL,
1465 "failed: alloc should not fail due to memory limit\n");
1467 talloc_report_full(root, stdout);
1469 printf("==== talloc_free(l2)\n");
1470 talloc_free(l2);
1472 talloc_report_full(root, stdout);
1474 printf("==== talloc_size(NULL, 2048)\n");
1475 l2 = talloc_size(NULL, 2048);
1477 talloc_report_full(root, stdout);
1479 printf("==== talloc_steal(l1, l2)\n");
1480 talloc_steal(l1, l2);
1482 talloc_report_full(root, stdout);
1484 printf("==== talloc_strdup(l2, level 3)\n");
1485 l3 = talloc_strdup(l2, "level 3");
1486 torture_assert("memlimit", l3 == NULL,
1487 "failed: alloc should fail due to memory limit\n");
1489 talloc_report_full(root, stdout);
1491 printf("==== talloc_free(l2)\n");
1492 talloc_free(l2);
1494 talloc_report_full(root, stdout);
1496 printf("==== talloc_strdup(NULL, level 2)\n");
1497 l2 = talloc_strdup(NULL, "level 2");
1498 talloc_steal(l1, l2);
1500 talloc_report_full(root, stdout);
1502 printf("==== talloc_strdup(l2, level 3)\n");
1503 l3 = talloc_strdup(l2, "level 3");
1504 torture_assert("memlimit", l3 != NULL,
1505 "failed: alloc should not fail due to memory limit\n");
1507 talloc_report_full(root, stdout);
1509 printf("==== talloc_set_memlimit(l3, 1024)\n");
1510 torture_assert("memlimit", talloc_set_memlimit(l3, 1024) == 0,
1511 "failed: setting memlimit should never fail\n");
1513 talloc_report_full(root, stdout);
1515 printf("==== talloc_strdup(l3, level 4)\n");
1516 l4 = talloc_strdup(l3, "level 4");
1517 torture_assert("memlimit", l4 != NULL,
1518 "failed: alloc should not fail due to memory limit\n");
1520 talloc_report_full(root, stdout);
1522 printf("==== talloc_set_memlimit(l4, 512)\n");
1523 torture_assert("memlimit", talloc_set_memlimit(l4, 512) == 0,
1524 "failed: setting memlimit should never fail\n");
1526 talloc_report_full(root, stdout);
1528 printf("==== talloc_strdup(l4, level 5)\n");
1529 l5 = talloc_strdup(l4, "level 5");
1530 torture_assert("memlimit", l5 != NULL,
1531 "failed: alloc should not fail due to memory limit\n");
1533 talloc_report_full(root, stdout);
1535 printf("==== talloc_realloc(NULL, l5, char, 600)\n");
1536 t = talloc_realloc(NULL, l5, char, 600);
1537 torture_assert("memlimit", t == NULL,
1538 "failed: alloc should fail due to memory limit\n");
1540 talloc_report_full(root, stdout);
1542 printf("==== talloc_realloc(NULL, l5, char, 5)\n");
1543 l5 = talloc_realloc(NULL, l5, char, 5);
1544 torture_assert("memlimit", l5 != NULL,
1545 "failed: alloc should not fail due to memory limit\n");
1547 talloc_report_full(root, stdout);
1549 printf("==== talloc_strdup(l3, level 4)\n");
1550 l4 = talloc_strdup(l3, "level 4");
1551 torture_assert("memlimit", l4 != NULL,
1552 "failed: alloc should not fail due to memory limit\n");
1554 talloc_report_full(root, stdout);
1556 printf("==== talloc_set_memlimit(l4, 512)\n");
1557 torture_assert("memlimit", talloc_set_memlimit(l4, 512) == 0,
1558 "failed: setting memlimit should never fail\n");
1560 talloc_report_full(root, stdout);
1562 printf("==== talloc_strdup(l4, level 5)\n");
1563 l5 = talloc_strdup(l4, "level 5");
1564 torture_assert("memlimit", l5 != NULL,
1565 "failed: alloc should not fail due to memory limit\n");
1567 talloc_report_full(root, stdout);
1569 printf("==== Make new temp context and steal l5\n");
1570 t = talloc_new(root);
1571 talloc_steal(t, l5);
1573 talloc_report_full(root, stdout);
1575 printf("==== talloc_size(t, 2048)\n");
1576 l1 = talloc_size(t, 2048);
1577 torture_assert("memlimit", l1 != NULL,
1578 "failed: alloc should not fail due to memory limit\n");
1580 talloc_report_full(root, stdout);
1581 talloc_free(root);
1583 /* Test memlimits with pools. */
1584 pool = talloc_pool(NULL, 10*1024);
1585 torture_assert("memlimit", pool != NULL,
1586 "failed: alloc should not fail due to memory limit\n");
1587 talloc_set_memlimit(pool, 10*1024);
1588 for (i = 0; i < 9; i++) {
1589 l1 = talloc_size(pool, 1024);
1590 torture_assert("memlimit", l1 != NULL,
1591 "failed: alloc should not fail due to memory limit\n");
1593 /* The next alloc should fail. */
1594 l2 = talloc_size(pool, 1024);
1595 torture_assert("memlimit", l2 == NULL,
1596 "failed: alloc should fail due to memory limit\n");
1598 /* Moving one of the children shouldn't change the limit,
1599 as it's still inside the pool. */
1600 root = talloc_new(NULL);
1601 talloc_steal(root, l1);
1602 l2 = talloc_size(pool, 1024);
1603 torture_assert("memlimit", l2 == NULL,
1604 "failed: alloc should fail due to memory limit\n");
1606 talloc_free(pool);
1607 talloc_free(root);
1608 printf("success: memlimit\n");
1610 return true;
1613 static void test_reset(void)
1615 talloc_set_log_fn(test_log_stdout);
1616 test_abort_stop();
1617 talloc_disable_null_tracking();
1618 talloc_enable_null_tracking_no_autofree();
1621 bool torture_local_talloc(struct torture_context *tctx)
1623 bool ret = true;
1625 setlinebuf(stdout);
1627 test_reset();
1628 ret &= test_pooled_object();
1629 test_reset();
1630 ret &= test_pool_nest();
1631 test_reset();
1632 ret &= test_ref1();
1633 test_reset();
1634 ret &= test_ref2();
1635 test_reset();
1636 ret &= test_ref3();
1637 test_reset();
1638 ret &= test_ref4();
1639 test_reset();
1640 ret &= test_unlink1();
1641 test_reset();
1642 ret &= test_misc();
1643 test_reset();
1644 ret &= test_realloc();
1645 test_reset();
1646 ret &= test_realloc_child();
1647 test_reset();
1648 ret &= test_steal();
1649 test_reset();
1650 ret &= test_move();
1651 test_reset();
1652 ret &= test_unref_reparent();
1653 test_reset();
1654 ret &= test_realloc_fn();
1655 test_reset();
1656 ret &= test_type();
1657 test_reset();
1658 ret &= test_lifeless();
1659 test_reset();
1660 ret &= test_loop();
1661 test_reset();
1662 ret &= test_free_parent_deny_child();
1663 test_reset();
1664 ret &= test_talloc_ptrtype();
1665 test_reset();
1666 ret &= test_talloc_free_in_destructor();
1667 test_reset();
1668 ret &= test_pool();
1669 test_reset();
1670 ret &= test_pool_steal();
1671 test_reset();
1672 ret &= test_free_ref_null_context();
1673 test_reset();
1674 ret &= test_rusty();
1675 test_reset();
1676 ret &= test_free_children();
1677 test_reset();
1678 ret &= test_memlimit();
1681 if (ret) {
1682 test_reset();
1683 ret &= test_speed();
1685 test_reset();
1686 ret &= test_autofree();
1688 test_reset();
1689 talloc_disable_null_tracking();
1690 return ret;