s4/ldb: fix standalone build
[metze/samba/wip.git] / source4 / 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, see <http://www.gnu.org/licenses/>.
22 */
23
24 /*
25  *  Name: ldb
26  *
27  *  Component: ldbtest
28  *
29  *  Description: utility to test ldb
30  *
31  *  Author: Andrew Tridgell
32  */
33
34 #ifdef _SAMBA_BUILD_
35 #include "includes.h"
36 #else
37 #include "ldb_includes.h"
38 #endif
39
40 #include "ldb.h"
41 #include "tools/cmdline.h"
42
43 static struct timespec tp1,tp2;
44 static struct ldb_cmdline *options;
45
46 static void _start_timer(void)
47 {
48         if (clock_gettime(CUSTOM_CLOCK_MONOTONIC, &tp1) != 0) {
49                 clock_gettime(CLOCK_REALTIME, &tp1);
50         }
51 }
52
53 static double _end_timer(void)
54 {
55         if (clock_gettime(CUSTOM_CLOCK_MONOTONIC, &tp2) != 0) {
56                 clock_gettime(CLOCK_REALTIME, &tp2);
57         }
58         return((tp2.tv_sec - tp1.tv_sec) + 
59                (tp2.tv_nsec - tp1.tv_nsec)*1.0e-9);
60 }
61
62 static void add_records(struct ldb_context *ldb,
63                         struct ldb_dn *basedn,
64                         unsigned int count)
65 {
66         struct ldb_message msg;
67         unsigned int i;
68
69 #if 0
70         if (ldb_lock(ldb, "transaction") != 0) {
71                 printf("transaction lock failed\n");
72                 exit(1);
73         }
74 #endif
75         for (i=0;i<count;i++) {
76                 struct ldb_message_element el[6];
77                 struct ldb_val vals[6][1];
78                 char *name;
79                 TALLOC_CTX *tmp_ctx = talloc_new(ldb);
80
81                 name = talloc_asprintf(tmp_ctx, "Test%d", i);
82
83                 msg.dn = ldb_dn_copy(tmp_ctx, basedn);
84                 ldb_dn_add_child_fmt(msg.dn, "cn=%s", name);
85                 msg.num_elements = 6;
86                 msg.elements = el;
87
88                 el[0].flags = 0;
89                 el[0].name = talloc_strdup(tmp_ctx, "cn");
90                 el[0].num_values = 1;
91                 el[0].values = vals[0];
92                 vals[0][0].data = (uint8_t *)name;
93                 vals[0][0].length = strlen(name);
94
95                 el[1].flags = 0;
96                 el[1].name = "title";
97                 el[1].num_values = 1;
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);
101
102                 el[2].flags = 0;
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);
108
109                 el[3].flags = 0;
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);
115
116                 el[4].flags = 0;
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);
122
123                 el[5].flags = 0;
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);
129
130                 ldb_delete(ldb, msg.dn);
131
132                 if (ldb_add(ldb, &msg) != 0) {
133                         printf("Add of %s failed - %s\n", name, ldb_errstring(ldb));
134                         exit(1);
135                 }
136
137                 printf("adding uid %s\r", name);
138                 fflush(stdout);
139
140                 talloc_free(tmp_ctx);
141         }
142 #if 0
143         if (ldb_unlock(ldb, "transaction") != 0) {
144                 printf("transaction unlock failed\n");
145                 exit(1);
146         }
147 #endif
148         printf("\n");
149 }
150
151 static void modify_records(struct ldb_context *ldb,
152                            struct ldb_dn *basedn,
153                            unsigned int count)
154 {
155         struct ldb_message msg;
156         unsigned int i;
157
158         for (i=0;i<count;i++) {
159                 struct ldb_message_element el[3];
160                 struct ldb_val vals[3];
161                 char *name;
162                 TALLOC_CTX *tmp_ctx = talloc_new(ldb);
163                 
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);
167
168                 msg.num_elements = 3;
169                 msg.elements = el;
170
171                 el[0].flags = LDB_FLAG_MOD_DELETE;
172                 el[0].name = talloc_strdup(tmp_ctx, "mail");
173                 el[0].num_values = 0;
174
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);
181
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);
188
189                 if (ldb_modify(ldb, &msg) != 0) {
190                         printf("Modify of %s failed - %s\n", name, ldb_errstring(ldb));
191                         exit(1);
192                 }
193
194                 printf("Modifying uid %s\r", name);
195                 fflush(stdout);
196
197                 talloc_free(tmp_ctx);
198         }
199
200         printf("\n");
201 }
202
203
204 static void delete_records(struct ldb_context *ldb,
205                            struct ldb_dn *basedn,
206                            unsigned int count)
207 {
208         unsigned int i;
209
210         for (i=0;i<count;i++) {
211                 struct ldb_dn *dn;
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);
215
216                 printf("Deleting uid Test%d\r", i);
217                 fflush(stdout);
218
219                 if (ldb_delete(ldb, dn) != 0) {
220                         printf("Delete of %s failed - %s\n", ldb_dn_get_linearized(dn), ldb_errstring(ldb));
221                         exit(1);
222                 }
223                 talloc_free(name);
224         }
225
226         printf("\n");
227 }
228
229 static void search_uid(struct ldb_context *ldb, struct ldb_dn *basedn,
230                        unsigned int nrecords, unsigned int nsearches)
231 {
232         unsigned int i;
233
234         for (i=0;i<nsearches;i++) {
235                 int uid = (i * 700 + 17) % (nrecords * 2);
236                 char *expr;
237                 struct ldb_result *res = NULL;
238                 int ret;
239
240                 expr = talloc_asprintf(ldb, "(uid=TEST%d)", uid);
241                 ret = ldb_search(ldb, ldb, &res, basedn, LDB_SCOPE_SUBTREE, NULL, "%s", expr);
242
243                 if (ret != LDB_SUCCESS || (uid < nrecords && res->count != 1)) {
244                         printf("Failed to find %s - %s\n", expr, ldb_errstring(ldb));
245                         exit(1);
246                 }
247
248                 if (uid >= nrecords && res->count > 0) {
249                         printf("Found %s !? - %d\n", expr, ret);
250                         exit(1);
251                 }
252
253                 printf("Testing uid %d/%d - %d  \r", i, uid, res->count);
254                 fflush(stdout);
255
256                 talloc_free(res);
257                 talloc_free(expr);
258         }
259
260         printf("\n");
261 }
262
263 static void start_test(struct ldb_context *ldb, unsigned int nrecords,
264                        unsigned int nsearches)
265 {
266         struct ldb_dn *basedn;
267
268         basedn = ldb_dn_new(ldb, ldb, options->basedn);
269         if ( ! ldb_dn_validate(basedn)) {
270                 printf("Invalid base DN\n");
271                 exit(1);
272         }
273
274         printf("Adding %d records\n", nrecords);
275         add_records(ldb, basedn, nrecords);
276
277         printf("Starting search on uid\n");
278         _start_timer();
279         search_uid(ldb, basedn, nrecords, nsearches);
280         printf("uid search took %.2f seconds\n", _end_timer());
281
282         printf("Modifying records\n");
283         modify_records(ldb, basedn, nrecords);
284
285         printf("Deleting records\n");
286         delete_records(ldb, basedn, nrecords);
287 }
288
289
290 /*
291       2) Store an @indexlist record
292
293       3) Store a record that contains fields that should be index according
294 to @index
295
296       4) disconnection from database
297
298       5) connect to same database
299
300       6) search for record added in step 3 using a search key that should
301 be indexed
302 */
303 static void start_test_index(struct ldb_context **ldb)
304 {
305         struct ldb_message *msg;
306         struct ldb_result *res = NULL;
307         struct ldb_dn *indexlist;
308         struct ldb_dn *basedn;
309         int ret;
310         int flags = 0;
311         const char *specials;
312
313         specials = getenv("LDB_SPECIALS");
314         if (specials && atoi(specials) == 0) {
315                 printf("LDB_SPECIALS disabled - skipping index test\n");
316                 return;
317         }
318
319         if (options->nosync) {
320                 flags |= LDB_FLG_NOSYNC;
321         }
322
323         printf("Starting index test\n");
324
325         indexlist = ldb_dn_new(*ldb, *ldb, "@INDEXLIST");
326
327         ldb_delete(*ldb, indexlist);
328
329         msg = ldb_msg_new(NULL);
330
331         msg->dn = indexlist;
332         ldb_msg_add_string(msg, "@IDXATTR", strdup("uid"));
333
334         if (ldb_add(*ldb, msg) != 0) {
335                 printf("Add of %s failed - %s\n", ldb_dn_get_linearized(msg->dn), ldb_errstring(*ldb));
336                 exit(1);
337         }
338
339         basedn = ldb_dn_new(*ldb, *ldb, options->basedn);
340
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"));
348
349         if (ldb_add(*ldb, msg) != 0) {
350                 printf("Add of %s failed - %s\n", ldb_dn_get_linearized(msg->dn), ldb_errstring(*ldb));
351                 exit(1);
352         }
353
354         if (talloc_free(*ldb) != 0) {
355                 printf("failed to free/close ldb database");
356                 exit(1);
357         }
358
359         (*ldb) = ldb_init(options, NULL);
360
361         ret = ldb_connect(*ldb, options->url, flags, NULL);
362         if (ret != 0) {
363                 printf("failed to connect to %s\n", options->url);
364                 exit(1);
365         }
366
367         basedn = ldb_dn_new(*ldb, *ldb, options->basedn);
368         msg->dn = basedn;
369         ldb_dn_add_child_fmt(msg->dn, "cn=test");
370
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");
374                 exit(1);
375         }
376         if(res->count != 1) {
377                 printf("Should have found 1 record - found %d\n", res->count);
378                 exit(1);
379         }
380
381         indexlist = ldb_dn_new(*ldb, *ldb, "@INDEXLIST");
382
383         if (ldb_delete(*ldb, msg->dn) != 0 ||
384             ldb_delete(*ldb, indexlist) != 0) {
385                 printf("cleanup failed - %s\n", ldb_errstring(*ldb));
386                 exit(1);
387         }
388
389         printf("Finished index test\n");
390 }
391
392
393 static void usage(void)
394 {
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");
400         printf("\n");
401         printf("tests ldb API\n\n");
402         exit(1);
403 }
404
405 int main(int argc, const char **argv)
406 {
407         TALLOC_CTX *mem_ctx = talloc_new(NULL);
408         struct ldb_context *ldb;
409
410         ldb = ldb_init(mem_ctx, NULL);
411
412         options = ldb_cmdline_process(ldb, argc, argv, usage);
413
414         talloc_steal(mem_ctx, options);
415
416         if (options->basedn == NULL) {
417                 options->basedn = "ou=Ldb Test,ou=People,o=University of Michigan,c=TEST";
418         }
419
420         srandom(1);
421
422         printf("Testing with num-records=%d and num-searches=%d\n", 
423                options->num_records, options->num_searches);
424
425         start_test(ldb,
426                    (unsigned int) options->num_records,
427                    (unsigned int) options->num_searches);
428
429         start_test_index(&ldb);
430
431         talloc_free(mem_ctx);
432
433         return 0;
434 }