No empty .Rs/.Re
[netbsd-mini2440.git] / external / bsd / openldap / dist / libraries / libldap / dntest.c
blobe2ff10aafca9ff8870aee30b8e08f4621bb75478
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.
6 * All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
10 * Public License.
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>.
16 /* ACKNOWLEDGEMENT:
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
23 * functions
25 #include "portable.h"
27 #include <stdio.h>
29 #include <ac/stdlib.h>
30 #include <ac/string.h>
31 #include <ac/unistd.h>
33 #include <ldap.h>
35 #include "ldap-int.h"
37 #include "ldif.h"
38 #include "lutil.h"
39 #include "lutil_ldap.h"
40 #include "ldap_defaults.h"
42 int
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;
50 while ( 1 ) {
51 int opt = getopt( argc, argv, "d:" );
53 if ( opt == EOF ) {
54 break;
57 switch ( opt ) {
58 case 'd':
59 debug = atoi( optarg );
60 break;
64 optind--;
65 argc -= optind;
66 argv += optind;
68 if ( argc < 2 ) {
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" );
73 return( 0 );
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 ) {
84 size_t len;
86 fgets( buf, sizeof( buf ), stdin );
87 len = strlen( buf );
88 if ( len > 0 && buf[ --len ] == '\n' ) {
89 buf[ len ] = '\0';
91 strin = buf;
92 } else {
93 strin = argv[ 1 ];
96 if ( argc >= 3 ) {
97 for ( i = 0; i < argc - 2; i++ ) {
98 char *s, *e;
99 for ( s = argv[ 2 + i ]; s; s = e ) {
100 e = strchr( s, ',' );
101 if ( e != NULL ) {
102 e[ 0 ] = '\0';
103 e++;
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;
132 f2 = 1;
134 rc = ldap_str2dn( strin, &dn, flags[ 0 ] );
136 if ( rc == LDAP_SUCCESS ) {
137 int i;
138 if ( dn ) {
139 for ( i = 0; dn[ i ]; i++ ) {
140 LDAPRDN rdn = dn[ i ];
141 char *rstr = NULL;
143 if ( ldap_rdn2str( rdn, &rstr, flags[ f2 ] ) ) {
144 fprintf( stdout, "\tldap_rdn2str() failed\n" );
145 continue;
148 fprintf( stdout, "\tldap_rdn2str() = \"%s\"\n", rstr );
149 ldap_memfree( rstr );
151 } else {
152 fprintf( stdout, "\tempty DN\n" );
156 str = NULL;
157 if ( rc == LDAP_SUCCESS &&
158 ldap_dn2str( dn, &str, flags[ f2 ] ) == LDAP_SUCCESS )
160 char **values, *tmp, *tmp2, *str2 = NULL;
161 int n;
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:
169 return( 0 );
171 case LDAP_DN_FORMAT_LDAPV3:
172 case LDAP_DN_FORMAT_LDAPV2:
173 n = ldap_dn2domain( strin, &tmp );
174 if ( n ) {
175 fprintf( stdout, "\nldap_dn2domain(\"%s\") FAILED\n", strin );
176 } else {
177 fprintf( stdout, "\nldap_dn2domain(\"%s\")\n"
178 "\t= \"%s\"\n", strin, tmp ? tmp : "" );
180 ldap_memfree( tmp );
182 tmp = ldap_dn2ufn( strin );
183 fprintf( stdout, "\nldap_dn2ufn(\"%s\")\n"
184 "\t= \"%s\"\n", strin, tmp ? tmp : "" );
185 ldap_memfree( 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"
192 "\t= \"%s\"\n",
193 tmp ? tmp : "", tmp2 ? tmp2 : "" );
194 ldap_memfree( tmp );
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 : "" );
200 ldap_memfree( 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++ ) {
205 char **vv;
206 int nn;
208 fprintf( stdout, "\t\"%s\"\n", values[ n ] );
210 fprintf( stdout, "\tldap_explode_rdn(\"%s\")\n",
211 values[ n ] );
212 vv = ldap_explode_rdn( values[ n ], 0 );
213 for ( nn = 0; vv && vv[ nn ]; nn++ ) {
214 fprintf( stdout, "\t\t'%s'\n",
215 vv[ nn ] );
217 LDAP_VFREE( vv );
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",
224 vv[ nn ] );
226 LDAP_VFREE( vv );
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 );
239 break;
242 dn2 = NULL;
243 rc = ldap_str2dn( str, &dn2, flags[ f2 ] );
244 str2 = NULL;
245 if ( rc == LDAP_SUCCESS &&
246 ldap_dn2str( dn2, &str2, flags[ f2 ] )
247 == LDAP_SUCCESS ) {
248 int iRDN;
250 fprintf( stdout, "\n\"%s\"\n\t == \"%s\" ? %s\n",
251 str, str2,
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 ];
261 int iAVA;
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",
269 iAVA + 1, iRDN + 1,
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",
279 iAVA + 1, iRDN + 1,
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 );
288 ldap_dnfree( dn2 );
289 ldap_memfree( str2 );
291 ldap_memfree( str );
293 ldap_dnfree( dn );
295 /* note: dn is not freed */
297 return( 0 );