2
Unix SMB/CIFS implementation.
4
local testing of talloc routines.
6
Copyright (C) Andrew Tridgell 2004
8
** NOTE! The following LGPL license applies to the talloc
9
** library. This does NOT imply that all of Samba is released
12
This library is free software; you can redistribute it and/or
13
modify it under the terms of the GNU Lesser General Public
14
License as published by the Free Software Foundation; either
15
version 3 of the License, or (at your option) any later version.
17
This library is distributed in the hope that it will be useful,
18
but WITHOUT ANY WARRANTY; without even the implied warranty of
19
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20
Lesser General Public License for more details.
22
You should have received a copy of the GNU Lesser General Public
23
License along with this library; if not, see <http://www.gnu.org/licenses/>.
27
#include "system/time.h"
30
static struct timeval timeval_current(void)
33
gettimeofday(&tv, NULL);
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); \
50
#define torture_assert_str_equal(test, arg1, arg2, desc) \
51
if (strcmp(arg1, arg2)) { \
52
printf("failure: %s [\n%s: Expected %s, got %s: %s\n]\n", \
53
test, __location__, arg1, arg2, desc); \
66
#define CHECK_SIZE(test, ptr, tsize) do { \
67
if (talloc_total_size(ptr) != (tsize)) { \
68
printf("failed: %s [\nwrong '%s' tree size: got %u expected %u\n]\n", \
70
(unsigned)talloc_total_size(ptr), \
72
talloc_report_full(ptr, stdout); \
77
#define CHECK_BLOCKS(test, ptr, tblocks) do { \
78
if (talloc_total_blocks(ptr) != (tblocks)) { \
79
printf("failed: %s [\nwrong '%s' tree blocks: got %u expected %u\n]\n", \
81
(unsigned)talloc_total_blocks(ptr), \
83
talloc_report_full(ptr, stdout); \
88
#define CHECK_PARENT(test, ptr, parent) do { \
89
if (talloc_parent(ptr) != (parent)) { \
90
printf("failed: %s [\n'%s' has wrong parent: got %p expected %p\n]\n", \
94
talloc_report_full(ptr, stdout); \
95
talloc_report_full(parent, stdout); \
96
talloc_report_full(NULL, stdout); \
105
static bool test_ref1(void)
107
void *root, *p1, *p2, *ref, *r1;
109
printf("test: ref1 [\nSINGLE REFERENCE FREE\n]\n");
111
root = talloc_named_const(NULL, 0, "root");
112
p1 = talloc_named_const(root, 1, "p1");
113
p2 = talloc_named_const(p1, 1, "p2");
114
talloc_named_const(p1, 1, "x1");
115
talloc_named_const(p1, 2, "x2");
116
talloc_named_const(p1, 3, "x3");
118
r1 = talloc_named_const(root, 1, "r1");
119
ref = talloc_reference(r1, p2);
120
talloc_report_full(root, stderr);
122
CHECK_BLOCKS("ref1", p1, 5);
123
CHECK_BLOCKS("ref1", p2, 1);
124
CHECK_BLOCKS("ref1", r1, 2);
126
fprintf(stderr, "Freeing p2\n");
128
talloc_report_full(root, stderr);
130
CHECK_BLOCKS("ref1", p1, 5);
131
CHECK_BLOCKS("ref1", p2, 1);
132
CHECK_BLOCKS("ref1", r1, 1);
134
fprintf(stderr, "Freeing p1\n");
136
talloc_report_full(root, stderr);
138
CHECK_BLOCKS("ref1", r1, 1);
140
fprintf(stderr, "Freeing r1\n");
142
talloc_report_full(NULL, stderr);
144
fprintf(stderr, "Testing NULL\n");
145
if (talloc_reference(root, NULL)) {
149
CHECK_BLOCKS("ref1", root, 1);
151
CHECK_SIZE("ref1", root, 0);
154
printf("success: ref1\n");
161
static bool test_ref2(void)
163
void *root, *p1, *p2, *ref, *r1;
165
printf("test: ref2 [\nDOUBLE REFERENCE FREE\n]\n");
166
root = talloc_named_const(NULL, 0, "root");
167
p1 = talloc_named_const(root, 1, "p1");
168
talloc_named_const(p1, 1, "x1");
169
talloc_named_const(p1, 1, "x2");
170
talloc_named_const(p1, 1, "x3");
171
p2 = talloc_named_const(p1, 1, "p2");
173
r1 = talloc_named_const(root, 1, "r1");
174
ref = talloc_reference(r1, p2);
175
talloc_report_full(root, stderr);
177
CHECK_BLOCKS("ref2", p1, 5);
178
CHECK_BLOCKS("ref2", p2, 1);
179
CHECK_BLOCKS("ref2", r1, 2);
181
fprintf(stderr, "Freeing ref\n");
183
talloc_report_full(root, stderr);
185
CHECK_BLOCKS("ref2", p1, 5);
186
CHECK_BLOCKS("ref2", p2, 1);
187
CHECK_BLOCKS("ref2", r1, 1);
189
fprintf(stderr, "Freeing p2\n");
191
talloc_report_full(root, stderr);
193
CHECK_BLOCKS("ref2", p1, 4);
194
CHECK_BLOCKS("ref2", r1, 1);
196
fprintf(stderr, "Freeing p1\n");
198
talloc_report_full(root, stderr);
200
CHECK_BLOCKS("ref2", r1, 1);
202
fprintf(stderr, "Freeing r1\n");
204
talloc_report_full(root, stderr);
206
CHECK_SIZE("ref2", root, 0);
209
printf("success: ref2\n");
216
static bool test_ref3(void)
218
void *root, *p1, *p2, *ref, *r1;
220
printf("test: ref3 [\nPARENT REFERENCE FREE\n]\n");
222
root = talloc_named_const(NULL, 0, "root");
223
p1 = talloc_named_const(root, 1, "p1");
224
p2 = talloc_named_const(root, 1, "p2");
225
r1 = talloc_named_const(p1, 1, "r1");
226
ref = talloc_reference(p2, r1);
227
talloc_report_full(root, stderr);
229
CHECK_BLOCKS("ref3", p1, 2);
230
CHECK_BLOCKS("ref3", p2, 2);
231
CHECK_BLOCKS("ref3", r1, 1);
233
fprintf(stderr, "Freeing p1\n");
235
talloc_report_full(root, stderr);
237
CHECK_BLOCKS("ref3", p2, 2);
238
CHECK_BLOCKS("ref3", r1, 1);
240
fprintf(stderr, "Freeing p2\n");
242
talloc_report_full(root, stderr);
244
CHECK_SIZE("ref3", root, 0);
248
printf("success: ref3\n");
255
static bool test_ref4(void)
257
void *root, *p1, *p2, *ref, *r1;
259
printf("test: ref4 [\nREFERRER REFERENCE FREE\n]\n");
261
root = talloc_named_const(NULL, 0, "root");
262
p1 = talloc_named_const(root, 1, "p1");
263
talloc_named_const(p1, 1, "x1");
264
talloc_named_const(p1, 1, "x2");
265
talloc_named_const(p1, 1, "x3");
266
p2 = talloc_named_const(p1, 1, "p2");
268
r1 = talloc_named_const(root, 1, "r1");
269
ref = talloc_reference(r1, p2);
270
talloc_report_full(root, stderr);
272
CHECK_BLOCKS("ref4", p1, 5);
273
CHECK_BLOCKS("ref4", p2, 1);
274
CHECK_BLOCKS("ref4", r1, 2);
276
fprintf(stderr, "Freeing r1\n");
278
talloc_report_full(root, stderr);
280
CHECK_BLOCKS("ref4", p1, 5);
281
CHECK_BLOCKS("ref4", p2, 1);
283
fprintf(stderr, "Freeing p2\n");
285
talloc_report_full(root, stderr);
287
CHECK_BLOCKS("ref4", p1, 4);
289
fprintf(stderr, "Freeing p1\n");
291
talloc_report_full(root, stderr);
293
CHECK_SIZE("ref4", root, 0);
297
printf("success: ref4\n");
305
static bool test_unlink1(void)
307
void *root, *p1, *p2, *ref, *r1;
309
printf("test: unlink [\nUNLINK\n]\n");
311
root = talloc_named_const(NULL, 0, "root");
312
p1 = talloc_named_const(root, 1, "p1");
313
talloc_named_const(p1, 1, "x1");
314
talloc_named_const(p1, 1, "x2");
315
talloc_named_const(p1, 1, "x3");
316
p2 = talloc_named_const(p1, 1, "p2");
318
r1 = talloc_named_const(p1, 1, "r1");
319
ref = talloc_reference(r1, p2);
320
talloc_report_full(root, stderr);
322
CHECK_BLOCKS("unlink", p1, 7);
323
CHECK_BLOCKS("unlink", p2, 1);
324
CHECK_BLOCKS("unlink", r1, 2);
326
fprintf(stderr, "Unreferencing r1\n");
327
talloc_unlink(r1, p2);
328
talloc_report_full(root, stderr);
330
CHECK_BLOCKS("unlink", p1, 6);
331
CHECK_BLOCKS("unlink", p2, 1);
332
CHECK_BLOCKS("unlink", r1, 1);
334
fprintf(stderr, "Freeing p1\n");
336
talloc_report_full(root, stderr);
338
CHECK_SIZE("unlink", root, 0);
342
printf("success: unlink\n");
346
static int fail_destructor(void *ptr)
352
miscellaneous tests to try to get a higher test coverage percentage
354
static bool test_misc(void)
361
printf("test: misc [\nMISCELLANEOUS\n]\n");
363
root = talloc_new(NULL);
365
p1 = talloc_size(root, 0x7fffffff);
366
torture_assert("misc", !p1, "failed: large talloc allowed\n");
368
p1 = talloc_strdup(root, "foo");
369
talloc_increase_ref_count(p1);
370
talloc_increase_ref_count(p1);
371
talloc_increase_ref_count(p1);
372
CHECK_BLOCKS("misc", p1, 1);
373
CHECK_BLOCKS("misc", root, 2);
375
CHECK_BLOCKS("misc", p1, 1);
376
CHECK_BLOCKS("misc", root, 2);
377
talloc_unlink(NULL, p1);
378
CHECK_BLOCKS("misc", p1, 1);
379
CHECK_BLOCKS("misc", root, 2);
380
p2 = talloc_strdup(p1, "foo");
381
torture_assert("misc", talloc_unlink(root, p2) == -1,
382
"failed: talloc_unlink() of non-reference context should return -1\n");
383
torture_assert("misc", talloc_unlink(p1, p2) == 0,
384
"failed: talloc_unlink() of parent should succeed\n");
386
CHECK_BLOCKS("misc", p1, 1);
387
CHECK_BLOCKS("misc", root, 2);
389
name = talloc_set_name(p1, "my name is %s", "foo");
390
torture_assert_str_equal("misc", talloc_get_name(p1), "my name is foo",
391
"failed: wrong name after talloc_set_name(my name is foo)");
392
CHECK_BLOCKS("misc", p1, 2);
393
CHECK_BLOCKS("misc", root, 3);
395
talloc_set_name_const(p1, NULL);
396
torture_assert_str_equal ("misc", talloc_get_name(p1), "UNNAMED",
397
"failed: wrong name after talloc_set_name(NULL)");
398
CHECK_BLOCKS("misc", p1, 2);
399
CHECK_BLOCKS("misc", root, 3);
401
torture_assert("misc", talloc_free(NULL) == -1,
402
"talloc_free(NULL) should give -1\n");
404
talloc_set_destructor(p1, fail_destructor);
405
torture_assert("misc", talloc_free(p1) == -1,
406
"Failed destructor should cause talloc_free to fail\n");
407
talloc_set_destructor(p1, NULL);
409
talloc_report(root, stderr);
412
p2 = (char *)talloc_zero_size(p1, 20);
413
torture_assert("misc", p2[19] == 0, "Failed to give zero memory\n");
416
torture_assert("misc", talloc_strdup(root, NULL) == NULL,
417
"failed: strdup on NULL should give NULL\n");
419
p2 = talloc_strndup(p1, "foo", 2);
420
torture_assert("misc", strcmp("fo", p2) == 0,
421
"strndup doesn't work\n");
422
p2 = talloc_asprintf_append(p2, "o%c", 'd');
423
torture_assert("misc", strcmp("food", p2) == 0,
424
"talloc_asprintf_append doesn't work\n");
425
CHECK_BLOCKS("misc", p2, 1);
426
CHECK_BLOCKS("misc", p1, 3);
428
p2 = talloc_asprintf_append(NULL, "hello %s", "world");
429
torture_assert("misc", strcmp("hello world", p2) == 0,
430
"talloc_asprintf_append doesn't work\n");
431
CHECK_BLOCKS("misc", p2, 1);
432
CHECK_BLOCKS("misc", p1, 3);
435
d = talloc_array(p1, double, 0x20000000);
436
torture_assert("misc", !d, "failed: integer overflow not detected\n");
438
d = talloc_realloc(p1, d, double, 0x20000000);
439
torture_assert("misc", !d, "failed: integer overflow not detected\n");
442
CHECK_BLOCKS("misc", root, 1);
444
p1 = talloc_named(root, 100, "%d bytes", 100);
445
CHECK_BLOCKS("misc", p1, 2);
446
CHECK_BLOCKS("misc", root, 3);
447
talloc_unlink(root, p1);
449
p1 = talloc_init("%d bytes", 200);
450
p2 = talloc_asprintf(p1, "my test '%s'", "string");
451
torture_assert_str_equal("misc", p2, "my test 'string'",
452
"failed: talloc_asprintf(\"my test '%%s'\", \"string\") gave: \"%s\"");
453
CHECK_BLOCKS("misc", p1, 3);
454
CHECK_SIZE("misc", p2, 17);
455
CHECK_BLOCKS("misc", root, 1);
456
talloc_unlink(NULL, p1);
458
p1 = talloc_named_const(root, 10, "p1");
459
p2 = (char *)talloc_named_const(root, 20, "p2");
460
(void)talloc_reference(p1, p2);
461
talloc_report_full(root, stderr);
462
talloc_unlink(root, p2);
463
talloc_report_full(root, stderr);
464
CHECK_BLOCKS("misc", p2, 1);
465
CHECK_BLOCKS("misc", p1, 2);
466
CHECK_BLOCKS("misc", root, 3);
467
talloc_unlink(p1, p2);
468
talloc_unlink(root, p1);
470
p1 = talloc_named_const(root, 10, "p1");
471
p2 = (char *)talloc_named_const(root, 20, "p2");
472
(void)talloc_reference(NULL, p2);
473
talloc_report_full(root, stderr);
474
talloc_unlink(root, p2);
475
talloc_report_full(root, stderr);
476
CHECK_BLOCKS("misc", p2, 1);
477
CHECK_BLOCKS("misc", p1, 1);
478
CHECK_BLOCKS("misc", root, 2);
479
talloc_unlink(NULL, p2);
480
talloc_unlink(root, p1);
482
/* Test that talloc_unlink is a no-op */
484
torture_assert("misc", talloc_unlink(root, NULL) == -1,
485
"failed: talloc_unlink(root, NULL) == -1\n");
487
talloc_report(root, stderr);
488
talloc_report(NULL, stderr);
490
CHECK_SIZE("misc", root, 0);
494
CHECK_SIZE("misc", NULL, 0);
496
talloc_enable_leak_report();
497
talloc_enable_leak_report_full();
499
printf("success: misc\n");
508
static bool test_realloc(void)
510
void *root, *p1, *p2;
512
printf("test: realloc [\nREALLOC\n]\n");
514
root = talloc_new(NULL);
516
p1 = talloc_size(root, 10);
517
CHECK_SIZE("realloc", p1, 10);
519
p1 = talloc_realloc_size(NULL, p1, 20);
520
CHECK_SIZE("realloc", p1, 20);
524
p2 = talloc_realloc_size(p1, NULL, 30);
528
p2 = talloc_realloc_size(p1, p2, 40);
530
CHECK_SIZE("realloc", p2, 40);
531
CHECK_SIZE("realloc", root, 60);
532
CHECK_BLOCKS("realloc", p1, 4);
534
p1 = talloc_realloc_size(NULL, p1, 20);
535
CHECK_SIZE("realloc", p1, 60);
537
talloc_increase_ref_count(p2);
538
torture_assert("realloc", talloc_realloc_size(NULL, p2, 5) == NULL,
539
"failed: talloc_realloc() on a referenced pointer should fail\n");
540
CHECK_BLOCKS("realloc", p1, 4);
542
talloc_realloc_size(NULL, p2, 0);
543
talloc_realloc_size(NULL, p2, 0);
544
CHECK_BLOCKS("realloc", p1, 3);
546
torture_assert("realloc", talloc_realloc_size(NULL, p1, 0x7fffffff) == NULL,
547
"failed: oversize talloc should fail\n");
549
talloc_realloc_size(NULL, p1, 0);
551
CHECK_BLOCKS("realloc", root, 1);
552
CHECK_SIZE("realloc", root, 0);
556
printf("success: REALLOC\n");
562
test realloc with a child
564
static bool test_realloc_child(void)
572
struct el2 **list, **list2, **list3;
575
printf("test: REALLOC WITH CHILD\n");
577
root = talloc_new(NULL);
579
el1 = talloc(root, struct el1);
580
el1->list = talloc(el1, struct el2 *);
581
el1->list[0] = talloc(el1->list, struct el2);
582
el1->list[0]->name = talloc_strdup(el1->list[0], "testing");
584
el1->list2 = talloc(el1, struct el2 *);
585
el1->list2[0] = talloc(el1->list2, struct el2);
586
el1->list2[0]->name = talloc_strdup(el1->list2[0], "testing2");
588
el1->list3 = talloc(el1, struct el2 *);
589
el1->list3[0] = talloc(el1->list3, struct el2);
590
el1->list3[0]->name = talloc_strdup(el1->list3[0], "testing2");
592
el2 = talloc(el1->list, struct el2);
593
el2 = talloc(el1->list2, struct el2);
594
el2 = talloc(el1->list3, struct el2);
596
el1->list = talloc_realloc(el1, el1->list, struct el2 *, 100);
597
el1->list2 = talloc_realloc(el1, el1->list2, struct el2 *, 200);
598
el1->list3 = talloc_realloc(el1, el1->list3, struct el2 *, 300);
602
printf("success: REALLOC WITH CHILD\n");
609
static bool test_type(void)
620
printf("test: type [\ntalloc type checking\n]\n");
622
root = talloc_new(NULL);
624
el1 = talloc(root, struct el1);
628
torture_assert("type", talloc_get_type(el1, struct el1) == el1,
629
"type check failed on el1\n");
630
torture_assert("type", talloc_get_type(el1, struct el2) == NULL,
631
"type check failed on el1 with el2\n");
632
talloc_set_type(el1, struct el2);
633
torture_assert("type", talloc_get_type(el1, struct el2) == (struct el2 *)el1,
634
"type set failed on el1 with el2\n");
638
printf("success: type\n");
645
static bool test_steal(void)
647
void *root, *p1, *p2;
649
printf("test: steal [\nSTEAL\n]\n");
651
root = talloc_new(NULL);
653
p1 = talloc_array(root, char, 10);
654
CHECK_SIZE("steal", p1, 10);
656
p2 = talloc_realloc(root, NULL, char, 20);
657
CHECK_SIZE("steal", p1, 10);
658
CHECK_SIZE("steal", root, 30);
660
torture_assert("steal", talloc_steal(p1, NULL) == NULL,
661
"failed: stealing NULL should give NULL\n");
663
torture_assert("steal", talloc_steal(p1, p1) == p1,
664
"failed: stealing to ourselves is a nop\n");
665
CHECK_BLOCKS("steal", root, 3);
666
CHECK_SIZE("steal", root, 30);
668
talloc_steal(NULL, p1);
669
talloc_steal(NULL, p2);
670
CHECK_BLOCKS("steal", root, 1);
671
CHECK_SIZE("steal", root, 0);
674
talloc_steal(root, p2);
675
CHECK_BLOCKS("steal", root, 2);
676
CHECK_SIZE("steal", root, 20);
680
CHECK_BLOCKS("steal", root, 1);
681
CHECK_SIZE("steal", root, 0);
685
p1 = talloc_size(NULL, 3);
686
talloc_report_full(NULL, stderr);
687
CHECK_SIZE("steal", NULL, 3);
690
printf("success: steal\n");
697
static bool test_move(void)
705
printf("test: move [\nMOVE\n]\n");
707
root = talloc_new(NULL);
709
t1 = talloc(root, struct t_move);
710
t2 = talloc(root, struct t_move);
711
t1->p = talloc_strdup(t1, "foo");
712
t1->x = talloc(t1, int);
715
t2->p = talloc_move(t2, &t1->p);
716
t2->x = talloc_move(t2, &t1->x);
717
torture_assert("move", t1->p == NULL && t1->x == NULL &&
718
strcmp(t2->p, "foo") == 0 && *t2->x == 42,
719
"talloc move failed");
723
printf("success: move\n");
729
test talloc_realloc_fn
731
static bool test_realloc_fn(void)
735
printf("test: realloc_fn [\ntalloc_realloc_fn\n]\n");
737
root = talloc_new(NULL);
739
p1 = talloc_realloc_fn(root, NULL, 10);
740
CHECK_BLOCKS("realloc_fn", root, 2);
741
CHECK_SIZE("realloc_fn", root, 10);
742
p1 = talloc_realloc_fn(root, p1, 20);
743
CHECK_BLOCKS("realloc_fn", root, 2);
744
CHECK_SIZE("realloc_fn", root, 20);
745
p1 = talloc_realloc_fn(root, p1, 0);
746
CHECK_BLOCKS("realloc_fn", root, 1);
747
CHECK_SIZE("realloc_fn", root, 0);
751
printf("success: realloc_fn\n");
756
static bool test_unref_reparent(void)
758
void *root, *p1, *p2, *c1;
760
printf("test: unref_reparent [\nUNREFERENCE AFTER PARENT FREED\n]\n");
762
root = talloc_named_const(NULL, 0, "root");
763
p1 = talloc_named_const(root, 1, "orig parent");
764
p2 = talloc_named_const(root, 1, "parent by reference");
766
c1 = talloc_named_const(p1, 1, "child");
767
talloc_reference(p2, c1);
769
CHECK_PARENT("unref_reparent", c1, p1);
773
CHECK_PARENT("unref_reparent", c1, p2);
775
talloc_unlink(p2, c1);
777
CHECK_SIZE("unref_reparent", root, 1);
782
printf("success: unref_reparent\n");
787
measure the speed of talloc versus malloc
789
static bool test_speed(void)
791
void *ctx = talloc_new(NULL);
793
const int loop = 1000;
797
printf("test: speed [\nTALLOC VS MALLOC SPEED\n]\n");
799
tv = timeval_current();
803
for (i=0;i<loop;i++) {
804
p1 = talloc_size(ctx, loop % 100);
805
p2 = talloc_strdup(p1, "foo bar");
806
p3 = talloc_size(p1, 300);
810
} while (timeval_elapsed(&tv) < 5.0);
812
fprintf(stderr, "talloc: %.0f ops/sec\n", count/timeval_elapsed(&tv));
816
tv = timeval_current();
820
for (i=0;i<loop;i++) {
821
p1 = malloc(loop % 100);
822
p2 = strdup("foo bar");
829
} while (timeval_elapsed(&tv) < 5.0);
830
fprintf(stderr, "malloc: %.0f ops/sec\n", count/timeval_elapsed(&tv));
832
printf("success: speed\n");
837
static bool test_lifeless(void)
839
void *top = talloc_new(NULL);
840
char *parent, *child;
841
void *child_owner = talloc_new(NULL);
843
printf("test: lifeless [\nTALLOC_UNLINK LOOP\n]\n");
845
parent = talloc_strdup(top, "parent");
846
child = talloc_strdup(parent, "child");
847
(void)talloc_reference(child, parent);
848
(void)talloc_reference(child_owner, child);
849
talloc_report_full(top, stderr);
850
talloc_unlink(top, parent);
852
talloc_report_full(top, stderr);
854
talloc_free(child_owner);
857
printf("success: lifeless\n");
861
static int loop_destructor_count;
863
static int test_loop_destructor(char *ptr)
865
loop_destructor_count++;
869
static bool test_loop(void)
871
void *top = talloc_new(NULL);
877
printf("test: loop [\nTALLOC LOOP DESTRUCTION\n]\n");
879
parent = talloc_strdup(top, "parent");
880
req1 = talloc(parent, struct req1);
881
req1->req2 = talloc_strdup(req1, "req2");
882
talloc_set_destructor(req1->req2, test_loop_destructor);
883
req1->req3 = talloc_strdup(req1, "req3");
884
(void)talloc_reference(req1->req3, req1);
885
talloc_report_full(top, stderr);
887
talloc_report_full(top, stderr);
888
talloc_report_full(NULL, stderr);
891
torture_assert("loop", loop_destructor_count == 1,
892
"FAILED TO FIRE LOOP DESTRUCTOR\n");
893
loop_destructor_count = 0;
895
printf("success: loop\n");
899
static int fail_destructor_str(char *ptr)
904
static bool test_free_parent_deny_child(void)
906
void *top = talloc_new(NULL);
911
printf("test: free_parent_deny_child [\nTALLOC FREE PARENT DENY CHILD\n]\n");
913
level1 = talloc_strdup(top, "level1");
914
level2 = talloc_strdup(level1, "level2");
915
level3 = talloc_strdup(level2, "level3");
917
talloc_set_destructor(level3, fail_destructor_str);
919
talloc_set_destructor(level3, NULL);
921
CHECK_PARENT("free_parent_deny_child", level3, top);
925
printf("success: free_parent_deny_child\n");
929
static bool test_talloc_ptrtype(void)
931
void *top = talloc_new(NULL);
935
} *s1, *s2, **s3, ***s4;
936
const char *location1;
937
const char *location2;
938
const char *location3;
939
const char *location4;
941
printf("test: ptrtype [\nTALLOC PTRTYPE\n]\n");
943
s1 = talloc_ptrtype(top, s1);location1 = __location__;
945
if (talloc_get_size(s1) != sizeof(struct struct1)) {
946
printf("failure: ptrtype [\n"
947
"talloc_ptrtype() allocated the wrong size %lu (should be %lu)\n"
948
"]\n", (unsigned long)talloc_get_size(s1),
949
(unsigned long)sizeof(struct struct1));
953
if (strcmp(location1, talloc_get_name(s1)) != 0) {
954
printf("failure: ptrtype [\n"
955
"talloc_ptrtype() sets the wrong name '%s' (should be '%s')\n]\n",
956
talloc_get_name(s1), location1);
960
s2 = talloc_array_ptrtype(top, s2, 10);location2 = __location__;
962
if (talloc_get_size(s2) != (sizeof(struct struct1) * 10)) {
963
printf("failure: ptrtype [\n"
964
"talloc_array_ptrtype() allocated the wrong size "
965
"%lu (should be %lu)\n]\n",
966
(unsigned long)talloc_get_size(s2),
967
(unsigned long)(sizeof(struct struct1)*10));
971
if (strcmp(location2, talloc_get_name(s2)) != 0) {
972
printf("failure: ptrtype [\n"
973
"talloc_array_ptrtype() sets the wrong name '%s' (should be '%s')\n]\n",
974
talloc_get_name(s2), location2);
978
s3 = talloc_array_ptrtype(top, s3, 10);location3 = __location__;
980
if (talloc_get_size(s3) != (sizeof(struct struct1 *) * 10)) {
981
printf("failure: ptrtype [\n"
982
"talloc_array_ptrtype() allocated the wrong size "
983
"%lu (should be %lu)\n]\n",
984
(unsigned long)talloc_get_size(s3),
985
(unsigned long)(sizeof(struct struct1 *)*10));
989
torture_assert_str_equal("ptrtype", location3, talloc_get_name(s3),
990
"talloc_array_ptrtype() sets the wrong name");
992
s4 = talloc_array_ptrtype(top, s4, 10);location4 = __location__;
994
if (talloc_get_size(s4) != (sizeof(struct struct1 **) * 10)) {
995
printf("failure: ptrtype [\n"
996
"talloc_array_ptrtype() allocated the wrong size "
997
"%lu (should be %lu)\n]\n",
998
(unsigned long)talloc_get_size(s4),
999
(unsigned long)(sizeof(struct struct1 **)*10));
1003
torture_assert_str_equal("ptrtype", location4, talloc_get_name(s4),
1004
"talloc_array_ptrtype() sets the wrong name");
1008
printf("success: ptrtype\n");
1012
static int _test_talloc_free_in_destructor(void **ptr)
1018
static bool test_talloc_free_in_destructor(void)
1027
printf("test: free_in_destructor [\nTALLOC FREE IN DESTRUCTOR\n]\n");
1029
level0 = talloc_new(NULL);
1030
level1 = talloc_new(level0);
1031
level2 = talloc_new(level1);
1032
level3 = talloc_new(level2);
1033
level4 = talloc_new(level3);
1034
level5 = talloc(level4, void *);
1037
(void)talloc_reference(level0, level3);
1038
(void)talloc_reference(level3, level3);
1039
(void)talloc_reference(level5, level3);
1041
talloc_set_destructor(level5, _test_talloc_free_in_destructor);
1043
talloc_free(level1);
1045
talloc_free(level0);
1047
printf("success: free_in_destructor\n");
1051
static bool test_autofree(void)
1053
#if _SAMBA_BUILD_ < 4
1054
/* autofree test would kill smbtorture */
1056
printf("test: autofree [\nTALLOC AUTOFREE CONTEXT\n]\n");
1058
p = talloc_autofree_context();
1061
p = talloc_autofree_context();
1064
printf("success: autofree\n");
1069
struct torture_context;
1070
bool torture_local_talloc(struct torture_context *tctx)
1076
talloc_disable_null_tracking();
1077
talloc_enable_null_tracking();
1083
ret &= test_unlink1();
1085
ret &= test_realloc();
1086
ret &= test_realloc_child();
1087
ret &= test_steal();
1089
ret &= test_unref_reparent();
1090
ret &= test_realloc_fn();
1092
ret &= test_lifeless();
1094
ret &= test_free_parent_deny_child();
1095
ret &= test_talloc_ptrtype();
1096
ret &= test_talloc_free_in_destructor();
1099
ret &= test_speed();
1101
ret &= test_autofree();
1106
#if _SAMBA_BUILD_ < 4
1109
bool ret = torture_local_talloc(NULL);