directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From elecha...@apache.org
Subject svn commit: r1039348 - in /directory/apacheds/trunk/kerberos-codec/src: main/java/org/apache/directory/shared/kerberos/codec/methodData/ main/java/org/apache/directory/shared/kerberos/codec/methodData/actions/ main/java/org/apache/directory/shared/kerb...
Date Fri, 26 Nov 2010 13:34:42 GMT
Author: elecharny
Date: Fri Nov 26 13:34:41 2010
New Revision: 1039348

URL: http://svn.apache.org/viewvc?rev=1039348&view=rev
Log:
o Added the METHOD-DATA grammar
o Renamed a class

Added:
    directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/methodData/
    directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/methodData/MethodDataContainer.java
    directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/methodData/MethodDataGrammar.java
    directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/methodData/MethodDataStatesEnum.java
    directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/methodData/actions/
    directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/methodData/actions/AddPaData.java
    directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/components/MethodData.java
    directory/apacheds/trunk/kerberos-codec/src/test/java/org/apache/directory/shared/kerberos/codec/ETypeInfo2DecoderTest.java
      - copied, changed from r1039334, directory/apacheds/trunk/kerberos-codec/src/test/java/org/apache/directory/shared/kerberos/codec/EtypeInfo2DecoderTest.java
    directory/apacheds/trunk/kerberos-codec/src/test/java/org/apache/directory/shared/kerberos/codec/MethodDataDecoderTest.java
Removed:
    directory/apacheds/trunk/kerberos-codec/src/test/java/org/apache/directory/shared/kerberos/codec/EtypeInfo2DecoderTest.java

Added: directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/methodData/MethodDataContainer.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/methodData/MethodDataContainer.java?rev=1039348&view=auto
==============================================================================
--- directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/methodData/MethodDataContainer.java
(added)
+++ directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/methodData/MethodDataContainer.java
Fri Nov 26 13:34:41 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.methodData;
+
+import org.apache.directory.shared.asn1.ber.AbstractContainer;
+import org.apache.directory.shared.kerberos.components.MethodData;
+import org.apache.directory.shared.kerberos.components.PaData;
+
+
+/**
+ * The METHOD-DATA container stores the METHOD-DATA decoded by the Asn1Decoder.
+ * 
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public class MethodDataContainer extends AbstractContainer
+{
+    /** holds MethodData */
+    private MethodData methodData = new MethodData();
+
+    /**
+     * Creates a new MethodDataContainer object.
+     */
+    public MethodDataContainer()
+    {
+        super();
+        this.stateStack = new int[1];
+        this.grammar = MethodDataGrammar.getInstance();
+        setTransition( MethodDataStatesEnum.START_STATE );
+    }
+
+
+    /**
+     * @return Returns the MethodData.
+     */
+    public MethodData getMethodData()
+    {
+        return methodData;
+    }
+
+    
+    /**
+     * Set a MethodData Object into the container
+     * 
+     * @param methodData The MethodData to set.
+     */
+    public void setMethodData( MethodData methodData )
+    {
+        this.methodData = methodData;
+    }
+
+    
+    /**
+     * Add a PaData Object into the list. It will be completed by the
+     * KerberosDecoder.
+     * 
+     * @param paData The paData to add.
+     */
+    public void addPaData( PaData paData )
+    {
+        methodData.addPaData( paData );
+    }
+}

Added: directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/methodData/MethodDataGrammar.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/methodData/MethodDataGrammar.java?rev=1039348&view=auto
==============================================================================
--- directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/methodData/MethodDataGrammar.java
(added)
+++ directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/methodData/MethodDataGrammar.java
Fri Nov 26 13:34:41 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.methodData;
+
+
+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.methodData.actions.AddPaData;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+/**
+ * This class implements the METHOD-DATA 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 MethodDataGrammar extends AbstractGrammar
+{
+    /** The logger */
+    static final Logger LOG = LoggerFactory.getLogger( MethodDataGrammar.class );
+
+    /** A speedup for logger */
+    static final boolean IS_DEBUG = LOG.isDebugEnabled();
+
+    /** The instance of grammar. MethodDataGrammar is a singleton */
+    private static Grammar instance = new MethodDataGrammar();
+
+
+    /**
+     * Creates a new MethodDataGrammar object.
+     */
+    private MethodDataGrammar()
+    {
+        setName( MethodDataGrammar.class.getName() );
+
+        // Create the transitions table
+        super.transitions = new GrammarTransition[MethodDataStatesEnum.LAST_METHOD_DATA_STATE.ordinal()][256];
+
+        // ============================================================================================
+        // METHOD-DATA
+        // ============================================================================================
+        // --------------------------------------------------------------------------------------------
+        // Transition from METHOD-DATA init to METHOD-DATA SEQ
+        // --------------------------------------------------------------------------------------------
+        // METHOD-DATA         ::= SEQUENCE
+        super.transitions[MethodDataStatesEnum.START_STATE.ordinal()][UniversalTag.SEQUENCE.getValue()]
= new GrammarTransition(
+            MethodDataStatesEnum.START_STATE, MethodDataStatesEnum.METHOD_DATA_SEQ_STATE,
UniversalTag.SEQUENCE.getValue(),
+            new CheckNotNullLength() );
+        
+        // --------------------------------------------------------------------------------------------
+        // Transition from METHOD-DATA SEQ to PA-DATA
+        // --------------------------------------------------------------------------------------------
+        // METHOD-DATA         ::= SEQUENCE OF <PA-DATA>
+        // 
+        super.transitions[MethodDataStatesEnum.METHOD_DATA_SEQ_STATE.ordinal()][UniversalTag.SEQUENCE.getValue()]
= new GrammarTransition(
+            MethodDataStatesEnum.METHOD_DATA_SEQ_STATE, MethodDataStatesEnum.METHOD_DATA_SEQ_STATE,
UniversalTag.SEQUENCE.getValue(),
+            new AddPaData() );
+    }
+
+
+    /**
+     * Get the instance of this grammar
+     * 
+     * @return An instance on the METHOD-DATA Grammar
+     */
+    public static Grammar getInstance()
+    {
+        return instance;
+    }
+}

Added: directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/methodData/MethodDataStatesEnum.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/methodData/MethodDataStatesEnum.java?rev=1039348&view=auto
==============================================================================
--- directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/methodData/MethodDataStatesEnum.java
(added)
+++ directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/methodData/MethodDataStatesEnum.java
Fri Nov 26 13:34:41 2010
@@ -0,0 +1,104 @@
+/*
+ *  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.methodData;
+
+
+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 METHOD-DATA grammar's constants. It is also used for debugging
+ * purpose
+ * 
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public enum MethodDataStatesEnum implements States
+{
+    // Start
+    START_STATE,                        // 0
+    
+    METHOD_DATA_SEQ_STATE,               // 1
+    
+    // End
+    LAST_METHOD_DATA_STATE;              // 2
+
+    
+    /**
+     * Get the grammar name
+     * 
+     * @param grammar The grammar code
+     * @return The grammar name
+     */
+    public String getGrammarName( int grammar )
+    {
+        return "METHOD_DATA_GRAMMAR";
+    }
+
+
+    /**
+     * Get the grammar name
+     * 
+     * @param grammar The grammar class
+     * @return The grammar name
+     */
+    public String getGrammarName( Grammar grammar )
+    {
+        if ( grammar instanceof KerberosMessageGrammar )
+        {
+            return "METHOD_DATA_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_METHOD_DATA_STATE.ordinal() ) ? "LAST_METHOD_DATA_STATE"
: name() );
+    }
+
+    
+    /**
+     * {@inheritDoc}
+     */
+    public boolean isEndState()
+    {
+        return this == LAST_METHOD_DATA_STATE;
+    }
+    
+    
+    /**
+     * {@inheritDoc}
+     */
+    public MethodDataStatesEnum getStartState()
+    {
+        return START_STATE;
+    }
+}

Added: directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/methodData/actions/AddPaData.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/methodData/actions/AddPaData.java?rev=1039348&view=auto
==============================================================================
--- directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/methodData/actions/AddPaData.java
(added)
+++ directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/methodData/actions/AddPaData.java
Fri Nov 26 13:34:41 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.methodData.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.methodData.MethodDataContainer;
+import org.apache.directory.shared.kerberos.codec.padata.PaDataContainer;
+import org.apache.directory.shared.kerberos.components.PaData;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+/**
+ * The action used to add an PA-DATA object
+ * 
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public class AddPaData 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 AddPaData action.
+     */
+    public AddPaData()
+    {
+        super( "Add a PA-DATA instance" );
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public void action( Asn1Container container ) throws DecoderException
+    {
+        MethodDataContainer methodDataContainer = ( MethodDataContainer ) container;
+
+        TLV tlv = methodDataContainer.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 PA-DATA
+        Asn1Decoder paDataDecoder = new Asn1Decoder();
+        
+        PaDataContainer paDataContainer = new PaDataContainer();
+        paDataContainer.setStream( container.getStream() );
+        
+        // Compute the start position in the stream for the PA-DATA to decode : 
+        // We have to move back to the PA-DATA tag
+        container.rewind();
+
+        // Decode the PA-DATA PDU
+        try
+        {
+            paDataDecoder.decode( container.getStream(), paDataContainer );
+        }
+        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 PA-DATA in the container
+        PaData paData = paDataContainer.getPaData();
+        methodDataContainer.addPaData( paData );
+        
+
+        if ( IS_DEBUG )
+        {
+            LOG.debug( "PA-DATA added : {}", paData );
+        }
+        
+        container.setGrammarEndAllowed( true );
+    }
+}

Added: directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/components/MethodData.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/components/MethodData.java?rev=1039348&view=auto
==============================================================================
--- directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/components/MethodData.java
(added)
+++ directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/components/MethodData.java
Fri Nov 26 13:34:41 2010
@@ -0,0 +1,278 @@
+/*
+ *  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.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 METHOD-DATA
+ * 
+ * The ASN.1 grammar is :
+ * <pre>
+ * METHOD-DATA     ::= SEQUENCE OF <PA-DATA>
+ *</pre>
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public class MethodData extends AbstractAsn1Object
+{
+    /** The logger */
+    private static final Logger LOG = LoggerFactory.getLogger( MethodData.class );
+
+    /** Speedup for logs */
+    private static final boolean IS_DEBUG = LOG.isDebugEnabled();
+
+    /** List of all PA-DATA stored */
+    private List<PaData> paDatas;
+
+    // Storage for computed lengths
+    private transient int methodDataLength;
+
+
+    /**
+     * Creates a new instance of MethodData.
+     */
+    public MethodData()
+    {
+        this.paDatas = new ArrayList<PaData>();
+    }
+    
+
+    /**
+     * Adds an {@link PaData} to the list
+     * @param paData The PaData to add
+     */
+    public void addPaData( PaData paData )
+    {
+        paDatas.add( paData );
+    }
+
+
+    /**
+     * Returns true if this {@link PaData} contains a specified {@link PaData}.
+     *
+     * @param address The paData we are looking for in the existing list
+     * @return true if this {@link PaData} contains a specified {@link PaData}.
+     */
+    public boolean contains( PaData paData )
+    {
+        if ( paDatas != null )
+        {
+            return paDatas.contains( paData );
+        }
+
+        return false;
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public int hashCode()
+    {
+        int hash = 37;
+        
+        if ( paDatas != null )
+        {
+            hash = hash * 17 + paDatas.size();
+            
+            for ( PaData paData : paDatas )
+            {
+                hash = hash * 17 + paData.hashCode();
+            }
+        }
+        
+        return hash;
+    }
+
+
+    /**
+     * Returns true if two {@link MethodData} are equal.
+     *
+     * @param that The {@link MethodData} we want to compare with the current one
+     * @return true if two {@link MethodData} are equal.
+     */
+    public boolean equals( MethodData that )
+    {
+        if ( that == null ) 
+        {
+            return false;
+        }
+        
+        // infoEntries can't be null after creation
+        if ( paDatas.size() != that.paDatas.size() )
+        {
+            return false;
+        }
+
+        for ( int i = 0; i < paDatas.size(); i++ )
+        {
+            if ( !paDatas.get( i ).equals( that.paDatas.get( i ) ) )
+            {
+                return false;
+            }
+        }
+
+        return true;
+    }
+
+
+    /**
+     * Returns the contained {@link PaData}s as an array.
+     *
+     * @return An array of {@link PaData}s.
+     */
+    public PaData[] getPaDatas()
+    {
+        return paDatas.toArray( new PaData[0] );
+    }
+
+
+    /**
+     * Compute the METHOD-DATA length
+     * <pre>
+     * METHOD-DATA :
+     * 
+     * 0x30 L1 METHOD-DATA sequence of PA-DATA
+     *  |
+     *  +--> 0x30 L2[1] PA-DATA[1]
+     *  |
+     *  +--> 0x30 L2[2] PA-DATA[2]
+     *  |
+     *  ...
+     *  |
+     *  +--> 0x30 L2[n] PA-DATA[n]
+     *        
+     *  where L1 = sum( L2[1], l2[2], ..., L2[n] )
+     * </pre>
+     */
+    public int computeLength()
+    {
+        // Compute the PA-DATA length.
+        methodDataLength = 0;
+
+        if ( ( paDatas != null ) && ( paDatas.size() != 0 ) )
+        {
+            for ( PaData paData : paDatas )
+            {
+                int length = paData.computeLength();
+                methodDataLength += length;
+            }
+        }
+
+        return 1 + TLV.getNbBytes( methodDataLength ) + methodDataLength;
+    }
+
+
+    /**
+     * Encode the METHOD-DATA message to a PDU. 
+     * <pre>
+     * METHOD-DATA :
+     * 
+     * 0x30 LL
+     *   0x30 LL PA-DATA[1] 
+     *   0x30 LL PA-DATA[1]
+     *   ... 
+     *   0x30 LL PA-DATA[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 METHOD-DATA SEQ Tag
+            buffer.put( UniversalTag.SEQUENCE.getValue() );
+            buffer.put( TLV.getBytes( methodDataLength ) );
+
+            // The PA-DATA list, if it's not empty
+            if ( ( paDatas != null ) && ( paDatas.size() != 0 ) )
+            {
+                for ( PaData paData : paDatas )
+                {
+                    paData.encode( buffer );
+                }
+            }
+        }
+        catch ( BufferOverflowException boe )
+        {
+            LOG.error( I18n.err( I18n.ERR_144, 1 + TLV.getNbBytes( methodDataLength )
+                + methodDataLength, buffer.capacity() ) );
+            throw new EncoderException( I18n.err( I18n.ERR_138 ) );
+        }
+
+        if ( IS_DEBUG )
+        {
+            LOG.debug( "METHOD-DATA encoding : {}", StringTools.dumpBytes( buffer.array()
) );
+            LOG.debug( "METHOD-DATA initial value : {}", toString() );
+        }
+
+        return buffer;
+    }
+
+
+    /**
+     * @see Object#toString()
+     */
+    public String toString()
+    {
+        StringBuilder sb = new StringBuilder();
+        boolean isFirst = true;
+        
+        sb.append( "METHOD-DATA : " );
+
+        for ( PaData paData : paDatas )
+        {
+            if ( isFirst )
+            {
+                isFirst = false;
+            }
+            else
+            {
+                sb.append( ", " );
+            }
+
+            sb.append( paData.toString() );
+        }
+
+        return sb.toString();
+    }
+}

Copied: directory/apacheds/trunk/kerberos-codec/src/test/java/org/apache/directory/shared/kerberos/codec/ETypeInfo2DecoderTest.java
(from r1039334, 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?p2=directory/apacheds/trunk/kerberos-codec/src/test/java/org/apache/directory/shared/kerberos/codec/ETypeInfo2DecoderTest.java&p1=directory/apacheds/trunk/kerberos-codec/src/test/java/org/apache/directory/shared/kerberos/codec/EtypeInfo2DecoderTest.java&r1=1039334&r2=1039348&rev=1039348&view=diff
==============================================================================
--- directory/apacheds/trunk/kerberos-codec/src/test/java/org/apache/directory/shared/kerberos/codec/EtypeInfo2DecoderTest.java
(original)
+++ directory/apacheds/trunk/kerberos-codec/src/test/java/org/apache/directory/shared/kerberos/codec/ETypeInfo2DecoderTest.java
Fri Nov 26 13:34:41 2010
@@ -47,7 +47,7 @@ import org.junit.runner.RunWith;
  */
 @RunWith(ConcurrentJunitRunner.class)
 @Concurrent()
-public class EtypeInfo2DecoderTest
+public class ETypeInfo2DecoderTest
 {
     /**
      * Test the decoding of a ETYPE-INFO2

Added: directory/apacheds/trunk/kerberos-codec/src/test/java/org/apache/directory/shared/kerberos/codec/MethodDataDecoderTest.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/kerberos-codec/src/test/java/org/apache/directory/shared/kerberos/codec/MethodDataDecoderTest.java?rev=1039348&view=auto
==============================================================================
--- directory/apacheds/trunk/kerberos-codec/src/test/java/org/apache/directory/shared/kerberos/codec/MethodDataDecoderTest.java
(added)
+++ directory/apacheds/trunk/kerberos-codec/src/test/java/org/apache/directory/shared/kerberos/codec/MethodDataDecoderTest.java
Fri Nov 26 13:34:41 2010
@@ -0,0 +1,182 @@
+/*
+ *  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.assertTrue;
+import static org.junit.Assert.fail;
+
+import java.nio.ByteBuffer;
+import java.util.Arrays;
+
+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.methodData.MethodDataContainer;
+import org.apache.directory.shared.kerberos.codec.types.PaDataType;
+import org.apache.directory.shared.kerberos.components.MethodData;
+import org.apache.directory.shared.kerberos.components.PaData;
+import org.apache.directory.shared.ldap.util.StringTools;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+
+/**
+ * Test the METHOD-DATA decoder.
+ * 
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+@RunWith(ConcurrentJunitRunner.class)
+@Concurrent()
+public class MethodDataDecoderTest
+{
+    /**
+     * Test the decoding of a METHOD-DATA
+     */
+    @Test
+    public void testMethodData()
+    {
+        Asn1Decoder kerberosDecoder = new Asn1Decoder();
+
+        ByteBuffer stream = ByteBuffer.allocate( 0x24 );
+        
+        stream.put( new byte[]
+            { 
+              0x30, 0x22,
+                0x30, 0x0F,
+                  (byte)0xA1, 0x03,                 // padata-type
+                    0x02, 0x01, 0x02,
+                  (byte)0xA2, 0x08,                 // padata-value
+                    0x04, 0x06, 'a', 'b', 'c', 'd', 'e', 'f',
+                0x30, 0x0F,
+                  (byte)0xA1, 0x03,                 // padata-type
+                    0x02, 0x01, 0x02,
+                  (byte)0xA2, 0x08,                 // padata-value
+                    0x04, 0x06, 'g', 'h', 'i', 'j', 'k', 'l'
+            } );
+
+        String decodedPdu = StringTools.dumpBytes( stream.array() );
+        stream.flip();
+
+        // Allocate a METHOD-DATA Container
+        Asn1Container methodDataContainer = new MethodDataContainer();
+        methodDataContainer.setStream( stream );
+
+        // Decode the MethodData PDU
+        try
+        {
+            kerberosDecoder.decode( stream, methodDataContainer );
+        }
+        catch ( DecoderException de )
+        {
+            fail( de.getMessage() );
+        }
+
+        // Check the decoded ETypeInfo
+        MethodData methodData = ( ( MethodDataContainer ) methodDataContainer ).getMethodData();
+
+        assertEquals( 2, methodData.getPaDatas().length );
+        
+        String[] expected = new String[]{ "abcdef", "ghijkl" };
+        int i = 0;
+        
+        for ( PaData paData : methodData.getPaDatas() )
+        {
+            assertEquals( PaDataType.PA_ENC_TIMESTAMP, paData.getPaDataType() );
+            assertTrue( Arrays.equals( StringTools.getBytesUtf8( expected[i] ), paData.getPaDataValue()
) );
+            i++;
+        }
+
+        // Check the encoding
+        ByteBuffer bb = ByteBuffer.allocate( methodData.computeLength() );
+        
+        try
+        {
+            bb = methodData.encode( bb );
+    
+            // Check the length
+            assertEquals( 0x24, bb.limit() );
+    
+            String encodedPdu = StringTools.dumpBytes( bb.array() );
+    
+            assertEquals( encodedPdu, decodedPdu );
+        }
+        catch ( EncoderException ee )
+        {
+            fail();
+        }
+    }
+    
+    
+    /**
+     * Test the decoding of a METHOD-DATA with nothing in it
+     */
+    @Test( expected = DecoderException.class)
+    public void testETypeInfoEmpty() throws DecoderException
+    {
+        Asn1Decoder kerberosDecoder = new Asn1Decoder();
+
+        ByteBuffer stream = ByteBuffer.allocate( 0x02 );
+        
+        stream.put( new byte[]
+            { 0x30, 0x00 } );
+
+        stream.flip();
+
+        // Allocate a METHOD-DATA Container
+        Asn1Container methodDataContainer = new MethodDataContainer();
+
+        // Decode the METHOD-DATA PDU
+        kerberosDecoder.decode( stream, methodDataContainer );
+        fail();
+    }
+    
+    
+    /**
+     * Test the decoding of a METHOD-DATA with empty PA-DATA in it
+     */
+    @Test( expected = DecoderException.class)
+    public void testETypeInfoNoETypeInfoEntry() throws DecoderException
+    {
+        Asn1Decoder kerberosDecoder = new Asn1Decoder();
+
+        ByteBuffer stream = ByteBuffer.allocate( 0x04 );
+        
+        stream.put( new byte[]
+            { 
+              0x30, 0x02,
+                (byte)0x30, 0x00                  // empty PA-DATA
+            } );
+
+        stream.flip();
+
+        // Allocate a METHOD-DATA Container
+        Asn1Container methodDataContainer = new MethodDataContainer();
+
+        // Decode the METHOD-DATA PDU
+        kerberosDecoder.decode( stream, methodDataContainer );
+        fail();
+        fail();
+    }
+}



Mime
View raw message