]> git.sur5r.net Git - openldap/blob - libraries/libldap/dntest.c
65acefa1e08709af626f53ff2cb73ef975fb31b7
[openldap] / libraries / libldap / dntest.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6 /*
7  * OpenLDAP DN API Test
8  *      Written by: Pierangelo Masarati <ando@OpenLDAP.org>
9  *
10  * This program is designed to test the ldap_str2dn/ldap_dn2str
11  * functions
12  */
13 #include "portable.h"
14
15 #include <stdio.h>
16
17 #include <ac/stdlib.h>
18 #include <ac/string.h>
19 #include <ac/unistd.h>
20
21 #include <ldap.h>
22
23 #include "ldap-int.h"
24
25 #include "ldif.h"
26 #include "lutil.h"
27 #include "lutil_ldap.h"
28 #include "ldap_defaults.h"
29
30 int
31 main( int argc, char *argv[] )
32 {
33         int             rc, i, debug = 0, f2 = 0;
34         unsigned        flags[ 2 ] = { 0U, 0 };
35         char            *strin, *str = NULL, buf[ 1024 ];
36         LDAPDN          dn, dn2 = NULL;
37
38         while ( 1 ) {
39                 int opt = getopt( argc, argv, "d:" );
40
41                 if ( opt == EOF ) {
42                         break;
43                 }
44
45                 switch ( opt ) {
46                 case 'd':
47                         debug = atoi( optarg );
48                         break;
49                 }
50         }
51
52         optind--;
53         argc -= optind;
54         argv += optind;
55
56         if ( argc < 2 ) {
57                 fprintf( stderr, "usage: dntest <dn> [flags-in[,...]] [flags-out[,...]]\n\n" );
58                 fprintf( stderr, "\tflags-in:   V3,V2,DCE,<flags>\n" );
59                 fprintf( stderr, "\tflags-out:  V3,V2,UFN,DCE,AD,<flags>\n\n" );
60                 fprintf( stderr, "\t<flags>: PRETTY,PEDANTIC,NOSPACES,NOONESPACE\n\n" );
61                 return( 0 );
62         }
63
64         if ( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
65                 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
66         }
67         if ( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
68                 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
69         }
70
71         if ( strcmp( argv[ 1 ], "-" ) == 0 ) {
72                 size_t len;
73                 
74                 fgets( buf, sizeof( buf ), stdin );
75                 len = strlen( buf );
76                 if ( len > 0 && buf[ --len ] == '\n' ) {
77                         buf[ len ] = '\0';
78                 }
79                 strin = buf;
80         } else {
81                 strin = argv[ 1 ];
82         }
83
84         if ( argc >= 3 ) {
85                 for ( i = 0; i < argc - 2; i++ ) {
86                         char *s, *e;
87                         for ( s = argv[ 2 + i ]; s; s = e ) {
88                                 e = strchr( s, ',' );
89                                 if ( e != NULL ) {
90                                         e[ 0 ] = '\0';
91                                         e++;
92                                 }
93         
94                                 if ( !strcasecmp( s, "V3" ) ) {
95                                         flags[ i ] |= LDAP_DN_FORMAT_LDAPV3;
96                                 } else if ( !strcasecmp( s, "V2" ) ) {
97                                         flags[ i ] |= LDAP_DN_FORMAT_LDAPV2;
98                                 } else if ( !strcasecmp( s, "DCE" ) ) {
99                                         flags[ i ] |= LDAP_DN_FORMAT_DCE;
100                                 } else if ( !strcasecmp( s, "UFN" ) ) {
101                                         flags[ i ] |= LDAP_DN_FORMAT_UFN;
102                                 } else if ( !strcasecmp( s, "AD" ) ) {
103                                         flags[ i ] |= LDAP_DN_FORMAT_AD_CANONICAL;
104                                 } else if ( !strcasecmp( s, "PRETTY" ) ) {
105                                         flags[ i ] |= LDAP_DN_PRETTY;
106                                 } else if ( !strcasecmp( s, "PEDANTIC" ) ) {
107                                         flags[ i ] |= LDAP_DN_PEDANTIC;
108                                 } else if ( !strcasecmp( s, "NOSPACES" ) ) {
109                                         flags[ i ] |= LDAP_DN_P_NOLEADTRAILSPACES;
110                                 } else if ( !strcasecmp( s, "NOONESPACE" ) ) {
111                                         flags[ i ] |= LDAP_DN_P_NOSPACEAFTERRDN;
112                                 }
113                         }
114                 }
115         }
116
117         if ( flags[ 1 ] == 0 )
118                 flags[ 1 ] = LDAP_DN_FORMAT_LDAPV3;
119
120         f2 = 1;
121
122         rc = ldap_str2dn( strin, &dn, flags[ 0 ] );
123
124         if ( rc == LDAP_SUCCESS ) {
125                 int i;
126                 if ( dn ) {
127                         for ( i = 0; dn[ i ]; i++ ) {
128                                 LDAPRDN         rdn = dn[ i ];
129                                 char            *rstr = NULL;
130
131                                 if ( ldap_rdn2str( rdn, &rstr, flags[ f2 ] ) ) {
132                                         fprintf( stdout, "\tldap_rdn2str() failed\n" );
133                                         continue;
134                                 }
135
136                                 fprintf( stdout, "\tldap_rdn2str() = \"%s\"\n", rstr );
137                                 ldap_memfree( rstr );
138                         }
139                 } else {
140                         fprintf( stdout, "\tempty DN\n" );
141                 }
142         }
143
144         str = NULL;
145         if ( rc == LDAP_SUCCESS &&
146                 ldap_dn2str( dn, &str, flags[ f2 ] ) == LDAP_SUCCESS )
147         {
148                 char    **values, *tmp, *tmp2, *str2 = NULL;
149                 int     n;
150                 
151                 fprintf( stdout, "\nldap_dn2str(ldap_str2dn(\"%s\"))\n"
152                                 "\t= \"%s\"\n", strin, str );
153                         
154                 switch ( flags[ f2 ] & LDAP_DN_FORMAT_MASK ) {
155                 case LDAP_DN_FORMAT_UFN:
156                 case LDAP_DN_FORMAT_AD_CANONICAL:
157                         return( 0 );
158
159                 case LDAP_DN_FORMAT_LDAPV3:
160                 case LDAP_DN_FORMAT_LDAPV2:
161                         n = ldap_dn2domain( strin, &tmp );
162                         if( n ) {
163                                 fprintf( stdout, "\nldap_dn2domain(\"%s\") FAILED\n", strin );
164                         } else {
165                                 fprintf( stdout, "\nldap_dn2domain(\"%s\")\n"
166                                         "\t= \"%s\"\n", strin, tmp );
167                         }
168                         ldap_memfree( tmp );
169
170                         tmp = ldap_dn2ufn( strin );
171                         fprintf( stdout, "\nldap_dn2ufn(\"%s\")\n"
172                                         "\t= \"%s\"\n", strin, tmp );
173                         ldap_memfree( tmp );
174
175                         tmp = ldap_dn2dcedn( strin );
176                         fprintf( stdout, "\nldap_dn2dcedn(\"%s\")\n"
177                                         "\t= \"%s\"\n", strin, tmp );
178                         tmp2 = ldap_dcedn2dn( tmp );
179                         fprintf( stdout, "\nldap_dcedn2dn(\"%s\")\n"
180                                         "\t= \"%s\"\n", tmp, tmp2 );
181                         ldap_memfree( tmp );
182                         ldap_memfree( tmp2 );
183
184                         tmp = ldap_dn2ad_canonical( strin );
185                         fprintf( stdout, "\nldap_dn2ad_canonical(\"%s\")\n"
186                                         "\t= \"%s\"\n", strin, tmp );
187                         ldap_memfree( tmp );
188
189                         fprintf( stdout, "\nldap_explode_dn(\"%s\"):\n", str );
190                         values = ldap_explode_dn( str, 0 );
191                         for ( n = 0; values && values[ n ]; n++ ) {
192                                 char    **vv;
193                                 int     nn;
194                                 
195                                 fprintf( stdout, "\t\"%s\"\n", values[ n ] );
196
197                                 fprintf( stdout, "\tldap_explode_rdn(\"%s\")\n",
198                                                 values[ n ] );
199                                 vv = ldap_explode_rdn( values[ n ], 0 );
200                                 for ( nn = 0; vv && vv[ nn ]; nn++ ) {
201                                         fprintf( stdout, "\t\t'%s'\n", 
202                                                         vv[ nn ] );
203                                 }
204                                 LDAP_VFREE( vv );
205
206                                 fprintf( stdout, "\tldap_explode_rdn(\"%s\")"
207                                                " (no types)\n", values[ n ] );
208                                 vv = ldap_explode_rdn( values[ n ], 1 );
209                                 for ( nn = 0; vv && vv[ nn ]; nn++ ) {
210                                         fprintf( stdout, "\t\t\t\"%s\"\n", 
211                                                         vv[ nn ] );
212                                 }
213                                 LDAP_VFREE( vv );
214                                 
215                         }
216                         LDAP_VFREE( values );
217
218                         fprintf( stdout, "\nldap_explode_dn(\"%s\")"
219                                         " (no types):\n", str );
220                         values = ldap_explode_dn( str, 1 );
221                         for ( n = 0; values && values[ n ]; n++ ) {
222                                 fprintf( stdout, "\t\"%s\"\n", values[ n ] );
223                         }
224                         LDAP_VFREE( values );
225
226                         break;
227                 }
228
229                 dn2 = NULL;     
230                 rc = ldap_str2dn( str, &dn2, flags[ f2 ] );
231                 str2 = NULL;
232                 if ( rc == LDAP_SUCCESS && 
233                                 ldap_dn2str( dn2, &str2, flags[ f2 ] )
234                                 == LDAP_SUCCESS ) {
235                         int     iRDN;
236                         
237                         fprintf( stdout, "\n\"%s\"\n\t == \"%s\" ? %s\n", 
238                                 str, str2, 
239                                 strcmp( str, str2 ) == 0 ? "yes" : "no" );
240
241                         if( dn != NULL && dn2 == NULL ) {
242                                 fprintf( stdout, "dn mismatch\n" );
243                         } else if (( dn != NULL ) && (dn2 != NULL))
244                                 for ( iRDN = 0; dn[ iRDN ] && dn2[ iRDN ]; iRDN++ )
245                         {
246                                 LDAPRDN         r = dn[ iRDN ];
247                                 LDAPRDN         r2 = dn2[ iRDN ];
248                                 int             iAVA;
249                                 
250                                 for ( iAVA = 0; r[ iAVA ] && r2[ iAVA ]; iAVA++ ) {
251                                         LDAPAVA         *a = r[ iAVA ];
252                                         LDAPAVA         *a2 = r2[ iAVA ];
253
254                                         if ( a->la_attr.bv_len != a2->la_attr.bv_len ) {
255                                                 fprintf( stdout, "ava(%d), rdn(%d) attr len mismatch (%ld->%ld)\n", 
256                                                                 iAVA + 1, iRDN + 1,
257                                                                 a->la_attr.bv_len, a2->la_attr.bv_len );
258                                         } else if ( memcmp( a->la_attr.bv_val, a2->la_attr.bv_val, a->la_attr.bv_len ) ) {
259                                                 fprintf( stdout, "ava(%d), rdn(%d) attr mismatch\n", 
260                                                                 iAVA + 1, iRDN + 1 );
261                                         } else if ( a->la_flags != a2->la_flags ) {
262                                                 fprintf( stdout, "ava(%d), rdn(%d) flag mismatch (%x->%x)\n", 
263                                                                 iAVA + 1, iRDN + 1, a->la_flags, a2->la_flags );
264                                         } else if ( a->la_value.bv_len != a2->la_value.bv_len ) {
265                                                 fprintf( stdout, "ava(%d), rdn(%d) value len mismatch (%ld->%ld)\n", 
266                                                                 iAVA + 1, iRDN + 1, 
267                                                                 a->la_value.bv_len, a2->la_value.bv_len );
268                                         } else if ( memcmp( a->la_value.bv_val, a2->la_value.bv_val, a->la_value.bv_len ) ) {
269                                                 fprintf( stdout, "ava(%d), rdn(%d) value mismatch\n", 
270                                                                 iAVA + 1, iRDN + 1 );
271                                         }
272                                 }
273                         }
274                         
275                         ldap_dnfree( dn2 );
276                         ldap_memfree( str2 );
277                 }
278                 ldap_memfree( str );
279         }
280         ldap_dnfree( dn );
281
282         /* note: dn is not freed */
283
284         return( 0 );
285 }
286