r5298: - got rid of pstring.h from includes.h. This at least makes it a bit
[samba.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 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/ldb.h"
37 #include "ldb/include/ldb_private.h"
38
39 #ifdef _SAMBA_BUILD_
40 #include "system/filesys.h"
41 #endif
42
43 static const char *ldb_url;
44 static const char *base_dn = "ou=Ldb Test,ou=People,o=University of Michigan,c=US";
45
46 static struct timeval tp1,tp2;
47
48 static void _start_timer(void)
49 {
50         gettimeofday(&tp1,NULL);
51 }
52
53 static double _end_timer(void)
54 {
55         gettimeofday(&tp2,NULL);
56         return((tp2.tv_sec - tp1.tv_sec) + 
57                (tp2.tv_usec - tp1.tv_usec)*1.0e-6);
58 }
59
60 static void add_records(struct ldb_context *ldb,
61                         const char *basedn,
62                         int count)
63 {
64         struct ldb_message msg;
65         int i;
66
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                 int j;
72
73                 asprintf(&name, "Test%d", i);
74
75                 asprintf(&msg.dn, "cn=%s,%s", name, basedn);
76                 msg.num_elements = 6;
77                 msg.elements = el;
78
79                 el[0].flags = 0;
80                 el[0].name = strdup("cn");
81                 el[0].num_values = 1;
82                 el[0].values = vals[0];
83                 vals[0][0].data = name;
84                 vals[0][0].length = strlen(name);
85
86                 el[1].flags = 0;
87                 el[1].name = strdup("title");
88                 el[1].num_values = 1;
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);
92
93                 el[2].flags = 0;
94                 el[2].name = strdup("uid");
95                 el[2].num_values = 1;
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);
99
100                 el[3].flags = 0;
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);
106
107                 el[4].flags = 0;
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);
113
114                 el[5].flags = 0;
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);
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                 for (j=0;j<msg.num_elements;j++) {
132                         free(el[j].name);
133                 }
134                 free(name);
135                 free(msg.dn);
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);
140         }
141
142         printf("\n");
143 }
144
145 static void modify_records(struct ldb_context *ldb,
146                            const char *basedn,
147                            int count)
148 {
149         struct ldb_message msg;
150         int i;
151
152         for (i=0;i<count;i++) {
153                 struct ldb_message_element el[3];
154                 struct ldb_val vals[3];
155                 char *name;
156                 int j;
157                 
158                 asprintf(&name, "Test%d", i);
159                 asprintf(&msg.dn, "cn=%s,%s", name, basedn);
160
161                 msg.num_elements = 3;
162                 msg.elements = el;
163
164                 el[0].flags = LDB_FLAG_MOD_DELETE;
165                 el[0].name = strdup("mail");
166                 el[0].num_values = 0;
167
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);
174
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);
181
182                 if (ldb_modify(ldb, &msg) != 0) {
183                         printf("Modify of %s failed - %s\n", name, ldb_errstring(ldb));
184                         exit(1);
185                 }
186
187                 printf("Modifying uid %s\r", name);
188                 fflush(stdout);
189
190                 for (j=0;j<msg.num_elements;j++) {
191                         free(el[j].name);
192                 }
193                 free(name);
194                 free(msg.dn);
195                 free(vals[1].data);
196                 free(vals[2].data);
197         }
198
199         printf("\n");
200 }
201
202
203 static void delete_records(struct ldb_context *ldb,
204                            const char *basedn,
205                            int count)
206 {
207         int i;
208
209         for (i=0;i<count;i++) {
210                 char *dn;
211                 asprintf(&dn, "cn=Test%d,%s", i, basedn);
212
213                 printf("Deleting uid Test%d\r", i);
214                 fflush(stdout);
215
216                 if (ldb_delete(ldb, dn) != 0) {
217                         printf("Delete of %s failed - %s\n", dn, ldb_errstring(ldb));
218                         exit(1);
219                 }
220                 free(dn);
221         }
222
223         printf("\n");
224 }
225
226 static void search_uid(struct ldb_context *ldb, int nrecords, int nsearches)
227 {
228         int i;
229
230         for (i=0;i<nsearches;i++) {
231                 int uid = (i * 700 + 17) % (nrecords * 2);
232                 char *expr;
233                 struct ldb_message **res;
234                 int ret;
235
236                 asprintf(&expr, "(uid=TEST%d)", uid);
237                 ret = ldb_search(ldb, base_dn, LDB_SCOPE_SUBTREE, expr, NULL, &res);
238
239                 if (uid < nrecords && ret != 1) {
240                         printf("Failed to find %s - %s\n", expr, ldb_errstring(ldb));
241                         exit(1);
242                 }
243
244                 if (uid >= nrecords && ret > 0) {
245                         printf("Found %s !? - %d\n", expr, ret);
246                         exit(1);
247                 }
248
249                 if (ret > 0) {
250                         ldb_search_free(ldb, res);
251                 }
252
253                 printf("testing uid %d/%d - %d  \r", i, uid, ret);
254                 fflush(stdout);
255
256                 free(expr);
257         }
258
259         printf("\n");
260 }
261
262 static void start_test(struct ldb_context *ldb, int nrecords, int nsearches)
263 {
264         printf("Adding %d records\n", nrecords);
265         add_records(ldb, base_dn, nrecords);
266
267         printf("Starting search on uid\n");
268         _start_timer();
269         search_uid(ldb, nrecords, nsearches);
270         printf("uid search took %.2f seconds\n", _end_timer());
271
272         printf("Modifying records\n");
273         modify_records(ldb, base_dn, nrecords);
274
275         printf("Deleting records\n");
276         delete_records(ldb, base_dn, 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_message **res;
297         int ret;
298
299         printf("Starting index test\n");
300
301         ldb_delete(*ldb, "@INDEXLIST");
302
303         msg = ldb_msg_new(NULL);
304
305         msg->dn = strdup("@INDEXLIST");
306         ldb_msg_add_string(*ldb, msg, "@IDXATTR", strdup("uid"));
307
308         if (ldb_add(*ldb, msg) != 0) {
309                 printf("Add of %s failed - %s\n", msg->dn, ldb_errstring(*ldb));
310                 exit(1);
311         }
312
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"));
319
320         if (ldb_add(*ldb, msg) != 0) {
321                 printf("Add of %s failed - %s\n", msg->dn, ldb_errstring(*ldb));
322                 exit(1);
323         }
324
325         if (ldb_close(*ldb) != 0) {
326                 printf("ldb_close failed - %s\n", ldb_errstring(*ldb));
327                 exit(1);
328         }
329
330         *ldb = ldb_connect(ldb_url, 0, NULL);
331
332         if (!*ldb) {
333                 perror("ldb_connect");
334                 exit(1);
335         }
336
337         ret = ldb_search(*ldb, base_dn, LDB_SCOPE_SUBTREE, "uid=test", NULL, &res);
338         if (ret != 1) {
339                 printf("Should have found 1 record - found %d\n", ret);
340                 exit(1);
341         }
342
343         if (ldb_delete(*ldb, msg->dn) != 0 ||
344             ldb_delete(*ldb, "@INDEXLIST") != 0) {
345                 printf("cleanup failed - %s\n", ldb_errstring(*ldb));
346                 exit(1);
347         }
348
349         printf("Finished index test\n");
350 }
351
352
353 static void usage(void)
354 {
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");
360         printf("\n");
361         printf("tests ldb API\n\n");
362         exit(1);
363 }
364
365  int main(int argc, char * const argv[])
366 {
367         struct ldb_context *ldb;
368         const char **options = NULL;
369         int ldbopts;
370         int opt;
371         int nrecords = 5000;
372         int nsearches = 2000;
373
374         ldb_url = getenv("LDB_URL");
375
376         ldbopts = 0;
377         while ((opt = getopt(argc, argv, "hH:r:s:b:o:")) != EOF) {
378                 switch (opt) {
379                 case 'H':
380                         ldb_url = optarg;
381                         break;
382
383                 case 'r':
384                         nrecords = atoi(optarg);
385                         break;
386
387                 case 'b':
388                         base_dn = optarg;
389                         break;
390
391                 case 's':
392                         nsearches = atoi(optarg);
393                         break;
394
395                 case 'o':
396                         options = ldb_options_parse(options, &ldbopts, optarg);
397                         break;
398
399                 case 'h':
400                 default:
401                         usage();
402                         break;
403                 }
404         }
405
406         if (!ldb_url) {
407                 fprintf(stderr, "You must specify a ldb URL\n\n");
408                 usage();
409         }
410
411         argc -= optind;
412         argv += optind;
413
414         ldb = ldb_connect(ldb_url, 0, options);
415
416         if (!ldb) {
417                 perror("ldb_connect");
418                 exit(1);
419         }
420
421         ldb_set_debug_stderr(ldb);
422
423         srandom(1);
424
425         start_test(ldb, nrecords, nsearches);
426
427         start_test_index(&ldb);
428
429         ldb_close(ldb);
430
431         return 0;
432 }