lib ldb key_value: Pass index cache size
[metze/samba/wip.git] / lib / ldb / tests / ldb_key_value_test.c
1 /*
2  * Tests exercising the ldb key value operations.
3  *
4  *  Copyright (C) Andrew Bartlett <abartlet@samba.org> 2019
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20
21 /*
22  * from cmocka.c:
23  * These headers or their equivalents should be included prior to
24  * including
25  * this header file.
26  *
27  * #include <stdarg.h>
28  * #include <stddef.h>
29  * #include <setjmp.h>
30  *
31  * This allows test applications to use custom definitions of C standard
32  * library functions and types.
33  *
34  */
35
36 /*
37  *
38  * Tests for the ldb key value layer
39  */
40 #include <stdarg.h>
41 #include <stddef.h>
42 #include <setjmp.h>
43 #include <cmocka.h>
44
45 #include <errno.h>
46 #include <unistd.h>
47 #include <talloc.h>
48 #include <tevent.h>
49 #include <string.h>
50 #include <ctype.h>
51
52 #include <sys/wait.h>
53
54 #include "ldb_key_value/ldb_kv.c"
55 #include "ldb_key_value/ldb_kv_cache.c"
56 #include "ldb_key_value/ldb_kv_index.c"
57 #include "ldb_key_value/ldb_kv_search.c"
58
59 #define DEFAULT_BE  "tdb"
60
61 #ifndef TEST_BE
62 #define TEST_BE DEFAULT_BE
63 #endif /* TEST_BE */
64
65 #define NUM_RECS 1024
66
67
68 struct test_ctx {
69 };
70
71 static int setup(void **state)
72 {
73         struct test_ctx *test_ctx;
74
75         test_ctx = talloc_zero(NULL, struct test_ctx);
76         *state = test_ctx;
77         return 0;
78 }
79
80 static int teardown(void **state)
81 {
82         struct test_ctx *test_ctx = talloc_get_type_abort(*state,
83                                                           struct test_ctx);
84
85         talloc_free(test_ctx);
86         return 0;
87 }
88
89 /*
90  * Test that the index cache is opened by ldb_kv_index_transaction_start
91  * and correctly initialised with the passed index cache size.
92  */
93 static void test_index_cache_init(void **state)
94 {
95         struct test_ctx *test_ctx = talloc_get_type_abort(
96                 *state,
97                 struct test_ctx);
98         struct ldb_module *module = NULL;
99         struct ldb_kv_private *ldb_kv = NULL;
100         int ret = LDB_SUCCESS;
101
102         module = talloc_zero(test_ctx, struct ldb_module);
103         ldb_kv = talloc_zero(test_ctx, struct ldb_kv_private);
104         ldb_module_set_private(module, ldb_kv);
105
106         ret = ldb_kv_index_transaction_start(module, 191);
107         assert_int_equal(LDB_SUCCESS, ret);
108
109         assert_non_null(ldb_kv->idxptr);
110         assert_non_null(ldb_kv->idxptr->itdb);
111         assert_int_equal(191, tdb_hash_size(ldb_kv->idxptr->itdb));
112
113         TALLOC_FREE(ldb_kv);
114         TALLOC_FREE(module);
115 }
116
117 static int mock_begin_write(struct ldb_kv_private* ldb_kv) {
118         return LDB_SUCCESS;
119 }
120 static int mock_abort_write(struct ldb_kv_private* ldb_kv) {
121         return LDB_SUCCESS;
122 }
123
124 /*
125  * Test that the index cache is set to the default cache size at the start of
126  * a transaction.
127  */
128 static void test_default_index_cache_size(void **state)
129 {
130         struct test_ctx *test_ctx = talloc_get_type_abort(
131                 *state,
132                 struct test_ctx);
133         struct ldb_module *module = NULL;
134         struct ldb_kv_private *ldb_kv = NULL;
135         int ret = LDB_SUCCESS;
136         const struct kv_db_ops ops = {
137                 .begin_write = mock_begin_write,
138                 .abort_write = mock_abort_write
139         };
140
141         module = talloc_zero(test_ctx, struct ldb_module);
142         ldb_kv = talloc_zero(test_ctx, struct ldb_kv_private);
143         ldb_kv->pid = getpid();
144         ldb_kv->kv_ops = &ops;
145         ldb_module_set_private(module, ldb_kv);
146
147         ret = ldb_kv_start_trans(module);
148         assert_int_equal(LDB_SUCCESS, ret);
149
150         assert_int_equal(
151                 DEFAULT_INDEX_CACHE_SIZE,
152                 tdb_hash_size(ldb_kv->idxptr->itdb));
153
154         ret = ldb_kv_del_trans(module);
155         assert_int_equal(LDB_SUCCESS, ret);
156
157         TALLOC_FREE(ldb_kv);
158         TALLOC_FREE(module);
159 }
160
161 int main(int argc, const char **argv)
162 {
163         const struct CMUnitTest tests[] = {
164                 cmocka_unit_test_setup_teardown(
165                         test_index_cache_init,
166                         setup,
167                         teardown),
168                 cmocka_unit_test_setup_teardown(
169                         test_default_index_cache_size,
170                         setup,
171                         teardown),
172         };
173
174         return cmocka_run_group_tests(tests, NULL, NULL);
175 }