You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by ka...@apache.org on 2010/11/27 10:19:30 UTC
svn commit: r1039629 - in /directory/apacheds/trunk/kerberos-codec/src:
main/java/org/apache/directory/shared/kerberos/codec/typedData/
main/java/org/apache/directory/shared/kerberos/codec/typedData/actions/
test/java/org/apache/directory/shared/kerber...
Author: kayyagari
Date: Sat Nov 27 09:19:30 2010
New Revision: 1039629
URL: http://svn.apache.org/viewvc?rev=1039629&view=rev
Log:
o grammar, actions and test cases of TypedData
Added:
directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/typedData/
directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/typedData/TypedDataContainer.java
directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/typedData/TypedDataGrammar.java
directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/typedData/TypedDataStatesEnum.java
directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/typedData/actions/
directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/typedData/actions/StoreTdData.java
directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/typedData/actions/StoreTdType.java
directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/typedData/actions/TypedDataInit.java
directory/apacheds/trunk/kerberos-codec/src/test/java/org/apache/directory/shared/kerberos/codec/TypedDataDecoderTest.java
Added: directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/typedData/TypedDataContainer.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/typedData/TypedDataContainer.java?rev=1039629&view=auto
==============================================================================
--- directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/typedData/TypedDataContainer.java (added)
+++ directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/typedData/TypedDataContainer.java Sat Nov 27 09:19:30 2010
@@ -0,0 +1,67 @@
+/*
+ * 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.typedData;
+
+import org.apache.directory.shared.asn1.ber.AbstractContainer;
+import org.apache.directory.shared.kerberos.components.TypedData;
+
+
+/**
+ * The TypedData container stores the TypedData decoded by the Asn1Decoder.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public class TypedDataContainer extends AbstractContainer
+{
+ /** An TypedData container */
+ private TypedData typedData;
+
+ /**
+ * Creates a new TypedDataContainer object.
+ */
+ public TypedDataContainer()
+ {
+ super();
+ this.stateStack = new int[1];
+ this.grammar = TypedDataGrammar.getInstance();
+ setTransition( TypedDataStatesEnum.START_STATE );
+ }
+
+
+ /**
+ * @return Returns the TypedData.
+ */
+ public TypedData getTypedData()
+ {
+ return typedData;
+ }
+
+
+ /**
+ * Set a TypedData Object into the container. It will be completed by the
+ * KerberosDecoder.
+ *
+ * @param typedData The TypedData to set.
+ */
+ public void setTypedData( TypedData typedData )
+ {
+ this.typedData = typedData;
+ }
+}
Added: directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/typedData/TypedDataGrammar.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/typedData/TypedDataGrammar.java?rev=1039629&view=auto
==============================================================================
--- directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/typedData/TypedDataGrammar.java (added)
+++ directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/typedData/TypedDataGrammar.java Sat Nov 27 09:19:30 2010
@@ -0,0 +1,146 @@
+/*
+ * 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.typedData;
+
+
+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.typedData.actions.StoreTdData;
+import org.apache.directory.shared.kerberos.codec.typedData.actions.StoreTdType;
+import org.apache.directory.shared.kerberos.codec.typedData.actions.TypedDataInit;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+/**
+ * This class implements the TypedData 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 TypedDataGrammar extends AbstractGrammar
+{
+ /** The logger */
+ static final Logger LOG = LoggerFactory.getLogger( TypedDataGrammar.class );
+
+ /** A speedup for logger */
+ static final boolean IS_DEBUG = LOG.isDebugEnabled();
+
+ /** The instance of grammar. TypedDataGrammar is a singleton */
+ private static Grammar instance = new TypedDataGrammar();
+
+
+ /**
+ * Creates a new TypedDataGrammar object.
+ */
+ private TypedDataGrammar()
+ {
+ setName( TypedDataGrammar.class.getName() );
+
+ // Create the transitions table
+ super.transitions = new GrammarTransition[TypedDataStatesEnum.LAST_TYPED_DATA_STATE.ordinal()][256];
+
+ // ============================================================================================
+ // TypedData
+ // ============================================================================================
+ // --------------------------------------------------------------------------------------------
+ // Transition from TypedData init to TypedData SEQ OF
+ // --------------------------------------------------------------------------------------------
+ // TypedData ::= SEQUENCE OF
+ super.transitions[TypedDataStatesEnum.START_STATE.ordinal()][UniversalTag.SEQUENCE.getValue()] = new GrammarTransition(
+ TypedDataStatesEnum.START_STATE, TypedDataStatesEnum.TYPED_DATA_SEQ_SEQ_STATE, UniversalTag.SEQUENCE.getValue(),
+ new TypedDataInit() );
+
+ // --------------------------------------------------------------------------------------------
+ // Transition from TypedData SEQ OF to SEQ
+ // --------------------------------------------------------------------------------------------
+ // TypedData ::= SEQUENCE OF SEQUENCE {
+ super.transitions[TypedDataStatesEnum.TYPED_DATA_SEQ_SEQ_STATE.ordinal()][UniversalTag.SEQUENCE.getValue()] = new GrammarTransition(
+ TypedDataStatesEnum.TYPED_DATA_SEQ_SEQ_STATE, TypedDataStatesEnum.TYPED_DATA_SEQ_STATE, UniversalTag.SEQUENCE.getValue(),
+ new CheckNotNullLength() );
+
+ // --------------------------------------------------------------------------------------------
+ // Transition from TypedData SEQ OF to tdType tag
+ // --------------------------------------------------------------------------------------------
+ // TypedData ::= SEQUENCE OF SEQUENCE {
+ // data-type [0]
+ super.transitions[TypedDataStatesEnum.TYPED_DATA_SEQ_STATE.ordinal()][KerberosConstants.TYPED_DATA_TDTYPE_TAG] = new GrammarTransition(
+ TypedDataStatesEnum.TYPED_DATA_SEQ_STATE, TypedDataStatesEnum.TYPED_DATA_TDTYPE_TAG_STATE, KerberosConstants.TYPED_DATA_TDTYPE_TAG,
+ new CheckNotNullLength() );
+
+ // --------------------------------------------------------------------------------------------
+ // Transition from adtype tag to tdtype value
+ // --------------------------------------------------------------------------------------------
+ // TypedData ::= SEQUENCE OF SEQUENCE {
+ // data-type [0] Int32,
+ super.transitions[TypedDataStatesEnum.TYPED_DATA_TDTYPE_TAG_STATE.ordinal()][UniversalTag.INTEGER.getValue()] = new GrammarTransition(
+ TypedDataStatesEnum.TYPED_DATA_TDTYPE_TAG_STATE, TypedDataStatesEnum.TYPED_DATA_TDTYPE_STATE, UniversalTag.INTEGER.getValue(),
+ new StoreTdType() );
+
+ // --------------------------------------------------------------------------------------------
+ // Transition from ad-type value to ad-data tag
+ // --------------------------------------------------------------------------------------------
+ // TypedData ::= SEQUENCE OF SEQUENCE {
+ // ...
+ // data-value [1]
+ super.transitions[TypedDataStatesEnum.TYPED_DATA_TDTYPE_STATE.ordinal()][KerberosConstants.TYPED_DATA_TDDATA_TAG] = new GrammarTransition(
+ TypedDataStatesEnum.TYPED_DATA_TDTYPE_STATE, TypedDataStatesEnum.TYPED_DATA_TDDATA_TAG_STATE, KerberosConstants.TYPED_DATA_TDDATA_TAG,
+ new CheckNotNullLength() );
+
+ // --------------------------------------------------------------------------------------------
+ // Transition from ad-data tag to ad-data value
+ // --------------------------------------------------------------------------------------------
+ // TypedData ::= SEQUENCE OF SEQUENCE {
+ // ...
+ // data-value [1] (OCTET STRING)
+ super.transitions[TypedDataStatesEnum.TYPED_DATA_TDDATA_TAG_STATE.ordinal()][UniversalTag.OCTET_STRING.getValue()] = new GrammarTransition(
+ TypedDataStatesEnum.TYPED_DATA_TDDATA_TAG_STATE, TypedDataStatesEnum.TYPED_DATA_TDDATA_STATE, UniversalTag.OCTET_STRING.getValue(),
+ new StoreTdData() );
+
+ // --------------------------------------------------------------------------------------------
+ // Transition from ad-data value to SEQUENCE
+ // --------------------------------------------------------------------------------------------
+ // TypedData ::= SEQUENCE {
+ // ...
+ // data-value [1] (OCTET STRING)
+ super.transitions[TypedDataStatesEnum.TYPED_DATA_TDDATA_STATE.ordinal()][UniversalTag.SEQUENCE.getValue()] = new GrammarTransition(
+ TypedDataStatesEnum.TYPED_DATA_TDDATA_STATE, TypedDataStatesEnum.TYPED_DATA_SEQ_STATE, UniversalTag.SEQUENCE.getValue(),
+ new CheckNotNullLength() );
+ }
+
+
+ // ~ Methods
+ // ------------------------------------------------------------------------------------
+
+ /**
+ * Get the instance of this grammar
+ *
+ * @return An instance on the TypedData Grammar
+ */
+ public static Grammar getInstance()
+ {
+ return instance;
+ }
+}
Added: directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/typedData/TypedDataStatesEnum.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/typedData/TypedDataStatesEnum.java?rev=1039629&view=auto
==============================================================================
--- directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/typedData/TypedDataStatesEnum.java (added)
+++ directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/typedData/TypedDataStatesEnum.java Sat Nov 27 09:19:30 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.typedData;
+
+
+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 TypedData grammar's constants. It is also used for debugging
+ * purpose
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public enum TypedDataStatesEnum implements States
+{
+ // Start
+ START_STATE, // 0
+
+ TYPED_DATA_SEQ_STATE, // 1
+
+ TYPED_DATA_SEQ_SEQ_STATE, // 2
+
+ TYPED_DATA_TDTYPE_TAG_STATE, // 3
+ TYPED_DATA_TDTYPE_STATE, // 4
+
+ TYPED_DATA_TDDATA_TAG_STATE, // 5
+ TYPED_DATA_TDDATA_STATE, // 6
+
+ // End
+ LAST_TYPED_DATA_STATE; // 7
+
+
+ /**
+ * Get the grammar name
+ *
+ * @param grammar The grammar code
+ * @return The grammar name
+ */
+ public String getGrammarName( int grammar )
+ {
+ return "TYPED_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 "TYPED_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_TYPED_DATA_STATE.ordinal() ) ? "LAST_TYPED_DATA_STATE" : name() );
+ }
+
+
+ /**
+ * {@inheritDoc}
+ */
+ public boolean isEndState()
+ {
+ return this == LAST_TYPED_DATA_STATE;
+ }
+
+
+ /**
+ * {@inheritDoc}
+ */
+ public TypedDataStatesEnum getStartState()
+ {
+ return START_STATE;
+ }
+}
Added: directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/typedData/actions/StoreTdData.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/typedData/actions/StoreTdData.java?rev=1039629&view=auto
==============================================================================
--- directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/typedData/actions/StoreTdData.java (added)
+++ directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/typedData/actions/StoreTdData.java Sat Nov 27 09:19:30 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.typedData.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.typedData.TypedDataContainer;
+import org.apache.directory.shared.ldap.util.StringTools;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+/**
+ * The action used to store the TypedData's ad-data
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public class StoreTdData extends GrammarAction
+{
+
+ /** The logger */
+ private static final Logger LOG = LoggerFactory.getLogger( StoreTdData.class );
+
+ /** Speedup for logs */
+ private static final boolean IS_DEBUG = LOG.isDebugEnabled();
+
+
+ /**
+ * Instantiates a new StoreTdData action.
+ */
+ public StoreTdData()
+ {
+ super( "TypedData data-value" );
+ }
+
+
+ /**
+ * {@inheritDoc}
+ */
+ public void action( Asn1Container container ) throws DecoderException
+ {
+ TypedDataContainer tdContainer = ( TypedDataContainer ) container;
+
+ TLV tlv = tdContainer.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 ) );
+ // }
+
+ tdContainer.getTypedData().setCurrentTdData( value.getData() );
+
+ if ( IS_DEBUG )
+ {
+ LOG.debug( "data-value : {}", StringTools.dumpBytes( value.getData() ) );
+ }
+
+ container.setGrammarEndAllowed( true );
+ }
+
+}
Added: directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/typedData/actions/StoreTdType.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/typedData/actions/StoreTdType.java?rev=1039629&view=auto
==============================================================================
--- directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/typedData/actions/StoreTdType.java (added)
+++ directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/typedData/actions/StoreTdType.java Sat Nov 27 09:19:30 2010
@@ -0,0 +1,59 @@
+/*
+ * 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.typedData.actions;
+
+
+import org.apache.directory.shared.asn1.ber.Asn1Container;
+import org.apache.directory.shared.kerberos.codec.actions.AbstractReadInteger;
+import org.apache.directory.shared.kerberos.codec.typedData.TypedDataContainer;
+import org.apache.directory.shared.kerberos.components.TypedData;
+
+
+/**
+ * The action used to store the TypedData tdType
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public class StoreTdType extends AbstractReadInteger
+{
+ /**
+ * Instantiates a new AuthorizationDataAdType action.
+ */
+ public StoreTdType()
+ {
+ super( "TypedData data-type" );
+ }
+
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ protected void setIntegerValue( int value, Asn1Container container )
+ {
+ TypedDataContainer typedDataContainer = ( TypedDataContainer ) container;
+
+ TypedData typedData = typedDataContainer.getTypedData();
+ typedData.createNewTD();
+ typedData.setCurrentTdType( value );
+
+ container.setGrammarEndAllowed( true );
+ }
+}
Added: directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/typedData/actions/TypedDataInit.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/typedData/actions/TypedDataInit.java?rev=1039629&view=auto
==============================================================================
--- directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/typedData/actions/TypedDataInit.java (added)
+++ directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/typedData/actions/TypedDataInit.java Sat Nov 27 09:19:30 2010
@@ -0,0 +1,87 @@
+/*
+ * 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.typedData.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.typedData.TypedDataContainer;
+import org.apache.directory.shared.kerberos.components.TypedData;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+/**
+ * The action used to initialize the TypedData object
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public class TypedDataInit extends GrammarAction
+{
+ /** The logger */
+ private static final Logger LOG = LoggerFactory.getLogger( TypedDataInit.class );
+
+ /** Speedup for logs */
+ private static final boolean IS_DEBUG = LOG.isDebugEnabled();
+
+
+ /**
+ * Instantiates a new TypedDataInit action.
+ */
+ public TypedDataInit()
+ {
+ super( "Creates a TypedData instance" );
+ }
+
+
+ /**
+ * {@inheritDoc}
+ */
+ public void action( Asn1Container container ) throws DecoderException
+ {
+ TypedDataContainer typedDataContainer = ( TypedDataContainer ) container;
+
+ TLV tlv = typedDataContainer.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 ) );
+ }
+
+ if ( typedDataContainer.getTypedData() == null )
+ {
+ TypedData typedData = new TypedData();
+ typedDataContainer.setTypedData( typedData );
+
+
+ if ( IS_DEBUG )
+ {
+ LOG.debug( "TypedData created" );
+ }
+ }
+ }
+}
Added: directory/apacheds/trunk/kerberos-codec/src/test/java/org/apache/directory/shared/kerberos/codec/TypedDataDecoderTest.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/kerberos-codec/src/test/java/org/apache/directory/shared/kerberos/codec/TypedDataDecoderTest.java?rev=1039629&view=auto
==============================================================================
--- directory/apacheds/trunk/kerberos-codec/src/test/java/org/apache/directory/shared/kerberos/codec/TypedDataDecoderTest.java (added)
+++ directory/apacheds/trunk/kerberos-codec/src/test/java/org/apache/directory/shared/kerberos/codec/TypedDataDecoderTest.java Sat Nov 27 09:19:30 2010
@@ -0,0 +1,241 @@
+/*
+ * 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.assertNotNull;
+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.typedData.TypedDataContainer;
+import org.apache.directory.shared.kerberos.components.TypedData;
+import org.apache.directory.shared.ldap.util.StringTools;
+import org.junit.Test;
+
+/**
+ * Test cases for TypedData decoder.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public class TypedDataDecoderTest
+{
+
+ @Test
+ public void testTypedData()
+ {
+ Asn1Decoder kerberosDecoder = new Asn1Decoder();
+
+ ByteBuffer stream = ByteBuffer.allocate( 0x21 );
+
+ stream.put( new byte[]
+ {
+ 0x30, 0x1F,
+ 0x30, 0x0F,
+ (byte)0xA0, 0x03, // data-type
+ 0x02, 0x01, 0x02,
+ (byte)0xA1, 0x08, // data-value
+ 0x04, 0x06, 'a', 'b', 'c', 'd', 'e', 'f',
+ 0x30, 0x0C,
+ (byte)0xA0, 0x03, // data-type
+ 0x02, 0x01, 0x02,
+ (byte)0xA1, 0x05, // data-value
+ 0x04, 0x03, 'g', 'h', 'i'
+ } );
+
+ String decodedPdu = StringTools.dumpBytes( stream.array() );
+ stream.flip();
+
+ TypedDataContainer typedDataContainer = new TypedDataContainer();
+
+ // Decode the TypedData PDU
+ try
+ {
+ kerberosDecoder.decode( stream, typedDataContainer );
+ }
+ catch ( DecoderException de )
+ {
+ fail( de.getMessage() );
+ }
+
+ TypedData typedData = typedDataContainer.getTypedData();
+
+ assertNotNull( typedData.getTypedData().size() );
+ assertEquals( 2, typedData.getTypedData().size() );
+
+ String[] expected = new String[]{ "abcdef", "ghi" };
+ int i = 0;
+
+ for ( TypedData.TD td : typedData.getTypedData() )
+ {
+ assertEquals( 2, td.getTdType() );
+ assertTrue( Arrays.equals( StringTools.getBytesUtf8( expected[i++] ), td.getTdData() ) );
+ }
+
+ // Check the encoding
+ ByteBuffer bb = ByteBuffer.allocate( typedData.computeLength() );
+
+ try
+ {
+ bb = typedData.encode( bb );
+
+ // Check the length
+ assertEquals( 0x21, bb.limit() );
+
+ String encodedPdu = StringTools.dumpBytes( bb.array() );
+
+ assertEquals( encodedPdu, decodedPdu );
+ }
+ catch ( EncoderException ee )
+ {
+ fail();
+ }
+ }
+
+
+ @Test( expected = DecoderException.class)
+ public void testTypedDataWithoutType() throws DecoderException
+ {
+ Asn1Decoder kerberosDecoder = new Asn1Decoder();
+
+ ByteBuffer stream = ByteBuffer.allocate( 0x09 );
+
+ stream.put( new byte[]
+ { 0x30, 0x7,
+ 0x30, 0x5,
+ (byte)0xA1, 0x03, // data-value
+ 0x04, 0x01, 'a'
+ } );
+
+ stream.flip();
+
+ TypedDataContainer typedDataContainer = new TypedDataContainer();
+
+ kerberosDecoder.decode( stream, typedDataContainer );
+ fail();
+ }
+
+ @Test
+ public void testTypedDataWithoutData() throws DecoderException
+ {
+ Asn1Decoder kerberosDecoder = new Asn1Decoder();
+
+ ByteBuffer stream = ByteBuffer.allocate( 0x09 );
+
+ stream.put( new byte[]
+ { 0x30, 0x7,
+ 0x30, 0x05,
+ (byte)0xA0, 0x03, // data-value
+ 0x02, 0x01, 0x02
+ } );
+
+ stream.flip();
+
+ TypedDataContainer typedDataContainer = new TypedDataContainer();
+
+ kerberosDecoder.decode( stream, typedDataContainer );
+
+ TypedData typedData = typedDataContainer.getTypedData();
+
+ assertNotNull( typedData.getTypedData() );
+ assertEquals( 1, typedData.getTypedData().size() );
+ assertEquals( 2, typedData.getCurrentTD().getTdType() );
+ }
+
+
+ @Test( expected = DecoderException.class)
+ public void testTypedDataWithIncorrectPdu() throws DecoderException
+ {
+ Asn1Decoder kerberosDecoder = new Asn1Decoder();
+
+ ByteBuffer stream = ByteBuffer.allocate( 0x04 );
+
+ stream.put( new byte[]
+ {
+ 0x30, 0x2,
+ 0x30, 0x0
+ } );
+
+ stream.flip();
+
+ TypedDataContainer typedDataContainer = new TypedDataContainer();
+
+ kerberosDecoder.decode( stream, typedDataContainer );
+ fail();
+ }
+
+
+ @Test( expected = DecoderException.class)
+ public void testTypedDataWithEmptyData() throws DecoderException
+ {
+ Asn1Decoder kerberosDecoder = new Asn1Decoder();
+
+ ByteBuffer stream = ByteBuffer.allocate( 0xD );
+
+ stream.put( new byte[]
+ {
+ 0x30, 0x0B,
+ 0x30, 0x09,
+ (byte)0xA0, 0x03, // data-type
+ 0x02, 0x01, 0x02,
+ (byte)0xA1, 0x02, // data-value
+ 0x04, 0x00
+ } );
+
+ stream.flip();
+
+ TypedDataContainer typedDataContainer = new TypedDataContainer();
+
+ kerberosDecoder.decode( stream, typedDataContainer );
+ fail();
+ }
+
+
+ @Test( expected = DecoderException.class)
+ public void testTypedDataWithEmptyType() throws DecoderException
+ {
+ Asn1Decoder kerberosDecoder = new Asn1Decoder();
+
+ ByteBuffer stream = ByteBuffer.allocate( 0xD );
+
+ stream.put( new byte[]
+ {
+ 0x30, 0x0B,
+ 0x30, 0x09,
+ (byte)0xA0, 0x02, // data-type
+ 0x02, 0x00,
+ (byte)0xA1, 0x03, // data-value
+ 0x04, 0x01, 0x02
+ } );
+
+ stream.flip();
+
+ TypedDataContainer typedDataContainer = new TypedDataContainer();
+
+ kerberosDecoder.decode( stream, typedDataContainer );
+ fail();
+ }
+}