2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1999-2013 The OpenLDAP Foundation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted only as authorized by the OpenLDAP
11 * A copy of this license is available in file LICENSE in the
12 * top-level directory of the distribution or, alternatively, at
13 * <http://www.OpenLDAP.org/license.html>.
16 * This work was initially developed by Kurt Spanier for inclusion
17 * in OpenLDAP Software.
24 #include "ac/stdlib.h"
28 #include "ac/socket.h"
29 #include "ac/string.h"
30 #include "ac/unistd.h"
38 #include "slapd-common.h"
44 do_read( char *uri, char *manager, struct berval *passwd,
45 char *entry, LDAP **ld,
46 char **attrs, int noattrs, int nobind, int maxloop,
47 int maxretries, int delay, int force, int chaserefs );
50 do_random( char *uri, char *manager, struct berval *passwd,
51 char *sbase, char *filter, char **attrs, int noattrs, int nobind,
52 int innerloop, int maxretries, int delay, int force, int chaserefs );
59 "-H <uri> | ([-h <host>] -p <port>) "
80 /* -S: just send requests without reading responses
81 * -SS: send all requests asynchronous and immediately start reading responses
82 * -SSS: send all requests asynchronous; then read responses
87 main( int argc, char **argv )
91 char *host = "localhost";
94 struct berval passwd = { 0, NULL };
99 int retries = RETRIES;
103 char *srchattrs[] = { "1.1", NULL };
104 char **attrs = srchattrs;
108 tester_init( "slapd-read", TESTER_READ );
110 /* by default, tolerate referrals and no such object */
111 tester_ignore_str2errlist( "REFERRAL,NO_SUCH_OBJECT" );
113 while ( (i = getopt( argc, argv, "ACD:e:Ff:H:h:i:L:l:p:r:St:T:w:" )) != EOF ) {
123 case 'H': /* the server uri */
124 uri = strdup( optarg );
127 case 'h': /* the servers host */
128 host = strdup( optarg );
132 tester_ignore_str2errlist( optarg );
139 case 'p': /* the servers port */
140 if ( lutil_atoi( &port, optarg ) != 0 ) {
145 case 'D': /* the servers manager */
146 manager = strdup( optarg );
149 case 'w': /* the server managers password */
150 passwd.bv_val = strdup( optarg );
151 passwd.bv_len = strlen( optarg );
152 memset( optarg, '*', passwd.bv_len );
155 case 'e': /* DN to search for */
156 entry = strdup( optarg );
159 case 'f': /* the search request */
160 filter = strdup( optarg );
167 case 'l': /* the number of loops */
168 if ( lutil_atoi( &loops, optarg ) != 0 ) {
173 case 'L': /* the number of outerloops */
174 if ( lutil_atoi( &outerloops, optarg ) != 0 ) {
179 case 'r': /* the number of retries */
180 if ( lutil_atoi( &retries, optarg ) != 0 ) {
189 case 't': /* delay in seconds */
190 if ( lutil_atoi( &delay, optarg ) != 0 ) {
196 attrs = ldap_str2charray( optarg, "," );
197 if ( attrs == NULL ) {
208 if (( entry == NULL ) || ( port == -1 && uri == NULL ))
211 if ( *entry == '\0' ) {
212 fprintf( stderr, "%s: invalid EMPTY entry DN.\n",
214 exit( EXIT_FAILURE );
217 if ( argv[optind] != NULL ) {
218 attrs = &argv[optind];
221 uri = tester_uri( uri, host, port );
223 for ( i = 0; i < outerloops; i++ ) {
224 if ( filter != NULL ) {
225 do_random( uri, manager, &passwd, entry, filter, attrs,
226 noattrs, nobind, loops, retries, delay, force,
230 do_read( uri, manager, &passwd, entry, NULL, attrs,
231 noattrs, nobind, loops, retries, delay, force,
236 exit( EXIT_SUCCESS );
240 do_random( char *uri, char *manager, struct berval *passwd,
241 char *sbase, char *filter, char **srchattrs, int noattrs, int nobind,
242 int innerloop, int maxretries, int delay, int force, int chaserefs )
245 int i = 0, do_retry = maxretries;
247 int rc = LDAP_SUCCESS;
248 int version = LDAP_VERSION3;
250 char **values = NULL;
251 LDAPMessage *res = NULL, *e = NULL;
253 attrs[ 0 ] = LDAP_NO_ATTRS;
256 ldap_initialize( &ld, uri );
258 tester_perror( "ldap_initialize", NULL );
259 exit( EXIT_FAILURE );
262 (void) ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version );
263 (void) ldap_set_option( ld, LDAP_OPT_REFERRALS,
264 chaserefs ? LDAP_OPT_ON : LDAP_OPT_OFF );
266 if ( do_retry == maxretries ) {
267 fprintf( stderr, "PID=%ld - Read(%d): base=\"%s\", filter=\"%s\".\n",
268 (long) pid, innerloop, sbase, filter );
272 rc = ldap_sasl_bind_s( ld, manager, LDAP_SASL_SIMPLE, passwd, NULL, NULL, NULL );
273 if ( rc != LDAP_SUCCESS ) {
274 tester_ldap_error( ld, "ldap_sasl_bind_s", NULL );
277 case LDAP_UNAVAILABLE:
282 exit( EXIT_FAILURE );
286 rc = ldap_search_ext_s( ld, sbase, LDAP_SCOPE_SUBTREE,
287 filter, attrs, 0, NULL, NULL, NULL, LDAP_NO_LIMIT, &res );
289 case LDAP_SIZELIMIT_EXCEEDED:
290 case LDAP_TIMELIMIT_EXCEEDED:
292 nvalues = ldap_count_entries( ld, res );
293 if ( nvalues == 0 ) {
295 tester_ldap_error( ld, "ldap_search_ext_s", NULL );
300 values = malloc( ( nvalues + 1 ) * sizeof( char * ) );
301 for ( i = 0, e = ldap_first_entry( ld, res ); e != NULL; i++, e = ldap_next_entry( ld, e ) )
303 values[ i ] = ldap_get_dn( ld, e );
309 if ( do_retry == maxretries ) {
310 fprintf( stderr, " PID=%ld - Read base=\"%s\" filter=\"%s\" got %d values.\n",
311 (long) pid, sbase, filter, nvalues );
314 for ( i = 0; i < innerloop; i++ ) {
315 #if 0 /* use high-order bits for better randomness (Numerical Recipes in "C") */
316 int r = rand() % nvalues;
318 int r = ((double)nvalues)*rand()/(RAND_MAX + 1.0);
320 do_read( uri, manager, passwd, values[ r ], &ld,
321 srchattrs, noattrs, nobind, 1, maxretries,
322 delay, force, chaserefs );
328 tester_ldap_error( ld, "ldap_search_ext_s", NULL );
332 fprintf( stderr, " PID=%ld - Read done (%d).\n", (long) pid, rc );
335 ldap_unbind_ext( ld, NULL, NULL );
340 do_read( char *uri, char *manager, struct berval *passwd, char *entry,
341 LDAP **ldp, char **attrs, int noattrs, int nobind, int maxloop,
342 int maxretries, int delay, int force, int chaserefs )
344 LDAP *ld = ldp ? *ldp : NULL;
345 int i = 0, do_retry = maxretries;
346 int rc = LDAP_SUCCESS;
347 int version = LDAP_VERSION3;
348 int *msgids = NULL, active = 0;
350 /* make room for msgid */
352 msgids = (int *)calloc( sizeof(int), maxloop );
357 ldap_initialize( &ld, uri );
359 tester_perror( "ldap_initialize", NULL );
360 exit( EXIT_FAILURE );
363 (void) ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version );
364 (void) ldap_set_option( ld, LDAP_OPT_REFERRALS,
365 chaserefs ? LDAP_OPT_ON : LDAP_OPT_OFF );
367 if ( do_retry == maxretries ) {
368 fprintf( stderr, "PID=%ld - Read(%d): entry=\"%s\".\n",
369 (long) pid, maxloop, entry );
373 rc = ldap_sasl_bind_s( ld, manager, LDAP_SASL_SIMPLE, passwd, NULL, NULL, NULL );
374 if ( rc != LDAP_SUCCESS ) {
375 tester_ldap_error( ld, "ldap_sasl_bind_s", NULL );
378 case LDAP_UNAVAILABLE:
379 if ( do_retry > 0 ) {
380 ldap_unbind_ext( ld, NULL, NULL );
392 exit( EXIT_FAILURE );
399 LDAPMessage *res = NULL;
403 rc = ldap_search_ext( ld, entry, LDAP_SCOPE_BASE,
404 NULL, attrs, noattrs, NULL, NULL,
405 NULL, LDAP_NO_LIMIT, &msgids[i] );
410 ">>> PID=%ld - Read maxloop=%d cnt=%d active=%d msgid=%d: "
412 (long) pid, maxloop, i, active, msgids[i],
419 int first = tester_ignore_err( rc );
422 /* only log if first occurrence */
423 if ( ( force < 2 && first > 0 ) || abs(first) == 1 ) {
424 tester_ldap_error( ld, "ldap_search_ext", NULL );
429 /* busy needs special handling */
430 snprintf( buf, sizeof( buf ), "entry=\"%s\"\n", entry );
431 tester_ldap_error( ld, "ldap_search_ext", buf );
432 if ( rc == LDAP_BUSY && do_retry > 0 ) {
433 ldap_unbind_ext( ld, NULL, NULL );
446 rc = ldap_result( ld, LDAP_RES_ANY, 0, NULL, &res );
449 /* gone really bad */
452 ">>> PID=%ld - Read maxloop=%d cnt=%d active=%d: "
453 "entry=\"%s\" ldap_result()=%d\n",
454 (long) pid, maxloop, i, active, entry, rc );
459 /* timeout (impossible) */
462 case LDAP_RES_SEARCH_ENTRY:
463 case LDAP_RES_SEARCH_REFERENCE:
467 case LDAP_RES_SEARCH_RESULT:
468 /* just remove, no error checking (TODO?) */
469 msgid = ldap_msgid( res );
470 ldap_parse_result( ld, res, &rc, NULL, NULL, NULL, NULL, 1 );
473 /* linear search, bah */
474 for ( j = 0; j < i; j++ ) {
475 if ( msgids[ j ] == msgid ) {
480 "<<< PID=%ld - ReadDone maxloop=%d cnt=%d active=%d msgid=%d: "
482 (long) pid, maxloop, j, active, msgid, entry );
490 /* other messages unexpected */
492 "### PID=%ld - Read(%d): "
493 "entry=\"%s\" attrs=%s%s. unexpected response tag=%d\n",
495 entry, attrs[0], attrs[1] ? " (more...)" : "", rc );
502 } while ( i < maxloop || active > 0 );
505 for ( ; i < maxloop; i++ ) {
506 LDAPMessage *res = NULL;
510 rc = ldap_search_ext( ld, entry, LDAP_SCOPE_BASE,
511 NULL, attrs, noattrs, NULL, NULL,
512 NULL, LDAP_NO_LIMIT, &msgid );
513 if ( rc == LDAP_SUCCESS ) continue;
517 rc = ldap_search_ext_s( ld, entry, LDAP_SCOPE_BASE,
518 NULL, attrs, noattrs, NULL, NULL, NULL,
519 LDAP_NO_LIMIT, &res );
525 int first = tester_ignore_err( rc );
528 snprintf( buf, sizeof( buf ), "ldap_search_ext_s(%s)", entry );
532 /* only log if first occurrence */
533 if ( ( force < 2 && first > 0 ) || abs(first) == 1 ) {
534 tester_ldap_error( ld, buf, NULL );
539 /* busy needs special handling */
540 tester_ldap_error( ld, buf, NULL );
541 if ( rc == LDAP_BUSY && do_retry > 0 ) {
542 ldap_unbind_ext( ld, NULL, NULL );
553 if ( msgids != NULL ) {
561 fprintf( stderr, " PID=%ld - Read done (%d).\n", (long) pid, rc );
564 ldap_unbind_ext( ld, NULL, NULL );