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 3 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, see <http://www.gnu.org/licenses/>.
|
---|
24 | */
|
---|
25 |
|
---|
26 | #include "replace.h"
|
---|
27 | #include "system/time.h"
|
---|
28 | #include "talloc.h"
|
---|
29 |
|
---|
30 | static struct timeval timeval_current(void)
|
---|
31 | {
|
---|
32 | struct timeval tv;
|
---|
33 | gettimeofday(&tv, NULL);
|
---|
34 | return tv;
|
---|
35 | }
|
---|
36 |
|
---|
37 | static double timeval_elapsed(struct timeval *tv)
|
---|
38 | {
|
---|
39 | struct timeval tv2 = timeval_current();
|
---|
40 | return (tv2.tv_sec - tv->tv_sec) +
|
---|
41 | (tv2.tv_usec - tv->tv_usec)*1.0e-6;
|
---|
42 | }
|
---|
43 |
|
---|
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); \
|
---|
47 | return false; \
|
---|
48 | }
|
---|
49 |
|
---|
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); \
|
---|
54 | return false; \
|
---|
55 | }
|
---|
56 |
|
---|
57 | #if _SAMBA_BUILD_==3
|
---|
58 | #ifdef malloc
|
---|
59 | #undef malloc
|
---|
60 | #endif
|
---|
61 | #ifdef strdup
|
---|
62 | #undef strdup
|
---|
63 | #endif
|
---|
64 | #endif
|
---|
65 |
|
---|
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", \
|
---|
69 | test, #ptr, \
|
---|
70 | (unsigned)talloc_total_size(ptr), \
|
---|
71 | (unsigned)tsize); \
|
---|
72 | talloc_report_full(ptr, stdout); \
|
---|
73 | return false; \
|
---|
74 | } \
|
---|
75 | } while (0)
|
---|
76 |
|
---|
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", \
|
---|
80 | test, #ptr, \
|
---|
81 | (unsigned)talloc_total_blocks(ptr), \
|
---|
82 | (unsigned)tblocks); \
|
---|
83 | talloc_report_full(ptr, stdout); \
|
---|
84 | return false; \
|
---|
85 | } \
|
---|
86 | } while (0)
|
---|
87 |
|
---|
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", \
|
---|
91 | test, #ptr, \
|
---|
92 | talloc_parent(ptr), \
|
---|
93 | (parent)); \
|
---|
94 | talloc_report_full(ptr, stdout); \
|
---|
95 | talloc_report_full(parent, stdout); \
|
---|
96 | talloc_report_full(NULL, stdout); \
|
---|
97 | return false; \
|
---|
98 | } \
|
---|
99 | } while (0)
|
---|
100 |
|
---|
101 |
|
---|
102 | /*
|
---|
103 | test references
|
---|
104 | */
|
---|
105 | static bool test_ref1(void)
|
---|
106 | {
|
---|
107 | void *root, *p1, *p2, *ref, *r1;
|
---|
108 |
|
---|
109 | printf("test: ref1\n# SINGLE REFERENCE FREE\n");
|
---|
110 |
|
---|
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");
|
---|
117 |
|
---|
118 | r1 = talloc_named_const(root, 1, "r1");
|
---|
119 | ref = talloc_reference(r1, p2);
|
---|
120 | talloc_report_full(root, stderr);
|
---|
121 |
|
---|
122 | CHECK_BLOCKS("ref1", p1, 5);
|
---|
123 | CHECK_BLOCKS("ref1", p2, 1);
|
---|
124 | CHECK_BLOCKS("ref1", r1, 2);
|
---|
125 |
|
---|
126 | fprintf(stderr, "Freeing p2\n");
|
---|
127 | talloc_free(p2);
|
---|
128 | talloc_report_full(root, stderr);
|
---|
129 |
|
---|
130 | CHECK_BLOCKS("ref1", p1, 5);
|
---|
131 | CHECK_BLOCKS("ref1", p2, 1);
|
---|
132 | CHECK_BLOCKS("ref1", r1, 1);
|
---|
133 |
|
---|
134 | fprintf(stderr, "Freeing p1\n");
|
---|
135 | talloc_free(p1);
|
---|
136 | talloc_report_full(root, stderr);
|
---|
137 |
|
---|
138 | CHECK_BLOCKS("ref1", r1, 1);
|
---|
139 |
|
---|
140 | fprintf(stderr, "Freeing r1\n");
|
---|
141 | talloc_free(r1);
|
---|
142 | talloc_report_full(NULL, stderr);
|
---|
143 |
|
---|
144 | fprintf(stderr, "Testing NULL\n");
|
---|
145 | if (talloc_reference(root, NULL)) {
|
---|
146 | return false;
|
---|
147 | }
|
---|
148 |
|
---|
149 | CHECK_BLOCKS("ref1", root, 1);
|
---|
150 |
|
---|
151 | CHECK_SIZE("ref1", root, 0);
|
---|
152 |
|
---|
153 | talloc_free(root);
|
---|
154 | printf("success: ref1\n");
|
---|
155 | return true;
|
---|
156 | }
|
---|
157 |
|
---|
158 | /*
|
---|
159 | test references
|
---|
160 | */
|
---|
161 | static bool test_ref2(void)
|
---|
162 | {
|
---|
163 | void *root, *p1, *p2, *ref, *r1;
|
---|
164 |
|
---|
165 | printf("test: ref2\n# DOUBLE REFERENCE FREE\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");
|
---|
172 |
|
---|
173 | r1 = talloc_named_const(root, 1, "r1");
|
---|
174 | ref = talloc_reference(r1, p2);
|
---|
175 | talloc_report_full(root, stderr);
|
---|
176 |
|
---|
177 | CHECK_BLOCKS("ref2", p1, 5);
|
---|
178 | CHECK_BLOCKS("ref2", p2, 1);
|
---|
179 | CHECK_BLOCKS("ref2", r1, 2);
|
---|
180 |
|
---|
181 | fprintf(stderr, "Freeing ref\n");
|
---|
182 | talloc_free(ref);
|
---|
183 | talloc_report_full(root, stderr);
|
---|
184 |
|
---|
185 | CHECK_BLOCKS("ref2", p1, 5);
|
---|
186 | CHECK_BLOCKS("ref2", p2, 1);
|
---|
187 | CHECK_BLOCKS("ref2", r1, 1);
|
---|
188 |
|
---|
189 | fprintf(stderr, "Freeing p2\n");
|
---|
190 | talloc_free(p2);
|
---|
191 | talloc_report_full(root, stderr);
|
---|
192 |
|
---|
193 | CHECK_BLOCKS("ref2", p1, 4);
|
---|
194 | CHECK_BLOCKS("ref2", r1, 1);
|
---|
195 |
|
---|
196 | fprintf(stderr, "Freeing p1\n");
|
---|
197 | talloc_free(p1);
|
---|
198 | talloc_report_full(root, stderr);
|
---|
199 |
|
---|
200 | CHECK_BLOCKS("ref2", r1, 1);
|
---|
201 |
|
---|
202 | fprintf(stderr, "Freeing r1\n");
|
---|
203 | talloc_free(r1);
|
---|
204 | talloc_report_full(root, stderr);
|
---|
205 |
|
---|
206 | CHECK_SIZE("ref2", root, 0);
|
---|
207 |
|
---|
208 | talloc_free(root);
|
---|
209 | printf("success: ref2\n");
|
---|
210 | return true;
|
---|
211 | }
|
---|
212 |
|
---|
213 | /*
|
---|
214 | test references
|
---|
215 | */
|
---|
216 | static bool test_ref3(void)
|
---|
217 | {
|
---|
218 | void *root, *p1, *p2, *ref, *r1;
|
---|
219 |
|
---|
220 | printf("test: ref3\n# PARENT REFERENCE FREE\n");
|
---|
221 |
|
---|
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);
|
---|
228 |
|
---|
229 | CHECK_BLOCKS("ref3", p1, 2);
|
---|
230 | CHECK_BLOCKS("ref3", p2, 2);
|
---|
231 | CHECK_BLOCKS("ref3", r1, 1);
|
---|
232 |
|
---|
233 | fprintf(stderr, "Freeing p1\n");
|
---|
234 | talloc_free(p1);
|
---|
235 | talloc_report_full(root, stderr);
|
---|
236 |
|
---|
237 | CHECK_BLOCKS("ref3", p2, 2);
|
---|
238 | CHECK_BLOCKS("ref3", r1, 1);
|
---|
239 |
|
---|
240 | fprintf(stderr, "Freeing p2\n");
|
---|
241 | talloc_free(p2);
|
---|
242 | talloc_report_full(root, stderr);
|
---|
243 |
|
---|
244 | CHECK_SIZE("ref3", root, 0);
|
---|
245 |
|
---|
246 | talloc_free(root);
|
---|
247 |
|
---|
248 | printf("success: ref3\n");
|
---|
249 | return true;
|
---|
250 | }
|
---|
251 |
|
---|
252 | /*
|
---|
253 | test references
|
---|
254 | */
|
---|
255 | static bool test_ref4(void)
|
---|
256 | {
|
---|
257 | void *root, *p1, *p2, *ref, *r1;
|
---|
258 |
|
---|
259 | printf("test: ref4\n# REFERRER REFERENCE FREE\n");
|
---|
260 |
|
---|
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");
|
---|
267 |
|
---|
268 | r1 = talloc_named_const(root, 1, "r1");
|
---|
269 | ref = talloc_reference(r1, p2);
|
---|
270 | talloc_report_full(root, stderr);
|
---|
271 |
|
---|
272 | CHECK_BLOCKS("ref4", p1, 5);
|
---|
273 | CHECK_BLOCKS("ref4", p2, 1);
|
---|
274 | CHECK_BLOCKS("ref4", r1, 2);
|
---|
275 |
|
---|
276 | fprintf(stderr, "Freeing r1\n");
|
---|
277 | talloc_free(r1);
|
---|
278 | talloc_report_full(root, stderr);
|
---|
279 |
|
---|
280 | CHECK_BLOCKS("ref4", p1, 5);
|
---|
281 | CHECK_BLOCKS("ref4", p2, 1);
|
---|
282 |
|
---|
283 | fprintf(stderr, "Freeing p2\n");
|
---|
284 | talloc_free(p2);
|
---|
285 | talloc_report_full(root, stderr);
|
---|
286 |
|
---|
287 | CHECK_BLOCKS("ref4", p1, 4);
|
---|
288 |
|
---|
289 | fprintf(stderr, "Freeing p1\n");
|
---|
290 | talloc_free(p1);
|
---|
291 | talloc_report_full(root, stderr);
|
---|
292 |
|
---|
293 | CHECK_SIZE("ref4", root, 0);
|
---|
294 |
|
---|
295 | talloc_free(root);
|
---|
296 |
|
---|
297 | printf("success: ref4\n");
|
---|
298 | return true;
|
---|
299 | }
|
---|
300 |
|
---|
301 |
|
---|
302 | /*
|
---|
303 | test references
|
---|
304 | */
|
---|
305 | static bool test_unlink1(void)
|
---|
306 | {
|
---|
307 | void *root, *p1, *p2, *ref, *r1;
|
---|
308 |
|
---|
309 | printf("test: unlink\n# UNLINK\n");
|
---|
310 |
|
---|
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");
|
---|
317 |
|
---|
318 | r1 = talloc_named_const(p1, 1, "r1");
|
---|
319 | ref = talloc_reference(r1, p2);
|
---|
320 | talloc_report_full(root, stderr);
|
---|
321 |
|
---|
322 | CHECK_BLOCKS("unlink", p1, 7);
|
---|
323 | CHECK_BLOCKS("unlink", p2, 1);
|
---|
324 | CHECK_BLOCKS("unlink", r1, 2);
|
---|
325 |
|
---|
326 | fprintf(stderr, "Unreferencing r1\n");
|
---|
327 | talloc_unlink(r1, p2);
|
---|
328 | talloc_report_full(root, stderr);
|
---|
329 |
|
---|
330 | CHECK_BLOCKS("unlink", p1, 6);
|
---|
331 | CHECK_BLOCKS("unlink", p2, 1);
|
---|
332 | CHECK_BLOCKS("unlink", r1, 1);
|
---|
333 |
|
---|
334 | fprintf(stderr, "Freeing p1\n");
|
---|
335 | talloc_free(p1);
|
---|
336 | talloc_report_full(root, stderr);
|
---|
337 |
|
---|
338 | CHECK_SIZE("unlink", root, 0);
|
---|
339 |
|
---|
340 | talloc_free(root);
|
---|
341 |
|
---|
342 | printf("success: unlink\n");
|
---|
343 | return true;
|
---|
344 | }
|
---|
345 |
|
---|
346 | static int fail_destructor(void *ptr)
|
---|
347 | {
|
---|
348 | return -1;
|
---|
349 | }
|
---|
350 |
|
---|
351 | /*
|
---|
352 | miscellaneous tests to try to get a higher test coverage percentage
|
---|
353 | */
|
---|
354 | static bool test_misc(void)
|
---|
355 | {
|
---|
356 | void *root, *p1;
|
---|
357 | char *p2;
|
---|
358 | double *d;
|
---|
359 | const char *name;
|
---|
360 |
|
---|
361 | printf("test: misc\n# MISCELLANEOUS\n");
|
---|
362 |
|
---|
363 | root = talloc_new(NULL);
|
---|
364 |
|
---|
365 | p1 = talloc_size(root, 0x7fffffff);
|
---|
366 | torture_assert("misc", !p1, "failed: large talloc allowed\n");
|
---|
367 |
|
---|
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);
|
---|
374 | talloc_free(p1);
|
---|
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");
|
---|
385 | talloc_free(p1);
|
---|
386 | CHECK_BLOCKS("misc", p1, 1);
|
---|
387 | CHECK_BLOCKS("misc", root, 2);
|
---|
388 |
|
---|
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);
|
---|
394 |
|
---|
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);
|
---|
400 |
|
---|
401 | torture_assert("misc", talloc_free(NULL) == -1,
|
---|
402 | "talloc_free(NULL) should give -1\n");
|
---|
403 |
|
---|
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);
|
---|
408 |
|
---|
409 | talloc_report(root, stderr);
|
---|
410 |
|
---|
411 |
|
---|
412 | p2 = (char *)talloc_zero_size(p1, 20);
|
---|
413 | torture_assert("misc", p2[19] == 0, "Failed to give zero memory\n");
|
---|
414 | talloc_free(p2);
|
---|
415 |
|
---|
416 | torture_assert("misc", talloc_strdup(root, NULL) == NULL,
|
---|
417 | "failed: strdup on NULL should give NULL\n");
|
---|
418 |
|
---|
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_buffer(p2, "o%c", 'd');
|
---|
423 | torture_assert("misc", strcmp("food", p2) == 0,
|
---|
424 | "talloc_asprintf_append_buffer doesn't work\n");
|
---|
425 | CHECK_BLOCKS("misc", p2, 1);
|
---|
426 | CHECK_BLOCKS("misc", p1, 3);
|
---|
427 |
|
---|
428 | p2 = talloc_asprintf_append_buffer(NULL, "hello %s", "world");
|
---|
429 | torture_assert("misc", strcmp("hello world", p2) == 0,
|
---|
430 | "talloc_asprintf_append_buffer doesn't work\n");
|
---|
431 | CHECK_BLOCKS("misc", p2, 1);
|
---|
432 | CHECK_BLOCKS("misc", p1, 3);
|
---|
433 | talloc_free(p2);
|
---|
434 |
|
---|
435 | d = talloc_array(p1, double, 0x20000000);
|
---|
436 | torture_assert("misc", !d, "failed: integer overflow not detected\n");
|
---|
437 |
|
---|
438 | d = talloc_realloc(p1, d, double, 0x20000000);
|
---|
439 | torture_assert("misc", !d, "failed: integer overflow not detected\n");
|
---|
440 |
|
---|
441 | talloc_free(p1);
|
---|
442 | CHECK_BLOCKS("misc", root, 1);
|
---|
443 |
|
---|
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);
|
---|
448 |
|
---|
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);
|
---|
457 |
|
---|
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);
|
---|
469 |
|
---|
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);
|
---|
481 |
|
---|
482 | /* Test that talloc_unlink is a no-op */
|
---|
483 |
|
---|
484 | torture_assert("misc", talloc_unlink(root, NULL) == -1,
|
---|
485 | "failed: talloc_unlink(root, NULL) == -1\n");
|
---|
486 |
|
---|
487 | talloc_report(root, stderr);
|
---|
488 | talloc_report(NULL, stderr);
|
---|
489 |
|
---|
490 | CHECK_SIZE("misc", root, 0);
|
---|
491 |
|
---|
492 | talloc_free(root);
|
---|
493 |
|
---|
494 | CHECK_SIZE("misc", NULL, 0);
|
---|
495 |
|
---|
496 | talloc_enable_leak_report();
|
---|
497 | talloc_enable_leak_report_full();
|
---|
498 |
|
---|
499 | printf("success: misc\n");
|
---|
500 |
|
---|
501 | return true;
|
---|
502 | }
|
---|
503 |
|
---|
504 |
|
---|
505 | /*
|
---|
506 | test realloc
|
---|
507 | */
|
---|
508 | static bool test_realloc(void)
|
---|
509 | {
|
---|
510 | void *root, *p1, *p2;
|
---|
511 |
|
---|
512 | printf("test: realloc\n# REALLOC\n");
|
---|
513 |
|
---|
514 | root = talloc_new(NULL);
|
---|
515 |
|
---|
516 | p1 = talloc_size(root, 10);
|
---|
517 | CHECK_SIZE("realloc", p1, 10);
|
---|
518 |
|
---|
519 | p1 = talloc_realloc_size(NULL, p1, 20);
|
---|
520 | CHECK_SIZE("realloc", p1, 20);
|
---|
521 |
|
---|
522 | talloc_new(p1);
|
---|
523 |
|
---|
524 | p2 = talloc_realloc_size(p1, NULL, 30);
|
---|
525 |
|
---|
526 | talloc_new(p1);
|
---|
527 |
|
---|
528 | p2 = talloc_realloc_size(p1, p2, 40);
|
---|
529 |
|
---|
530 | CHECK_SIZE("realloc", p2, 40);
|
---|
531 | CHECK_SIZE("realloc", root, 60);
|
---|
532 | CHECK_BLOCKS("realloc", p1, 4);
|
---|
533 |
|
---|
534 | p1 = talloc_realloc_size(NULL, p1, 20);
|
---|
535 | CHECK_SIZE("realloc", p1, 60);
|
---|
536 |
|
---|
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);
|
---|
541 |
|
---|
542 | talloc_realloc_size(NULL, p2, 0);
|
---|
543 | talloc_realloc_size(NULL, p2, 0);
|
---|
544 | CHECK_BLOCKS("realloc", p1, 3);
|
---|
545 |
|
---|
546 | torture_assert("realloc", talloc_realloc_size(NULL, p1, 0x7fffffff) == NULL,
|
---|
547 | "failed: oversize talloc should fail\n");
|
---|
548 |
|
---|
549 | talloc_realloc_size(NULL, p1, 0);
|
---|
550 |
|
---|
551 | CHECK_BLOCKS("realloc", root, 1);
|
---|
552 | CHECK_SIZE("realloc", root, 0);
|
---|
553 |
|
---|
554 | talloc_free(root);
|
---|
555 |
|
---|
556 | printf("success: realloc\n");
|
---|
557 |
|
---|
558 | return true;
|
---|
559 | }
|
---|
560 |
|
---|
561 | /*
|
---|
562 | test realloc with a child
|
---|
563 | */
|
---|
564 | static bool test_realloc_child(void)
|
---|
565 | {
|
---|
566 | void *root;
|
---|
567 | struct el2 {
|
---|
568 | const char *name;
|
---|
569 | } *el2;
|
---|
570 | struct el1 {
|
---|
571 | int count;
|
---|
572 | struct el2 **list, **list2, **list3;
|
---|
573 | } *el1;
|
---|
574 |
|
---|
575 | printf("test: REALLOC WITH CHILD\n");
|
---|
576 |
|
---|
577 | root = talloc_new(NULL);
|
---|
578 |
|
---|
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");
|
---|
583 |
|
---|
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");
|
---|
587 |
|
---|
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");
|
---|
591 |
|
---|
592 | el2 = talloc(el1->list, struct el2);
|
---|
593 | el2 = talloc(el1->list2, struct el2);
|
---|
594 | el2 = talloc(el1->list3, struct el2);
|
---|
595 |
|
---|
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);
|
---|
599 |
|
---|
600 | talloc_free(root);
|
---|
601 |
|
---|
602 | printf("success: REALLOC WITH CHILD\n");
|
---|
603 | return true;
|
---|
604 | }
|
---|
605 |
|
---|
606 | /*
|
---|
607 | test type checking
|
---|
608 | */
|
---|
609 | static bool test_type(void)
|
---|
610 | {
|
---|
611 | void *root;
|
---|
612 | struct el1 {
|
---|
613 | int count;
|
---|
614 | };
|
---|
615 | struct el2 {
|
---|
616 | int count;
|
---|
617 | };
|
---|
618 | struct el1 *el1;
|
---|
619 |
|
---|
620 | printf("test: type\n# talloc type checking\n");
|
---|
621 |
|
---|
622 | root = talloc_new(NULL);
|
---|
623 |
|
---|
624 | el1 = talloc(root, struct el1);
|
---|
625 |
|
---|
626 | el1->count = 1;
|
---|
627 |
|
---|
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");
|
---|
635 |
|
---|
636 | talloc_free(root);
|
---|
637 |
|
---|
638 | printf("success: type\n");
|
---|
639 | return true;
|
---|
640 | }
|
---|
641 |
|
---|
642 | /*
|
---|
643 | test steal
|
---|
644 | */
|
---|
645 | static bool test_steal(void)
|
---|
646 | {
|
---|
647 | void *root, *p1, *p2;
|
---|
648 |
|
---|
649 | printf("test: steal\n# STEAL\n");
|
---|
650 |
|
---|
651 | root = talloc_new(NULL);
|
---|
652 |
|
---|
653 | p1 = talloc_array(root, char, 10);
|
---|
654 | CHECK_SIZE("steal", p1, 10);
|
---|
655 |
|
---|
656 | p2 = talloc_realloc(root, NULL, char, 20);
|
---|
657 | CHECK_SIZE("steal", p1, 10);
|
---|
658 | CHECK_SIZE("steal", root, 30);
|
---|
659 |
|
---|
660 | torture_assert("steal", talloc_steal(p1, NULL) == NULL,
|
---|
661 | "failed: stealing NULL should give NULL\n");
|
---|
662 |
|
---|
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);
|
---|
667 |
|
---|
668 | talloc_steal(NULL, p1);
|
---|
669 | talloc_steal(NULL, p2);
|
---|
670 | CHECK_BLOCKS("steal", root, 1);
|
---|
671 | CHECK_SIZE("steal", root, 0);
|
---|
672 |
|
---|
673 | talloc_free(p1);
|
---|
674 | talloc_steal(root, p2);
|
---|
675 | CHECK_BLOCKS("steal", root, 2);
|
---|
676 | CHECK_SIZE("steal", root, 20);
|
---|
677 |
|
---|
678 | talloc_free(p2);
|
---|
679 |
|
---|
680 | CHECK_BLOCKS("steal", root, 1);
|
---|
681 | CHECK_SIZE("steal", root, 0);
|
---|
682 |
|
---|
683 | talloc_free(root);
|
---|
684 |
|
---|
685 | p1 = talloc_size(NULL, 3);
|
---|
686 | talloc_report_full(NULL, stderr);
|
---|
687 | CHECK_SIZE("steal", NULL, 3);
|
---|
688 | talloc_free(p1);
|
---|
689 |
|
---|
690 | printf("success: steal\n");
|
---|
691 | return true;
|
---|
692 | }
|
---|
693 |
|
---|
694 | /*
|
---|
695 | test move
|
---|
696 | */
|
---|
697 | static bool test_move(void)
|
---|
698 | {
|
---|
699 | void *root;
|
---|
700 | struct t_move {
|
---|
701 | char *p;
|
---|
702 | int *x;
|
---|
703 | } *t1, *t2;
|
---|
704 |
|
---|
705 | printf("test: move\n# MOVE\n");
|
---|
706 |
|
---|
707 | root = talloc_new(NULL);
|
---|
708 |
|
---|
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);
|
---|
713 | *t1->x = 42;
|
---|
714 |
|
---|
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");
|
---|
720 |
|
---|
721 | talloc_free(root);
|
---|
722 |
|
---|
723 | printf("success: move\n");
|
---|
724 |
|
---|
725 | return true;
|
---|
726 | }
|
---|
727 |
|
---|
728 | /*
|
---|
729 | test talloc_realloc_fn
|
---|
730 | */
|
---|
731 | static bool test_realloc_fn(void)
|
---|
732 | {
|
---|
733 | void *root, *p1;
|
---|
734 |
|
---|
735 | printf("test: realloc_fn\n# talloc_realloc_fn\n");
|
---|
736 |
|
---|
737 | root = talloc_new(NULL);
|
---|
738 |
|
---|
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);
|
---|
748 |
|
---|
749 | talloc_free(root);
|
---|
750 |
|
---|
751 | printf("success: realloc_fn\n");
|
---|
752 | return true;
|
---|
753 | }
|
---|
754 |
|
---|
755 |
|
---|
756 | static bool test_unref_reparent(void)
|
---|
757 | {
|
---|
758 | void *root, *p1, *p2, *c1;
|
---|
759 |
|
---|
760 | printf("test: unref_reparent\n# UNREFERENCE AFTER PARENT FREED\n");
|
---|
761 |
|
---|
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");
|
---|
765 |
|
---|
766 | c1 = talloc_named_const(p1, 1, "child");
|
---|
767 | talloc_reference(p2, c1);
|
---|
768 |
|
---|
769 | CHECK_PARENT("unref_reparent", c1, p1);
|
---|
770 |
|
---|
771 | talloc_free(p1);
|
---|
772 |
|
---|
773 | CHECK_PARENT("unref_reparent", c1, p2);
|
---|
774 |
|
---|
775 | talloc_unlink(p2, c1);
|
---|
776 |
|
---|
777 | CHECK_SIZE("unref_reparent", root, 1);
|
---|
778 |
|
---|
779 | talloc_free(p2);
|
---|
780 | talloc_free(root);
|
---|
781 |
|
---|
782 | printf("success: unref_reparent\n");
|
---|
783 | return true;
|
---|
784 | }
|
---|
785 |
|
---|
786 | /*
|
---|
787 | measure the speed of talloc versus malloc
|
---|
788 | */
|
---|
789 | static bool test_speed(void)
|
---|
790 | {
|
---|
791 | void *ctx = talloc_new(NULL);
|
---|
792 | unsigned count;
|
---|
793 | const int loop = 1000;
|
---|
794 | int i;
|
---|
795 | struct timeval tv;
|
---|
796 |
|
---|
797 | printf("test: speed\n# TALLOC VS MALLOC SPEED\n");
|
---|
798 |
|
---|
799 | tv = timeval_current();
|
---|
800 | count = 0;
|
---|
801 | do {
|
---|
802 | void *p1, *p2, *p3;
|
---|
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);
|
---|
807 | talloc_free(p1);
|
---|
808 | }
|
---|
809 | count += 3 * loop;
|
---|
810 | } while (timeval_elapsed(&tv) < 5.0);
|
---|
811 |
|
---|
812 | fprintf(stderr, "talloc: %.0f ops/sec\n", count/timeval_elapsed(&tv));
|
---|
813 |
|
---|
814 | talloc_free(ctx);
|
---|
815 |
|
---|
816 | ctx = talloc_pool(NULL, 1024);
|
---|
817 |
|
---|
818 | tv = timeval_current();
|
---|
819 | count = 0;
|
---|
820 | do {
|
---|
821 | void *p1, *p2, *p3;
|
---|
822 | for (i=0;i<loop;i++) {
|
---|
823 | p1 = talloc_size(ctx, loop % 100);
|
---|
824 | p2 = talloc_strdup(p1, "foo bar");
|
---|
825 | p3 = talloc_size(p1, 300);
|
---|
826 | talloc_free_children(ctx);
|
---|
827 | }
|
---|
828 | count += 3 * loop;
|
---|
829 | } while (timeval_elapsed(&tv) < 5.0);
|
---|
830 |
|
---|
831 | talloc_free(ctx);
|
---|
832 |
|
---|
833 | fprintf(stderr, "talloc_pool: %.0f ops/sec\n", count/timeval_elapsed(&tv));
|
---|
834 |
|
---|
835 | tv = timeval_current();
|
---|
836 | count = 0;
|
---|
837 | do {
|
---|
838 | void *p1, *p2, *p3;
|
---|
839 | for (i=0;i<loop;i++) {
|
---|
840 | p1 = malloc(loop % 100);
|
---|
841 | p2 = strdup("foo bar");
|
---|
842 | p3 = malloc(300);
|
---|
843 | free(p1);
|
---|
844 | free(p2);
|
---|
845 | free(p3);
|
---|
846 | }
|
---|
847 | count += 3 * loop;
|
---|
848 | } while (timeval_elapsed(&tv) < 5.0);
|
---|
849 | fprintf(stderr, "malloc: %.0f ops/sec\n", count/timeval_elapsed(&tv));
|
---|
850 |
|
---|
851 | printf("success: speed\n");
|
---|
852 |
|
---|
853 | return true;
|
---|
854 | }
|
---|
855 |
|
---|
856 | static bool test_lifeless(void)
|
---|
857 | {
|
---|
858 | void *top = talloc_new(NULL);
|
---|
859 | char *parent, *child;
|
---|
860 | void *child_owner = talloc_new(NULL);
|
---|
861 |
|
---|
862 | printf("test: lifeless\n# TALLOC_UNLINK LOOP\n");
|
---|
863 |
|
---|
864 | parent = talloc_strdup(top, "parent");
|
---|
865 | child = talloc_strdup(parent, "child");
|
---|
866 | (void)talloc_reference(child, parent);
|
---|
867 | (void)talloc_reference(child_owner, child);
|
---|
868 | talloc_report_full(top, stderr);
|
---|
869 | talloc_unlink(top, parent);
|
---|
870 | talloc_free(child);
|
---|
871 | talloc_report_full(top, stderr);
|
---|
872 | talloc_free(top);
|
---|
873 | talloc_free(child_owner);
|
---|
874 | talloc_free(child);
|
---|
875 |
|
---|
876 | printf("success: lifeless\n");
|
---|
877 | return true;
|
---|
878 | }
|
---|
879 |
|
---|
880 | static int loop_destructor_count;
|
---|
881 |
|
---|
882 | static int test_loop_destructor(char *ptr)
|
---|
883 | {
|
---|
884 | loop_destructor_count++;
|
---|
885 | return 0;
|
---|
886 | }
|
---|
887 |
|
---|
888 | static bool test_loop(void)
|
---|
889 | {
|
---|
890 | void *top = talloc_new(NULL);
|
---|
891 | char *parent;
|
---|
892 | struct req1 {
|
---|
893 | char *req2, *req3;
|
---|
894 | } *req1;
|
---|
895 |
|
---|
896 | printf("test: loop\n# TALLOC LOOP DESTRUCTION\n");
|
---|
897 |
|
---|
898 | parent = talloc_strdup(top, "parent");
|
---|
899 | req1 = talloc(parent, struct req1);
|
---|
900 | req1->req2 = talloc_strdup(req1, "req2");
|
---|
901 | talloc_set_destructor(req1->req2, test_loop_destructor);
|
---|
902 | req1->req3 = talloc_strdup(req1, "req3");
|
---|
903 | (void)talloc_reference(req1->req3, req1);
|
---|
904 | talloc_report_full(top, stderr);
|
---|
905 | talloc_free(parent);
|
---|
906 | talloc_report_full(top, stderr);
|
---|
907 | talloc_report_full(NULL, stderr);
|
---|
908 | talloc_free(top);
|
---|
909 |
|
---|
910 | torture_assert("loop", loop_destructor_count == 1,
|
---|
911 | "FAILED TO FIRE LOOP DESTRUCTOR\n");
|
---|
912 | loop_destructor_count = 0;
|
---|
913 |
|
---|
914 | printf("success: loop\n");
|
---|
915 | return true;
|
---|
916 | }
|
---|
917 |
|
---|
918 | static int fail_destructor_str(char *ptr)
|
---|
919 | {
|
---|
920 | return -1;
|
---|
921 | }
|
---|
922 |
|
---|
923 | static bool test_free_parent_deny_child(void)
|
---|
924 | {
|
---|
925 | void *top = talloc_new(NULL);
|
---|
926 | char *level1;
|
---|
927 | char *level2;
|
---|
928 | char *level3;
|
---|
929 |
|
---|
930 | printf("test: free_parent_deny_child\n# TALLOC FREE PARENT DENY CHILD\n");
|
---|
931 |
|
---|
932 | level1 = talloc_strdup(top, "level1");
|
---|
933 | level2 = talloc_strdup(level1, "level2");
|
---|
934 | level3 = talloc_strdup(level2, "level3");
|
---|
935 |
|
---|
936 | talloc_set_destructor(level3, fail_destructor_str);
|
---|
937 | talloc_free(level1);
|
---|
938 | talloc_set_destructor(level3, NULL);
|
---|
939 |
|
---|
940 | CHECK_PARENT("free_parent_deny_child", level3, top);
|
---|
941 |
|
---|
942 | talloc_free(top);
|
---|
943 |
|
---|
944 | printf("success: free_parent_deny_child\n");
|
---|
945 | return true;
|
---|
946 | }
|
---|
947 |
|
---|
948 | static bool test_talloc_ptrtype(void)
|
---|
949 | {
|
---|
950 | void *top = talloc_new(NULL);
|
---|
951 | struct struct1 {
|
---|
952 | int foo;
|
---|
953 | int bar;
|
---|
954 | } *s1, *s2, **s3, ***s4;
|
---|
955 | const char *location1;
|
---|
956 | const char *location2;
|
---|
957 | const char *location3;
|
---|
958 | const char *location4;
|
---|
959 |
|
---|
960 | printf("test: ptrtype\n# TALLOC PTRTYPE\n");
|
---|
961 |
|
---|
962 | s1 = talloc_ptrtype(top, s1);location1 = __location__;
|
---|
963 |
|
---|
964 | if (talloc_get_size(s1) != sizeof(struct struct1)) {
|
---|
965 | printf("failure: ptrtype [\n"
|
---|
966 | "talloc_ptrtype() allocated the wrong size %lu (should be %lu)\n"
|
---|
967 | "]\n", (unsigned long)talloc_get_size(s1),
|
---|
968 | (unsigned long)sizeof(struct struct1));
|
---|
969 | return false;
|
---|
970 | }
|
---|
971 |
|
---|
972 | if (strcmp(location1, talloc_get_name(s1)) != 0) {
|
---|
973 | printf("failure: ptrtype [\n"
|
---|
974 | "talloc_ptrtype() sets the wrong name '%s' (should be '%s')\n]\n",
|
---|
975 | talloc_get_name(s1), location1);
|
---|
976 | return false;
|
---|
977 | }
|
---|
978 |
|
---|
979 | s2 = talloc_array_ptrtype(top, s2, 10);location2 = __location__;
|
---|
980 |
|
---|
981 | if (talloc_get_size(s2) != (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(s2),
|
---|
986 | (unsigned long)(sizeof(struct struct1)*10));
|
---|
987 | return false;
|
---|
988 | }
|
---|
989 |
|
---|
990 | if (strcmp(location2, talloc_get_name(s2)) != 0) {
|
---|
991 | printf("failure: ptrtype [\n"
|
---|
992 | "talloc_array_ptrtype() sets the wrong name '%s' (should be '%s')\n]\n",
|
---|
993 | talloc_get_name(s2), location2);
|
---|
994 | return false;
|
---|
995 | }
|
---|
996 |
|
---|
997 | s3 = talloc_array_ptrtype(top, s3, 10);location3 = __location__;
|
---|
998 |
|
---|
999 | if (talloc_get_size(s3) != (sizeof(struct struct1 *) * 10)) {
|
---|
1000 | printf("failure: ptrtype [\n"
|
---|
1001 | "talloc_array_ptrtype() allocated the wrong size "
|
---|
1002 | "%lu (should be %lu)\n]\n",
|
---|
1003 | (unsigned long)talloc_get_size(s3),
|
---|
1004 | (unsigned long)(sizeof(struct struct1 *)*10));
|
---|
1005 | return false;
|
---|
1006 | }
|
---|
1007 |
|
---|
1008 | torture_assert_str_equal("ptrtype", location3, talloc_get_name(s3),
|
---|
1009 | "talloc_array_ptrtype() sets the wrong name");
|
---|
1010 |
|
---|
1011 | s4 = talloc_array_ptrtype(top, s4, 10);location4 = __location__;
|
---|
1012 |
|
---|
1013 | if (talloc_get_size(s4) != (sizeof(struct struct1 **) * 10)) {
|
---|
1014 | printf("failure: ptrtype [\n"
|
---|
1015 | "talloc_array_ptrtype() allocated the wrong size "
|
---|
1016 | "%lu (should be %lu)\n]\n",
|
---|
1017 | (unsigned long)talloc_get_size(s4),
|
---|
1018 | (unsigned long)(sizeof(struct struct1 **)*10));
|
---|
1019 | return false;
|
---|
1020 | }
|
---|
1021 |
|
---|
1022 | torture_assert_str_equal("ptrtype", location4, talloc_get_name(s4),
|
---|
1023 | "talloc_array_ptrtype() sets the wrong name");
|
---|
1024 |
|
---|
1025 | talloc_free(top);
|
---|
1026 |
|
---|
1027 | printf("success: ptrtype\n");
|
---|
1028 | return true;
|
---|
1029 | }
|
---|
1030 |
|
---|
1031 | static int _test_talloc_free_in_destructor(void **ptr)
|
---|
1032 | {
|
---|
1033 | talloc_free(*ptr);
|
---|
1034 | return 0;
|
---|
1035 | }
|
---|
1036 |
|
---|
1037 | static bool test_talloc_free_in_destructor(void)
|
---|
1038 | {
|
---|
1039 | void *level0;
|
---|
1040 | void *level1;
|
---|
1041 | void *level2;
|
---|
1042 | void *level3;
|
---|
1043 | void *level4;
|
---|
1044 | void **level5;
|
---|
1045 |
|
---|
1046 | printf("test: free_in_destructor\n# TALLOC FREE IN DESTRUCTOR\n");
|
---|
1047 |
|
---|
1048 | level0 = talloc_new(NULL);
|
---|
1049 | level1 = talloc_new(level0);
|
---|
1050 | level2 = talloc_new(level1);
|
---|
1051 | level3 = talloc_new(level2);
|
---|
1052 | level4 = talloc_new(level3);
|
---|
1053 | level5 = talloc(level4, void *);
|
---|
1054 |
|
---|
1055 | *level5 = level3;
|
---|
1056 | (void)talloc_reference(level0, level3);
|
---|
1057 | (void)talloc_reference(level3, level3);
|
---|
1058 | (void)talloc_reference(level5, level3);
|
---|
1059 |
|
---|
1060 | talloc_set_destructor(level5, _test_talloc_free_in_destructor);
|
---|
1061 |
|
---|
1062 | talloc_free(level1);
|
---|
1063 |
|
---|
1064 | talloc_free(level0);
|
---|
1065 |
|
---|
1066 | printf("success: free_in_destructor\n");
|
---|
1067 | return true;
|
---|
1068 | }
|
---|
1069 |
|
---|
1070 | static bool test_autofree(void)
|
---|
1071 | {
|
---|
1072 | #if _SAMBA_BUILD_ < 4
|
---|
1073 | /* autofree test would kill smbtorture */
|
---|
1074 | void *p;
|
---|
1075 | printf("test: autofree\n# TALLOC AUTOFREE CONTEXT\n");
|
---|
1076 |
|
---|
1077 | p = talloc_autofree_context();
|
---|
1078 | talloc_free(p);
|
---|
1079 |
|
---|
1080 | p = talloc_autofree_context();
|
---|
1081 | talloc_free(p);
|
---|
1082 |
|
---|
1083 | printf("success: autofree\n");
|
---|
1084 | #endif
|
---|
1085 | return true;
|
---|
1086 | }
|
---|
1087 |
|
---|
1088 | static bool test_pool(void)
|
---|
1089 | {
|
---|
1090 | void *pool;
|
---|
1091 | void *p1, *p2, *p3, *p4;
|
---|
1092 |
|
---|
1093 | pool = talloc_pool(NULL, 1024);
|
---|
1094 |
|
---|
1095 | p1 = talloc_size(pool, 80);
|
---|
1096 | p2 = talloc_size(pool, 20);
|
---|
1097 | p3 = talloc_size(p1, 50);
|
---|
1098 | p4 = talloc_size(p3, 1000);
|
---|
1099 |
|
---|
1100 | talloc_free(pool);
|
---|
1101 |
|
---|
1102 | return true;
|
---|
1103 | }
|
---|
1104 |
|
---|
1105 | struct torture_context;
|
---|
1106 | bool torture_local_talloc(struct torture_context *tctx)
|
---|
1107 | {
|
---|
1108 | bool ret = true;
|
---|
1109 |
|
---|
1110 | setlinebuf(stdout);
|
---|
1111 |
|
---|
1112 | talloc_disable_null_tracking();
|
---|
1113 | talloc_enable_null_tracking();
|
---|
1114 |
|
---|
1115 | ret &= test_ref1();
|
---|
1116 | ret &= test_ref2();
|
---|
1117 | ret &= test_ref3();
|
---|
1118 | ret &= test_ref4();
|
---|
1119 | ret &= test_unlink1();
|
---|
1120 | ret &= test_misc();
|
---|
1121 | ret &= test_realloc();
|
---|
1122 | ret &= test_realloc_child();
|
---|
1123 | ret &= test_steal();
|
---|
1124 | ret &= test_move();
|
---|
1125 | ret &= test_unref_reparent();
|
---|
1126 | ret &= test_realloc_fn();
|
---|
1127 | ret &= test_type();
|
---|
1128 | ret &= test_lifeless();
|
---|
1129 | ret &= test_loop();
|
---|
1130 | ret &= test_free_parent_deny_child();
|
---|
1131 | ret &= test_talloc_ptrtype();
|
---|
1132 | ret &= test_talloc_free_in_destructor();
|
---|
1133 | ret &= test_pool();
|
---|
1134 |
|
---|
1135 | if (ret) {
|
---|
1136 | ret &= test_speed();
|
---|
1137 | }
|
---|
1138 | ret &= test_autofree();
|
---|
1139 |
|
---|
1140 | return ret;
|
---|
1141 | }
|
---|
1142 |
|
---|
1143 | #if _SAMBA_BUILD_ < 4
|
---|
1144 | int main(void)
|
---|
1145 | {
|
---|
1146 | bool ret = torture_local_talloc(NULL);
|
---|
1147 | if (!ret)
|
---|
1148 | return -1;
|
---|
1149 | return 0;
|
---|
1150 | }
|
---|
1151 | #endif
|
---|