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,
44 int innerloop, int maxretries, int delay );
47 do_random( char *uri, char *manager, struct berval *passwd,
48 char *sbase, char *filter, char *attr, int innerloop,
49 int maxretries, int delay );
56 "-H <uri> | ([-h <host>] -p <port>) "
71 main( int argc, char **argv )
75 char *host = "localhost";
78 struct berval passwd = { 0, NULL };
84 int retries = RETRIES;
87 tester_init( "slapd-search" );
89 while ( (i = getopt( argc, argv, "a:b:D:f:H:h:l:L:p:w:r:t:" )) != EOF ) {
91 case 'H': /* the server uri */
92 uri = strdup( optarg );
95 case 'h': /* the servers host */
96 host = strdup( optarg );
99 case 'p': /* the servers port */
100 if ( lutil_atoi( &port, optarg ) != 0 ) {
105 case 'D': /* the servers manager */
106 manager = strdup( optarg );
109 case 'w': /* the server managers password */
110 passwd.bv_val = strdup( optarg );
111 passwd.bv_len = strlen( optarg );
115 attr = strdup( optarg );
118 case 'b': /* file with search base */
119 sbase = strdup( optarg );
122 case 'f': /* the search request */
123 filter = strdup( optarg );
126 case 'l': /* number of loops */
127 if ( lutil_atoi( &loops, optarg ) != 0 ) {
132 case 'L': /* number of loops */
133 if ( lutil_atoi( &outerloops, optarg ) != 0 ) {
138 case 'r': /* number of retries */
139 if ( lutil_atoi( &retries, optarg ) != 0 ) {
144 case 't': /* delay in seconds */
145 if ( lutil_atoi( &delay, optarg ) != 0 ) {
156 if (( sbase == NULL ) || ( filter == NULL ) || ( port == -1 && uri == NULL ))
159 if ( *filter == '\0' ) {
161 fprintf( stderr, "%s: invalid EMPTY search filter.\n",
163 exit( EXIT_FAILURE );
167 uri = tester_uri( uri, host, port );
169 for ( i = 0; i < outerloops; i++ ) {
170 if ( attr != NULL ) {
171 do_random( uri, manager, &passwd, sbase, filter, attr,
172 loops, retries, delay );
175 do_search( uri, manager, &passwd, sbase, filter, NULL,
176 loops, retries, delay );
180 exit( EXIT_SUCCESS );
185 do_random( char *uri, char *manager, struct berval *passwd,
186 char *sbase, char *filter, char *attr,
187 int innerloop, int maxretries, int delay )
190 int i = 0, do_retry = maxretries;
192 pid_t pid = getpid();
193 int rc = LDAP_SUCCESS;
194 int version = LDAP_VERSION3;
196 char **values = NULL;
197 LDAPMessage *res = NULL;
202 ldap_initialize( &ld, uri );
204 tester_perror( "ldap_initialize" );
205 exit( EXIT_FAILURE );
208 (void) ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version );
210 if ( do_retry == maxretries ) {
211 fprintf( stderr, "PID=%ld - Search(%d): base=\"%s\", filter=\"%s\" attr=\"%s\".\n",
212 (long) pid, innerloop, sbase, filter, attr );
215 rc = ldap_sasl_bind_s( ld, manager, LDAP_SASL_SIMPLE, passwd, NULL, NULL, NULL );
216 if ( rc != LDAP_SUCCESS ) {
217 tester_ldap_error( ld, "ldap_sasl_bind_s" );
220 case LDAP_UNAVAILABLE:
225 exit( EXIT_FAILURE );
228 rc = ldap_search_ext_s( ld, sbase, LDAP_SCOPE_SUBTREE,
229 filter, attrs, 0, NULL, NULL, NULL, LDAP_NO_LIMIT, &res );
230 if ( rc != LDAP_SUCCESS ) {
231 tester_ldap_error( ld, "ldap_search_ext_s" );
235 for ( e = ldap_first_entry( ld, res ); e != NULL; e = ldap_next_entry( ld, e ) )
237 struct berval **v = ldap_get_values_len( ld, e, attr );
240 int n = ldap_count_values_len( v );
243 values = realloc( values, ( nvalues + n + 1 )*sizeof( char * ) );
244 for ( j = 0; j < n; j++ ) {
245 values[ nvalues + j ] = strdup( v[ j ]->bv_val );
247 values[ nvalues + j ] = NULL;
249 ldap_value_free_len( v );
255 for ( i = 0; i < innerloop; i++ ) {
258 snprintf( buf, sizeof( buf ), "(%s=%s)", attr, values[ rand() % nvalues ] );
260 do_search( uri, manager, passwd, sbase, buf, &ld,
261 1, maxretries, delay );
266 fprintf( stderr, " PID=%ld - Search done (%d).\n", (long) pid, rc );
269 ldap_unbind_ext( ld, NULL, NULL );
273 do_search( char *uri, char *manager, struct berval *passwd,
274 char *sbase, char *filter, LDAP **ldp,
275 int innerloop, int maxretries, int delay )
277 LDAP *ld = ldp ? *ldp : NULL;
278 int i = 0, do_retry = maxretries;
279 char *attrs[] = { "cn", "sn", NULL };
280 pid_t pid = getpid();
281 int rc = LDAP_SUCCESS;
282 int version = LDAP_VERSION3;
286 ldap_initialize( &ld, uri );
288 tester_perror( "ldap_initialize" );
289 exit( EXIT_FAILURE );
292 (void) ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version );
294 if ( do_retry == maxretries ) {
295 fprintf( stderr, "PID=%ld - Search(%d): base=\"%s\", filter=\"%s\".\n",
296 (long) pid, innerloop, sbase, filter );
299 rc = ldap_sasl_bind_s( ld, manager, LDAP_SASL_SIMPLE, passwd, NULL, NULL, NULL );
300 if ( rc != LDAP_SUCCESS ) {
301 tester_ldap_error( ld, "ldap_sasl_bind_s" );
304 case LDAP_UNAVAILABLE:
305 if ( do_retry > 0 ) {
306 ldap_unbind_ext( ld, NULL, NULL );
317 exit( EXIT_FAILURE );
321 for ( ; i < innerloop; i++ ) {
324 rc = ldap_search_ext_s( ld, sbase, LDAP_SCOPE_SUBTREE,
325 filter, attrs, 0, NULL, NULL, NULL, LDAP_NO_LIMIT, &res );
326 if ( rc != LDAP_SUCCESS ) {
327 tester_ldap_error( ld, "ldap_search_ext_s" );
328 if ( rc == LDAP_BUSY && do_retry > 0 ) {
329 ldap_unbind_ext( ld, NULL, NULL );
333 if ( rc != LDAP_NO_SUCH_OBJECT ) break;
346 fprintf( stderr, " PID=%ld - Search done (%d).\n", (long) pid, rc );
349 ldap_unbind_ext( ld, NULL, NULL );