]> git.sur5r.net Git - openldap/blob - libraries/liblber/etest.c
0099e82ff52638869eb77709a7c57d2f2432cb73
[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-int.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         ber_int_debug = -1;
55
56         if ( argc < 2 ) {
57                 usage( argv[0] );
58                 return( EXIT_FAILURE );
59         }
60
61 #ifdef HAVE_CONSOLE_H
62         ccommand( &argv );
63         cshow( stdout );
64
65         if (( fd = open( "lber-test", O_WRONLY|O_CREAT|O_TRUNC|O_BINARY ))
66                 < 0 ) {
67             perror( "open" );
68             return( EXIT_FAILURE );
69         }
70
71 #else
72         fd = fileno(stdout);
73 #endif /* MACOS */
74
75         sb = ber_sockbuf_alloc_fd( fd );
76
77         if( sb == NULL ) {
78                 perror( "lber_sockbuf_alloc_fd" );
79                 return( EXIT_FAILURE );
80         }
81
82         if ( (ber = ber_alloc_t( LBER_USE_DER )) == NULL ) {
83                 perror( "ber_alloc" );
84                 return( EXIT_FAILURE );
85         }
86
87         fprintf(stderr, "encode: start\n" );
88         if( ber_printf( ber, "{" /*}*/ ) ) {
89                 perror( "ber_printf {" /*}*/ );
90                 return( EXIT_FAILURE );
91         }
92
93         for ( s = argv[1]; *s; s++ ) {
94                 char *buf;
95                 char fmt[2];
96
97                 fmt[0] = *s;
98                 fmt[1] = '\0';
99
100                 fprintf(stderr, "encode: %s\n", fmt );
101                 switch ( *s ) {
102                 case 'i':       /* int */
103                 case 'b':       /* boolean */
104                 case 'e':       /* enumeration */
105                         buf = getbuf();
106                         rc = ber_printf( ber, fmt, atoi(buf) );
107                         break;
108
109                 case 'n':       /* null */
110                 case '{':       /* begin sequence */
111                 case '}':       /* end sequence */
112                 case '[':       /* begin set */
113                 case ']':       /* end set */
114                         rc = ber_printf( ber, fmt );
115                         break;
116
117                 case 'o':       /* octet string (non-null terminated) */
118                 case 'B':       /* bit string */
119                         buf = getbuf();
120                         rc = ber_printf( ber, fmt, buf, strlen(buf) );
121                         break;
122
123                 case 's':       /* string */
124                 case 't':       /* tag for the next element */
125                         buf = getbuf();
126                         rc = ber_printf( ber, fmt, buf );
127                         break;
128
129                 default:
130 #ifdef LDAP_LIBUI
131                         fprintf( stderr, "unknown fmt %c\n", *fmt );
132 #endif /* LDAP_LIBUI */
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 }