3 * These headers or their equivalents should be included prior to
11 * This allows test applications to use custom definitions of C standard
12 * library functions and types.
23 #define TEVENT_DEPRECATED 1
27 #include <ldb_module.h>
28 #include <ldb_private.h>
35 #define DEFAULT_BE "tdb"
38 #define TEST_BE DEFAULT_BE
42 struct tevent_context *ev;
43 struct ldb_context *ldb;
46 const char *lockfile; /* lockfile is separate */
51 static void unlink_old_db(struct ldbtest_ctx *test_ctx)
56 ret = unlink(test_ctx->lockfile);
57 if (ret == -1 && errno != ENOENT) {
62 ret = unlink(test_ctx->dbfile);
63 if (ret == -1 && errno != ENOENT) {
68 static int ldbtest_noconn_setup(void **state)
70 struct ldbtest_ctx *test_ctx;
72 test_ctx = talloc_zero(NULL, struct ldbtest_ctx);
73 assert_non_null(test_ctx);
75 test_ctx->ev = tevent_context_init(test_ctx);
76 assert_non_null(test_ctx->ev);
78 test_ctx->ldb = ldb_init(test_ctx, test_ctx->ev);
79 assert_non_null(test_ctx->ldb);
81 test_ctx->dbfile = talloc_strdup(test_ctx, "apitest.ldb");
82 assert_non_null(test_ctx->dbfile);
84 test_ctx->lockfile = talloc_asprintf(test_ctx, "%s-lock",
86 assert_non_null(test_ctx->lockfile);
88 test_ctx->dbpath = talloc_asprintf(test_ctx,
89 TEST_BE"://%s", test_ctx->dbfile);
90 assert_non_null(test_ctx->dbpath);
92 unlink_old_db(test_ctx);
97 static int ldbtest_noconn_teardown(void **state)
99 struct ldbtest_ctx *test_ctx = talloc_get_type_abort(*state,
102 unlink_old_db(test_ctx);
103 talloc_free(test_ctx);
107 static void test_connect(void **state)
109 struct ldbtest_ctx *test_ctx = talloc_get_type_abort(*state,
113 ret = ldb_connect(test_ctx->ldb, test_ctx->dbpath, 0, NULL);
114 assert_int_equal(ret, 0);
117 static int ldbtest_setup(void **state)
119 struct ldbtest_ctx *test_ctx;
122 ldbtest_noconn_setup((void **) &test_ctx);
124 ret = ldb_connect(test_ctx->ldb, test_ctx->dbpath, 0, NULL);
125 assert_int_equal(ret, 0);
131 static int ldbtest_teardown(void **state)
133 struct ldbtest_ctx *test_ctx = talloc_get_type_abort(*state,
135 ldbtest_noconn_teardown((void **) &test_ctx);
139 static void test_ldb_add(void **state)
142 struct ldb_message *msg;
143 struct ldbtest_ctx *test_ctx = talloc_get_type_abort(*state,
147 tmp_ctx = talloc_new(test_ctx);
148 assert_non_null(tmp_ctx);
150 msg = ldb_msg_new(tmp_ctx);
151 assert_non_null(msg);
153 msg->dn = ldb_dn_new_fmt(msg, test_ctx->ldb, "dc=test");
154 assert_non_null(msg->dn);
156 ret = ldb_msg_add_string(msg, "cn", "test_cn_val");
157 assert_int_equal(ret, 0);
159 ret = ldb_add(test_ctx->ldb, msg);
160 assert_int_equal(ret, 0);
162 talloc_free(tmp_ctx);
165 static void test_ldb_search(void **state)
168 struct ldb_message *msg;
169 struct ldbtest_ctx *test_ctx = talloc_get_type_abort(*state,
172 struct ldb_dn *basedn;
173 struct ldb_dn *basedn2;
174 struct ldb_result *result = NULL;
176 tmp_ctx = talloc_new(test_ctx);
177 assert_non_null(tmp_ctx);
179 basedn = ldb_dn_new_fmt(tmp_ctx, test_ctx->ldb, "dc=test");
180 assert_non_null(basedn);
182 ret = ldb_search(test_ctx->ldb, tmp_ctx, &result, basedn,
183 LDB_SCOPE_BASE, NULL, NULL);
184 assert_int_equal(ret, 0);
185 assert_non_null(result);
186 assert_int_equal(result->count, 0);
188 msg = ldb_msg_new(tmp_ctx);
189 assert_non_null(msg);
192 assert_non_null(msg->dn);
194 ret = ldb_msg_add_string(msg, "cn", "test_cn_val1");
195 assert_int_equal(ret, 0);
197 ret = ldb_add(test_ctx->ldb, msg);
198 assert_int_equal(ret, 0);
200 basedn2 = ldb_dn_new_fmt(tmp_ctx, test_ctx->ldb, "dc=test2");
201 assert_non_null(basedn2);
203 msg = ldb_msg_new(tmp_ctx);
204 assert_non_null(msg);
207 assert_non_null(msg->dn);
209 ret = ldb_msg_add_string(msg, "cn", "test_cn_val2");
210 assert_int_equal(ret, 0);
212 ret = ldb_add(test_ctx->ldb, msg);
213 assert_int_equal(ret, 0);
215 ret = ldb_search(test_ctx->ldb, tmp_ctx, &result, basedn,
216 LDB_SCOPE_BASE, NULL, NULL);
217 assert_int_equal(ret, 0);
218 assert_non_null(result);
219 assert_int_equal(result->count, 1);
220 assert_string_equal(ldb_dn_get_linearized(result->msgs[0]->dn),
221 ldb_dn_get_linearized(basedn));
223 ret = ldb_search(test_ctx->ldb, tmp_ctx, &result, basedn2,
224 LDB_SCOPE_BASE, NULL, NULL);
225 assert_int_equal(ret, 0);
226 assert_non_null(result);
227 assert_int_equal(result->count, 1);
228 assert_string_equal(ldb_dn_get_linearized(result->msgs[0]->dn),
229 ldb_dn_get_linearized(basedn2));
231 talloc_free(tmp_ctx);
234 static int base_search_count(struct ldbtest_ctx *test_ctx, const char *entry_dn)
237 struct ldb_dn *basedn;
238 struct ldb_result *result = NULL;
242 tmp_ctx = talloc_new(test_ctx);
243 assert_non_null(tmp_ctx);
245 basedn = ldb_dn_new_fmt(tmp_ctx, test_ctx->ldb, "%s", entry_dn);
246 assert_non_null(basedn);
248 ret = ldb_search(test_ctx->ldb, tmp_ctx, &result, basedn,
249 LDB_SCOPE_BASE, NULL, NULL);
250 assert_int_equal(ret, LDB_SUCCESS);
251 assert_non_null(result);
253 count = result->count;
254 talloc_free(tmp_ctx);
258 static int sub_search_count(struct ldbtest_ctx *test_ctx,
263 struct ldb_dn *basedn;
264 struct ldb_result *result = NULL;
268 tmp_ctx = talloc_new(test_ctx);
269 assert_non_null(tmp_ctx);
271 basedn = ldb_dn_new_fmt(tmp_ctx, test_ctx->ldb, "%s", base_dn);
272 assert_non_null(basedn);
274 ret = ldb_search(test_ctx->ldb, tmp_ctx, &result, basedn,
275 LDB_SCOPE_SUBTREE, NULL, "%s", filter);
276 assert_int_equal(ret, LDB_SUCCESS);
277 assert_non_null(result);
279 count = result->count;
280 talloc_free(tmp_ctx);
284 /* In general it would be better if utility test functions didn't assert
285 * but only returned a value, then assert in the test shows correct
288 static void assert_dn_exists(struct ldbtest_ctx *test_ctx,
289 const char *entry_dn)
293 count = base_search_count(test_ctx, entry_dn);
294 assert_int_equal(count, 1);
297 static void assert_dn_doesnt_exist(struct ldbtest_ctx *test_ctx,
298 const char *entry_dn)
302 count = base_search_count(test_ctx, entry_dn);
303 assert_int_equal(count, 0);
306 static void add_dn_with_cn(struct ldbtest_ctx *test_ctx,
308 const char *cn_value)
312 struct ldb_message *msg;
314 tmp_ctx = talloc_new(test_ctx);
315 assert_non_null(tmp_ctx);
317 assert_dn_doesnt_exist(test_ctx,
318 ldb_dn_get_linearized(dn));
320 msg = ldb_msg_new(tmp_ctx);
321 assert_non_null(msg);
324 ret = ldb_msg_add_string(msg, "cn", cn_value);
325 assert_int_equal(ret, LDB_SUCCESS);
327 ret = ldb_add(test_ctx->ldb, msg);
328 assert_int_equal(ret, LDB_SUCCESS);
330 assert_dn_exists(test_ctx,
331 ldb_dn_get_linearized(dn));
332 talloc_free(tmp_ctx);
335 static void test_ldb_del(void **state)
338 struct ldbtest_ctx *test_ctx = talloc_get_type_abort(*state,
340 const char *basedn = "dc=ldb_del_test";
343 dn = ldb_dn_new_fmt(test_ctx, test_ctx->ldb, "%s", basedn);
346 add_dn_with_cn(test_ctx, dn, "test_del_cn_val");
348 ret = ldb_delete(test_ctx->ldb, dn);
349 assert_int_equal(ret, LDB_SUCCESS);
351 assert_dn_doesnt_exist(test_ctx, basedn);
354 static void test_ldb_del_noexist(void **state)
356 struct ldbtest_ctx *test_ctx = talloc_get_type_abort(*state,
358 struct ldb_dn *basedn;
361 basedn = ldb_dn_new(test_ctx, test_ctx->ldb, "dc=nosuchplace");
362 assert_non_null(basedn);
364 ret = ldb_delete(test_ctx->ldb, basedn);
365 assert_int_equal(ret, LDB_ERR_NO_SUCH_OBJECT);
368 static void test_ldb_handle(void **state)
371 struct ldbtest_ctx *test_ctx = talloc_get_type_abort(*state,
374 struct ldb_dn *basedn;
375 struct ldb_request *request = NULL;
376 struct ldb_request *request2 = NULL;
377 struct ldb_result *res = NULL;
378 const char *attrs[] = { "cn", NULL };
380 tmp_ctx = talloc_new(test_ctx);
381 assert_non_null(tmp_ctx);
383 basedn = ldb_dn_new_fmt(tmp_ctx, test_ctx->ldb, "dc=test");
384 assert_non_null(basedn);
386 res = talloc_zero(tmp_ctx, struct ldb_result);
387 assert_non_null(res);
389 ret = ldb_build_search_req(&request, test_ctx->ldb, tmp_ctx,
390 basedn, LDB_SCOPE_BASE,
391 NULL, attrs, NULL, res,
392 ldb_search_default_callback,
394 assert_int_equal(ret, 0);
396 /* We are against ldb_tdb, so expect private event contexts */
397 assert_ptr_not_equal(ldb_handle_get_event_context(request->handle),
398 ldb_get_event_context(test_ctx->ldb));
400 ret = ldb_build_search_req(&request2, test_ctx->ldb, tmp_ctx,
401 basedn, LDB_SCOPE_BASE,
402 NULL, attrs, NULL, res,
403 ldb_search_default_callback,
405 assert_int_equal(ret, 0);
407 /* Expect that same event context will be chained */
408 assert_ptr_equal(ldb_handle_get_event_context(request->handle),
409 ldb_handle_get_event_context(request2->handle));
411 /* Now force this to use the global context */
412 ldb_handle_use_global_event_context(request2->handle);
413 assert_ptr_equal(ldb_handle_get_event_context(request2->handle),
414 ldb_get_event_context(test_ctx->ldb));
416 talloc_free(tmp_ctx);
419 static void test_ldb_build_search_req(void **state)
422 struct ldbtest_ctx *test_ctx = talloc_get_type_abort(*state,
425 struct ldb_dn *basedn;
426 struct ldb_request *request = NULL;
427 struct ldb_request *request2 = NULL;
428 struct ldb_result *res = NULL;
429 const char *attrs[] = { "cn", NULL };
431 tmp_ctx = talloc_new(test_ctx);
432 assert_non_null(tmp_ctx);
434 basedn = ldb_dn_new_fmt(tmp_ctx, test_ctx->ldb, "dc=test");
435 assert_non_null(basedn);
437 res = talloc_zero(tmp_ctx, struct ldb_result);
438 assert_non_null(res);
440 ret = ldb_build_search_req(&request, test_ctx->ldb, tmp_ctx,
441 basedn, LDB_SCOPE_BASE,
442 NULL, attrs, NULL, res,
443 ldb_search_default_callback,
445 assert_int_equal(ret, 0);
447 assert_int_equal(request->operation, LDB_SEARCH);
448 assert_ptr_equal(request->op.search.base, basedn);
449 assert_int_equal(request->op.search.scope, LDB_SCOPE_BASE);
450 assert_non_null(request->op.search.tree);
451 assert_ptr_equal(request->op.search.attrs, attrs);
452 assert_ptr_equal(request->context, res);
453 assert_ptr_equal(request->callback, ldb_search_default_callback);
455 ret = ldb_build_search_req(&request2, test_ctx->ldb, tmp_ctx,
456 basedn, LDB_SCOPE_BASE,
457 NULL, attrs, NULL, res,
458 ldb_search_default_callback,
460 assert_int_equal(ret, 0);
461 assert_ptr_equal(request, request2->handle->parent);
462 assert_int_equal(request->starttime, request2->starttime);
463 assert_int_equal(request->timeout, request2->timeout);
465 talloc_free(tmp_ctx);
468 static void add_keyval(struct ldbtest_ctx *test_ctx,
473 struct ldb_message *msg;
475 msg = ldb_msg_new(test_ctx);
476 assert_non_null(msg);
478 msg->dn = ldb_dn_new_fmt(msg, test_ctx->ldb, "%s=%s", key, val);
479 assert_non_null(msg->dn);
481 ret = ldb_msg_add_string(msg, key, val);
482 assert_int_equal(ret, 0);
484 ret = ldb_add(test_ctx->ldb, msg);
485 assert_int_equal(ret, 0);
490 static struct ldb_result *get_keyval(struct ldbtest_ctx *test_ctx,
495 struct ldb_result *result;
496 struct ldb_dn *basedn;
498 basedn = ldb_dn_new_fmt(test_ctx, test_ctx->ldb, "%s=%s", key, val);
499 assert_non_null(basedn);
501 ret = ldb_search(test_ctx->ldb, test_ctx, &result, basedn,
502 LDB_SCOPE_BASE, NULL, NULL);
503 assert_int_equal(ret, 0);
508 static void test_transactions(void **state)
511 struct ldbtest_ctx *test_ctx = talloc_get_type_abort(*state,
513 struct ldb_result *res;
515 /* start lev-0 transaction */
516 ret = ldb_transaction_start(test_ctx->ldb);
517 assert_int_equal(ret, 0);
519 add_keyval(test_ctx, "vegetable", "carrot");
521 /* commit lev-0 transaction */
522 ret = ldb_transaction_commit(test_ctx->ldb);
523 assert_int_equal(ret, 0);
525 /* start another lev-1 nested transaction */
526 ret = ldb_transaction_start(test_ctx->ldb);
527 assert_int_equal(ret, 0);
529 add_keyval(test_ctx, "fruit", "apple");
531 /* abort lev-1 nested transaction */
532 ret = ldb_transaction_cancel(test_ctx->ldb);
533 assert_int_equal(ret, 0);
535 res = get_keyval(test_ctx, "vegetable", "carrot");
536 assert_non_null(res);
537 assert_int_equal(res->count, 1);
539 res = get_keyval(test_ctx, "fruit", "apple");
540 assert_non_null(res);
541 assert_int_equal(res->count, 0);
544 struct ldb_mod_test_ctx {
545 struct ldbtest_ctx *ldb_test_ctx;
546 const char *entry_dn;
554 static struct ldb_message *build_mod_msg(TALLOC_CTX *mem_ctx,
555 struct ldbtest_ctx *test_ctx,
560 struct ldb_message *msg;
564 msg = ldb_msg_new(mem_ctx);
565 assert_non_null(msg);
567 msg->dn = ldb_dn_new_fmt(msg, test_ctx->ldb, "%s", dn);
568 assert_non_null(msg->dn);
570 for (i = 0; kvs[i].key != NULL; i++) {
572 ret = ldb_msg_add_empty(msg, kvs[i].key,
574 assert_int_equal(ret, 0);
578 ret = ldb_msg_add_string(msg, kvs[i].key, kvs[i].val);
579 assert_int_equal(ret, LDB_SUCCESS);
586 static void ldb_test_add_data(TALLOC_CTX *mem_ctx,
587 struct ldbtest_ctx *ldb_test_ctx,
592 struct ldb_message *msg;
593 struct ldb_result *result = NULL;
596 tmp_ctx = talloc_new(mem_ctx);
597 assert_non_null(tmp_ctx);
599 msg = build_mod_msg(tmp_ctx, ldb_test_ctx,
601 assert_non_null(msg);
603 ret = ldb_add(ldb_test_ctx->ldb, msg);
604 assert_int_equal(ret, LDB_SUCCESS);
606 ret = ldb_search(ldb_test_ctx->ldb, tmp_ctx, &result, msg->dn,
607 LDB_SCOPE_BASE, NULL, NULL);
608 assert_int_equal(ret, LDB_SUCCESS);
609 assert_non_null(result);
610 assert_int_equal(result->count, 1);
611 assert_string_equal(ldb_dn_get_linearized(result->msgs[0]->dn),
612 ldb_dn_get_linearized(msg->dn));
614 talloc_free(tmp_ctx);
617 static void ldb_test_remove_data(TALLOC_CTX *mem_ctx,
618 struct ldbtest_ctx *ldb_test_ctx,
622 struct ldb_dn *basedn;
626 tmp_ctx = talloc_new(mem_ctx);
627 assert_non_null(tmp_ctx);
629 basedn = ldb_dn_new_fmt(tmp_ctx, ldb_test_ctx->ldb,
631 assert_non_null(basedn);
633 ret = ldb_delete(ldb_test_ctx->ldb, basedn);
634 assert_true(ret == LDB_SUCCESS || ret == LDB_ERR_NO_SUCH_OBJECT);
636 count = base_search_count(ldb_test_ctx, ldb_dn_get_linearized(basedn));
637 assert_int_equal(count, 0);
639 talloc_free(tmp_ctx);
642 static void mod_test_add_data(struct ldb_mod_test_ctx *mod_test_ctx,
645 ldb_test_add_data(mod_test_ctx,
646 mod_test_ctx->ldb_test_ctx,
647 mod_test_ctx->entry_dn,
651 static void mod_test_remove_data(struct ldb_mod_test_ctx *mod_test_ctx)
653 ldb_test_remove_data(mod_test_ctx,
654 mod_test_ctx->ldb_test_ctx,
655 mod_test_ctx->entry_dn);
658 static struct ldb_result *run_mod_test(struct ldb_mod_test_ctx *mod_test_ctx,
663 struct ldb_result *res;
664 struct ldb_message *mod_msg;
665 struct ldb_dn *basedn;
666 struct ldbtest_ctx *ldb_test_ctx;
669 ldb_test_ctx = mod_test_ctx->ldb_test_ctx;
671 tmp_ctx = talloc_new(mod_test_ctx);
672 assert_non_null(tmp_ctx);
674 mod_msg = build_mod_msg(tmp_ctx, ldb_test_ctx, mod_test_ctx->entry_dn,
676 assert_non_null(mod_msg);
678 ret = ldb_modify(ldb_test_ctx->ldb, mod_msg);
679 assert_int_equal(ret, LDB_SUCCESS);
681 basedn = ldb_dn_new_fmt(tmp_ctx, ldb_test_ctx->ldb,
682 "%s", mod_test_ctx->entry_dn);
683 assert_non_null(basedn);
685 ret = ldb_search(ldb_test_ctx->ldb, mod_test_ctx, &res, basedn,
686 LDB_SCOPE_BASE, NULL, NULL);
687 assert_int_equal(ret, LDB_SUCCESS);
688 assert_non_null(res);
689 assert_int_equal(res->count, 1);
690 assert_string_equal(ldb_dn_get_linearized(res->msgs[0]->dn),
691 ldb_dn_get_linearized(mod_msg->dn));
693 talloc_free(tmp_ctx);
697 static int ldb_modify_test_setup(void **state)
699 struct ldbtest_ctx *ldb_test_ctx;
700 struct ldb_mod_test_ctx *mod_test_ctx;
701 struct keyval kvs[] = {
702 { "cn", "test_mod_cn" },
706 ldbtest_setup((void **) &ldb_test_ctx);
708 mod_test_ctx = talloc(ldb_test_ctx, struct ldb_mod_test_ctx);
709 assert_non_null(mod_test_ctx);
711 mod_test_ctx->entry_dn = "dc=mod_test_entry";
712 mod_test_ctx->ldb_test_ctx = ldb_test_ctx;
714 mod_test_remove_data(mod_test_ctx);
715 mod_test_add_data(mod_test_ctx, kvs);
716 *state = mod_test_ctx;
720 static int ldb_modify_test_teardown(void **state)
722 struct ldb_mod_test_ctx *mod_test_ctx = \
723 talloc_get_type_abort(*state,
724 struct ldb_mod_test_ctx);
725 struct ldbtest_ctx *ldb_test_ctx;
727 ldb_test_ctx = mod_test_ctx->ldb_test_ctx;
729 mod_test_remove_data(mod_test_ctx);
730 talloc_free(mod_test_ctx);
732 ldbtest_teardown((void **) &ldb_test_ctx);
736 static void test_ldb_modify_add_key(void **state)
738 struct ldb_mod_test_ctx *mod_test_ctx = \
739 talloc_get_type_abort(*state,
740 struct ldb_mod_test_ctx);
741 struct keyval mod_kvs[] = {
742 { "name", "test_mod_name" },
745 struct ldb_result *res;
746 struct ldb_message_element *el;
748 res = run_mod_test(mod_test_ctx, LDB_FLAG_MOD_ADD, mod_kvs);
749 assert_non_null(res);
751 /* Check cn is intact and name was added */
752 assert_int_equal(res->count, 1);
753 el = ldb_msg_find_element(res->msgs[0], "cn");
755 assert_int_equal(el->num_values, 1);
756 assert_string_equal(el->values[0].data, "test_mod_cn");
758 el = ldb_msg_find_element(res->msgs[0], "name");
760 assert_int_equal(el->num_values, 1);
761 assert_string_equal(el->values[0].data, "test_mod_name");
764 static void test_ldb_modify_extend_key(void **state)
766 struct ldb_mod_test_ctx *mod_test_ctx = \
767 talloc_get_type_abort(*state,
768 struct ldb_mod_test_ctx);
769 struct keyval mod_kvs[] = {
770 { "cn", "test_mod_cn2" },
773 struct ldb_result *res;
774 struct ldb_message_element *el;
776 res = run_mod_test(mod_test_ctx, LDB_FLAG_MOD_ADD, mod_kvs);
777 assert_non_null(res);
779 /* Check cn was extended with another value */
780 assert_int_equal(res->count, 1);
781 el = ldb_msg_find_element(res->msgs[0], "cn");
783 assert_int_equal(el->num_values, 2);
784 assert_string_equal(el->values[0].data, "test_mod_cn");
785 assert_string_equal(el->values[1].data, "test_mod_cn2");
788 static void test_ldb_modify_add_key_noval(void **state)
790 struct ldb_mod_test_ctx *mod_test_ctx = \
791 talloc_get_type_abort(*state,
792 struct ldb_mod_test_ctx);
793 struct ldb_message *mod_msg;
794 struct ldbtest_ctx *ldb_test_ctx;
795 struct ldb_message_element *el;
798 ldb_test_ctx = mod_test_ctx->ldb_test_ctx;
800 mod_msg = ldb_msg_new(mod_test_ctx);
801 assert_non_null(mod_msg);
803 mod_msg->dn = ldb_dn_new_fmt(mod_msg, ldb_test_ctx->ldb,
804 "%s", mod_test_ctx->entry_dn);
805 assert_non_null(mod_msg->dn);
807 el = talloc_zero(mod_msg, struct ldb_message_element);
808 el->flags = LDB_FLAG_MOD_ADD;
810 el->name = talloc_strdup(el, "cn");
811 assert_non_null(el->name);
813 mod_msg->elements = el;
814 mod_msg->num_elements = 1;
816 ret = ldb_modify(ldb_test_ctx->ldb, mod_msg);
817 assert_int_equal(ret, LDB_ERR_CONSTRAINT_VIOLATION);
820 static void test_ldb_modify_replace_key(void **state)
822 struct ldb_mod_test_ctx *mod_test_ctx = \
823 talloc_get_type_abort(*state,
824 struct ldb_mod_test_ctx);
825 const char *new_cn = "new_cn";
826 struct keyval mod_kvs[] = {
830 struct ldb_result *res;
831 struct ldb_message_element *el;
833 res = run_mod_test(mod_test_ctx, LDB_FLAG_MOD_REPLACE, mod_kvs);
834 assert_non_null(res);
836 /* Check cn was replaced */
837 assert_int_equal(res->count, 1);
838 el = ldb_msg_find_element(res->msgs[0], "cn");
840 assert_int_equal(el->num_values, 1);
841 assert_string_equal(el->values[0].data, new_cn);
844 static void test_ldb_modify_replace_noexist_key(void **state)
846 struct ldb_mod_test_ctx *mod_test_ctx = \
847 talloc_get_type_abort(*state,
848 struct ldb_mod_test_ctx);
849 struct keyval mod_kvs[] = {
850 { "name", "name_val" },
853 struct ldb_result *res;
854 struct ldb_message_element *el;
856 res = run_mod_test(mod_test_ctx, LDB_FLAG_MOD_REPLACE, mod_kvs);
857 assert_non_null(res);
859 /* Check cn is intact and name was added */
860 assert_int_equal(res->count, 1);
861 el = ldb_msg_find_element(res->msgs[0], "cn");
863 assert_int_equal(el->num_values, 1);
864 assert_string_equal(el->values[0].data, "test_mod_cn");
866 el = ldb_msg_find_element(res->msgs[0], mod_kvs[0].key);
868 assert_int_equal(el->num_values, 1);
869 assert_string_equal(el->values[0].data, mod_kvs[0].val);
872 static void test_ldb_modify_replace_zero_vals(void **state)
874 struct ldb_mod_test_ctx *mod_test_ctx = \
875 talloc_get_type_abort(*state,
876 struct ldb_mod_test_ctx);
877 struct ldb_message_element *el;
878 struct ldb_result *res;
879 struct keyval kvs[] = {
884 /* cn must be gone */
885 res = run_mod_test(mod_test_ctx, LDB_FLAG_MOD_REPLACE, kvs);
886 assert_non_null(res);
887 el = ldb_msg_find_element(res->msgs[0], "cn");
891 static void test_ldb_modify_replace_noexist_key_zero_vals(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 ldb_message_element *el;
897 struct ldb_result *res;
898 struct keyval kvs[] = {
899 { "noexist_key", NULL },
903 /* cn must be gone */
904 res = run_mod_test(mod_test_ctx, LDB_FLAG_MOD_REPLACE, kvs);
905 assert_non_null(res);
907 /* cn should be intact */
908 el = ldb_msg_find_element(res->msgs[0], "cn");
912 static void test_ldb_modify_del_key(void **state)
914 struct ldb_mod_test_ctx *mod_test_ctx = \
915 talloc_get_type_abort(*state,
916 struct ldb_mod_test_ctx);
917 struct ldb_message_element *el;
918 struct ldb_result *res;
919 struct keyval kvs[] = {
924 /* cn must be gone */
925 res = run_mod_test(mod_test_ctx, LDB_FLAG_MOD_DELETE, kvs);
926 assert_non_null(res);
928 el = ldb_msg_find_element(res->msgs[0], "cn");
932 static void test_ldb_modify_del_keyval(void **state)
934 struct ldb_mod_test_ctx *mod_test_ctx = \
935 talloc_get_type_abort(*state,
936 struct ldb_mod_test_ctx);
937 struct ldb_message_element *el;
938 struct ldb_result *res;
939 struct keyval kvs[] = {
940 { "cn", "test_mod_cn" },
944 /* cn must be gone */
945 res = run_mod_test(mod_test_ctx, LDB_FLAG_MOD_DELETE, kvs);
946 assert_non_null(res);
948 el = ldb_msg_find_element(res->msgs[0], "cn");
952 struct search_test_ctx {
953 struct ldbtest_ctx *ldb_test_ctx;
957 static char *get_full_dn(TALLOC_CTX *mem_ctx,
958 struct search_test_ctx *search_test_ctx,
963 full_dn = talloc_asprintf(mem_ctx,
964 "%s,%s", rdn, search_test_ctx->base_dn);
965 assert_non_null(full_dn);
970 static void search_test_add_data(struct search_test_ctx *search_test_ctx,
976 full_dn = get_full_dn(search_test_ctx, search_test_ctx, rdn);
978 ldb_test_add_data(search_test_ctx,
979 search_test_ctx->ldb_test_ctx,
984 static void search_test_remove_data(struct search_test_ctx *search_test_ctx,
989 full_dn = talloc_asprintf(search_test_ctx,
990 "%s,%s", rdn, search_test_ctx->base_dn);
991 assert_non_null(full_dn);
993 ldb_test_remove_data(search_test_ctx,
994 search_test_ctx->ldb_test_ctx,
998 static int ldb_search_test_setup(void **state)
1000 struct ldbtest_ctx *ldb_test_ctx;
1001 struct search_test_ctx *search_test_ctx;
1002 struct keyval kvs[] = {
1003 { "cn", "test_search_cn" },
1004 { "cn", "test_search_cn2" },
1005 { "uid", "test_search_uid" },
1006 { "uid", "test_search_uid2" },
1009 struct keyval kvs2[] = {
1010 { "cn", "test_search_2_cn" },
1011 { "cn", "test_search_2_cn2" },
1012 { "uid", "test_search_2_uid" },
1013 { "uid", "test_search_2_uid2" },
1017 ldbtest_setup((void **) &ldb_test_ctx);
1019 search_test_ctx = talloc(ldb_test_ctx, struct search_test_ctx);
1020 assert_non_null(search_test_ctx);
1022 search_test_ctx->base_dn = "dc=search_test_entry";
1023 search_test_ctx->ldb_test_ctx = ldb_test_ctx;
1025 search_test_remove_data(search_test_ctx, "cn=test_search_cn");
1026 search_test_add_data(search_test_ctx, "cn=test_search_cn", kvs);
1028 search_test_remove_data(search_test_ctx, "cn=test_search_2_cn");
1029 search_test_add_data(search_test_ctx, "cn=test_search_2_cn", kvs2);
1031 *state = search_test_ctx;
1035 static int ldb_search_test_teardown(void **state)
1037 struct search_test_ctx *search_test_ctx = talloc_get_type_abort(*state,
1038 struct search_test_ctx);
1039 struct ldbtest_ctx *ldb_test_ctx;
1041 ldb_test_ctx = search_test_ctx->ldb_test_ctx;
1043 search_test_remove_data(search_test_ctx, "cn=test_search_cn");
1044 search_test_remove_data(search_test_ctx, "cn=test_search_2_cn");
1045 ldbtest_teardown((void **) &ldb_test_ctx);
1049 static void assert_attr_has_vals(struct ldb_message *msg,
1054 struct ldb_message_element *el;
1057 el = ldb_msg_find_element(msg, attr);
1058 assert_non_null(el);
1060 assert_int_equal(el->num_values, nvals);
1061 for (i = 0; i < nvals; i++) {
1062 assert_string_equal(el->values[i].data,
1067 static void assert_has_no_attr(struct ldb_message *msg,
1070 struct ldb_message_element *el;
1072 el = ldb_msg_find_element(msg, attr);
1076 static bool has_dn(struct ldb_message *msg, const char *dn)
1080 msgdn = ldb_dn_get_linearized(msg->dn);
1081 if (strcmp(dn, msgdn) == 0) {
1088 static void test_search_match_none(void **state)
1090 struct search_test_ctx *search_test_ctx = talloc_get_type_abort(*state,
1091 struct search_test_ctx);
1094 count = base_search_count(search_test_ctx->ldb_test_ctx,
1095 "dc=no_such_entry");
1096 assert_int_equal(count, 0);
1099 static void test_search_match_one(void **state)
1101 struct search_test_ctx *search_test_ctx = talloc_get_type_abort(*state,
1102 struct search_test_ctx);
1104 struct ldb_dn *basedn;
1105 struct ldb_result *result = NULL;
1106 const char *cn_vals[] = { "test_search_cn",
1107 "test_search_cn2" };
1108 const char *uid_vals[] = { "test_search_uid",
1109 "test_search_uid2" };
1111 basedn = ldb_dn_new_fmt(search_test_ctx,
1112 search_test_ctx->ldb_test_ctx->ldb,
1114 search_test_ctx->base_dn);
1115 assert_non_null(basedn);
1117 ret = ldb_search(search_test_ctx->ldb_test_ctx->ldb,
1121 LDB_SCOPE_SUBTREE, NULL,
1122 "cn=test_search_cn");
1123 assert_int_equal(ret, 0);
1124 assert_non_null(result);
1125 assert_int_equal(result->count, 1);
1127 assert_attr_has_vals(result->msgs[0], "cn", cn_vals, 2);
1128 assert_attr_has_vals(result->msgs[0], "uid", uid_vals, 2);
1131 static void test_search_match_filter(void **state)
1133 struct search_test_ctx *search_test_ctx = talloc_get_type_abort(*state,
1134 struct search_test_ctx);
1136 struct ldb_dn *basedn;
1137 struct ldb_result *result = NULL;
1138 const char *cn_vals[] = { "test_search_cn",
1139 "test_search_cn2" };
1140 const char *attrs[] = { "cn", NULL };
1142 basedn = ldb_dn_new_fmt(search_test_ctx,
1143 search_test_ctx->ldb_test_ctx->ldb,
1145 search_test_ctx->base_dn);
1146 assert_non_null(basedn);
1148 ret = ldb_search(search_test_ctx->ldb_test_ctx->ldb,
1154 "cn=test_search_cn");
1155 assert_int_equal(ret, 0);
1156 assert_non_null(result);
1157 assert_int_equal(result->count, 1);
1159 assert_attr_has_vals(result->msgs[0], "cn", cn_vals, 2);
1160 assert_has_no_attr(result->msgs[0], "uid");
1163 static void assert_expected(struct search_test_ctx *search_test_ctx,
1164 struct ldb_message *msg)
1168 const char *cn_vals[] = { "test_search_cn",
1169 "test_search_cn2" };
1170 const char *uid_vals[] = { "test_search_uid",
1171 "test_search_uid2" };
1172 const char *cn2_vals[] = { "test_search_2_cn",
1173 "test_search_2_cn2" };
1174 const char *uid2_vals[] = { "test_search_2_uid",
1175 "test_search_2_uid2" };
1177 full_dn1 = get_full_dn(search_test_ctx,
1179 "cn=test_search_cn");
1181 full_dn2 = get_full_dn(search_test_ctx,
1183 "cn=test_search_2_cn");
1185 if (has_dn(msg, full_dn1) == true) {
1186 assert_attr_has_vals(msg, "cn", cn_vals, 2);
1187 assert_attr_has_vals(msg, "uid", uid_vals, 2);
1188 } else if (has_dn(msg, full_dn2) == true) {
1189 assert_attr_has_vals(msg, "cn", cn2_vals, 2);
1190 assert_attr_has_vals(msg, "uid", uid2_vals, 2);
1196 static void test_search_match_both(void **state)
1198 struct search_test_ctx *search_test_ctx = talloc_get_type_abort(*state,
1199 struct search_test_ctx);
1201 struct ldb_dn *basedn;
1202 struct ldb_result *result = NULL;
1204 basedn = ldb_dn_new_fmt(search_test_ctx,
1205 search_test_ctx->ldb_test_ctx->ldb,
1207 search_test_ctx->base_dn);
1208 assert_non_null(basedn);
1210 ret = ldb_search(search_test_ctx->ldb_test_ctx->ldb,
1214 LDB_SCOPE_SUBTREE, NULL,
1215 "cn=test_search_*");
1216 assert_int_equal(ret, 0);
1217 assert_non_null(result);
1218 assert_int_equal(result->count, 2);
1220 assert_expected(search_test_ctx, result->msgs[0]);
1221 assert_expected(search_test_ctx, result->msgs[1]);
1224 static void test_search_match_basedn(void **state)
1226 struct search_test_ctx *search_test_ctx = talloc_get_type_abort(*state,
1227 struct search_test_ctx);
1229 struct ldb_dn *basedn;
1230 struct ldb_result *result = NULL;
1231 struct ldb_message *msg;
1233 basedn = ldb_dn_new_fmt(search_test_ctx,
1234 search_test_ctx->ldb_test_ctx->ldb,
1236 assert_non_null(basedn);
1238 ret = ldb_search(search_test_ctx->ldb_test_ctx->ldb,
1242 LDB_SCOPE_SUBTREE, NULL,
1244 assert_int_equal(ret, 0);
1246 /* Add 'checkBaseOnSearch' to @OPTIONS */
1247 msg = ldb_msg_new(search_test_ctx);
1248 assert_non_null(msg);
1250 msg->dn = ldb_dn_new_fmt(msg,
1251 search_test_ctx->ldb_test_ctx->ldb,
1253 assert_non_null(msg->dn);
1255 ret = ldb_msg_add_string(msg, "checkBaseOnSearch", "TRUE");
1256 assert_int_equal(ret, 0);
1258 ret = ldb_add(search_test_ctx->ldb_test_ctx->ldb, msg);
1259 assert_int_equal(ret, 0);
1262 /* The search should return LDB_ERR_NO_SUCH_OBJECT */
1263 ret = ldb_search(search_test_ctx->ldb_test_ctx->ldb,
1267 LDB_SCOPE_SUBTREE, NULL,
1269 assert_int_equal(ret, LDB_ERR_NO_SUCH_OBJECT);
1271 ret = ldb_delete(search_test_ctx->ldb_test_ctx->ldb, msg->dn);
1272 assert_int_equal(ret, 0);
1277 * This test is complex.
1278 * The purpose is to test for a deadlock detected between ldb_search()
1279 * and ldb_transaction_commit(). The deadlock happens if in process
1281 * - (1) the all-record lock is taken in ltdb_search()
1282 * - (2) the ldb_transaction_start() call is made
1283 * - (1) an un-indexed search starts (forced here by doing it in
1285 * - (2) the ldb_transaction_commit() is called.
1286 * This returns LDB_ERR_BUSY if the deadlock is detected
1288 * With ldb 1.1.29 and tdb 1.3.12 we avoid this only due to a missing
1289 * lock call in ltdb_search() due to a refcounting bug in
1293 struct search_against_transaction_ctx {
1294 struct ldbtest_ctx *test_ctx;
1297 struct ldb_dn *basedn;
1300 static int test_ldb_search_against_transaction_callback2(struct ldb_request *req,
1301 struct ldb_reply *ares)
1303 struct search_against_transaction_ctx *ctx = req->context;
1304 switch (ares->type) {
1305 case LDB_REPLY_ENTRY:
1307 if (ctx->res_count != 1) {
1313 case LDB_REPLY_REFERRAL:
1316 case LDB_REPLY_DONE:
1317 return ldb_request_done(req, LDB_SUCCESS);
1325 * This purpose of this callback is to trigger a transaction in
1326 * the child process while the all-record lock is held, but before
1327 * we take any locks in the tdb_traverse_read() handler.
1329 * In tdb 1.3.12 tdb_traverse_read() take the read transaction lock
1330 * however in ldb 1.1.29 ltdb_search() forgets to take the all-record
1331 * lock (except the very first time) due to a ref-counting bug.
1335 static int test_ldb_search_against_transaction_callback1(struct ldb_request *req,
1336 struct ldb_reply *ares)
1341 struct search_against_transaction_ctx *ctx = req->context;
1342 switch (ares->type) {
1343 case LDB_REPLY_ENTRY:
1346 case LDB_REPLY_REFERRAL:
1349 case LDB_REPLY_DONE:
1350 return ldb_request_done(req, LDB_SUCCESS);
1354 assert_int_equal(ret, 0);
1356 ctx->child_pid = fork();
1357 if (ctx->child_pid == 0) {
1358 TALLOC_CTX *tmp_ctx = NULL;
1359 struct ldb_message *msg;
1360 TALLOC_FREE(ctx->test_ctx->ldb);
1361 TALLOC_FREE(ctx->test_ctx->ev);
1362 ctx->test_ctx->ev = tevent_context_init(ctx->test_ctx);
1363 if (ctx->test_ctx->ev == NULL) {
1364 exit(LDB_ERR_OPERATIONS_ERROR);
1367 ctx->test_ctx->ldb = ldb_init(ctx->test_ctx,
1369 if (ctx->test_ctx->ldb == NULL) {
1370 exit(LDB_ERR_OPERATIONS_ERROR);
1373 ret = ldb_connect(ctx->test_ctx->ldb,
1374 ctx->test_ctx->dbpath, 0, NULL);
1375 if (ret != LDB_SUCCESS) {
1379 tmp_ctx = talloc_new(ctx->test_ctx);
1380 if (tmp_ctx == NULL) {
1381 exit(LDB_ERR_OPERATIONS_ERROR);
1384 msg = ldb_msg_new(tmp_ctx);
1386 exit(LDB_ERR_OPERATIONS_ERROR);
1389 msg->dn = ldb_dn_new_fmt(msg, ctx->test_ctx->ldb,
1391 if (msg->dn == NULL) {
1392 exit(LDB_ERR_OPERATIONS_ERROR);
1395 ret = ldb_msg_add_string(msg, "cn", "test_cn_val");
1397 exit(LDB_ERR_OPERATIONS_ERROR);
1400 ret = ldb_transaction_start(ctx->test_ctx->ldb);
1405 ret = write(pipes[1], "GO", 2);
1407 exit(LDB_ERR_OPERATIONS_ERROR);
1410 ret = ldb_add(ctx->test_ctx->ldb, msg);
1415 ret = ldb_transaction_commit(ctx->test_ctx->ldb);
1419 ret = read(pipes[0], buf, 2);
1420 assert_int_equal(ret, 2);
1422 /* This search must be unindexed (ie traverse in tdb) */
1423 ret = ldb_build_search_req(&req,
1431 test_ldb_search_against_transaction_callback2,
1434 * we don't assert on these return codes until after the search is
1435 * finished, or the clean up will fail because we hold locks.
1438 ret2 = ldb_request(ctx->test_ctx->ldb, req);
1440 if (ret2 == LDB_SUCCESS) {
1441 ret2 = ldb_wait(req->handle, LDB_WAIT_ALL);
1443 assert_int_equal(ret, 0);
1444 assert_int_equal(ret2, 0);
1445 assert_int_equal(ctx->res_count, 2);
1450 static void test_ldb_search_against_transaction(void **state)
1452 struct search_test_ctx *search_test_ctx = talloc_get_type_abort(*state,
1453 struct search_test_ctx);
1454 struct search_against_transaction_ctx
1457 .test_ctx = search_test_ctx->ldb_test_ctx
1461 struct ldb_request *req;
1464 struct ldb_dn *base_search_dn;
1466 tevent_loop_allow_nesting(search_test_ctx->ldb_test_ctx->ev);
1469 = ldb_dn_new_fmt(search_test_ctx,
1470 search_test_ctx->ldb_test_ctx->ldb,
1471 "cn=test_search_cn,%s",
1472 search_test_ctx->base_dn);
1473 assert_non_null(base_search_dn);
1476 = ldb_dn_new_fmt(search_test_ctx,
1477 search_test_ctx->ldb_test_ctx->ldb,
1479 search_test_ctx->base_dn);
1480 assert_non_null(ctx.basedn);
1483 /* This search must be indexed (ie no traverse in tdb) */
1484 ret = ldb_build_search_req(&req,
1485 search_test_ctx->ldb_test_ctx->ldb,
1492 test_ldb_search_against_transaction_callback1,
1494 assert_int_equal(ret, 0);
1495 ret = ldb_request(search_test_ctx->ldb_test_ctx->ldb, req);
1497 if (ret == LDB_SUCCESS) {
1498 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
1500 assert_int_equal(ret, 0);
1501 assert_int_equal(ctx.res_count, 2);
1503 pid = waitpid(ctx.child_pid, &wstatus, 0);
1504 assert_int_equal(pid, ctx.child_pid);
1506 assert_true(WIFEXITED(wstatus));
1508 assert_int_equal(WEXITSTATUS(wstatus), 0);
1513 static int ldb_case_test_setup(void **state)
1516 struct ldb_ldif *ldif;
1517 struct ldbtest_ctx *ldb_test_ctx;
1518 const char *attrs_ldif = \
1520 "cn: CASE_INSENSITIVE\n"
1522 struct keyval kvs[] = {
1523 { "cn", "CaseInsensitiveValue" },
1524 { "uid", "CaseSensitiveValue" },
1529 ldbtest_setup((void **) &ldb_test_ctx);
1531 while ((ldif = ldb_ldif_read_string(ldb_test_ctx->ldb, &attrs_ldif))) {
1532 ret = ldb_add(ldb_test_ctx->ldb, ldif->msg);
1533 assert_int_equal(ret, LDB_SUCCESS);
1536 ldb_test_add_data(ldb_test_ctx,
1538 "cn=CaseInsensitiveValue",
1541 *state = ldb_test_ctx;
1545 static int ldb_case_test_teardown(void **state)
1548 struct ldbtest_ctx *ldb_test_ctx = talloc_get_type_abort(*state,
1549 struct ldbtest_ctx);
1551 struct ldb_dn *del_dn;
1553 del_dn = ldb_dn_new_fmt(ldb_test_ctx,
1556 assert_non_null(del_dn);
1558 ret = ldb_delete(ldb_test_ctx->ldb, del_dn);
1559 assert_int_equal(ret, LDB_SUCCESS);
1561 assert_dn_doesnt_exist(ldb_test_ctx,
1564 ldb_test_remove_data(ldb_test_ctx, ldb_test_ctx,
1565 "cn=CaseInsensitiveValue");
1567 ldbtest_teardown((void **) &ldb_test_ctx);
1571 static void test_ldb_attrs_case_insensitive(void **state)
1574 struct ldbtest_ctx *ldb_test_ctx = talloc_get_type_abort(*state,
1575 struct ldbtest_ctx);
1577 /* cn matches exact case */
1578 cnt = sub_search_count(ldb_test_ctx, "", "cn=CaseInsensitiveValue");
1579 assert_int_equal(cnt, 1);
1581 /* cn matches lower case */
1582 cnt = sub_search_count(ldb_test_ctx, "", "cn=caseinsensitivevalue");
1583 assert_int_equal(cnt, 1);
1585 /* uid matches exact case */
1586 cnt = sub_search_count(ldb_test_ctx, "", "uid=CaseSensitiveValue");
1587 assert_int_equal(cnt, 1);
1589 /* uid does not match lower case */
1590 cnt = sub_search_count(ldb_test_ctx, "", "uid=casesensitivevalue");
1591 assert_int_equal(cnt, 0);
1594 static struct ldb_schema_attribute cn_attr_1;
1595 static struct ldb_schema_attribute cn_attr_2;
1596 static struct ldb_schema_attribute default_attr;
1599 override the name to attribute handler function
1601 static const struct ldb_schema_attribute *ldb_test_attribute_handler_override(struct ldb_context *ldb,
1605 if (private_data != NULL && ldb_attr_cmp(name, "cn") == 0) {
1607 } else if (private_data == NULL && ldb_attr_cmp(name, "cn") == 0) {
1609 } else if (ldb_attr_cmp(name, "uid") == 0) {
1612 return &default_attr;
1615 static void test_ldb_attrs_case_handler(void **state)
1619 const struct ldb_schema_syntax *syntax;
1621 struct ldbtest_ctx *ldb_test_ctx = talloc_get_type_abort(*state,
1622 struct ldbtest_ctx);
1623 struct ldb_context *ldb = ldb_test_ctx->ldb;
1625 /* cn matches lower case */
1626 cnt = sub_search_count(ldb_test_ctx, "", "cn=caseinsensitivevalue");
1627 assert_int_equal(cnt, 1);
1629 syntax = ldb_standard_syntax_by_name(ldb, LDB_SYNTAX_OCTET_STRING);
1630 assert_non_null(syntax);
1632 ret = ldb_schema_attribute_fill_with_syntax(ldb, ldb,
1634 syntax, &default_attr);
1635 assert_int_equal(ret, LDB_SUCCESS);
1637 syntax = ldb_standard_syntax_by_name(ldb, LDB_SYNTAX_OCTET_STRING);
1638 assert_non_null(syntax);
1640 ret = ldb_schema_attribute_fill_with_syntax(ldb, ldb,
1642 syntax, &cn_attr_1);
1643 assert_int_equal(ret, LDB_SUCCESS);
1646 * Set an attribute handler, which will fail to match as we
1647 * force case sensitive
1649 ldb_schema_attribute_set_override_handler(ldb,
1650 ldb_test_attribute_handler_override,
1653 /* cn does not matche lower case */
1654 cnt = sub_search_count(ldb_test_ctx, "", "cn=caseinsensitivevalue");
1655 assert_int_equal(cnt, 0);
1657 syntax = ldb_standard_syntax_by_name(ldb, LDB_SYNTAX_DIRECTORY_STRING);
1658 assert_non_null(syntax);
1660 ret = ldb_schema_attribute_fill_with_syntax(ldb, ldb,
1662 syntax, &cn_attr_2);
1663 assert_int_equal(ret, LDB_SUCCESS);
1666 * Set an attribute handler, which will match as we
1667 * force case insensitive
1669 ldb_schema_attribute_set_override_handler(ldb,
1670 ldb_test_attribute_handler_override,
1673 /* cn matches lower case */
1674 cnt = sub_search_count(ldb_test_ctx, "", "cn=caseinsensitivevalue");
1675 assert_int_equal(cnt, 1);
1680 static void test_ldb_attrs_index_handler(void **state)
1684 const struct ldb_schema_syntax *syntax;
1685 struct ldb_ldif *ldif;
1687 const char *index_ldif = \
1692 struct ldbtest_ctx *ldb_test_ctx = talloc_get_type_abort(*state,
1693 struct ldbtest_ctx);
1694 struct ldb_context *ldb = ldb_test_ctx->ldb;
1696 /* cn matches lower case */
1697 cnt = sub_search_count(ldb_test_ctx, "", "cn=caseinsensitivevalue");
1698 assert_int_equal(cnt, 1);
1700 syntax = ldb_standard_syntax_by_name(ldb, LDB_SYNTAX_OCTET_STRING);
1701 assert_non_null(syntax);
1703 ret = ldb_schema_attribute_fill_with_syntax(ldb, ldb,
1705 syntax, &cn_attr_1);
1706 assert_int_equal(ret, LDB_SUCCESS);
1708 syntax = ldb_standard_syntax_by_name(ldb, LDB_SYNTAX_DIRECTORY_STRING);
1709 assert_non_null(syntax);
1711 ret = ldb_schema_attribute_fill_with_syntax(ldb, ldb,
1712 "cn", LDB_ATTR_FLAG_INDEXED,
1713 syntax, &cn_attr_2);
1714 assert_int_equal(ret, LDB_SUCCESS);
1717 * Set an attribute handler
1719 ldb_schema_attribute_set_override_handler(ldb,
1720 ldb_test_attribute_handler_override,
1723 /* cn matches lower case */
1724 cnt = sub_search_count(ldb_test_ctx, "", "cn=caseinsensitivevalue");
1725 assert_int_equal(cnt, 1);
1727 /* Add the index (actually any modify will do) */
1728 while ((ldif = ldb_ldif_read_string(ldb_test_ctx->ldb, &index_ldif))) {
1729 ret = ldb_add(ldb_test_ctx->ldb, ldif->msg);
1730 assert_int_equal(ret, LDB_SUCCESS);
1733 ldb_schema_set_override_indexlist(ldb, false);
1735 /* cn does match as there is an index now */
1736 cnt = sub_search_count(ldb_test_ctx, "", "cn=caseinsensitivevalue");
1737 assert_int_equal(cnt, 1);
1740 * Set an attribute handler, which will later fail to match as we
1741 * didn't re-index the DB
1743 ldb_schema_attribute_set_override_handler(ldb,
1744 ldb_test_attribute_handler_override,
1748 * cn does not match as we changed the case sensitivity, but
1751 * This shows that the override is in control
1753 cnt = sub_search_count(ldb_test_ctx, "", "cn=caseinsensitivevalue");
1754 assert_int_equal(cnt, 0);
1758 static int ldb_case_attrs_index_test_teardown(void **state)
1761 struct ldbtest_ctx *ldb_test_ctx = talloc_get_type_abort(*state,
1762 struct ldbtest_ctx);
1763 struct ldb_dn *del_dn;
1765 del_dn = ldb_dn_new_fmt(ldb_test_ctx,
1768 assert_non_null(del_dn);
1770 ret = ldb_delete(ldb_test_ctx->ldb, del_dn);
1771 if (ret != LDB_ERR_NO_SUCH_OBJECT) {
1772 assert_int_equal(ret, LDB_SUCCESS);
1775 assert_dn_doesnt_exist(ldb_test_ctx,
1778 ldb_case_test_teardown(state);
1783 struct rename_test_ctx {
1784 struct ldbtest_ctx *ldb_test_ctx;
1786 struct ldb_dn *basedn;
1787 const char *str_basedn;
1789 const char *teardown_dn;
1792 static int ldb_rename_test_setup(void **state)
1794 struct ldbtest_ctx *ldb_test_ctx;
1795 struct rename_test_ctx *rename_test_ctx;
1796 const char *strdn = "dc=rename_test_entry_from";
1798 ldbtest_setup((void **) &ldb_test_ctx);
1800 rename_test_ctx = talloc(ldb_test_ctx, struct rename_test_ctx);
1801 assert_non_null(rename_test_ctx);
1802 rename_test_ctx->ldb_test_ctx = ldb_test_ctx;
1803 assert_non_null(rename_test_ctx->ldb_test_ctx);
1805 rename_test_ctx->basedn = ldb_dn_new_fmt(rename_test_ctx,
1806 rename_test_ctx->ldb_test_ctx->ldb,
1808 assert_non_null(rename_test_ctx->basedn);
1810 rename_test_ctx->str_basedn = strdn;
1811 rename_test_ctx->teardown_dn = strdn;
1813 add_dn_with_cn(ldb_test_ctx,
1814 rename_test_ctx->basedn,
1815 "test_rename_cn_val");
1817 *state = rename_test_ctx;
1821 static int ldb_rename_test_teardown(void **state)
1824 struct rename_test_ctx *rename_test_ctx = talloc_get_type_abort(*state,
1825 struct rename_test_ctx);
1826 struct ldbtest_ctx *ldb_test_ctx;
1827 struct ldb_dn *del_dn;
1829 ldb_test_ctx = rename_test_ctx->ldb_test_ctx;
1831 del_dn = ldb_dn_new_fmt(rename_test_ctx,
1832 rename_test_ctx->ldb_test_ctx->ldb,
1833 "%s", rename_test_ctx->teardown_dn);
1834 assert_non_null(del_dn);
1836 ret = ldb_delete(ldb_test_ctx->ldb, del_dn);
1837 assert_int_equal(ret, LDB_SUCCESS);
1839 assert_dn_doesnt_exist(ldb_test_ctx,
1840 rename_test_ctx->teardown_dn);
1842 ldbtest_teardown((void **) &ldb_test_ctx);
1846 static void test_ldb_rename(void **state)
1848 struct rename_test_ctx *rename_test_ctx =
1849 talloc_get_type_abort(*state, struct rename_test_ctx);
1851 const char *str_new_dn = "dc=rename_test_entry_to";
1852 struct ldb_dn *new_dn;
1854 new_dn = ldb_dn_new_fmt(rename_test_ctx,
1855 rename_test_ctx->ldb_test_ctx->ldb,
1857 assert_non_null(new_dn);
1859 ret = ldb_rename(rename_test_ctx->ldb_test_ctx->ldb,
1860 rename_test_ctx->basedn,
1862 assert_int_equal(ret, LDB_SUCCESS);
1864 assert_dn_exists(rename_test_ctx->ldb_test_ctx, str_new_dn);
1865 assert_dn_doesnt_exist(rename_test_ctx->ldb_test_ctx,
1866 rename_test_ctx->str_basedn);
1867 rename_test_ctx->teardown_dn = str_new_dn;
1869 /* FIXME - test the values which didn't change */
1872 static void test_ldb_rename_from_doesnt_exist(void **state)
1874 struct rename_test_ctx *rename_test_ctx = talloc_get_type_abort(
1876 struct rename_test_ctx);
1878 const char *str_new_dn = "dc=rename_test_entry_to";
1879 const char *str_bad_old_dn = "dc=rename_test_no_such_entry";
1880 struct ldb_dn *new_dn;
1881 struct ldb_dn *bad_old_dn;
1883 new_dn = ldb_dn_new_fmt(rename_test_ctx,
1884 rename_test_ctx->ldb_test_ctx->ldb,
1886 assert_non_null(new_dn);
1888 bad_old_dn = ldb_dn_new_fmt(rename_test_ctx,
1889 rename_test_ctx->ldb_test_ctx->ldb,
1890 "%s", str_bad_old_dn);
1891 assert_non_null(bad_old_dn);
1893 assert_dn_doesnt_exist(rename_test_ctx->ldb_test_ctx,
1896 ret = ldb_rename(rename_test_ctx->ldb_test_ctx->ldb,
1897 bad_old_dn, new_dn);
1898 assert_int_equal(ret, LDB_ERR_NO_SUCH_OBJECT);
1900 assert_dn_doesnt_exist(rename_test_ctx->ldb_test_ctx,
1904 static void test_ldb_rename_to_exists(void **state)
1906 struct rename_test_ctx *rename_test_ctx = talloc_get_type_abort(
1908 struct rename_test_ctx);
1910 const char *str_new_dn = "dc=rename_test_already_exists";
1911 struct ldb_dn *new_dn;
1913 new_dn = ldb_dn_new_fmt(rename_test_ctx,
1914 rename_test_ctx->ldb_test_ctx->ldb,
1916 assert_non_null(new_dn);
1918 add_dn_with_cn(rename_test_ctx->ldb_test_ctx,
1920 "test_rename_cn_val");
1922 ret = ldb_rename(rename_test_ctx->ldb_test_ctx->ldb,
1923 rename_test_ctx->basedn,
1925 assert_int_equal(ret, LDB_ERR_ENTRY_ALREADY_EXISTS);
1927 /* Old object must still exist */
1928 assert_dn_exists(rename_test_ctx->ldb_test_ctx,
1929 rename_test_ctx->str_basedn);
1931 ret = ldb_delete(rename_test_ctx->ldb_test_ctx->ldb,
1933 assert_int_equal(ret, LDB_SUCCESS);
1935 assert_dn_exists(rename_test_ctx->ldb_test_ctx,
1936 rename_test_ctx->teardown_dn);
1939 static void test_ldb_rename_self(void **state)
1941 struct rename_test_ctx *rename_test_ctx = talloc_get_type_abort(
1943 struct rename_test_ctx);
1946 /* Oddly enough, this is a success in ldb.. */
1947 ret = ldb_rename(rename_test_ctx->ldb_test_ctx->ldb,
1948 rename_test_ctx->basedn,
1949 rename_test_ctx->basedn);
1950 assert_int_equal(ret, LDB_SUCCESS);
1952 /* Old object must still exist */
1953 assert_dn_exists(rename_test_ctx->ldb_test_ctx,
1954 rename_test_ctx->str_basedn);
1957 static void test_ldb_rename_dn_case_change(void **state)
1959 struct rename_test_ctx *rename_test_ctx = talloc_get_type_abort(
1961 struct rename_test_ctx);
1964 struct ldb_dn *new_dn;
1967 str_new_dn = talloc_strdup(rename_test_ctx, rename_test_ctx->str_basedn);
1968 assert_non_null(str_new_dn);
1969 for (i = 0; str_new_dn[i]; i++) {
1970 str_new_dn[i] = toupper(str_new_dn[i]);
1973 new_dn = ldb_dn_new_fmt(rename_test_ctx,
1974 rename_test_ctx->ldb_test_ctx->ldb,
1976 assert_non_null(new_dn);
1978 ret = ldb_rename(rename_test_ctx->ldb_test_ctx->ldb,
1979 rename_test_ctx->basedn,
1981 assert_int_equal(ret, LDB_SUCCESS);
1983 /* DNs are case insensitive, so both searches will match */
1984 assert_dn_exists(rename_test_ctx->ldb_test_ctx, str_new_dn);
1985 assert_dn_exists(rename_test_ctx->ldb_test_ctx,
1986 rename_test_ctx->str_basedn);
1987 /* FIXME - test the values didn't change */
1990 int main(int argc, const char **argv)
1992 const struct CMUnitTest tests[] = {
1993 cmocka_unit_test_setup_teardown(test_connect,
1994 ldbtest_noconn_setup,
1995 ldbtest_noconn_teardown),
1996 cmocka_unit_test_setup_teardown(test_ldb_add,
1999 cmocka_unit_test_setup_teardown(test_ldb_search,
2002 cmocka_unit_test_setup_teardown(test_ldb_del,
2005 cmocka_unit_test_setup_teardown(test_ldb_del_noexist,
2008 cmocka_unit_test_setup_teardown(test_ldb_handle,
2011 cmocka_unit_test_setup_teardown(test_ldb_build_search_req,
2014 cmocka_unit_test_setup_teardown(test_transactions,
2017 cmocka_unit_test_setup_teardown(test_ldb_modify_add_key,
2018 ldb_modify_test_setup,
2019 ldb_modify_test_teardown),
2020 cmocka_unit_test_setup_teardown(test_ldb_modify_extend_key,
2021 ldb_modify_test_setup,
2022 ldb_modify_test_teardown),
2023 cmocka_unit_test_setup_teardown(test_ldb_modify_add_key_noval,
2024 ldb_modify_test_setup,
2025 ldb_modify_test_teardown),
2026 cmocka_unit_test_setup_teardown(test_ldb_modify_replace_key,
2027 ldb_modify_test_setup,
2028 ldb_modify_test_teardown),
2029 cmocka_unit_test_setup_teardown(test_ldb_modify_replace_noexist_key,
2030 ldb_modify_test_setup,
2031 ldb_modify_test_teardown),
2032 cmocka_unit_test_setup_teardown(test_ldb_modify_replace_zero_vals,
2033 ldb_modify_test_setup,
2034 ldb_modify_test_teardown),
2035 cmocka_unit_test_setup_teardown(test_ldb_modify_replace_noexist_key_zero_vals,
2036 ldb_modify_test_setup,
2037 ldb_modify_test_teardown),
2038 cmocka_unit_test_setup_teardown(test_ldb_modify_del_key,
2039 ldb_modify_test_setup,
2040 ldb_modify_test_teardown),
2041 cmocka_unit_test_setup_teardown(test_ldb_modify_del_keyval,
2042 ldb_modify_test_setup,
2043 ldb_modify_test_teardown),
2044 cmocka_unit_test_setup_teardown(test_search_match_none,
2045 ldb_search_test_setup,
2046 ldb_search_test_teardown),
2047 cmocka_unit_test_setup_teardown(test_search_match_one,
2048 ldb_search_test_setup,
2049 ldb_search_test_teardown),
2050 cmocka_unit_test_setup_teardown(test_search_match_filter,
2051 ldb_search_test_setup,
2052 ldb_search_test_teardown),
2053 cmocka_unit_test_setup_teardown(test_search_match_both,
2054 ldb_search_test_setup,
2055 ldb_search_test_teardown),
2056 cmocka_unit_test_setup_teardown(test_search_match_basedn,
2057 ldb_search_test_setup,
2058 ldb_search_test_teardown),
2059 cmocka_unit_test_setup_teardown(test_ldb_search_against_transaction,
2060 ldb_search_test_setup,
2061 ldb_search_test_teardown),
2062 cmocka_unit_test_setup_teardown(test_ldb_attrs_case_insensitive,
2063 ldb_case_test_setup,
2064 ldb_case_test_teardown),
2065 cmocka_unit_test_setup_teardown(test_ldb_attrs_case_handler,
2066 ldb_case_test_setup,
2067 ldb_case_test_teardown),
2068 cmocka_unit_test_setup_teardown(test_ldb_attrs_index_handler,
2069 ldb_case_test_setup,
2070 ldb_case_attrs_index_test_teardown),
2071 cmocka_unit_test_setup_teardown(test_ldb_rename,
2072 ldb_rename_test_setup,
2073 ldb_rename_test_teardown),
2074 cmocka_unit_test_setup_teardown(test_ldb_rename_from_doesnt_exist,
2075 ldb_rename_test_setup,
2076 ldb_rename_test_teardown),
2077 cmocka_unit_test_setup_teardown(test_ldb_rename_to_exists,
2078 ldb_rename_test_setup,
2079 ldb_rename_test_teardown),
2080 cmocka_unit_test_setup_teardown(test_ldb_rename_self,
2081 ldb_rename_test_setup,
2082 ldb_rename_test_teardown),
2083 cmocka_unit_test_setup_teardown(test_ldb_rename_dn_case_change,
2084 ldb_rename_test_setup,
2085 ldb_rename_test_teardown),
2088 return cmocka_run_group_tests(tests, NULL, NULL);