]> git.sur5r.net Git - openldap/blob - tests/progs/slapd-read.c
initialize random seed; use high-order bits for better randomness
[openldap] / tests / progs / slapd-read.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1999-2006 The OpenLDAP Foundation.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted only as authorized by the OpenLDAP
9  * Public License.
10  *
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>.
14  */
15 /* ACKNOWLEDGEMENTS:
16  * This work was initially developed by Kurt Spanier for inclusion
17  * in OpenLDAP Software.
18  */
19
20 #include "portable.h"
21
22 #include <stdio.h>
23
24 #include <ac/stdlib.h>
25
26 #include <ac/ctype.h>
27 #include <ac/param.h>
28 #include <ac/socket.h>
29 #include <ac/string.h>
30 #include <ac/unistd.h>
31 #include <ac/wait.h>
32
33 #include <ldap.h>
34 #include <lutil.h>
35
36 #include "slapd-common.h"
37
38 #define LOOPS   100
39 #define RETRIES 0
40
41 static void
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 );
45
46 static void
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 );
50
51 static void
52 usage( char *name )
53 {
54         fprintf( stderr,
55                 "usage: %s "
56                 "-H <uri> | ([-h <host>] -p <port>) "
57                 "-D <manager> "
58                 "-w <passwd> "
59                 "-e <entry> "
60                 "[-A] "
61                 "[-C] "
62                 "[-F] "
63                 "[-f filter] "
64                 "[-i <ignore>] "
65                 "[-l <loops>] "
66                 "[-L <outerloops>] "
67                 "[-r <maxretries>] "
68                 "[-t <delay>]\n",
69                 name );
70         exit( EXIT_FAILURE );
71 }
72
73 int
74 main( int argc, char **argv )
75 {
76         int             i;
77         char            *uri = NULL;
78         char            *host = "localhost";
79         int             port = -1;
80         char            *manager = NULL;
81         struct berval   passwd = { 0, NULL };
82         char            *entry = NULL;
83         char            *filter  = NULL;
84         int             loops = LOOPS;
85         int             outerloops = 1;
86         int             retries = RETRIES;
87         int             delay = 0;
88         int             force = 0;
89         int             chaserefs = 0;
90         int             noattrs = 0;
91
92         tester_init( "slapd-read", TESTER_READ );
93
94         /* by default, tolerate referrals and no such object */
95         tester_ignore_str2errlist( "REFERRAL,NO_SUCH_OBJECT" );
96
97         while ( (i = getopt( argc, argv, "ACD:H:h:i:p:e:Ff:l:L:r:t:w:" )) != EOF ) {
98                 switch( i ) {
99                 case 'A':
100                         noattrs++;
101                         break;
102
103                 case 'C':
104                         chaserefs++;
105                         break;
106
107                 case 'H':               /* the server uri */
108                         uri = strdup( optarg );
109                         break;
110
111                 case 'h':               /* the servers host */
112                         host = strdup( optarg );
113                         break;
114
115                 case 'i':
116                         tester_ignore_str2errlist( optarg );
117                         break;
118
119                 case 'p':               /* the servers port */
120                         if ( lutil_atoi( &port, optarg ) != 0 ) {
121                                 usage( argv[0] );
122                         }
123                         break;
124
125                 case 'D':               /* the servers manager */
126                         manager = strdup( optarg );
127                         break;
128
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 );
133                         break;
134
135                 case 'e':               /* DN to search for */
136                         entry = strdup( optarg );
137                         break;
138
139                 case 'f':               /* the search request */
140                         filter = strdup( optarg );
141                         break;
142
143                 case 'F':
144                         force++;
145                         break;
146
147                 case 'l':               /* the number of loops */
148                         if ( lutil_atoi( &loops, optarg ) != 0 ) {
149                                 usage( argv[0] );
150                         }
151                         break;
152
153                 case 'L':               /* the number of outerloops */
154                         if ( lutil_atoi( &outerloops, optarg ) != 0 ) {
155                                 usage( argv[0] );
156                         }
157                         break;
158
159                 case 'r':               /* the number of retries */
160                         if ( lutil_atoi( &retries, optarg ) != 0 ) {
161                                 usage( argv[0] );
162                         }
163                         break;
164
165                 case 't':               /* delay in seconds */
166                         if ( lutil_atoi( &delay, optarg ) != 0 ) {
167                                 usage( argv[0] );
168                         }
169                         break;
170
171                 default:
172                         usage( argv[0] );
173                         break;
174                 }
175         }
176
177         if (( entry == NULL ) || ( port == -1 && uri == NULL ))
178                 usage( argv[0] );
179
180         if ( *entry == '\0' ) {
181                 fprintf( stderr, "%s: invalid EMPTY entry DN.\n",
182                                 argv[0] );
183                 exit( EXIT_FAILURE );
184         }
185
186         uri = tester_uri( uri, host, port );
187
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,
192                                 chaserefs );
193
194                 } else {
195                         do_read( uri, manager, &passwd, entry, NULL, noattrs,
196                                 loops, retries, delay, force, chaserefs );
197                 }
198         }
199
200         exit( EXIT_SUCCESS );
201 }
202
203 static void
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 )
207 {
208         LDAP    *ld = NULL;
209         int     i = 0, do_retry = maxretries;
210         char    *attrs[ 2 ];
211         pid_t   pid = getpid();
212         int     rc = LDAP_SUCCESS;
213         int     version = LDAP_VERSION3;
214         int     nvalues = 0;
215         char    **values = NULL;
216         LDAPMessage *res = NULL, *e = NULL;
217
218         srand( pid );
219
220         attrs[ 0 ] = LDAP_NO_ATTRS;
221         attrs[ 1 ] = NULL;
222
223         ldap_initialize( &ld, uri );
224         if ( ld == NULL ) {
225                 tester_perror( "ldap_initialize", NULL );
226                 exit( EXIT_FAILURE );
227         }
228
229         (void) ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version ); 
230         (void) ldap_set_option( ld, LDAP_OPT_REFERRALS,
231                 chaserefs ? LDAP_OPT_ON : LDAP_OPT_OFF );
232
233         if ( do_retry == maxretries ) {
234                 fprintf( stderr, "PID=%ld - Read(%d): base=\"%s\", filter=\"%s\".\n",
235                                 (long) pid, innerloop, sbase, filter );
236         }
237
238         rc = ldap_sasl_bind_s( ld, manager, LDAP_SASL_SIMPLE, passwd, NULL, NULL, NULL );
239         if ( rc != LDAP_SUCCESS ) {
240                 tester_ldap_error( ld, "ldap_sasl_bind_s", NULL );
241                 switch ( rc ) {
242                 case LDAP_BUSY:
243                 case LDAP_UNAVAILABLE:
244                 /* fallthru */
245                 default:
246                         break;
247                 }
248                 exit( EXIT_FAILURE );
249         }
250
251         rc = ldap_search_ext_s( ld, sbase, LDAP_SCOPE_SUBTREE,
252                 filter, attrs, 0, NULL, NULL, NULL, LDAP_NO_LIMIT, &res );
253         switch ( rc ) {
254         case LDAP_SIZELIMIT_EXCEEDED:
255         case LDAP_TIMELIMIT_EXCEEDED:
256         case LDAP_SUCCESS:
257                 nvalues = ldap_count_entries( ld, res );
258                 if ( nvalues == 0 ) {
259                         if ( rc ) {
260                                 tester_ldap_error( ld, "ldap_search_ext_s", NULL );
261                         }
262                         break;
263                 }
264
265                 values = malloc( ( nvalues + 1 ) * sizeof( char * ) );
266                 for ( i = 0, e = ldap_first_entry( ld, res ); e != NULL; i++, e = ldap_next_entry( ld, e ) )
267                 {
268                         values[ i ] = ldap_get_dn( ld, e );
269                 }
270                 values[ i ] = NULL;
271
272                 ldap_msgfree( res );
273
274                 if ( do_retry == maxretries ) {
275                         fprintf( stderr, "  PID=%ld - Read base=\"%s\" filter=\"%s\" got %d values.\n",
276                                 (long) pid, sbase, filter, nvalues );
277                 }
278
279                 for ( i = 0; i < innerloop; i++ ) {
280 #if 0   /* use high-order bits for better randomness (Numerical Recipes in "C") */
281                         int     r = rand() % nvalues;
282 #endif
283                         int     r = ((double)nvalues)*rand()/(RAND_MAX + 1.0);
284
285                         do_read( uri, manager, passwd, values[ r ], &ld,
286                                 noattrs, 1, maxretries, delay, force,
287                                 chaserefs );
288                 }
289                 break;
290
291         default:
292                 tester_ldap_error( ld, "ldap_search_ext_s", NULL );
293                 break;
294         }
295
296         fprintf( stderr, " PID=%ld - Search done (%d).\n", (long) pid, rc );
297
298         if ( ld != NULL ) {
299                 ldap_unbind_ext( ld, NULL, NULL );
300         }
301 }
302
303 static void
304 do_read( char *uri, char *manager, struct berval *passwd, char *entry,
305         LDAP **ldp, int noattrs, int maxloop,
306         int maxretries, int delay, int force, int chaserefs )
307 {
308         LDAP    *ld = ldp ? *ldp : NULL;
309         int     i = 0, do_retry = maxretries;
310         char    *attrs[] = { "1.1", NULL };
311         pid_t   pid = getpid();
312         int     rc = LDAP_SUCCESS;
313         int     version = LDAP_VERSION3;
314
315 retry:;
316         if ( ld == NULL ) {
317                 ldap_initialize( &ld, uri );
318                 if ( ld == NULL ) {
319                         tester_perror( "ldap_initialize", NULL );
320                         exit( EXIT_FAILURE );
321                 }
322
323                 (void) ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version ); 
324                 (void) ldap_set_option( ld, LDAP_OPT_REFERRALS,
325                         chaserefs ? LDAP_OPT_ON : LDAP_OPT_OFF );
326
327                 if ( do_retry == maxretries ) {
328                         fprintf( stderr, "PID=%ld - Read(%d): entry=\"%s\".\n",
329                                 (long) pid, maxloop, entry );
330                 }
331
332                 rc = ldap_sasl_bind_s( ld, manager, LDAP_SASL_SIMPLE, passwd, NULL, NULL, NULL );
333                 if ( rc != LDAP_SUCCESS ) {
334                         tester_ldap_error( ld, "ldap_sasl_bind_s", NULL );
335                         switch ( rc ) {
336                         case LDAP_BUSY:
337                         case LDAP_UNAVAILABLE:
338                                 if ( do_retry > 0 ) {
339                                         ldap_unbind_ext( ld, NULL, NULL );
340                                         do_retry--;
341                                         if ( delay != 0 ) {
342                                             sleep( delay );
343                                         }
344                                         goto retry;
345                                 }
346                         /* fallthru */
347                         default:
348                                 break;
349                         }
350                         exit( EXIT_FAILURE );
351                 }
352         }
353
354         for ( ; i < maxloop; i++ ) {
355                 LDAPMessage *res = NULL;
356
357                 rc = ldap_search_ext_s( ld, entry, LDAP_SCOPE_BASE,
358                                 NULL, attrs, noattrs, NULL, NULL, NULL,
359                                 LDAP_NO_LIMIT, &res );
360                 if ( res != NULL ) {
361                         ldap_msgfree( res );
362                 }
363
364                 if ( rc ) {
365                         unsigned        first = tester_ignore_err( rc );
366                         char            buf[ BUFSIZ ];
367
368                         snprintf( buf, sizeof( buf ), "ldap_search_ext_s(%s)", entry );
369
370                         /* if ignore.. */
371                         if ( first ) {
372                                 /* only log if first occurrence */
373                                 if ( force < 2 || first == 1 ) {
374                                         tester_ldap_error( ld, buf, NULL );
375                                 }
376                                 continue;
377                         }
378
379                         /* busy needs special handling */
380                         tester_ldap_error( ld, buf, NULL );
381                         if ( rc == LDAP_BUSY && do_retry > 0 ) {
382                                 ldap_unbind_ext( ld, NULL, NULL );
383                                 ld = NULL;
384                                 do_retry--;
385                                 goto retry;
386                         }
387                         break;
388                 }
389         }
390
391         if ( ldp != NULL ) {
392                 *ldp = ld;
393
394         } else {
395                 fprintf( stderr, " PID=%ld - Read done (%d).\n", (long) pid, rc );
396
397                 if ( ld != NULL ) {
398                         ldap_unbind_ext( ld, NULL, NULL );
399                 }
400         }
401 }
402