]> git.sur5r.net Git - openldap/blobdiff - tests/progs/slapd-search.c
Add missing revision string
[openldap] / tests / progs / slapd-search.c
index 945f8c9f2f55bf9bcb8a436c30ae8aa3b1bb2ce4..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 noattrs,
+       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 noattrs, int innerloop,
-       int maxretries, int delay, int force, int chaserefs );
+       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 );
 }
@@ -81,8 +92,11 @@ 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;
@@ -90,14 +104,16 @@ main( int argc, char **argv )
        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:p:w:r:t:" )) != EOF ) {
-               switch( i ) {
+       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;
@@ -118,9 +134,13 @@ main( int argc, char **argv )
                        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;
 
@@ -131,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':
@@ -151,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' ) {
 
@@ -190,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,
-                               noattrs, loops, retries, delay, force, chaserefs );
+                       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,
-                               noattrs, loops, retries, delay, force, chaserefs );
+                       do_search( uri, manager, &passwd,
+                               sbase, scope, filter, NULL,
+                               attrs, noattrs, nobind,
+                               loops, retries, delay, force, chaserefs );
                }
        }
 
@@ -209,13 +252,13 @@ main( int argc, char **argv )
 
 static void
 do_random( char *uri, char *manager, struct berval *passwd,
-       char *sbase, char *filter, char *attr, int noattrs,
+       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;
@@ -240,17 +283,19 @@ do_random( char *uri, char *manager, struct berval *passwd,
                                (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", NULL );
-               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,
@@ -286,6 +331,12 @@ 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 - Search base=\"%s\" filter=\"%s\" got %d values.\n",
                                (long) pid, sbase, filter, nvalues );
@@ -293,11 +344,17 @@ do_random( char *uri, char *manager, struct berval *passwd,
 
                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, noattrs,
-                                       1, maxretries, delay, force, chaserefs );
+                       do_search( uri, manager, passwd,
+                               sbase, scope, buf, &ld,
+                               srchattrs, noattrs, nobind,
+                               1, maxretries, delay, force, chaserefs );
                }
                break;
 
@@ -306,7 +363,7 @@ do_random( char *uri, char *manager, struct berval *passwd,
                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 );
@@ -315,14 +372,12 @@ do_random( char *uri, char *manager, struct berval *passwd,
 
 static void
 do_search( char *uri, char *manager, struct berval *passwd,
-               char *sbase, char *filter, LDAP **ldp,
-               int noattrs, int innerloop, int maxretries, int delay,
-               int force, int chaserefs )
+       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;
        char    buf[ BUFSIZ ];
@@ -341,38 +396,46 @@ retry:;
                        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 ) {
-                       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 );
-                                       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,
+               rc = ldap_search_ext_s( ld, sbase, scope,
                                filter, attrs, noattrs, NULL, NULL,
                                NULL, LDAP_NO_LIMIT, &res );
                if ( res != NULL ) {
@@ -409,7 +472,7 @@ retry:;
                *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 );