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