source: branches/samba-3.5.x/source3/lib/ldb/tools/ldbtest.c

Last change on this file was 414, checked in by Herwig Bauernfeind, 15 years ago

Samba 3.5.0: Initial import

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