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
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"
59 #define DEFAULT_BE "tdb"
62 #define TEST_BE DEFAULT_BE
71 static int setup(void **state)
73 struct test_ctx *test_ctx;
75 test_ctx = talloc_zero(NULL, struct test_ctx);
80 static int teardown(void **state)
82 struct test_ctx *test_ctx = talloc_get_type_abort(*state,
85 talloc_free(test_ctx);
90 * Test that the index cache is opened by ldb_kv_index_transaction_start
91 * and correctly initialised with the passed index cache size.
93 static void test_index_cache_init(void **state)
95 struct test_ctx *test_ctx = talloc_get_type_abort(
98 struct ldb_module *module = NULL;
99 struct ldb_kv_private *ldb_kv = NULL;
100 int ret = LDB_SUCCESS;
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);
106 ret = ldb_kv_index_transaction_start(module, 191);
107 assert_int_equal(LDB_SUCCESS, ret);
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));
117 static int mock_begin_write(struct ldb_kv_private* ldb_kv) {
120 static int mock_abort_write(struct ldb_kv_private* ldb_kv) {
125 * Test that the index cache is set to the default cache size at the start of
128 static void test_default_index_cache_size(void **state)
130 struct test_ctx *test_ctx = talloc_get_type_abort(
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
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);
147 ret = ldb_kv_start_trans(module);
148 assert_int_equal(LDB_SUCCESS, ret);
151 DEFAULT_INDEX_CACHE_SIZE,
152 tdb_hash_size(ldb_kv->idxptr->itdb));
154 ret = ldb_kv_del_trans(module);
155 assert_int_equal(LDB_SUCCESS, ret);
161 int main(int argc, const char **argv)
163 const struct CMUnitTest tests[] = {
164 cmocka_unit_test_setup_teardown(
165 test_index_cache_init,
168 cmocka_unit_test_setup_teardown(
169 test_default_index_cache_size,
174 return cmocka_run_group_tests(tests, NULL, NULL);