directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From elecha...@apache.org
Subject svn commit: r234226 [2/4] - in /directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber: ./ digester/ digester/rules/ primitives/
Date Sun, 21 Aug 2005 15:03:59 GMT
Added: directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/LengthTest.java
URL: http://svn.apache.org/viewcvs/directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/LengthTest.java?rev=234226&view=auto
==============================================================================
--- directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/LengthTest.java (added)
+++ directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/LengthTest.java Sun Aug 21 08:03:46 2005
@@ -0,0 +1,284 @@
+/*
+ *   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.asn1.ber ;
+
+
+import junit.framework.TestCase ;
+
+import java.nio.ByteBuffer ;
+
+import org.apache.asn1.codec.DecoderException;
+import org.apache.asn1.ber.Length;
+
+
+/**
+ * Tests the Length class.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">
+ * Apache Directory Project</a>
+ * @version $Rev: 157644 $
+ */
+public class LengthTest extends TestCase
+{
+    /**
+     * Tests the long form when a byte is used for the length's length and
+     * another is used for the value length for a total of two bytes for the
+     * length field itself.
+     */
+    public void testLongTwoBytes() throws DecoderException
+    {
+        ByteBuffer list = ByteBuffer.allocate( 2 ) ;
+        list.put( (byte) 0x81 ) ;
+        list.put( (byte) 0x01 ) ;
+        list.flip();
+        assertEquals( 0x01, Length.getLength( list ) );
+
+        list = ByteBuffer.allocate( 2 ) ;
+        list.put( (byte) 0x81 ) ;
+        list.put( (byte) 0x05 ) ;
+        list.flip();
+        assertEquals( 0x05, Length.getLength( list ) );
+
+        list = ByteBuffer.allocate( 2 ) ;
+        list.put( (byte) 0x81 ) ;
+        list.put( (byte) 0xFF ) ;
+        list.flip();
+        assertEquals( 0xFF, Length.getLength( list ) );
+    }
+
+
+    /**
+     * Tests the long form when a byte is used for the length's length and
+     * two more are used for the value length for a total of three bytes for the
+     * length field itself.
+     */
+    public void testLongThreeBytes() throws DecoderException
+    {
+        ByteBuffer list = ByteBuffer.allocate( 3 ) ;
+        list.put( (byte) 0x82 ) ;
+        list.put( (byte) 0x00 ) ;
+        list.put( (byte) 0x01 ) ;
+        list.flip();
+        assertEquals( 0x01, Length.getLength( list ) );
+
+        list = ByteBuffer.allocate( 3 ) ;
+        list.put( (byte) 0x82 ) ;
+        list.put( (byte) 0x00 ) ;
+        list.put( (byte) 0x05 ) ;
+        list.flip();
+        assertEquals( 0x05, Length.getLength( list ) );
+
+        list = ByteBuffer.allocate( 3 ) ;
+        list.put( (byte) 0x82 ) ;
+        list.put( (byte) 0x00 ) ;
+        list.put( (byte) 0xFF ) ;
+        list.flip();
+        assertEquals( 0xFF, Length.getLength( list ) );
+
+        list = ByteBuffer.allocate( 3 ) ;
+        list.put( (byte) 0x82 ) ;
+        list.put( (byte) 0x01 ) ;
+        list.put( (byte) 0x05 ) ;
+        list.flip();
+        assertEquals( 0x0105, Length.getLength( list ) );
+
+        list = ByteBuffer.allocate( 3 ) ;
+        list.put( (byte) 0x82 ) ;
+        list.put( (byte) 0x01 ) ;
+        list.put( (byte) 0xFF ) ;
+        list.flip();
+        assertEquals( 0x01FF, Length.getLength( list ) );
+
+        list = ByteBuffer.allocate( 3 ) ;
+        list.put( (byte) 0x82 ) ;
+        list.put( (byte) 0x80 ) ;
+        list.put( (byte) 0x00 ) ;
+        list.flip();
+        assertEquals( 32768, Length.getLength( list ) );
+    }
+
+
+    /**
+     * Tests the long form when a byte is used for the length's length and
+     * three more are used for the value length for a total of four bytes for
+     * the length field itself.
+     */
+    public void testLongFourBytes() throws DecoderException
+    {
+        ByteBuffer list = ByteBuffer.allocate( 4 ) ;
+        list.put( (byte) 0x83 ) ;
+        list.put( (byte) 0x00 ) ;
+        list.put( (byte) 0x00 ) ;
+        list.put( (byte) 0x01 ) ;
+        list.flip();
+        assertEquals( 0x01, Length.getLength( list ) );
+
+        list = ByteBuffer.allocate( 4 ) ;
+        list.put( (byte) 0x83 ) ;
+        list.put( (byte) 0x00 ) ;
+        list.put( (byte) 0x00 ) ;
+        list.put( (byte) 0x05 ) ;
+        list.flip();
+        assertEquals( 0x05, Length.getLength( list ) );
+
+        list = ByteBuffer.allocate( 4 ) ;
+        list.put( (byte) 0x83 ) ;
+        list.put( (byte) 0x00 ) ;
+        list.put( (byte) 0x00 ) ;
+        list.put( (byte) 0xFF ) ;
+        list.flip();
+        assertEquals( 0xFF, Length.getLength( list ) );
+    }
+
+
+    /**
+     * Tests the long form when a byte is used for the length's length and
+     * four more are used for the value length for a total of five bytes for
+     * the length field itself.
+     */
+    public void testLongFiveBytes() throws DecoderException
+    {
+        ByteBuffer list = ByteBuffer.allocate( 5 ) ;
+        list.put( (byte) 0x84 ) ;
+        list.put( (byte) 0x00 ) ;
+        list.put( (byte) 0x00 ) ;
+        list.put( (byte) 0x00 ) ;
+        list.put( (byte) 0x01 ) ;
+        list.flip();
+        assertEquals( 0x01, Length.getLength( list ) );
+
+        list = ByteBuffer.allocate( 5 ) ;
+        list.put( (byte) 0x84 ) ;
+        list.put( (byte) 0x00 ) ;
+        list.put( (byte) 0x00 ) ;
+        list.put( (byte) 0x00 ) ;
+        list.put( (byte) 0x05 ) ;
+        list.flip();
+        assertEquals( 0x05, Length.getLength( list ) );
+
+        list = ByteBuffer.allocate( 5 ) ;
+        list.put( (byte) 0x84 ) ;
+        list.put( (byte) 0x00 ) ;
+        list.put( (byte) 0x00 ) ;
+        list.put( (byte) 0x00 ) ;
+        list.put( (byte) 0xFF ) ;
+        list.flip();
+        assertEquals( 0xFF, Length.getLength( list ) );
+    }
+
+
+    /**
+     * Tests to make sure certain length sizes are not allowed.  Basically we
+     * are capping off the length at 2^32-1 which corresponds to 5 total length
+     * bytes in the long form or the indeterminate form.
+     */
+    public void testMaxLength() throws Exception
+    {
+        ByteBuffer list = ByteBuffer.allocate( 6 ) ;
+        list.put( (byte) 0x1 ) ;
+        list.put( (byte) 0x1 ) ;
+        list.put( (byte) 0x1 ) ;
+        list.put( (byte) 0x1 ) ;
+        list.put( (byte) 0x1 ) ;
+        list.put( (byte) 0x1 ) ;
+        list.flip();
+
+        try
+        {
+            Length.getLength( list ) ;
+            fail( "should fail before we get here" ) ;
+        }
+        catch ( DecoderException t )
+        {
+            assertNotNull( t ) ;
+        }
+        
+        
+        list.clear() ;
+        list.put(( byte ) 0x7 ) ;
+        list.flip() ;
+        assertEquals( 7, Length.getLength( list ) ) ;
+    }
+
+
+    /**
+     * Makes sure no additions can be made after short form fixation.
+     */
+    public void testShortFixation() throws Exception
+    {
+        byte[] bites = { (byte) 0x01, (byte) 0xff } ;
+        
+        Length length = new Length() ;
+        length.add( bites[0] ) ;
+        
+        try
+        {
+            length.add( bites[1] ) ;
+            fail( "should never get here due to illegal state" ) ;
+        }
+        catch ( Throwable t ) 
+        {
+            assertNotNull( t ) ;
+        }
+
+        assertEquals( 1, length.getLength() );
+    }
+
+    /**
+     * Test that a Length could not begin with a 0xFF byte, which is
+     * reserved for future extensions.
+     *
+     */
+    public void testRestrictedValueForFutureExtension() 
+    {
+    	Length length = new Length();
+
+        try
+        {
+        	length.add( (byte) 0xFF ) ;
+        	length.add( (byte) 0x01 ) ;
+            fail( "should fail before we get here" ) ;
+        }
+        catch ( DecoderException t )
+        {
+            assertNotNull( t ) ;
+        }
+    }
+
+    /**
+     * Test that an indefinite Length form is rejected. 
+     *
+     */
+    public void testIndefiniteLength() 
+    {
+    	Length length = new Length();
+
+        try
+        {
+        	length.add( (byte) 0x80 ) ;
+        	length.add( (byte) 0x01 ) ;
+        	length.add( (byte) 0x00 ) ;
+        	length.add( (byte) 0x00 ) ;
+            fail( "should fail before we get here" ) ;
+        }
+        catch ( Throwable t )
+        {
+            assertNotNull( t ) ;
+        }
+    }
+}
+

Added: directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/MultiByteLengthTests.java
URL: http://svn.apache.org/viewcvs/directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/MultiByteLengthTests.java?rev=234226&view=auto
==============================================================================
--- directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/MultiByteLengthTests.java (added)
+++ directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/MultiByteLengthTests.java Sun Aug 21 08:03:46 2005
@@ -0,0 +1,150 @@
+/*
+ *   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.asn1.ber ;
+
+
+import org.apache.asn1.ber.AbstractDecoderTestCase;
+import org.apache.asn1.ber.BERDecoderState;
+
+
+/**
+ * Tests single byte length encodings in a BER TLV.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">
+ * Apache Directory Project</a>
+ * @version $Rev: 157644 $
+ */
+public class MultiByteLengthTests extends AbstractDecoderTestCase
+{
+    /**
+     * Creates a single byte lenth test case.
+     * 
+     * @param name the name of this test
+     */
+    public MultiByteLengthTests( String name )
+    {
+        super( name ) ;
+    }
+
+    
+    public void testLength128() throws Exception
+    {
+        Tuple tlv = decode( "01000001" ) ;
+        assertEquals( 1, tlv.id ) ;
+        assertEquals( 0, tlvList.size() ) ;
+        assertEquals( true, tlv.isPrimitive ) ;
+        assertEquals( TypeClass.APPLICATION, tlv.typeClass ) ;
+        assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+        assertEquals( 0, tlv.length ) ;
+        
+        tlv = decode( "10000001" ) ;
+        assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+        assertEquals( 0, tlv.length ) ;
+
+        tlv = decode( "10000000" ) ;
+        assertEquals( BERDecoderState.VALUE, decoder.getState() ) ;
+        assertEquals( 128, tlv.length ) ;
+    }
+
+
+    public void testLength129() throws Exception
+    {
+        Tuple tlv = decode( "01000001" ) ;
+        assertEquals( 1, tlv.id ) ;
+        assertEquals( 0, tlvList.size() ) ;
+        assertEquals( true, tlv.isPrimitive ) ;
+        assertEquals( TypeClass.APPLICATION, tlv.typeClass ) ;
+        assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+        assertEquals( 0, tlv.length ) ;
+        
+        tlv = decode( "10000001" ) ;
+        assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+        assertEquals( 0, tlv.length ) ;
+
+        tlv = decode( "10000001" ) ;
+        assertEquals( BERDecoderState.VALUE, decoder.getState() ) ;
+        assertEquals( 129, tlv.length ) ;
+    }
+
+
+    public void testLength255() throws Exception
+    {
+        Tuple tlv = decode( "01000001" ) ;
+        assertEquals( 1, tlv.id ) ;
+        assertEquals( 0, tlvList.size() ) ;
+        assertEquals( true, tlv.isPrimitive ) ;
+        assertEquals( TypeClass.APPLICATION, tlv.typeClass ) ;
+        assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+        assertEquals( 0, tlv.length ) ;
+        
+        tlv = decode( "10000001" ) ;
+        assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+        assertEquals( 0, tlv.length ) ;
+
+        tlv = decode( "11111111" ) ;
+        assertEquals( BERDecoderState.VALUE, decoder.getState() ) ;
+        assertEquals( 255, tlv.length ) ;
+    }
+    
+    
+    public void testLength32768() throws Exception
+    {
+        Tuple tlv = decode( "01000001" ) ;
+        assertEquals( 1, tlv.id ) ;
+        assertEquals( 0, tlvList.size() ) ;
+        assertEquals( true, tlv.isPrimitive ) ;
+        assertEquals( TypeClass.APPLICATION, tlv.typeClass ) ;
+        assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+        assertEquals( 0, tlv.length ) ;
+        
+        tlv = decode( (byte)0x82 ) ;
+        assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+        assertEquals( 0, tlv.length ) ;
+
+        tlv = decode( (byte) 0x80 ) ;
+        assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+        assertEquals( 0, tlv.length ) ;
+
+        tlv = decode( (byte) 0x00 ) ;
+        assertEquals( BERDecoderState.VALUE, decoder.getState() ) ;
+        assertEquals( 32768, tlv.length ) ;
+    }
+    
+    
+    public void testLength65535() throws Exception
+    {
+        Tuple tlv = decode( "01000001" ) ;
+        assertEquals( 1, tlv.id ) ;
+        assertEquals( 0, tlvList.size() ) ;
+        assertEquals( true, tlv.isPrimitive ) ;
+        assertEquals( TypeClass.APPLICATION, tlv.typeClass ) ;
+        assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+        assertEquals( 0, tlv.length ) ;
+        
+        tlv = decode( "10000010" ) ;
+        assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+        assertEquals( 0, tlv.length ) ;
+
+        tlv = decode( "11111111" ) ;
+        assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+        assertEquals( 0, tlv.length ) ;
+
+        tlv = decode( "11111111" ) ;
+        assertEquals( BERDecoderState.VALUE, decoder.getState() ) ;
+        assertEquals( 65535, tlv.length ) ;
+    }
+}
\ No newline at end of file

Added: directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/MultiByteTagTests.java
URL: http://svn.apache.org/viewcvs/directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/MultiByteTagTests.java?rev=234226&view=auto
==============================================================================
--- directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/MultiByteTagTests.java (added)
+++ directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/MultiByteTagTests.java Sun Aug 21 08:03:46 2005
@@ -0,0 +1,226 @@
+/*
+ *   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.asn1.ber ;
+
+
+import org.apache.asn1.ber.AbstractDecoderTestCase;
+import org.apache.asn1.ber.BERDecoderState;
+
+
+/**
+ * Here we test simple 1 byte tag and length values to test the decoder's 
+ * ability to handle these most simple tlvs.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">
+ * Apache Directory Project</a>
+ * @version $Rev: 157644 $
+ */
+public class MultiByteTagTests extends AbstractDecoderTestCase
+{
+    /** precalculated left shift of 1 by 14 places */
+    private static final int BIT_13 = 1 << 14 ;
+    /** precalculated left shift of 1 by 16 places */
+    private static final int BIT_15 = 1 << 16 ;
+    /** precalculated left shift of 1 by 21 places */
+    private static final int BIT_20 = 1 << 21 ;
+    /** precalculated left shift of 1 by 24 places */
+    private static final int BIT_23 = 1 << 24 ;
+    /** precalculated left shift of 1 by 28 places */
+    private static final int BIT_27 = 1 << 28 ;
+    
+
+    /**
+     * @param arg0
+     */
+    public MultiByteTagTests( String arg0 )
+    {
+        super( arg0 ) ;
+    }
+    
+    
+    public void testId31() throws Exception
+    {
+        Tuple tlv = decode( "01011111" ) ;
+        assertEquals( 0, tlv.id ) ;
+        assertEquals( 0, tlvList.size() ) ;
+        assertEquals( true, tlv.isPrimitive ) ;
+        assertEquals( TypeClass.APPLICATION, tlv.typeClass ) ;
+        assertEquals( BERDecoderState.TAG, decoder.getState() ) ;
+
+        tlv = decode( "00011111" ) ;
+        assertEquals( 31, tlv.id ) ;
+        assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+    }
+
+
+    public void testId100() throws Exception
+    {
+        Tuple tlv = decode( "01011111" ) ;
+        assertEquals( 0, tlv.id ) ;
+        assertEquals( 0, tlvList.size() ) ;
+        assertEquals( true, tlv.isPrimitive ) ;
+        assertEquals( TypeClass.APPLICATION, tlv.typeClass ) ;
+        assertEquals( BERDecoderState.TAG, decoder.getState() ) ;
+
+        tlv = decode( "01100100" ) ;
+        assertEquals( 100, tlv.id ) ;
+        assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+    }
+
+
+    public void testId127() throws Exception
+    {
+        Tuple tlv = decode( "01011111" ) ;
+        assertEquals( 0, tlv.id ) ;
+        assertEquals( 0, tlvList.size() ) ;
+        assertEquals( true, tlv.isPrimitive ) ;
+        assertEquals( TypeClass.APPLICATION, tlv.typeClass ) ;
+        assertEquals( BERDecoderState.TAG, decoder.getState() ) ;
+
+        tlv = decode( "01111111" ) ;
+        assertEquals( 127, tlv.id ) ;
+        assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+    }
+
+
+    public void testId128() throws Exception
+    {
+        Tuple tlv = decode( "01011111" ) ;
+        assertEquals( 0, tlv.id ) ;
+        assertEquals( 0, tlvList.size() ) ;
+        assertEquals( true, tlv.isPrimitive ) ;
+        assertEquals( TypeClass.APPLICATION, tlv.typeClass ) ;
+        assertEquals( BERDecoderState.TAG, decoder.getState() ) ;
+
+        tlv = decode( "10000000" ) ;
+        assertEquals( 0, tlv.id ) ;
+        assertEquals( BERDecoderState.TAG, decoder.getState() ) ;
+
+        tlv = decode( "00000001" ) ;
+        assertEquals( 1, tlv.id ) ;
+        assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+    }
+
+
+    public void testId129() throws Exception
+    {
+        Tuple tlv = decode( "01011111" ) ;
+        assertEquals( 0, tlv.id ) ;
+        assertEquals( 0, tlvList.size() ) ;
+        assertEquals( true, tlv.isPrimitive ) ;
+        assertEquals( TypeClass.APPLICATION, tlv.typeClass ) ;
+        assertEquals( BERDecoderState.TAG, decoder.getState() ) ;
+
+        tlv = decode( "10000001" ) ;
+        assertEquals( 0, tlv.id ) ;
+        assertEquals( BERDecoderState.TAG, decoder.getState() ) ;
+
+        tlv = decode( "00000001" ) ;
+        assertEquals( 129, tlv.id ) ;
+        assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+    }
+
+
+
+
+    public void testIdShift14() throws Exception
+    {
+        Tuple tlv = decode( "01011111" ) ;
+        assertEquals( 0, tlv.id ) ;
+        assertEquals( 0, tlvList.size() ) ;
+        assertEquals( true, tlv.isPrimitive ) ;
+        assertEquals( TypeClass.APPLICATION, tlv.typeClass ) ;
+        assertEquals( BERDecoderState.TAG, decoder.getState() ) ;
+
+        tlv = decode( "10000001" ) ;
+        assertEquals( 0, tlv.id ) ;
+        assertEquals( BERDecoderState.TAG, decoder.getState() ) ;
+
+        tlv = decode( "10000000" ) ;
+        assertEquals( 0, tlv.id ) ;
+        assertEquals( BERDecoderState.TAG, decoder.getState() ) ;
+
+        tlv = decode( "00000000" ) ;
+        assertEquals( BIT_13, tlv.id ) ;
+        assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+    }
+
+
+    public void testIdShift14Minus1() throws Exception
+    {
+        Tuple tlv = decode( "01011111" ) ;
+        assertEquals( 0, tlv.id ) ;
+        assertEquals( 0, tlvList.size() ) ;
+        assertEquals( true, tlv.isPrimitive ) ;
+        assertEquals( TypeClass.APPLICATION, tlv.typeClass ) ;
+        assertEquals( BERDecoderState.TAG, decoder.getState() ) ;
+
+        tlv = decode( "11111111" ) ;
+        assertEquals( 0, tlv.id ) ;
+        assertEquals( BERDecoderState.TAG, decoder.getState() ) ;
+
+        tlv = decode( "01111111" ) ;
+        assertEquals( BIT_13 - 1, tlv.id ) ;
+        assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+    }
+
+
+    public void testIdShift14Plus1() throws Exception
+    {
+        Tuple tlv = decode( "01011111" ) ;
+        assertEquals( 0, tlv.id ) ;
+        assertEquals( 0, tlvList.size() ) ;
+        assertEquals( true, tlv.isPrimitive ) ;
+        assertEquals( TypeClass.APPLICATION, tlv.typeClass ) ;
+        assertEquals( BERDecoderState.TAG, decoder.getState() ) ;
+
+        tlv = decode( "10000001" ) ;
+        assertEquals( 0, tlv.id ) ;
+        assertEquals( BERDecoderState.TAG, decoder.getState() ) ;
+
+        tlv = decode( "10000000" ) ;
+        assertEquals( 0, tlv.id ) ;
+        assertEquals( BERDecoderState.TAG, decoder.getState() ) ;
+
+        tlv = decode( "00000001" ) ;
+        assertEquals( BIT_13 + 1, tlv.id ) ;
+        assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+    }
+
+    
+    public void testIdShift21Minus1() throws Exception
+    {
+        Tuple tlv = decode( "01011111" ) ;
+        assertEquals( 0, tlv.id ) ;
+        assertEquals( 0, tlvList.size() ) ;
+        assertEquals( true, tlv.isPrimitive ) ;
+        assertEquals( TypeClass.APPLICATION, tlv.typeClass ) ;
+        assertEquals( BERDecoderState.TAG, decoder.getState() ) ;
+
+        tlv = decode( "11111111" ) ;
+        assertEquals( 0, tlv.id ) ;
+        assertEquals( BERDecoderState.TAG, decoder.getState() ) ;
+
+        tlv = decode( "11111111" ) ;
+        assertEquals( 0, tlv.id ) ;
+        assertEquals( BERDecoderState.TAG, decoder.getState() ) ;
+
+        tlv = decode( "01111111" ) ;
+        assertEquals( BIT_20 - 1, tlv.id ) ;
+        assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+    }
+}

Added: directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/SimplePrimitiveTLVTests.java
URL: http://svn.apache.org/viewcvs/directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/SimplePrimitiveTLVTests.java?rev=234226&view=auto
==============================================================================
--- directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/SimplePrimitiveTLVTests.java (added)
+++ directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/SimplePrimitiveTLVTests.java Sun Aug 21 08:03:46 2005
@@ -0,0 +1,167 @@
+/*
+ *   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.asn1.ber ;
+
+import java.nio.ByteBuffer;
+
+import org.apache.asn1.ber.AbstractDecoderTestCase;
+import org.apache.asn1.ber.BERDecoderState;
+
+
+/**
+ * Performs simple primitive tlv tests.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">
+ * Apache Directory Project</a>
+ * @version $Rev: 157644 $
+ */
+public class SimplePrimitiveTLVTests extends AbstractDecoderTestCase
+{
+
+    /**
+     * @param arg0
+     */
+    public SimplePrimitiveTLVTests( String arg0 )
+    {
+        super( arg0 ) ;
+    }
+    
+    
+    public void testSingleSimpleTLV() throws Exception
+    {
+        // decode tag
+        Tuple tlv = decode( "01000001" ) ;
+        assertEquals( 1, tlv.id ) ;
+        assertEquals( 0, tlvList.size() ) ;
+        assertEquals( true, tlv.isPrimitive ) ;
+        assertEquals( TypeClass.APPLICATION, tlv.typeClass ) ;
+        assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+        
+        // decode length 
+        tlv = decode( "00000001" ) ;
+        assertEquals( BERDecoderState.VALUE, decoder.getState() ) ;
+        assertEquals( 1, tlv.length ) ;
+        
+        // decode value
+        tlv = decode( "01010101" ) ;
+        assertEquals( BERDecoderState.TAG, decoder.getState() ) ;
+        assertEquals( 1, tlvList.size() ) ;
+        assertNotNull( tlv.valueChunk ) ;
+        assertEquals( 0x0055, 0x00ff & tlv.valueChunk.get( 0 ) ) ;
+    }
+
+
+    public void testMultipleSimpleTLV() throws Exception
+    {
+        // decode tag
+        Tuple tlv = decode( "01000001" ) ;
+        assertEquals( 1, tlv.id ) ;
+        assertEquals( 0, tlvList.size() ) ;
+        assertEquals( true, tlv.isPrimitive ) ;
+        assertEquals( TypeClass.APPLICATION, tlv.typeClass ) ;
+        assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+        
+        // decode length 
+        tlv = decode( "00000001" ) ;
+        assertEquals( BERDecoderState.VALUE, decoder.getState() ) ;
+        assertEquals( 1, tlv.length ) ;
+        
+        // decode value
+        tlv = decode( "01010101" ) ;
+        assertEquals( BERDecoderState.TAG, decoder.getState() ) ;
+        assertEquals( 1, tlvList.size() ) ;
+        assertNotNull( tlv.valueChunk ) ;
+        assertEquals( 0x0055, 0x00ff & tlv.valueChunk.get( 0 ) ) ;
+
+    
+        // decode tag
+        tlv = decode( "01000001" ) ;
+        assertEquals( 1, tlv.id ) ;
+        assertEquals( 1, tlvList.size() ) ;
+        assertEquals( true, tlv.isPrimitive ) ;
+        assertEquals( TypeClass.APPLICATION, tlv.typeClass ) ;
+        assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+        
+        // decode length 
+        tlv = decode( "00000001" ) ;
+        assertEquals( BERDecoderState.VALUE, decoder.getState() ) ;
+        assertEquals( 1, tlv.length ) ;
+        
+        // decode value
+        tlv = decode( "01010101" ) ;
+        assertEquals( BERDecoderState.TAG, decoder.getState() ) ;
+        assertEquals( 2, tlvList.size() ) ;
+        assertNotNull( tlv.valueChunk ) ;
+        assertEquals( 0x0055, 0x00ff & tlv.valueChunk.get( 0 ) ) ;
+
+    
+        // decode tag
+        tlv = decode( "01000001" ) ;
+        assertEquals( 1, tlv.id ) ;
+        assertEquals( 2, tlvList.size() ) ;
+        assertEquals( true, tlv.isPrimitive ) ;
+        assertEquals( TypeClass.APPLICATION, tlv.typeClass ) ;
+        assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+        
+        // decode length 
+        tlv = decode( "00000001" ) ;
+        assertEquals( BERDecoderState.VALUE, decoder.getState() ) ;
+        assertEquals( 1, tlv.length ) ;
+        
+        // decode value
+        tlv = decode( "01010101" ) ;
+        assertEquals( BERDecoderState.TAG, decoder.getState() ) ;
+        assertEquals( 3, tlvList.size() ) ;
+        assertNotNull( tlv.valueChunk ) ;
+        assertEquals( 0x0055, 0x00ff & tlv.valueChunk.get( 0 ) ) ;
+    }
+
+
+    public void testSingleSimpleLongTLV() throws Exception
+    {
+        // decode tag
+        Tuple tlv = decode( "01000001" ) ;
+        assertEquals( 1, tlv.id ) ;
+        assertEquals( 0, tlvList.size() ) ;
+        assertEquals( true, tlv.isPrimitive ) ;
+        assertEquals( TypeClass.APPLICATION, tlv.typeClass ) ;
+        assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+        
+        // decode length 
+        tlv = decode( "10000001" ) ;
+        assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+        assertEquals( 0, tlv.length ) ;
+        
+        tlv = decode( "00000111" ) ;
+        assertEquals( BERDecoderState.VALUE, decoder.getState() ) ;
+        assertEquals( 7, tlv.length ) ;
+        
+        // decode value
+        tlv = decode( "01010101" + "01010101" + "01010101" + "01010101"
+                + "01010101" + "01010101" + "01010101" ) ;
+        assertEquals( BERDecoderState.TAG, decoder.getState() ) ;
+        assertEquals( 1, tlvList.size() ) ;
+        assertNotNull( tlv.valueChunk ) ;
+        assertEquals( 7, tlv.valueChunk.capacity() ) ;
+        
+        ByteBuffer value = tlv.valueChunk.duplicate() ;
+        for ( int ii = 0 ; ii < 7; ii++ )
+        {    
+            assertEquals( 0x0055, 0x00ff & value.get( ii ) ) ;
+        }
+    }
+}

Added: directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/SingleByteLengthTests.java
URL: http://svn.apache.org/viewcvs/directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/SingleByteLengthTests.java?rev=234226&view=auto
==============================================================================
--- directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/SingleByteLengthTests.java (added)
+++ directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/SingleByteLengthTests.java Sun Aug 21 08:03:46 2005
@@ -0,0 +1,90 @@
+/*
+ *   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.asn1.ber ;
+
+
+import org.apache.asn1.ber.AbstractDecoderTestCase;
+import org.apache.asn1.ber.BERDecoderState;
+
+
+/**
+ * Tests single byte length encodings in a BER TLV.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">
+ * Apache Directory Project</a>
+ * @version $Rev: 157644 $
+ */
+public class SingleByteLengthTests extends AbstractDecoderTestCase
+{
+    /**
+     * Creates a single byte lenth test case.
+     * 
+     * @param name the name of this test
+     */
+    public SingleByteLengthTests( String name )
+    {
+        super( name ) ;
+    }
+
+    
+    public void testLength0() throws Exception
+    {
+        Tuple tlv = decode( "00000000" + "01000001" ) ;
+        assertEquals( 1, tlv.id ) ;
+        assertEquals( 1, tlvList.size() ) ;
+        assertEquals( true, tlv.isPrimitive ) ;
+        assertEquals( TypeClass.APPLICATION, tlv.typeClass ) ;
+        assertEquals( BERDecoderState.TAG, decoder.getState() ) ;
+        assertEquals( 0, tlv.length ) ;
+    }
+
+
+    public void testLength1() throws Exception
+    {
+        Tuple tlv = decode( "00000001" + "01000001" ) ;
+        assertEquals( 1, tlv.id ) ;
+        assertEquals( 0, tlvList.size() ) ;
+        assertEquals( true, tlv.isPrimitive ) ;
+        assertEquals( TypeClass.APPLICATION, tlv.typeClass ) ;
+        assertEquals( BERDecoderState.VALUE, decoder.getState() ) ;
+        assertEquals( 1, tlv.length ) ;
+    }
+
+
+    public void testLength3() throws Exception
+    {
+        Tuple tlv = decode( "00000011" + "01000001" ) ;
+        assertEquals( 1, tlv.id ) ;
+        assertEquals( 0, tlvList.size() ) ;
+        assertEquals( true, tlv.isPrimitive ) ;
+        assertEquals( TypeClass.APPLICATION, tlv.typeClass ) ;
+        assertEquals( BERDecoderState.VALUE, decoder.getState() ) ;
+        assertEquals( 3, tlv.length ) ;
+    }
+
+
+    public void testLength127() throws Exception
+    {
+        Tuple tlv = decode( "01111111" + "01000001" ) ;
+        assertEquals( 1, tlv.id ) ;
+        assertEquals( 0, tlvList.size() ) ;
+        assertEquals( true, tlv.isPrimitive ) ;
+        assertEquals( TypeClass.APPLICATION, tlv.typeClass ) ;
+        assertEquals( BERDecoderState.VALUE, decoder.getState() ) ;
+        assertEquals( 127, tlv.length ) ;
+    }
+}

Added: directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/SingleByteTagTests.java
URL: http://svn.apache.org/viewcvs/directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/SingleByteTagTests.java?rev=234226&view=auto
==============================================================================
--- directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/SingleByteTagTests.java (added)
+++ directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/SingleByteTagTests.java Sun Aug 21 08:03:46 2005
@@ -0,0 +1,454 @@
+/*
+ *   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.asn1.ber ;
+
+
+import org.apache.asn1.ber.AbstractDecoderTestCase;
+import org.apache.asn1.ber.BERDecoderState;
+
+
+/**
+ * Here we test simple 1 byte tag and length values to test the decoder's 
+ * ability to handle these most simple tlvs.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">
+ * Apache Directory Project</a>
+ * @version $Rev: 157644 $
+ */
+public class SingleByteTagTests extends AbstractDecoderTestCase
+{
+
+    /**
+     * @param arg0
+     */
+    public SingleByteTagTests( String arg0 )
+    {
+        super( arg0 ) ;
+    }
+    
+    
+    public void testAppTypeClass() throws Exception
+    {
+        Tuple tlv = decode( "01000001" ) ;
+        assertEquals( 1, tlv.id ) ;
+        assertEquals( 0, tlvList.size() ) ;
+        assertEquals( true, tlv.isPrimitive ) ;
+        assertEquals( TypeClass.APPLICATION, tlv.typeClass ) ;
+        assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+    }
+
+
+    public void testPrivTypeClass() throws Exception
+    {
+        Tuple tlv = decode( "11000001" ) ;
+        assertEquals( 1, tlv.id ) ;
+        assertEquals( 0, tlvList.size() ) ;
+        assertEquals( true, tlv.isPrimitive ) ;
+        assertEquals( TypeClass.PRIVATE, tlv.typeClass ) ;
+        assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+    }
+
+
+    public void testCtxTypeClass() throws Exception
+    {
+        Tuple tlv = decode( "10000001" ) ;
+        assertEquals( 1, tlv.id ) ;
+        assertEquals( 0, tlvList.size() ) ;
+        assertEquals( true, tlv.isPrimitive ) ;
+        assertEquals( TypeClass.CONTEXT_SPECIFIC, tlv.typeClass ) ;
+        assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+    }
+
+    
+    public void testUniTypeClass() throws Exception
+    {
+        Tuple tlv = decode( "01000001" ) ;
+        assertEquals( 1, tlv.id ) ;
+        assertEquals( 0, tlvList.size() ) ;
+        assertEquals( true, tlv.isPrimitive ) ;
+        assertEquals( TypeClass.APPLICATION, tlv.typeClass ) ;
+        assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+
+    }
+    
+    public void testIllegalStateWithUniTypeClass() throws Exception
+    {
+        try
+        {
+            Tuple tlv = decode( "00000001" ) ;
+        }
+        catch( IllegalStateException e ) 
+        {
+            assertNotNull( e ) ;
+        }
+    }
+
+    public void testId1() throws Exception
+    {
+        Tuple tlv = decode( "01000001" ) ;
+        assertEquals( 1, tlv.id ) ;
+        assertEquals( 0, tlvList.size() ) ;
+        assertEquals( true, tlv.isPrimitive ) ;
+        assertEquals( TypeClass.APPLICATION, tlv.typeClass ) ;
+        assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+    }
+
+
+    public void testId2() throws Exception
+    {
+        Tuple tlv = decode( "01000010" ) ;
+        assertEquals( 2, tlv.id ) ;
+        assertEquals( 0, tlvList.size() ) ;
+        assertEquals( true, tlv.isPrimitive ) ;
+        assertEquals( TypeClass.APPLICATION, tlv.typeClass ) ;
+        assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+    }
+
+
+    public void testId3() throws Exception
+    {
+        Tuple tlv = decode( "01000011" ) ;
+        assertEquals( 3, tlv.id ) ;
+        assertEquals( 0, tlvList.size() ) ;
+        assertEquals( true, tlv.isPrimitive ) ;
+        assertEquals( TypeClass.APPLICATION, tlv.typeClass ) ;
+        assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+    }
+
+
+    public void testId4() throws Exception
+    {
+        Tuple tlv = decode( "01000100" ) ;
+        assertEquals( 4, tlv.id ) ;
+        assertEquals( 0, tlvList.size() ) ;
+        assertEquals( true, tlv.isPrimitive ) ;
+        assertEquals( TypeClass.APPLICATION, tlv.typeClass ) ;
+        assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+    }
+
+
+    public void testId5() throws Exception
+    {
+        Tuple tlv = decode( "01000101" ) ;
+        assertEquals( 5, tlv.id ) ;
+        assertEquals( 0, tlvList.size() ) ;
+        assertEquals( true, tlv.isPrimitive ) ;
+        assertEquals( TypeClass.APPLICATION, tlv.typeClass ) ;
+        assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+    }
+
+
+    public void testId6() throws Exception
+    {
+        Tuple tlv = decode( "01000110" ) ;
+        assertEquals( 6, tlv.id ) ;
+        assertEquals( 0, tlvList.size() ) ;
+        assertEquals( true, tlv.isPrimitive ) ;
+        assertEquals( TypeClass.APPLICATION, tlv.typeClass ) ;
+        assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+    }
+
+
+    public void testId7() throws Exception
+    {
+        Tuple tlv = decode( "01000111" ) ;
+        assertEquals( 7, tlv.id ) ;
+        assertEquals( 0, tlvList.size() ) ;
+        assertEquals( true, tlv.isPrimitive ) ;
+        assertEquals( TypeClass.APPLICATION, tlv.typeClass ) ;
+        assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+    }
+
+
+    public void testId8() throws Exception
+    {
+        Tuple tlv = decode( "01001000" ) ;
+        assertEquals( 8, tlv.id ) ;
+        assertEquals( 0, tlvList.size() ) ;
+        assertEquals( true, tlv.isPrimitive ) ;
+        assertEquals( TypeClass.APPLICATION, tlv.typeClass ) ;
+        assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+    }
+
+
+    public void testId9() throws Exception
+    {
+        Tuple tlv = decode( "01001001" ) ;
+        assertEquals( 9, tlv.id ) ;
+        assertEquals( 0, tlvList.size() ) ;
+        assertEquals( true, tlv.isPrimitive ) ;
+        assertEquals( TypeClass.APPLICATION, tlv.typeClass ) ;
+        assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+    }
+
+
+    public void testId10() throws Exception
+    {
+        Tuple tlv = decode( "01001010" ) ;
+        assertEquals( 10, tlv.id ) ;
+        assertEquals( 0, tlvList.size() ) ;
+        assertEquals( true, tlv.isPrimitive ) ;
+        assertEquals( TypeClass.APPLICATION, tlv.typeClass ) ;
+        assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+    }
+
+
+    public void testId11() throws Exception
+    {
+        Tuple tlv = decode( "01001011" ) ;
+        assertEquals( 11, tlv.id ) ;
+        assertEquals( 0, tlvList.size() ) ;
+        assertEquals( true, tlv.isPrimitive ) ;
+        assertEquals( TypeClass.APPLICATION, tlv.typeClass ) ;
+        assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+    }
+
+
+    public void testId12() throws Exception
+    {
+        Tuple tlv = decode( "01001100" ) ;
+        assertEquals( 12, tlv.id ) ;
+        assertEquals( 0, tlvList.size() ) ;
+        assertEquals( true, tlv.isPrimitive ) ;
+        assertEquals( TypeClass.APPLICATION, tlv.typeClass ) ;
+        assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+    }
+
+
+    public void testId13() throws Exception
+    {
+        Tuple tlv = decode( "01001101" ) ;
+        assertEquals( 13, tlv.id ) ;
+        assertEquals( 0, tlvList.size() ) ;
+        assertEquals( true, tlv.isPrimitive ) ;
+        assertEquals( TypeClass.APPLICATION, tlv.typeClass ) ;
+        assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+    }
+
+
+    public void testId14() throws Exception
+    {
+        Tuple tlv = decode( "01001110" ) ;
+        assertEquals( 14, tlv.id ) ;
+        assertEquals( 0, tlvList.size() ) ;
+        assertEquals( true, tlv.isPrimitive ) ;
+        assertEquals( TypeClass.APPLICATION, tlv.typeClass ) ;
+        assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+    }
+
+
+    public void testId15() throws Exception
+    {
+        Tuple tlv = decode( "01001111" ) ;
+        assertEquals( 15, tlv.id ) ;
+        assertEquals( 0, tlvList.size() ) ;
+        assertEquals( true, tlv.isPrimitive ) ;
+        assertEquals( TypeClass.APPLICATION, tlv.typeClass ) ;
+        assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+    }
+
+
+    public void testId16() throws Exception
+    {
+        Tuple tlv = decode( "01010000" ) ;
+        assertEquals( 16, tlv.id ) ;
+        assertEquals( 0, tlvList.size() ) ;
+        assertEquals( true, tlv.isPrimitive ) ;
+        assertEquals( TypeClass.APPLICATION, tlv.typeClass ) ;
+        assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+    }
+
+
+    public void testId17() throws Exception
+    {
+        Tuple tlv = decode( "01010001" ) ;
+        assertEquals( 17, tlv.id ) ;
+        assertEquals( 0, tlvList.size() ) ;
+        assertEquals( true, tlv.isPrimitive ) ;
+        assertEquals( TypeClass.APPLICATION, tlv.typeClass ) ;
+        assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+    }
+
+
+    public void testId18() throws Exception
+    {
+        Tuple tlv = decode( "01010010" ) ;
+        assertEquals( 18, tlv.id ) ;
+        assertEquals( 0, tlvList.size() ) ;
+        assertEquals( true, tlv.isPrimitive ) ;
+        assertEquals( TypeClass.APPLICATION, tlv.typeClass ) ;
+        assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+    }
+
+
+    public void testId19() throws Exception
+    {
+        Tuple tlv = decode( "01010011" ) ;
+        assertEquals( 19, tlv.id ) ;
+        assertEquals( 0, tlvList.size() ) ;
+        assertEquals( true, tlv.isPrimitive ) ;
+        assertEquals( TypeClass.APPLICATION, tlv.typeClass ) ;
+        assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+    }
+
+
+    public void testId20() throws Exception
+    {
+        Tuple tlv = decode( "01010100" ) ;
+        assertEquals( 20, tlv.id ) ;
+        assertEquals( 0, tlvList.size() ) ;
+        assertEquals( true, tlv.isPrimitive ) ;
+        assertEquals( TypeClass.APPLICATION, tlv.typeClass ) ;
+        assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+    }
+
+
+    public void testId21() throws Exception
+    {
+        Tuple tlv = decode( "01010101" ) ;
+        assertEquals( 21, tlv.id ) ;
+        assertEquals( 0, tlvList.size() ) ;
+        assertEquals( true, tlv.isPrimitive ) ;
+        assertEquals( TypeClass.APPLICATION, tlv.typeClass ) ;
+        assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+    }
+
+
+    public void testId22() throws Exception
+    {
+        Tuple tlv = decode( "01010110" ) ;
+        assertEquals( 22, tlv.id ) ;
+        assertEquals( 0, tlvList.size() ) ;
+        assertEquals( true, tlv.isPrimitive ) ;
+        assertEquals( TypeClass.APPLICATION, tlv.typeClass ) ;
+        assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+    }
+
+
+    public void testId23() throws Exception
+    {
+        Tuple tlv = decode( "01010111" ) ;
+        assertEquals( 23, tlv.id ) ;
+        assertEquals( 0, tlvList.size() ) ;
+        assertEquals( true, tlv.isPrimitive ) ;
+        assertEquals( TypeClass.APPLICATION, tlv.typeClass ) ;
+        assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+    }
+
+
+    public void testId24() throws Exception
+    {
+        Tuple tlv = decode( "01011000" ) ;
+        assertEquals( 24, tlv.id ) ;
+        assertEquals( 0, tlvList.size() ) ;
+        assertEquals( true, tlv.isPrimitive ) ;
+        assertEquals( TypeClass.APPLICATION, tlv.typeClass ) ;
+        assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+    }
+
+
+    public void testId25() throws Exception
+    {
+        Tuple tlv = decode( "01011001" ) ;
+        assertEquals( 25, tlv.id ) ;
+        assertEquals( 0, tlvList.size() ) ;
+        assertEquals( true, tlv.isPrimitive ) ;
+        assertEquals( TypeClass.APPLICATION, tlv.typeClass ) ;
+        assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+    }
+
+
+    public void testId26() throws Exception
+    {
+        Tuple tlv = decode( "01011010" ) ;
+        assertEquals( 26, tlv.id ) ;
+        assertEquals( 0, tlvList.size() ) ;
+        assertEquals( true, tlv.isPrimitive ) ;
+        assertEquals( TypeClass.APPLICATION, tlv.typeClass ) ;
+        assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+    }
+
+
+    public void testId27() throws Exception
+    {
+        Tuple tlv = decode( "01011011" ) ;
+        assertEquals( 27, tlv.id ) ;
+        assertEquals( 0, tlvList.size() ) ;
+        assertEquals( true, tlv.isPrimitive ) ;
+        assertEquals( TypeClass.APPLICATION, tlv.typeClass ) ;
+        assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+    }
+
+
+    public void testId28() throws Exception
+    {
+        Tuple tlv = decode( "01011100" ) ;
+        assertEquals( 28, tlv.id ) ;
+        assertEquals( 0, tlvList.size() ) ;
+        assertEquals( true, tlv.isPrimitive ) ;
+        assertEquals( TypeClass.APPLICATION, tlv.typeClass ) ;
+        assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+    }
+
+
+    public void testId29() throws Exception
+    {
+        Tuple tlv = decode( "01011101" ) ;
+        assertEquals( 29, tlv.id ) ;
+        assertEquals( 0, tlvList.size() ) ;
+        assertEquals( true, tlv.isPrimitive ) ;
+        assertEquals( TypeClass.APPLICATION, tlv.typeClass ) ;
+        assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+    }
+
+
+    public void testId30() throws Exception
+    {
+        Tuple tlv = decode( "01011110" ) ;
+        assertEquals( 30, tlv.id ) ;
+        assertEquals( 0, tlvList.size() ) ;
+        assertEquals( true, tlv.isPrimitive ) ;
+        assertEquals( TypeClass.APPLICATION, tlv.typeClass ) ;
+        assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+    }
+    
+    
+    public void testIdOverLimit() throws Exception
+    {
+        // this is the long form
+        Tuple tlv = decode( "01011111" ) ;
+        
+        assertEquals( 0, tlv.id ) ;
+        assertEquals( 0, tlvList.size() ) ;
+        assertEquals( true, tlv.isPrimitive ) ;
+        assertEquals( TypeClass.APPLICATION, tlv.typeClass ) ;
+        
+        // state did not switch since we are still reading the long tag  
+        assertEquals( BERDecoderState.TAG, decoder.getState() ) ;
+    }
+
+    
+    public void testIsConstructed() throws Exception
+    {
+        Tuple tlv = decode( "01111110" ) ;
+        assertEquals( 30, tlv.id ) ;
+        assertEquals( 0, tlvList.size() ) ;
+        assertEquals( false, tlv.isPrimitive ) ;
+        assertEquals( TypeClass.APPLICATION, tlv.typeClass ) ;
+        assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+    }
+}

Added: directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/TagTest.java
URL: http://svn.apache.org/viewcvs/directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/TagTest.java?rev=234226&view=auto
==============================================================================
--- directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/TagTest.java (added)
+++ directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/TagTest.java Sun Aug 21 08:03:46 2005
@@ -0,0 +1,405 @@
+/*
+ *   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.asn1.ber ;
+
+
+import org.apache.commons.lang.ArrayUtils ;
+import org.apache.asn1.ber.primitives.UniversalTag;
+import org.apache.asn1.ber.primitives.UniversalTag;
+import org.apache.asn1.ber.Tag;
+
+import junit.framework.TestCase ;
+
+import java.nio.BufferOverflowException;
+
+
+/**
+ * Tests the BER utility functions.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">
+ * Apache Directory Project</a>
+ * @version $Rev: 157644 $
+ */
+public class TagTest extends TestCase
+{
+    private static final int BIT_0 = 0x01 ;
+    private static final int BIT_1 = 0x02 ;
+    private static final int BIT_2 = 0x04 ;
+    private static final int BIT_3 = 0x08 ;
+    private static final int BIT_4 = 0x10 ;
+    private static final int BIT_5 = 0x20 ;
+    private static final int BIT_6 = 0x40 ;
+    private static final int BIT_7 = 0x80 ;
+
+    
+    public static void main( String[] args )
+    {
+        junit.textui.TestRunner.run( TagTest.class ) ;
+    }
+
+    
+    /*
+     * @see TestCase#setUp()
+     */
+    protected void setUp() throws Exception
+    {
+        super.setUp();
+    }
+
+    
+    /*
+     * @see TestCase#tearDown()
+     */
+    protected void tearDown() throws Exception
+    {
+        super.tearDown();
+    }
+
+    
+    /**
+     * Constructor for TagTest.
+     * @param arg0
+     */
+    public TagTest(String arg0)
+    {
+        super(arg0);
+    }
+    
+    
+    public void getTypeClass()
+    {
+        assertEquals( TypeClass.UNIVERSAL, TypeClass.getTypeClass( 0 ) ) ;
+    }
+    
+    
+    public void testIsPrimitive() throws Exception
+    {
+        byte octet = BIT_5 ;
+        
+        assertFalse( Tag.isPrimitive( octet ) ) ;
+        assertFalse( Tag.isPrimitive( BIT_5 ) ) ;
+        
+        assertTrue( Tag.isPrimitive( 0 ) ) ;
+        assertTrue( Tag.isPrimitive( BIT_0 ) ) ;
+        assertTrue( Tag.isPrimitive( BIT_1 ) ) ;
+        assertTrue( Tag.isPrimitive( BIT_2 ) ) ;
+        assertTrue( Tag.isPrimitive( BIT_3 ) ) ;
+        assertTrue( Tag.isPrimitive( BIT_4 ) ) ;
+        assertTrue( Tag.isPrimitive( BIT_6 ) ) ;
+        assertTrue( Tag.isPrimitive( BIT_7 ) ) ;
+    }
+
+
+    public void testIsConstructed() throws Exception
+    {
+        byte octet = BIT_5 ;
+        
+        assertTrue( Tag.isConstructed( octet ) ) ;
+        assertTrue( Tag.isConstructed( BIT_5 ) ) ;
+        
+        assertFalse( Tag.isConstructed( 0 ) ) ;
+        assertFalse( Tag.isConstructed( BIT_0 ) ) ;
+        assertFalse( Tag.isConstructed( BIT_1 ) ) ;
+        assertFalse( Tag.isConstructed( BIT_2 ) ) ;
+        assertFalse( Tag.isConstructed( BIT_3 ) ) ;
+        assertFalse( Tag.isConstructed( BIT_4 ) ) ;
+        assertFalse( Tag.isConstructed( BIT_6 ) ) ;
+        assertFalse( Tag.isConstructed( BIT_7 ) ) ;
+    }
+    
+    
+    public void testGetTagIdInt() throws Exception
+    {
+        int rawTag = Tag.getIntEncodedTag( TypeClass.UNIVERSAL, 0, false );
+        assertEquals( Tag.getTagId( rawTag ), 0 );
+
+        rawTag = Tag.getIntEncodedTag( TypeClass.UNIVERSAL, 3, false );
+        assertEquals( Tag.getTagId( rawTag ), 3 );
+
+        rawTag = Tag.getIntEncodedTag( TypeClass.UNIVERSAL, 30, false );
+        assertEquals( Tag.getTagId( rawTag ), 30 );
+
+        rawTag = Tag.getIntEncodedTag( TypeClass.UNIVERSAL, 31, false );
+        assertEquals( Tag.getTagId( rawTag ), 31 );
+
+        rawTag = Tag.getIntEncodedTag( TypeClass.UNIVERSAL, 126, false );
+        assertEquals( Tag.getTagId( rawTag ), 126 );
+
+        rawTag = Tag.getIntEncodedTag( TypeClass.UNIVERSAL, 127, false );
+        assertEquals( Tag.getTagId( rawTag ), 127 );
+
+        rawTag = Tag.getIntEncodedTag( TypeClass.UNIVERSAL, 128, false );
+        assertEquals( Tag.getTagId( rawTag ), 128 );
+
+        rawTag = Tag.getIntEncodedTag( TypeClass.UNIVERSAL, 16382, false );
+        assertEquals( Tag.getTagId( rawTag ), 16382 );
+
+        rawTag = Tag.getIntEncodedTag( TypeClass.UNIVERSAL, 16383, false );
+        assertEquals( Tag.getTagId( rawTag ), 16383 );
+
+        rawTag = Tag.getIntEncodedTag( TypeClass.UNIVERSAL, 16384, false );
+        assertEquals( Tag.getTagId( rawTag ), 16384 );
+
+        rawTag = Tag.getIntEncodedTag( TypeClass.UNIVERSAL, 2097150, false );
+        assertEquals( Tag.getTagId( rawTag ), 2097150 );
+
+        rawTag = Tag.getIntEncodedTag( TypeClass.UNIVERSAL, 2097151, false );
+        assertEquals( Tag.getTagId( rawTag ), 2097151 );
+    }
+
+
+    public void testGetTagIdByteArray() throws Exception
+    {
+        byte[] octets = new byte[1] ;
+
+        for ( int ii = 0 ; ii < 128; ii++ )
+        {
+            octets[0] = ( byte ) ii ;
+
+            if ( ii < 31 )
+            {
+                assertEquals( Tag.getTagId( octets ), ii ) ;
+            }
+            else
+            {
+                assertTrue( Tag.getTagId( octets ) != ii ) ;
+            }
+        }
+
+        octets = new byte[2] ;
+        octets[0] = 31 ;
+        octets[1] = 0 ;
+
+        for ( int ii = 31 ; ii < 255; ii++ )
+        {
+            octets[1] = ( byte ) ii ;
+
+            if ( ii < 128 )
+            {
+                assertEquals( Tag.getTagId( octets ), ii ) ;
+            }
+            else
+            {
+                assertTrue( Tag.getTagId( octets ) != ii ) ;
+            }
+        }
+
+        octets = new byte[3] ;
+        octets[0] = 31 ;
+        octets[1] = 0 ;
+        octets[2] = 0 ;
+
+        for ( int ii = 128 ; ii < 20000; ii++ )
+        {
+            octets[1] = (byte)((ii >> 7) & Tag.LONG_MASK);
+            octets[2] = (byte)(ii & Tag.LONG_MASK);
+
+            if ( ii < 16384 )
+            {
+                assertEquals( Tag.getTagId( octets ), ii ) ;
+            }
+            else
+            {
+                assertTrue( Tag.getTagId( octets ) != ii ) ;
+            }
+        }
+
+        octets = new byte[4] ;
+        octets[0] = 31 ;
+        octets[1] = 0 ; // shift 0
+        octets[2] = 0 ; // shift 7
+        octets[3] = 0 ; // shift 14
+
+        for ( int ii = 16384 ; ii < 2100000 ; ii++ )
+        {
+            octets[1] = (byte)((ii >> 14) & Tag.LONG_MASK);
+            octets[2] = (byte)((ii >> 7) & Tag.LONG_MASK);
+            octets[3] = (byte)(ii & Tag.LONG_MASK);
+
+            if ( ii < 2097152 )
+            {
+                assertEquals( Tag.getTagId( octets ), ii ) ;
+            }
+            else
+            {
+                assertTrue( Tag.getTagId( octets ) != ii ) ;
+            }
+        }
+
+        try
+        {
+            Tag.getTagId( new byte[5] ) ;
+            fail( "should fail before getting here" ) ;
+        }
+        catch ( Throwable t )
+        {
+            assertNotNull( t ) ;
+        }
+
+
+        try
+        {
+            Tag.getTagId( new byte[12] ) ;
+            fail( "should fail before getting here" ) ;
+        }
+        catch ( Throwable t )
+        {
+            assertNotNull( t ) ;
+        }
+    }
+
+
+    public void testIsRawTagConstructed()
+    {
+        int rawTag = Tag.getIntEncodedTag( TypeClass.APPLICATION, 1234, true );
+        assertTrue( Tag.isRawTagConstructed( rawTag ) );
+
+        rawTag = Tag.getIntEncodedTag( TypeClass.APPLICATION, 1234, false );
+        assertFalse( Tag.isRawTagConstructed( rawTag ) );
+
+        rawTag = Tag.getIntEncodedTag( TypeClass.CONTEXT_SPECIFIC, 128, true );
+        assertTrue( Tag.isRawTagConstructed( rawTag ) );
+
+        rawTag = Tag.getIntEncodedTag( TypeClass.PRIVATE, 234, false );
+        assertFalse( Tag.isRawTagConstructed( rawTag ) );
+    }
+
+
+    public void testTagLimits() throws Exception
+    {
+        byte[] bites = { (byte) 0xff, (byte) 0xff, (byte) 0x8f, (byte) 0x0f } ;
+
+        Tag tag = new Tag() ;
+        tag.add( bites[0] ) ;
+        tag.add( bites[1] ) ;
+        tag.add( bites[2] ) ;
+        tag.add( bites[3] ) ;
+
+        byte[] octets = tag.getOctets() ;
+        assertTrue( ArrayUtils.isEquals( bites, octets ) ) ;
+
+        byte[] tooMany = { (byte) 0xff, (byte) 0xff, (byte) 0x8f, (byte) 0x8f, (byte) 0x0f } ;
+
+        tag = new Tag() ;
+        tag.add( tooMany[0] ) ;
+        tag.add( tooMany[1] ) ;
+        tag.add( tooMany[2] ) ;
+        tag.add( tooMany[3] ) ;
+
+        try
+        {
+            tag.add( tooMany[4] ) ;
+            fail( "should never get here due to exception" ) ;
+        }
+        catch( BufferOverflowException e )
+        {
+        }
+    }
+
+
+    public void testGetOctets() throws Exception
+    {
+        byte[] bites = { (byte) 0xff, (byte) 0xff, (byte) 0x0f } ;
+        
+        Tag tag = new Tag() ;
+        tag.add( bites[0] ) ;
+        tag.add( bites[1] ) ;
+        tag.add( bites[2] ) ;
+        
+        byte[] octets = tag.getOctets() ;
+        assertTrue( ArrayUtils.isEquals( bites, octets ) ) ;
+    }
+    
+    
+    public void testGetOctets2() throws Exception
+    {
+        byte[] bites = { (byte) 0x00, (byte) 0xff } ;
+        
+        Tag tag = new Tag() ;
+        tag.add( bites[0] ) ;
+        
+        try
+        {
+            tag.add( bites[1] ) ;
+            fail( "should never get here due to illegal state" ) ;
+        }
+        catch ( Throwable t ) 
+        {
+            assertNotNull( t ) ;
+        }
+    }
+
+
+    public void testGetIntEncodedTag()
+    {
+        assertEquals( UniversalTag.INTEGER.getPrimitiveTag(),
+                Tag.getIntEncodedTag( TypeClass.UNIVERSAL,
+                        UniversalTag.INTEGER.getTagId(), false ) ) ;
+
+        assertEquals( 0x01000000,
+                Tag.getIntEncodedTag( TypeClass.UNIVERSAL, 1, false ) ) ;
+
+        assertEquals( 0x0F000000,
+                Tag.getIntEncodedTag( TypeClass.UNIVERSAL, 15, false ) ) ;
+
+        assertEquals( 0x1E000000,
+                Tag.getIntEncodedTag( TypeClass.UNIVERSAL, 30, false ) ) ;
+
+        assertEquals( 0x1F1F0000,
+                Tag.getIntEncodedTag( TypeClass.UNIVERSAL, 31, false ) ) ;
+
+        assertEquals( 0x1F7E0000,
+                Tag.getIntEncodedTag( TypeClass.UNIVERSAL, 126, false ) ) ;
+
+        assertEquals( 0x1F7F0000,
+                Tag.getIntEncodedTag( TypeClass.UNIVERSAL, 127, false ) ) ;
+
+        assertEquals( 0x1F810000,
+                Tag.getIntEncodedTag( TypeClass.UNIVERSAL, 128, false ) ) ;
+
+        assertEquals( 0x1F810100,
+                Tag.getIntEncodedTag( TypeClass.UNIVERSAL, 129, false ) ) ;
+
+        assertEquals( 0x3FFF7E00,
+                Tag.getIntEncodedTag( TypeClass.UNIVERSAL, (1<<14)-2, true ) ) ;
+
+        assertEquals( 0x1FFF7F00,
+                Tag.getIntEncodedTag( TypeClass.UNIVERSAL, (1<<14)-1, false ) ) ;
+
+        assertEquals( 0xDF818000,
+                Tag.getIntEncodedTag( TypeClass.PRIVATE, (1<<14), false ) ) ;
+
+        assertEquals( 0x5F818001,
+                Tag.getIntEncodedTag( TypeClass.APPLICATION, (1<<14)+1, false ) ) ;
+
+        assertEquals( 0x9FFFFF7E,
+                Tag.getIntEncodedTag( TypeClass.CONTEXT_SPECIFIC,
+                        (1<<21)-2, false ) ) ;
+
+        assertEquals( 0x1FFFFF7F,
+                Tag.getIntEncodedTag( TypeClass.UNIVERSAL, (1<<21)-1, false ) ) ;
+
+        try
+        {
+            Tag.getIntEncodedTag( TypeClass.UNIVERSAL, (1<<21), false ) ;
+            fail( "should never get here due to an exception" ) ;
+        }
+        catch( IllegalArgumentException e )
+        {
+        }
+    }
+}



Mime
View raw message