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"
40 #include "system/filesys.h"
43 static const char *ldb_url;
44 static const char *base_dn = "ou=Ldb Test,ou=People,o=University of Michigan,c=US";
46 static struct timeval tp1,tp2;
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];
73 asprintf(&name, "Test%d", i);
75 asprintf(&msg.dn, "cn=%s,%s", name, basedn);
80 el[0].name = strdup("cn");
82 el[0].values = vals[0];
83 vals[0][0].data = name;
84 vals[0][0].length = strlen(name);
87 el[1].name = strdup("title");
89 el[1].values = vals[1];
90 asprintf((char **)&vals[1][0].data, "The title of %s", name);
91 vals[1][0].length = strlen(vals[1][0].data);
94 el[2].name = strdup("uid");
96 el[2].values = vals[2];
97 vals[2][0].data = ldb_casefold(ldb, name);
98 vals[2][0].length = strlen(vals[2][0].data);
101 el[3].name = strdup("mail");
102 el[3].num_values = 1;
103 el[3].values = vals[3];
104 asprintf((char **)&vals[3][0].data, "%s@example.com", name);
105 vals[3][0].length = strlen(vals[3][0].data);
108 el[4].name = strdup("objectClass");
109 el[4].num_values = 1;
110 el[4].values = vals[4];
111 vals[4][0].data = strdup("OpenLDAPperson");
112 vals[4][0].length = strlen(vals[4][0].data);
115 el[5].name = strdup("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 for (j=0;j<msg.num_elements;j++) {
136 free(vals[1][0].data);
137 talloc_free(vals[2][0].data);
138 free(vals[3][0].data);
139 free(vals[4][0].data);
145 static void modify_records(struct ldb_context *ldb,
149 struct ldb_message msg;
152 for (i=0;i<count;i++) {
153 struct ldb_message_element el[3];
154 struct ldb_val vals[3];
158 asprintf(&name, "Test%d", i);
159 asprintf(&msg.dn, "cn=%s,%s", name, basedn);
161 msg.num_elements = 3;
164 el[0].flags = LDB_FLAG_MOD_DELETE;
165 el[0].name = strdup("mail");
166 el[0].num_values = 0;
168 el[1].flags = LDB_FLAG_MOD_ADD;
169 el[1].name = strdup("mail");
170 el[1].num_values = 1;
171 el[1].values = &vals[1];
172 asprintf((char **)&vals[1].data, "%s@other.example.com", name);
173 vals[1].length = strlen(vals[1].data);
175 el[2].flags = LDB_FLAG_MOD_REPLACE;
176 el[2].name = strdup("mail");
177 el[2].num_values = 1;
178 el[2].values = &vals[2];
179 asprintf((char **)&vals[2].data, "%s@other2.example.com", name);
180 vals[2].length = strlen(vals[2].data);
182 if (ldb_modify(ldb, &msg) != 0) {
183 printf("Modify of %s failed - %s\n", name, ldb_errstring(ldb));
187 printf("Modifying uid %s\r", name);
190 for (j=0;j<msg.num_elements;j++) {
203 static void delete_records(struct ldb_context *ldb,
209 for (i=0;i<count;i++) {
211 asprintf(&dn, "cn=Test%d,%s", i, basedn);
213 printf("Deleting uid Test%d\r", i);
216 if (ldb_delete(ldb, dn) != 0) {
217 printf("Delete of %s failed - %s\n", dn, ldb_errstring(ldb));
226 static void search_uid(struct ldb_context *ldb, int nrecords, int nsearches)
230 for (i=0;i<nsearches;i++) {
231 int uid = (i * 700 + 17) % (nrecords * 2);
233 struct ldb_message **res;
236 asprintf(&expr, "(uid=TEST%d)", uid);
237 ret = ldb_search(ldb, base_dn, LDB_SCOPE_SUBTREE, expr, NULL, &res);
239 if (uid < nrecords && ret != 1) {
240 printf("Failed to find %s - %s\n", expr, ldb_errstring(ldb));
244 if (uid >= nrecords && ret > 0) {
245 printf("Found %s !? - %d\n", expr, ret);
250 ldb_search_free(ldb, res);
253 printf("testing uid %d/%d - %d \r", i, uid, ret);
262 static void start_test(struct ldb_context *ldb, int nrecords, int nsearches)
264 printf("Adding %d records\n", nrecords);
265 add_records(ldb, base_dn, nrecords);
267 printf("Starting search on uid\n");
269 search_uid(ldb, nrecords, nsearches);
270 printf("uid search took %.2f seconds\n", _end_timer());
272 printf("Modifying records\n");
273 modify_records(ldb, base_dn, nrecords);
275 printf("Deleting records\n");
276 delete_records(ldb, base_dn, 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_message **res;
299 printf("Starting index test\n");
301 ldb_delete(*ldb, "@INDEXLIST");
303 msg = ldb_msg_new(NULL);
305 msg->dn = strdup("@INDEXLIST");
306 ldb_msg_add_string(*ldb, msg, "@IDXATTR", strdup("uid"));
308 if (ldb_add(*ldb, msg) != 0) {
309 printf("Add of %s failed - %s\n", msg->dn, ldb_errstring(*ldb));
313 memset(msg, 0, sizeof(*msg));
314 asprintf(&msg->dn, "cn=%s,%s", "test", base_dn);
315 ldb_msg_add_string(*ldb, msg, "cn", strdup("test"));
316 ldb_msg_add_string(*ldb, msg, "sn", strdup("test"));
317 ldb_msg_add_string(*ldb, msg, "uid", strdup("test"));
318 ldb_msg_add_string(*ldb, msg, "objectClass", strdup("OpenLDAPperson"));
320 if (ldb_add(*ldb, msg) != 0) {
321 printf("Add of %s failed - %s\n", msg->dn, ldb_errstring(*ldb));
325 if (ldb_close(*ldb) != 0) {
326 printf("ldb_close failed - %s\n", ldb_errstring(*ldb));
330 *ldb = ldb_connect(ldb_url, 0, NULL);
333 perror("ldb_connect");
337 ret = ldb_search(*ldb, base_dn, LDB_SCOPE_SUBTREE, "uid=test", NULL, &res);
339 printf("Should have found 1 record - found %d\n", ret);
343 if (ldb_delete(*ldb, msg->dn) != 0 ||
344 ldb_delete(*ldb, "@INDEXLIST") != 0) {
345 printf("cleanup failed - %s\n", ldb_errstring(*ldb));
349 printf("Finished index test\n");
353 static void usage(void)
355 printf("Usage: ldbtest <options>\n");
356 printf("Options:\n");
357 printf(" -H ldb_url choose the database (or $LDB_URL)\n");
358 printf(" -r nrecords database size to use\n");
359 printf(" -s nsearches number of searches to do\n");
361 printf("tests ldb API\n\n");
365 int main(int argc, char * const argv[])
367 struct ldb_context *ldb;
368 const char **options = NULL;
372 int nsearches = 2000;
374 ldb_url = getenv("LDB_URL");
377 while ((opt = getopt(argc, argv, "hH:r:s:b:o:")) != EOF) {
384 nrecords = atoi(optarg);
392 nsearches = atoi(optarg);
396 options = ldb_options_parse(options, &ldbopts, optarg);
407 fprintf(stderr, "You must specify a ldb URL\n\n");
414 ldb = ldb_connect(ldb_url, 0, options);
417 perror("ldb_connect");
421 ldb_set_debug_stderr(ldb);
425 start_test(ldb, nrecords, nsearches);
427 start_test_index(&ldb);