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