]> git.sur5r.net Git - openldap/blobdiff - tests/progs/slapd-search.c
Add missing revision string
[openldap] / tests / progs / slapd-search.c
index dfd97ad2299eae1b28aa30847612a7710a7451d8..367732528b91957c0d2be8ea1236acc6a0051ff2 100644 (file)
@@ -1,7 +1,7 @@
 /* $OpenLDAP$ */
 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  *
- * Copyright 1999-2005 The OpenLDAP Foundation.
+ * Copyright 1999-2008 The OpenLDAP Foundation.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
 
 #include <stdio.h>
 
-#include <ac/stdlib.h>
+#include "ac/stdlib.h"
 
-#include <ac/ctype.h>
-#include <ac/param.h>
-#include <ac/socket.h>
-#include <ac/string.h>
-#include <ac/unistd.h>
-#include <ac/wait.h>
+#include "ac/ctype.h"
+#include "ac/param.h"
+#include "ac/socket.h"
+#include "ac/string.h"
+#include "ac/unistd.h"
+#include "ac/wait.h"
 
-#define LDAP_DEPRECATED 1
-#include <ldap.h>
+#include "ldap.h"
+#include "lutil.h"
+#include "ldap_pvt.h"
+
+#include "slapd-common.h"
 
 #define LOOPS  100
 #define RETRIES        0
 
 static void
-do_search( char *uri, char *host, int port, char *manager, char *passwd,
-               char *sbase, char *filter, int maxloop, int maxretries );
+do_search( char *uri, char *manager, struct berval *passwd,
+       char *sbase, int scope, char *filter, LDAP **ldp,
+       char **attrs, int noattrs, int nobind,
+       int innerloop, int maxretries, int delay, int force, int chaserefs );
+
+static void
+do_random( char *uri, char *manager, struct berval *passwd,
+       char *sbase, int scope, char *filter, char *attr,
+       char **attrs, int noattrs, int nobind,
+       int innerloop, int maxretries, int delay, int force, int chaserefs );
 
 static void
-usage( char *name )
+usage( char *name, char o )
 {
-       fprintf( stderr, "usage: %s [-h <host>] -p port -b <searchbase> -f <searchfiter> [-l <loops>]\n",
+       if ( o != '\0' ) {
+               fprintf( stderr, "unknown/incorrect option \"%c\"\n", o );
+       }
+
+        fprintf( stderr,
+               "usage: %s "
+               "-H <uri> | ([-h <host>] -p <port>) "
+               "-D <manager> "
+               "-w <passwd> "
+               "-b <searchbase> "
+               "-s <scope> "
+               "-f <searchfilter> "
+               "[-a <attr>] "
+               "[-A] "
+               "[-C] "
+               "[-F] "
+               "[-N] "
+               "[-i <ignore>] "
+               "[-l <loops>] "
+               "[-L <outerloops>] "
+               "[-r <maxretries>] "
+               "[-t <delay>] "
+               "[<attrs>] "
+               "\n",
                        name );
        exit( EXIT_FAILURE );
 }
@@ -56,14 +90,38 @@ main( int argc, char **argv )
        char            *host = "localhost";
        int             port = -1;
        char            *manager = NULL;
-       char            *passwd = NULL;
+       struct berval   passwd = { 0, NULL };
        char            *sbase = NULL;
+       int             scope = LDAP_SCOPE_SUBTREE;
        char            *filter  = NULL;
+       char            *attr = NULL;
+       char            *srchattrs[] = { "cn", "sn", NULL };
+       char            **attrs = srchattrs;
        int             loops = LOOPS;
+       int             outerloops = 1;
        int             retries = RETRIES;
+       int             delay = 0;
+       int             force = 0;
+       int             chaserefs = 0;
+       int             noattrs = 0;
+       int             nobind = 0;
+
+       tester_init( "slapd-search", TESTER_SEARCH );
+
+       /* by default, tolerate referrals and no such object */
+       tester_ignore_str2errlist( "REFERRAL,NO_SUCH_OBJECT" );
+
+       while ( ( i = getopt( argc, argv, "Aa:b:CD:f:FH:h:i:l:L:Np:r:s:t:T:w:" ) ) != EOF )
+       {
+               switch ( i ) {
+               case 'A':
+                       noattrs++;
+                       break;
+
+               case 'C':
+                       chaserefs++;
+                       break;
 
-       while ( (i = getopt( argc, argv, "b:D:f:H:h:l:p:w:r:" )) != EOF ) {
-               switch( i ) {
                case 'H':               /* the server uri */
                        uri = strdup( optarg );
                        break;
@@ -72,8 +130,18 @@ main( int argc, char **argv )
                        host = strdup( optarg );
                        break;
 
+               case 'i':
+                       tester_ignore_str2errlist( optarg );
+                       break;
+
+               case 'N':
+                       nobind++;
+                       break;
+
                case 'p':               /* the servers port */
-                       port = atoi( optarg );
+                       if ( lutil_atoi( &port, optarg ) != 0 ) {
+                               usage( argv[0], i );
+                       }
                        break;
 
                case 'D':               /* the servers manager */
@@ -81,7 +149,13 @@ main( int argc, char **argv )
                        break;
 
                case 'w':               /* the server managers password */
-                       passwd = strdup( optarg );
+                       passwd.bv_val = strdup( optarg );
+                       passwd.bv_len = strlen( optarg );
+                       memset( optarg, '*', passwd.bv_len );
+                       break;
+
+               case 'a':
+                       attr = strdup( optarg );
                        break;
 
                case 'b':               /* file with search base */
@@ -92,22 +166,56 @@ main( int argc, char **argv )
                        filter = strdup( optarg );
                        break;
 
+               case 'F':
+                       force++;
+                       break;
+
                case 'l':               /* number of loops */
-                       loops = atoi( optarg );
+                       if ( lutil_atoi( &loops, optarg ) != 0 ) {
+                               usage( argv[0], i );
+                       }
+                       break;
+
+               case 'L':               /* number of loops */
+                       if ( lutil_atoi( &outerloops, optarg ) != 0 ) {
+                               usage( argv[0], i );
+                       }
                        break;
 
                case 'r':               /* number of retries */
-                       retries = atoi( optarg );
+                       if ( lutil_atoi( &retries, optarg ) != 0 ) {
+                               usage( argv[0], i );
+                       }
+                       break;
+
+               case 't':               /* delay in seconds */
+                       if ( lutil_atoi( &delay, optarg ) != 0 ) {
+                               usage( argv[0], i );
+                       }
+                       break;
+
+               case 'T':
+                       attrs = ldap_str2charray( optarg, "," );
+                       if ( attrs == NULL ) {
+                               usage( argv[0], i );
+                       }
+                       break;
+
+               case 's':
+                       scope = ldap_pvt_str2scope( optarg );
+                       if ( scope == -1 ) {
+                               usage( argv[0], i );
+                       }
                        break;
 
                default:
-                       usage( argv[0] );
+                       usage( argv[0], i );
                        break;
                }
        }
 
        if (( sbase == NULL ) || ( filter == NULL ) || ( port == -1 && uri == NULL ))
-               usage( argv[0] );
+               usage( argv[0], '\0' );
 
        if ( *filter == '\0' ) {
 
@@ -117,76 +225,257 @@ main( int argc, char **argv )
 
        }
 
-       do_search( uri, host, port, manager, passwd, sbase, filter,
-                       ( 10 * loops ), retries );
+       if ( argv[optind] != NULL ) {
+               attrs = &argv[optind];
+       }
+
+       uri = tester_uri( uri, host, port );
+
+       for ( i = 0; i < outerloops; i++ ) {
+               if ( attr != NULL ) {
+                       do_random( uri, manager, &passwd,
+                               sbase, scope, filter, attr,
+                               attrs, noattrs, nobind,
+                               loops, retries, delay, force, chaserefs );
+
+               } else {
+                       do_search( uri, manager, &passwd,
+                               sbase, scope, filter, NULL,
+                               attrs, noattrs, nobind,
+                               loops, retries, delay, force, chaserefs );
+               }
+       }
+
        exit( EXIT_SUCCESS );
 }
 
 
 static void
-do_search( char *uri, char *host, int port, char *manager, char *passwd,
-               char *sbase, char *filter, int maxloop, int maxretries )
+do_random( char *uri, char *manager, struct berval *passwd,
+       char *sbase, int scope, char *filter, char *attr,
+       char **srchattrs, int noattrs, int nobind,
+       int innerloop, int maxretries, int delay, int force, int chaserefs )
 {
        LDAP    *ld = NULL;
        int     i = 0, do_retry = maxretries;
-       char    *attrs[] = { "cn", "sn", NULL };
-       pid_t   pid = getpid();
+       char    *attrs[ 2 ];
        int     rc = LDAP_SUCCESS;
+       int     version = LDAP_VERSION3;
+       int     nvalues = 0;
+       char    **values = NULL;
+       LDAPMessage *res = NULL, *e = NULL;
 
-retry:;
-       if ( uri ) {
-               ldap_initialize( &ld, uri );
-       } else {
-               ld = ldap_init( host, port );
-       }
+       attrs[ 0 ] = attr;
+       attrs[ 1 ] = NULL;
+
+       ldap_initialize( &ld, uri );
        if ( ld == NULL ) {
-               perror( "ldap_init" );
+               tester_perror( "ldap_initialize", NULL );
                exit( EXIT_FAILURE );
        }
 
-       {
-               int version = LDAP_VERSION3;
-               (void) ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION,
-                       &version ); 
-       }
+       (void) ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version ); 
+       (void) ldap_set_option( ld, LDAP_OPT_REFERRALS,
+               chaserefs ? LDAP_OPT_ON : LDAP_OPT_OFF );
 
        if ( do_retry == maxretries ) {
-               fprintf( stderr, "PID=%ld - Search(%d): base=\"%s\", filter=\"%s\".\n",
-                               (long) pid, maxloop, sbase, filter );
+               fprintf( stderr, "PID=%ld - Search(%d): base=\"%s\", filter=\"%s\" attr=\"%s\".\n",
+                               (long) pid, innerloop, sbase, filter, attr );
        }
 
-       rc = ldap_bind_s( ld, manager, passwd, LDAP_AUTH_SIMPLE );
-       if ( rc != LDAP_SUCCESS ) {
-               if ( rc == LDAP_BUSY && do_retry == 1 ) {
-                       do_retry = 0;
-                       goto retry;
+       if ( nobind == 0 ) {
+               rc = ldap_sasl_bind_s( ld, manager, LDAP_SASL_SIMPLE, passwd, NULL, NULL, NULL );
+               if ( rc != LDAP_SUCCESS ) {
+                       tester_ldap_error( ld, "ldap_sasl_bind_s", NULL );
+                       switch ( rc ) {
+                       case LDAP_BUSY:
+                       case LDAP_UNAVAILABLE:
+                       /* fallthru */
+                       default:
+                               break;
+                       }
+                       exit( EXIT_FAILURE );
                }
-               ldap_perror( ld, "ldap_bind" );
-               exit( EXIT_FAILURE );
        }
 
-       for ( i = 0; i < maxloop; i++ ) {
-               LDAPMessage *res;
-
-               rc = ldap_search_s( ld, sbase, LDAP_SCOPE_SUBTREE,
-                               filter, attrs, 0, &res );
-               if ( rc != LDAP_SUCCESS ) {
-                       ldap_perror( ld, "ldap_search" );
-                       if ( rc == LDAP_BUSY && do_retry == 1 ) {
-                               do_retry = 0;
-                               goto retry;
+       rc = ldap_search_ext_s( ld, sbase, LDAP_SCOPE_SUBTREE,
+               filter, attrs, 0, NULL, NULL, NULL, LDAP_NO_LIMIT, &res );
+       switch ( rc ) {
+       case LDAP_SIZELIMIT_EXCEEDED:
+       case LDAP_TIMELIMIT_EXCEEDED:
+       case LDAP_SUCCESS:
+               if ( ldap_count_entries( ld, res ) == 0 ) {
+                       if ( rc ) {
+                               tester_ldap_error( ld, "ldap_search_ext_s", NULL );
                        }
-                       if ( rc != LDAP_NO_SUCH_OBJECT ) break;
-                       continue;
+                       break;
+               }
 
+               for ( e = ldap_first_entry( ld, res ); e != NULL; e = ldap_next_entry( ld, e ) )
+               {
+                       struct berval **v = ldap_get_values_len( ld, e, attr );
+
+                       if ( v != NULL ) {
+                               int n = ldap_count_values_len( v );
+                               int j;
+
+                               values = realloc( values, ( nvalues + n + 1 )*sizeof( char * ) );
+                               for ( j = 0; j < n; j++ ) {
+                                       values[ nvalues + j ] = strdup( v[ j ]->bv_val );
+                               }
+                               values[ nvalues + j ] = NULL;
+                               nvalues += n;
+                               ldap_value_free_len( v );
+                       }
                }
 
                ldap_msgfree( res );
+
+               if ( !values ) {
+                       fprintf( stderr, "  PID=%ld - Search base=\"%s\" filter=\"%s\" got %d values.\n",
+                               (long) pid, sbase, filter, nvalues );
+                       exit(EXIT_FAILURE);
+               }
+
+               if ( do_retry == maxretries ) {
+                       fprintf( stderr, "  PID=%ld - Search base=\"%s\" filter=\"%s\" got %d values.\n",
+                               (long) pid, sbase, filter, nvalues );
+               }
+
+               for ( i = 0; i < innerloop; i++ ) {
+                       char    buf[ BUFSIZ ];
+#if 0  /* use high-order bits for better randomness (Numerical Recipes in "C") */
+                       int     r = rand() % nvalues;
+#endif
+                       int     r = ((double)nvalues)*rand()/(RAND_MAX + 1.0);
+
+                       snprintf( buf, sizeof( buf ), "(%s=%s)", attr, values[ r ] );
+
+                       do_search( uri, manager, passwd,
+                               sbase, scope, buf, &ld,
+                               srchattrs, noattrs, nobind,
+                               1, maxretries, delay, force, chaserefs );
+               }
+               break;
+
+       default:
+               tester_ldap_error( ld, "ldap_search_ext_s", NULL );
+               break;
        }
 
-       fprintf( stderr, " PID=%ld - Search done (%d).\n", (long) pid, rc );
+       fprintf( stderr, "  PID=%ld - Search done (%d).\n", (long) pid, rc );
 
-       ldap_unbind( ld );
+       if ( ld != NULL ) {
+               ldap_unbind_ext( ld, NULL, NULL );
+       }
 }
 
+static void
+do_search( char *uri, char *manager, struct berval *passwd,
+       char *sbase, int scope, char *filter, LDAP **ldp,
+       char **attrs, int noattrs, int nobind,
+       int innerloop, int maxretries, int delay, int force, int chaserefs )
+{
+       LDAP    *ld = ldp ? *ldp : NULL;
+       int     i = 0, do_retry = maxretries;
+       int     rc = LDAP_SUCCESS;
+       int     version = LDAP_VERSION3;
+       char    buf[ BUFSIZ ];
+
+
+retry:;
+       if ( ld == NULL ) {
+               ldap_initialize( &ld, uri );
+               if ( ld == NULL ) {
+                       tester_perror( "ldap_initialize", NULL );
+                       exit( EXIT_FAILURE );
+               }
+
+               (void) ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version ); 
+               (void) ldap_set_option( ld, LDAP_OPT_REFERRALS,
+                       chaserefs ? LDAP_OPT_ON : LDAP_OPT_OFF );
+
+               if ( do_retry == maxretries ) {
+                       fprintf( stderr,
+                               "PID=%ld - Search(%d): "
+                               "base=\"%s\" scope=%s filter=\"%s\" "
+                               "attrs=%s%s.\n",
+                               (long) pid, innerloop,
+                               sbase, ldap_pvt_scope2str( scope ), filter,
+                               attrs[0], attrs[1] ? " (more...)" : "" );
+               }
+
+               if ( nobind == 0 ) {
+                       rc = ldap_sasl_bind_s( ld, manager, LDAP_SASL_SIMPLE, passwd, NULL, NULL, NULL );
+                       if ( rc != LDAP_SUCCESS ) {
+                               snprintf( buf, sizeof( buf ),
+                                       "bindDN=\"%s\"", manager );
+                               tester_ldap_error( ld, "ldap_sasl_bind_s", buf );
+                               switch ( rc ) {
+                               case LDAP_BUSY:
+                               case LDAP_UNAVAILABLE:
+                                       if ( do_retry > 0 ) {
+                                               ldap_unbind_ext( ld, NULL, NULL );
+                                               ld = NULL;
+                                               do_retry--;
+                                               if ( delay != 0 ) {
+                                                   sleep( delay );
+                                               }
+                                               goto retry;
+                                       }
+                               /* fallthru */
+                               default:
+                                       break;
+                               }
+                               exit( EXIT_FAILURE );
+                       }
+               }
+       }
+
+       for ( ; i < innerloop; i++ ) {
+               LDAPMessage *res = NULL;
+
+               rc = ldap_search_ext_s( ld, sbase, scope,
+                               filter, attrs, noattrs, NULL, NULL,
+                               NULL, LDAP_NO_LIMIT, &res );
+               if ( res != NULL ) {
+                       ldap_msgfree( res );
+               }
+
+               if ( rc ) {
+                       unsigned first = tester_ignore_err( rc );
+                       /* if ignore.. */
+                       if ( first ) {
+                               /* only log if first occurrence */
+                               if ( force < 2 || first == 1 ) {
+                                       tester_ldap_error( ld, "ldap_search_ext_s", NULL );
+                               }
+                               continue;
+                       }
 
+                       /* busy needs special handling */
+                       snprintf( buf, sizeof( buf ),
+                               "base=\"%s\" filter=\"%s\"\n",
+                               sbase, filter );
+                       tester_ldap_error( ld, "ldap_search_ext_s", buf );
+                       if ( rc == LDAP_BUSY && do_retry > 0 ) {
+                               ldap_unbind_ext( ld, NULL, NULL );
+                               ld = NULL;
+                               do_retry--;
+                               goto retry;
+                       }
+                       break;
+               }
+       }
+
+       if ( ldp != NULL ) {
+               *ldp = ld;
+
+       } else {
+               fprintf( stderr, "  PID=%ld - Search done (%d).\n", (long) pid, rc );
+
+               if ( ld != NULL ) {
+                       ldap_unbind_ext( ld, NULL, NULL );
+               }
+       }
+}