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