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/>.
23 * These headers or their equivalents should be included prior to
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
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"
62 #define TEST_BE DEFAULT_BE
66 int ldb_kv_cache_reload(struct ldb_module
*module
) {
69 int ldb_kv_cache_load(struct ldb_module
*module
) {
72 int ldb_kv_check_at_attributes_values(const struct ldb_val
*value
) {
75 int ldb_kv_increase_sequence_number(struct ldb_module
*module
) {
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
);
90 static int teardown(void **state
)
92 struct test_ctx
*test_ctx
= talloc_get_type_abort(*state
,
95 talloc_free(test_ctx
);
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(
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
));
127 static int mock_begin_write(struct ldb_kv_private
* ldb_kv
) {
130 static int mock_abort_write(struct ldb_kv_private
* ldb_kv
) {
135 * Test that the index cache is set to the default cache size at the start of
138 static void test_default_index_cache_size(void **state
)
140 struct test_ctx
*test_ctx
= talloc_get_type_abort(
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
);
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
);
172 static int db_size
= 0;
173 static size_t mock_get_size(struct ldb_kv_private
*ldb_kv
) {
177 static int mock_iterate(
178 struct ldb_kv_private
*ldb_kv
,
179 ldb_kv_traverse_fn fn
,
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(
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
);
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
));
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(
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
);
253 DEFAULT_INDEX_CACHE_SIZE
,
254 ldb_kv
->index_transaction_cache_size
);
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(
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
);
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(
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
);
313 DEFAULT_INDEX_CACHE_SIZE
,
314 ldb_kv
->index_transaction_cache_size
);
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(
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",
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
);
346 DEFAULT_INDEX_CACHE_SIZE
,
347 ldb_kv
->index_transaction_cache_size
);
354 int main(int argc
, const char **argv
)
356 const struct CMUnitTest tests
[] = {
357 cmocka_unit_test_setup_teardown(
358 test_index_cache_init
,
361 cmocka_unit_test_setup_teardown(
362 test_default_index_cache_size
,
365 cmocka_unit_test_setup_teardown(
366 test_reindex_cache_size
,
369 cmocka_unit_test_setup_teardown(
370 test_init_store_default_index_cache_size
,
373 cmocka_unit_test_setup_teardown(
374 test_init_store_set_index_cache_size
,
377 cmocka_unit_test_setup_teardown(
378 test_init_store_set_index_cache_size_non_numeric
,
381 cmocka_unit_test_setup_teardown(
382 test_init_store_set_index_cache_size_range
,
387 cmocka_set_message_output(CM_OUTPUT_SUBUNIT
);
389 return cmocka_run_group_tests(tests
, NULL
, NULL
);