2 Unix SMB/CIFS implementation.
4 local testing of talloc routines.
6 Copyright (C) Andrew Tridgell 2004
7 Converted to ccan tests by Rusty Russell 2008
9 ** NOTE! The following LGPL license applies to the talloc
10 ** library. This does NOT imply that all of Samba is released
13 This library is free software; you can redistribute it and/or
14 modify it under the terms of the GNU Lesser General Public
15 License as published by the Free Software Foundation; either
16 version 2 of the License, or (at your option) any later version.
18 This library is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 Lesser General Public License for more details.
23 You should have received a copy of the GNU Lesser General Public
24 License along with this library; if not, write to the Free Software
25 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 #include <ccan/failtest/failtest_override.h>
29 #include <ccan/talloc/talloc.c>
31 #include <ccan/tap/tap.h>
32 #include <ccan/failtest/failtest.h>
34 #define torture_assert(test, expr, str) \
35 ok(expr, "%s [\n%s: Expression %s failed: %s\n]\n", \
36 test, __location__, #expr, str)
38 #define torture_assert_str_equal(test, arg1, arg2, desc) \
39 ok(strcmp(arg1, arg2) == 0, \
40 "%s [\n%s: Expected %s, got %s: %s\n]\n", \
41 test, __location__, arg1, arg2, desc)
43 #define CHECK_SIZE(test, ptr, tsize) \
44 ok(talloc_total_size(ptr) == (tsize), \
45 "%s [\nwrong '%s' tree size: got %u expected %u\n]\n", \
47 (unsigned)talloc_total_size(ptr), \
50 #define CHECK_BLOCKS(test, ptr, tblocks) \
51 ok(talloc_total_blocks(ptr) == (tblocks), \
52 "%s [\nwrong '%s' tree blocks: got %u expected %u\n]\n", \
54 (unsigned)talloc_total_blocks(ptr), \
57 #define CHECK_PARENT(test, ptr, parent) \
58 ok(talloc_parent(ptr) == (parent), \
59 "%s [\n'%s' has wrong parent: got %p expected %p\n]\n", \
64 struct torture_context;
66 static bool test_unref_reparent(const struct torture_context *ctx)
68 void *root, *p1, *p2, *c1;
71 root = talloc_named_const(ctx, 0, "root");
74 p1 = talloc_named_const(root, 1, "orig parent");
77 p2 = talloc_named_const(root, 1, "parent by reference");
81 c1 = talloc_named_const(p1, 1, "child");
85 if (!talloc_reference(p2, c1))
88 CHECK_PARENT("unref_reparent", c1, p1);
92 CHECK_PARENT("unref_reparent", c1, p2);
94 talloc_unlink(p2, c1);
96 CHECK_SIZE("unref_reparent", root, 1);
106 static bool test_lifeless(const struct torture_context *ctx)
108 void *top = talloc_new(ctx);
109 char *parent, *child;
110 void *child_owner = talloc_new(ctx);
112 parent = talloc_strdup(top, "parent");
115 child = talloc_strdup(parent, "child");
120 if (!talloc_reference(child, parent)) {
125 if (!talloc_reference(child_owner, child)) {
126 talloc_unlink(child, parent);
131 talloc_unlink(top, parent);
134 talloc_free(child_owner);
140 int main(int argc, char *argv[])
143 failtest_init(argc, argv);
145 talloc_enable_null_tracking();
147 ok1(test_unref_reparent(NULL) && test_lifeless(NULL));
148 /* This closes the leak, but don't free any other leaks! */
149 ok1(!talloc_chunk_from_ptr(null_context)->child);
150 talloc_disable_null_tracking();
152 failtest_exit(exit_status());