s3: Pass smbd_server_connection to srv_decrypt_buffer
[Samba/gebeck_regimport.git] / lib / talloc / testsuite.c
blob003d74bf6dc31adafcda2c7bd322aa5dd6bfcc3f
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 #if _SAMBA_BUILD_==3
61 #ifdef malloc
62 #undef malloc
63 #endif
64 #ifdef strdup
65 #undef strdup
66 #endif
67 #endif
69 #define CHECK_SIZE(test, ptr, tsize) do { \
70 if (talloc_total_size(ptr) != (tsize)) { \
71 printf("failed: %s [\n%s: wrong '%s' tree size: got %u expected %u\n]\n", \
72 test, __location__, #ptr, \
73 (unsigned)talloc_total_size(ptr), \
74 (unsigned)tsize); \
75 talloc_report_full(ptr, stdout); \
76 return false; \
77 } \
78 } while (0)
80 #define CHECK_BLOCKS(test, ptr, tblocks) do { \
81 if (talloc_total_blocks(ptr) != (tblocks)) { \
82 printf("failed: %s [\n%s: wrong '%s' tree blocks: got %u expected %u\n]\n", \
83 test, __location__, #ptr, \
84 (unsigned)talloc_total_blocks(ptr), \
85 (unsigned)tblocks); \
86 talloc_report_full(ptr, stdout); \
87 return false; \
88 } \
89 } while (0)
91 #define CHECK_PARENT(test, ptr, parent) do { \
92 if (talloc_parent(ptr) != (parent)) { \
93 printf("failed: %s [\n%s: '%s' has wrong parent: got %p expected %p\n]\n", \
94 test, __location__, #ptr, \
95 talloc_parent(ptr), \
96 (parent)); \
97 talloc_report_full(ptr, stdout); \
98 talloc_report_full(parent, stdout); \
99 talloc_report_full(NULL, stdout); \
100 return false; \
102 } while (0)
104 static unsigned int test_abort_count;
106 #if 0
107 static void test_abort_fn(const char *reason)
109 printf("# test_abort_fn(%s)\n", reason);
110 test_abort_count++;
113 static void test_abort_start(void)
115 test_abort_count = 0;
116 talloc_set_abort_fn(test_abort_fn);
118 #endif
120 static void test_abort_stop(void)
122 test_abort_count = 0;
123 talloc_set_abort_fn(NULL);
126 static void test_log_stdout(const char *message)
128 fprintf(stdout, "%s", message);
132 test references
134 static bool test_ref1(void)
136 void *root, *p1, *p2, *ref, *r1;
138 printf("test: ref1\n# SINGLE REFERENCE FREE\n");
140 root = talloc_named_const(NULL, 0, "root");
141 p1 = talloc_named_const(root, 1, "p1");
142 p2 = talloc_named_const(p1, 1, "p2");
143 talloc_named_const(p1, 1, "x1");
144 talloc_named_const(p1, 2, "x2");
145 talloc_named_const(p1, 3, "x3");
147 r1 = talloc_named_const(root, 1, "r1");
148 ref = talloc_reference(r1, p2);
149 talloc_report_full(root, stderr);
151 CHECK_BLOCKS("ref1", p1, 5);
152 CHECK_BLOCKS("ref1", p2, 1);
153 CHECK_BLOCKS("ref1", r1, 2);
155 fprintf(stderr, "Freeing p2\n");
156 talloc_unlink(r1, p2);
157 talloc_report_full(root, stderr);
159 CHECK_BLOCKS("ref1", p1, 5);
160 CHECK_BLOCKS("ref1", p2, 1);
161 CHECK_BLOCKS("ref1", r1, 1);
163 fprintf(stderr, "Freeing p1\n");
164 talloc_free(p1);
165 talloc_report_full(root, stderr);
167 CHECK_BLOCKS("ref1", r1, 1);
169 fprintf(stderr, "Freeing r1\n");
170 talloc_free(r1);
171 talloc_report_full(NULL, stderr);
173 fprintf(stderr, "Testing NULL\n");
174 if (talloc_reference(root, NULL)) {
175 return false;
178 CHECK_BLOCKS("ref1", root, 1);
180 CHECK_SIZE("ref1", root, 0);
182 talloc_free(root);
183 printf("success: ref1\n");
184 return true;
188 test references
190 static bool test_ref2(void)
192 void *root, *p1, *p2, *ref, *r1;
194 printf("test: ref2\n# DOUBLE REFERENCE FREE\n");
195 root = talloc_named_const(NULL, 0, "root");
196 p1 = talloc_named_const(root, 1, "p1");
197 talloc_named_const(p1, 1, "x1");
198 talloc_named_const(p1, 1, "x2");
199 talloc_named_const(p1, 1, "x3");
200 p2 = talloc_named_const(p1, 1, "p2");
202 r1 = talloc_named_const(root, 1, "r1");
203 ref = talloc_reference(r1, p2);
204 talloc_report_full(root, stderr);
206 CHECK_BLOCKS("ref2", p1, 5);
207 CHECK_BLOCKS("ref2", p2, 1);
208 CHECK_BLOCKS("ref2", r1, 2);
210 fprintf(stderr, "Freeing ref\n");
211 talloc_unlink(r1, ref);
212 talloc_report_full(root, stderr);
214 CHECK_BLOCKS("ref2", p1, 5);
215 CHECK_BLOCKS("ref2", p2, 1);
216 CHECK_BLOCKS("ref2", r1, 1);
218 fprintf(stderr, "Freeing p2\n");
219 talloc_free(p2);
220 talloc_report_full(root, stderr);
222 CHECK_BLOCKS("ref2", p1, 4);
223 CHECK_BLOCKS("ref2", r1, 1);
225 fprintf(stderr, "Freeing p1\n");
226 talloc_free(p1);
227 talloc_report_full(root, stderr);
229 CHECK_BLOCKS("ref2", r1, 1);
231 fprintf(stderr, "Freeing r1\n");
232 talloc_free(r1);
233 talloc_report_full(root, stderr);
235 CHECK_SIZE("ref2", root, 0);
237 talloc_free(root);
238 printf("success: ref2\n");
239 return true;
243 test references
245 static bool test_ref3(void)
247 void *root, *p1, *p2, *ref, *r1;
249 printf("test: ref3\n# PARENT REFERENCE FREE\n");
251 root = talloc_named_const(NULL, 0, "root");
252 p1 = talloc_named_const(root, 1, "p1");
253 p2 = talloc_named_const(root, 1, "p2");
254 r1 = talloc_named_const(p1, 1, "r1");
255 ref = talloc_reference(p2, r1);
256 talloc_report_full(root, stderr);
258 CHECK_BLOCKS("ref3", p1, 2);
259 CHECK_BLOCKS("ref3", p2, 2);
260 CHECK_BLOCKS("ref3", r1, 1);
262 fprintf(stderr, "Freeing p1\n");
263 talloc_free(p1);
264 talloc_report_full(root, stderr);
266 CHECK_BLOCKS("ref3", p2, 2);
267 CHECK_BLOCKS("ref3", r1, 1);
269 fprintf(stderr, "Freeing p2\n");
270 talloc_free(p2);
271 talloc_report_full(root, stderr);
273 CHECK_SIZE("ref3", root, 0);
275 talloc_free(root);
277 printf("success: ref3\n");
278 return true;
282 test references
284 static bool test_ref4(void)
286 void *root, *p1, *p2, *ref, *r1;
288 printf("test: ref4\n# REFERRER REFERENCE FREE\n");
290 root = talloc_named_const(NULL, 0, "root");
291 p1 = talloc_named_const(root, 1, "p1");
292 talloc_named_const(p1, 1, "x1");
293 talloc_named_const(p1, 1, "x2");
294 talloc_named_const(p1, 1, "x3");
295 p2 = talloc_named_const(p1, 1, "p2");
297 r1 = talloc_named_const(root, 1, "r1");
298 ref = talloc_reference(r1, p2);
299 talloc_report_full(root, stderr);
301 CHECK_BLOCKS("ref4", p1, 5);
302 CHECK_BLOCKS("ref4", p2, 1);
303 CHECK_BLOCKS("ref4", r1, 2);
305 fprintf(stderr, "Freeing r1\n");
306 talloc_free(r1);
307 talloc_report_full(root, stderr);
309 CHECK_BLOCKS("ref4", p1, 5);
310 CHECK_BLOCKS("ref4", p2, 1);
312 fprintf(stderr, "Freeing p2\n");
313 talloc_free(p2);
314 talloc_report_full(root, stderr);
316 CHECK_BLOCKS("ref4", p1, 4);
318 fprintf(stderr, "Freeing p1\n");
319 talloc_free(p1);
320 talloc_report_full(root, stderr);
322 CHECK_SIZE("ref4", root, 0);
324 talloc_free(root);
326 printf("success: ref4\n");
327 return true;
332 test references
334 static bool test_unlink1(void)
336 void *root, *p1, *p2, *ref, *r1;
338 printf("test: unlink\n# UNLINK\n");
340 root = talloc_named_const(NULL, 0, "root");
341 p1 = talloc_named_const(root, 1, "p1");
342 talloc_named_const(p1, 1, "x1");
343 talloc_named_const(p1, 1, "x2");
344 talloc_named_const(p1, 1, "x3");
345 p2 = talloc_named_const(p1, 1, "p2");
347 r1 = talloc_named_const(p1, 1, "r1");
348 ref = talloc_reference(r1, p2);
349 talloc_report_full(root, stderr);
351 CHECK_BLOCKS("unlink", p1, 7);
352 CHECK_BLOCKS("unlink", p2, 1);
353 CHECK_BLOCKS("unlink", r1, 2);
355 fprintf(stderr, "Unreferencing r1\n");
356 talloc_unlink(r1, p2);
357 talloc_report_full(root, stderr);
359 CHECK_BLOCKS("unlink", p1, 6);
360 CHECK_BLOCKS("unlink", p2, 1);
361 CHECK_BLOCKS("unlink", r1, 1);
363 fprintf(stderr, "Freeing p1\n");
364 talloc_free(p1);
365 talloc_report_full(root, stderr);
367 CHECK_SIZE("unlink", root, 0);
369 talloc_free(root);
371 printf("success: unlink\n");
372 return true;
375 static int fail_destructor(void *ptr)
377 return -1;
381 miscellaneous tests to try to get a higher test coverage percentage
383 static bool test_misc(void)
385 void *root, *p1;
386 char *p2;
387 double *d;
388 const char *name;
390 printf("test: misc\n# MISCELLANEOUS\n");
392 root = talloc_new(NULL);
394 p1 = talloc_size(root, 0x7fffffff);
395 torture_assert("misc", !p1, "failed: large talloc allowed\n");
397 p1 = talloc_strdup(root, "foo");
398 talloc_increase_ref_count(p1);
399 talloc_increase_ref_count(p1);
400 talloc_increase_ref_count(p1);
401 CHECK_BLOCKS("misc", p1, 1);
402 CHECK_BLOCKS("misc", root, 2);
403 talloc_unlink(NULL, p1);
404 CHECK_BLOCKS("misc", p1, 1);
405 CHECK_BLOCKS("misc", root, 2);
406 talloc_unlink(NULL, p1);
407 CHECK_BLOCKS("misc", p1, 1);
408 CHECK_BLOCKS("misc", root, 2);
409 p2 = talloc_strdup(p1, "foo");
410 torture_assert("misc", talloc_unlink(root, p2) == -1,
411 "failed: talloc_unlink() of non-reference context should return -1\n");
412 torture_assert("misc", talloc_unlink(p1, p2) == 0,
413 "failed: talloc_unlink() of parent should succeed\n");
414 talloc_unlink(NULL, p1);
415 CHECK_BLOCKS("misc", p1, 1);
416 CHECK_BLOCKS("misc", root, 2);
418 name = talloc_set_name(p1, "my name is %s", "foo");
419 torture_assert_str_equal("misc", talloc_get_name(p1), "my name is foo",
420 "failed: wrong name after talloc_set_name(my name is foo)");
421 CHECK_BLOCKS("misc", p1, 2);
422 CHECK_BLOCKS("misc", root, 3);
424 talloc_set_name_const(p1, NULL);
425 torture_assert_str_equal ("misc", talloc_get_name(p1), "UNNAMED",
426 "failed: wrong name after talloc_set_name(NULL)");
427 CHECK_BLOCKS("misc", p1, 2);
428 CHECK_BLOCKS("misc", root, 3);
430 torture_assert("misc", talloc_free(NULL) == -1,
431 "talloc_free(NULL) should give -1\n");
433 talloc_set_destructor(p1, fail_destructor);
434 torture_assert("misc", talloc_free(p1) == -1,
435 "Failed destructor should cause talloc_free to fail\n");
436 talloc_set_destructor(p1, NULL);
438 talloc_report(root, stderr);
441 p2 = (char *)talloc_zero_size(p1, 20);
442 torture_assert("misc", p2[19] == 0, "Failed to give zero memory\n");
443 talloc_free(p2);
445 torture_assert("misc", talloc_strdup(root, NULL) == NULL,
446 "failed: strdup on NULL should give NULL\n");
448 p2 = talloc_strndup(p1, "foo", 2);
449 torture_assert("misc", strcmp("fo", p2) == 0,
450 "strndup doesn't work\n");
451 p2 = talloc_asprintf_append_buffer(p2, "o%c", 'd');
452 torture_assert("misc", strcmp("food", p2) == 0,
453 "talloc_asprintf_append_buffer doesn't work\n");
454 CHECK_BLOCKS("misc", p2, 1);
455 CHECK_BLOCKS("misc", p1, 3);
457 p2 = talloc_asprintf_append_buffer(NULL, "hello %s", "world");
458 torture_assert("misc", strcmp("hello world", p2) == 0,
459 "talloc_asprintf_append_buffer doesn't work\n");
460 CHECK_BLOCKS("misc", p2, 1);
461 CHECK_BLOCKS("misc", p1, 3);
462 talloc_free(p2);
464 d = talloc_array(p1, double, 0x20000000);
465 torture_assert("misc", !d, "failed: integer overflow not detected\n");
467 d = talloc_realloc(p1, d, double, 0x20000000);
468 torture_assert("misc", !d, "failed: integer overflow not detected\n");
470 talloc_free(p1);
471 CHECK_BLOCKS("misc", root, 1);
473 p1 = talloc_named(root, 100, "%d bytes", 100);
474 CHECK_BLOCKS("misc", p1, 2);
475 CHECK_BLOCKS("misc", root, 3);
476 talloc_unlink(root, p1);
478 p1 = talloc_init("%d bytes", 200);
479 p2 = talloc_asprintf(p1, "my test '%s'", "string");
480 torture_assert_str_equal("misc", p2, "my test 'string'",
481 "failed: talloc_asprintf(\"my test '%%s'\", \"string\") gave: \"%s\"");
482 CHECK_BLOCKS("misc", p1, 3);
483 CHECK_SIZE("misc", p2, 17);
484 CHECK_BLOCKS("misc", root, 1);
485 talloc_unlink(NULL, p1);
487 p1 = talloc_named_const(root, 10, "p1");
488 p2 = (char *)talloc_named_const(root, 20, "p2");
489 (void)talloc_reference(p1, p2);
490 talloc_report_full(root, stderr);
491 talloc_unlink(root, p2);
492 talloc_report_full(root, stderr);
493 CHECK_BLOCKS("misc", p2, 1);
494 CHECK_BLOCKS("misc", p1, 2);
495 CHECK_BLOCKS("misc", root, 3);
496 talloc_unlink(p1, p2);
497 talloc_unlink(root, p1);
499 p1 = talloc_named_const(root, 10, "p1");
500 p2 = (char *)talloc_named_const(root, 20, "p2");
501 (void)talloc_reference(NULL, p2);
502 talloc_report_full(root, stderr);
503 talloc_unlink(root, p2);
504 talloc_report_full(root, stderr);
505 CHECK_BLOCKS("misc", p2, 1);
506 CHECK_BLOCKS("misc", p1, 1);
507 CHECK_BLOCKS("misc", root, 2);
508 talloc_unlink(NULL, p2);
509 talloc_unlink(root, p1);
511 /* Test that talloc_unlink is a no-op */
513 torture_assert("misc", talloc_unlink(root, NULL) == -1,
514 "failed: talloc_unlink(root, NULL) == -1\n");
516 talloc_report(root, stderr);
517 talloc_report(NULL, stderr);
519 CHECK_SIZE("misc", root, 0);
521 talloc_free(root);
523 CHECK_SIZE("misc", NULL, 0);
525 talloc_enable_null_tracking_no_autofree();
526 talloc_enable_leak_report();
527 talloc_enable_leak_report_full();
529 printf("success: misc\n");
531 return true;
536 test realloc
538 static bool test_realloc(void)
540 void *root, *p1, *p2;
542 printf("test: realloc\n# REALLOC\n");
544 root = talloc_new(NULL);
546 p1 = talloc_size(root, 10);
547 CHECK_SIZE("realloc", p1, 10);
549 p1 = talloc_realloc_size(NULL, p1, 20);
550 CHECK_SIZE("realloc", p1, 20);
552 talloc_new(p1);
554 p2 = talloc_realloc_size(p1, NULL, 30);
556 talloc_new(p1);
558 p2 = talloc_realloc_size(p1, p2, 40);
560 CHECK_SIZE("realloc", p2, 40);
561 CHECK_SIZE("realloc", root, 60);
562 CHECK_BLOCKS("realloc", p1, 4);
564 p1 = talloc_realloc_size(NULL, p1, 20);
565 CHECK_SIZE("realloc", p1, 60);
567 talloc_increase_ref_count(p2);
568 torture_assert("realloc", talloc_realloc_size(NULL, p2, 5) == NULL,
569 "failed: talloc_realloc() on a referenced pointer should fail\n");
570 CHECK_BLOCKS("realloc", p1, 4);
572 talloc_realloc_size(NULL, p2, 0);
573 talloc_realloc_size(NULL, p2, 0);
574 CHECK_BLOCKS("realloc", p1, 4);
575 talloc_realloc_size(p1, p2, 0);
576 CHECK_BLOCKS("realloc", p1, 3);
578 torture_assert("realloc", talloc_realloc_size(NULL, p1, 0x7fffffff) == NULL,
579 "failed: oversize talloc should fail\n");
581 talloc_realloc_size(NULL, p1, 0);
582 CHECK_BLOCKS("realloc", root, 4);
583 talloc_realloc_size(root, p1, 0);
584 CHECK_BLOCKS("realloc", root, 1);
586 CHECK_SIZE("realloc", root, 0);
588 talloc_free(root);
590 printf("success: realloc\n");
592 return true;
596 test realloc with a child
598 static bool test_realloc_child(void)
600 void *root;
601 struct el2 {
602 const char *name;
603 } *el2;
604 struct el1 {
605 int count;
606 struct el2 **list, **list2, **list3;
607 } *el1;
609 printf("test: REALLOC WITH CHILD\n");
611 root = talloc_new(NULL);
613 el1 = talloc(root, struct el1);
614 el1->list = talloc(el1, struct el2 *);
615 el1->list[0] = talloc(el1->list, struct el2);
616 el1->list[0]->name = talloc_strdup(el1->list[0], "testing");
618 el1->list2 = talloc(el1, struct el2 *);
619 el1->list2[0] = talloc(el1->list2, struct el2);
620 el1->list2[0]->name = talloc_strdup(el1->list2[0], "testing2");
622 el1->list3 = talloc(el1, struct el2 *);
623 el1->list3[0] = talloc(el1->list3, struct el2);
624 el1->list3[0]->name = talloc_strdup(el1->list3[0], "testing2");
626 el2 = talloc(el1->list, struct el2);
627 el2 = talloc(el1->list2, struct el2);
628 el2 = talloc(el1->list3, struct el2);
630 el1->list = talloc_realloc(el1, el1->list, struct el2 *, 100);
631 el1->list2 = talloc_realloc(el1, el1->list2, struct el2 *, 200);
632 el1->list3 = talloc_realloc(el1, el1->list3, struct el2 *, 300);
634 talloc_free(root);
636 printf("success: REALLOC WITH CHILD\n");
637 return true;
641 test type checking
643 static bool test_type(void)
645 void *root;
646 struct el1 {
647 int count;
649 struct el2 {
650 int count;
652 struct el1 *el1;
654 printf("test: type\n# talloc type checking\n");
656 root = talloc_new(NULL);
658 el1 = talloc(root, struct el1);
660 el1->count = 1;
662 torture_assert("type", talloc_get_type(el1, struct el1) == el1,
663 "type check failed on el1\n");
664 torture_assert("type", talloc_get_type(el1, struct el2) == NULL,
665 "type check failed on el1 with el2\n");
666 talloc_set_type(el1, struct el2);
667 torture_assert("type", talloc_get_type(el1, struct el2) == (struct el2 *)el1,
668 "type set failed on el1 with el2\n");
670 talloc_free(root);
672 printf("success: type\n");
673 return true;
677 test steal
679 static bool test_steal(void)
681 void *root, *p1, *p2;
683 printf("test: steal\n# STEAL\n");
685 root = talloc_new(NULL);
687 p1 = talloc_array(root, char, 10);
688 CHECK_SIZE("steal", p1, 10);
690 p2 = talloc_realloc(root, NULL, char, 20);
691 CHECK_SIZE("steal", p1, 10);
692 CHECK_SIZE("steal", root, 30);
694 torture_assert("steal", talloc_steal(p1, NULL) == NULL,
695 "failed: stealing NULL should give NULL\n");
697 torture_assert("steal", talloc_steal(p1, p1) == p1,
698 "failed: stealing to ourselves is a nop\n");
699 CHECK_BLOCKS("steal", root, 3);
700 CHECK_SIZE("steal", root, 30);
702 talloc_steal(NULL, p1);
703 talloc_steal(NULL, p2);
704 CHECK_BLOCKS("steal", root, 1);
705 CHECK_SIZE("steal", root, 0);
707 talloc_free(p1);
708 talloc_steal(root, p2);
709 CHECK_BLOCKS("steal", root, 2);
710 CHECK_SIZE("steal", root, 20);
712 talloc_free(p2);
714 CHECK_BLOCKS("steal", root, 1);
715 CHECK_SIZE("steal", root, 0);
717 talloc_free(root);
719 p1 = talloc_size(NULL, 3);
720 talloc_report_full(NULL, stderr);
721 CHECK_SIZE("steal", NULL, 3);
722 talloc_free(p1);
724 printf("success: steal\n");
725 return true;
729 test move
731 static bool test_move(void)
733 void *root;
734 struct t_move {
735 char *p;
736 int *x;
737 } *t1, *t2;
739 printf("test: move\n# MOVE\n");
741 root = talloc_new(NULL);
743 t1 = talloc(root, struct t_move);
744 t2 = talloc(root, struct t_move);
745 t1->p = talloc_strdup(t1, "foo");
746 t1->x = talloc(t1, int);
747 *t1->x = 42;
749 t2->p = talloc_move(t2, &t1->p);
750 t2->x = talloc_move(t2, &t1->x);
751 torture_assert("move", t1->p == NULL && t1->x == NULL &&
752 strcmp(t2->p, "foo") == 0 && *t2->x == 42,
753 "talloc move failed");
755 talloc_free(root);
757 printf("success: move\n");
759 return true;
763 test talloc_realloc_fn
765 static bool test_realloc_fn(void)
767 void *root, *p1;
769 printf("test: realloc_fn\n# talloc_realloc_fn\n");
771 root = talloc_new(NULL);
773 p1 = talloc_realloc_fn(root, NULL, 10);
774 CHECK_BLOCKS("realloc_fn", root, 2);
775 CHECK_SIZE("realloc_fn", root, 10);
776 p1 = talloc_realloc_fn(root, p1, 20);
777 CHECK_BLOCKS("realloc_fn", root, 2);
778 CHECK_SIZE("realloc_fn", root, 20);
779 p1 = talloc_realloc_fn(root, p1, 0);
780 CHECK_BLOCKS("realloc_fn", root, 1);
781 CHECK_SIZE("realloc_fn", root, 0);
783 talloc_free(root);
785 printf("success: realloc_fn\n");
786 return true;
790 static bool test_unref_reparent(void)
792 void *root, *p1, *p2, *c1;
794 printf("test: unref_reparent\n# UNREFERENCE AFTER PARENT FREED\n");
796 root = talloc_named_const(NULL, 0, "root");
797 p1 = talloc_named_const(root, 1, "orig parent");
798 p2 = talloc_named_const(root, 1, "parent by reference");
800 c1 = talloc_named_const(p1, 1, "child");
801 talloc_reference(p2, c1);
803 CHECK_PARENT("unref_reparent", c1, p1);
805 talloc_free(p1);
807 CHECK_PARENT("unref_reparent", c1, p2);
809 talloc_unlink(p2, c1);
811 CHECK_SIZE("unref_reparent", root, 1);
813 talloc_free(p2);
814 talloc_free(root);
816 printf("success: unref_reparent\n");
817 return true;
821 measure the speed of talloc versus malloc
823 static bool test_speed(void)
825 void *ctx = talloc_new(NULL);
826 unsigned count;
827 const int loop = 1000;
828 int i;
829 struct timeval tv;
831 printf("test: speed\n# TALLOC VS MALLOC SPEED\n");
833 tv = timeval_current();
834 count = 0;
835 do {
836 void *p1, *p2, *p3;
837 for (i=0;i<loop;i++) {
838 p1 = talloc_size(ctx, loop % 100);
839 p2 = talloc_strdup(p1, "foo bar");
840 p3 = talloc_size(p1, 300);
841 talloc_free(p1);
843 count += 3 * loop;
844 } while (timeval_elapsed(&tv) < 5.0);
846 fprintf(stderr, "talloc: %.0f ops/sec\n", count/timeval_elapsed(&tv));
848 talloc_free(ctx);
850 ctx = talloc_pool(NULL, 1024);
852 tv = timeval_current();
853 count = 0;
854 do {
855 void *p1, *p2, *p3;
856 for (i=0;i<loop;i++) {
857 p1 = talloc_size(ctx, loop % 100);
858 p2 = talloc_strdup(p1, "foo bar");
859 p3 = talloc_size(p1, 300);
860 talloc_free_children(ctx);
862 count += 3 * loop;
863 } while (timeval_elapsed(&tv) < 5.0);
865 talloc_free(ctx);
867 fprintf(stderr, "talloc_pool: %.0f ops/sec\n", count/timeval_elapsed(&tv));
869 tv = timeval_current();
870 count = 0;
871 do {
872 void *p1, *p2, *p3;
873 for (i=0;i<loop;i++) {
874 p1 = malloc(loop % 100);
875 p2 = strdup("foo bar");
876 p3 = malloc(300);
877 free(p1);
878 free(p2);
879 free(p3);
881 count += 3 * loop;
882 } while (timeval_elapsed(&tv) < 5.0);
883 fprintf(stderr, "malloc: %.0f ops/sec\n", count/timeval_elapsed(&tv));
885 printf("success: speed\n");
887 return true;
890 static bool test_lifeless(void)
892 void *top = talloc_new(NULL);
893 char *parent, *child;
894 void *child_owner = talloc_new(NULL);
896 printf("test: lifeless\n# TALLOC_UNLINK LOOP\n");
898 parent = talloc_strdup(top, "parent");
899 child = talloc_strdup(parent, "child");
900 (void)talloc_reference(child, parent);
901 (void)talloc_reference(child_owner, child);
902 talloc_report_full(top, stderr);
903 talloc_unlink(top, parent);
904 talloc_unlink(top, child);
905 talloc_report_full(top, stderr);
906 talloc_free(top);
907 talloc_free(child_owner);
908 talloc_free(child);
910 printf("success: lifeless\n");
911 return true;
914 static int loop_destructor_count;
916 static int test_loop_destructor(char *ptr)
918 loop_destructor_count++;
919 return 0;
922 static bool test_loop(void)
924 void *top = talloc_new(NULL);
925 char *parent;
926 struct req1 {
927 char *req2, *req3;
928 } *req1;
930 printf("test: loop\n# TALLOC LOOP DESTRUCTION\n");
932 parent = talloc_strdup(top, "parent");
933 req1 = talloc(parent, struct req1);
934 req1->req2 = talloc_strdup(req1, "req2");
935 talloc_set_destructor(req1->req2, test_loop_destructor);
936 req1->req3 = talloc_strdup(req1, "req3");
937 (void)talloc_reference(req1->req3, req1);
938 talloc_report_full(top, stderr);
939 talloc_free(parent);
940 talloc_report_full(top, stderr);
941 talloc_report_full(NULL, stderr);
942 talloc_free(top);
944 torture_assert("loop", loop_destructor_count == 1,
945 "FAILED TO FIRE LOOP DESTRUCTOR\n");
946 loop_destructor_count = 0;
948 printf("success: loop\n");
949 return true;
952 static int fail_destructor_str(char *ptr)
954 return -1;
957 static bool test_free_parent_deny_child(void)
959 void *top = talloc_new(NULL);
960 char *level1;
961 char *level2;
962 char *level3;
964 printf("test: free_parent_deny_child\n# TALLOC FREE PARENT DENY CHILD\n");
966 level1 = talloc_strdup(top, "level1");
967 level2 = talloc_strdup(level1, "level2");
968 level3 = talloc_strdup(level2, "level3");
970 talloc_set_destructor(level3, fail_destructor_str);
971 talloc_free(level1);
972 talloc_set_destructor(level3, NULL);
974 CHECK_PARENT("free_parent_deny_child", level3, top);
976 talloc_free(top);
978 printf("success: free_parent_deny_child\n");
979 return true;
982 static bool test_talloc_ptrtype(void)
984 void *top = talloc_new(NULL);
985 struct struct1 {
986 int foo;
987 int bar;
988 } *s1, *s2, **s3, ***s4;
989 const char *location1;
990 const char *location2;
991 const char *location3;
992 const char *location4;
994 printf("test: ptrtype\n# TALLOC PTRTYPE\n");
996 s1 = talloc_ptrtype(top, s1);location1 = __location__;
998 if (talloc_get_size(s1) != sizeof(struct struct1)) {
999 printf("failure: ptrtype [\n"
1000 "talloc_ptrtype() allocated the wrong size %lu (should be %lu)\n"
1001 "]\n", (unsigned long)talloc_get_size(s1),
1002 (unsigned long)sizeof(struct struct1));
1003 return false;
1006 if (strcmp(location1, talloc_get_name(s1)) != 0) {
1007 printf("failure: ptrtype [\n"
1008 "talloc_ptrtype() sets the wrong name '%s' (should be '%s')\n]\n",
1009 talloc_get_name(s1), location1);
1010 return false;
1013 s2 = talloc_array_ptrtype(top, s2, 10);location2 = __location__;
1015 if (talloc_get_size(s2) != (sizeof(struct struct1) * 10)) {
1016 printf("failure: ptrtype [\n"
1017 "talloc_array_ptrtype() allocated the wrong size "
1018 "%lu (should be %lu)\n]\n",
1019 (unsigned long)talloc_get_size(s2),
1020 (unsigned long)(sizeof(struct struct1)*10));
1021 return false;
1024 if (strcmp(location2, talloc_get_name(s2)) != 0) {
1025 printf("failure: ptrtype [\n"
1026 "talloc_array_ptrtype() sets the wrong name '%s' (should be '%s')\n]\n",
1027 talloc_get_name(s2), location2);
1028 return false;
1031 s3 = talloc_array_ptrtype(top, s3, 10);location3 = __location__;
1033 if (talloc_get_size(s3) != (sizeof(struct struct1 *) * 10)) {
1034 printf("failure: ptrtype [\n"
1035 "talloc_array_ptrtype() allocated the wrong size "
1036 "%lu (should be %lu)\n]\n",
1037 (unsigned long)talloc_get_size(s3),
1038 (unsigned long)(sizeof(struct struct1 *)*10));
1039 return false;
1042 torture_assert_str_equal("ptrtype", location3, talloc_get_name(s3),
1043 "talloc_array_ptrtype() sets the wrong name");
1045 s4 = talloc_array_ptrtype(top, s4, 10);location4 = __location__;
1047 if (talloc_get_size(s4) != (sizeof(struct struct1 **) * 10)) {
1048 printf("failure: ptrtype [\n"
1049 "talloc_array_ptrtype() allocated the wrong size "
1050 "%lu (should be %lu)\n]\n",
1051 (unsigned long)talloc_get_size(s4),
1052 (unsigned long)(sizeof(struct struct1 **)*10));
1053 return false;
1056 torture_assert_str_equal("ptrtype", location4, talloc_get_name(s4),
1057 "talloc_array_ptrtype() sets the wrong name");
1059 talloc_free(top);
1061 printf("success: ptrtype\n");
1062 return true;
1065 static int _test_talloc_free_in_destructor(void **ptr)
1067 talloc_free(*ptr);
1068 return 0;
1071 static bool test_talloc_free_in_destructor(void)
1073 void *level0;
1074 void *level1;
1075 void *level2;
1076 void *level3;
1077 void *level4;
1078 void **level5;
1080 printf("test: free_in_destructor\n# TALLOC FREE IN DESTRUCTOR\n");
1082 level0 = talloc_new(NULL);
1083 level1 = talloc_new(level0);
1084 level2 = talloc_new(level1);
1085 level3 = talloc_new(level2);
1086 level4 = talloc_new(level3);
1087 level5 = talloc(level4, void *);
1089 *level5 = level3;
1090 (void)talloc_reference(level0, level3);
1091 (void)talloc_reference(level3, level3);
1092 (void)talloc_reference(level5, level3);
1094 talloc_set_destructor(level5, _test_talloc_free_in_destructor);
1096 talloc_free(level1);
1098 talloc_free(level0);
1100 printf("success: free_in_destructor\n");
1101 return true;
1104 static bool test_autofree(void)
1106 #if _SAMBA_BUILD_ < 4
1107 /* autofree test would kill smbtorture */
1108 void *p;
1109 printf("test: autofree\n# TALLOC AUTOFREE CONTEXT\n");
1111 p = talloc_autofree_context();
1112 talloc_free(p);
1114 p = talloc_autofree_context();
1115 talloc_free(p);
1117 printf("success: autofree\n");
1118 #endif
1119 return true;
1122 static bool test_pool(void)
1124 void *pool;
1125 void *p1, *p2, *p3, *p4;
1126 void *p2_2;
1128 pool = talloc_pool(NULL, 1024);
1130 p1 = talloc_size(pool, 80);
1131 memset(p1, 0x11, talloc_get_size(p1));
1132 p2 = talloc_size(pool, 20);
1133 memset(p2, 0x11, talloc_get_size(p2));
1134 p3 = talloc_size(p1, 50);
1135 memset(p3, 0x11, talloc_get_size(p3));
1136 p4 = talloc_size(p3, 1000);
1137 memset(p4, 0x11, talloc_get_size(p4));
1139 #if 1 /* this relies on ALWAYS_REALLOC == 0 in talloc.c */
1140 p2_2 = talloc_realloc_size(pool, p2, 20+1);
1141 torture_assert("pool realloc 20+1", p2_2 == p2, "failed: pointer changed");
1142 memset(p2, 0x11, talloc_get_size(p2));
1143 p2_2 = talloc_realloc_size(pool, p2, 20-1);
1144 torture_assert("pool realloc 20-1", p2_2 == p2, "failed: pointer changed");
1145 memset(p2, 0x11, talloc_get_size(p2));
1146 p2_2 = talloc_realloc_size(pool, p2, 20-1);
1147 torture_assert("pool realloc 20-1", p2_2 == p2, "failed: pointer changed");
1148 memset(p2, 0x11, talloc_get_size(p2));
1150 talloc_free(p3);
1152 /* this should reclaim the memory of p4 and p3 */
1153 p2_2 = talloc_realloc_size(pool, p2, 400);
1154 torture_assert("pool realloc 400", p2_2 == p2, "failed: pointer changed");
1155 memset(p2, 0x11, talloc_get_size(p2));
1157 talloc_free(p1);
1159 /* this should reclaim the memory of p1 */
1160 p2_2 = talloc_realloc_size(pool, p2, 800);
1161 torture_assert("pool realloc 800", p2_2 == p1, "failed: pointer not changed");
1162 p2 = p2_2;
1163 memset(p2, 0x11, talloc_get_size(p2));
1165 /* this should do a malloc */
1166 p2_2 = talloc_realloc_size(pool, p2, 1800);
1167 torture_assert("pool realloc 1800", p2_2 != p2, "failed: pointer not changed");
1168 p2 = p2_2;
1169 memset(p2, 0x11, talloc_get_size(p2));
1171 /* this should reclaim the memory from the pool */
1172 p3 = talloc_size(pool, 80);
1173 torture_assert("pool alloc 80", p3 == p1, "failed: pointer changed");
1174 memset(p3, 0x11, talloc_get_size(p3));
1176 talloc_free(p2);
1177 talloc_free(p3);
1179 p1 = talloc_size(pool, 80);
1180 memset(p1, 0x11, talloc_get_size(p1));
1181 p2 = talloc_size(pool, 20);
1182 memset(p2, 0x11, talloc_get_size(p2));
1184 talloc_free(p1);
1186 p2_2 = talloc_realloc_size(pool, p2, 20-1);
1187 torture_assert("pool realloc 20-1", p2_2 == p2, "failed: pointer changed");
1188 memset(p2, 0x11, talloc_get_size(p2));
1189 p2_2 = talloc_realloc_size(pool, p2, 20-1);
1190 torture_assert("pool realloc 20-1", p2_2 == p2, "failed: pointer changed");
1191 memset(p2, 0x11, talloc_get_size(p2));
1193 /* this should do a malloc */
1194 p2_2 = talloc_realloc_size(pool, p2, 1800);
1195 torture_assert("pool realloc 1800", p2_2 != p2, "failed: pointer not changed");
1196 p2 = p2_2;
1197 memset(p2, 0x11, talloc_get_size(p2));
1199 /* this should reclaim the memory from the pool */
1200 p3 = talloc_size(pool, 800);
1201 torture_assert("pool alloc 800", p3 == p1, "failed: pointer changed");
1202 memset(p3, 0x11, talloc_get_size(p3));
1204 #endif /* this relies on ALWAYS_REALLOC == 0 in talloc.c */
1206 talloc_free(pool);
1208 return true;
1211 static bool test_pool_steal(void)
1213 void *root;
1214 void *pool;
1215 void *p1, *p2;
1216 void *p1_2, *p2_2;
1217 size_t hdr;
1218 size_t ofs1, ofs2;
1220 root = talloc_new(NULL);
1221 pool = talloc_pool(root, 1024);
1223 p1 = talloc_size(pool, 4 * 16);
1224 torture_assert("pool allocate 4 * 16", p1 != NULL, "failed ");
1225 memset(p1, 0x11, talloc_get_size(p1));
1226 p2 = talloc_size(pool, 4 * 16);
1227 torture_assert("pool allocate 4 * 16", p2 > p1, "failed: !(p2 > p1) ");
1228 memset(p2, 0x11, talloc_get_size(p2));
1230 ofs1 = PTR_DIFF(p2, p1);
1231 hdr = ofs1 - talloc_get_size(p1);
1233 talloc_steal(root, p1);
1234 talloc_steal(root, p2);
1236 talloc_free(pool);
1238 p1_2 = p1;
1240 #if 1 /* this relies on ALWAYS_REALLOC == 0 in talloc.c */
1241 p1_2 = talloc_realloc_size(root, p1, 5 * 16);
1242 torture_assert("pool realloc 5 * 16", p1_2 > p2, "failed: pointer not changed");
1243 memset(p1_2, 0x11, talloc_get_size(p1_2));
1244 ofs1 = PTR_DIFF(p1_2, p2);
1245 ofs2 = talloc_get_size(p2) + hdr;
1247 torture_assert("pool realloc ", ofs1 == ofs2, "failed: pointer offset unexpected");
1249 p2_2 = talloc_realloc_size(root, p2, 3 * 16);
1250 torture_assert("pool realloc 5 * 16", p2_2 == p2, "failed: pointer changed");
1251 memset(p2_2, 0x11, talloc_get_size(p2_2));
1252 #endif /* this relies on ALWAYS_REALLOC == 0 in talloc.c */
1254 talloc_free(p1_2);
1256 p2_2 = p2;
1258 #if 1 /* this relies on ALWAYS_REALLOC == 0 in talloc.c */
1259 /* now we should reclaim the full pool */
1260 p2_2 = talloc_realloc_size(root, p2, 8 * 16);
1261 torture_assert("pool realloc 8 * 16", p2_2 == p1, "failed: pointer not expected");
1262 p2 = p2_2;
1263 memset(p2_2, 0x11, talloc_get_size(p2_2));
1265 /* now we malloc and free the full pool space */
1266 p2_2 = talloc_realloc_size(root, p2, 2 * 1024);
1267 torture_assert("pool realloc 2 * 1024", p2_2 != p1, "failed: pointer not expected");
1268 memset(p2_2, 0x11, talloc_get_size(p2_2));
1270 #endif /* this relies on ALWAYS_REALLOC == 0 in talloc.c */
1272 talloc_free(p2_2);
1274 talloc_free(root);
1276 return true;
1279 static bool test_free_ref_null_context(void)
1281 void *p1, *p2, *p3;
1282 int ret;
1284 talloc_disable_null_tracking();
1285 p1 = talloc_new(NULL);
1286 p2 = talloc_new(NULL);
1288 p3 = talloc_reference(p2, p1);
1289 torture_assert("reference", p3 == p1, "failed: reference on null");
1291 ret = talloc_free(p1);
1292 torture_assert("ref free with null parent", ret == 0, "failed: free with null parent");
1293 talloc_free(p2);
1295 talloc_enable_null_tracking_no_autofree();
1296 p1 = talloc_new(NULL);
1297 p2 = talloc_new(NULL);
1299 p3 = talloc_reference(p2, p1);
1300 torture_assert("reference", p3 == p1, "failed: reference on null");
1302 ret = talloc_free(p1);
1303 torture_assert("ref free with null tracked parent", ret == 0, "failed: free with null parent");
1304 talloc_free(p2);
1306 return true;
1309 static bool test_rusty(void)
1311 void *root;
1312 const char *p1;
1314 talloc_enable_null_tracking();
1315 root = talloc_new(NULL);
1316 p1 = talloc_strdup(root, "foo");
1317 talloc_increase_ref_count(p1);
1318 talloc_report_full(root, stdout);
1319 talloc_free(root);
1320 CHECK_BLOCKS("null_context", NULL, 2);
1321 return true;
1324 static bool test_free_children(void)
1326 void *root;
1327 const char *p1, *p2, *name, *name2;
1329 talloc_enable_null_tracking();
1330 root = talloc_new(NULL);
1331 p1 = talloc_strdup(root, "foo1");
1332 p2 = talloc_strdup(p1, "foo2");
1334 talloc_set_name(p1, "%s", "testname");
1335 talloc_free_children(p1);
1336 /* check its still a valid talloc ptr */
1337 talloc_get_size(talloc_get_name(p1));
1338 if (strcmp(talloc_get_name(p1), "testname") != 0) {
1339 return false;
1342 talloc_set_name(p1, "%s", "testname");
1343 name = talloc_get_name(p1);
1344 talloc_free_children(p1);
1345 /* check its still a valid talloc ptr */
1346 talloc_get_size(talloc_get_name(p1));
1347 torture_assert("name", name == talloc_get_name(p1), "name ptr changed");
1348 torture_assert("namecheck", strcmp(talloc_get_name(p1), "testname") == 0,
1349 "wrong name");
1350 CHECK_BLOCKS("name1", p1, 2);
1352 /* note that this does not free the old child name */
1353 talloc_set_name_const(p1, "testname2");
1354 name2 = talloc_get_name(p1);
1355 /* but this does */
1356 talloc_free_children(p1);
1357 torture_assert("namecheck", strcmp(talloc_get_name(p1), "testname2") == 0,
1358 "wrong name");
1359 CHECK_BLOCKS("name1", p1, 1);
1361 talloc_report_full(root, stdout);
1362 talloc_free(root);
1363 return true;
1367 static void test_reset(void)
1369 talloc_set_log_fn(test_log_stdout);
1370 test_abort_stop();
1371 talloc_disable_null_tracking();
1372 talloc_enable_null_tracking_no_autofree();
1375 bool torture_local_talloc(struct torture_context *tctx)
1377 bool ret = true;
1379 setlinebuf(stdout);
1381 test_reset();
1382 ret &= test_ref1();
1383 test_reset();
1384 ret &= test_ref2();
1385 test_reset();
1386 ret &= test_ref3();
1387 test_reset();
1388 ret &= test_ref4();
1389 test_reset();
1390 ret &= test_unlink1();
1391 test_reset();
1392 ret &= test_misc();
1393 test_reset();
1394 ret &= test_realloc();
1395 test_reset();
1396 ret &= test_realloc_child();
1397 test_reset();
1398 ret &= test_steal();
1399 test_reset();
1400 ret &= test_move();
1401 test_reset();
1402 ret &= test_unref_reparent();
1403 test_reset();
1404 ret &= test_realloc_fn();
1405 test_reset();
1406 ret &= test_type();
1407 test_reset();
1408 ret &= test_lifeless();
1409 test_reset();
1410 ret &= test_loop();
1411 test_reset();
1412 ret &= test_free_parent_deny_child();
1413 test_reset();
1414 ret &= test_talloc_ptrtype();
1415 test_reset();
1416 ret &= test_talloc_free_in_destructor();
1417 test_reset();
1418 ret &= test_pool();
1419 test_reset();
1420 ret &= test_pool_steal();
1421 test_reset();
1422 ret &= test_free_ref_null_context();
1423 test_reset();
1424 ret &= test_rusty();
1425 test_reset();
1426 ret &= test_free_children();
1428 if (ret) {
1429 test_reset();
1430 ret &= test_speed();
1432 test_reset();
1433 ret &= test_autofree();
1435 test_reset();
1436 talloc_disable_null_tracking();
1437 return ret;