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>) "
73 main( int argc, char **argv )
77 char *host = "localhost";
80 struct berval passwd = { 0, NULL };
85 int retries = RETRIES;
91 tester_init( "slapd-read" );
93 while ( (i = getopt( argc, argv, "ACD:H:h:p:e:Ff:l:L:r:t:w:" )) != EOF ) {
103 case 'H': /* the server uri */
104 uri = strdup( optarg );
107 case 'h': /* the servers host */
108 host = strdup( optarg );
111 case 'p': /* the servers port */
112 if ( lutil_atoi( &port, optarg ) != 0 ) {
117 case 'D': /* the servers manager */
118 manager = strdup( optarg );
121 case 'w': /* the server managers password */
122 passwd.bv_val = strdup( optarg );
123 passwd.bv_len = strlen( optarg );
126 case 'e': /* DN to search for */
127 entry = strdup( optarg );
130 case 'f': /* the search request */
131 filter = strdup( optarg );
138 case 'l': /* the number of loops */
139 if ( lutil_atoi( &loops, optarg ) != 0 ) {
144 case 'L': /* the number of outerloops */
145 if ( lutil_atoi( &outerloops, optarg ) != 0 ) {
150 case 'r': /* the number of retries */
151 if ( lutil_atoi( &retries, optarg ) != 0 ) {
156 case 't': /* delay in seconds */
157 if ( lutil_atoi( &delay, optarg ) != 0 ) {
168 if (( entry == NULL ) || ( port == -1 && uri == NULL ))
171 if ( *entry == '\0' ) {
172 fprintf( stderr, "%s: invalid EMPTY entry DN.\n",
174 exit( EXIT_FAILURE );
177 uri = tester_uri( uri, host, port );
179 for ( i = 0; i < outerloops; i++ ) {
180 if ( filter != NULL ) {
181 do_random( uri, manager, &passwd, entry, filter,
182 noattrs, loops, retries, delay, force,
186 do_read( uri, manager, &passwd, entry, NULL, noattrs,
187 loops, retries, delay, force, chaserefs );
191 exit( EXIT_SUCCESS );
195 do_random( char *uri, char *manager, struct berval *passwd,
196 char *sbase, char *filter, int noattrs,
197 int innerloop, int maxretries, int delay, int force, int chaserefs )
200 int i = 0, do_retry = maxretries;
202 pid_t pid = getpid();
203 int rc = LDAP_SUCCESS;
204 int version = LDAP_VERSION3;
206 char **values = NULL;
207 LDAPMessage *res = NULL, *e = NULL;
209 attrs[ 0 ] = LDAP_NO_ATTRS;
212 ldap_initialize( &ld, uri );
214 tester_perror( "ldap_initialize", NULL );
215 exit( EXIT_FAILURE );
218 (void) ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version );
219 (void) ldap_set_option( ld, LDAP_OPT_REFERRALS,
220 chaserefs ? LDAP_OPT_ON : LDAP_OPT_OFF );
222 if ( do_retry == maxretries ) {
223 fprintf( stderr, "PID=%ld - Read(%d): base=\"%s\", filter=\"%s\".\n",
224 (long) pid, innerloop, sbase, filter );
227 rc = ldap_sasl_bind_s( ld, manager, LDAP_SASL_SIMPLE, passwd, NULL, NULL, NULL );
228 if ( rc != LDAP_SUCCESS ) {
229 tester_ldap_error( ld, "ldap_sasl_bind_s", NULL );
232 case LDAP_UNAVAILABLE:
237 exit( EXIT_FAILURE );
240 rc = ldap_search_ext_s( ld, sbase, LDAP_SCOPE_SUBTREE,
241 filter, attrs, 0, NULL, NULL, NULL, LDAP_NO_LIMIT, &res );
243 case LDAP_SIZELIMIT_EXCEEDED:
244 case LDAP_TIMELIMIT_EXCEEDED:
246 nvalues = ldap_count_entries( ld, res );
247 if ( nvalues == 0 ) {
249 tester_ldap_error( ld, "ldap_search_ext_s", NULL );
254 values = malloc( ( nvalues + 1 ) * sizeof( char * ) );
255 for ( i = 0, e = ldap_first_entry( ld, res ); e != NULL; i++, e = ldap_next_entry( ld, e ) )
257 values[ i ] = ldap_get_dn( ld, e );
263 if ( do_retry == maxretries ) {
264 fprintf( stderr, "PID=%ld - got %d values.\n", (long) pid, nvalues );
267 for ( i = 0; i < innerloop; i++ ) {
268 do_read( uri, manager, passwd, values[ rand() % nvalues ], &ld,
269 noattrs, 1, maxretries, delay, force,
275 tester_ldap_error( ld, "ldap_search_ext_s", NULL );
279 fprintf( stderr, " PID=%ld - Search done (%d).\n", (long) pid, rc );
282 ldap_unbind_ext( ld, NULL, NULL );
287 do_read( char *uri, char *manager, struct berval *passwd, char *entry,
288 LDAP **ldp, int noattrs, int maxloop,
289 int maxretries, int delay, int force, int chaserefs )
291 LDAP *ld = ldp ? *ldp : NULL;
292 int i = 0, do_retry = maxretries;
293 char *attrs[] = { "1.1", NULL };
294 pid_t pid = getpid();
295 int rc = LDAP_SUCCESS;
296 int version = LDAP_VERSION3;
301 ldap_initialize( &ld, uri );
303 tester_perror( "ldap_initialize", NULL );
304 exit( EXIT_FAILURE );
307 (void) ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version );
308 (void) ldap_set_option( ld, LDAP_OPT_REFERRALS,
309 chaserefs ? LDAP_OPT_ON : LDAP_OPT_OFF );
311 if ( do_retry == maxretries ) {
312 fprintf( stderr, "PID=%ld - Read(%d): entry=\"%s\".\n",
313 (long) pid, maxloop, entry );
316 rc = ldap_sasl_bind_s( ld, manager, LDAP_SASL_SIMPLE, passwd, NULL, NULL, NULL );
317 if ( rc != LDAP_SUCCESS ) {
318 tester_ldap_error( ld, "ldap_sasl_bind_s", NULL );
321 case LDAP_UNAVAILABLE:
322 if ( do_retry > 0 ) {
323 ldap_unbind_ext( ld, NULL, NULL );
334 exit( EXIT_FAILURE );
338 for ( ; i < maxloop; i++ ) {
339 LDAPMessage *res = NULL;
341 rc = ldap_search_ext_s( ld, entry, LDAP_SCOPE_BASE,
342 NULL, attrs, noattrs, NULL, NULL, NULL,
343 LDAP_NO_LIMIT, &res );
346 /* don't log: it's intended */
353 tester_ldap_error( ld, "ldap_search_ext_s", NULL );
360 tester_ldap_error( ld, "ldap_search_ext_s", NULL );
361 if ( rc == LDAP_BUSY && do_retry > 0 ) {
365 if ( rc != LDAP_NO_SUCH_OBJECT ) {
381 fprintf( stderr, " PID=%ld - Read done (%d).\n", (long) pid, rc );
384 ldap_unbind_ext( ld, NULL, NULL );