3 * Copyright 1998-2000 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>
29 #include "lutil_ldap.h"
30 #include "ldap_defaults.h"
33 main( int argc, char *argv[] )
35 int rc, i, debug = 0, f2 = 0;
36 unsigned flags[ 2 ] = { 0U, 0U };
37 char *strin, *str, *str2, buf[ 1024 ];
38 LDAPDN *dn, *dn2 = NULL;
41 int opt = getopt( argc, argv, "d:" );
49 debug = atoi( optarg );
59 fprintf( stderr, "usage: dntest <dn> [flags-in[,...]] [flags-out[,...]]\n\n" );
60 fprintf( stderr, "\tflags-in: V3,V2,DCE,<pedantic>\n" );
61 fprintf( stderr, "\tflags-out: V3,V2,UFN,DCE,AD,<pedantic>\n\n" );
62 fprintf( stderr, "\t<pedantic>: PEDANTIC,NOSPACES,NOONESPACE\n\n" );
66 if ( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
67 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
69 if ( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
70 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
73 if ( strcmp( argv[ 1 ], "-" ) == 0 ) {
76 fgets( buf, sizeof( buf ), stdin );
77 len = strlen( buf ) - 1;
78 if ( len >= 0 && buf[ len ] == '\n' ) {
87 for ( i = 0; i < argc - 2; i++ ) {
89 for ( s = argv[ 2 + i ]; s; s = e ) {
96 if ( !strcasecmp( s, "V3" ) ) {
97 flags[ i ] |= LDAP_DN_FORMAT_LDAPV3;
98 } else if ( !strcasecmp( s, "V2" ) ) {
99 flags[ i ] |= LDAP_DN_FORMAT_LDAPV2;
100 } else if ( !strcasecmp( s, "DCE" ) ) {
101 flags[ i ] |= LDAP_DN_FORMAT_DCE;
102 } else if ( !strcasecmp( s, "UFN" ) ) {
103 flags[ i ] |= LDAP_DN_FORMAT_UFN;
104 } else if ( !strcasecmp( s, "AD" ) ) {
105 flags[ i ] |= LDAP_DN_FORMAT_AD_CANONICAL;
106 } else if ( !strcasecmp( s, "PRETTY" ) ) {
107 flags[ i ] |= LDAP_DN_PRETTY;
108 } else if ( !strcasecmp( s, "PEDANTIC" ) ) {
109 flags[ i ] |= LDAP_DN_PEDANTIC;
110 } else if ( !strcasecmp( s, "NOSPACES" ) ) {
111 flags[ i ] |= LDAP_DN_P_NOLEADTRAILSPACES;
112 } else if ( !strcasecmp( s, "NOONESPACE" ) ) {
113 flags[ i ] |= LDAP_DN_P_NOSPACEAFTERRDN;
119 f2 = argc > 3 ? 1 : 0;
121 rc = ldap_str2dn( strin, &dn, flags[ 0 ] );
123 if ( rc == LDAP_SUCCESS ) {
125 for ( i = 0; dn[ i ]; i++ ) {
126 LDAPRDN *rdn = dn[ i ][ 0 ];
129 if ( ldap_rdn2str( rdn, &rstr, flags[ f2 ] ) ) {
130 fprintf( stdout, "\tldap_rdn2str() failed\n" );
134 fprintf( stdout, "\tldap_rdn2str() = \"%s\"\n", rstr );
135 ldap_memfree( rstr );
139 if ( rc == LDAP_SUCCESS &&
140 ldap_dn2str( dn, &str, flags[ f2 ] ) == LDAP_SUCCESS ) {
141 char **values, *tmp, *tmp2;
144 fprintf( stdout, "\nldap_dn2str(ldap_str2dn(\"%s\"))\n"
145 "\t= \"%s\"\n", strin, str );
147 switch ( flags[ f2 ] & LDAP_DN_FORMAT_MASK ) {
148 case LDAP_DN_FORMAT_UFN:
149 case LDAP_DN_FORMAT_AD_CANONICAL:
152 case LDAP_DN_FORMAT_LDAPV3:
153 case LDAP_DN_FORMAT_LDAPV2:
154 tmp = ldap_dn2ufn( strin );
155 fprintf( stdout, "\nldap_dn2ufn(\"%s\")\n"
156 "\t= \"%s\"\n", strin, tmp );
158 tmp = ldap_dn2dcedn( strin );
159 fprintf( stdout, "\nldap_dn2dcedn(\"%s\")\n"
160 "\t= \"%s\"\n", strin, tmp );
161 tmp2 = ldap_dcedn2dn( tmp );
162 fprintf( stdout, "\nldap_dcedn2dn(\"%s\")\n"
163 "\t= \"%s\"\n", tmp, tmp2 );
165 ldap_memfree( tmp2 );
166 tmp = ldap_dn2ad_canonical( strin );
167 fprintf( stdout, "\nldap_dn2ad_canonical(\"%s\")\n"
168 "\t= \"%s\"\n", strin, tmp );
171 fprintf( stdout, "\nldap_explode_dn(\"%s\"):\n", str );
172 values = ldap_explode_dn( str, 0 );
173 for ( n = 0; values && values[ n ]; n++ ) {
177 fprintf( stdout, "\t\"%s\"\n", values[ n ] );
179 fprintf( stdout, "\tldap_explode_rdn(\"%s\")\n",
181 vv = ldap_explode_rdn( values[ n ], 0 );
182 for ( nn = 0; vv && vv[ nn ]; nn++ ) {
183 fprintf( stdout, "\t\t'%s'\n",
188 fprintf( stdout, "\tldap_explode_rdn(\"%s\")"
189 " (no types)\n", values[ n ] );
190 vv = ldap_explode_rdn( values[ n ], 1 );
191 for ( nn = 0; vv && vv[ nn ]; nn++ ) {
192 fprintf( stdout, "\t\t\t\"%s\"\n",
198 LDAP_VFREE( values );
200 fprintf( stdout, "\nldap_explode_dn(\"%s\")"
201 " (no types):\n", str );
202 values = ldap_explode_dn( str, 1 );
203 for ( n = 0; values && values[ n ]; n++ ) {
204 fprintf( stdout, "\t\"%s\"\n", values[ n ] );
206 LDAP_VFREE( values );
211 rc = ldap_str2dn( str, &dn2, flags[ f2 ] );
212 if ( rc == LDAP_SUCCESS &&
213 ldap_dn2str( dn2, &str2, flags[ f2 ] )
217 fprintf( stdout, "\n\"%s\"\n\t == \"%s\" ? %s\n",
219 strcmp( str, str2 ) == 0 ? "yes" : "no" );
221 for ( iRDN = 0; dn[ iRDN ] && dn2[ iRDN ]; iRDN++ ) {
222 LDAPRDN *r = dn[ iRDN ][ 0 ];
223 LDAPRDN *r2 = dn2[ iRDN ][ 0 ];
226 for ( iAVA = 0; r[ iAVA ] && r[ iAVA ]; iAVA++ ) {
227 LDAPAVA *a = r[ iAVA ][ 0 ];
228 LDAPAVA *a2 = r2[ iAVA ][ 0 ];
230 if ( a->la_attr->bv_len != a2->la_attr->bv_len
231 || memcmp( a->la_attr->bv_val, a2->la_attr->bv_val, a->la_attr->bv_len )
232 || a->la_flags != a2->la_flags
233 || a->la_value->bv_len != a2->la_value->bv_len
234 || memcmp( a->la_value->bv_val, a2->la_value->bv_val, a->la_value->bv_len ) ) {
235 fprintf( stdout, "mismatch\n" );
241 ldapava_free_dn( dn2 );
242 ldap_memfree( str2 );
246 ldapava_free_dn( dn );
248 /* note: dn is not freed */