ldb: Lock the whole backend database for the duration of a search
[Samba.git] / lib / ldb / tests / ldb_mod_op_test.c
blob6357f83dd024a4f363782f9b24af0361494afce3
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 <setjmp.h>
17 #include <cmocka.h>
19 #include <errno.h>
20 #include <unistd.h>
21 #include <talloc.h>
23 #define TEVENT_DEPRECATED 1
24 #include <tevent.h>
26 #include <ldb.h>
27 #include <ldb_module.h>
28 #include <ldb_private.h>
29 #include <string.h>
30 #include <ctype.h>
32 #include <sys/wait.h>
35 #define DEFAULT_BE "tdb"
37 #ifndef TEST_BE
38 #define TEST_BE DEFAULT_BE
39 #endif /* TEST_BE */
41 struct ldbtest_ctx {
42 struct tevent_context *ev;
43 struct ldb_context *ldb;
45 const char *dbfile;
46 const char *lockfile; /* lockfile is separate */
48 const char *dbpath;
51 static void unlink_old_db(struct ldbtest_ctx *test_ctx)
53 int ret;
55 errno = 0;
56 ret = unlink(test_ctx->lockfile);
57 if (ret == -1 && errno != ENOENT) {
58 fail();
61 errno = 0;
62 ret = unlink(test_ctx->dbfile);
63 if (ret == -1 && errno != ENOENT) {
64 fail();
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",
85 test_ctx->dbfile);
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);
93 *state = test_ctx;
94 return 0;
97 static int ldbtest_noconn_teardown(void **state)
99 struct ldbtest_ctx *test_ctx = talloc_get_type_abort(*state,
100 struct ldbtest_ctx);
102 unlink_old_db(test_ctx);
103 talloc_free(test_ctx);
104 return 0;
107 static void test_connect(void **state)
109 struct ldbtest_ctx *test_ctx = talloc_get_type_abort(*state,
110 struct ldbtest_ctx);
111 int ret;
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;
120 int ret;
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);
127 *state = test_ctx;
128 return 0;
131 static int ldbtest_teardown(void **state)
133 struct ldbtest_ctx *test_ctx = talloc_get_type_abort(*state,
134 struct ldbtest_ctx);
135 ldbtest_noconn_teardown((void **) &test_ctx);
136 return 0;
139 static void test_ldb_add(void **state)
141 int ret;
142 struct ldb_message *msg;
143 struct ldbtest_ctx *test_ctx = talloc_get_type_abort(*state,
144 struct ldbtest_ctx);
145 TALLOC_CTX *tmp_ctx;
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)
167 int ret;
168 struct ldb_message *msg;
169 struct ldbtest_ctx *test_ctx = talloc_get_type_abort(*state,
170 struct ldbtest_ctx);
171 TALLOC_CTX *tmp_ctx;
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);
191 msg->dn = basedn;
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);
206 msg->dn = basedn2;
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)
236 TALLOC_CTX *tmp_ctx;
237 struct ldb_dn *basedn;
238 struct ldb_result *result = NULL;
239 int ret;
240 int count;
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);
255 return count;
258 static int sub_search_count(struct ldbtest_ctx *test_ctx,
259 const char *base_dn,
260 const char *filter)
262 TALLOC_CTX *tmp_ctx;
263 struct ldb_dn *basedn;
264 struct ldb_result *result = NULL;
265 int ret;
266 int count;
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);
281 return count;
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
286 * line
288 static void assert_dn_exists(struct ldbtest_ctx *test_ctx,
289 const char *entry_dn)
291 int count;
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)
300 int count;
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,
307 struct ldb_dn *dn,
308 const char *cn_value)
310 int ret;
311 TALLOC_CTX *tmp_ctx;
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);
322 msg->dn = dn;
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)
337 int ret;
338 struct ldbtest_ctx *test_ctx = talloc_get_type_abort(*state,
339 struct ldbtest_ctx);
340 const char *basedn = "dc=ldb_del_test";
341 struct ldb_dn *dn;
343 dn = ldb_dn_new_fmt(test_ctx, test_ctx->ldb, "%s", basedn);
344 assert_non_null(dn);
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,
357 struct ldbtest_ctx);
358 struct ldb_dn *basedn;
359 int ret;
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)
370 int ret;
371 struct ldbtest_ctx *test_ctx = talloc_get_type_abort(*state,
372 struct ldbtest_ctx);
373 TALLOC_CTX *tmp_ctx;
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,
393 NULL);
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,
404 request);
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)
421 int ret;
422 struct ldbtest_ctx *test_ctx = talloc_get_type_abort(*state,
423 struct ldbtest_ctx);
424 TALLOC_CTX *tmp_ctx;
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,
444 NULL);
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,
459 request);
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,
469 const char *key,
470 const char *val)
472 int ret;
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);
487 talloc_free(msg);
490 static struct ldb_result *get_keyval(struct ldbtest_ctx *test_ctx,
491 const char *key,
492 const char *val)
494 int ret;
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);
505 return result;
508 static void test_transactions(void **state)
510 int ret;
511 struct ldbtest_ctx *test_ctx = talloc_get_type_abort(*state,
512 struct ldbtest_ctx);
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;
549 struct keyval {
550 const char *key;
551 const char *val;
554 static struct ldb_message *build_mod_msg(TALLOC_CTX *mem_ctx,
555 struct ldbtest_ctx *test_ctx,
556 const char *dn,
557 int modify_flags,
558 struct keyval *kvs)
560 struct ldb_message *msg;
561 int ret;
562 int i;
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++) {
571 if (modify_flags) {
572 ret = ldb_msg_add_empty(msg, kvs[i].key,
573 modify_flags, NULL);
574 assert_int_equal(ret, 0);
577 if (kvs[i].val) {
578 ret = ldb_msg_add_string(msg, kvs[i].key, kvs[i].val);
579 assert_int_equal(ret, LDB_SUCCESS);
583 return msg;
586 static void ldb_test_add_data(TALLOC_CTX *mem_ctx,
587 struct ldbtest_ctx *ldb_test_ctx,
588 const char *basedn,
589 struct keyval *kvs)
591 TALLOC_CTX *tmp_ctx;
592 struct ldb_message *msg;
593 struct ldb_result *result = NULL;
594 int ret;
596 tmp_ctx = talloc_new(mem_ctx);
597 assert_non_null(tmp_ctx);
599 msg = build_mod_msg(tmp_ctx, ldb_test_ctx,
600 basedn, 0, kvs);
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,
619 const char *strdn)
621 TALLOC_CTX *tmp_ctx;
622 struct ldb_dn *basedn;
623 int ret;
624 size_t count;
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,
630 "%s", strdn);
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,
643 struct keyval *kvs)
645 ldb_test_add_data(mod_test_ctx,
646 mod_test_ctx->ldb_test_ctx,
647 mod_test_ctx->entry_dn,
648 kvs);
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,
659 int modify_flags,
660 struct keyval *kvs)
662 TALLOC_CTX *tmp_ctx;
663 struct ldb_result *res;
664 struct ldb_message *mod_msg;
665 struct ldb_dn *basedn;
666 struct ldbtest_ctx *ldb_test_ctx;
667 int ret;
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,
675 modify_flags, kvs);
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);
694 return res;
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" },
703 { NULL, NULL },
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;
717 return 0;
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);
733 return 0;
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" },
743 { NULL, NULL },
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");
754 assert_non_null(el);
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");
759 assert_non_null(el);
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" },
771 { NULL, NULL },
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");
782 assert_non_null(el);
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;
796 int ret;
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;
809 assert_non_null(el);
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[] = {
827 { "cn", new_cn },
828 { NULL, NULL },
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");
839 assert_non_null(el);
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" },
851 { NULL, NULL },
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");
862 assert_non_null(el);
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);
867 assert_non_null(el);
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[] = {
880 { "cn", NULL },
881 { NULL, NULL },
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");
888 assert_null(el);
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 },
900 { NULL, 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");
909 assert_non_null(el);
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[] = {
920 { "cn", NULL },
921 { NULL, NULL },
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");
929 assert_null(el);
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" },
941 { NULL, NULL },
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");
949 assert_null(el);
952 struct search_test_ctx {
953 struct ldbtest_ctx *ldb_test_ctx;
954 const char *base_dn;
957 static char *get_full_dn(TALLOC_CTX *mem_ctx,
958 struct search_test_ctx *search_test_ctx,
959 const char *rdn)
961 char *full_dn;
963 full_dn = talloc_asprintf(mem_ctx,
964 "%s,%s", rdn, search_test_ctx->base_dn);
965 assert_non_null(full_dn);
967 return full_dn;
970 static void search_test_add_data(struct search_test_ctx *search_test_ctx,
971 const char *rdn,
972 struct keyval *kvs)
974 char *full_dn;
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,
980 full_dn,
981 kvs);
984 static void search_test_remove_data(struct search_test_ctx *search_test_ctx,
985 const char *rdn)
987 char *full_dn;
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,
995 full_dn);
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" },
1007 { NULL, NULL },
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" },
1014 { NULL, NULL },
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;
1032 return 0;
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);
1046 return 0;
1049 static void assert_attr_has_vals(struct ldb_message *msg,
1050 const char *attr,
1051 const char *vals[],
1052 const size_t nvals)
1054 struct ldb_message_element *el;
1055 size_t i;
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,
1063 vals[i]);
1067 static void assert_has_no_attr(struct ldb_message *msg,
1068 const char *attr)
1070 struct ldb_message_element *el;
1072 el = ldb_msg_find_element(msg, attr);
1073 assert_null(el);
1076 static bool has_dn(struct ldb_message *msg, const char *dn)
1078 const char *msgdn;
1080 msgdn = ldb_dn_get_linearized(msg->dn);
1081 if (strcmp(dn, msgdn) == 0) {
1082 return true;
1085 return false;
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);
1092 size_t count;
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);
1103 int ret;
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,
1113 "%s",
1114 search_test_ctx->base_dn);
1115 assert_non_null(basedn);
1117 ret = ldb_search(search_test_ctx->ldb_test_ctx->ldb,
1118 search_test_ctx,
1119 &result,
1120 basedn,
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);
1135 int ret;
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,
1144 "%s",
1145 search_test_ctx->base_dn);
1146 assert_non_null(basedn);
1148 ret = ldb_search(search_test_ctx->ldb_test_ctx->ldb,
1149 search_test_ctx,
1150 &result,
1151 basedn,
1152 LDB_SCOPE_SUBTREE,
1153 attrs,
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)
1166 char *full_dn1;
1167 char *full_dn2;
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,
1178 search_test_ctx,
1179 "cn=test_search_cn");
1181 full_dn2 = get_full_dn(search_test_ctx,
1182 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);
1191 } else {
1192 fail();
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);
1200 int ret;
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,
1206 "%s",
1207 search_test_ctx->base_dn);
1208 assert_non_null(basedn);
1210 ret = ldb_search(search_test_ctx->ldb_test_ctx->ldb,
1211 search_test_ctx,
1212 &result,
1213 basedn,
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);
1228 int ret;
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,
1235 "dc=nosuchdn");
1236 assert_non_null(basedn);
1238 ret = ldb_search(search_test_ctx->ldb_test_ctx->ldb,
1239 search_test_ctx,
1240 &result,
1241 basedn,
1242 LDB_SCOPE_SUBTREE, NULL,
1243 "cn=*");
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,
1252 "@OPTIONS");
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);
1261 /* Search again */
1262 /* The search should return LDB_ERR_NO_SUCH_OBJECT */
1263 ret = ldb_search(search_test_ctx->ldb_test_ctx->ldb,
1264 search_test_ctx,
1265 &result,
1266 basedn,
1267 LDB_SCOPE_SUBTREE, NULL,
1268 "cn=*");
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
1280 * (1) and (2):
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
1284 * the callback
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
1290 * ltdb_lock_read()
1293 struct search_against_transaction_ctx {
1294 struct ldbtest_ctx *test_ctx;
1295 int res_count;
1296 pid_t child_pid;
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:
1306 ctx->res_count++;
1307 if (ctx->res_count != 1) {
1308 return LDB_SUCCESS;
1311 break;
1313 case LDB_REPLY_REFERRAL:
1314 break;
1316 case LDB_REPLY_DONE:
1317 return ldb_request_done(req, LDB_SUCCESS);
1320 return 0;
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)
1338 int ret, ret2;
1339 int pipes[2];
1340 char buf[2];
1341 struct search_against_transaction_ctx *ctx = req->context;
1342 switch (ares->type) {
1343 case LDB_REPLY_ENTRY:
1344 break;
1346 case LDB_REPLY_REFERRAL:
1347 return LDB_SUCCESS;
1349 case LDB_REPLY_DONE:
1350 return ldb_request_done(req, LDB_SUCCESS);
1353 ret = pipe(pipes);
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,
1368 ctx->test_ctx->ev);
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) {
1376 exit(ret);
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);
1385 if (msg == NULL) {
1386 exit(LDB_ERR_OPERATIONS_ERROR);
1389 msg->dn = ldb_dn_new_fmt(msg, ctx->test_ctx->ldb,
1390 "dc=test");
1391 if (msg->dn == NULL) {
1392 exit(LDB_ERR_OPERATIONS_ERROR);
1395 ret = ldb_msg_add_string(msg, "cn", "test_cn_val");
1396 if (ret != 0) {
1397 exit(LDB_ERR_OPERATIONS_ERROR);
1400 ret = ldb_transaction_start(ctx->test_ctx->ldb);
1401 if (ret != 0) {
1402 exit(ret);
1405 ret = write(pipes[1], "GO", 2);
1406 if (ret != 2) {
1407 exit(LDB_ERR_OPERATIONS_ERROR);
1410 ret = ldb_add(ctx->test_ctx->ldb, msg);
1411 if (ret != 0) {
1412 exit(ret);
1415 ret = ldb_transaction_commit(ctx->test_ctx->ldb);
1416 exit(ret);
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,
1424 ctx->test_ctx->ldb,
1425 ctx->test_ctx,
1426 ctx->basedn,
1427 LDB_SCOPE_SUBTREE,
1428 "cn=*", NULL,
1429 NULL,
1430 ctx,
1431 test_ldb_search_against_transaction_callback2,
1432 NULL);
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);
1447 return LDB_SUCCESS;
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
1455 ctx =
1456 { .res_count = 0,
1457 .test_ctx = search_test_ctx->ldb_test_ctx
1460 int ret;
1461 struct ldb_request *req;
1462 pid_t pid;
1463 int wstatus;
1464 struct ldb_dn *base_search_dn;
1466 tevent_loop_allow_nesting(search_test_ctx->ldb_test_ctx->ev);
1468 base_search_dn
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);
1475 ctx.basedn
1476 = ldb_dn_new_fmt(search_test_ctx,
1477 search_test_ctx->ldb_test_ctx->ldb,
1478 "%s",
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,
1486 search_test_ctx,
1487 base_search_dn,
1488 LDB_SCOPE_BASE,
1489 "cn=*", NULL,
1490 NULL,
1491 &ctx,
1492 test_ldb_search_against_transaction_callback1,
1493 NULL);
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);
1514 * This test is also complex.
1515 * The purpose is to test if a modify can occur during an ldb_search()
1516 * This would be a failure if if in process
1517 * (1) and (2):
1518 * - (1) ltdb_search() starts and calls back for one entry
1519 * - (2) one of the entries to be matched is modified
1520 * - (1) the indexed search tries to return the modified entry, but
1521 * it is no longer found, either:
1522 * - despite it still matching (dn changed)
1523 * - it no longer matching (attrs changed)
1525 * We also try un-indexed to show that the behaviour differs on this
1526 * point, which it should not (an index should only impact search
1527 * speed).
1530 struct modify_during_search_test_ctx {
1531 struct ldbtest_ctx *test_ctx;
1532 int res_count;
1533 pid_t child_pid;
1534 struct ldb_dn *basedn;
1535 bool got_cn;
1536 bool got_2_cn;
1537 bool rename;
1541 * This purpose of this callback is to trigger a write in
1542 * the child process while a search is in progress.
1544 * In tdb 1.3.12 tdb_traverse_read() take the read transaction lock
1545 * however in ldb 1.1.31 ltdb_search() forgets to take the all-record
1546 * lock (except the very first time) due to a ref-counting bug.
1548 * We assume that if the write will proceed, it will proceed in a 3
1549 * second window after the function is called.
1552 static int test_ldb_modify_during_search_callback1(struct ldb_request *req,
1553 struct ldb_reply *ares)
1555 int ret;
1556 int pipes[2];
1557 char buf[2];
1558 struct modify_during_search_test_ctx *ctx = req->context;
1559 switch (ares->type) {
1560 case LDB_REPLY_ENTRY:
1562 const struct ldb_val *cn_val
1563 = ldb_dn_get_component_val(ares->message->dn, 0);
1564 const char *cn = (char *)cn_val->data;
1565 ctx->res_count++;
1566 if (strcmp(cn, "test_search_cn") == 0) {
1567 ctx->got_cn = true;
1568 } else if (strcmp(cn, "test_search_2_cn") == 0) {
1569 ctx->got_2_cn = true;
1571 if (ctx->res_count == 2) {
1572 return LDB_SUCCESS;
1574 break;
1576 case LDB_REPLY_REFERRAL:
1577 return LDB_SUCCESS;
1579 case LDB_REPLY_DONE:
1580 return ldb_request_done(req, LDB_SUCCESS);
1583 ret = pipe(pipes);
1584 assert_int_equal(ret, 0);
1586 ctx->child_pid = fork();
1587 if (ctx->child_pid == 0 && ctx->rename) {
1588 TALLOC_CTX *tmp_ctx = NULL;
1589 struct ldb_dn *dn, *new_dn;
1590 TALLOC_FREE(ctx->test_ctx->ldb);
1591 TALLOC_FREE(ctx->test_ctx->ev);
1592 ctx->test_ctx->ev = tevent_context_init(ctx->test_ctx);
1593 if (ctx->test_ctx->ev == NULL) {
1594 exit(LDB_ERR_OPERATIONS_ERROR);
1597 ctx->test_ctx->ldb = ldb_init(ctx->test_ctx,
1598 ctx->test_ctx->ev);
1599 if (ctx->test_ctx->ldb == NULL) {
1600 exit(LDB_ERR_OPERATIONS_ERROR);
1603 ret = ldb_connect(ctx->test_ctx->ldb,
1604 ctx->test_ctx->dbpath, 0, NULL);
1605 if (ret != LDB_SUCCESS) {
1606 exit(ret);
1609 tmp_ctx = talloc_new(ctx->test_ctx);
1610 if (tmp_ctx == NULL) {
1611 exit(LDB_ERR_OPERATIONS_ERROR);
1614 if (ctx->got_cn) {
1615 /* Modify the other one */
1616 dn = ldb_dn_new_fmt(tmp_ctx, ctx->test_ctx->ldb,
1617 "cn=test_search_2_cn,"
1618 "dc=search_test_entry");
1619 } else {
1620 dn = ldb_dn_new_fmt(tmp_ctx, ctx->test_ctx->ldb,
1621 "cn=test_search_cn,"
1622 "dc=search_test_entry");
1624 if (dn == NULL) {
1625 exit(LDB_ERR_OPERATIONS_ERROR);
1628 new_dn = ldb_dn_new_fmt(tmp_ctx, ctx->test_ctx->ldb,
1629 "cn=test_search_cn_renamed,"
1630 "dc=search_test_entry");
1631 if (new_dn == NULL) {
1632 exit(LDB_ERR_OPERATIONS_ERROR);
1635 ret = ldb_transaction_start(ctx->test_ctx->ldb);
1636 if (ret != 0) {
1637 exit(ret);
1640 if (write(pipes[1], "GO", 2) != 2) {
1641 exit(LDB_ERR_OPERATIONS_ERROR);
1644 ret = ldb_rename(ctx->test_ctx->ldb, dn, new_dn);
1645 if (ret != 0) {
1646 exit(ret);
1649 ret = ldb_transaction_commit(ctx->test_ctx->ldb);
1650 exit(ret);
1652 } else if (ctx->child_pid == 0) {
1653 TALLOC_CTX *tmp_ctx = NULL;
1654 struct ldb_message *msg;
1655 struct ldb_message_element *el;
1656 TALLOC_FREE(ctx->test_ctx->ldb);
1657 TALLOC_FREE(ctx->test_ctx->ev);
1658 ctx->test_ctx->ev = tevent_context_init(ctx->test_ctx);
1659 if (ctx->test_ctx->ev == NULL) {
1660 exit(LDB_ERR_OPERATIONS_ERROR);
1663 ctx->test_ctx->ldb = ldb_init(ctx->test_ctx,
1664 ctx->test_ctx->ev);
1665 if (ctx->test_ctx->ldb == NULL) {
1666 exit(LDB_ERR_OPERATIONS_ERROR);
1669 ret = ldb_connect(ctx->test_ctx->ldb,
1670 ctx->test_ctx->dbpath, 0, NULL);
1671 if (ret != LDB_SUCCESS) {
1672 exit(ret);
1675 tmp_ctx = talloc_new(ctx->test_ctx);
1676 if (tmp_ctx == NULL) {
1677 exit(LDB_ERR_OPERATIONS_ERROR);
1680 msg = ldb_msg_new(tmp_ctx);
1681 if (msg == NULL) {
1682 exit(LDB_ERR_OPERATIONS_ERROR);
1685 if (ctx->got_cn) {
1686 /* Modify the other one */
1687 msg->dn = ldb_dn_new_fmt(msg, ctx->test_ctx->ldb,
1688 "cn=test_search_2_cn,"
1689 "dc=search_test_entry");
1690 } else {
1691 msg->dn = ldb_dn_new_fmt(msg, ctx->test_ctx->ldb,
1692 "cn=test_search_cn,"
1693 "dc=search_test_entry");
1695 if (msg->dn == NULL) {
1696 exit(LDB_ERR_OPERATIONS_ERROR);
1699 ret = ldb_msg_add_string(msg, "filterAttr", "TRUE");
1700 if (ret != 0) {
1701 exit(LDB_ERR_OPERATIONS_ERROR);
1703 el = ldb_msg_find_element(msg, "filterAttr");
1704 if (el == NULL) {
1705 exit(LDB_ERR_OPERATIONS_ERROR);
1707 el->flags = LDB_FLAG_MOD_REPLACE;
1709 ret = ldb_transaction_start(ctx->test_ctx->ldb);
1710 if (ret != 0) {
1711 exit(ret);
1714 if (write(pipes[1], "GO", 2) != 2) {
1715 exit(LDB_ERR_OPERATIONS_ERROR);
1718 ret = ldb_modify(ctx->test_ctx->ldb, msg);
1719 if (ret != 0) {
1720 exit(ret);
1723 ret = ldb_transaction_commit(ctx->test_ctx->ldb);
1724 exit(ret);
1728 * With TDB 1.3.13 and before "tdb: Remove locking from tdb_traverse_read()"
1729 * we will hang here because the child process can not proceed to
1730 * sending the "GO" as it is blocked at ldb_transaction_start().
1733 ret = read(pipes[0], buf, 2);
1734 assert_int_equal(ret, 2);
1736 sleep(3);
1738 return LDB_SUCCESS;
1741 static void test_ldb_modify_during_search(void **state, bool add_index,
1742 bool rename)
1744 struct search_test_ctx *search_test_ctx = talloc_get_type_abort(*state,
1745 struct search_test_ctx);
1746 struct modify_during_search_test_ctx
1747 ctx =
1748 { .res_count = 0,
1749 .test_ctx = search_test_ctx->ldb_test_ctx,
1750 .rename = rename
1753 int ret;
1754 struct ldb_request *req;
1755 pid_t pid;
1756 int wstatus;
1758 if (add_index) {
1759 struct ldb_message *msg;
1760 struct ldb_dn *indexlist = ldb_dn_new(search_test_ctx,
1761 search_test_ctx->ldb_test_ctx->ldb,
1762 "@INDEXLIST");
1763 assert_non_null(indexlist);
1765 msg = ldb_msg_new(search_test_ctx);
1766 assert_non_null(msg);
1768 msg->dn = indexlist;
1770 ret = ldb_msg_add_string(msg, "@IDXATTR", "cn");
1771 assert_int_equal(ret, LDB_SUCCESS);
1773 ret = ldb_add(search_test_ctx->ldb_test_ctx->ldb,
1774 msg);
1776 assert_int_equal(ret, LDB_SUCCESS);
1779 tevent_loop_allow_nesting(search_test_ctx->ldb_test_ctx->ev);
1781 ctx.basedn
1782 = ldb_dn_new_fmt(search_test_ctx,
1783 search_test_ctx->ldb_test_ctx->ldb,
1784 "%s",
1785 search_test_ctx->base_dn);
1786 assert_non_null(ctx.basedn);
1790 * This search must be over multiple items, and should include
1791 * the new name after a rename, to show that it would match
1792 * both before and after that modify
1794 ret = ldb_build_search_req(&req,
1795 search_test_ctx->ldb_test_ctx->ldb,
1796 search_test_ctx,
1797 ctx.basedn,
1798 LDB_SCOPE_SUBTREE,
1799 "(&(!(filterAttr=*))"
1800 "(|(cn=test_search_cn_renamed)"
1801 "(cn=test_search_cn)"
1802 "(cn=test_search_2_cn)"
1803 "))",
1804 NULL,
1805 NULL,
1806 &ctx,
1807 test_ldb_modify_during_search_callback1,
1808 NULL);
1809 assert_int_equal(ret, 0);
1810 ret = ldb_request(search_test_ctx->ldb_test_ctx->ldb, req);
1812 if (ret == LDB_SUCCESS) {
1813 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
1815 assert_int_equal(ret, 0);
1816 assert_int_equal(ctx.res_count, 2);
1817 assert_int_equal(ctx.got_cn, true);
1818 assert_int_equal(ctx.got_2_cn, true);
1820 pid = waitpid(ctx.child_pid, &wstatus, 0);
1821 assert_int_equal(pid, ctx.child_pid);
1823 assert_true(WIFEXITED(wstatus));
1825 assert_int_equal(WEXITSTATUS(wstatus), 0);
1830 static void test_ldb_modify_during_indexed_search(void **state)
1832 return test_ldb_modify_during_search(state, true, false);
1835 static void test_ldb_modify_during_unindexed_search(void **state)
1837 return test_ldb_modify_during_search(state, false, false);
1840 static void test_ldb_rename_during_indexed_search(void **state)
1842 return test_ldb_modify_during_search(state, true, true);
1845 static void test_ldb_rename_during_unindexed_search(void **state)
1847 return test_ldb_modify_during_search(state, false, true);
1851 * This test is also complex.
1853 * The purpose is to test if a modify can occur during an ldb_search()
1854 * before the end of the callback
1856 * This would be a failure if if in process
1857 * (1) and (2):
1858 * - (1) ldb_search() starts and calls back for a number of entries
1859 * - (2) an entry in the DB is allowed to change before the callback returns
1860 * - (1) the callback can see the modification
1865 * This purpose of this callback is to trigger a write in
1866 * the child process while a search DONE callback is in progress.
1868 * In ldb 1.1.31 ldb_search() omitted to take a all-record
1869 * lock for the full duration of the search and callbacks
1871 * We assume that if the write will proceed, it will proceed in a 3
1872 * second window after the function is called.
1875 static int test_ldb_modify_during_whole_search_callback1(struct ldb_request *req,
1876 struct ldb_reply *ares)
1878 int ret;
1879 int pipes[2];
1880 char buf[2];
1881 struct modify_during_search_test_ctx *ctx = req->context;
1882 struct ldb_dn *search_dn;
1883 struct ldb_result *res2;
1884 unsigned res_count;
1885 switch (ares->type) {
1886 case LDB_REPLY_ENTRY:
1887 case LDB_REPLY_REFERRAL:
1888 return LDB_SUCCESS;
1890 case LDB_REPLY_DONE:
1891 break;
1894 ret = pipe(pipes);
1895 assert_int_equal(ret, 0);
1897 ctx->child_pid = fork();
1898 if (ctx->child_pid == 0) {
1899 TALLOC_CTX *tmp_ctx = NULL;
1900 struct ldb_message *msg;
1901 struct ldb_message_element *el;
1902 TALLOC_FREE(ctx->test_ctx->ldb);
1903 TALLOC_FREE(ctx->test_ctx->ev);
1904 ctx->test_ctx->ev = tevent_context_init(ctx->test_ctx);
1905 if (ctx->test_ctx->ev == NULL) {
1906 exit(LDB_ERR_OPERATIONS_ERROR);
1909 ctx->test_ctx->ldb = ldb_init(ctx->test_ctx,
1910 ctx->test_ctx->ev);
1911 if (ctx->test_ctx->ldb == NULL) {
1912 exit(LDB_ERR_OPERATIONS_ERROR);
1915 ret = ldb_connect(ctx->test_ctx->ldb,
1916 ctx->test_ctx->dbpath, 0, NULL);
1917 if (ret != LDB_SUCCESS) {
1918 exit(ret);
1921 tmp_ctx = talloc_new(ctx->test_ctx);
1922 if (tmp_ctx == NULL) {
1923 exit(LDB_ERR_OPERATIONS_ERROR);
1926 msg = ldb_msg_new(tmp_ctx);
1927 if (msg == NULL) {
1928 exit(LDB_ERR_OPERATIONS_ERROR);
1931 msg->dn = ldb_dn_new_fmt(msg, ctx->test_ctx->ldb,
1932 "cn=test_search_cn,"
1933 "dc=search_test_entry");
1934 if (msg->dn == NULL) {
1935 exit(LDB_ERR_OPERATIONS_ERROR);
1938 ret = ldb_msg_add_string(msg, "filterAttr", "TRUE");
1939 if (ret != 0) {
1940 exit(LDB_ERR_OPERATIONS_ERROR);
1942 el = ldb_msg_find_element(msg, "filterAttr");
1943 if (el == NULL) {
1944 exit(LDB_ERR_OPERATIONS_ERROR);
1946 el->flags = LDB_FLAG_MOD_REPLACE;
1948 ret = ldb_transaction_start(ctx->test_ctx->ldb);
1949 if (ret != 0) {
1950 exit(ret);
1953 if (write(pipes[1], "GO", 2) != 2) {
1954 exit(LDB_ERR_OPERATIONS_ERROR);
1957 ret = ldb_modify(ctx->test_ctx->ldb, msg);
1958 if (ret != 0) {
1959 exit(ret);
1962 ret = ldb_transaction_commit(ctx->test_ctx->ldb);
1963 exit(ret);
1966 ret = read(pipes[0], buf, 2);
1967 assert_int_equal(ret, 2);
1969 sleep(3);
1972 * If writes are not blocked until after this function, we
1973 * will be able to successfully search for this modification
1974 * here
1977 search_dn = ldb_dn_new_fmt(ares, ctx->test_ctx->ldb,
1978 "cn=test_search_cn,"
1979 "dc=search_test_entry");
1981 ret = ldb_search(ctx->test_ctx->ldb, ares,
1982 &res2, search_dn, LDB_SCOPE_BASE, NULL,
1983 "filterAttr=TRUE");
1986 * We do this in an unusual order, because if we fail an assert before
1987 * ldb_request_done(), we will also fail to clean up as we hold locks.
1990 res_count = res2->count;
1991 ldb_request_done(req, LDB_SUCCESS);
1992 assert_int_equal(ret, 0);
1994 /* We should not have got the result */
1995 assert_int_equal(res_count, 0);
1997 return ret;
2000 static void test_ldb_modify_during_whole_search(void **state)
2002 struct search_test_ctx *search_test_ctx = talloc_get_type_abort(*state,
2003 struct search_test_ctx);
2004 struct modify_during_search_test_ctx
2005 ctx =
2007 .test_ctx = search_test_ctx->ldb_test_ctx,
2010 int ret;
2011 struct ldb_request *req;
2012 pid_t pid;
2013 int wstatus;
2014 struct ldb_dn *search_dn;
2015 struct ldb_result *res2;
2017 tevent_loop_allow_nesting(search_test_ctx->ldb_test_ctx->ev);
2019 ctx.basedn
2020 = ldb_dn_new_fmt(search_test_ctx,
2021 search_test_ctx->ldb_test_ctx->ldb,
2022 "%s",
2023 search_test_ctx->base_dn);
2024 assert_non_null(ctx.basedn);
2028 * The search just needs to call DONE, we don't care about the
2029 * contents of the search for this test
2031 ret = ldb_build_search_req(&req,
2032 search_test_ctx->ldb_test_ctx->ldb,
2033 search_test_ctx,
2034 ctx.basedn,
2035 LDB_SCOPE_SUBTREE,
2036 "(&(!(filterAttr=*))"
2037 "(cn=test_search_cn))",
2038 NULL,
2039 NULL,
2040 &ctx,
2041 test_ldb_modify_during_whole_search_callback1,
2042 NULL);
2043 assert_int_equal(ret, 0);
2044 ret = ldb_request(search_test_ctx->ldb_test_ctx->ldb, req);
2046 if (ret == LDB_SUCCESS) {
2047 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
2049 assert_int_equal(ret, 0);
2051 pid = waitpid(ctx.child_pid, &wstatus, 0);
2052 assert_int_equal(pid, ctx.child_pid);
2054 assert_true(WIFEXITED(wstatus));
2056 assert_int_equal(WEXITSTATUS(wstatus), 0);
2059 * If writes are blocked until after the search function, we
2060 * will be able to successfully search for this modification
2061 * now
2064 search_dn = ldb_dn_new_fmt(search_test_ctx,
2065 search_test_ctx->ldb_test_ctx->ldb,
2066 "cn=test_search_cn,"
2067 "dc=search_test_entry");
2069 ret = ldb_search(search_test_ctx->ldb_test_ctx->ldb,
2070 search_test_ctx,
2071 &res2, search_dn, LDB_SCOPE_BASE, NULL,
2072 "filterAttr=TRUE");
2073 assert_int_equal(ret, 0);
2075 /* We got the result */
2076 assert_int_equal(res2->count, 1);
2079 static int ldb_case_test_setup(void **state)
2081 int ret;
2082 struct ldb_ldif *ldif;
2083 struct ldbtest_ctx *ldb_test_ctx;
2084 const char *attrs_ldif = \
2085 "dn: @ATTRIBUTES\n"
2086 "cn: CASE_INSENSITIVE\n"
2087 "\n";
2088 struct keyval kvs[] = {
2089 { "cn", "CaseInsensitiveValue" },
2090 { "uid", "CaseSensitiveValue" },
2091 { NULL, NULL },
2095 ldbtest_setup((void **) &ldb_test_ctx);
2097 while ((ldif = ldb_ldif_read_string(ldb_test_ctx->ldb, &attrs_ldif))) {
2098 ret = ldb_add(ldb_test_ctx->ldb, ldif->msg);
2099 assert_int_equal(ret, LDB_SUCCESS);
2102 ldb_test_add_data(ldb_test_ctx,
2103 ldb_test_ctx,
2104 "cn=CaseInsensitiveValue",
2105 kvs);
2107 *state = ldb_test_ctx;
2108 return 0;
2111 static int ldb_case_test_teardown(void **state)
2113 int ret;
2114 struct ldbtest_ctx *ldb_test_ctx = talloc_get_type_abort(*state,
2115 struct ldbtest_ctx);
2117 struct ldb_dn *del_dn;
2119 del_dn = ldb_dn_new_fmt(ldb_test_ctx,
2120 ldb_test_ctx->ldb,
2121 "@ATTRIBUTES");
2122 assert_non_null(del_dn);
2124 ret = ldb_delete(ldb_test_ctx->ldb, del_dn);
2125 assert_int_equal(ret, LDB_SUCCESS);
2127 assert_dn_doesnt_exist(ldb_test_ctx,
2128 "@ATTRIBUTES");
2130 ldb_test_remove_data(ldb_test_ctx, ldb_test_ctx,
2131 "cn=CaseInsensitiveValue");
2133 ldbtest_teardown((void **) &ldb_test_ctx);
2134 return 0;
2137 static void test_ldb_attrs_case_insensitive(void **state)
2139 int cnt;
2140 struct ldbtest_ctx *ldb_test_ctx = talloc_get_type_abort(*state,
2141 struct ldbtest_ctx);
2143 /* cn matches exact case */
2144 cnt = sub_search_count(ldb_test_ctx, "", "cn=CaseInsensitiveValue");
2145 assert_int_equal(cnt, 1);
2147 /* cn matches lower case */
2148 cnt = sub_search_count(ldb_test_ctx, "", "cn=caseinsensitivevalue");
2149 assert_int_equal(cnt, 1);
2151 /* uid matches exact case */
2152 cnt = sub_search_count(ldb_test_ctx, "", "uid=CaseSensitiveValue");
2153 assert_int_equal(cnt, 1);
2155 /* uid does not match lower case */
2156 cnt = sub_search_count(ldb_test_ctx, "", "uid=casesensitivevalue");
2157 assert_int_equal(cnt, 0);
2160 static struct ldb_schema_attribute cn_attr_1;
2161 static struct ldb_schema_attribute cn_attr_2;
2162 static struct ldb_schema_attribute default_attr;
2165 override the name to attribute handler function
2167 static const struct ldb_schema_attribute *ldb_test_attribute_handler_override(struct ldb_context *ldb,
2168 void *private_data,
2169 const char *name)
2171 if (private_data != NULL && ldb_attr_cmp(name, "cn") == 0) {
2172 return &cn_attr_1;
2173 } else if (private_data == NULL && ldb_attr_cmp(name, "cn") == 0) {
2174 return &cn_attr_2;
2175 } else if (ldb_attr_cmp(name, "uid") == 0) {
2176 return &cn_attr_2;
2178 return &default_attr;
2181 static void test_ldb_attrs_case_handler(void **state)
2183 int cnt;
2184 int ret;
2185 const struct ldb_schema_syntax *syntax;
2187 struct ldbtest_ctx *ldb_test_ctx = talloc_get_type_abort(*state,
2188 struct ldbtest_ctx);
2189 struct ldb_context *ldb = ldb_test_ctx->ldb;
2191 /* cn matches lower case */
2192 cnt = sub_search_count(ldb_test_ctx, "", "cn=caseinsensitivevalue");
2193 assert_int_equal(cnt, 1);
2195 syntax = ldb_standard_syntax_by_name(ldb, LDB_SYNTAX_OCTET_STRING);
2196 assert_non_null(syntax);
2198 ret = ldb_schema_attribute_fill_with_syntax(ldb, ldb,
2199 "*", 0,
2200 syntax, &default_attr);
2201 assert_int_equal(ret, LDB_SUCCESS);
2203 syntax = ldb_standard_syntax_by_name(ldb, LDB_SYNTAX_OCTET_STRING);
2204 assert_non_null(syntax);
2206 ret = ldb_schema_attribute_fill_with_syntax(ldb, ldb,
2207 "cn", 0,
2208 syntax, &cn_attr_1);
2209 assert_int_equal(ret, LDB_SUCCESS);
2212 * Set an attribute handler, which will fail to match as we
2213 * force case sensitive
2215 ldb_schema_attribute_set_override_handler(ldb,
2216 ldb_test_attribute_handler_override,
2217 (void *)1);
2219 /* cn does not matche lower case */
2220 cnt = sub_search_count(ldb_test_ctx, "", "cn=caseinsensitivevalue");
2221 assert_int_equal(cnt, 0);
2223 syntax = ldb_standard_syntax_by_name(ldb, LDB_SYNTAX_DIRECTORY_STRING);
2224 assert_non_null(syntax);
2226 ret = ldb_schema_attribute_fill_with_syntax(ldb, ldb,
2227 "cn", 0,
2228 syntax, &cn_attr_2);
2229 assert_int_equal(ret, LDB_SUCCESS);
2232 * Set an attribute handler, which will match as we
2233 * force case insensitive
2235 ldb_schema_attribute_set_override_handler(ldb,
2236 ldb_test_attribute_handler_override,
2237 NULL);
2239 /* cn matches lower case */
2240 cnt = sub_search_count(ldb_test_ctx, "", "cn=caseinsensitivevalue");
2241 assert_int_equal(cnt, 1);
2246 static void test_ldb_attrs_index_handler(void **state)
2248 int cnt;
2249 int ret;
2250 const struct ldb_schema_syntax *syntax;
2251 struct ldb_ldif *ldif;
2253 const char *index_ldif = \
2254 "dn: @INDEXLIST\n"
2255 "@IDXATTR: cn\n"
2256 "\n";
2258 struct ldbtest_ctx *ldb_test_ctx = talloc_get_type_abort(*state,
2259 struct ldbtest_ctx);
2260 struct ldb_context *ldb = ldb_test_ctx->ldb;
2262 /* cn matches lower case */
2263 cnt = sub_search_count(ldb_test_ctx, "", "cn=caseinsensitivevalue");
2264 assert_int_equal(cnt, 1);
2266 syntax = ldb_standard_syntax_by_name(ldb, LDB_SYNTAX_OCTET_STRING);
2267 assert_non_null(syntax);
2269 ret = ldb_schema_attribute_fill_with_syntax(ldb, ldb,
2270 "cn", 0,
2271 syntax, &cn_attr_1);
2272 assert_int_equal(ret, LDB_SUCCESS);
2274 syntax = ldb_standard_syntax_by_name(ldb, LDB_SYNTAX_DIRECTORY_STRING);
2275 assert_non_null(syntax);
2277 ret = ldb_schema_attribute_fill_with_syntax(ldb, ldb,
2278 "cn", LDB_ATTR_FLAG_INDEXED,
2279 syntax, &cn_attr_2);
2280 assert_int_equal(ret, LDB_SUCCESS);
2283 * Set an attribute handler
2285 ldb_schema_attribute_set_override_handler(ldb,
2286 ldb_test_attribute_handler_override,
2287 NULL);
2289 /* cn matches lower case */
2290 cnt = sub_search_count(ldb_test_ctx, "", "cn=caseinsensitivevalue");
2291 assert_int_equal(cnt, 1);
2293 /* Add the index (actually any modify will do) */
2294 while ((ldif = ldb_ldif_read_string(ldb_test_ctx->ldb, &index_ldif))) {
2295 ret = ldb_add(ldb_test_ctx->ldb, ldif->msg);
2296 assert_int_equal(ret, LDB_SUCCESS);
2299 ldb_schema_set_override_indexlist(ldb, false);
2301 /* cn does match as there is an index now */
2302 cnt = sub_search_count(ldb_test_ctx, "", "cn=caseinsensitivevalue");
2303 assert_int_equal(cnt, 1);
2306 * Set an attribute handler, which will later fail to match as we
2307 * didn't re-index the DB
2309 ldb_schema_attribute_set_override_handler(ldb,
2310 ldb_test_attribute_handler_override,
2311 (void *)1);
2314 * cn does not match as we changed the case sensitivity, but
2315 * didn't re-index
2317 * This shows that the override is in control
2319 cnt = sub_search_count(ldb_test_ctx, "", "cn=caseinsensitivevalue");
2320 assert_int_equal(cnt, 0);
2324 static int ldb_case_attrs_index_test_teardown(void **state)
2326 int ret;
2327 struct ldbtest_ctx *ldb_test_ctx = talloc_get_type_abort(*state,
2328 struct ldbtest_ctx);
2329 struct ldb_dn *del_dn;
2331 del_dn = ldb_dn_new_fmt(ldb_test_ctx,
2332 ldb_test_ctx->ldb,
2333 "@INDEXLIST");
2334 assert_non_null(del_dn);
2336 ret = ldb_delete(ldb_test_ctx->ldb, del_dn);
2337 if (ret != LDB_ERR_NO_SUCH_OBJECT) {
2338 assert_int_equal(ret, LDB_SUCCESS);
2341 assert_dn_doesnt_exist(ldb_test_ctx,
2342 "@INDEXLIST");
2344 ldb_case_test_teardown(state);
2345 return 0;
2349 struct rename_test_ctx {
2350 struct ldbtest_ctx *ldb_test_ctx;
2352 struct ldb_dn *basedn;
2353 const char *str_basedn;
2355 const char *teardown_dn;
2358 static int ldb_rename_test_setup(void **state)
2360 struct ldbtest_ctx *ldb_test_ctx;
2361 struct rename_test_ctx *rename_test_ctx;
2362 const char *strdn = "dc=rename_test_entry_from";
2364 ldbtest_setup((void **) &ldb_test_ctx);
2366 rename_test_ctx = talloc(ldb_test_ctx, struct rename_test_ctx);
2367 assert_non_null(rename_test_ctx);
2368 rename_test_ctx->ldb_test_ctx = ldb_test_ctx;
2369 assert_non_null(rename_test_ctx->ldb_test_ctx);
2371 rename_test_ctx->basedn = ldb_dn_new_fmt(rename_test_ctx,
2372 rename_test_ctx->ldb_test_ctx->ldb,
2373 "%s", strdn);
2374 assert_non_null(rename_test_ctx->basedn);
2376 rename_test_ctx->str_basedn = strdn;
2377 rename_test_ctx->teardown_dn = strdn;
2379 add_dn_with_cn(ldb_test_ctx,
2380 rename_test_ctx->basedn,
2381 "test_rename_cn_val");
2383 *state = rename_test_ctx;
2384 return 0;
2387 static int ldb_rename_test_teardown(void **state)
2389 int ret;
2390 struct rename_test_ctx *rename_test_ctx = talloc_get_type_abort(*state,
2391 struct rename_test_ctx);
2392 struct ldbtest_ctx *ldb_test_ctx;
2393 struct ldb_dn *del_dn;
2395 ldb_test_ctx = rename_test_ctx->ldb_test_ctx;
2397 del_dn = ldb_dn_new_fmt(rename_test_ctx,
2398 rename_test_ctx->ldb_test_ctx->ldb,
2399 "%s", rename_test_ctx->teardown_dn);
2400 assert_non_null(del_dn);
2402 ret = ldb_delete(ldb_test_ctx->ldb, del_dn);
2403 assert_int_equal(ret, LDB_SUCCESS);
2405 assert_dn_doesnt_exist(ldb_test_ctx,
2406 rename_test_ctx->teardown_dn);
2408 ldbtest_teardown((void **) &ldb_test_ctx);
2409 return 0;
2412 static void test_ldb_rename(void **state)
2414 struct rename_test_ctx *rename_test_ctx =
2415 talloc_get_type_abort(*state, struct rename_test_ctx);
2416 int ret;
2417 const char *str_new_dn = "dc=rename_test_entry_to";
2418 struct ldb_dn *new_dn;
2420 new_dn = ldb_dn_new_fmt(rename_test_ctx,
2421 rename_test_ctx->ldb_test_ctx->ldb,
2422 "%s", str_new_dn);
2423 assert_non_null(new_dn);
2425 ret = ldb_rename(rename_test_ctx->ldb_test_ctx->ldb,
2426 rename_test_ctx->basedn,
2427 new_dn);
2428 assert_int_equal(ret, LDB_SUCCESS);
2430 assert_dn_exists(rename_test_ctx->ldb_test_ctx, str_new_dn);
2431 assert_dn_doesnt_exist(rename_test_ctx->ldb_test_ctx,
2432 rename_test_ctx->str_basedn);
2433 rename_test_ctx->teardown_dn = str_new_dn;
2435 /* FIXME - test the values which didn't change */
2438 static void test_ldb_rename_from_doesnt_exist(void **state)
2440 struct rename_test_ctx *rename_test_ctx = talloc_get_type_abort(
2441 *state,
2442 struct rename_test_ctx);
2443 int ret;
2444 const char *str_new_dn = "dc=rename_test_entry_to";
2445 const char *str_bad_old_dn = "dc=rename_test_no_such_entry";
2446 struct ldb_dn *new_dn;
2447 struct ldb_dn *bad_old_dn;
2449 new_dn = ldb_dn_new_fmt(rename_test_ctx,
2450 rename_test_ctx->ldb_test_ctx->ldb,
2451 "%s", str_new_dn);
2452 assert_non_null(new_dn);
2454 bad_old_dn = ldb_dn_new_fmt(rename_test_ctx,
2455 rename_test_ctx->ldb_test_ctx->ldb,
2456 "%s", str_bad_old_dn);
2457 assert_non_null(bad_old_dn);
2459 assert_dn_doesnt_exist(rename_test_ctx->ldb_test_ctx,
2460 str_bad_old_dn);
2462 ret = ldb_rename(rename_test_ctx->ldb_test_ctx->ldb,
2463 bad_old_dn, new_dn);
2464 assert_int_equal(ret, LDB_ERR_NO_SUCH_OBJECT);
2466 assert_dn_doesnt_exist(rename_test_ctx->ldb_test_ctx,
2467 str_new_dn);
2470 static void test_ldb_rename_to_exists(void **state)
2472 struct rename_test_ctx *rename_test_ctx = talloc_get_type_abort(
2473 *state,
2474 struct rename_test_ctx);
2475 int ret;
2476 const char *str_new_dn = "dc=rename_test_already_exists";
2477 struct ldb_dn *new_dn;
2479 new_dn = ldb_dn_new_fmt(rename_test_ctx,
2480 rename_test_ctx->ldb_test_ctx->ldb,
2481 "%s", str_new_dn);
2482 assert_non_null(new_dn);
2484 add_dn_with_cn(rename_test_ctx->ldb_test_ctx,
2485 new_dn,
2486 "test_rename_cn_val");
2488 ret = ldb_rename(rename_test_ctx->ldb_test_ctx->ldb,
2489 rename_test_ctx->basedn,
2490 new_dn);
2491 assert_int_equal(ret, LDB_ERR_ENTRY_ALREADY_EXISTS);
2493 /* Old object must still exist */
2494 assert_dn_exists(rename_test_ctx->ldb_test_ctx,
2495 rename_test_ctx->str_basedn);
2497 ret = ldb_delete(rename_test_ctx->ldb_test_ctx->ldb,
2498 new_dn);
2499 assert_int_equal(ret, LDB_SUCCESS);
2501 assert_dn_exists(rename_test_ctx->ldb_test_ctx,
2502 rename_test_ctx->teardown_dn);
2505 static void test_ldb_rename_self(void **state)
2507 struct rename_test_ctx *rename_test_ctx = talloc_get_type_abort(
2508 *state,
2509 struct rename_test_ctx);
2510 int ret;
2512 /* Oddly enough, this is a success in ldb.. */
2513 ret = ldb_rename(rename_test_ctx->ldb_test_ctx->ldb,
2514 rename_test_ctx->basedn,
2515 rename_test_ctx->basedn);
2516 assert_int_equal(ret, LDB_SUCCESS);
2518 /* Old object must still exist */
2519 assert_dn_exists(rename_test_ctx->ldb_test_ctx,
2520 rename_test_ctx->str_basedn);
2523 static void test_ldb_rename_dn_case_change(void **state)
2525 struct rename_test_ctx *rename_test_ctx = talloc_get_type_abort(
2526 *state,
2527 struct rename_test_ctx);
2528 int ret;
2529 char *str_new_dn;
2530 struct ldb_dn *new_dn;
2531 unsigned i;
2533 str_new_dn = talloc_strdup(rename_test_ctx, rename_test_ctx->str_basedn);
2534 assert_non_null(str_new_dn);
2535 for (i = 0; str_new_dn[i]; i++) {
2536 str_new_dn[i] = toupper(str_new_dn[i]);
2539 new_dn = ldb_dn_new_fmt(rename_test_ctx,
2540 rename_test_ctx->ldb_test_ctx->ldb,
2541 "%s", str_new_dn);
2542 assert_non_null(new_dn);
2544 ret = ldb_rename(rename_test_ctx->ldb_test_ctx->ldb,
2545 rename_test_ctx->basedn,
2546 new_dn);
2547 assert_int_equal(ret, LDB_SUCCESS);
2549 /* DNs are case insensitive, so both searches will match */
2550 assert_dn_exists(rename_test_ctx->ldb_test_ctx, str_new_dn);
2551 assert_dn_exists(rename_test_ctx->ldb_test_ctx,
2552 rename_test_ctx->str_basedn);
2553 /* FIXME - test the values didn't change */
2556 int main(int argc, const char **argv)
2558 const struct CMUnitTest tests[] = {
2559 cmocka_unit_test_setup_teardown(test_connect,
2560 ldbtest_noconn_setup,
2561 ldbtest_noconn_teardown),
2562 cmocka_unit_test_setup_teardown(test_ldb_add,
2563 ldbtest_setup,
2564 ldbtest_teardown),
2565 cmocka_unit_test_setup_teardown(test_ldb_search,
2566 ldbtest_setup,
2567 ldbtest_teardown),
2568 cmocka_unit_test_setup_teardown(test_ldb_del,
2569 ldbtest_setup,
2570 ldbtest_teardown),
2571 cmocka_unit_test_setup_teardown(test_ldb_del_noexist,
2572 ldbtest_setup,
2573 ldbtest_teardown),
2574 cmocka_unit_test_setup_teardown(test_ldb_handle,
2575 ldbtest_setup,
2576 ldbtest_teardown),
2577 cmocka_unit_test_setup_teardown(test_ldb_build_search_req,
2578 ldbtest_setup,
2579 ldbtest_teardown),
2580 cmocka_unit_test_setup_teardown(test_transactions,
2581 ldbtest_setup,
2582 ldbtest_teardown),
2583 cmocka_unit_test_setup_teardown(test_ldb_modify_add_key,
2584 ldb_modify_test_setup,
2585 ldb_modify_test_teardown),
2586 cmocka_unit_test_setup_teardown(test_ldb_modify_extend_key,
2587 ldb_modify_test_setup,
2588 ldb_modify_test_teardown),
2589 cmocka_unit_test_setup_teardown(test_ldb_modify_add_key_noval,
2590 ldb_modify_test_setup,
2591 ldb_modify_test_teardown),
2592 cmocka_unit_test_setup_teardown(test_ldb_modify_replace_key,
2593 ldb_modify_test_setup,
2594 ldb_modify_test_teardown),
2595 cmocka_unit_test_setup_teardown(test_ldb_modify_replace_noexist_key,
2596 ldb_modify_test_setup,
2597 ldb_modify_test_teardown),
2598 cmocka_unit_test_setup_teardown(test_ldb_modify_replace_zero_vals,
2599 ldb_modify_test_setup,
2600 ldb_modify_test_teardown),
2601 cmocka_unit_test_setup_teardown(test_ldb_modify_replace_noexist_key_zero_vals,
2602 ldb_modify_test_setup,
2603 ldb_modify_test_teardown),
2604 cmocka_unit_test_setup_teardown(test_ldb_modify_del_key,
2605 ldb_modify_test_setup,
2606 ldb_modify_test_teardown),
2607 cmocka_unit_test_setup_teardown(test_ldb_modify_del_keyval,
2608 ldb_modify_test_setup,
2609 ldb_modify_test_teardown),
2610 cmocka_unit_test_setup_teardown(test_search_match_none,
2611 ldb_search_test_setup,
2612 ldb_search_test_teardown),
2613 cmocka_unit_test_setup_teardown(test_search_match_one,
2614 ldb_search_test_setup,
2615 ldb_search_test_teardown),
2616 cmocka_unit_test_setup_teardown(test_search_match_filter,
2617 ldb_search_test_setup,
2618 ldb_search_test_teardown),
2619 cmocka_unit_test_setup_teardown(test_search_match_both,
2620 ldb_search_test_setup,
2621 ldb_search_test_teardown),
2622 cmocka_unit_test_setup_teardown(test_search_match_basedn,
2623 ldb_search_test_setup,
2624 ldb_search_test_teardown),
2625 cmocka_unit_test_setup_teardown(test_ldb_search_against_transaction,
2626 ldb_search_test_setup,
2627 ldb_search_test_teardown),
2628 cmocka_unit_test_setup_teardown(test_ldb_modify_during_unindexed_search,
2629 ldb_search_test_setup,
2630 ldb_search_test_teardown),
2631 cmocka_unit_test_setup_teardown(test_ldb_modify_during_indexed_search,
2632 ldb_search_test_setup,
2633 ldb_search_test_teardown),
2634 cmocka_unit_test_setup_teardown(test_ldb_rename_during_unindexed_search,
2635 ldb_search_test_setup,
2636 ldb_search_test_teardown),
2637 cmocka_unit_test_setup_teardown(test_ldb_rename_during_indexed_search,
2638 ldb_search_test_setup,
2639 ldb_search_test_teardown),
2640 cmocka_unit_test_setup_teardown(test_ldb_modify_during_whole_search,
2641 ldb_search_test_setup,
2642 ldb_search_test_teardown),
2643 cmocka_unit_test_setup_teardown(test_ldb_attrs_case_insensitive,
2644 ldb_case_test_setup,
2645 ldb_case_test_teardown),
2646 cmocka_unit_test_setup_teardown(test_ldb_attrs_case_handler,
2647 ldb_case_test_setup,
2648 ldb_case_test_teardown),
2649 cmocka_unit_test_setup_teardown(test_ldb_attrs_index_handler,
2650 ldb_case_test_setup,
2651 ldb_case_attrs_index_test_teardown),
2652 cmocka_unit_test_setup_teardown(test_ldb_rename,
2653 ldb_rename_test_setup,
2654 ldb_rename_test_teardown),
2655 cmocka_unit_test_setup_teardown(test_ldb_rename_from_doesnt_exist,
2656 ldb_rename_test_setup,
2657 ldb_rename_test_teardown),
2658 cmocka_unit_test_setup_teardown(test_ldb_rename_to_exists,
2659 ldb_rename_test_setup,
2660 ldb_rename_test_teardown),
2661 cmocka_unit_test_setup_teardown(test_ldb_rename_self,
2662 ldb_rename_test_setup,
2663 ldb_rename_test_teardown),
2664 cmocka_unit_test_setup_teardown(test_ldb_rename_dn_case_change,
2665 ldb_rename_test_setup,
2666 ldb_rename_test_teardown),
2669 return cmocka_run_group_tests(tests, NULL, NULL);