]> git.sur5r.net Git - openldap/blob - libraries/libldap/ftest.c
fix non-printable flag detection; improve dn test (passes all but last in http:/...
[openldap] / libraries / libldap / ftest.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2002 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( char *filter );
26
27 int usage()
28 {
29         fprintf( stderr, "usage:\n"
30                 "  ftest [-d n] filter\n"
31                 "    filter - RFC 2254 string representation of an "
32                         "LDAP search filter\n" );
33         return EXIT_FAILURE;
34 }
35
36 int
37 main( int argc, char *argv[] )
38 {
39         int c;
40         int debug=0;
41         char *filter=NULL;
42
43     while( (c = getopt( argc, argv, "d:" )) != EOF ) {
44                 switch ( c ) {
45                 case 'd':
46                         debug = atoi( optarg );
47                         break;
48                 default:
49                         fprintf( stderr, "ftest: unrecognized option -%c\n",
50                                 optopt );
51                         return usage();
52                 }
53         }
54
55         if ( debug ) {
56                 if ( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug )
57                         != LBER_OPT_SUCCESS )
58                 {
59                         fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n",
60                                 debug );
61                 }
62                 if ( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug )
63                         != LDAP_OPT_SUCCESS )
64                 {
65                         fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n",
66                                 debug );
67                 }
68         }
69
70         if ( argc - optind != 1 ) {
71                 return usage();
72         }
73
74         return filter2ber( strdup( argv[optind] ) );
75 }
76
77 static int filter2ber( char *filter )
78 {
79         int rc;
80         struct berval *bv = NULL;
81         BerElement *ber;
82
83         printf( "Filter: %s\n", filter );
84
85         ber = ber_alloc_t( LBER_USE_DER );
86         if( ber == NULL ) {
87                 perror( "ber_alloc_t" );
88                 return EXIT_FAILURE;
89         }
90
91         rc = ldap_int_put_filter( ber, filter );
92         if( rc < 0 ) {
93                 fprintf( stderr, "Filter error!\n");
94                 return EXIT_FAILURE;
95         }
96
97         rc = ber_flatten( ber, &bv );
98         if( rc < 0 ) {
99                 perror( "ber_flatten" );
100                 return EXIT_FAILURE;
101         }
102
103         printf( "BER encoding (len=%ld):\n", (long) bv->bv_len );
104         ber_bprint( bv->bv_val, bv->bv_len );
105
106         ber_free( ber, 0 );
107         ber_bvfree( bv );
108
109         return EXIT_SUCCESS;
110 }
111