1 /* dntest.c -- OpenLDAP DN API Test Program */
2 /* $OpenLDAP: pkg/ldap/libraries/libldap/dntest.c,v 1.27.2.3 2008/02/11 23:26:41 kurt Exp $ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2008 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
17 * This program was initially developed by Pierangelo Masarati <ando@OpenLDAP.org>
18 * for inclusion in OpenLDAP Software.
22 * This program is designed to test the ldap_str2dn/ldap_dn2str
29 #include <ac/stdlib.h>
30 #include <ac/string.h>
31 #include <ac/unistd.h>
39 #include "lutil_ldap.h"
40 #include "ldap_defaults.h"
43 main( int argc
, char *argv
[] )
45 int rc
, i
, debug
= 0, f2
= 0;
46 unsigned flags
[ 2 ] = { 0U, 0 };
47 char *strin
, *str
= NULL
, buf
[ 1024 ];
48 LDAPDN dn
, dn2
= NULL
;
51 int opt
= getopt( argc
, argv
, "d:" );
59 debug
= atoi( optarg
);
69 fprintf( stderr
, "usage: dntest <dn> [flags-in[,...]] [flags-out[,...]]\n\n" );
70 fprintf( stderr
, "\tflags-in: V3,V2,DCE,<flags>\n" );
71 fprintf( stderr
, "\tflags-out: V3,V2,UFN,DCE,AD,<flags>\n\n" );
72 fprintf( stderr
, "\t<flags>: PRETTY,PEDANTIC,NOSPACES,NOONESPACE\n\n" );
76 if ( ber_set_option( NULL
, LBER_OPT_DEBUG_LEVEL
, &debug
) != LBER_OPT_SUCCESS
) {
77 fprintf( stderr
, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug
);
79 if ( ldap_set_option( NULL
, LDAP_OPT_DEBUG_LEVEL
, &debug
) != LDAP_OPT_SUCCESS
) {
80 fprintf( stderr
, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug
);
83 if ( strcmp( argv
[ 1 ], "-" ) == 0 ) {
86 fgets( buf
, sizeof( buf
), stdin
);
88 if ( len
> 0 && buf
[ --len
] == '\n' ) {
97 for ( i
= 0; i
< argc
- 2; i
++ ) {
99 for ( s
= argv
[ 2 + i
]; s
; s
= e
) {
100 e
= strchr( s
, ',' );
106 if ( !strcasecmp( s
, "V3" ) ) {
107 flags
[ i
] |= LDAP_DN_FORMAT_LDAPV3
;
108 } else if ( !strcasecmp( s
, "V2" ) ) {
109 flags
[ i
] |= LDAP_DN_FORMAT_LDAPV2
;
110 } else if ( !strcasecmp( s
, "DCE" ) ) {
111 flags
[ i
] |= LDAP_DN_FORMAT_DCE
;
112 } else if ( !strcasecmp( s
, "UFN" ) ) {
113 flags
[ i
] |= LDAP_DN_FORMAT_UFN
;
114 } else if ( !strcasecmp( s
, "AD" ) ) {
115 flags
[ i
] |= LDAP_DN_FORMAT_AD_CANONICAL
;
116 } else if ( !strcasecmp( s
, "PRETTY" ) ) {
117 flags
[ i
] |= LDAP_DN_PRETTY
;
118 } else if ( !strcasecmp( s
, "PEDANTIC" ) ) {
119 flags
[ i
] |= LDAP_DN_PEDANTIC
;
120 } else if ( !strcasecmp( s
, "NOSPACES" ) ) {
121 flags
[ i
] |= LDAP_DN_P_NOLEADTRAILSPACES
;
122 } else if ( !strcasecmp( s
, "NOONESPACE" ) ) {
123 flags
[ i
] |= LDAP_DN_P_NOSPACEAFTERRDN
;
129 if ( flags
[ 1 ] == 0 )
130 flags
[ 1 ] = LDAP_DN_FORMAT_LDAPV3
;
134 rc
= ldap_str2dn( strin
, &dn
, flags
[ 0 ] );
136 if ( rc
== LDAP_SUCCESS
) {
139 for ( i
= 0; dn
[ i
]; i
++ ) {
140 LDAPRDN rdn
= dn
[ i
];
143 if ( ldap_rdn2str( rdn
, &rstr
, flags
[ f2
] ) ) {
144 fprintf( stdout
, "\tldap_rdn2str() failed\n" );
148 fprintf( stdout
, "\tldap_rdn2str() = \"%s\"\n", rstr
);
149 ldap_memfree( rstr
);
152 fprintf( stdout
, "\tempty DN\n" );
157 if ( rc
== LDAP_SUCCESS
&&
158 ldap_dn2str( dn
, &str
, flags
[ f2
] ) == LDAP_SUCCESS
)
160 char **values
, *tmp
, *tmp2
, *str2
= NULL
;
163 fprintf( stdout
, "\nldap_dn2str(ldap_str2dn(\"%s\"))\n"
164 "\t= \"%s\"\n", strin
, str
);
166 switch ( flags
[ f2
] & LDAP_DN_FORMAT_MASK
) {
167 case LDAP_DN_FORMAT_UFN
:
168 case LDAP_DN_FORMAT_AD_CANONICAL
:
171 case LDAP_DN_FORMAT_LDAPV3
:
172 case LDAP_DN_FORMAT_LDAPV2
:
173 n
= ldap_dn2domain( strin
, &tmp
);
175 fprintf( stdout
, "\nldap_dn2domain(\"%s\") FAILED\n", strin
);
177 fprintf( stdout
, "\nldap_dn2domain(\"%s\")\n"
178 "\t= \"%s\"\n", strin
, tmp
? tmp
: "" );
182 tmp
= ldap_dn2ufn( strin
);
183 fprintf( stdout
, "\nldap_dn2ufn(\"%s\")\n"
184 "\t= \"%s\"\n", strin
, tmp
? tmp
: "" );
187 tmp
= ldap_dn2dcedn( strin
);
188 fprintf( stdout
, "\nldap_dn2dcedn(\"%s\")\n"
189 "\t= \"%s\"\n", strin
, tmp
? tmp
: "" );
190 tmp2
= ldap_dcedn2dn( tmp
);
191 fprintf( stdout
, "\nldap_dcedn2dn(\"%s\")\n"
193 tmp
? tmp
: "", tmp2
? tmp2
: "" );
195 ldap_memfree( tmp2
);
197 tmp
= ldap_dn2ad_canonical( strin
);
198 fprintf( stdout
, "\nldap_dn2ad_canonical(\"%s\")\n"
199 "\t= \"%s\"\n", strin
, tmp
? tmp
: "" );
202 fprintf( stdout
, "\nldap_explode_dn(\"%s\"):\n", str
);
203 values
= ldap_explode_dn( str
, 0 );
204 for ( n
= 0; values
&& values
[ n
]; n
++ ) {
208 fprintf( stdout
, "\t\"%s\"\n", values
[ n
] );
210 fprintf( stdout
, "\tldap_explode_rdn(\"%s\")\n",
212 vv
= ldap_explode_rdn( values
[ n
], 0 );
213 for ( nn
= 0; vv
&& vv
[ nn
]; nn
++ ) {
214 fprintf( stdout
, "\t\t'%s'\n",
219 fprintf( stdout
, "\tldap_explode_rdn(\"%s\")"
220 " (no types)\n", values
[ n
] );
221 vv
= ldap_explode_rdn( values
[ n
], 1 );
222 for ( nn
= 0; vv
&& vv
[ nn
]; nn
++ ) {
223 fprintf( stdout
, "\t\t\t\"%s\"\n",
229 LDAP_VFREE( values
);
231 fprintf( stdout
, "\nldap_explode_dn(\"%s\")"
232 " (no types):\n", str
);
233 values
= ldap_explode_dn( str
, 1 );
234 for ( n
= 0; values
&& values
[ n
]; n
++ ) {
235 fprintf( stdout
, "\t\"%s\"\n", values
[ n
] );
237 LDAP_VFREE( values
);
243 rc
= ldap_str2dn( str
, &dn2
, flags
[ f2
] );
245 if ( rc
== LDAP_SUCCESS
&&
246 ldap_dn2str( dn2
, &str2
, flags
[ f2
] )
250 fprintf( stdout
, "\n\"%s\"\n\t == \"%s\" ? %s\n",
252 strcmp( str
, str2
) == 0 ? "yes" : "no" );
254 if( dn
!= NULL
&& dn2
== NULL
) {
255 fprintf( stdout
, "dn mismatch\n" );
256 } else if (( dn
!= NULL
) && (dn2
!= NULL
))
257 for ( iRDN
= 0; dn
[ iRDN
] && dn2
[ iRDN
]; iRDN
++ )
259 LDAPRDN r
= dn
[ iRDN
];
260 LDAPRDN r2
= dn2
[ iRDN
];
263 for ( iAVA
= 0; r
[ iAVA
] && r2
[ iAVA
]; iAVA
++ ) {
264 LDAPAVA
*a
= r
[ iAVA
];
265 LDAPAVA
*a2
= r2
[ iAVA
];
267 if ( a
->la_attr
.bv_len
!= a2
->la_attr
.bv_len
) {
268 fprintf( stdout
, "ava(%d), rdn(%d) attr len mismatch (%ld->%ld)\n",
270 a
->la_attr
.bv_len
, a2
->la_attr
.bv_len
);
271 } else if ( memcmp( a
->la_attr
.bv_val
, a2
->la_attr
.bv_val
, a
->la_attr
.bv_len
) ) {
272 fprintf( stdout
, "ava(%d), rdn(%d) attr mismatch\n",
273 iAVA
+ 1, iRDN
+ 1 );
274 } else if ( a
->la_flags
!= a2
->la_flags
) {
275 fprintf( stdout
, "ava(%d), rdn(%d) flag mismatch (%x->%x)\n",
276 iAVA
+ 1, iRDN
+ 1, a
->la_flags
, a2
->la_flags
);
277 } else if ( a
->la_value
.bv_len
!= a2
->la_value
.bv_len
) {
278 fprintf( stdout
, "ava(%d), rdn(%d) value len mismatch (%ld->%ld)\n",
280 a
->la_value
.bv_len
, a2
->la_value
.bv_len
);
281 } else if ( memcmp( a
->la_value
.bv_val
, a2
->la_value
.bv_val
, a
->la_value
.bv_len
) ) {
282 fprintf( stdout
, "ava(%d), rdn(%d) value mismatch\n",
283 iAVA
+ 1, iRDN
+ 1 );
289 ldap_memfree( str2
);
295 /* note: dn is not freed */