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 );
298 if ( do_retry == maxretries ) {
299 fprintf( stderr, " PID=%ld - Search base=\"%s\" filter=\"%s\" got %d values.\n",
300 (long) pid, sbase, filter, nvalues );
303 for ( i = 0; i < innerloop; i++ ) {
305 #if 0 /* use high-order bits for better randomness (Numerical Recipes in "C") */
306 int r = rand() % nvalues;
308 int r = ((double)nvalues)*rand()/(RAND_MAX + 1.0);
310 snprintf( buf, sizeof( buf ), "(%s=%s)", attr, values[ r ] );
312 do_search( uri, manager, passwd, sbase, buf, &ld, noattrs, nobind,
313 1, maxretries, delay, force, chaserefs );
318 tester_ldap_error( ld, "ldap_search_ext_s", NULL );
322 fprintf( stderr, " PID=%ld - Search done (%d).\n", (long) pid, rc );
325 ldap_unbind_ext( ld, NULL, NULL );
330 do_search( char *uri, char *manager, struct berval *passwd,
331 char *sbase, char *filter, LDAP **ldp, int noattrs, int nobind,
332 int innerloop, int maxretries, int delay, int force, int chaserefs )
334 LDAP *ld = ldp ? *ldp : NULL;
335 int i = 0, do_retry = maxretries;
336 char *attrs[] = { "cn", "sn", NULL };
337 int rc = LDAP_SUCCESS;
338 int version = LDAP_VERSION3;
344 ldap_initialize( &ld, uri );
346 tester_perror( "ldap_initialize", NULL );
347 exit( EXIT_FAILURE );
350 (void) ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version );
351 (void) ldap_set_option( ld, LDAP_OPT_REFERRALS,
352 chaserefs ? LDAP_OPT_ON : LDAP_OPT_OFF );
354 if ( do_retry == maxretries ) {
355 fprintf( stderr, "PID=%ld - Search(%d): base=\"%s\", filter=\"%s\".\n",
356 (long) pid, innerloop, sbase, filter );
360 rc = ldap_sasl_bind_s( ld, manager, LDAP_SASL_SIMPLE, passwd, NULL, NULL, NULL );
361 if ( rc != LDAP_SUCCESS ) {
362 snprintf( buf, sizeof( buf ),
363 "bindDN=\"%s\"", manager );
364 tester_ldap_error( ld, "ldap_sasl_bind_s", buf );
367 case LDAP_UNAVAILABLE:
368 if ( do_retry > 0 ) {
369 ldap_unbind_ext( ld, NULL, NULL );
380 exit( EXIT_FAILURE );
385 for ( ; i < innerloop; i++ ) {
386 LDAPMessage *res = NULL;
388 rc = ldap_search_ext_s( ld, sbase, LDAP_SCOPE_SUBTREE,
389 filter, attrs, noattrs, NULL, NULL,
390 NULL, LDAP_NO_LIMIT, &res );
396 unsigned first = tester_ignore_err( rc );
399 /* only log if first occurrence */
400 if ( force < 2 || first == 1 ) {
401 tester_ldap_error( ld, "ldap_search_ext_s", NULL );
406 /* busy needs special handling */
407 snprintf( buf, sizeof( buf ),
408 "base=\"%s\" filter=\"%s\"\n",
410 tester_ldap_error( ld, "ldap_search_ext_s", buf );
411 if ( rc == LDAP_BUSY && do_retry > 0 ) {
412 ldap_unbind_ext( ld, NULL, NULL );
425 fprintf( stderr, " PID=%ld - Search done (%d).\n", (long) pid, rc );
428 ldap_unbind_ext( ld, NULL, NULL );