1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
|
/*
ldb database library
Copyright (C) Andrew Tridgell 2004
** NOTE! The following LGPL license applies to the ldb
** library. This does NOT imply that all of Samba is released
** under the LGPL
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/*
* Name: ldb
*
* Component: ldbtest
*
* Description: utility to test ldb
*
* Author: Andrew Tridgell
*/
#include "includes.h"
#include "ldb/include/ldb.h"
#include "ldb/include/ldb_private.h"
#ifdef _SAMBA_BUILD_
#include "system/filesys.h"
#include "system/time.h"
#endif
static const char *ldb_url;
static const char *base_dn = "ou=Ldb Test,ou=People,o=University of Michigan,c=US";
static struct timeval tp1,tp2;
static void _start_timer(void)
{
gettimeofday(&tp1,NULL);
}
static double _end_timer(void)
{
gettimeofday(&tp2,NULL);
return((tp2.tv_sec - tp1.tv_sec) +
(tp2.tv_usec - tp1.tv_usec)*1.0e-6);
}
static void add_records(struct ldb_context *ldb,
const char *basedn,
int count)
{
struct ldb_message msg;
int i;
for (i=0;i<count;i++) {
struct ldb_message_element el[6];
struct ldb_val vals[6][1];
char *name;
int j;
asprintf(&name, "Test%d", i);
asprintf(&msg.dn, "cn=%s,%s", name, basedn);
msg.num_elements = 6;
msg.elements = el;
el[0].flags = 0;
el[0].name = strdup("cn");
el[0].num_values = 1;
el[0].values = vals[0];
vals[0][0].data = name;
vals[0][0].length = strlen(name);
el[1].flags = 0;
el[1].name = strdup("title");
el[1].num_values = 1;
el[1].values = vals[1];
asprintf((char **)&vals[1][0].data, "The title of %s", name);
vals[1][0].length = strlen(vals[1][0].data);
el[2].flags = 0;
el[2].name = strdup("uid");
el[2].num_values = 1;
el[2].values = vals[2];
vals[2][0].data = ldb_casefold(ldb, name);
vals[2][0].length = strlen(vals[2][0].data);
el[3].flags = 0;
el[3].name = strdup("mail");
el[3].num_values = 1;
el[3].values = vals[3];
asprintf((char **)&vals[3][0].data, "%s@example.com", name);
vals[3][0].length = strlen(vals[3][0].data);
el[4].flags = 0;
el[4].name = strdup("objectClass");
el[4].num_values = 1;
el[4].values = vals[4];
vals[4][0].data = strdup("OpenLDAPperson");
vals[4][0].length = strlen(vals[4][0].data);
el[5].flags = 0;
el[5].name = strdup("sn");
el[5].num_values = 1;
el[5].values = vals[5];
vals[5][0].data = name;
vals[5][0].length = strlen(vals[5][0].data);
ldb_delete(ldb, msg.dn);
if (ldb_add(ldb, &msg) != 0) {
printf("Add of %s failed - %s\n", name, ldb_errstring(ldb));
exit(1);
}
printf("adding uid %s\r", name);
fflush(stdout);
for (j=0;j<msg.num_elements;j++) {
free(el[j].name);
}
free(name);
free(msg.dn);
free(vals[1][0].data);
talloc_free(vals[2][0].data);
free(vals[3][0].data);
free(vals[4][0].data);
}
printf("\n");
}
static void modify_records(struct ldb_context *ldb,
const char *basedn,
int count)
{
struct ldb_message msg;
int i;
for (i=0;i<count;i++) {
struct ldb_message_element el[3];
struct ldb_val vals[3];
char *name;
int j;
asprintf(&name, "Test%d", i);
asprintf(&msg.dn, "cn=%s,%s", name, basedn);
msg.num_elements = 3;
msg.elements = el;
el[0].flags = LDB_FLAG_MOD_DELETE;
el[0].name = strdup("mail");
el[0].num_values = 0;
el[1].flags = LDB_FLAG_MOD_ADD;
el[1].name = strdup("mail");
el[1].num_values = 1;
el[1].values = &vals[1];
asprintf((char **)&vals[1].data, "%s@other.example.com", name);
vals[1].length = strlen(vals[1].data);
el[2].flags = LDB_FLAG_MOD_REPLACE;
el[2].name = strdup("mail");
el[2].num_values = 1;
el[2].values = &vals[2];
asprintf((char **)&vals[2].data, "%s@other2.example.com", name);
vals[2].length = strlen(vals[2].data);
if (ldb_modify(ldb, &msg) != 0) {
printf("Modify of %s failed - %s\n", name, ldb_errstring(ldb));
exit(1);
}
printf("Modifying uid %s\r", name);
fflush(stdout);
for (j=0;j<msg.num_elements;j++) {
free(el[j].name);
}
free(name);
free(msg.dn);
free(vals[1].data);
free(vals[2].data);
}
printf("\n");
}
static void delete_records(struct ldb_context *ldb,
const char *basedn,
int count)
{
int i;
for (i=0;i<count;i++) {
char *dn;
asprintf(&dn, "cn=Test%d,%s", i, basedn);
printf("Deleting uid Test%d\r", i);
fflush(stdout);
if (ldb_delete(ldb, dn) != 0) {
printf("Delete of %s failed - %s\n", dn, ldb_errstring(ldb));
exit(1);
}
free(dn);
}
printf("\n");
}
static void search_uid(struct ldb_context *ldb, int nrecords, int nsearches)
{
int i;
for (i=0;i<nsearches;i++) {
int uid = (i * 700 + 17) % (nrecords * 2);
char *expr;
struct ldb_message **res;
int ret;
asprintf(&expr, "(uid=TEST%d)", uid);
ret = ldb_search(ldb, base_dn, LDB_SCOPE_SUBTREE, expr, NULL, &res);
if (uid < nrecords && ret != 1) {
printf("Failed to find %s - %s\n", expr, ldb_errstring(ldb));
exit(1);
}
if (uid >= nrecords && ret > 0) {
printf("Found %s !? - %d\n", expr, ret);
exit(1);
}
if (ret > 0) {
ldb_search_free(ldb, res);
}
printf("testing uid %d/%d - %d \r", i, uid, ret);
fflush(stdout);
free(expr);
}
printf("\n");
}
static void start_test(struct ldb_context *ldb, int nrecords, int nsearches)
{
printf("Adding %d records\n", nrecords);
add_records(ldb, base_dn, nrecords);
printf("Starting search on uid\n");
_start_timer();
search_uid(ldb, nrecords, nsearches);
printf("uid search took %.2f seconds\n", _end_timer());
printf("Modifying records\n");
modify_records(ldb, base_dn, nrecords);
printf("Deleting records\n");
delete_records(ldb, base_dn, nrecords);
}
/*
2) Store an @indexlist record
3) Store a record that contains fields that should be index according
to @index
4) disconnection from database
5) connect to same database
6) search for record added in step 3 using a search key that should
be indexed
*/
static void start_test_index(struct ldb_context **ldb)
{
struct ldb_message *msg;
struct ldb_message **res;
int ret;
printf("Starting index test\n");
ldb_delete(*ldb, "@INDEXLIST");
msg = ldb_msg_new(NULL);
msg->dn = strdup("@INDEXLIST");
ldb_msg_add_string(*ldb, msg, "@IDXATTR", strdup("uid"));
if (ldb_add(*ldb, msg) != 0) {
printf("Add of %s failed - %s\n", msg->dn, ldb_errstring(*ldb));
exit(1);
}
memset(msg, 0, sizeof(*msg));
asprintf(&msg->dn, "cn=%s,%s", "test", base_dn);
ldb_msg_add_string(*ldb, msg, "cn", strdup("test"));
ldb_msg_add_string(*ldb, msg, "sn", strdup("test"));
ldb_msg_add_string(*ldb, msg, "uid", strdup("test"));
ldb_msg_add_string(*ldb, msg, "objectClass", strdup("OpenLDAPperson"));
if (ldb_add(*ldb, msg) != 0) {
printf("Add of %s failed - %s\n", msg->dn, ldb_errstring(*ldb));
exit(1);
}
if (talloc_free(*ldb) != 0) {
printf("failed to free/close ldb database");
exit(1);
}
*ldb = ldb_connect(ldb_url, 0, NULL);
if (!*ldb) {
perror("ldb_connect");
exit(1);
}
ret = ldb_search(*ldb, base_dn, LDB_SCOPE_SUBTREE, "uid=test", NULL, &res);
if (ret != 1) {
printf("Should have found 1 record - found %d\n", ret);
exit(1);
}
if (ldb_delete(*ldb, msg->dn) != 0 ||
ldb_delete(*ldb, "@INDEXLIST") != 0) {
printf("cleanup failed - %s\n", ldb_errstring(*ldb));
exit(1);
}
printf("Finished index test\n");
}
static void usage(void)
{
printf("Usage: ldbtest <options>\n");
printf("Options:\n");
printf(" -H ldb_url choose the database (or $LDB_URL)\n");
printf(" -r nrecords database size to use\n");
printf(" -s nsearches number of searches to do\n");
printf("\n");
printf("tests ldb API\n\n");
exit(1);
}
int main(int argc, char * const argv[])
{
struct ldb_context *ldb;
const char **options = NULL;
int ldbopts;
int opt;
int nrecords = 5000;
int nsearches = 2000;
ldb_url = getenv("LDB_URL");
ldbopts = 0;
while ((opt = getopt(argc, argv, "hH:r:s:b:o:")) != EOF) {
switch (opt) {
case 'H':
ldb_url = optarg;
break;
case 'r':
nrecords = atoi(optarg);
break;
case 'b':
base_dn = optarg;
break;
case 's':
nsearches = atoi(optarg);
break;
case 'o':
options = ldb_options_parse(options, &ldbopts, optarg);
break;
case 'h':
default:
usage();
break;
}
}
if (!ldb_url) {
fprintf(stderr, "You must specify a ldb URL\n\n");
usage();
}
argc -= optind;
argv += optind;
ldb = ldb_connect(ldb_url, 0, options);
if (!ldb) {
perror("ldb_connect");
exit(1);
}
ldb_set_debug_stderr(ldb);
srandom(1);
start_test(ldb, nrecords, nsearches);
start_test_index(&ldb);
talloc_free(ldb);
return 0;
}
|