2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1999-2006 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,
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 innerloop,
49 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 };
88 int retries = RETRIES;
94 tester_init( "slapd-search", TESTER_SEARCH );
96 /* by default, tolerate referrals and no such object */
97 tester_ignore_str2errlist( "REFERRAL,NO_SUCH_OBJECT" );
99 while ( (i = getopt( argc, argv, "Aa:b:CD:f:FH:h:i:l:L:p:w:r:t:" )) != 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 );
121 case 'p': /* the servers port */
122 if ( lutil_atoi( &port, optarg ) != 0 ) {
127 case 'D': /* the servers manager */
128 manager = strdup( optarg );
131 case 'w': /* the server managers password */
132 passwd.bv_val = strdup( optarg );
133 passwd.bv_len = strlen( optarg );
134 memset( optarg, '*', passwd.bv_len );
138 attr = strdup( optarg );
141 case 'b': /* file with search base */
142 sbase = strdup( optarg );
145 case 'f': /* the search request */
146 filter = strdup( optarg );
153 case 'l': /* number of loops */
154 if ( lutil_atoi( &loops, optarg ) != 0 ) {
159 case 'L': /* number of loops */
160 if ( lutil_atoi( &outerloops, optarg ) != 0 ) {
165 case 'r': /* 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 (( sbase == NULL ) || ( filter == NULL ) || ( port == -1 && uri == NULL ))
186 if ( *filter == '\0' ) {
188 fprintf( stderr, "%s: invalid EMPTY search filter.\n",
190 exit( EXIT_FAILURE );
194 uri = tester_uri( uri, host, port );
196 for ( i = 0; i < outerloops; i++ ) {
197 if ( attr != NULL ) {
198 do_random( uri, manager, &passwd, sbase, filter, attr,
199 noattrs, loops, retries, delay, force, chaserefs );
202 do_search( uri, manager, &passwd, sbase, filter, NULL,
203 noattrs, loops, retries, delay, force, chaserefs );
207 exit( EXIT_SUCCESS );
212 do_random( char *uri, char *manager, struct berval *passwd,
213 char *sbase, char *filter, char *attr, int noattrs,
214 int innerloop, int maxretries, int delay, int force, int chaserefs )
217 int i = 0, do_retry = maxretries;
219 pid_t pid = getpid();
220 int rc = LDAP_SUCCESS;
221 int version = LDAP_VERSION3;
223 char **values = NULL;
224 LDAPMessage *res = NULL, *e = NULL;
231 ldap_initialize( &ld, uri );
233 tester_perror( "ldap_initialize", NULL );
234 exit( EXIT_FAILURE );
237 (void) ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version );
238 (void) ldap_set_option( ld, LDAP_OPT_REFERRALS,
239 chaserefs ? LDAP_OPT_ON : LDAP_OPT_OFF );
241 if ( do_retry == maxretries ) {
242 fprintf( stderr, "PID=%ld - Search(%d): base=\"%s\", filter=\"%s\" attr=\"%s\".\n",
243 (long) pid, innerloop, sbase, filter, attr );
246 rc = ldap_sasl_bind_s( ld, manager, LDAP_SASL_SIMPLE, passwd, NULL, NULL, NULL );
247 if ( rc != LDAP_SUCCESS ) {
248 tester_ldap_error( ld, "ldap_sasl_bind_s", NULL );
251 case LDAP_UNAVAILABLE:
256 exit( EXIT_FAILURE );
259 rc = ldap_search_ext_s( ld, sbase, LDAP_SCOPE_SUBTREE,
260 filter, attrs, 0, NULL, NULL, NULL, LDAP_NO_LIMIT, &res );
262 case LDAP_SIZELIMIT_EXCEEDED:
263 case LDAP_TIMELIMIT_EXCEEDED:
265 if ( ldap_count_entries( ld, res ) == 0 ) {
267 tester_ldap_error( ld, "ldap_search_ext_s", NULL );
272 for ( e = ldap_first_entry( ld, res ); e != NULL; e = ldap_next_entry( ld, e ) )
274 struct berval **v = ldap_get_values_len( ld, e, attr );
277 int n = ldap_count_values_len( v );
280 values = realloc( values, ( nvalues + n + 1 )*sizeof( char * ) );
281 for ( j = 0; j < n; j++ ) {
282 values[ nvalues + j ] = strdup( v[ j ]->bv_val );
284 values[ nvalues + j ] = NULL;
286 ldap_value_free_len( v );
292 if ( do_retry == maxretries ) {
293 fprintf( stderr, " PID=%ld - Search base=\"%s\" filter=\"%s\" got %d values.\n",
294 (long) pid, sbase, filter, nvalues );
297 for ( i = 0; i < innerloop; i++ ) {
299 #if 0 /* use high-order bits for better randomness (Numerical Recipes in "C") */
300 int r = rand() % nvalues;
302 int r = ((double)nvalues)*rand()/(RAND_MAX + 1.0);
304 snprintf( buf, sizeof( buf ), "(%s=%s)", attr, values[ r ] );
306 do_search( uri, manager, passwd, sbase, buf, &ld, noattrs,
307 1, maxretries, delay, force, chaserefs );
312 tester_ldap_error( ld, "ldap_search_ext_s", NULL );
316 fprintf( stderr, " PID=%ld - Search done (%d).\n", (long) pid, rc );
319 ldap_unbind_ext( ld, NULL, NULL );
324 do_search( char *uri, char *manager, struct berval *passwd,
325 char *sbase, char *filter, LDAP **ldp,
326 int noattrs, int innerloop, int maxretries, int delay,
327 int force, int chaserefs )
329 LDAP *ld = ldp ? *ldp : NULL;
330 int i = 0, do_retry = maxretries;
331 char *attrs[] = { "cn", "sn", NULL };
332 pid_t pid = getpid();
333 int rc = LDAP_SUCCESS;
334 int version = LDAP_VERSION3;
340 ldap_initialize( &ld, uri );
342 tester_perror( "ldap_initialize", NULL );
343 exit( EXIT_FAILURE );
346 (void) ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version );
347 (void) ldap_set_option( ld, LDAP_OPT_REFERRALS,
348 chaserefs ? LDAP_OPT_ON : LDAP_OPT_OFF );
350 if ( do_retry == maxretries ) {
351 fprintf( stderr, "PID=%ld - Search(%d): base=\"%s\", filter=\"%s\".\n",
352 (long) pid, innerloop, sbase, filter );
355 rc = ldap_sasl_bind_s( ld, manager, LDAP_SASL_SIMPLE, passwd, NULL, NULL, NULL );
356 if ( rc != LDAP_SUCCESS ) {
357 snprintf( buf, sizeof( buf ),
358 "bindDN=\"%s\"", manager );
359 tester_ldap_error( ld, "ldap_sasl_bind_s", buf );
362 case LDAP_UNAVAILABLE:
363 if ( do_retry > 0 ) {
364 ldap_unbind_ext( ld, NULL, NULL );
375 exit( EXIT_FAILURE );
379 for ( ; i < innerloop; i++ ) {
380 LDAPMessage *res = NULL;
382 rc = ldap_search_ext_s( ld, sbase, LDAP_SCOPE_SUBTREE,
383 filter, attrs, noattrs, NULL, NULL,
384 NULL, LDAP_NO_LIMIT, &res );
390 unsigned first = tester_ignore_err( rc );
393 /* only log if first occurrence */
394 if ( force < 2 || first == 1 ) {
395 tester_ldap_error( ld, "ldap_search_ext_s", NULL );
400 /* busy needs special handling */
401 snprintf( buf, sizeof( buf ),
402 "base=\"%s\" filter=\"%s\"\n",
404 tester_ldap_error( ld, "ldap_search_ext_s", buf );
405 if ( rc == LDAP_BUSY && do_retry > 0 ) {
406 ldap_unbind_ext( ld, NULL, NULL );
419 fprintf( stderr, " PID=%ld - Search done (%d).\n", (long) pid, rc );
422 ldap_unbind_ext( ld, NULL, NULL );