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_search( char *uri, char *manager, struct berval *passwd,
43 char *sbase, char *filter, LDAP **ldp, int noattrs, int nobind,
44 int innerloop, int maxretries, int delay, int force, int chaserefs );
47 do_random( char *uri, char *manager, struct berval *passwd,
48 char *sbase, char *filter, char *attr, int noattrs, int nobind,
49 int innerloop, int maxretries, int delay, int force, int chaserefs );
56 "-H <uri> | ([-h <host>] -p <port>) "
76 main( int argc, char **argv )
80 char *host = "localhost";
83 struct berval passwd = { 0, NULL };
89 int retries = RETRIES;
96 tester_init( "slapd-search", TESTER_SEARCH );
98 /* by default, tolerate referrals and no such object */
99 tester_ignore_str2errlist( "REFERRAL,NO_SUCH_OBJECT" );
101 while ( ( i = getopt( argc, argv, "Aa:b:CD:f:FH:h:i:l:L:Np:r:t:w:" ) ) != EOF )
112 case 'H': /* the server uri */
113 uri = strdup( optarg );
116 case 'h': /* the servers host */
117 host = strdup( optarg );
121 tester_ignore_str2errlist( optarg );
128 case 'p': /* the servers port */
129 if ( lutil_atoi( &port, optarg ) != 0 ) {
134 case 'D': /* the servers manager */
135 manager = strdup( optarg );
138 case 'w': /* the server managers password */
139 passwd.bv_val = strdup( optarg );
140 passwd.bv_len = strlen( optarg );
141 memset( optarg, '*', passwd.bv_len );
145 attr = strdup( optarg );
148 case 'b': /* file with search base */
149 sbase = strdup( optarg );
152 case 'f': /* the search request */
153 filter = strdup( optarg );
160 case 'l': /* number of loops */
161 if ( lutil_atoi( &loops, optarg ) != 0 ) {
166 case 'L': /* number of loops */
167 if ( lutil_atoi( &outerloops, optarg ) != 0 ) {
172 case 'r': /* number of retries */
173 if ( lutil_atoi( &retries, optarg ) != 0 ) {
178 case 't': /* delay in seconds */
179 if ( lutil_atoi( &delay, optarg ) != 0 ) {
190 if (( sbase == NULL ) || ( filter == NULL ) || ( port == -1 && uri == NULL ))
193 if ( *filter == '\0' ) {
195 fprintf( stderr, "%s: invalid EMPTY search filter.\n",
197 exit( EXIT_FAILURE );
201 uri = tester_uri( uri, host, port );
203 for ( i = 0; i < outerloops; i++ ) {
204 if ( attr != NULL ) {
205 do_random( uri, manager, &passwd, sbase, filter, attr,
206 noattrs, nobind, loops, retries, delay, force, chaserefs );
209 do_search( uri, manager, &passwd, sbase, filter, NULL,
210 noattrs, nobind, loops, retries, delay, force, chaserefs );
214 exit( EXIT_SUCCESS );
219 do_random( char *uri, char *manager, struct berval *passwd,
220 char *sbase, char *filter, char *attr, int noattrs, int nobind,
221 int innerloop, int maxretries, int delay, int force, int chaserefs )
224 int i = 0, do_retry = maxretries;
226 int rc = LDAP_SUCCESS;
227 int version = LDAP_VERSION3;
229 char **values = NULL;
230 LDAPMessage *res = NULL, *e = NULL;
235 ldap_initialize( &ld, uri );
237 tester_perror( "ldap_initialize", NULL );
238 exit( EXIT_FAILURE );
241 (void) ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version );
242 (void) ldap_set_option( ld, LDAP_OPT_REFERRALS,
243 chaserefs ? LDAP_OPT_ON : LDAP_OPT_OFF );
245 if ( do_retry == maxretries ) {
246 fprintf( stderr, "PID=%ld - Search(%d): base=\"%s\", filter=\"%s\" attr=\"%s\".\n",
247 (long) pid, innerloop, sbase, filter, attr );
251 rc = ldap_sasl_bind_s( ld, manager, LDAP_SASL_SIMPLE, passwd, NULL, NULL, NULL );
252 if ( rc != LDAP_SUCCESS ) {
253 tester_ldap_error( ld, "ldap_sasl_bind_s", NULL );
256 case LDAP_UNAVAILABLE:
261 exit( EXIT_FAILURE );
265 rc = ldap_search_ext_s( ld, sbase, LDAP_SCOPE_SUBTREE,
266 filter, attrs, 0, NULL, NULL, NULL, LDAP_NO_LIMIT, &res );
268 case LDAP_SIZELIMIT_EXCEEDED:
269 case LDAP_TIMELIMIT_EXCEEDED:
271 if ( ldap_count_entries( ld, res ) == 0 ) {
273 tester_ldap_error( ld, "ldap_search_ext_s", NULL );
278 for ( e = ldap_first_entry( ld, res ); e != NULL; e = ldap_next_entry( ld, e ) )
280 struct berval **v = ldap_get_values_len( ld, e, attr );
283 int n = ldap_count_values_len( v );
286 values = realloc( values, ( nvalues + n + 1 )*sizeof( char * ) );
287 for ( j = 0; j < n; j++ ) {
288 values[ nvalues + j ] = strdup( v[ j ]->bv_val );
290 values[ nvalues + j ] = NULL;
292 ldap_value_free_len( v );
299 fprintf( stderr, " PID=%ld - Search base=\"%s\" filter=\"%s\" got %d values.\n",
300 (long) pid, sbase, filter, nvalues );
304 if ( do_retry == maxretries ) {
305 fprintf( stderr, " PID=%ld - Search base=\"%s\" filter=\"%s\" got %d values.\n",
306 (long) pid, sbase, filter, nvalues );
309 for ( i = 0; i < innerloop; i++ ) {
311 #if 0 /* use high-order bits for better randomness (Numerical Recipes in "C") */
312 int r = rand() % nvalues;
314 int r = ((double)nvalues)*rand()/(RAND_MAX + 1.0);
316 snprintf( buf, sizeof( buf ), "(%s=%s)", attr, values[ r ] );
318 do_search( uri, manager, passwd, sbase, buf, &ld, noattrs, nobind,
319 1, maxretries, delay, force, chaserefs );
324 tester_ldap_error( ld, "ldap_search_ext_s", NULL );
328 fprintf( stderr, " PID=%ld - Search done (%d).\n", (long) pid, rc );
331 ldap_unbind_ext( ld, NULL, NULL );
336 do_search( char *uri, char *manager, struct berval *passwd,
337 char *sbase, char *filter, LDAP **ldp, int noattrs, int nobind,
338 int innerloop, int maxretries, int delay, int force, int chaserefs )
340 LDAP *ld = ldp ? *ldp : NULL;
341 int i = 0, do_retry = maxretries;
342 char *attrs[] = { "cn", "sn", NULL };
343 int rc = LDAP_SUCCESS;
344 int version = LDAP_VERSION3;
350 ldap_initialize( &ld, uri );
352 tester_perror( "ldap_initialize", NULL );
353 exit( EXIT_FAILURE );
356 (void) ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version );
357 (void) ldap_set_option( ld, LDAP_OPT_REFERRALS,
358 chaserefs ? LDAP_OPT_ON : LDAP_OPT_OFF );
360 if ( do_retry == maxretries ) {
361 fprintf( stderr, "PID=%ld - Search(%d): base=\"%s\", filter=\"%s\".\n",
362 (long) pid, innerloop, sbase, filter );
366 rc = ldap_sasl_bind_s( ld, manager, LDAP_SASL_SIMPLE, passwd, NULL, NULL, NULL );
367 if ( rc != LDAP_SUCCESS ) {
368 snprintf( buf, sizeof( buf ),
369 "bindDN=\"%s\"", manager );
370 tester_ldap_error( ld, "ldap_sasl_bind_s", buf );
373 case LDAP_UNAVAILABLE:
374 if ( do_retry > 0 ) {
375 ldap_unbind_ext( ld, NULL, NULL );
386 exit( EXIT_FAILURE );
391 for ( ; i < innerloop; i++ ) {
392 LDAPMessage *res = NULL;
394 rc = ldap_search_ext_s( ld, sbase, LDAP_SCOPE_SUBTREE,
395 filter, attrs, noattrs, NULL, NULL,
396 NULL, LDAP_NO_LIMIT, &res );
402 unsigned first = tester_ignore_err( rc );
405 /* only log if first occurrence */
406 if ( force < 2 || first == 1 ) {
407 tester_ldap_error( ld, "ldap_search_ext_s", NULL );
412 /* busy needs special handling */
413 snprintf( buf, sizeof( buf ),
414 "base=\"%s\" filter=\"%s\"\n",
416 tester_ldap_error( ld, "ldap_search_ext_s", buf );
417 if ( rc == LDAP_BUSY && do_retry > 0 ) {
418 ldap_unbind_ext( ld, NULL, NULL );
431 fprintf( stderr, " PID=%ld - Search done (%d).\n", (long) pid, rc );
434 ldap_unbind_ext( ld, NULL, NULL );