You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by el...@apache.org on 2005/08/21 17:03:59 UTC
svn commit: r234226 [3/4] - in
/directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber: ./
digester/ digester/rules/ primitives/
Added: directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/TupleTest.java
URL: http://svn.apache.org/viewcvs/directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/TupleTest.java?rev=234226&view=auto
==============================================================================
--- directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/TupleTest.java (added)
+++ directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/TupleTest.java Sun Aug 21 08:03:46 2005
@@ -0,0 +1,1134 @@
+/*
+ * Copyright 2004-2005 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 java.util.ArrayList;
+import java.util.Collections;
+
+import org.apache.commons.lang.ArrayUtils ;
+import org.apache.asn1.codec.binary.BinaryCodec;
+import org.apache.asn1.ber.Length;
+import org.apache.asn1.ber.Tuple;
+
+import junit.framework.TestCase ;
+
+
+/**
+ * Tests Tuple class.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">
+ * Apache Directory Project</a>
+ * @version $Rev: 157644 $
+ */
+public class TupleTest extends TestCase
+{
+ /** 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 ;
+
+ /** for convenience in handling nulls */
+ private static final ByteBuffer EMPTY_BUFFER =
+ ByteBuffer.wrap( ArrayUtils.EMPTY_BYTE_ARRAY ) ;
+
+ public static void main(String[] args)
+ {
+ junit.textui.TestRunner.run(TupleTest.class);
+ }
+
+ /*
+ * @see TestCase#setUp()
+ */
+ protected void setUp() throws Exception
+ {
+ super.setUp();
+ }
+
+ /*
+ * @see TestCase#tearDown()
+ */
+ protected void tearDown() throws Exception
+ {
+ super.tearDown();
+ }
+
+ /**
+ * Constructor for TupleTest.
+ * @param arg0
+ */
+ public TupleTest(String arg0)
+ {
+ super(arg0);
+ }
+
+ /*
+ * Class to test for void Tuple()
+ */
+ public void testTuple()
+ {
+ assertNotNull( new Tuple() ) ;
+ }
+
+ /*
+ * Class to test for void Tuple(int)
+ */
+ public void testTupleint()
+ {
+ Tuple t0 = new Tuple( 0, 0 ) ;
+ assertEquals( 0, t0.id ) ;
+ Tuple t1 = new Tuple( 1, 0 ) ;
+ assertEquals( 1, t1.id ) ;
+ assertFalse( t0.equals(t1) ) ;
+ }
+
+ /*
+ * Class to test for void Tuple(int, int)
+ */
+ public void testTupleintint()
+ {
+ Tuple t0 = new Tuple( 0, 0 ) ;
+ assertEquals( 0, t0.id ) ;
+ assertEquals( 0, t0.length ) ;
+ Tuple t1 = new Tuple( 0, 1 ) ;
+ assertEquals( 0, t1.id ) ;
+ assertEquals( 1, t1.length ) ;
+ assertFalse( t0.equals(t1) ) ;
+ }
+
+ /*
+ * Class to test for void Tuple(int, int, TypeClass)
+ */
+ public void testTupleintintTypeClass()
+ {
+ Tuple t0 = new Tuple( 0, 0, TypeClass.PRIVATE ) ;
+ assertEquals( 0, t0.id ) ;
+ assertEquals( 0, t0.length ) ;
+ assertEquals( TypeClass.PRIVATE, t0.getTypeClass() ) ;
+ Tuple t1 = new Tuple( 0, 1, null ) ;
+ assertEquals( 0, t1.id ) ;
+ assertEquals( 1, t1.length ) ;
+ assertFalse( t0.equals(t1) ) ;
+ assertEquals( TypeClass.APPLICATION, t1.getTypeClass() ) ;
+ }
+
+ /*
+ * Class to test for void Tuple(int, int, TypeClass, boolean, byte[])
+ */
+ public void testTupleintTypeClass()
+ {
+ Tuple t = new Tuple( 2, TypeClass.PRIVATE ) ;
+ assertEquals( 2, t.getId() ) ;
+ assertEquals( TypeClass.PRIVATE, t.getTypeClass() ) ;
+ assertEquals( false, t.isPrimitive() ) ;
+ assertEquals( Length.INDEFINITE, t.getLength() ) ;
+ assertEquals( EMPTY_BUFFER, t.getLastValueChunk() ) ;
+
+ t = new Tuple( 2, (TypeClass) null ) ;
+ assertEquals( 2, t.getId() ) ;
+ assertEquals( TypeClass.APPLICATION, t.getTypeClass() ) ;
+ assertEquals( false, t.isPrimitive() ) ;
+ assertEquals( Length.INDEFINITE, t.getLength() ) ;
+ assertEquals( EMPTY_BUFFER, t.getLastValueChunk() ) ;
+ }
+
+ public void testGetId()
+ {
+ Tuple t = new Tuple() ;
+ assertEquals( 0, t.getId() ) ;
+ t = new Tuple( 2, 0 ) ;
+ assertEquals( 2, t.getId() ) ;
+ t.id = 21 ;
+ assertEquals( 21, t.getId() ) ;
+ }
+
+
+ public void testSize()
+ {
+ Tuple t = new Tuple( 1, TypeClass.APPLICATION ) ;
+ assertEquals( 2, t.size() ) ;
+ t.id = 32 ;
+ assertEquals( 3, t.size() ) ;
+ t.id = 127 ;
+ assertEquals( 4, t.size() ) ;
+ t.id = 128 ;
+ assertEquals( 4, t.size() ) ;
+ t.id = 1 << 14 ;
+ assertEquals( 5, t.size() ) ;
+ t.id = 1 << 21 ;
+ assertEquals( 6, t.size() ) ;
+
+ t.length = 127 ;
+ assertEquals( 6+127, t.size() ) ;
+ t.length = 128 ;
+ assertEquals( 7+128, t.size() ) ;
+ t.length = 255 ;
+ assertEquals( 7+255, t.size() ) ;
+ t.length = 256 ;
+ assertEquals( 8+256, t.size() ) ;
+ }
+
+
+ public void testIsIndefinite()
+ {
+ Tuple t = new Tuple() ;
+ assertFalse( t.isIndefinite() ) ;
+ t.length = Length.INDEFINITE ;
+ assertTrue( t.isIndefinite() ) ;
+ }
+
+
+ public void testIsIndefiniteTerminator()
+ {
+ Tuple t = new Tuple() ;
+ assertFalse( t.isIndefiniteTerminator() ) ;
+ t.id = 0 ;
+ t.length = 0 ;
+ t.isPrimitive = true ;
+ t.typeClass = TypeClass.UNIVERSAL ;
+ assertTrue( t.isIndefiniteTerminator() ) ;
+ }
+
+
+ public void testIsPrimitive()
+ {
+ Tuple t = new Tuple() ;
+ assertTrue( t.isPrimitive() ) ;
+ t.isPrimitive = false ;
+ assertFalse( t.isPrimitive() ) ;
+ }
+
+ public void testGetLength()
+ {
+ Tuple t = new Tuple() ;
+ assertEquals( 0, t.getLength() ) ;
+ t = new Tuple( 1, 2 ) ;
+ assertEquals( 2, t.getLength() ) ;
+ t.length = 21 ;
+ assertEquals( 21, t.getLength() ) ;
+ }
+
+ public void testGetTypeClass()
+ {
+ Tuple t = new Tuple() ;
+ assertEquals( t.typeClass, TypeClass.APPLICATION ) ;
+ t = new Tuple( 0, 0 ) ;
+ assertEquals( TypeClass.APPLICATION, t.getTypeClass() ) ;
+ t.typeClass = TypeClass.PRIVATE ;
+ assertEquals( TypeClass.PRIVATE, t.getTypeClass() ) ;
+ }
+
+ public void testGetValue()
+ {
+ Tuple t = new Tuple() ;
+ assertEquals( EMPTY_BUFFER, t.getLastValueChunk() ) ;
+ byte[] bites = {1, 2, 3, 45} ;
+ t.valueChunk = ByteBuffer.wrap( bites ) ;
+ assertEquals( ByteBuffer.wrap( bites ), t.getLastValueChunk() ) ;
+ t.clear() ;
+ assertEquals( EMPTY_BUFFER, t.getLastValueChunk() ) ;
+ }
+
+ public void testClear()
+ {
+ Tuple t = new Tuple() ;
+ t.id = 12 ;
+ assertEquals( 12, t.id ) ;
+ t.clear() ;
+ assertEquals( 0, t.id ) ;
+
+ t.length = 12 ;
+ assertEquals( 12, t.length ) ;
+ t.clear() ;
+ assertEquals( Length.UNDEFINED, t.length ) ;
+
+ t.index = 12 ;
+ assertEquals( 12, t.index ) ;
+ t.clear() ;
+ assertEquals( 0, t.index ) ;
+
+ t.isPrimitive = false ;
+ assertEquals( false, t.isPrimitive ) ;
+ t.clear() ;
+ assertEquals( true, t.isPrimitive ) ;
+
+ t.typeClass = TypeClass.CONTEXT_SPECIFIC ;
+ assertEquals( TypeClass.CONTEXT_SPECIFIC, t.typeClass ) ;
+ t.clear() ;
+ assertEquals( TypeClass.APPLICATION, t.typeClass ) ;
+
+ t.valueChunk = ByteBuffer.allocate( 3 ) ;
+ assertNotNull( t.valueChunk ) ;
+ t.clear() ;
+ assertEquals( EMPTY_BUFFER, t.valueChunk ) ;
+
+ t.valueIndex = 12 ;
+ assertEquals( 12, t.valueIndex ) ;
+ t.clear() ;
+ assertEquals( Length.UNDEFINED, t.valueIndex ) ;
+
+ }
+
+ /*
+ * Class to test for boolean equals(Object)
+ */
+ public void testEqualsObject()
+ {
+ Tuple tnull0 = new Tuple() ;
+ tnull0.valueChunk = null ;
+ Tuple tnull1 = new Tuple() ;
+ tnull1.valueChunk = null ;
+ tnull0.equals( tnull1 ) ;
+
+ tnull1.equals( tnull1 ) ;
+ tnull0.equals( tnull0 ) ;
+
+ Tuple t0 = new Tuple() ;
+ Tuple t1 = ( Tuple ) t0.clone() ;
+
+ assertTrue( t0.equals( t1 ) ) ;
+ t0.id = 23 ;
+ assertFalse( t0.equals( t1 ) ) ;
+ t1 = ( Tuple ) t0.clone() ;
+ assertTrue( t0.equals( t1 ) ) ;
+
+ // indices are not taken into account in Tuple.equals(Object)
+ t0.index = 23 ;
+ t1.index = 33 ;
+ assertTrue( t0.equals( t1 ) ) ;
+ t1 = ( Tuple ) t0.clone() ;
+ assertTrue( t0.equals( t1 ) ) ;
+
+ t0.isPrimitive = false ;
+ t1.isPrimitive = true ;
+ assertFalse( t0.equals( t1 ) ) ;
+ t1 = ( Tuple ) t0.clone() ;
+ assertTrue( t0.equals( t1 ) ) ;
+
+ t0.length = 23 ;
+ assertFalse( t0.equals( t1 ) ) ;
+ t1 = ( Tuple ) t0.clone() ;
+ assertTrue( t0.equals( t1 ) ) ;
+
+ t0.typeClass = TypeClass.PRIVATE ;
+ t1.typeClass = TypeClass.UNIVERSAL ;
+ assertFalse( t0.equals( t1 ) ) ;
+ t1 = ( Tuple ) t0.clone() ;
+ assertTrue( t0.equals( t1 ) ) ;
+
+ // indices are not taken into account in Tuple.equals(Object)
+ t0.valueIndex = 23 ;
+ t1.valueIndex = 3 ;
+ assertTrue( t0.equals( t1 ) ) ;
+ t1 = ( Tuple ) t0.clone() ;
+ assertTrue( t0.equals( t1 ) ) ;
+
+ t0.valueChunk = ByteBuffer.allocate( 4 ) ;
+ t1.valueChunk = null ;
+
+ // The buffer does not factor into equality
+ assertTrue( t0.equals( t1 ) ) ;
+
+ t1 = ( Tuple ) t0.clone() ;
+ assertTrue( t0.equals( t1 ) ) ;
+
+ assertFalse( t0.equals( new Object() )) ;
+ }
+
+ /*
+ * Class to test for Object clone()
+ */
+ public void testClone()
+ {
+ Tuple t = new Tuple() ;
+ assertTrue( t.equals( t.clone() ) ) ;
+ }
+
+ public void testToEncodedBufferConstructed()
+ {
+ Tuple t = null ;
+ ByteBuffer encoded ;
+
+ t = new Tuple( 0, 0 ) ;
+ encoded = t.toEncodedBuffer( Collections.EMPTY_LIST ) ;
+ assertEquals(
+ "00000000" +
+ "01100000"
+ , toAsciiString( encoded ) ) ;
+
+ t = new Tuple( 2, 0 ) ;
+ encoded = t.toEncodedBuffer( Collections.EMPTY_LIST ) ;
+ assertEquals(
+ "00000000" +
+ "01100010"
+ , toAsciiString( encoded ) ) ;
+
+ t = new Tuple( 30, 0 ) ;
+ encoded = t.toEncodedBuffer( Collections.EMPTY_LIST ) ;
+ assertEquals(
+ "00000000" +
+ "01111110"
+ , toAsciiString( encoded ) ) ;
+
+ t = new Tuple( 31, 0 ) ;
+ encoded = t.toEncodedBuffer( Collections.EMPTY_LIST ) ;
+ assertEquals(
+ "00000000" +
+ "00011111" +
+ "01111111"
+ , toAsciiString( encoded ) ) ;
+
+ t = new Tuple( 128, 0 ) ;
+ encoded = t.toEncodedBuffer( Collections.EMPTY_LIST ) ;
+ assertEquals(
+ "00000000" +
+ "00000000" +
+ "10000001" +
+ "01111111"
+ , toAsciiString( encoded ) ) ;
+
+ t = new Tuple( 128, 127 ) ;
+ ArrayList list = new ArrayList() ;
+ list.add( ByteBuffer.allocate( 127 ) ) ;
+ encoded = t.toEncodedBuffer( list ) ;
+ assertEquals(
+ "01111111" +
+ "00000000" +
+ "10000001" +
+ "01111111"
+ , toAsciiString( encoded ) ) ;
+
+ t = new Tuple( 128, 128 ) ;
+ list.clear() ;
+ list.add( ByteBuffer.allocate( 128 ) ) ;
+ encoded = t.toEncodedBuffer( list ) ;
+ assertEquals(
+ "10000000" +
+ "10000001" +
+ "00000000" +
+ "10000001" +
+ "01111111"
+ , toAsciiString( encoded ) ) ;
+
+ t = new Tuple( 128, 255 ) ;
+ list.clear() ;
+ list.add( ByteBuffer.allocate( 255 ) ) ;
+ encoded = t.toEncodedBuffer( list ) ;
+ assertEquals(
+ "11111111" +
+ "10000001" +
+ "00000000" +
+ "10000001" +
+ "01111111"
+ , toAsciiString( encoded ) ) ;
+
+ t = new Tuple( 128, 256 ) ;
+ list.clear() ;
+ list.add( ByteBuffer.allocate( 256 ) ) ;
+ encoded = t.toEncodedBuffer( list ) ;
+ assertEquals(
+ "00000000" +
+ "00000001" +
+ "10000010" +
+ "00000000" +
+ "10000001" +
+ "01111111"
+ , toAsciiString( encoded ) ) ;
+ }
+
+ public void testToEncodedBufferPrimitive()
+ {
+ Tuple t = null ;
+ ByteBuffer encoded ;
+ byte[] data ;
+
+ t = new Tuple( 0, 0, true, TypeClass.APPLICATION ) ;
+ encoded = t.toEncodedBuffer( Collections.EMPTY_LIST ) ;
+ assertEquals(
+ "00000000" +
+ "01000000"
+ , toAsciiString( encoded ) ) ;
+
+ t = new Tuple( 2, 0, true, TypeClass.APPLICATION ) ;
+ encoded = t.toEncodedBuffer( Collections.EMPTY_LIST ) ;
+ assertEquals(
+ "00000000" +
+ "01000010"
+ , toAsciiString( encoded ) ) ;
+
+ t = new Tuple( 30, 0, true, TypeClass.APPLICATION ) ;
+ encoded = t.toEncodedBuffer( Collections.EMPTY_LIST ) ;
+ assertEquals(
+ "00000000" +
+ "01011110"
+ , toAsciiString( encoded ) ) ;
+
+ t = new Tuple( 31, 0, true, TypeClass.APPLICATION ) ;
+ encoded = t.toEncodedBuffer( Collections.EMPTY_LIST ) ;
+ assertEquals(
+ "00000000" +
+ "00011111" +
+ "01011111"
+ , toAsciiString( encoded ) ) ;
+
+ t = new Tuple( 128, 0, true, TypeClass.APPLICATION ) ;
+ encoded = t.toEncodedBuffer( Collections.EMPTY_LIST ) ;
+ assertEquals(
+ "00000000" +
+ "00000000" +
+ "10000001" +
+ "01011111"
+ , toAsciiString( encoded ) ) ;
+
+ data = new byte[1] ;
+ t = new Tuple( 128, 1, true, TypeClass.APPLICATION ) ;
+ ArrayList list = new ArrayList() ;
+ list.add( ByteBuffer.wrap( data ) ) ;
+ encoded = t.toEncodedBuffer( list ) ;
+ assertEquals(
+ "00000000" +
+ "00000001" +
+ "00000000" +
+ "10000001" +
+ "01011111"
+ , toAsciiString( encoded ) ) ;
+
+ data = new byte[127] ;
+ t = new Tuple( 128, 127, true, TypeClass.APPLICATION ) ;
+ list.clear() ;
+ list.add( ByteBuffer.wrap( data ) ) ;
+ encoded = t.toEncodedBuffer( list ) ;
+ assertEquals( BinaryCodec.toAsciiString( data ) +
+ "01111111" +
+ "00000000" +
+ "10000001" +
+ "01011111"
+ , toAsciiString( encoded ) ) ;
+
+ data = new byte[128] ;
+ t = new Tuple( 128, 128, true, TypeClass.APPLICATION ) ;
+ list.clear() ;
+ list.add( ByteBuffer.wrap( data ) ) ;
+ encoded = t.toEncodedBuffer( list ) ;
+ assertEquals( BinaryCodec.toAsciiString( data ) +
+ "10000000" +
+ "10000001" +
+ "00000000" +
+ "10000001" +
+ "01011111"
+ , toAsciiString( encoded ) ) ;
+
+ data = new byte[255] ;
+ t = new Tuple( 128, 255, true, TypeClass.APPLICATION ) ;
+ list.clear() ;
+ list.add( ByteBuffer.wrap( data ) ) ;
+ encoded = t.toEncodedBuffer( list ) ;
+ assertEquals( BinaryCodec.toAsciiString( data ) +
+ "11111111" +
+ "10000001" +
+ "00000000" +
+ "10000001" +
+ "01011111"
+ , toAsciiString( encoded ) ) ;
+
+ data = new byte[256] ;
+ t = new Tuple( 128, 256, true, TypeClass.APPLICATION ) ;
+ list.clear() ;
+ list.add( ByteBuffer.wrap( data ) ) ;
+ encoded = t.toEncodedBuffer( list ) ;
+ assertEquals( BinaryCodec.toAsciiString( data ) +
+ "00000000" +
+ "00000001" +
+ "10000010" +
+ "00000000" +
+ "10000001" +
+ "01011111"
+ , toAsciiString( encoded ) ) ;
+ }
+
+
+ public String toAsciiString( ByteBuffer buf )
+ {
+ return BinaryCodec.toAsciiString( buf.array() ) ;
+ }
+
+
+ public void testSetTagBufferint()
+ {
+ ByteBuffer bites = ByteBuffer.allocate( 1 ) ;
+ Tuple t = new Tuple( 0, 0 ) ;
+ t.setTag( bites, 1 ) ;
+ String binary = toAsciiString( bites ) ;
+ assertEquals( "01100000", binary ) ;
+
+ bites = ByteBuffer.allocate( 1 ) ;
+ t = new Tuple( 30, 0 ) ;
+ t.setTag( bites, 1 ) ;
+ binary = toAsciiString( bites ) ;
+ assertEquals( "01111110", binary ) ;
+
+ bites = ByteBuffer.allocate( 1 ) ;
+ t = new Tuple( 30, 0 ) ;
+ t.isPrimitive = true ;
+ t.setTag( bites, 1 ) ;
+ binary = toAsciiString( bites ) ;
+ assertEquals( "01011110", binary ) ;
+
+ bites = ByteBuffer.allocate( 2 ) ;
+ t = new Tuple( 31, 0 ) ;
+ t.setTag( bites, 2 ) ;
+ binary = toAsciiString( bites ) ;
+ assertEquals( "00011111" + "01111111", binary ) ;
+
+ bites = ByteBuffer.allocate( 2 ) ;
+ t = new Tuple( 127, 0 ) ;
+ t.setTag( bites, 2 ) ;
+ binary = toAsciiString( bites ) ;
+ assertEquals( "01111111" + "01111111", binary ) ;
+
+ bites = ByteBuffer.allocate( 3 ) ;
+ t = new Tuple( 128, 0 ) ;
+ t.setTag( bites, 3 ) ;
+ binary = toAsciiString( bites ) ;
+ assertEquals( "00000000" + "10000001" + "01111111", binary ) ;
+
+ bites = ByteBuffer.allocate( 3 ) ;
+ t = new Tuple( BIT_13 - 1, 0 ) ;
+ t.setTag( bites, 3 ) ;
+ binary = toAsciiString( bites ) ;
+ assertEquals( "01111111" +
+ "11111111" +
+ "01111111", binary ) ;
+
+ bites = ByteBuffer.allocate( 4 ) ;
+ t = new Tuple( BIT_13, 0 ) ;
+ t.setTag( bites, 4 ) ;
+ binary = toAsciiString( bites ) ;
+ assertEquals( "00000000" +
+ "10000000" +
+ "10000001" +
+ "01111111", binary ) ;
+
+ bites = ByteBuffer.allocate( 4 ) ;
+ t = new Tuple( BIT_13 + 1, 0 ) ;
+ t.setTag( bites, 4 ) ;
+ binary = toAsciiString( bites ) ;
+ assertEquals( "00000001" +
+ "10000000" +
+ "10000001" +
+ "01111111", binary ) ;
+
+ bites = ByteBuffer.allocate( 4 ) ;
+ t = new Tuple( BIT_20 - 1, 0 ) ;
+ t.setTag( bites, 4 ) ;
+ binary = toAsciiString( bites ) ;
+ assertEquals( "01111111" +
+ "11111111" +
+ "11111111" +
+ "01111111", binary ) ;
+
+ bites = ByteBuffer.allocate( 5 ) ;
+ t = new Tuple( BIT_20, 0 ) ;
+ t.setTag( bites, 5 ) ;
+ binary = toAsciiString( bites ) ;
+ assertEquals( "00000000" +
+ "10000000" +
+ "10000000" +
+ "10000001" +
+ "01111111", binary ) ;
+
+ bites = ByteBuffer.allocate( 5 ) ;
+ t = new Tuple( BIT_20 + 1, 0 ) ;
+ t.setTag( bites, 5 ) ;
+ binary = toAsciiString( bites ) ;
+ assertEquals( "00000001" +
+ "10000000" +
+ "10000000" +
+ "10000001" +
+ "01111111", binary ) ;
+
+ bites = ByteBuffer.allocate( 5 ) ;
+ t = new Tuple( BIT_27 - 1, 0 ) ;
+ t.setTag( bites, 5 ) ;
+ binary = toAsciiString( bites ) ;
+ assertEquals( "01111111" +
+ "11111111" +
+ "11111111" +
+ "11111111" +
+ "01111111", binary ) ;
+
+ bites = ByteBuffer.allocate( 6 ) ;
+ t = new Tuple( BIT_27, 0 ) ;
+
+ try
+ {
+ t.setTag( bites, 6 ) ;
+ fail( "should never reach this point due to thrown exception" ) ;
+ }
+ catch( IllegalArgumentException e )
+ {
+ assertNotNull( e ) ;
+ }
+ }
+
+ public void testSetTagbyteArrayint()
+ {
+ ByteBuffer bites = ByteBuffer.allocate( 1 ) ;
+ Tuple t = new Tuple( 0, 0 ) ;
+ t.setTag( bites, 1 ) ;
+ String binary = toAsciiString( bites ) ;
+ assertEquals( "01100000", binary ) ;
+
+ bites = ByteBuffer.allocate( 1 ) ;
+ t = new Tuple( 30, 0 ) ;
+ t.setTag( bites, 1 ) ;
+ binary = toAsciiString( bites ) ;
+ assertEquals( "01111110", binary ) ;
+
+ bites = ByteBuffer.allocate( 1 ) ;
+ t = new Tuple( 30, 0 ) ;
+ t.isPrimitive = true ;
+ t.setTag( bites, 1 ) ;
+ binary = toAsciiString( bites ) ;
+ assertEquals( "01011110", binary ) ;
+
+ bites = ByteBuffer.allocate( 2 ) ;
+ t = new Tuple( 31, 0 ) ;
+ t.setTag( bites, 2 ) ;
+ binary = toAsciiString( bites ) ;
+ assertEquals( "00011111" + "01111111", binary ) ;
+
+ bites = ByteBuffer.allocate( 2 ) ;
+ t = new Tuple( 127, 0 ) ;
+ t.setTag( bites, 2 ) ;
+ binary = toAsciiString( bites ) ;
+ assertEquals( "01111111" + "01111111", binary ) ;
+
+ bites = ByteBuffer.allocate( 3 ) ;
+ t = new Tuple( 128, 0 ) ;
+ t.setTag( bites, 3 ) ;
+ binary = toAsciiString( bites ) ;
+ assertEquals( "00000000" + "10000001" + "01111111", binary ) ;
+
+ bites = ByteBuffer.allocate( 3 ) ;
+ t = new Tuple( BIT_13 - 1, 0 ) ;
+ t.setTag( bites, 3 ) ;
+ binary = toAsciiString( bites ) ;
+ assertEquals( "01111111" +
+ "11111111" +
+ "01111111", binary ) ;
+
+ bites = ByteBuffer.allocate( 4 ) ;
+ t = new Tuple( BIT_13, 0 ) ;
+ t.setTag( bites, 4 ) ;
+ binary = toAsciiString( bites ) ;
+ assertEquals( "00000000" +
+ "10000000" +
+ "10000001" +
+ "01111111", binary ) ;
+
+ bites = ByteBuffer.allocate( 4 ) ;
+ t = new Tuple( BIT_13 + 1, 0 ) ;
+ t.setTag( bites, 4 ) ;
+ binary = toAsciiString( bites ) ;
+ assertEquals( "00000001" +
+ "10000000" +
+ "10000001" +
+ "01111111", binary ) ;
+
+ bites = ByteBuffer.allocate( 4 ) ;
+ t = new Tuple( BIT_20 - 1, 0 ) ;
+ t.setTag( bites, 4 ) ;
+ binary = toAsciiString( bites ) ;
+ assertEquals( "01111111" +
+ "11111111" +
+ "11111111" +
+ "01111111", binary ) ;
+
+ bites = ByteBuffer.allocate( 5 ) ;
+ t = new Tuple( BIT_20, 0 ) ;
+ t.setTag( bites, 5 ) ;
+ binary = toAsciiString( bites ) ;
+ assertEquals( "00000000" +
+ "10000000" +
+ "10000000" +
+ "10000001" +
+ "01111111", binary ) ;
+
+ bites = ByteBuffer.allocate( 5 ) ;
+ t = new Tuple( BIT_20 + 1, 0 ) ;
+ t.setTag( bites, 5 ) ;
+ binary = toAsciiString( bites ) ;
+ assertEquals( "00000001" +
+ "10000000" +
+ "10000000" +
+ "10000001" +
+ "01111111", binary ) ;
+
+ bites = ByteBuffer.allocate( 5 ) ;
+ t = new Tuple( BIT_27 - 1, 0 ) ;
+ t.setTag( bites, 5 ) ;
+ binary = toAsciiString( bites ) ;
+ assertEquals( "01111111" +
+ "11111111" +
+ "11111111" +
+ "11111111" +
+ "01111111", binary ) ;
+
+ bites = ByteBuffer.allocate( 6 ) ;
+ t = new Tuple( BIT_27, 0 ) ;
+
+ try
+ {
+ t.setTag( bites, 6 ) ;
+ fail( "should never reach this point due to thrown exception" ) ;
+ }
+ catch( IllegalArgumentException e )
+ {
+ assertNotNull( e ) ;
+ }
+ }
+
+
+ String toAsciiString( int raw )
+ {
+ byte[] intBytes = new byte[4] ;
+ intBytes[0] = (byte) ( (int) 0x000000ff & raw ) ;
+ intBytes[1] = (byte) ( (int) ( 0x0000ff00 & raw ) >> 8 ) ;
+ intBytes[2] = (byte) ( (int) ( 0x00ff0000 & raw ) >> 16 ) ;
+ intBytes[3] = (byte) ( (int) ( 0xff000000 & raw ) >> 24 ) ;
+
+ return BinaryCodec.toAsciiString( intBytes ) ;
+ }
+
+
+ public void testSetLengthBuffer()
+ {
+ ByteBuffer bites = ByteBuffer.allocate( 1 ) ;
+ Tuple t = new Tuple( 0, 0 ) ;
+ t.setLength( bites, 1 ) ;
+ String binary = toAsciiString( bites ) ;
+ assertEquals( "00000000", binary ) ;
+
+ bites = ByteBuffer.allocate( 1 ) ;
+ t = new Tuple( 30, 15 ) ;
+ t.setLength( bites, 1 ) ;
+ binary = toAsciiString( bites ) ;
+ assertEquals( "00001111", binary ) ;
+
+ bites = ByteBuffer.allocate( 1 ) ;
+ t = new Tuple( 30, 127 ) ;
+ t.setLength( bites, 1 ) ;
+ binary = toAsciiString( bites ) ;
+ assertEquals( "01111111", binary ) ;
+
+ bites = ByteBuffer.allocate( 2 ) ;
+ t = new Tuple( 30, 128 ) ;
+ t.setLength( bites, 2 ) ;
+ binary = toAsciiString( bites ) ;
+ assertEquals( "10000000" + "10000001", binary ) ;
+
+ bites = ByteBuffer.allocate( 2 ) ;
+ t = new Tuple( 30, 255 ) ;
+ t.setLength( bites, 2 ) ;
+ binary = toAsciiString( bites ) ;
+ assertEquals( "11111111" + "10000001", binary ) ;
+
+ bites = ByteBuffer.allocate( 3 ) ;
+ t = new Tuple( 30, 256 ) ;
+ t.setLength( bites, 3 ) ;
+ binary = toAsciiString( bites ) ;
+ assertEquals( "00000000" + "00000001" + "10000010", binary ) ;
+
+ bites = ByteBuffer.allocate( 3 ) ;
+ t = new Tuple( 30, BIT_15 - 1 ) ;
+ t.setLength( bites, 3 ) ;
+ binary = toAsciiString( bites ) ;
+ assertEquals( "11111111" +
+ "11111111" + "10000010", binary ) ;
+
+ bites = ByteBuffer.allocate( 4 ) ;
+ t = new Tuple( 30, BIT_15 ) ;
+ t.setLength( bites, 4 ) ;
+ binary = toAsciiString( bites ) ;
+ assertEquals( "00000000" + "00000000" + "00000001" + "10000011", binary ) ;
+
+ bites = ByteBuffer.allocate( 4 ) ;
+ t = new Tuple( 30, BIT_15 + 1 ) ;
+ t.setLength( bites, 4 ) ;
+ binary = toAsciiString( bites ) ;
+ assertEquals( "00000001" +
+ "00000000" +
+ "00000001" + "10000011", binary ) ;
+
+ bites = ByteBuffer.allocate( 4 ) ;
+ t = new Tuple( 30, BIT_23 - 1 ) ;
+ t.setLength( bites, 4 ) ;
+ binary = toAsciiString( bites ) ;
+ assertEquals( "11111111" +
+ "11111111" +
+ "11111111" + "10000011", binary ) ;
+
+ bites = ByteBuffer.allocate( 5 ) ;
+ t = new Tuple( 30, BIT_23 ) ;
+ t.setLength( bites, 5 ) ;
+ binary = toAsciiString( bites ) ;
+ assertEquals( "00000000" +
+ "00000000" +
+ "00000000" +
+ "00000001" +
+ "10000100", binary ) ;
+
+ bites = ByteBuffer.allocate( 5 ) ;
+ t = new Tuple( 30, BIT_23 + 1 ) ;
+ t.setLength( bites, 5 ) ;
+ binary = toAsciiString( bites ) ;
+ assertEquals( "00000001" +
+ "00000000" +
+ "00000000" +
+ "00000001" + "10000100", binary ) ;
+
+ bites = ByteBuffer.allocate( 5 ) ;
+ t = new Tuple( 30, Integer.MAX_VALUE ) ;
+ t.setLength( bites, 5 ) ;
+ binary = toAsciiString( bites ) ;
+ assertEquals( "11111111" +
+ "11111111" +
+ "11111111" +
+ "01111111" + "10000100", binary ) ;
+
+
+ bites = ByteBuffer.allocate( 6 ) ;
+ t = new Tuple( 30, Integer.MAX_VALUE + 1 ) ;
+
+ try
+ {
+ t.setLength( bites, 6 ) ;
+ fail( "should never get here due to thrown exception" ) ;
+ }
+ catch( IllegalArgumentException e )
+ {
+ assertNotNull( e ) ;
+ }
+ }
+
+ public void testSetLengthbyteArrayintint()
+ {
+ ByteBuffer bites = ByteBuffer.allocate( 1 ) ;
+ Tuple t = new Tuple( 0, 0 ) ;
+ t.setLength( bites, 1 ) ;
+ String binary = toAsciiString( bites ) ;
+ assertEquals( "00000000", binary ) ;
+
+ bites = ByteBuffer.allocate( 1 ) ;
+ t = new Tuple( 30, 15 ) ;
+ t.setLength( bites, 1 ) ;
+ binary = toAsciiString( bites ) ;
+ assertEquals( "00001111", binary ) ;
+
+ bites = ByteBuffer.allocate( 1 ) ;
+ t = new Tuple( 30, 127 ) ;
+ t.setLength( bites, 1 ) ;
+ binary = toAsciiString( bites ) ;
+ assertEquals( "01111111", binary ) ;
+
+ bites = ByteBuffer.allocate( 2 ) ;
+ t = new Tuple( 30, 128 ) ;
+ t.setLength( bites, 2 ) ;
+ binary = toAsciiString( bites ) ;
+ assertEquals( "10000000" + "10000001", binary ) ;
+
+ bites = ByteBuffer.allocate( 2 ) ;
+ t = new Tuple( 30, 255 ) ;
+ t.setLength( bites, 2 ) ;
+ binary = toAsciiString( bites ) ;
+ assertEquals( "11111111" + "10000001", binary ) ;
+
+ bites = ByteBuffer.allocate( 3 ) ;
+ t = new Tuple( 30, 256 ) ;
+ t.setLength( bites, 3 ) ;
+ binary = toAsciiString( bites ) ;
+ assertEquals( "00000000" +
+ "00000001" + "10000010", binary ) ;
+
+ bites = ByteBuffer.allocate( 3 ) ;
+ t = new Tuple( 30, BIT_15 - 1 ) ;
+ t.setLength( bites, 3 ) ;
+ binary = toAsciiString( bites ) ;
+ assertEquals( "11111111" + "11111111" + "10000010", binary ) ;
+
+ bites = ByteBuffer.allocate( 4 ) ;
+ t = new Tuple( 30, BIT_15 ) ;
+ t.setLength( bites, 4 ) ;
+ binary = toAsciiString( bites ) ;
+ assertEquals( "00000000" + "00000000" +
+ "00000001" + "10000011", binary ) ;
+
+ bites = ByteBuffer.allocate( 4 ) ;
+ t = new Tuple( 30, BIT_15 + 1 ) ;
+ t.setLength( bites, 4 ) ;
+ binary = toAsciiString( bites ) ;
+ assertEquals( "00000001" + "00000000" +
+ "00000001" + "10000011", binary ) ;
+
+ bites = ByteBuffer.allocate( 4 ) ;
+ t = new Tuple( 30, BIT_23 - 1 ) ;
+ t.setLength( bites, 4 ) ;
+ binary = toAsciiString( bites ) ;
+ assertEquals( "11111111" + "11111111" +
+ "11111111" + "10000011", binary ) ;
+
+ bites = ByteBuffer.allocate( 5 ) ;
+ t = new Tuple( 30, BIT_23 ) ;
+ t.setLength( bites, 5 ) ;
+ binary = toAsciiString( bites ) ;
+ assertEquals( "00000000" +
+ "00000000" +
+ "00000000" +
+ "00000001" + "10000100", binary ) ;
+
+ bites = ByteBuffer.allocate( 5 ) ;
+ t = new Tuple( 30, BIT_23 + 1 ) ;
+ t.setLength( bites, 5 ) ;
+ binary = toAsciiString( bites ) ;
+ assertEquals( "00000001" +
+ "00000000" +
+ "00000000" +
+ "00000001" + "10000100", binary ) ;
+
+ bites = ByteBuffer.allocate( 5 ) ;
+ t = new Tuple( 30, Integer.MAX_VALUE ) ;
+ t.setLength( bites, 5 ) ;
+ binary = toAsciiString( bites ) ;
+ assertEquals( "11111111" +
+ "11111111" +
+ "11111111" +
+ "01111111" + "10000100", binary ) ;
+
+
+ bites = ByteBuffer.allocate( 6 ) ;
+ t = new Tuple( 30, Integer.MAX_VALUE + 1 ) ;
+
+ try
+ {
+ t.setLength( bites, 6 ) ;
+ fail( "should never get here due to thrown exception" ) ;
+ }
+ catch( IllegalArgumentException e )
+ {
+ assertNotNull( e ) ;
+ }
+ }
+
+ public void testGetTagLength()
+ {
+ Tuple t = new Tuple() ;
+ assertEquals( 1, t.getTagLength() ) ;
+ t.id = 30 ;
+ assertEquals( 1, t.getTagLength() ) ;
+ t.id = 31 ;
+ assertEquals( 2, t.getTagLength() ) ;
+ t.id = 100 ;
+ assertEquals( 3, t.getTagLength() ) ;
+ t.id = 127 ;
+ assertEquals( 3, t.getTagLength() ) ;
+ t.id = 128 ;
+ assertEquals( 3, t.getTagLength() ) ;
+ t.id = 129 ;
+ assertEquals( 3, t.getTagLength() ) ;
+
+ t.id = BIT_13 - 1 ;
+ assertEquals( 3, t.getTagLength() ) ;
+ t.id = BIT_13 ;
+ assertEquals( 4, t.getTagLength() ) ;
+ t.id = BIT_13 + 100 ;
+ assertEquals( 4, t.getTagLength() ) ;
+
+ t.id = BIT_20 - 1 ;
+ assertEquals( 4, t.getTagLength() ) ;
+ t.id = BIT_20 ;
+ assertEquals( 5, t.getTagLength() ) ;
+ t.id = BIT_20 + 100 ;
+ assertEquals( 5, t.getTagLength() ) ;
+
+ t.id = BIT_27 - 1 ;
+ assertEquals( 5, t.getTagLength() ) ;
+
+ t.id = BIT_27 ;
+
+ try
+ {
+ assertEquals( 6, t.getTagLength() ) ;
+ fail( "should throw an exception before getting here" ) ;
+ }
+ catch( IllegalArgumentException e )
+ {
+ assertNotNull( e ) ;
+ }
+ }
+
+ public void testGetLengthLength()
+ {
+ Tuple t = new Tuple() ;
+ assertEquals( 1, t.getLengthLength() ) ;
+ t.length = 127 ;
+ assertEquals( 1, t.getLengthLength() ) ;
+ t.length = 128 ;
+ assertEquals( 2, t.getLengthLength() ) ;
+ t.length = 255 ;
+ assertEquals( 2, t.getLengthLength() ) ;
+ t.length = 256 ;
+ assertEquals( 3, t.getLengthLength() ) ;
+
+ t.length = BIT_15 - 1 ;
+ assertEquals( 3, t.getLengthLength() ) ;
+ t.length = BIT_15 ;
+ assertEquals( 4, t.getLengthLength() ) ;
+ t.length = BIT_15 + 100 ;
+ assertEquals( 4, t.getLengthLength() ) ;
+
+ t.length = BIT_23 - 1 ;
+ assertEquals( 4, t.getLengthLength() ) ;
+ t.length = BIT_23 ;
+ assertEquals( 5, t.getLengthLength() ) ;
+ t.length = BIT_23 + 100 ;
+ assertEquals( 5, t.getLengthLength() ) ;
+
+ t.length = Integer.MAX_VALUE ;
+ assertEquals( 5, t.getLengthLength() ) ;
+
+
+ t.length = Integer.MAX_VALUE + 1 ;
+ try
+ {
+ assertEquals( 6, t.getLengthLength() ) ;
+ fail( "should throw an exception before getting here" ) ;
+ }
+ catch( IllegalArgumentException e )
+ {
+ assertNotNull( e ) ;
+ }
+
+ }
+}
Added: directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/TupleTreeDecoderTest.java
URL: http://svn.apache.org/viewcvs/directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/TupleTreeDecoderTest.java?rev=234226&view=auto
==============================================================================
--- directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/TupleTreeDecoderTest.java (added)
+++ directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/TupleTreeDecoderTest.java Sun Aug 21 08:03:46 2005
@@ -0,0 +1,107 @@
+/*
+ * 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 org.apache.asn1.codec.stateful.DecoderCallback;
+import org.apache.asn1.codec.stateful.DecoderMonitorAdapter;
+import org.apache.asn1.codec.stateful.StatefulDecoder;
+import org.apache.commons.lang.time.StopWatch;
+
+import java.nio.ByteBuffer;
+import java.util.Collections;
+
+
+/**
+ * Tests the TupleTreeDecoder.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">
+ * Apache Directory Project</a>
+ * @version $Rev: 158144 $
+ */
+public class TupleTreeDecoderTest extends TestCase implements DecoderCallback
+{
+ DefaultMutableTupleNode root = null ;
+
+ public static void main( String[] args)
+ {
+ TupleTreeDecoderTest test = new TupleTreeDecoderTest() ;
+
+ try { test.testTTD() ; } catch ( Exception e ) { e.printStackTrace() ; }
+ }
+
+ public void testSetMonitor()
+ {
+ TupleTreeDecoder decoder = new TupleTreeDecoder() ;
+ decoder.setDecoderMonitor( null ) ;
+ decoder.setDecoderMonitor( new DecoderMonitorAdapter() ) ;
+ }
+
+
+
+ public void testTTD2() throws Exception
+ {
+ TupleTreeDecoder decoder = new TupleTreeDecoder() ;
+ Tuple t = new Tuple( 1, 0, true, TypeClass.APPLICATION ) ;
+ ByteBuffer encoded = t.toEncodedBuffer( Collections.EMPTY_LIST ) ;
+ decoder.decode( encoded ) ;
+
+ t = new Tuple( 1, 0, true, TypeClass.APPLICATION ) ;
+ encoded = t.toEncodedBuffer( Collections.EMPTY_LIST ) ;
+ decoder.decode( encoded ) ;
+ }
+
+
+ public void testTTD4() throws Exception
+ {
+ Tuple t = new Tuple( 1, 0, true, TypeClass.APPLICATION ) ;
+ ByteBuffer encoded = t.toEncodedBuffer( Collections.EMPTY_LIST ) ;
+
+ ByteBuffer shorter = ByteBuffer.allocate( encoded.capacity() - 1 ) ;
+ shorter.put( ( ByteBuffer ) encoded.limit( shorter.limit() - 1 ) ) ;
+ assertNull( TupleTreeDecoder.treeDecode( shorter ) ) ;
+ }
+
+
+ public void testTTD() throws Exception
+ {
+ // Setup the bind request
+ byte[] pdu = {0x30, 0x28, 0x02, 0x01, 0x11, 0x66, 0x23, 0x04, 0x1F, 0x75, 0x69, 0x64, 0x3D, 0x61, 0x6B, 0x61, 0x72, 0x61, 0x73, 0x75, 0x6C, 0x75, 0x2C, 0x64, 0x63, 0x3D, 0x65, 0x78, 0x61, 0x6D, 0x70, 0x6C, 0x65, 0x2C, 0x64, 0x63, 0x3D, 0x63, 0x6F, 0x6D, 0x30, 0x00};
+
+ StopWatch watch = new StopWatch() ;
+ watch.start() ;
+ TupleTreeDecoder decoder = new TupleTreeDecoder() ;
+ decoder.setCallback( this ) ;
+ decoder.decode( ByteBuffer.wrap( pdu ) ) ;
+ watch.stop() ;
+
+ StringBuffer buf = new StringBuffer() ;
+ root.printDepthFirst( buf, 0 ) ;
+ }
+
+
+ /* (non-Javadoc)
+ * @see org.apache.asn1.codec.stateful.DecoderCallback#
+ * decodeOccurred(org.apache.asn1.codec.stateful.StatefulDecoder,
+ * java.lang.Object)
+ */
+ public void decodeOccurred( StatefulDecoder decoder, Object decoded )
+ {
+ root = ( DefaultMutableTupleNode ) decoded ;
+ }
+}
Added: directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/TypeClassTest.java
URL: http://svn.apache.org/viewcvs/directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/TypeClassTest.java?rev=234226&view=auto
==============================================================================
--- directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/TypeClassTest.java (added)
+++ directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/TypeClassTest.java Sun Aug 21 08:03:46 2005
@@ -0,0 +1,147 @@
+/*
+ * 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.util.List;
+import java.util.Map;
+
+import junit.framework.TestCase ;
+import org.apache.asn1.ber.TypeClass;
+
+
+/**
+ * Tests TypeClass class.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">
+ * Apache Directory Project</a>
+ * @version $Rev: 157644 $
+ */
+public class TypeClassTest extends TestCase
+{
+
+ public static void main(String[] args)
+ {
+ junit.textui.TestRunner.run(TypeClassTest.class);
+ }
+
+ /*
+ * @see TestCase#setUp()
+ */
+ protected void setUp() throws Exception
+ {
+ super.setUp();
+ }
+
+ /*
+ * @see TestCase#tearDown()
+ */
+ protected void tearDown() throws Exception
+ {
+ super.tearDown();
+ }
+
+ /**
+ * Constructor for TypeClassTest.
+ * @param arg0
+ */
+ public TypeClassTest(String arg0)
+ {
+ super(arg0);
+ }
+
+ /*
+ * Class to test for TypeClass getTypeClass(String)
+ */
+ public void testGetTypeClassString()
+ {
+ assertEquals( TypeClass.APPLICATION, TypeClass.getTypeClass(
+ TypeClass.APPLICATION.getName() ) ) ;
+ assertEquals( TypeClass.UNIVERSAL, TypeClass.getTypeClass(
+ TypeClass.UNIVERSAL.getName() ) ) ;
+ assertEquals( TypeClass.PRIVATE, TypeClass.getTypeClass(
+ TypeClass.PRIVATE.getName() ) ) ;
+ assertEquals( TypeClass.CONTEXT_SPECIFIC, TypeClass.getTypeClass(
+ TypeClass.CONTEXT_SPECIFIC.getName() ) ) ;
+
+ assertEquals( TypeClass.APPLICATION, TypeClass.getTypeClass(
+ "application") ) ;
+ assertEquals( TypeClass.UNIVERSAL, TypeClass.getTypeClass(
+ "Universal" ) ) ;
+ assertEquals( TypeClass.PRIVATE, TypeClass.getTypeClass(
+ "PRivatE" ) ) ;
+ assertEquals( TypeClass.CONTEXT_SPECIFIC, TypeClass.getTypeClass(
+ "context_specific" ) ) ;
+
+ try
+ {
+ TypeClass.getTypeClass( "asdf" ) ;
+ fail( "exception should prevent this failure" ) ;
+ }
+ catch ( Throwable t )
+ {
+ assertNotNull( t ) ;
+ }
+ }
+
+ public void testList()
+ {
+ List list = TypeClass.list() ;
+ assertNotNull( list ) ;
+ assertEquals( 4, list.size() ) ;
+ assertTrue( list.contains( TypeClass.PRIVATE ) ) ;
+ assertTrue( list.contains( TypeClass.UNIVERSAL ) ) ;
+ assertTrue( list.contains( TypeClass.APPLICATION ) ) ;
+ assertTrue( list.contains( TypeClass.CONTEXT_SPECIFIC ) ) ;
+ }
+
+ public void testMap()
+ {
+ Map map = TypeClass.map() ;
+ assertNotNull( map ) ;
+ assertEquals( 4, map.size() ) ;
+ assertTrue( map.containsKey( TypeClass.PRIVATE.getName() ) ) ;
+ assertTrue( map.containsKey( TypeClass.UNIVERSAL.getName() ) ) ;
+ assertTrue( map.containsKey( TypeClass.APPLICATION.getName() ) ) ;
+ assertTrue( map.containsKey( TypeClass.CONTEXT_SPECIFIC.getName() ) ) ;
+ }
+
+ /*
+ * Class to test for TypeClass getTypeClass(int)
+ */
+ public void testGetTypeClassint()
+ {
+ assertEquals( TypeClass.APPLICATION, TypeClass.getTypeClass(
+ TypeClass.APPLICATION_VAL ) ) ;
+ assertEquals( TypeClass.PRIVATE, TypeClass.getTypeClass(
+ TypeClass.PRIVATE_VAL ) ) ;
+ assertEquals( TypeClass.UNIVERSAL, TypeClass.getTypeClass(
+ TypeClass.UNIVERSAL_VAL ) ) ;
+ assertEquals( TypeClass.CONTEXT_SPECIFIC, TypeClass.getTypeClass(
+ TypeClass.CONTEXT_SPECIFIC_VAL ) ) ;
+
+ try
+ {
+ TypeClass.getTypeClass( 35 ) ;
+ fail( "exception should prevent this failure" ) ;
+ }
+ catch( Throwable t )
+ {
+ assertNotNull( t ) ;
+ }
+ }
+}
Added: directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/digester/AbstractRuleTest.java
URL: http://svn.apache.org/viewcvs/directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/digester/AbstractRuleTest.java?rev=234226&view=auto
==============================================================================
--- directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/digester/AbstractRuleTest.java (added)
+++ directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/digester/AbstractRuleTest.java Sun Aug 21 08:03:46 2005
@@ -0,0 +1,44 @@
+/*
+ * 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.digester;
+
+import junit.framework.*;
+import org.apache.asn1.ber.TypeClass;
+import org.apache.asn1.ber.digester.AbstractRule;
+
+import java.nio.ByteBuffer;
+
+
+/**
+ * Tests the AbstractRule which does not have much but is
+ * really used to shut clover up.
+ */
+public class AbstractRuleTest extends TestCase
+{
+ public void testAll()
+ {
+ AbstractRule rule = new MockRule() ;
+ rule.getDigester() ;
+ rule.setDigester( null ) ;
+ rule.tag( 0, true, TypeClass.APPLICATION ) ;
+ rule.length( 3 ) ;
+ rule.value( ByteBuffer.allocate( 3 ) ) ;
+ rule.finish() ;
+ }
+
+ class MockRule extends AbstractRule {}
+}
\ No newline at end of file
Added: directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/digester/BERDigesterTest.java
URL: http://svn.apache.org/viewcvs/directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/digester/BERDigesterTest.java?rev=234226&view=auto
==============================================================================
--- directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/digester/BERDigesterTest.java (added)
+++ directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/digester/BERDigesterTest.java Sun Aug 21 08:03:46 2005
@@ -0,0 +1,1012 @@
+/*
+ * 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.digester ;
+
+
+import junit.framework.TestCase;
+import org.apache.asn1.ber.TypeClass;
+
+import java.net.URL;
+import java.net.URLClassLoader;
+import java.nio.ByteBuffer;
+import java.util.EmptyStackException;
+
+
+/**
+ * A test case for the BERDigester.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev: 158144 $
+ */
+public class BERDigesterTest extends TestCase
+{
+ BERDigester digester ;
+
+
+ /**
+ * Sets up the decoder rulesBase system.
+ *
+ * @throws Exception
+ */
+ public void setUp() throws Exception
+ {
+ super.setUp();
+
+ digester = new BERDigester() ;
+ }
+
+
+ /**
+ * Clears and nulls out the rulesBase.
+ *
+ * @throws Exception
+ */
+ public void tearDown() throws Exception
+ {
+ super.tearDown();
+
+ digester.clear() ;
+ digester = null ;
+ }
+
+
+ /**
+ * Tests the BERDigester.addRule(int[],Rule) method.
+ */
+ public void testAddRule() throws Exception
+ {
+ int[] pat0 = { 1, 2, 3 } ;
+ MockRule rule0 = new MockRule() ;
+
+ digester.addRule( pat0, rule0 ) ;
+ }
+
+
+ /**
+ * Tests the BERDigester.setRules(Rules) method.
+ */
+ public void testSetRules() throws Exception
+ {
+ RulesBase rules = new RulesBase() ;
+ digester.setRules( rules ) ;
+ assertSame( rules.getDigester(), digester ) ;
+ }
+
+
+ /**
+ * Tests the BERDigester.getRules() method.
+ */
+ public void testGetRules() throws Exception
+ {
+ Rules rules = digester.getRules() ;
+ assertNotNull( rules ) ;
+ assertTrue( rules.rules().isEmpty() ) ;
+ }
+
+
+ /**
+ * Tests the BERDigester.getCount() method.
+ */
+ public void testGetCount() throws Exception
+ {
+ assertEquals( 0, digester.getCount() ) ;
+ }
+
+
+ /**
+ * Tests the BERDigester.peek() method.
+ */
+ public void testPeek() throws Exception
+ {
+ try
+ {
+ digester.peek() ;
+ }
+ catch( EmptyStackException e )
+ {
+ assertNotNull( e ) ;
+ return ;
+ }
+
+ fail( "should never reach this line" ) ;
+ }
+
+
+ /**
+ * Tests the BERDigester.peek(int) method.
+ */
+ public void testPeekint() throws Exception
+ {
+ try
+ {
+ digester.peek() ;
+ }
+ catch( EmptyStackException e )
+ {
+ assertNotNull( e ) ;
+ }
+
+ try
+ {
+ digester.peek( -1 ) ;
+ }
+ catch( IndexOutOfBoundsException e )
+ {
+ assertNotNull( e ) ;
+ }
+
+ try
+ {
+ digester.peek( Integer.MAX_VALUE ) ;
+ }
+ catch( EmptyStackException e )
+ {
+ assertNotNull( e ) ;
+ }
+
+ try
+ {
+ digester.peek( Integer.MIN_VALUE ) ;
+ }
+ catch( IndexOutOfBoundsException e )
+ {
+ assertNotNull( e ) ;
+ }
+ }
+
+
+ /**
+ * Tests the BERDigester.pop() method.
+ */
+ public void testPop() throws Exception
+ {
+ try
+ {
+ digester.pop() ;
+ }
+ catch( EmptyStackException e )
+ {
+ assertNotNull( e ) ;
+ return ;
+ }
+
+ fail( "should never reach this line" ) ;
+ }
+
+
+ /**
+ * Tests the BERDigester.push(Object) method.
+ */
+ public void testPush() throws Exception
+ {
+ Object o0 = new Object() ;
+ Object o1 = new Object() ;
+ Object o2 = new Object() ;
+
+ try
+ {
+ digester.pop() ;
+ fail( "should not get here" ) ;
+ }
+ catch( EmptyStackException e )
+ {
+ assertNotNull( e ) ;
+ }
+
+ digester.push( o0 ) ;
+ assertSame( o0, digester.peek() ) ;
+ assertSame( o0, digester.peek( 0 ) ) ;
+
+ digester.push( o1 ) ;
+ assertSame( o1, digester.peek() ) ;
+ assertSame( o1, digester.peek( 0 ) ) ;
+ assertSame( o0, digester.peek( 1 ) ) ;
+
+ digester.push( o2 ) ;
+ assertSame( o2, digester.peek() ) ;
+ assertSame( o2, digester.peek( 0 ) ) ;
+ assertSame( o1, digester.peek( 1 ) ) ;
+ assertSame( o0, digester.peek( 2 ) ) ;
+
+ assertSame( o2, digester.pop() ) ;
+ assertSame( o1, digester.pop() ) ;
+ assertSame( o0, digester.pop() ) ;
+
+ try
+ {
+ digester.pop() ;
+ fail( "should not get here" ) ;
+ }
+ catch( EmptyStackException e )
+ {
+ assertNotNull( e ) ;
+ }
+ }
+
+
+ /**
+ * Tests the BERDigester.GetRoot() method.
+ */
+ public void testGetRoot() throws Exception
+ {
+ Object o0 = new Object() ;
+ Object o1 = new Object() ;
+ Object o2 = new Object() ;
+
+ try
+ {
+ digester.pop() ;
+ fail( "should not get here" ) ;
+ }
+ catch( EmptyStackException e )
+ {
+ assertNotNull( e ) ;
+ }
+
+ digester.push( o0 ) ;
+ assertSame( o0, digester.getRoot() ) ;
+
+ digester.push( o1 ) ;
+ assertSame( o0, digester.getRoot() ) ;
+
+ digester.push( o2 ) ;
+
+ assertSame( o2, digester.pop() ) ;
+ assertSame( o1, digester.pop() ) ;
+ assertSame( o0, digester.pop() ) ;
+
+ try
+ {
+ digester.pop() ;
+ fail( "should not get here" ) ;
+ }
+ catch( EmptyStackException e )
+ {
+ assertNotNull( e ) ;
+ }
+
+ assertSame( o0, digester.getRoot() ) ;
+ }
+
+
+ /**
+ * Tests the BERDigester.getTopTag() method.
+ */
+ public void testGetTopTag() throws Exception
+ {
+ assertEquals( BERDigester.NO_TOP_TAG, digester.getTopTag() ) ;
+ digester.decode( ByteBuffer.wrap( new byte[] { (byte) 3} ) ) ;
+ assertFalse( BERDigester.NO_TOP_TAG == digester.getTopTag() ); ;
+ }
+
+
+ /**
+ * Tests the BERDigester.getClassLoader() method.
+ */
+ public void testGetClassLoader() throws Exception
+ {
+ assertSame( BERDigester.class.getClassLoader(), digester.getClassLoader() ) ;
+ }
+
+
+ /**
+ * Tests the BERDigester.setClassLoader() method.
+ */
+ public void testSetClassLoader() throws Exception
+ {
+ assertSame( BERDigester.class.getClassLoader(), digester.getClassLoader() ) ;
+ URL[] urls = { new URL( "file:///." ) } ;
+ ClassLoader cl = new URLClassLoader( urls ) ;
+ digester.setClassLoader( cl ) ;
+ assertSame( cl, digester.getClassLoader() ) ;
+ }
+
+
+ /**
+ * Tests the BERDigester.getUseContextClassLoader() method.
+ */
+ public void testGetUseContextClassLoader() throws Exception
+ {
+ assertSame( BERDigester.class.getClassLoader(), digester.getClassLoader() ) ;
+ assertFalse( digester.getUseContextClassLoader() ) ;
+ }
+
+
+ /**
+ * Tests the BERDigester.setUseContextClassLoader() method.
+ */
+ public void testSetUseContextClassLoader() throws Exception
+ {
+ digester.setUseContextClassLoader( true ) ;
+ assertTrue( digester.getUseContextClassLoader() ) ;
+ assertSame( Thread.currentThread().getContextClassLoader(),
+ digester.getClassLoader() ) ;
+ Thread.currentThread().setContextClassLoader( null ) ;
+ assertSame( BERDigester.class.getClassLoader(), digester.getClassLoader() ) ;
+ }
+
+
+ // ------------------------------------------------------------------------
+ // test the event fireing routines that trigger rules
+ // ------------------------------------------------------------------------
+
+
+ /**
+ * Tests the BERDigester.fireTagEvent(int,boolean,TypeClass) method.
+ */
+ public void testFireTagEvent() throws Exception
+ {
+ digester.fireTagEvent( 0, true, TypeClass.UNIVERSAL ) ;
+
+ int[] pat0 = { 0x10000000 } ;
+ CollectorRule rule0 = new CollectorRule() ;
+ digester.addRule( pat0, rule0 ) ;
+ ByteBuffer buf = ByteBuffer.allocate( 1 ) ;
+ buf.put( ( byte ) 0x10 ) ;
+ buf.flip() ;
+
+ // Nothing should have fired yet
+ assertFalse( rule0.tagFired ) ;
+ assertFalse( rule0.lengthFired ) ;
+ assertFalse( rule0.valueFired ) ;
+ assertFalse( rule0.hasFinished ) ;
+
+ digester.decode( buf ) ;
+ assertTrue( rule0.tagFired ) ;
+ assertEquals( 16, rule0.id ) ;
+ assertEquals( TypeClass.UNIVERSAL, rule0.typeClass ) ;
+ assertEquals( true, rule0.isPrimitive ) ;
+ assertFalse( rule0.lengthFired ) ;
+ assertFalse( rule0.valueFired ) ;
+ assertFalse( rule0.hasFinished ) ;
+ assertEquals( 0x10000000, digester.getTopTag() ) ;
+ }
+
+
+ /**
+ * Tests the BERDigester.fireLengthEvent(int) method.
+ */
+ public void testFireLengthEvent() throws Exception
+ {
+ digester.fireLengthEvent( 0 ) ;
+
+ int[] pat0 = { 0x10000000 } ;
+ CollectorRule rule0 = new CollectorRule() ;
+ digester.addRule( pat0, rule0 ) ;
+ ByteBuffer buf = ByteBuffer.allocate( 1 ) ;
+ buf.put( ( byte ) 0x10 ) ;
+ buf.flip() ;
+
+ // Nothing should have fired yet
+ assertFalse( rule0.tagFired ) ;
+ assertFalse( rule0.lengthFired ) ;
+ assertFalse( rule0.valueFired ) ;
+ assertFalse( rule0.hasFinished ) ;
+
+ digester.decode( buf ) ;
+ assertTrue( rule0.tagFired ) ;
+ assertEquals( 16, rule0.id ) ;
+ assertEquals( TypeClass.UNIVERSAL, rule0.typeClass ) ;
+ assertEquals( true, rule0.isPrimitive ) ;
+ assertFalse( rule0.lengthFired ) ;
+ assertFalse( rule0.valueFired ) ;
+ assertFalse( rule0.hasFinished ) ;
+ assertEquals( 0x10000000, digester.getTopTag() ) ;
+ }
+
+
+ /**
+ * Tests the BERDigester.fireValueEvent(ByteBuffer) method.
+ */
+ public void testFireValueEvent() throws Exception
+ {
+ digester.fireValueEvent( ByteBuffer.allocate( 0 ) ) ;
+
+ int[] pat0 = { 0x10000000 } ;
+ CollectorRule rule0 = new CollectorRule() ;
+ digester.addRule( pat0, rule0 ) ;
+ ByteBuffer buf = ByteBuffer.allocate( 1 ) ;
+ buf.put( ( byte ) 0x10 ) ;
+ buf.flip() ;
+
+ // Nothing should have fired yet
+ assertFalse( rule0.tagFired ) ;
+ assertFalse( rule0.lengthFired ) ;
+ assertFalse( rule0.valueFired ) ;
+ assertFalse( rule0.hasFinished ) ;
+
+ digester.decode( buf ) ;
+ assertTrue( rule0.tagFired ) ;
+ assertEquals( 16, rule0.id ) ;
+ assertEquals( TypeClass.UNIVERSAL, rule0.typeClass ) ;
+ assertEquals( true, rule0.isPrimitive ) ;
+ assertFalse( rule0.lengthFired ) ;
+ assertFalse( rule0.valueFired ) ;
+ assertFalse( rule0.hasFinished ) ;
+ assertEquals( 0x10000000, digester.getTopTag() ) ;
+
+ buf.rewind() ;
+ buf.put( ( byte ) 0x02 ) ;
+ buf.flip() ;
+ digester.decode( buf ) ;
+ assertTrue( rule0.tagFired ) ;
+ assertEquals( 2, rule0.length ) ;
+ assertTrue( rule0.lengthFired ) ;
+ assertFalse( rule0.valueFired ) ;
+ assertFalse( rule0.hasFinished ) ;
+ assertEquals( 0x10000000, digester.getTopTag() ) ;
+
+ buf.rewind() ;
+ buf.put( ( byte ) 0x07 ) ;
+ buf.flip() ;
+ digester.decode( buf ) ;
+ assertTrue( rule0.tagFired ) ;
+ assertEquals( 2, rule0.length ) ;
+ assertTrue( rule0.lengthFired ) ;
+ assertTrue( rule0.valueFired ) ;
+ assertFalse( rule0.hasFinished ) ;
+ assertEquals( 0x10000000, digester.getTopTag() ) ;
+ assertEquals( buf.get(0), rule0.buf.get(0) ) ;
+ }
+
+
+ /**
+ * Tests the BERDecoder.fireFinishEvent() methods.
+ */
+ public void testFireFinishEvent() throws Exception
+ {
+ int[] pat0 = { 0x10000000 } ;
+ CollectorRule rule0 = new CollectorRule() ;
+ digester.addRule( pat0, rule0 ) ;
+ ByteBuffer buf = ByteBuffer.allocate( 1 ) ;
+ buf.put( ( byte ) 0x10 ) ;
+ buf.flip() ;
+
+ // Nothing should have fired yet
+ assertFalse( rule0.tagFired ) ;
+ assertFalse( rule0.lengthFired ) ;
+ assertFalse( rule0.valueFired ) ;
+ assertFalse( rule0.hasFinished ) ;
+
+ digester.decode( buf ) ;
+ assertTrue( rule0.tagFired ) ;
+ assertEquals( 16, rule0.id ) ;
+ assertEquals( TypeClass.UNIVERSAL, rule0.typeClass ) ;
+ assertEquals( true, rule0.isPrimitive ) ;
+ assertFalse( rule0.lengthFired ) ;
+ assertFalse( rule0.valueFired ) ;
+ assertFalse( rule0.hasFinished ) ;
+ assertEquals( 0x10000000, digester.getTopTag() ) ;
+
+ buf.rewind() ;
+ buf.put( ( byte ) 0x02 ) ;
+ buf.flip() ;
+ digester.decode( buf ) ;
+ assertTrue( rule0.tagFired ) ;
+ assertEquals( 2, rule0.length ) ;
+ assertTrue( rule0.lengthFired ) ;
+ assertFalse( rule0.valueFired ) ;
+ assertFalse( rule0.hasFinished ) ;
+ assertEquals( 0x10000000, digester.getTopTag() ) ;
+
+ buf.rewind() ;
+ buf.put( ( byte ) 0x07 ) ;
+ buf.flip() ;
+ digester.decode( buf ) ;
+ assertTrue( rule0.tagFired ) ;
+ assertEquals( 2, rule0.length ) ;
+ assertTrue( rule0.lengthFired ) ;
+ assertTrue( rule0.valueFired ) ;
+ assertFalse( rule0.hasFinished ) ;
+ assertEquals( 0x10000000, digester.getTopTag() ) ;
+ assertEquals( buf.get(0), rule0.buf.get(0) ) ;
+
+ buf.rewind() ;
+ buf.put( ( byte ) 0x09 ) ;
+ buf.flip() ;
+ digester.decode( buf ) ;
+ assertTrue( rule0.tagFired ) ;
+ assertEquals( 2, rule0.length ) ;
+ assertTrue( rule0.lengthFired ) ;
+ assertTrue( rule0.valueFired ) ;
+ assertTrue( rule0.hasFinished ) ;
+ assertEquals( BERDigester.NO_TOP_TAG, digester.getTopTag() ) ;
+ assertEquals( buf.get(0), rule0.buf.get(0) ) ;
+ }
+
+
+ /**
+ * Tests the when rules throw errors.
+ */
+ public void testErrorOnTag() throws Exception
+ {
+ int[] pat0 = { 0x10000000 } ;
+ ErrorRule rule0 = new ErrorRule() ;
+ digester.addRule( pat0, rule0 ) ;
+ rule0.throwErrors() ;
+
+ ByteBuffer buf = ByteBuffer.allocate( 1 ) ;
+ buf.put( ( byte ) 0x10 ) ;
+ buf.flip() ;
+
+ try
+ {
+ digester.decode( buf ) ;
+ }
+ catch( Error e )
+ {
+ assertNotNull( e ) ;
+ return ;
+ }
+
+ fail( "should never get here" ) ;
+ }
+
+
+ public void testErrorOnLength() throws Exception
+ {
+ int[] pat0 = { 0x10000000 } ;
+ ErrorRule rule0 = new ErrorRule() ;
+ digester.addRule( pat0, rule0 ) ;
+
+ ByteBuffer buf = ByteBuffer.allocate( 1 ) ;
+ buf.put( ( byte ) 0x10 ) ;
+ buf.flip() ;
+
+ digester.decode( buf ) ;
+
+ buf.rewind() ;
+ buf.put( ( byte ) 0x02 ) ;
+ buf.flip() ;
+
+ rule0.throwErrors() ;
+ try
+ {
+ digester.decode( buf ) ;
+ }
+ catch( Error e )
+ {
+ assertNotNull( e ) ;
+ return ;
+ }
+
+ fail( "should never get here" ) ;
+ }
+
+
+ public void testErrorOnValue() throws Exception
+ {
+ int[] pat0 = { 0x10000000 } ;
+ ErrorRule rule0 = new ErrorRule() ;
+ digester.addRule( pat0, rule0 ) ;
+
+ ByteBuffer buf = ByteBuffer.allocate( 1 ) ;
+ buf.put( ( byte ) 0x10 ) ;
+ buf.flip() ;
+
+ digester.decode( buf ) ;
+
+ buf.rewind() ;
+ buf.put( ( byte ) 0x02 ) ;
+ buf.flip() ;
+
+ digester.decode( buf ) ;
+
+ buf.rewind() ;
+ buf.put( ( byte ) 0x02 ) ;
+ buf.flip() ;
+
+ rule0.throwErrors() ;
+ try
+ {
+ digester.decode( buf ) ;
+ }
+ catch( Error e )
+ {
+ assertNotNull( e ) ;
+ return ;
+ }
+
+ fail( "should never get here" ) ;
+ }
+
+
+ public void testErrorOnFinish() throws Exception
+ {
+ int[] pat0 = { 0x10000000 } ;
+ ErrorRule rule0 = new ErrorRule() ;
+ digester.addRule( pat0, rule0 ) ;
+
+ ByteBuffer buf = ByteBuffer.allocate( 1 ) ;
+ buf.put( ( byte ) 0x10 ) ;
+ buf.flip() ;
+
+ digester.decode( buf ) ;
+
+ buf.rewind() ;
+ buf.put( ( byte ) 0x02 ) ;
+ buf.flip() ;
+
+ digester.decode( buf ) ;
+
+ buf.rewind() ;
+ buf.put( ( byte ) 0x07 ) ;
+ buf.flip() ;
+
+ digester.decode( buf ) ;
+
+ buf.rewind() ;
+ buf.put( ( byte ) 0x09 ) ;
+ buf.flip() ;
+
+ rule0.throwErrorOnFinish() ;
+ try
+ {
+ digester.decode( buf ) ;
+ }
+ catch( Error e )
+ {
+ assertNotNull( e ) ;
+ return ;
+ }
+
+ fail( "should never get here" ) ;
+ }
+
+
+ /**
+ * Tests the when rules throw errors.
+ */
+ public void testExceptionOnTag() throws Exception
+ {
+ int[] pat0 = { 0x10000000 } ;
+ ExceptionRule rule0 = new ExceptionRule() ;
+ digester.addRule( pat0, rule0 ) ;
+ rule0.throwErrors() ;
+
+ ByteBuffer buf = ByteBuffer.allocate( 1 ) ;
+ buf.put( ( byte ) 0x10 ) ;
+ buf.flip() ;
+
+ try
+ {
+ digester.decode( buf ) ;
+ }
+ catch( RuntimeException e )
+ {
+ assertNotNull( e ) ;
+ return ;
+ }
+
+ fail( "should never get here" ) ;
+ }
+
+
+ public void testExceptionOnLength() throws Exception
+ {
+ int[] pat0 = { 0x10000000 } ;
+ ExceptionRule rule0 = new ExceptionRule() ;
+ digester.addRule( pat0, rule0 ) ;
+
+ ByteBuffer buf = ByteBuffer.allocate( 1 ) ;
+ buf.put( ( byte ) 0x10 ) ;
+ buf.flip() ;
+
+ digester.decode( buf ) ;
+
+ buf.rewind() ;
+ buf.put( ( byte ) 0x02 ) ;
+ buf.flip() ;
+
+ rule0.throwErrors() ;
+ try
+ {
+ digester.decode( buf ) ;
+ }
+ catch( RuntimeException e )
+ {
+ assertNotNull( e ) ;
+ return ;
+ }
+
+ fail( "should never get here" ) ;
+ }
+
+
+ public void testExceptionOnValue() throws Exception
+ {
+ int[] pat0 = { 0x10000000 } ;
+ ExceptionRule rule0 = new ExceptionRule() ;
+ digester.addRule( pat0, rule0 ) ;
+
+ ByteBuffer buf = ByteBuffer.allocate( 1 ) ;
+ buf.put( ( byte ) 0x10 ) ;
+ buf.flip() ;
+
+ digester.decode( buf ) ;
+
+ buf.rewind() ;
+ buf.put( ( byte ) 0x02 ) ;
+ buf.flip() ;
+
+ digester.decode( buf ) ;
+
+ buf.rewind() ;
+ buf.put( ( byte ) 0x02 ) ;
+ buf.flip() ;
+
+ rule0.throwErrors() ;
+ try
+ {
+ digester.decode( buf ) ;
+ }
+ catch( RuntimeException e )
+ {
+ assertNotNull( e ) ;
+ return ;
+ }
+
+ fail( "should never get here" ) ;
+ }
+
+
+ public void testExceptionOnFinish() throws Exception
+ {
+ int[] pat0 = { 0x10000000 } ;
+ ExceptionRule rule0 = new ExceptionRule() ;
+ digester.addRule( pat0, rule0 ) ;
+
+ ByteBuffer buf = ByteBuffer.allocate( 1 ) ;
+ buf.put( ( byte ) 0x10 ) ;
+ buf.flip() ;
+
+ digester.decode( buf ) ;
+
+ buf.rewind() ;
+ buf.put( ( byte ) 0x02 ) ;
+ buf.flip() ;
+
+ digester.decode( buf ) ;
+
+ buf.rewind() ;
+ buf.put( ( byte ) 0x07 ) ;
+ buf.flip() ;
+
+ digester.decode( buf ) ;
+
+ buf.rewind() ;
+ buf.put( ( byte ) 0x09 ) ;
+ buf.flip() ;
+
+ rule0.throwErrorOnFinish() ;
+ try
+ {
+ digester.decode( buf ) ;
+ }
+ catch( RuntimeException e )
+ {
+ assertNotNull( e ) ;
+ return ;
+ }
+
+ fail( "should never get here" ) ;
+ }
+
+
+ public void testFullTlv() throws Exception
+ {
+ int[] pat0 = { 0x10000000 } ;
+ int[] pat1 = { 0x11000000 } ;
+ CollectorRule rule0 = new CollectorRule() ;
+ CollectorRule rule1 = new CollectorRule() ;
+ digester.addRule( pat0, rule0 ) ;
+ digester.addRule( pat1, rule1 ) ;
+ byte[] bites = { 0x10, 0x02, 0x07, 0x09 } ;
+ ByteBuffer buf = ByteBuffer.wrap( bites ) ;
+ digester.decode( buf ) ;
+ }
+
+
+ public void testNestedTlvs() throws Exception
+ {
+ int[] pat0 = { 0x14000000, 0x10000000 } ;
+ int[] pat1 = { 0x11000000 } ;
+ CollectorRule rule0 = new CollectorRule() ;
+ CollectorRule rule1 = new CollectorRule() ;
+ digester.addRule( pat0, rule0 ) ;
+ digester.addRule( pat1, rule1 ) ;
+ byte[] bites = {
+ 0x34, 0x04, // constructed id = 20 w/ 4 byte value
+ 0x10, 0x02, 0x07, 0x09 // primitive id = 16 w/ 2 byte value
+ } ;
+ ByteBuffer buf = ByteBuffer.wrap( bites ) ;
+ digester.decode( buf ) ;
+ }
+
+
+ public void testNestedTlvsWithErrors() throws Exception
+ {
+ int[] pat0 = { 0x14000000, 0x10000000 } ;
+ int[] pat1 = { 0x14000000 } ;
+ ErrorRule rule0 = new ErrorRule() ;
+ ErrorRule rule1 = new ErrorRule() ;
+ digester.addRule( pat0, rule0 ) ;
+ digester.addRule( pat1, rule1 ) ;
+ byte[] bites = {
+ 0x34, 0x04, // constructed id = 20 w/ 4 byte value
+ 0x10, 0x02, 0x07 // primitive id = 16 w/ 2 byte value
+ // last byte missing
+ } ;
+ ByteBuffer buf = ByteBuffer.wrap( bites ) ;
+ digester.decode( buf ) ;
+
+ rule1.throwErrorOnFinish() ;
+ buf = ByteBuffer.allocate( 1 ) ;
+ buf.put( 0, ( byte ) 0x09 ) ;
+
+ try
+ {
+ digester.decode( buf ) ;
+ }
+ catch( Error e )
+ {
+ return ;
+ }
+
+ fail( "should never get here" ) ;
+ }
+
+
+ class MockRule extends AbstractRule {}
+
+
+ class CollectorRule extends AbstractRule
+ {
+ boolean tagFired = false ;
+ boolean lengthFired = false ;
+ boolean valueFired = false ;
+ int id = -1 ;
+ boolean isPrimitive = false ;
+ TypeClass typeClass = null ;
+ int length = -1 ;
+ ByteBuffer buf = null ;
+ boolean hasFinished = false ;
+
+ public void tag( int id, boolean isPrimitive, TypeClass typeClass )
+ {
+ this.id = id ;
+ this.typeClass = typeClass ;
+ this.isPrimitive = isPrimitive ;
+ this.tagFired = true ;
+ }
+
+ public void length( int length )
+ {
+ this.length = length ;
+ this.lengthFired = true ;
+ }
+
+ public void value( ByteBuffer buf )
+ {
+ this.buf = buf ;
+ this.valueFired = true ;
+ }
+
+ public void finish()
+ {
+ this.hasFinished = true ;
+ }
+ }
+
+
+ class ErrorRule extends AbstractRule
+ {
+ private boolean doThrow = false ;
+ private boolean doThrowOnFinish = false ;
+
+ public void throwErrors()
+ {
+ doThrow = true ;
+ }
+
+ public void throwErrorOnFinish()
+ {
+ doThrowOnFinish = true ;
+ }
+
+ public void tag( int id, boolean isPrimitive, TypeClass typeClass )
+ {
+ if ( doThrow )
+ {
+ throw new Error() ;
+ }
+ }
+
+ public void length( int length )
+ {
+ if ( doThrow )
+ {
+ throw new Error() ;
+ }
+ }
+
+ public void value( ByteBuffer buf )
+ {
+ if ( doThrow )
+ {
+ throw new Error() ;
+ }
+ }
+
+ public void finish()
+ {
+ if ( doThrowOnFinish )
+ {
+ throw new Error() ;
+ }
+ }
+ }
+
+
+ class ExceptionRule extends AbstractRule
+ {
+ private boolean doThrow = false ;
+ private boolean doThrowOnFinish = false ;
+
+ public void throwErrors()
+ {
+ doThrow = true ;
+ }
+
+ public void throwErrorOnFinish()
+ {
+ doThrowOnFinish = true ;
+ }
+
+ public void tag( int id, boolean isPrimitive, TypeClass typeClass )
+ {
+ if ( doThrow )
+ {
+ throw new RuntimeException() ;
+ }
+ }
+
+ public void length( int length )
+ {
+ if ( doThrow )
+ {
+ throw new RuntimeException() ;
+ }
+ }
+
+ public void value( ByteBuffer buf )
+ {
+ if ( doThrow )
+ {
+ throw new RuntimeException() ;
+ }
+ }
+
+ public void finish()
+ {
+ if ( doThrowOnFinish )
+ {
+ throw new RuntimeException() ;
+ }
+ }
+ }
+}
\ No newline at end of file
Added: directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/digester/RulesBaseTest.java
URL: http://svn.apache.org/viewcvs/directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/digester/RulesBaseTest.java?rev=234226&view=auto
==============================================================================
--- directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/digester/RulesBaseTest.java (added)
+++ directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/digester/RulesBaseTest.java Sun Aug 21 08:03:46 2005
@@ -0,0 +1,178 @@
+/*
+ * 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.digester ;
+
+
+import junit.framework.TestCase ;
+
+import java.util.List ;
+
+import org.apache.commons.collections.primitives.IntStack;
+import org.apache.asn1.ber.digester.AbstractRule;
+import org.apache.asn1.ber.digester.BERDigester;
+import org.apache.asn1.ber.digester.RulesBase;
+
+
+/**
+ * A test case for the RulesBase.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev: 157644 $
+ */
+public class RulesBaseTest extends TestCase
+{
+ int[] p0 = { 1, 2, 3 } ;
+ IntStack s0 = new IntStack( p0 ) ;
+ int[] p1 = { 1, 6, 7, 8 } ;
+ IntStack s1 = new IntStack( p1 ) ;
+ int[] p2 = { 4, 5, 6 } ;
+ IntStack s2 = new IntStack( p2 ) ;
+ MockRule r0 = new MockRule() ;
+ MockRule r1 = new MockRule() ;
+ MockRule r2 = new MockRule() ;
+ RulesBase rulesBase ;
+
+
+ /**
+ * Sets up the decoder rulesBase system.
+ *
+ * @throws Exception
+ */
+ public void setUp() throws Exception
+ {
+ super.setUp() ;
+
+ rulesBase = new RulesBase() ;
+ }
+
+
+ /**
+ * Clears and nulls out the rulesBase.
+ *
+ * @throws Exception
+ */
+ public void tearDown() throws Exception
+ {
+ super.tearDown() ;
+
+ rulesBase.clear() ;
+ rulesBase = null ;
+ }
+
+
+ /**
+ * Tests the RulesBase.add(int[],Rule) method.
+ */
+ public void testAdd()
+ {
+
+ assertTrue( "Should be empty on creation", rulesBase.rules().isEmpty() ) ;
+
+ rulesBase.add( p0, r0 ) ;
+ assertFalse( rulesBase.rules().isEmpty() ) ;
+ assertEquals( "Should have 1 rule after 1st add", 1, rulesBase.rules().size() ) ;
+ assertSame( "1st rule should be r0", rulesBase.rules().get( 0 ), r0 ) ;
+
+ rulesBase.add( p1, r1 ) ;
+ assertFalse( rulesBase.rules().isEmpty() ) ;
+ assertEquals( "Should have 2 rules after 2nd add", 2, rulesBase.rules().size() ) ;
+ assertSame( "2nd rule should be r1", rulesBase.rules().get( 1 ), r1 ) ;
+
+ rulesBase.add( p2, r2 ) ;
+ assertFalse( rulesBase.rules().isEmpty() ) ;
+ assertEquals( "Should have 3 rules after 3rd add", 3, rulesBase.rules().size() ) ;
+ assertSame( "3rd rule should be r2", rulesBase.rules().get( 2 ), r2 ) ;
+ }
+
+
+ /**
+ * Tests the RulesBase.match(int[]) method.
+ */
+ public void testMatchint()
+ {
+ List matched = null ;
+
+ matched = rulesBase.match( p0 ) ;
+ assertTrue( "match on p0 should not return any rules", matched.isEmpty() ) ;
+ rulesBase.add( p0, r0 ) ;
+ matched = rulesBase.match( p0 ) ;
+ assertSame( "match on p0 should return r0 only", matched.get( 0 ), r0 ) ;
+ assertEquals( "match on p0 should only match for one rule", 1, matched.size() ) ;
+
+ matched = rulesBase.match( p1 ) ;
+ assertTrue( "match on p1 should not return any rules", matched.isEmpty() ) ;
+ rulesBase.add( p1, r1 ) ;
+ matched = rulesBase.match( p1 ) ;
+ assertSame( "match on p1 should return r1 only", matched.get( 0 ), r1 ) ;
+ assertEquals( "match on p1 should only match for one rule", 1, matched.size() ) ;
+
+ matched = rulesBase.match( p2 ) ;
+ assertTrue( "match on p2 should not return any rules", matched.isEmpty() ) ;
+ rulesBase.add( p2, r2 ) ;
+ matched = rulesBase.match( p2 ) ;
+ assertSame( "match on p2 should return r2 only", matched.get( 0 ), r2 ) ;
+ assertEquals( "match on p2 should only match for one rule", 1, matched.size() ) ;
+ }
+
+
+ /**
+ * Tests the RulesBase.match(int[]) method.
+ */
+ public void testMatchIntStack()
+ {
+ List matched = null ;
+
+ matched = rulesBase.match( s0 ) ;
+ assertTrue( "match on s0 should not return any rules", matched.isEmpty() ) ;
+ rulesBase.add( p0, r0 ) ;
+ matched = rulesBase.match( s0 ) ;
+ assertSame( "match on s0 should return r0 only", matched.get( 0 ), r0 ) ;
+ assertEquals( "match on s0 should only match for one rule", 1, matched.size() ) ;
+
+ matched = rulesBase.match( s1 ) ;
+ assertTrue( "match on s1 should not return any rules", matched.isEmpty() ) ;
+ rulesBase.add( p1, r1 ) ;
+ matched = rulesBase.match( s1 ) ;
+ assertSame( "match on s1 should return r1 only", matched.get( 0 ), r1 ) ;
+ assertEquals( "match on s1 should only match for one rule", 1, matched.size() ) ;
+
+ matched = rulesBase.match( s2 ) ;
+ assertTrue( "match on s2 should not return any rules", matched.isEmpty() ) ;
+ rulesBase.add( p2, r2 ) ;
+ matched = rulesBase.match( s2 ) ;
+ assertSame( "match on s2 should return r2 only", matched.get( 0 ), r2 ) ;
+ assertEquals( "match on s2 should only match for one rule", 1, matched.size() ) ;
+ }
+
+
+ public void testGetDigester()
+ {
+ assertNull( rulesBase.getDigester() ) ;
+ }
+
+
+ public void testSetDigester()
+ {
+ assertNull( rulesBase.getDigester() ) ;
+ BERDigester digester = new BERDigester() ;
+ rulesBase.setDigester( digester ) ;
+ assertSame( digester, rulesBase.getDigester() ) ;
+ }
+
+
+ class MockRule extends AbstractRule {}
+}
\ No newline at end of file
Added: directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/digester/TagNodeTest.java
URL: http://svn.apache.org/viewcvs/directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/digester/TagNodeTest.java?rev=234226&view=auto
==============================================================================
--- directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/digester/TagNodeTest.java (added)
+++ directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/digester/TagNodeTest.java Sun Aug 21 08:03:46 2005
@@ -0,0 +1,72 @@
+/*
+ * 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.digester;
+
+
+import junit.framework.*;
+import org.apache.asn1.ber.digester.TagNode;
+
+
+/**
+ * Unit test for the TagNode class.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">
+ * Apache Directory Project</a>
+ * @version $Rev: 157644 $
+ */
+public class TagNodeTest extends TestCase
+{
+ TagNode n0 = null ;
+ TagNode n1 = null ;
+ TagNode n2 = null ;
+
+
+ public void setUp() throws Exception
+ {
+ super.setUp() ;
+
+ n0 = new TagNode( new Integer( 0 ) ) ;
+ n1 = new TagNode( new Integer( 1 ) ) ;
+ n2 = new TagNode( new Integer( 2 ) ) ;
+ }
+
+ /**
+ * Tests the TagNode.getDepth() method.
+ */
+ public void testDepth()
+ {
+ assertEquals( 0, n0.getDepth() ) ;
+ n0.addNode( n1 ) ;
+ assertEquals( 0, n0.getDepth() ) ;
+ assertEquals( 1, n1.getDepth() ) ;
+ n1.addNode( n2 ) ;
+ assertEquals( 0, n0.getDepth() ) ;
+ assertEquals( 1, n1.getDepth() ) ;
+ assertEquals( 2, n2.getDepth() ) ;
+ }
+
+
+ /**
+ * Tests the TagNode.hasChildren() method.
+ */
+ public void testHasChildren()
+ {
+ assertFalse( n0.hasChild( new Integer( 1 ) ) ) ;
+ n0.addNode( n1 ) ;
+ assertTrue( n0.hasChild( new Integer( 1 ) ) ) ;
+ }
+}
\ No newline at end of file