Code

t/: Added initial tests for core/object.
[sysdb.git] / t / core / object_test.c
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 START_TEST(test_obj_create)
56 {
57         struct noop {
58                 sdb_object_t super;
59                 int data;
60         };
61         sdb_type_t noop_type = {
62                 /* size = */ sizeof(struct noop),
63                 /* init = */ obj_init_noop,
64                 /* destroy = */ obj_destroy_noop,
65         };
67         sdb_object_t *obj;
68         const char *name = "test-object";
70         init_noop_called = 0;
71         init_noop_retval = 0;
72         destroy_noop_called = 0;
73         obj = sdb_object_create(name, noop_type);
74         fail_unless(obj != NULL,
75                         "sdb_object_create() = NULL; expected: a new object");
76         fail_unless(obj->type.size == noop_type.size,
77                         "after sdb_object_create(): type size mismatch; got: %zu; "
78                         "expected: %zu", obj->type.size, noop_type.size);
79         fail_unless(obj->ref_cnt == 1,
80                         "after sdb_object_create(): obj->ref_cnt = %d; expected: 1",
81                         obj->ref_cnt);
82         fail_unless(!strcmp(obj->name, "test-object"),
83                         "after sdb_object_create(): obj->name = '%s'; expected: '%s'",
84                         obj->name, name);
85         fail_unless(obj->name != name,
86                         "after sdb_object_create(): obj->name was not strdup()'ed");
88         fail_unless(init_noop_called == 1,
89                         "sdb_object_create() did not call object's init function");
90         fail_unless(destroy_noop_called == 0,
91                         "sdb_object_create() called object's destroy function");
92         fail_unless(((struct noop *)obj)->data == 0,
93                         "sdb_object_create() did not initialize data to zero");
95         sdb_object_deref(obj);
96         /* the memory address at 'obj' is no longer valid but usually this check
97          * should still work */
98         fail_unless(obj->ref_cnt == 0,
99                         "after sdb_object_deref(): obj->ref_cnt = %d; expected: 0",
100                         obj->ref_cnt);
101         fail_unless(destroy_noop_called == 1,
102                         "sdb_object_deref() did not call object's destroy function");
104         init_noop_called = 0;
105         init_noop_retval = -1;
106         destroy_noop_called = 0;
107         obj = sdb_object_create(name, noop_type);
108         fail_unless(obj == NULL,
109                         "sdb_object_create() = %p; expected NULL (init returned -1)",
110                         obj);
111         fail_unless(init_noop_called == 1,
112                         "sdb_object_create() did not call object's init function");
113         fail_unless(destroy_noop_called == 1,
114                         "sdb_object_create() did not call object's destroy function "
115                         "after init failure");
117         noop_type.size = 1;
118         init_noop_called = 0;
119         init_noop_retval = 0;
120         destroy_noop_called = 0;
121         obj = sdb_object_create(name, noop_type);
122         fail_unless(obj == NULL,
123                         "sdb_object_create() = %p; expected NULL (type's size too small)",
124                         obj);
125         fail_unless(init_noop_called == 0,
126                         "sdb_object_create() called object's init function "
127                         "when size was too small");
128         fail_unless(destroy_noop_called == 0,
129                         "sdb_object_create() called object's destroy function "
130                         "when size was too small");
132         noop_type.size = sizeof(struct noop);
133         init_noop_retval = 0;
134         noop_type.init = NULL;
135         obj = sdb_object_create(name, noop_type);
136         fail_unless(obj != NULL,
137                         "sdb_object_create() fails without init callback");
138         sdb_object_deref(obj);
140         noop_type.destroy = NULL;
141         obj = sdb_object_create(name, noop_type);
142         fail_unless(obj != NULL,
143                         "sdb_object_create() fails without destroy callback");
144         sdb_object_deref(obj);
146 END_TEST
148 Suite *
149 core_object_suite(void)
151         Suite *s = suite_create("core::object");
152         TCase *tc;
154         tc = tcase_create("core");
155         tcase_add_test(tc, test_obj_create);
156         suite_add_tcase(s, tc);
158         return s;
159 } /* core_object_suite */
161 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */