1396c16836d9bc9467d883fd79b4d6e9dde0d2a6
[ambi/samba-autobuild/.git] / lib / ldb / tests / ldb_mod_op_test.c
1 /*
2  * from cmocka.c:
3  * These headers or their equivalents should be included prior to
4  * including
5  * this header file.
6  *
7  * #include <stdarg.h>
8  * #include <stddef.h>
9  * #include <setjmp.h>
10  *
11  * This allows test applications to use custom definitions of C standard
12  * library functions and types.
13  */
14 #include <stdarg.h>
15 #include <stddef.h>
16 #include <setjmp.h>
17 #include <cmocka.h>
18
19 #include <cmocka.h>
20
21 #include <errno.h>
22 #include <unistd.h>
23 #include <talloc.h>
24 #include <ldb.h>
25
26 #define DEFAULT_BE  "tdb"
27
28 #ifndef TEST_BE
29 #define TEST_BE DEFAULT_BE
30 #endif /* TEST_BE */
31
32 struct ldbtest_ctx {
33         struct tevent_context *ev;
34         struct ldb_context *ldb;
35
36         const char *dbfile;
37         const char *lockfile;
38
39         const char *dbpath;
40         const char *lockpath;   /* lockfile is separate */
41 };
42
43 static int ldbtest_noconn_setup(void **state)
44 {
45         struct ldbtest_ctx *test_ctx;
46
47         test_ctx = talloc_zero(NULL, struct ldbtest_ctx);
48         assert_non_null(test_ctx);
49
50         test_ctx->ev = tevent_context_init(test_ctx);
51         assert_non_null(test_ctx->ev);
52
53         test_ctx->ldb = ldb_init(test_ctx, test_ctx->ev);
54         assert_non_null(test_ctx->ldb);
55
56         test_ctx->dbfile = talloc_strdup(test_ctx, "apitest.ldb");
57         assert_non_null(test_ctx->dbfile);
58
59         test_ctx->lockfile = talloc_asprintf(test_ctx,
60                                              "%s-lock", test_ctx->dbfile);
61         assert_non_null(test_ctx->lockfile);
62
63         test_ctx->dbpath = talloc_asprintf(test_ctx,
64                                           TEST_BE"://%s", test_ctx->dbfile);
65         assert_non_null(test_ctx->dbpath);
66
67         test_ctx->lockpath = talloc_asprintf(test_ctx,
68                                              "%s-lock", test_ctx->dbpath);
69         assert_non_null(test_ctx->lockpath);
70
71         *state = test_ctx;
72         return 0;
73 }
74
75 static int ldbtest_noconn_teardown(void **state)
76 {
77         struct ldbtest_ctx *test_ctx = talloc_get_type_abort(*state,
78                                                         struct ldbtest_ctx);
79
80         unlink(test_ctx->lockfile);
81
82         unlink(test_ctx->dbfile);
83
84         talloc_free(test_ctx);
85         return 0;
86 }
87
88 static void test_connect(void **state)
89 {
90         struct ldbtest_ctx *test_ctx = talloc_get_type_abort(*state,
91                                                         struct ldbtest_ctx);
92         int ret;
93
94         ret = ldb_connect(test_ctx->ldb, test_ctx->dbpath, 0, NULL);
95         assert_int_equal(ret, 0);
96 }
97
98 static int ldbtest_setup(void **state)
99 {
100         struct ldbtest_ctx *test_ctx;
101         int ret;
102
103         ldbtest_noconn_setup((void **) &test_ctx);
104
105         ret = ldb_connect(test_ctx->ldb, test_ctx->dbpath, 0, NULL);
106         assert_int_equal(ret, 0);
107
108         *state = test_ctx;
109         return 0;
110 }
111
112 static int ldbtest_teardown(void **state)
113 {
114         struct ldbtest_ctx *test_ctx = talloc_get_type_abort(*state,
115                                                         struct ldbtest_ctx);
116         ldbtest_noconn_teardown((void **) &test_ctx);
117         return 0;
118 }
119
120 static void test_ldb_add(void **state)
121 {
122         int ret;
123         struct ldb_message *msg;
124         struct ldbtest_ctx *test_ctx = talloc_get_type_abort(*state,
125                                                         struct ldbtest_ctx);
126         TALLOC_CTX *tmp_ctx;
127
128         tmp_ctx = talloc_new(test_ctx);
129         assert_non_null(tmp_ctx);
130
131         msg = ldb_msg_new(tmp_ctx);
132         assert_non_null(msg);
133
134         msg->dn = ldb_dn_new_fmt(msg, test_ctx->ldb, "dc=test");
135         assert_non_null(msg->dn);
136
137         ret = ldb_msg_add_string(msg, "cn", "test_cn_val");
138         assert_int_equal(ret, 0);
139
140         ret = ldb_add(test_ctx->ldb, msg);
141         assert_int_equal(ret, 0);
142
143         talloc_free(tmp_ctx);
144 }
145
146 static void test_ldb_search(void **state)
147 {
148         int ret;
149         struct ldb_message *msg;
150         struct ldbtest_ctx *test_ctx = talloc_get_type_abort(*state,
151                                                         struct ldbtest_ctx);
152         TALLOC_CTX *tmp_ctx;
153         struct ldb_dn *basedn;
154         struct ldb_dn *basedn2;
155         struct ldb_result *result = NULL;
156
157         tmp_ctx = talloc_new(test_ctx);
158         assert_non_null(tmp_ctx);
159
160         basedn = ldb_dn_new_fmt(tmp_ctx, test_ctx->ldb, "dc=test");
161         assert_non_null(basedn);
162
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);
168
169         msg = ldb_msg_new(tmp_ctx);
170         assert_non_null(msg);
171
172         msg->dn = basedn;
173         assert_non_null(msg->dn);
174
175         ret = ldb_msg_add_string(msg, "cn", "test_cn_val1");
176         assert_int_equal(ret, 0);
177
178         ret = ldb_add(test_ctx->ldb, msg);
179         assert_int_equal(ret, 0);
180
181         basedn2 = ldb_dn_new_fmt(tmp_ctx, test_ctx->ldb, "dc=test2");
182         assert_non_null(basedn2);
183
184         msg = ldb_msg_new(tmp_ctx);
185         assert_non_null(msg);
186
187         msg->dn = basedn2;
188         assert_non_null(msg->dn);
189
190         ret = ldb_msg_add_string(msg, "cn", "test_cn_val2");
191         assert_int_equal(ret, 0);
192
193         ret = ldb_add(test_ctx->ldb, msg);
194         assert_int_equal(ret, 0);
195
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));
203
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));
211
212         talloc_free(tmp_ctx);
213 }
214
215 int main(int argc, const char **argv)
216 {
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,
222                                                 ldbtest_setup,
223                                                 ldbtest_teardown),
224                 cmocka_unit_test_setup_teardown(test_ldb_search,
225                                                 ldbtest_setup,
226                                                 ldbtest_teardown),
227         };
228
229         return cmocka_run_group_tests(tests, NULL, NULL);
230 }