]> git.sur5r.net Git - openldap/blobdiff - tests/progs/slapd-search.c
Add missing revision string
[openldap] / tests / progs / slapd-search.c
index 3d330ac204e4474343cbaaa8668c7bb97809c7f4..367732528b91957c0d2be8ea1236acc6a0051ff2 100644 (file)
@@ -1,7 +1,7 @@
 /* $OpenLDAP$ */
 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  *
- * Copyright 1999-2006 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"
 
-#include <ldap.h>
-#include <lutil.h>
+#include "ldap.h"
+#include "lutil.h"
+#include "ldap_pvt.h"
 
 #include "slapd-common.h"
 
 
 static void
 do_search( char *uri, char *manager, struct berval *passwd,
-       char *sbase, char *filter, LDAP **ldp,
-       int innerloop, int maxretries, int delay, int force );
+       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, char *filter, char *attr, int innerloop,
-       int maxretries, int delay, int force );
+       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 )
 {
+       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>]\n",
+               "[-t <delay>] "
+               "[<attrs>] "
+               "\n",
                        name );
        exit( EXIT_FAILURE );
 }
@@ -78,18 +92,36 @@ main( int argc, char **argv )
        char            *manager = 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" );
 
-       tester_init( "slapd-search" );
+       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, "a:b:D:f:FH:h:l:L:p:w:r:t:" )) != EOF ) {
-               switch( i ) {
                case 'H':               /* the server uri */
                        uri = strdup( optarg );
                        break;
@@ -98,9 +130,17 @@ 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 */
                        if ( lutil_atoi( &port, optarg ) != 0 ) {
-                               usage( argv[0] );
+                               usage( argv[0], i );
                        }
                        break;
 
@@ -111,6 +151,7 @@ main( int argc, char **argv )
                case 'w':               /* the server managers password */
                        passwd.bv_val = strdup( optarg );
                        passwd.bv_len = strlen( optarg );
+                       memset( optarg, '*', passwd.bv_len );
                        break;
 
                case 'a':
@@ -131,36 +172,50 @@ main( int argc, char **argv )
 
                case 'l':               /* number of loops */
                        if ( lutil_atoi( &loops, optarg ) != 0 ) {
-                               usage( argv[0] );
+                               usage( argv[0], i );
                        }
                        break;
 
                case 'L':               /* number of loops */
                        if ( lutil_atoi( &outerloops, optarg ) != 0 ) {
-                               usage( argv[0] );
+                               usage( argv[0], i );
                        }
                        break;
 
                case 'r':               /* number of retries */
                        if ( lutil_atoi( &retries, optarg ) != 0 ) {
-                               usage( argv[0] );
+                               usage( argv[0], i );
                        }
                        break;
 
                case 't':               /* delay in seconds */
                        if ( lutil_atoi( &delay, optarg ) != 0 ) {
-                               usage( argv[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' ) {
 
@@ -170,16 +225,24 @@ main( int argc, char **argv )
 
        }
 
+       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, filter, attr,
-                                       loops, retries, delay, force );
+                       do_random( uri, manager, &passwd,
+                               sbase, scope, filter, attr,
+                               attrs, noattrs, nobind,
+                               loops, retries, delay, force, chaserefs );
 
                } else {
-                       do_search( uri, manager, &passwd, sbase, filter, NULL,
-                                       loops, retries, delay, force );
+                       do_search( uri, manager, &passwd,
+                               sbase, scope, filter, NULL,
+                               attrs, noattrs, nobind,
+                               loops, retries, delay, force, chaserefs );
                }
        }
 
@@ -189,56 +252,65 @@ main( int argc, char **argv )
 
 static void
 do_random( char *uri, char *manager, struct berval *passwd,
-       char *sbase, char *filter, char *attr,
-       int innerloop, int maxretries, int delay, int force )
+       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[ 2 ];
-       pid_t   pid = getpid();
        int     rc = LDAP_SUCCESS;
        int     version = LDAP_VERSION3;
        int     nvalues = 0;
        char    **values = NULL;
-       LDAPMessage *res = NULL;
+       LDAPMessage *res = NULL, *e = NULL;
 
        attrs[ 0 ] = attr;
        attrs[ 1 ] = NULL;
 
        ldap_initialize( &ld, uri );
        if ( ld == NULL ) {
-               tester_perror( "ldap_initialize" );
+               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, LDAP_OPT_OFF );
+       (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\" attr=\"%s\".\n",
                                (long) pid, innerloop, sbase, filter, attr );
        }
 
-       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" );
-               switch ( rc ) {
-               case LDAP_BUSY:
-               case LDAP_UNAVAILABLE:
-               /* fallthru */
-               default:
-                       break;
+       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 );
                }
-               exit( EXIT_FAILURE );
        }
 
        rc = ldap_search_ext_s( ld, sbase, LDAP_SCOPE_SUBTREE,
                filter, attrs, 0, NULL, NULL, NULL, LDAP_NO_LIMIT, &res );
-       if ( rc != LDAP_SUCCESS ) {
-               tester_ldap_error( ld, "ldap_search_ext_s" );
+       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 );
+                       }
+                       break;
+               }
 
-       } else {
-               LDAPMessage *e;
                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 );
@@ -259,123 +331,148 @@ do_random( char *uri, char *manager, struct berval *passwd,
 
                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 - got %d values.\n", (long) pid, nvalues );
+                       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[ rand() % nvalues ] );
+                       snprintf( buf, sizeof( buf ), "(%s=%s)", attr, values[ r ] );
 
-                       do_search( uri, manager, passwd, sbase, buf, &ld,
-                                       1, maxretries, delay, force );
+                       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 );
 
        if ( ld != NULL ) {
                ldap_unbind_ext( ld, NULL, NULL );
        }
 }
+
 static void
 do_search( char *uri, char *manager, struct berval *passwd,
-               char *sbase, char *filter, LDAP **ldp,
-               int innerloop, int maxretries, int delay, int force )
+       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;
-       char    *attrs[] = { "cn", "sn", NULL };
-       pid_t   pid = getpid();
        int     rc = LDAP_SUCCESS;
        int     version = LDAP_VERSION3;
-       int     first = 1;
+       char    buf[ BUFSIZ ];
+
 
 retry:;
        if ( ld == NULL ) {
                ldap_initialize( &ld, uri );
                if ( ld == NULL ) {
-                       tester_perror( "ldap_initialize" );
+                       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, LDAP_OPT_OFF );
+               (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, innerloop, sbase, filter );
+                       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...)" : "" );
                }
 
-               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" );
-                       switch ( rc ) {
-                       case LDAP_BUSY:
-                       case LDAP_UNAVAILABLE:
-                               if ( do_retry > 0 ) {
-                                       ldap_unbind_ext( ld, NULL, NULL );
-                                       do_retry--;
-                                       if ( delay != 0 ) {
-                                           sleep( delay );
+               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;
                                        }
-                                       goto retry;
+                               /* fallthru */
+                               default:
+                                       break;
                                }
-                       /* fallthru */
-                       default:
-                               break;
+                               exit( EXIT_FAILURE );
                        }
-                       exit( EXIT_FAILURE );
                }
        }
 
        for ( ; i < innerloop; i++ ) {
                LDAPMessage *res = NULL;
 
-               rc = ldap_search_ext_s( ld, sbase, LDAP_SCOPE_SUBTREE,
-                               filter, attrs, 0, NULL, 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 );
                }
 
-               switch ( rc ) {
-               case LDAP_REFERRAL:
-                       /* don't log: it's intended */
-                       if ( force >= 2 ) {
-                               if ( !first ) {
-                                       break;
+               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 );
                                }
-                               first = 0;
+                               continue;
                        }
-                       tester_ldap_error( ld, "ldap_search_ext_s" );
-                       /* fallthru */
 
-               case LDAP_SUCCESS:
-                       break;
-
-               default:
-                       tester_ldap_error( ld, "ldap_search_ext_s" );
+                       /* 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;
                        }
-                       if ( rc != LDAP_NO_SUCH_OBJECT ) {
-                               goto done;
-                       }
                        break;
                }
        }
 
-done:;
        if ( ldp != NULL ) {
                *ldp = ld;
 
        } else {
-               fprintf( stderr, " PID=%ld - Search done (%d).\n", (long) pid, rc );
+               fprintf( stderr, "  PID=%ld - Search done (%d).\n", (long) pid, rc );
 
                if ( ld != NULL ) {
                        ldap_unbind_ext( ld, NULL, NULL );