]> git.sur5r.net Git - openldap/blob - libraries/liblber/etest.c
Regarding previous commit:
[openldap] / libraries / liblber / etest.c
1 /* test.c - lber encoding test program */
2 /*
3  * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6 /* Portions
7  * Copyright (c) 1990 Regents of the University of Michigan.
8  * All rights reserved.
9  */
10
11 #include "portable.h"
12
13 #include <stdio.h>
14
15 #include <ac/stdlib.h>
16
17 #include <ac/socket.h>
18 #include <ac/string.h>
19 #include <ac/unistd.h>
20
21 #ifdef HAVE_CONSOLE_H
22 #include <console.h>
23 #endif /* HAVE_CONSOLE_H */
24
25 #include "lber.h"
26
27 static void usage( char *name )
28 {
29         fprintf( stderr, "usage: %s fmtstring\n", name );
30 }
31
32 static char* getbuf() {
33         char *p;
34         static char buf[128];
35
36         if ( fgets( buf, sizeof(buf), stdin ) == NULL )
37                 return NULL;
38
39         if ( (p = strchr( buf, '\n' )) != NULL )
40                 *p = '\0';
41
42         return buf;
43 }
44
45 int
46 main( int argc, char **argv )
47 {
48         char    *s;
49
50         int                     fd, rc;
51         BerElement      *ber;
52         Sockbuf         *sb;
53
54         /* enable debugging */
55         int ival = -1;
56         ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &ival );
57
58         if ( argc < 2 ) {
59                 usage( argv[0] );
60                 return( EXIT_FAILURE );
61         }
62
63 #ifdef HAVE_CONSOLE_H
64         ccommand( &argv );
65         cshow( stdout );
66
67         if (( fd = open( "lber-test", O_WRONLY|O_CREAT|O_TRUNC|O_BINARY ))
68                 < 0 ) {
69             perror( "open" );
70             return( EXIT_FAILURE );
71         }
72
73 #else
74         fd = fileno(stdout);
75 #endif
76
77         sb = ber_sockbuf_alloc_fd( fd );
78
79         if( sb == NULL ) {
80                 perror( "lber_sockbuf_alloc_fd" );
81                 return( EXIT_FAILURE );
82         }
83
84         if ( (ber = ber_alloc_t( LBER_USE_DER )) == NULL ) {
85                 perror( "ber_alloc" );
86                 return( EXIT_FAILURE );
87         }
88
89         fprintf(stderr, "encode: start\n" );
90         if( ber_printf( ber, "{" /*}*/ ) ) {
91                 perror( "ber_printf {" /*}*/ );
92                 return( EXIT_FAILURE );
93         }
94
95         for ( s = argv[1]; *s; s++ ) {
96                 char *buf;
97                 char fmt[2];
98
99                 fmt[0] = *s;
100                 fmt[1] = '\0';
101
102                 fprintf(stderr, "encode: %s\n", fmt );
103                 switch ( *s ) {
104                 case 'i':       /* int */
105                 case 'b':       /* boolean */
106                 case 'e':       /* enumeration */
107                         buf = getbuf();
108                         rc = ber_printf( ber, fmt, atoi(buf) );
109                         break;
110
111                 case 'n':       /* null */
112                 case '{':       /* begin sequence */
113                 case '}':       /* end sequence */
114                 case '[':       /* begin set */
115                 case ']':       /* end set */
116                         rc = ber_printf( ber, fmt );
117                         break;
118
119                 case 'o':       /* octet string (non-null terminated) */
120                 case 'B':       /* bit string */
121                         buf = getbuf();
122                         rc = ber_printf( ber, fmt, buf, strlen(buf) );
123                         break;
124
125                 case 's':       /* string */
126                 case 't':       /* tag for the next element */
127                         buf = getbuf();
128                         rc = ber_printf( ber, fmt, buf );
129                         break;
130
131                 default:
132                         fprintf( stderr, "encode: unknown fmt %c\n", *fmt );
133                         rc = -1;
134                         break;
135                 }
136
137                 if( rc == -1 ) {
138                         perror( "ber_printf" );
139                         return( EXIT_FAILURE );
140                 }
141         }
142
143         fprintf(stderr, "encode: end\n" );
144         if( ber_printf( ber, /*{*/ "}" ) == -1 ) {
145                 perror( /*{*/ "ber_printf }" );
146                 return( EXIT_FAILURE );
147         }
148
149         if ( ber_flush( sb, ber, 1 ) == -1 ) {
150                 perror( "ber_flush" );
151                 return( EXIT_FAILURE );
152         }
153
154         ber_sockbuf_free( sb );
155         return( EXIT_SUCCESS );
156 }