3 * These headers or their equivalents should be included prior to
11 * This allows test applications to use custom definitions of C standard
12 * library functions and types.
26 #define DEFAULT_BE "tdb"
29 #define TEST_BE DEFAULT_BE
33 struct tevent_context *ev;
34 struct ldb_context *ldb;
40 const char *lockpath; /* lockfile is separate */
43 static int ldbtest_noconn_setup(void **state)
45 struct ldbtest_ctx *test_ctx;
47 test_ctx = talloc_zero(NULL, struct ldbtest_ctx);
48 assert_non_null(test_ctx);
50 test_ctx->ev = tevent_context_init(test_ctx);
51 assert_non_null(test_ctx->ev);
53 test_ctx->ldb = ldb_init(test_ctx, test_ctx->ev);
54 assert_non_null(test_ctx->ldb);
56 test_ctx->dbfile = talloc_strdup(test_ctx, "apitest.ldb");
57 assert_non_null(test_ctx->dbfile);
59 test_ctx->lockfile = talloc_asprintf(test_ctx,
60 "%s-lock", test_ctx->dbfile);
61 assert_non_null(test_ctx->lockfile);
63 test_ctx->dbpath = talloc_asprintf(test_ctx,
64 TEST_BE"://%s", test_ctx->dbfile);
65 assert_non_null(test_ctx->dbpath);
67 test_ctx->lockpath = talloc_asprintf(test_ctx,
68 "%s-lock", test_ctx->dbpath);
69 assert_non_null(test_ctx->lockpath);
75 static int ldbtest_noconn_teardown(void **state)
77 struct ldbtest_ctx *test_ctx = talloc_get_type_abort(*state,
80 unlink(test_ctx->lockfile);
82 unlink(test_ctx->dbfile);
84 talloc_free(test_ctx);
88 static void test_connect(void **state)
90 struct ldbtest_ctx *test_ctx = talloc_get_type_abort(*state,
94 ret = ldb_connect(test_ctx->ldb, test_ctx->dbpath, 0, NULL);
95 assert_int_equal(ret, 0);
98 static int ldbtest_setup(void **state)
100 struct ldbtest_ctx *test_ctx;
103 ldbtest_noconn_setup((void **) &test_ctx);
105 ret = ldb_connect(test_ctx->ldb, test_ctx->dbpath, 0, NULL);
106 assert_int_equal(ret, 0);
112 static int ldbtest_teardown(void **state)
114 struct ldbtest_ctx *test_ctx = talloc_get_type_abort(*state,
116 ldbtest_noconn_teardown((void **) &test_ctx);
120 static void test_ldb_add(void **state)
123 struct ldb_message *msg;
124 struct ldbtest_ctx *test_ctx = talloc_get_type_abort(*state,
128 tmp_ctx = talloc_new(test_ctx);
129 assert_non_null(tmp_ctx);
131 msg = ldb_msg_new(tmp_ctx);
132 assert_non_null(msg);
134 msg->dn = ldb_dn_new_fmt(msg, test_ctx->ldb, "dc=test");
135 assert_non_null(msg->dn);
137 ret = ldb_msg_add_string(msg, "cn", "test_cn_val");
138 assert_int_equal(ret, 0);
140 ret = ldb_add(test_ctx->ldb, msg);
141 assert_int_equal(ret, 0);
143 talloc_free(tmp_ctx);
146 static void test_ldb_search(void **state)
149 struct ldb_message *msg;
150 struct ldbtest_ctx *test_ctx = talloc_get_type_abort(*state,
153 struct ldb_dn *basedn;
154 struct ldb_dn *basedn2;
155 struct ldb_result *result = NULL;
157 tmp_ctx = talloc_new(test_ctx);
158 assert_non_null(tmp_ctx);
160 basedn = ldb_dn_new_fmt(tmp_ctx, test_ctx->ldb, "dc=test");
161 assert_non_null(basedn);
163 ret = ldb_search(test_ctx->ldb, tmp_ctx, &result, basedn,
164 LDB_SCOPE_BASE, NULL, NULL);
165 assert_int_equal(ret, 0);
166 assert_non_null(result);
167 assert_int_equal(result->count, 0);
169 msg = ldb_msg_new(tmp_ctx);
170 assert_non_null(msg);
173 assert_non_null(msg->dn);
175 ret = ldb_msg_add_string(msg, "cn", "test_cn_val1");
176 assert_int_equal(ret, 0);
178 ret = ldb_add(test_ctx->ldb, msg);
179 assert_int_equal(ret, 0);
181 basedn2 = ldb_dn_new_fmt(tmp_ctx, test_ctx->ldb, "dc=test2");
182 assert_non_null(basedn2);
184 msg = ldb_msg_new(tmp_ctx);
185 assert_non_null(msg);
188 assert_non_null(msg->dn);
190 ret = ldb_msg_add_string(msg, "cn", "test_cn_val2");
191 assert_int_equal(ret, 0);
193 ret = ldb_add(test_ctx->ldb, msg);
194 assert_int_equal(ret, 0);
196 ret = ldb_search(test_ctx->ldb, tmp_ctx, &result, basedn,
197 LDB_SCOPE_BASE, NULL, NULL);
198 assert_int_equal(ret, 0);
199 assert_non_null(result);
200 assert_int_equal(result->count, 1);
201 assert_string_equal(ldb_dn_get_linearized(result->msgs[0]->dn),
202 ldb_dn_get_linearized(basedn));
204 ret = ldb_search(test_ctx->ldb, tmp_ctx, &result, basedn2,
205 LDB_SCOPE_BASE, NULL, NULL);
206 assert_int_equal(ret, 0);
207 assert_non_null(result);
208 assert_int_equal(result->count, 1);
209 assert_string_equal(ldb_dn_get_linearized(result->msgs[0]->dn),
210 ldb_dn_get_linearized(basedn2));
212 talloc_free(tmp_ctx);
215 int main(int argc, const char **argv)
217 const struct CMUnitTest tests[] = {
218 cmocka_unit_test_setup_teardown(test_connect,
219 ldbtest_noconn_setup,
220 ldbtest_noconn_teardown),
221 cmocka_unit_test_setup_teardown(test_ldb_add,
224 cmocka_unit_test_setup_teardown(test_ldb_search,
229 return cmocka_run_group_tests(tests, NULL, NULL);