directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From akaras...@apache.org
Subject svn commit: rev 9967 - in incubator/directory/snickers/trunk/ber/src: java/org/apache/snickers/ber/digester/rules java/org/apache/snickers/ber/primitives test/org/apache/snickers/ber test/org/apache/snickers/ber/digester/rules test/org/apache/snickers/ber/primitives
Date Mon, 12 Apr 2004 03:05:17 GMT
Author: akarasulu
Date: Sun Apr 11 20:05:16 2004
New Revision: 9967

Added:
   incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/ber/digester/rules/
   incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/ber/digester/rules/PrimitiveIntDecodeRule.java
   incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/ber/primitives/PrimitiveUtils.java
   incubator/directory/snickers/trunk/ber/src/test/org/apache/snickers/ber/digester/rules/
   incubator/directory/snickers/trunk/ber/src/test/org/apache/snickers/ber/digester/rules/PrimitiveIntDecodeRuleTest.java
   incubator/directory/snickers/trunk/ber/src/test/org/apache/snickers/ber/primitives/
   incubator/directory/snickers/trunk/ber/src/test/org/apache/snickers/ber/primitives/PrimitiveUtilsTest.java
Modified:
   incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/ber/primitives/UniversalTag.java
   incubator/directory/snickers/trunk/ber/src/test/org/apache/snickers/ber/EncodeDecodeTests.java
Log:
o created primitive utility class for primitive encode/decode ops
o created test case for primitive utilities for 100% test coverage
o created rule for accumulating and decoding ASN.1 INTEGER to a Java int
o created test case for PrimitiveIntDecoderRule for 100% test coverage


Added: incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/ber/digester/rules/PrimitiveIntDecodeRule.java
==============================================================================
--- (empty file)
+++ incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/ber/digester/rules/PrimitiveIntDecodeRule.java
Sun Apr 11 20:05:16 2004
@@ -0,0 +1,138 @@
+/*
+ *   Copyright 2004 The Apache Software Foundation
+ *
+ *   Licensed under the Apache License, Version 2.0 (the "License");
+ *   you may not use this file except in compliance with the License.
+ *   You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing, software
+ *   distributed under the License is distributed on an "AS IS" BASIS,
+ *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *   See the License for the specific language governing permissions and
+ *   limitations under the License.
+ *
+ */
+package org.apache.snickers.ber.digester.rules ;
+
+
+import org.apache.snickers.ber.TypeClass ;
+import org.apache.snickers.ber.digester.AbstractRule ;
+import org.apache.snickers.ber.primitives.UniversalTag ;
+import org.apache.snickers.ber.primitives.PrimitiveUtils ;
+
+import java.nio.ByteBuffer ;
+
+
+/**
+ * A rule to Decode a BER encoded ASN.1 INTEGER into a Java primitive int.
+ * <p>
+ * The bytes to form the integer are extracted from the BER value which may
+ * arrive in multiple chunks.  The individual bytes are temporarily stored
+ * within a 4 byte array while incrementing a counter to track the capture.
+ * Once gathered the bytes are decoded into a int in the finish
+ * </p>
+ * <p>
+ * As a side effect once the decode is complete, the primitive value is pushed
+ * onto the primitive int stack to be utilized by other rules later.  If there
+ * is a loss of precision where the ASN.1 INTEGER is larger or smaller than
+ * the maximum or minimum value of a Java primitive integer an exception is
+ * thrown.
+ * </p>
+ *
+ * @author <a href="mailto:directory-dev@incubator.apache.org">Apache Directory Project</a>
+ * @version $Rev$
+ */
+public class PrimitiveIntDecodeRule extends AbstractRule
+{
+    /** the bytes used to form the Java primitive integer */
+    private final byte[] value = new byte[4] ;
+    /** the current 8 bit position to fill in the integer */
+    private int pos ;
+    /** the number of bytes we must read */
+    private int length ;
+
+
+    /* (non-Javadoc)
+     * @see org.apache.snickers.ber.Rule#tag(int, boolean,
+     * org.apache.snickers.ber.TypeClass)
+     */
+    public void tag( int id, boolean isPrimitive, TypeClass typeClass )
+    {
+        if ( id != UniversalTag.INTEGER.getTagId() )
+        {
+            throw new IllegalArgumentException(
+                    "Expecting " + UniversalTag.INTEGER.getName()
+                    + " with an id of " + UniversalTag.INTEGER.getTagId()
+                    + " but instead got a tag id of " + id ) ;
+        }
+    }
+
+
+    /* (non-Javadoc)
+     * @see org.apache.snickers.ber.Rule#length(int)
+     */
+    public void length( int length )
+    {
+        if ( length > 4 || length < 0 )
+        {
+            throw new IllegalArgumentException( "The target primitive for this "
+                + "rule can only hold integers of 32 bits or 4 bytes.  "
+                + "The length of the field however is " + length ) ;
+        }
+
+        this.length = length ;
+    }
+
+
+    /* (non-Javadoc)
+     * @see org.apache.snickers.ber.Rule#value(java.nio.ByteBuffer)
+     */
+    public void value( ByteBuffer buf )
+    {
+        if ( buf == null )
+        {
+            return ;
+        }
+
+        while ( buf.hasRemaining() && ( pos + 1 ) <= length )
+        {
+            value[pos] = buf.get() ;
+            pos++ ;
+        }
+    }
+
+
+    /* (non-Javadoc)
+     * @see org.apache.snickers.ber.Rule#finish()
+     */
+    public void finish()
+    {
+        // decode and push primitive integer onto stack
+        int numba = PrimitiveUtils.decodeInt( this.value, 0, this.length ) ;
+
+        if ( getDigester() != null )
+        {
+            getDigester().pushInt( numba ) ;
+        }
+
+        // cleanup
+        this.pos = 0 ;
+        this.length = 0 ;
+    }
+
+
+    /**
+     * Used for testing to check and see if a value of the byte buffer has
+     * been set after a value event.
+     *
+     * @param bite the byte value to check
+     * @param ii the index to check the byte for in the values array
+     * @return true if the byte value matches the byte value in the array
+     */
+    boolean equals( byte bite, int ii )
+    {
+        return value[ii] == bite ;
+    }
+}

Added: incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/ber/primitives/PrimitiveUtils.java
==============================================================================
--- (empty file)
+++ incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/ber/primitives/PrimitiveUtils.java
Sun Apr 11 20:05:16 2004
@@ -0,0 +1,100 @@
+/*
+ *   Copyright 2004 The Apache Software Foundation
+ *
+ *   Licensed under the Apache License, Version 2.0 (the "License");
+ *   you may not use this file except in compliance with the License.
+ *   You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing, software
+ *   distributed under the License is distributed on an "AS IS" BASIS,
+ *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *   See the License for the specific language governing permissions and
+ *   limitations under the License.
+ *
+ */
+package org.apache.snickers.ber.primitives ;
+
+
+/**
+ * Utilities for decoding and encoding primitive constructs.
+ *
+ * @author <a href="mailto:directory-dev@incubator.apache.org">Apache Directory Project</a>
+ * @version $Rev$
+ */
+public class PrimitiveUtils
+{
+    /**
+     * Decodes a BER encoded ASN.1 INTEGER into a Java primitive int.
+     *
+     * @param bites the bytes containing the encoded ASN.1 INTEGER
+     * @param offset the offset from zero where the bytes begin
+     * @param length the length of the bytes to read from the offset
+     * @return the decoded primitive int or zero if the length is 0
+     * @throws IllegalArgumentException if the length is not within
+     * range [0-4]
+     * @throws IndexOutOfBoundsException if offset is less than 0 or
+     * it is greater than bites.length - length
+     */
+    public static int decodeInt( byte[] bites, int offset, int length )
+    {
+        int value = 0 ;
+
+        if ( length == 0 )
+        {
+            return 0 ;
+        }
+
+        switch( length )
+        {
+            case( 1 ):
+                if ( ( bites[offset+0] & 0x80 ) == 0x80 )
+                {
+                    value |= bites[offset+0] | 0xffffff00 ;
+                }
+                else
+                {
+                    value |= bites[offset+0] ;
+                }
+
+                break ;
+            case( 2 ):
+                if ( ( bites[offset+0] & 0x80 ) == 0x80 )
+                {
+                    value |= ( bites[offset+0] << 8 ) | 0xffff0000 ;
+                }
+                else
+                {
+                    value |= ( bites[offset+0] << 8 ) ;
+                }
+
+                value |= bites[offset+1] ;
+                break ;
+            case( 3 ):
+                if ( ( bites[offset+0] & 0x80 ) == 0x80 )
+                {
+                    value |= ( bites[offset+0] << 16 ) | 0xff000000 ;
+                }
+                else
+                {
+                    value |= ( bites[offset+0] << 16 ) ;
+                }
+
+                value |= ( bites[offset+1] << 8 ) ;
+                value |= bites[offset+2] ;
+                break ;
+            case( 4 ):
+                value |= ( bites[offset+0] << 24 ) ;
+                value |= ( bites[offset+1] << 16 ) ;
+                value |= ( bites[offset+2] << 8 ) ;
+                value |= bites[offset+3] ;
+                break ;
+            default:
+                throw new IllegalArgumentException(
+                        "Length should be in range [0-4]" ) ;
+        }
+
+        return value ;
+    }
+}

Modified: incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/ber/primitives/UniversalTag.java
==============================================================================
--- incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/ber/primitives/UniversalTag.java
(original)
+++ incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/ber/primitives/UniversalTag.java
Sun Apr 11 20:05:16 2004
@@ -139,105 +139,192 @@
     public static final int BMP_STRING_VAL =                0x1e000000 ;
     /** value for the tag */
     public static final int RESERVED31_VAL =                0x1f000000 ;
-    
+
+    /** value for the tag */
+    public static final int RESERVED0_ID =                 0 ;
+    /** value for the tag */
+    public static final int BOOLEAN_ID =                   1 ;
+    /** value for the tag */
+    public static final int INTEGER_ID =                   2 ;
+    /** value for the tag */
+    public static final int BIT_STRING_ID =                3 ;
+    /** value for the tag */
+    public static final int OCTET_STRING_ID =              4 ;
+    /** value for the tag */
+    public static final int NULL_ID =                      5 ;
+    /** value for the tag */
+    public static final int OBJECT_IDENTIFIER_ID =         6 ;
+    /** value for the tag */
+    public static final int OBJECT_DESCRIPTOR_ID =         7 ;
+    /** value for the tag */
+    public static final int EXTERNAL_INSTANCE_OF_ID =      8 ;
+    /** value for the tag */
+    public static final int REAL_ID =                      9 ;
+    /** value for the tag */
+    public static final int ENUMERATED_ID =                10 ;
+    /** value for the tag */
+    public static final int EMBEDDED_PDV_ID =              11 ;
+    /** value for the tag */
+    public static final int UTF8_STRING_ID =               12 ;
+    /** value for the tag */
+    public static final int RELATIVE_OID_ID =              13 ;
+    /** value for the tag */
+    public static final int RESERVED14_ID =                14 ;
+    /** value for the tag */
+    public static final int RESERVED15_ID =                15 ;
+    /** value for the tag */
+    public static final int SEQUENCE_SEQUENCE_OF_ID =      16 ;
+    /** value for the tag */
+    public static final int SET_SET_OF_ID =                17 ;
+    /** value for the tag */
+    public static final int NUMERIC_STRING_ID =            18 ;
+    /** value for the tag */
+    public static final int PRINTABLE_STRING_ID =          19 ;
+    /** value for the tag */
+    public static final int TELETEX_STRING_ID =            20 ;
+    /** value for the tag */
+    public static final int VIDEOTEX_STRING_ID =           21 ;
+    /** value for the tag */
+    public static final int IA5_STRING_ID =                22 ;
+    /** value for the tag */
+    public static final int UTC_TIME_ID =                  23 ;
+    /** value for the tag */
+    public static final int GENERALIZED_TIME_ID =          24 ;
+    /** value for the tag */
+    public static final int GRAPHIC_STRING_ID =            25 ;
+    /** value for the tag */
+    public static final int VISIBLE_STRING_ID =            26 ;
+    /** value for the tag */
+    public static final int GENERAL_STRING_ID =            27 ;
+    /** value for the tag */
+    public static final int UNIVERSAL_STRING_ID =          28 ;
+    /** value for the tag */
+    public static final int CHARACTER_STRING_ID =          29 ;
+    /** value for the tag */
+    public static final int BMP_STRING_ID =                30 ;
+    /** value for the tag */
+    public static final int RESERVED31_ID =                31 ;
+
     /** enum for the tag */
     public static final UniversalTag BOOLEAN = 
-        new UniversalTag( "BOOLEAN", BOOLEAN_VAL ) ;
+        new UniversalTag( "BOOLEAN", BOOLEAN_VAL, BOOLEAN_ID ) ;
     /** enum for the tag */
     public static final UniversalTag RESERVED0 = 
-        new UniversalTag( "RESERVED0", RESERVED0_VAL ) ; 
+        new UniversalTag( "RESERVED0", RESERVED0_VAL, RESERVED0_ID ) ;
     /** enum for the tag */
     public static final UniversalTag INTEGER = 
-        new UniversalTag( "INTEGER", INTEGER_VAL ) ; 
+        new UniversalTag( "INTEGER", INTEGER_VAL, INTEGER_ID ) ;
     /** enum for the tag */
     public static final UniversalTag BIT_STRING = 
-        new UniversalTag( "BIT_STRING", BIT_STRING_VAL ) ; 
+        new UniversalTag( "BIT_STRING", BIT_STRING_VAL, BIT_STRING_ID ) ;
     /** enum for the tag */
     public static final UniversalTag OCTET_STRING = 
-        new UniversalTag( "OCTET_STRING", OCTET_STRING_VAL ) ; 
+        new UniversalTag( "OCTET_STRING", OCTET_STRING_VAL, OCTET_STRING_ID ) ;
     /** enum for the tag */
     public static final UniversalTag NULL = 
-        new UniversalTag( "NULL", NULL_VAL ) ; 
+        new UniversalTag( "NULL", NULL_VAL, NULL_ID ) ;
     /** enum for the tag */
     public static final UniversalTag OBJECT_IDENTIFIER = 
-        new UniversalTag( "OBJECT_IDENTIFIER", OBJECT_IDENTIFIER_VAL ) ; 
+        new UniversalTag( "OBJECT_IDENTIFIER", OBJECT_IDENTIFIER_VAL,
+                OBJECT_IDENTIFIER_ID ) ;
     /** enum for the tag */
     public static final UniversalTag OBJECT_DESCRIPTOR = 
-        new UniversalTag( "OBJECT_DESCRIPTOR", OBJECT_DESCRIPTOR_VAL ) ; 
+        new UniversalTag( "OBJECT_DESCRIPTOR", OBJECT_DESCRIPTOR_VAL,
+                OBJECT_DESCRIPTOR_ID ) ;
     /** enum for the tag */
     public static final UniversalTag EXTERNAL_INSTANCE_OF = 
-        new UniversalTag( "EXTERNAL_INSTANCE_OF", EXTERNAL_INSTANCE_OF_VAL ) ; 
+        new UniversalTag( "EXTERNAL_INSTANCE_OF", EXTERNAL_INSTANCE_OF_VAL,
+                EXTERNAL_INSTANCE_OF_ID ) ;
     /** enum for the tag */
     public static final UniversalTag REAL = 
-        new UniversalTag( "REAL", REAL_VAL ) ; 
+        new UniversalTag( "REAL", REAL_VAL, REAL_ID ) ;
     /** enum for the tag */
     public static final UniversalTag ENUMERATED = 
-        new UniversalTag( "ENUMERATED", ENUMERATED_VAL ) ; 
+        new UniversalTag( "ENUMERATED", ENUMERATED_VAL, ENUMERATED_ID ) ;
     /** enum for the tag */
     public static final UniversalTag EMBEDDED_PDV = 
-        new UniversalTag( "EMBEDDED_PDV", EMBEDDED_PDV_VAL ) ; 
+        new UniversalTag( "EMBEDDED_PDV", EMBEDDED_PDV_VAL, EMBEDDED_PDV_ID ) ;
     /** enum for the tag */
     public static final UniversalTag UTF8_STRING = 
-        new UniversalTag( "UTF8_STRING", UTF8_STRING_VAL ) ; 
+        new UniversalTag( "UTF8_STRING", UTF8_STRING_VAL, UTF8_STRING_ID ) ;
     /** enum for the tag */
     public static final UniversalTag RELATIVE_OID = 
-        new UniversalTag( "RELATIVE_OID", RELATIVE_OID_VAL ) ; 
+        new UniversalTag( "RELATIVE_OID", RELATIVE_OID_VAL, RELATIVE_OID_ID ) ;
     /** enum for the tag */
     public static final UniversalTag RESERVED14 = 
-        new UniversalTag( "RESERVED14", RESERVED14_VAL ) ; 
+        new UniversalTag( "RESERVED14", RESERVED14_VAL, RESERVED14_ID ) ;
     /** enum for the tag */
     public static final UniversalTag RESERVED15 = 
-        new UniversalTag( "RESERVED15", RESERVED15_VAL ) ; 
+        new UniversalTag( "RESERVED15", RESERVED15_VAL, RESERVED15_ID ) ;
     /** enum for the tag */
     public static final UniversalTag SEQUENCE_SEQUENCE_OF = 
-        new UniversalTag( "SEQUENCE_SEQUENCE_OF", SEQUENCE_SEQUENCE_OF_VAL ) ; 
+        new UniversalTag( "SEQUENCE_SEQUENCE_OF", SEQUENCE_SEQUENCE_OF_VAL,
+                SEQUENCE_SEQUENCE_OF_ID ) ;
     /** enum for the tag */
     public static final UniversalTag SET_SET_OF = 
-        new UniversalTag( "SET_SET_OF", SET_SET_OF_VAL ) ; 
+        new UniversalTag( "SET_SET_OF", SET_SET_OF_VAL, SET_SET_OF_ID ) ;
     /** enum for the tag */
     public static final UniversalTag NUMERIC_STRING = 
-        new UniversalTag( "NUMERIC_STRING", NUMERIC_STRING_VAL ) ; 
+        new UniversalTag( "NUMERIC_STRING", NUMERIC_STRING_VAL,
+                NUMERIC_STRING_ID ) ;
     /** enum for the tag */
     public static final UniversalTag PRINTABLE_STRING = 
-        new UniversalTag( "PRINTABLE_STRING", PRINTABLE_STRING_VAL ) ; 
+        new UniversalTag( "PRINTABLE_STRING", PRINTABLE_STRING_VAL,
+                PRINTABLE_STRING_ID ) ;
     /** enum for the tag */
     public static final UniversalTag TELETEX_STRING = 
-        new UniversalTag( "TELETEX_STRING", TELETEX_STRING_VAL ) ; 
+        new UniversalTag( "TELETEX_STRING", TELETEX_STRING_VAL,
+                TELETEX_STRING_ID ) ;
     /** enum for the tag */
     public static final UniversalTag VIDEOTEX_STRING = 
-        new UniversalTag( "VIDEOTEX_STRING", VIDEOTEX_STRING_VAL ) ; 
+        new UniversalTag( "VIDEOTEX_STRING", VIDEOTEX_STRING_VAL,
+                VIDEOTEX_STRING_ID ) ;
     /** enum for the tag */
     public static final UniversalTag IA5_STRING = 
-        new UniversalTag( "IA5_STRING", IA5_STRING_VAL ) ; 
+        new UniversalTag( "IA5_STRING", IA5_STRING_VAL, IA5_STRING_ID ) ;
     /** enum for the tag */
     public static final UniversalTag UTC_TIME = 
-        new UniversalTag( "UTC_TIME", UTC_TIME_VAL ) ; 
+        new UniversalTag( "UTC_TIME", UTC_TIME_VAL, UTC_TIME_ID ) ;
     /** enum for the tag */
     public static final UniversalTag GENERALIZED_TIME = 
-        new UniversalTag( "GENERALIZED_TIME", GENERALIZED_TIME_VAL ) ; 
+        new UniversalTag( "GENERALIZED_TIME", GENERALIZED_TIME_VAL,
+                GENERALIZED_TIME_ID ) ;
     /** enum for the tag */
     public static final UniversalTag GRAPHIC_STRING = 
-        new UniversalTag( "GRAPHIC_STRING", GRAPHIC_STRING_VAL ) ; 
+        new UniversalTag( "GRAPHIC_STRING", GRAPHIC_STRING_VAL,
+                GRAPHIC_STRING_ID ) ;
     /** enum for the tag */
     public static final UniversalTag VISIBLE_STRING = 
-        new UniversalTag( "VISIBLE_STRING", VISIBLE_STRING_VAL ) ; 
+        new UniversalTag( "VISIBLE_STRING", VISIBLE_STRING_VAL,
+                VISIBLE_STRING_ID ) ;
     /** enum for the tag */
     public static final UniversalTag GENERAL_STRING = 
-        new UniversalTag( "GENERAL_STRING", GENERAL_STRING_VAL ) ; 
+        new UniversalTag( "GENERAL_STRING", GENERAL_STRING_VAL,
+                GENERAL_STRING_ID ) ;
     /** enum for the tag */
     public static final UniversalTag UNIVERSAL_STRING = 
-        new UniversalTag( "UNIVERSAL_STRING", UNIVERSAL_STRING_VAL ) ; 
+        new UniversalTag( "UNIVERSAL_STRING", UNIVERSAL_STRING_VAL,
+                UNIVERSAL_STRING_ID ) ;
     /** enum for the tag */
     public static final UniversalTag CHARACTER_STRING = 
-        new UniversalTag( "CHARACTER_STRING", CHARACTER_STRING_VAL ) ; 
+        new UniversalTag( "CHARACTER_STRING", CHARACTER_STRING_VAL,
+                CHARACTER_STRING_ID ) ;
     /** enum for the tag */
     public static final UniversalTag BMP_STRING = 
-        new UniversalTag( "BMP_STRING", BMP_STRING_VAL ) ; 
+        new UniversalTag( "BMP_STRING", BMP_STRING_VAL, BMP_STRING_ID ) ;
     /** enum for the tag */
     public static final UniversalTag RESERVED31 = 
-        new UniversalTag( "RESERVED31", RESERVED31_VAL ) ; 
-    
-    
+        new UniversalTag( "RESERVED31", RESERVED31_VAL, RESERVED31_ID ) ;
+
+
+    // -----------------------------------------------------------------------
+    // Members
+    // -----------------------------------------------------------------------
+
+    /** the id of the UNIVERSAL tag */
+    private final int id ;
+
+
     /**
      * Private constructor so no other instances can be created other than the
      * public static constants in this class.
@@ -245,12 +332,24 @@
      * @param name a string name for the enumeration value.
      * @param value the integer value of the enumeration.
      */
-    private UniversalTag( final String name, final int value )
+    private UniversalTag( final String name, final int value, final int id )
     {
         super( name, value ) ;
+        this.id = id ;
     }
-    
-    
+
+
+    /**
+     * Gets the id of the universal tag.
+     *
+     * @return the id of the universal tag
+     */
+    public int getTagId()
+    {
+        return this.id ;
+    }
+
+
     /**
      * Gets a List of the enumerations for ASN.1 UNIVERSAL type tags.
      * 
@@ -277,7 +376,7 @@
     /**
      * Gets the ASN.1 UNIVERSAL type tag's enum using a tag value.
      * 
-     * @param octet the first octet of the TLV
+     * @param value the first octet of the TLV
      * @return the valued enum for the ASN.1 UNIVERSAL type tag
      */
     public static UniversalTag getUniversalTag( int value )
@@ -383,8 +482,146 @@
             case( RESERVED31_VAL ):
                 type = RESERVED31 ;
                 break ;
+            default:
+                String msg ;
+                if ( value < 31 && value > -1 )
+                {
+                    msg = "Looks like you're using an id instead of a tag" ;
+                }
+                else
+                {
+                    msg = "Tag value of " + value + " not recognized" ;
+                }
+
+                throw new IllegalArgumentException( msg ) ;
         }
         
+        return type ;
+    }
+
+
+    /**
+     * Gets the ASN.1 UNIVERSAL type tag's enum using a tag value.
+     *
+     * @param id the first octet of the TLV
+     * @return the valued enum for the ASN.1 UNIVERSAL type tag
+     */
+    public static UniversalTag getUniversalTagById( int id )
+    {
+        UniversalTag type = null ;
+
+        switch ( id )
+        {
+            case( RESERVED0_ID ):
+                type = RESERVED0 ;
+                break ;
+            case( BOOLEAN_ID ):
+                type = BOOLEAN ;
+                break ;
+            case( INTEGER_ID ):
+                type = INTEGER ;
+                break ;
+            case( BIT_STRING_ID ):
+                type = BIT_STRING ;
+                break ;
+            case( OCTET_STRING_ID ):
+                type = OCTET_STRING ;
+                break ;
+            case( NULL_ID ):
+                type = NULL ;
+                break ;
+            case( OBJECT_IDENTIFIER_ID ):
+                type = OBJECT_IDENTIFIER ;
+                break ;
+            case( OBJECT_DESCRIPTOR_ID ):
+                type = OBJECT_DESCRIPTOR ;
+                break ;
+            case( EXTERNAL_INSTANCE_OF_ID ):
+                type = EXTERNAL_INSTANCE_OF ;
+                break ;
+            case( REAL_ID ):
+                type = REAL ;
+                break ;
+            case( ENUMERATED_ID ):
+                type = ENUMERATED ;
+                break ;
+            case( EMBEDDED_PDV_ID ):
+                type = EMBEDDED_PDV ;
+                break ;
+            case( UTF8_STRING_ID ):
+                type = UTF8_STRING ;
+                break ;
+            case( RELATIVE_OID_ID ):
+                type = RELATIVE_OID ;
+                break ;
+            case( RESERVED14_ID ):
+                type = RESERVED14 ;
+                break ;
+            case( RESERVED15_ID ):
+                type = RESERVED15 ;
+                break ;
+            case( SEQUENCE_SEQUENCE_OF_ID ):
+                type = SEQUENCE_SEQUENCE_OF ;
+                break ;
+            case( SET_SET_OF_ID ):
+                type = SET_SET_OF ;
+                break ;
+            case( NUMERIC_STRING_ID ):
+                type = NUMERIC_STRING ;
+                break ;
+            case( PRINTABLE_STRING_ID ):
+                type = PRINTABLE_STRING ;
+                break ;
+            case( TELETEX_STRING_ID ):
+                type = TELETEX_STRING ;
+                break ;
+            case( VIDEOTEX_STRING_ID ):
+                type = VIDEOTEX_STRING ;
+                break ;
+            case( IA5_STRING_ID ):
+                type = IA5_STRING ;
+                break ;
+            case( UTC_TIME_ID ):
+                type = UTC_TIME ;
+                break ;
+            case( GENERALIZED_TIME_ID ):
+                type = GENERALIZED_TIME ;
+                break ;
+            case( GRAPHIC_STRING_ID ):
+                type = GRAPHIC_STRING ;
+                break ;
+            case( VISIBLE_STRING_ID ):
+                type = VISIBLE_STRING ;
+                break ;
+            case( GENERAL_STRING_ID ):
+                type = GENERAL_STRING ;
+                break ;
+            case( UNIVERSAL_STRING_ID ):
+                type = UNIVERSAL_STRING ;
+                break ;
+            case( CHARACTER_STRING_ID ):
+                type = CHARACTER_STRING ;
+                break ;
+            case( BMP_STRING_ID ):
+                type = BMP_STRING ;
+                break ;
+            case( RESERVED31_ID ):
+                type = RESERVED31 ;
+                break ;
+            default:
+                String msg ;
+                if ( id > 31 || id < 0 )
+                {
+                    msg = "Looks like you're using a tag instead of an id" ;
+                }
+                else
+                {
+                    msg = "Id of " + id + " not recognized as a universal id" ;
+                }
+
+                throw new IllegalArgumentException( msg ) ;
+        }
+
         return type ;
     }
 }

Modified: incubator/directory/snickers/trunk/ber/src/test/org/apache/snickers/ber/EncodeDecodeTests.java
==============================================================================
--- incubator/directory/snickers/trunk/ber/src/test/org/apache/snickers/ber/EncodeDecodeTests.java
(original)
+++ incubator/directory/snickers/trunk/ber/src/test/org/apache/snickers/ber/EncodeDecodeTests.java
Sun Apr 11 20:05:16 2004
@@ -21,10 +21,7 @@
 import java.io.ByteArrayInputStream ;
 
 import org.apache.commons.codec.binary.Hex ;
-import org.apache.ldap.common.message.AbandonRequest ;
-import org.apache.ldap.common.message.MessageDecoder ;
-import org.apache.ldap.common.message.MessageEncoder ;
-import org.apache.ldap.common.message.AbandonRequestImpl ;
+import org.apache.ldap.common.message.*;
 
 import junit.framework.TestCase ;
 
@@ -74,5 +71,51 @@
         byte[] snickersBytes = new byte[buf.remaining()] ;
         buf.get( snickersBytes ) ;
         String snickersEncoded = new String( Hex.encodeHex( snickersBytes ) ) ;
+    }
+
+
+    public void testBindRequest() throws Exception
+    {
+        DefaultMutableTupleNode root = null ;
+        ByteBuffer buf = null ;
+        BindRequestImpl request = new BindRequestImpl( 1 ) ;
+        request.setCredentials( "password".getBytes() ) ;
+        request.setName( "uid=akarasulu,dc=example,dc=com" ) ;
+        request.setSimple( true ) ;
+        request.setVersion3( true ) ;
+
+        MessageEncoder encoder = new MessageEncoder() ;
+        byte[] snaccBytes = encoder.encode( request ) ;
+
+        String snaccEncoded = new String( Hex.encodeHex( snaccBytes ) ) ;
+        System.out.println( "snacc encoded = [" + snaccEncoded + "]["
+                + snaccBytes.length + "]" ) ;
+
+        MessageDecoder decoder = new MessageDecoder() ;
+        BindRequest msg = ( BindRequest ) decoder.decode( null,
+                new ByteArrayInputStream( snaccBytes ) ) ;
+        assertTrue( msg.getMessageId() == request.getMessageId() ) ;
+
+        System.out.println( "snacc decoded = " + msg ) ;
+
+        root = ( DefaultMutableTupleNode )
+                TupleTreeDecoder.treeDecode( ByteBuffer.wrap( snaccBytes ) ) ;
+
+        if ( root == null )
+        {
+            System.out.println( "nothing decoded" ) ;
+            return ;
+        }
+
+        buf = ByteBuffer.allocate( root.size() ) ;
+        root.encode( buf ) ;
+        buf.flip() ;
+        byte[] snickersBytes = new byte[buf.remaining()] ;
+        buf.get( snickersBytes ) ;
+        String snickersEncoded = new String( Hex.encodeHex( snickersBytes ) ) ;
+
+        StringBuffer sb = new StringBuffer() ;
+        root.printDepthFirst( sb, 0 ) ;
+        System.out.println( sb.toString() ) ;
     }
 }

Added: incubator/directory/snickers/trunk/ber/src/test/org/apache/snickers/ber/digester/rules/PrimitiveIntDecodeRuleTest.java
==============================================================================
--- (empty file)
+++ incubator/directory/snickers/trunk/ber/src/test/org/apache/snickers/ber/digester/rules/PrimitiveIntDecodeRuleTest.java
Sun Apr 11 20:05:16 2004
@@ -0,0 +1,443 @@
+/*
+ *   Copyright 2004 The Apache Software Foundation
+ *
+ *   Licensed under the Apache License, Version 2.0 (the "License");
+ *   you may not use this file except in compliance with the License.
+ *   You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing, software
+ *   distributed under the License is distributed on an "AS IS" BASIS,
+ *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *   See the License for the specific language governing permissions and
+ *   limitations under the License.
+ *
+ */
+package org.apache.snickers.ber.digester.rules ;
+
+
+import junit.framework.TestCase ;
+
+import org.apache.snickers.ber.TypeClass ;
+import org.apache.snickers.ber.digester.BERDigester ;
+import org.apache.snickers.ber.primitives.UniversalTag ;
+
+import java.nio.ByteBuffer;
+import java.math.BigInteger;
+
+
+/**
+ * Tests the PrimitiveIntDecodeRule.
+ *
+ * @author <a href="mailto:directory-dev@incubator.apache.org">Apache Directory Project</a>
+ * @version $Rev$
+ */
+public class PrimitiveIntDecodeRuleTest extends TestCase
+{
+    PrimitiveIntDecodeRule rule ;
+    BERDigester digester ;
+
+
+    protected void setUp() throws Exception
+    {
+        super.setUp() ;
+        rule = new PrimitiveIntDecodeRule() ;
+        digester = new BERDigester() ;
+        rule.setDigester( digester ) ;
+    }
+
+
+    protected void tearDown() throws Exception
+    {
+        super.tearDown() ;
+        rule = null ;
+        digester = null ;
+    }
+
+
+    public void testTag()
+    {
+        rule.tag( UniversalTag.INTEGER.getTagId(), true,
+                TypeClass.UNIVERSAL ) ;
+
+        try
+        {
+            rule.tag( 0, false, null ) ;
+            fail( "should never get here" ) ;
+        }
+        catch ( IllegalArgumentException e )
+        {
+        }
+    }
+
+
+    public void testLength()
+    {
+        rule.tag( UniversalTag.INTEGER.getTagId(), true,
+                TypeClass.UNIVERSAL ) ;
+
+        rule.length( 0 ) ;
+        rule.length( 1 ) ;
+        rule.length( 2 ) ;
+        rule.length( 3 ) ;
+        rule.length( 4 ) ;
+
+        try
+        {
+            rule.length( -1 ) ;
+            fail( "should never get here due to exception" ) ;
+        }
+        catch ( IllegalArgumentException e )
+        {
+        }
+
+        try
+        {
+            rule.length( 5 ) ;
+            fail( "should never get here due to exception" ) ;
+        }
+        catch ( IllegalArgumentException e )
+        {
+        }
+    }
+
+
+    public void testValue0()
+    {
+        rule.tag( UniversalTag.INTEGER.getTagId(), true,
+                TypeClass.UNIVERSAL ) ;
+        rule.length( 0 ) ;
+        rule.value( null ) ;
+    }
+
+
+    public void testValue1()
+    {
+        rule.tag( UniversalTag.INTEGER.getTagId(), true,
+                TypeClass.UNIVERSAL ) ;
+        rule.length( 1 ) ;
+        byte[] bites = { 0x45 } ;
+        ByteBuffer buf = ByteBuffer.wrap( bites ) ;
+        rule.value( buf ) ;
+        assertTrue( rule.equals( (byte) 0x45, 0 ) ) ;
+        assertTrue( rule.equals( (byte) 0x00, 1 ) ) ;
+        assertTrue( rule.equals( (byte) 0x00, 2 ) ) ;
+        assertTrue( rule.equals( (byte) 0x00, 3 ) ) ;
+        buf.clear() ;
+        rule.value( buf ) ;
+        assertTrue( rule.equals( (byte) 0x45, 0 ) ) ;
+        assertTrue( rule.equals( (byte) 0x00, 1 ) ) ;
+        assertTrue( rule.equals( (byte) 0x00, 2 ) ) ;
+        assertTrue( rule.equals( (byte) 0x00, 3 ) ) ;
+    }
+
+
+    public void testValue2()
+    {
+        rule.tag( UniversalTag.INTEGER.getTagId(), true,
+                TypeClass.UNIVERSAL ) ;
+        rule.length( 2 ) ;
+        byte[] bites = { 0x45, 0x23 } ;
+        ByteBuffer buf = ByteBuffer.wrap( bites ) ;
+        rule.value( buf ) ;
+        assertTrue( rule.equals( (byte) 0x45, 0 ) ) ;
+        assertTrue( rule.equals( (byte) 0x23, 1 ) ) ;
+        assertTrue( rule.equals( (byte) 0x00, 2 ) ) ;
+        assertTrue( rule.equals( (byte) 0x00, 3 ) ) ;
+        buf.clear() ;
+        rule.value( buf ) ;
+        assertTrue( rule.equals( (byte) 0x45, 0 ) ) ;
+        assertTrue( rule.equals( (byte) 0x23, 1 ) ) ;
+        assertTrue( rule.equals( (byte) 0x00, 2 ) ) ;
+        assertTrue( rule.equals( (byte) 0x00, 3 ) ) ;
+    }
+
+
+    public void testValue2Fragmented()
+    {
+        rule.tag( UniversalTag.INTEGER.getTagId(), true,
+                TypeClass.UNIVERSAL ) ;
+        rule.length( 2 ) ;
+        byte[] bites0 = { 0x45 } ;
+        byte[] bites1 = { 0x23 } ;
+        ByteBuffer buf = ByteBuffer.wrap( bites0 ) ;
+        rule.value( buf ) ;
+        assertTrue( rule.equals( (byte) 0x45, 0 ) ) ;
+        assertTrue( rule.equals( (byte) 0x00, 1 ) ) ;
+        assertTrue( rule.equals( (byte) 0x00, 2 ) ) ;
+        assertTrue( rule.equals( (byte) 0x00, 3 ) ) ;
+        buf = ByteBuffer.wrap( bites1 ) ;
+        rule.value( buf ) ;
+        assertTrue( rule.equals( (byte) 0x45, 0 ) ) ;
+        assertTrue( rule.equals( (byte) 0x23, 1 ) ) ;
+        assertTrue( rule.equals( (byte) 0x00, 2 ) ) ;
+        assertTrue( rule.equals( (byte) 0x00, 3 ) ) ;
+        buf.clear() ;
+        rule.value( buf ) ;
+        assertTrue( rule.equals( (byte) 0x45, 0 ) ) ;
+        assertTrue( rule.equals( (byte) 0x23, 1 ) ) ;
+        assertTrue( rule.equals( (byte) 0x00, 2 ) ) ;
+        assertTrue( rule.equals( (byte) 0x00, 3 ) ) ;
+    }
+
+
+    public void testValue3()
+    {
+        rule.tag( UniversalTag.INTEGER.getTagId(), true,
+                TypeClass.UNIVERSAL ) ;
+        rule.length( 3 ) ;
+        byte[] bites = { 0x45, 0x23, 0x12 } ;
+        ByteBuffer buf = ByteBuffer.wrap( bites ) ;
+        rule.value( buf ) ;
+        assertTrue( rule.equals( (byte) 0x45, 0 ) ) ;
+        assertTrue( rule.equals( (byte) 0x23, 1 ) ) ;
+        assertTrue( rule.equals( (byte) 0x12, 2 ) ) ;
+        assertTrue( rule.equals( (byte) 0x00, 3 ) ) ;
+        buf.clear() ;
+        rule.value( buf ) ;
+        assertTrue( rule.equals( (byte) 0x45, 0 ) ) ;
+        assertTrue( rule.equals( (byte) 0x23, 1 ) ) ;
+        assertTrue( rule.equals( (byte) 0x12, 2 ) ) ;
+        assertTrue( rule.equals( (byte) 0x00, 3 ) ) ;
+    }
+
+
+    public void testValue4()
+    {
+        rule.tag( UniversalTag.INTEGER.getTagId(), true,
+                TypeClass.UNIVERSAL ) ;
+        rule.length( 4 ) ;
+        byte[] bites = { 0x45, 0x23, 0x12, 0x01 } ;
+        ByteBuffer buf = ByteBuffer.wrap( bites ) ;
+        rule.value( buf ) ;
+        assertTrue( rule.equals( (byte) 0x45, 0 ) ) ;
+        assertTrue( rule.equals( (byte) 0x23, 1 ) ) ;
+        assertTrue( rule.equals( (byte) 0x12, 2 ) ) ;
+        assertTrue( rule.equals( (byte) 0x01, 3 ) ) ;
+        buf.clear() ;
+        rule.value( buf ) ;
+        assertTrue( rule.equals( (byte) 0x45, 0 ) ) ;
+        assertTrue( rule.equals( (byte) 0x23, 1 ) ) ;
+        assertTrue( rule.equals( (byte) 0x12, 2 ) ) ;
+        assertTrue( rule.equals( (byte) 0x01, 3 ) ) ;
+    }
+
+
+    public void testValue5()
+    {
+        rule.tag( UniversalTag.INTEGER.getTagId(), true,
+                TypeClass.UNIVERSAL ) ;
+        rule.length( 4 ) ;
+        byte[] bites = { 0x45, 0x23, 0x12, 0x01, 0x07 } ;
+        ByteBuffer buf = ByteBuffer.wrap( bites ) ;
+        rule.value( buf ) ;
+        assertTrue( rule.equals( (byte) 0x45, 0 ) ) ;
+        assertTrue( rule.equals( (byte) 0x23, 1 ) ) ;
+        assertTrue( rule.equals( (byte) 0x12, 2 ) ) ;
+        assertTrue( rule.equals( (byte) 0x01, 3 ) ) ;
+        buf.clear() ;
+        rule.value( buf ) ;
+        assertTrue( rule.equals( (byte) 0x45, 0 ) ) ;
+        assertTrue( rule.equals( (byte) 0x23, 1 ) ) ;
+        assertTrue( rule.equals( (byte) 0x12, 2 ) ) ;
+        assertTrue( rule.equals( (byte) 0x01, 3 ) ) ;
+    }
+
+
+    public void testFinishNullDigester()
+    {
+        rule.setDigester( null ) ;
+        rule.tag( UniversalTag.INTEGER.getTagId(), true,
+                TypeClass.UNIVERSAL ) ;
+        rule.length( 0 ) ;
+        rule.value( null ) ;
+        rule.finish() ;
+
+        assertEquals( 0, digester.getIntCount() ) ;
+    }
+
+
+    public void testFinish0()
+    {
+        rule.tag( UniversalTag.INTEGER.getTagId(), true,
+                TypeClass.UNIVERSAL ) ;
+        rule.length( 0 ) ;
+        rule.value( null ) ;
+        rule.finish() ;
+
+        assertEquals( 1, digester.getIntCount() ) ;
+        assertEquals( 0, digester.popInt() ) ;
+        assertEquals( 0, digester.getIntCount() ) ;
+    }
+
+
+    public void testFinish1()
+    {
+        rule.tag( UniversalTag.INTEGER.getTagId(), true,
+                TypeClass.UNIVERSAL ) ;
+        rule.length( 1 ) ;
+        byte[] bites = { 0x45 } ;
+        ByteBuffer buf = ByteBuffer.wrap( bites ) ;
+        rule.value( buf ) ;
+        assertTrue( rule.equals( (byte) 0x45, 0 ) ) ;
+        assertTrue( rule.equals( (byte) 0x00, 1 ) ) ;
+        assertTrue( rule.equals( (byte) 0x00, 2 ) ) ;
+        assertTrue( rule.equals( (byte) 0x00, 3 ) ) ;
+        buf.clear() ;
+        rule.value( buf ) ;
+        assertTrue( rule.equals( (byte) 0x45, 0 ) ) ;
+        assertTrue( rule.equals( (byte) 0x00, 1 ) ) ;
+        assertTrue( rule.equals( (byte) 0x00, 2 ) ) ;
+        assertTrue( rule.equals( (byte) 0x00, 3 ) ) ;
+
+        rule.finish() ;
+        assertEquals( 1, digester.getIntCount() ) ;
+        assertEquals( 0x45, digester.popInt() ) ;
+        assertEquals( 0, digester.getIntCount() ) ;
+    }
+
+
+    public void testFinish2()
+    {
+        rule.tag( UniversalTag.INTEGER.getTagId(), true,
+                TypeClass.UNIVERSAL ) ;
+        rule.length( 2 ) ;
+        byte[] bites = { 0x45, 0x23 } ;
+        ByteBuffer buf = ByteBuffer.wrap( bites ) ;
+        rule.value( buf ) ;
+        assertTrue( rule.equals( (byte) 0x45, 0 ) ) ;
+        assertTrue( rule.equals( (byte) 0x23, 1 ) ) ;
+        assertTrue( rule.equals( (byte) 0x00, 2 ) ) ;
+        assertTrue( rule.equals( (byte) 0x00, 3 ) ) ;
+        buf.clear() ;
+        rule.value( buf ) ;
+        assertTrue( rule.equals( (byte) 0x45, 0 ) ) ;
+        assertTrue( rule.equals( (byte) 0x23, 1 ) ) ;
+        assertTrue( rule.equals( (byte) 0x00, 2 ) ) ;
+        assertTrue( rule.equals( (byte) 0x00, 3 ) ) ;
+
+        rule.finish() ;
+        BigInteger big = new BigInteger( bites ) ;
+        assertEquals( 1, digester.getIntCount() ) ;
+        assertEquals( big.intValue(), digester.popInt() ) ;
+        assertEquals( 0, digester.getIntCount() ) ;
+    }
+
+
+    public void testFinish2Fragmented()
+    {
+        rule.tag( UniversalTag.INTEGER.getTagId(), true,
+                TypeClass.UNIVERSAL ) ;
+        rule.length( 2 ) ;
+        byte[] bites0 = { 0x45 } ;
+        byte[] bites1 = { 0x23 } ;
+        ByteBuffer buf = ByteBuffer.wrap( bites0 ) ;
+        rule.value( buf ) ;
+        assertTrue( rule.equals( (byte) 0x45, 0 ) ) ;
+        assertTrue( rule.equals( (byte) 0x00, 1 ) ) ;
+        assertTrue( rule.equals( (byte) 0x00, 2 ) ) ;
+        assertTrue( rule.equals( (byte) 0x00, 3 ) ) ;
+        buf = ByteBuffer.wrap( bites1 ) ;
+        rule.value( buf ) ;
+        assertTrue( rule.equals( (byte) 0x45, 0 ) ) ;
+        assertTrue( rule.equals( (byte) 0x23, 1 ) ) ;
+        assertTrue( rule.equals( (byte) 0x00, 2 ) ) ;
+        assertTrue( rule.equals( (byte) 0x00, 3 ) ) ;
+        buf.clear() ;
+        rule.value( buf ) ;
+        assertTrue( rule.equals( (byte) 0x45, 0 ) ) ;
+        assertTrue( rule.equals( (byte) 0x23, 1 ) ) ;
+        assertTrue( rule.equals( (byte) 0x00, 2 ) ) ;
+        assertTrue( rule.equals( (byte) 0x00, 3 ) ) ;
+        rule.finish() ;
+
+        assertEquals( 1, digester.getIntCount() ) ;
+        byte[] bitesUsed = { 0x45, 0x23 } ;
+        BigInteger big = new BigInteger( bitesUsed ) ;
+        assertEquals( big.intValue(), digester.popInt() ) ;
+        assertEquals( 0, digester.getIntCount() ) ;
+    }
+
+
+    public void testFinish3()
+    {
+        rule.tag( UniversalTag.INTEGER.getTagId(), true,
+                TypeClass.UNIVERSAL ) ;
+        rule.length( 3 ) ;
+        byte[] bites = { 0x45, 0x23, 0x12 } ;
+        ByteBuffer buf = ByteBuffer.wrap( bites ) ;
+        rule.value( buf ) ;
+        assertTrue( rule.equals( (byte) 0x45, 0 ) ) ;
+        assertTrue( rule.equals( (byte) 0x23, 1 ) ) ;
+        assertTrue( rule.equals( (byte) 0x12, 2 ) ) ;
+        assertTrue( rule.equals( (byte) 0x00, 3 ) ) ;
+        buf.clear() ;
+        rule.value( buf ) ;
+        assertTrue( rule.equals( (byte) 0x45, 0 ) ) ;
+        assertTrue( rule.equals( (byte) 0x23, 1 ) ) ;
+        assertTrue( rule.equals( (byte) 0x12, 2 ) ) ;
+        assertTrue( rule.equals( (byte) 0x00, 3 ) ) ;
+        rule.finish() ;
+
+        assertEquals( 1, digester.getIntCount() ) ;
+        BigInteger big = new BigInteger( bites ) ;
+        assertEquals( big.intValue(), digester.popInt() ) ;
+        assertEquals( 0, digester.getIntCount() ) ;
+    }
+
+
+    public void testFinish4()
+    {
+        rule.tag( UniversalTag.INTEGER.getTagId(), true,
+                TypeClass.UNIVERSAL ) ;
+        rule.length( 4 ) ;
+        byte[] bites = { 0x45, 0x23, 0x12, 0x01 } ;
+        ByteBuffer buf = ByteBuffer.wrap( bites ) ;
+        rule.value( buf ) ;
+        assertTrue( rule.equals( (byte) 0x45, 0 ) ) ;
+        assertTrue( rule.equals( (byte) 0x23, 1 ) ) ;
+        assertTrue( rule.equals( (byte) 0x12, 2 ) ) ;
+        assertTrue( rule.equals( (byte) 0x01, 3 ) ) ;
+        buf.clear() ;
+        rule.value( buf ) ;
+        assertTrue( rule.equals( (byte) 0x45, 0 ) ) ;
+        assertTrue( rule.equals( (byte) 0x23, 1 ) ) ;
+        assertTrue( rule.equals( (byte) 0x12, 2 ) ) ;
+        assertTrue( rule.equals( (byte) 0x01, 3 ) ) ;
+        rule.finish() ;
+
+        assertEquals( 1, digester.getIntCount() ) ;
+        BigInteger big = new BigInteger( bites ) ;
+        assertEquals( big.intValue(), digester.popInt() ) ;
+        assertEquals( 0, digester.getIntCount() ) ;
+    }
+
+
+    public void testFinish5()
+    {
+        rule.tag( UniversalTag.INTEGER.getTagId(), true,
+                TypeClass.UNIVERSAL ) ;
+        rule.length( 4 ) ;
+        byte[] bites = { 0x45, 0x23, 0x12, 0x01, 0x07 } ;
+        ByteBuffer buf = ByteBuffer.wrap( bites ) ;
+        rule.value( buf ) ;
+        assertTrue( rule.equals( (byte) 0x45, 0 ) ) ;
+        assertTrue( rule.equals( (byte) 0x23, 1 ) ) ;
+        assertTrue( rule.equals( (byte) 0x12, 2 ) ) ;
+        assertTrue( rule.equals( (byte) 0x01, 3 ) ) ;
+        buf.clear() ;
+        rule.value( buf ) ;
+        assertTrue( rule.equals( (byte) 0x45, 0 ) ) ;
+        assertTrue( rule.equals( (byte) 0x23, 1 ) ) ;
+        assertTrue( rule.equals( (byte) 0x12, 2 ) ) ;
+        assertTrue( rule.equals( (byte) 0x01, 3 ) ) ;
+        rule.finish() ;
+
+        assertEquals( 1, digester.getIntCount() ) ;
+        byte[] bitesUsed = { 0x45, 0x23, 0x12, 0x01 } ;
+        BigInteger big = new BigInteger( bitesUsed ) ;
+        assertEquals( big.intValue(), digester.popInt() ) ;
+        assertEquals( 0, digester.getIntCount() ) ;
+    }
+}
\ No newline at end of file

Added: incubator/directory/snickers/trunk/ber/src/test/org/apache/snickers/ber/primitives/PrimitiveUtilsTest.java
==============================================================================
--- (empty file)
+++ incubator/directory/snickers/trunk/ber/src/test/org/apache/snickers/ber/primitives/PrimitiveUtilsTest.java
Sun Apr 11 20:05:16 2004
@@ -0,0 +1,74 @@
+/*
+ *   Copyright 2004 The Apache Software Foundation
+ *
+ *   Licensed under the Apache License, Version 2.0 (the "License");
+ *   you may not use this file except in compliance with the License.
+ *   You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing, software
+ *   distributed under the License is distributed on an "AS IS" BASIS,
+ *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *   See the License for the specific language governing permissions and
+ *   limitations under the License.
+ *
+ */
+package org.apache.snickers.ber.primitives ;
+
+
+import junit.framework.TestCase ;
+
+import java.math.BigInteger ;
+
+
+/**
+ * Tests the PrimitiveUtil methods.
+ *
+ * @author <a href="mailto:directory-dev@incubator.apache.org">Apache Directory Project</a>
+ * @version $Rev$
+ */
+public class PrimitiveUtilsTest extends TestCase
+{
+    public static byte[] bites0 = { ( byte ) 0x96, 0x46 } ;
+    public static byte[] bites1 = { ( byte ) 0x76, 0x46 } ;
+    public static byte[] bites2 = { ( byte ) 0x96 } ;
+    public static byte[] bites3 = { ( byte ) 0x46 } ;
+    public static byte[] bites4 = { ( byte ) 0x46, 0x34, 0x12 } ;
+    public static byte[] bites5 = { ( byte ) 0x96, 0x34, 0x12 } ;
+    public static byte[] bites6 = { ( byte ) 0x4F, 0x46, 0x34, 0x12 } ;
+    public static byte[] bites7 = { ( byte ) 0xFF, 0x26, 0x34, 0x12 } ;
+
+    public static byte[][] byteArrays = {
+        bites0, bites1, bites2, bites3, bites4, bites5, bites6, bites7
+    } ;
+
+
+    /**
+     * Tests the PrimitiveUtils.decodeInt(byte[], int, int) method.
+     * Uses the BigInteger class to verify correct encoding because
+     * that's what a BigInteger uses.
+     */
+    public void testDecodeInt()
+    {
+        assertEquals( 0, PrimitiveUtils.decodeInt( null, 0, 0 ) ) ;
+
+        for ( int ii = 0; ii < byteArrays.length; ii++ )
+        {
+            int value = PrimitiveUtils.decodeInt( byteArrays[ii], 0,
+                    byteArrays[ii].length ) ;
+            BigInteger big = new BigInteger( byteArrays[ii] ) ;
+            assertEquals( big.intValue(), value ) ;
+        }
+
+        try
+        {
+            PrimitiveUtils.decodeInt( bites7, 0, -1 ) ;
+            fail( "should never get here due to an exception" ) ;
+        }
+        catch( IllegalArgumentException e )
+        {
+            assertNotNull( e ) ;
+        }
+    }
+}
\ No newline at end of file

Mime
View raw message