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