r19831: Big ldb_dn optimization and interfaces enhancement patch
[jelmer/samba4-debian.git] / source / lib / ldb / tools / ldbtest.c
1 /* 
2    ldb database library
3
4    Copyright (C) Andrew Tridgell  2004
5
6      ** NOTE! The following LGPL license applies to the ldb
7      ** library. This does NOT imply that all of Samba is released
8      ** under the LGPL
9    
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.
14
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.
19
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
23 */
24
25 /*
26  *  Name: ldb
27  *
28  *  Component: ldbtest
29  *
30  *  Description: utility to test ldb
31  *
32  *  Author: Andrew Tridgell
33  */
34
35 #include "includes.h"
36 #include "ldb/include/includes.h"
37 #include "ldb/tools/cmdline.h"
38
39 static struct timeval tp1,tp2;
40 static struct ldb_cmdline *options;
41
42 static void _start_timer(void)
43 {
44         gettimeofday(&tp1,NULL);
45 }
46
47 static double _end_timer(void)
48 {
49         gettimeofday(&tp2,NULL);
50         return((tp2.tv_sec - tp1.tv_sec) + 
51                (tp2.tv_usec - tp1.tv_usec)*1.0e-6);
52 }
53
54 static void add_records(struct ldb_context *ldb,
55                         struct ldb_dn *basedn,
56                         int count)
57 {
58         struct ldb_message msg;
59         int i;
60
61 #if 0
62         if (ldb_lock(ldb, "transaction") != 0) {
63                 printf("transaction lock failed\n");
64                 exit(1);
65         }
66 #endif
67         for (i=0;i<count;i++) {
68                 struct ldb_message_element el[6];
69                 struct ldb_val vals[6][1];
70                 char *name;
71                 TALLOC_CTX *tmp_ctx = talloc_new(ldb);
72
73                 name = talloc_asprintf(tmp_ctx, "Test%d", i);
74
75                 msg.dn = ldb_dn_copy(tmp_ctx, basedn);
76                 ldb_dn_add_child_fmt(msg.dn, "cn=%s", name);
77                 msg.num_elements = 6;
78                 msg.elements = el;
79
80                 el[0].flags = 0;
81                 el[0].name = talloc_strdup(tmp_ctx, "cn");
82                 el[0].num_values = 1;
83                 el[0].values = vals[0];
84                 vals[0][0].data = (uint8_t *)name;
85                 vals[0][0].length = strlen(name);
86
87                 el[1].flags = 0;
88                 el[1].name = "title";
89                 el[1].num_values = 1;
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);
93
94                 el[2].flags = 0;
95                 el[2].name = talloc_strdup(tmp_ctx, "uid");
96                 el[2].num_values = 1;
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);
100
101                 el[3].flags = 0;
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);
107
108                 el[4].flags = 0;
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);
114
115                 el[5].flags = 0;
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);
121
122                 ldb_delete(ldb, msg.dn);
123
124                 if (ldb_add(ldb, &msg) != 0) {
125                         printf("Add of %s failed - %s\n", name, ldb_errstring(ldb));
126                         exit(1);
127                 }
128
129                 printf("adding uid %s\r", name);
130                 fflush(stdout);
131
132                 talloc_free(tmp_ctx);
133         }
134 #if 0
135         if (ldb_unlock(ldb, "transaction") != 0) {
136                 printf("transaction unlock failed\n");
137                 exit(1);
138         }
139 #endif
140         printf("\n");
141 }
142
143 static void modify_records(struct ldb_context *ldb,
144                            struct ldb_dn *basedn,
145                            int count)
146 {
147         struct ldb_message msg;
148         int i;
149
150         for (i=0;i<count;i++) {
151                 struct ldb_message_element el[3];
152                 struct ldb_val vals[3];
153                 char *name;
154                 TALLOC_CTX *tmp_ctx = talloc_new(ldb);
155                 
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);
159
160                 msg.num_elements = 3;
161                 msg.elements = el;
162
163                 el[0].flags = LDB_FLAG_MOD_DELETE;
164                 el[0].name = talloc_strdup(tmp_ctx, "mail");
165                 el[0].num_values = 0;
166
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);
173
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);
180
181                 if (ldb_modify(ldb, &msg) != 0) {
182                         printf("Modify of %s failed - %s\n", name, ldb_errstring(ldb));
183                         exit(1);
184                 }
185
186                 printf("Modifying uid %s\r", name);
187                 fflush(stdout);
188
189                 talloc_free(tmp_ctx);
190         }
191
192         printf("\n");
193 }
194
195
196 static void delete_records(struct ldb_context *ldb,
197                            struct ldb_dn *basedn,
198                            int count)
199 {
200         int i;
201
202         for (i=0;i<count;i++) {
203                 struct ldb_dn *dn;
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);
207
208                 printf("Deleting uid Test%d\r", i);
209                 fflush(stdout);
210
211                 if (ldb_delete(ldb, dn) != 0) {
212                         printf("Delete of %s failed - %s\n", ldb_dn_get_linearized(dn), ldb_errstring(ldb));
213                         exit(1);
214                 }
215                 talloc_free(name);
216         }
217
218         printf("\n");
219 }
220
221 static void search_uid(struct ldb_context *ldb, struct ldb_dn *basedn, int nrecords, int nsearches)
222 {
223         int i;
224
225         for (i=0;i<nsearches;i++) {
226                 int uid = (i * 700 + 17) % (nrecords * 2);
227                 char *expr;
228                 struct ldb_result *res = NULL;
229                 int ret;
230
231                 expr = talloc_asprintf(ldb, "(uid=TEST%d)", uid);
232                 ret = ldb_search(ldb, basedn, LDB_SCOPE_SUBTREE, expr, NULL, &res);
233
234                 if (ret != LDB_SUCCESS || (uid < nrecords && res->count != 1)) {
235                         printf("Failed to find %s - %s\n", expr, ldb_errstring(ldb));
236                         exit(1);
237                 }
238
239                 if (uid >= nrecords && res->count > 0) {
240                         printf("Found %s !? - %d\n", expr, ret);
241                         exit(1);
242                 }
243
244                 printf("testing uid %d/%d - %d  \r", i, uid, res->count);
245                 fflush(stdout);
246
247                 talloc_free(res);
248                 talloc_free(expr);
249         }
250
251         printf("\n");
252 }
253
254 static void start_test(struct ldb_context *ldb, int nrecords, int nsearches)
255 {
256         struct ldb_dn *basedn;
257
258         basedn = ldb_dn_new(ldb, ldb, options->basedn);
259         if ( ! ldb_dn_validate(basedn)) {
260                 printf("Invalid base DN\n");
261                 exit(1);
262         }
263
264         printf("Adding %d records\n", nrecords);
265         add_records(ldb, basedn, nrecords);
266
267         printf("Starting search on uid\n");
268         _start_timer();
269         search_uid(ldb, basedn, nrecords, nsearches);
270         printf("uid search took %.2f seconds\n", _end_timer());
271
272         printf("Modifying records\n");
273         modify_records(ldb, basedn, nrecords);
274
275         printf("Deleting records\n");
276         delete_records(ldb, basedn, nrecords);
277 }
278
279
280 /*
281       2) Store an @indexlist record
282
283       3) Store a record that contains fields that should be index according
284 to @index
285
286       4) disconnection from database
287
288       5) connect to same database
289
290       6) search for record added in step 3 using a search key that should
291 be indexed
292 */
293 static void start_test_index(struct ldb_context **ldb)
294 {
295         struct ldb_message *msg;
296         struct ldb_result *res = NULL;
297         struct ldb_dn *indexlist;
298         struct ldb_dn *basedn;
299         int ret;
300         int flags = 0;
301         const char *specials;
302
303         specials = getenv("LDB_SPECIALS");
304         if (specials && atoi(specials) == 0) {
305                 printf("LDB_SPECIALS disabled - skipping index test\n");
306                 return;
307         }
308
309         if (options->nosync) {
310                 flags |= LDB_FLG_NOSYNC;
311         }
312
313         printf("Starting index test\n");
314
315         indexlist = ldb_dn_new(*ldb, *ldb, "@INDEXLIST");
316
317         ldb_delete(*ldb, indexlist);
318
319         msg = ldb_msg_new(NULL);
320
321         msg->dn = indexlist;
322         ldb_msg_add_string(msg, "@IDXATTR", strdup("uid"));
323
324         if (ldb_add(*ldb, msg) != 0) {
325                 printf("Add of %s failed - %s\n", ldb_dn_linearize(*ldb, msg->dn), ldb_errstring(*ldb));
326                 exit(1);
327         }
328
329         basedn = ldb_dn_new(*ldb, *ldb, options->basedn);
330
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"));
338
339         if (ldb_add(*ldb, msg) != 0) {
340                 printf("Add of %s failed - %s\n", ldb_dn_linearize(*ldb, msg->dn), ldb_errstring(*ldb));
341                 exit(1);
342         }
343
344         if (talloc_free(*ldb) != 0) {
345                 printf("failed to free/close ldb database");
346                 exit(1);
347         }
348
349         (*ldb) = ldb_init(options);
350
351         ret = ldb_connect(*ldb, options->url, flags, NULL);
352         if (ret != 0) {
353                 printf("failed to connect to %s\n", options->url);
354                 exit(1);
355         }
356
357         basedn = ldb_dn_new(*ldb, *ldb, options->basedn);
358
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");
362                 exit(1);
363         }
364         if(res->count != 1) {
365                 printf("Should have found 1 record - found %d\n", res->count);
366                 exit(1);
367         }
368
369         indexlist = ldb_dn_new(*ldb, *ldb, "@INDEXLIST");
370
371         if (ldb_delete(*ldb, msg->dn) != 0 ||
372             ldb_delete(*ldb, indexlist) != 0) {
373                 printf("cleanup failed - %s\n", ldb_errstring(*ldb));
374                 exit(1);
375         }
376
377         printf("Finished index test\n");
378 }
379
380
381 static void usage(void)
382 {
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");
388         printf("\n");
389         printf("tests ldb API\n\n");
390         exit(1);
391 }
392
393 int main(int argc, const char **argv)
394 {
395         TALLOC_CTX *mem_ctx = talloc_new(NULL);
396         struct ldb_context *ldb;
397
398         ldb_global_init();
399
400         ldb = ldb_init(mem_ctx);
401
402         options = ldb_cmdline_process(ldb, argc, argv, usage);
403
404         talloc_steal(mem_ctx, options);
405
406         if (options->basedn == NULL) {
407                 options->basedn = "ou=Ldb Test,ou=People,o=University of Michigan,c=TEST";
408         }
409
410         srandom(1);
411
412         printf("Testing with num-records=%d and num-searches=%d\n", 
413                options->num_records, options->num_searches);
414
415         start_test(ldb, options->num_records, options->num_searches);
416
417         start_test_index(&ldb);
418
419         talloc_free(mem_ctx);
420
421         return 0;
422 }