summaryrefslogtreecommitdiffstats
path: root/lib/ldb/tests/ldb_key_value_test.c
blob: 97f717b5ee077083d4ba501ca348d974f0c70d18 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
/*
 * Tests exercising the ldb key value operations.
 *
 *  Copyright (C) Andrew Bartlett <abartlet@samba.org> 2019
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

/*
 * from cmocka.c:
 * These headers or their equivalents should be included prior to
 * including
 * this header file.
 *
 * #include <stdarg.h>
 * #include <stddef.h>
 * #include <setjmp.h>
 *
 * This allows test applications to use custom definitions of C standard
 * library functions and types.
 *
 */

/*
 *
 * Tests for the ldb key value layer
 */
#include <stdarg.h>
#include <stddef.h>
#include <stdint.h>
#include <setjmp.h>
#include <cmocka.h>

#include <errno.h>
#include <unistd.h>
#include <talloc.h>
#include <tevent.h>
#include <string.h>
#include <ctype.h>

#include <sys/wait.h>

#include "ldb_key_value/ldb_kv.c"
#include "ldb_key_value/ldb_kv_index.c"
#include "ldb_key_value/ldb_kv_search.c"

#define DEFAULT_BE  "tdb"

#ifndef TEST_BE
#define TEST_BE DEFAULT_BE
#endif /* TEST_BE */

#define NUM_RECS 1024
int ldb_kv_cache_reload(struct ldb_module *module) {
	return LDB_SUCCESS;
}
int ldb_kv_cache_load(struct ldb_module *module) {
	return LDB_SUCCESS;
}
int ldb_kv_check_at_attributes_values(const struct ldb_val *value) {
	return LDB_SUCCESS;
}
int ldb_kv_increase_sequence_number(struct ldb_module *module) {
	return LDB_SUCCESS;
}

struct test_ctx { uint8_t dummy; };

static int setup(void **state)
{
	struct test_ctx *test_ctx;

	test_ctx = talloc_zero(NULL, struct test_ctx);
	*state = test_ctx;
	return 0;
}

static int teardown(void **state)
{
	struct test_ctx *test_ctx = talloc_get_type_abort(*state,
							  struct test_ctx);

	talloc_free(test_ctx);
	return 0;
}

/*
 * Test that the index cache is opened by ldb_kv_index_transaction_start
 * and correctly initialised with the passed index cache size.
 */
static void test_index_cache_init(void **state)
{
	struct test_ctx *test_ctx = talloc_get_type_abort(
		*state,
		struct test_ctx);
	struct ldb_module *module = NULL;
	struct ldb_kv_private *ldb_kv = NULL;
	int ret = LDB_SUCCESS;

	module = talloc_zero(test_ctx, struct ldb_module);
	ldb_kv = talloc_zero(test_ctx, struct ldb_kv_private);
	ldb_module_set_private(module, ldb_kv);

	ret = ldb_kv_index_transaction_start(module, 191);
	assert_int_equal(LDB_SUCCESS, ret);

	assert_non_null(ldb_kv->idxptr);
	assert_non_null(ldb_kv->idxptr->itdb);
	assert_int_equal(191, tdb_hash_size(ldb_kv->idxptr->itdb));

	TALLOC_FREE(ldb_kv);
	TALLOC_FREE(module);
}

static int mock_begin_write(struct ldb_kv_private* ldb_kv) {
	return LDB_SUCCESS;
}
static int mock_abort_write(struct ldb_kv_private* ldb_kv) {
	return LDB_SUCCESS;
}

/*
 * Test that the index cache is set to the default cache size at the start of
 * a transaction.
 */
static void test_default_index_cache_size(void **state)
{
	struct test_ctx *test_ctx = talloc_get_type_abort(
		*state,
		struct test_ctx);
	struct ldb_module *module = NULL;
	struct ldb_kv_private *ldb_kv = NULL;
	int ret = LDB_SUCCESS;
	const struct kv_db_ops ops = {
		.begin_write = mock_begin_write,
		.abort_write = mock_abort_write
	};

	module = talloc_zero(test_ctx, struct ldb_module);
	ldb_kv = talloc_zero(test_ctx, struct ldb_kv_private);
	ldb_kv->pid = getpid();
	ldb_kv->kv_ops = &ops;
	ldb_kv->index_transaction_cache_size = DEFAULT_INDEX_CACHE_SIZE;
	ldb_module_set_private(module, ldb_kv);

	ret = ldb_kv_start_trans(module);
	assert_int_equal(LDB_SUCCESS, ret);

	assert_int_equal(
		DEFAULT_INDEX_CACHE_SIZE,
		tdb_hash_size(ldb_kv->idxptr->itdb));

	ret = ldb_kv_del_trans(module);
	assert_int_equal(LDB_SUCCESS, ret);

	TALLOC_FREE(ldb_kv);
	TALLOC_FREE(module);
}

static int db_size = 0;
static size_t mock_get_size(struct ldb_kv_private *ldb_kv) {
	return db_size;
}

static int mock_iterate(
	struct ldb_kv_private *ldb_kv,
	ldb_kv_traverse_fn fn,
	void *ctx) {
	return 1;
}

/*
 * Test that the index cache is correctly sized by the re_index call
 */
static void test_reindex_cache_size(void **state)
{
	struct test_ctx *test_ctx = talloc_get_type_abort(
		*state,
		struct test_ctx);
	struct ldb_module *module = NULL;
	struct ldb_kv_private *ldb_kv = NULL;
	int ret = LDB_SUCCESS;
	const struct kv_db_ops ops = {
		.iterate = mock_iterate,
		.get_size = mock_get_size,
	};

	module = talloc_zero(test_ctx, struct ldb_module);
	ldb_kv = talloc_zero(test_ctx, struct ldb_kv_private);
	ldb_kv->kv_ops = &ops;
	ldb_module_set_private(module, ldb_kv);

	/*
	 * Use a value less than the DEFAULT_INDEX_CACHE_SIZE
	 * Should get the DEFAULT_INDEX_CACHE_SIZE
	 */
	db_size = DEFAULT_INDEX_CACHE_SIZE - 1;
	ret = ldb_kv_reindex(module);
	assert_int_equal(LDB_SUCCESS, ret);

	assert_int_equal(
		DEFAULT_INDEX_CACHE_SIZE,
		tdb_hash_size(ldb_kv->idxptr->itdb));

	/*
	 * Use a value greater than the DEFAULT_INDEX_CACHE_SIZE
	 * Should get the value specified.
	 */
	db_size = DEFAULT_INDEX_CACHE_SIZE + 1;
	ret = ldb_kv_reindex(module);
	assert_int_equal(LDB_SUCCESS, ret);

	assert_int_equal(db_size, tdb_hash_size(ldb_kv->idxptr->itdb));

	TALLOC_FREE(ldb_kv);
	TALLOC_FREE(module);
}

/*
 * Test that ldb_kv_init_store sets the default index transaction cache size
 * if the option is not supplied.
 */
static void test_init_store_default_index_cache_size(void **state)
{
	struct test_ctx *test_ctx = talloc_get_type_abort(
		*state,
		struct test_ctx);
	struct ldb_module *module = NULL;
	struct ldb_kv_private *ldb_kv = NULL;
	struct ldb_context *ldb = NULL;
	int ret = LDB_SUCCESS;

	module = talloc_zero(test_ctx, struct ldb_module);
	ldb = talloc_zero(test_ctx, struct ldb_context);
	ldb_kv = talloc_zero(test_ctx, struct ldb_kv_private);

	ret = ldb_kv_init_store(ldb_kv, "test", ldb, NULL, &module);
	assert_int_equal(LDB_SUCCESS, ret);

	assert_int_equal(
		DEFAULT_INDEX_CACHE_SIZE,
		ldb_kv->index_transaction_cache_size);

	TALLOC_FREE(ldb_kv);
	TALLOC_FREE(module);
	TALLOC_FREE(ldb);
}

/*
 * Test that ldb_kv_init_store sets the index transaction cache size
 * to the value specified in the option.
 */
static void test_init_store_set_index_cache_size(void **state)
{
	struct test_ctx *test_ctx = talloc_get_type_abort(
		*state,
		struct test_ctx);
	struct ldb_module *module = NULL;
	struct ldb_kv_private *ldb_kv = NULL;
	struct ldb_context *ldb = NULL;
	const char *options[] = {"transaction_index_cache_size:1900", NULL};
	int ret = LDB_SUCCESS;

	module = talloc_zero(test_ctx, struct ldb_module);
	ldb = talloc_zero(test_ctx, struct ldb_context);
	ldb_kv = talloc_zero(test_ctx, struct ldb_kv_private);

	ret = ldb_kv_init_store(ldb_kv, "test", ldb, options, &module);
	assert_int_equal(LDB_SUCCESS, ret);

	assert_int_equal( 1900, ldb_kv->index_transaction_cache_size);

	TALLOC_FREE(ldb_kv);
	TALLOC_FREE(module);
	TALLOC_FREE(ldb);
}

/*
 * Test that ldb_kv_init_store sets the default index transaction cache size
 * if the value specified in the option is not a number.
 */
static void test_init_store_set_index_cache_size_non_numeric(void **state)
{
	struct test_ctx *test_ctx = talloc_get_type_abort(
		*state,
		struct test_ctx);
	struct ldb_module *module = NULL;
	struct ldb_kv_private *ldb_kv = NULL;
	struct ldb_context *ldb = NULL;
	const char *options[] = {"transaction_index_cache_size:fred", NULL};
	int ret = LDB_SUCCESS;

	module = talloc_zero(test_ctx, struct ldb_module);
	ldb = talloc_zero(test_ctx, struct ldb_context);
	ldb_kv = talloc_zero(test_ctx, struct ldb_kv_private);

	ret = ldb_kv_init_store(ldb_kv, "test", ldb, options, &module);
	assert_int_equal(LDB_SUCCESS, ret);

	assert_int_equal(
		DEFAULT_INDEX_CACHE_SIZE,
		ldb_kv->index_transaction_cache_size);

	TALLOC_FREE(ldb_kv);
	TALLOC_FREE(module);
	TALLOC_FREE(ldb);
}

/*
 * Test that ldb_kv_init_store sets the default index transaction cache size
 * if the value specified is too large
 */
static void test_init_store_set_index_cache_size_range(void **state)
{
	struct test_ctx *test_ctx = talloc_get_type_abort(
		*state,
		struct test_ctx);
	struct ldb_module *module = NULL;
	struct ldb_kv_private *ldb_kv = NULL;
	struct ldb_context *ldb = NULL;
	const char *options[] = {
		"transaction_index_cache_size:0xfffffffffffffffffffffffffffff",
		NULL};
	int ret = LDB_SUCCESS;

	module = talloc_zero(test_ctx, struct ldb_module);
	ldb = talloc_zero(test_ctx, struct ldb_context);
	ldb_kv = talloc_zero(test_ctx, struct ldb_kv_private);

	ret = ldb_kv_init_store(ldb_kv, "test", ldb, options, &module);
	assert_int_equal(LDB_SUCCESS, ret);

	assert_int_equal(
		DEFAULT_INDEX_CACHE_SIZE,
		ldb_kv->index_transaction_cache_size);

	TALLOC_FREE(ldb_kv);
	TALLOC_FREE(module);
	TALLOC_FREE(ldb);
}

int main(int argc, const char **argv)
{
	const struct CMUnitTest tests[] = {
		cmocka_unit_test_setup_teardown(
			test_index_cache_init,
			setup,
			teardown),
		cmocka_unit_test_setup_teardown(
			test_default_index_cache_size,
			setup,
			teardown),
		cmocka_unit_test_setup_teardown(
			test_reindex_cache_size,
			setup,
			teardown),
		cmocka_unit_test_setup_teardown(
			test_init_store_default_index_cache_size,
			setup,
			teardown),
		cmocka_unit_test_setup_teardown(
			test_init_store_set_index_cache_size,
			setup,
			teardown),
		cmocka_unit_test_setup_teardown(
			test_init_store_set_index_cache_size_non_numeric,
			setup,
			teardown),
		cmocka_unit_test_setup_teardown(
			test_init_store_set_index_cache_size_range,
			setup,
			teardown),
	};

	return cmocka_run_group_tests(tests, NULL, NULL);
}