]> git.sur5r.net Git - openldap/blob - tests/progs/slapd-search.c
Add more reads/searches to pound a little harder
[openldap] / tests / progs / slapd-search.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6 #include "portable.h"
7
8 #include <stdio.h>
9
10 #include <ac/stdlib.h>
11
12 #include <ac/ctype.h>
13 #include <ac/param.h>
14 #include <ac/socket.h>
15 #include <ac/string.h>
16 #include <ac/unistd.h>
17 #include <ac/wait.h>
18
19
20 #include <ldap.h>
21
22 #define LOOPS   100
23
24 static void
25 do_search( char *host, int port, char *sbase, char *filter, int maxloop );
26
27 static void
28 usage( char *name )
29 {
30         fprintf( stderr, "usage: %s [-h <host>] -p port -b <searchbase> -f <searchfiter> [-l <loops>]\n",
31                         name );
32         exit( EXIT_FAILURE );
33 }
34
35 int
36 main( int argc, char **argv )
37 {
38         int             i;
39         char        *host = "localhost";
40         int                     port = -1;
41         char        *sbase = NULL;
42         char            *filter  = NULL;
43         int                     loops = LOOPS;
44
45         while ( (i = getopt( argc, argv, "h:p:b:f:l:" )) != EOF ) {
46                 switch( i ) {
47                         case 'h':               /* the servers host */
48                                 host = strdup( optarg );
49                         break;
50
51                         case 'p':               /* the servers port */
52                                 port = atoi( optarg );
53                                 break;
54
55                         case 'b':               /* file with search base */
56                                 sbase = strdup( optarg );
57                         break;
58
59                         case 'f':               /* the search request */
60                                 filter = strdup( optarg );
61                                 break;
62
63                         case 'l':               /* number of loops */
64                                 loops = atoi( optarg );
65                                 break;
66
67                         default:
68                                 usage( argv[0] );
69                                 break;
70                 }
71         }
72
73         if (( sbase == NULL ) || ( filter == NULL ) || ( port == -1 ))
74                 usage( argv[0] );
75
76         if ( *filter == '\0' ) {
77
78                 fprintf( stderr, "%s: invalid EMPTY search filter.\n",
79                                 argv[0] );
80                 exit( EXIT_FAILURE );
81
82         }
83
84         do_search( host, port, sbase, filter, ( 10 * loops ));
85         exit( EXIT_SUCCESS );
86 }
87
88
89 static void
90 do_search( char *host, int port, char *sbase, char *filter, int maxloop )
91 {
92         LDAP    *ld;
93         int     i;
94         char    *attrs[] = { "cn", "sn", NULL };
95         pid_t   pid = getpid();
96
97         if (( ld = ldap_init( host, port )) == NULL ) {
98                 perror( "ldap_init" );
99                 exit( EXIT_FAILURE );
100         }
101
102         {
103                 int version = LDAP_VERSION3;
104                 (void) ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION,
105                         &version ); 
106         }
107
108         if ( ldap_bind_s( ld, NULL, NULL, LDAP_AUTH_SIMPLE ) != LDAP_SUCCESS ) {
109                 ldap_perror( ld, "ldap_bind" );
110                  exit( EXIT_FAILURE );
111         }
112
113
114         fprintf( stderr, "PID=%ld - Search(%d): base=\"%s\", filter=\"%s\".\n",
115                                 (long) pid, maxloop, sbase, filter );
116
117         for ( i = 0; i < maxloop; i++ ) {
118                 LDAPMessage *res;
119                 int         rc;
120
121                 if (( rc = ldap_search_s( ld, sbase, LDAP_SCOPE_SUBTREE,
122                                 filter, attrs, 0, &res )) != LDAP_SUCCESS ) {
123
124                         ldap_perror( ld, "ldap_search" );
125                         if ( rc != LDAP_NO_SUCH_OBJECT ) break;
126                         continue;
127
128                 }
129
130                 ldap_msgfree( res );
131         }
132
133         fprintf( stderr, " PID=%ld - Search done.\n", (long) pid );
134
135         ldap_unbind( ld );
136 }
137
138