From 15ad5de6aa72955042ff43548448c7a9e0c5d373 Mon Sep 17 00:00:00 2001 From: Jakub Hrozek Date: Sat, 17 Jan 2015 18:06:09 +0100 Subject: [PATCH] ldb:tests: Add a simple cmocka test for ldb_connect() Signed-off-by: Jakub Hrozek Reviewed-by: Andreas Schneider Reviewed-by: Andrew Bartlet --- lib/ldb/tests/ldb_mod_op_test.c | 107 ++++++++++++++++++++++++++++++++ 1 file changed, 107 insertions(+) create mode 100644 lib/ldb/tests/ldb_mod_op_test.c diff --git a/lib/ldb/tests/ldb_mod_op_test.c b/lib/ldb/tests/ldb_mod_op_test.c new file mode 100644 index 00000000000..afd47a9773d --- /dev/null +++ b/lib/ldb/tests/ldb_mod_op_test.c @@ -0,0 +1,107 @@ +/* + * from cmocka.c: + * These headers or their equivalents should be included prior to + * including + * this header file. + * + * #include + * #include + * #include + * + * This allows test applications to use custom definitions of C standard + * library functions and types. + */ +#include +#include +#include +#include + +#include + +#include +#include +#include +#include + +#define DEFAULT_BE "tdb" + +#ifndef TEST_BE +#define TEST_BE DEFAULT_BE +#endif /* TEST_BE */ + +struct ldbtest_ctx { + struct tevent_context *ev; + struct ldb_context *ldb; + + const char *dbfile; + const char *lockfile; + + const char *dbpath; + const char *lockpath; /* lockfile is separate */ +}; + +static int ldbtest_noconn_setup(void **state) +{ + struct ldbtest_ctx *test_ctx; + + test_ctx = talloc_zero(NULL, struct ldbtest_ctx); + assert_non_null(test_ctx); + + test_ctx->ev = tevent_context_init(test_ctx); + assert_non_null(test_ctx->ev); + + test_ctx->ldb = ldb_init(test_ctx, test_ctx->ev); + assert_non_null(test_ctx->ldb); + + test_ctx->dbfile = talloc_strdup(test_ctx, "apitest.ldb"); + assert_non_null(test_ctx->dbfile); + + test_ctx->lockfile = talloc_asprintf(test_ctx, + "%s-lock", test_ctx->dbfile); + assert_non_null(test_ctx->lockfile); + + test_ctx->dbpath = talloc_asprintf(test_ctx, + TEST_BE"://%s", test_ctx->dbfile); + assert_non_null(test_ctx->dbpath); + + test_ctx->lockpath = talloc_asprintf(test_ctx, + "%s-lock", test_ctx->dbpath); + assert_non_null(test_ctx->lockpath); + + *state = test_ctx; + return 0; +} + +static int ldbtest_noconn_teardown(void **state) +{ + struct ldbtest_ctx *test_ctx = talloc_get_type_abort(*state, + struct ldbtest_ctx); + + unlink(test_ctx->lockfile); + + unlink(test_ctx->dbfile); + + talloc_free(test_ctx); + return 0; +} + +static void test_connect(void **state) +{ + struct ldbtest_ctx *test_ctx = talloc_get_type_abort(*state, + struct ldbtest_ctx); + int ret; + + ret = ldb_connect(test_ctx->ldb, test_ctx->dbpath, 0, NULL); + assert_int_equal(ret, 0); +} + +int main(int argc, const char **argv) +{ + const struct CMUnitTest tests[] = { + cmocka_unit_test_setup_teardown(test_connect, + ldbtest_noconn_setup, + ldbtest_noconn_teardown), + }; + + return cmocka_run_group_tests(tests, NULL, NULL); +} -- 2.34.1