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_read( char *uri, char *manager, struct berval *passwd,
43 char *entry, LDAP **ld, int noattrs, int maxloop,
44 int maxretries, int delay, int force, int chaserefs );
47 do_random( char *uri, char *manager, struct berval *passwd,
48 char *sbase, char *filter, int noattrs,
49 int innerloop, int maxretries, int delay, int force, int chaserefs );
56 "-H <uri> | ([-h <host>] -p <port>) "
74 main( int argc, char **argv )
78 char *host = "localhost";
81 struct berval passwd = { 0, NULL };
86 int retries = RETRIES;
92 tester_init( "slapd-read", TESTER_READ );
94 /* by default, tolerate referrals and no such object */
95 tester_ignore_str2errlist( "REFERRAL,NO_SUCH_OBJECT" );
97 while ( (i = getopt( argc, argv, "ACD:H:h:i:p:e:Ff:l:L:r:t:w:" )) != EOF ) {
107 case 'H': /* the server uri */
108 uri = strdup( optarg );
111 case 'h': /* the servers host */
112 host = strdup( optarg );
116 tester_ignore_str2errlist( optarg );
119 case 'p': /* the servers port */
120 if ( lutil_atoi( &port, optarg ) != 0 ) {
125 case 'D': /* the servers manager */
126 manager = strdup( optarg );
129 case 'w': /* the server managers password */
130 passwd.bv_val = strdup( optarg );
131 passwd.bv_len = strlen( optarg );
132 memset( optarg, '*', passwd.bv_len );
135 case 'e': /* DN to search for */
136 entry = strdup( optarg );
139 case 'f': /* the search request */
140 filter = strdup( optarg );
147 case 'l': /* the number of loops */
148 if ( lutil_atoi( &loops, optarg ) != 0 ) {
153 case 'L': /* the number of outerloops */
154 if ( lutil_atoi( &outerloops, optarg ) != 0 ) {
159 case 'r': /* the number of retries */
160 if ( lutil_atoi( &retries, optarg ) != 0 ) {
165 case 't': /* delay in seconds */
166 if ( lutil_atoi( &delay, optarg ) != 0 ) {
177 if (( entry == NULL ) || ( port == -1 && uri == NULL ))
180 if ( *entry == '\0' ) {
181 fprintf( stderr, "%s: invalid EMPTY entry DN.\n",
183 exit( EXIT_FAILURE );
186 uri = tester_uri( uri, host, port );
188 for ( i = 0; i < outerloops; i++ ) {
189 if ( filter != NULL ) {
190 do_random( uri, manager, &passwd, entry, filter,
191 noattrs, loops, retries, delay, force,
195 do_read( uri, manager, &passwd, entry, NULL, noattrs,
196 loops, retries, delay, force, chaserefs );
200 exit( EXIT_SUCCESS );
204 do_random( char *uri, char *manager, struct berval *passwd,
205 char *sbase, char *filter, int noattrs,
206 int innerloop, int maxretries, int delay, int force, int chaserefs )
209 int i = 0, do_retry = maxretries;
211 pid_t pid = getpid();
212 int rc = LDAP_SUCCESS;
213 int version = LDAP_VERSION3;
215 char **values = NULL;
216 LDAPMessage *res = NULL, *e = NULL;
218 attrs[ 0 ] = LDAP_NO_ATTRS;
221 ldap_initialize( &ld, uri );
223 tester_perror( "ldap_initialize", NULL );
224 exit( EXIT_FAILURE );
227 (void) ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version );
228 (void) ldap_set_option( ld, LDAP_OPT_REFERRALS,
229 chaserefs ? LDAP_OPT_ON : LDAP_OPT_OFF );
231 if ( do_retry == maxretries ) {
232 fprintf( stderr, "PID=%ld - Read(%d): base=\"%s\", filter=\"%s\".\n",
233 (long) pid, innerloop, sbase, filter );
236 rc = ldap_sasl_bind_s( ld, manager, LDAP_SASL_SIMPLE, passwd, NULL, NULL, NULL );
237 if ( rc != LDAP_SUCCESS ) {
238 tester_ldap_error( ld, "ldap_sasl_bind_s", NULL );
241 case LDAP_UNAVAILABLE:
246 exit( EXIT_FAILURE );
249 rc = ldap_search_ext_s( ld, sbase, LDAP_SCOPE_SUBTREE,
250 filter, attrs, 0, NULL, NULL, NULL, LDAP_NO_LIMIT, &res );
252 case LDAP_SIZELIMIT_EXCEEDED:
253 case LDAP_TIMELIMIT_EXCEEDED:
255 nvalues = ldap_count_entries( ld, res );
256 if ( nvalues == 0 ) {
258 tester_ldap_error( ld, "ldap_search_ext_s", NULL );
263 values = malloc( ( nvalues + 1 ) * sizeof( char * ) );
264 for ( i = 0, e = ldap_first_entry( ld, res ); e != NULL; i++, e = ldap_next_entry( ld, e ) )
266 values[ i ] = ldap_get_dn( ld, e );
272 if ( do_retry == maxretries ) {
273 fprintf( stderr, " PID=%ld - Read base=\"%s\" filter=\"%s\" got %d values.\n",
274 (long) pid, sbase, filter, nvalues );
277 for ( i = 0; i < innerloop; i++ ) {
278 do_read( uri, manager, passwd, values[ rand() % nvalues ], &ld,
279 noattrs, 1, maxretries, delay, force,
285 tester_ldap_error( ld, "ldap_search_ext_s", NULL );
289 fprintf( stderr, " PID=%ld - Search done (%d).\n", (long) pid, rc );
292 ldap_unbind_ext( ld, NULL, NULL );
297 do_read( char *uri, char *manager, struct berval *passwd, char *entry,
298 LDAP **ldp, int noattrs, int maxloop,
299 int maxretries, int delay, int force, int chaserefs )
301 LDAP *ld = ldp ? *ldp : NULL;
302 int i = 0, do_retry = maxretries;
303 char *attrs[] = { "1.1", NULL };
304 pid_t pid = getpid();
305 int rc = LDAP_SUCCESS;
306 int version = LDAP_VERSION3;
310 ldap_initialize( &ld, uri );
312 tester_perror( "ldap_initialize", NULL );
313 exit( EXIT_FAILURE );
316 (void) ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version );
317 (void) ldap_set_option( ld, LDAP_OPT_REFERRALS,
318 chaserefs ? LDAP_OPT_ON : LDAP_OPT_OFF );
320 if ( do_retry == maxretries ) {
321 fprintf( stderr, "PID=%ld - Read(%d): entry=\"%s\".\n",
322 (long) pid, maxloop, entry );
325 rc = ldap_sasl_bind_s( ld, manager, LDAP_SASL_SIMPLE, passwd, NULL, NULL, NULL );
326 if ( rc != LDAP_SUCCESS ) {
327 tester_ldap_error( ld, "ldap_sasl_bind_s", NULL );
330 case LDAP_UNAVAILABLE:
331 if ( do_retry > 0 ) {
332 ldap_unbind_ext( ld, NULL, NULL );
343 exit( EXIT_FAILURE );
347 for ( ; i < maxloop; i++ ) {
348 LDAPMessage *res = NULL;
350 rc = ldap_search_ext_s( ld, entry, LDAP_SCOPE_BASE,
351 NULL, attrs, noattrs, NULL, NULL, NULL,
352 LDAP_NO_LIMIT, &res );
358 unsigned first = tester_ignore_err( rc );
361 snprintf( buf, sizeof( buf ), "ldap_search_ext_s(%s)", entry );
365 /* only log if first occurrence */
366 if ( force < 2 || first == 1 ) {
367 tester_ldap_error( ld, buf, NULL );
372 /* busy needs special handling */
373 tester_ldap_error( ld, buf, NULL );
374 if ( rc == LDAP_BUSY && do_retry > 0 ) {
375 ldap_unbind_ext( ld, NULL, NULL );
388 fprintf( stderr, " PID=%ld - Read done (%d).\n", (long) pid, rc );
391 ldap_unbind_ext( ld, NULL, NULL );