1 /*
2 * SysDB - t/core/object_test.c
3 * Copyright (C) 2013 Sebastian 'tokkee' Harl <sh@tokkee.org>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
19 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
28 #include "core/object.h"
29 #include "libsysdb_test.h"
31 #include <check.h>
33 /*
34 * private data types
35 */
37 static int init_noop_called = 0;
38 static int init_noop_retval = 0;
39 static int
40 obj_init_noop(sdb_object_t *obj, va_list __attribute__((unused)) ap)
41 {
42 ++init_noop_called;
43 fail_unless(obj != NULL, "obj init function: received obj == NULL");
44 return init_noop_retval;
45 } /* obj_init_noop */
47 static int destroy_noop_called = 0;
48 static void
49 obj_destroy_noop(sdb_object_t *obj)
50 {
51 ++destroy_noop_called;
52 fail_unless(obj != NULL, "obj destroy function: received obj == NULL");
53 } /* obj_destroy_noop */
55 struct noop {
56 sdb_object_t super;
57 int data;
58 };
59 static sdb_type_t noop_type = {
60 /* size = */ sizeof(struct noop),
61 /* init = */ obj_init_noop,
62 /* destroy = */ obj_destroy_noop,
63 };
65 static void *wrapped = (void *)0x42;
67 static int destroy_wrapper_called = 0;
68 static void
69 wrapper_destroy(void *obj)
70 {
71 ++destroy_wrapper_called;
72 fail_unless(obj == wrapped,
73 "wrapper_destroy received unexpected obj %p; expected: %p",
74 obj, wrapped);
75 } /* wrapper_destroy */
77 START_TEST(test_obj_create)
78 {
79 sdb_object_t *obj;
80 sdb_type_t test_type = noop_type;
82 const char *name = "test-object";
84 init_noop_called = 0;
85 init_noop_retval = 0;
86 destroy_noop_called = 0;
87 obj = sdb_object_create(name, test_type);
88 fail_unless(obj != NULL,
89 "sdb_object_create() = NULL; expected: a new object");
90 fail_unless(obj->type.size == test_type.size,
91 "after sdb_object_create(): type size mismatch; got: %zu; "
92 "expected: %zu", obj->type.size, test_type.size);
93 fail_unless(obj->type.init == obj_init_noop,
94 "after sdb_object_create(): type init = %p; exptected: %p",
95 obj->type.init, obj_init_noop);
96 fail_unless(obj->type.destroy == obj_destroy_noop,
97 "after sdb_object_create(): type destroy = %p; exptected: %p",
98 obj->type.destroy, obj_destroy_noop);
99 fail_unless(obj->ref_cnt == 1,
100 "after sdb_object_create(): obj->ref_cnt = %d; expected: 1",
101 obj->ref_cnt);
102 fail_unless(!strcmp(obj->name, "test-object"),
103 "after sdb_object_create(): obj->name = '%s'; expected: '%s'",
104 obj->name, name);
105 fail_unless(obj->name != name,
106 "after sdb_object_create(): obj->name was not strdup()'ed");
108 fail_unless(init_noop_called == 1,
109 "sdb_object_create() did not call object's init function");
110 fail_unless(destroy_noop_called == 0,
111 "sdb_object_create() called object's destroy function");
112 fail_unless(((struct noop *)obj)->data == 0,
113 "sdb_object_create() did not initialize data to zero");
115 sdb_object_deref(obj);
116 /* the memory address at 'obj' is no longer valid but usually this check
117 * should still work */
118 fail_unless(obj->ref_cnt == 0,
119 "after sdb_object_deref(): obj->ref_cnt = %d; expected: 0",
120 obj->ref_cnt);
121 fail_unless(destroy_noop_called == 1,
122 "sdb_object_deref() did not call object's destroy function");
124 init_noop_called = 0;
125 init_noop_retval = -1;
126 destroy_noop_called = 0;
127 obj = sdb_object_create(name, test_type);
128 fail_unless(obj == NULL,
129 "sdb_object_create() = %p; expected NULL (init returned -1)",
130 obj);
131 fail_unless(init_noop_called == 1,
132 "sdb_object_create() did not call object's init function");
133 fail_unless(destroy_noop_called == 1,
134 "sdb_object_create() did not call object's destroy function "
135 "after init failure");
137 test_type.size = 1;
138 init_noop_called = 0;
139 init_noop_retval = 0;
140 destroy_noop_called = 0;
141 obj = sdb_object_create(name, test_type);
142 fail_unless(obj == NULL,
143 "sdb_object_create() = %p; expected NULL (type's size too small)",
144 obj);
145 fail_unless(init_noop_called == 0,
146 "sdb_object_create() called object's init function "
147 "when size was too small");
148 fail_unless(destroy_noop_called == 0,
149 "sdb_object_create() called object's destroy function "
150 "when size was too small");
152 test_type.size = sizeof(struct noop);
153 init_noop_retval = 0;
154 test_type.init = NULL;
155 obj = sdb_object_create(name, test_type);
156 fail_unless(obj != NULL,
157 "sdb_object_create() fails without init callback");
158 sdb_object_deref(obj);
160 test_type.destroy = NULL;
161 obj = sdb_object_create(name, test_type);
162 fail_unless(obj != NULL,
163 "sdb_object_create() fails without destroy callback");
164 sdb_object_deref(obj);
166 init_noop_called = 0;
167 obj = sdb_object_create_simple(name, sizeof(struct noop));
168 fail_unless(obj != NULL,
169 "sdb_object_create_simple() = NULL; expected: <obj>");
170 fail_unless(obj->type.size == sizeof(struct noop),
171 "sdb_object_create_simple() created object of size %zu; "
172 "expected: %zu", obj->type.size, sizeof(struct noop));
173 fail_unless(obj->type.init == NULL,
174 "sdb_object_create_simple() did not set init=NULL");
175 fail_unless(obj->type.destroy == NULL,
176 "sdb_object_create_simple() did not set destroy=NULL");
177 fail_unless(init_noop_called == 0,
178 "sdb_object_create_simple() unexpectedly called noop's init");
179 sdb_object_deref(obj);
181 obj = sdb_object_create_T(NULL, struct noop);
182 fail_unless(obj != NULL,
183 "sdb_object_create_simple() = NULL; expected: <obj>");
184 fail_unless(obj->type.size == sizeof(struct noop),
185 "sdb_object_create_simple() created object of size %zu; "
186 "expected: %zu", obj->type.size, sizeof(struct noop));
187 fail_unless(obj->type.init == NULL,
188 "sdb_object_create_simple() did not set init=NULL");
189 fail_unless(obj->type.destroy == NULL,
190 "sdb_object_create_simple() did not set destroy=NULL");
191 fail_unless(init_noop_called == 0,
192 "sdb_object_create_simple() unexpectedly called noop's init");
193 sdb_object_deref(obj);
194 }
195 END_TEST
197 START_TEST(test_obj_wrapper)
198 {
199 sdb_object_t *obj;
200 const char *name = "wrapped-object";
202 destroy_wrapper_called = 0;
203 obj = sdb_object_create_wrapper(name, wrapped, wrapper_destroy);
204 fail_unless(obj != NULL,
205 "sdb_object_create_wrapper() = NULL; expected: wrapper object");
206 fail_unless(obj->ref_cnt == 1,
207 "after sdb_object_create_wrapper(); obj->ref_cnt = %d; "
208 "expected: 1", obj->ref_cnt);
209 fail_unless(!strcmp(obj->name, name),
210 "after sdb_object_create_wrapper(); obj->name = %s; expected: %s",
211 obj->name, name);
212 fail_unless(obj->name != name,
213 "sdb_object_create_wrapper() did not copy object name");
214 fail_unless(SDB_OBJ_WRAPPER(obj)->data == wrapped,
215 "wrapped object wraps unexpected data %p; expected: %p",
216 SDB_OBJ_WRAPPER(obj)->data, wrapped);
218 fail_unless(destroy_wrapper_called == 0,
219 "sdb_object_create_wrapper() called object's destructor");
221 sdb_object_deref(obj);
222 fail_unless(destroy_wrapper_called == 1,
223 "sdb_object_deref() did not call wrapped object's destructor");
224 }
225 END_TEST
227 START_TEST(test_obj_ref)
228 {
229 sdb_object_t *obj;
230 sdb_type_t test_type = noop_type;
232 init_noop_called = 0;
233 init_noop_retval = 0;
234 destroy_noop_called = 0;
236 obj = sdb_object_create("test-object", test_type);
237 fail_unless(obj != NULL,
238 "sdb_object_create() = NULL; expected: valid object");
240 sdb_object_ref(obj);
241 fail_unless(obj->ref_cnt == 2,
242 "after db_object_ref(): obj->ref_cnt = %d; expected: 2",
243 obj->ref_cnt);
245 sdb_object_ref(obj);
246 fail_unless(obj->ref_cnt == 3,
247 "after db_object_ref(): obj->ref_cnt = %d; expected: 3",
248 obj->ref_cnt);
250 obj->ref_cnt = 42;
251 sdb_object_ref(obj);
252 fail_unless(obj->ref_cnt == 43,
253 "after db_object_ref(): obj->ref_cnt = %d; expected: 43",
254 obj->ref_cnt);
256 fail_unless(init_noop_called == 1,
257 "after some sdb_object_ref(); object's init called %d times; "
258 "expected: 1", init_noop_called);
259 fail_unless(destroy_noop_called == 0,
260 "after some sdb_object_ref(); object's destroy called %d time%s; "
261 "expected: 0", destroy_noop_called == 1 ? "" : "2",
262 destroy_noop_called);
264 sdb_object_deref(obj);
265 fail_unless(obj->ref_cnt == 42,
266 "after db_object_deref(): obj->ref_cnt = %d; expected: 42",
267 obj->ref_cnt);
269 obj->ref_cnt = 23;
270 sdb_object_deref(obj);
271 fail_unless(obj->ref_cnt == 22,
272 "after db_object_deref(): obj->ref_cnt = %d; expected: 22",
273 obj->ref_cnt);
275 fail_unless(init_noop_called == 1,
276 "after some sdb_object_{de,}ref(); object's init called %d times; "
277 "expected: 1", init_noop_called);
278 fail_unless(destroy_noop_called == 0,
279 "after some sdb_object_{de,}ref(); object's destroy called "
280 "%d time%s; expected: 0", destroy_noop_called == 1 ? "" : "2",
281 destroy_noop_called);
283 /* test_obj_create already checks the ref_cnt == 1 case */
284 obj->ref_cnt = 0;
285 sdb_object_deref(obj);
286 fail_unless(obj->ref_cnt <= 0,
287 "after db_object_deref(): obj->ref_cnt = %d; expected: <= 0",
288 obj->ref_cnt);
290 fail_unless(init_noop_called == 1,
291 "after some sdb_object_{de,}ref(); object's init called %d times; "
292 "expected: 1", init_noop_called);
293 fail_unless(destroy_noop_called == 1,
294 "after some sdb_object_{de,}ref(); object's destroy called "
295 "%d times; expected: 1", destroy_noop_called);
297 /* this should work */
298 sdb_object_deref(NULL);
299 }
300 END_TEST
302 START_TEST(test_obj_cmp)
303 {
304 sdb_object_t *obj1, *obj2, *obj3, *obj4;
305 int status;
307 obj1 = sdb_object_create("a", noop_type);
308 obj2 = sdb_object_create("b", noop_type);
309 obj3 = sdb_object_create("B", noop_type);
310 obj4 = sdb_object_create("c", noop_type);
312 status = sdb_object_cmp_by_name(obj1, obj2);
313 fail_unless(status == -1,
314 "sdb_object_cmp_by_name('a', 'b') = %d; expected: -1", status);
315 status = sdb_object_cmp_by_name(obj2, obj3);
316 fail_unless(status == 0,
317 "sdb_object_cmp_by_name('b', 'B') = %d; expected: 0", status);
318 status = sdb_object_cmp_by_name(obj4, obj3);
319 fail_unless(status == 1,
320 "sdb_object_cmp_by_name('c', 'B') = %d; expected: 1", status);
321 status = sdb_object_cmp_by_name(obj1, obj1);
322 fail_unless(status == 0,
323 "sdb_object_cmp_by_name('a', 'a') = %d; expected: 0", status);
325 sdb_object_deref(obj1);
326 sdb_object_deref(obj2);
327 sdb_object_deref(obj3);
328 sdb_object_deref(obj4);
329 }
330 END_TEST
332 Suite *
333 core_object_suite(void)
334 {
335 Suite *s = suite_create("core::object");
336 TCase *tc;
338 tc = tcase_create("core");
339 tcase_add_test(tc, test_obj_create);
340 tcase_add_test(tc, test_obj_wrapper);
341 tcase_add_test(tc, test_obj_ref);
342 tcase_add_test(tc, test_obj_cmp);
343 suite_add_tcase(s, tc);
345 return s;
346 } /* core_object_suite */
348 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */