2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1999-2007 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>
36 #include "slapd-common.h"
42 do_read( char *uri, char *manager, struct berval *passwd,
43 char *entry, LDAP **ld, int noattrs, int nobind, int maxloop,
44 int maxretries, int delay, int force, int chaserefs );
47 do_random( char *uri, char *manager, struct berval *passwd,
48 char *sbase, char *filter, int noattrs, int nobind,
49 int innerloop, int maxretries, int delay, int force, int chaserefs );
56 "-H <uri> | ([-h <host>] -p <port>) "
75 main( int argc, char **argv )
79 char *host = "localhost";
82 struct berval passwd = { 0, NULL };
87 int retries = RETRIES;
94 tester_init( "slapd-read", TESTER_READ );
96 /* by default, tolerate referrals and no such object */
97 tester_ignore_str2errlist( "REFERRAL,NO_SUCH_OBJECT" );
99 while ( (i = getopt( argc, argv, "ACD:e:Ff:H:h:i:L:l:p:r:t:w:" )) != EOF ) {
109 case 'H': /* the server uri */
110 uri = strdup( optarg );
113 case 'h': /* the servers host */
114 host = strdup( optarg );
118 tester_ignore_str2errlist( optarg );
125 case 'p': /* the servers port */
126 if ( lutil_atoi( &port, optarg ) != 0 ) {
131 case 'D': /* the servers manager */
132 manager = strdup( optarg );
135 case 'w': /* the server managers password */
136 passwd.bv_val = strdup( optarg );
137 passwd.bv_len = strlen( optarg );
138 memset( optarg, '*', passwd.bv_len );
141 case 'e': /* DN to search for */
142 entry = strdup( optarg );
145 case 'f': /* the search request */
146 filter = strdup( optarg );
153 case 'l': /* the number of loops */
154 if ( lutil_atoi( &loops, optarg ) != 0 ) {
159 case 'L': /* the number of outerloops */
160 if ( lutil_atoi( &outerloops, optarg ) != 0 ) {
165 case 'r': /* the number of retries */
166 if ( lutil_atoi( &retries, optarg ) != 0 ) {
171 case 't': /* delay in seconds */
172 if ( lutil_atoi( &delay, optarg ) != 0 ) {
183 if (( entry == NULL ) || ( port == -1 && uri == NULL ))
186 if ( *entry == '\0' ) {
187 fprintf( stderr, "%s: invalid EMPTY entry DN.\n",
189 exit( EXIT_FAILURE );
192 uri = tester_uri( uri, host, port );
194 for ( i = 0; i < outerloops; i++ ) {
195 if ( filter != NULL ) {
196 do_random( uri, manager, &passwd, entry, filter,
197 noattrs, nobind, loops, retries, delay, force,
201 do_read( uri, manager, &passwd, entry, NULL, noattrs, nobind,
202 loops, retries, delay, force, chaserefs );
206 exit( EXIT_SUCCESS );
210 do_random( char *uri, char *manager, struct berval *passwd,
211 char *sbase, char *filter, int noattrs, int nobind,
212 int innerloop, int maxretries, int delay, int force, int chaserefs )
215 int i = 0, do_retry = maxretries;
217 int rc = LDAP_SUCCESS;
218 int version = LDAP_VERSION3;
220 char **values = NULL;
221 LDAPMessage *res = NULL, *e = NULL;
223 attrs[ 0 ] = LDAP_NO_ATTRS;
226 ldap_initialize( &ld, uri );
228 tester_perror( "ldap_initialize", NULL );
229 exit( EXIT_FAILURE );
232 (void) ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version );
233 (void) ldap_set_option( ld, LDAP_OPT_REFERRALS,
234 chaserefs ? LDAP_OPT_ON : LDAP_OPT_OFF );
236 if ( do_retry == maxretries ) {
237 fprintf( stderr, "PID=%ld - Read(%d): base=\"%s\", filter=\"%s\".\n",
238 (long) pid, innerloop, sbase, filter );
242 rc = ldap_sasl_bind_s( ld, manager, LDAP_SASL_SIMPLE, passwd, NULL, NULL, NULL );
243 if ( rc != LDAP_SUCCESS ) {
244 tester_ldap_error( ld, "ldap_sasl_bind_s", NULL );
247 case LDAP_UNAVAILABLE:
252 exit( EXIT_FAILURE );
256 rc = ldap_search_ext_s( ld, sbase, LDAP_SCOPE_SUBTREE,
257 filter, attrs, 0, NULL, NULL, NULL, LDAP_NO_LIMIT, &res );
259 case LDAP_SIZELIMIT_EXCEEDED:
260 case LDAP_TIMELIMIT_EXCEEDED:
262 nvalues = ldap_count_entries( ld, res );
263 if ( nvalues == 0 ) {
265 tester_ldap_error( ld, "ldap_search_ext_s", NULL );
270 values = malloc( ( nvalues + 1 ) * sizeof( char * ) );
271 for ( i = 0, e = ldap_first_entry( ld, res ); e != NULL; i++, e = ldap_next_entry( ld, e ) )
273 values[ i ] = ldap_get_dn( ld, e );
279 if ( do_retry == maxretries ) {
280 fprintf( stderr, " PID=%ld - Read base=\"%s\" filter=\"%s\" got %d values.\n",
281 (long) pid, sbase, filter, nvalues );
284 for ( i = 0; i < innerloop; i++ ) {
285 #if 0 /* use high-order bits for better randomness (Numerical Recipes in "C") */
286 int r = rand() % nvalues;
288 int r = ((double)nvalues)*rand()/(RAND_MAX + 1.0);
290 do_read( uri, manager, passwd, values[ r ], &ld,
291 noattrs, nobind, 1, maxretries, delay, force,
298 tester_ldap_error( ld, "ldap_search_ext_s", NULL );
302 fprintf( stderr, " PID=%ld - Search done (%d).\n", (long) pid, rc );
305 ldap_unbind_ext( ld, NULL, NULL );
310 do_read( char *uri, char *manager, struct berval *passwd, char *entry,
311 LDAP **ldp, int noattrs, int nobind, int maxloop,
312 int maxretries, int delay, int force, int chaserefs )
314 LDAP *ld = ldp ? *ldp : NULL;
315 int i = 0, do_retry = maxretries;
316 char *attrs[] = { "1.1", NULL };
317 int rc = LDAP_SUCCESS;
318 int version = LDAP_VERSION3;
322 ldap_initialize( &ld, uri );
324 tester_perror( "ldap_initialize", NULL );
325 exit( EXIT_FAILURE );
328 (void) ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version );
329 (void) ldap_set_option( ld, LDAP_OPT_REFERRALS,
330 chaserefs ? LDAP_OPT_ON : LDAP_OPT_OFF );
332 if ( do_retry == maxretries ) {
333 fprintf( stderr, "PID=%ld - Read(%d): entry=\"%s\".\n",
334 (long) pid, maxloop, entry );
338 rc = ldap_sasl_bind_s( ld, manager, LDAP_SASL_SIMPLE, passwd, NULL, NULL, NULL );
339 if ( rc != LDAP_SUCCESS ) {
340 tester_ldap_error( ld, "ldap_sasl_bind_s", NULL );
343 case LDAP_UNAVAILABLE:
344 if ( do_retry > 0 ) {
345 ldap_unbind_ext( ld, NULL, NULL );
356 exit( EXIT_FAILURE );
361 for ( ; i < maxloop; i++ ) {
362 LDAPMessage *res = NULL;
364 rc = ldap_search_ext_s( ld, entry, LDAP_SCOPE_BASE,
365 NULL, attrs, noattrs, NULL, NULL, NULL,
366 LDAP_NO_LIMIT, &res );
372 unsigned first = tester_ignore_err( rc );
375 snprintf( buf, sizeof( buf ), "ldap_search_ext_s(%s)", entry );
379 /* only log if first occurrence */
380 if ( force < 2 || first == 1 ) {
381 tester_ldap_error( ld, buf, NULL );
386 /* busy needs special handling */
387 tester_ldap_error( ld, buf, NULL );
388 if ( rc == LDAP_BUSY && do_retry > 0 ) {
389 ldap_unbind_ext( ld, NULL, NULL );
402 fprintf( stderr, " PID=%ld - Read done (%d).\n", (long) pid, rc );
405 ldap_unbind_ext( ld, NULL, NULL );