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