]> git.sur5r.net Git - openldap/blob - libraries/liblber/etest.c
Remove wrapping sequence to align with dtest
[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 )
38                 return NULL;
39
40         if ( (p = strchr( buf, '\n' )) != NULL )
41                 *p = '\0';
42
43         return buf;
44 }
45
46 int
47 main( int argc, char **argv )
48 {
49         char    *s;
50
51         int                     fd, rc;
52         BerElement      *ber;
53         Sockbuf         *sb;
54
55         /* enable debugging */
56         int ival = -1;
57         ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &ival );
58
59         if ( argc < 2 ) {
60                 usage( argv[0] );
61                 return( EXIT_FAILURE );
62         }
63
64 #ifdef HAVE_CONSOLE_H
65         ccommand( &argv );
66         cshow( stdout );
67
68         if (( fd = open( "lber-test", O_WRONLY|O_CREAT|O_TRUNC|O_BINARY ))
69                 < 0 ) {
70             perror( "open" );
71             return( EXIT_FAILURE );
72         }
73
74 #else
75         fd = fileno(stdout);
76 #endif
77
78         sb = ber_sockbuf_alloc();
79         ber_sockbuf_add_io( sb, &ber_sockbuf_io_fd, LBER_SBIOD_LEVEL_PROVIDER,
80                 (void *)&fd );
81
82         if( sb == NULL ) {
83                 perror( "ber_sockbuf_alloc_fd" );
84                 return( EXIT_FAILURE );
85         }
86
87         if ( (ber = ber_alloc_t( LBER_USE_DER )) == NULL ) {
88                 perror( "ber_alloc" );
89                 return( EXIT_FAILURE );
90         }
91
92         fprintf(stderr, "encode: start\n" );
93
94         for ( s = argv[1]; *s; s++ ) {
95                 char *buf;
96                 char fmt[2];
97
98                 fmt[0] = *s;
99                 fmt[1] = '\0';
100
101                 fprintf(stderr, "encode: %s\n", fmt );
102                 switch ( *s ) {
103                 case 'i':       /* int */
104                 case 'b':       /* boolean */
105                 case 'e':       /* enumeration */
106                         buf = getbuf();
107                         rc = ber_printf( ber, fmt, atoi(buf) );
108                         break;
109
110                 case 'n':       /* null */
111                 case '{':       /* begin sequence */
112                 case '}':       /* end sequence */
113                 case '[':       /* begin set */
114                 case ']':       /* end set */
115                         rc = ber_printf( ber, fmt );
116                         break;
117
118                 case 'o':       /* octet string (non-null terminated) */
119                 case 'B':       /* bit string */
120                         buf = getbuf();
121                         rc = ber_printf( ber, fmt, buf, strlen(buf) );
122                         break;
123
124                 case 's':       /* string */
125                 case 't':       /* tag for the next element */
126                         buf = getbuf();
127                         rc = ber_printf( ber, fmt, buf );
128                         break;
129
130                 default:
131                         fprintf( stderr, "encode: unknown fmt %c\n", *fmt );
132                         rc = -1;
133                         break;
134                 }
135
136                 if( rc == -1 ) {
137                         perror( "ber_printf" );
138                         return( EXIT_FAILURE );
139                 }
140         }
141
142         fprintf(stderr, "encode: end\n" );
143
144         if ( ber_flush( sb, ber, 1 ) == -1 ) {
145                 perror( "ber_flush" );
146                 return( EXIT_FAILURE );
147         }
148
149         ber_sockbuf_free( sb );
150         return( EXIT_SUCCESS );
151 }