ldb_mdb/tests: Tests for wrap open
[Samba.git] / lib / ldb / tests / ldb_lmdb_test.c
blobf2b04ecd0d7c410890ac9f2cb88e3b85bbe79009
1 /*
2 * lmdb backend specific tests for ldb
4 * Copyright (C) Andrew Bartlett <abartlet@samba.org> 2018
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 * lmdb backend specific tests for ldb
24 * Setup and tear down code copied from ldb_mod_op_test.c
28 * from cmocka.c:
29 * These headers or their equivalents should be included prior to
30 * including
31 * this header file.
33 * #include <stdarg.h>
34 * #include <stddef.h>
35 * #include <setjmp.h>
37 * This allows test applications to use custom definitions of C standard
38 * library functions and types.
41 #include <stdarg.h>
42 #include <stddef.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 <ldb.h>
51 #include <ldb_module.h>
52 #include <ldb_private.h>
53 #include <string.h>
54 #include <ctype.h>
56 #include <sys/wait.h>
58 #include "../ldb_tdb/ldb_tdb.h"
59 #include "../ldb_mdb/ldb_mdb.h"
61 #define TEST_BE "mdb"
63 #define LMDB_MAX_KEY_SIZE 511
65 struct ldbtest_ctx {
66 struct tevent_context *ev;
67 struct ldb_context *ldb;
69 const char *dbfile;
70 const char *lockfile; /* lockfile is separate */
72 const char *dbpath;
75 static void unlink_old_db(struct ldbtest_ctx *test_ctx)
77 int ret;
79 errno = 0;
80 ret = unlink(test_ctx->lockfile);
81 if (ret == -1 && errno != ENOENT) {
82 fail();
85 errno = 0;
86 ret = unlink(test_ctx->dbfile);
87 if (ret == -1 && errno != ENOENT) {
88 fail();
92 static int ldbtest_noconn_setup(void **state)
94 struct ldbtest_ctx *test_ctx;
96 test_ctx = talloc_zero(NULL, struct ldbtest_ctx);
97 assert_non_null(test_ctx);
99 test_ctx->ev = tevent_context_init(test_ctx);
100 assert_non_null(test_ctx->ev);
102 test_ctx->ldb = ldb_init(test_ctx, test_ctx->ev);
103 assert_non_null(test_ctx->ldb);
105 test_ctx->dbfile = talloc_strdup(test_ctx, "apitest.ldb");
106 assert_non_null(test_ctx->dbfile);
108 test_ctx->lockfile = talloc_asprintf(test_ctx, "%s-lock",
109 test_ctx->dbfile);
110 assert_non_null(test_ctx->lockfile);
112 test_ctx->dbpath = talloc_asprintf(test_ctx,
113 TEST_BE"://%s", test_ctx->dbfile);
114 assert_non_null(test_ctx->dbpath);
116 unlink_old_db(test_ctx);
117 *state = test_ctx;
118 return 0;
121 static int ldbtest_noconn_teardown(void **state)
123 struct ldbtest_ctx *test_ctx = talloc_get_type_abort(*state,
124 struct ldbtest_ctx);
126 unlink_old_db(test_ctx);
127 talloc_free(test_ctx);
128 return 0;
131 static int ldbtest_setup(void **state)
133 struct ldbtest_ctx *test_ctx;
134 int ret;
135 struct ldb_ldif *ldif;
136 const char *index_ldif = \
137 "dn: @INDEXLIST\n"
138 "@IDXGUID: objectUUID\n"
139 "@IDX_DN_GUID: GUID\n"
140 "\n";
142 ldbtest_noconn_setup((void **) &test_ctx);
144 ret = ldb_connect(test_ctx->ldb, test_ctx->dbpath, 0, NULL);
145 assert_int_equal(ret, 0);
147 while ((ldif = ldb_ldif_read_string(test_ctx->ldb, &index_ldif))) {
148 ret = ldb_add(test_ctx->ldb, ldif->msg);
149 assert_int_equal(ret, LDB_SUCCESS);
151 *state = test_ctx;
152 return 0;
155 static int ldbtest_teardown(void **state)
157 struct ldbtest_ctx *test_ctx = talloc_get_type_abort(*state,
158 struct ldbtest_ctx);
159 ldbtest_noconn_teardown((void **) &test_ctx);
160 return 0;
163 static void test_ldb_add_key_len_gt_max(void **state)
165 int ret;
166 int xs_size = 0;
167 struct ldb_message *msg;
168 struct ldbtest_ctx *test_ctx = talloc_get_type_abort(*state,
169 struct ldbtest_ctx);
170 char *xs = NULL;
171 TALLOC_CTX *tmp_ctx;
173 tmp_ctx = talloc_new(test_ctx);
174 assert_non_null(tmp_ctx);
176 msg = ldb_msg_new(tmp_ctx);
177 assert_non_null(msg);
180 * The zero terminator is part of the key if we were not in
181 * GUID mode
184 xs_size = LMDB_MAX_KEY_SIZE - 7; /* "dn=dc=" and the zero terminator */
185 xs_size += 1; /* want key on char too long */
186 xs = talloc_zero_size(tmp_ctx, (xs_size + 1));
187 memset(xs, 'x', xs_size);
189 msg->dn = ldb_dn_new_fmt(msg, test_ctx->ldb, "dc=%s", xs);
190 assert_non_null(msg->dn);
192 ret = ldb_msg_add_string(msg, "cn", "test_cn_val");
193 assert_int_equal(ret, 0);
195 ret = ldb_msg_add_string(msg, "objectUUID", "0123456789abcdef");
196 assert_int_equal(ret, 0);
198 ret = ldb_add(test_ctx->ldb, msg);
199 assert_int_equal(ret, LDB_SUCCESS);
201 talloc_free(tmp_ctx);
204 static void test_ldb_add_key_len_eq_max(void **state)
206 int ret;
207 int xs_size = 0;
208 struct ldb_message *msg;
209 struct ldbtest_ctx *test_ctx = talloc_get_type_abort(*state,
210 struct ldbtest_ctx);
211 char *xs = NULL;
212 TALLOC_CTX *tmp_ctx;
214 tmp_ctx = talloc_new(test_ctx);
215 assert_non_null(tmp_ctx);
217 msg = ldb_msg_new(tmp_ctx);
218 assert_non_null(msg);
221 * The zero terminator is part of the key if we were not in
222 * GUID mode
225 xs_size = LMDB_MAX_KEY_SIZE - 7; /* "dn=dc=" and the zero terminator */
226 xs = talloc_zero_size(tmp_ctx, (xs_size + 1));
227 memset(xs, 'x', xs_size);
229 msg->dn = ldb_dn_new_fmt(msg, test_ctx->ldb, "dc=%s", xs);
230 assert_non_null(msg->dn);
232 ret = ldb_msg_add_string(msg, "cn", "test_cn_val");
233 assert_int_equal(ret, 0);
235 ret = ldb_msg_add_string(msg, "objectUUID", "0123456789abcdef");
236 assert_int_equal(ret, 0);
238 ret = ldb_add(test_ctx->ldb, msg);
239 assert_int_equal(ret, 0);
241 talloc_free(tmp_ctx);
244 static int ldbtest_setup_noguid(void **state)
246 struct ldbtest_ctx *test_ctx;
247 int ret;
249 ldbtest_noconn_setup((void **) &test_ctx);
251 ret = ldb_connect(test_ctx->ldb, test_ctx->dbpath, 0, NULL);
252 assert_int_equal(ret, 0);
254 *state = test_ctx;
255 return 0;
258 static void test_ldb_add_special_key_len_gt_max(void **state)
260 int ret;
261 int xs_size = 0;
262 struct ldb_message *msg;
263 struct ldbtest_ctx *test_ctx = talloc_get_type_abort(*state,
264 struct ldbtest_ctx);
265 char *xs = NULL;
266 TALLOC_CTX *tmp_ctx;
268 tmp_ctx = talloc_new(test_ctx);
269 assert_non_null(tmp_ctx);
271 msg = ldb_msg_new(tmp_ctx);
272 assert_non_null(msg);
275 * The zero terminator is part of the key if we were not in
276 * GUID mode
279 xs_size = LMDB_MAX_KEY_SIZE - 5; /* "dn=@" and the zero terminator */
280 xs_size += 1; /* want key on char too long */
281 xs = talloc_zero_size(tmp_ctx, (xs_size + 1));
282 memset(xs, 'x', xs_size);
284 msg->dn = ldb_dn_new_fmt(msg, test_ctx->ldb, "@%s", xs);
285 assert_non_null(msg->dn);
287 ret = ldb_msg_add_string(msg, "cn", "test_cn_val");
288 assert_int_equal(ret, 0);
290 ret = ldb_add(test_ctx->ldb, msg);
291 assert_int_equal(ret, LDB_ERR_PROTOCOL_ERROR);
293 talloc_free(tmp_ctx);
296 static void test_ldb_add_special_key_len_eq_max(void **state)
298 int ret;
299 int xs_size = 0;
300 struct ldb_message *msg;
301 struct ldbtest_ctx *test_ctx = talloc_get_type_abort(*state,
302 struct ldbtest_ctx);
303 char *xs = NULL;
304 TALLOC_CTX *tmp_ctx;
306 tmp_ctx = talloc_new(test_ctx);
307 assert_non_null(tmp_ctx);
309 msg = ldb_msg_new(tmp_ctx);
310 assert_non_null(msg);
313 * The zero terminator is part of the key if we were not in
314 * GUID mode
317 xs_size = LMDB_MAX_KEY_SIZE - 5; /* "dn=@" and the zero terminator */
318 xs = talloc_zero_size(tmp_ctx, (xs_size + 1));
319 memset(xs, 'x', xs_size);
321 msg->dn = ldb_dn_new_fmt(msg, test_ctx->ldb, "@%s", xs);
322 assert_non_null(msg->dn);
324 ret = ldb_msg_add_string(msg, "cn", "test_cn_val");
325 assert_int_equal(ret, 0);
327 ret = ldb_add(test_ctx->ldb, msg);
328 assert_int_equal(ret, LDB_SUCCESS);
330 talloc_free(tmp_ctx);
333 static void test_ldb_add_dn_no_guid_mode(void **state)
335 int ret;
336 int xs_size = 0;
337 struct ldb_message *msg;
338 struct ldbtest_ctx *test_ctx = talloc_get_type_abort(*state,
339 struct ldbtest_ctx);
340 char *xs = NULL;
341 TALLOC_CTX *tmp_ctx;
343 tmp_ctx = talloc_new(test_ctx);
344 assert_non_null(tmp_ctx);
346 msg = ldb_msg_new(tmp_ctx);
347 assert_non_null(msg);
350 * The zero terminator is part of the key if we were not in
351 * GUID mode
354 xs_size = LMDB_MAX_KEY_SIZE - 7; /* "dn=dc=" and the zero terminator */
355 xs_size += 1; /* want key on char too long */
356 xs = talloc_zero_size(tmp_ctx, (xs_size + 1));
357 memset(xs, 'x', xs_size);
359 msg->dn = ldb_dn_new_fmt(msg, test_ctx->ldb, "dc=%s", xs);
360 assert_non_null(msg->dn);
362 ret = ldb_msg_add_string(msg, "cn", "test_cn_val");
363 assert_int_equal(ret, 0);
365 ret = ldb_msg_add_string(msg, "objectUUID", "0123456789abcdef");
366 assert_int_equal(ret, 0);
368 ret = ldb_add(test_ctx->ldb, msg);
369 assert_int_equal(ret, LDB_ERR_UNWILLING_TO_PERFORM);
371 talloc_free(tmp_ctx);
374 static struct MDB_env *get_mdb_env(struct ldb_context *ldb)
376 void *data = NULL;
377 struct ltdb_private *ltdb = NULL;
378 struct lmdb_private *lmdb = NULL;
379 struct MDB_env *env = NULL;
381 data = ldb_module_get_private(ldb->modules);
382 assert_non_null(data);
384 ltdb = talloc_get_type(data, struct ltdb_private);
385 assert_non_null(ltdb);
387 lmdb = ltdb->lmdb_private;
388 assert_non_null(lmdb);
390 env = lmdb->env;
391 assert_non_null(env);
393 return env;
396 static void test_multiple_opens(void **state)
398 struct ldb_context *ldb1 = NULL;
399 struct ldb_context *ldb2 = NULL;
400 struct ldb_context *ldb3 = NULL;
401 struct MDB_env *env1 = NULL;
402 struct MDB_env *env2 = NULL;
403 struct MDB_env *env3 = NULL;
404 int ret;
405 struct ldbtest_ctx *test_ctx = NULL;
407 test_ctx = talloc_get_type_abort(*state, struct ldbtest_ctx);
410 * Open the database again
412 ldb1 = ldb_init(test_ctx, test_ctx->ev);
413 ret = ldb_connect(ldb1, test_ctx->dbpath, LDB_FLG_RDONLY, NULL);
414 assert_int_equal(ret, 0);
416 ldb2 = ldb_init(test_ctx, test_ctx->ev);
417 ret = ldb_connect(ldb2, test_ctx->dbpath, LDB_FLG_RDONLY, NULL);
418 assert_int_equal(ret, 0);
420 ldb3 = ldb_init(test_ctx, test_ctx->ev);
421 ret = ldb_connect(ldb3, test_ctx->dbpath, 0, NULL);
422 assert_int_equal(ret, 0);
424 * We now have 3 ldb's open pointing to the same on disk database
425 * they should all share the same MDB_env
427 env1 = get_mdb_env(ldb1);
428 env2 = get_mdb_env(ldb2);
429 env3 = get_mdb_env(ldb3);
431 assert_ptr_equal(env1, env2);
432 assert_ptr_equal(env1, env3);
435 int main(int argc, const char **argv)
437 const struct CMUnitTest tests[] = {
438 cmocka_unit_test_setup_teardown(
439 test_ldb_add_key_len_eq_max,
440 ldbtest_setup,
441 ldbtest_teardown),
442 cmocka_unit_test_setup_teardown(
443 test_ldb_add_key_len_gt_max,
444 ldbtest_setup,
445 ldbtest_teardown),
446 cmocka_unit_test_setup_teardown(
447 test_ldb_add_special_key_len_eq_max,
448 ldbtest_setup_noguid,
449 ldbtest_teardown),
450 cmocka_unit_test_setup_teardown(
451 test_ldb_add_special_key_len_gt_max,
452 ldbtest_setup_noguid,
453 ldbtest_teardown),
454 cmocka_unit_test_setup_teardown(
455 test_ldb_add_dn_no_guid_mode,
456 ldbtest_setup_noguid,
457 ldbtest_teardown),
458 cmocka_unit_test_setup_teardown(
459 test_multiple_opens,
460 ldbtest_setup,
461 ldbtest_teardown),
464 return cmocka_run_group_tests(tests, NULL, NULL);