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 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 asprintf(&name, "Test%d", i);
75 msg.dn = talloc_asprintf(tmp_ctx, "cn=%s,%s", name, basedn);
80 el[0].name = talloc_strdup(tmp_ctx, "cn");
82 el[0].values = vals[0];
83 vals[0][0].data = name;
84 vals[0][0].length = strlen(name);
87 el[1].name = talloc_strdup(tmp_ctx, "title");
89 el[1].values = vals[1];
90 vals[1][0].data = talloc_asprintf(tmp_ctx, "The title of %s", name);
91 vals[1][0].length = strlen(vals[1][0].data);
94 el[2].name = talloc_strdup(tmp_ctx, "uid");
96 el[2].values = vals[2];
97 vals[2][0].data = ldb_casefold(tmp_ctx, name);
98 vals[2][0].length = strlen(vals[2][0].data);
101 el[3].name = talloc_strdup(tmp_ctx, "mail");
102 el[3].num_values = 1;
103 el[3].values = vals[3];
104 vals[3][0].data = talloc_asprintf(tmp_ctx, "%s@example.com", name);
105 vals[3][0].length = strlen(vals[3][0].data);
108 el[4].name = talloc_strdup(tmp_ctx, "objectClass");
109 el[4].num_values = 1;
110 el[4].values = vals[4];
111 vals[4][0].data = talloc_strdup(tmp_ctx, "OpenLDAPperson");
112 vals[4][0].length = strlen(vals[4][0].data);
115 el[5].name = talloc_strdup(tmp_ctx, "sn");
116 el[5].num_values = 1;
117 el[5].values = vals[5];
118 vals[5][0].data = name;
119 vals[5][0].length = strlen(vals[5][0].data);
121 ldb_delete(ldb, msg.dn);
123 if (ldb_add(ldb, &msg) != 0) {
124 printf("Add of %s failed - %s\n", name, ldb_errstring(ldb));
128 printf("adding uid %s\r", name);
131 talloc_free(tmp_ctx);
137 static void modify_records(struct ldb_context *ldb,
141 struct ldb_message msg;
144 for (i=0;i<count;i++) {
145 struct ldb_message_element el[3];
146 struct ldb_val vals[3];
148 TALLOC_CTX *tmp_ctx = talloc_new(ldb);
150 name = talloc_asprintf(tmp_ctx, "Test%d", i);
151 msg.dn = talloc_asprintf(tmp_ctx, "cn=%s,%s", name, basedn);
153 msg.num_elements = 3;
156 el[0].flags = LDB_FLAG_MOD_DELETE;
157 el[0].name = talloc_strdup(tmp_ctx, "mail");
158 el[0].num_values = 0;
160 el[1].flags = LDB_FLAG_MOD_ADD;
161 el[1].name = talloc_strdup(tmp_ctx, "mail");
162 el[1].num_values = 1;
163 el[1].values = &vals[1];
164 vals[1].data = talloc_asprintf(tmp_ctx, "%s@other.example.com", name);
165 vals[1].length = strlen(vals[1].data);
167 el[2].flags = LDB_FLAG_MOD_REPLACE;
168 el[2].name = talloc_strdup(tmp_ctx, "mail");
169 el[2].num_values = 1;
170 el[2].values = &vals[2];
171 vals[2].data = talloc_asprintf(tmp_ctx, "%s@other2.example.com", name);
172 vals[2].length = strlen(vals[2].data);
174 if (ldb_modify(ldb, &msg) != 0) {
175 printf("Modify of %s failed - %s\n", name, ldb_errstring(ldb));
179 printf("Modifying uid %s\r", name);
182 talloc_free(tmp_ctx);
189 static void delete_records(struct ldb_context *ldb,
195 for (i=0;i<count;i++) {
197 asprintf(&dn, "cn=Test%d,%s", i, basedn);
199 printf("Deleting uid Test%d\r", i);
202 if (ldb_delete(ldb, dn) != 0) {
203 printf("Delete of %s failed - %s\n", dn, ldb_errstring(ldb));
212 static void search_uid(struct ldb_context *ldb, int nrecords, int nsearches)
216 for (i=0;i<nsearches;i++) {
217 int uid = (i * 700 + 17) % (nrecords * 2);
219 struct ldb_message **res;
222 asprintf(&expr, "(uid=TEST%d)", uid);
223 ret = ldb_search(ldb, options->basedn, LDB_SCOPE_SUBTREE, expr, NULL, &res);
225 if (uid < nrecords && ret != 1) {
226 printf("Failed to find %s - %s\n", expr, ldb_errstring(ldb));
230 if (uid >= nrecords && ret > 0) {
231 printf("Found %s !? - %d\n", expr, ret);
239 printf("testing uid %d/%d - %d \r", i, uid, ret);
248 static void start_test(struct ldb_context *ldb, int nrecords, int nsearches)
250 printf("Adding %d records\n", nrecords);
251 add_records(ldb, options->basedn, nrecords);
253 printf("Starting search on uid\n");
255 search_uid(ldb, nrecords, nsearches);
256 printf("uid search took %.2f seconds\n", _end_timer());
258 printf("Modifying records\n");
259 modify_records(ldb, options->basedn, nrecords);
261 printf("Deleting records\n");
262 delete_records(ldb, options->basedn, nrecords);
267 2) Store an @indexlist record
269 3) Store a record that contains fields that should be index according
272 4) disconnection from database
274 5) connect to same database
276 6) search for record added in step 3 using a search key that should
279 static void start_test_index(struct ldb_context **ldb)
281 struct ldb_message *msg;
282 struct ldb_message **res;
285 printf("Starting index test\n");
287 ldb_delete(*ldb, "@INDEXLIST");
289 msg = ldb_msg_new(NULL);
291 msg->dn = strdup("@INDEXLIST");
292 ldb_msg_add_string(*ldb, msg, "@IDXATTR", strdup("uid"));
294 if (ldb_add(*ldb, msg) != 0) {
295 printf("Add of %s failed - %s\n", msg->dn, ldb_errstring(*ldb));
299 memset(msg, 0, sizeof(*msg));
300 asprintf(&msg->dn, "cn=%s,%s", "test", options->basedn);
301 ldb_msg_add_string(*ldb, msg, "cn", strdup("test"));
302 ldb_msg_add_string(*ldb, msg, "sn", strdup("test"));
303 ldb_msg_add_string(*ldb, msg, "uid", strdup("test"));
304 ldb_msg_add_string(*ldb, msg, "objectClass", strdup("OpenLDAPperson"));
306 if (ldb_add(*ldb, msg) != 0) {
307 printf("Add of %s failed - %s\n", msg->dn, ldb_errstring(*ldb));
311 if (talloc_free(*ldb) != 0) {
312 printf("failed to free/close ldb database");
316 (*ldb) = ldb_init(options);
318 ret = ldb_connect(*ldb, options->url, 0, NULL);
320 printf("failed to connect to %s\n", options->url);
324 ret = ldb_search(*ldb, options->basedn, LDB_SCOPE_SUBTREE, "uid=test", NULL, &res);
326 printf("Should have found 1 record - found %d\n", ret);
330 if (ldb_delete(*ldb, msg->dn) != 0 ||
331 ldb_delete(*ldb, "@INDEXLIST") != 0) {
332 printf("cleanup failed - %s\n", ldb_errstring(*ldb));
336 printf("Finished index test\n");
340 static void usage(void)
342 printf("Usage: ldbtest <options>\n");
343 printf("Options:\n");
344 printf(" -H ldb_url choose the database (or $LDB_URL)\n");
345 printf(" -r nrecords database size to use\n");
346 printf(" -s nsearches number of searches to do\n");
348 printf("tests ldb API\n\n");
352 int main(int argc, const char **argv)
354 TALLOC_CTX *mem_ctx = talloc_new(NULL);
355 struct ldb_context *ldb;
358 ldb = ldb_init(mem_ctx);
360 options = ldb_cmdline_process(ldb, argc, argv, usage);
362 talloc_steal(mem_ctx, options);
364 if (options->basedn == NULL) {
365 options->basedn = "ou=Ldb Test,ou=People,o=University of Michigan,c=US";
370 start_test(ldb, options->num_records, options->num_searches);
372 start_test_index(&ldb);
374 talloc_free(mem_ctx);