directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From elecha...@apache.org
Subject svn commit: r588371 - in /directory/apacheds/branches/bigbang/kerberos-shared/src: main/java/org/apache/directory/server/kerberos/shared/io/decoder/ main/java/org/apache/directory/server/kerberos/shared/io/encoder/ main/java/org/apache/directory/server...
Date Thu, 25 Oct 2007 22:13:42 GMT
Author: elecharny
Date: Thu Oct 25 15:13:41 2007
New Revision: 588371

URL: http://svn.apache.org/viewvc?rev=588371&view=rev
Log:
Updated the TransitedEncoding class
Added a test for this class

Added:
    directory/apacheds/branches/bigbang/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/messages/value/types/TransitedEncodingType.java
    directory/apacheds/branches/bigbang/kerberos-shared/src/test/java/org/apache/directory/server/kerberos/shared/messages/value/TransitedEncodingTest.java
Modified:
    directory/apacheds/branches/bigbang/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/io/decoder/EncTicketPartDecoder.java
    directory/apacheds/branches/bigbang/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/io/encoder/TransitedEncodingEncoder.java
    directory/apacheds/branches/bigbang/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/messages/value/TransitedEncoding.java

Modified: directory/apacheds/branches/bigbang/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/io/decoder/EncTicketPartDecoder.java
URL: http://svn.apache.org/viewvc/directory/apacheds/branches/bigbang/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/io/decoder/EncTicketPartDecoder.java?rev=588371&r1=588370&r2=588371&view=diff
==============================================================================
--- directory/apacheds/branches/bigbang/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/io/decoder/EncTicketPartDecoder.java
(original)
+++ directory/apacheds/branches/bigbang/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/io/decoder/EncTicketPartDecoder.java
Thu Oct 25 15:13:41 2007
@@ -28,7 +28,7 @@
 import org.apache.directory.server.kerberos.shared.messages.components.EncTicketPartModifier;
 import org.apache.directory.server.kerberos.shared.messages.value.TicketFlags;
 import org.apache.directory.server.kerberos.shared.messages.value.TransitedEncoding;
-import org.apache.directory.server.kerberos.shared.messages.value.TransitedEncodingType;
+import org.apache.directory.server.kerberos.shared.messages.value.types.TransitedEncodingType;
 import org.apache.directory.shared.asn1.der.ASN1InputStream;
 import org.apache.directory.shared.asn1.der.DERApplicationSpecific;
 import org.apache.directory.shared.asn1.der.DERBitString;
@@ -84,7 +84,7 @@
     {
         EncTicketPartModifier modifier = new EncTicketPartModifier();
 
-        for ( Enumeration e = sequence.getObjects(); e.hasMoreElements(); )
+        for ( Enumeration<DEREncodable> e = sequence.getObjects(); e.hasMoreElements();
)
         {
             DERTaggedObject object = ( DERTaggedObject ) e.nextElement();
             int tag = object.getTagNo();
@@ -96,42 +96,52 @@
                     DERBitString tag0 = ( DERBitString ) derObject;
                     modifier.setFlags( new TicketFlags( tag0.getOctets() ) );
                     break;
+                    
                 case 1:
                     DERSequence tag1 = ( DERSequence ) derObject;
                     modifier.setSessionKey( EncryptionKeyDecoder.decode( tag1 ) );
                     break;
+                    
                 case 2:
                     DERGeneralString tag2 = ( DERGeneralString ) derObject;
                     modifier.setClientRealm( tag2.getString() );
                     break;
+                    
                 case 3:
                     DERSequence tag3 = ( DERSequence ) derObject;
                     modifier.setClientName( PrincipalNameDecoder.decode( tag3 ) );
                     break;
+                    
                 case 4:
                     DERSequence tag4 = ( DERSequence ) derObject;
                     modifier.setTransitedEncoding( decodeTransitedEncoding( tag4 ) );
                     break;
+                    
                 case 5:
                     DERGeneralizedTime tag5 = ( DERGeneralizedTime ) derObject;
                     modifier.setAuthTime( KerberosTimeDecoder.decode( tag5 ) );
                     break;
+                    
                 case 6:
                     DERGeneralizedTime tag6 = ( DERGeneralizedTime ) derObject;
                     modifier.setStartTime( KerberosTimeDecoder.decode( tag6 ) );
                     break;
+                    
                 case 7:
                     DERGeneralizedTime tag7 = ( DERGeneralizedTime ) derObject;
                     modifier.setEndTime( KerberosTimeDecoder.decode( tag7 ) );
                     break;
+                    
                 case 8:
                     DERGeneralizedTime tag8 = ( DERGeneralizedTime ) derObject;
                     modifier.setRenewTill( KerberosTimeDecoder.decode( tag8 ) );
                     break;
+                    
                 case 9:
                     DERSequence tag9 = ( DERSequence ) derObject;
                     modifier.setClientAddresses( HostAddressDecoder.decodeSequence( tag9
) );
                     break;
+                    
                 case 10:
                     DERSequence tag10 = ( DERSequence ) derObject;
                     modifier.setAuthorizationData( AuthorizationDataDecoder.decodeSequence(
tag10 ) );
@@ -153,7 +163,7 @@
         TransitedEncodingType type = TransitedEncodingType.NULL;
         byte[] contents = null;
 
-        for ( Enumeration e = sequence.getObjects(); e.hasMoreElements(); )
+        for ( Enumeration<DEREncodable> e = sequence.getObjects(); e.hasMoreElements();
)
         {
             DERTaggedObject object = ( DERTaggedObject ) e.nextElement();
             int tag = object.getTagNo();
@@ -165,6 +175,7 @@
                     DERInteger tag0 = ( DERInteger ) derObject;
                     type = TransitedEncodingType.getTypeByOrdinal( tag0.intValue() );
                     break;
+                    
                 case 1:
                     DEROctetString tag1 = ( DEROctetString ) derObject;
                     contents = tag1.getOctets();

Modified: directory/apacheds/branches/bigbang/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/io/encoder/TransitedEncodingEncoder.java
URL: http://svn.apache.org/viewvc/directory/apacheds/branches/bigbang/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/io/encoder/TransitedEncodingEncoder.java?rev=588371&r1=588370&r2=588371&view=diff
==============================================================================
--- directory/apacheds/branches/bigbang/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/io/encoder/TransitedEncodingEncoder.java
(original)
+++ directory/apacheds/branches/bigbang/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/io/encoder/TransitedEncodingEncoder.java
Thu Oct 25 15:13:41 2007
@@ -44,7 +44,7 @@
 
         DERSequence sequence = new DERSequence();
 
-        sequence.add( new DERTaggedObject( 0, DERInteger.valueOf( te.getType().getOrdinal()
) ) );
+        sequence.add( new DERTaggedObject( 0, DERInteger.valueOf( te.getTrType().getOrdinal()
) ) );
         sequence.add( new DERTaggedObject( 1, new DEROctetString( te.getContents() ) ) );
 
         return sequence;

Modified: directory/apacheds/branches/bigbang/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/messages/value/TransitedEncoding.java
URL: http://svn.apache.org/viewvc/directory/apacheds/branches/bigbang/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/messages/value/TransitedEncoding.java?rev=588371&r1=588370&r2=588371&view=diff
==============================================================================
--- directory/apacheds/branches/bigbang/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/messages/value/TransitedEncoding.java
(original)
+++ directory/apacheds/branches/bigbang/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/messages/value/TransitedEncoding.java
Thu Oct 25 15:13:41 2007
@@ -20,22 +20,63 @@
 package org.apache.directory.server.kerberos.shared.messages.value;
 
 
+import java.nio.BufferOverflowException;
+import java.nio.ByteBuffer;
+
+import org.apache.directory.server.kerberos.shared.messages.value.types.TransitedEncodingType;
+import org.apache.directory.shared.asn1.AbstractAsn1Object;
+import org.apache.directory.shared.asn1.ber.tlv.TLV;
+import org.apache.directory.shared.asn1.ber.tlv.UniversalTag;
+import org.apache.directory.shared.asn1.ber.tlv.Value;
+import org.apache.directory.shared.asn1.codec.EncoderException;
+import org.apache.directory.shared.ldap.util.StringTools;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
 /**
+ * The TransitedEncoding structure.
+ * 
+ * The ASN.1 grammar is :
+ * 
+ * -- encoded Transited field
+ * TransitedEncoding       ::= SEQUENCE {
+ *         tr-type         [0] Int32 -- must be registered --,
+ *         contents        [1] OCTET STRING
+ * }
+ * 
  * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
  * @version $Rev$, $Date$
  */
-public class TransitedEncoding
+public class TransitedEncoding extends AbstractAsn1Object
 {
-    private TransitedEncodingType type;
+    /** The logger */
+    private static final Logger log = LoggerFactory.getLogger( TransitedEncoding.class );
+
+    /** Speedup for logs */
+    private static final boolean IS_DEBUG = log.isDebugEnabled();
+
+    /** The transited type. One of :
+     * NULL
+     * DOMAIN_X500_COMPRESS
+     */
+    private TransitedEncodingType trType;
+
+    /** The transited data */
     private byte[] contents;
 
+    // Storage for computed lengths
+    private transient int trTypeLength;
+    private transient int contentsLength;
+    private transient int transitedEncodingLength;
+
 
     /**
      * Creates a new instance of TransitedEncoding.
      */
     public TransitedEncoding()
     {
-        type = TransitedEncodingType.NULL;
+        trType = TransitedEncodingType.NULL;
         contents = new byte[0];
     }
 
@@ -46,9 +87,9 @@
      * @param type
      * @param contents
      */
-    public TransitedEncoding( TransitedEncodingType type, byte[] contents )
+    public TransitedEncoding( TransitedEncodingType trType, byte[] contents )
     {
-        this.type = type;
+        this.trType = trType;
         this.contents = contents;
     }
 
@@ -62,6 +103,16 @@
     {
         return contents;
     }
+    
+    
+    /**
+     * Set the contents
+     * @param contents The contents
+     */
+    public void setContents( byte[] contents )
+    {
+        this.contents = contents;
+    }
 
 
     /**
@@ -69,8 +120,137 @@
      *
      * @return The {@link TransitedEncodingType}.
      */
-    public TransitedEncodingType getType()
+    public TransitedEncodingType getTrType()
+    {
+        return trType;
+    }
+    
+    
+    /**
+     * Set the transited encoding type
+     * @param trType The transited encoding type
+     */
+    public void setTrType( TransitedEncodingType trType )
     {
-        return type;
+        this.trType = trType;
+    }
+
+
+    /**
+     * Compute the TransitedEncoding length
+     * 
+     * TransitedEncoding :
+     * 
+     * 0x30 L1 TransitedEncoding
+     *  |
+     *  +--> 0xA0 L2 trType tag
+     *  |     |
+     *  |     +--> 0x02 L2-1 trType (int)
+     *  |
+     *  +--> 0xA1 L3 contents tag
+     *        |
+     *        +--> 0x04 L3-1 contents (OCTET STRING)
+     *        
+     *  where L1 = L2 + lenght(0xA0) + length(L2) +
+     *             L3 + lenght(0xA1) + length(L3) 
+     *  and
+     *  L2 = L2-1 + length(0x02) + length( L2-1) 
+     *  L3 = L3-1 + length(0x04) + length( L3-1) 
+     */
+    public int computeLength()
+    {
+        // Compute the trType. The Length will always be contained in 1 byte
+        trTypeLength = 1 + 1 + Value.getNbBytes( trType.getOrdinal() );
+        transitedEncodingLength = 1 + TLV.getNbBytes( trTypeLength ) + trTypeLength;
+
+        // Compute the keyValue
+        if ( contents == null )
+        {
+            contentsLength = 1 + 1;
+        }
+        else
+        {
+            contentsLength = 1 + TLV.getNbBytes( contents.length ) + contents.length;
+        }
+
+        transitedEncodingLength += 1 + TLV.getNbBytes( contentsLength ) + contentsLength;
+
+        // Compute the whole sequence length
+        int transitedEncodingSeqLength = 1 + Value.getNbBytes( transitedEncodingLength )
+ transitedEncodingLength;
+
+        return transitedEncodingSeqLength;
+
+    }
+
+
+    /**
+     * Encode the TransitedEncoding message to a PDU. 
+     * 
+     * TransitedEncoding :
+     * 
+     * 0x30 LL
+     *   0xA0 LL 
+     *     0x02 0x01 trType
+     *   0xA1 LL 
+     *     0x04 LL adData
+     * 
+     * @param buffer The buffer where to put the PDU. It should have been allocated
+     * before, with the right size.
+     * @return The constructed PDU.
+     */
+    public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
+    {
+        if ( buffer == null )
+        {
+            throw new EncoderException( "Cannot put a PDU in a null buffer !" );
+        }
+
+        try
+        {
+            // The AuthorizationDataEntry SEQ Tag
+            buffer.put( UniversalTag.SEQUENCE_TAG );
+            buffer.put( TLV.getBytes( transitedEncodingLength ) );
+
+            // The tr-type, first the tag, then the value
+            buffer.put( ( byte ) 0xA0 );
+            buffer.put( TLV.getBytes( trTypeLength ) );
+            Value.encode( buffer, trType.getOrdinal() );
+
+            // The contents, first the tag, then the value
+            buffer.put( ( byte ) 0xA1 );
+            buffer.put( TLV.getBytes( contentsLength ) );
+            Value.encode( buffer, contents );
+        }
+        catch ( BufferOverflowException boe )
+        {
+            log.error(
+                "Cannot encode the TransitedEncoding object, the PDU size is {} when only
{} bytes has been allocated",
+                1 + TLV.getNbBytes( transitedEncodingLength ) + transitedEncodingLength,
buffer.capacity() );
+            throw new EncoderException( "The PDU buffer size is too small !" );
+        }
+
+        if ( IS_DEBUG )
+        {
+            log.debug( "TransitedEncoding encoding : {}", StringTools.dumpBytes( buffer.array()
) );
+            log.debug( "TransitedEncoding initial value : {}", toString() );
+        }
+
+        return buffer;
+    }
+
+
+    /**
+     * @see Object#toString()
+     */
+    public String toString()
+    {
+        StringBuilder sb = new StringBuilder();
+
+        sb.append( "TransitedEncoding : {\n" );
+        sb.append( "    tr-type: " ).append( trType ).append( '\n' );
+
+        sb.append( "    contents: " ).append( StringTools.dumpBytes( contents ) ).append(
"\n}\n" );
+
+        return sb.toString();
     }
 }

Added: directory/apacheds/branches/bigbang/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/messages/value/types/TransitedEncodingType.java
URL: http://svn.apache.org/viewvc/directory/apacheds/branches/bigbang/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/messages/value/types/TransitedEncodingType.java?rev=588371&view=auto
==============================================================================
--- directory/apacheds/branches/bigbang/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/messages/value/types/TransitedEncodingType.java
(added)
+++ directory/apacheds/branches/bigbang/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/messages/value/types/TransitedEncodingType.java
Thu Oct 25 15:13:41 2007
@@ -0,0 +1,92 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you 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.directory.server.kerberos.shared.messages.value.types;
+
+
+/**
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev: 540371 $, $Date: 2007-05-22 02:00:43 +0200 (Tue, 22 May 2007) $
+ */
+public enum TransitedEncodingType
+{
+    /**
+     * Constant for the "null" transited encoding type.
+     */
+    NULL( 0 ),
+
+    /**
+     * Constant for the "Domain X500 compress" transited encoding type.
+     */
+    DOMAIN_X500_COMPRESS( 1 );
+
+
+    /**
+     * The value/code for the transited encoding type.
+     */
+    private final int ordinal;
+
+
+    /**
+     * Private constructor prevents construction outside of this class.
+     */
+    private TransitedEncodingType( int ordinal )
+    {
+        this.ordinal = ordinal;
+    }
+
+
+    /**
+     * Returns the transited encoding type when specified by its ordinal.
+     *
+     * @param type
+     * @return The transited encoding type.
+     */
+    public static TransitedEncodingType getTypeByOrdinal( int type )
+    {
+        switch ( type )
+        {
+            case 1  : return DOMAIN_X500_COMPRESS;
+            default : return NULL;
+        }
+    }
+
+
+    /**
+     * Returns the number associated with this transited encoding type.
+     *
+     * @return The transited encoding type ordinal.
+     */
+    public int getOrdinal()
+    {
+        return ordinal;
+    }
+
+    /**
+     * @see Object#toString()
+     */
+    public String toString()
+    {
+        switch ( this )
+        {
+            case DOMAIN_X500_COMPRESS : return "Domain X500 compress (1)";
+            default :                   return "null (0)";
+        }
+    }
+}

Added: directory/apacheds/branches/bigbang/kerberos-shared/src/test/java/org/apache/directory/server/kerberos/shared/messages/value/TransitedEncodingTest.java
URL: http://svn.apache.org/viewvc/directory/apacheds/branches/bigbang/kerberos-shared/src/test/java/org/apache/directory/server/kerberos/shared/messages/value/TransitedEncodingTest.java?rev=588371&view=auto
==============================================================================
--- directory/apacheds/branches/bigbang/kerberos-shared/src/test/java/org/apache/directory/server/kerberos/shared/messages/value/TransitedEncodingTest.java
(added)
+++ directory/apacheds/branches/bigbang/kerberos-shared/src/test/java/org/apache/directory/server/kerberos/shared/messages/value/TransitedEncodingTest.java
Thu Oct 25 15:13:41 2007
@@ -0,0 +1,80 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you 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.directory.server.kerberos.shared.messages.value;
+
+
+import java.nio.ByteBuffer;
+import java.util.Arrays;
+
+import org.apache.directory.server.kerberos.shared.messages.value.types.TransitedEncodingType;
+
+import junit.framework.TestCase;
+
+
+/**
+ * Test the TransitedEncoding encoding and decoding
+ * 
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev: 542147 $, $Date: 2007-05-28 10:14:21 +0200 (Mon, 28 May 2007) $
+ */
+public class TransitedEncodingTest extends TestCase
+{
+    public void testEncodingFast() throws Exception
+    {
+        TransitedEncoding te = new TransitedEncoding( TransitedEncodingType.DOMAIN_X500_COMPRESS,
new byte[]
+            { 0x01, 0x02, 0x03 } );
+
+        ByteBuffer encoded = ByteBuffer.allocate( te.computeLength() );
+
+        te.encode( encoded );
+
+        byte[] expectedResult = new byte[]
+            { 
+              0x30, 0x0c, 
+                ( byte ) 0xA0, 0x03, 
+                  0x02, 0x01, 0x01, 
+                ( byte ) 0xA1, 0x05, 
+                  0x04, 0x03, 0x01, 0x02, 0x03 
+            };
+
+        assertTrue( Arrays.equals( expectedResult, encoded.array() ) );
+    }
+
+
+    public void testEncodingNoStructureFast() throws Exception
+    {
+        TransitedEncoding te = new TransitedEncoding( TransitedEncodingType.DOMAIN_X500_COMPRESS,
null );
+
+        ByteBuffer encoded = ByteBuffer.allocate( te.computeLength() );
+
+        te.encode( encoded );
+
+        byte[] expectedResult = new byte[]
+            { 
+              0x30, 0x09, 
+                ( byte ) 0xA0, 0x03, 
+                  0x02, 0x01, 0x01, 
+                ( byte ) 0xA1, 0x02, 
+                  0x04, 0x00 
+            };
+
+        assertTrue( Arrays.equals( expectedResult, encoded.array() ) );
+    }
+}



Mime
View raw message