3 * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
8 * Written by: Pierangelo Masarati <ando@OpenLDAP.org>
10 * This program is designed to test the ldap_str2dn/ldap_dn2str
17 #include <ac/stdlib.h>
18 #include <ac/string.h>
19 #include <ac/unistd.h>
27 #include "lutil_ldap.h"
28 #include "ldap_defaults.h"
31 main( int argc, char *argv[] )
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;
39 int opt = getopt( argc, argv, "d:" );
47 debug = atoi( optarg );
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" );
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 );
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 );
71 if ( strcmp( argv[ 1 ], "-" ) == 0 ) {
74 fgets( buf, sizeof( buf ), stdin );
76 if ( len > 0 && buf[ --len ] == '\n' ) {
85 for ( i = 0; i < argc - 2; i++ ) {
87 for ( s = argv[ 2 + i ]; s; s = e ) {
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;
117 if ( flags[ 1 ] == 0 )
118 flags[ 1 ] = LDAP_DN_FORMAT_LDAPV3;
122 rc = ldap_str2dn( strin, &dn, flags[ 0 ] );
124 if ( rc == LDAP_SUCCESS ) {
127 for ( i = 0; dn[ i ]; i++ ) {
128 LDAPRDN rdn = dn[ i ];
131 if ( ldap_rdn2str( rdn, &rstr, flags[ f2 ] ) ) {
132 fprintf( stdout, "\tldap_rdn2str() failed\n" );
136 fprintf( stdout, "\tldap_rdn2str() = \"%s\"\n", rstr );
137 ldap_memfree( rstr );
140 fprintf( stdout, "\tempty DN\n" );
145 if ( rc == LDAP_SUCCESS &&
146 ldap_dn2str( dn, &str, flags[ f2 ] ) == LDAP_SUCCESS )
148 char **values, *tmp, *tmp2, *str2 = NULL;
151 fprintf( stdout, "\nldap_dn2str(ldap_str2dn(\"%s\"))\n"
152 "\t= \"%s\"\n", strin, str );
154 switch ( flags[ f2 ] & LDAP_DN_FORMAT_MASK ) {
155 case LDAP_DN_FORMAT_UFN:
156 case LDAP_DN_FORMAT_AD_CANONICAL:
159 case LDAP_DN_FORMAT_LDAPV3:
160 case LDAP_DN_FORMAT_LDAPV2:
161 n = ldap_dn2domain( strin, &tmp );
163 fprintf( stdout, "\nldap_dn2domain(\"%s\") FAILED\n", strin );
165 fprintf( stdout, "\nldap_dn2domain(\"%s\")\n"
166 "\t= \"%s\"\n", strin, tmp );
170 tmp = ldap_dn2ufn( strin );
171 fprintf( stdout, "\nldap_dn2ufn(\"%s\")\n"
172 "\t= \"%s\"\n", strin, tmp );
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 );
182 ldap_memfree( tmp2 );
184 tmp = ldap_dn2ad_canonical( strin );
185 fprintf( stdout, "\nldap_dn2ad_canonical(\"%s\")\n"
186 "\t= \"%s\"\n", strin, tmp );
189 fprintf( stdout, "\nldap_explode_dn(\"%s\"):\n", str );
190 values = ldap_explode_dn( str, 0 );
191 for ( n = 0; values && values[ n ]; n++ ) {
195 fprintf( stdout, "\t\"%s\"\n", values[ n ] );
197 fprintf( stdout, "\tldap_explode_rdn(\"%s\")\n",
199 vv = ldap_explode_rdn( values[ n ], 0 );
200 for ( nn = 0; vv && vv[ nn ]; nn++ ) {
201 fprintf( stdout, "\t\t'%s'\n",
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",
216 LDAP_VFREE( values );
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 ] );
224 LDAP_VFREE( values );
230 rc = ldap_str2dn( str, &dn2, flags[ f2 ] );
232 if ( rc == LDAP_SUCCESS &&
233 ldap_dn2str( dn2, &str2, flags[ f2 ] )
237 fprintf( stdout, "\n\"%s\"\n\t == \"%s\" ? %s\n",
239 strcmp( str, str2 ) == 0 ? "yes" : "no" );
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++ )
246 LDAPRDN r = dn[ iRDN ];
247 LDAPRDN r2 = dn2[ iRDN ];
250 for ( iAVA = 0; r[ iAVA ] && r2[ iAVA ]; iAVA++ ) {
251 LDAPAVA *a = r[ iAVA ];
252 LDAPAVA *a2 = r2[ iAVA ];
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",
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",
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 );
276 ldap_memfree( str2 );
282 /* note: dn is not freed */