directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kayyag...@apache.org
Subject svn commit: r1034550 - in /directory/apacheds/trunk/kerberos-codec/src: main/java/org/apache/directory/shared/kerberos/codec/padata/ main/java/org/apache/directory/shared/kerberos/codec/padata/actions/ test/java/org/apache/directory/shared/kerberos/codec/
Date Fri, 12 Nov 2010 20:04:39 GMT
Author: kayyagari
Date: Fri Nov 12 20:04:38 2010
New Revision: 1034550

URL: http://svn.apache.org/viewvc?rev=1034550&view=rev
Log:
o implementation of PaData codec

Added:
    directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/padata/
    directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/padata/PaDataContainer.java
    directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/padata/PaDataGrammar.java
    directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/padata/PaDataStatesEnum.java
    directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/padata/actions/
    directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/padata/actions/PaDataDataType.java
    directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/padata/actions/PaDataInit.java
    directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/padata/actions/PaDataValue.java
    directory/apacheds/trunk/kerberos-codec/src/test/java/org/apache/directory/shared/kerberos/codec/PaDataDecoderTest.java

Added: directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/padata/PaDataContainer.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/padata/PaDataContainer.java?rev=1034550&view=auto
==============================================================================
--- directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/padata/PaDataContainer.java
(added)
+++ directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/padata/PaDataContainer.java
Fri Nov 12 20:04:38 2010
@@ -0,0 +1,66 @@
+/*
+ *  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.padata;
+
+import org.apache.directory.shared.asn1.ber.AbstractContainer;
+import org.apache.directory.shared.kerberos.components.PaData;
+
+
+/**
+ * The PaData container stores the PaData decoded by the Asn1Decoder.
+ * 
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public class PaDataContainer extends AbstractContainer
+{
+    /** holds PaData */
+    private PaData paData;
+
+    /**
+     * Creates a new PaDataContainer object.
+     */
+    public PaDataContainer()
+    {
+        super();
+        this.stateStack = new int[1];
+        this.grammar = PaDataGrammar.getInstance();
+        setTransition( PaDataStatesEnum.START_STATE );
+    }
+
+
+    /**
+     * @return Returns the PaData.
+     */
+    public PaData getPaData()
+    {
+        return paData;
+    }
+
+    
+    /**
+     * Set a PaData Object into the container
+     * 
+     * @param paData The PaData to set.
+     */
+    public void setPaData( PaData paData )
+    {
+        this.paData = paData;
+    }
+}

Added: directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/padata/PaDataGrammar.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/padata/PaDataGrammar.java?rev=1034550&view=auto
==============================================================================
--- directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/padata/PaDataGrammar.java
(added)
+++ directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/padata/PaDataGrammar.java
Fri Nov 12 20:04:38 2010
@@ -0,0 +1,126 @@
+/*
+ *  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.padata;
+
+
+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.KerberosConstants;
+import org.apache.directory.shared.kerberos.codec.actions.CheckNotNullLength;
+import org.apache.directory.shared.kerberos.codec.padata.actions.PaDataDataType;
+import org.apache.directory.shared.kerberos.codec.padata.actions.PaDataInit;
+import org.apache.directory.shared.kerberos.codec.padata.actions.PaDataValue;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+/**
+ * This class implements the PaData 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 PaDataGrammar extends AbstractGrammar
+{
+    /** The logger */
+    static final Logger LOG = LoggerFactory.getLogger( PaDataGrammar.class );
+
+    /** A speedup for logger */
+    static final boolean IS_DEBUG = LOG.isDebugEnabled();
+
+    /** The instance of grammar. PaDataGrammar is a singleton */
+    private static Grammar instance = new PaDataGrammar();
+
+
+    /**
+     * Creates a new PaDataGrammar object.
+     */
+    private PaDataGrammar()
+    {
+        setName( PaDataGrammar.class.getName() );
+
+        // Create the transitions table
+        super.transitions = new GrammarTransition[PaDataStatesEnum.LAST_PADATA_STATE.ordinal()][256];
+
+        // ============================================================================================
+        // PaData 
+        // ============================================================================================
+        // --------------------------------------------------------------------------------------------
+        // Transition from PaData init to PaData SEQ OF
+        // --------------------------------------------------------------------------------------------
+        // PA-DATA         ::= SEQUENCE {
+        super.transitions[PaDataStatesEnum.START_STATE.ordinal()][UniversalTag.SEQUENCE.getValue()]
= new GrammarTransition(
+            PaDataStatesEnum.START_STATE, PaDataStatesEnum.PADATA_SEQ_STATE, UniversalTag.SEQUENCE.getValue(),
+            new PaDataInit() );
+        
+        // --------------------------------------------------------------------------------------------
+        // Transition from PaData SEQ to padata-type tag
+        // --------------------------------------------------------------------------------------------
+        // PA-DATA         ::= SEQUENCE {
+        //       padata-type     [1]
+        super.transitions[PaDataStatesEnum.PADATA_SEQ_STATE.ordinal()][KerberosConstants.PADATA_TYPE_TAG]
= new GrammarTransition(
+            PaDataStatesEnum.PADATA_SEQ_STATE, PaDataStatesEnum.PADATA_TYPE_TAG_STATE, KerberosConstants.PADATA_TYPE_TAG,
+            new CheckNotNullLength() );
+        
+        // --------------------------------------------------------------------------------------------
+        // Transition from PaData type tag to padata-type
+        // --------------------------------------------------------------------------------------------
+        // PA-DATA         ::= SEQUENCE {
+        //       padata-type     [1] Int32
+        super.transitions[PaDataStatesEnum.PADATA_TYPE_TAG_STATE.ordinal()][UniversalTag.INTEGER.getValue()]
= new GrammarTransition(
+            PaDataStatesEnum.PADATA_TYPE_TAG_STATE, PaDataStatesEnum.PADATA_TYPE_STATE, UniversalTag.INTEGER.getValue(),
+            new PaDataDataType() );
+        
+        // --------------------------------------------------------------------------------------------
+        // Transition from padata-type to padata-value tag
+        // --------------------------------------------------------------------------------------------
+        // PA-DATA         ::= SEQUENCE {
+        //          padata-value    [2]
+        super.transitions[PaDataStatesEnum.PADATA_TYPE_STATE.ordinal()][KerberosConstants.PADATA_VALUE_TAG]
= new GrammarTransition(
+            PaDataStatesEnum.PADATA_TYPE_STATE, PaDataStatesEnum.PADATA_VALUE_TAG_STATE,
KerberosConstants.PADATA_VALUE_TAG,
+            new CheckNotNullLength() );
+        
+        // --------------------------------------------------------------------------------------------
+        // Transition from padata-value tag to padata-value
+        // --------------------------------------------------------------------------------------------
+        // PA-DATA         ::= SEQUENCE {
+        //          padata-value    [2] OCTET STRING
+        super.transitions[PaDataStatesEnum.PADATA_VALUE_TAG_STATE.ordinal()][UniversalTag.OCTET_STRING.getValue()]
= new GrammarTransition(
+            PaDataStatesEnum.PADATA_VALUE_TAG_STATE, PaDataStatesEnum.PADATA_VALUE_STATE,
UniversalTag.OCTET_STRING.getValue(),
+            new PaDataValue() );
+    }
+
+
+    // ~ Methods
+    // ------------------------------------------------------------------------------------
+
+    /**
+     * Get the instance of this grammar
+     * 
+     * @return An instance on the PaData Grammar
+     */
+    public static Grammar getInstance()
+    {
+        return instance;
+    }
+}

Added: directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/padata/PaDataStatesEnum.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/padata/PaDataStatesEnum.java?rev=1034550&view=auto
==============================================================================
--- directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/padata/PaDataStatesEnum.java
(added)
+++ directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/padata/PaDataStatesEnum.java
Fri Nov 12 20:04:38 2010
@@ -0,0 +1,112 @@
+/*
+ *  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.padata;
+
+
+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 PaData grammar's constants. It is also used for debugging
+ * purpose
+ * 
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public enum PaDataStatesEnum implements States
+{
+    // Start
+    START_STATE,                  // 0
+    
+    PADATA_SEQ_STATE,           // 1
+    
+    PADATA_TYPE_TAG_STATE,      // 2
+    
+    PADATA_TYPE_STATE,          // 3
+    
+    PADATA_VALUE_TAG_STATE,  // 4
+    
+    PADATA_VALUE_STATE,      // 5
+    
+    // End
+    LAST_PADATA_STATE; // 6
+
+    
+    /**
+     * Get the grammar name
+     * 
+     * @param grammar The grammar code
+     * @return The grammar name
+     */
+    public String getGrammarName( int grammar )
+    {
+        return "PADATA_GRAMMAR";
+    }
+
+
+    /**
+     * Get the grammar name
+     * 
+     * @param grammar The grammar class
+     * @return The grammar name
+     */
+    public String getGrammarName( Grammar grammar )
+    {
+        if ( grammar instanceof KerberosMessageGrammar )
+        {
+            return "PADATA_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_PADATA_STATE.ordinal() ) ? "LAST_PADATA_STATE" : name()
);
+    }
+
+    
+    /**
+     * {@inheritDoc}
+     */
+    public boolean isEndState()
+    {
+        return this == LAST_PADATA_STATE;
+    }
+    
+    
+    /**
+     * {@inheritDoc}
+     */
+    public PaDataStatesEnum getStartState()
+    {
+        return START_STATE;
+    }
+}

Added: directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/padata/actions/PaDataDataType.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/padata/actions/PaDataDataType.java?rev=1034550&view=auto
==============================================================================
--- directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/padata/actions/PaDataDataType.java
(added)
+++ directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/padata/actions/PaDataDataType.java
Fri Nov 12 20:04:38 2010
@@ -0,0 +1,105 @@
+/*
+ *   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.padata.actions;
+
+
+import org.apache.directory.shared.asn1.ber.Asn1Container;
+import org.apache.directory.shared.asn1.ber.grammar.GrammarAction;
+import org.apache.directory.shared.asn1.ber.tlv.TLV;
+import org.apache.directory.shared.asn1.ber.tlv.Value;
+import org.apache.directory.shared.asn1.codec.DecoderException;
+import org.apache.directory.shared.asn1.util.IntegerDecoder;
+import org.apache.directory.shared.asn1.util.IntegerDecoderException;
+import org.apache.directory.shared.i18n.I18n;
+import org.apache.directory.shared.kerberos.codec.padata.PaDataContainer;
+import org.apache.directory.shared.kerberos.codec.types.PaDataType;
+import org.apache.directory.shared.kerberos.components.PaData;
+import org.apache.directory.shared.ldap.util.StringTools;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+/**
+ * Sets the PaData's data type.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public class PaDataDataType extends GrammarAction
+{
+    /** The logger */
+    private static final Logger LOG = LoggerFactory.getLogger( PaDataDataType.class );
+
+    /** Speedup for logs */
+    private static final boolean IS_DEBUG = LOG.isDebugEnabled();
+
+
+    /**
+     * Creates a new instance of PaDataDataType.
+     */
+    public PaDataDataType()
+    {
+        super( "PaDataData's padata-type" );
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public void action( Asn1Container container ) throws DecoderException
+    {
+        PaDataContainer paDataContainer = ( PaDataContainer ) container;
+
+        TLV tlv = paDataContainer.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 ) );
+        }
+
+        PaData paData = paDataContainer.getPaData();
+        // The PaData's type is an integer
+        Value value = tlv.getValue();
+
+        try
+        {
+            int paDataType = IntegerDecoder.parse( value );
+
+            paData.setPaDataType( PaDataType.getTypeByValue( paDataType ) );
+
+            if ( IS_DEBUG )
+            {
+                LOG.debug( "padata-type : " + paDataType );
+            }
+        }
+        catch ( IntegerDecoderException ide )
+        {
+            LOG.error( I18n.err( I18n.ERR_04070, StringTools.dumpBytes( value.getData() ),
ide
+                .getLocalizedMessage() ) );
+
+            // This will generate a PROTOCOL_ERROR
+            throw new DecoderException( ide.getMessage() );
+        }
+    }
+}

Added: directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/padata/actions/PaDataInit.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/padata/actions/PaDataInit.java?rev=1034550&view=auto
==============================================================================
--- directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/padata/actions/PaDataInit.java
(added)
+++ directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/padata/actions/PaDataInit.java
Fri Nov 12 20:04:38 2010
@@ -0,0 +1,83 @@
+/*
+ *  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.padata.actions;
+
+
+import org.apache.directory.shared.asn1.ber.Asn1Container;
+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.padata.PaDataContainer;
+import org.apache.directory.shared.kerberos.components.PaData;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+/**
+ * The action used to initialize the PaData object
+ * 
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public class PaDataInit extends GrammarAction
+{
+    /** The logger */
+    private static final Logger LOG = LoggerFactory.getLogger( PaDataInit.class );
+
+    /** Speedup for logs */
+    private static final boolean IS_DEBUG = LOG.isDebugEnabled();
+
+
+    /**
+     * Instantiates a new PaDataInit action.
+     */
+    public PaDataInit()
+    {
+        super( "Creates a PaData instance" );
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public void action( Asn1Container container ) throws DecoderException
+    {
+        PaDataContainer paDataContainer = ( PaDataContainer ) container;
+
+        TLV tlv = paDataContainer.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 ) );
+        }
+        
+        PaData paData = new PaData();
+        paDataContainer.setPaData( paData );
+        
+        if ( IS_DEBUG )
+        {
+            LOG.debug( "PaData created" );
+        }
+    }
+}

Added: directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/padata/actions/PaDataValue.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/padata/actions/PaDataValue.java?rev=1034550&view=auto
==============================================================================
--- directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/padata/actions/PaDataValue.java
(added)
+++ directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/padata/actions/PaDataValue.java
Fri Nov 12 20:04:38 2010
@@ -0,0 +1,98 @@
+/*
+ *  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.padata.actions;
+
+
+import org.apache.directory.shared.asn1.ber.Asn1Container;
+import org.apache.directory.shared.asn1.ber.grammar.GrammarAction;
+import org.apache.directory.shared.asn1.ber.tlv.TLV;
+import org.apache.directory.shared.asn1.ber.tlv.Value;
+import org.apache.directory.shared.asn1.codec.DecoderException;
+import org.apache.directory.shared.i18n.I18n;
+import org.apache.directory.shared.kerberos.codec.padata.PaDataContainer;
+import org.apache.directory.shared.kerberos.components.PaData;
+import org.apache.directory.shared.ldap.util.StringTools;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+/**
+ * The action used to store the PaData's padata-value
+ * 
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public class PaDataValue extends GrammarAction
+{
+    /** The logger */
+    private static final Logger LOG = LoggerFactory.getLogger( PaDataValue.class );
+
+    /** Speedup for logs */
+    private static final boolean IS_DEBUG = LOG.isDebugEnabled();
+
+
+    /**
+     * Instantiates a new PaDataValue action.
+     */
+    public PaDataValue()
+    {
+        super( "PaData's padata-value" );
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public void action( Asn1Container container ) throws DecoderException
+    {
+        PaDataContainer paDataContainer = ( PaDataContainer ) container;
+
+        TLV tlv = paDataContainer.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 ) );
+        }
+        
+        Value value = tlv.getValue();
+        
+        // The encrypted data should not be null
+        if ( value.getData() == null ) 
+        {
+            LOG.error( I18n.err( I18n.ERR_04066 ) );
+
+            // This will generate a PROTOCOL_ERROR
+            throw new DecoderException( I18n.err( I18n.ERR_04067 ) );
+        }
+        
+        PaData paData = paDataContainer.getPaData();
+        paData.setPaDataValue( value.getData() );
+        
+        if ( IS_DEBUG )
+        {
+            LOG.debug( "padata value : {}", StringTools.dumpBytes( value.getData() ) );
+        }
+        
+        paDataContainer.setGrammarEndAllowed( true );
+    }
+}

Added: directory/apacheds/trunk/kerberos-codec/src/test/java/org/apache/directory/shared/kerberos/codec/PaDataDecoderTest.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/kerberos-codec/src/test/java/org/apache/directory/shared/kerberos/codec/PaDataDecoderTest.java?rev=1034550&view=auto
==============================================================================
--- directory/apacheds/trunk/kerberos-codec/src/test/java/org/apache/directory/shared/kerberos/codec/PaDataDecoderTest.java
(added)
+++ directory/apacheds/trunk/kerberos-codec/src/test/java/org/apache/directory/shared/kerberos/codec/PaDataDecoderTest.java
Fri Nov 12 20:04:38 2010
@@ -0,0 +1,169 @@
+/*
+ *   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.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.padata.PaDataContainer;
+import org.apache.directory.shared.kerberos.codec.types.PaDataType;
+import org.apache.directory.shared.kerberos.components.PaData;
+import org.apache.directory.shared.ldap.util.StringTools;
+import org.junit.Test;
+
+/**
+ * Test cases for PaData codec.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public class PaDataDecoderTest
+{
+    @Test
+    public void testDecodePaData()
+    {
+        Asn1Decoder krbDecoder = new Asn1Decoder();
+        
+        ByteBuffer stream = ByteBuffer.allocate( 0x11 );
+
+        stream.put( new byte[]
+            { 
+                0x30, 0x0F,
+                  (byte)0xA1, 0x03,                 // padata-type
+                    0x02, 0x01, 0x02,
+                  (byte)0xA2, 0x08,                 // padata-value
+                    0x04, 0x06, 'p', 'a', 'd', 'a', 't', 'a'
+            } );
+        
+        String decodedPdu = StringTools.dumpBytes( stream.array() );
+        stream.flip();
+
+        PaDataContainer container = new PaDataContainer();
+        
+        try
+        {
+            krbDecoder.decode( stream, container );
+        }
+        catch ( DecoderException de )
+        {
+            de.printStackTrace();
+            
+            fail( de.getMessage() );
+        }
+
+        PaData checksum = container.getPaData();
+        
+        assertEquals( PaDataType.getTypeByValue( 2 ), checksum.getPaDataType() );
+        assertTrue( Arrays.equals( StringTools.getBytesUtf8( "padata" ), checksum.getPaDataValue()
) );
+        
+        ByteBuffer bb = ByteBuffer.allocate( checksum.computeLength() );
+        
+        try
+        {
+            bb = checksum.encode( bb );
+    
+            // Check the length
+            assertEquals( 0x11, bb.limit() );
+    
+            String encodedPdu = StringTools.dumpBytes( bb.array() );
+    
+            assertEquals( encodedPdu, decodedPdu );
+        }
+        catch ( EncoderException ee )
+        {
+            fail();
+        }
+    }
+    
+    
+    @Test( expected = DecoderException.class )
+    public void testDecodePaDataWithoutType() throws DecoderException
+    {
+        Asn1Decoder krbDecoder = new Asn1Decoder();
+        
+        ByteBuffer stream = ByteBuffer.allocate( 0xC );
+
+        stream.put( new byte[]
+            { 
+                0x30, 0xA,
+                  (byte)0xA2, 0x08,                 // padata-value
+                      0x04, 0x06, 'p', 'a', 'd', 'a', 't', 'a'
+            } );
+        
+        stream.flip();
+
+        PaDataContainer chkContainer = new PaDataContainer();
+        
+        krbDecoder.decode( stream, chkContainer );
+        fail();
+    }
+    
+    
+    @Test( expected = DecoderException.class )
+    public void testDecodeChecksumWithoutPaDataValue() throws DecoderException
+    {
+        Asn1Decoder krbDecoder = new Asn1Decoder();
+        
+        ByteBuffer stream = ByteBuffer.allocate( 0x07 );
+
+        stream.put( new byte[]
+            { 
+                0x30, 0x05,
+                  (byte)0xA0, 0x03,                 // padata-type
+                    0x02, 0x01, 0x02
+            } );
+        
+        stream.flip();
+
+        PaDataContainer container = new PaDataContainer();
+        
+        krbDecoder.decode( stream, container );
+        fail();
+    }
+    
+    
+    @Test( expected = DecoderException.class )
+    public void testDecodePaDataWithEmptySeq() throws DecoderException
+    {
+        Asn1Decoder krbDecoder = new Asn1Decoder();
+        
+        ByteBuffer stream = ByteBuffer.allocate( 2 );
+
+        stream.put( new byte[]
+            { 
+                0x30, 0x0
+            } );
+        
+        stream.flip();
+
+        PaDataContainer container = new PaDataContainer();
+        
+        krbDecoder.decode( stream, container );
+        fail();
+    }
+
+}



Mime
View raw message