ldb:tests: Show that writes do not appear during an ldb_search()
[Samba.git] / lib / ldb / tests / ldb_mod_op_test.c
blob475138e403e4a21933de2b02669102db236015f6
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);
1850 static int ldb_case_test_setup(void **state)
1852 int ret;
1853 struct ldb_ldif *ldif;
1854 struct ldbtest_ctx *ldb_test_ctx;
1855 const char *attrs_ldif = \
1856 "dn: @ATTRIBUTES\n"
1857 "cn: CASE_INSENSITIVE\n"
1858 "\n";
1859 struct keyval kvs[] = {
1860 { "cn", "CaseInsensitiveValue" },
1861 { "uid", "CaseSensitiveValue" },
1862 { NULL, NULL },
1866 ldbtest_setup((void **) &ldb_test_ctx);
1868 while ((ldif = ldb_ldif_read_string(ldb_test_ctx->ldb, &attrs_ldif))) {
1869 ret = ldb_add(ldb_test_ctx->ldb, ldif->msg);
1870 assert_int_equal(ret, LDB_SUCCESS);
1873 ldb_test_add_data(ldb_test_ctx,
1874 ldb_test_ctx,
1875 "cn=CaseInsensitiveValue",
1876 kvs);
1878 *state = ldb_test_ctx;
1879 return 0;
1882 static int ldb_case_test_teardown(void **state)
1884 int ret;
1885 struct ldbtest_ctx *ldb_test_ctx = talloc_get_type_abort(*state,
1886 struct ldbtest_ctx);
1888 struct ldb_dn *del_dn;
1890 del_dn = ldb_dn_new_fmt(ldb_test_ctx,
1891 ldb_test_ctx->ldb,
1892 "@ATTRIBUTES");
1893 assert_non_null(del_dn);
1895 ret = ldb_delete(ldb_test_ctx->ldb, del_dn);
1896 assert_int_equal(ret, LDB_SUCCESS);
1898 assert_dn_doesnt_exist(ldb_test_ctx,
1899 "@ATTRIBUTES");
1901 ldb_test_remove_data(ldb_test_ctx, ldb_test_ctx,
1902 "cn=CaseInsensitiveValue");
1904 ldbtest_teardown((void **) &ldb_test_ctx);
1905 return 0;
1908 static void test_ldb_attrs_case_insensitive(void **state)
1910 int cnt;
1911 struct ldbtest_ctx *ldb_test_ctx = talloc_get_type_abort(*state,
1912 struct ldbtest_ctx);
1914 /* cn matches exact case */
1915 cnt = sub_search_count(ldb_test_ctx, "", "cn=CaseInsensitiveValue");
1916 assert_int_equal(cnt, 1);
1918 /* cn matches lower case */
1919 cnt = sub_search_count(ldb_test_ctx, "", "cn=caseinsensitivevalue");
1920 assert_int_equal(cnt, 1);
1922 /* uid matches exact case */
1923 cnt = sub_search_count(ldb_test_ctx, "", "uid=CaseSensitiveValue");
1924 assert_int_equal(cnt, 1);
1926 /* uid does not match lower case */
1927 cnt = sub_search_count(ldb_test_ctx, "", "uid=casesensitivevalue");
1928 assert_int_equal(cnt, 0);
1931 static struct ldb_schema_attribute cn_attr_1;
1932 static struct ldb_schema_attribute cn_attr_2;
1933 static struct ldb_schema_attribute default_attr;
1936 override the name to attribute handler function
1938 static const struct ldb_schema_attribute *ldb_test_attribute_handler_override(struct ldb_context *ldb,
1939 void *private_data,
1940 const char *name)
1942 if (private_data != NULL && ldb_attr_cmp(name, "cn") == 0) {
1943 return &cn_attr_1;
1944 } else if (private_data == NULL && ldb_attr_cmp(name, "cn") == 0) {
1945 return &cn_attr_2;
1946 } else if (ldb_attr_cmp(name, "uid") == 0) {
1947 return &cn_attr_2;
1949 return &default_attr;
1952 static void test_ldb_attrs_case_handler(void **state)
1954 int cnt;
1955 int ret;
1956 const struct ldb_schema_syntax *syntax;
1958 struct ldbtest_ctx *ldb_test_ctx = talloc_get_type_abort(*state,
1959 struct ldbtest_ctx);
1960 struct ldb_context *ldb = ldb_test_ctx->ldb;
1962 /* cn matches lower case */
1963 cnt = sub_search_count(ldb_test_ctx, "", "cn=caseinsensitivevalue");
1964 assert_int_equal(cnt, 1);
1966 syntax = ldb_standard_syntax_by_name(ldb, LDB_SYNTAX_OCTET_STRING);
1967 assert_non_null(syntax);
1969 ret = ldb_schema_attribute_fill_with_syntax(ldb, ldb,
1970 "*", 0,
1971 syntax, &default_attr);
1972 assert_int_equal(ret, LDB_SUCCESS);
1974 syntax = ldb_standard_syntax_by_name(ldb, LDB_SYNTAX_OCTET_STRING);
1975 assert_non_null(syntax);
1977 ret = ldb_schema_attribute_fill_with_syntax(ldb, ldb,
1978 "cn", 0,
1979 syntax, &cn_attr_1);
1980 assert_int_equal(ret, LDB_SUCCESS);
1983 * Set an attribute handler, which will fail to match as we
1984 * force case sensitive
1986 ldb_schema_attribute_set_override_handler(ldb,
1987 ldb_test_attribute_handler_override,
1988 (void *)1);
1990 /* cn does not matche lower case */
1991 cnt = sub_search_count(ldb_test_ctx, "", "cn=caseinsensitivevalue");
1992 assert_int_equal(cnt, 0);
1994 syntax = ldb_standard_syntax_by_name(ldb, LDB_SYNTAX_DIRECTORY_STRING);
1995 assert_non_null(syntax);
1997 ret = ldb_schema_attribute_fill_with_syntax(ldb, ldb,
1998 "cn", 0,
1999 syntax, &cn_attr_2);
2000 assert_int_equal(ret, LDB_SUCCESS);
2003 * Set an attribute handler, which will match as we
2004 * force case insensitive
2006 ldb_schema_attribute_set_override_handler(ldb,
2007 ldb_test_attribute_handler_override,
2008 NULL);
2010 /* cn matches lower case */
2011 cnt = sub_search_count(ldb_test_ctx, "", "cn=caseinsensitivevalue");
2012 assert_int_equal(cnt, 1);
2017 static void test_ldb_attrs_index_handler(void **state)
2019 int cnt;
2020 int ret;
2021 const struct ldb_schema_syntax *syntax;
2022 struct ldb_ldif *ldif;
2024 const char *index_ldif = \
2025 "dn: @INDEXLIST\n"
2026 "@IDXATTR: cn\n"
2027 "\n";
2029 struct ldbtest_ctx *ldb_test_ctx = talloc_get_type_abort(*state,
2030 struct ldbtest_ctx);
2031 struct ldb_context *ldb = ldb_test_ctx->ldb;
2033 /* cn matches lower case */
2034 cnt = sub_search_count(ldb_test_ctx, "", "cn=caseinsensitivevalue");
2035 assert_int_equal(cnt, 1);
2037 syntax = ldb_standard_syntax_by_name(ldb, LDB_SYNTAX_OCTET_STRING);
2038 assert_non_null(syntax);
2040 ret = ldb_schema_attribute_fill_with_syntax(ldb, ldb,
2041 "cn", 0,
2042 syntax, &cn_attr_1);
2043 assert_int_equal(ret, LDB_SUCCESS);
2045 syntax = ldb_standard_syntax_by_name(ldb, LDB_SYNTAX_DIRECTORY_STRING);
2046 assert_non_null(syntax);
2048 ret = ldb_schema_attribute_fill_with_syntax(ldb, ldb,
2049 "cn", LDB_ATTR_FLAG_INDEXED,
2050 syntax, &cn_attr_2);
2051 assert_int_equal(ret, LDB_SUCCESS);
2054 * Set an attribute handler
2056 ldb_schema_attribute_set_override_handler(ldb,
2057 ldb_test_attribute_handler_override,
2058 NULL);
2060 /* cn matches lower case */
2061 cnt = sub_search_count(ldb_test_ctx, "", "cn=caseinsensitivevalue");
2062 assert_int_equal(cnt, 1);
2064 /* Add the index (actually any modify will do) */
2065 while ((ldif = ldb_ldif_read_string(ldb_test_ctx->ldb, &index_ldif))) {
2066 ret = ldb_add(ldb_test_ctx->ldb, ldif->msg);
2067 assert_int_equal(ret, LDB_SUCCESS);
2070 ldb_schema_set_override_indexlist(ldb, false);
2072 /* cn does match as there is an index now */
2073 cnt = sub_search_count(ldb_test_ctx, "", "cn=caseinsensitivevalue");
2074 assert_int_equal(cnt, 1);
2077 * Set an attribute handler, which will later fail to match as we
2078 * didn't re-index the DB
2080 ldb_schema_attribute_set_override_handler(ldb,
2081 ldb_test_attribute_handler_override,
2082 (void *)1);
2085 * cn does not match as we changed the case sensitivity, but
2086 * didn't re-index
2088 * This shows that the override is in control
2090 cnt = sub_search_count(ldb_test_ctx, "", "cn=caseinsensitivevalue");
2091 assert_int_equal(cnt, 0);
2095 static int ldb_case_attrs_index_test_teardown(void **state)
2097 int ret;
2098 struct ldbtest_ctx *ldb_test_ctx = talloc_get_type_abort(*state,
2099 struct ldbtest_ctx);
2100 struct ldb_dn *del_dn;
2102 del_dn = ldb_dn_new_fmt(ldb_test_ctx,
2103 ldb_test_ctx->ldb,
2104 "@INDEXLIST");
2105 assert_non_null(del_dn);
2107 ret = ldb_delete(ldb_test_ctx->ldb, del_dn);
2108 if (ret != LDB_ERR_NO_SUCH_OBJECT) {
2109 assert_int_equal(ret, LDB_SUCCESS);
2112 assert_dn_doesnt_exist(ldb_test_ctx,
2113 "@INDEXLIST");
2115 ldb_case_test_teardown(state);
2116 return 0;
2120 struct rename_test_ctx {
2121 struct ldbtest_ctx *ldb_test_ctx;
2123 struct ldb_dn *basedn;
2124 const char *str_basedn;
2126 const char *teardown_dn;
2129 static int ldb_rename_test_setup(void **state)
2131 struct ldbtest_ctx *ldb_test_ctx;
2132 struct rename_test_ctx *rename_test_ctx;
2133 const char *strdn = "dc=rename_test_entry_from";
2135 ldbtest_setup((void **) &ldb_test_ctx);
2137 rename_test_ctx = talloc(ldb_test_ctx, struct rename_test_ctx);
2138 assert_non_null(rename_test_ctx);
2139 rename_test_ctx->ldb_test_ctx = ldb_test_ctx;
2140 assert_non_null(rename_test_ctx->ldb_test_ctx);
2142 rename_test_ctx->basedn = ldb_dn_new_fmt(rename_test_ctx,
2143 rename_test_ctx->ldb_test_ctx->ldb,
2144 "%s", strdn);
2145 assert_non_null(rename_test_ctx->basedn);
2147 rename_test_ctx->str_basedn = strdn;
2148 rename_test_ctx->teardown_dn = strdn;
2150 add_dn_with_cn(ldb_test_ctx,
2151 rename_test_ctx->basedn,
2152 "test_rename_cn_val");
2154 *state = rename_test_ctx;
2155 return 0;
2158 static int ldb_rename_test_teardown(void **state)
2160 int ret;
2161 struct rename_test_ctx *rename_test_ctx = talloc_get_type_abort(*state,
2162 struct rename_test_ctx);
2163 struct ldbtest_ctx *ldb_test_ctx;
2164 struct ldb_dn *del_dn;
2166 ldb_test_ctx = rename_test_ctx->ldb_test_ctx;
2168 del_dn = ldb_dn_new_fmt(rename_test_ctx,
2169 rename_test_ctx->ldb_test_ctx->ldb,
2170 "%s", rename_test_ctx->teardown_dn);
2171 assert_non_null(del_dn);
2173 ret = ldb_delete(ldb_test_ctx->ldb, del_dn);
2174 assert_int_equal(ret, LDB_SUCCESS);
2176 assert_dn_doesnt_exist(ldb_test_ctx,
2177 rename_test_ctx->teardown_dn);
2179 ldbtest_teardown((void **) &ldb_test_ctx);
2180 return 0;
2183 static void test_ldb_rename(void **state)
2185 struct rename_test_ctx *rename_test_ctx =
2186 talloc_get_type_abort(*state, struct rename_test_ctx);
2187 int ret;
2188 const char *str_new_dn = "dc=rename_test_entry_to";
2189 struct ldb_dn *new_dn;
2191 new_dn = ldb_dn_new_fmt(rename_test_ctx,
2192 rename_test_ctx->ldb_test_ctx->ldb,
2193 "%s", str_new_dn);
2194 assert_non_null(new_dn);
2196 ret = ldb_rename(rename_test_ctx->ldb_test_ctx->ldb,
2197 rename_test_ctx->basedn,
2198 new_dn);
2199 assert_int_equal(ret, LDB_SUCCESS);
2201 assert_dn_exists(rename_test_ctx->ldb_test_ctx, str_new_dn);
2202 assert_dn_doesnt_exist(rename_test_ctx->ldb_test_ctx,
2203 rename_test_ctx->str_basedn);
2204 rename_test_ctx->teardown_dn = str_new_dn;
2206 /* FIXME - test the values which didn't change */
2209 static void test_ldb_rename_from_doesnt_exist(void **state)
2211 struct rename_test_ctx *rename_test_ctx = talloc_get_type_abort(
2212 *state,
2213 struct rename_test_ctx);
2214 int ret;
2215 const char *str_new_dn = "dc=rename_test_entry_to";
2216 const char *str_bad_old_dn = "dc=rename_test_no_such_entry";
2217 struct ldb_dn *new_dn;
2218 struct ldb_dn *bad_old_dn;
2220 new_dn = ldb_dn_new_fmt(rename_test_ctx,
2221 rename_test_ctx->ldb_test_ctx->ldb,
2222 "%s", str_new_dn);
2223 assert_non_null(new_dn);
2225 bad_old_dn = ldb_dn_new_fmt(rename_test_ctx,
2226 rename_test_ctx->ldb_test_ctx->ldb,
2227 "%s", str_bad_old_dn);
2228 assert_non_null(bad_old_dn);
2230 assert_dn_doesnt_exist(rename_test_ctx->ldb_test_ctx,
2231 str_bad_old_dn);
2233 ret = ldb_rename(rename_test_ctx->ldb_test_ctx->ldb,
2234 bad_old_dn, new_dn);
2235 assert_int_equal(ret, LDB_ERR_NO_SUCH_OBJECT);
2237 assert_dn_doesnt_exist(rename_test_ctx->ldb_test_ctx,
2238 str_new_dn);
2241 static void test_ldb_rename_to_exists(void **state)
2243 struct rename_test_ctx *rename_test_ctx = talloc_get_type_abort(
2244 *state,
2245 struct rename_test_ctx);
2246 int ret;
2247 const char *str_new_dn = "dc=rename_test_already_exists";
2248 struct ldb_dn *new_dn;
2250 new_dn = ldb_dn_new_fmt(rename_test_ctx,
2251 rename_test_ctx->ldb_test_ctx->ldb,
2252 "%s", str_new_dn);
2253 assert_non_null(new_dn);
2255 add_dn_with_cn(rename_test_ctx->ldb_test_ctx,
2256 new_dn,
2257 "test_rename_cn_val");
2259 ret = ldb_rename(rename_test_ctx->ldb_test_ctx->ldb,
2260 rename_test_ctx->basedn,
2261 new_dn);
2262 assert_int_equal(ret, LDB_ERR_ENTRY_ALREADY_EXISTS);
2264 /* Old object must still exist */
2265 assert_dn_exists(rename_test_ctx->ldb_test_ctx,
2266 rename_test_ctx->str_basedn);
2268 ret = ldb_delete(rename_test_ctx->ldb_test_ctx->ldb,
2269 new_dn);
2270 assert_int_equal(ret, LDB_SUCCESS);
2272 assert_dn_exists(rename_test_ctx->ldb_test_ctx,
2273 rename_test_ctx->teardown_dn);
2276 static void test_ldb_rename_self(void **state)
2278 struct rename_test_ctx *rename_test_ctx = talloc_get_type_abort(
2279 *state,
2280 struct rename_test_ctx);
2281 int ret;
2283 /* Oddly enough, this is a success in ldb.. */
2284 ret = ldb_rename(rename_test_ctx->ldb_test_ctx->ldb,
2285 rename_test_ctx->basedn,
2286 rename_test_ctx->basedn);
2287 assert_int_equal(ret, LDB_SUCCESS);
2289 /* Old object must still exist */
2290 assert_dn_exists(rename_test_ctx->ldb_test_ctx,
2291 rename_test_ctx->str_basedn);
2294 static void test_ldb_rename_dn_case_change(void **state)
2296 struct rename_test_ctx *rename_test_ctx = talloc_get_type_abort(
2297 *state,
2298 struct rename_test_ctx);
2299 int ret;
2300 char *str_new_dn;
2301 struct ldb_dn *new_dn;
2302 unsigned i;
2304 str_new_dn = talloc_strdup(rename_test_ctx, rename_test_ctx->str_basedn);
2305 assert_non_null(str_new_dn);
2306 for (i = 0; str_new_dn[i]; i++) {
2307 str_new_dn[i] = toupper(str_new_dn[i]);
2310 new_dn = ldb_dn_new_fmt(rename_test_ctx,
2311 rename_test_ctx->ldb_test_ctx->ldb,
2312 "%s", str_new_dn);
2313 assert_non_null(new_dn);
2315 ret = ldb_rename(rename_test_ctx->ldb_test_ctx->ldb,
2316 rename_test_ctx->basedn,
2317 new_dn);
2318 assert_int_equal(ret, LDB_SUCCESS);
2320 /* DNs are case insensitive, so both searches will match */
2321 assert_dn_exists(rename_test_ctx->ldb_test_ctx, str_new_dn);
2322 assert_dn_exists(rename_test_ctx->ldb_test_ctx,
2323 rename_test_ctx->str_basedn);
2324 /* FIXME - test the values didn't change */
2327 int main(int argc, const char **argv)
2329 const struct CMUnitTest tests[] = {
2330 cmocka_unit_test_setup_teardown(test_connect,
2331 ldbtest_noconn_setup,
2332 ldbtest_noconn_teardown),
2333 cmocka_unit_test_setup_teardown(test_ldb_add,
2334 ldbtest_setup,
2335 ldbtest_teardown),
2336 cmocka_unit_test_setup_teardown(test_ldb_search,
2337 ldbtest_setup,
2338 ldbtest_teardown),
2339 cmocka_unit_test_setup_teardown(test_ldb_del,
2340 ldbtest_setup,
2341 ldbtest_teardown),
2342 cmocka_unit_test_setup_teardown(test_ldb_del_noexist,
2343 ldbtest_setup,
2344 ldbtest_teardown),
2345 cmocka_unit_test_setup_teardown(test_ldb_handle,
2346 ldbtest_setup,
2347 ldbtest_teardown),
2348 cmocka_unit_test_setup_teardown(test_ldb_build_search_req,
2349 ldbtest_setup,
2350 ldbtest_teardown),
2351 cmocka_unit_test_setup_teardown(test_transactions,
2352 ldbtest_setup,
2353 ldbtest_teardown),
2354 cmocka_unit_test_setup_teardown(test_ldb_modify_add_key,
2355 ldb_modify_test_setup,
2356 ldb_modify_test_teardown),
2357 cmocka_unit_test_setup_teardown(test_ldb_modify_extend_key,
2358 ldb_modify_test_setup,
2359 ldb_modify_test_teardown),
2360 cmocka_unit_test_setup_teardown(test_ldb_modify_add_key_noval,
2361 ldb_modify_test_setup,
2362 ldb_modify_test_teardown),
2363 cmocka_unit_test_setup_teardown(test_ldb_modify_replace_key,
2364 ldb_modify_test_setup,
2365 ldb_modify_test_teardown),
2366 cmocka_unit_test_setup_teardown(test_ldb_modify_replace_noexist_key,
2367 ldb_modify_test_setup,
2368 ldb_modify_test_teardown),
2369 cmocka_unit_test_setup_teardown(test_ldb_modify_replace_zero_vals,
2370 ldb_modify_test_setup,
2371 ldb_modify_test_teardown),
2372 cmocka_unit_test_setup_teardown(test_ldb_modify_replace_noexist_key_zero_vals,
2373 ldb_modify_test_setup,
2374 ldb_modify_test_teardown),
2375 cmocka_unit_test_setup_teardown(test_ldb_modify_del_key,
2376 ldb_modify_test_setup,
2377 ldb_modify_test_teardown),
2378 cmocka_unit_test_setup_teardown(test_ldb_modify_del_keyval,
2379 ldb_modify_test_setup,
2380 ldb_modify_test_teardown),
2381 cmocka_unit_test_setup_teardown(test_search_match_none,
2382 ldb_search_test_setup,
2383 ldb_search_test_teardown),
2384 cmocka_unit_test_setup_teardown(test_search_match_one,
2385 ldb_search_test_setup,
2386 ldb_search_test_teardown),
2387 cmocka_unit_test_setup_teardown(test_search_match_filter,
2388 ldb_search_test_setup,
2389 ldb_search_test_teardown),
2390 cmocka_unit_test_setup_teardown(test_search_match_both,
2391 ldb_search_test_setup,
2392 ldb_search_test_teardown),
2393 cmocka_unit_test_setup_teardown(test_search_match_basedn,
2394 ldb_search_test_setup,
2395 ldb_search_test_teardown),
2396 cmocka_unit_test_setup_teardown(test_ldb_search_against_transaction,
2397 ldb_search_test_setup,
2398 ldb_search_test_teardown),
2399 cmocka_unit_test_setup_teardown(test_ldb_modify_during_unindexed_search,
2400 ldb_search_test_setup,
2401 ldb_search_test_teardown),
2402 cmocka_unit_test_setup_teardown(test_ldb_modify_during_indexed_search,
2403 ldb_search_test_setup,
2404 ldb_search_test_teardown),
2405 cmocka_unit_test_setup_teardown(test_ldb_rename_during_unindexed_search,
2406 ldb_search_test_setup,
2407 ldb_search_test_teardown),
2408 cmocka_unit_test_setup_teardown(test_ldb_rename_during_indexed_search,
2409 ldb_search_test_setup,
2410 ldb_search_test_teardown),
2411 cmocka_unit_test_setup_teardown(test_ldb_attrs_case_insensitive,
2412 ldb_case_test_setup,
2413 ldb_case_test_teardown),
2414 cmocka_unit_test_setup_teardown(test_ldb_attrs_case_handler,
2415 ldb_case_test_setup,
2416 ldb_case_test_teardown),
2417 cmocka_unit_test_setup_teardown(test_ldb_attrs_index_handler,
2418 ldb_case_test_setup,
2419 ldb_case_attrs_index_test_teardown),
2420 cmocka_unit_test_setup_teardown(test_ldb_rename,
2421 ldb_rename_test_setup,
2422 ldb_rename_test_teardown),
2423 cmocka_unit_test_setup_teardown(test_ldb_rename_from_doesnt_exist,
2424 ldb_rename_test_setup,
2425 ldb_rename_test_teardown),
2426 cmocka_unit_test_setup_teardown(test_ldb_rename_to_exists,
2427 ldb_rename_test_setup,
2428 ldb_rename_test_teardown),
2429 cmocka_unit_test_setup_teardown(test_ldb_rename_self,
2430 ldb_rename_test_setup,
2431 ldb_rename_test_teardown),
2432 cmocka_unit_test_setup_teardown(test_ldb_rename_dn_case_change,
2433 ldb_rename_test_setup,
2434 ldb_rename_test_teardown),
2437 return cmocka_run_group_tests(tests, NULL, NULL);