ctdb-daemon: Use ctdb_parse_node_address() in ctdbd
[samba4-gss.git] / lib / ldb / tests / ldb_mod_op_test.c
blobbe4f458d27f5e6ae6be15eaa9c4e4a1ecfbd7bd6
1 /*
2 * from cmocka.c:
3 * These headers or their equivalents should be included prior to
4 * including
5 * this header file.
7 * #include <stdarg.h>
8 * #include <stddef.h>
9 * #include <setjmp.h>
11 * This allows test applications to use custom definitions of C standard
12 * library functions and types.
14 #include <stdarg.h>
15 #include <stddef.h>
16 #include <stdint.h>
17 #include <setjmp.h>
18 #include <cmocka.h>
20 #include <errno.h>
21 #include <unistd.h>
22 #include <talloc.h>
24 #define TEVENT_DEPRECATED 1
25 #include <tevent.h>
27 #include <ldb.h>
28 #include <ldb_module.h>
29 #include <ldb_private.h>
30 #include <string.h>
31 #include <ctype.h>
33 #include <sys/wait.h>
36 #define DEFAULT_BE "tdb"
38 #ifndef TEST_BE
39 #define TEST_BE DEFAULT_BE
40 #endif /* TEST_BE */
42 #ifdef TEST_LMDB
43 #include "lmdb.h"
44 #include "../ldb_tdb/ldb_tdb.h"
45 #include "../ldb_mdb/ldb_mdb.h"
46 #endif
48 struct ldbtest_ctx {
49 struct tevent_context *ev;
50 struct ldb_context *ldb;
52 const char *dbfile;
53 const char *lockfile; /* lockfile is separate */
55 const char *dbpath;
56 char *debug_string;
59 static void unlink_old_db(struct ldbtest_ctx *test_ctx)
61 int ret;
63 errno = 0;
64 ret = unlink(test_ctx->lockfile);
65 if (ret == -1 && errno != ENOENT) {
66 fail();
69 errno = 0;
70 ret = unlink(test_ctx->dbfile);
71 if (ret == -1 && errno != ENOENT) {
72 fail();
76 static int ldbtest_noconn_setup(void **state)
78 struct ldbtest_ctx *test_ctx;
80 test_ctx = talloc_zero(NULL, struct ldbtest_ctx);
81 assert_non_null(test_ctx);
83 test_ctx->ev = tevent_context_init(test_ctx);
84 assert_non_null(test_ctx->ev);
86 test_ctx->ldb = ldb_init(test_ctx, test_ctx->ev);
87 assert_non_null(test_ctx->ldb);
89 test_ctx->dbfile = talloc_strdup(test_ctx, "apitest.ldb");
90 assert_non_null(test_ctx->dbfile);
92 test_ctx->lockfile = talloc_asprintf(test_ctx, "%s-lock",
93 test_ctx->dbfile);
94 assert_non_null(test_ctx->lockfile);
96 test_ctx->dbpath = talloc_asprintf(test_ctx,
97 TEST_BE"://%s", test_ctx->dbfile);
98 assert_non_null(test_ctx->dbpath);
100 unlink_old_db(test_ctx);
101 *state = test_ctx;
102 return 0;
105 static int ldbtest_noconn_teardown(void **state)
107 struct ldbtest_ctx *test_ctx = talloc_get_type_abort(*state,
108 struct ldbtest_ctx);
110 unlink_old_db(test_ctx);
111 talloc_free(test_ctx);
112 return 0;
115 static void test_connect(void **state)
117 struct ldbtest_ctx *test_ctx = talloc_get_type_abort(*state,
118 struct ldbtest_ctx);
119 int ret;
121 ret = ldb_connect(test_ctx->ldb, test_ctx->dbpath, 0, NULL);
122 assert_int_equal(ret, 0);
125 static struct ldb_message *get_test_ldb_message(TALLOC_CTX *mem_ctx,
126 struct ldb_context *ldb)
128 struct ldb_message *msg = ldb_msg_new(mem_ctx);
129 int ret;
130 assert_non_null(msg);
132 msg->dn = ldb_dn_new(msg, ldb, "dc=samba,dc=org");
133 assert_non_null(msg->dn);
134 ret = ldb_msg_add_string(msg, "public", "key");
135 assert_int_equal(ret, LDB_SUCCESS);
136 ret = ldb_msg_add_string(msg, "supersecret", "password");
137 assert_int_equal(ret, LDB_SUCCESS);
138 ret = ldb_msg_add_string(msg, "binary", "\xff\xff\0");
139 assert_int_equal(ret, LDB_SUCCESS);
140 return msg;
143 static void test_ldif_message(void **state)
145 struct ldbtest_ctx *test_ctx = talloc_get_type_abort(*state,
146 struct ldbtest_ctx);
147 char *got_ldif;
148 const char *expected_ldif =
149 "dn: dc=samba,dc=org\n"
150 "changetype: add\n"
151 "public: key\n"
152 "supersecret: password\n"
153 "binary:: //8=\n"
154 "\n";
156 struct ldb_message *msg = get_test_ldb_message(test_ctx,
157 test_ctx->ldb);
159 got_ldif = ldb_ldif_message_string(test_ctx->ldb,
160 test_ctx,
161 LDB_CHANGETYPE_ADD,
162 msg);
163 assert_string_equal(got_ldif, expected_ldif);
164 TALLOC_FREE(got_ldif);
167 static void test_ldif_message_redacted(void **state)
169 struct ldbtest_ctx *test_ctx = talloc_get_type_abort(*state,
170 struct ldbtest_ctx);
171 int ret;
172 char *got_ldif;
173 const char *expected_ldif =
174 "dn: dc=samba,dc=org\n"
175 "changetype: add\n"
176 "public: key\n"
177 "# supersecret::: REDACTED SECRET ATTRIBUTE\n"
178 "binary:: //8=\n"
179 "\n";
181 const char *secret_attrs[] = {
182 "supersecret",
183 NULL
186 struct ldb_message *msg = ldb_msg_new(test_ctx);
188 ldb_set_opaque(test_ctx->ldb,
189 LDB_SECRET_ATTRIBUTE_LIST_OPAQUE,
190 secret_attrs);
192 assert_non_null(msg);
194 msg->dn = ldb_dn_new(msg, test_ctx->ldb, "dc=samba,dc=org");
195 ret = ldb_msg_add_string(msg, "public", "key");
196 assert_int_equal(ret, LDB_SUCCESS);
197 ret = ldb_msg_add_string(msg, "supersecret", "password");
198 assert_int_equal(ret, LDB_SUCCESS);
199 ret = ldb_msg_add_string(msg, "binary", "\xff\xff\0");
200 assert_int_equal(ret, LDB_SUCCESS);
201 got_ldif = ldb_ldif_message_redacted_string(test_ctx->ldb,
202 test_ctx,
203 LDB_CHANGETYPE_ADD,
204 msg);
205 assert_string_equal(got_ldif, expected_ldif);
206 TALLOC_FREE(got_ldif);
207 assert_int_equal(ret, 0);
210 static int ldbtest_setup(void **state)
212 struct ldbtest_ctx *test_ctx;
213 struct ldb_ldif *ldif;
214 #ifdef GUID_IDX
215 const char *index_ldif = \
216 "dn: @INDEXLIST\n"
217 "@IDXGUID: objectUUID\n"
218 "@IDX_DN_GUID: GUID\n"
219 "\n";
220 #else
221 const char *index_ldif = "\n";
222 #endif
223 int ret;
225 ldbtest_noconn_setup((void **) &test_ctx);
227 ret = ldb_connect(test_ctx->ldb, test_ctx->dbpath, 0, NULL);
228 assert_int_equal(ret, 0);
230 while ((ldif = ldb_ldif_read_string(test_ctx->ldb, &index_ldif))) {
231 ret = ldb_add(test_ctx->ldb, ldif->msg);
232 assert_int_equal(ret, LDB_SUCCESS);
234 *state = test_ctx;
235 return 0;
238 static int ldbtest_teardown(void **state)
240 struct ldbtest_ctx *test_ctx = talloc_get_type_abort(*state,
241 struct ldbtest_ctx);
242 ldbtest_noconn_teardown((void **) &test_ctx);
243 return 0;
246 static void test_ldb_add(void **state)
248 int ret;
249 struct ldb_message *msg;
250 struct ldbtest_ctx *test_ctx = talloc_get_type_abort(*state,
251 struct ldbtest_ctx);
252 TALLOC_CTX *tmp_ctx;
254 tmp_ctx = talloc_new(test_ctx);
255 assert_non_null(tmp_ctx);
257 msg = ldb_msg_new(tmp_ctx);
258 assert_non_null(msg);
260 msg->dn = ldb_dn_new_fmt(msg, test_ctx->ldb, "dc=test");
261 assert_non_null(msg->dn);
263 ret = ldb_msg_add_string(msg, "cn", "test_cn_val");
264 assert_int_equal(ret, 0);
266 ret = ldb_msg_add_string(msg, "objectUUID", "0123456789abcdef");
267 assert_int_equal(ret, 0);
269 ret = ldb_add(test_ctx->ldb, msg);
270 assert_int_equal(ret, 0);
272 talloc_free(tmp_ctx);
275 static void test_ldb_search(void **state)
277 int ret;
278 struct ldb_message *msg;
279 struct ldbtest_ctx *test_ctx = talloc_get_type_abort(*state,
280 struct ldbtest_ctx);
281 TALLOC_CTX *tmp_ctx;
282 struct ldb_dn *basedn;
283 struct ldb_dn *basedn2;
284 struct ldb_result *result = NULL;
286 tmp_ctx = talloc_new(test_ctx);
287 assert_non_null(tmp_ctx);
289 basedn = ldb_dn_new_fmt(tmp_ctx, test_ctx->ldb, "dc=test");
290 assert_non_null(basedn);
292 ret = ldb_search(test_ctx->ldb, tmp_ctx, &result, basedn,
293 LDB_SCOPE_BASE, NULL, NULL);
294 assert_int_equal(ret, 0);
295 assert_non_null(result);
296 assert_int_equal(result->count, 0);
298 msg = ldb_msg_new(tmp_ctx);
299 assert_non_null(msg);
301 msg->dn = basedn;
302 assert_non_null(msg->dn);
304 ret = ldb_msg_add_string(msg, "cn", "test_cn_val1");
305 assert_int_equal(ret, 0);
307 ret = ldb_msg_add_string(msg, "objectUUID", "0123456789abcde1");
308 assert_int_equal(ret, 0);
310 ret = ldb_add(test_ctx->ldb, msg);
311 assert_int_equal(ret, 0);
313 basedn2 = ldb_dn_new_fmt(tmp_ctx, test_ctx->ldb, "dc=test2");
314 assert_non_null(basedn2);
316 msg = ldb_msg_new(tmp_ctx);
317 assert_non_null(msg);
319 msg->dn = basedn2;
320 assert_non_null(msg->dn);
322 ret = ldb_msg_add_string(msg, "cn", "test_cn_val2");
323 assert_int_equal(ret, 0);
325 ret = ldb_msg_add_string(msg, "objectUUID", "0123456789abcde2");
326 assert_int_equal(ret, 0);
328 ret = ldb_add(test_ctx->ldb, msg);
329 assert_int_equal(ret, 0);
331 ret = ldb_search(test_ctx->ldb, tmp_ctx, &result, basedn,
332 LDB_SCOPE_BASE, NULL, NULL);
333 assert_int_equal(ret, 0);
334 assert_non_null(result);
335 assert_int_equal(result->count, 1);
336 assert_string_equal(ldb_dn_get_linearized(result->msgs[0]->dn),
337 ldb_dn_get_linearized(basedn));
339 ret = ldb_search(test_ctx->ldb, tmp_ctx, &result, basedn2,
340 LDB_SCOPE_BASE, NULL, NULL);
341 assert_int_equal(ret, 0);
342 assert_non_null(result);
343 assert_int_equal(result->count, 1);
344 assert_string_equal(ldb_dn_get_linearized(result->msgs[0]->dn),
345 ldb_dn_get_linearized(basedn2));
347 talloc_free(tmp_ctx);
350 static int base_search_count(struct ldbtest_ctx *test_ctx, const char *entry_dn)
352 TALLOC_CTX *tmp_ctx;
353 struct ldb_dn *basedn;
354 struct ldb_result *result = NULL;
355 int ret;
356 int count;
358 tmp_ctx = talloc_new(test_ctx);
359 assert_non_null(tmp_ctx);
361 basedn = ldb_dn_new_fmt(tmp_ctx, test_ctx->ldb, "%s", entry_dn);
362 assert_non_null(basedn);
364 ret = ldb_search(test_ctx->ldb, tmp_ctx, &result, basedn,
365 LDB_SCOPE_BASE, NULL, NULL);
366 assert_int_equal(ret, LDB_SUCCESS);
367 assert_non_null(result);
369 count = result->count;
370 talloc_free(tmp_ctx);
371 return count;
374 static int sub_search_count(struct ldbtest_ctx *test_ctx,
375 const char *base_dn,
376 const char *filter)
378 TALLOC_CTX *tmp_ctx;
379 struct ldb_dn *basedn;
380 struct ldb_result *result = NULL;
381 int ret;
382 int count;
384 tmp_ctx = talloc_new(test_ctx);
385 assert_non_null(tmp_ctx);
387 basedn = ldb_dn_new_fmt(tmp_ctx, test_ctx->ldb, "%s", base_dn);
388 assert_non_null(basedn);
390 ret = ldb_search(test_ctx->ldb, tmp_ctx, &result, basedn,
391 LDB_SCOPE_SUBTREE, NULL, "%s", filter);
392 assert_int_equal(ret, LDB_SUCCESS);
393 assert_non_null(result);
395 count = result->count;
396 talloc_free(tmp_ctx);
397 return count;
400 /* In general it would be better if utility test functions didn't assert
401 * but only returned a value, then assert in the test shows correct
402 * line
404 static void assert_dn_exists(struct ldbtest_ctx *test_ctx,
405 const char *entry_dn)
407 int count;
409 count = base_search_count(test_ctx, entry_dn);
410 assert_int_equal(count, 1);
413 static void assert_dn_doesnt_exist(struct ldbtest_ctx *test_ctx,
414 const char *entry_dn)
416 int count;
418 count = base_search_count(test_ctx, entry_dn);
419 assert_int_equal(count, 0);
422 static void add_dn_with_cn(struct ldbtest_ctx *test_ctx,
423 struct ldb_dn *dn,
424 const char *cn_value,
425 const char *uuid_value)
427 int ret;
428 TALLOC_CTX *tmp_ctx;
429 struct ldb_message *msg;
431 tmp_ctx = talloc_new(test_ctx);
432 assert_non_null(tmp_ctx);
434 assert_dn_doesnt_exist(test_ctx,
435 ldb_dn_get_linearized(dn));
437 msg = ldb_msg_new(tmp_ctx);
438 assert_non_null(msg);
439 msg->dn = dn;
441 ret = ldb_msg_add_string(msg, "cn", cn_value);
442 assert_int_equal(ret, LDB_SUCCESS);
444 ret = ldb_msg_add_string(msg, "objectUUID", uuid_value);
445 assert_int_equal(ret, 0);
447 ret = ldb_add(test_ctx->ldb, msg);
448 assert_int_equal(ret, LDB_SUCCESS);
450 assert_dn_exists(test_ctx,
451 ldb_dn_get_linearized(dn));
452 talloc_free(tmp_ctx);
455 static void test_ldb_del(void **state)
457 int ret;
458 struct ldbtest_ctx *test_ctx = talloc_get_type_abort(*state,
459 struct ldbtest_ctx);
460 const char *basedn = "dc=ldb_del_test";
461 struct ldb_dn *dn;
463 dn = ldb_dn_new_fmt(test_ctx, test_ctx->ldb, "%s", basedn);
464 assert_non_null(dn);
466 add_dn_with_cn(test_ctx, dn,
467 "test_del_cn_val",
468 "0123456789abcdef");
470 ret = ldb_delete(test_ctx->ldb, dn);
471 assert_int_equal(ret, LDB_SUCCESS);
473 assert_dn_doesnt_exist(test_ctx, basedn);
476 static void test_ldb_del_noexist(void **state)
478 struct ldbtest_ctx *test_ctx = talloc_get_type_abort(*state,
479 struct ldbtest_ctx);
480 struct ldb_dn *basedn;
481 int ret;
483 basedn = ldb_dn_new(test_ctx, test_ctx->ldb, "dc=nosuchplace");
484 assert_non_null(basedn);
486 ret = ldb_delete(test_ctx->ldb, basedn);
487 assert_int_equal(ret, LDB_ERR_NO_SUCH_OBJECT);
490 static void test_ldb_handle(void **state)
492 int ret;
493 struct ldbtest_ctx *test_ctx = talloc_get_type_abort(*state,
494 struct ldbtest_ctx);
495 TALLOC_CTX *tmp_ctx;
496 struct ldb_dn *basedn;
497 struct ldb_request *request = NULL;
498 struct ldb_request *request2 = NULL;
499 struct ldb_result *res = NULL;
500 const char *attrs[] = { "cn", NULL };
502 tmp_ctx = talloc_new(test_ctx);
503 assert_non_null(tmp_ctx);
505 basedn = ldb_dn_new_fmt(tmp_ctx, test_ctx->ldb, "dc=test");
506 assert_non_null(basedn);
508 res = talloc_zero(tmp_ctx, struct ldb_result);
509 assert_non_null(res);
511 ret = ldb_build_search_req(&request, test_ctx->ldb, tmp_ctx,
512 basedn, LDB_SCOPE_BASE,
513 NULL, attrs, NULL, res,
514 ldb_search_default_callback,
515 NULL);
516 assert_int_equal(ret, 0);
518 /* We are against ldb_tdb, so expect private event contexts */
519 assert_ptr_not_equal(ldb_handle_get_event_context(request->handle),
520 ldb_get_event_context(test_ctx->ldb));
522 ret = ldb_build_search_req(&request2, test_ctx->ldb, tmp_ctx,
523 basedn, LDB_SCOPE_BASE,
524 NULL, attrs, NULL, res,
525 ldb_search_default_callback,
526 request);
527 assert_int_equal(ret, 0);
529 /* Expect that same event context will be chained */
530 assert_ptr_equal(ldb_handle_get_event_context(request->handle),
531 ldb_handle_get_event_context(request2->handle));
533 /* Now force this to use the global context */
534 ldb_handle_use_global_event_context(request2->handle);
535 assert_ptr_equal(ldb_handle_get_event_context(request2->handle),
536 ldb_get_event_context(test_ctx->ldb));
538 talloc_free(tmp_ctx);
541 static void test_ldb_build_search_req(void **state)
543 int ret;
544 struct ldbtest_ctx *test_ctx = talloc_get_type_abort(*state,
545 struct ldbtest_ctx);
546 TALLOC_CTX *tmp_ctx;
547 struct ldb_dn *basedn;
548 struct ldb_request *request = NULL;
549 struct ldb_request *request2 = NULL;
550 struct ldb_result *res = NULL;
551 const char *attrs[] = { "cn", NULL };
553 tmp_ctx = talloc_new(test_ctx);
554 assert_non_null(tmp_ctx);
556 basedn = ldb_dn_new_fmt(tmp_ctx, test_ctx->ldb, "dc=test");
557 assert_non_null(basedn);
559 res = talloc_zero(tmp_ctx, struct ldb_result);
560 assert_non_null(res);
562 ret = ldb_build_search_req(&request, test_ctx->ldb, tmp_ctx,
563 basedn, LDB_SCOPE_BASE,
564 NULL, attrs, NULL, res,
565 ldb_search_default_callback,
566 NULL);
567 assert_int_equal(ret, 0);
569 assert_int_equal(request->operation, LDB_SEARCH);
570 assert_ptr_equal(request->op.search.base, basedn);
571 assert_int_equal(request->op.search.scope, LDB_SCOPE_BASE);
572 assert_non_null(request->op.search.tree);
573 assert_ptr_equal(request->op.search.attrs, attrs);
574 assert_ptr_equal(request->context, res);
575 assert_ptr_equal(request->callback, ldb_search_default_callback);
577 ret = ldb_build_search_req(&request2, test_ctx->ldb, tmp_ctx,
578 basedn, LDB_SCOPE_BASE,
579 NULL, attrs, NULL, res,
580 ldb_search_default_callback,
581 request);
582 assert_int_equal(ret, 0);
583 assert_ptr_equal(request, request2->handle->parent);
584 assert_int_equal(request->starttime, request2->starttime);
585 assert_int_equal(request->timeout, request2->timeout);
587 talloc_free(tmp_ctx);
590 static void add_keyval(struct ldbtest_ctx *test_ctx,
591 const char *key,
592 const char *val,
593 const char *uuid)
595 int ret;
596 struct ldb_message *msg;
598 msg = ldb_msg_new(test_ctx);
599 assert_non_null(msg);
601 msg->dn = ldb_dn_new_fmt(msg, test_ctx->ldb, "%s=%s", key, val);
602 assert_non_null(msg->dn);
604 ret = ldb_msg_add_string(msg, key, val);
605 assert_int_equal(ret, 0);
607 ret = ldb_msg_add_string(msg, "objectUUID", uuid);
608 assert_int_equal(ret, 0);
610 ret = ldb_add(test_ctx->ldb, msg);
611 assert_int_equal(ret, 0);
613 talloc_free(msg);
616 static struct ldb_result *get_keyval(struct ldbtest_ctx *test_ctx,
617 const char *key,
618 const char *val)
620 int ret;
621 struct ldb_result *result;
622 struct ldb_dn *basedn;
624 basedn = ldb_dn_new_fmt(test_ctx, test_ctx->ldb, "%s=%s", key, val);
625 assert_non_null(basedn);
627 ret = ldb_search(test_ctx->ldb, test_ctx, &result, basedn,
628 LDB_SCOPE_BASE, NULL, NULL);
629 assert_int_equal(ret, 0);
631 return result;
634 static void test_transactions(void **state)
636 int ret;
637 struct ldbtest_ctx *test_ctx = talloc_get_type_abort(*state,
638 struct ldbtest_ctx);
639 struct ldb_result *res;
641 /* start lev-0 transaction */
642 ret = ldb_transaction_start(test_ctx->ldb);
643 assert_int_equal(ret, 0);
645 add_keyval(test_ctx, "vegetable", "carrot",
646 "0123456789abcde0");
648 /* commit lev-0 transaction */
649 ret = ldb_transaction_commit(test_ctx->ldb);
650 assert_int_equal(ret, 0);
652 /* start another lev-1 nested transaction */
653 ret = ldb_transaction_start(test_ctx->ldb);
654 assert_int_equal(ret, 0);
656 add_keyval(test_ctx, "fruit", "apple",
657 "0123456789abcde1");
659 /* abort lev-1 nested transaction */
660 ret = ldb_transaction_cancel(test_ctx->ldb);
661 assert_int_equal(ret, 0);
663 res = get_keyval(test_ctx, "vegetable", "carrot");
664 assert_non_null(res);
665 assert_int_equal(res->count, 1);
667 res = get_keyval(test_ctx, "fruit", "apple");
668 assert_non_null(res);
669 assert_int_equal(res->count, 0);
672 static void test_nested_transactions(void **state)
674 int ret;
675 struct ldbtest_ctx *test_ctx = talloc_get_type_abort(*state,
676 struct ldbtest_ctx);
677 struct ldb_result *res;
679 /* start lev-0 transaction */
680 ret = ldb_transaction_start(test_ctx->ldb);
681 assert_int_equal(ret, 0);
683 add_keyval(test_ctx, "vegetable", "carrot",
684 "0123456789abcde0");
687 /* start another lev-1 nested transaction */
688 ret = ldb_transaction_start(test_ctx->ldb);
689 assert_int_equal(ret, 0);
691 add_keyval(test_ctx, "fruit", "apple",
692 "0123456789abcde1");
694 /* abort lev-1 nested transaction */
695 ret = ldb_transaction_cancel(test_ctx->ldb);
696 assert_int_equal(ret, 0);
698 /* commit lev-0 transaction */
699 ret = ldb_transaction_commit(test_ctx->ldb);
700 assert_int_equal(ret, 0);
702 res = get_keyval(test_ctx, "vegetable", "carrot");
703 assert_non_null(res);
704 assert_int_equal(res->count, 1);
706 /* This documents the current ldb behaviour, i.e. nested
707 * transactions are not supported. And the cancellation of the nested
708 * transaction has no effect.
710 res = get_keyval(test_ctx, "fruit", "apple");
711 assert_non_null(res);
712 assert_int_equal(res->count, 1);
714 struct ldb_mod_test_ctx {
715 struct ldbtest_ctx *ldb_test_ctx;
716 const char *entry_dn;
719 struct keyval {
720 const char *key;
721 const char *val;
724 static struct ldb_message *build_mod_msg(TALLOC_CTX *mem_ctx,
725 struct ldbtest_ctx *test_ctx,
726 const char *dn,
727 int modify_flags,
728 struct keyval *kvs)
730 struct ldb_message *msg;
731 int ret;
732 int i;
734 msg = ldb_msg_new(mem_ctx);
735 assert_non_null(msg);
737 msg->dn = ldb_dn_new_fmt(msg, test_ctx->ldb, "%s", dn);
738 assert_non_null(msg->dn);
740 for (i = 0; kvs[i].key != NULL; i++) {
741 if (modify_flags) {
742 ret = ldb_msg_add_empty(msg, kvs[i].key,
743 modify_flags, NULL);
744 assert_int_equal(ret, 0);
747 if (kvs[i].val) {
748 ret = ldb_msg_add_string(msg, kvs[i].key, kvs[i].val);
749 assert_int_equal(ret, LDB_SUCCESS);
753 return msg;
756 static void ldb_test_add_data(TALLOC_CTX *mem_ctx,
757 struct ldbtest_ctx *ldb_test_ctx,
758 const char *basedn,
759 struct keyval *kvs)
761 TALLOC_CTX *tmp_ctx;
762 struct ldb_message *msg;
763 struct ldb_result *result = NULL;
764 int ret;
766 tmp_ctx = talloc_new(mem_ctx);
767 assert_non_null(tmp_ctx);
769 msg = build_mod_msg(tmp_ctx, ldb_test_ctx,
770 basedn, 0, kvs);
771 assert_non_null(msg);
773 ret = ldb_add(ldb_test_ctx->ldb, msg);
774 assert_int_equal(ret, LDB_SUCCESS);
776 ret = ldb_search(ldb_test_ctx->ldb, tmp_ctx, &result, msg->dn,
777 LDB_SCOPE_BASE, NULL, NULL);
778 assert_int_equal(ret, LDB_SUCCESS);
779 assert_non_null(result);
780 assert_int_equal(result->count, 1);
781 assert_string_equal(ldb_dn_get_linearized(result->msgs[0]->dn),
782 ldb_dn_get_linearized(msg->dn));
784 talloc_free(tmp_ctx);
787 static void ldb_test_remove_data(TALLOC_CTX *mem_ctx,
788 struct ldbtest_ctx *ldb_test_ctx,
789 const char *strdn)
791 TALLOC_CTX *tmp_ctx;
792 struct ldb_dn *basedn;
793 int ret;
794 size_t count;
796 tmp_ctx = talloc_new(mem_ctx);
797 assert_non_null(tmp_ctx);
799 basedn = ldb_dn_new_fmt(tmp_ctx, ldb_test_ctx->ldb,
800 "%s", strdn);
801 assert_non_null(basedn);
803 ret = ldb_delete(ldb_test_ctx->ldb, basedn);
804 assert_true(ret == LDB_SUCCESS || ret == LDB_ERR_NO_SUCH_OBJECT);
806 count = base_search_count(ldb_test_ctx, ldb_dn_get_linearized(basedn));
807 assert_int_equal(count, 0);
809 talloc_free(tmp_ctx);
812 static void mod_test_add_data(struct ldb_mod_test_ctx *mod_test_ctx,
813 struct keyval *kvs)
815 ldb_test_add_data(mod_test_ctx,
816 mod_test_ctx->ldb_test_ctx,
817 mod_test_ctx->entry_dn,
818 kvs);
821 static void mod_test_remove_data(struct ldb_mod_test_ctx *mod_test_ctx)
823 ldb_test_remove_data(mod_test_ctx,
824 mod_test_ctx->ldb_test_ctx,
825 mod_test_ctx->entry_dn);
828 static struct ldb_result *run_mod_test(struct ldb_mod_test_ctx *mod_test_ctx,
829 int modify_flags,
830 struct keyval *kvs)
832 TALLOC_CTX *tmp_ctx;
833 struct ldb_result *res;
834 struct ldb_message *mod_msg;
835 struct ldb_dn *basedn;
836 struct ldbtest_ctx *ldb_test_ctx;
837 int ret;
839 ldb_test_ctx = mod_test_ctx->ldb_test_ctx;
841 tmp_ctx = talloc_new(mod_test_ctx);
842 assert_non_null(tmp_ctx);
844 mod_msg = build_mod_msg(tmp_ctx, ldb_test_ctx, mod_test_ctx->entry_dn,
845 modify_flags, kvs);
846 assert_non_null(mod_msg);
848 ret = ldb_modify(ldb_test_ctx->ldb, mod_msg);
849 assert_int_equal(ret, LDB_SUCCESS);
851 basedn = ldb_dn_new_fmt(tmp_ctx, ldb_test_ctx->ldb,
852 "%s", mod_test_ctx->entry_dn);
853 assert_non_null(basedn);
855 ret = ldb_search(ldb_test_ctx->ldb, mod_test_ctx, &res, basedn,
856 LDB_SCOPE_BASE, NULL, NULL);
857 assert_int_equal(ret, LDB_SUCCESS);
858 assert_non_null(res);
859 assert_int_equal(res->count, 1);
860 assert_string_equal(ldb_dn_get_linearized(res->msgs[0]->dn),
861 ldb_dn_get_linearized(mod_msg->dn));
863 talloc_free(tmp_ctx);
864 return res;
867 static int ldb_modify_test_setup(void **state)
869 struct ldbtest_ctx *ldb_test_ctx;
870 struct ldb_mod_test_ctx *mod_test_ctx;
871 struct keyval kvs[] = {
872 { "cn", "test_mod_cn" },
873 { "objectUUID", "0123456789abcdef"},
874 { NULL, NULL },
877 ldbtest_setup((void **) &ldb_test_ctx);
879 mod_test_ctx = talloc(ldb_test_ctx, struct ldb_mod_test_ctx);
880 assert_non_null(mod_test_ctx);
882 mod_test_ctx->entry_dn = "dc=mod_test_entry";
883 mod_test_ctx->ldb_test_ctx = ldb_test_ctx;
885 mod_test_remove_data(mod_test_ctx);
886 mod_test_add_data(mod_test_ctx, kvs);
887 *state = mod_test_ctx;
888 return 0;
891 static int ldb_modify_test_teardown(void **state)
893 struct ldb_mod_test_ctx *mod_test_ctx = \
894 talloc_get_type_abort(*state,
895 struct ldb_mod_test_ctx);
896 struct ldbtest_ctx *ldb_test_ctx;
898 ldb_test_ctx = mod_test_ctx->ldb_test_ctx;
900 mod_test_remove_data(mod_test_ctx);
901 talloc_free(mod_test_ctx);
903 ldbtest_teardown((void **) &ldb_test_ctx);
904 return 0;
907 static void test_ldb_modify_add_key(void **state)
909 struct ldb_mod_test_ctx *mod_test_ctx = \
910 talloc_get_type_abort(*state,
911 struct ldb_mod_test_ctx);
912 struct keyval mod_kvs[] = {
913 { "name", "test_mod_name" },
914 { NULL, NULL },
916 struct ldb_result *res;
917 struct ldb_message_element *el;
919 res = run_mod_test(mod_test_ctx, LDB_FLAG_MOD_ADD, mod_kvs);
920 assert_non_null(res);
922 /* Check cn is intact and name was added */
923 assert_int_equal(res->count, 1);
924 el = ldb_msg_find_element(res->msgs[0], "cn");
925 assert_non_null(el);
926 assert_int_equal(el->num_values, 1);
927 assert_string_equal((const char *)el->values[0].data, "test_mod_cn");
929 el = ldb_msg_find_element(res->msgs[0], "name");
930 assert_non_null(el);
931 assert_int_equal(el->num_values, 1);
932 assert_string_equal((const char *)el->values[0].data, "test_mod_name");
935 static void test_ldb_modify_extend_key(void **state)
937 struct ldb_mod_test_ctx *mod_test_ctx = \
938 talloc_get_type_abort(*state,
939 struct ldb_mod_test_ctx);
940 struct keyval mod_kvs[] = {
941 { "cn", "test_mod_cn2" },
942 { NULL, NULL },
944 struct ldb_result *res;
945 struct ldb_message_element *el;
947 res = run_mod_test(mod_test_ctx, LDB_FLAG_MOD_ADD, mod_kvs);
948 assert_non_null(res);
950 /* Check cn was extended with another value */
951 assert_int_equal(res->count, 1);
952 el = ldb_msg_find_element(res->msgs[0], "cn");
953 assert_non_null(el);
954 assert_int_equal(el->num_values, 2);
955 assert_string_equal((const char *)el->values[0].data, "test_mod_cn");
956 assert_string_equal((const char *)el->values[1].data, "test_mod_cn2");
959 static void test_ldb_modify_add_key_noval(void **state)
961 struct ldb_mod_test_ctx *mod_test_ctx = \
962 talloc_get_type_abort(*state,
963 struct ldb_mod_test_ctx);
964 struct ldb_message *mod_msg;
965 struct ldbtest_ctx *ldb_test_ctx;
966 struct ldb_message_element *el;
967 int ret;
969 ldb_test_ctx = mod_test_ctx->ldb_test_ctx;
971 mod_msg = ldb_msg_new(mod_test_ctx);
972 assert_non_null(mod_msg);
974 mod_msg->dn = ldb_dn_new_fmt(mod_msg, ldb_test_ctx->ldb,
975 "%s", mod_test_ctx->entry_dn);
976 assert_non_null(mod_msg->dn);
978 el = talloc_zero(mod_msg, struct ldb_message_element);
979 el->flags = LDB_FLAG_MOD_ADD;
980 assert_non_null(el);
981 el->name = talloc_strdup(el, "cn");
982 assert_non_null(el->name);
984 mod_msg->elements = el;
985 mod_msg->num_elements = 1;
987 ret = ldb_modify(ldb_test_ctx->ldb, mod_msg);
988 assert_int_equal(ret, LDB_ERR_CONSTRAINT_VIOLATION);
991 static void test_ldb_modify_replace_key(void **state)
993 struct ldb_mod_test_ctx *mod_test_ctx = \
994 talloc_get_type_abort(*state,
995 struct ldb_mod_test_ctx);
996 const char *new_cn = "new_cn";
997 struct keyval mod_kvs[] = {
998 { "cn", new_cn },
999 { NULL, NULL },
1001 struct ldb_result *res;
1002 struct ldb_message_element *el;
1004 res = run_mod_test(mod_test_ctx, LDB_FLAG_MOD_REPLACE, mod_kvs);
1005 assert_non_null(res);
1007 /* Check cn was replaced */
1008 assert_int_equal(res->count, 1);
1009 el = ldb_msg_find_element(res->msgs[0], "cn");
1010 assert_non_null(el);
1011 assert_int_equal(el->num_values, 1);
1012 assert_string_equal((const char *)el->values[0].data, new_cn);
1015 static void test_ldb_modify_replace_noexist_key(void **state)
1017 struct ldb_mod_test_ctx *mod_test_ctx = \
1018 talloc_get_type_abort(*state,
1019 struct ldb_mod_test_ctx);
1020 struct keyval mod_kvs[] = {
1021 { "name", "name_val" },
1022 { NULL, NULL },
1024 struct ldb_result *res;
1025 struct ldb_message_element *el;
1027 res = run_mod_test(mod_test_ctx, LDB_FLAG_MOD_REPLACE, mod_kvs);
1028 assert_non_null(res);
1030 /* Check cn is intact and name was added */
1031 assert_int_equal(res->count, 1);
1032 el = ldb_msg_find_element(res->msgs[0], "cn");
1033 assert_non_null(el);
1034 assert_int_equal(el->num_values, 1);
1035 assert_string_equal((const char *)el->values[0].data, "test_mod_cn");
1037 el = ldb_msg_find_element(res->msgs[0], mod_kvs[0].key);
1038 assert_non_null(el);
1039 assert_int_equal(el->num_values, 1);
1040 assert_string_equal((const char *)el->values[0].data, mod_kvs[0].val);
1043 static void test_ldb_modify_replace_zero_vals(void **state)
1045 struct ldb_mod_test_ctx *mod_test_ctx = \
1046 talloc_get_type_abort(*state,
1047 struct ldb_mod_test_ctx);
1048 struct ldb_message_element *el;
1049 struct ldb_result *res;
1050 struct keyval kvs[] = {
1051 { "cn", NULL },
1052 { NULL, NULL },
1055 /* cn must be gone */
1056 res = run_mod_test(mod_test_ctx, LDB_FLAG_MOD_REPLACE, kvs);
1057 assert_non_null(res);
1058 el = ldb_msg_find_element(res->msgs[0], "cn");
1059 assert_null(el);
1062 static void test_ldb_modify_replace_noexist_key_zero_vals(void **state)
1064 struct ldb_mod_test_ctx *mod_test_ctx = \
1065 talloc_get_type_abort(*state,
1066 struct ldb_mod_test_ctx);
1067 struct ldb_message_element *el;
1068 struct ldb_result *res;
1069 struct keyval kvs[] = {
1070 { "noexist_key", NULL },
1071 { NULL, NULL },
1074 /* cn must be gone */
1075 res = run_mod_test(mod_test_ctx, LDB_FLAG_MOD_REPLACE, kvs);
1076 assert_non_null(res);
1078 /* cn should be intact */
1079 el = ldb_msg_find_element(res->msgs[0], "cn");
1080 assert_non_null(el);
1083 static void test_ldb_modify_del_key(void **state)
1085 struct ldb_mod_test_ctx *mod_test_ctx = \
1086 talloc_get_type_abort(*state,
1087 struct ldb_mod_test_ctx);
1088 struct ldb_message_element *el;
1089 struct ldb_result *res;
1090 struct keyval kvs[] = {
1091 { "cn", NULL },
1092 { NULL, NULL },
1095 /* cn must be gone */
1096 res = run_mod_test(mod_test_ctx, LDB_FLAG_MOD_DELETE, kvs);
1097 assert_non_null(res);
1099 el = ldb_msg_find_element(res->msgs[0], "cn");
1100 assert_null(el);
1103 static void test_ldb_modify_del_keyval(void **state)
1105 struct ldb_mod_test_ctx *mod_test_ctx = \
1106 talloc_get_type_abort(*state,
1107 struct ldb_mod_test_ctx);
1108 struct ldb_message_element *el;
1109 struct ldb_result *res;
1110 struct keyval kvs[] = {
1111 { "cn", "test_mod_cn" },
1112 { NULL, NULL },
1115 /* cn must be gone */
1116 res = run_mod_test(mod_test_ctx, LDB_FLAG_MOD_DELETE, kvs);
1117 assert_non_null(res);
1119 el = ldb_msg_find_element(res->msgs[0], "cn");
1120 assert_null(el);
1123 struct search_test_ctx {
1124 struct ldbtest_ctx *ldb_test_ctx;
1125 const char *base_dn;
1128 static char *get_full_dn(TALLOC_CTX *mem_ctx,
1129 struct search_test_ctx *search_test_ctx,
1130 const char *rdn)
1132 char *full_dn;
1134 full_dn = talloc_asprintf(mem_ctx,
1135 "%s,%s", rdn, search_test_ctx->base_dn);
1136 assert_non_null(full_dn);
1138 return full_dn;
1141 static void search_test_add_data(struct search_test_ctx *search_test_ctx,
1142 const char *rdn,
1143 struct keyval *kvs)
1145 char *full_dn;
1147 full_dn = get_full_dn(search_test_ctx, search_test_ctx, rdn);
1149 ldb_test_add_data(search_test_ctx,
1150 search_test_ctx->ldb_test_ctx,
1151 full_dn,
1152 kvs);
1155 static void search_test_remove_data(struct search_test_ctx *search_test_ctx,
1156 const char *rdn)
1158 char *full_dn;
1160 full_dn = talloc_asprintf(search_test_ctx,
1161 "%s,%s", rdn, search_test_ctx->base_dn);
1162 assert_non_null(full_dn);
1164 ldb_test_remove_data(search_test_ctx,
1165 search_test_ctx->ldb_test_ctx,
1166 full_dn);
1169 static int ldb_search_test_setup(void **state)
1171 struct ldbtest_ctx *ldb_test_ctx;
1172 struct search_test_ctx *search_test_ctx;
1173 struct keyval kvs[] = {
1174 { "cn", "test_search_cn" },
1175 { "cn", "test_search_cn2" },
1176 { "uid", "test_search_uid" },
1177 { "uid", "test_search_uid2" },
1178 { "objectUUID", "0123456789abcde0"},
1179 { NULL, NULL },
1181 struct keyval kvs2[] = {
1182 { "cn", "test_search_2_cn" },
1183 { "cn", "test_search_2_cn2" },
1184 { "uid", "test_search_2_uid" },
1185 { "uid", "test_search_2_uid2" },
1186 { "objectUUID", "0123456789abcde1"},
1187 { NULL, NULL },
1190 ldbtest_setup((void **) &ldb_test_ctx);
1192 search_test_ctx = talloc(ldb_test_ctx, struct search_test_ctx);
1193 assert_non_null(search_test_ctx);
1195 search_test_ctx->base_dn = "dc=search_test_entry";
1196 search_test_ctx->ldb_test_ctx = ldb_test_ctx;
1198 search_test_remove_data(search_test_ctx, "cn=test_search_cn");
1199 search_test_add_data(search_test_ctx, "cn=test_search_cn", kvs);
1201 search_test_remove_data(search_test_ctx, "cn=test_search_2_cn");
1202 search_test_add_data(search_test_ctx, "cn=test_search_2_cn", kvs2);
1204 *state = search_test_ctx;
1205 return 0;
1208 static int ldb_search_test_teardown(void **state)
1210 struct search_test_ctx *search_test_ctx = talloc_get_type_abort(*state,
1211 struct search_test_ctx);
1212 struct ldbtest_ctx *ldb_test_ctx;
1214 ldb_test_ctx = search_test_ctx->ldb_test_ctx;
1216 search_test_remove_data(search_test_ctx, "cn=test_search_cn");
1217 search_test_remove_data(search_test_ctx, "cn=test_search_2_cn");
1218 ldbtest_teardown((void **) &ldb_test_ctx);
1219 return 0;
1222 static void assert_attr_has_vals(struct ldb_message *msg,
1223 const char *attr,
1224 const char *vals[],
1225 const size_t nvals)
1227 struct ldb_message_element *el;
1228 size_t i;
1230 el = ldb_msg_find_element(msg, attr);
1231 assert_non_null(el);
1233 assert_int_equal(el->num_values, nvals);
1234 for (i = 0; i < nvals; i++) {
1235 assert_string_equal((const char *)el->values[i].data,
1236 vals[i]);
1240 static void assert_has_no_attr(struct ldb_message *msg,
1241 const char *attr)
1243 struct ldb_message_element *el;
1245 el = ldb_msg_find_element(msg, attr);
1246 assert_null(el);
1249 static bool has_dn(struct ldb_message *msg, const char *dn)
1251 const char *msgdn;
1253 msgdn = ldb_dn_get_linearized(msg->dn);
1254 if (strcmp(dn, msgdn) == 0) {
1255 return true;
1258 return false;
1261 static void test_search_match_none(void **state)
1263 struct search_test_ctx *search_test_ctx = talloc_get_type_abort(*state,
1264 struct search_test_ctx);
1265 size_t count;
1267 count = base_search_count(search_test_ctx->ldb_test_ctx,
1268 "dc=no_such_entry");
1269 assert_int_equal(count, 0);
1272 static void test_search_match_one(void **state)
1274 struct search_test_ctx *search_test_ctx = talloc_get_type_abort(*state,
1275 struct search_test_ctx);
1276 int ret;
1277 struct ldb_dn *basedn;
1278 struct ldb_result *result = NULL;
1279 const char *cn_vals[] = { "test_search_cn",
1280 "test_search_cn2" };
1281 const char *uid_vals[] = { "test_search_uid",
1282 "test_search_uid2" };
1284 basedn = ldb_dn_new_fmt(search_test_ctx,
1285 search_test_ctx->ldb_test_ctx->ldb,
1286 "%s",
1287 search_test_ctx->base_dn);
1288 assert_non_null(basedn);
1290 ret = ldb_search(search_test_ctx->ldb_test_ctx->ldb,
1291 search_test_ctx,
1292 &result,
1293 basedn,
1294 LDB_SCOPE_SUBTREE, NULL,
1295 "cn=test_search_cn");
1296 assert_int_equal(ret, 0);
1297 assert_non_null(result);
1298 assert_int_equal(result->count, 1);
1300 assert_attr_has_vals(result->msgs[0], "cn", cn_vals, 2);
1301 assert_attr_has_vals(result->msgs[0], "uid", uid_vals, 2);
1304 static void test_search_match_filter(void **state)
1306 struct search_test_ctx *search_test_ctx = talloc_get_type_abort(*state,
1307 struct search_test_ctx);
1308 int ret;
1309 struct ldb_dn *basedn;
1310 struct ldb_result *result = NULL;
1311 const char *cn_vals[] = { "test_search_cn",
1312 "test_search_cn2" };
1313 const char *attrs[] = { "cn", NULL };
1315 basedn = ldb_dn_new_fmt(search_test_ctx,
1316 search_test_ctx->ldb_test_ctx->ldb,
1317 "%s",
1318 search_test_ctx->base_dn);
1319 assert_non_null(basedn);
1321 ret = ldb_search(search_test_ctx->ldb_test_ctx->ldb,
1322 search_test_ctx,
1323 &result,
1324 basedn,
1325 LDB_SCOPE_SUBTREE,
1326 attrs,
1327 "cn=test_search_cn");
1328 assert_int_equal(ret, 0);
1329 assert_non_null(result);
1330 assert_int_equal(result->count, 1);
1332 assert_attr_has_vals(result->msgs[0], "cn", cn_vals, 2);
1333 assert_has_no_attr(result->msgs[0], "uid");
1336 static void assert_expected(struct search_test_ctx *search_test_ctx,
1337 struct ldb_message *msg)
1339 char *full_dn1;
1340 char *full_dn2;
1341 const char *cn_vals[] = { "test_search_cn",
1342 "test_search_cn2" };
1343 const char *uid_vals[] = { "test_search_uid",
1344 "test_search_uid2" };
1345 const char *cn2_vals[] = { "test_search_2_cn",
1346 "test_search_2_cn2" };
1347 const char *uid2_vals[] = { "test_search_2_uid",
1348 "test_search_2_uid2" };
1350 full_dn1 = get_full_dn(search_test_ctx,
1351 search_test_ctx,
1352 "cn=test_search_cn");
1354 full_dn2 = get_full_dn(search_test_ctx,
1355 search_test_ctx,
1356 "cn=test_search_2_cn");
1358 if (has_dn(msg, full_dn1) == true) {
1359 assert_attr_has_vals(msg, "cn", cn_vals, 2);
1360 assert_attr_has_vals(msg, "uid", uid_vals, 2);
1361 } else if (has_dn(msg, full_dn2) == true) {
1362 assert_attr_has_vals(msg, "cn", cn2_vals, 2);
1363 assert_attr_has_vals(msg, "uid", uid2_vals, 2);
1364 } else {
1365 fail();
1369 static void test_search_match_both(void **state)
1371 struct search_test_ctx *search_test_ctx = talloc_get_type_abort(*state,
1372 struct search_test_ctx);
1373 int ret;
1374 struct ldb_dn *basedn;
1375 struct ldb_result *result = NULL;
1377 basedn = ldb_dn_new_fmt(search_test_ctx,
1378 search_test_ctx->ldb_test_ctx->ldb,
1379 "%s",
1380 search_test_ctx->base_dn);
1381 assert_non_null(basedn);
1383 ret = ldb_search(search_test_ctx->ldb_test_ctx->ldb,
1384 search_test_ctx,
1385 &result,
1386 basedn,
1387 LDB_SCOPE_SUBTREE, NULL,
1388 "cn=test_search_*");
1389 assert_int_equal(ret, 0);
1390 assert_non_null(result);
1391 assert_int_equal(result->count, 2);
1393 assert_expected(search_test_ctx, result->msgs[0]);
1394 assert_expected(search_test_ctx, result->msgs[1]);
1397 static void test_search_match_basedn(void **state)
1399 struct search_test_ctx *search_test_ctx = talloc_get_type_abort(*state,
1400 struct search_test_ctx);
1401 int ret;
1402 struct ldb_dn *basedn;
1403 struct ldb_result *result = NULL;
1404 struct ldb_message *msg;
1406 basedn = ldb_dn_new_fmt(search_test_ctx,
1407 search_test_ctx->ldb_test_ctx->ldb,
1408 "dc=nosuchdn");
1409 assert_non_null(basedn);
1411 ret = ldb_search(search_test_ctx->ldb_test_ctx->ldb,
1412 search_test_ctx,
1413 &result,
1414 basedn,
1415 LDB_SCOPE_SUBTREE, NULL,
1416 "cn=*");
1417 assert_int_equal(ret, 0);
1419 /* Add 'checkBaseOnSearch' to @OPTIONS */
1420 msg = ldb_msg_new(search_test_ctx);
1421 assert_non_null(msg);
1423 msg->dn = ldb_dn_new_fmt(msg,
1424 search_test_ctx->ldb_test_ctx->ldb,
1425 "@OPTIONS");
1426 assert_non_null(msg->dn);
1428 ret = ldb_msg_add_string(msg, "checkBaseOnSearch", "TRUE");
1429 assert_int_equal(ret, 0);
1431 ret = ldb_add(search_test_ctx->ldb_test_ctx->ldb, msg);
1432 assert_int_equal(ret, 0);
1434 /* Search again */
1435 /* The search should return LDB_ERR_NO_SUCH_OBJECT */
1436 ret = ldb_search(search_test_ctx->ldb_test_ctx->ldb,
1437 search_test_ctx,
1438 &result,
1439 basedn,
1440 LDB_SCOPE_SUBTREE, NULL,
1441 "cn=*");
1442 assert_int_equal(ret, LDB_ERR_NO_SUCH_OBJECT);
1444 ret = ldb_delete(search_test_ctx->ldb_test_ctx->ldb, msg->dn);
1445 assert_int_equal(ret, 0);
1450 * This test is complex.
1451 * The purpose is to test for a deadlock detected between ldb_search()
1452 * and ldb_transaction_commit(). The deadlock happens if in process
1453 * (1) and (2):
1454 * - (1) the all-record lock is taken in ltdb_search()
1455 * - (2) the ldb_transaction_start() call is made
1456 * - (1) an un-indexed search starts (forced here by doing it in
1457 * the callback
1458 * - (2) the ldb_transaction_commit() is called.
1459 * This returns LDB_ERR_BUSY if the deadlock is detected
1461 * With ldb 1.1.31 and tdb 1.3.12 we avoid this only due to a missing
1462 * lock call in ltdb_search() due to a refcounting bug in
1463 * ltdb_lock_read()
1466 struct search_against_transaction_ctx {
1467 struct ldbtest_ctx *test_ctx;
1468 int res_count;
1469 pid_t child_pid;
1470 struct ldb_dn *basedn;
1473 static int test_ldb_search_against_transaction_callback2(struct ldb_request *req,
1474 struct ldb_reply *ares)
1476 struct search_against_transaction_ctx *ctx = req->context;
1477 switch (ares->type) {
1478 case LDB_REPLY_ENTRY:
1479 ctx->res_count++;
1480 if (ctx->res_count != 1) {
1481 return LDB_SUCCESS;
1484 break;
1486 case LDB_REPLY_REFERRAL:
1487 break;
1489 case LDB_REPLY_DONE:
1490 return ldb_request_done(req, LDB_SUCCESS);
1493 return 0;
1498 * This purpose of this callback is to trigger a transaction in
1499 * the child process while the all-record lock is held, but before
1500 * we take any locks in the tdb_traverse_read() handler.
1502 * In tdb 1.3.12 tdb_traverse_read() take the read transaction lock
1503 * however in ldb 1.1.31 ltdb_search() forgets to take the all-record
1504 * lock (except the very first time) due to a ref-counting bug.
1508 static int test_ldb_search_against_transaction_callback1(struct ldb_request *req,
1509 struct ldb_reply *ares)
1511 int ret, ret2;
1512 int pipes[2];
1513 char buf[2];
1514 struct search_against_transaction_ctx *ctx = req->context;
1515 switch (ares->type) {
1516 case LDB_REPLY_ENTRY:
1517 break;
1519 case LDB_REPLY_REFERRAL:
1520 return LDB_SUCCESS;
1522 case LDB_REPLY_DONE:
1523 return ldb_request_done(req, LDB_SUCCESS);
1526 ret = pipe(pipes);
1527 assert_int_equal(ret, 0);
1529 ctx->child_pid = fork();
1530 if (ctx->child_pid == 0) {
1531 TALLOC_CTX *tmp_ctx = NULL;
1532 struct ldb_message *msg;
1533 TALLOC_FREE(ctx->test_ctx->ldb);
1534 TALLOC_FREE(ctx->test_ctx->ev);
1535 close(pipes[0]);
1536 ctx->test_ctx->ev = tevent_context_init(ctx->test_ctx);
1537 if (ctx->test_ctx->ev == NULL) {
1538 exit(LDB_ERR_OPERATIONS_ERROR);
1541 ctx->test_ctx->ldb = ldb_init(ctx->test_ctx,
1542 ctx->test_ctx->ev);
1543 if (ctx->test_ctx->ldb == NULL) {
1544 exit(LDB_ERR_OPERATIONS_ERROR);
1547 ret = ldb_connect(ctx->test_ctx->ldb,
1548 ctx->test_ctx->dbpath, 0, NULL);
1549 if (ret != LDB_SUCCESS) {
1550 exit(ret);
1553 tmp_ctx = talloc_new(ctx->test_ctx);
1554 if (tmp_ctx == NULL) {
1555 exit(LDB_ERR_OPERATIONS_ERROR);
1558 msg = ldb_msg_new(tmp_ctx);
1559 if (msg == NULL) {
1560 exit(LDB_ERR_OPERATIONS_ERROR);
1563 msg->dn = ldb_dn_new_fmt(msg, ctx->test_ctx->ldb,
1564 "dc=test");
1565 if (msg->dn == NULL) {
1566 exit(LDB_ERR_OPERATIONS_ERROR);
1569 ret = ldb_msg_add_string(msg, "cn", "test_cn_val");
1570 if (ret != 0) {
1571 exit(LDB_ERR_OPERATIONS_ERROR);
1574 ret = ldb_transaction_start(ctx->test_ctx->ldb);
1575 if (ret != 0) {
1576 exit(ret);
1579 ret = write(pipes[1], "GO", 2);
1580 if (ret != 2) {
1581 exit(LDB_ERR_OPERATIONS_ERROR);
1584 ret = ldb_msg_add_string(msg, "objectUUID",
1585 "0123456789abcdef");
1586 if (ret != 0) {
1587 exit(ret);
1590 ret = ldb_add(ctx->test_ctx->ldb, msg);
1591 if (ret != 0) {
1592 exit(ret);
1595 ret = ldb_transaction_commit(ctx->test_ctx->ldb);
1596 exit(ret);
1598 close(pipes[1]);
1599 ret = read(pipes[0], buf, 2);
1600 assert_int_equal(ret, 2);
1602 /* This search must be unindexed (ie traverse in tdb) */
1603 ret = ldb_build_search_req(&req,
1604 ctx->test_ctx->ldb,
1605 ctx->test_ctx,
1606 ctx->basedn,
1607 LDB_SCOPE_SUBTREE,
1608 "cn=*", NULL,
1609 NULL,
1610 ctx,
1611 test_ldb_search_against_transaction_callback2,
1612 NULL);
1614 * we don't assert on these return codes until after the search is
1615 * finished, or the clean up will fail because we hold locks.
1618 ret2 = ldb_request(ctx->test_ctx->ldb, req);
1620 if (ret2 == LDB_SUCCESS) {
1621 ret2 = ldb_wait(req->handle, LDB_WAIT_ALL);
1623 assert_int_equal(ret, 0);
1624 assert_int_equal(ret2, 0);
1625 assert_int_equal(ctx->res_count, 2);
1627 return LDB_SUCCESS;
1630 static void test_ldb_search_against_transaction(void **state)
1632 struct search_test_ctx *search_test_ctx = talloc_get_type_abort(*state,
1633 struct search_test_ctx);
1634 struct search_against_transaction_ctx
1635 ctx =
1636 { .res_count = 0,
1637 .test_ctx = search_test_ctx->ldb_test_ctx
1640 int ret;
1641 struct ldb_request *req;
1642 pid_t pid;
1643 int wstatus;
1644 struct ldb_dn *base_search_dn;
1646 tevent_loop_allow_nesting(search_test_ctx->ldb_test_ctx->ev);
1648 base_search_dn
1649 = ldb_dn_new_fmt(search_test_ctx,
1650 search_test_ctx->ldb_test_ctx->ldb,
1651 "cn=test_search_cn,%s",
1652 search_test_ctx->base_dn);
1653 assert_non_null(base_search_dn);
1655 ctx.basedn
1656 = ldb_dn_new_fmt(search_test_ctx,
1657 search_test_ctx->ldb_test_ctx->ldb,
1658 "%s",
1659 search_test_ctx->base_dn);
1660 assert_non_null(ctx.basedn);
1663 /* This search must be indexed (ie no traverse in tdb) */
1664 ret = ldb_build_search_req(&req,
1665 search_test_ctx->ldb_test_ctx->ldb,
1666 search_test_ctx,
1667 base_search_dn,
1668 LDB_SCOPE_BASE,
1669 "cn=*", NULL,
1670 NULL,
1671 &ctx,
1672 test_ldb_search_against_transaction_callback1,
1673 NULL);
1674 assert_int_equal(ret, 0);
1675 ret = ldb_request(search_test_ctx->ldb_test_ctx->ldb, req);
1677 if (ret == LDB_SUCCESS) {
1678 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
1680 assert_int_equal(ret, 0);
1681 assert_int_equal(ctx.res_count, 2);
1683 pid = waitpid(ctx.child_pid, &wstatus, 0);
1684 assert_int_equal(pid, ctx.child_pid);
1686 assert_true(WIFEXITED(wstatus));
1688 assert_int_equal(WEXITSTATUS(wstatus), 0);
1694 * This test is also complex.
1695 * The purpose is to test if a modify can occur during an ldb_search()
1696 * This would be a failure if if in process
1697 * (1) and (2):
1698 * - (1) ltdb_search() starts and calls back for one entry
1699 * - (2) one of the entries to be matched is modified
1700 * - (1) the indexed search tries to return the modified entry, but
1701 * it is no longer found, either:
1702 * - despite it still matching (dn changed)
1703 * - it no longer matching (attrs changed)
1705 * We also try un-indexed to show that the behaviour differs on this
1706 * point, which it should not (an index should only impact search
1707 * speed).
1710 struct modify_during_search_test_ctx {
1711 struct ldbtest_ctx *test_ctx;
1712 int res_count;
1713 pid_t child_pid;
1714 struct ldb_dn *basedn;
1715 bool got_cn;
1716 bool got_2_cn;
1717 bool rename;
1721 * This purpose of this callback is to trigger a write in
1722 * the child process while a search is in progress.
1724 * In tdb 1.3.12 tdb_traverse_read() take the read transaction lock
1725 * however in ldb 1.1.31 ltdb_search() forgets to take the all-record
1726 * lock (except the very first time) due to a ref-counting bug.
1728 * We assume that if the write will proceed, it will proceed in a 3
1729 * second window after the function is called.
1732 static int test_ldb_modify_during_search_callback1(struct ldb_request *req,
1733 struct ldb_reply *ares)
1735 int ret;
1736 int pipes[2];
1737 char buf[2];
1738 struct modify_during_search_test_ctx *ctx = req->context;
1739 switch (ares->type) {
1740 case LDB_REPLY_ENTRY:
1742 const struct ldb_val *cn_val
1743 = ldb_dn_get_component_val(ares->message->dn, 0);
1744 const char *cn = (char *)cn_val->data;
1745 ctx->res_count++;
1746 if (strcmp(cn, "test_search_cn") == 0) {
1747 ctx->got_cn = true;
1748 } else if (strcmp(cn, "test_search_2_cn") == 0) {
1749 ctx->got_2_cn = true;
1751 if (ctx->res_count == 2) {
1752 return LDB_SUCCESS;
1754 break;
1756 case LDB_REPLY_REFERRAL:
1757 return LDB_SUCCESS;
1759 case LDB_REPLY_DONE:
1760 return ldb_request_done(req, LDB_SUCCESS);
1763 ret = pipe(pipes);
1764 assert_int_equal(ret, 0);
1766 ctx->child_pid = fork();
1767 if (ctx->child_pid == 0 && ctx->rename) {
1768 TALLOC_CTX *tmp_ctx = NULL;
1769 struct ldb_dn *dn, *new_dn;
1770 TALLOC_FREE(ctx->test_ctx->ldb);
1771 TALLOC_FREE(ctx->test_ctx->ev);
1772 close(pipes[0]);
1773 ctx->test_ctx->ev = tevent_context_init(ctx->test_ctx);
1774 if (ctx->test_ctx->ev == NULL) {
1775 exit(LDB_ERR_OPERATIONS_ERROR);
1778 ctx->test_ctx->ldb = ldb_init(ctx->test_ctx,
1779 ctx->test_ctx->ev);
1780 if (ctx->test_ctx->ldb == NULL) {
1781 exit(LDB_ERR_OPERATIONS_ERROR);
1784 ret = ldb_connect(ctx->test_ctx->ldb,
1785 ctx->test_ctx->dbpath, 0, NULL);
1786 if (ret != LDB_SUCCESS) {
1787 exit(ret);
1790 tmp_ctx = talloc_new(ctx->test_ctx);
1791 if (tmp_ctx == NULL) {
1792 exit(LDB_ERR_OPERATIONS_ERROR);
1795 if (ctx->got_cn) {
1796 /* Modify the other one */
1797 dn = ldb_dn_new_fmt(tmp_ctx, ctx->test_ctx->ldb,
1798 "cn=test_search_2_cn,"
1799 "dc=search_test_entry");
1800 } else {
1801 dn = ldb_dn_new_fmt(tmp_ctx, ctx->test_ctx->ldb,
1802 "cn=test_search_cn,"
1803 "dc=search_test_entry");
1805 if (dn == NULL) {
1806 exit(LDB_ERR_OPERATIONS_ERROR);
1809 new_dn = ldb_dn_new_fmt(tmp_ctx, ctx->test_ctx->ldb,
1810 "cn=test_search_cn_renamed,"
1811 "dc=search_test_entry");
1812 if (new_dn == NULL) {
1813 exit(LDB_ERR_OPERATIONS_ERROR);
1816 ret = ldb_transaction_start(ctx->test_ctx->ldb);
1817 if (ret != 0) {
1818 exit(ret);
1821 if (write(pipes[1], "GO", 2) != 2) {
1822 exit(LDB_ERR_OPERATIONS_ERROR);
1825 ret = ldb_rename(ctx->test_ctx->ldb, dn, new_dn);
1826 if (ret != 0) {
1827 exit(ret);
1830 ret = ldb_transaction_commit(ctx->test_ctx->ldb);
1831 exit(ret);
1833 } else if (ctx->child_pid == 0) {
1834 TALLOC_CTX *tmp_ctx = NULL;
1835 struct ldb_message *msg;
1836 struct ldb_message_element *el;
1837 TALLOC_FREE(ctx->test_ctx->ldb);
1838 TALLOC_FREE(ctx->test_ctx->ev);
1839 close(pipes[0]);
1840 ctx->test_ctx->ev = tevent_context_init(ctx->test_ctx);
1841 if (ctx->test_ctx->ev == NULL) {
1842 exit(LDB_ERR_OPERATIONS_ERROR);
1845 ctx->test_ctx->ldb = ldb_init(ctx->test_ctx,
1846 ctx->test_ctx->ev);
1847 if (ctx->test_ctx->ldb == NULL) {
1848 exit(LDB_ERR_OPERATIONS_ERROR);
1851 ret = ldb_connect(ctx->test_ctx->ldb,
1852 ctx->test_ctx->dbpath, 0, NULL);
1853 if (ret != LDB_SUCCESS) {
1854 exit(ret);
1857 tmp_ctx = talloc_new(ctx->test_ctx);
1858 if (tmp_ctx == NULL) {
1859 exit(LDB_ERR_OPERATIONS_ERROR);
1862 msg = ldb_msg_new(tmp_ctx);
1863 if (msg == NULL) {
1864 exit(LDB_ERR_OPERATIONS_ERROR);
1867 if (ctx->got_cn) {
1868 /* Modify the other one */
1869 msg->dn = ldb_dn_new_fmt(msg, ctx->test_ctx->ldb,
1870 "cn=test_search_2_cn,"
1871 "dc=search_test_entry");
1872 } else {
1873 msg->dn = ldb_dn_new_fmt(msg, ctx->test_ctx->ldb,
1874 "cn=test_search_cn,"
1875 "dc=search_test_entry");
1877 if (msg->dn == NULL) {
1878 exit(LDB_ERR_OPERATIONS_ERROR);
1881 ret = ldb_msg_add_string(msg, "filterAttr", "TRUE");
1882 if (ret != 0) {
1883 exit(LDB_ERR_OPERATIONS_ERROR);
1885 el = ldb_msg_find_element(msg, "filterAttr");
1886 if (el == NULL) {
1887 exit(LDB_ERR_OPERATIONS_ERROR);
1889 el->flags = LDB_FLAG_MOD_REPLACE;
1891 ret = ldb_transaction_start(ctx->test_ctx->ldb);
1892 if (ret != 0) {
1893 exit(ret);
1896 if (write(pipes[1], "GO", 2) != 2) {
1897 exit(LDB_ERR_OPERATIONS_ERROR);
1900 ret = ldb_modify(ctx->test_ctx->ldb, msg);
1901 if (ret != 0) {
1902 exit(ret);
1905 ret = ldb_transaction_commit(ctx->test_ctx->ldb);
1906 exit(ret);
1910 * With TDB 1.3.13 and before "tdb: Remove locking from tdb_traverse_read()"
1911 * we will hang here because the child process can not proceed to
1912 * sending the "GO" as it is blocked at ldb_transaction_start().
1915 close(pipes[1]);
1916 ret = read(pipes[0], buf, 2);
1917 assert_int_equal(ret, 2);
1919 sleep(3);
1921 return LDB_SUCCESS;
1924 static void test_ldb_modify_during_search(void **state, bool add_index,
1925 bool rename)
1927 struct search_test_ctx *search_test_ctx = talloc_get_type_abort(*state,
1928 struct search_test_ctx);
1929 struct modify_during_search_test_ctx
1930 ctx =
1931 { .res_count = 0,
1932 .test_ctx = search_test_ctx->ldb_test_ctx,
1933 .rename = rename
1936 int ret;
1937 struct ldb_request *req;
1938 pid_t pid;
1939 int wstatus;
1941 if (add_index) {
1942 struct ldb_message *msg;
1943 struct ldb_dn *indexlist = ldb_dn_new(search_test_ctx,
1944 search_test_ctx->ldb_test_ctx->ldb,
1945 "@INDEXLIST");
1946 assert_non_null(indexlist);
1948 msg = ldb_msg_new(search_test_ctx);
1949 assert_non_null(msg);
1951 msg->dn = indexlist;
1953 ret = ldb_msg_add_string(msg, "@IDXATTR", "cn");
1954 assert_int_equal(ret, LDB_SUCCESS);
1955 ret = ldb_add(search_test_ctx->ldb_test_ctx->ldb,
1956 msg);
1957 if (ret == LDB_ERR_ENTRY_ALREADY_EXISTS) {
1958 msg->elements[0].flags = LDB_FLAG_MOD_ADD;
1959 ret = ldb_modify(search_test_ctx->ldb_test_ctx->ldb,
1960 msg);
1962 assert_int_equal(ret, LDB_SUCCESS);
1965 tevent_loop_allow_nesting(search_test_ctx->ldb_test_ctx->ev);
1967 ctx.basedn
1968 = ldb_dn_new_fmt(search_test_ctx,
1969 search_test_ctx->ldb_test_ctx->ldb,
1970 "%s",
1971 search_test_ctx->base_dn);
1972 assert_non_null(ctx.basedn);
1976 * This search must be over multiple items, and should include
1977 * the new name after a rename, to show that it would match
1978 * both before and after that modify
1980 ret = ldb_build_search_req(&req,
1981 search_test_ctx->ldb_test_ctx->ldb,
1982 search_test_ctx,
1983 ctx.basedn,
1984 LDB_SCOPE_SUBTREE,
1985 "(&(!(filterAttr=*))"
1986 "(|(cn=test_search_cn_renamed)"
1987 "(cn=test_search_cn)"
1988 "(cn=test_search_2_cn)"
1989 "))",
1990 NULL,
1991 NULL,
1992 &ctx,
1993 test_ldb_modify_during_search_callback1,
1994 NULL);
1995 assert_int_equal(ret, 0);
1996 ret = ldb_request(search_test_ctx->ldb_test_ctx->ldb, req);
1998 if (ret == LDB_SUCCESS) {
1999 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
2001 assert_int_equal(ret, 0);
2002 assert_int_equal(ctx.res_count, 2);
2003 assert_int_equal(ctx.got_cn, true);
2004 assert_int_equal(ctx.got_2_cn, true);
2006 pid = waitpid(ctx.child_pid, &wstatus, 0);
2007 assert_int_equal(pid, ctx.child_pid);
2009 assert_true(WIFEXITED(wstatus));
2011 assert_int_equal(WEXITSTATUS(wstatus), 0);
2016 static void test_ldb_modify_during_indexed_search(void **state)
2018 test_ldb_modify_during_search(state, true, false);
2021 static void test_ldb_modify_during_unindexed_search(void **state)
2023 test_ldb_modify_during_search(state, false, false);
2026 static void test_ldb_rename_during_indexed_search(void **state)
2028 test_ldb_modify_during_search(state, true, true);
2031 static void test_ldb_rename_during_unindexed_search(void **state)
2033 test_ldb_modify_during_search(state, false, true);
2037 * This test is also complex.
2039 * The purpose is to test if a modify can occur during an ldb_search()
2040 * before the end of the callback
2042 * This would be a failure if if in process
2043 * (1) and (2):
2044 * - (1) ldb_search() starts and calls back for a number of entries
2045 * - (2) an entry in the DB is allowed to change before the callback returns
2046 * - (1) the callback can see the modification
2051 * This purpose of this callback is to trigger a write in
2052 * the child process while a search DONE callback is in progress.
2054 * In ldb 1.1.31 ldb_search() omitted to take a all-record
2055 * lock for the full duration of the search and callbacks
2057 * We assume that if the write will proceed, it will proceed in a 3
2058 * second window after the function is called.
2061 static int test_ldb_modify_during_whole_search_callback1(struct ldb_request *req,
2062 struct ldb_reply *ares)
2064 int ret;
2065 int pipes[2];
2066 char buf[2];
2067 struct modify_during_search_test_ctx *ctx = req->context;
2068 struct ldb_dn *search_dn;
2069 struct ldb_result *res2;
2070 unsigned res_count;
2071 switch (ares->type) {
2072 case LDB_REPLY_ENTRY:
2073 case LDB_REPLY_REFERRAL:
2074 return LDB_SUCCESS;
2076 case LDB_REPLY_DONE:
2077 break;
2080 ret = pipe(pipes);
2081 assert_int_equal(ret, 0);
2083 ctx->child_pid = fork();
2084 if (ctx->child_pid == 0) {
2085 TALLOC_CTX *tmp_ctx = NULL;
2086 struct ldb_message *msg;
2087 struct ldb_message_element *el;
2088 TALLOC_FREE(ctx->test_ctx->ldb);
2089 TALLOC_FREE(ctx->test_ctx->ev);
2090 close(pipes[0]);
2091 ctx->test_ctx->ev = tevent_context_init(ctx->test_ctx);
2092 if (ctx->test_ctx->ev == NULL) {
2093 exit(LDB_ERR_OPERATIONS_ERROR);
2096 ctx->test_ctx->ldb = ldb_init(ctx->test_ctx,
2097 ctx->test_ctx->ev);
2098 if (ctx->test_ctx->ldb == NULL) {
2099 exit(LDB_ERR_OPERATIONS_ERROR);
2102 ret = ldb_connect(ctx->test_ctx->ldb,
2103 ctx->test_ctx->dbpath, 0, NULL);
2104 if (ret != LDB_SUCCESS) {
2105 exit(ret);
2108 tmp_ctx = talloc_new(ctx->test_ctx);
2109 if (tmp_ctx == NULL) {
2110 exit(LDB_ERR_OPERATIONS_ERROR);
2113 msg = ldb_msg_new(tmp_ctx);
2114 if (msg == NULL) {
2115 exit(LDB_ERR_OPERATIONS_ERROR);
2118 msg->dn = ldb_dn_new_fmt(msg, ctx->test_ctx->ldb,
2119 "cn=test_search_cn,"
2120 "dc=search_test_entry");
2121 if (msg->dn == NULL) {
2122 exit(LDB_ERR_OPERATIONS_ERROR);
2125 ret = ldb_msg_add_string(msg, "filterAttr", "TRUE");
2126 if (ret != 0) {
2127 exit(LDB_ERR_OPERATIONS_ERROR);
2129 el = ldb_msg_find_element(msg, "filterAttr");
2130 if (el == NULL) {
2131 exit(LDB_ERR_OPERATIONS_ERROR);
2133 el->flags = LDB_FLAG_MOD_REPLACE;
2135 ret = ldb_transaction_start(ctx->test_ctx->ldb);
2136 if (ret != 0) {
2137 exit(ret);
2140 if (write(pipes[1], "GO", 2) != 2) {
2141 exit(LDB_ERR_OPERATIONS_ERROR);
2144 ret = ldb_modify(ctx->test_ctx->ldb, msg);
2145 if (ret != 0) {
2146 exit(ret);
2149 ret = ldb_transaction_commit(ctx->test_ctx->ldb);
2150 exit(ret);
2153 close(pipes[1]);
2154 ret = read(pipes[0], buf, 2);
2155 assert_int_equal(ret, 2);
2157 sleep(3);
2160 * If writes are not blocked until after this function, we
2161 * will be able to successfully search for this modification
2162 * here
2165 search_dn = ldb_dn_new_fmt(ares, ctx->test_ctx->ldb,
2166 "cn=test_search_cn,"
2167 "dc=search_test_entry");
2169 ret = ldb_search(ctx->test_ctx->ldb, ares,
2170 &res2, search_dn, LDB_SCOPE_BASE, NULL,
2171 "filterAttr=TRUE");
2174 * We do this in an unusual order, because if we fail an assert before
2175 * ldb_request_done(), we will also fail to clean up as we hold locks.
2178 res_count = res2->count;
2179 ldb_request_done(req, LDB_SUCCESS);
2180 assert_int_equal(ret, 0);
2182 /* We should not have got the result */
2183 assert_int_equal(res_count, 0);
2185 return ret;
2188 static void test_ldb_modify_during_whole_search(void **state)
2190 struct search_test_ctx *search_test_ctx = talloc_get_type_abort(*state,
2191 struct search_test_ctx);
2192 struct modify_during_search_test_ctx
2193 ctx =
2195 .test_ctx = search_test_ctx->ldb_test_ctx,
2198 int ret;
2199 struct ldb_request *req;
2200 pid_t pid;
2201 int wstatus;
2202 struct ldb_dn *search_dn;
2203 struct ldb_result *res2;
2205 tevent_loop_allow_nesting(search_test_ctx->ldb_test_ctx->ev);
2207 ctx.basedn
2208 = ldb_dn_new_fmt(search_test_ctx,
2209 search_test_ctx->ldb_test_ctx->ldb,
2210 "%s",
2211 search_test_ctx->base_dn);
2212 assert_non_null(ctx.basedn);
2216 * The search just needs to call DONE, we don't care about the
2217 * contents of the search for this test
2219 ret = ldb_build_search_req(&req,
2220 search_test_ctx->ldb_test_ctx->ldb,
2221 search_test_ctx,
2222 ctx.basedn,
2223 LDB_SCOPE_SUBTREE,
2224 "(&(!(filterAttr=*))"
2225 "(cn=test_search_cn))",
2226 NULL,
2227 NULL,
2228 &ctx,
2229 test_ldb_modify_during_whole_search_callback1,
2230 NULL);
2231 assert_int_equal(ret, 0);
2232 ret = ldb_request(search_test_ctx->ldb_test_ctx->ldb, req);
2234 if (ret == LDB_SUCCESS) {
2235 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
2237 assert_int_equal(ret, 0);
2239 pid = waitpid(ctx.child_pid, &wstatus, 0);
2240 assert_int_equal(pid, ctx.child_pid);
2242 assert_true(WIFEXITED(wstatus));
2244 assert_int_equal(WEXITSTATUS(wstatus), 0);
2247 * If writes are blocked until after the search function, we
2248 * will be able to successfully search for this modification
2249 * now
2252 search_dn = ldb_dn_new_fmt(search_test_ctx,
2253 search_test_ctx->ldb_test_ctx->ldb,
2254 "cn=test_search_cn,"
2255 "dc=search_test_entry");
2257 ret = ldb_search(search_test_ctx->ldb_test_ctx->ldb,
2258 search_test_ctx,
2259 &res2, search_dn, LDB_SCOPE_BASE, NULL,
2260 "filterAttr=TRUE");
2261 assert_int_equal(ret, 0);
2263 /* We got the result */
2264 assert_int_equal(res2->count, 1);
2268 * This test is also complex.
2270 * The purpose is to test if a modify can occur during an ldb_search()
2271 * before the request is destroyed with TALLOC_FREE()
2273 * This would be a failure if in process
2274 * (1) and (2):
2275 * - (1) ldb_search() starts and waits
2276 * - (2) an entry in the DB is allowed to change before the ldb_wait() is called
2277 * - (1) the original process can see the modification before the TALLOC_FREE()
2278 * also we check that
2279 * - (1) the original process can see the modification after the TALLOC_FREE()
2284 * This purpose of this callback is to trigger a write in
2285 * the child process before the ldb_wait() is called
2287 * In ldb 1.1.31 ldb_search() omitted to take a all-record
2288 * lock for the full duration of the search and callbacks
2290 * We assume that if the write will proceed, it will proceed in a 3
2291 * second window after the function is called.
2294 static int test_ldb_modify_before_ldb_wait_callback1(struct ldb_request *req,
2295 struct ldb_reply *ares)
2297 switch (ares->type) {
2298 case LDB_REPLY_ENTRY:
2299 case LDB_REPLY_REFERRAL:
2300 return LDB_SUCCESS;
2302 case LDB_REPLY_DONE:
2303 break;
2306 return ldb_request_done(req, LDB_SUCCESS);
2309 static void test_ldb_modify_before_ldb_wait(void **state)
2311 struct search_test_ctx *search_test_ctx = talloc_get_type_abort(*state,
2312 struct search_test_ctx);
2313 int ret;
2314 struct ldb_request *req;
2315 pid_t pid;
2316 int wstatus;
2317 struct ldb_dn *search_dn;
2318 struct ldb_dn *basedn;
2319 struct ldb_result *res2;
2320 int pipes[2];
2321 char buf[2];
2322 pid_t child_pid;
2323 unsigned res_count;
2325 search_dn = ldb_dn_new_fmt(search_test_ctx,
2326 search_test_ctx->ldb_test_ctx->ldb,
2327 "cn=test_search_cn,"
2328 "dc=search_test_entry");
2329 assert_non_null(search_dn);
2331 basedn = ldb_dn_new_fmt(search_test_ctx,
2332 search_test_ctx->ldb_test_ctx->ldb,
2333 "%s",
2334 search_test_ctx->base_dn);
2335 assert_non_null(basedn);
2338 * The search just needs to call DONE, we don't care about the
2339 * contents of the search for this test
2341 ret = ldb_build_search_req(&req,
2342 search_test_ctx->ldb_test_ctx->ldb,
2343 search_test_ctx,
2344 basedn,
2345 LDB_SCOPE_SUBTREE,
2346 "(&(!(filterAttr=*))"
2347 "(cn=test_search_cn))",
2348 NULL,
2349 NULL,
2350 NULL,
2351 test_ldb_modify_before_ldb_wait_callback1,
2352 NULL);
2353 assert_int_equal(ret, 0);
2354 ret = ldb_request(search_test_ctx->ldb_test_ctx->ldb, req);
2356 ret = pipe(pipes);
2357 assert_int_equal(ret, 0);
2359 child_pid = fork();
2360 if (child_pid == 0) {
2361 TALLOC_CTX *tmp_ctx = NULL;
2362 struct ldb_message *msg;
2363 struct ldb_message_element *el;
2364 TALLOC_FREE(search_test_ctx->ldb_test_ctx->ldb);
2365 TALLOC_FREE(search_test_ctx->ldb_test_ctx->ev);
2366 close(pipes[0]);
2367 search_test_ctx->ldb_test_ctx->ev = tevent_context_init(search_test_ctx->ldb_test_ctx);
2368 if (search_test_ctx->ldb_test_ctx->ev == NULL) {
2369 exit(LDB_ERR_OPERATIONS_ERROR);
2372 search_test_ctx->ldb_test_ctx->ldb = ldb_init(search_test_ctx->ldb_test_ctx,
2373 search_test_ctx->ldb_test_ctx->ev);
2374 if (search_test_ctx->ldb_test_ctx->ldb == NULL) {
2375 exit(LDB_ERR_OPERATIONS_ERROR);
2378 ret = ldb_connect(search_test_ctx->ldb_test_ctx->ldb,
2379 search_test_ctx->ldb_test_ctx->dbpath, 0, NULL);
2380 if (ret != LDB_SUCCESS) {
2381 exit(ret);
2384 tmp_ctx = talloc_new(search_test_ctx->ldb_test_ctx);
2385 if (tmp_ctx == NULL) {
2386 exit(LDB_ERR_OPERATIONS_ERROR);
2389 msg = ldb_msg_new(tmp_ctx);
2390 if (msg == NULL) {
2391 exit(LDB_ERR_OPERATIONS_ERROR);
2395 * We must re-create this DN from a string to ensure
2396 * it does not reference the now-gone LDB context of
2397 * the parent
2399 msg->dn = ldb_dn_new_fmt(search_test_ctx,
2400 search_test_ctx->ldb_test_ctx->ldb,
2401 "cn=test_search_cn,"
2402 "dc=search_test_entry");
2404 if (msg->dn == NULL) {
2405 exit(LDB_ERR_OPERATIONS_ERROR);
2408 ret = ldb_msg_add_string(msg, "filterAttr", "TRUE");
2409 if (ret != 0) {
2410 exit(LDB_ERR_OPERATIONS_ERROR);
2412 el = ldb_msg_find_element(msg, "filterAttr");
2413 if (el == NULL) {
2414 exit(LDB_ERR_OPERATIONS_ERROR);
2416 el->flags = LDB_FLAG_MOD_REPLACE;
2418 ret = ldb_transaction_start(search_test_ctx->ldb_test_ctx->ldb);
2419 if (ret != 0) {
2420 exit(ret);
2423 if (write(pipes[1], "GO", 2) != 2) {
2424 exit(LDB_ERR_OPERATIONS_ERROR);
2427 ret = ldb_modify(search_test_ctx->ldb_test_ctx->ldb, msg);
2428 if (ret != 0) {
2429 exit(ret);
2432 ret = ldb_transaction_commit(search_test_ctx->ldb_test_ctx->ldb);
2433 exit(ret);
2435 close(pipes[1]);
2437 ret = read(pipes[0], buf, 2);
2438 assert_int_equal(ret, 2);
2440 sleep(3);
2443 * If writes are not blocked until after the (never called) ldb_wait(), we
2444 * will be able to successfully search for this modification
2445 * here
2448 ret = ldb_search(search_test_ctx->ldb_test_ctx->ldb, search_test_ctx,
2449 &res2, search_dn, LDB_SCOPE_BASE, NULL,
2450 "filterAttr=TRUE");
2453 * We avoid making assertions before TALLOC_FREE()ing the request,
2454 * lest the assert fail and mess with the clean-up because we still
2455 * have locks.
2457 res_count = res2->count;
2458 TALLOC_FREE(req);
2460 /* We should not have got the result */
2461 assert_int_equal(res_count, 0);
2462 assert_int_equal(ret, 0);
2464 pid = waitpid(child_pid, &wstatus, 0);
2465 assert_int_equal(pid, child_pid);
2467 assert_true(WIFEXITED(wstatus));
2469 assert_int_equal(WEXITSTATUS(wstatus), 0);
2472 * If writes are blocked until after the search request was freed, we
2473 * will be able to successfully search for this modification
2474 * now
2477 search_dn = ldb_dn_new_fmt(search_test_ctx,
2478 search_test_ctx->ldb_test_ctx->ldb,
2479 "cn=test_search_cn,"
2480 "dc=search_test_entry");
2482 ret = ldb_search(search_test_ctx->ldb_test_ctx->ldb,
2483 search_test_ctx,
2484 &res2, search_dn, LDB_SCOPE_BASE, NULL,
2485 "filterAttr=TRUE");
2486 assert_int_equal(ret, 0);
2488 /* We got the result */
2489 assert_int_equal(res2->count, 1);
2493 * This test is also complex.
2494 * The purpose is to test if a modify can occur during an ldb_search()
2495 * This would be a failure if if in process
2496 * (1) and (2):
2497 * - (1) ltdb_search() starts and calls back for one entry
2498 * - (2) one of the entries to be matched is modified
2499 * - (1) the indexed search tries to return the modified entry, but
2500 * it is no longer found, either:
2501 * - despite it still matching (dn changed)
2502 * - it no longer matching (attrs changed)
2504 * We also try un-indexed to show that the behaviour differs on this
2505 * point, which it should not (an index should only impact search
2506 * speed).
2510 * This purpose of this callback is to trigger a write in the callback
2511 * so as to change in in-memory index code while looping over the
2512 * index result.
2515 static int test_ldb_callback_modify_during_search_callback1(struct ldb_request *req,
2516 struct ldb_reply *ares)
2518 int ret;
2519 struct modify_during_search_test_ctx *ctx = req->context;
2520 struct ldb_dn *dn = NULL, *new_dn = NULL;
2521 TALLOC_CTX *tmp_ctx = talloc_new(ctx->test_ctx);
2522 struct ldb_message *msg = NULL;
2524 assert_non_null(tmp_ctx);
2526 switch (ares->type) {
2527 case LDB_REPLY_ENTRY:
2529 const struct ldb_val *cn_val
2530 = ldb_dn_get_component_val(ares->message->dn, 0);
2531 const char *cn = (char *)cn_val->data;
2532 ctx->res_count++;
2533 if (strcmp(cn, "test_search_cn") == 0) {
2534 ctx->got_cn = true;
2535 } else if (strcmp(cn, "test_search_2_cn") == 0) {
2536 ctx->got_2_cn = true;
2538 if (ctx->res_count == 2) {
2539 return LDB_SUCCESS;
2541 break;
2543 case LDB_REPLY_REFERRAL:
2544 return LDB_SUCCESS;
2546 case LDB_REPLY_DONE:
2547 return ldb_request_done(req, LDB_SUCCESS);
2550 if (ctx->rename) {
2551 if (ctx->got_2_cn) {
2552 /* Modify this one */
2553 dn = ldb_dn_new_fmt(tmp_ctx,
2554 ctx->test_ctx->ldb,
2555 "cn=test_search_2_cn,%s",
2556 ldb_dn_get_linearized(ctx->basedn));
2557 } else {
2558 dn = ldb_dn_new_fmt(tmp_ctx,
2559 ctx->test_ctx->ldb,
2560 "cn=test_search_cn,%s",
2561 ldb_dn_get_linearized(ctx->basedn));
2563 assert_non_null(dn);
2565 new_dn = ldb_dn_new_fmt(tmp_ctx,
2566 ctx->test_ctx->ldb,
2567 "cn=test_search_cn_renamed,"
2568 "dc=not_search_test_entry");
2569 assert_non_null(new_dn);
2571 ret = ldb_rename(ctx->test_ctx->ldb, dn, new_dn);
2572 assert_int_equal(ret, 0);
2574 } else {
2575 if (ctx->got_2_cn) {
2576 /* Delete this one */
2577 dn = ldb_dn_new_fmt(tmp_ctx,
2578 ctx->test_ctx->ldb,
2579 "cn=test_search_2_cn,%s",
2580 ldb_dn_get_linearized(ctx->basedn));
2581 } else {
2582 dn = ldb_dn_new_fmt(tmp_ctx,
2583 ctx->test_ctx->ldb,
2584 "cn=test_search_cn,%s",
2585 ldb_dn_get_linearized(ctx->basedn));
2587 assert_non_null(dn);
2589 ret = ldb_delete(ctx->test_ctx->ldb, dn);
2590 assert_int_equal(ret, 0);
2594 * Now fill in the position we just removed from the
2595 * index to ensure we fail the test (otherwise we just read
2596 * past the end of the array and find the value we wanted to
2597 * skip)
2599 msg = ldb_msg_new(tmp_ctx);
2600 assert_non_null(msg);
2602 /* We deliberately use ou= not cn= here */
2603 msg->dn = ldb_dn_new_fmt(msg,
2604 ctx->test_ctx->ldb,
2605 "ou=test_search_cn_extra,%s",
2606 ldb_dn_get_linearized(ctx->basedn));
2608 ret = ldb_msg_add_string(msg,
2609 "objectUUID",
2610 "0123456789abcde3");
2612 ret = ldb_add(ctx->test_ctx->ldb,
2613 msg);
2614 assert_int_equal(ret, LDB_SUCCESS);
2616 TALLOC_FREE(tmp_ctx);
2617 return LDB_SUCCESS;
2620 static void test_ldb_callback_modify_during_search(void **state, bool add_index,
2621 bool rename)
2623 struct search_test_ctx *search_test_ctx = talloc_get_type_abort(*state,
2624 struct search_test_ctx);
2625 struct modify_during_search_test_ctx
2626 ctx =
2627 { .res_count = 0,
2628 .test_ctx = search_test_ctx->ldb_test_ctx,
2629 .rename = rename
2632 int ret;
2633 struct ldb_request *req;
2635 ret = ldb_transaction_start(search_test_ctx->ldb_test_ctx->ldb);
2636 assert_int_equal(ret, 0);
2638 if (add_index) {
2639 struct ldb_message *msg;
2640 struct ldb_dn *indexlist = ldb_dn_new(search_test_ctx,
2641 search_test_ctx->ldb_test_ctx->ldb,
2642 "@INDEXLIST");
2643 assert_non_null(indexlist);
2645 msg = ldb_msg_new(search_test_ctx);
2646 assert_non_null(msg);
2648 msg->dn = indexlist;
2650 ret = ldb_msg_add_string(msg, "@IDXONE", "1");
2651 assert_int_equal(ret, LDB_SUCCESS);
2652 ret = ldb_msg_add_string(msg, "@IDXATTR", "cn");
2653 assert_int_equal(ret, LDB_SUCCESS);
2654 ret = ldb_add(search_test_ctx->ldb_test_ctx->ldb,
2655 msg);
2656 if (ret == LDB_ERR_ENTRY_ALREADY_EXISTS) {
2657 msg->elements[0].flags = LDB_FLAG_MOD_ADD;
2658 msg->elements[1].flags = LDB_FLAG_MOD_ADD;
2659 ret = ldb_modify(search_test_ctx->ldb_test_ctx->ldb,
2660 msg);
2662 assert_int_equal(ret, LDB_SUCCESS);
2665 * Now bring the IDXONE index into memory by modifying
2666 * it. This exposes an issue in ldb_tdb
2668 msg = ldb_msg_new(search_test_ctx);
2669 assert_non_null(msg);
2671 msg->dn = ldb_dn_new_fmt(search_test_ctx,
2672 search_test_ctx->ldb_test_ctx->ldb,
2673 "cn=test_search_cn_extra,%s",
2674 search_test_ctx->base_dn);
2676 ret = ldb_msg_add_string(msg,
2677 "objectUUID",
2678 "0123456789abcde2");
2680 ret = ldb_add(search_test_ctx->ldb_test_ctx->ldb,
2681 msg);
2682 assert_int_equal(ret, LDB_SUCCESS);
2684 ret = ldb_delete(search_test_ctx->ldb_test_ctx->ldb,
2685 msg->dn);
2686 assert_int_equal(ret, LDB_SUCCESS);
2689 tevent_loop_allow_nesting(search_test_ctx->ldb_test_ctx->ev);
2691 ctx.basedn
2692 = ldb_dn_new_fmt(search_test_ctx,
2693 search_test_ctx->ldb_test_ctx->ldb,
2694 "%s",
2695 search_test_ctx->base_dn);
2696 assert_non_null(ctx.basedn);
2700 * This search must be over multiple items, and should include
2701 * the new name after a rename, to show that it would match
2702 * both before and after that modify
2704 * This needs to be a search that isn't matched by an index so
2705 * that we just use the one-level index.
2707 ret = ldb_build_search_req(&req,
2708 search_test_ctx->ldb_test_ctx->ldb,
2709 search_test_ctx,
2710 ctx.basedn,
2711 LDB_SCOPE_ONELEVEL,
2712 "(cn=*)",
2713 NULL,
2714 NULL,
2715 &ctx,
2716 test_ldb_callback_modify_during_search_callback1,
2717 NULL);
2718 assert_int_equal(ret, 0);
2720 ret = ldb_request(search_test_ctx->ldb_test_ctx->ldb, req);
2722 if (ret == LDB_SUCCESS) {
2723 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
2725 assert_int_equal(ret, 0);
2727 ret = ldb_transaction_commit(search_test_ctx->ldb_test_ctx->ldb);
2728 assert_int_equal(ret, 0);
2730 assert_int_equal(ctx.res_count, 2);
2731 assert_int_equal(ctx.got_cn, true);
2732 assert_int_equal(ctx.got_2_cn, true);
2735 static void test_ldb_callback_delete_during_indexed_search(void **state)
2737 test_ldb_callback_modify_during_search(state, true, false);
2740 static void test_ldb_callback_delete_during_unindexed_search(void **state)
2742 test_ldb_callback_modify_during_search(state, false, false);
2745 static void test_ldb_callback_rename_during_indexed_search(void **state)
2747 test_ldb_callback_modify_during_search(state, true, true);
2750 static void test_ldb_callback_rename_during_unindexed_search(void **state)
2752 test_ldb_callback_modify_during_search(state, false, true);
2755 static int ldb_case_test_setup(void **state)
2757 int ret;
2758 struct ldb_ldif *ldif;
2759 struct ldbtest_ctx *ldb_test_ctx;
2760 const char *attrs_ldif = \
2761 "dn: @ATTRIBUTES\n"
2762 "cn: CASE_INSENSITIVE\n"
2763 "\n";
2764 struct keyval kvs[] = {
2765 { "cn", "CaseInsensitiveValue" },
2766 { "uid", "CaseSensitiveValue" },
2767 { "objectUUID", "0123456789abcdef" },
2768 { NULL, NULL },
2772 ldbtest_setup((void **) &ldb_test_ctx);
2774 while ((ldif = ldb_ldif_read_string(ldb_test_ctx->ldb, &attrs_ldif))) {
2775 ret = ldb_add(ldb_test_ctx->ldb, ldif->msg);
2776 assert_int_equal(ret, LDB_SUCCESS);
2779 ldb_test_add_data(ldb_test_ctx,
2780 ldb_test_ctx,
2781 "cn=CaseInsensitiveValue",
2782 kvs);
2784 *state = ldb_test_ctx;
2785 return 0;
2788 static int ldb_case_test_teardown(void **state)
2790 int ret;
2791 struct ldbtest_ctx *ldb_test_ctx = talloc_get_type_abort(*state,
2792 struct ldbtest_ctx);
2794 struct ldb_dn *del_dn;
2796 del_dn = ldb_dn_new_fmt(ldb_test_ctx,
2797 ldb_test_ctx->ldb,
2798 "@ATTRIBUTES");
2799 assert_non_null(del_dn);
2801 ret = ldb_delete(ldb_test_ctx->ldb, del_dn);
2802 assert_int_equal(ret, LDB_SUCCESS);
2804 assert_dn_doesnt_exist(ldb_test_ctx,
2805 "@ATTRIBUTES");
2807 ldb_test_remove_data(ldb_test_ctx, ldb_test_ctx,
2808 "cn=CaseInsensitiveValue");
2810 ldbtest_teardown((void **) &ldb_test_ctx);
2811 return 0;
2814 static void test_ldb_attrs_case_insensitive(void **state)
2816 int cnt;
2817 struct ldbtest_ctx *ldb_test_ctx = talloc_get_type_abort(*state,
2818 struct ldbtest_ctx);
2820 /* cn matches exact case */
2821 cnt = sub_search_count(ldb_test_ctx, "", "cn=CaseInsensitiveValue");
2822 assert_int_equal(cnt, 1);
2824 /* cn matches lower case */
2825 cnt = sub_search_count(ldb_test_ctx, "", "cn=caseinsensitivevalue");
2826 assert_int_equal(cnt, 1);
2828 /* uid matches exact case */
2829 cnt = sub_search_count(ldb_test_ctx, "", "uid=CaseSensitiveValue");
2830 assert_int_equal(cnt, 1);
2832 /* uid does not match lower case */
2833 cnt = sub_search_count(ldb_test_ctx, "", "uid=casesensitivevalue");
2834 assert_int_equal(cnt, 0);
2837 static struct ldb_schema_attribute cn_attr_1;
2838 static struct ldb_schema_attribute cn_attr_2;
2839 static struct ldb_schema_attribute default_attr;
2842 override the name to attribute handler function
2844 static const struct ldb_schema_attribute *ldb_test_attribute_handler_override(struct ldb_context *ldb,
2845 void *private_data,
2846 const char *name)
2848 if (private_data != NULL && ldb_attr_cmp(name, "cn") == 0) {
2849 return &cn_attr_1;
2850 } else if (private_data == NULL && ldb_attr_cmp(name, "cn") == 0) {
2851 return &cn_attr_2;
2852 } else if (ldb_attr_cmp(name, "uid") == 0) {
2853 return &cn_attr_2;
2855 return &default_attr;
2858 static void test_ldb_attrs_case_handler(void **state)
2860 int cnt;
2861 int ret;
2862 const struct ldb_schema_syntax *syntax;
2864 struct ldbtest_ctx *ldb_test_ctx = talloc_get_type_abort(*state,
2865 struct ldbtest_ctx);
2866 struct ldb_context *ldb = ldb_test_ctx->ldb;
2868 /* cn matches lower case */
2869 cnt = sub_search_count(ldb_test_ctx, "", "cn=caseinsensitivevalue");
2870 assert_int_equal(cnt, 1);
2872 syntax = ldb_standard_syntax_by_name(ldb, LDB_SYNTAX_OCTET_STRING);
2873 assert_non_null(syntax);
2875 ret = ldb_schema_attribute_fill_with_syntax(ldb, ldb,
2876 "*", 0,
2877 syntax, &default_attr);
2878 assert_int_equal(ret, LDB_SUCCESS);
2880 syntax = ldb_standard_syntax_by_name(ldb, LDB_SYNTAX_OCTET_STRING);
2881 assert_non_null(syntax);
2883 ret = ldb_schema_attribute_fill_with_syntax(ldb, ldb,
2884 "cn", 0,
2885 syntax, &cn_attr_1);
2886 assert_int_equal(ret, LDB_SUCCESS);
2889 * Set an attribute handler, which will fail to match as we
2890 * force case sensitive
2892 ldb_schema_attribute_set_override_handler(ldb,
2893 ldb_test_attribute_handler_override,
2894 (void *)1);
2896 /* cn does not matche lower case */
2897 cnt = sub_search_count(ldb_test_ctx, "", "cn=caseinsensitivevalue");
2898 assert_int_equal(cnt, 0);
2900 syntax = ldb_standard_syntax_by_name(ldb, LDB_SYNTAX_DIRECTORY_STRING);
2901 assert_non_null(syntax);
2903 ret = ldb_schema_attribute_fill_with_syntax(ldb, ldb,
2904 "cn", 0,
2905 syntax, &cn_attr_2);
2906 assert_int_equal(ret, LDB_SUCCESS);
2909 * Set an attribute handler, which will match as we
2910 * force case insensitive
2912 ldb_schema_attribute_set_override_handler(ldb,
2913 ldb_test_attribute_handler_override,
2914 NULL);
2916 /* cn matches lower case */
2917 cnt = sub_search_count(ldb_test_ctx, "", "cn=caseinsensitivevalue");
2918 assert_int_equal(cnt, 1);
2923 static void test_ldb_attrs_index_handler(void **state)
2925 int cnt;
2926 int ret;
2927 const struct ldb_schema_syntax *syntax;
2928 struct ldb_ldif *ldif;
2930 const char *index_ldif = \
2931 "dn: @INDEXLIST\n"
2932 "@IDXATTR: cn\n"
2933 "\n";
2935 struct ldbtest_ctx *ldb_test_ctx = talloc_get_type_abort(*state,
2936 struct ldbtest_ctx);
2937 struct ldb_context *ldb = ldb_test_ctx->ldb;
2939 /* cn matches lower case */
2940 cnt = sub_search_count(ldb_test_ctx, "", "cn=caseinsensitivevalue");
2941 assert_int_equal(cnt, 1);
2943 syntax = ldb_standard_syntax_by_name(ldb, LDB_SYNTAX_OCTET_STRING);
2944 assert_non_null(syntax);
2946 ret = ldb_schema_attribute_fill_with_syntax(ldb, ldb,
2947 "cn", 0,
2948 syntax, &cn_attr_1);
2949 assert_int_equal(ret, LDB_SUCCESS);
2951 syntax = ldb_standard_syntax_by_name(ldb, LDB_SYNTAX_DIRECTORY_STRING);
2952 assert_non_null(syntax);
2954 ret = ldb_schema_attribute_fill_with_syntax(ldb, ldb,
2955 "cn", LDB_ATTR_FLAG_INDEXED,
2956 syntax, &cn_attr_2);
2957 assert_int_equal(ret, LDB_SUCCESS);
2959 syntax = ldb_standard_syntax_by_name(ldb, LDB_SYNTAX_OCTET_STRING);
2960 assert_non_null(syntax);
2962 ret = ldb_schema_attribute_fill_with_syntax(ldb, ldb,
2963 "", 0,
2964 syntax, &default_attr);
2965 assert_int_equal(ret, LDB_SUCCESS);
2968 * Set an attribute handler
2970 ldb_schema_attribute_set_override_handler(ldb,
2971 ldb_test_attribute_handler_override,
2972 NULL);
2974 /* cn matches lower case */
2975 cnt = sub_search_count(ldb_test_ctx, "", "cn=caseinsensitivevalue");
2976 assert_int_equal(cnt, 1);
2978 /* Add the index (actually any modify will do) */
2979 while ((ldif = ldb_ldif_read_string(ldb_test_ctx->ldb, &index_ldif))) {
2980 ret = ldb_add(ldb_test_ctx->ldb, ldif->msg);
2981 if (ret == LDB_ERR_ENTRY_ALREADY_EXISTS) {
2982 ldif->msg->elements[0].flags = LDB_FLAG_MOD_ADD;
2983 ret = ldb_modify(ldb_test_ctx->ldb,
2984 ldif->msg);
2986 assert_int_equal(ret, LDB_SUCCESS);
2989 ldb_schema_set_override_indexlist(ldb, false);
2991 /* cn does match as there is an index now */
2992 cnt = sub_search_count(ldb_test_ctx, "", "cn=caseinsensitivevalue");
2993 assert_int_equal(cnt, 1);
2996 * Set an attribute handler, which will later fail to match as we
2997 * didn't re-index the DB
2999 ldb_schema_attribute_set_override_handler(ldb,
3000 ldb_test_attribute_handler_override,
3001 (void *)1);
3004 * cn does not match as we changed the case sensitivity, but
3005 * didn't re-index
3007 * This shows that the override is in control
3009 cnt = sub_search_count(ldb_test_ctx, "", "cn=caseinsensitivevalue");
3010 assert_int_equal(cnt, 0);
3014 static int ldb_case_attrs_index_test_teardown(void **state)
3016 int ret;
3017 struct ldbtest_ctx *ldb_test_ctx = talloc_get_type_abort(*state,
3018 struct ldbtest_ctx);
3019 struct ldb_dn *del_dn;
3021 del_dn = ldb_dn_new_fmt(ldb_test_ctx,
3022 ldb_test_ctx->ldb,
3023 "@INDEXLIST");
3024 assert_non_null(del_dn);
3026 ret = ldb_delete(ldb_test_ctx->ldb, del_dn);
3027 if (ret != LDB_ERR_NO_SUCH_OBJECT) {
3028 assert_int_equal(ret, LDB_SUCCESS);
3031 assert_dn_doesnt_exist(ldb_test_ctx,
3032 "@INDEXLIST");
3034 ldb_case_test_teardown(state);
3035 return 0;
3039 struct rename_test_ctx {
3040 struct ldbtest_ctx *ldb_test_ctx;
3042 struct ldb_dn *basedn;
3043 const char *str_basedn;
3045 const char *teardown_dn;
3048 static int ldb_rename_test_setup(void **state)
3050 struct ldbtest_ctx *ldb_test_ctx;
3051 struct rename_test_ctx *rename_test_ctx;
3052 const char *strdn = "dc=rename_test_entry_from";
3054 ldbtest_setup((void **) &ldb_test_ctx);
3056 rename_test_ctx = talloc(ldb_test_ctx, struct rename_test_ctx);
3057 assert_non_null(rename_test_ctx);
3058 rename_test_ctx->ldb_test_ctx = ldb_test_ctx;
3059 assert_non_null(rename_test_ctx->ldb_test_ctx);
3061 rename_test_ctx->basedn = ldb_dn_new_fmt(rename_test_ctx,
3062 rename_test_ctx->ldb_test_ctx->ldb,
3063 "%s", strdn);
3064 assert_non_null(rename_test_ctx->basedn);
3066 rename_test_ctx->str_basedn = strdn;
3067 rename_test_ctx->teardown_dn = strdn;
3069 add_dn_with_cn(ldb_test_ctx,
3070 rename_test_ctx->basedn,
3071 "test_rename_cn_val",
3072 "0123456789abcde0");
3074 *state = rename_test_ctx;
3075 return 0;
3078 static int ldb_rename_test_teardown(void **state)
3080 int ret;
3081 struct rename_test_ctx *rename_test_ctx = talloc_get_type_abort(*state,
3082 struct rename_test_ctx);
3083 struct ldbtest_ctx *ldb_test_ctx;
3084 struct ldb_dn *del_dn;
3086 ldb_test_ctx = rename_test_ctx->ldb_test_ctx;
3088 del_dn = ldb_dn_new_fmt(rename_test_ctx,
3089 rename_test_ctx->ldb_test_ctx->ldb,
3090 "%s", rename_test_ctx->teardown_dn);
3091 assert_non_null(del_dn);
3093 ret = ldb_delete(ldb_test_ctx->ldb, del_dn);
3094 assert_int_equal(ret, LDB_SUCCESS);
3096 assert_dn_doesnt_exist(ldb_test_ctx,
3097 rename_test_ctx->teardown_dn);
3099 ldbtest_teardown((void **) &ldb_test_ctx);
3100 return 0;
3103 static void test_ldb_rename(void **state)
3105 struct rename_test_ctx *rename_test_ctx =
3106 talloc_get_type_abort(*state, struct rename_test_ctx);
3107 int ret;
3108 const char *str_new_dn = "dc=rename_test_entry_to";
3109 struct ldb_dn *new_dn;
3111 new_dn = ldb_dn_new_fmt(rename_test_ctx,
3112 rename_test_ctx->ldb_test_ctx->ldb,
3113 "%s", str_new_dn);
3114 assert_non_null(new_dn);
3116 ret = ldb_rename(rename_test_ctx->ldb_test_ctx->ldb,
3117 rename_test_ctx->basedn,
3118 new_dn);
3119 assert_int_equal(ret, LDB_SUCCESS);
3121 assert_dn_exists(rename_test_ctx->ldb_test_ctx, str_new_dn);
3122 assert_dn_doesnt_exist(rename_test_ctx->ldb_test_ctx,
3123 rename_test_ctx->str_basedn);
3124 rename_test_ctx->teardown_dn = str_new_dn;
3126 /* FIXME - test the values which didn't change */
3129 static void test_ldb_rename_from_doesnt_exist(void **state)
3131 struct rename_test_ctx *rename_test_ctx = talloc_get_type_abort(
3132 *state,
3133 struct rename_test_ctx);
3134 int ret;
3135 const char *str_new_dn = "dc=rename_test_entry_to";
3136 const char *str_bad_old_dn = "dc=rename_test_no_such_entry";
3137 struct ldb_dn *new_dn;
3138 struct ldb_dn *bad_old_dn;
3140 new_dn = ldb_dn_new_fmt(rename_test_ctx,
3141 rename_test_ctx->ldb_test_ctx->ldb,
3142 "%s", str_new_dn);
3143 assert_non_null(new_dn);
3145 bad_old_dn = ldb_dn_new_fmt(rename_test_ctx,
3146 rename_test_ctx->ldb_test_ctx->ldb,
3147 "%s", str_bad_old_dn);
3148 assert_non_null(bad_old_dn);
3150 assert_dn_doesnt_exist(rename_test_ctx->ldb_test_ctx,
3151 str_bad_old_dn);
3153 ret = ldb_rename(rename_test_ctx->ldb_test_ctx->ldb,
3154 bad_old_dn, new_dn);
3155 assert_int_equal(ret, LDB_ERR_NO_SUCH_OBJECT);
3157 assert_dn_doesnt_exist(rename_test_ctx->ldb_test_ctx,
3158 str_new_dn);
3161 static void test_ldb_rename_to_exists(void **state)
3163 struct rename_test_ctx *rename_test_ctx = talloc_get_type_abort(
3164 *state,
3165 struct rename_test_ctx);
3166 int ret;
3167 const char *str_new_dn = "dc=rename_test_already_exists";
3168 struct ldb_dn *new_dn;
3170 new_dn = ldb_dn_new_fmt(rename_test_ctx,
3171 rename_test_ctx->ldb_test_ctx->ldb,
3172 "%s", str_new_dn);
3173 assert_non_null(new_dn);
3175 add_dn_with_cn(rename_test_ctx->ldb_test_ctx,
3176 new_dn,
3177 "test_rename_cn_val",
3178 "0123456789abcde1");
3180 ret = ldb_rename(rename_test_ctx->ldb_test_ctx->ldb,
3181 rename_test_ctx->basedn,
3182 new_dn);
3183 assert_int_equal(ret, LDB_ERR_ENTRY_ALREADY_EXISTS);
3185 /* Old object must still exist */
3186 assert_dn_exists(rename_test_ctx->ldb_test_ctx,
3187 rename_test_ctx->str_basedn);
3189 ret = ldb_delete(rename_test_ctx->ldb_test_ctx->ldb,
3190 new_dn);
3191 assert_int_equal(ret, LDB_SUCCESS);
3193 assert_dn_exists(rename_test_ctx->ldb_test_ctx,
3194 rename_test_ctx->teardown_dn);
3197 static void test_ldb_rename_self(void **state)
3199 struct rename_test_ctx *rename_test_ctx = talloc_get_type_abort(
3200 *state,
3201 struct rename_test_ctx);
3202 int ret;
3204 /* Oddly enough, this is a success in ldb.. */
3205 ret = ldb_rename(rename_test_ctx->ldb_test_ctx->ldb,
3206 rename_test_ctx->basedn,
3207 rename_test_ctx->basedn);
3208 assert_int_equal(ret, LDB_SUCCESS);
3210 /* Old object must still exist */
3211 assert_dn_exists(rename_test_ctx->ldb_test_ctx,
3212 rename_test_ctx->str_basedn);
3215 static void test_ldb_rename_dn_case_change(void **state)
3217 struct rename_test_ctx *rename_test_ctx = talloc_get_type_abort(
3218 *state,
3219 struct rename_test_ctx);
3220 int ret;
3221 char *str_new_dn;
3222 struct ldb_dn *new_dn;
3223 unsigned i;
3225 str_new_dn = talloc_strdup(rename_test_ctx, rename_test_ctx->str_basedn);
3226 assert_non_null(str_new_dn);
3227 for (i = 0; str_new_dn[i]; i++) {
3228 str_new_dn[i] = toupper(str_new_dn[i]);
3231 new_dn = ldb_dn_new_fmt(rename_test_ctx,
3232 rename_test_ctx->ldb_test_ctx->ldb,
3233 "%s", str_new_dn);
3234 assert_non_null(new_dn);
3236 ret = ldb_rename(rename_test_ctx->ldb_test_ctx->ldb,
3237 rename_test_ctx->basedn,
3238 new_dn);
3239 assert_int_equal(ret, LDB_SUCCESS);
3241 /* DNs are case insensitive, so both searches will match */
3242 assert_dn_exists(rename_test_ctx->ldb_test_ctx, str_new_dn);
3243 assert_dn_exists(rename_test_ctx->ldb_test_ctx,
3244 rename_test_ctx->str_basedn);
3245 /* FIXME - test the values didn't change */
3248 static int ldb_read_only_setup(void **state)
3250 struct ldbtest_ctx *test_ctx;
3252 ldbtest_setup((void **) &test_ctx);
3254 *state = test_ctx;
3255 return 0;
3258 static int ldb_read_only_teardown(void **state)
3260 struct ldbtest_ctx *test_ctx = talloc_get_type_abort(*state,
3261 struct ldbtest_ctx);
3262 ldbtest_teardown((void **) &test_ctx);
3263 return 0;
3266 static void test_read_only(void **state)
3268 struct ldb_context *ro_ldb = NULL;
3269 struct ldb_context *rw_ldb = NULL;
3270 int ret;
3271 TALLOC_CTX *tmp_ctx = NULL;
3273 struct ldbtest_ctx *test_ctx = talloc_get_type_abort(*state,
3274 struct ldbtest_ctx);
3276 * Close the ldb context freeing it this will ensure it exists on
3277 * disk and can be opened in read only mode
3279 TALLOC_FREE(test_ctx->ldb);
3282 * Open the database in read only and read write mode,
3283 * ensure it's opened in read only mode first
3285 ro_ldb = ldb_init(test_ctx, test_ctx->ev);
3286 ret = ldb_connect(ro_ldb, test_ctx->dbpath, LDB_FLG_RDONLY, NULL);
3287 assert_int_equal(ret, 0);
3289 rw_ldb = ldb_init(test_ctx, test_ctx->ev);
3290 ret = ldb_connect(rw_ldb, test_ctx->dbpath, 0, NULL);
3291 assert_int_equal(ret, 0);
3295 * Set up a context for the temporary variables
3297 tmp_ctx = talloc_new(test_ctx);
3298 assert_non_null(tmp_ctx);
3301 * Ensure that we can search the read write database
3304 struct ldb_result *result = NULL;
3305 struct ldb_dn *dn = ldb_dn_new_fmt(tmp_ctx, rw_ldb,
3306 "dc=test");
3307 assert_non_null(dn);
3309 ret = ldb_search(rw_ldb, tmp_ctx, &result, dn,
3310 LDB_SCOPE_BASE, NULL, NULL);
3311 assert_int_equal(ret, LDB_SUCCESS);
3312 TALLOC_FREE(result);
3313 TALLOC_FREE(dn);
3317 * Ensure that we can search the read only database
3320 struct ldb_result *result = NULL;
3321 struct ldb_dn *dn = ldb_dn_new_fmt(tmp_ctx, ro_ldb,
3322 "dc=test");
3323 assert_non_null(dn);
3325 ret = ldb_search(ro_ldb, tmp_ctx, &result, dn,
3326 LDB_SCOPE_BASE, NULL, NULL);
3327 assert_int_equal(ret, LDB_SUCCESS);
3328 TALLOC_FREE(result);
3329 TALLOC_FREE(dn);
3332 * Ensure that a write to the read only database fails
3335 struct ldb_message *msg = NULL;
3336 msg = ldb_msg_new(tmp_ctx);
3337 assert_non_null(msg);
3339 msg->dn = ldb_dn_new_fmt(msg, ro_ldb, "dc=test");
3340 assert_non_null(msg->dn);
3342 ret = ldb_msg_add_string(msg, "cn", "test_cn_val");
3343 assert_int_equal(ret, 0);
3345 ret = ldb_msg_add_string(msg, "objectUUID",
3346 "0123456789abcde1");
3347 assert_int_equal(ret, LDB_SUCCESS);
3349 ret = ldb_add(ro_ldb, msg);
3350 assert_int_equal(ret, LDB_ERR_UNWILLING_TO_PERFORM);
3351 TALLOC_FREE(msg);
3355 * Ensure that a write to the read write database succeeds
3358 struct ldb_message *msg = NULL;
3359 msg = ldb_msg_new(tmp_ctx);
3360 assert_non_null(msg);
3362 msg->dn = ldb_dn_new_fmt(msg, rw_ldb, "dc=test");
3363 assert_non_null(msg->dn);
3365 ret = ldb_msg_add_string(msg, "cn", "test_cn_val");
3366 assert_int_equal(ret, 0);
3368 ret = ldb_msg_add_string(msg, "objectUUID",
3369 "0123456789abcde2");
3370 assert_int_equal(ret, LDB_SUCCESS);
3372 ret = ldb_add(rw_ldb, msg);
3373 assert_int_equal(ret, LDB_SUCCESS);
3374 TALLOC_FREE(msg);
3378 * Ensure that a delete from a read only database fails
3381 struct ldb_dn *dn = ldb_dn_new_fmt(tmp_ctx, ro_ldb, "dc=test");
3382 assert_non_null(dn);
3384 ret = ldb_delete(ro_ldb, dn);
3385 assert_int_equal(ret, LDB_ERR_UNWILLING_TO_PERFORM);
3386 TALLOC_FREE(dn);
3391 * Ensure that a delete from a read write succeeds
3394 struct ldb_dn *dn = ldb_dn_new_fmt(tmp_ctx, rw_ldb, "dc=test");
3395 assert_non_null(dn);
3397 ret = ldb_delete(rw_ldb, dn);
3398 assert_int_equal(ret, LDB_SUCCESS);
3399 TALLOC_FREE(dn);
3401 TALLOC_FREE(tmp_ctx);
3404 static bool unique_values = false;
3406 static int unique_index_test_module_add(
3407 struct ldb_module *module,
3408 struct ldb_request *req)
3410 if (unique_values) {
3411 struct ldb_message *msg = discard_const(req->op.add.message);
3412 struct ldb_message_element *el = NULL;
3413 el = ldb_msg_find_element(msg, "cn");
3414 if (el != NULL) {
3415 el->flags |= LDB_FLAG_INTERNAL_FORCE_UNIQUE_INDEX;
3419 return ldb_next_request(module, req);
3422 static int unique_index_test_module_init(struct ldb_module *module)
3424 return ldb_next_init(module);
3427 static const struct ldb_module_ops ldb_unique_index_test_module_ops = {
3428 .name = "unique_index_test",
3429 .init_context = unique_index_test_module_init,
3430 .add = unique_index_test_module_add,
3433 static int ldb_unique_index_test_setup(void **state)
3435 int ret;
3436 struct ldb_ldif *ldif;
3437 struct ldbtest_ctx *ldb_test_ctx;
3438 const char *attrs_ldif = \
3439 "dn: @ATTRIBUTES\n"
3440 "cn: UNIQUE_INDEX\n"
3441 "\n";
3442 const char *index_ldif = \
3443 "dn: @INDEXLIST\n"
3444 "@IDXATTR: cn\n"
3445 #ifdef GUID_IDX
3446 "@IDXGUID: objectUUID\n"
3447 "@IDX_DN_GUID: GUID\n"
3448 #endif
3449 "\n";
3450 const char *options[] = {"modules:unique_index_test", NULL};
3453 ret = ldb_register_module(&ldb_unique_index_test_module_ops);
3454 assert_true(ret == LDB_SUCCESS || ret == LDB_ERR_ENTRY_ALREADY_EXISTS);
3455 ldbtest_noconn_setup((void **) &ldb_test_ctx);
3458 ret = ldb_connect(ldb_test_ctx->ldb, ldb_test_ctx->dbpath, 0, options);
3459 assert_int_equal(ret, 0);
3461 while ((ldif = ldb_ldif_read_string(ldb_test_ctx->ldb, &attrs_ldif))) {
3462 ret = ldb_add(ldb_test_ctx->ldb, ldif->msg);
3463 assert_int_equal(ret, LDB_SUCCESS);
3466 while ((ldif = ldb_ldif_read_string(ldb_test_ctx->ldb, &index_ldif))) {
3467 ret = ldb_add(ldb_test_ctx->ldb, ldif->msg);
3468 assert_int_equal(ret, LDB_SUCCESS);
3471 unique_values = true;
3473 *state = ldb_test_ctx;
3474 return 0;
3477 static int ldb_unique_index_test_teardown(void **state)
3479 int ret;
3480 struct ldbtest_ctx *ldb_test_ctx = talloc_get_type_abort(*state,
3481 struct ldbtest_ctx);
3482 struct ldb_dn *del_dn;
3484 del_dn = ldb_dn_new_fmt(ldb_test_ctx,
3485 ldb_test_ctx->ldb,
3486 "@INDEXLIST");
3487 assert_non_null(del_dn);
3489 ret = ldb_delete(ldb_test_ctx->ldb, del_dn);
3490 if (ret != LDB_ERR_NO_SUCH_OBJECT) {
3491 assert_int_equal(ret, LDB_SUCCESS);
3494 assert_dn_doesnt_exist(ldb_test_ctx,
3495 "@INDEXLIST");
3497 TALLOC_FREE(del_dn);
3499 del_dn = ldb_dn_new_fmt(ldb_test_ctx,
3500 ldb_test_ctx->ldb,
3501 "@ATTRIBUTES");
3502 assert_non_null(del_dn);
3504 ret = ldb_delete(ldb_test_ctx->ldb, del_dn);
3505 if (ret != LDB_ERR_NO_SUCH_OBJECT) {
3506 assert_int_equal(ret, LDB_SUCCESS);
3509 assert_dn_doesnt_exist(ldb_test_ctx,
3510 "@ATTRIBUTES");
3512 ldbtest_teardown((void **) &ldb_test_ctx);
3513 return 0;
3517 static void test_ldb_add_unique_value_to_unique_index(void **state)
3519 int ret;
3520 struct ldb_message *msg;
3521 struct ldbtest_ctx *test_ctx = talloc_get_type_abort(*state,
3522 struct ldbtest_ctx);
3523 TALLOC_CTX *tmp_ctx;
3525 tmp_ctx = talloc_new(test_ctx);
3526 assert_non_null(tmp_ctx);
3528 msg = ldb_msg_new(tmp_ctx);
3529 assert_non_null(msg);
3531 msg->dn = ldb_dn_new_fmt(msg, test_ctx->ldb, "dc=test");
3532 assert_non_null(msg->dn);
3534 ret = ldb_msg_add_string(msg, "cn", "test_unique_index");
3535 assert_int_equal(ret, LDB_SUCCESS);
3537 ret = ldb_msg_add_string(msg, "objectUUID",
3538 "0123456789abcde1");
3539 assert_int_equal(ret, LDB_SUCCESS);
3541 ret = ldb_add(test_ctx->ldb, msg);
3542 assert_int_equal(ret, LDB_SUCCESS);
3544 talloc_free(tmp_ctx);
3547 static int ldb_non_unique_index_test_setup(void **state)
3549 int ret;
3550 struct ldb_ldif *ldif;
3551 struct ldbtest_ctx *ldb_test_ctx;
3552 const char *index_ldif = \
3553 "dn: @INDEXLIST\n"
3554 "@IDXATTR: cn\n"
3555 #ifdef GUID_IDX
3556 "@IDXGUID: objectUUID\n"
3557 "@IDX_DN_GUID: GUID\n"
3558 #endif
3559 "\n";
3560 const char *options[] = {"modules:unique_index_test", NULL};
3563 ret = ldb_register_module(&ldb_unique_index_test_module_ops);
3564 assert_true(ret == LDB_SUCCESS || ret == LDB_ERR_ENTRY_ALREADY_EXISTS);
3565 ldbtest_noconn_setup((void **) &ldb_test_ctx);
3568 ret = ldb_connect(ldb_test_ctx->ldb, ldb_test_ctx->dbpath, 0, options);
3569 assert_int_equal(ret, 0);
3571 while ((ldif = ldb_ldif_read_string(ldb_test_ctx->ldb, &index_ldif))) {
3572 ret = ldb_add(ldb_test_ctx->ldb, ldif->msg);
3573 assert_int_equal(ret, LDB_SUCCESS);
3576 unique_values = true;
3578 *state = ldb_test_ctx;
3579 return 0;
3582 static int ldb_non_unique_index_test_teardown(void **state)
3584 int ret;
3585 struct ldbtest_ctx *ldb_test_ctx = talloc_get_type_abort(*state,
3586 struct ldbtest_ctx);
3587 struct ldb_dn *del_dn;
3589 del_dn = ldb_dn_new_fmt(ldb_test_ctx,
3590 ldb_test_ctx->ldb,
3591 "@INDEXLIST");
3592 assert_non_null(del_dn);
3594 ret = ldb_delete(ldb_test_ctx->ldb, del_dn);
3595 if (ret != LDB_ERR_NO_SUCH_OBJECT) {
3596 assert_int_equal(ret, LDB_SUCCESS);
3599 assert_dn_doesnt_exist(ldb_test_ctx,
3600 "@INDEXLIST");
3602 TALLOC_FREE(del_dn);
3604 ldbtest_teardown((void **) &ldb_test_ctx);
3605 return 0;
3608 static void test_ldb_add_duplicate_value_to_unique_index(void **state)
3610 int ret;
3611 struct ldb_message *msg01;
3612 struct ldb_message *msg02;
3613 struct ldbtest_ctx *test_ctx = talloc_get_type_abort(*state,
3614 struct ldbtest_ctx);
3615 TALLOC_CTX *tmp_ctx;
3617 tmp_ctx = talloc_new(test_ctx);
3618 assert_non_null(tmp_ctx);
3620 msg01 = ldb_msg_new(tmp_ctx);
3621 assert_non_null(msg01);
3623 msg01->dn = ldb_dn_new_fmt(msg01, test_ctx->ldb, "dc=test01");
3624 assert_non_null(msg01->dn);
3626 ret = ldb_msg_add_string(msg01, "cn", "test_unique_index");
3627 assert_int_equal(ret, LDB_SUCCESS);
3629 ret = ldb_msg_add_string(msg01, "objectUUID",
3630 "0123456789abcde1");
3631 assert_int_equal(ret, LDB_SUCCESS);
3633 ret = ldb_add(test_ctx->ldb, msg01);
3634 assert_int_equal(ret, LDB_SUCCESS);
3636 msg02 = ldb_msg_new(tmp_ctx);
3637 assert_non_null(msg02);
3639 msg02->dn = ldb_dn_new_fmt(msg02, test_ctx->ldb, "dc=test02");
3640 assert_non_null(msg02->dn);
3642 ret = ldb_msg_add_string(msg02, "cn", "test_unique_index");
3643 assert_int_equal(ret, LDB_SUCCESS);
3645 ret = ldb_msg_add_string(msg02, "objectUUID",
3646 "0123456789abcde2");
3647 assert_int_equal(ret, LDB_SUCCESS);
3649 ret = ldb_add(test_ctx->ldb, msg02);
3650 assert_int_equal(ret, LDB_ERR_CONSTRAINT_VIOLATION);
3651 talloc_free(tmp_ctx);
3654 static void test_ldb_add_to_index_duplicates_allowed(void **state)
3656 int ret;
3657 struct ldb_message *msg01;
3658 struct ldb_message *msg02;
3659 struct ldbtest_ctx *test_ctx = talloc_get_type_abort(*state,
3660 struct ldbtest_ctx);
3661 TALLOC_CTX *tmp_ctx;
3663 unique_values = false;
3665 tmp_ctx = talloc_new(test_ctx);
3666 assert_non_null(tmp_ctx);
3669 msg01 = ldb_msg_new(tmp_ctx);
3670 assert_non_null(msg01);
3672 msg01->dn = ldb_dn_new_fmt(msg01, test_ctx->ldb, "dc=test01");
3673 assert_non_null(msg01->dn);
3675 ret = ldb_msg_add_string(msg01, "cn", "test_unique_index");
3676 assert_int_equal(ret, LDB_SUCCESS);
3678 ret = ldb_msg_add_string(msg01, "objectUUID",
3679 "0123456789abcde1");
3681 ret = ldb_add(test_ctx->ldb, msg01);
3682 assert_int_equal(ret, LDB_SUCCESS);
3684 msg02 = ldb_msg_new(tmp_ctx);
3685 assert_non_null(msg02);
3687 msg02->dn = ldb_dn_new_fmt(msg02, test_ctx->ldb, "dc=test02");
3688 assert_non_null(msg02->dn);
3690 ret = ldb_msg_add_string(msg02, "cn", "test_unique_index");
3691 assert_int_equal(ret, LDB_SUCCESS);
3693 ret = ldb_msg_add_string(msg02, "objectUUID",
3694 "0123456789abcde2");
3696 ret = ldb_add(test_ctx->ldb, msg02);
3697 assert_int_equal(ret, LDB_SUCCESS);
3698 talloc_free(tmp_ctx);
3701 static void test_ldb_add_to_index_unique_values_required(void **state)
3703 int ret;
3704 struct ldb_message *msg01;
3705 struct ldb_message *msg02;
3706 struct ldbtest_ctx *test_ctx = talloc_get_type_abort(*state,
3707 struct ldbtest_ctx);
3708 TALLOC_CTX *tmp_ctx;
3710 unique_values = true;
3712 tmp_ctx = talloc_new(test_ctx);
3713 assert_non_null(tmp_ctx);
3716 msg01 = ldb_msg_new(tmp_ctx);
3717 assert_non_null(msg01);
3719 msg01->dn = ldb_dn_new_fmt(msg01, test_ctx->ldb, "dc=test01");
3720 assert_non_null(msg01->dn);
3722 ret = ldb_msg_add_string(msg01, "cn", "test_unique_index");
3723 assert_int_equal(ret, LDB_SUCCESS);
3725 ret = ldb_msg_add_string(msg01, "objectUUID",
3726 "0123456789abcde1");
3728 ret = ldb_add(test_ctx->ldb, msg01);
3729 assert_int_equal(ret, LDB_SUCCESS);
3731 msg02 = ldb_msg_new(tmp_ctx);
3732 assert_non_null(msg02);
3734 msg02->dn = ldb_dn_new_fmt(msg02, test_ctx->ldb, "dc=test02");
3735 assert_non_null(msg02->dn);
3737 ret = ldb_msg_add_string(msg02, "cn", "test_unique_index");
3738 assert_int_equal(ret, LDB_SUCCESS);
3740 ret = ldb_msg_add_string(msg02, "objectUUID",
3741 "0123456789abcde2");
3743 ret = ldb_add(test_ctx->ldb, msg02);
3744 assert_int_equal(ret, LDB_ERR_CONSTRAINT_VIOLATION);
3745 talloc_free(tmp_ctx);
3748 static void PRINTF_ATTRIBUTE(3, 0) ldb_debug_string(
3749 void *context,
3750 enum ldb_debug_level level,
3751 const char *fmt, va_list ap)
3753 struct ldbtest_ctx *test_ctx =
3754 talloc_get_type_abort(context, struct ldbtest_ctx);
3756 if (level <= LDB_DEBUG_WARNING) {
3757 test_ctx->debug_string = talloc_vasprintf(test_ctx, fmt, ap);
3761 static void test_ldb_unique_index_duplicate_logging(void **state)
3763 int ret;
3764 struct ldb_message *msg01;
3765 struct ldb_message *msg02;
3766 struct ldbtest_ctx *test_ctx = talloc_get_type_abort(*state,
3767 struct ldbtest_ctx);
3768 TALLOC_CTX *tmp_ctx;
3769 char *p = NULL;
3771 /* The GUID mode is not compatible with this test */
3772 #ifdef GUID_IDX
3773 return;
3774 #endif
3776 ldb_set_debug(test_ctx->ldb, ldb_debug_string, test_ctx);
3777 tmp_ctx = talloc_new(test_ctx);
3778 assert_non_null(tmp_ctx);
3780 msg01 = ldb_msg_new(tmp_ctx);
3781 assert_non_null(msg01);
3783 msg01->dn = ldb_dn_new_fmt(msg01, test_ctx->ldb, "dc=test01");
3784 assert_non_null(msg01->dn);
3786 ret = ldb_msg_add_string(msg01, "cn", "test_unique_index");
3787 assert_int_equal(ret, LDB_SUCCESS);
3789 ret = ldb_msg_add_string(msg01, "objectUUID",
3790 "0123456789abcde1");
3792 ret = ldb_add(test_ctx->ldb, msg01);
3793 assert_int_equal(ret, LDB_SUCCESS);
3795 msg02 = ldb_msg_new(tmp_ctx);
3796 assert_non_null(msg02);
3798 msg02->dn = ldb_dn_new_fmt(msg02, test_ctx->ldb, "dc=test02");
3799 assert_non_null(msg02->dn);
3801 ret = ldb_msg_add_string(msg02, "cn", "test_unique_index");
3802 assert_int_equal(ret, LDB_SUCCESS);
3804 ret = ldb_msg_add_string(msg02, "objectUUID",
3805 "0123456789abcde2");
3807 ret = ldb_add(test_ctx->ldb, msg02);
3808 assert_int_equal(ret, LDB_ERR_CONSTRAINT_VIOLATION);
3810 assert_non_null(test_ctx->debug_string);
3811 p = strstr(
3812 test_ctx->debug_string,
3813 "unique index violation on cn "
3814 "in dc=test02, conflicts with dc=test01 in "
3815 "@INDEX:CN:test_unique_index");
3816 assert_non_null(p);
3817 TALLOC_FREE(test_ctx->debug_string);
3818 talloc_free(tmp_ctx);
3821 static void test_ldb_duplicate_dn_logging(void **state)
3823 int ret;
3824 struct ldb_message *msg01;
3825 struct ldb_message *msg02;
3826 struct ldbtest_ctx *test_ctx = talloc_get_type_abort(*state,
3827 struct ldbtest_ctx);
3828 TALLOC_CTX *tmp_ctx;
3830 /* The GUID mode is not compatible with this test */
3831 #ifdef GUID_IDX
3832 return;
3833 #endif
3835 ldb_set_debug(test_ctx->ldb, ldb_debug_string, test_ctx);
3836 tmp_ctx = talloc_new(test_ctx);
3837 assert_non_null(tmp_ctx);
3839 msg01 = ldb_msg_new(tmp_ctx);
3840 assert_non_null(msg01);
3842 msg01->dn = ldb_dn_new_fmt(msg01, test_ctx->ldb, "dc=test01");
3843 assert_non_null(msg01->dn);
3845 ret = ldb_msg_add_string(msg01, "cn", "test_unique_index01");
3846 assert_int_equal(ret, LDB_SUCCESS);
3848 ret = ldb_msg_add_string(msg01, "objectUUID",
3849 "0123456789abcde1");
3851 ret = ldb_add(test_ctx->ldb, msg01);
3852 assert_int_equal(ret, LDB_SUCCESS);
3854 msg02 = ldb_msg_new(tmp_ctx);
3855 assert_non_null(msg02);
3857 msg02->dn = ldb_dn_new_fmt(msg02, test_ctx->ldb, "dc=test01");
3858 assert_non_null(msg02->dn);
3860 ret = ldb_msg_add_string(msg02, "cn", "test_unique_index02");
3861 assert_int_equal(ret, LDB_SUCCESS);
3863 ret = ldb_msg_add_string(msg02, "objectUUID",
3864 "0123456789abcde2");
3866 ret = ldb_add(test_ctx->ldb, msg02);
3867 assert_int_equal(ret, LDB_ERR_ENTRY_ALREADY_EXISTS);
3869 assert_null(test_ctx->debug_string);
3870 talloc_free(tmp_ctx);
3873 static int ldb_guid_index_test_setup(void **state)
3875 int ret;
3876 struct ldb_ldif *ldif;
3877 struct ldbtest_ctx *ldb_test_ctx;
3878 const char *attrs_ldif = \
3879 "dn: @ATTRIBUTES\n"
3880 "cn: UNIQUE_INDEX\n"
3881 "\n";
3882 const char *index_ldif = \
3883 "dn: @INDEXLIST\n"
3884 "@IDXATTR: cn\n"
3885 "@IDXGUID: objectUUID\n"
3886 "@IDX_DN_GUID: GUID\n"
3887 "\n";
3889 ldbtest_noconn_setup((void **) &ldb_test_ctx);
3892 ret = ldb_connect(ldb_test_ctx->ldb, ldb_test_ctx->dbpath, 0, NULL);
3893 assert_int_equal(ret, 0);
3895 while ((ldif = ldb_ldif_read_string(ldb_test_ctx->ldb, &attrs_ldif))) {
3896 ret = ldb_add(ldb_test_ctx->ldb, ldif->msg);
3897 assert_int_equal(ret, LDB_SUCCESS);
3900 while ((ldif = ldb_ldif_read_string(ldb_test_ctx->ldb, &index_ldif))) {
3901 ret = ldb_add(ldb_test_ctx->ldb, ldif->msg);
3902 assert_int_equal(ret, LDB_SUCCESS);
3905 *state = ldb_test_ctx;
3906 return 0;
3909 static int ldb_guid_index_test_teardown(void **state)
3911 int ret;
3912 struct ldbtest_ctx *ldb_test_ctx = talloc_get_type_abort(*state,
3913 struct ldbtest_ctx);
3914 struct ldb_dn *del_dn;
3916 del_dn = ldb_dn_new_fmt(ldb_test_ctx,
3917 ldb_test_ctx->ldb,
3918 "@INDEXLIST");
3919 assert_non_null(del_dn);
3921 ret = ldb_delete(ldb_test_ctx->ldb, del_dn);
3922 if (ret != LDB_ERR_NO_SUCH_OBJECT) {
3923 assert_int_equal(ret, LDB_SUCCESS);
3926 assert_dn_doesnt_exist(ldb_test_ctx,
3927 "@INDEXLIST");
3929 TALLOC_FREE(del_dn);
3931 del_dn = ldb_dn_new_fmt(ldb_test_ctx,
3932 ldb_test_ctx->ldb,
3933 "@ATTRIBUTES");
3934 assert_non_null(del_dn);
3936 ret = ldb_delete(ldb_test_ctx->ldb, del_dn);
3937 if (ret != LDB_ERR_NO_SUCH_OBJECT) {
3938 assert_int_equal(ret, LDB_SUCCESS);
3941 assert_dn_doesnt_exist(ldb_test_ctx,
3942 "@ATTRIBUTES");
3944 ldbtest_teardown((void **) &ldb_test_ctx);
3945 return 0;
3949 static void test_ldb_unique_index_duplicate_with_guid(void **state)
3951 int ret;
3952 struct ldb_message *msg01;
3953 struct ldb_message *msg02;
3954 struct ldbtest_ctx *test_ctx = talloc_get_type_abort(*state,
3955 struct ldbtest_ctx);
3956 TALLOC_CTX *tmp_ctx;
3957 char *p = NULL;
3959 ldb_set_debug(test_ctx->ldb, ldb_debug_string, test_ctx);
3960 tmp_ctx = talloc_new(test_ctx);
3961 assert_non_null(tmp_ctx);
3963 msg01 = ldb_msg_new(tmp_ctx);
3964 assert_non_null(msg01);
3966 msg01->dn = ldb_dn_new_fmt(msg01, test_ctx->ldb, "dc=test01");
3967 assert_non_null(msg01->dn);
3969 ret = ldb_msg_add_string(msg01, "cn", "test_unique_index");
3970 assert_int_equal(ret, LDB_SUCCESS);
3972 ret = ldb_msg_add_string(msg01, "objectUUID", "0123456789abcdef");
3973 assert_int_equal(ret, LDB_SUCCESS);
3975 ret = ldb_add(test_ctx->ldb, msg01);
3976 assert_int_equal(ret, LDB_SUCCESS);
3978 msg02 = ldb_msg_new(tmp_ctx);
3979 assert_non_null(msg02);
3981 msg02->dn = ldb_dn_new_fmt(msg02, test_ctx->ldb, "dc=test02");
3982 assert_non_null(msg02->dn);
3984 ret = ldb_msg_add_string(msg02, "cn", "test_unique_index");
3985 assert_int_equal(ret, LDB_SUCCESS);
3987 ret = ldb_msg_add_string(msg02, "objectUUID", "0123456789abcde0");
3988 assert_int_equal(ret, LDB_SUCCESS);
3990 ret = ldb_add(test_ctx->ldb, msg02);
3991 assert_int_equal(ret, LDB_ERR_CONSTRAINT_VIOLATION);
3993 assert_non_null(test_ctx->debug_string);
3994 p = strstr(
3995 test_ctx->debug_string,
3996 "unique index violation on cn in dc=test02, conflicts with "
3997 "objectUUID 0123456789abcdef in @INDEX:CN:test_unique_index");
3998 assert_non_null(p);
3999 TALLOC_FREE(test_ctx->debug_string);
4000 talloc_free(tmp_ctx);
4003 static void test_ldb_guid_index_duplicate_dn_logging(void **state)
4005 int ret;
4006 struct ldb_message *msg01;
4007 struct ldb_message *msg02;
4008 struct ldbtest_ctx *test_ctx = talloc_get_type_abort(*state,
4009 struct ldbtest_ctx);
4010 TALLOC_CTX *tmp_ctx;
4012 ldb_set_debug(test_ctx->ldb, ldb_debug_string, test_ctx);
4013 tmp_ctx = talloc_new(test_ctx);
4014 assert_non_null(tmp_ctx);
4016 msg01 = ldb_msg_new(tmp_ctx);
4017 assert_non_null(msg01);
4019 msg01->dn = ldb_dn_new_fmt(msg01, test_ctx->ldb, "dc=test01");
4020 assert_non_null(msg01->dn);
4022 ret = ldb_msg_add_string(msg01, "cn", "test_unique_index01");
4023 assert_int_equal(ret, LDB_SUCCESS);
4025 ret = ldb_msg_add_string(msg01, "objectUUID", "0123456789abcdef");
4026 assert_int_equal(ret, LDB_SUCCESS);
4028 ret = ldb_add(test_ctx->ldb, msg01);
4029 assert_int_equal(ret, LDB_SUCCESS);
4031 msg02 = ldb_msg_new(tmp_ctx);
4032 assert_non_null(msg02);
4034 msg02->dn = ldb_dn_new_fmt(msg02, test_ctx->ldb, "dc=test01");
4035 assert_non_null(msg02->dn);
4037 ret = ldb_msg_add_string(msg02, "cn", "test_unique_index02");
4038 assert_int_equal(ret, LDB_SUCCESS);
4040 ret = ldb_msg_add_string(msg02, "objectUUID", "0123456789abcde1");
4041 assert_int_equal(ret, LDB_SUCCESS);
4043 ret = ldb_add(test_ctx->ldb, msg02);
4044 assert_int_equal(ret, LDB_ERR_ENTRY_ALREADY_EXISTS);
4046 assert_null(test_ctx->debug_string);
4047 talloc_free(tmp_ctx);
4050 static void test_ldb_talloc_destructor_transaction_cleanup(void **state)
4052 struct ldbtest_ctx *test_ctx = NULL;
4054 test_ctx = talloc_get_type_abort(*state, struct ldbtest_ctx);
4055 assert_non_null(test_ctx);
4057 ldb_transaction_start(test_ctx->ldb);
4060 * Trigger the destructor
4062 TALLOC_FREE(test_ctx->ldb);
4065 * Now ensure that a new connection can be opened
4068 TALLOC_CTX *tctx = talloc_new(test_ctx);
4069 struct ldbtest_ctx *ctx = talloc_zero(tctx, struct ldbtest_ctx);
4070 struct ldb_dn *basedn;
4071 struct ldb_result *result = NULL;
4072 int ret;
4074 ldbtest_setup((void *)&ctx);
4076 basedn = ldb_dn_new_fmt(tctx, ctx->ldb, "dc=test");
4077 assert_non_null(basedn);
4079 ret = ldb_search(ctx->ldb,
4080 tctx,
4081 &result,
4082 basedn,
4083 LDB_SCOPE_BASE,
4084 NULL,
4085 NULL);
4086 assert_int_equal(ret, 0);
4087 assert_non_null(result);
4088 assert_int_equal(result->count, 0);
4090 ldbtest_teardown((void *)&ctx);
4094 #ifdef TEST_LMDB
4095 static int test_ldb_multiple_connections_callback(struct ldb_request *req,
4096 struct ldb_reply *ares)
4098 int ret;
4099 int pipes[2];
4100 char buf[2];
4101 int pid, child_pid;
4102 int wstatus;
4104 switch (ares->type) {
4105 case LDB_REPLY_ENTRY:
4106 break;
4108 case LDB_REPLY_REFERRAL:
4109 return LDB_SUCCESS;
4111 case LDB_REPLY_DONE:
4112 return ldb_request_done(req, LDB_SUCCESS);
4117 * We open a new ldb on an ldb that is already open and
4118 * then close it.
4120 * If the multiple connection wrapping is correct the
4121 * underlying MDB_env will be left open and we should see
4122 * an active reader in the child we fork next
4124 struct ldb_context *ldb = NULL;
4125 struct tevent_context *ev = NULL;
4126 TALLOC_CTX *mem_ctx = talloc_new(NULL);
4128 ev = tevent_context_init(mem_ctx);
4129 assert_non_null(ev);
4131 ldb = ldb_init(mem_ctx, ev);
4132 assert_non_null(ldb);
4134 ret = ldb_connect(ldb, TEST_BE"://apitest.ldb" , 0, NULL);
4135 if (ret != LDB_SUCCESS) {
4136 return ret;
4138 TALLOC_FREE(ldb);
4139 TALLOC_FREE(mem_ctx);
4142 ret = pipe(pipes);
4143 assert_int_equal(ret, 0);
4145 child_pid = fork();
4146 if (child_pid == 0) {
4147 struct MDB_env *env = NULL;
4148 struct MDB_envinfo stat;
4149 close(pipes[0]);
4152 * Check that there are exactly two readers on the MDB file
4153 * backing the ldb.
4156 ret = mdb_env_create(&env);
4157 if (ret != 0) {
4158 print_error(__location__
4159 " mdb_env_create returned (%d)",
4160 ret);
4161 exit(ret);
4164 ret = mdb_env_open(env,
4165 "apitest.ldb",
4166 MDB_NOSUBDIR | MDB_NOTLS,
4167 0644);
4168 if (ret != 0) {
4169 print_error(__location__
4170 " mdb_env_open returned (%d)",
4171 ret);
4172 exit(ret);
4175 ret = mdb_env_info(env, &stat);
4176 if (ret != 0) {
4177 print_error(__location__
4178 " mdb_env_info returned (%d)",
4179 ret);
4180 exit(ret);
4182 if (stat.me_numreaders != 2) {
4183 print_error(__location__
4184 " Incorrect number of readers (%d)",
4185 stat.me_numreaders);
4186 exit(LDB_ERR_CONSTRAINT_VIOLATION);
4189 ret = write(pipes[1], "GO", 2);
4190 if (ret != 2) {
4191 print_error(__location__
4192 " write returned (%d)",
4193 ret);
4194 exit(LDB_ERR_OPERATIONS_ERROR);
4196 exit(LDB_SUCCESS);
4198 close(pipes[1]);
4199 ret = read(pipes[0], buf, 2);
4200 assert_int_equal(ret, 2);
4202 pid = waitpid(child_pid, &wstatus, 0);
4203 assert_int_equal(pid, child_pid);
4205 assert_true(WIFEXITED(wstatus));
4207 assert_int_equal(WEXITSTATUS(wstatus), 0);
4208 return LDB_SUCCESS;
4212 static void test_ldb_close_with_multiple_connections(void **state)
4214 struct search_test_ctx *search_test_ctx = NULL;
4215 struct ldb_dn *search_dn = NULL;
4216 struct ldb_request *req = NULL;
4217 int ret = 0;
4219 search_test_ctx = talloc_get_type_abort(*state, struct search_test_ctx);
4220 assert_non_null(search_test_ctx);
4222 search_dn = ldb_dn_new_fmt(search_test_ctx,
4223 search_test_ctx->ldb_test_ctx->ldb,
4224 "cn=test_search_cn,"
4225 "dc=search_test_entry");
4226 assert_non_null(search_dn);
4229 * The search just needs to call DONE, we don't care about the
4230 * contents of the search for this test
4232 ret = ldb_build_search_req(&req,
4233 search_test_ctx->ldb_test_ctx->ldb,
4234 search_test_ctx,
4235 search_dn,
4236 LDB_SCOPE_SUBTREE,
4237 "(&(!(filterAttr=*))"
4238 "(cn=test_search_cn))",
4239 NULL,
4240 NULL,
4241 NULL,
4242 test_ldb_multiple_connections_callback,
4243 NULL);
4244 assert_int_equal(ret, 0);
4246 ret = ldb_request(search_test_ctx->ldb_test_ctx->ldb, req);
4247 assert_int_equal(ret, 0);
4249 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
4250 assert_int_equal(ret, 0);
4253 #endif
4255 static void test_transaction_start_across_fork(void **state)
4257 struct ldb_context *ldb1 = NULL;
4258 int ret;
4259 struct ldbtest_ctx *test_ctx = NULL;
4260 int pipes[2];
4261 char buf[2];
4262 int wstatus;
4263 pid_t pid, child_pid;
4265 test_ctx = talloc_get_type_abort(*state, struct ldbtest_ctx);
4268 * Open the database
4270 ldb1 = ldb_init(test_ctx, test_ctx->ev);
4271 ret = ldb_connect(ldb1, test_ctx->dbpath, 0, NULL);
4272 assert_int_equal(ret, 0);
4274 ret = pipe(pipes);
4275 assert_int_equal(ret, 0);
4277 child_pid = fork();
4278 if (child_pid == 0) {
4279 close(pipes[0]);
4280 ret = ldb_transaction_start(ldb1);
4281 if (ret != LDB_ERR_PROTOCOL_ERROR) {
4282 print_error(__location__": ldb_transaction_start "
4283 "returned (%d) %s\n",
4284 ret,
4285 ldb1->err_string);
4286 exit(LDB_ERR_OTHER);
4289 ret = write(pipes[1], "GO", 2);
4290 if (ret != 2) {
4291 print_error(__location__
4292 " write returned (%d)",
4293 ret);
4294 exit(LDB_ERR_OPERATIONS_ERROR);
4296 exit(LDB_SUCCESS);
4298 close(pipes[1]);
4299 ret = read(pipes[0], buf, 2);
4300 assert_int_equal(ret, 2);
4302 pid = waitpid(child_pid, &wstatus, 0);
4303 assert_int_equal(pid, child_pid);
4305 assert_true(WIFEXITED(wstatus));
4307 assert_int_equal(WEXITSTATUS(wstatus), 0);
4310 static void test_transaction_commit_across_fork(void **state)
4312 struct ldb_context *ldb1 = NULL;
4313 int ret;
4314 struct ldbtest_ctx *test_ctx = NULL;
4315 int pipes[2];
4316 char buf[2];
4317 int wstatus;
4318 pid_t pid, child_pid;
4320 test_ctx = talloc_get_type_abort(*state, struct ldbtest_ctx);
4323 * Open the database
4325 ldb1 = ldb_init(test_ctx, test_ctx->ev);
4326 ret = ldb_connect(ldb1, test_ctx->dbpath, 0, NULL);
4327 assert_int_equal(ret, 0);
4329 ret = ldb_transaction_start(ldb1);
4330 assert_int_equal(ret, 0);
4332 ret = pipe(pipes);
4333 assert_int_equal(ret, 0);
4335 child_pid = fork();
4336 if (child_pid == 0) {
4337 close(pipes[0]);
4338 ret = ldb_transaction_commit(ldb1);
4340 if (ret != LDB_ERR_PROTOCOL_ERROR) {
4341 print_error(__location__": ldb_transaction_commit "
4342 "returned (%d) %s\n",
4343 ret,
4344 ldb1->err_string);
4345 exit(LDB_ERR_OTHER);
4348 ret = write(pipes[1], "GO", 2);
4349 if (ret != 2) {
4350 print_error(__location__
4351 " write returned (%d)",
4352 ret);
4353 exit(LDB_ERR_OPERATIONS_ERROR);
4355 exit(LDB_SUCCESS);
4357 close(pipes[1]);
4358 ret = read(pipes[0], buf, 2);
4359 assert_int_equal(ret, 2);
4361 pid = waitpid(child_pid, &wstatus, 0);
4362 assert_int_equal(pid, child_pid);
4364 assert_true(WIFEXITED(wstatus));
4366 assert_int_equal(WEXITSTATUS(wstatus), 0);
4369 static void test_lock_read_across_fork(void **state)
4371 struct ldb_context *ldb1 = NULL;
4372 int ret;
4373 struct ldbtest_ctx *test_ctx = NULL;
4374 int pipes[2];
4375 char buf[2];
4376 int wstatus;
4377 pid_t pid, child_pid;
4379 test_ctx = talloc_get_type_abort(*state, struct ldbtest_ctx);
4382 * Open the database
4384 ldb1 = ldb_init(test_ctx, test_ctx->ev);
4385 ret = ldb_connect(ldb1, test_ctx->dbpath, 0, NULL);
4386 assert_int_equal(ret, 0);
4388 ret = pipe(pipes);
4389 assert_int_equal(ret, 0);
4391 child_pid = fork();
4392 if (child_pid == 0) {
4393 struct ldb_dn *basedn;
4394 struct ldb_result *result = NULL;
4396 close(pipes[0]);
4398 basedn = ldb_dn_new_fmt(test_ctx, test_ctx->ldb, "dc=test");
4399 assert_non_null(basedn);
4401 ret = ldb_search(test_ctx->ldb,
4402 test_ctx,
4403 &result,
4404 basedn,
4405 LDB_SCOPE_BASE,
4406 NULL,
4407 NULL);
4408 if (ret != LDB_ERR_PROTOCOL_ERROR) {
4409 print_error(__location__": ldb_search "
4410 "returned (%d) %s\n",
4411 ret,
4412 ldb1->err_string);
4413 exit(LDB_ERR_OTHER);
4416 ret = write(pipes[1], "GO", 2);
4417 if (ret != 2) {
4418 print_error(__location__
4419 " write returned (%d)",
4420 ret);
4421 exit(LDB_ERR_OPERATIONS_ERROR);
4423 exit(LDB_SUCCESS);
4425 close(pipes[1]);
4426 ret = read(pipes[0], buf, 2);
4427 assert_int_equal(ret, 2);
4429 pid = waitpid(child_pid, &wstatus, 0);
4430 assert_int_equal(pid, child_pid);
4432 assert_true(WIFEXITED(wstatus));
4434 assert_int_equal(WEXITSTATUS(wstatus), 0);
4438 * Ensure that the search actually succeeds on the opening
4439 * pid
4441 struct ldb_dn *basedn;
4442 struct ldb_result *result = NULL;
4444 close(pipes[0]);
4446 basedn = ldb_dn_new_fmt(test_ctx, test_ctx->ldb, "dc=test");
4447 assert_non_null(basedn);
4449 ret = ldb_search(test_ctx->ldb,
4450 test_ctx,
4451 &result,
4452 basedn,
4453 LDB_SCOPE_BASE,
4454 NULL,
4455 NULL);
4456 assert_int_equal(0, ret);
4460 static void test_multiple_opens_across_fork(void **state)
4462 struct ldb_context *ldb1 = NULL;
4463 struct ldb_context *ldb2 = NULL;
4464 int ret;
4465 struct ldbtest_ctx *test_ctx = NULL;
4466 int pipes[2];
4467 char buf[2];
4468 int wstatus;
4469 pid_t pid, child_pid;
4471 test_ctx = talloc_get_type_abort(*state, struct ldbtest_ctx);
4474 * Open the database again
4476 ldb1 = ldb_init(test_ctx, test_ctx->ev);
4477 ret = ldb_connect(ldb1, test_ctx->dbpath, LDB_FLG_RDONLY, NULL);
4478 assert_int_equal(ret, 0);
4480 ldb2 = ldb_init(test_ctx, test_ctx->ev);
4481 ret = ldb_connect(ldb2, test_ctx->dbpath, 0, NULL);
4482 assert_int_equal(ret, 0);
4484 ret = pipe(pipes);
4485 assert_int_equal(ret, 0);
4487 child_pid = fork();
4488 if (child_pid == 0) {
4489 struct ldb_context *ldb3 = NULL;
4491 close(pipes[0]);
4492 ldb3 = ldb_init(test_ctx, test_ctx->ev);
4493 ret = ldb_connect(ldb3, test_ctx->dbpath, 0, NULL);
4494 if (ret != 0) {
4495 print_error(__location__": ldb_connect returned (%d)\n",
4496 ret);
4497 exit(ret);
4499 ret = write(pipes[1], "GO", 2);
4500 if (ret != 2) {
4501 print_error(__location__
4502 " write returned (%d)",
4503 ret);
4504 exit(LDB_ERR_OPERATIONS_ERROR);
4506 exit(LDB_SUCCESS);
4508 close(pipes[1]);
4509 ret = read(pipes[0], buf, 2);
4510 assert_int_equal(ret, 2);
4512 pid = waitpid(child_pid, &wstatus, 0);
4513 assert_int_equal(pid, child_pid);
4515 assert_true(WIFEXITED(wstatus));
4517 assert_int_equal(WEXITSTATUS(wstatus), 0);
4520 int main(int argc, const char **argv)
4522 const struct CMUnitTest tests[] = {
4523 cmocka_unit_test_setup_teardown(test_connect,
4524 ldbtest_noconn_setup,
4525 ldbtest_noconn_teardown),
4526 cmocka_unit_test_setup_teardown(test_ldif_message,
4527 ldbtest_noconn_setup,
4528 ldbtest_noconn_teardown),
4529 cmocka_unit_test_setup_teardown(test_ldif_message_redacted,
4530 ldbtest_noconn_setup,
4531 ldbtest_noconn_teardown),
4532 cmocka_unit_test_setup_teardown(test_ldb_add,
4533 ldbtest_setup,
4534 ldbtest_teardown),
4535 cmocka_unit_test_setup_teardown(test_ldb_search,
4536 ldbtest_setup,
4537 ldbtest_teardown),
4538 cmocka_unit_test_setup_teardown(test_ldb_del,
4539 ldbtest_setup,
4540 ldbtest_teardown),
4541 cmocka_unit_test_setup_teardown(test_ldb_del_noexist,
4542 ldbtest_setup,
4543 ldbtest_teardown),
4544 cmocka_unit_test_setup_teardown(test_ldb_handle,
4545 ldbtest_setup,
4546 ldbtest_teardown),
4547 cmocka_unit_test_setup_teardown(test_ldb_build_search_req,
4548 ldbtest_setup,
4549 ldbtest_teardown),
4550 cmocka_unit_test_setup_teardown(test_transactions,
4551 ldbtest_setup,
4552 ldbtest_teardown),
4553 cmocka_unit_test_setup_teardown(test_nested_transactions,
4554 ldbtest_setup,
4555 ldbtest_teardown),
4556 cmocka_unit_test_setup_teardown(test_ldb_modify_add_key,
4557 ldb_modify_test_setup,
4558 ldb_modify_test_teardown),
4559 cmocka_unit_test_setup_teardown(test_ldb_modify_extend_key,
4560 ldb_modify_test_setup,
4561 ldb_modify_test_teardown),
4562 cmocka_unit_test_setup_teardown(test_ldb_modify_add_key_noval,
4563 ldb_modify_test_setup,
4564 ldb_modify_test_teardown),
4565 cmocka_unit_test_setup_teardown(test_ldb_modify_replace_key,
4566 ldb_modify_test_setup,
4567 ldb_modify_test_teardown),
4568 cmocka_unit_test_setup_teardown(test_ldb_modify_replace_noexist_key,
4569 ldb_modify_test_setup,
4570 ldb_modify_test_teardown),
4571 cmocka_unit_test_setup_teardown(test_ldb_modify_replace_zero_vals,
4572 ldb_modify_test_setup,
4573 ldb_modify_test_teardown),
4574 cmocka_unit_test_setup_teardown(test_ldb_modify_replace_noexist_key_zero_vals,
4575 ldb_modify_test_setup,
4576 ldb_modify_test_teardown),
4577 cmocka_unit_test_setup_teardown(test_ldb_modify_del_key,
4578 ldb_modify_test_setup,
4579 ldb_modify_test_teardown),
4580 cmocka_unit_test_setup_teardown(test_ldb_modify_del_keyval,
4581 ldb_modify_test_setup,
4582 ldb_modify_test_teardown),
4583 cmocka_unit_test_setup_teardown(test_search_match_none,
4584 ldb_search_test_setup,
4585 ldb_search_test_teardown),
4586 cmocka_unit_test_setup_teardown(test_search_match_one,
4587 ldb_search_test_setup,
4588 ldb_search_test_teardown),
4589 cmocka_unit_test_setup_teardown(test_search_match_filter,
4590 ldb_search_test_setup,
4591 ldb_search_test_teardown),
4592 cmocka_unit_test_setup_teardown(test_search_match_both,
4593 ldb_search_test_setup,
4594 ldb_search_test_teardown),
4595 cmocka_unit_test_setup_teardown(test_search_match_basedn,
4596 ldb_search_test_setup,
4597 ldb_search_test_teardown),
4598 cmocka_unit_test_setup_teardown(test_ldb_search_against_transaction,
4599 ldb_search_test_setup,
4600 ldb_search_test_teardown),
4601 cmocka_unit_test_setup_teardown(test_ldb_modify_during_unindexed_search,
4602 ldb_search_test_setup,
4603 ldb_search_test_teardown),
4604 cmocka_unit_test_setup_teardown(test_ldb_modify_during_indexed_search,
4605 ldb_search_test_setup,
4606 ldb_search_test_teardown),
4607 cmocka_unit_test_setup_teardown(test_ldb_rename_during_unindexed_search,
4608 ldb_search_test_setup,
4609 ldb_search_test_teardown),
4610 cmocka_unit_test_setup_teardown(test_ldb_rename_during_indexed_search,
4611 ldb_search_test_setup,
4612 ldb_search_test_teardown),
4613 cmocka_unit_test_setup_teardown(test_ldb_callback_rename_during_unindexed_search,
4614 ldb_search_test_setup,
4615 ldb_search_test_teardown),
4616 cmocka_unit_test_setup_teardown(test_ldb_callback_rename_during_indexed_search,
4617 ldb_search_test_setup,
4618 ldb_search_test_teardown),
4619 cmocka_unit_test_setup_teardown(test_ldb_callback_delete_during_unindexed_search,
4620 ldb_search_test_setup,
4621 ldb_search_test_teardown),
4622 cmocka_unit_test_setup_teardown(test_ldb_callback_delete_during_indexed_search,
4623 ldb_search_test_setup,
4624 ldb_search_test_teardown),
4625 cmocka_unit_test_setup_teardown(test_ldb_modify_during_whole_search,
4626 ldb_search_test_setup,
4627 ldb_search_test_teardown),
4628 cmocka_unit_test_setup_teardown(test_ldb_modify_before_ldb_wait,
4629 ldb_search_test_setup,
4630 ldb_search_test_teardown),
4631 cmocka_unit_test_setup_teardown(test_ldb_attrs_case_insensitive,
4632 ldb_case_test_setup,
4633 ldb_case_test_teardown),
4634 cmocka_unit_test_setup_teardown(test_ldb_attrs_case_handler,
4635 ldb_case_test_setup,
4636 ldb_case_test_teardown),
4637 cmocka_unit_test_setup_teardown(test_ldb_attrs_index_handler,
4638 ldb_case_test_setup,
4639 ldb_case_attrs_index_test_teardown),
4640 cmocka_unit_test_setup_teardown(test_ldb_rename,
4641 ldb_rename_test_setup,
4642 ldb_rename_test_teardown),
4643 cmocka_unit_test_setup_teardown(test_ldb_rename_from_doesnt_exist,
4644 ldb_rename_test_setup,
4645 ldb_rename_test_teardown),
4646 cmocka_unit_test_setup_teardown(test_ldb_rename_to_exists,
4647 ldb_rename_test_setup,
4648 ldb_rename_test_teardown),
4649 cmocka_unit_test_setup_teardown(test_ldb_rename_self,
4650 ldb_rename_test_setup,
4651 ldb_rename_test_teardown),
4652 cmocka_unit_test_setup_teardown(test_ldb_rename_dn_case_change,
4653 ldb_rename_test_setup,
4654 ldb_rename_test_teardown),
4655 cmocka_unit_test_setup_teardown(test_read_only,
4656 ldb_read_only_setup,
4657 ldb_read_only_teardown),
4658 cmocka_unit_test_setup_teardown(
4659 test_ldb_add_unique_value_to_unique_index,
4660 ldb_unique_index_test_setup,
4661 ldb_unique_index_test_teardown),
4662 cmocka_unit_test_setup_teardown(
4663 test_ldb_add_duplicate_value_to_unique_index,
4664 ldb_unique_index_test_setup,
4665 ldb_unique_index_test_teardown),
4666 cmocka_unit_test_setup_teardown(
4667 test_ldb_add_to_index_duplicates_allowed,
4668 ldb_non_unique_index_test_setup,
4669 ldb_non_unique_index_test_teardown),
4670 cmocka_unit_test_setup_teardown(
4671 test_ldb_add_to_index_unique_values_required,
4672 ldb_non_unique_index_test_setup,
4673 ldb_non_unique_index_test_teardown),
4674 /* These tests are not compatible with mdb */
4675 cmocka_unit_test_setup_teardown(
4676 test_ldb_unique_index_duplicate_logging,
4677 ldb_unique_index_test_setup,
4678 ldb_unique_index_test_teardown),
4679 cmocka_unit_test_setup_teardown(
4680 test_ldb_duplicate_dn_logging,
4681 ldb_unique_index_test_setup,
4682 ldb_unique_index_test_teardown),
4683 cmocka_unit_test_setup_teardown(
4684 test_ldb_guid_index_duplicate_dn_logging,
4685 ldb_guid_index_test_setup,
4686 ldb_guid_index_test_teardown),
4687 cmocka_unit_test_setup_teardown(
4688 test_ldb_unique_index_duplicate_with_guid,
4689 ldb_guid_index_test_setup,
4690 ldb_guid_index_test_teardown),
4691 cmocka_unit_test_setup_teardown(
4692 test_ldb_talloc_destructor_transaction_cleanup,
4693 ldbtest_setup,
4694 ldbtest_teardown),
4695 #ifdef TEST_LMDB
4696 cmocka_unit_test_setup_teardown(
4697 test_ldb_close_with_multiple_connections,
4698 ldb_search_test_setup,
4699 ldb_search_test_teardown),
4700 #endif
4701 cmocka_unit_test_setup_teardown(
4702 test_transaction_start_across_fork,
4703 ldbtest_setup,
4704 ldbtest_teardown),
4705 cmocka_unit_test_setup_teardown(
4706 test_transaction_commit_across_fork,
4707 ldbtest_setup,
4708 ldbtest_teardown),
4709 cmocka_unit_test_setup_teardown(
4710 test_lock_read_across_fork,
4711 ldbtest_setup,
4712 ldbtest_teardown),
4713 cmocka_unit_test_setup_teardown(
4714 test_multiple_opens_across_fork,
4715 ldbtest_setup,
4716 ldbtest_teardown),
4719 if (argc > 1) {
4720 cmocka_set_test_filter(argv[1]);
4723 cmocka_set_message_output(CM_OUTPUT_SUBUNIT);
4725 return cmocka_run_group_tests(tests, NULL, NULL);