ctdb-daemon: Use ctdb_parse_node_address() in ctdbd
[samba4-gss.git] / lib / ldb / tests / ldb_key_value_test.c
blob7bbfc7925bf631bf6cb838dde33c0f3113cb3850
1 /*
2 * Tests exercising the ldb key value operations.
4 * Copyright (C) Andrew Bartlett <abartlet@samba.org> 2019
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.
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.
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/>.
22 * from cmocka.c:
23 * These headers or their equivalents should be included prior to
24 * including
25 * this header file.
27 * #include <stdarg.h>
28 * #include <stddef.h>
29 * #include <setjmp.h>
31 * This allows test applications to use custom definitions of C standard
32 * library functions and types.
38 * Tests for the ldb key value layer
40 #include <stdarg.h>
41 #include <stddef.h>
42 #include <stdint.h>
43 #include <setjmp.h>
44 #include <cmocka.h>
46 #include <errno.h>
47 #include <unistd.h>
48 #include <talloc.h>
49 #include <tevent.h>
50 #include <string.h>
51 #include <ctype.h>
53 #include <sys/wait.h>
55 #include "ldb_key_value/ldb_kv.c"
56 #include "ldb_key_value/ldb_kv_index.c"
57 #include "ldb_key_value/ldb_kv_search.c"
59 #define DEFAULT_BE "tdb"
61 #ifndef TEST_BE
62 #define TEST_BE DEFAULT_BE
63 #endif /* TEST_BE */
65 #define NUM_RECS 1024
66 int ldb_kv_cache_reload(struct ldb_module *module) {
67 return LDB_SUCCESS;
69 int ldb_kv_cache_load(struct ldb_module *module) {
70 return LDB_SUCCESS;
72 int ldb_kv_check_at_attributes_values(const struct ldb_val *value) {
73 return LDB_SUCCESS;
75 int ldb_kv_increase_sequence_number(struct ldb_module *module) {
76 return LDB_SUCCESS;
79 struct test_ctx { uint8_t dummy; };
81 static int setup(void **state)
83 struct test_ctx *test_ctx;
85 test_ctx = talloc_zero(NULL, struct test_ctx);
86 *state = test_ctx;
87 return 0;
90 static int teardown(void **state)
92 struct test_ctx *test_ctx = talloc_get_type_abort(*state,
93 struct test_ctx);
95 talloc_free(test_ctx);
96 return 0;
100 * Test that the index cache is opened by ldb_kv_index_transaction_start
101 * and correctly initialised with the passed index cache size.
103 static void test_index_cache_init(void **state)
105 struct test_ctx *test_ctx = talloc_get_type_abort(
106 *state,
107 struct test_ctx);
108 struct ldb_module *module = NULL;
109 struct ldb_kv_private *ldb_kv = NULL;
110 int ret = LDB_SUCCESS;
112 module = talloc_zero(test_ctx, struct ldb_module);
113 ldb_kv = talloc_zero(test_ctx, struct ldb_kv_private);
114 ldb_module_set_private(module, ldb_kv);
116 ret = ldb_kv_index_transaction_start(module, 191);
117 assert_int_equal(LDB_SUCCESS, ret);
119 assert_non_null(ldb_kv->idxptr);
120 assert_non_null(ldb_kv->idxptr->itdb);
121 assert_int_equal(191, tdb_hash_size(ldb_kv->idxptr->itdb));
123 TALLOC_FREE(ldb_kv);
124 TALLOC_FREE(module);
127 static int mock_begin_write(struct ldb_kv_private* ldb_kv) {
128 return LDB_SUCCESS;
130 static int mock_abort_write(struct ldb_kv_private* ldb_kv) {
131 return LDB_SUCCESS;
135 * Test that the index cache is set to the default cache size at the start of
136 * a transaction.
138 static void test_default_index_cache_size(void **state)
140 struct test_ctx *test_ctx = talloc_get_type_abort(
141 *state,
142 struct test_ctx);
143 struct ldb_module *module = NULL;
144 struct ldb_kv_private *ldb_kv = NULL;
145 int ret = LDB_SUCCESS;
146 const struct kv_db_ops ops = {
147 .begin_write = mock_begin_write,
148 .abort_write = mock_abort_write
151 module = talloc_zero(test_ctx, struct ldb_module);
152 ldb_kv = talloc_zero(test_ctx, struct ldb_kv_private);
153 ldb_kv->pid = getpid();
154 ldb_kv->kv_ops = &ops;
155 ldb_kv->index_transaction_cache_size = DEFAULT_INDEX_CACHE_SIZE;
156 ldb_module_set_private(module, ldb_kv);
158 ret = ldb_kv_start_trans(module);
159 assert_int_equal(LDB_SUCCESS, ret);
161 assert_int_equal(
162 DEFAULT_INDEX_CACHE_SIZE,
163 tdb_hash_size(ldb_kv->idxptr->itdb));
165 ret = ldb_kv_del_trans(module);
166 assert_int_equal(LDB_SUCCESS, ret);
168 TALLOC_FREE(ldb_kv);
169 TALLOC_FREE(module);
172 static int db_size = 0;
173 static size_t mock_get_size(struct ldb_kv_private *ldb_kv) {
174 return db_size;
177 static int mock_iterate(
178 struct ldb_kv_private *ldb_kv,
179 ldb_kv_traverse_fn fn,
180 void *ctx) {
181 return 1;
185 * Test that the index cache is correctly sized by the re_index call
187 static void test_reindex_cache_size(void **state)
189 struct test_ctx *test_ctx = talloc_get_type_abort(
190 *state,
191 struct test_ctx);
192 struct ldb_module *module = NULL;
193 struct ldb_kv_private *ldb_kv = NULL;
194 int ret = LDB_SUCCESS;
195 const struct kv_db_ops ops = {
196 .iterate = mock_iterate,
197 .get_size = mock_get_size,
200 module = talloc_zero(test_ctx, struct ldb_module);
201 ldb_kv = talloc_zero(test_ctx, struct ldb_kv_private);
202 ldb_kv->kv_ops = &ops;
203 ldb_module_set_private(module, ldb_kv);
206 * Use a value less than the DEFAULT_INDEX_CACHE_SIZE
207 * Should get the DEFAULT_INDEX_CACHE_SIZE
209 db_size = DEFAULT_INDEX_CACHE_SIZE - 1;
210 ret = ldb_kv_reindex(module);
211 assert_int_equal(LDB_SUCCESS, ret);
213 assert_int_equal(
214 DEFAULT_INDEX_CACHE_SIZE,
215 tdb_hash_size(ldb_kv->idxptr->itdb));
218 * Use a value greater than the DEFAULT_INDEX_CACHE_SIZE
219 * Should get the value specified.
221 db_size = DEFAULT_INDEX_CACHE_SIZE + 1;
222 ret = ldb_kv_reindex(module);
223 assert_int_equal(LDB_SUCCESS, ret);
225 assert_int_equal(db_size, tdb_hash_size(ldb_kv->idxptr->itdb));
227 TALLOC_FREE(ldb_kv);
228 TALLOC_FREE(module);
232 * Test that ldb_kv_init_store sets the default index transaction cache size
233 * if the option is not supplied.
235 static void test_init_store_default_index_cache_size(void **state)
237 struct test_ctx *test_ctx = talloc_get_type_abort(
238 *state,
239 struct test_ctx);
240 struct ldb_module *module = NULL;
241 struct ldb_kv_private *ldb_kv = NULL;
242 struct ldb_context *ldb = NULL;
243 int ret = LDB_SUCCESS;
245 module = talloc_zero(test_ctx, struct ldb_module);
246 ldb = talloc_zero(test_ctx, struct ldb_context);
247 ldb_kv = talloc_zero(test_ctx, struct ldb_kv_private);
249 ret = ldb_kv_init_store(ldb_kv, "test", ldb, NULL, &module);
250 assert_int_equal(LDB_SUCCESS, ret);
252 assert_int_equal(
253 DEFAULT_INDEX_CACHE_SIZE,
254 ldb_kv->index_transaction_cache_size);
256 TALLOC_FREE(ldb_kv);
257 TALLOC_FREE(module);
258 TALLOC_FREE(ldb);
262 * Test that ldb_kv_init_store sets the index transaction cache size
263 * to the value specified in the option.
265 static void test_init_store_set_index_cache_size(void **state)
267 struct test_ctx *test_ctx = talloc_get_type_abort(
268 *state,
269 struct test_ctx);
270 struct ldb_module *module = NULL;
271 struct ldb_kv_private *ldb_kv = NULL;
272 struct ldb_context *ldb = NULL;
273 const char *options[] = {"transaction_index_cache_size:1900", NULL};
274 int ret = LDB_SUCCESS;
276 module = talloc_zero(test_ctx, struct ldb_module);
277 ldb = talloc_zero(test_ctx, struct ldb_context);
278 ldb_kv = talloc_zero(test_ctx, struct ldb_kv_private);
280 ret = ldb_kv_init_store(ldb_kv, "test", ldb, options, &module);
281 assert_int_equal(LDB_SUCCESS, ret);
283 assert_int_equal( 1900, ldb_kv->index_transaction_cache_size);
285 TALLOC_FREE(ldb_kv);
286 TALLOC_FREE(module);
287 TALLOC_FREE(ldb);
291 * Test that ldb_kv_init_store sets the default index transaction cache size
292 * if the value specified in the option is not a number.
294 static void test_init_store_set_index_cache_size_non_numeric(void **state)
296 struct test_ctx *test_ctx = talloc_get_type_abort(
297 *state,
298 struct test_ctx);
299 struct ldb_module *module = NULL;
300 struct ldb_kv_private *ldb_kv = NULL;
301 struct ldb_context *ldb = NULL;
302 const char *options[] = {"transaction_index_cache_size:fred", NULL};
303 int ret = LDB_SUCCESS;
305 module = talloc_zero(test_ctx, struct ldb_module);
306 ldb = talloc_zero(test_ctx, struct ldb_context);
307 ldb_kv = talloc_zero(test_ctx, struct ldb_kv_private);
309 ret = ldb_kv_init_store(ldb_kv, "test", ldb, options, &module);
310 assert_int_equal(LDB_SUCCESS, ret);
312 assert_int_equal(
313 DEFAULT_INDEX_CACHE_SIZE,
314 ldb_kv->index_transaction_cache_size);
316 TALLOC_FREE(ldb_kv);
317 TALLOC_FREE(module);
318 TALLOC_FREE(ldb);
322 * Test that ldb_kv_init_store sets the default index transaction cache size
323 * if the value specified is too large
325 static void test_init_store_set_index_cache_size_range(void **state)
327 struct test_ctx *test_ctx = talloc_get_type_abort(
328 *state,
329 struct test_ctx);
330 struct ldb_module *module = NULL;
331 struct ldb_kv_private *ldb_kv = NULL;
332 struct ldb_context *ldb = NULL;
333 const char *options[] = {
334 "transaction_index_cache_size:0xfffffffffffffffffffffffffffff",
335 NULL};
336 int ret = LDB_SUCCESS;
338 module = talloc_zero(test_ctx, struct ldb_module);
339 ldb = talloc_zero(test_ctx, struct ldb_context);
340 ldb_kv = talloc_zero(test_ctx, struct ldb_kv_private);
342 ret = ldb_kv_init_store(ldb_kv, "test", ldb, options, &module);
343 assert_int_equal(LDB_SUCCESS, ret);
345 assert_int_equal(
346 DEFAULT_INDEX_CACHE_SIZE,
347 ldb_kv->index_transaction_cache_size);
349 TALLOC_FREE(ldb_kv);
350 TALLOC_FREE(module);
351 TALLOC_FREE(ldb);
354 int main(int argc, const char **argv)
356 const struct CMUnitTest tests[] = {
357 cmocka_unit_test_setup_teardown(
358 test_index_cache_init,
359 setup,
360 teardown),
361 cmocka_unit_test_setup_teardown(
362 test_default_index_cache_size,
363 setup,
364 teardown),
365 cmocka_unit_test_setup_teardown(
366 test_reindex_cache_size,
367 setup,
368 teardown),
369 cmocka_unit_test_setup_teardown(
370 test_init_store_default_index_cache_size,
371 setup,
372 teardown),
373 cmocka_unit_test_setup_teardown(
374 test_init_store_set_index_cache_size,
375 setup,
376 teardown),
377 cmocka_unit_test_setup_teardown(
378 test_init_store_set_index_cache_size_non_numeric,
379 setup,
380 teardown),
381 cmocka_unit_test_setup_teardown(
382 test_init_store_set_index_cache_size_range,
383 setup,
384 teardown),
387 cmocka_set_message_output(CM_OUTPUT_SUBUNIT);
389 return cmocka_run_group_tests(tests, NULL, NULL);