]> git.ozlabs.org Git - ccan/blob - ccan/talloc/test/run-test_ref.c
base64: fix for unsigned chars (e.g. ARM).
[ccan] / ccan / talloc / test / run-test_ref.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    local testing of talloc routines.
5
6    Copyright (C) Andrew Tridgell 2004
7    Converted to ccan tests by Rusty Russell 2008
8    
9      ** NOTE! The following LGPL license applies to the talloc
10      ** library. This does NOT imply that all of Samba is released
11      ** under the LGPL
12    
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.
17
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.
22
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
26 */
27
28 #include <ccan/failtest/failtest_override.h>
29 #include <ccan/talloc/talloc.c>
30 #include <stdbool.h>
31 #include <ccan/tap/tap.h>
32 #include <ccan/failtest/failtest.h>
33
34 #define torture_assert(test, expr, str)                                 \
35         ok(expr, "%s [\n%s: Expression %s failed: %s\n]\n",             \
36            test, __location__, #expr, str)
37
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)
42
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",      \
46            test, #ptr,                                                  \
47            (unsigned)talloc_total_size(ptr),                            \
48            (unsigned)tsize)
49
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",    \
53            test, #ptr,                                                  \
54            (unsigned)talloc_total_blocks(ptr),                          \
55            (unsigned)tblocks)
56
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",     \
60            test, #ptr,                                                  \
61            talloc_parent(ptr),                                          \
62            (parent))
63
64 struct torture_context;
65
66 /*
67   test references 
68 */
69 static bool test_ref1(const struct torture_context *ctx)
70 {
71         void *root, *p1, *p2, *ref, *r1;
72         bool ret = false;
73
74         root = talloc_named_const(ctx, 0, "root");
75         if (!root)
76                 goto out;
77         p1 = talloc_named_const(root, 1, "p1");
78         if (!p1)
79                 goto out;
80         p2 = talloc_named_const(p1, 1, "p2");
81         if (!p2)
82                 goto out;
83         if (!talloc_named_const(p1, 1, "x1"))
84                 goto out;
85         if (!talloc_named_const(p1, 2, "x2"))
86                 goto out;
87         if (!talloc_named_const(p1, 3, "x3"))
88                 goto out;
89
90         r1 = talloc_named_const(root, 1, "r1"); 
91         if (!r1)
92                 goto out;
93         ref = talloc_reference(r1, p2);
94         if (!ref)
95                 goto out;
96
97         CHECK_BLOCKS("ref1", p1, 5);
98         CHECK_BLOCKS("ref1", p2, 1);
99         CHECK_BLOCKS("ref1", r1, 2);
100
101         talloc_free(p2);
102
103         CHECK_BLOCKS("ref1", p1, 5);
104         CHECK_BLOCKS("ref1", p2, 1);
105         CHECK_BLOCKS("ref1", r1, 1);
106
107         talloc_free(p1);
108
109         CHECK_BLOCKS("ref1", r1, 1);
110
111         talloc_free(r1);
112
113         if (talloc_reference(root, NULL)) {
114                 return false;
115         }
116
117         CHECK_BLOCKS("ref1", root, 1);
118
119         CHECK_SIZE("ref1", root, 0);
120         ret = true;
121 out:
122         talloc_free(root);
123         return ret;
124 }
125
126 /*
127   test references 
128 */
129 static bool test_ref2(const struct torture_context *ctx)
130 {
131         void *root, *p1, *p2, *ref, *r1;
132         bool ret = false;
133
134         root = talloc_named_const(ctx, 0, "root");
135         if (!root)
136                 goto out;
137         p1 = talloc_named_const(root, 1, "p1");
138         if (!p1)
139                 goto out;
140         if (!talloc_named_const(p1, 1, "x1"))
141                 goto out;
142         if (!talloc_named_const(p1, 1, "x2"))
143                 goto out;
144         if (!talloc_named_const(p1, 1, "x3"))
145                 goto out;
146         p2 = talloc_named_const(p1, 1, "p2");
147         if (!p2)
148                 goto out;
149
150         r1 = talloc_named_const(root, 1, "r1"); 
151         if (!r1)
152                 goto out;
153         ref = talloc_reference(r1, p2);
154         if (!ref)
155                 goto out;
156
157         CHECK_BLOCKS("ref2", p1, 5);
158         CHECK_BLOCKS("ref2", p2, 1);
159         CHECK_BLOCKS("ref2", r1, 2);
160
161         talloc_free(ref);
162
163         CHECK_BLOCKS("ref2", p1, 5);
164         CHECK_BLOCKS("ref2", p2, 1);
165         CHECK_BLOCKS("ref2", r1, 1);
166
167         talloc_free(p2);
168
169         CHECK_BLOCKS("ref2", p1, 4);
170         CHECK_BLOCKS("ref2", r1, 1);
171
172         talloc_free(p1);
173
174         CHECK_BLOCKS("ref2", r1, 1);
175
176         talloc_free(r1);
177
178         CHECK_SIZE("ref2", root, 0);
179         ret = true;
180
181 out:
182         talloc_free(root);
183         return ret;
184 }
185
186 /*
187   test references 
188 */
189 static bool test_ref3(const struct torture_context *ctx)
190 {
191         void *root, *p1, *p2, *ref, *r1;
192         bool ret = false;
193
194         root = talloc_named_const(ctx, 0, "root");
195         if (!root)
196                 goto out;
197         p1 = talloc_named_const(root, 1, "p1");
198         if (!p1)
199                 goto out;
200         p2 = talloc_named_const(root, 1, "p2");
201         if (!p2)
202                 goto out;
203         r1 = talloc_named_const(p1, 1, "r1");
204         if (!r1)
205                 goto out;
206         ref = talloc_reference(p2, r1);
207         if (!ref)
208                 goto out;
209
210         CHECK_BLOCKS("ref3", p1, 2);
211         CHECK_BLOCKS("ref3", p2, 2);
212         CHECK_BLOCKS("ref3", r1, 1);
213
214         talloc_free(p1);
215
216         CHECK_BLOCKS("ref3", p2, 2);
217         CHECK_BLOCKS("ref3", r1, 1);
218
219         talloc_free(p2);
220
221         CHECK_SIZE("ref3", root, 0);
222
223         ret = true;
224 out:
225         talloc_free(root);
226
227         return ret;
228 }
229
230 /*
231   test references 
232 */
233 static bool test_ref4(const struct torture_context *ctx)
234 {
235         void *root, *p1, *p2, *ref, *r1;
236         bool ret = false;
237
238         root = talloc_named_const(ctx, 0, "root");
239         if (!root)
240                 goto out;
241         p1 = talloc_named_const(root, 1, "p1");
242         if (!p1)
243                 goto out;
244         if (!talloc_named_const(p1, 1, "x1"))
245                 goto out;
246         if (!talloc_named_const(p1, 1, "x2"))
247                 goto out;
248         if (!talloc_named_const(p1, 1, "x3"))
249                 goto out;
250         p2 = talloc_named_const(p1, 1, "p2");
251         if (!p2)
252                 goto out;
253
254         r1 = talloc_named_const(root, 1, "r1"); 
255         if (!r1)
256                 goto out;
257         ref = talloc_reference(r1, p2);
258         if (!ref)
259                 goto out;
260
261         CHECK_BLOCKS("ref4", p1, 5);
262         CHECK_BLOCKS("ref4", p2, 1);
263         CHECK_BLOCKS("ref4", r1, 2);
264
265         talloc_free(r1);
266
267         CHECK_BLOCKS("ref4", p1, 5);
268         CHECK_BLOCKS("ref4", p2, 1);
269
270         talloc_free(p2);
271
272         CHECK_BLOCKS("ref4", p1, 4);
273
274         talloc_free(p1);
275
276         CHECK_SIZE("ref4", root, 0);
277
278         ret = true;
279 out:
280         talloc_free(root);
281
282         return ret;
283 }
284
285 int main(int argc, char *argv[])
286 {
287         plan_tests(34);
288         failtest_init(argc, argv);
289
290         talloc_enable_null_tracking();
291         if (null_context) {
292                 ok1(test_ref1(NULL)
293                     && test_ref2(NULL)
294                     && test_ref3(NULL)
295                     && test_ref4(NULL));
296                 /* This closes the leak, but don't free any other leaks! */
297                 ok1(!talloc_chunk_from_ptr(null_context)->child);
298                 talloc_disable_null_tracking();
299         }
300         failtest_exit(exit_status());
301 }
302