2 * Tests exercising the ldb key value operations.
4 * Copyright (C) Andrew Bartlett <abartlet@samba.org> 2018
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 * These headers or their equivalents should be included prior to
31 * This allows test applications to use custom definitions of C standard
32 * library functions and types.
45 #define NO_FAILURE INT_MAX
46 #define FAILURE_LDB_ERR LDB_ERR_OTHER
49 * To test failure in ldb_kv_add, ldb_kv_delete, ldb_kv_modify and ldb_kv_rename
50 * we use the following global variables and macros to trigger a failure in
51 * the ldb_kv_<op>_internal functions. This allows testing of the sub
52 * transaction commits and roll backs in those operations.
54 * NOTE: Not all back ends support nested/sub transactions
56 int cmocka_unit_test_fail_add_internal_after
= NO_FAILURE
;
57 #define CMOCKA_UNIT_TEST_ADD_INTERNAL_FAIL \
59 cmocka_unit_test_fail_add_internal_after--;\
60 if (cmocka_unit_test_fail_add_internal_after <= 0) {\
61 assert_int_equal(LDB_SUCCESS, ret);\
62 ret = FAILURE_LDB_ERR;\
66 int cmocka_unit_test_fail_delete_internal_after = NO_FAILURE;
67 #define CMOCKA_UNIT_TEST_DELETE_INTERNAL_FAIL \
69 cmocka_unit_test_fail_delete_internal_after--;\
70 if (cmocka_unit_test_fail_delete_internal_after <= 0) {\
71 assert_int_equal(LDB_SUCCESS, ret);\
72 ret = FAILURE_LDB_ERR;\
76 int cmocka_unit_test_fail_rename_internal_after = NO_FAILURE;
77 #define CMOCKA_UNIT_TEST_RENAME_INTERNAL_FAIL \
79 cmocka_unit_test_fail_rename_internal_after--;\
80 if (cmocka_unit_test_fail_rename_internal_after <= 0) {\
81 assert_int_equal(LDB_SUCCESS, ret);\
82 ret = FAILURE_LDB_ERR;\
86 int cmocka_unit_test_fail_modify_internal_after = NO_FAILURE;
87 #define CMOCKA_UNIT_TEST_MODIFY_INTERNAL_FAIL \
89 cmocka_unit_test_fail_modify_internal_after--;\
90 if (cmocka_unit_test_fail_modify_internal_after <= 0) {\
91 assert_int_equal(LDB_SUCCESS, ret);\
92 ret = FAILURE_LDB_ERR;\
96 #include "ldb_key_value/ldb_kv.c"
99 #define DEFAULT_BE "tdb"
102 #define TEST_BE DEFAULT_BE
105 #define NUM_RECS 1024
109 struct tevent_context
*ev
;
110 struct ldb_context
*ldb
;
113 const char *lockfile
; /* lockfile is separate */
119 * Remove the database files
121 static void unlink_old_db(struct test_ctx
*test_ctx
)
126 ret
= unlink(test_ctx
->lockfile
);
127 if (ret
== -1 && errno
!= ENOENT
) {
132 ret
= unlink(test_ctx
->dbfile
);
133 if (ret
== -1 && errno
!= ENOENT
) {
141 static int noconn_setup(void **state
)
143 struct test_ctx
*test_ctx
;
144 cmocka_unit_test_fail_add_internal_after
= NO_FAILURE
;
145 cmocka_unit_test_fail_delete_internal_after
= NO_FAILURE
;
146 cmocka_unit_test_fail_rename_internal_after
= NO_FAILURE
;
147 cmocka_unit_test_fail_modify_internal_after
= NO_FAILURE
;
149 test_ctx
= talloc_zero(NULL
, struct test_ctx
);
150 assert_non_null(test_ctx
);
152 test_ctx
->ev
= tevent_context_init(test_ctx
);
153 assert_non_null(test_ctx
->ev
);
155 test_ctx
->ldb
= ldb_init(test_ctx
, test_ctx
->ev
);
156 assert_non_null(test_ctx
->ldb
);
158 test_ctx
->dbfile
= talloc_strdup(test_ctx
, "kvopstest.ldb");
159 assert_non_null(test_ctx
->dbfile
);
161 test_ctx
->lockfile
= talloc_asprintf(test_ctx
, "%s-lock",
163 assert_non_null(test_ctx
->lockfile
);
165 test_ctx
->dbpath
= talloc_asprintf(test_ctx
,
166 TEST_BE
"://%s", test_ctx
->dbfile
);
167 assert_non_null(test_ctx
->dbpath
);
169 unlink_old_db(test_ctx
);
177 static int noconn_teardown(void **state
)
179 struct test_ctx
*test_ctx
= talloc_get_type_abort(*state
,
182 unlink_old_db(test_ctx
);
183 talloc_free(test_ctx
);
190 static int setup(void **state
)
192 struct test_ctx
*test_ctx
;
194 struct ldb_ldif
*ldif
;
195 const char *index_ldif
= \
197 "@IDXGUID: objectUUID\n"
198 "@IDX_DN_GUID: GUID\n"
201 noconn_setup((void **) &test_ctx
);
203 ret
= ldb_connect(test_ctx
->ldb
, test_ctx
->dbpath
, 0, NULL
);
204 assert_int_equal(ret
, 0);
206 while ((ldif
= ldb_ldif_read_string(test_ctx
->ldb
, &index_ldif
))) {
207 ret
= ldb_add(test_ctx
->ldb
, ldif
->msg
);
208 assert_int_equal(ret
, LDB_SUCCESS
);
217 static int teardown(void **state
)
219 struct test_ctx
*test_ctx
= talloc_get_type_abort(*state
,
221 noconn_teardown((void **) &test_ctx
);
226 * Build an ldb_kv_context that can be passed to the ldb_kv operation under test
228 static struct ldb_kv_context
* build_ldb_kv_context(
230 struct ldb_module
*module
,
231 struct ldb_request
*req
)
233 struct ldb_kv_context
*ldb_kv_ctx
= NULL
;
235 ldb_kv_ctx
= talloc_zero(ctx
, struct ldb_kv_context
);
236 assert_non_null(ldb_kv_ctx
);
238 ldb_kv_ctx
->module
= module
;
239 ldb_kv_ctx
->req
= req
;
245 * Build an add request
247 static struct ldb_request
*build_add_request(
249 struct ldb_context
*ldb
,
255 struct ldb_message
*msg
;
256 struct ldb_request
*req
;
258 msg
= ldb_msg_new(ctx
);
259 assert_non_null(msg
);
261 msg
->dn
= ldb_dn_new_fmt(msg
, ldb
, "dc=%s", dc
);
262 assert_non_null(msg
->dn
);
264 ret
= ldb_msg_add_string(msg
, "cn", cn
);
265 assert_int_equal(ret
, 0);
267 ret
= ldb_msg_add_string(msg
, "objectUUID", uuid
);
268 assert_int_equal(ret
, 0);
270 ret
= ldb_msg_sanity_check(ldb
, msg
);
271 assert_int_equal(ret
, LDB_SUCCESS
);
273 ret
= ldb_build_add_req(
274 &req
, ldb
, ldb
, msg
, NULL
, NULL
, ldb_op_default_callback
, NULL
);
275 assert_int_equal(ret
, LDB_SUCCESS
);
280 * Build a delete request
282 static struct ldb_request
*build_delete_request(
284 struct ldb_context
*ldb
,
287 int ret
= LDB_SUCCESS
;
288 struct ldb_dn
*dn
= NULL
;
289 struct ldb_request
*req
= NULL
;
291 dn
= ldb_dn_new_fmt(ctx
, ldb
, "dc=%s", dc
);
294 ret
= ldb_build_del_req(
295 &req
, ldb
, ctx
, dn
, NULL
, NULL
, ldb_op_default_callback
, NULL
);
296 assert_int_equal(ret
, LDB_SUCCESS
);
301 * Build a rename request
303 static struct ldb_request
*build_rename_request(
305 struct ldb_context
*ldb
,
309 int ret
= LDB_SUCCESS
;
310 struct ldb_dn
*old_dn
= NULL
;
311 struct ldb_dn
*new_dn
= NULL
;
312 struct ldb_request
*req
= NULL
;
314 old_dn
= ldb_dn_new_fmt(ctx
, ldb
, "dc=%s", old_dc
);
315 assert_non_null(old_dn
);
317 new_dn
= ldb_dn_new_fmt(ctx
, ldb
, "dc=%s", new_dc
);
318 assert_non_null(new_dn
);
320 ret
= ldb_build_rename_req(
328 ldb_op_default_callback
,
330 assert_int_equal(ret
, LDB_SUCCESS
);
335 * Build a ldb modify request
337 static struct ldb_request
*build_modify_request(
339 struct ldb_context
*ldb
,
344 struct ldb_message
*msg
;
345 struct ldb_request
*req
;
347 msg
= ldb_msg_new(ctx
);
348 assert_non_null(msg
);
350 msg
->dn
= ldb_dn_new_fmt(msg
, ldb
, "dc=%s", dc
);
351 assert_non_null(msg
->dn
);
353 ret
= ldb_msg_add_empty(msg
, "cn", LDB_FLAG_MOD_REPLACE
, NULL
);
354 assert_int_equal(ret
, LDB_SUCCESS
);
355 ret
= ldb_msg_add_string(msg
, "cn", cn
);
356 assert_int_equal(ret
, LDB_SUCCESS
);
358 ret
= ldb_msg_sanity_check(ldb
, msg
);
359 assert_int_equal(ret
, LDB_SUCCESS
);
361 ret
= ldb_build_mod_req(
362 &req
, ldb
, ldb
, msg
, NULL
, NULL
, ldb_op_default_callback
, NULL
);
363 assert_int_equal(ret
, LDB_SUCCESS
);
368 * Delete a record from the database
370 static void delete_record(
372 struct ldb_context
*ldb
,
375 struct ldb_kv_context
*ldb_kv_ctx
= NULL
;
376 struct ldb_dn
*basedn
= NULL
;
377 struct ldb_result
*result
= NULL
;
378 struct ldb_request
*req
= NULL
;
379 int ret
= LDB_SUCCESS
;
381 req
= build_delete_request(ctx
, ldb
, dc
);
382 ldb_kv_ctx
= build_ldb_kv_context(ctx
, ldb
->modules
, req
);
384 ret
= ldb_transaction_start(ldb
);
385 assert_int_equal(ret
, LDB_SUCCESS
);
387 cmocka_unit_test_fail_delete_internal_after
= NO_FAILURE
;
388 cmocka_unit_test_fail_modify_internal_after
= NO_FAILURE
;
389 ret
= ldb_kv_delete(ldb_kv_ctx
);
390 assert_int_equal(ret
, LDB_SUCCESS
);
391 TALLOC_FREE(ldb_kv_ctx
);
394 ret
= ldb_transaction_commit(ldb
);
395 assert_int_equal(ret
, LDB_SUCCESS
);
398 * Ensure that the record was actually deleted.
400 basedn
= ldb_dn_new_fmt(ctx
, ldb
, "dc=%s", dc
);
401 assert_non_null(basedn
);
406 ret
= ldb_search(ldb
, ctx
, &result
, basedn
, LDB_SCOPE_BASE
, NULL
, NULL
);
407 assert_int_equal(ret
, LDB_SUCCESS
);
408 assert_non_null(result
);
409 assert_int_equal(result
->count
, 0);
415 * Add a record to the database
417 static void add_record(
419 struct ldb_context
*ldb
,
425 struct ldb_request
*req
= NULL
;
426 int ret
= LDB_SUCCESS
;
427 struct ldb_kv_context
*ldb_kv_ctx
= NULL
;
428 struct ldb_dn
*basedn
= NULL
;
429 struct ldb_result
*result
= NULL
;
431 req
= build_add_request(ctx
, ldb
, dc
, uuid
, cn
);
433 ldb_req_set_location(req
, "add_record");
435 assert_int_equal(ret
, LDB_SUCCESS
);
438 ldb_kv_ctx
= build_ldb_kv_context(ctx
, ldb
->modules
, req
);
439 cmocka_unit_test_fail_add_internal_after
= NO_FAILURE
;
440 cmocka_unit_test_fail_modify_internal_after
= NO_FAILURE
;
442 ret
= ldb_transaction_start(ldb
);
443 assert_int_equal(ret
, LDB_SUCCESS
);
445 ret
= ldb_kv_add(ldb_kv_ctx
);
446 assert_int_equal(ret
, LDB_SUCCESS
);
447 TALLOC_FREE(ldb_kv_ctx
);
450 ret
= ldb_transaction_commit(ldb
);
451 assert_int_equal(ret
, LDB_SUCCESS
);
454 * Ensure that the record was actually written.
456 basedn
= ldb_dn_new_fmt(ctx
, ldb
, "dc=%s", dc
);
457 assert_non_null(basedn
);
462 ret
= ldb_search(ldb
, ctx
, &result
, basedn
, LDB_SCOPE_BASE
, NULL
, NULL
);
463 assert_int_equal(ret
, LDB_SUCCESS
);
464 assert_non_null(result
);
465 assert_int_equal(result
->count
, 1);
470 * CN search unindexed
481 assert_int_equal(ret
, LDB_SUCCESS
);
482 assert_non_null(result
);
483 assert_int_equal(result
->count
, 1);
489 * Test that a failed add operation does not change the database.
491 static void test_add_failure(void **state
)
493 int ret
= LDB_SUCCESS
;
494 struct test_ctx
*test_ctx
= talloc_get_type_abort(*state
,
496 struct ldb_request
*req
= NULL
;
497 struct ldb_dn
*basedn
= NULL
;
498 struct ldb_result
*result
= NULL
;
499 struct ldb_kv_context
*ldb_kv_ctx
= NULL
;
501 TALLOC_CTX
*tmp_ctx
= talloc_new(test_ctx
);
502 assert_non_null(tmp_ctx
);
504 req
= build_add_request(
509 "test_add_failure_value");
511 ldb_req_set_location(req
, "test_add_failure");
513 ldb_kv_ctx
= build_ldb_kv_context(tmp_ctx
, test_ctx
->ldb
->modules
, req
);
514 cmocka_unit_test_fail_add_internal_after
= 1;
516 ret
= ldb_transaction_start(test_ctx
->ldb
);
517 assert_int_equal(ret
, LDB_SUCCESS
);
519 ret
= ldb_kv_add(ldb_kv_ctx
);
521 assert_int_equal(ret
, FAILURE_LDB_ERR
);
522 TALLOC_FREE(ldb_kv_ctx
);
527 * a search for "cn=test_add_failure_value" should fail
528 * as the transaction containing the operation should have been
529 * rolled back leaving the database consistent
531 * This should be an un-indexed search so the index caches won't be
534 basedn
= ldb_dn_new_fmt(
539 assert_non_null(basedn
);
542 test_ctx
->ldb
, tmp_ctx
,
548 "test_add_failure_value");
549 assert_int_equal(ret
, LDB_SUCCESS
);
550 assert_non_null(result
);
551 assert_int_equal(result
->count
, 0);
555 ldb_transaction_cancel(test_ctx
->ldb
);
556 TALLOC_FREE(tmp_ctx
);
561 * Test that a failed delete operation does not modify the database.
563 static void test_delete_failure(void **state
)
565 int ret
= LDB_SUCCESS
;
566 struct test_ctx
*test_ctx
= talloc_get_type_abort(*state
,
568 struct ldb_request
*req
= NULL
;
569 struct ldb_dn
*basedn
= NULL
;
570 struct ldb_result
*result
= NULL
;
571 struct ldb_kv_context
*ldb_kv_ctx
= NULL
;
573 TALLOC_CTX
*tmp_ctx
= talloc_new(test_ctx
);
574 assert_non_null(tmp_ctx
);
579 "test_delete_failure",
581 "test_delete_failure_value");
583 req
= build_delete_request(
586 "test_delete_failure");
588 ldb_kv_ctx
= build_ldb_kv_context(tmp_ctx
, test_ctx
->ldb
->modules
, req
);
589 cmocka_unit_test_fail_delete_internal_after
= 1;
591 ret
= ldb_transaction_start(test_ctx
->ldb
);
592 assert_int_equal(ret
, LDB_SUCCESS
);
594 ret
= ldb_kv_delete(ldb_kv_ctx
);
595 assert_int_equal(ret
, FAILURE_LDB_ERR
);
596 TALLOC_FREE(ldb_kv_ctx
);
600 * a search for "cn=test_add_failure_value" should succeed
601 * as the transaction containing the operation should have been
602 * rolled back leaving the database consistent
604 * This should be an un-indexed search so the index caches won't be
607 basedn
= ldb_dn_new_fmt(
611 "test_delete_failure");
612 assert_non_null(basedn
);
615 test_ctx
->ldb
, tmp_ctx
,
621 "test_delete_failure_value");
622 assert_int_equal(ret
, LDB_SUCCESS
);
623 assert_non_null(result
);
624 assert_int_equal(result
->count
, 1);
629 ldb_transaction_cancel(test_ctx
->ldb
);
633 "test_delete_failure");
634 TALLOC_FREE(tmp_ctx
);
638 * Test that a failed rename operation dies not change the database
640 static void test_rename_failure(void **state
)
642 int ret
= LDB_SUCCESS
;
643 struct test_ctx
*test_ctx
= talloc_get_type_abort(*state
,
645 struct ldb_request
*req
= NULL
;
646 struct ldb_dn
*basedn
= NULL
;
647 struct ldb_result
*result
= NULL
;
648 struct ldb_kv_context
*ldb_kv_ctx
= NULL
;
650 TALLOC_CTX
*tmp_ctx
= talloc_new(test_ctx
);
651 assert_non_null(tmp_ctx
);
656 "test_rename_failure",
658 "test_rename_failure_value");
660 req
= build_rename_request(
663 "test_rename_failure",
664 "test_rename_failure_renamed");
666 ldb_kv_ctx
= build_ldb_kv_context(tmp_ctx
, test_ctx
->ldb
->modules
, req
);
667 cmocka_unit_test_fail_rename_internal_after
= 1;
669 ret
= ldb_transaction_start(test_ctx
->ldb
);
670 assert_int_equal(ret
, LDB_SUCCESS
);
672 ret
= ldb_kv_rename(ldb_kv_ctx
);
673 assert_int_equal(ret
, FAILURE_LDB_ERR
);
674 TALLOC_FREE(ldb_kv_ctx
);
678 * The original record should be present
680 basedn
= ldb_dn_new_fmt(
684 "test_rename_failure");
685 assert_non_null(basedn
);
688 test_ctx
->ldb
, tmp_ctx
,
694 "test_rename_failure_value");
695 assert_int_equal(ret
, LDB_SUCCESS
);
696 assert_non_null(result
);
697 assert_int_equal(result
->count
, 1);
702 * And the renamed record should not be present
704 basedn
= ldb_dn_new_fmt(
708 "test_rename_failure_renamed");
709 assert_non_null(basedn
);
712 test_ctx
->ldb
, tmp_ctx
,
718 "test_rename_failure_value");
719 assert_int_equal(ret
, LDB_SUCCESS
);
720 assert_non_null(result
);
721 assert_int_equal(result
->count
, 0);
725 ldb_transaction_cancel(test_ctx
->ldb
);
729 "test_rename_failure");
730 TALLOC_FREE(tmp_ctx
);
734 * Test that a failed modification operation does not change the database
736 static void test_modify_failure(void **state
)
738 int ret
= LDB_SUCCESS
;
739 struct test_ctx
*test_ctx
= talloc_get_type_abort(*state
,
741 struct ldb_request
*req
= NULL
;
742 struct ldb_dn
*basedn
= NULL
;
743 struct ldb_result
*result
= NULL
;
744 struct ldb_kv_context
*ldb_kv_ctx
= NULL
;
746 TALLOC_CTX
*tmp_ctx
= talloc_new(test_ctx
);
747 assert_non_null(tmp_ctx
);
752 "test_modify_failure",
754 "test_modify_failure_value");
756 req
= build_modify_request(
759 "test_modify_failure",
760 "test_modify_failure_value_modified");
762 ldb_kv_ctx
= build_ldb_kv_context(tmp_ctx
, test_ctx
->ldb
->modules
, req
);
763 cmocka_unit_test_fail_modify_internal_after
= 2;
765 ret
= ldb_transaction_start(test_ctx
->ldb
);
766 assert_int_equal(ret
, LDB_SUCCESS
);
768 ret
= ldb_kv_modify(ldb_kv_ctx
);
769 assert_int_equal(ret
, FAILURE_LDB_ERR
);
770 TALLOC_FREE(ldb_kv_ctx
);
775 * The original value should be present
777 basedn
= ldb_dn_new_fmt(
781 "test_modify_failure");
782 assert_non_null(basedn
);
785 test_ctx
->ldb
, tmp_ctx
,
791 "test_modify_failure_value");
792 assert_int_equal(ret
, LDB_SUCCESS
);
793 assert_non_null(result
);
794 assert_int_equal(result
->count
, 1);
798 * And the modified record should not be present
801 test_ctx
->ldb
, tmp_ctx
,
807 "test_modify_failure_value_modified");
808 assert_int_equal(ret
, LDB_SUCCESS
);
809 assert_non_null(result
);
810 assert_int_equal(result
->count
, 0);
814 ldb_transaction_cancel(test_ctx
->ldb
);
818 "test_modify_failure");
819 TALLOC_FREE(tmp_ctx
);
822 int main(int argc
, const char **argv
)
824 const struct CMUnitTest tests
[] = {
825 cmocka_unit_test_setup_teardown(
829 cmocka_unit_test_setup_teardown(
833 cmocka_unit_test_setup_teardown(
837 cmocka_unit_test_setup_teardown(
843 cmocka_set_message_output(CM_OUTPUT_SUBUNIT
);
845 return cmocka_run_group_tests(tests
, NULL
, NULL
);