Line data Source code
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 2 : static void unlink_old_db(struct ldbtest_ctx *test_ctx) 66 : { 67 : int ret; 68 : 69 2 : errno = 0; 70 2 : ret = unlink(test_ctx->lockfile); 71 2 : if (ret == -1 && errno != ENOENT) { 72 0 : fail(); 73 : } 74 : 75 2 : errno = 0; 76 2 : ret = unlink(test_ctx->dbfile); 77 2 : if (ret == -1 && errno != ENOENT) { 78 0 : fail(); 79 : } 80 2 : } 81 : 82 1 : static int ldbtest_noconn_setup(void **state) 83 : { 84 : struct ldbtest_ctx *test_ctx; 85 : 86 1 : test_ctx = talloc_zero(NULL, struct ldbtest_ctx); 87 1 : assert_non_null(test_ctx); 88 : 89 1 : test_ctx->ev = tevent_context_init(test_ctx); 90 1 : assert_non_null(test_ctx->ev); 91 : 92 1 : test_ctx->ldb = ldb_init(test_ctx, test_ctx->ev); 93 1 : assert_non_null(test_ctx->ldb); 94 : 95 1 : test_ctx->dbfile = talloc_strdup(test_ctx, "apitest.ldb"); 96 1 : assert_non_null(test_ctx->dbfile); 97 : 98 1 : test_ctx->lockfile = talloc_asprintf(test_ctx, "%s-lock", 99 : test_ctx->dbfile); 100 1 : assert_non_null(test_ctx->lockfile); 101 : 102 1 : test_ctx->dbpath = talloc_asprintf(test_ctx, 103 : TEST_BE"://%s", test_ctx->dbfile); 104 1 : assert_non_null(test_ctx->dbpath); 105 : 106 1 : unlink_old_db(test_ctx); 107 1 : *state = test_ctx; 108 1 : return 0; 109 : } 110 : 111 1 : static int ldbtest_noconn_teardown(void **state) 112 : { 113 1 : struct ldbtest_ctx *test_ctx = talloc_get_type_abort(*state, 114 : struct ldbtest_ctx); 115 : 116 1 : unlink_old_db(test_ctx); 117 1 : talloc_free(test_ctx); 118 1 : return 0; 119 : } 120 : 121 1 : static int ldbtest_setup(void **state) 122 : { 123 : struct ldbtest_ctx *test_ctx; 124 : int ret; 125 : 126 1 : ldbtest_noconn_setup((void **) &test_ctx); 127 : 128 1 : ret = ldb_connect(test_ctx->ldb, test_ctx->dbpath, 0, NULL); 129 1 : assert_int_equal(ret, LDB_ERR_OTHER); 130 : 131 1 : *state = test_ctx; 132 1 : return 0; 133 : } 134 : 135 1 : static int ldbtest_teardown(void **state) 136 : { 137 1 : struct ldbtest_ctx *test_ctx = talloc_get_type_abort(*state, 138 : struct ldbtest_ctx); 139 1 : ldbtest_noconn_teardown((void **) &test_ctx); 140 1 : return 0; 141 : } 142 : 143 1 : static void test_ldb_lmdb_not_found(void **state) 144 : { 145 : // Actual test in ldbtest_setup 146 1 : assert_int_equal(0, 0); 147 1 : } 148 : 149 1 : int main(int argc, const char **argv) 150 : { 151 1 : 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 1 : return cmocka_run_group_tests(tests, NULL, NULL); 159 : }