directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From akaras...@apache.org
Subject svn commit: rev 9339 - in incubator/directory/snickers/trunk/ber/src: java/org/apache/snickers/ber test/org/apache/snickers/ber
Date Wed, 10 Mar 2004 07:14:23 GMT
Author: akarasulu
Date: Tue Mar  9 23:14:22 2004
New Revision: 9339

Added:
   incubator/directory/snickers/trunk/ber/src/test/org/apache/snickers/ber/ConstructedTLVTests.java
Modified:
   incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/ber/BERDecoder.java
Log:
Added more tests now on nested tlv structures.  Everything is working ok with
the tests but the indices on the constructed structures are off.  I'm afraid 
that these miscalculations will show up as errors where nesting is messed up
because constructed TLVs with the long length form will incorrectly thing
the value is gathered.

We need to make sure we write assertions around the values that all indices
should have.  Furthermore we need more elaborate tests where lots of long
nested structures are created.  We can get there by making sure the TLV encoding
works and using that to build structures that are fed into the decoder.


Modified: incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/ber/BERDecoder.java
==============================================================================
--- incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/ber/BERDecoder.java
(original)
+++ incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/ber/BERDecoder.java
Tue Mar  9 23:14:22 2004
@@ -227,8 +227,9 @@
      */
     private void decodeLength( ByteBuffer buf ) throws DecoderException
     {
-        for ( ; buf.hasRemaining(); tlv.index++ )
+        while ( buf.hasRemaining() )
         {
+            tlv.index++ ;
             byte octet = buf.get() ;
             lengthBuffer.add( octet ) ;
             
@@ -264,8 +265,10 @@
                 {
                     lengthOfLength = INDEFINATE ;
                     tlv.index = INDEFINATE ;
+                    tlv.length = INDEFINATE ;
                     tlv.valueIndex = UNDEFINED ;
                     cb.lengthDecoded( tlv ) ;
+                    lengthBuffer.clear() ;
                     tlvStack.push( tlv.clone() ) ;
                     tlv.clear() ;
                     state = BERDecoderState.TAG ;
@@ -346,6 +349,57 @@
             return ;
         }
         
+        /*
+         * Check for a INDEFINATE length TLV when tlv is primitive with 
+         * zero length and a type class of UNIVERSAL which is reserved
+         * for use by encoding rules.
+         */
+        if ( tlv.typeClass == TypeClass.UNIVERSAL && tlv.length == 0 )
+        {
+            String msg = "expected indefinate length TLV on the stack" ;
+
+            if ( tlvStack.isEmpty() )
+            {
+                IllegalStateException e = new IllegalStateException(
+                         msg + " but the stack is empty" ) ;
+                    
+                if ( monitor == null )
+                {
+                    throw e ;
+                }
+                else
+                {
+                    monitor.fatalError( this, e ) ;
+                }
+                    
+                return ;
+            }
+
+            Tuple top = ( Tuple ) tlvStack.peek() ;
+            if ( top.length != INDEFINATE )
+            {
+                IllegalStateException e = new IllegalStateException(
+                         msg + " but TLV on top has a definate length" ) ;
+                    
+                if ( monitor == null )
+                {
+                    throw e ;
+                }
+                else
+                {
+                    monitor.fatalError( this, e ) ;
+                }
+                    
+                return ;
+            }
+                
+            tlvStack.pop() ;
+            cb.decodeOccurred( this, top ) ;
+            tlv.clear() ;
+            state = BERDecoderState.TAG ;
+            return ;
+        }
+        
         if ( ! buf.hasRemaining() )
         {
             IllegalArgumentException e = new IllegalArgumentException(
@@ -382,64 +436,12 @@
         {
             buf.get( value, offset, needToRead ) ;
             tlv.valueIndex = tlv.length ;
+            tlv.index += tlv.length ;
+            cb.decodeOccurred( this, tlv ) ;
             
-            /*
-             * Check for a INDEFINATE length TLV when tlv is primitive with 
-             * zero length and a type class of UNIVERSAL which is reserved
-             * for use by encoding rules.
-             */
-            if ( tlv.typeClass == TypeClass.UNIVERSAL &&
-                 tlv.length == 0 )
-            {
-                String msg = "expected indefinate length TLV on the stack" ;
-
-                if ( tlvStack.isEmpty() )
-                {
-                    IllegalStateException e = new IllegalStateException(
-                             msg + " but the stack is empty" ) ;
-                    
-                    if ( monitor == null )
-                    {
-                        throw e ;
-                    }
-                    else
-                    {
-                        monitor.fatalError( this, e ) ;
-                    }
-                    
-                    return ;
-                }
-
-                Tuple top = ( Tuple ) tlvStack.peek() ;
-                if ( top.length != INDEFINATE )
-                {
-                    IllegalStateException e = new IllegalStateException(
-                             msg + " but TLV on top has a definate length" ) ;
-                    
-                    if ( monitor == null )
-                    {
-                        throw e ;
-                    }
-                    else
-                    {
-                        monitor.fatalError( this, e ) ;
-                    }
-                    
-                    return ;
-                }
-                
-                tlvStack.pop() ;
-                cb.decodeOccurred( this, top ) ;
-            }
-            else
-            {
-                cb.decodeOccurred( this, tlv ) ;
-            }
-            
-            tlv.clear() ;
-
             if ( tlvStack.isEmpty() )
             {
+                tlv.clear() ;
                 state = BERDecoderState.TAG ;
                 return ;
             }
@@ -453,17 +455,24 @@
             {
                 Tuple t = ( Tuple ) tlvStack.get( ii ) ;
                 
-                if ( t.length != INDEFINATE )
-                {    
-                    t.index += tlv.length ;
-                    t.valueIndex += tlv.length ;
+                t.index += tlv.index + 1 ;
+                
+                if ( t.valueIndex == UNDEFINED )
+                {
+                    t.valueIndex = 0 ;
                 }
+                
+                t.valueIndex += tlv.index + 1 ;
             }
             
             do
             {
                 Tuple top = ( Tuple ) tlvStack.peek() ;
                 
+                if ( top.length == INDEFINATE )
+                {
+                    break ;
+                }
                 if ( top.valueIndex >= top.length )
                 {
                     tlvStack.pop() ;
@@ -475,6 +484,7 @@
                 }
             } while( tlvStack.size() > 0 ) ;
 
+            tlv.clear() ;
             state = BERDecoderState.TAG ;
         }
         else

Added: incubator/directory/snickers/trunk/ber/src/test/org/apache/snickers/ber/ConstructedTLVTests.java
==============================================================================
--- (empty file)
+++ incubator/directory/snickers/trunk/ber/src/test/org/apache/snickers/ber/ConstructedTLVTests.java
Tue Mar  9 23:14:22 2004
@@ -0,0 +1,156 @@
+/*
+ *   Copyright 2004 The Apache Software Foundation
+ *
+ *   Licensed under the Apache License, Version 2.0 (the "License");
+ *   you may not use this file except in compliance with the License.
+ *   You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing, software
+ *   distributed under the License is distributed on an "AS IS" BASIS,
+ *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *   See the License for the specific language governing permissions and
+ *   limitations under the License.
+ *
+ */
+package org.apache.snickers.ber ;
+
+
+/**
+ * Performs constructed tlv tests.
+ *
+ * @author <a href="mailto:directory-dev@incubator.apache.org">
+ * Apache Directory Project</a>
+ * @version $Rev$
+ */
+public class ConstructedTLVTests extends BERDecoderTest
+{
+
+    /**
+     * @param arg0
+     */
+    public ConstructedTLVTests( String arg0 )
+    {
+        super( arg0 ) ;
+    }
+    
+    
+    public void testConstructedDefinateTLV() throws Exception
+    {
+        // decode tag
+        Tuple outter = decode( "01100001" ) ;
+        assertEquals( 1, outter.id ) ;
+        assertEquals( 0, tlvList.size() ) ;
+        assertEquals( false, outter.isPrimitive ) ;
+        assertEquals( TypeClass.APPLICATION, outter.typeClass ) ;
+        assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+        
+        // decode length 
+        outter = decode( "00000011" ) ;
+        assertEquals( BERDecoderState.TAG, decoder.getState() ) ;
+        
+        // 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( 2, tlvList.size() ) ;
+    }
+
+
+    public void testMultipleIndefinateTLV() throws Exception
+    {
+        // decode tag
+        Tuple outter = decode( "01100001" ) ;
+        assertEquals( 1, outter.id ) ;
+        assertEquals( 0, tlvList.size() ) ;
+        assertEquals( false, outter.isPrimitive ) ;
+        assertEquals( TypeClass.APPLICATION, outter.typeClass ) ;
+        assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+        
+        // decode length 
+        outter = decode( "10000000" ) ;
+        assertEquals( BERDecoderState.TAG, decoder.getState() ) ;
+        
+        // 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 ) ;
+        assertEquals( 0, tlvList.size() ) ;
+        
+        // decode value
+        tlv = decode( "01010101" ) ;
+        assertEquals( BERDecoderState.TAG, decoder.getState() ) ;
+        assertEquals( 1, tlvList.size() ) ;
+        assertNotNull( tlv.value ) ;
+        assertEquals( 0x0055, 0x00ff & ( int ) ( (byte[]) tlv.value)[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.value ) ;
+        assertEquals( 0x0055, 0x00ff & ( int ) ( (byte[]) tlv.value)[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.value ) ;
+        assertEquals( 0x0055, 0x00ff & ( int ) ( (byte[]) tlv.value)[0] ) ;
+
+        decode( "00000000" ) ;
+        decode( "00000000" ) ;
+        
+        assertEquals( 4, tlvList.size() ) ;
+        assertEquals( BERDecoderState.TAG, decoder.getState() ) ;
+    }
+}

Mime
View raw message