4 Copyright (C) Andrew Tridgell 2004
6 ** NOTE! The following LGPL license applies to the ldb
7 ** library. This does NOT imply that all of Samba is released
10 This library is free software; you can redistribute it and/or
11 modify it under the terms of the GNU Lesser General Public
12 License as published by the Free Software Foundation; either
13 version 2 of the License, or (at your option) any later version.
15 This library is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 Lesser General Public License for more details.
20 You should have received a copy of the GNU Lesser General Public
21 License along with this library; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 * Description: utility to test ldb
32 * Author: Andrew Tridgell
36 #include "ldb/include/includes.h"
37 #include "ldb/tools/cmdline.h"
39 static struct timeval tp1,tp2;
40 static struct ldb_cmdline *options;
42 static void _start_timer(void)
44 gettimeofday(&tp1,NULL);
47 static double _end_timer(void)
49 gettimeofday(&tp2,NULL);
50 return((tp2.tv_sec - tp1.tv_sec) +
51 (tp2.tv_usec - tp1.tv_usec)*1.0e-6);
54 static void add_records(struct ldb_context *ldb,
55 struct ldb_dn *basedn,
58 struct ldb_message msg;
62 if (ldb_lock(ldb, "transaction") != 0) {
63 printf("transaction lock failed\n");
67 for (i=0;i<count;i++) {
68 struct ldb_message_element el[6];
69 struct ldb_val vals[6][1];
71 TALLOC_CTX *tmp_ctx = talloc_new(ldb);
73 name = talloc_asprintf(tmp_ctx, "Test%d", i);
75 msg.dn = ldb_dn_copy(tmp_ctx, basedn);
76 ldb_dn_add_child_fmt(msg.dn, "cn=%s", name);
81 el[0].name = talloc_strdup(tmp_ctx, "cn");
83 el[0].values = vals[0];
84 vals[0][0].data = (uint8_t *)name;
85 vals[0][0].length = strlen(name);
90 el[1].values = vals[1];
91 vals[1][0].data = (uint8_t *)talloc_asprintf(tmp_ctx, "The title of %s", name);
92 vals[1][0].length = strlen((char *)vals[1][0].data);
95 el[2].name = talloc_strdup(tmp_ctx, "uid");
97 el[2].values = vals[2];
98 vals[2][0].data = (uint8_t *)ldb_casefold(ldb, tmp_ctx, name);
99 vals[2][0].length = strlen((char *)vals[2][0].data);
102 el[3].name = talloc_strdup(tmp_ctx, "mail");
103 el[3].num_values = 1;
104 el[3].values = vals[3];
105 vals[3][0].data = (uint8_t *)talloc_asprintf(tmp_ctx, "%s@example.com", name);
106 vals[3][0].length = strlen((char *)vals[3][0].data);
109 el[4].name = talloc_strdup(tmp_ctx, "objectClass");
110 el[4].num_values = 1;
111 el[4].values = vals[4];
112 vals[4][0].data = (uint8_t *)talloc_strdup(tmp_ctx, "OpenLDAPperson");
113 vals[4][0].length = strlen((char *)vals[4][0].data);
116 el[5].name = talloc_strdup(tmp_ctx, "sn");
117 el[5].num_values = 1;
118 el[5].values = vals[5];
119 vals[5][0].data = (uint8_t *)name;
120 vals[5][0].length = strlen((char *)vals[5][0].data);
122 ldb_delete(ldb, msg.dn);
124 if (ldb_add(ldb, &msg) != 0) {
125 printf("Add of %s failed - %s\n", name, ldb_errstring(ldb));
129 printf("adding uid %s\r", name);
132 talloc_free(tmp_ctx);
135 if (ldb_unlock(ldb, "transaction") != 0) {
136 printf("transaction unlock failed\n");
143 static void modify_records(struct ldb_context *ldb,
144 struct ldb_dn *basedn,
147 struct ldb_message msg;
150 for (i=0;i<count;i++) {
151 struct ldb_message_element el[3];
152 struct ldb_val vals[3];
154 TALLOC_CTX *tmp_ctx = talloc_new(ldb);
156 name = talloc_asprintf(tmp_ctx, "Test%d", i);
157 msg.dn = ldb_dn_copy(tmp_ctx, basedn);
158 ldb_dn_add_child_fmt(msg.dn, "cn=%s", name);
160 msg.num_elements = 3;
163 el[0].flags = LDB_FLAG_MOD_DELETE;
164 el[0].name = talloc_strdup(tmp_ctx, "mail");
165 el[0].num_values = 0;
167 el[1].flags = LDB_FLAG_MOD_ADD;
168 el[1].name = talloc_strdup(tmp_ctx, "mail");
169 el[1].num_values = 1;
170 el[1].values = &vals[1];
171 vals[1].data = (uint8_t *)talloc_asprintf(tmp_ctx, "%s@other.example.com", name);
172 vals[1].length = strlen((char *)vals[1].data);
174 el[2].flags = LDB_FLAG_MOD_REPLACE;
175 el[2].name = talloc_strdup(tmp_ctx, "mail");
176 el[2].num_values = 1;
177 el[2].values = &vals[2];
178 vals[2].data = (uint8_t *)talloc_asprintf(tmp_ctx, "%s@other2.example.com", name);
179 vals[2].length = strlen((char *)vals[2].data);
181 if (ldb_modify(ldb, &msg) != 0) {
182 printf("Modify of %s failed - %s\n", name, ldb_errstring(ldb));
186 printf("Modifying uid %s\r", name);
189 talloc_free(tmp_ctx);
196 static void delete_records(struct ldb_context *ldb,
197 struct ldb_dn *basedn,
202 for (i=0;i<count;i++) {
204 char *name = talloc_asprintf(ldb, "Test%d", i);
205 dn = ldb_dn_copy(name, basedn);
206 ldb_dn_add_child_fmt(dn, "cn=%s", name);
208 printf("Deleting uid Test%d\r", i);
211 if (ldb_delete(ldb, dn) != 0) {
212 printf("Delete of %s failed - %s\n", ldb_dn_get_linearized(dn), ldb_errstring(ldb));
221 static void search_uid(struct ldb_context *ldb, struct ldb_dn *basedn, int nrecords, int nsearches)
225 for (i=0;i<nsearches;i++) {
226 int uid = (i * 700 + 17) % (nrecords * 2);
228 struct ldb_result *res = NULL;
231 expr = talloc_asprintf(ldb, "(uid=TEST%d)", uid);
232 ret = ldb_search(ldb, basedn, LDB_SCOPE_SUBTREE, expr, NULL, &res);
234 if (ret != LDB_SUCCESS || (uid < nrecords && res->count != 1)) {
235 printf("Failed to find %s - %s\n", expr, ldb_errstring(ldb));
239 if (uid >= nrecords && res->count > 0) {
240 printf("Found %s !? - %d\n", expr, ret);
244 printf("testing uid %d/%d - %d \r", i, uid, res->count);
254 static void start_test(struct ldb_context *ldb, int nrecords, int nsearches)
256 struct ldb_dn *basedn;
258 basedn = ldb_dn_new(ldb, ldb, options->basedn);
259 if ( ! ldb_dn_validate(basedn)) {
260 printf("Invalid base DN\n");
264 printf("Adding %d records\n", nrecords);
265 add_records(ldb, basedn, nrecords);
267 printf("Starting search on uid\n");
269 search_uid(ldb, basedn, nrecords, nsearches);
270 printf("uid search took %.2f seconds\n", _end_timer());
272 printf("Modifying records\n");
273 modify_records(ldb, basedn, nrecords);
275 printf("Deleting records\n");
276 delete_records(ldb, basedn, nrecords);
281 2) Store an @indexlist record
283 3) Store a record that contains fields that should be index according
286 4) disconnection from database
288 5) connect to same database
290 6) search for record added in step 3 using a search key that should
293 static void start_test_index(struct ldb_context **ldb)
295 struct ldb_message *msg;
296 struct ldb_result *res = NULL;
297 struct ldb_dn *indexlist;
298 struct ldb_dn *basedn;
301 const char *specials;
303 specials = getenv("LDB_SPECIALS");
304 if (specials && atoi(specials) == 0) {
305 printf("LDB_SPECIALS disabled - skipping index test\n");
309 if (options->nosync) {
310 flags |= LDB_FLG_NOSYNC;
313 printf("Starting index test\n");
315 indexlist = ldb_dn_new(*ldb, *ldb, "@INDEXLIST");
317 ldb_delete(*ldb, indexlist);
319 msg = ldb_msg_new(NULL);
322 ldb_msg_add_string(msg, "@IDXATTR", strdup("uid"));
324 if (ldb_add(*ldb, msg) != 0) {
325 printf("Add of %s failed - %s\n", ldb_dn_linearize(*ldb, msg->dn), ldb_errstring(*ldb));
329 basedn = ldb_dn_new(*ldb, *ldb, options->basedn);
331 memset(msg, 0, sizeof(*msg));
332 msg->dn = ldb_dn_copy(msg, basedn);
333 ldb_dn_add_child_fmt(msg->dn, "cn=test");
334 ldb_msg_add_string(msg, "cn", strdup("test"));
335 ldb_msg_add_string(msg, "sn", strdup("test"));
336 ldb_msg_add_string(msg, "uid", strdup("test"));
337 ldb_msg_add_string(msg, "objectClass", strdup("OpenLDAPperson"));
339 if (ldb_add(*ldb, msg) != 0) {
340 printf("Add of %s failed - %s\n", ldb_dn_linearize(*ldb, msg->dn), ldb_errstring(*ldb));
344 if (talloc_free(*ldb) != 0) {
345 printf("failed to free/close ldb database");
349 (*ldb) = ldb_init(options);
351 ret = ldb_connect(*ldb, options->url, flags, NULL);
353 printf("failed to connect to %s\n", options->url);
357 basedn = ldb_dn_new(*ldb, *ldb, options->basedn);
359 ret = ldb_search(*ldb, basedn, LDB_SCOPE_SUBTREE, "uid=test", NULL, &res);
360 if (ret != LDB_SUCCESS) {
361 printf("Search with (uid=test) filter failed!\n");
364 if(res->count != 1) {
365 printf("Should have found 1 record - found %d\n", res->count);
369 indexlist = ldb_dn_new(*ldb, *ldb, "@INDEXLIST");
371 if (ldb_delete(*ldb, msg->dn) != 0 ||
372 ldb_delete(*ldb, indexlist) != 0) {
373 printf("cleanup failed - %s\n", ldb_errstring(*ldb));
377 printf("Finished index test\n");
381 static void usage(void)
383 printf("Usage: ldbtest <options>\n");
384 printf("Options:\n");
385 printf(" -H ldb_url choose the database (or $LDB_URL)\n");
386 printf(" --num-records nrecords database size to use\n");
387 printf(" --num-searches nsearches number of searches to do\n");
389 printf("tests ldb API\n\n");
393 int main(int argc, const char **argv)
395 TALLOC_CTX *mem_ctx = talloc_new(NULL);
396 struct ldb_context *ldb;
400 ldb = ldb_init(mem_ctx);
402 options = ldb_cmdline_process(ldb, argc, argv, usage);
404 talloc_steal(mem_ctx, options);
406 if (options->basedn == NULL) {
407 options->basedn = "ou=Ldb Test,ou=People,o=University of Michigan,c=TEST";
412 printf("Testing with num-records=%d and num-searches=%d\n",
413 options->num_records, options->num_searches);
415 start_test(ldb, options->num_records, options->num_searches);
417 start_test_index(&ldb);
419 talloc_free(mem_ctx);