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 3 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, see <http://www.gnu.org/licenses/>.
29 * Description: utility to test ldb
31 * Author: Andrew Tridgell
37 #include "ldb_includes.h"
41 #include "tools/cmdline.h"
43 static struct timespec tp1,tp2;
44 static struct ldb_cmdline *options;
46 static void _start_timer(void)
48 if (clock_gettime(CUSTOM_CLOCK_MONOTONIC, &tp1) != 0) {
49 clock_gettime(CLOCK_REALTIME, &tp1);
53 static double _end_timer(void)
55 if (clock_gettime(CUSTOM_CLOCK_MONOTONIC, &tp2) != 0) {
56 clock_gettime(CLOCK_REALTIME, &tp2);
58 return((tp2.tv_sec - tp1.tv_sec) +
59 (tp2.tv_nsec - tp1.tv_nsec)*1.0e-9);
62 static void add_records(struct ldb_context *ldb,
63 struct ldb_dn *basedn,
66 struct ldb_message msg;
70 if (ldb_lock(ldb, "transaction") != 0) {
71 printf("transaction lock failed\n");
75 for (i=0;i<count;i++) {
76 struct ldb_message_element el[6];
77 struct ldb_val vals[6][1];
79 TALLOC_CTX *tmp_ctx = talloc_new(ldb);
81 name = talloc_asprintf(tmp_ctx, "Test%d", i);
83 msg.dn = ldb_dn_copy(tmp_ctx, basedn);
84 ldb_dn_add_child_fmt(msg.dn, "cn=%s", name);
89 el[0].name = talloc_strdup(tmp_ctx, "cn");
91 el[0].values = vals[0];
92 vals[0][0].data = (uint8_t *)name;
93 vals[0][0].length = strlen(name);
98 el[1].values = vals[1];
99 vals[1][0].data = (uint8_t *)talloc_asprintf(tmp_ctx, "The title of %s", name);
100 vals[1][0].length = strlen((char *)vals[1][0].data);
103 el[2].name = talloc_strdup(tmp_ctx, "uid");
104 el[2].num_values = 1;
105 el[2].values = vals[2];
106 vals[2][0].data = (uint8_t *)ldb_casefold(ldb, tmp_ctx, name, strlen(name));
107 vals[2][0].length = strlen((char *)vals[2][0].data);
110 el[3].name = talloc_strdup(tmp_ctx, "mail");
111 el[3].num_values = 1;
112 el[3].values = vals[3];
113 vals[3][0].data = (uint8_t *)talloc_asprintf(tmp_ctx, "%s@example.com", name);
114 vals[3][0].length = strlen((char *)vals[3][0].data);
117 el[4].name = talloc_strdup(tmp_ctx, "objectClass");
118 el[4].num_values = 1;
119 el[4].values = vals[4];
120 vals[4][0].data = (uint8_t *)talloc_strdup(tmp_ctx, "OpenLDAPperson");
121 vals[4][0].length = strlen((char *)vals[4][0].data);
124 el[5].name = talloc_strdup(tmp_ctx, "sn");
125 el[5].num_values = 1;
126 el[5].values = vals[5];
127 vals[5][0].data = (uint8_t *)name;
128 vals[5][0].length = strlen((char *)vals[5][0].data);
130 ldb_delete(ldb, msg.dn);
132 if (ldb_add(ldb, &msg) != 0) {
133 printf("Add of %s failed - %s\n", name, ldb_errstring(ldb));
137 printf("adding uid %s\r", name);
140 talloc_free(tmp_ctx);
143 if (ldb_unlock(ldb, "transaction") != 0) {
144 printf("transaction unlock failed\n");
151 static void modify_records(struct ldb_context *ldb,
152 struct ldb_dn *basedn,
155 struct ldb_message msg;
158 for (i=0;i<count;i++) {
159 struct ldb_message_element el[3];
160 struct ldb_val vals[3];
162 TALLOC_CTX *tmp_ctx = talloc_new(ldb);
164 name = talloc_asprintf(tmp_ctx, "Test%d", i);
165 msg.dn = ldb_dn_copy(tmp_ctx, basedn);
166 ldb_dn_add_child_fmt(msg.dn, "cn=%s", name);
168 msg.num_elements = 3;
171 el[0].flags = LDB_FLAG_MOD_DELETE;
172 el[0].name = talloc_strdup(tmp_ctx, "mail");
173 el[0].num_values = 0;
175 el[1].flags = LDB_FLAG_MOD_ADD;
176 el[1].name = talloc_strdup(tmp_ctx, "mail");
177 el[1].num_values = 1;
178 el[1].values = &vals[1];
179 vals[1].data = (uint8_t *)talloc_asprintf(tmp_ctx, "%s@other.example.com", name);
180 vals[1].length = strlen((char *)vals[1].data);
182 el[2].flags = LDB_FLAG_MOD_REPLACE;
183 el[2].name = talloc_strdup(tmp_ctx, "mail");
184 el[2].num_values = 1;
185 el[2].values = &vals[2];
186 vals[2].data = (uint8_t *)talloc_asprintf(tmp_ctx, "%s@other2.example.com", name);
187 vals[2].length = strlen((char *)vals[2].data);
189 if (ldb_modify(ldb, &msg) != 0) {
190 printf("Modify of %s failed - %s\n", name, ldb_errstring(ldb));
194 printf("Modifying uid %s\r", name);
197 talloc_free(tmp_ctx);
204 static void delete_records(struct ldb_context *ldb,
205 struct ldb_dn *basedn,
210 for (i=0;i<count;i++) {
212 char *name = talloc_asprintf(ldb, "Test%d", i);
213 dn = ldb_dn_copy(name, basedn);
214 ldb_dn_add_child_fmt(dn, "cn=%s", name);
216 printf("Deleting uid Test%d\r", i);
219 if (ldb_delete(ldb, dn) != 0) {
220 printf("Delete of %s failed - %s\n", ldb_dn_get_linearized(dn), ldb_errstring(ldb));
229 static void search_uid(struct ldb_context *ldb, struct ldb_dn *basedn,
230 unsigned int nrecords, unsigned int nsearches)
234 for (i=0;i<nsearches;i++) {
235 int uid = (i * 700 + 17) % (nrecords * 2);
237 struct ldb_result *res = NULL;
240 expr = talloc_asprintf(ldb, "(uid=TEST%d)", uid);
241 ret = ldb_search(ldb, ldb, &res, basedn, LDB_SCOPE_SUBTREE, NULL, "%s", expr);
243 if (ret != LDB_SUCCESS || (uid < nrecords && res->count != 1)) {
244 printf("Failed to find %s - %s\n", expr, ldb_errstring(ldb));
248 if (uid >= nrecords && res->count > 0) {
249 printf("Found %s !? - %d\n", expr, ret);
253 printf("Testing uid %d/%d - %d \r", i, uid, res->count);
263 static void start_test(struct ldb_context *ldb, unsigned int nrecords,
264 unsigned int nsearches)
266 struct ldb_dn *basedn;
268 basedn = ldb_dn_new(ldb, ldb, options->basedn);
269 if ( ! ldb_dn_validate(basedn)) {
270 printf("Invalid base DN\n");
274 printf("Adding %d records\n", nrecords);
275 add_records(ldb, basedn, nrecords);
277 printf("Starting search on uid\n");
279 search_uid(ldb, basedn, nrecords, nsearches);
280 printf("uid search took %.2f seconds\n", _end_timer());
282 printf("Modifying records\n");
283 modify_records(ldb, basedn, nrecords);
285 printf("Deleting records\n");
286 delete_records(ldb, basedn, nrecords);
291 2) Store an @indexlist record
293 3) Store a record that contains fields that should be index according
296 4) disconnection from database
298 5) connect to same database
300 6) search for record added in step 3 using a search key that should
303 static void start_test_index(struct ldb_context **ldb)
305 struct ldb_message *msg;
306 struct ldb_result *res = NULL;
307 struct ldb_dn *indexlist;
308 struct ldb_dn *basedn;
311 const char *specials;
313 specials = getenv("LDB_SPECIALS");
314 if (specials && atoi(specials) == 0) {
315 printf("LDB_SPECIALS disabled - skipping index test\n");
319 if (options->nosync) {
320 flags |= LDB_FLG_NOSYNC;
323 printf("Starting index test\n");
325 indexlist = ldb_dn_new(*ldb, *ldb, "@INDEXLIST");
327 ldb_delete(*ldb, indexlist);
329 msg = ldb_msg_new(NULL);
332 ldb_msg_add_string(msg, "@IDXATTR", strdup("uid"));
334 if (ldb_add(*ldb, msg) != 0) {
335 printf("Add of %s failed - %s\n", ldb_dn_get_linearized(msg->dn), ldb_errstring(*ldb));
339 basedn = ldb_dn_new(*ldb, *ldb, options->basedn);
341 memset(msg, 0, sizeof(*msg));
342 msg->dn = ldb_dn_copy(msg, basedn);
343 ldb_dn_add_child_fmt(msg->dn, "cn=test");
344 ldb_msg_add_string(msg, "cn", strdup("test"));
345 ldb_msg_add_string(msg, "sn", strdup("test"));
346 ldb_msg_add_string(msg, "uid", strdup("test"));
347 ldb_msg_add_string(msg, "objectClass", strdup("OpenLDAPperson"));
349 if (ldb_add(*ldb, msg) != 0) {
350 printf("Add of %s failed - %s\n", ldb_dn_get_linearized(msg->dn), ldb_errstring(*ldb));
354 if (talloc_free(*ldb) != 0) {
355 printf("failed to free/close ldb database");
359 (*ldb) = ldb_init(options, NULL);
361 ret = ldb_connect(*ldb, options->url, flags, NULL);
363 printf("failed to connect to %s\n", options->url);
367 basedn = ldb_dn_new(*ldb, *ldb, options->basedn);
369 ldb_dn_add_child_fmt(msg->dn, "cn=test");
371 ret = ldb_search(*ldb, *ldb, &res, basedn, LDB_SCOPE_SUBTREE, NULL, "uid=test");
372 if (ret != LDB_SUCCESS) {
373 printf("Search with (uid=test) filter failed!\n");
376 if(res->count != 1) {
377 printf("Should have found 1 record - found %d\n", res->count);
381 indexlist = ldb_dn_new(*ldb, *ldb, "@INDEXLIST");
383 if (ldb_delete(*ldb, msg->dn) != 0 ||
384 ldb_delete(*ldb, indexlist) != 0) {
385 printf("cleanup failed - %s\n", ldb_errstring(*ldb));
389 printf("Finished index test\n");
393 static void usage(void)
395 printf("Usage: ldbtest <options>\n");
396 printf("Options:\n");
397 printf(" -H ldb_url choose the database (or $LDB_URL)\n");
398 printf(" --num-records nrecords database size to use\n");
399 printf(" --num-searches nsearches number of searches to do\n");
401 printf("tests ldb API\n\n");
405 int main(int argc, const char **argv)
407 TALLOC_CTX *mem_ctx = talloc_new(NULL);
408 struct ldb_context *ldb;
410 ldb = ldb_init(mem_ctx, NULL);
412 options = ldb_cmdline_process(ldb, argc, argv, usage);
414 talloc_steal(mem_ctx, options);
416 if (options->basedn == NULL) {
417 options->basedn = "ou=Ldb Test,ou=People,o=University of Michigan,c=TEST";
422 printf("Testing with num-records=%d and num-searches=%d\n",
423 options->num_records, options->num_searches);
426 (unsigned int) options->num_records,
427 (unsigned int) options->num_searches);
429 start_test_index(&ldb);
431 talloc_free(mem_ctx);