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 int fail_destructor(void *ptr)
72 miscellaneous tests to try to get a higher test coverage percentage
74 static bool test_misc(const struct torture_context *ctx)
82 root = talloc_new(ctx);
86 p1 = talloc_size(root, 0x7fffffff);
87 torture_assert("misc", !p1, "failed: large talloc allowed\n");
89 p1 = talloc_strdup(root, "foo");
93 if (talloc_increase_ref_count(p1) != 0)
95 if (talloc_increase_ref_count(p1) != 0)
97 if (talloc_increase_ref_count(p1) != 0)
99 CHECK_BLOCKS("misc", p1, 1);
100 CHECK_BLOCKS("misc", root, 2);
102 CHECK_BLOCKS("misc", p1, 1);
103 CHECK_BLOCKS("misc", root, 2);
104 talloc_unlink(NULL, p1);
105 CHECK_BLOCKS("misc", p1, 1);
106 CHECK_BLOCKS("misc", root, 2);
107 p2 = talloc_strdup(p1, "foo");
110 torture_assert("misc", talloc_unlink(root, p2) == -1,
111 "failed: talloc_unlink() of non-reference context should return -1\n");
112 torture_assert("misc", talloc_unlink(p1, p2) == 0,
113 "failed: talloc_unlink() of parent should succeed\n");
115 CHECK_BLOCKS("misc", p1, 1);
116 CHECK_BLOCKS("misc", root, 2);
118 name = talloc_set_name(p1, "my name is %s", "foo");
121 torture_assert_str_equal("misc", talloc_get_name(p1), "my name is foo",
122 "failed: wrong name after talloc_set_name(my name is foo)");
123 CHECK_BLOCKS("misc", p1, 2);
124 CHECK_BLOCKS("misc", root, 3);
126 talloc_set_name_const(p1, NULL);
127 torture_assert_str_equal ("misc", talloc_get_name(p1), "UNNAMED",
128 "failed: wrong name after talloc_set_name(NULL)");
129 CHECK_BLOCKS("misc", p1, 2);
130 CHECK_BLOCKS("misc", root, 3);
132 torture_assert("misc", talloc_free(NULL) == -1,
133 "talloc_free(NULL) should give -1\n");
135 talloc_set_destructor(p1, fail_destructor);
136 torture_assert("misc", talloc_free(p1) == -1,
137 "Failed destructor should cause talloc_free to fail\n");
138 talloc_set_destructor(p1, NULL);
140 p2 = (char *)talloc_zero_size(p1, 20);
143 torture_assert("misc", p2[19] == 0, "Failed to give zero memory\n");
146 torture_assert("misc", talloc_strdup(root, NULL) == NULL,
147 "failed: strdup on NULL should give NULL\n");
149 p2 = talloc_strndup(p1, "foo", 2);
152 torture_assert("misc", strcmp("fo", p2) == 0,
153 "strndup doesn't work\n");
154 p2 = talloc_asprintf_append(p2, "o%c", 'd');
157 torture_assert("misc", strcmp("food", p2) == 0,
158 "talloc_asprintf_append doesn't work\n");
159 CHECK_BLOCKS("misc", p2, 1);
160 CHECK_BLOCKS("misc", p1, 3);
162 p2 = talloc_asprintf_append(NULL, "hello %s", "world");
165 torture_assert("misc", strcmp("hello world", p2) == 0,
166 "talloc_asprintf_append doesn't work\n");
167 CHECK_BLOCKS("misc", p2, 1);
168 CHECK_BLOCKS("misc", p1, 3);
171 d = talloc_array(p1, double, 0x20000000);
172 torture_assert("misc", !d, "failed: integer overflow not detected\n");
174 d = talloc_realloc(p1, d, double, 0x20000000);
175 torture_assert("misc", !d, "failed: integer overflow not detected\n");
178 CHECK_BLOCKS("misc", root, 1);
180 p1 = talloc_named(root, 100, "%d bytes", 100);
183 CHECK_BLOCKS("misc", p1, 2);
184 CHECK_BLOCKS("misc", root, 3);
185 talloc_unlink(root, p1);
187 p1 = talloc_init("%d bytes", 200);
190 p2 = talloc_asprintf(p1, "my test '%s'", "string");
193 torture_assert_str_equal("misc", p2, "my test 'string'",
194 "failed: talloc_asprintf(\"my test '%%s'\", \"string\") gave: \"%s\"");
195 CHECK_BLOCKS("misc", p1, 3);
196 CHECK_SIZE("misc", p2, 17);
197 CHECK_BLOCKS("misc", root, 1);
198 talloc_unlink(NULL, p1);
200 p1 = talloc_named_const(root, 10, "p1");
203 p2 = (char *)talloc_named_const(root, 20, "p2");
206 if (!talloc_reference(p1, p2))
208 talloc_unlink(root, p2);
209 CHECK_BLOCKS("misc", p2, 1);
210 CHECK_BLOCKS("misc", p1, 2);
211 CHECK_BLOCKS("misc", root, 3);
212 talloc_unlink(p1, p2);
213 talloc_unlink(root, p1);
215 p1 = talloc_named_const(root, 10, "p1");
218 p2 = (char *)talloc_named_const(root, 20, "p2");
221 if (!talloc_reference(NULL, p2))
223 talloc_unlink(root, p2);
224 CHECK_BLOCKS("misc", p2, 1);
225 CHECK_BLOCKS("misc", p1, 1);
226 CHECK_BLOCKS("misc", root, 2);
227 talloc_unlink(NULL, p2);
228 talloc_unlink(root, p1);
230 /* Test that talloc_unlink is a no-op */
232 torture_assert("misc", talloc_unlink(root, NULL) == -1,
233 "failed: talloc_unlink(root, NULL) == -1\n");
235 CHECK_SIZE("misc", root, 0);
237 talloc_enable_leak_report();
238 talloc_enable_leak_report_full();
245 int main(int argc, char *argv[])
248 failtest_init(argc, argv);
250 talloc_enable_null_tracking();
252 ok1(test_misc(NULL));
253 /* This closes the leak, but don't free any other leaks! */
254 ok1(!talloc_chunk_from_ptr(null_context)->child);
255 talloc_disable_null_tracking();
257 failtest_exit(exit_status());