1 /* 2 * Ensure lmdb backend is disabled 3 * 4 * Copyright (C) Mathieu Parent <math.parent@gmail.com> 2019 5 * 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. 10 * 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. 15 * 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/>. 18 * 19 */ 20 21 /* 22 * Ensure lmdb backend is disabled 23 * 24 * Setup and tear down code copied from ldb_lmdb_test.c 25 */ 26 27 /* 28 * from cmocka.c: 29 * These headers or their equivalents should be included prior to 30 * including 31 * this header file. 32 * 33 * #include <stdarg.h> 34 * #include <stddef.h> 35 * #include <setjmp.h> 36 * 37 * This allows test applications to use custom definitions of C standard 38 * library functions and types. 39 * 40 */ 41 #include <stdarg.h> 42 #include <stddef.h> 43 #include <stdint.h> 44 #include <setjmp.h> 45 #include <cmocka.h> 46 47 #include <errno.h> 48 #include <unistd.h> 49 #include <talloc.h> 50 #include <tevent.h> 51 #include <ldb.h> 52 53 #define TEST_BE "mdb" 54 55 struct ldbtest_ctx { 56 struct tevent_context *ev; 57 struct ldb_context *ldb; 58 59 const char *dbfile; 60 const char *lockfile; /* lockfile is separate */ 61 62 const char *dbpath; 63 }; 64 65 static void unlink_old_db(struct ldbtest_ctx *test_ctx) 66 { 67 int ret; 68 69 errno = 0; 70 ret = unlink(test_ctx->lockfile); 71 if (ret == -1 && errno != ENOENT) { 72 fail(); 73 } 74 75 errno = 0; 76 ret = unlink(test_ctx->dbfile); 77 if (ret == -1 && errno != ENOENT) { 78 fail(); 79 } 80 } 81 82 static int ldbtest_noconn_setup(void **state) 83 { 84 struct ldbtest_ctx *test_ctx; 85 86 test_ctx = talloc_zero(NULL, struct ldbtest_ctx); 87 assert_non_null(test_ctx); 88 89 test_ctx->ev = tevent_context_init(test_ctx); 90 assert_non_null(test_ctx->ev); 91 92 test_ctx->ldb = ldb_init(test_ctx, test_ctx->ev); 93 assert_non_null(test_ctx->ldb); 94 95 test_ctx->dbfile = talloc_strdup(test_ctx, "apitest.ldb"); 96 assert_non_null(test_ctx->dbfile); 97 98 test_ctx->lockfile = talloc_asprintf(test_ctx, "%s-lock", 99 test_ctx->dbfile); 100 assert_non_null(test_ctx->lockfile); 101 102 test_ctx->dbpath = talloc_asprintf(test_ctx, 103 TEST_BE"://%s", test_ctx->dbfile); 104 assert_non_null(test_ctx->dbpath); 105 106 unlink_old_db(test_ctx); 107 *state = test_ctx; 108 return 0; 109 } 110 111 static int ldbtest_noconn_teardown(void **state) 112 { 113 struct ldbtest_ctx *test_ctx = talloc_get_type_abort(*state, 114 struct ldbtest_ctx); 115 116 unlink_old_db(test_ctx); 117 talloc_free(test_ctx); 118 return 0; 119 } 120 121 static int ldbtest_setup(void **state) 122 { 123 struct ldbtest_ctx *test_ctx; 124 int ret; 125 126 ldbtest_noconn_setup((void **) &test_ctx); 127 128 ret = ldb_connect(test_ctx->ldb, test_ctx->dbpath, 0, NULL); 129 assert_int_equal(ret, LDB_ERR_OTHER); 130 131 *state = test_ctx; 132 return 0; 133 } 134 135 static int ldbtest_teardown(void **state) 136 { 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; 141 } 142 143 static void test_ldb_lmdb_not_found(void **state) 144 { 145 // Actual test in ldbtest_setup 146 assert_int_equal(0, 0); 147 } 148 149 int main(int argc, const char **argv) 150 { 151 const struct CMUnitTest tests[] = { 152 cmocka_unit_test_setup_teardown( 153 test_ldb_lmdb_not_found, 154 ldbtest_setup, 155 ldbtest_teardown), 156 }; 157 158 return cmocka_run_group_tests(tests, NULL, NULL); 159 } 160