]> git.sur5r.net Git - openldap/blob - libraries/libldap/ftest.c
Add ftest (filter test) to the mix, needs work.
[openldap] / libraries / libldap / ftest.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6 /* OpenLDAP Filter API Test */
7
8 #include "portable.h"
9
10 #include <ac/stdlib.h>
11 #include <ac/string.h>
12 #include <ac/unistd.h>
13
14 #include <stdio.h>
15
16 #include <ldap.h>
17
18 #include "ldap-int.h"
19
20 #include "ldif.h"
21 #include "lutil.h"
22 #include "lutil_ldap.h"
23 #include "ldap_defaults.h"
24
25 static int filter2ber( const char *filter );
26
27 int usage()
28 {
29         fprintf( stderr, "usage:\n"
30                 "       ftest [filter]" );
31         return EXIT_FAILURE;
32 }
33
34 int
35 main( int argc, char *argv[] )
36 {
37         int i, debug=0, ber=0;
38         char *filter=NULL;
39
40     while( (i = getopt( argc, argv, "Aa:Ss:"
41         "bd:" )) != EOF )
42     {
43                 switch ( i ) {
44                 case 'b':
45                         ber++;
46                         break;
47                 case 'd':
48                         debug = atoi( optarg );
49                         break;
50                 default:
51                         fprintf( stderr, "ftest: unrecognized option -%c\n",
52                                 optopt );
53                         return usage();
54                 }
55         }
56
57         if ( debug ) {
58                 if ( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug )
59                         != LBER_OPT_SUCCESS )
60                 {
61                         fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n",
62                                 debug );
63                 }
64                 if ( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug )
65                         != LDAP_OPT_SUCCESS )
66                 {
67                         fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n",
68                                 debug );
69                 }
70         }
71
72         if( argc - optind > 1 ) {
73                 return usage();
74         } else if ( argc - optind == 1 ) {
75                 if( ber ) {
76                         fprintf( stderr, "ftest: parameter %s unexpected\n",
77                                 argv[optind] );
78                         return usage();
79                 }
80                 return filter2ber( argv[optind] );
81         }
82
83         return EXIT_FAILURE;
84 }
85
86 static int filter2ber( const char *filter )
87 {
88         int rc;
89         BerElement *ber = ber_alloc_t( LBER_USE_DER );
90         struct berval *bv = NULL;
91
92         if( ber == NULL ) {
93                 perror( "ber_alloc_t" );
94                 return EXIT_FAILURE;
95         }
96
97         rc = ldap_int_put_filter( ber, (char *) filter );
98
99         ber_dump( ber, 0 );
100
101         rc = ber_flatten( ber, &bv );
102
103         if( rc < 0 ) {
104                 perror( "ber_flatten" );
105                 return EXIT_FAILURE;
106         }
107
108         ber_free( ber, 1 );
109         ber_bvfree( bv );
110
111         return EXIT_SUCCESS;
112 }