source: branches/samba-3.0/source/lib/talloc/testsuite.c

Last change on this file was 1, checked in by Paul Smedley, 18 years ago

Initial code import

File size: 24.5 KB
Line 
1/*
2 Unix SMB/CIFS implementation.
3
4 local testing of talloc routines.
5
6 Copyright (C) Andrew Tridgell 2004
7
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
11
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 2 of the License, or (at your option) any later version.
16
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.
21
22 You should have received a copy of the GNU Lesser General Public
23 License along with this library; if not, write to the Free Software
24 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25*/
26
27#include "replace.h"
28#include "system/time.h"
29#include "talloc.h"
30
31static struct timeval timeval_current(void)
32{
33 struct timeval tv;
34 gettimeofday(&tv, NULL);
35 return tv;
36}
37
38static double timeval_elapsed(struct timeval *tv)
39{
40 struct timeval tv2 = timeval_current();
41 return (tv2.tv_sec - tv->tv_sec) +
42 (tv2.tv_usec - tv->tv_usec)*1.0e-6;
43}
44
45#define torture_assert(test, expr, str) if (!(expr)) { \
46 printf("failure: %s [\n%s: Expression %s failed: %s\n]\n", \
47 test, __location__, #expr, str); \
48 return false; \
49}
50
51#define torture_assert_str_equal(test, arg1, arg2, desc) \
52 if (strcmp(arg1, arg2)) { \
53 printf("failure: %s [\n%s: Expected %s, got %s: %s\n]\n", \
54 test, __location__, arg1, arg2, desc); \
55 return false; \
56 }
57
58#if _SAMBA_BUILD_==3
59#ifdef malloc
60#undef malloc
61#endif
62#ifdef strdup
63#undef strdup
64#endif
65#endif
66
67#define CHECK_SIZE(test, ptr, tsize) do { \
68 if (talloc_total_size(ptr) != (tsize)) { \
69 printf("failed: %s [\nwrong '%s' tree size: got %u expected %u\n]\n", \
70 test, #ptr, \
71 (unsigned)talloc_total_size(ptr), \
72 (unsigned)tsize); \
73 talloc_report_full(ptr, stdout); \
74 return false; \
75 } \
76} while (0)
77
78#define CHECK_BLOCKS(test, ptr, tblocks) do { \
79 if (talloc_total_blocks(ptr) != (tblocks)) { \
80 printf("failed: %s [\nwrong '%s' tree blocks: got %u expected %u\n]\n", \
81 test, #ptr, \
82 (unsigned)talloc_total_blocks(ptr), \
83 (unsigned)tblocks); \
84 talloc_report_full(ptr, stdout); \
85 return false; \
86 } \
87} while (0)
88
89#define CHECK_PARENT(test, ptr, parent) do { \
90 if (talloc_parent(ptr) != (parent)) { \
91 printf("failed: %s [\n'%s' has wrong parent: got %p expected %p\n]\n", \
92 test, #ptr, \
93 talloc_parent(ptr), \
94 (parent)); \
95 talloc_report_full(ptr, stdout); \
96 talloc_report_full(parent, stdout); \
97 talloc_report_full(NULL, stdout); \
98 return false; \
99 } \
100} while (0)
101
102
103/*
104 test references
105*/
106static bool test_ref1(void)
107{
108 void *root, *p1, *p2, *ref, *r1;
109
110 printf("test: ref1 [\nSINGLE REFERENCE FREE\n]\n");
111
112 root = talloc_named_const(NULL, 0, "root");
113 p1 = talloc_named_const(root, 1, "p1");
114 p2 = talloc_named_const(p1, 1, "p2");
115 talloc_named_const(p1, 1, "x1");
116 talloc_named_const(p1, 2, "x2");
117 talloc_named_const(p1, 3, "x3");
118
119 r1 = talloc_named_const(root, 1, "r1");
120 ref = talloc_reference(r1, p2);
121 talloc_report_full(root, stderr);
122
123 CHECK_BLOCKS("ref1", p1, 5);
124 CHECK_BLOCKS("ref1", p2, 1);
125 CHECK_BLOCKS("ref1", r1, 2);
126
127 fprintf(stderr, "Freeing p2\n");
128 talloc_free(p2);
129 talloc_report_full(root, stderr);
130
131 CHECK_BLOCKS("ref1", p1, 5);
132 CHECK_BLOCKS("ref1", p2, 1);
133 CHECK_BLOCKS("ref1", r1, 1);
134
135 fprintf(stderr, "Freeing p1\n");
136 talloc_free(p1);
137 talloc_report_full(root, stderr);
138
139 CHECK_BLOCKS("ref1", r1, 1);
140
141 fprintf(stderr, "Freeing r1\n");
142 talloc_free(r1);
143 talloc_report_full(NULL, stderr);
144
145 fprintf(stderr, "Testing NULL\n");
146 if (talloc_reference(root, NULL)) {
147 return false;
148 }
149
150 CHECK_BLOCKS("ref1", root, 1);
151
152 CHECK_SIZE("ref1", root, 0);
153
154 talloc_free(root);
155 printf("success: ref1\n");
156 return true;
157}
158
159/*
160 test references
161*/
162static bool test_ref2(void)
163{
164 void *root, *p1, *p2, *ref, *r1;
165
166 printf("test: ref2 [\nDOUBLE REFERENCE FREE\n]\n");
167 root = talloc_named_const(NULL, 0, "root");
168 p1 = talloc_named_const(root, 1, "p1");
169 talloc_named_const(p1, 1, "x1");
170 talloc_named_const(p1, 1, "x2");
171 talloc_named_const(p1, 1, "x3");
172 p2 = talloc_named_const(p1, 1, "p2");
173
174 r1 = talloc_named_const(root, 1, "r1");
175 ref = talloc_reference(r1, p2);
176 talloc_report_full(root, stderr);
177
178 CHECK_BLOCKS("ref2", p1, 5);
179 CHECK_BLOCKS("ref2", p2, 1);
180 CHECK_BLOCKS("ref2", r1, 2);
181
182 fprintf(stderr, "Freeing ref\n");
183 talloc_free(ref);
184 talloc_report_full(root, stderr);
185
186 CHECK_BLOCKS("ref2", p1, 5);
187 CHECK_BLOCKS("ref2", p2, 1);
188 CHECK_BLOCKS("ref2", r1, 1);
189
190 fprintf(stderr, "Freeing p2\n");
191 talloc_free(p2);
192 talloc_report_full(root, stderr);
193
194 CHECK_BLOCKS("ref2", p1, 4);
195 CHECK_BLOCKS("ref2", r1, 1);
196
197 fprintf(stderr, "Freeing p1\n");
198 talloc_free(p1);
199 talloc_report_full(root, stderr);
200
201 CHECK_BLOCKS("ref2", r1, 1);
202
203 fprintf(stderr, "Freeing r1\n");
204 talloc_free(r1);
205 talloc_report_full(root, stderr);
206
207 CHECK_SIZE("ref2", root, 0);
208
209 talloc_free(root);
210 printf("success: ref2\n");
211 return true;
212}
213
214/*
215 test references
216*/
217static bool test_ref3(void)
218{
219 void *root, *p1, *p2, *ref, *r1;
220
221 printf("test: ref3 [\nPARENT REFERENCE FREE\n]\n");
222
223 root = talloc_named_const(NULL, 0, "root");
224 p1 = talloc_named_const(root, 1, "p1");
225 p2 = talloc_named_const(root, 1, "p2");
226 r1 = talloc_named_const(p1, 1, "r1");
227 ref = talloc_reference(p2, r1);
228 talloc_report_full(root, stderr);
229
230 CHECK_BLOCKS("ref3", p1, 2);
231 CHECK_BLOCKS("ref3", p2, 2);
232 CHECK_BLOCKS("ref3", r1, 1);
233
234 fprintf(stderr, "Freeing p1\n");
235 talloc_free(p1);
236 talloc_report_full(root, stderr);
237
238 CHECK_BLOCKS("ref3", p2, 2);
239 CHECK_BLOCKS("ref3", r1, 1);
240
241 fprintf(stderr, "Freeing p2\n");
242 talloc_free(p2);
243 talloc_report_full(root, stderr);
244
245 CHECK_SIZE("ref3", root, 0);
246
247 talloc_free(root);
248
249 printf("success: ref3\n");
250 return true;
251}
252
253/*
254 test references
255*/
256static bool test_ref4(void)
257{
258 void *root, *p1, *p2, *ref, *r1;
259
260 printf("test: ref4 [\nREFERRER REFERENCE FREE\n]\n");
261
262 root = talloc_named_const(NULL, 0, "root");
263 p1 = talloc_named_const(root, 1, "p1");
264 talloc_named_const(p1, 1, "x1");
265 talloc_named_const(p1, 1, "x2");
266 talloc_named_const(p1, 1, "x3");
267 p2 = talloc_named_const(p1, 1, "p2");
268
269 r1 = talloc_named_const(root, 1, "r1");
270 ref = talloc_reference(r1, p2);
271 talloc_report_full(root, stderr);
272
273 CHECK_BLOCKS("ref4", p1, 5);
274 CHECK_BLOCKS("ref4", p2, 1);
275 CHECK_BLOCKS("ref4", r1, 2);
276
277 fprintf(stderr, "Freeing r1\n");
278 talloc_free(r1);
279 talloc_report_full(root, stderr);
280
281 CHECK_BLOCKS("ref4", p1, 5);
282 CHECK_BLOCKS("ref4", p2, 1);
283
284 fprintf(stderr, "Freeing p2\n");
285 talloc_free(p2);
286 talloc_report_full(root, stderr);
287
288 CHECK_BLOCKS("ref4", p1, 4);
289
290 fprintf(stderr, "Freeing p1\n");
291 talloc_free(p1);
292 talloc_report_full(root, stderr);
293
294 CHECK_SIZE("ref4", root, 0);
295
296 talloc_free(root);
297
298 printf("success: ref4\n");
299 return true;
300}
301
302
303/*
304 test references
305*/
306static bool test_unlink1(void)
307{
308 void *root, *p1, *p2, *ref, *r1;
309
310 printf("test: unlink [\nUNLINK\n]\n");
311
312 root = talloc_named_const(NULL, 0, "root");
313 p1 = talloc_named_const(root, 1, "p1");
314 talloc_named_const(p1, 1, "x1");
315 talloc_named_const(p1, 1, "x2");
316 talloc_named_const(p1, 1, "x3");
317 p2 = talloc_named_const(p1, 1, "p2");
318
319 r1 = talloc_named_const(p1, 1, "r1");
320 ref = talloc_reference(r1, p2);
321 talloc_report_full(root, stderr);
322
323 CHECK_BLOCKS("unlink", p1, 7);
324 CHECK_BLOCKS("unlink", p2, 1);
325 CHECK_BLOCKS("unlink", r1, 2);
326
327 fprintf(stderr, "Unreferencing r1\n");
328 talloc_unlink(r1, p2);
329 talloc_report_full(root, stderr);
330
331 CHECK_BLOCKS("unlink", p1, 6);
332 CHECK_BLOCKS("unlink", p2, 1);
333 CHECK_BLOCKS("unlink", r1, 1);
334
335 fprintf(stderr, "Freeing p1\n");
336 talloc_free(p1);
337 talloc_report_full(root, stderr);
338
339 CHECK_SIZE("unlink", root, 0);
340
341 talloc_free(root);
342
343 printf("success: unlink\n");
344 return true;
345}
346
347static int fail_destructor(void *ptr)
348{
349 return -1;
350}
351
352/*
353 miscellaneous tests to try to get a higher test coverage percentage
354*/
355static bool test_misc(void)
356{
357 void *root, *p1;
358 char *p2;
359 double *d;
360 const char *name;
361
362 printf("test: misc [\nMISCELLANEOUS\n]\n");
363
364 root = talloc_new(NULL);
365
366 p1 = talloc_size(root, 0x7fffffff);
367 torture_assert("misc", !p1, "failed: large talloc allowed\n");
368
369 p1 = talloc_strdup(root, "foo");
370 talloc_increase_ref_count(p1);
371 talloc_increase_ref_count(p1);
372 talloc_increase_ref_count(p1);
373 CHECK_BLOCKS("misc", p1, 1);
374 CHECK_BLOCKS("misc", root, 2);
375 talloc_free(p1);
376 CHECK_BLOCKS("misc", p1, 1);
377 CHECK_BLOCKS("misc", root, 2);
378 talloc_unlink(NULL, p1);
379 CHECK_BLOCKS("misc", p1, 1);
380 CHECK_BLOCKS("misc", root, 2);
381 p2 = talloc_strdup(p1, "foo");
382 torture_assert("misc", talloc_unlink(root, p2) == -1,
383 "failed: talloc_unlink() of non-reference context should return -1\n");
384 torture_assert("misc", talloc_unlink(p1, p2) == 0,
385 "failed: talloc_unlink() of parent should succeed\n");
386 talloc_free(p1);
387 CHECK_BLOCKS("misc", p1, 1);
388 CHECK_BLOCKS("misc", root, 2);
389
390 name = talloc_set_name(p1, "my name is %s", "foo");
391 torture_assert_str_equal("misc", talloc_get_name(p1), "my name is foo",
392 "failed: wrong name after talloc_set_name(my name is foo)");
393 CHECK_BLOCKS("misc", p1, 2);
394 CHECK_BLOCKS("misc", root, 3);
395
396 talloc_set_name_const(p1, NULL);
397 torture_assert_str_equal ("misc", talloc_get_name(p1), "UNNAMED",
398 "failed: wrong name after talloc_set_name(NULL)");
399 CHECK_BLOCKS("misc", p1, 2);
400 CHECK_BLOCKS("misc", root, 3);
401
402 torture_assert("misc", talloc_free(NULL) == -1,
403 "talloc_free(NULL) should give -1\n");
404
405 talloc_set_destructor(p1, fail_destructor);
406 torture_assert("misc", talloc_free(p1) == -1,
407 "Failed destructor should cause talloc_free to fail\n");
408 talloc_set_destructor(p1, NULL);
409
410 talloc_report(root, stderr);
411
412
413 p2 = (char *)talloc_zero_size(p1, 20);
414 torture_assert("misc", p2[19] == 0, "Failed to give zero memory\n");
415 talloc_free(p2);
416
417 torture_assert("misc", talloc_strdup(root, NULL) == NULL,
418 "failed: strdup on NULL should give NULL\n");
419
420 p2 = talloc_strndup(p1, "foo", 2);
421 torture_assert("misc", strcmp("fo", p2) == 0,
422 "strndup doesn't work\n");
423 p2 = talloc_asprintf_append(p2, "o%c", 'd');
424 torture_assert("misc", strcmp("food", p2) == 0,
425 "talloc_asprintf_append doesn't work\n");
426 CHECK_BLOCKS("misc", p2, 1);
427 CHECK_BLOCKS("misc", p1, 3);
428
429 p2 = talloc_asprintf_append(NULL, "hello %s", "world");
430 torture_assert("misc", strcmp("hello world", p2) == 0,
431 "talloc_asprintf_append doesn't work\n");
432 CHECK_BLOCKS("misc", p2, 1);
433 CHECK_BLOCKS("misc", p1, 3);
434 talloc_free(p2);
435
436 d = talloc_array(p1, double, 0x20000000);
437 torture_assert("misc", !d, "failed: integer overflow not detected\n");
438
439 d = talloc_realloc(p1, d, double, 0x20000000);
440 torture_assert("misc", !d, "failed: integer overflow not detected\n");
441
442 talloc_free(p1);
443 CHECK_BLOCKS("misc", root, 1);
444
445 p1 = talloc_named(root, 100, "%d bytes", 100);
446 CHECK_BLOCKS("misc", p1, 2);
447 CHECK_BLOCKS("misc", root, 3);
448 talloc_unlink(root, p1);
449
450 p1 = talloc_init("%d bytes", 200);
451 p2 = talloc_asprintf(p1, "my test '%s'", "string");
452 torture_assert_str_equal("misc", p2, "my test 'string'",
453 "failed: talloc_asprintf(\"my test '%%s'\", \"string\") gave: \"%s\"");
454 CHECK_BLOCKS("misc", p1, 3);
455 CHECK_SIZE("misc", p2, 17);
456 CHECK_BLOCKS("misc", root, 1);
457 talloc_unlink(NULL, p1);
458
459 p1 = talloc_named_const(root, 10, "p1");
460 p2 = (char *)talloc_named_const(root, 20, "p2");
461 (void)talloc_reference(p1, p2);
462 talloc_report_full(root, stderr);
463 talloc_unlink(root, p2);
464 talloc_report_full(root, stderr);
465 CHECK_BLOCKS("misc", p2, 1);
466 CHECK_BLOCKS("misc", p1, 2);
467 CHECK_BLOCKS("misc", root, 3);
468 talloc_unlink(p1, p2);
469 talloc_unlink(root, p1);
470
471 p1 = talloc_named_const(root, 10, "p1");
472 p2 = (char *)talloc_named_const(root, 20, "p2");
473 (void)talloc_reference(NULL, p2);
474 talloc_report_full(root, stderr);
475 talloc_unlink(root, p2);
476 talloc_report_full(root, stderr);
477 CHECK_BLOCKS("misc", p2, 1);
478 CHECK_BLOCKS("misc", p1, 1);
479 CHECK_BLOCKS("misc", root, 2);
480 talloc_unlink(NULL, p2);
481 talloc_unlink(root, p1);
482
483 /* Test that talloc_unlink is a no-op */
484
485 torture_assert("misc", talloc_unlink(root, NULL) == -1,
486 "failed: talloc_unlink(root, NULL) == -1\n");
487
488 talloc_report(root, stderr);
489 talloc_report(NULL, stderr);
490
491 CHECK_SIZE("misc", root, 0);
492
493 talloc_free(root);
494
495 CHECK_SIZE("misc", NULL, 0);
496
497 talloc_enable_leak_report();
498 talloc_enable_leak_report_full();
499
500 printf("success: misc\n");
501
502 return true;
503}
504
505
506/*
507 test realloc
508*/
509static bool test_realloc(void)
510{
511 void *root, *p1, *p2;
512
513 printf("test: realloc [\nREALLOC\n]\n");
514
515 root = talloc_new(NULL);
516
517 p1 = talloc_size(root, 10);
518 CHECK_SIZE("realloc", p1, 10);
519
520 p1 = talloc_realloc_size(NULL, p1, 20);
521 CHECK_SIZE("realloc", p1, 20);
522
523 talloc_new(p1);
524
525 p2 = talloc_realloc_size(p1, NULL, 30);
526
527 talloc_new(p1);
528
529 p2 = talloc_realloc_size(p1, p2, 40);
530
531 CHECK_SIZE("realloc", p2, 40);
532 CHECK_SIZE("realloc", root, 60);
533 CHECK_BLOCKS("realloc", p1, 4);
534
535 p1 = talloc_realloc_size(NULL, p1, 20);
536 CHECK_SIZE("realloc", p1, 60);
537
538 talloc_increase_ref_count(p2);
539 torture_assert("realloc", talloc_realloc_size(NULL, p2, 5) == NULL,
540 "failed: talloc_realloc() on a referenced pointer should fail\n");
541 CHECK_BLOCKS("realloc", p1, 4);
542
543 talloc_realloc_size(NULL, p2, 0);
544 talloc_realloc_size(NULL, p2, 0);
545 CHECK_BLOCKS("realloc", p1, 3);
546
547 torture_assert("realloc", talloc_realloc_size(NULL, p1, 0x7fffffff) == NULL,
548 "failed: oversize talloc should fail\n");
549
550 talloc_realloc_size(NULL, p1, 0);
551
552 CHECK_BLOCKS("realloc", root, 1);
553 CHECK_SIZE("realloc", root, 0);
554
555 talloc_free(root);
556
557 printf("success: REALLOC\n");
558
559 return true;
560}
561
562/*
563 test realloc with a child
564*/
565static bool test_realloc_child(void)
566{
567 void *root;
568 struct el2 {
569 const char *name;
570 } *el2;
571 struct el1 {
572 int count;
573 struct el2 **list, **list2, **list3;
574 } *el1;
575
576 printf("test: REALLOC WITH CHILD\n");
577
578 root = talloc_new(NULL);
579
580 el1 = talloc(root, struct el1);
581 el1->list = talloc(el1, struct el2 *);
582 el1->list[0] = talloc(el1->list, struct el2);
583 el1->list[0]->name = talloc_strdup(el1->list[0], "testing");
584
585 el1->list2 = talloc(el1, struct el2 *);
586 el1->list2[0] = talloc(el1->list2, struct el2);
587 el1->list2[0]->name = talloc_strdup(el1->list2[0], "testing2");
588
589 el1->list3 = talloc(el1, struct el2 *);
590 el1->list3[0] = talloc(el1->list3, struct el2);
591 el1->list3[0]->name = talloc_strdup(el1->list3[0], "testing2");
592
593 el2 = talloc(el1->list, struct el2);
594 el2 = talloc(el1->list2, struct el2);
595 el2 = talloc(el1->list3, struct el2);
596
597 el1->list = talloc_realloc(el1, el1->list, struct el2 *, 100);
598 el1->list2 = talloc_realloc(el1, el1->list2, struct el2 *, 200);
599 el1->list3 = talloc_realloc(el1, el1->list3, struct el2 *, 300);
600
601 talloc_free(root);
602
603 printf("success: REALLOC WITH CHILD\n");
604 return true;
605}
606
607/*
608 test type checking
609*/
610static bool test_type(void)
611{
612 void *root;
613 struct el1 {
614 int count;
615 };
616 struct el2 {
617 int count;
618 };
619 struct el1 *el1;
620
621 printf("test: type [\ntalloc type checking\n]\n");
622
623 root = talloc_new(NULL);
624
625 el1 = talloc(root, struct el1);
626
627 el1->count = 1;
628
629 torture_assert("type", talloc_get_type(el1, struct el1) == el1,
630 "type check failed on el1\n");
631 torture_assert("type", talloc_get_type(el1, struct el2) == NULL,
632 "type check failed on el1 with el2\n");
633 talloc_set_type(el1, struct el2);
634 torture_assert("type", talloc_get_type(el1, struct el2) == (struct el2 *)el1,
635 "type set failed on el1 with el2\n");
636
637 talloc_free(root);
638
639 printf("success: type\n");
640 return true;
641}
642
643/*
644 test steal
645*/
646static bool test_steal(void)
647{
648 void *root, *p1, *p2;
649
650 printf("test: steal [\nSTEAL\n]\n");
651
652 root = talloc_new(NULL);
653
654 p1 = talloc_array(root, char, 10);
655 CHECK_SIZE("steal", p1, 10);
656
657 p2 = talloc_realloc(root, NULL, char, 20);
658 CHECK_SIZE("steal", p1, 10);
659 CHECK_SIZE("steal", root, 30);
660
661 torture_assert("steal", talloc_steal(p1, NULL) == NULL,
662 "failed: stealing NULL should give NULL\n");
663
664 torture_assert("steal", talloc_steal(p1, p1) == p1,
665 "failed: stealing to ourselves is a nop\n");
666 CHECK_BLOCKS("steal", root, 3);
667 CHECK_SIZE("steal", root, 30);
668
669 talloc_steal(NULL, p1);
670 talloc_steal(NULL, p2);
671 CHECK_BLOCKS("steal", root, 1);
672 CHECK_SIZE("steal", root, 0);
673
674 talloc_free(p1);
675 talloc_steal(root, p2);
676 CHECK_BLOCKS("steal", root, 2);
677 CHECK_SIZE("steal", root, 20);
678
679 talloc_free(p2);
680
681 CHECK_BLOCKS("steal", root, 1);
682 CHECK_SIZE("steal", root, 0);
683
684 talloc_free(root);
685
686 p1 = talloc_size(NULL, 3);
687 talloc_report_full(NULL, stderr);
688 CHECK_SIZE("steal", NULL, 3);
689 talloc_free(p1);
690
691 printf("success: steal\n");
692 return true;
693}
694
695/*
696 test move
697*/
698static bool test_move(void)
699{
700 void *root;
701 struct t_move {
702 char *p;
703 int *x;
704 } *t1, *t2;
705
706 printf("test: move [\nMOVE\n]\n");
707
708 root = talloc_new(NULL);
709
710 t1 = talloc(root, struct t_move);
711 t2 = talloc(root, struct t_move);
712 t1->p = talloc_strdup(t1, "foo");
713 t1->x = talloc(t1, int);
714 *t1->x = 42;
715
716 t2->p = talloc_move(t2, &t1->p);
717 t2->x = talloc_move(t2, &t1->x);
718 torture_assert("move", t1->p == NULL && t1->x == NULL &&
719 strcmp(t2->p, "foo") == 0 && *t2->x == 42,
720 "talloc move failed");
721
722 talloc_free(root);
723
724 printf("success: move\n");
725
726 return true;
727}
728
729/*
730 test talloc_realloc_fn
731*/
732static bool test_realloc_fn(void)
733{
734 void *root, *p1;
735
736 printf("test: realloc_fn [\ntalloc_realloc_fn\n]\n");
737
738 root = talloc_new(NULL);
739
740 p1 = talloc_realloc_fn(root, NULL, 10);
741 CHECK_BLOCKS("realloc_fn", root, 2);
742 CHECK_SIZE("realloc_fn", root, 10);
743 p1 = talloc_realloc_fn(root, p1, 20);
744 CHECK_BLOCKS("realloc_fn", root, 2);
745 CHECK_SIZE("realloc_fn", root, 20);
746 p1 = talloc_realloc_fn(root, p1, 0);
747 CHECK_BLOCKS("realloc_fn", root, 1);
748 CHECK_SIZE("realloc_fn", root, 0);
749
750 talloc_free(root);
751
752 printf("success: realloc_fn\n");
753 return true;
754}
755
756
757static bool test_unref_reparent(void)
758{
759 void *root, *p1, *p2, *c1;
760
761 printf("test: unref_reparent [\nUNREFERENCE AFTER PARENT FREED\n]\n");
762
763 root = talloc_named_const(NULL, 0, "root");
764 p1 = talloc_named_const(root, 1, "orig parent");
765 p2 = talloc_named_const(root, 1, "parent by reference");
766
767 c1 = talloc_named_const(p1, 1, "child");
768 talloc_reference(p2, c1);
769
770 CHECK_PARENT("unref_reparent", c1, p1);
771
772 talloc_free(p1);
773
774 CHECK_PARENT("unref_reparent", c1, p2);
775
776 talloc_unlink(p2, c1);
777
778 CHECK_SIZE("unref_reparent", root, 1);
779
780 talloc_free(p2);
781 talloc_free(root);
782
783 printf("success: unref_reparent\n");
784 return true;
785}
786
787/*
788 measure the speed of talloc versus malloc
789*/
790static bool test_speed(void)
791{
792 void *ctx = talloc_new(NULL);
793 unsigned count;
794 const int loop = 1000;
795 int i;
796 struct timeval tv;
797
798 printf("test: speed [\nTALLOC VS MALLOC SPEED\n]\n");
799
800 tv = timeval_current();
801 count = 0;
802 do {
803 void *p1, *p2, *p3;
804 for (i=0;i<loop;i++) {
805 p1 = talloc_size(ctx, loop % 100);
806 p2 = talloc_strdup(p1, "foo bar");
807 p3 = talloc_size(p1, 300);
808 talloc_free(p1);
809 }
810 count += 3 * loop;
811 } while (timeval_elapsed(&tv) < 5.0);
812
813 fprintf(stderr, "talloc: %.0f ops/sec\n", count/timeval_elapsed(&tv));
814
815 talloc_free(ctx);
816
817 tv = timeval_current();
818 count = 0;
819 do {
820 void *p1, *p2, *p3;
821 for (i=0;i<loop;i++) {
822 p1 = malloc(loop % 100);
823 p2 = strdup("foo bar");
824 p3 = malloc(300);
825 free(p1);
826 free(p2);
827 free(p3);
828 }
829 count += 3 * loop;
830 } while (timeval_elapsed(&tv) < 5.0);
831 fprintf(stderr, "malloc: %.0f ops/sec\n", count/timeval_elapsed(&tv));
832
833 printf("success: speed\n");
834
835 return true;
836}
837
838static bool test_lifeless(void)
839{
840 void *top = talloc_new(NULL);
841 char *parent, *child;
842 void *child_owner = talloc_new(NULL);
843
844 printf("test: lifeless [\nTALLOC_UNLINK LOOP\n]\n");
845
846 parent = talloc_strdup(top, "parent");
847 child = talloc_strdup(parent, "child");
848 (void)talloc_reference(child, parent);
849 (void)talloc_reference(child_owner, child);
850 talloc_report_full(top, stderr);
851 talloc_unlink(top, parent);
852 talloc_free(child);
853 talloc_report_full(top, stderr);
854 talloc_free(top);
855 talloc_free(child_owner);
856 talloc_free(child);
857
858 printf("success: lifeless\n");
859 return true;
860}
861
862static int loop_destructor_count;
863
864static int test_loop_destructor(char *ptr)
865{
866 loop_destructor_count++;
867 return 0;
868}
869
870static bool test_loop(void)
871{
872 void *top = talloc_new(NULL);
873 char *parent;
874 struct req1 {
875 char *req2, *req3;
876 } *req1;
877
878 printf("test: loop [\nTALLOC LOOP DESTRUCTION\n]\n");
879
880 parent = talloc_strdup(top, "parent");
881 req1 = talloc(parent, struct req1);
882 req1->req2 = talloc_strdup(req1, "req2");
883 talloc_set_destructor(req1->req2, test_loop_destructor);
884 req1->req3 = talloc_strdup(req1, "req3");
885 (void)talloc_reference(req1->req3, req1);
886 talloc_report_full(top, stderr);
887 talloc_free(parent);
888 talloc_report_full(top, stderr);
889 talloc_report_full(NULL, stderr);
890 talloc_free(top);
891
892 torture_assert("loop", loop_destructor_count == 1,
893 "FAILED TO FIRE LOOP DESTRUCTOR\n");
894 loop_destructor_count = 0;
895
896 printf("success: loop\n");
897 return true;
898}
899
900static int fail_destructor_str(char *ptr)
901{
902 return -1;
903}
904
905static bool test_free_parent_deny_child(void)
906{
907 void *top = talloc_new(NULL);
908 char *level1;
909 char *level2;
910 char *level3;
911
912 printf("test: free_parent_deny_child [\nTALLOC FREE PARENT DENY CHILD\n]\n");
913
914 level1 = talloc_strdup(top, "level1");
915 level2 = talloc_strdup(level1, "level2");
916 level3 = talloc_strdup(level2, "level3");
917
918 talloc_set_destructor(level3, fail_destructor_str);
919 talloc_free(level1);
920 talloc_set_destructor(level3, NULL);
921
922 CHECK_PARENT("free_parent_deny_child", level3, top);
923
924 talloc_free(top);
925
926 printf("success: free_parent_deny_child\n");
927 return true;
928}
929
930static bool test_talloc_ptrtype(void)
931{
932 void *top = talloc_new(NULL);
933 struct struct1 {
934 int foo;
935 int bar;
936 } *s1, *s2, **s3, ***s4;
937 const char *location1;
938 const char *location2;
939 const char *location3;
940 const char *location4;
941
942 printf("test: ptrtype [\nTALLOC PTRTYPE\n]\n");
943
944 s1 = talloc_ptrtype(top, s1);location1 = __location__;
945
946 if (talloc_get_size(s1) != sizeof(struct struct1)) {
947 printf("failure: ptrtype [\n"
948 "talloc_ptrtype() allocated the wrong size %lu (should be %lu)\n"
949 "]\n", (unsigned long)talloc_get_size(s1),
950 (unsigned long)sizeof(struct struct1));
951 return false;
952 }
953
954 if (strcmp(location1, talloc_get_name(s1)) != 0) {
955 printf("failure: ptrtype [\n"
956 "talloc_ptrtype() sets the wrong name '%s' (should be '%s')\n]\n",
957 talloc_get_name(s1), location1);
958 return false;
959 }
960
961 s2 = talloc_array_ptrtype(top, s2, 10);location2 = __location__;
962
963 if (talloc_get_size(s2) != (sizeof(struct struct1) * 10)) {
964 printf("failure: ptrtype [\n"
965 "talloc_array_ptrtype() allocated the wrong size "
966 "%lu (should be %lu)\n]\n",
967 (unsigned long)talloc_get_size(s2),
968 (unsigned long)(sizeof(struct struct1)*10));
969 return false;
970 }
971
972 if (strcmp(location2, talloc_get_name(s2)) != 0) {
973 printf("failure: ptrtype [\n"
974 "talloc_array_ptrtype() sets the wrong name '%s' (should be '%s')\n]\n",
975 talloc_get_name(s2), location2);
976 return false;
977 }
978
979 s3 = talloc_array_ptrtype(top, s3, 10);location3 = __location__;
980
981 if (talloc_get_size(s3) != (sizeof(struct struct1 *) * 10)) {
982 printf("failure: ptrtype [\n"
983 "talloc_array_ptrtype() allocated the wrong size "
984 "%lu (should be %lu)\n]\n",
985 (unsigned long)talloc_get_size(s3),
986 (unsigned long)(sizeof(struct struct1 *)*10));
987 return false;
988 }
989
990 torture_assert_str_equal("ptrtype", location3, talloc_get_name(s3),
991 "talloc_array_ptrtype() sets the wrong name");
992
993 s4 = talloc_array_ptrtype(top, s4, 10);location4 = __location__;
994
995 if (talloc_get_size(s4) != (sizeof(struct struct1 **) * 10)) {
996 printf("failure: TALLOC PTRTYPE [\n"
997 "talloc_array_ptrtype() allocated the wrong size "
998 "%lu (should be %lu)\n]\n",
999 (unsigned long)talloc_get_size(s4),
1000 (unsigned long)(sizeof(struct struct1 **)*10));
1001 return false;
1002 }
1003
1004 torture_assert_str_equal("ptrtype", location4, talloc_get_name(s4),
1005 "talloc_array_ptrtype() sets the wrong name");
1006
1007 talloc_free(top);
1008
1009 printf("success: ptrtype\n");
1010 return true;
1011}
1012
1013static bool test_autofree(void)
1014{
1015 void *p;
1016 printf("test: autofree [\nTALLOC AUTOFREE CONTEXT\n]\n");
1017
1018 p = talloc_autofree_context();
1019 talloc_free(p);
1020
1021 p = talloc_autofree_context();
1022 talloc_free(p);
1023
1024 printf("success: autofree\n");
1025 return true;
1026}
1027
1028int main(void)
1029{
1030 bool ret = true;
1031
1032 talloc_disable_null_tracking();
1033 talloc_enable_null_tracking();
1034
1035 ret &= test_ref1();
1036 ret &= test_ref2();
1037 ret &= test_ref3();
1038 ret &= test_ref4();
1039 ret &= test_unlink1();
1040 ret &= test_misc();
1041 ret &= test_realloc();
1042 ret &= test_realloc_child();
1043 ret &= test_steal();
1044 ret &= test_move();
1045 ret &= test_unref_reparent();
1046 ret &= test_realloc_fn();
1047 ret &= test_type();
1048 ret &= test_lifeless();
1049 ret &= test_loop();
1050 ret &= test_free_parent_deny_child();
1051 ret &= test_talloc_ptrtype();
1052
1053 if (ret) {
1054 ret &= test_speed();
1055 }
1056 ret &= test_autofree();
1057
1058 if (!ret)
1059 return -1;
1060 return 0;
1061}
Note: See TracBrowser for help on using the repository browser.