xref: /onnv-gate/usr/src/cmd/ldap/common/etest.c (revision 3857:21b9b714e4ab)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  *
3*3857Sstevel  * Portions Copyright 1998 Sun Microsystems, Inc.  All rights reserved.
4*3857Sstevel  * Use is subject to license terms.
50Sstevel@tonic-gate  *
60Sstevel@tonic-gate  */
70Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
80Sstevel@tonic-gate /* test.c - lber encoding test program */
90Sstevel@tonic-gate /*
100Sstevel@tonic-gate  * Copyright (c) 1990 Regents of the University of Michigan.
110Sstevel@tonic-gate  * All rights reserved.
120Sstevel@tonic-gate  */
130Sstevel@tonic-gate 
140Sstevel@tonic-gate #include <stdio.h>
150Sstevel@tonic-gate #include <string.h>
160Sstevel@tonic-gate #ifdef MACOS
170Sstevel@tonic-gate #include <stdlib.h>
180Sstevel@tonic-gate #include <unix.h>
190Sstevel@tonic-gate #include <fcntl.h>
200Sstevel@tonic-gate #include <console.h>
210Sstevel@tonic-gate #else /* MACOS */
220Sstevel@tonic-gate #include <sys/types.h>
230Sstevel@tonic-gate #include <sys/socket.h>
240Sstevel@tonic-gate #endif /* MACOS */
250Sstevel@tonic-gate #include "lber.h"
260Sstevel@tonic-gate 
usage(char * name)270Sstevel@tonic-gate static usage( char *name )
280Sstevel@tonic-gate {
290Sstevel@tonic-gate 	fprintf( stderr, "usage: %s fmtstring\n", name );
300Sstevel@tonic-gate }
310Sstevel@tonic-gate 
main(int argc,char ** argv)320Sstevel@tonic-gate main( int argc, char **argv )
330Sstevel@tonic-gate {
340Sstevel@tonic-gate 	int		i, num, len;
350Sstevel@tonic-gate 	char		*s, *p;
360Sstevel@tonic-gate 	Seqorset	*sos = NULLSEQORSET;
370Sstevel@tonic-gate 	BerElement	*ber;
380Sstevel@tonic-gate 	Sockbuf		sb;
390Sstevel@tonic-gate 	extern char	*optarg;
400Sstevel@tonic-gate 
410Sstevel@tonic-gate 	if ( argc < 2 ) {
420Sstevel@tonic-gate 		usage( argv[0] );
430Sstevel@tonic-gate 		exit( 1 );
440Sstevel@tonic-gate 	}
450Sstevel@tonic-gate 
460Sstevel@tonic-gate 	bzero( &sb, sizeof(sb) );
470Sstevel@tonic-gate 	sb.sb_sd = 1;
480Sstevel@tonic-gate 	sb.sb_ber.ber_buf = NULL;
490Sstevel@tonic-gate 
500Sstevel@tonic-gate #ifdef MACOS
510Sstevel@tonic-gate 	ccommand( &argv );
520Sstevel@tonic-gate 	cshow( stdout );
530Sstevel@tonic-gate 
540Sstevel@tonic-gate        if (( sb.sb_sd = open( "lber-test", O_WRONLY|O_CREAT|O_TRUNC|O_BINARY ))
550Sstevel@tonic-gate 		< 0 ) {
560Sstevel@tonic-gate 	    perror( "open" );
570Sstevel@tonic-gate 	    exit( 1 );
580Sstevel@tonic-gate 	}
590Sstevel@tonic-gate #endif /* MACOS */
600Sstevel@tonic-gate 
610Sstevel@tonic-gate 	if ( (ber = ber_alloc()) == NULLBER ) {
620Sstevel@tonic-gate 		perror( "ber_alloc" );
630Sstevel@tonic-gate 		exit( 1 );
640Sstevel@tonic-gate 	}
650Sstevel@tonic-gate 
660Sstevel@tonic-gate 	num = 7;
670Sstevel@tonic-gate 	if ( ber_printf( ber, "{ti}", 0x1f44, num ) == -1 ) {
680Sstevel@tonic-gate 		fprintf( stderr, "ber_printf returns -1" );
690Sstevel@tonic-gate 		exit( 1 );
700Sstevel@tonic-gate 	}
710Sstevel@tonic-gate 
720Sstevel@tonic-gate 	if ( ber_flush( &sb, ber, 1 ) == -1 ) {
730Sstevel@tonic-gate 		perror( "ber_flush" );
740Sstevel@tonic-gate 		exit( 1 );
750Sstevel@tonic-gate 	}
760Sstevel@tonic-gate #ifdef notdef
770Sstevel@tonic-gate 	for ( s = argv[1]; *s; s++ ) {
780Sstevel@tonic-gate 		if ( fgets( buf, sizeof(buf), stdin ) == NULL )
790Sstevel@tonic-gate 			break;
800Sstevel@tonic-gate 		if ( (p = strchr( buf, '\n' )) != NULL )
810Sstevel@tonic-gate 			*p = '\0';
820Sstevel@tonic-gate 
830Sstevel@tonic-gate 		switch ( *s ) {
840Sstevel@tonic-gate 		case 'i':	/* int */
850Sstevel@tonic-gate 		case 'b':	/* boolean */
860Sstevel@tonic-gate 			i = atoi( buf );
870Sstevel@tonic-gate 			if ( ber_printf( ber, "i", i ) == -1 ) {
880Sstevel@tonic-gate 				fprintf( stderr, "ber_printf i\n" );
890Sstevel@tonic-gate 				exit( 1 );
900Sstevel@tonic-gate 			}
910Sstevel@tonic-gate 			break;
920Sstevel@tonic-gate 
930Sstevel@tonic-gate 		case 'e':	/* enumeration */
940Sstevel@tonic-gate 			i = va_arg( ap, int );
950Sstevel@tonic-gate 			rc = ber_put_enum( ber, i, (char)ber->ber_tag );
960Sstevel@tonic-gate 			break;
970Sstevel@tonic-gate 
980Sstevel@tonic-gate 		case 'n':	/* null */
990Sstevel@tonic-gate 			rc = ber_put_null( ber, (char)ber->ber_tag );
1000Sstevel@tonic-gate 			break;
1010Sstevel@tonic-gate 
1020Sstevel@tonic-gate 		case 'o':	/* octet string (non-null terminated) */
1030Sstevel@tonic-gate 			s = va_arg( ap, char * );
1040Sstevel@tonic-gate 			len = va_arg( ap, int );
1050Sstevel@tonic-gate 			rc = ber_put_ostring( ber, s, len, (char)ber->ber_tag );
1060Sstevel@tonic-gate 			break;
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate 		case 's':	/* string */
1090Sstevel@tonic-gate 			s = va_arg( ap, char * );
1100Sstevel@tonic-gate 			rc = ber_put_string( ber, s, (char)ber->ber_tag );
1110Sstevel@tonic-gate 			break;
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate 		case 'B':	/* bit string */
1140Sstevel@tonic-gate 			s = va_arg( ap, char * );
1150Sstevel@tonic-gate 			len = va_arg( ap, int );	/* in bits */
1160Sstevel@tonic-gate 			rc = ber_put_bitstring( ber, s, len, (char)ber->ber_tag );
1170Sstevel@tonic-gate 			break;
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate 		case 't':	/* tag for the next element */
1200Sstevel@tonic-gate 			ber->ber_tag = va_arg( ap, int );
1210Sstevel@tonic-gate 			ber->ber_usertag = 1;
1220Sstevel@tonic-gate 			break;
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate 		case 'v':	/* vector of strings */
1250Sstevel@tonic-gate 			if ( (ss = va_arg( ap, char ** )) == NULL )
1260Sstevel@tonic-gate 				break;
1270Sstevel@tonic-gate 			for ( i = 0; ss[i] != NULL; i++ ) {
1280Sstevel@tonic-gate 				if ( (rc = ber_put_string( ber, ss[i],
1290Sstevel@tonic-gate 				    (char)ber->ber_tag )) == -1 )
1300Sstevel@tonic-gate 					break;
1310Sstevel@tonic-gate 			}
1320Sstevel@tonic-gate 			break;
1330Sstevel@tonic-gate 
1340Sstevel@tonic-gate 		case 'V':	/* sequences of strings + lengths */
1350Sstevel@tonic-gate 			if ( (bv = va_arg( ap, struct berval ** )) == NULL )
1360Sstevel@tonic-gate 				break;
1370Sstevel@tonic-gate 			for ( i = 0; bv[i] != NULL; i++ ) {
1380Sstevel@tonic-gate 				if ( (rc = ber_put_ostring( ber, bv[i]->bv_val,
1390Sstevel@tonic-gate 				    bv[i]->bv_len, (char)ber->ber_tag )) == -1 )
1400Sstevel@tonic-gate 					break;
1410Sstevel@tonic-gate 			}
1420Sstevel@tonic-gate 			break;
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate 		case '{':	/* begin sequence */
1450Sstevel@tonic-gate 			rc = ber_start_seq( ber, (char)ber->ber_tag );
1460Sstevel@tonic-gate 			break;
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate 		case '}':	/* end sequence */
1490Sstevel@tonic-gate 			rc = ber_put_seqorset( ber );
1500Sstevel@tonic-gate 			break;
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate 		case '[':	/* begin set */
1530Sstevel@tonic-gate 			rc = ber_start_set( ber, (char)ber->ber_tag );
1540Sstevel@tonic-gate 			break;
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate 		case ']':	/* end set */
1570Sstevel@tonic-gate 			rc = ber_put_seqorset( ber );
1580Sstevel@tonic-gate 			break;
1590Sstevel@tonic-gate 
1600Sstevel@tonic-gate 		default:
1610Sstevel@tonic-gate #ifndef NO_USERINTERFACE
1620Sstevel@tonic-gate 			fprintf( stderr, "unknown fmt %c\n", *fmt );
1630Sstevel@tonic-gate #endif /* NO_USERINTERFACE */
1640Sstevel@tonic-gate 			rc = -1;
1650Sstevel@tonic-gate 			break;
1660Sstevel@tonic-gate 		}
1670Sstevel@tonic-gate 		}
1680Sstevel@tonic-gate 	}
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate #endif
1710Sstevel@tonic-gate 
1720Sstevel@tonic-gate 	return( 0 );
1730Sstevel@tonic-gate }
174