directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From elecha...@apache.org
Subject svn commit: r1034497 - in /directory/apacheds/trunk/kerberos-codec/src: main/java/org/apache/directory/shared/kerberos/codec/hostAddresses/ main/java/org/apache/directory/shared/kerberos/codec/hostAddresses/actions/ main/java/org/apache/directory/share...
Date Fri, 12 Nov 2010 18:04:34 GMT
Author: elecharny
Date: Fri Nov 12 18:04:34 2010
New Revision: 1034497

URL: http://svn.apache.org/viewvc?rev=1034497&view=rev
Log:
Added the decoder for HostAddresses

Added:
    directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/hostAddresses/
    directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/hostAddresses/HostAddressesContainer.java
    directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/hostAddresses/HostAddressesGrammar.java
    directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/hostAddresses/HostAddressesStatesEnum.java
    directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/hostAddresses/actions/
    directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/hostAddresses/actions/AddHostAddress.java
    directory/apacheds/trunk/kerberos-codec/src/test/java/org/apache/directory/shared/kerberos/codec/HostAddressesDecoderTest.java
Modified:
    directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/components/HostAddresses.java

Added: directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/hostAddresses/HostAddressesContainer.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/hostAddresses/HostAddressesContainer.java?rev=1034497&view=auto
==============================================================================
--- directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/hostAddresses/HostAddressesContainer.java
(added)
+++ directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/hostAddresses/HostAddressesContainer.java
Fri Nov 12 18:04:34 2010
@@ -0,0 +1,68 @@
+/*
+ *  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.hostAddresses;
+
+import org.apache.directory.shared.asn1.ber.AbstractContainer;
+import org.apache.directory.shared.kerberos.components.HostAddress;
+import org.apache.directory.shared.kerberos.components.HostAddresses;
+
+
+/**
+ * The HostAddresses container stores the HostAddresses decoded by the Asn1Decoder.
+ * 
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public class HostAddressesContainer extends AbstractContainer
+{
+    /** The list of HostAddress */
+    private HostAddresses hostAddresses = new HostAddresses();
+
+    /**
+     * Creates a new HostAddressesContainer object.
+     */
+    public HostAddressesContainer()
+    {
+        super();
+        this.stateStack = new int[1];
+        this.grammar = HostAddressesGrammar.getInstance();
+        setTransition( HostAddressesStatesEnum.START_STATE );
+    }
+
+
+    /**
+     * @return Returns the HostAddresses.
+     */
+    public HostAddresses getHostAddresses()
+    {
+        return hostAddresses;
+    }
+
+    
+    /**
+     * Add a HostAddress Object into the list. It will be completed by the
+     * KerberosDecoder.
+     * 
+     * @param hostAddress The HostAddress to add.
+     */
+    public void addHostAddress( HostAddress hostAddress )
+    {
+        this.hostAddresses.addHostAddress( hostAddress );
+    }
+}

Added: directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/hostAddresses/HostAddressesGrammar.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/hostAddresses/HostAddressesGrammar.java?rev=1034497&view=auto
==============================================================================
--- directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/hostAddresses/HostAddressesGrammar.java
(added)
+++ directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/hostAddresses/HostAddressesGrammar.java
Fri Nov 12 18:04:34 2010
@@ -0,0 +1,96 @@
+/*
+ *  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.hostAddresses;
+
+
+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.hostAddresses.actions.AddHostAddress;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+/**
+ * This class implements the HostAddresses 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 HostAddressesGrammar extends AbstractGrammar
+{
+    /** The logger */
+    static final Logger LOG = LoggerFactory.getLogger( HostAddressesGrammar.class );
+
+    /** A speedup for logger */
+    static final boolean IS_DEBUG = LOG.isDebugEnabled();
+
+    /** The instance of grammar. HostAddressesGrammar is a singleton */
+    private static Grammar instance = new HostAddressesGrammar();
+
+
+    /**
+     * Creates a new HostAddressGrammar object.
+     */
+    private HostAddressesGrammar()
+    {
+        setName( HostAddressesGrammar.class.getName() );
+
+        // Create the transitions table
+        super.transitions = new GrammarTransition[HostAddressesStatesEnum.LAST_HOST_ADDRESSES_STATE.ordinal()][256];
+
+        // ============================================================================================
+        // HostAddresses 
+        // ============================================================================================
+        // --------------------------------------------------------------------------------------------
+        // Transition from HostAddresses init to HostAddresses SEQUENCE OF
+        // --------------------------------------------------------------------------------------------
+        // HostAddress   ::= SEQUENCE OF
+        super.transitions[HostAddressesStatesEnum.START_STATE.ordinal()][UniversalTag.SEQUENCE.getValue()]
= new GrammarTransition(
+            HostAddressesStatesEnum.START_STATE, HostAddressesStatesEnum.HOST_ADDRESSES_ADDRESS_STATE,
UniversalTag.SEQUENCE.getValue(),
+            new CheckNotNullLength() );
+        
+        // --------------------------------------------------------------------------------------------
+        // Transition from HostAddresses SEQ to HostAddress
+        // --------------------------------------------------------------------------------------------
+        // HostAddresses   ::= SEQUENCE OF
+        //         HostAddress
+        super.transitions[HostAddressesStatesEnum.HOST_ADDRESSES_ADDRESS_STATE.ordinal()][UniversalTag.SEQUENCE.getValue()]
= new GrammarTransition(
+            HostAddressesStatesEnum.HOST_ADDRESSES_ADDRESS_STATE, HostAddressesStatesEnum.HOST_ADDRESSES_ADDRESS_STATE,
UniversalTag.SEQUENCE.getValue(),
+            new AddHostAddress() );
+    }
+
+
+    // ~ Methods
+    // ------------------------------------------------------------------------------------
+
+    /**
+     * Get the instance of this grammar
+     * 
+     * @return An instance on the HostAddresses Grammar
+     */
+    public static Grammar getInstance()
+    {
+        return instance;
+    }
+}

Added: directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/hostAddresses/HostAddressesStatesEnum.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/hostAddresses/HostAddressesStatesEnum.java?rev=1034497&view=auto
==============================================================================
--- directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/hostAddresses/HostAddressesStatesEnum.java
(added)
+++ directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/hostAddresses/HostAddressesStatesEnum.java
Fri Nov 12 18:04:34 2010
@@ -0,0 +1,107 @@
+/*
+ *  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.hostAddresses;
+
+
+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 HostAddresess grammar's constants. It is also used for debugging
+ * purpose
+ * 
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public enum HostAddressesStatesEnum implements States
+{
+    // Start
+    START_STATE,                            // 0
+    
+    // ----- HostAddresses message --------------------------------------
+    HOST_ADDRESSES_SEQ_STATE,               // 1
+    
+    HOST_ADDRESSES_ADDRESS_STATE,           // 2
+    
+    // End
+    LAST_HOST_ADDRESSES_STATE;              // 3
+
+    
+    /**
+     * Get the grammar name
+     * 
+     * @param grammar The grammar code
+     * @return The grammar name
+     */
+    public String getGrammarName( int grammar )
+    {
+        return "HOST_ADDRESSES_GRAMMAR";
+    }
+
+
+    /**
+     * Get the grammar name
+     * 
+     * @param grammar The grammar class
+     * @return The grammar name
+     */
+    public String getGrammarName( Grammar grammar )
+    {
+        if ( grammar instanceof KerberosMessageGrammar )
+        {
+            return "HOST_ADDRESSES_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_HOST_ADDRESSES_STATE.ordinal() ) ? "HOST_ADDRESSES_END_STATE"
: name() );
+    }
+
+    
+    /**
+     * {@inheritDoc}
+     */
+    public boolean isEndState()
+    {
+        return this == LAST_HOST_ADDRESSES_STATE;
+    }
+    
+    
+    /**
+     * {@inheritDoc}
+     */
+    public HostAddressesStatesEnum getStartState()
+    {
+        return START_STATE;
+    }
+}

Added: directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/hostAddresses/actions/AddHostAddress.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/hostAddresses/actions/AddHostAddress.java?rev=1034497&view=auto
==============================================================================
--- directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/hostAddresses/actions/AddHostAddress.java
(added)
+++ directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/codec/hostAddresses/actions/AddHostAddress.java
Fri Nov 12 18:04:34 2010
@@ -0,0 +1,111 @@
+/*
+ *  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.hostAddresses.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.hostAddress.HostAddressContainer;
+import org.apache.directory.shared.kerberos.codec.hostAddresses.HostAddressesContainer;
+import org.apache.directory.shared.kerberos.components.HostAddress;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+/**
+ * The action used to add an HostAddresses object
+ * 
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public class AddHostAddress 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 AddHostAddress action.
+     */
+    public AddHostAddress()
+    {
+        super( "Add an HostAddress instance" );
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public void action( Asn1Container container ) throws DecoderException
+    {
+        HostAddressesContainer hostAddressesContainer = ( HostAddressesContainer ) container;
+
+        TLV tlv = hostAddressesContainer.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 HostAddress
+        Asn1Decoder hostAddressDecoder = new Asn1Decoder();
+        
+        HostAddressContainer hostAddressContainer = new HostAddressContainer();
+
+        // Decode the HostAddress PDU
+        try
+        {
+            // We have to move back the Stream 
+            int moveBack = 1 + TLV.getNbBytes( tlv.getLength() );
+            container.getStream().position( container.getStream().position() - moveBack );
+            
+            hostAddressDecoder.decode( container.getStream(), hostAddressContainer );
+        }
+        catch ( DecoderException de )
+        {
+            throw de;
+        }
+
+        // Store the hostAddress in the container
+        HostAddress hostAddress = hostAddressContainer.getHostAddress();
+        hostAddressesContainer.addHostAddress( hostAddress );
+        
+        // Update the parent
+        container.setParentTLV( tlv.getParent() );
+
+        if ( IS_DEBUG )
+        {
+            LOG.debug( "HostAddress added : {}", hostAddress );
+        }
+        
+        container.setGrammarEndAllowed( true );
+    }
+}

Modified: directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/components/HostAddresses.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/components/HostAddresses.java?rev=1034497&r1=1034496&r2=1034497&view=diff
==============================================================================
--- directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/components/HostAddresses.java
(original)
+++ directory/apacheds/trunk/kerberos-codec/src/main/java/org/apache/directory/shared/kerberos/components/HostAddresses.java
Fri Nov 12 18:04:34 2010
@@ -40,13 +40,13 @@ import org.slf4j.LoggerFactory;
  * Store a list of addresses.
  * 
  * The ASN.1 grammar is :
- * 
+ * <pre>
  * -- NOTE: HostAddresses is always used as an OPTIONAL field and
  * -- should not be empty.
  * HostAddresses   -- NOTE: subtly different from rfc1510,
  *                 -- but has a value mapping and encodes the same
  *         ::= SEQUENCE OF HostAddress
- *
+ *</pre>
  * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
  */
 public class HostAddresses extends AbstractAsn1Object

Added: directory/apacheds/trunk/kerberos-codec/src/test/java/org/apache/directory/shared/kerberos/codec/HostAddressesDecoderTest.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/kerberos-codec/src/test/java/org/apache/directory/shared/kerberos/codec/HostAddressesDecoderTest.java?rev=1034497&view=auto
==============================================================================
--- directory/apacheds/trunk/kerberos-codec/src/test/java/org/apache/directory/shared/kerberos/codec/HostAddressesDecoderTest.java
(added)
+++ directory/apacheds/trunk/kerberos-codec/src/test/java/org/apache/directory/shared/kerberos/codec/HostAddressesDecoderTest.java
Fri Nov 12 18:04:34 2010
@@ -0,0 +1,186 @@
+/*
+ *  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.hostAddress.HostAddressContainer;
+import org.apache.directory.shared.kerberos.codec.hostAddresses.HostAddressesContainer;
+import org.apache.directory.shared.kerberos.codec.types.HostAddrType;
+import org.apache.directory.shared.kerberos.components.HostAddress;
+import org.apache.directory.shared.kerberos.components.HostAddresses;
+import org.apache.directory.shared.ldap.util.StringTools;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+
+/**
+ * Test the HostAddresses decoder.
+ * 
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+@RunWith(ConcurrentJunitRunner.class)
+@Concurrent()
+public class HostAddressesDecoderTest
+{
+    /**
+     * Test the decoding of a HostAddresses
+     */
+    @Test
+    public void testHostAddresses()
+    {
+        Asn1Decoder kerberosDecoder = new Asn1Decoder();
+
+        ByteBuffer stream = ByteBuffer.allocate( 0x44 );
+        
+        stream.put( new byte[]
+            { 
+              0x30, 0x42,
+                0x30, 0x14,
+                  (byte)0xA0, 0x03,                 // addr-type
+                    0x02, 0x01, 0x02,               // IPV4
+                  (byte)0xA1, 0x0D,                 // address : 192.168.0.1
+                    0x04, 0x0B, '1', '9', '2', '.', '1', '6', '8', '.', '0', '.', '1',
+                0x30, 0x14,
+                  (byte)0xA0, 0x03,                 // addr-type
+                    0x02, 0x01, 0x02,               // IPV4
+                  (byte)0xA1, 0x0D,                 // address : 192.168.0.2
+                    0x04, 0x0B, '1', '9', '2', '.', '1', '6', '8', '.', '0', '.', '2',
+                0x30, 0x14,
+                  (byte)0xA0, 0x03,                 // addr-type
+                    0x02, 0x01, 0x02,               // IPV4
+                  (byte)0xA1, 0x0D,                 // address : 192.168.0.3
+                    0x04, 0x0B, '1', '9', '2', '.', '1', '6', '8', '.', '0', '.', '3'
+            } );
+
+        String decodedPdu = StringTools.dumpBytes( stream.array() );
+        stream.flip();
+
+        // Allocate a HostAddresses Container
+        Asn1Container hostAddressesContainer = new HostAddressesContainer();
+        hostAddressesContainer.setStream( stream );
+
+        // Decode the HostAddresses PDU
+        try
+        {
+            kerberosDecoder.decode( stream, hostAddressesContainer );
+        }
+        catch ( DecoderException de )
+        {
+            fail( de.getMessage() );
+        }
+
+        // Check the decoded HostAddress
+        HostAddresses hostAddresses = ( ( HostAddressesContainer ) hostAddressesContainer
).getHostAddresses();
+
+        assertEquals( 3, hostAddresses.getAddresses().length );
+        
+        String[] expected = new String[]{ "192.168.0.1", "192.168.0.2", "192.168.0.3" };
+        int i = 0;
+        
+        for ( HostAddress hostAddress : hostAddresses.getAddresses() )
+        {
+            assertEquals( HostAddrType.ADDRTYPE_INET, hostAddress.getAddrType() );
+            assertTrue( Arrays.equals( StringTools.getBytesUtf8( expected[i] ), hostAddress.getAddress()
) );
+            i++;
+        }
+
+        // Check the encoding
+        ByteBuffer bb = ByteBuffer.allocate( hostAddresses.computeLength() );
+        
+        try
+        {
+            bb = hostAddresses.encode( bb );
+    
+            // Check the length
+            assertEquals( 0x44, bb.limit() );
+    
+            String encodedPdu = StringTools.dumpBytes( bb.array() );
+    
+            assertEquals( encodedPdu, decodedPdu );
+        }
+        catch ( EncoderException ee )
+        {
+            fail();
+        }
+    }
+    
+    
+    /**
+     * Test the decoding of a HostAddresses with nothing in it
+     */
+    @Test( expected = DecoderException.class)
+    public void testHostAddressEmpty() throws DecoderException
+    {
+        Asn1Decoder kerberosDecoder = new Asn1Decoder();
+
+        ByteBuffer stream = ByteBuffer.allocate( 0x02 );
+        
+        stream.put( new byte[]
+            { 0x30, 0x00 } );
+
+        stream.flip();
+
+        // Allocate a HostAddress Container
+        Asn1Container hostAddressesContainer = new HostAddressesContainer();
+
+        // Decode the HostAddress PDU
+        kerberosDecoder.decode( stream, hostAddressesContainer );
+        fail();
+    }
+    
+    
+    /**
+     * Test the decoding of a HostAddresses with empty hostAddress in it
+     */
+    @Test( expected = DecoderException.class)
+    public void testHostAddressesNoHostAddress() throws DecoderException
+    {
+        Asn1Decoder kerberosDecoder = new Asn1Decoder();
+
+        ByteBuffer stream = ByteBuffer.allocate( 0x04 );
+        
+        stream.put( new byte[]
+            { 0x30, 0x02,
+                (byte)0x30, 0x00                  // empty HostAddress
+            } );
+
+        stream.flip();
+
+        // Allocate a HostAddress Container
+        Asn1Container hostAddressesContainer = new HostAddressContainer();
+
+        // Decode the HostAddresses PDU
+        kerberosDecoder.decode( stream, hostAddressesContainer );
+        fail();
+    }
+}



Mime
View raw message