]> git.sur5r.net Git - openldap/blob - tests/progs/slapd-read.c
cleanup testers
[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                 "[-l <loops>] "
65                 "[-L <outerloops>] "
66                 "[-r <maxretries>] "
67                 "[-t <delay>]\n",
68                 name );
69         exit( EXIT_FAILURE );
70 }
71
72 int
73 main( int argc, char **argv )
74 {
75         int             i;
76         char            *uri = NULL;
77         char            *host = "localhost";
78         int             port = -1;
79         char            *manager = NULL;
80         struct berval   passwd = { 0, NULL };
81         char            *entry = NULL;
82         char            *filter  = NULL;
83         int             loops = LOOPS;
84         int             outerloops = 1;
85         int             retries = RETRIES;
86         int             delay = 0;
87         int             force = 0;
88         int             chaserefs = 0;
89         int             noattrs = 0;
90
91         tester_init( "slapd-read" );
92
93         while ( (i = getopt( argc, argv, "ACD:H:h:p:e:Ff:l:L:r:t:w:" )) != EOF ) {
94                 switch( i ) {
95                 case 'A':
96                         noattrs++;
97                         break;
98
99                 case 'C':
100                         chaserefs++;
101                         break;
102
103                 case 'H':               /* the server uri */
104                         uri = strdup( optarg );
105                         break;
106
107                 case 'h':               /* the servers host */
108                         host = strdup( optarg );
109                         break;
110
111                 case 'p':               /* the servers port */
112                         if ( lutil_atoi( &port, optarg ) != 0 ) {
113                                 usage( argv[0] );
114                         }
115                         break;
116
117                 case 'D':               /* the servers manager */
118                         manager = strdup( optarg );
119                         break;
120
121                 case 'w':               /* the server managers password */
122                         passwd.bv_val = strdup( optarg );
123                         passwd.bv_len = strlen( optarg );
124                         break;
125
126                 case 'e':               /* DN to search for */
127                         entry = strdup( optarg );
128                         break;
129
130                 case 'f':               /* the search request */
131                         filter = strdup( optarg );
132                         break;
133
134                 case 'F':
135                         force++;
136                         break;
137
138                 case 'l':               /* the number of loops */
139                         if ( lutil_atoi( &loops, optarg ) != 0 ) {
140                                 usage( argv[0] );
141                         }
142                         break;
143
144                 case 'L':               /* the number of outerloops */
145                         if ( lutil_atoi( &outerloops, optarg ) != 0 ) {
146                                 usage( argv[0] );
147                         }
148                         break;
149
150                 case 'r':               /* the number of retries */
151                         if ( lutil_atoi( &retries, optarg ) != 0 ) {
152                                 usage( argv[0] );
153                         }
154                         break;
155
156                 case 't':               /* delay in seconds */
157                         if ( lutil_atoi( &delay, optarg ) != 0 ) {
158                                 usage( argv[0] );
159                         }
160                         break;
161
162                 default:
163                         usage( argv[0] );
164                         break;
165                 }
166         }
167
168         if (( entry == NULL ) || ( port == -1 && uri == NULL ))
169                 usage( argv[0] );
170
171         if ( *entry == '\0' ) {
172                 fprintf( stderr, "%s: invalid EMPTY entry DN.\n",
173                                 argv[0] );
174                 exit( EXIT_FAILURE );
175         }
176
177         uri = tester_uri( uri, host, port );
178
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,
183                                 chaserefs );
184
185                 } else {
186                         do_read( uri, manager, &passwd, entry, NULL, noattrs,
187                                 loops, retries, delay, force, chaserefs );
188                 }
189         }
190
191         exit( EXIT_SUCCESS );
192 }
193
194 static void
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 )
198 {
199         LDAP    *ld = NULL;
200         int     i = 0, do_retry = maxretries;
201         char    *attrs[ 2 ];
202         pid_t   pid = getpid();
203         int     rc = LDAP_SUCCESS;
204         int     version = LDAP_VERSION3;
205         int     nvalues = 0;
206         char    **values = NULL;
207         LDAPMessage *res = NULL, *e = NULL;
208
209         attrs[ 0 ] = LDAP_NO_ATTRS;
210         attrs[ 1 ] = NULL;
211
212         ldap_initialize( &ld, uri );
213         if ( ld == NULL ) {
214                 tester_perror( "ldap_initialize", NULL );
215                 exit( EXIT_FAILURE );
216         }
217
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 );
221
222         if ( do_retry == maxretries ) {
223                 fprintf( stderr, "PID=%ld - Read(%d): base=\"%s\", filter=\"%s\".\n",
224                                 (long) pid, innerloop, sbase, filter );
225         }
226
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 );
230                 switch ( rc ) {
231                 case LDAP_BUSY:
232                 case LDAP_UNAVAILABLE:
233                 /* fallthru */
234                 default:
235                         break;
236                 }
237                 exit( EXIT_FAILURE );
238         }
239
240         rc = ldap_search_ext_s( ld, sbase, LDAP_SCOPE_SUBTREE,
241                 filter, attrs, 0, NULL, NULL, NULL, LDAP_NO_LIMIT, &res );
242         switch ( rc ) {
243         case LDAP_SIZELIMIT_EXCEEDED:
244         case LDAP_TIMELIMIT_EXCEEDED:
245         case LDAP_SUCCESS:
246                 nvalues = ldap_count_entries( ld, res );
247                 if ( nvalues == 0 ) {
248                         if ( rc ) {
249                                 tester_ldap_error( ld, "ldap_search_ext_s", NULL );
250                         }
251                         break;
252                 }
253
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 ) )
256                 {
257                         values[ i ] = ldap_get_dn( ld, e );
258                 }
259                 values[ i ] = NULL;
260
261                 ldap_msgfree( res );
262
263                 if ( do_retry == maxretries ) {
264                         fprintf( stderr, "  PID=%ld - got %d values.\n", (long) pid, nvalues );
265                 }
266
267                 for ( i = 0; i < innerloop; i++ ) {
268                         do_read( uri, manager, passwd, values[ rand() % nvalues ], &ld,
269                                 noattrs, 1, maxretries, delay, force,
270                                 chaserefs );
271                 }
272                 break;
273
274         default:
275                 tester_ldap_error( ld, "ldap_search_ext_s", NULL );
276                 break;
277         }
278
279         fprintf( stderr, " PID=%ld - Search done (%d).\n", (long) pid, rc );
280
281         if ( ld != NULL ) {
282                 ldap_unbind_ext( ld, NULL, NULL );
283         }
284 }
285
286 static void
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 )
290 {
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;
297         int     first = 1;
298
299 retry:;
300         if ( ld == NULL ) {
301                 ldap_initialize( &ld, uri );
302                 if ( ld == NULL ) {
303                         tester_perror( "ldap_initialize", NULL );
304                         exit( EXIT_FAILURE );
305                 }
306
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 );
310
311                 if ( do_retry == maxretries ) {
312                         fprintf( stderr, "PID=%ld - Read(%d): entry=\"%s\".\n",
313                                 (long) pid, maxloop, entry );
314                 }
315
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 );
319                         switch ( rc ) {
320                         case LDAP_BUSY:
321                         case LDAP_UNAVAILABLE:
322                                 if ( do_retry > 0 ) {
323                                         ldap_unbind_ext( ld, NULL, NULL );
324                                         do_retry--;
325                                         if ( delay != 0 ) {
326                                             sleep( delay );
327                                         }
328                                         goto retry;
329                                 }
330                         /* fallthru */
331                         default:
332                                 break;
333                         }
334                         exit( EXIT_FAILURE );
335                 }
336         }
337
338         for ( ; i < maxloop; i++ ) {
339                 LDAPMessage *res = NULL;
340
341                 rc = ldap_search_ext_s( ld, entry, LDAP_SCOPE_BASE,
342                                 NULL, attrs, noattrs, NULL, NULL, NULL,
343                                 LDAP_NO_LIMIT, &res );
344                 switch ( rc ) {
345                 case LDAP_REFERRAL:
346                         /* don't log: it's intended */
347                         if ( force >= 2 ) {
348                                 if ( !first ) {
349                                         break;
350                                 }
351                                 first = 0;
352                         }
353                         tester_ldap_error( ld, "ldap_search_ext_s", NULL );
354                         /* fallthru */
355
356                 case LDAP_SUCCESS:
357                         break;
358
359                 default:
360                         tester_ldap_error( ld, "ldap_search_ext_s", NULL );
361                         if ( rc == LDAP_BUSY && do_retry > 0 ) {
362                                 do_retry--;
363                                 goto retry;
364                         }
365                         if ( rc != LDAP_NO_SUCH_OBJECT ) {
366                                 goto done;
367                         }
368                         break;
369                 }
370
371                 if ( res != NULL ) {
372                         ldap_msgfree( res );
373                 }
374         }
375
376 done:;
377         if ( ldp != NULL ) {
378                 *ldp = ld;
379
380         } else {
381                 fprintf( stderr, " PID=%ld - Read done (%d).\n", (long) pid, rc );
382
383                 if ( ld != NULL ) {
384                         ldap_unbind_ext( ld, NULL, NULL );
385                 }
386         }
387 }
388