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;
229 ldap_initialize( &ld, uri );
231 tester_perror( "ldap_initialize", NULL );
232 exit( EXIT_FAILURE );
235 (void) ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version );
236 (void) ldap_set_option( ld, LDAP_OPT_REFERRALS,
237 chaserefs ? LDAP_OPT_ON : LDAP_OPT_OFF );
239 if ( do_retry == maxretries ) {
240 fprintf( stderr, "PID=%ld - Search(%d): base=\"%s\", filter=\"%s\" attr=\"%s\".\n",
241 (long) pid, innerloop, sbase, filter, attr );
244 rc = ldap_sasl_bind_s( ld, manager, LDAP_SASL_SIMPLE, passwd, NULL, NULL, NULL );
245 if ( rc != LDAP_SUCCESS ) {
246 tester_ldap_error( ld, "ldap_sasl_bind_s", NULL );
249 case LDAP_UNAVAILABLE:
254 exit( EXIT_FAILURE );
257 rc = ldap_search_ext_s( ld, sbase, LDAP_SCOPE_SUBTREE,
258 filter, attrs, 0, NULL, NULL, NULL, LDAP_NO_LIMIT, &res );
260 case LDAP_SIZELIMIT_EXCEEDED:
261 case LDAP_TIMELIMIT_EXCEEDED:
263 if ( ldap_count_entries( ld, res ) == 0 ) {
265 tester_ldap_error( ld, "ldap_search_ext_s", NULL );
270 for ( e = ldap_first_entry( ld, res ); e != NULL; e = ldap_next_entry( ld, e ) )
272 struct berval **v = ldap_get_values_len( ld, e, attr );
275 int n = ldap_count_values_len( v );
278 values = realloc( values, ( nvalues + n + 1 )*sizeof( char * ) );
279 for ( j = 0; j < n; j++ ) {
280 values[ nvalues + j ] = strdup( v[ j ]->bv_val );
282 values[ nvalues + j ] = NULL;
284 ldap_value_free_len( v );
290 if ( do_retry == maxretries ) {
291 fprintf( stderr, " PID=%ld - Search base=\"%s\" filter=\"%s\" got %d values.\n",
292 (long) pid, sbase, filter, nvalues );
295 for ( i = 0; i < innerloop; i++ ) {
298 snprintf( buf, sizeof( buf ), "(%s=%s)", attr, values[ rand() % nvalues ] );
300 do_search( uri, manager, passwd, sbase, buf, &ld, noattrs,
301 1, maxretries, delay, force, chaserefs );
306 tester_ldap_error( ld, "ldap_search_ext_s", NULL );
310 fprintf( stderr, " PID=%ld - Search done (%d).\n", (long) pid, rc );
313 ldap_unbind_ext( ld, NULL, NULL );
318 do_search( char *uri, char *manager, struct berval *passwd,
319 char *sbase, char *filter, LDAP **ldp,
320 int noattrs, int innerloop, int maxretries, int delay,
321 int force, int chaserefs )
323 LDAP *ld = ldp ? *ldp : NULL;
324 int i = 0, do_retry = maxretries;
325 char *attrs[] = { "cn", "sn", NULL };
326 pid_t pid = getpid();
327 int rc = LDAP_SUCCESS;
328 int version = LDAP_VERSION3;
334 ldap_initialize( &ld, uri );
336 tester_perror( "ldap_initialize", NULL );
337 exit( EXIT_FAILURE );
340 (void) ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version );
341 (void) ldap_set_option( ld, LDAP_OPT_REFERRALS,
342 chaserefs ? LDAP_OPT_ON : LDAP_OPT_OFF );
344 if ( do_retry == maxretries ) {
345 fprintf( stderr, "PID=%ld - Search(%d): base=\"%s\", filter=\"%s\".\n",
346 (long) pid, innerloop, sbase, filter );
349 rc = ldap_sasl_bind_s( ld, manager, LDAP_SASL_SIMPLE, passwd, NULL, NULL, NULL );
350 if ( rc != LDAP_SUCCESS ) {
351 snprintf( buf, sizeof( buf ),
352 "bindDN=\"%s\"", manager );
353 tester_ldap_error( ld, "ldap_sasl_bind_s", buf );
356 case LDAP_UNAVAILABLE:
357 if ( do_retry > 0 ) {
358 ldap_unbind_ext( ld, NULL, NULL );
369 exit( EXIT_FAILURE );
373 for ( ; i < innerloop; i++ ) {
374 LDAPMessage *res = NULL;
376 rc = ldap_search_ext_s( ld, sbase, LDAP_SCOPE_SUBTREE,
377 filter, attrs, noattrs, NULL, NULL,
378 NULL, LDAP_NO_LIMIT, &res );
384 unsigned first = tester_ignore_err( rc );
387 /* only log if first occurrence */
388 if ( force < 2 || first == 1 ) {
389 tester_ldap_error( ld, "ldap_search_ext_s", NULL );
394 /* busy needs special handling */
395 snprintf( buf, sizeof( buf ),
396 "base=\"%s\" filter=\"%s\"\n",
398 tester_ldap_error( ld, "ldap_search_ext_s", buf );
399 if ( rc == LDAP_BUSY && do_retry > 0 ) {
400 ldap_unbind_ext( ld, NULL, NULL );
413 fprintf( stderr, " PID=%ld - Search done (%d).\n", (long) pid, rc );
416 ldap_unbind_ext( ld, NULL, NULL );