]> git.ozlabs.org Git - ccan/blob - ccan/talloc/test/run-test_realloc.c
talloc: use failtest to test failure paths.
[ccan] / ccan / talloc / test / run-test_realloc.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 realloc
68 */
69 static bool test_realloc(const struct torture_context *ctx)
70 {
71         void *root, *p1, *p2;
72         bool ret = false;
73
74         root = talloc_new(ctx);
75         if (!root)
76                 goto out;
77
78         p1 = talloc_size(root, 10);
79         if (!p1)
80                 goto out;
81         CHECK_SIZE("realloc", p1, 10);
82
83         p1 = talloc_realloc_size(NULL, p1, 20);
84         if (!p1)
85                 goto out;
86         CHECK_SIZE("realloc", p1, 20);
87
88         if (!talloc_new(p1))
89                 goto out;
90
91         p2 = talloc_realloc_size(p1, NULL, 30);
92         if (!p2)
93                 goto out;
94
95         if (!talloc_new(p1))
96                 goto out;
97
98         p2 = talloc_realloc_size(p1, p2, 40);
99         if (!p2)
100                 goto out;
101
102         CHECK_SIZE("realloc", p2, 40);
103         CHECK_SIZE("realloc", root, 60);
104         CHECK_BLOCKS("realloc", p1, 4);
105
106         p1 = talloc_realloc_size(NULL, p1, 20);
107         if (!p1)
108                 goto out;
109         CHECK_SIZE("realloc", p1, 60);
110
111         if (talloc_increase_ref_count(p2) != 0)
112                 goto out;
113         torture_assert("realloc", talloc_realloc_size(NULL, p2, 5) == NULL,
114                 "failed: talloc_realloc() on a referenced pointer should fail\n");
115         CHECK_BLOCKS("realloc", p1, 4);
116
117         ok1(talloc_realloc_size(NULL, p2, 0) == NULL);
118         ok1(talloc_realloc_size(NULL, p2, 0) == NULL);
119         CHECK_BLOCKS("realloc", p1, 3);
120
121         torture_assert("realloc", talloc_realloc_size(NULL, p1, 0x7fffffff) == NULL,
122                 "failed: oversize talloc should fail\n");
123
124         ok1(talloc_realloc_size(NULL, p1, 0) == NULL);
125
126         CHECK_BLOCKS("realloc", root, 1);
127         CHECK_SIZE("realloc", root, 0);
128         ret = true;
129         
130 out:
131         talloc_free(root);
132
133         return ret;
134 }
135
136 /*
137   test realloc with a child
138 */
139 static bool test_realloc_child(const struct torture_context *ctx)
140 {
141         void *root;
142         struct el2 {
143                 const char *name;
144         } *el2; 
145         struct el1 {
146                 int count;
147                 struct el2 **list, **list2, **list3;
148         } *el1;
149         bool ret = false;
150
151         root = talloc_new(ctx);
152         if (!root)
153                 goto out;
154
155         el1 = talloc(root, struct el1);
156         if (!el1)
157                 goto out;
158         el1->list = talloc(el1, struct el2 *);
159         if (!el1->list)
160                 goto out;
161         el1->list[0] = talloc(el1->list, struct el2);
162         if (!el1->list[0])
163                 goto out;
164         el1->list[0]->name = talloc_strdup(el1->list[0], "testing");
165         if (!el1->list[0]->name)
166                 goto out;
167
168         el1->list2 = talloc(el1, struct el2 *);
169         if (!el1->list2)
170                 goto out;
171         el1->list2[0] = talloc(el1->list2, struct el2);
172         if (!el1->list2[0])
173                 goto out;
174         el1->list2[0]->name = talloc_strdup(el1->list2[0], "testing2");
175         if (!el1->list2[0]->name)
176                 goto out;
177
178         el1->list3 = talloc(el1, struct el2 *);
179         if (!el1->list3)
180                 goto out;
181         el1->list3[0] = talloc(el1->list3, struct el2);
182         if (!el1->list3[0])
183                 goto out;
184         el1->list3[0]->name = talloc_strdup(el1->list3[0], "testing2");
185         if (!el1->list3[0]->name)
186                 goto out;
187         
188         el2 = talloc(el1->list, struct el2);
189         if (!el2)
190                 goto out;
191         el2 = talloc(el1->list2, struct el2);
192         if (!el2)
193                 goto out;
194         el2 = talloc(el1->list3, struct el2);
195         if (!el2)
196                 goto out;
197
198         el1->list = talloc_realloc(el1, el1->list, struct el2 *, 100);
199         if (!el1->list)
200                 goto out;
201         el1->list2 = talloc_realloc(el1, el1->list2, struct el2 *, 200);
202         if (!el1->list2)
203                 goto out;
204         el1->list3 = talloc_realloc(el1, el1->list3, struct el2 *, 300);
205         if (!el1->list3)
206                 goto out;
207
208         ret = true;
209 out:
210         talloc_free(root);
211
212         return ret;
213 }
214
215 int main(int argc, char *argv[])
216 {
217         plan_tests(17);
218         failtest_init(argc, argv);
219
220         talloc_enable_null_tracking();
221         if (null_context) {
222                 ok1(test_realloc(NULL) && test_realloc_child(NULL));
223                 /* This closes the leak, but don't free any other leaks! */
224                 ok1(!talloc_chunk_from_ptr(null_context)->child);
225                 talloc_disable_null_tracking();
226         }
227         failtest_exit(exit_status());
228 }
229