]> git.ozlabs.org Git - ccan/blob - ccan/talloc/test/run-test_misc.c
talloc: use failtest to test failure paths.
[ccan] / ccan / talloc / test / run-test_misc.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 static int fail_destructor(void *ptr)
67 {
68         return -1;
69 }
70
71 /*
72   miscellaneous tests to try to get a higher test coverage percentage
73 */
74 static bool test_misc(const struct torture_context *ctx)
75 {
76         void *root, *p1;
77         char *p2;
78         double *d;
79         const char *name;
80         bool ret = false;
81
82         root = talloc_new(ctx);
83         if (!root)
84                 goto out;
85
86         p1 = talloc_size(root, 0x7fffffff);
87         torture_assert("misc", !p1, "failed: large talloc allowed\n");
88
89         p1 = talloc_strdup(root, "foo");
90         if (!p1)
91                 goto out;
92
93         if (talloc_increase_ref_count(p1) != 0)
94                 goto out;
95         if (talloc_increase_ref_count(p1) != 0)
96                 goto out;
97         if (talloc_increase_ref_count(p1) != 0)
98                 goto out;
99         CHECK_BLOCKS("misc", p1, 1);
100         CHECK_BLOCKS("misc", root, 2);
101         talloc_free(p1);
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");
108         if (!p2)
109                 goto out;
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");
114         talloc_free(p1);
115         CHECK_BLOCKS("misc", p1, 1);
116         CHECK_BLOCKS("misc", root, 2);
117
118         name = talloc_set_name(p1, "my name is %s", "foo");
119         if (!name)
120                 goto out;
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);
125
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);
131
132         torture_assert("misc", talloc_free(NULL) == -1, 
133                                    "talloc_free(NULL) should give -1\n");
134
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);
139
140         p2 = (char *)talloc_zero_size(p1, 20);
141         if (!p2)
142                 goto out;
143         torture_assert("misc", p2[19] == 0, "Failed to give zero memory\n");
144         talloc_free(p2);
145
146         torture_assert("misc", talloc_strdup(root, NULL) == NULL,
147                 "failed: strdup on NULL should give NULL\n");
148
149         p2 = talloc_strndup(p1, "foo", 2);
150         if (!p2)
151                 goto out;
152         torture_assert("misc", strcmp("fo", p2) == 0, 
153                                    "strndup doesn't work\n");
154         p2 = talloc_asprintf_append(p2, "o%c", 'd');
155         if (!p2)
156                 goto out;
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);
161
162         p2 = talloc_asprintf_append(NULL, "hello %s", "world");
163         if (!p2)
164                 goto out;
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);
169         talloc_free(p2);
170
171         d = talloc_array(p1, double, 0x20000000);
172         torture_assert("misc", !d, "failed: integer overflow not detected\n");
173
174         d = talloc_realloc(p1, d, double, 0x20000000);
175         torture_assert("misc", !d, "failed: integer overflow not detected\n");
176
177         talloc_free(p1);
178         CHECK_BLOCKS("misc", root, 1);
179
180         p1 = talloc_named(root, 100, "%d bytes", 100);
181         if (!p1)
182                 goto out;
183         CHECK_BLOCKS("misc", p1, 2);
184         CHECK_BLOCKS("misc", root, 3);
185         talloc_unlink(root, p1);
186
187         p1 = talloc_init("%d bytes", 200);
188         if (!p1)
189                 goto out;
190         p2 = talloc_asprintf(p1, "my test '%s'", "string");
191         if (!p2)
192                 goto out;
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);
199
200         p1 = talloc_named_const(root, 10, "p1");
201         if (!p1)
202                 goto out;
203         p2 = (char *)talloc_named_const(root, 20, "p2");
204         if (!p2)
205                 goto out;
206         if (!talloc_reference(p1, p2))
207                 goto out;
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);
214
215         p1 = talloc_named_const(root, 10, "p1");
216         if (!p1)
217                 goto out;
218         p2 = (char *)talloc_named_const(root, 20, "p2");
219         if (!p2)
220                 goto out;
221         if (!talloc_reference(NULL, p2))
222                 goto out;
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);
229
230         /* Test that talloc_unlink is a no-op */
231
232         torture_assert("misc", talloc_unlink(root, NULL) == -1,
233                 "failed: talloc_unlink(root, NULL) == -1\n");
234
235         CHECK_SIZE("misc", root, 0);
236
237         talloc_enable_leak_report();
238         talloc_enable_leak_report_full();
239         ret = true;
240 out:
241         talloc_free(root);
242         return ret;
243 }
244
245 int main(int argc, char *argv[])
246 {
247         plan_tests(47);
248         failtest_init(argc, argv);
249
250         talloc_enable_null_tracking();
251         if (null_context) {
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();
256         }
257         failtest_exit(exit_status());
258 }