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/ldb.h"
37 #include "ldb/include/ldb_private.h"
38 #include "ldb/tools/cmdline.h"
41 #include "system/filesys.h"
42 #include "system/time.h"
45 static struct timeval tp1,tp2;
46 static struct ldb_cmdline *options;
48 static void _start_timer(void)
50 gettimeofday(&tp1,NULL);
53 static double _end_timer(void)
55 gettimeofday(&tp2,NULL);
56 return((tp2.tv_sec - tp1.tv_sec) +
57 (tp2.tv_usec - tp1.tv_usec)*1.0e-6);
60 static void add_records(struct ldb_context *ldb,
64 struct ldb_message msg;
67 if (ldb_lock(ldb, "transaction") != 0) {
68 printf("transaction lock failed\n");
72 for (i=0;i<count;i++) {
73 struct ldb_message_element el[6];
74 struct ldb_val vals[6][1];
76 TALLOC_CTX *tmp_ctx = talloc_new(ldb);
78 asprintf(&name, "Test%d", i);
80 msg.dn = talloc_asprintf(tmp_ctx, "cn=%s,%s", name, basedn);
85 el[0].name = talloc_strdup(tmp_ctx, "cn");
87 el[0].values = vals[0];
88 vals[0][0].data = name;
89 vals[0][0].length = strlen(name);
92 el[1].name = talloc_strdup(tmp_ctx, "title");
94 el[1].values = vals[1];
95 vals[1][0].data = talloc_asprintf(tmp_ctx, "The title of %s", name);
96 vals[1][0].length = strlen(vals[1][0].data);
99 el[2].name = talloc_strdup(tmp_ctx, "uid");
100 el[2].num_values = 1;
101 el[2].values = vals[2];
102 vals[2][0].data = ldb_casefold(tmp_ctx, name);
103 vals[2][0].length = strlen(vals[2][0].data);
106 el[3].name = talloc_strdup(tmp_ctx, "mail");
107 el[3].num_values = 1;
108 el[3].values = vals[3];
109 vals[3][0].data = talloc_asprintf(tmp_ctx, "%s@example.com", name);
110 vals[3][0].length = strlen(vals[3][0].data);
113 el[4].name = talloc_strdup(tmp_ctx, "objectClass");
114 el[4].num_values = 1;
115 el[4].values = vals[4];
116 vals[4][0].data = talloc_strdup(tmp_ctx, "OpenLDAPperson");
117 vals[4][0].length = strlen(vals[4][0].data);
120 el[5].name = talloc_strdup(tmp_ctx, "sn");
121 el[5].num_values = 1;
122 el[5].values = vals[5];
123 vals[5][0].data = name;
124 vals[5][0].length = strlen(vals[5][0].data);
126 ldb_delete(ldb, msg.dn);
128 if (ldb_add(ldb, &msg) != 0) {
129 printf("Add of %s failed - %s\n", name, ldb_errstring(ldb));
133 printf("adding uid %s\r", name);
136 talloc_free(tmp_ctx);
139 if (ldb_unlock(ldb, "transaction") != 0) {
140 printf("transaction unlock failed\n");
147 static void modify_records(struct ldb_context *ldb,
151 struct ldb_message msg;
154 for (i=0;i<count;i++) {
155 struct ldb_message_element el[3];
156 struct ldb_val vals[3];
158 TALLOC_CTX *tmp_ctx = talloc_new(ldb);
160 name = talloc_asprintf(tmp_ctx, "Test%d", i);
161 msg.dn = talloc_asprintf(tmp_ctx, "cn=%s,%s", name, basedn);
163 msg.num_elements = 3;
166 el[0].flags = LDB_FLAG_MOD_DELETE;
167 el[0].name = talloc_strdup(tmp_ctx, "mail");
168 el[0].num_values = 0;
170 el[1].flags = LDB_FLAG_MOD_ADD;
171 el[1].name = talloc_strdup(tmp_ctx, "mail");
172 el[1].num_values = 1;
173 el[1].values = &vals[1];
174 vals[1].data = talloc_asprintf(tmp_ctx, "%s@other.example.com", name);
175 vals[1].length = strlen(vals[1].data);
177 el[2].flags = LDB_FLAG_MOD_REPLACE;
178 el[2].name = talloc_strdup(tmp_ctx, "mail");
179 el[2].num_values = 1;
180 el[2].values = &vals[2];
181 vals[2].data = talloc_asprintf(tmp_ctx, "%s@other2.example.com", name);
182 vals[2].length = strlen(vals[2].data);
184 if (ldb_modify(ldb, &msg) != 0) {
185 printf("Modify of %s failed - %s\n", name, ldb_errstring(ldb));
189 printf("Modifying uid %s\r", name);
192 talloc_free(tmp_ctx);
199 static void delete_records(struct ldb_context *ldb,
205 for (i=0;i<count;i++) {
207 asprintf(&dn, "cn=Test%d,%s", i, basedn);
209 printf("Deleting uid Test%d\r", i);
212 if (ldb_delete(ldb, dn) != 0) {
213 printf("Delete of %s failed - %s\n", dn, ldb_errstring(ldb));
222 static void search_uid(struct ldb_context *ldb, int nrecords, int nsearches)
226 for (i=0;i<nsearches;i++) {
227 int uid = (i * 700 + 17) % (nrecords * 2);
229 struct ldb_message **res;
232 asprintf(&expr, "(uid=TEST%d)", uid);
233 ret = ldb_search(ldb, options->basedn, LDB_SCOPE_SUBTREE, expr, NULL, &res);
235 if (uid < nrecords && ret != 1) {
236 printf("Failed to find %s - %s\n", expr, ldb_errstring(ldb));
240 if (uid >= nrecords && ret > 0) {
241 printf("Found %s !? - %d\n", expr, ret);
249 printf("testing uid %d/%d - %d \r", i, uid, ret);
258 static void start_test(struct ldb_context *ldb, int nrecords, int nsearches)
260 printf("Adding %d records\n", nrecords);
261 add_records(ldb, options->basedn, nrecords);
263 printf("Starting search on uid\n");
265 search_uid(ldb, nrecords, nsearches);
266 printf("uid search took %.2f seconds\n", _end_timer());
268 printf("Modifying records\n");
269 modify_records(ldb, options->basedn, nrecords);
271 printf("Deleting records\n");
272 delete_records(ldb, options->basedn, nrecords);
277 2) Store an @indexlist record
279 3) Store a record that contains fields that should be index according
282 4) disconnection from database
284 5) connect to same database
286 6) search for record added in step 3 using a search key that should
289 static void start_test_index(struct ldb_context **ldb)
291 struct ldb_message *msg;
292 struct ldb_message **res;
295 printf("Starting index test\n");
297 ldb_delete(*ldb, "@INDEXLIST");
299 msg = ldb_msg_new(NULL);
301 msg->dn = strdup("@INDEXLIST");
302 ldb_msg_add_string(*ldb, msg, "@IDXATTR", strdup("uid"));
304 if (ldb_add(*ldb, msg) != 0) {
305 printf("Add of %s failed - %s\n", msg->dn, ldb_errstring(*ldb));
309 memset(msg, 0, sizeof(*msg));
310 asprintf(&msg->dn, "cn=%s,%s", "test", options->basedn);
311 ldb_msg_add_string(*ldb, msg, "cn", strdup("test"));
312 ldb_msg_add_string(*ldb, msg, "sn", strdup("test"));
313 ldb_msg_add_string(*ldb, msg, "uid", strdup("test"));
314 ldb_msg_add_string(*ldb, msg, "objectClass", strdup("OpenLDAPperson"));
316 if (ldb_add(*ldb, msg) != 0) {
317 printf("Add of %s failed - %s\n", msg->dn, ldb_errstring(*ldb));
321 if (talloc_free(*ldb) != 0) {
322 printf("failed to free/close ldb database");
326 (*ldb) = ldb_init(options);
328 ret = ldb_connect(*ldb, options->url, 0, NULL);
330 printf("failed to connect to %s\n", options->url);
334 ret = ldb_search(*ldb, options->basedn, LDB_SCOPE_SUBTREE, "uid=test", NULL, &res);
336 printf("Should have found 1 record - found %d\n", ret);
340 if (ldb_delete(*ldb, msg->dn) != 0 ||
341 ldb_delete(*ldb, "@INDEXLIST") != 0) {
342 printf("cleanup failed - %s\n", ldb_errstring(*ldb));
346 printf("Finished index test\n");
350 static void usage(void)
352 printf("Usage: ldbtest <options>\n");
353 printf("Options:\n");
354 printf(" -H ldb_url choose the database (or $LDB_URL)\n");
355 printf(" -r nrecords database size to use\n");
356 printf(" -s nsearches number of searches to do\n");
358 printf("tests ldb API\n\n");
362 int main(int argc, const char **argv)
364 TALLOC_CTX *mem_ctx = talloc_new(NULL);
365 struct ldb_context *ldb;
367 ldb = ldb_init(mem_ctx);
369 options = ldb_cmdline_process(ldb, argc, argv, usage);
371 talloc_steal(mem_ctx, options);
373 if (options->basedn == NULL) {
374 options->basedn = "ou=Ldb Test,ou=People,o=University of Michigan,c=US";
379 start_test(ldb, options->num_records, options->num_searches);
381 start_test_index(&ldb);
383 talloc_free(mem_ctx);