lib/util: inline lib/util/util_runcmd.h again
[Samba.git] / lib / ldb / tests / ldb_lmdb_size_test.c
blobaf015fa72b5b7a5ea18f7f0ea72951722bc2d131
1 /*
2 * lmdb backend specific tests for ldb
3 * Tests for truncated index keys
5 * Copyright (C) Andrew Bartlett <abartlet@samba.org> 2018
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 * These tests confirm that database sizes of > 4GB are supported
24 * Due to the disk space requirement they are not run as part of the normal
25 * self test runs.
27 * Setup and tear down code copied from ldb_mod_op_test.c
31 * from cmocka.c:
32 * These headers or their equivalents should be included prior to
33 * including
34 * this header file.
36 * #include <stdarg.h>
37 * #include <stddef.h>
38 * #include <setjmp.h>
40 * This allows test applications to use custom definitions of C standard
41 * library functions and types.
44 #include <stdarg.h>
45 #include <stddef.h>
46 #include <setjmp.h>
47 #include <cmocka.h>
49 #include <errno.h>
50 #include <unistd.h>
51 #include <talloc.h>
52 #include <tevent.h>
53 #include <ldb.h>
54 #include <ldb_module.h>
55 #include <ldb_private.h>
56 #include <string.h>
57 #include <ctype.h>
59 #include <sys/wait.h>
61 #include <lmdb.h>
64 #define TEST_BE "mdb"
66 struct ldbtest_ctx {
67 struct tevent_context *ev;
68 struct ldb_context *ldb;
70 const char *dbfile;
71 const char *lockfile; /* lockfile is separate */
73 const char *dbpath;
76 static void unlink_old_db(struct ldbtest_ctx *test_ctx)
78 int ret;
80 errno = 0;
81 ret = unlink(test_ctx->lockfile);
82 if (ret == -1 && errno != ENOENT) {
83 fail();
86 errno = 0;
87 ret = unlink(test_ctx->dbfile);
88 if (ret == -1 && errno != ENOENT) {
89 fail();
93 static int ldbtest_noconn_setup(void **state)
95 struct ldbtest_ctx *test_ctx;
97 test_ctx = talloc_zero(NULL, struct ldbtest_ctx);
98 assert_non_null(test_ctx);
100 test_ctx->ev = tevent_context_init(test_ctx);
101 assert_non_null(test_ctx->ev);
103 test_ctx->ldb = ldb_init(test_ctx, test_ctx->ev);
104 assert_non_null(test_ctx->ldb);
106 test_ctx->dbfile = talloc_strdup(test_ctx, "apitest.ldb");
107 assert_non_null(test_ctx->dbfile);
109 test_ctx->lockfile = talloc_asprintf(test_ctx, "%s-lock",
110 test_ctx->dbfile);
111 assert_non_null(test_ctx->lockfile);
113 test_ctx->dbpath = talloc_asprintf(test_ctx,
114 TEST_BE"://%s", test_ctx->dbfile);
115 assert_non_null(test_ctx->dbpath);
117 unlink_old_db(test_ctx);
118 *state = test_ctx;
119 return 0;
122 static int ldbtest_noconn_teardown(void **state)
124 struct ldbtest_ctx *test_ctx = talloc_get_type_abort(*state,
125 struct ldbtest_ctx);
127 unlink_old_db(test_ctx);
128 talloc_free(test_ctx);
129 return 0;
132 static int ldbtest_setup(void **state)
134 struct ldbtest_ctx *test_ctx;
135 int ret;
137 ldbtest_noconn_setup((void **) &test_ctx);
139 ret = ldb_connect(test_ctx->ldb, test_ctx->dbpath, 0, NULL);
140 assert_int_equal(ret, 0);
142 *state = test_ctx;
143 return 0;
146 static int ldbtest_teardown(void **state)
148 struct ldbtest_ctx *test_ctx = talloc_get_type_abort(*state,
149 struct ldbtest_ctx);
150 ldbtest_noconn_teardown((void **) &test_ctx);
151 return 0;
154 static void test_db_size_gt_4GB(void **state)
156 int ret, x;
157 struct ldb_message *msg;
158 struct ldbtest_ctx *test_ctx = talloc_get_type_abort(*state,
159 struct ldbtest_ctx);
160 const int MB = 1024 * 1024;
161 char *blob = NULL;
163 TALLOC_CTX *tmp_ctx;
165 tmp_ctx = talloc_new(test_ctx);
166 assert_non_null(tmp_ctx);
169 blob = talloc_zero_size(tmp_ctx, (MB + 1));
170 assert_non_null(blob);
171 memset(blob, 'x', MB);
174 for (x = 0; x < 6144; x++) {
175 msg = ldb_msg_new(tmp_ctx);
176 assert_non_null(msg);
178 msg->dn = ldb_dn_new_fmt(msg, test_ctx->ldb, "dc=test%d", x);
179 assert_non_null(msg->dn);
181 ldb_transaction_start(test_ctx->ldb);
182 ret = ldb_msg_add_string(msg, "blob", blob);
183 assert_int_equal(ret, 0);
185 ret = ldb_add(test_ctx->ldb, msg);
186 assert_int_equal(ret, 0);
187 ldb_transaction_commit(test_ctx->ldb);
189 TALLOC_FREE(msg);
191 talloc_free(tmp_ctx);
193 struct stat s;
194 ret = stat(test_ctx->dbfile, &s);
195 assert_int_equal(ret, 0);
196 assert_true(s.st_size > (6144LL * MB));
200 int main(int argc, const char **argv)
202 const struct CMUnitTest tests[] = {
203 cmocka_unit_test_setup_teardown(
204 test_db_size_gt_4GB,
205 ldbtest_setup,
206 ldbtest_teardown),
209 return cmocka_run_group_tests(tests, NULL, NULL);