]> git.sur5r.net Git - openldap/commitdiff
Enhance encode/decode test programs to encode sets, ie:
authorKurt Zeilenga <kurt@openldap.org>
Wed, 19 May 1999 19:10:55 +0000 (19:10 +0000)
committerKurt Zeilenga <kurt@openldap.org>
Wed, 19 May 1999 19:10:55 +0000 (19:10 +0000)
./etest '{sis}' | ./dtest '{sis}'

libraries/liblber/dtest.c
libraries/liblber/etest.c

index 4d84ea5999ff8ae3ef997327508644e551102d42..9fadb37edf48acc147e5727b4cd4580b803f1200 100644 (file)
@@ -38,12 +38,22 @@ static void usage( char *name )
 int
 main( int argc, char **argv )
 {
-       long            i;
-       unsigned long   len;
-       int             tag;
+       char *s;
+       int rc;
+
+       unsigned long tag, len;
+
        BerElement      *ber;
        Sockbuf         *sb;
 
+       /* enable debugging */
+       ber_int_debug = -1;
+
+       if ( argc < 2 ) {
+               usage( argv[0] );
+               return( EXIT_FAILURE );
+       }
+
 #ifdef HAVE_CONSOLE_H
        ccommand( &argv );
        cshow( stdout );
@@ -56,17 +66,28 @@ main( int argc, char **argv )
                return( EXIT_FAILURE );
        }
 
-       if ( (tag = ber_get_next( sb, &len, ber )) == -1 ) {
+       if(( tag = ber_get_next( sb, &len, ber) ) == LBER_ERROR ) {
                perror( "ber_get_next" );
                return( EXIT_FAILURE );
        }
-       printf( "message has tag 0x%x and length %ld\n", tag, len );
 
-       if ( ber_scanf( ber, "i", &i ) == LBER_ERROR ) {
-               fprintf( stderr, "ber_scanf returns -1\n" );
-               exit( EXIT_FAILURE );
+       printf("decode: message tag 0x%lx and length %ld\n",
+               tag, len );
+
+       for( s = argv[1]; *s; s++ ) {
+               char buf[128];
+               char fmt[2];
+               fmt[0] = *s;
+               fmt[1] = '\0';
+
+               printf("decode: format %s\n", fmt );
+               rc = ber_scanf( ber, fmt, buf );
+
+               if( rc == LBER_ERROR ) {
+                       perror( "ber_scanf" );
+                       return( EXIT_FAILURE );
+               }
        }
-       printf( "got int %ld\n", i );
 
        ber_sockbuf_free( sb );
        return( EXIT_SUCCESS );
index a41d9c379d0ac34853ea90eaf79dbcffa49c6ec4..7037b61090d01a512369960fc2953593f88e305d 100644 (file)
 #include <console.h>
 #endif /* HAVE_CONSOLE_H */
 
-#include <lber.h>
+#include "lber-int.h"
 
 static void usage( char *name )
 {
        fprintf( stderr, "usage: %s fmtstring\n", name );
 }
 
+static char* getbuf() {
+       char *p;
+       static char buf[128];
+
+       if ( fgets( buf, sizeof(buf), stdin ) == NULL )
+               return NULL;
+
+       if ( (p = strchr( buf, '\n' )) != NULL )
+               *p = '\0';
+
+       return buf;
+}
+
 int
 main( int argc, char **argv )
 {
-#ifdef notdef
-       int             i, len;
-       char    *s, *p;
-#endif
-       int                     fd, num;
-       Seqorset        *sos = NULL;
+       char    *s;
+
+       int                     fd, rc;
        BerElement      *ber;
        Sockbuf         *sb;
 
+       /* enable debugging */
+       ber_int_debug = -1;
+
        if ( argc < 2 ) {
                usage( argv[0] );
                exit( 1 );
        }
 
-
 #ifdef HAVE_CONSOLE_H
        ccommand( &argv );
        cshow( stdout );
@@ -54,11 +66,12 @@ main( int argc, char **argv )
            perror( "open" );
            exit( 1 );
        }
+
 #else
        fd = fileno(stdout);
 #endif /* MACOS */
 
-       sb = ber_sockbuf_alloc_fd( fd);
+       sb = ber_sockbuf_alloc_fd( fd );
 
        if( sb == NULL ) {
                perror( "lber_sockbuf_alloc_fd" );
@@ -70,95 +83,40 @@ main( int argc, char **argv )
                exit( 1 );
        }
 
-#ifndef notdef
-       num = 7;
-       if ( ber_printf( ber, "{ti}", 0x1f44UL, num ) == -1 ) {
-               fprintf( stderr, "ber_printf returns -1" );
-               exit( 1 );
-       }
-
-#else
        for ( s = argv[1]; *s; s++ ) {
-               if ( fgets( buf, sizeof(buf), stdin ) == NULL )
-                       break;
-               if ( (p = strchr( buf, '\n' )) != NULL )
-                       *p = '\0';
+               char *buf;
+               char fmt[2];
 
+               fmt[0] = *s;
+               fmt[1] = '\0';
+
+               printf("encode: %s\n", fmt );
                switch ( *s ) {
                case 'i':       /* int */
                case 'b':       /* boolean */
-                       i = atoi( buf );
-                       if ( ber_printf( ber, "i", i ) == -1 ) {
-                               fprintf( stderr, "ber_printf i\n" );
-                               exit( 1 );
-                       }
-                       break;
-
                case 'e':       /* enumeration */
-                       i = va_arg( ap, int );
-                       rc = ber_put_enum( ber, i, (char)ber->ber_tag );
+                       buf = getbuf();
+                       rc = ber_printf( ber, fmt, atoi(buf) );
                        break;
 
                case 'n':       /* null */
-                       rc = ber_put_null( ber, (char)ber->ber_tag );
+               case '{':       /* begin sequence */
+               case '}':       /* end sequence */
+               case '[':       /* begin set */
+               case ']':       /* end set */
+                       rc = ber_printf( ber, fmt );
                        break;
 
                case 'o':       /* octet string (non-null terminated) */
-                       s = va_arg( ap, char * );
-                       len = va_arg( ap, int );
-                       rc = ber_put_ostring( ber, s, len, (char)ber->ber_tag );
-                       break;
-
-               case 's':       /* string */
-                       s = va_arg( ap, char * );
-                       rc = ber_put_string( ber, s, (char)ber->ber_tag );
-                       break;
-
                case 'B':       /* bit string */
-                       s = va_arg( ap, char * );
-                       len = va_arg( ap, int );        /* in bits */
-                       rc = ber_put_bitstring( ber, s, len, (char)ber->ber_tag );
+                       buf = getbuf();
+                       rc = ber_printf( ber, fmt, buf, strlen(buf) );
                        break;
 
+               case 's':       /* string */
                case 't':       /* tag for the next element */
-                       ber->ber_tag = va_arg( ap, int );
-                       ber->ber_usertag = 1;
-                       break;
-
-               case 'v':       /* vector of strings */
-                       if ( (ss = va_arg( ap, char ** )) == NULL )
-                               break;
-                       for ( i = 0; ss[i] != NULL; i++ ) {
-                               if ( (rc = ber_put_string( ber, ss[i],
-                                   (char)ber->ber_tag )) == -1 )
-                                       break;
-                       }
-                       break;
-
-               case 'V':       /* sequences of strings + lengths */
-                       if ( (bv = va_arg( ap, struct berval ** )) == NULL )
-                               break;
-                       for ( i = 0; bv[i] != NULL; i++ ) {
-                               if ( (rc = ber_put_ostring( ber, bv[i]->bv_val,
-                                   bv[i]->bv_len, (char)ber->ber_tag )) == -1 )
-                                       break;
-                       }
-                       break;
-
-               case '{':       /* begin sequence */
-                       rc = ber_start_seq( ber, (char)ber->ber_tag );
-                       break;
-
-               case '}':       /* end sequence */
-                       rc = ber_put_seqorset( ber );
-                       break;
-
-               case '[':       /* begin set */
-                       rc = ber_start_set( ber, (char)ber->ber_tag );
-                       break;
-
-               case ']':       /* end set */
-                       rc = ber_put_seqorset( ber );
+                       buf = getbuf();
+                       rc = ber_printf( ber, fmt, buf );
                        break;
 
                default:
@@ -168,9 +126,12 @@ main( int argc, char **argv )
                        rc = -1;
                        break;
                }
+
+               if( rc == -1 ) {
+                       perror( "ber_printf" );
+                       exit( 1 );
                }
        }
-#endif
 
        if ( ber_flush( sb, ber, 1 ) == -1 ) {
                perror( "ber_flush" );