xref: /illumos-gate/usr/src/cmd/ldap/common/etest.c (revision 2a8bcb4e)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  *
35e45752aSstevel  * Portions Copyright 1998 Sun Microsystems, Inc.  All rights reserved.
45e45752aSstevel  * Use is subject to license terms.
57c478bd9Sstevel@tonic-gate  *
67c478bd9Sstevel@tonic-gate  */
7*2a8bcb4eSToomas Soome 
87c478bd9Sstevel@tonic-gate /* test.c - lber encoding test program */
97c478bd9Sstevel@tonic-gate /*
107c478bd9Sstevel@tonic-gate  * Copyright (c) 1990 Regents of the University of Michigan.
117c478bd9Sstevel@tonic-gate  * All rights reserved.
127c478bd9Sstevel@tonic-gate  */
137c478bd9Sstevel@tonic-gate 
147c478bd9Sstevel@tonic-gate #include <stdio.h>
157c478bd9Sstevel@tonic-gate #include <string.h>
167c478bd9Sstevel@tonic-gate #ifdef MACOS
177c478bd9Sstevel@tonic-gate #include <stdlib.h>
187c478bd9Sstevel@tonic-gate #include <unix.h>
197c478bd9Sstevel@tonic-gate #include <fcntl.h>
207c478bd9Sstevel@tonic-gate #include <console.h>
217c478bd9Sstevel@tonic-gate #else /* MACOS */
227c478bd9Sstevel@tonic-gate #include <sys/types.h>
237c478bd9Sstevel@tonic-gate #include <sys/socket.h>
247c478bd9Sstevel@tonic-gate #endif /* MACOS */
257c478bd9Sstevel@tonic-gate #include "lber.h"
267c478bd9Sstevel@tonic-gate 
usage(char * name)277c478bd9Sstevel@tonic-gate static usage( char *name )
287c478bd9Sstevel@tonic-gate {
297c478bd9Sstevel@tonic-gate 	fprintf( stderr, "usage: %s fmtstring\n", name );
307c478bd9Sstevel@tonic-gate }
317c478bd9Sstevel@tonic-gate 
main(int argc,char ** argv)327c478bd9Sstevel@tonic-gate main( int argc, char **argv )
337c478bd9Sstevel@tonic-gate {
347c478bd9Sstevel@tonic-gate 	int		i, num, len;
357c478bd9Sstevel@tonic-gate 	char		*s, *p;
367c478bd9Sstevel@tonic-gate 	Seqorset	*sos = NULLSEQORSET;
377c478bd9Sstevel@tonic-gate 	BerElement	*ber;
387c478bd9Sstevel@tonic-gate 	Sockbuf		sb;
397c478bd9Sstevel@tonic-gate 	extern char	*optarg;
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate 	if ( argc < 2 ) {
427c478bd9Sstevel@tonic-gate 		usage( argv[0] );
437c478bd9Sstevel@tonic-gate 		exit( 1 );
447c478bd9Sstevel@tonic-gate 	}
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate 	bzero( &sb, sizeof(sb) );
477c478bd9Sstevel@tonic-gate 	sb.sb_sd = 1;
487c478bd9Sstevel@tonic-gate 	sb.sb_ber.ber_buf = NULL;
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate #ifdef MACOS
517c478bd9Sstevel@tonic-gate 	ccommand( &argv );
527c478bd9Sstevel@tonic-gate 	cshow( stdout );
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate        if (( sb.sb_sd = open( "lber-test", O_WRONLY|O_CREAT|O_TRUNC|O_BINARY ))
557c478bd9Sstevel@tonic-gate 		< 0 ) {
567c478bd9Sstevel@tonic-gate 	    perror( "open" );
577c478bd9Sstevel@tonic-gate 	    exit( 1 );
587c478bd9Sstevel@tonic-gate 	}
597c478bd9Sstevel@tonic-gate #endif /* MACOS */
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate 	if ( (ber = ber_alloc()) == NULLBER ) {
627c478bd9Sstevel@tonic-gate 		perror( "ber_alloc" );
637c478bd9Sstevel@tonic-gate 		exit( 1 );
647c478bd9Sstevel@tonic-gate 	}
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate 	num = 7;
677c478bd9Sstevel@tonic-gate 	if ( ber_printf( ber, "{ti}", 0x1f44, num ) == -1 ) {
687c478bd9Sstevel@tonic-gate 		fprintf( stderr, "ber_printf returns -1" );
697c478bd9Sstevel@tonic-gate 		exit( 1 );
707c478bd9Sstevel@tonic-gate 	}
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate 	if ( ber_flush( &sb, ber, 1 ) == -1 ) {
737c478bd9Sstevel@tonic-gate 		perror( "ber_flush" );
747c478bd9Sstevel@tonic-gate 		exit( 1 );
757c478bd9Sstevel@tonic-gate 	}
767c478bd9Sstevel@tonic-gate #ifdef notdef
777c478bd9Sstevel@tonic-gate 	for ( s = argv[1]; *s; s++ ) {
787c478bd9Sstevel@tonic-gate 		if ( fgets( buf, sizeof(buf), stdin ) == NULL )
797c478bd9Sstevel@tonic-gate 			break;
807c478bd9Sstevel@tonic-gate 		if ( (p = strchr( buf, '\n' )) != NULL )
817c478bd9Sstevel@tonic-gate 			*p = '\0';
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate 		switch ( *s ) {
847c478bd9Sstevel@tonic-gate 		case 'i':	/* int */
857c478bd9Sstevel@tonic-gate 		case 'b':	/* boolean */
867c478bd9Sstevel@tonic-gate 			i = atoi( buf );
877c478bd9Sstevel@tonic-gate 			if ( ber_printf( ber, "i", i ) == -1 ) {
887c478bd9Sstevel@tonic-gate 				fprintf( stderr, "ber_printf i\n" );
897c478bd9Sstevel@tonic-gate 				exit( 1 );
907c478bd9Sstevel@tonic-gate 			}
917c478bd9Sstevel@tonic-gate 			break;
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate 		case 'e':	/* enumeration */
947c478bd9Sstevel@tonic-gate 			i = va_arg( ap, int );
957c478bd9Sstevel@tonic-gate 			rc = ber_put_enum( ber, i, (char)ber->ber_tag );
967c478bd9Sstevel@tonic-gate 			break;
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate 		case 'n':	/* null */
997c478bd9Sstevel@tonic-gate 			rc = ber_put_null( ber, (char)ber->ber_tag );
1007c478bd9Sstevel@tonic-gate 			break;
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate 		case 'o':	/* octet string (non-null terminated) */
1037c478bd9Sstevel@tonic-gate 			s = va_arg( ap, char * );
1047c478bd9Sstevel@tonic-gate 			len = va_arg( ap, int );
1057c478bd9Sstevel@tonic-gate 			rc = ber_put_ostring( ber, s, len, (char)ber->ber_tag );
1067c478bd9Sstevel@tonic-gate 			break;
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate 		case 's':	/* string */
1097c478bd9Sstevel@tonic-gate 			s = va_arg( ap, char * );
1107c478bd9Sstevel@tonic-gate 			rc = ber_put_string( ber, s, (char)ber->ber_tag );
1117c478bd9Sstevel@tonic-gate 			break;
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate 		case 'B':	/* bit string */
1147c478bd9Sstevel@tonic-gate 			s = va_arg( ap, char * );
1157c478bd9Sstevel@tonic-gate 			len = va_arg( ap, int );	/* in bits */
1167c478bd9Sstevel@tonic-gate 			rc = ber_put_bitstring( ber, s, len, (char)ber->ber_tag );
1177c478bd9Sstevel@tonic-gate 			break;
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate 		case 't':	/* tag for the next element */
1207c478bd9Sstevel@tonic-gate 			ber->ber_tag = va_arg( ap, int );
1217c478bd9Sstevel@tonic-gate 			ber->ber_usertag = 1;
1227c478bd9Sstevel@tonic-gate 			break;
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate 		case 'v':	/* vector of strings */
1257c478bd9Sstevel@tonic-gate 			if ( (ss = va_arg( ap, char ** )) == NULL )
1267c478bd9Sstevel@tonic-gate 				break;
1277c478bd9Sstevel@tonic-gate 			for ( i = 0; ss[i] != NULL; i++ ) {
1287c478bd9Sstevel@tonic-gate 				if ( (rc = ber_put_string( ber, ss[i],
1297c478bd9Sstevel@tonic-gate 				    (char)ber->ber_tag )) == -1 )
1307c478bd9Sstevel@tonic-gate 					break;
1317c478bd9Sstevel@tonic-gate 			}
1327c478bd9Sstevel@tonic-gate 			break;
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate 		case 'V':	/* sequences of strings + lengths */
1357c478bd9Sstevel@tonic-gate 			if ( (bv = va_arg( ap, struct berval ** )) == NULL )
1367c478bd9Sstevel@tonic-gate 				break;
1377c478bd9Sstevel@tonic-gate 			for ( i = 0; bv[i] != NULL; i++ ) {
1387c478bd9Sstevel@tonic-gate 				if ( (rc = ber_put_ostring( ber, bv[i]->bv_val,
1397c478bd9Sstevel@tonic-gate 				    bv[i]->bv_len, (char)ber->ber_tag )) == -1 )
1407c478bd9Sstevel@tonic-gate 					break;
1417c478bd9Sstevel@tonic-gate 			}
1427c478bd9Sstevel@tonic-gate 			break;
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate 		case '{':	/* begin sequence */
1457c478bd9Sstevel@tonic-gate 			rc = ber_start_seq( ber, (char)ber->ber_tag );
1467c478bd9Sstevel@tonic-gate 			break;
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate 		case '}':	/* end sequence */
1497c478bd9Sstevel@tonic-gate 			rc = ber_put_seqorset( ber );
1507c478bd9Sstevel@tonic-gate 			break;
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate 		case '[':	/* begin set */
1537c478bd9Sstevel@tonic-gate 			rc = ber_start_set( ber, (char)ber->ber_tag );
1547c478bd9Sstevel@tonic-gate 			break;
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate 		case ']':	/* end set */
1577c478bd9Sstevel@tonic-gate 			rc = ber_put_seqorset( ber );
1587c478bd9Sstevel@tonic-gate 			break;
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate 		default:
1617c478bd9Sstevel@tonic-gate #ifndef NO_USERINTERFACE
1627c478bd9Sstevel@tonic-gate 			fprintf( stderr, "unknown fmt %c\n", *fmt );
1637c478bd9Sstevel@tonic-gate #endif /* NO_USERINTERFACE */
1647c478bd9Sstevel@tonic-gate 			rc = -1;
1657c478bd9Sstevel@tonic-gate 			break;
1667c478bd9Sstevel@tonic-gate 		}
1677c478bd9Sstevel@tonic-gate 		}
1687c478bd9Sstevel@tonic-gate 	}
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate #endif
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate 	return( 0 );
1737c478bd9Sstevel@tonic-gate }
174