xref: /titanic_52/usr/src/cmd/ldap/common/etest.c (revision 24fe0b3bf671e123467ce1df0b67cadd3614c8e4)
1 /*
2  *
3  * Portions Copyright 1998 Sun Microsystems, Inc.  All rights reserved.
4  * Use is subject to license terms.
5  *
6  */
7 #pragma ident	"%Z%%M%	%I%	%E% SMI"
8 /* test.c - lber encoding test program */
9 /*
10  * Copyright (c) 1990 Regents of the University of Michigan.
11  * All rights reserved.
12  */
13 
14 #include <stdio.h>
15 #include <string.h>
16 #ifdef MACOS
17 #include <stdlib.h>
18 #include <unix.h>
19 #include <fcntl.h>
20 #include <console.h>
21 #else /* MACOS */
22 #include <sys/types.h>
23 #include <sys/socket.h>
24 #endif /* MACOS */
25 #include "lber.h"
26 
27 static usage( char *name )
28 {
29 	fprintf( stderr, "usage: %s fmtstring\n", name );
30 }
31 
32 main( int argc, char **argv )
33 {
34 	int		i, num, len;
35 	char		*s, *p;
36 	Seqorset	*sos = NULLSEQORSET;
37 	BerElement	*ber;
38 	Sockbuf		sb;
39 	extern char	*optarg;
40 
41 	if ( argc < 2 ) {
42 		usage( argv[0] );
43 		exit( 1 );
44 	}
45 
46 	bzero( &sb, sizeof(sb) );
47 	sb.sb_sd = 1;
48 	sb.sb_ber.ber_buf = NULL;
49 
50 #ifdef MACOS
51 	ccommand( &argv );
52 	cshow( stdout );
53 
54        if (( sb.sb_sd = open( "lber-test", O_WRONLY|O_CREAT|O_TRUNC|O_BINARY ))
55 		< 0 ) {
56 	    perror( "open" );
57 	    exit( 1 );
58 	}
59 #endif /* MACOS */
60 
61 	if ( (ber = ber_alloc()) == NULLBER ) {
62 		perror( "ber_alloc" );
63 		exit( 1 );
64 	}
65 
66 	num = 7;
67 	if ( ber_printf( ber, "{ti}", 0x1f44, num ) == -1 ) {
68 		fprintf( stderr, "ber_printf returns -1" );
69 		exit( 1 );
70 	}
71 
72 	if ( ber_flush( &sb, ber, 1 ) == -1 ) {
73 		perror( "ber_flush" );
74 		exit( 1 );
75 	}
76 #ifdef notdef
77 	for ( s = argv[1]; *s; s++ ) {
78 		if ( fgets( buf, sizeof(buf), stdin ) == NULL )
79 			break;
80 		if ( (p = strchr( buf, '\n' )) != NULL )
81 			*p = '\0';
82 
83 		switch ( *s ) {
84 		case 'i':	/* int */
85 		case 'b':	/* boolean */
86 			i = atoi( buf );
87 			if ( ber_printf( ber, "i", i ) == -1 ) {
88 				fprintf( stderr, "ber_printf i\n" );
89 				exit( 1 );
90 			}
91 			break;
92 
93 		case 'e':	/* enumeration */
94 			i = va_arg( ap, int );
95 			rc = ber_put_enum( ber, i, (char)ber->ber_tag );
96 			break;
97 
98 		case 'n':	/* null */
99 			rc = ber_put_null( ber, (char)ber->ber_tag );
100 			break;
101 
102 		case 'o':	/* octet string (non-null terminated) */
103 			s = va_arg( ap, char * );
104 			len = va_arg( ap, int );
105 			rc = ber_put_ostring( ber, s, len, (char)ber->ber_tag );
106 			break;
107 
108 		case 's':	/* string */
109 			s = va_arg( ap, char * );
110 			rc = ber_put_string( ber, s, (char)ber->ber_tag );
111 			break;
112 
113 		case 'B':	/* bit string */
114 			s = va_arg( ap, char * );
115 			len = va_arg( ap, int );	/* in bits */
116 			rc = ber_put_bitstring( ber, s, len, (char)ber->ber_tag );
117 			break;
118 
119 		case 't':	/* tag for the next element */
120 			ber->ber_tag = va_arg( ap, int );
121 			ber->ber_usertag = 1;
122 			break;
123 
124 		case 'v':	/* vector of strings */
125 			if ( (ss = va_arg( ap, char ** )) == NULL )
126 				break;
127 			for ( i = 0; ss[i] != NULL; i++ ) {
128 				if ( (rc = ber_put_string( ber, ss[i],
129 				    (char)ber->ber_tag )) == -1 )
130 					break;
131 			}
132 			break;
133 
134 		case 'V':	/* sequences of strings + lengths */
135 			if ( (bv = va_arg( ap, struct berval ** )) == NULL )
136 				break;
137 			for ( i = 0; bv[i] != NULL; i++ ) {
138 				if ( (rc = ber_put_ostring( ber, bv[i]->bv_val,
139 				    bv[i]->bv_len, (char)ber->ber_tag )) == -1 )
140 					break;
141 			}
142 			break;
143 
144 		case '{':	/* begin sequence */
145 			rc = ber_start_seq( ber, (char)ber->ber_tag );
146 			break;
147 
148 		case '}':	/* end sequence */
149 			rc = ber_put_seqorset( ber );
150 			break;
151 
152 		case '[':	/* begin set */
153 			rc = ber_start_set( ber, (char)ber->ber_tag );
154 			break;
155 
156 		case ']':	/* end set */
157 			rc = ber_put_seqorset( ber );
158 			break;
159 
160 		default:
161 #ifndef NO_USERINTERFACE
162 			fprintf( stderr, "unknown fmt %c\n", *fmt );
163 #endif /* NO_USERINTERFACE */
164 			rc = -1;
165 			break;
166 		}
167 		}
168 	}
169 
170 #endif
171 
172 	return( 0 );
173 }
174