directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From elecha...@apache.org
Subject svn commit: r1038383 - in /directory/apacheds/trunk/kerberos-codec/src: main/java/org/apache/directory/shared/kerberos/codec/etypeInfo/ main/java/org/apache/directory/shared/kerberos/codec/etypeInfo2/ main/java/org/apache/directory/shared/kerberos/code...
Date Tue, 23 Nov 2010 23:30:48 GMT
Author: elecharny
Date: Tue Nov 23 23:30:47 2010
New Revision: 1038383

URL: http://svn.apache.org/viewvc?rev=1038383&view=rev
Log:
o Added the ETYPE-INFO2 gramamr
o Fxed some typoes in Javadocs

Added:
    directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/etypeInfo2/
    directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/etypeInfo2/ETypeInfo2Container.java
    directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/etypeInfo2/ETypeInfo2Grammar.java
    directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/etypeInfo2/ETypeInfo2StatesEnum.java
    directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/etypeInfo2/actions/
    directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/etypeInfo2/actions/AddETypeInfo2Entry.java
    directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/components/ETypeInfo2.java
    directory/apacheds/trunk/kerberos-codec/src/test/java/org/apache/directory/shared/kerberos/codec/EtypeInfo2DecoderTest.java
Modified:
    directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/etypeInfo/ETypeInfoContainer.java
    directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/components/ETypeInfo.java

Modified: directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/etypeInfo/ETypeInfoContainer.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/etypeInfo/ETypeInfoContainer.java?rev=1038383&r1=1038382&r2=1038383&view=diff
==============================================================================
--- directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/etypeInfo/ETypeInfoContainer.java
(original)
+++ directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/etypeInfo/ETypeInfoContainer.java
Tue Nov 23 23:30:47 2010
@@ -70,7 +70,7 @@ public class ETypeInfoContainer extends 
      * Add a EtypeInfoEntry Object into the list. It will be completed by the
      * KerberosDecoder.
      * 
-     * @param hostAddress The HostAddress to add.
+     * @param etypeInfoEntry The ETypeInfoEntry to add.
      */
     public void addEtypeInfoEntry( ETypeInfoEntry etypeInfoEntry )
     {

Added: directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/etypeInfo2/ETypeInfo2Container.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/etypeInfo2/ETypeInfo2Container.java?rev=1038383&view=auto
==============================================================================
--- directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/etypeInfo2/ETypeInfo2Container.java
(added)
+++ directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/etypeInfo2/ETypeInfo2Container.java
Tue Nov 23 23:30:47 2010
@@ -0,0 +1,79 @@
+/*
+ *  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.shared.kerberos.codec.etypeInfo2;
+
+import org.apache.directory.shared.asn1.ber.AbstractContainer;
+import org.apache.directory.shared.kerberos.components.ETypeInfo2;
+import org.apache.directory.shared.kerberos.components.ETypeInfo2Entry;
+
+
+/**
+ * The ETypeInfo container stores the ETYPE-INFO2 decoded by the Asn1Decoder.
+ * 
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public class ETypeInfo2Container extends AbstractContainer
+{
+    /** holds ETypeInfo2 */
+    private ETypeInfo2 etypeInfo2 = new ETypeInfo2();
+
+    /**
+     * Creates a new ETypeInfo2Container object.
+     */
+    public ETypeInfo2Container()
+    {
+        super();
+        this.stateStack = new int[1];
+        this.grammar = ETypeInfo2Grammar.getInstance();
+        setTransition( ETypeInfo2StatesEnum.START_STATE );
+    }
+
+
+    /**
+     * @return Returns the ETypeInfo2.
+     */
+    public ETypeInfo2 getETypeInfo2()
+    {
+        return etypeInfo2;
+    }
+
+    
+    /**
+     * Set a ETypeInfo2 Object into the container
+     * 
+     * @param etypeInfo2 The ETypeInfo2 to set.
+     */
+    public void setETypeInfo2( ETypeInfo2 etypeInfo2 )
+    {
+        this.etypeInfo2 = etypeInfo2;
+    }
+
+    
+    /**
+     * Add a EtypeInfo2Entry Object into the list. It will be completed by the
+     * KerberosDecoder.
+     * 
+     * @param etypeInfo2Entry The EtypeInfo2Entry to add.
+     */
+    public void addEtypeInfo2Entry( ETypeInfo2Entry etypeInfo2Entry )
+    {
+        etypeInfo2.addETypeInfo2Entry( etypeInfo2Entry );
+    }
+}

Added: directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/etypeInfo2/ETypeInfo2Grammar.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/etypeInfo2/ETypeInfo2Grammar.java?rev=1038383&view=auto
==============================================================================
--- directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/etypeInfo2/ETypeInfo2Grammar.java
(added)
+++ directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/etypeInfo2/ETypeInfo2Grammar.java
Tue Nov 23 23:30:47 2010
@@ -0,0 +1,93 @@
+/*
+ *  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.shared.kerberos.codec.etypeInfo2;
+
+
+import org.apache.directory.shared.asn1.ber.grammar.AbstractGrammar;
+import org.apache.directory.shared.asn1.ber.grammar.Grammar;
+import org.apache.directory.shared.asn1.ber.grammar.GrammarTransition;
+import org.apache.directory.shared.asn1.ber.tlv.UniversalTag;
+import org.apache.directory.shared.kerberos.codec.actions.CheckNotNullLength;
+import org.apache.directory.shared.kerberos.codec.etypeInfo2.actions.AddETypeInfo2Entry;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+/**
+ * This class implements the ETYPE-INFO2 structure. All the actions are declared
+ * in this class. As it is a singleton, these declaration are only done once. If
+ * an action is to be added or modified, this is where the work is to be done !
+ * 
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public final class ETypeInfo2Grammar extends AbstractGrammar
+{
+    /** The logger */
+    static final Logger LOG = LoggerFactory.getLogger( ETypeInfo2Grammar.class );
+
+    /** A speedup for logger */
+    static final boolean IS_DEBUG = LOG.isDebugEnabled();
+
+    /** The instance of grammar. ETypeInfo2Grammar is a singleton */
+    private static Grammar instance = new ETypeInfo2Grammar();
+
+
+    /**
+     * Creates a new ETypeInfo2Grammar object.
+     */
+    private ETypeInfo2Grammar()
+    {
+        setName( ETypeInfo2Grammar.class.getName() );
+
+        // Create the transitions table
+        super.transitions = new GrammarTransition[ETypeInfo2StatesEnum.LAST_ETYPE_INFO2_STATE.ordinal()][256];
+
+        // ============================================================================================
+        // ETYPE-INFO2 
+        // ============================================================================================
+        // --------------------------------------------------------------------------------------------
+        // Transition from ETYPE-INFO2 init to ETYPE-INFO2 SEQ
+        // --------------------------------------------------------------------------------------------
+        // ETYPE-INFO2-ENTRY         ::= SEQUENCE
+        super.transitions[ETypeInfo2StatesEnum.START_STATE.ordinal()][UniversalTag.SEQUENCE.getValue()]
= new GrammarTransition(
+            ETypeInfo2StatesEnum.START_STATE, ETypeInfo2StatesEnum.ETYPE_INFO2_SEQ_STATE,
UniversalTag.SEQUENCE.getValue(),
+            new CheckNotNullLength() );
+        
+        // --------------------------------------------------------------------------------------------
+        // Transition from ETYPE-INFO2 init to ETYPE-INFO2 SEQ
+        // --------------------------------------------------------------------------------------------
+        // ETYPE-INFO2-ENTRY         ::= SEQUENCE OF <ETYPE-INFO2-ENTRY>
+        // 
+        super.transitions[ETypeInfo2StatesEnum.ETYPE_INFO2_SEQ_STATE.ordinal()][UniversalTag.SEQUENCE.getValue()]
= new GrammarTransition(
+            ETypeInfo2StatesEnum.ETYPE_INFO2_SEQ_STATE, ETypeInfo2StatesEnum.ETYPE_INFO2_SEQ_STATE,
UniversalTag.SEQUENCE.getValue(),
+            new AddETypeInfo2Entry() );
+    }
+
+
+    /**
+     * Get the instance of this grammar
+     * 
+     * @return An instance on the ETYPE-INFO Grammar
+     */
+    public static Grammar getInstance()
+    {
+        return instance;
+    }
+}

Added: directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/etypeInfo2/ETypeInfo2StatesEnum.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/etypeInfo2/ETypeInfo2StatesEnum.java?rev=1038383&view=auto
==============================================================================
--- directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/etypeInfo2/ETypeInfo2StatesEnum.java
(added)
+++ directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/etypeInfo2/ETypeInfo2StatesEnum.java
Tue Nov 23 23:30:47 2010
@@ -0,0 +1,106 @@
+/*
+ *  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.shared.kerberos.codec.etypeInfo2;
+
+
+import org.apache.directory.shared.asn1.ber.grammar.Grammar;
+import org.apache.directory.shared.asn1.ber.grammar.States;
+import org.apache.directory.shared.kerberos.codec.KerberosMessageGrammar;
+
+
+/**
+ * This class store the ETYPE-INFO2 grammar's constants. It is also used for debugging
+ * purpose
+ * 
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public enum ETypeInfo2StatesEnum implements States
+{
+    // Start
+    START_STATE,                        // 0
+    
+    ETYPE_INFO2_SEQ_STATE,               // 1
+    
+    ETYPE_INFO2_ETYPE_INFO_ENTRY_STATE,  // 2
+    
+    // End
+    LAST_ETYPE_INFO2_STATE;              // 3
+
+    
+    /**
+     * Get the grammar name
+     * 
+     * @param grammar The grammar code
+     * @return The grammar name
+     */
+    public String getGrammarName( int grammar )
+    {
+        return "ETYPE_INFO2_GRAMMAR";
+    }
+
+
+    /**
+     * Get the grammar name
+     * 
+     * @param grammar The grammar class
+     * @return The grammar name
+     */
+    public String getGrammarName( Grammar grammar )
+    {
+        if ( grammar instanceof KerberosMessageGrammar )
+        {
+            return "ETYPE_INFO2_GRAMMAR";
+        }
+        else
+        {
+            return "UNKNOWN GRAMMAR";
+        }
+    }
+
+
+    /**
+     * Get the string representing the state
+     * 
+     * @param state The state number
+     * @return The String representing the state
+     */
+    public String getState( int state )
+    {
+        return ( ( state == LAST_ETYPE_INFO2_STATE.ordinal() ) ? "LAST_ETYPE_INFO2_STATE"
: name() );
+    }
+
+    
+    /**
+     * {@inheritDoc}
+     */
+    public boolean isEndState()
+    {
+        return this == LAST_ETYPE_INFO2_STATE;
+    }
+    
+    
+    /**
+     * {@inheritDoc}
+     */
+    public ETypeInfo2StatesEnum getStartState()
+    {
+        return START_STATE;
+    }
+}

Added: directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/etypeInfo2/actions/AddETypeInfo2Entry.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/etypeInfo2/actions/AddETypeInfo2Entry.java?rev=1038383&view=auto
==============================================================================
--- directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/etypeInfo2/actions/AddETypeInfo2Entry.java
(added)
+++ directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/etypeInfo2/actions/AddETypeInfo2Entry.java
Tue Nov 23 23:30:47 2010
@@ -0,0 +1,116 @@
+/*
+ *  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.shared.kerberos.codec.etypeInfo2.actions;
+
+
+import org.apache.directory.shared.asn1.ber.Asn1Container;
+import org.apache.directory.shared.asn1.ber.Asn1Decoder;
+import org.apache.directory.shared.asn1.ber.grammar.GrammarAction;
+import org.apache.directory.shared.asn1.ber.tlv.TLV;
+import org.apache.directory.shared.asn1.codec.DecoderException;
+import org.apache.directory.shared.i18n.I18n;
+import org.apache.directory.shared.kerberos.codec.KerberosMessageGrammar;
+import org.apache.directory.shared.kerberos.codec.etypeInfo2.ETypeInfo2Container;
+import org.apache.directory.shared.kerberos.codec.etypeInfo2Entry.ETypeInfo2EntryContainer;
+import org.apache.directory.shared.kerberos.components.ETypeInfo2Entry;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+/**
+ * The action used to add an ETypeInfo2Entry object
+ * 
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public class AddETypeInfo2Entry extends GrammarAction
+{
+    /** The logger */
+    private static final Logger LOG = LoggerFactory.getLogger( KerberosMessageGrammar.class
);
+
+    /** Speedup for logs */
+    private static final boolean IS_DEBUG = LOG.isDebugEnabled();
+
+
+    /**
+     * Instantiates a new AddETypeInfo2Entry action.
+     */
+    public AddETypeInfo2Entry()
+    {
+        super( "Add an ETypeInfo2Entry instance" );
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public void action( Asn1Container container ) throws DecoderException
+    {
+        ETypeInfo2Container etypeInfo2Container = ( ETypeInfo2Container ) container;
+
+        TLV tlv = etypeInfo2Container.getCurrentTLV();
+
+        // The Length should not be null
+        if ( tlv.getLength() == 0 )
+        {
+            LOG.error( I18n.err( I18n.ERR_04066 ) );
+
+            // This will generate a PROTOCOL_ERROR
+            throw new DecoderException( I18n.err( I18n.ERR_04067 ) );
+        }
+        
+        // Now, let's decode the ETYPE-INFO2-ENTRY
+        Asn1Decoder etypeInfo2EntryDecoder = new Asn1Decoder();
+        
+        ETypeInfo2EntryContainer etypeInfo2EntryContainer = new ETypeInfo2EntryContainer();
+        etypeInfo2EntryContainer.setStream( container.getStream() );
+        
+        // Compute the start position in the stream for the ETypeInfoEntry to decode : 
+        // We have to move back to the ETypeInfoEntry tag
+        container.rewind();
+
+        // Decode the ETypeInfo2Entry PDU
+        try
+        {
+            etypeInfo2EntryDecoder.decode( container.getStream(), etypeInfo2EntryContainer
);
+        }
+        catch ( DecoderException de )
+        {
+            throw de;
+        }
+        
+        // Update the expected length for the current TLV
+        tlv.setExpectedLength( tlv.getExpectedLength() - tlv.getLength() );
+
+        // Update the parent
+        container.updateParent();
+
+        // Store the ETypeInfoEntry in the container
+        ETypeInfo2Entry etypeInfo2Entry = etypeInfo2EntryContainer.getETypeInfo2Entry();
+        etypeInfo2Container.addEtypeInfo2Entry( etypeInfo2Entry );
+        
+
+        if ( IS_DEBUG )
+        {
+            LOG.debug( "ETYPE-INFO2-ENTRY added : {}", etypeInfo2Entry );
+        }
+        
+        container.setGrammarEndAllowed( true );
+    }
+}

Modified: directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/components/ETypeInfo.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/components/ETypeInfo.java?rev=1038383&r1=1038382&r2=1038383&view=diff
==============================================================================
--- directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/components/ETypeInfo.java
(original)
+++ directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/components/ETypeInfo.java
Tue Nov 23 23:30:47 2010
@@ -37,7 +37,7 @@ import org.slf4j.LoggerFactory;
 
 
 /**
- * Store a list of ETYPE-INFO-ENTRY.
+ * Store a list of ETYPE-INFO
  * 
  * The ASN.1 grammar is :
  * <pre>

Added: directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/components/ETypeInfo2.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/components/ETypeInfo2.java?rev=1038383&view=auto
==============================================================================
--- directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/components/ETypeInfo2.java
(added)
+++ directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/components/ETypeInfo2.java
Tue Nov 23 23:30:47 2010
@@ -0,0 +1,295 @@
+/*
+ *  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.shared.kerberos.components;
+
+
+import java.nio.BufferOverflowException;
+import java.nio.ByteBuffer;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+import org.apache.directory.server.i18n.I18n;
+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.codec.EncoderException;
+import org.apache.directory.shared.ldap.util.StringTools;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+/**
+ * Store a list of ETYPE-INFO2.
+ * 
+ * The ASN.1 grammar is :
+ * <pre>
+ * ETYPE-INFO2              ::= SEQUENCE SIZE (1..MAX) OF ETYPE-INFO2-ENTRY
+ *</pre>
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public class ETypeInfo2 extends AbstractAsn1Object
+{
+    /** The logger */
+    private static final Logger LOG = LoggerFactory.getLogger( ETypeInfo2.class );
+
+    /** Speedup for logs */
+    private static final boolean IS_DEBUG = LOG.isDebugEnabled();
+
+    /** List of all ETYPE-INFO2-ENTRY stored */
+    private List<ETypeInfo2Entry> etypeInfo2Entries;
+
+    // Storage for computed lengths
+    private transient int etypeInfo2Length;
+
+
+    /**
+     * Creates a new instance of ETypeInfo2.
+     */
+    public ETypeInfo2()
+    {
+        this.etypeInfo2Entries = new ArrayList<ETypeInfo2Entry>();
+    }
+    
+
+    /**
+     * Creates a new instance of ETypeInfo2.
+     *
+     * @param etypeInfo2Entries The associated etypeInfo2Entries
+     */
+    public ETypeInfo2( ETypeInfo2Entry[] etypeInfo2Entries )
+    {
+        if ( etypeInfo2Entries == null )
+        {
+            this.etypeInfo2Entries = new ArrayList<ETypeInfo2Entry>();
+        }
+        else
+        {
+            this.etypeInfo2Entries = Arrays.asList( etypeInfo2Entries );
+        }
+    }
+
+
+    /**
+     * Adds an {@link ETypeInfo2Entry} to the list
+     * @param etypeInfo2Entry The ETypeInfo2Entry to add
+     */
+    public void addETypeInfo2Entry( ETypeInfo2Entry etypeInfo2Entry )
+    {
+        etypeInfo2Entries.add( etypeInfo2Entry );
+    }
+
+
+    /**
+     * Returns true if this {@link ETypeInfo2Entry} contains a specified {@link ETypeInfo2Entry}.
+     *
+     * @param address The etypeInfo2Entry we are looking for in the existing list
+     * @return true if this {@link ETypeInfo2Entry} contains a specified {@link ETypeInfo2Entry}.
+     */
+    public boolean contains( ETypeInfo2Entry etypeInfo2Entry )
+    {
+        if ( etypeInfo2Entries != null )
+        {
+            return etypeInfo2Entries.contains( etypeInfo2Entry );
+        }
+
+        return false;
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public int hashCode()
+    {
+        int hash = 37;
+        
+        if ( etypeInfo2Entries != null )
+        {
+            hash = hash * 17 + etypeInfo2Entries.size();
+            
+            for ( ETypeInfo2Entry etypeInfo2Entry : etypeInfo2Entries )
+            {
+                hash = hash * 17 + etypeInfo2Entry.hashCode();
+            }
+        }
+        
+        return hash;
+    }
+
+
+    /**
+     * Returns true if two {@link ETypeInfo2} are equal.
+     *
+     * @param that The {@link ETypeInfo2} we want to compare with the current one
+     * @return true if two {@link ETypeInfo2} are equal.
+     */
+    public boolean equals( ETypeInfo2 that )
+    {
+        if ( that == null ) 
+        {
+            return false;
+        }
+        
+        // infoEntries can't be null after creation
+        if ( etypeInfo2Entries.size() != that.etypeInfo2Entries.size() )
+        {
+            return false;
+        }
+
+        for ( int i = 0; i < etypeInfo2Entries.size(); i++ )
+        {
+            if ( !etypeInfo2Entries.get( i ).equals( that.etypeInfo2Entries.get( i ) ) )
+            {
+                return false;
+            }
+        }
+
+        return true;
+    }
+
+
+    /**
+     * Returns the contained {@link ETypeInfo2Entry}s as an array.
+     *
+     * @return An array of {@link ETypeInfo2Entry}s.
+     */
+    public ETypeInfo2Entry[] getETypeInfo2Entries()
+    {
+        return etypeInfo2Entries.toArray( new ETypeInfo2Entry[0] );
+    }
+
+
+    /**
+     * Compute the ETypeInfo2 length
+     * <pre>
+     * ETypeInfo2 :
+     * 
+     * 0x30 L1 ETypeInfo2 sequence of ETypeInfo2Entry
+     *  |
+     *  +--> 0x30 L2[1] ETypeInfo2Entry[1]
+     *  |
+     *  +--> 0x30 L2[2] ETypeInfo2Entry[2]
+     *  |
+     *  ...
+     *  |
+     *  +--> 0x30 L2[n] ETypeInfo2Entry[n]
+     *        
+     *  where L1 = sum( L2[1], l2[2], ..., L2[n] )
+     * </pre>
+     */
+    public int computeLength()
+    {
+        // Compute the addresses length.
+        etypeInfo2Length = 0;
+
+        if ( ( etypeInfo2Entries != null ) && ( etypeInfo2Entries.size() != 0 ) )
+        {
+            for ( ETypeInfo2Entry info2Entry : etypeInfo2Entries )
+            {
+                int length = info2Entry.computeLength();
+                etypeInfo2Length += length;
+            }
+        }
+
+        return 1 + TLV.getNbBytes( etypeInfo2Length ) + etypeInfo2Length;
+    }
+
+
+    /**
+     * Encode the ETypeInfo2 message to a PDU. 
+     * <pre>
+     * ETypeInfo2 :
+     * 
+     * 0x30 LL
+     *   0x30 LL ETypeInfo2Entry[1] 
+     *   0x30 LL ETypeInfo2Entry[1]
+     *   ... 
+     *   0x30 LL ETypeInfo2Entry[1] 
+     * </pre>
+     * @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( I18n.err( I18n.ERR_148 ) );
+        }
+
+        try
+        {
+            // The ETypeInfo2Entry SEQ Tag
+            buffer.put( UniversalTag.SEQUENCE.getValue() );
+            buffer.put( TLV.getBytes( etypeInfo2Length ) );
+
+            // The hostAddress list, if it's not empty
+            if ( ( etypeInfo2Entries != null ) && ( etypeInfo2Entries.size() != 0
) )
+            {
+                for ( ETypeInfo2Entry info2Entry : etypeInfo2Entries )
+                {
+                    info2Entry.encode( buffer );
+                }
+            }
+        }
+        catch ( BufferOverflowException boe )
+        {
+            LOG.error( I18n.err( I18n.ERR_144, 1 + TLV.getNbBytes( etypeInfo2Length )
+                + etypeInfo2Length, buffer.capacity() ) );
+            throw new EncoderException( I18n.err( I18n.ERR_138 ) );
+        }
+
+        if ( IS_DEBUG )
+        {
+            LOG.debug( "ETYPE-INFO encoding : {}", StringTools.dumpBytes( buffer.array()
) );
+            LOG.debug( "ETYPE-INFO initial value : {}", toString() );
+        }
+
+        return buffer;
+    }
+
+
+    /**
+     * @see Object#toString()
+     */
+    public String toString()
+    {
+        StringBuilder sb = new StringBuilder();
+        boolean isFirst = true;
+
+        for ( ETypeInfo2Entry info2Entry : etypeInfo2Entries )
+        {
+            if ( isFirst )
+            {
+                isFirst = false;
+            }
+            else
+            {
+                sb.append( ", " );
+            }
+
+            sb.append( info2Entry.toString() );
+        }
+
+        return sb.toString();
+    }
+}

Added: directory/apacheds/trunk/kerberos-codec/src/test/java/org/apache/directory/shared/kerberos/codec/EtypeInfo2DecoderTest.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/kerberos-codec/src/test/java/org/apache/directory/shared/kerberos/codec/EtypeInfo2DecoderTest.java?rev=1038383&view=auto
==============================================================================
--- directory/apacheds/trunk/kerberos-codec/src/test/java/org/apache/directory/shared/kerberos/codec/EtypeInfo2DecoderTest.java
(added)
+++ directory/apacheds/trunk/kerberos-codec/src/test/java/org/apache/directory/shared/kerberos/codec/EtypeInfo2DecoderTest.java
Tue Nov 23 23:30:47 2010
@@ -0,0 +1,179 @@
+/*
+ *  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.shared.kerberos.codec;
+
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.fail;
+
+import java.nio.ByteBuffer;
+
+import org.apache.directory.junit.tools.Concurrent;
+import org.apache.directory.junit.tools.ConcurrentJunitRunner;
+import org.apache.directory.shared.asn1.ber.Asn1Container;
+import org.apache.directory.shared.asn1.ber.Asn1Decoder;
+import org.apache.directory.shared.asn1.codec.DecoderException;
+import org.apache.directory.shared.asn1.codec.EncoderException;
+import org.apache.directory.shared.kerberos.codec.etypeInfo2.ETypeInfo2Container;
+import org.apache.directory.shared.kerberos.codec.types.EncryptionType;
+import org.apache.directory.shared.kerberos.components.ETypeInfo2;
+import org.apache.directory.shared.kerberos.components.ETypeInfo2Entry;
+import org.apache.directory.shared.ldap.util.StringTools;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+
+/**
+ * Test the ETYPE-INFO2 decoder.
+ * 
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+@RunWith(ConcurrentJunitRunner.class)
+@Concurrent()
+public class EtypeInfo2DecoderTest
+{
+    /**
+     * Test the decoding of a ETYPE-INFO2
+     */
+    @Test
+    public void testETypeInfo2()
+    {
+        Asn1Decoder kerberosDecoder = new Asn1Decoder();
+
+        ByteBuffer stream = ByteBuffer.allocate( 0x20 );
+        
+        stream.put( new byte[]
+            { 
+              0x30, 0x1E,
+                0x30, 0x0D,
+                  (byte)0xA0, 0x03,                 // etype
+                    0x02, 0x01, 0x05,
+                  (byte)0xA1, 0x06,                 // salt
+                    0x1B, 0x04, 0x31, 0x32, 0x33, 0x34,
+                0x30, 0x0D,
+                  (byte)0xA0, 0x03,                 // etype
+                    0x02, 0x01, 0x05,
+                  (byte)0xA1, 0x06,                 // salt
+                    0x1B, 0x04, 0x35, 0x36, 0x37, 0x38
+            } );
+
+        String decodedPdu = StringTools.dumpBytes( stream.array() );
+        stream.flip();
+
+        // Allocate a ETypeInfo2 Container
+        Asn1Container etypeInfo2Container = new ETypeInfo2Container();
+        etypeInfo2Container.setStream( stream );
+
+        // Decode the ETypeInfo2 PDU
+        try
+        {
+            kerberosDecoder.decode( stream, etypeInfo2Container );
+        }
+        catch ( DecoderException de )
+        {
+            fail( de.getMessage() );
+        }
+
+        // Check the decoded ETypeInfo2
+        ETypeInfo2 etypeInfo2 = ( ( ETypeInfo2Container ) etypeInfo2Container ).getETypeInfo2();
+
+        assertEquals( 2, etypeInfo2.getETypeInfo2Entries().length );
+        
+        String[] expected = new String[]{ "1234", "5678" };
+        int i = 0;
+        
+        for ( ETypeInfo2Entry etypeInfo2Entry : etypeInfo2.getETypeInfo2Entries() )
+        {
+            assertEquals( EncryptionType.DES3_CBC_MD5, etypeInfo2Entry.getEType() );
+            assertEquals( expected[i], etypeInfo2Entry.getSalt() );
+            i++;
+        }
+
+        // Check the encoding
+        ByteBuffer bb = ByteBuffer.allocate( etypeInfo2.computeLength() );
+        
+        try
+        {
+            bb = etypeInfo2.encode( bb );
+    
+            // Check the length
+            assertEquals( 0x20, bb.limit() );
+    
+            String encodedPdu = StringTools.dumpBytes( bb.array() );
+    
+            assertEquals( encodedPdu, decodedPdu );
+        }
+        catch ( EncoderException ee )
+        {
+            fail();
+        }
+    }
+    
+    
+    /**
+     * Test the decoding of a ETypeInfo2 with nothing in it
+     */
+    @Test( expected = DecoderException.class)
+    public void testETypeInfo2Empty() throws DecoderException
+    {
+        Asn1Decoder kerberosDecoder = new Asn1Decoder();
+
+        ByteBuffer stream = ByteBuffer.allocate( 0x02 );
+        
+        stream.put( new byte[]
+            { 0x30, 0x00 } );
+
+        stream.flip();
+
+        // Allocate a ETypeInfo2 Container
+        Asn1Container etypeInfo2Container = new ETypeInfo2Container();
+
+        // Decode the ETypeInfo2 PDU
+        kerberosDecoder.decode( stream, etypeInfo2Container );
+        fail();
+    }
+    
+    
+    /**
+     * Test the decoding of a ETypeInfo2 with empty ETypeInfo2Entry in it
+     */
+    @Test( expected = DecoderException.class)
+    public void testETypeInfo2NoETypeInfo2Entry() throws DecoderException
+    {
+        Asn1Decoder kerberosDecoder = new Asn1Decoder();
+
+        ByteBuffer stream = ByteBuffer.allocate( 0x04 );
+        
+        stream.put( new byte[]
+            { 
+              0x30, 0x02,
+                (byte)0x30, 0x00                  // empty ETypeInfo2Entry
+            } );
+
+        stream.flip();
+
+        // Allocate a ETypeInfo2 Container
+        Asn1Container etypeInfo2Container = new ETypeInfo2Container();
+
+        // Decode the ETypeInfo2 PDU
+        kerberosDecoder.decode( stream, etypeInfo2Container );
+        fail();
+    }
+}



Mime
View raw message