s3:utils: Fix 'Usage:' for 'net ads enctypes'
[samba4-gss.git] / lib / ldb / tests / ldb_no_lmdb_test.c
blob4fca6634d598f09e38414d3a05f75d077c5475f5
1 /*
2 * Ensure lmdb backend is disabled
4 * Copyright (C) Mathieu Parent <math.parent@gmail.com> 2019
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/>.
22 * Ensure lmdb backend is disabled
24 * Setup and tear down code copied from ldb_lmdb_test.c
28 * from cmocka.c:
29 * These headers or their equivalents should be included prior to
30 * including
31 * this header file.
33 * #include <stdarg.h>
34 * #include <stddef.h>
35 * #include <setjmp.h>
37 * This allows test applications to use custom definitions of C standard
38 * library functions and types.
41 #include <stdarg.h>
42 #include <stddef.h>
43 #include <stdint.h>
44 #include <setjmp.h>
45 #include <cmocka.h>
47 #include <errno.h>
48 #include <unistd.h>
49 #include <talloc.h>
50 #include <tevent.h>
51 #include <ldb.h>
53 #define TEST_BE "mdb"
55 struct ldbtest_ctx {
56 struct tevent_context *ev;
57 struct ldb_context *ldb;
59 const char *dbfile;
60 const char *lockfile; /* lockfile is separate */
62 const char *dbpath;
65 static void unlink_old_db(struct ldbtest_ctx *test_ctx)
67 int ret;
69 errno = 0;
70 ret = unlink(test_ctx->lockfile);
71 if (ret == -1 && errno != ENOENT) {
72 fail();
75 errno = 0;
76 ret = unlink(test_ctx->dbfile);
77 if (ret == -1 && errno != ENOENT) {
78 fail();
82 static int ldbtest_noconn_setup(void **state)
84 struct ldbtest_ctx *test_ctx;
86 test_ctx = talloc_zero(NULL, struct ldbtest_ctx);
87 assert_non_null(test_ctx);
89 test_ctx->ev = tevent_context_init(test_ctx);
90 assert_non_null(test_ctx->ev);
92 test_ctx->ldb = ldb_init(test_ctx, test_ctx->ev);
93 assert_non_null(test_ctx->ldb);
95 test_ctx->dbfile = talloc_strdup(test_ctx, "apitest.ldb");
96 assert_non_null(test_ctx->dbfile);
98 test_ctx->lockfile = talloc_asprintf(test_ctx, "%s-lock",
99 test_ctx->dbfile);
100 assert_non_null(test_ctx->lockfile);
102 test_ctx->dbpath = talloc_asprintf(test_ctx,
103 TEST_BE"://%s", test_ctx->dbfile);
104 assert_non_null(test_ctx->dbpath);
106 unlink_old_db(test_ctx);
107 *state = test_ctx;
108 return 0;
111 static int ldbtest_noconn_teardown(void **state)
113 struct ldbtest_ctx *test_ctx = talloc_get_type_abort(*state,
114 struct ldbtest_ctx);
116 unlink_old_db(test_ctx);
117 talloc_free(test_ctx);
118 return 0;
121 static int ldbtest_setup(void **state)
123 struct ldbtest_ctx *test_ctx;
124 int ret;
126 ldbtest_noconn_setup((void **) &test_ctx);
128 ret = ldb_connect(test_ctx->ldb, test_ctx->dbpath, 0, NULL);
129 assert_int_equal(ret, LDB_ERR_OTHER);
131 *state = test_ctx;
132 return 0;
135 static int ldbtest_teardown(void **state)
137 struct ldbtest_ctx *test_ctx = talloc_get_type_abort(*state,
138 struct ldbtest_ctx);
139 ldbtest_noconn_teardown((void **) &test_ctx);
140 return 0;
143 static void test_ldb_lmdb_not_found(void **state)
145 // Actual test in ldbtest_setup
146 assert_int_equal(0, 0);
149 int main(int argc, const char **argv)
151 const struct CMUnitTest tests[] = {
152 cmocka_unit_test_setup_teardown(
153 test_ldb_lmdb_not_found,
154 ldbtest_setup,
155 ldbtest_teardown),
158 cmocka_set_message_output(CM_OUTPUT_SUBUNIT);
160 return cmocka_run_group_tests(tests, NULL, NULL);