aa185f12b8fbd2089fd5a2c19be0cdfc3d81131c
[kai/samba-autobuild/.git] / source3 / 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 3 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                         const 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_build_child(tmp_ctx, "cn", name, basedn);
76                 msg.num_elements = 6;
77                 msg.elements = el;
78
79                 el[0].flags = 0;
80                 el[0].name = talloc_strdup(tmp_ctx, "cn");
81                 el[0].num_values = 1;
82                 el[0].values = vals[0];
83                 vals[0][0].data = (uint8_t *)name;
84                 vals[0][0].length = strlen(name);
85
86                 el[1].flags = 0;
87                 el[1].name = "title";
88                 el[1].num_values = 1;
89                 el[1].values = vals[1];
90                 vals[1][0].data = (uint8_t *)talloc_asprintf(tmp_ctx, "The title of %s", name);
91                 vals[1][0].length = strlen((char *)vals[1][0].data);
92
93                 el[2].flags = 0;
94                 el[2].name = talloc_strdup(tmp_ctx, "uid");
95                 el[2].num_values = 1;
96                 el[2].values = vals[2];
97                 vals[2][0].data = (uint8_t *)ldb_casefold(ldb, tmp_ctx, name);
98                 vals[2][0].length = strlen((char *)vals[2][0].data);
99
100                 el[3].flags = 0;
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 = (uint8_t *)talloc_asprintf(tmp_ctx, "%s@example.com", name);
105                 vals[3][0].length = strlen((char *)vals[3][0].data);
106
107                 el[4].flags = 0;
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 = (uint8_t *)talloc_strdup(tmp_ctx, "OpenLDAPperson");
112                 vals[4][0].length = strlen((char *)vals[4][0].data);
113
114                 el[5].flags = 0;
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 = (uint8_t *)name;
119                 vals[5][0].length = strlen((char *)vals[5][0].data);
120
121                 ldb_delete(ldb, msg.dn);
122
123                 if (ldb_add(ldb, &msg) != 0) {
124                         printf("Add of %s failed - %s\n", name, ldb_errstring(ldb));
125                         exit(1);
126                 }
127
128                 printf("adding uid %s\r", name);
129                 fflush(stdout);
130
131                 talloc_free(tmp_ctx);
132         }
133 #if 0
134         if (ldb_unlock(ldb, "transaction") != 0) {
135                 printf("transaction unlock failed\n");
136                 exit(1);
137         }
138 #endif
139         printf("\n");
140 }
141
142 static void modify_records(struct ldb_context *ldb,
143                            const struct ldb_dn *basedn,
144                            int count)
145 {
146         struct ldb_message msg;
147         int i;
148
149         for (i=0;i<count;i++) {
150                 struct ldb_message_element el[3];
151                 struct ldb_val vals[3];
152                 char *name;
153                 TALLOC_CTX *tmp_ctx = talloc_new(ldb);
154                 
155                 name = talloc_asprintf(tmp_ctx, "Test%d", i);
156                 msg.dn = ldb_dn_build_child(tmp_ctx, "cn", name, basedn);
157
158                 msg.num_elements = 3;
159                 msg.elements = el;
160
161                 el[0].flags = LDB_FLAG_MOD_DELETE;
162                 el[0].name = talloc_strdup(tmp_ctx, "mail");
163                 el[0].num_values = 0;
164
165                 el[1].flags = LDB_FLAG_MOD_ADD;
166                 el[1].name = talloc_strdup(tmp_ctx, "mail");
167                 el[1].num_values = 1;
168                 el[1].values = &vals[1];
169                 vals[1].data = (uint8_t *)talloc_asprintf(tmp_ctx, "%s@other.example.com", name);
170                 vals[1].length = strlen((char *)vals[1].data);
171
172                 el[2].flags = LDB_FLAG_MOD_REPLACE;
173                 el[2].name = talloc_strdup(tmp_ctx, "mail");
174                 el[2].num_values = 1;
175                 el[2].values = &vals[2];
176                 vals[2].data = (uint8_t *)talloc_asprintf(tmp_ctx, "%s@other2.example.com", name);
177                 vals[2].length = strlen((char *)vals[2].data);
178
179                 if (ldb_modify(ldb, &msg) != 0) {
180                         printf("Modify of %s failed - %s\n", name, ldb_errstring(ldb));
181                         exit(1);
182                 }
183
184                 printf("Modifying uid %s\r", name);
185                 fflush(stdout);
186
187                 talloc_free(tmp_ctx);
188         }
189
190         printf("\n");
191 }
192
193
194 static void delete_records(struct ldb_context *ldb,
195                            const struct ldb_dn *basedn,
196                            int count)
197 {
198         int i;
199
200         for (i=0;i<count;i++) {
201                 struct ldb_dn *dn;
202                 char *name = talloc_asprintf(ldb, "Test%d", i);
203                 dn = ldb_dn_build_child(name, "cn", name, basedn);
204
205                 printf("Deleting uid Test%d\r", i);
206                 fflush(stdout);
207
208                 if (ldb_delete(ldb, dn) != 0) {
209                         printf("Delete of %s failed - %s\n", ldb_dn_linearize(ldb, dn), ldb_errstring(ldb));
210                         exit(1);
211                 }
212                 talloc_free(name);
213         }
214
215         printf("\n");
216 }
217
218 static void search_uid(struct ldb_context *ldb, struct ldb_dn *basedn, int nrecords, int nsearches)
219 {
220         int i;
221
222         for (i=0;i<nsearches;i++) {
223                 int uid = (i * 700 + 17) % (nrecords * 2);
224                 char *expr;
225                 struct ldb_result *res = NULL;
226                 int ret;
227
228                 expr = talloc_asprintf(ldb, "(uid=TEST%d)", uid);
229                 ret = ldb_search(ldb, basedn, LDB_SCOPE_SUBTREE, expr, NULL, &res);
230
231                 if (ret != LDB_SUCCESS || (uid < nrecords && res->count != 1)) {
232                         printf("Failed to find %s - %s\n", expr, ldb_errstring(ldb));
233                         exit(1);
234                 }
235
236                 if (uid >= nrecords && res->count > 0) {
237                         printf("Found %s !? - %d\n", expr, ret);
238                         exit(1);
239                 }
240
241                 printf("testing uid %d/%d - %d  \r", i, uid, res->count);
242                 fflush(stdout);
243
244                 talloc_free(res);
245                 talloc_free(expr);
246         }
247
248         printf("\n");
249 }
250
251 static void start_test(struct ldb_context *ldb, int nrecords, int nsearches)
252 {
253         struct ldb_dn *basedn;
254
255         basedn = ldb_dn_explode(ldb, options->basedn);
256
257         printf("Adding %d records\n", nrecords);
258         add_records(ldb, basedn, nrecords);
259
260         printf("Starting search on uid\n");
261         _start_timer();
262         search_uid(ldb, basedn, nrecords, nsearches);
263         printf("uid search took %.2f seconds\n", _end_timer());
264
265         printf("Modifying records\n");
266         modify_records(ldb, basedn, nrecords);
267
268         printf("Deleting records\n");
269         delete_records(ldb, basedn, nrecords);
270 }
271
272
273 /*
274       2) Store an @indexlist record
275
276       3) Store a record that contains fields that should be index according
277 to @index
278
279       4) disconnection from database
280
281       5) connect to same database
282
283       6) search for record added in step 3 using a search key that should
284 be indexed
285 */
286 static void start_test_index(struct ldb_context **ldb)
287 {
288         struct ldb_message *msg;
289         struct ldb_result *res = NULL;
290         struct ldb_dn *indexlist;
291         struct ldb_dn *basedn;
292         int ret;
293         int flags = 0;
294         const char *specials;
295
296         specials = getenv("LDB_SPECIALS");
297         if (specials && atoi(specials) == 0) {
298                 printf("LDB_SPECIALS disabled - skipping index test\n");
299                 return;
300         }
301
302         if (options->nosync) {
303                 flags |= LDB_FLG_NOSYNC;
304         }
305
306         printf("Starting index test\n");
307
308         indexlist = ldb_dn_explode(NULL, "@INDEXLIST");
309
310         ldb_delete(*ldb, indexlist);
311
312         msg = ldb_msg_new(NULL);
313
314         msg->dn = indexlist;
315         ldb_msg_add_string(msg, "@IDXATTR", strdup("uid"));
316
317         if (ldb_add(*ldb, msg) != 0) {
318                 printf("Add of %s failed - %s\n", ldb_dn_linearize(*ldb, msg->dn), ldb_errstring(*ldb));
319                 exit(1);
320         }
321
322         basedn = ldb_dn_explode(NULL, options->basedn);
323
324         memset(msg, 0, sizeof(*msg));
325         msg->dn = ldb_dn_build_child(msg, "cn", "test", basedn);
326         ldb_msg_add_string(msg, "cn", strdup("test"));
327         ldb_msg_add_string(msg, "sn", strdup("test"));
328         ldb_msg_add_string(msg, "uid", strdup("test"));
329         ldb_msg_add_string(msg, "objectClass", strdup("OpenLDAPperson"));
330
331         if (ldb_add(*ldb, msg) != 0) {
332                 printf("Add of %s failed - %s\n", ldb_dn_linearize(*ldb, msg->dn), ldb_errstring(*ldb));
333                 exit(1);
334         }
335
336         if (talloc_free(*ldb) != 0) {
337                 printf("failed to free/close ldb database");
338                 exit(1);
339         }
340
341         (*ldb) = ldb_init(options);
342         
343         ret = ldb_connect(*ldb, options->url, flags, NULL);
344         if (ret != 0) {
345                 printf("failed to connect to %s\n", options->url);
346                 exit(1);
347         }
348
349         ret = ldb_search(*ldb, basedn, LDB_SCOPE_SUBTREE, "uid=test", NULL, &res);
350         if (ret != LDB_SUCCESS) { 
351                 printf("Search with (uid=test) filter failed!\n");
352                 exit(1);
353         }
354         if(res->count != 1) {
355                 printf("Should have found 1 record - found %d\n", res->count);
356                 exit(1);
357         }
358
359         if (ldb_delete(*ldb, msg->dn) != 0 ||
360             ldb_delete(*ldb, indexlist) != 0) {
361                 printf("cleanup failed - %s\n", ldb_errstring(*ldb));
362                 exit(1);
363         }
364
365         printf("Finished index test\n");
366 }
367
368
369 static void usage(void)
370 {
371         printf("Usage: ldbtest <options>\n");
372         printf("Options:\n");
373         printf("  -H ldb_url       choose the database (or $LDB_URL)\n");
374         printf("  --num-records  nrecords      database size to use\n");
375         printf("  --num-searches nsearches     number of searches to do\n");
376         printf("\n");
377         printf("tests ldb API\n\n");
378         exit(1);
379 }
380
381 int main(int argc, const char **argv)
382 {
383         TALLOC_CTX *mem_ctx = talloc_new(NULL);
384         struct ldb_context *ldb;
385
386         ldb_global_init();
387
388         ldb = ldb_init(mem_ctx);
389
390         options = ldb_cmdline_process(ldb, argc, argv, usage);
391
392         talloc_steal(mem_ctx, options);
393
394         if (options->basedn == NULL) {
395                 options->basedn = "ou=Ldb Test,ou=People,o=University of Michigan,c=TEST";
396         }
397
398         srandom(1);
399
400         printf("Testing with num-records=%d and num-searches=%d\n", 
401                options->num_records, options->num_searches);
402
403         start_test(ldb, options->num_records, options->num_searches);
404
405         start_test_index(&ldb);
406
407         talloc_free(mem_ctx);
408
409         return 0;
410 }