]> git.sur5r.net Git - openldap/blob - libraries/liblber/etest.c
cb532f440255b786187962698d68962f189ddb2f
[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 #include <stdlib.h>
15
16 #include <ac/socket.h>
17 #include <ac/string.h>
18 #include <ac/unistd.h>
19
20 #ifdef HAVE_CONSOLE_H
21 #include <console.h>
22 #endif /* HAVE_CONSOLE_H */
23
24 #include "lber.h"
25
26 static void usage( char *name )
27 {
28         fprintf( stderr, "usage: %s fmtstring\n", name );
29 }
30
31 static char* getbuf() {
32         char *p;
33         static char buf[128];
34
35         if ( fgets( buf, sizeof(buf), stdin ) == NULL )
36                 return NULL;
37
38         if ( (p = strchr( buf, '\n' )) != NULL )
39                 *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 /* MACOS */
75
76         sb = ber_sockbuf_alloc_fd( fd );
77
78         if( sb == NULL ) {
79                 perror( "lber_sockbuf_alloc_fd" );
80                 return( EXIT_FAILURE );
81         }
82
83         if ( (ber = ber_alloc_t( LBER_USE_DER )) == NULL ) {
84                 perror( "ber_alloc" );
85                 return( EXIT_FAILURE );
86         }
87
88         fprintf(stderr, "encode: start\n" );
89         if( ber_printf( ber, "{" /*}*/ ) ) {
90                 perror( "ber_printf {" /*}*/ );
91                 return( EXIT_FAILURE );
92         }
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 #ifdef LDAP_LIBUI
132                         fprintf( stderr, "unknown fmt %c\n", *fmt );
133 #endif /* LDAP_LIBUI */
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, /*{*/ "}" ) == -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 }