directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From akaras...@apache.org
Subject svn commit: rev 9496 - incubator/directory/snickers/branches/refactoring/ber/src/test/org/apache/snickers/ber
Date Mon, 15 Mar 2004 22:32:37 GMT
Author: akarasulu
Date: Mon Mar 15 14:32:36 2004
New Revision: 9496

Added:
   incubator/directory/snickers/branches/refactoring/ber/src/test/org/apache/snickers/ber/LengthTest.java
  (contents, props changed)
   incubator/directory/snickers/branches/refactoring/ber/src/test/org/apache/snickers/ber/TagTest.java
  (contents, props changed)
Modified:
   incubator/directory/snickers/branches/refactoring/ber/src/test/org/apache/snickers/ber/BERDecoderTest.java
   incubator/directory/snickers/branches/refactoring/ber/src/test/org/apache/snickers/ber/DefaultMutableTupleNodeTest.java
   incubator/directory/snickers/branches/refactoring/ber/src/test/org/apache/snickers/ber/LdapMessageTests.java
   incubator/directory/snickers/branches/refactoring/ber/src/test/org/apache/snickers/ber/TupleTreeDecoderTest.java
Log:
o Basically broke down the BERDecoder into separate stateful decoders which
  are used by the BERDecoder when it switches its state from reading a tag,
  length and on to a value.  The code is much easier to handle and several
  old bugs are no longer present.  The decoder overall works like a champion.

o Clover shows green all over and the code has been cleaned up along with
  internal documentation - javadocs.  

o Added equals comparison functionality to TLV TupleNodes.

We're ready to merge back this code now to the main trunk.




Modified: incubator/directory/snickers/branches/refactoring/ber/src/test/org/apache/snickers/ber/BERDecoderTest.java
==============================================================================
--- incubator/directory/snickers/branches/refactoring/ber/src/test/org/apache/snickers/ber/BERDecoderTest.java
(original)
+++ incubator/directory/snickers/branches/refactoring/ber/src/test/org/apache/snickers/ber/BERDecoderTest.java
Mon Mar 15 14:32:36 2004
@@ -17,12 +17,15 @@
 package org.apache.snickers.ber ;
 
 
-import java.nio.ByteBuffer;
+import java.nio.ByteBuffer ;
 
-import org.apache.commons.codec.stateful.DecoderMonitorAdapter;
 import org.apache.commons.lang.ArrayUtils ;
 import org.apache.commons.lang.RandomStringUtils ;
 
+import org.apache.commons.codec.stateful.DecoderCallback;
+import org.apache.commons.codec.stateful.DecoderMonitorAdapter ;
+import org.apache.commons.codec.stateful.StatefulDecoder;
+
 
 /**
  * Tests the decoder using various complext TLV decoding scenarios and performs
@@ -152,5 +155,108 @@
         // automatically set to top because after t1 is delivered top is 
         decoded = decode( t1 ) ; 
         assertTrue( decoded.equals( top ) ) ;
+    }
+    
+    
+    public void testDecodeOccurred()
+    {
+        try
+        {
+            decoder.decodeOccurred( null, null ) ;
+            fail( "should never get here due to exception being thrown" ) ;
+        }
+        catch ( IllegalArgumentException e )
+        {
+            assertNotNull( e ) ;
+        }
+    }
+    
+    
+    public void testFireTagDecoded() throws Exception
+    {
+        decoder.setDecoderMonitor( new BERMonitor() ) ;
+        String str0 = RandomStringUtils.randomAlphanumeric(20) ;
+        Tuple t0 = new Tuple( 2, str0.getBytes() ) ;
+        String str1 = RandomStringUtils.randomAlphanumeric(20) ;
+        Tuple t1 = new Tuple( 3, str1.getBytes() ) ;
+        Tuple top = new Tuple( 1, t0.size() + t1.size() ) ;
+        Tuple decoded = decode( t0 ) ; 
+        assertTrue( decoded.equals( t0 ) ) ;
+        
+        // automatically set to top because after t1 is delivered top is 
+        decoded = decode( t1 ) ; 
+        
+        assertTrue( decode( top ).equals( top ) ) ;
+    }
+
+
+    public void testFireTagDecoded2() throws Exception
+    {
+        decoder.setDecoderMonitor( null ) ;
+        String str0 = RandomStringUtils.randomAlphanumeric(20) ;
+        Tuple t0 = new Tuple( 2, str0.getBytes() ) ;
+        String str1 = RandomStringUtils.randomAlphanumeric(20) ;
+        Tuple t1 = new Tuple( 3, str1.getBytes() ) ;
+        Tuple top = new Tuple( 1, t0.size() + t1.size() ) ;
+        Tuple decoded = decode( t0 ) ; 
+        assertTrue( decoded.equals( t0 ) ) ;
+        
+        // automatically set to top because after t1 is delivered top is 
+        decoded = decode( t1 ) ; 
+        assertTrue( decode( top ).equals( top ) ) ;
+    }
+
+
+    public void testFireTagDecoded3() throws Exception
+    {
+        decoder.setDecoderMonitor( new BERMonitor() ) ;
+        decoder.setCallback( null ) ;
+        String str0 = RandomStringUtils.randomAlphanumeric(20) ;
+        Tuple t0 = new Tuple( 2, str0.getBytes() ) ;
+        String str1 = RandomStringUtils.randomAlphanumeric(20) ;
+        Tuple t1 = new Tuple( 3, str1.getBytes() ) ;
+        Tuple top = new Tuple( 1, t0.size() + t1.size() ) ;
+        Tuple decoded = decode( t0 ) ; 
+        
+        // automatically set to top because after t1 is delivered top is 
+        decoded = decode( t1 ) ; 
+        assertTrue( decode( top ).equals( top ) ) ;
+    }
+
+
+    public void testFireTagDecoded4() throws Exception
+    {
+        decoder.setDecoderMonitor( null ) ;
+        decoder.setCallback( null ) ;
+        String str0 = RandomStringUtils.randomAlphanumeric(20) ;
+        Tuple t0 = new Tuple( 2, str0.getBytes() ) ;
+        String str1 = RandomStringUtils.randomAlphanumeric(20) ;
+        Tuple t1 = new Tuple( 3, str1.getBytes() ) ;
+        Tuple top = new Tuple( 1, t0.size() + t1.size() ) ;
+        Tuple decoded = decode( t0 ) ; 
+        
+        // automatically set to top because after t1 is delivered top is 
+        decoded = decode( t1 ) ; 
+        assertTrue( decode( top ).equals( top ) ) ;
+    }
+
+
+    class BERMonitor implements BERDecoderMonitor
+    {
+        public void callbackOccured(StatefulDecoder decoder,
+                DecoderCallback cb, Object decoded) { }
+        
+        public void error(StatefulDecoder decoder, Exception exception) { }
+        
+        public void callbackSet(StatefulDecoder decoder, DecoderCallback oldcb,
+                DecoderCallback newcb) { }
+        
+        public void fatalError(StatefulDecoder decoder, Exception exception){}
+        
+        public void lengthDecoded(Tuple tlv, byte[] lengthData) { }
+        
+        public void tagDecoded(Tuple tlv, byte[] tagData) { }
+        
+        public void warning(StatefulDecoder decoder, Exception exception) { }
     }
 }

Modified: incubator/directory/snickers/branches/refactoring/ber/src/test/org/apache/snickers/ber/DefaultMutableTupleNodeTest.java
==============================================================================
--- incubator/directory/snickers/branches/refactoring/ber/src/test/org/apache/snickers/ber/DefaultMutableTupleNodeTest.java
(original)
+++ incubator/directory/snickers/branches/refactoring/ber/src/test/org/apache/snickers/ber/DefaultMutableTupleNodeTest.java
Mon Mar 15 14:32:36 2004
@@ -16,15 +16,17 @@
  */
 package org.apache.snickers.ber ;
 
-import java.nio.ByteBuffer;
-import java.util.ArrayList;
-import java.util.Enumeration;
-
-import javax.swing.tree.MutableTreeNode;
-
-import org.apache.commons.codec.stateful.DecoderCallback;
-import org.apache.commons.codec.stateful.StatefulDecoder;
-import org.apache.commons.lang.ArrayUtils;
+
+import java.nio.ByteBuffer ;
+import java.util.ArrayList ;
+import java.util.Enumeration ;
+
+import javax.swing.tree.MutableTreeNode ;
+
+import org.apache.commons.codec.stateful.DecoderCallback ;
+import org.apache.commons.codec.stateful.StatefulDecoder ;
+
+import org.apache.commons.lang.ArrayUtils ;
 
 
 /**
@@ -367,6 +369,115 @@
         assertEquals( decoded.getTuple(), top.getTuple()) ;
     }
 
+    
+    public void testEquals()
+    {
+        DefaultMutableTupleNode top = new DefaultMutableTupleNode() ;
+        DefaultMutableTupleNode middle = new DefaultMutableTupleNode() ;
+        DefaultMutableTupleNode bottom = new DefaultMutableTupleNode() ;
+        top.insert( (MutableTupleNode) middle, 0 ) ;
+        middle.setParent( (MutableTupleNode) top) ;
+        middle.insert( (MutableTupleNode) bottom, 0 ) ;
+        bottom.setParent( (MutableTupleNode) middle) ;
+        
+        Tuple middleTuple = new Tuple( 1, 3, TypeClass.APPLICATION  ) ;
+        Tuple topTuple = new Tuple ( 2, 5, TypeClass.APPLICATION ) ;
+        Tuple bottomTuple = new Tuple( 3, new byte[1]) ;
+        
+        bottomTuple.typeClass = TypeClass.UNIVERSAL ;
+        top.setTuple( topTuple ) ;
+        middle.setTuple( middleTuple ) ;
+        bottom.setTuple( bottomTuple ) ;
+
+        assertTrue( top.equals( top ) ) ;
+        
+        DefaultMutableTupleNode topClone = new DefaultMutableTupleNode() ;
+        topClone.setTuple( topTuple ) ;
+        assertFalse( top.equals(topClone)) ;
+
+        topClone = new DefaultMutableTupleNode() ;
+        topClone.setTuple( bottomTuple ) ;
+        assertFalse( top.equals(topClone)) ;
+        
+        topClone = new DefaultMutableTupleNode() ;
+        DefaultMutableTupleNode middleClone = new DefaultMutableTupleNode() ;
+        DefaultMutableTupleNode bottomClone = new DefaultMutableTupleNode() ;
+        topClone.insert( (MutableTupleNode) middleClone, 0 ) ;
+        middleClone.setParent( (MutableTupleNode) topClone) ;
+        middleClone.insert( (MutableTupleNode) bottomClone, 0 ) ;
+        bottomClone.setParent( (MutableTupleNode) middleClone) ;
+        
+        Tuple middleTupleClone = new Tuple( 1, 3, TypeClass.APPLICATION  ) ;
+        Tuple topTupleClone = new Tuple ( 2, 5, TypeClass.APPLICATION ) ;
+        Tuple bottomTupleClone= new Tuple( 3, new byte[1]) ;
+        
+        bottomTupleClone.typeClass = TypeClass.UNIVERSAL ;
+        topClone.setTuple( topTupleClone ) ;
+        middleClone.setTuple( middleTupleClone ) ;
+        bottomClone.setTuple( bottomTupleClone ) ;
+
+        assertTrue( bottom.equals( bottomClone ) ) ;
+        assertTrue( middle.equals( middleClone ) ) ;
+        assertTrue( top.equals( topClone ) ) ;
+     }
+    
+    
+    public void testEquals2()
+    {
+        DefaultMutableTupleNode top = new DefaultMutableTupleNode() ;
+        DefaultMutableTupleNode middle = new DefaultMutableTupleNode() ;
+        DefaultMutableTupleNode bottom = new DefaultMutableTupleNode() ;
+        top.insert( (MutableTupleNode) middle, 0 ) ;
+        middle.setParent( (MutableTupleNode) top) ;
+        middle.insert( (MutableTupleNode) bottom, 0 ) ;
+        bottom.setParent( (MutableTupleNode) middle) ;
+        
+        Tuple middleTuple = new Tuple( 1, 3, TypeClass.APPLICATION  ) ;
+        Tuple topTuple = new Tuple ( 2, 5, TypeClass.APPLICATION ) ;
+        Tuple bottomTuple = new Tuple( 3, new byte[1]) ;
+        
+        bottomTuple.typeClass = TypeClass.UNIVERSAL ;
+        top.setTuple( topTuple ) ;
+        middle.setTuple( middleTuple ) ;
+        bottom.setTuple( bottomTuple ) ;
+
+        assertTrue( top.equals( top ) ) ;
+
+        top.printDepthFirst(new StringBuffer(), 0 ) ;
+        
+        DefaultMutableTupleNode topClone = new DefaultMutableTupleNode() ;
+        topClone.setTuple( topTuple ) ;
+        assertFalse( top.equals(topClone)) ;
+
+        topClone = new DefaultMutableTupleNode() ;
+        topClone.setTuple( bottomTuple ) ;
+        assertFalse( top.equals(topClone)) ;
+        
+        topClone = new DefaultMutableTupleNode() ;
+        DefaultMutableTupleNode middleClone = new DefaultMutableTupleNode() ;
+        DefaultMutableTupleNode bottomClone = new DefaultMutableTupleNode() ;
+        topClone.insert( (MutableTupleNode) middleClone, 0 ) ;
+        middleClone.setParent( (MutableTupleNode) topClone) ;
+        middleClone.insert( (MutableTupleNode) bottomClone, 0 ) ;
+        bottomClone.setParent( (MutableTupleNode) middleClone) ;
+        
+        Tuple middleTupleClone = new Tuple( 1, 3, TypeClass.APPLICATION  ) ;
+        Tuple topTupleClone = new Tuple ( 2, 5, TypeClass.APPLICATION ) ;
+        Tuple bottomTupleClone= new Tuple( 3, new byte[1]) ;
+        
+        bottomTupleClone.typeClass = TypeClass.UNIVERSAL ;
+        topClone.setTuple( topTupleClone ) ;
+        middleClone.setTuple( bottomTupleClone ) ;
+        bottomClone.setTuple( middleTupleClone ) ;
+
+        assertFalse( bottom.equals( bottomClone ) ) ;
+        assertFalse( middle.equals( middleClone ) ) ;
+        assertFalse( top.equals( topClone ) ) ;
+        assertFalse( top.equals( new Object() ) ) ;
+        assertFalse( top.equals( null ) ) ;
+     }
+    
+    
     /*
      * Class to test for String toString()
      */
@@ -376,6 +487,7 @@
 
     public void testPrintDepthFirst()
     {
+        
     }
 
     public void testChildren()
@@ -502,5 +614,51 @@
     {
         DefaultMutableTupleNode node = new DefaultMutableTupleNode() ;
         node.setUserObject( new Tuple() ) ;
+    }
+    
+    
+    public void testAddFirst()
+    {
+        DefaultMutableTupleNode parent = new DefaultMutableTupleNode() ;
+        DefaultMutableTupleNode node = new DefaultMutableTupleNode() ;
+        DefaultMutableTupleNode end = new DefaultMutableTupleNode() ;
+        parent.addFront( end ) ;
+        end.setParent( (MutableTupleNode) parent) ;
+        parent.addFront( node ) ;
+        node.setParent( (MutableTupleNode) parent) ;
+        
+        Tuple primitive = new Tuple( 1, new byte[3] ) ;
+        Tuple constructed = new Tuple ( 2, TypeClass.APPLICATION ) ;
+        Tuple terminator = new Tuple( 0, true, ArrayUtils.EMPTY_BYTE_ARRAY) ;
+        terminator.typeClass = TypeClass.UNIVERSAL ;
+        parent.setTuple( constructed ) ;
+        node.setTuple( primitive ) ;
+        end.setTuple(terminator) ;
+
+        parent.toDepthFirstString() ;
+        parent.analyze() ;
+        assertFalse( parent.isLeaf() ) ;
+    }
+
+    
+    public void testAddLast()
+    {
+        DefaultMutableTupleNode parent = new DefaultMutableTupleNode() ;
+        DefaultMutableTupleNode node = new DefaultMutableTupleNode() ;
+        DefaultMutableTupleNode end = new DefaultMutableTupleNode() ;
+        parent.addFront( node ) ;
+        node.setParent( (MutableTupleNode) parent) ;
+        parent.addLast( end ) ;
+        end.setParent( (MutableTupleNode) parent) ;
+        
+        Tuple primitive = new Tuple( 1, new byte[3] ) ;
+        Tuple constructed = new Tuple ( 2, TypeClass.APPLICATION ) ;
+        Tuple terminator = new Tuple( 0, true, ArrayUtils.EMPTY_BYTE_ARRAY) ;
+        terminator.typeClass = TypeClass.UNIVERSAL ;
+        parent.setTuple( constructed ) ;
+        node.setTuple( primitive ) ;
+        end.setTuple(terminator) ;
+
+        assertFalse( parent.isLeaf() ) ;
     }
 }

Modified: incubator/directory/snickers/branches/refactoring/ber/src/test/org/apache/snickers/ber/LdapMessageTests.java
==============================================================================
--- incubator/directory/snickers/branches/refactoring/ber/src/test/org/apache/snickers/ber/LdapMessageTests.java
(original)
+++ incubator/directory/snickers/branches/refactoring/ber/src/test/org/apache/snickers/ber/LdapMessageTests.java
Mon Mar 15 14:32:36 2004
@@ -19,8 +19,13 @@
 
 import java.math.BigInteger ;
 
+import java.nio.ByteBuffer ;
+
 import javax.naming.directory.Attributes ;
 
+import org.apache.commons.codec.stateful.CallbackHistory ;
+
+import org.apache.ldap.common.message.Message ;
 import org.apache.ldap.common.message.ScopeEnum ;
 import org.apache.ldap.common.filter.PresenceNode ;
 import org.apache.ldap.common.message.AddRequestImpl ;
@@ -45,12 +50,63 @@
  */
 public class LdapMessageTests extends AbstractDecoderTestCase
 {
+    TupleTreeDecoder ttd = null ;
+    CallbackHistory history = null ;
+    
+    
     public LdapMessageTests()
     {
         super ( LdapMessageTests.class.getName() ) ;
     }
     
     
+    /* (non-Javadoc)
+     * @see org.apache.snickers.ber.AbstractDecoderTestCase#setUp()
+     */
+    protected void setUp() throws Exception
+    {
+        super.setUp();
+        
+        ttd = new TupleTreeDecoder() ;
+        history = new CallbackHistory( 1 ) ;
+        ttd.setCallback( history ) ;
+    }
+    
+    
+    /* (non-Javadoc)
+     * @see org.apache.snickers.ber.AbstractDecoderTestCase#tearDown()
+     */
+    protected void tearDown() throws Exception
+    {
+        super.tearDown();
+        
+        ttd = null ;
+        history = null ;
+    }
+    
+    
+    /**
+     * Creates a tuple node tree from a message.
+     * 
+     * @param msg the message to encode and decode
+     * @return
+     * @throws DecoderException
+     */
+    public DefaultMutableTupleNode roundTripTest( Message msg ) throws Exception
+    {
+        DefaultMutableTupleNode node = null ;
+        ByteBuffer buf1 = encode( msg ) ;
+        ttd.decode( buf1 ) ;
+        node = ( DefaultMutableTupleNode ) history.getMostRecent() ;
+        
+        ByteBuffer buf2 = ByteBuffer.allocate( node.size() ) ;
+        node.encode( buf2 ) ;
+        assertTrue( buf1.equals( buf2 ) ) ;
+        
+        return node ;
+    }
+    
+    
     public void testBindMessage() throws Exception
     {
         BindRequestImpl request = new BindRequestImpl( BigInteger.ONE ) ;
@@ -59,6 +115,7 @@
         request.setCredentials( "password".getBytes() ) ;
         request.setVersion3( true ) ;
         decode( request ) ;
+        roundTripTest( request ) ;
         assertFalse( tlvList.isEmpty() ) ;
     }
 
@@ -78,6 +135,7 @@
         
         request.setEntry( attrs ) ;
         decode( request ) ;
+        roundTripTest( request ) ;
         assertFalse( tlvList.isEmpty() ) ;
     }
 
@@ -87,6 +145,7 @@
         DeleteRequestImpl request = new DeleteRequestImpl( BigInteger.ONE ) ;
         request.setName( "uid=akarasulu,dc=example,dc=com" ) ;
         decode( request ) ;
+        roundTripTest( request ) ;
         assertFalse( tlvList.isEmpty() ) ;
     }
 
@@ -96,6 +155,7 @@
         AbandonRequestImpl request = new AbandonRequestImpl( BigInteger.ONE ) ;
         request.setAbandoned( new BigInteger( "3" ) ) ;
         decode( request ) ;
+        roundTripTest( request ) ;
         assertFalse( tlvList.isEmpty() ) ;
     }
 
@@ -107,6 +167,7 @@
         request.setAttributeId( "testattr" ) ;
         request.setName( "uid=akarasulu,dc=example,dc=com" ) ;
         decode( request ) ;
+        roundTripTest( request ) ;
         assertFalse( tlvList.isEmpty() ) ;
     }
 
@@ -117,6 +178,7 @@
         request.setOid( "1234.1234.1324" ) ;
         request.setPayload( "Hello World".getBytes() ) ;
         decode( request ) ;
+        roundTripTest( request ) ;
         assertFalse( tlvList.isEmpty() ) ;
     }
 
@@ -129,16 +191,109 @@
         request.setNewRdn( "uid=aok" ) ;
         request.setNewSuperior( "dc=example,dc=com" ) ;
         decode( request ) ;
+        roundTripTest( request ) ;
         assertFalse( tlvList.isEmpty() ) ;
     }
 
 
+    /* ------------------------------------------------------------------------
+     
+Here's what the modify message looks like:
+     
+LDAP Modify Operation without any modification items
+dn = "uid=akarasulu,dc=example,dc=com"
+message id = 17
+
+[
+0  -> 48  0x30, 0011 0000 (const) Universal id = 16 
+                Sequence and Sequence-of types
+1  -> 42  0x2a, 0100 0010 length = 42
+
+   [
+    2  -> 2   0x2,  0000 0010 (prim) Universal id = 2, Integer type
+    3  -> 1   0x1,  0000 0001 length = 1 
+    4  -> 17  0x11, 0001 0001 integer value = 17
+   ]
+
+   [
+    5  -> 102 0x66, 0110 0110 (const) Application id = 6
+                    LDAPv3 RFC 2251 defines APPLICATION 6 for ModifyRequests
+    6  -> 35  0x23, 0010 0011 length = 35
+       [
+           7  -> 4   0x4,  0000 0100  ( prim ) UNIVERSAL id = 4
+                           Octetstring type
+           8  -> 31  0x1f, 0001 1111  length = 31
+
+           Octetstring = "uid=akarasulu,dc=example,dc=com"
+
+           [117, 105, 100, 61, 97, 107, 97, 114, 97, 115, 117, 108, 117, 44, 
+            100, 99, 61, 101, 120, 97, 109, 112, 108, 101, 44, 100, 99, 61, 
+            99, 111, 109]
+
+
+            9  -> 117 0x75, 
+            10 -> 105 0x69, 
+            11 -> 100 0x64, 
+            12 -> 61  0x3d, 
+            13 -> 97  0x61, 
+            14 -> 107 0x6b, 
+            15 -> 97  0x61, 
+            16 -> 114 0x72, 
+            17 -> 97  0x61, 
+            18 -> 115 0x73, 
+            19 -> 117 0x75, 
+            20 -> 108 0x6c, 
+            21 -> 117 0x75, 
+            22 -> 44  0x2c, 
+            23 -> 100 0x64, 
+            24 -> 99  0x63, 
+            25 -> 61  0x3d, 
+            26 -> 101 0x65, 
+            27 -> 120 0x78, 
+            28 -> 97  0x61, 
+            29 -> 109 0x6d, 
+            30 -> 112 0x70, 
+            31 -> 108 0x6c, 
+            32 -> 101 0x65, 
+            33 -> 44  0x2c, 
+            34 -> 100 0x64, 
+            35 -> 99  0x63, 
+            36 -> 61  0x3d, 
+            37 -> 99  0x63, 
+            38 -> 111 0x6f, 
+            39 -> 109 0x6d,
+
+38    33    40 -> 48  0x30, 0011 0000 (const) Universal id = 16
+                            Sequence and Sequence-of types
+39    34    41 -> 0   0x0,  length = 0
+    
+        ]
+    
+    [
+40  42 -> -96 0xa0, 1010 0000 (prim) Context-specific id = 0
+                    "controls" part of the LDAP message Envelope
+                    is specified as context-specific [0] when no
+                    no type class name is explicitly given
+
+        LDAPMessage ::= SEQUENCE 
+        {
+            messageID INTEGER,
+            protocolOp CHOICE { ... },
+            controls [0] Controls OPTIONAL
+        }
+
+41  43 -> 0   0x0   length = 0
+    ]
+]
+     
+     ------------------------------------------------------------------------ */
     public void testModifyMessage() throws Exception
     {
         ModifyRequestImpl request = 
             new ModifyRequestImpl( new BigInteger( "17" ) ) ;
         request.setName( "uid=akarasulu,dc=example,dc=com" ) ;
         decode( request ) ;
+        roundTripTest( request ) ;
         assertFalse( tlvList.isEmpty() ) ;
     }
 
@@ -154,6 +309,7 @@
         request.setSizeLimit( BigInteger.ZERO ) ;
         request.setTimeLimit( BigInteger.ZERO ) ;
         decode( request ) ;
+        roundTripTest( request ) ;
         assertFalse( tlvList.isEmpty() ) ;
     }
 }

Added: incubator/directory/snickers/branches/refactoring/ber/src/test/org/apache/snickers/ber/LengthTest.java
==============================================================================
--- (empty file)
+++ incubator/directory/snickers/branches/refactoring/ber/src/test/org/apache/snickers/ber/LengthTest.java
Mon Mar 15 14:32:36 2004
@@ -0,0 +1,124 @@
+/*
+ *   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 ;
+
+
+import org.apache.commons.collections.primitives.ArrayByteList ;
+import org.apache.commons.lang.ArrayUtils;
+
+import junit.framework.TestCase ;
+
+
+/**
+ * Tests the Length class.
+ *
+ * @author <a href="mailto:directory-dev@incubator.apache.org">
+ * Apache Directory Project</a>
+ * @version $Rev$
+ */
+public class LengthTest extends TestCase
+{
+
+    public void testIsFixated()
+    {
+    }
+
+    public void testClear()
+    {
+    }
+
+    public void testFixate()
+    {
+    }
+
+    public void testAdd()
+    {
+    }
+
+    /*
+     * Class to test for int getLength()
+     */
+    public void testGetLength() throws Exception
+    {
+        ArrayByteList list = new ArrayByteList() ;
+        list.add( (byte) 0x1 ) ;
+        list.add( (byte) 0x1 ) ;
+        list.add( (byte) 0x1 ) ;
+        list.add( (byte) 0x1 ) ;
+        list.add( (byte) 0x1 ) ;
+        list.add( (byte) 0x1 ) ;
+        list.add( (byte) 0x1 ) ;
+        list.add( (byte) 0x1 ) ;
+        
+        try
+        {
+            Length.getLength( list ) ;
+            fail( "should fail before we get here" ) ;
+        }
+        catch ( Throwable t )
+        {
+            assertNotNull( t ) ;
+        }
+        
+        
+        list.clear() ;
+        list.add(( byte ) 0x7 ) ;
+        assertEquals( 7, Length.getLength( list ) ) ;
+    }
+
+    public void testSize()
+    {
+    }
+
+    /*
+     * Class to test for int getLength(ArrayByteList)
+     */
+    public void testGetLengthArrayByteList()
+    {
+    }
+
+    public void testGetOctets() throws Exception
+    {
+        byte[] bites = { (byte) 0xff, (byte) 0xff, (byte) 0x0f } ;
+        
+        Length length = new Length() ;
+        length.add( bites[0] ) ;
+        length.add( bites[1] ) ;
+        length.add( bites[2] ) ;
+        
+        assertTrue( ArrayUtils.isEquals( bites, length.getOctets() ) ) ;
+    }
+    
+    
+    public void testGetOctets2() throws Exception
+    {
+        byte[] bites = { (byte) 0x00, (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 ) ;
+        }
+    }
+}

Added: incubator/directory/snickers/branches/refactoring/ber/src/test/org/apache/snickers/ber/TagTest.java
==============================================================================
--- (empty file)
+++ incubator/directory/snickers/branches/refactoring/ber/src/test/org/apache/snickers/ber/TagTest.java
Mon Mar 15 14:32:36 2004
@@ -0,0 +1,254 @@
+/*
+ *   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 ;
+
+
+import org.apache.commons.lang.ArrayUtils ;
+import org.apache.commons.codec.binary.Binary ;
+import org.apache.commons.collections.primitives.ArrayByteList ;
+
+import junit.framework.TestCase ;
+
+
+/**
+ * Tests the BER utility functions.
+ *
+ * @author <a href="mailto:directory-dev@incubator.apache.org">
+ * Apache Directory Project</a>
+ * @version $Rev$
+ */
+public class TagTest extends TestCase
+{
+
+    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( (byte) 0 ) );
+    }
+    
+    public void testIsPrimitive() throws Exception
+    {
+        byte octet = Binary.BIT_5 ;
+        
+        assertFalse( Tag.isPrimitive( octet ) ) ;
+        assertFalse( Tag.isPrimitive( Binary.BIT_5 ) ) ;
+        
+        assertTrue( Tag.isPrimitive( 0 ) ) ;
+        assertTrue( Tag.isPrimitive( Binary.BIT_0 ) ) ;
+        assertTrue( Tag.isPrimitive( Binary.BIT_1 ) ) ;
+        assertTrue( Tag.isPrimitive( Binary.BIT_2 ) ) ;
+        assertTrue( Tag.isPrimitive( Binary.BIT_3 ) ) ;
+        assertTrue( Tag.isPrimitive( Binary.BIT_4 ) ) ;
+        assertTrue( Tag.isPrimitive( Binary.BIT_6 ) ) ;
+        assertTrue( Tag.isPrimitive( Binary.BIT_7 ) ) ;
+    }
+
+
+    public void testIsConstructed() throws Exception
+    {
+        byte octet = Binary.BIT_5 ;
+        
+        assertTrue( Tag.isConstructed( octet ) ) ;
+        assertTrue( Tag.isConstructed( Binary.BIT_5 ) ) ;
+        
+        assertFalse( Tag.isConstructed( 0 ) ) ;
+        assertFalse( Tag.isConstructed( Binary.BIT_0 ) ) ;
+        assertFalse( Tag.isConstructed( Binary.BIT_1 ) ) ;
+        assertFalse( Tag.isConstructed( Binary.BIT_2 ) ) ;
+        assertFalse( Tag.isConstructed( Binary.BIT_3 ) ) ;
+        assertFalse( Tag.isConstructed( Binary.BIT_4 ) ) ;
+        assertFalse( Tag.isConstructed( Binary.BIT_6 ) ) ;
+        assertFalse( Tag.isConstructed( Binary.BIT_7 ) ) ;
+    }
+    
+    
+    public void testGetTagId() 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 & Tag.LONG_MASK ) ;
+            octets[2] = ( byte ) ( ( ii >> 7 ) & Tag.LONG_MASK ) ;
+            
+            if ( ii < 16384 ) 
+            {
+                assertEquals( Tag.getTagId( octets ), ii ) ;
+            }
+            else
+            {
+                assertTrue( Tag.getTagId( octets ) != ii ) ;
+            }
+        }
+
+        octets = new byte[6] ;
+        octets[0] = 31 ;
+        octets[1] = 0 ; // shift 0
+        octets[2] = 0 ; // shift 7
+        octets[3] = 0 ; // shift 14
+        octets[4] = 0 ; // shift 21
+        octets[5] = 0 ; // shift 28
+        
+        for ( int ii = 16384 ; ii < 2100000 ; ii++ )
+        {
+            octets[1] = ( byte ) ( ii & Tag.LONG_MASK ) ;
+            octets[2] = ( byte ) ( ( ii >> 7 ) & Tag.LONG_MASK ) ;
+            octets[3] = ( byte ) ( ( ii >> 14 ) & Tag.LONG_MASK ) ;
+            
+            if ( ii < 2097152 ) 
+            {
+                assertEquals( Tag.getTagId( octets ), ii ) ;
+            }
+            else
+            {
+                assertTrue( Tag.getTagId( octets ) != ii ) ;
+            }
+        }
+        
+        ArrayByteList list = new ArrayByteList() ;
+        list.add( (byte)1) ;
+        assertEquals( 1, Tag.getTagId( list ) ) ;
+
+        list.add( (byte)1) ;
+        list.add( (byte)1) ;
+        list.add( (byte)1) ;
+        list.add( (byte)1) ;
+        list.add( (byte)1) ;
+        list.add( (byte)1) ;
+        list.add( (byte)1) ;
+        list.add( (byte)1) ;
+        try
+        {
+            Tag.getTagId( list ) ;
+            fail( "should fail before getting here" ) ;
+        }
+        catch ( Throwable t )
+        {
+            assertNotNull( t ) ;
+        }
+
+    
+        try
+        {
+            Tag.getTagId( new byte[56] ) ;
+            fail( "should fail before getting here" ) ;
+        }
+        catch ( Throwable t )
+        {
+            assertNotNull( t ) ;
+        }
+    }
+    
+    
+    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] ) ;
+        
+        assertTrue( ArrayUtils.isEquals( bites, tag.getOctets() ) ) ;
+    }
+    
+    
+    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 ) ;
+        }
+    }
+}

Modified: incubator/directory/snickers/branches/refactoring/ber/src/test/org/apache/snickers/ber/TupleTreeDecoderTest.java
==============================================================================
--- incubator/directory/snickers/branches/refactoring/ber/src/test/org/apache/snickers/ber/TupleTreeDecoderTest.java
(original)
+++ incubator/directory/snickers/branches/refactoring/ber/src/test/org/apache/snickers/ber/TupleTreeDecoderTest.java
Mon Mar 15 14:32:36 2004
@@ -24,14 +24,14 @@
 import java.nio.ByteBuffer ;
 
 import org.apache.commons.codec.stateful.DecoderCallback ;
-import org.apache.commons.codec.stateful.DecoderMonitorAdapter;
 import org.apache.commons.codec.stateful.StatefulDecoder ;
+import org.apache.commons.codec.stateful.DecoderMonitorAdapter ;
 
-import org.apache.commons.lang.ArrayUtils;
+import org.apache.commons.lang.ArrayUtils ;
 import org.apache.commons.lang.time.StopWatch ;
 
 import org.apache.ldap.common.message.MessageEncoder ;
-import org.apache.ldap.common.message.BindRequestImpl ;
+import org.apache.ldap.common.message.ModifyRequestImpl ;
 
 import junit.framework.TestCase ;
 
@@ -65,27 +65,36 @@
     public void testTTD2() throws Exception
     {
         TupleTreeDecoder decoder = new TupleTreeDecoder() ;
-        decoder.handleTuple(new Tuple( 1, ArrayUtils.EMPTY_BYTE_ARRAY )) ;
+        decoder.decode(ByteBuffer.wrap( 
+                        new Tuple( 1, ArrayUtils.EMPTY_BYTE_ARRAY ).encode())) ;
         decoder.decode( ByteBuffer.wrap( new Tuple( 1, 
                                 ArrayUtils.EMPTY_BYTE_ARRAY ).encode() ) ) ;
     }    
     
+
+    public void testTTD4() throws Exception
+    {
+        byte[] bites = new Tuple( 1, ArrayUtils.EMPTY_BYTE_ARRAY ).encode() ;
+        byte[] shorter = new byte[bites.length-1] ;
+        System.arraycopy( bites, 0, shorter, 0, shorter.length ) ;
+        
+        assertNull( TupleTreeDecoder.treeDecode( ByteBuffer.wrap( shorter ) ) );
+    }    
+    
+
     public void testTTD() throws Exception
     {
         ByteArrayOutputStream out = new ByteArrayOutputStream() ;
         
         // Setup the bind request
-        BindRequestImpl l_request = new BindRequestImpl( BigInteger.ONE ) ;
-        l_request.setName( "uid=akarasulu,dc=example,dc=com" ) ;
-        l_request.setSimple( true ) ;
-        l_request.setCredentials( "password".getBytes() ) ;
-        l_request.setVersion3( true ) ;
+        ModifyRequestImpl request = new ModifyRequestImpl( new BigInteger("17") ) ;
+        request.setName( "uid=akarasulu,dc=example,dc=com" ) ;
         
         // Setup the ASN1 Enoder
-        MessageEncoder l_encoder = new MessageEncoder();
+        MessageEncoder encoder = new MessageEncoder();
  
         // Send encoded request to server
-        l_encoder.encode( null, out, l_request );
+        encoder.encode( null, out, request );
         
         StopWatch watch = new StopWatch() ;
         watch.start() ;
@@ -93,6 +102,7 @@
         decoder.setCallback( this ) ;
         decoder.decode( ByteBuffer.wrap( out.toByteArray() ) ) ;
         watch.stop() ;
+        
         System.out.println( "time for decode = " + watch.getTime() ) ;
         
         StringBuffer buf = new StringBuffer() ;

Mime
View raw message