directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From elecha...@apache.org
Subject svn commit: r1646604 - in /directory/shared/trunk/ldap/codec/core/src: main/java/org/apache/directory/api/ldap/codec/controls/proxiedauthz/ test/java/org/apache/directory/api/ldap/codec/controls/proxiedauthz/
Date Fri, 19 Dec 2014 01:25:36 GMT
Author: elecharny
Date: Fri Dec 19 01:25:35 2014
New Revision: 1646604

URL: http://svn.apache.org/r1646604
Log:
Added the ProxiedAuthz control, and its tests

Added:
    directory/shared/trunk/ldap/codec/core/src/main/java/org/apache/directory/api/ldap/codec/controls/proxiedauthz/
    directory/shared/trunk/ldap/codec/core/src/main/java/org/apache/directory/api/ldap/codec/controls/proxiedauthz/ProxiedAuthzContainer.java
    directory/shared/trunk/ldap/codec/core/src/main/java/org/apache/directory/api/ldap/codec/controls/proxiedauthz/ProxiedAuthzDecorator.java
    directory/shared/trunk/ldap/codec/core/src/main/java/org/apache/directory/api/ldap/codec/controls/proxiedauthz/ProxiedAuthzFactory.java
    directory/shared/trunk/ldap/codec/core/src/main/java/org/apache/directory/api/ldap/codec/controls/proxiedauthz/ProxiedAuthzGrammar.java
    directory/shared/trunk/ldap/codec/core/src/main/java/org/apache/directory/api/ldap/codec/controls/proxiedauthz/ProxiedAuthzStates.java
    directory/shared/trunk/ldap/codec/core/src/test/java/org/apache/directory/api/ldap/codec/controls/proxiedauthz/
    directory/shared/trunk/ldap/codec/core/src/test/java/org/apache/directory/api/ldap/codec/controls/proxiedauthz/ProxiedAuthzControlTest.java

Added: directory/shared/trunk/ldap/codec/core/src/main/java/org/apache/directory/api/ldap/codec/controls/proxiedauthz/ProxiedAuthzContainer.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/codec/core/src/main/java/org/apache/directory/api/ldap/codec/controls/proxiedauthz/ProxiedAuthzContainer.java?rev=1646604&view=auto
==============================================================================
--- directory/shared/trunk/ldap/codec/core/src/main/java/org/apache/directory/api/ldap/codec/controls/proxiedauthz/ProxiedAuthzContainer.java
(added)
+++ directory/shared/trunk/ldap/codec/core/src/main/java/org/apache/directory/api/ldap/codec/controls/proxiedauthz/ProxiedAuthzContainer.java
Fri Dec 19 01:25:35 2014
@@ -0,0 +1,119 @@
+/*
+ *  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.api.ldap.codec.controls.proxiedauthz;
+
+
+import org.apache.directory.api.asn1.ber.AbstractContainer;
+import org.apache.directory.api.ldap.codec.api.LdapApiService;
+import org.apache.directory.api.ldap.model.message.controls.ProxiedAuthz;
+
+
+/**
+ * The ProxiedAuthz control container.
+ * 
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public class ProxiedAuthzContainer extends AbstractContainer
+{
+    /** ProxiedAuthzControl */
+    private ProxiedAuthzDecorator control;
+
+    /** The codec that encodes and decodes */
+    private LdapApiService codec;
+
+
+    /**
+     * Creates a new ProxiedAuthzContainer object. We will store one
+     * grammar, it's enough ...
+     */
+    public ProxiedAuthzContainer( LdapApiService codec )
+    {
+        super();
+        this.codec = codec;
+        grammar = ProxiedAuthzGrammar.getInstance();
+        setTransition( ProxiedAuthzStates.START_STATE );
+    }
+
+
+    /**
+     * Creates a container with decorator, optionally decorating the supplied
+     * Control if it is not a decorator implementation.
+     *
+     * @param control The EntryChange ControlDecorator, or a Control to be
+     * wrapped by a new decorator.
+     */
+    public ProxiedAuthzContainer( LdapApiService codec, ProxiedAuthz control )
+    {
+        this( codec );
+        decorate( control );
+    }
+
+
+    /**
+     * @return Returns the ProxiedAuthzControl.
+     */
+    public ProxiedAuthzDecorator getProxiedAuthzDecorator()
+    {
+        return control;
+    }
+
+
+    /**
+     * Checks to see if the supplied ProxiedAuthz implementation is a decorator
+     * and if so just sets the ProxiedAuthzDecorator to it. Otherwise the supplied
+     * control is decorated by creating a new ProxiedAuthzDecorator to wrap the
+     * object.
+     *
+     * @param control The ProxiedAuthz Control to wrap, if it is not a decorator.
+     */
+    public void decorate( ProxiedAuthz control )
+    {
+        if ( control instanceof ProxiedAuthzDecorator )
+        {
+            this.control = ( ProxiedAuthzDecorator ) control;
+        }
+        else
+        {
+            this.control = new ProxiedAuthzDecorator( codec, control );
+        }
+    }
+
+
+    /**
+     * Set a ProxiedAuthzontrol Object into the container. It will be completed
+     * by the ldapDecoder.
+     * 
+     * @param control the ProxiedAuthzControl to set.
+     */
+    public void setProxiedAuthzDecorator( ProxiedAuthzDecorator control )
+    {
+        this.control = control;
+    }
+
+
+    /**
+     * Clean the container
+     */
+    public void clean()
+    {
+        super.clean();
+        control = null;
+    }
+}

Added: directory/shared/trunk/ldap/codec/core/src/main/java/org/apache/directory/api/ldap/codec/controls/proxiedauthz/ProxiedAuthzDecorator.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/codec/core/src/main/java/org/apache/directory/api/ldap/codec/controls/proxiedauthz/ProxiedAuthzDecorator.java?rev=1646604&view=auto
==============================================================================
--- directory/shared/trunk/ldap/codec/core/src/main/java/org/apache/directory/api/ldap/codec/controls/proxiedauthz/ProxiedAuthzDecorator.java
(added)
+++ directory/shared/trunk/ldap/codec/core/src/main/java/org/apache/directory/api/ldap/codec/controls/proxiedauthz/ProxiedAuthzDecorator.java
Fri Dec 19 01:25:35 2014
@@ -0,0 +1,196 @@
+/*
+ *  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.api.ldap.codec.controls.proxiedauthz;
+
+
+import java.nio.ByteBuffer;
+
+import org.apache.directory.api.asn1.Asn1Object;
+import org.apache.directory.api.asn1.DecoderException;
+import org.apache.directory.api.asn1.EncoderException;
+import org.apache.directory.api.asn1.ber.Asn1Decoder;
+import org.apache.directory.api.asn1.ber.tlv.BerValue;
+import org.apache.directory.api.asn1.ber.tlv.TLV;
+import org.apache.directory.api.i18n.I18n;
+import org.apache.directory.api.ldap.codec.api.ControlDecorator;
+import org.apache.directory.api.ldap.codec.api.LdapApiService;
+import org.apache.directory.api.ldap.model.message.controls.ProxiedAuthz;
+import org.apache.directory.api.ldap.model.message.controls.ProxiedAuthzImpl;
+import org.apache.directory.api.util.Strings;
+
+
+/**
+ * An ProxiedAuthz implementation, that wraps and decorates the Control with codec
+ * specific functionality.
+ *
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public class ProxiedAuthzDecorator extends ControlDecorator<ProxiedAuthz> implements
ProxiedAuthz
+{
+    /** A temporary storage for the authzId */
+    private byte[] authzIdBytes = null;
+
+    /** An instance of this decoder */
+    private static final Asn1Decoder decoder = new Asn1Decoder();
+
+
+    /**
+     * Creates a new instance of ProxiedAuthzDecoder wrapping a newly created
+     * ProxiedAuthz Control object.
+     */
+    public ProxiedAuthzDecorator( LdapApiService codec )
+    {
+        super( codec, new ProxiedAuthzImpl() );
+    }
+
+
+    /**
+     * Creates a new instance of ProxiedAuthzDecorator wrapping the supplied
+     * ProxiedAuthz Control.
+     *
+     * @param control The ProxiedAuthz Control to be decorated.
+     */
+    public ProxiedAuthzDecorator( LdapApiService codec, ProxiedAuthz control )
+    {
+        super( codec, control );
+    }
+
+
+    /**
+     * Internally used to not have to cast the decorated Control.
+     *
+     * @return the decorated Control.
+     */
+    private ProxiedAuthz getProxiedAuthz()
+    {
+        return ( ProxiedAuthz ) getDecorated();
+    }
+
+
+    /**
+     * Compute the ProxiedAuthzControl length 
+     * 
+     *  0x04 L1 authzId] 
+     */
+    public int computeLength()
+    {
+        int valueLength = 0;
+
+        if ( getAuthzId() != null )
+        {
+            authzIdBytes = Strings.getBytesUtf8( getAuthzId() );
+            valueLength = 1 + TLV.getNbBytes( authzIdBytes.length ) + authzIdBytes.length;
+        }
+        else
+        {
+            // Empty string
+            valueLength = 1 + 1;
+        }
+
+        return valueLength;
+    }
+
+
+    /**
+     * Encodes the ProxiedAuthz control.
+     * 
+     * @param buffer The encoded sink
+     * @return A ByteBuffer that contains the encoded PDU
+     * @throws EncoderException If anything goes wrong.
+     */
+    public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
+    {
+        if ( buffer == null )
+        {
+            throw new EncoderException( I18n.err( I18n.ERR_04023 ) );
+        }
+
+        if ( getAuthzId() != null )
+        {
+            BerValue.encode( buffer, authzIdBytes );
+        }
+
+        return buffer;
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public byte[] getValue()
+    {
+        if ( value == null )
+        {
+            try
+            {
+                computeLength();
+                ByteBuffer buffer = ByteBuffer.allocate( valueLength );
+
+                if ( authzIdBytes != null )
+                {
+                    BerValue.encode( buffer, authzIdBytes );
+                }
+                else
+                {
+                    BerValue.encode( buffer, Strings.EMPTY_BYTES );
+                }
+
+                value = buffer.array();
+            }
+            catch ( Exception e )
+            {
+                return null;
+            }
+        }
+
+        return value;
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public String getAuthzId()
+    {
+        return getProxiedAuthz().getAuthzId();
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public void setAuthzId( String authzId )
+    {
+        getProxiedAuthz().setAuthzId( authzId );
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public Asn1Object decode( byte[] controlBytes ) throws DecoderException
+    {
+        ByteBuffer bb = ByteBuffer.wrap( controlBytes );
+        ProxiedAuthzContainer container = new ProxiedAuthzContainer( getCodecService(), this
);
+        decoder.decode( bb, container );
+        return this;
+    }
+}

Added: directory/shared/trunk/ldap/codec/core/src/main/java/org/apache/directory/api/ldap/codec/controls/proxiedauthz/ProxiedAuthzFactory.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/codec/core/src/main/java/org/apache/directory/api/ldap/codec/controls/proxiedauthz/ProxiedAuthzFactory.java?rev=1646604&view=auto
==============================================================================
--- directory/shared/trunk/ldap/codec/core/src/main/java/org/apache/directory/api/ldap/codec/controls/proxiedauthz/ProxiedAuthzFactory.java
(added)
+++ directory/shared/trunk/ldap/codec/core/src/main/java/org/apache/directory/api/ldap/codec/controls/proxiedauthz/ProxiedAuthzFactory.java
Fri Dec 19 01:25:35 2014
@@ -0,0 +1,77 @@
+/*
+ *   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.api.ldap.codec.controls.proxiedauthz;
+
+
+import org.apache.directory.api.ldap.codec.api.CodecControl;
+import org.apache.directory.api.ldap.codec.api.ControlFactory;
+import org.apache.directory.api.ldap.codec.api.LdapApiService;
+import org.apache.directory.api.ldap.model.message.controls.ProxiedAuthz;
+
+
+/**
+ * A {@link ControlFactory} for {@link ProxiedAuthz} controls.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev$, $Date$
+ */
+public class ProxiedAuthzFactory implements ControlFactory<ProxiedAuthz>
+{
+    /** The LDAP codec service */
+    private LdapApiService codec;
+
+
+    /**
+     * Creates a new instance of ProxiedAuthzFactory.
+     *
+     * @param codec The LDAP codec.
+     */
+    public ProxiedAuthzFactory( LdapApiService codec )
+    {
+        this.codec = codec;
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public String getOid()
+    {
+        return ProxiedAuthz.OID;
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public CodecControl<ProxiedAuthz> newCodecControl()
+    {
+        return new ProxiedAuthzDecorator( codec );
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public CodecControl<ProxiedAuthz> newCodecControl( ProxiedAuthz control )
+    {
+        return new ProxiedAuthzDecorator( codec, control );
+    }
+}

Added: directory/shared/trunk/ldap/codec/core/src/main/java/org/apache/directory/api/ldap/codec/controls/proxiedauthz/ProxiedAuthzGrammar.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/codec/core/src/main/java/org/apache/directory/api/ldap/codec/controls/proxiedauthz/ProxiedAuthzGrammar.java?rev=1646604&view=auto
==============================================================================
--- directory/shared/trunk/ldap/codec/core/src/main/java/org/apache/directory/api/ldap/codec/controls/proxiedauthz/ProxiedAuthzGrammar.java
(added)
+++ directory/shared/trunk/ldap/codec/core/src/main/java/org/apache/directory/api/ldap/codec/controls/proxiedauthz/ProxiedAuthzGrammar.java
Fri Dec 19 01:25:35 2014
@@ -0,0 +1,113 @@
+/*
+ *  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.api.ldap.codec.controls.proxiedauthz;
+
+
+import org.apache.directory.api.asn1.DecoderException;
+import org.apache.directory.api.asn1.ber.grammar.AbstractGrammar;
+import org.apache.directory.api.asn1.ber.grammar.Grammar;
+import org.apache.directory.api.asn1.ber.grammar.GrammarAction;
+import org.apache.directory.api.asn1.ber.grammar.GrammarTransition;
+import org.apache.directory.api.asn1.ber.tlv.BerValue;
+import org.apache.directory.api.asn1.ber.tlv.UniversalTag;
+import org.apache.directory.api.util.Strings;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+/**
+ * This class implements the ProxiedAuthzControl. All the actions are declared in
+ * this class. As it is a singleton, these declaration are only done once.
+ * 
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public final class ProxiedAuthzGrammar extends AbstractGrammar<ProxiedAuthzContainer>
+{
+    /** The logger */
+    static final Logger LOG = LoggerFactory.getLogger( ProxiedAuthzGrammar.class );
+
+    /** Speedup for logs */
+    static final boolean IS_DEBUG = LOG.isDebugEnabled();
+
+    /** The instance of grammar. ProxiedAuthzGrammar is a singleton */
+    private static Grammar<?> instance = new ProxiedAuthzGrammar();
+
+
+    /**
+     * Creates a new ProxiedAuthzGrammar object.
+     */
+    @SuppressWarnings("unchecked")
+    private ProxiedAuthzGrammar()
+    {
+        setName( ProxiedAuthzGrammar.class.getName() );
+
+        // Create the transitions table
+        super.transitions = new GrammarTransition[ProxiedAuthzStates.LAST_PA_STATE.ordinal()][256];
+
+        // ============================================================================================
+        // Transition from start state to Proxied Authz authzId
+        // ============================================================================================
+        // ProxiedAuthzNotification ::= OCTET_STRING authzId
+        //
+        // Initialization of the structure
+        super.transitions[ProxiedAuthzStates.START_STATE.ordinal()][UniversalTag.OCTET_STRING.getValue()]
=
+            new GrammarTransition<ProxiedAuthzContainer>( ProxiedAuthzStates.START_STATE,
+                ProxiedAuthzStates.AUTHZ_ID_STATE,
+                UniversalTag.OCTET_STRING.getValue(),
+                new GrammarAction<ProxiedAuthzContainer>( "Set ProxiedAuthzControl
authzId" )
+                {
+                    public void action( ProxiedAuthzContainer container ) throws DecoderException
+                    {
+                        BerValue value = container.getCurrentTLV().getValue();
+                        String authzId;
+
+                        try
+                        {
+                            authzId = Strings.utf8ToString( value.getData() );
+                            container.getProxiedAuthzDecorator().setAuthzId( authzId );
+                        }
+                        catch ( RuntimeException re )
+                        {
+                            LOG.error( re.getMessage() );
+                            throw new DecoderException( re.getMessage() );
+                        }
+
+                        if ( IS_DEBUG )
+                        {
+                            LOG.debug( "authzId = " + authzId );
+                        }
+
+                        // We can have an END transition
+                        container.setGrammarEndAllowed( true );
+                    }
+                } );
+    }
+
+
+    /**
+     * This class is a singleton.
+     * 
+     * @return An instance on this grammar
+     */
+    public static Grammar<?> getInstance()
+    {
+        return instance;
+    }
+}

Added: directory/shared/trunk/ldap/codec/core/src/main/java/org/apache/directory/api/ldap/codec/controls/proxiedauthz/ProxiedAuthzStates.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/codec/core/src/main/java/org/apache/directory/api/ldap/codec/controls/proxiedauthz/ProxiedAuthzStates.java?rev=1646604&view=auto
==============================================================================
--- directory/shared/trunk/ldap/codec/core/src/main/java/org/apache/directory/api/ldap/codec/controls/proxiedauthz/ProxiedAuthzStates.java
(added)
+++ directory/shared/trunk/ldap/codec/core/src/main/java/org/apache/directory/api/ldap/codec/controls/proxiedauthz/ProxiedAuthzStates.java
Fri Dec 19 01:25:35 2014
@@ -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.api.ldap.codec.controls.proxiedauthz;
+
+
+import org.apache.directory.api.asn1.ber.grammar.Grammar;
+import org.apache.directory.api.asn1.ber.grammar.States;
+
+
+/**
+ * This class store the ProxiedAuthzControl's grammar constants. It is also used
+ * for debugging purposes.
+ * 
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public enum ProxiedAuthzStates implements States
+{
+    /** The END_STATE */
+    END_STATE,
+
+    // =========================================================================
+    // Proxied Authz control grammar states
+    // =========================================================================
+
+    /** Sequence Tag */
+    START_STATE,
+
+    /** authzId */
+    AUTHZ_ID_STATE,
+
+    /** terminal state */
+    LAST_PA_STATE;
+
+    /**
+     * Get the grammar name
+     * 
+     * @return The grammar name
+     */
+    public String getGrammarName()
+    {
+        return "PROXIED_AUTHZ_GRAMMAR";
+    }
+
+
+    /**
+     * Get the grammar name
+     * 
+     * @param grammar The grammar class
+     * @return The grammar name
+     */
+    public String getGrammarName( Grammar<?> grammar )
+    {
+        if ( grammar instanceof ProxiedAuthzGrammar )
+        {
+            return "PROXIED_AUTHZ_GRAMMAR";
+        }
+
+        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 == END_STATE.ordinal() ) ? "PA_END_STATE" : name() );
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public boolean isEndState()
+    {
+        return this == END_STATE;
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public ProxiedAuthzStates getStartState()
+    {
+        return START_STATE;
+    }
+}

Added: directory/shared/trunk/ldap/codec/core/src/test/java/org/apache/directory/api/ldap/codec/controls/proxiedauthz/ProxiedAuthzControlTest.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/codec/core/src/test/java/org/apache/directory/api/ldap/codec/controls/proxiedauthz/ProxiedAuthzControlTest.java?rev=1646604&view=auto
==============================================================================
--- directory/shared/trunk/ldap/codec/core/src/test/java/org/apache/directory/api/ldap/codec/controls/proxiedauthz/ProxiedAuthzControlTest.java
(added)
+++ directory/shared/trunk/ldap/codec/core/src/test/java/org/apache/directory/api/ldap/codec/controls/proxiedauthz/ProxiedAuthzControlTest.java
Fri Dec 19 01:25:35 2014
@@ -0,0 +1,227 @@
+/*
+ *  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.api.ldap.codec.controls.proxiedauthz;
+
+
+import static org.junit.Assert.assertEquals;
+
+import java.nio.ByteBuffer;
+
+import com.mycila.junit.concurrent.Concurrency;
+import com.mycila.junit.concurrent.ConcurrentJunitRunner;
+
+import org.apache.directory.api.asn1.DecoderException;
+import org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest;
+import org.apache.directory.api.ldap.model.message.controls.ProxiedAuthz;
+import org.apache.directory.api.util.Strings;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+
+/**
+ * Test the ProxiedAuthzControlTest codec
+ * 
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+@RunWith(ConcurrentJunitRunner.class)
+@Concurrency()
+public class ProxiedAuthzControlTest extends AbstractCodecServiceTest
+{
+    /**
+     * Test the decoding of a ProxiedAuthzControl with a DN user
+     */
+    @Test
+    public void testDecodeProxiedAuthzControlDnSuccess() throws Exception
+    {
+        ByteBuffer bb = ByteBuffer.allocate( 0x16 );
+        bb.put( new byte[]
+            {
+                0x04, 0x14, // ProxiedAuthzNotification ::= dn:dc=example,dc=com
+                  'd', 'n', ':', 'd', 'c', '=', 'e', 'x', 'a', 'm', 'p', 'l', 'e', ',', 'd',
'c', '=', 'c', 'o', 'm'
+        } );
+        bb.flip();
+
+        ProxiedAuthzDecorator decorator = new ProxiedAuthzDecorator( codec );
+
+        ProxiedAuthz proxiedAuthz = ( ProxiedAuthz ) decorator.decode( bb.array() );
+
+        assertEquals( "dn:dc=example,dc=com", proxiedAuthz.getAuthzId() );
+    }
+
+
+    /**
+     * Test the decoding of a ProxiedAuthzControl with a normal user
+     */
+    @Test
+    public void testDecodeProxiedAuthzControlUSuccess() throws Exception
+    {
+        ByteBuffer bb = ByteBuffer.allocate( 0x0E );
+        bb.put( new byte[]
+            {
+                0x04, 0x0C, // ProxiedAuthzNotification ::= u:el├ęcharny
+                  'u', ':', 'e', 'l', (byte)0xc3, (byte)0xa9, 'c', 'h', 'a', 'r', 'n', 'y'
+        } );
+        bb.flip();
+
+        ProxiedAuthzDecorator decorator = new ProxiedAuthzDecorator( codec );
+
+        ProxiedAuthz proxiedAuthz = ( ProxiedAuthz ) decorator.decode( bb.array() );
+
+        assertEquals( "u:el\u00e9charny", proxiedAuthz.getAuthzId() );
+    }
+
+
+    /**
+     * Test the decoding of a ProxiedAuthzControl with a anonymous user
+     */
+    @Test
+    public void testDecodeProxiedAuthzControlAnonymousSuccess() throws Exception
+    {
+        ByteBuffer bb = ByteBuffer.allocate( 0x06 );
+        bb.put( new byte[]
+            {
+                0x04, 0x00, // ProxiedAuthzNotification ::= anonymous
+        } );
+        bb.flip();
+
+        ProxiedAuthzDecorator decorator = new ProxiedAuthzDecorator( codec );
+
+        ProxiedAuthz proxiedAuthz = ( ProxiedAuthz ) decorator.decode( bb.array() );
+
+        assertEquals( "", proxiedAuthz.getAuthzId() );
+    }
+    
+    
+    /**
+     * Test the decoding of a ProxiedAuthzControl with a wrong DN user
+     */
+    @Test( expected = DecoderException.class)
+    public void testDecodeProxiedAuthzControlWrongDn() throws Exception
+    {
+        ByteBuffer bb = ByteBuffer.allocate( 0x12 );
+        bb.put( new byte[]
+            {
+                0x04, 0x10, // ProxiedAuthzNotification ::= dn:dc=example,dc=com
+                  'd', 'n', ':', 'd', 'c', '=', 'e', 'x', 'a', 'm', 'p', 'l', 'e', ',', 'd',
'c'
+        } );
+        bb.flip();
+
+        ProxiedAuthzDecorator decorator = new ProxiedAuthzDecorator( codec );
+
+        decorator.decode( bb.array() );
+    }
+    
+    
+    /**
+     * Test the decoding of a ProxiedAuthzControl with a wrong user
+     */
+    @Test( expected = DecoderException.class)
+    public void testDecodeProxiedAuthzControlWrongAuthzId() throws Exception
+    {
+        ByteBuffer bb = ByteBuffer.allocate( 0x0A );
+        bb.put( new byte[]
+            {
+                0x04, 0x08, // ProxiedAuthzNotification ::= dn:dc=example,dc=com
+                  'v', 'n', ':', 'w', 'r', 'o', 'n', 'g'
+        } );
+        bb.flip();
+
+        ProxiedAuthzDecorator decorator = new ProxiedAuthzDecorator( codec );
+
+        decorator.decode( bb.array() );
+    }
+
+
+    /**
+     * Test encoding of a ProxiedAuthzControl.
+     */
+    @Test
+    public void testEncodeProxiedDnAuthzControl() throws Exception
+    {
+        ByteBuffer bb = ByteBuffer.allocate( 0x16 );
+        bb.put( new byte[]
+            {
+            0x04, 0x14, // ProxiedAuthzNotification ::= dn:dc=example,dc=com
+              'd', 'n', ':', 'd', 'c', '=', 'e', 'x', 'a', 'm', 'p', 'l', 'e', ',', 'd',
'c', '=', 'c', 'o', 'm'
+            } );
+
+        String expected = Strings.dumpBytes( bb.array() );
+        bb.flip();
+
+        ProxiedAuthzDecorator decorator = new ProxiedAuthzDecorator( codec );
+
+        ProxiedAuthz proxiedAuthz = ( ProxiedAuthz ) decorator.getDecorated();
+        proxiedAuthz.setAuthzId( "dn:dc=example,dc=com" );
+        bb = decorator.encode( ByteBuffer.allocate( decorator.computeLength() ) );
+        String decoded = Strings.dumpBytes( bb.array() );
+        assertEquals( expected, decoded );
+    }
+
+
+    /**
+     * Test encoding of a ProxiedAuthzControl.
+     */
+    @Test
+    public void testEncodeProxiedUserAuthzControl() throws Exception
+    {
+        ByteBuffer bb = ByteBuffer.allocate( 0x0E );
+        bb.put( new byte[]
+            {
+                0x04, 0x0C, // ProxiedAuthzNotification ::= u:el├ęcharny
+                  'u', ':', 'e', 'l', (byte)0xc3, (byte)0xa9, 'c', 'h', 'a', 'r', 'n', 'y'
+        } );
+
+        String expected = Strings.dumpBytes( bb.array() );
+        bb.flip();
+
+        ProxiedAuthzDecorator decorator = new ProxiedAuthzDecorator( codec );
+
+        ProxiedAuthz proxiedAuthz = ( ProxiedAuthz ) decorator.getDecorated();
+        proxiedAuthz.setAuthzId( "u:el\u00e9charny" );
+        bb = decorator.encode( ByteBuffer.allocate( decorator.computeLength() ) );
+        String decoded = Strings.dumpBytes( bb.array() );
+        assertEquals( expected, decoded );
+    }
+
+
+    /**
+     * Test encoding of a ProxiedAuthzControl.
+     */
+    @Test
+    public void testEncodeProxiedAnonymousAuthzControl() throws Exception
+    {
+        ByteBuffer bb = ByteBuffer.allocate( 0x02 );
+        bb.put( new byte[]
+            {
+                0x04, 0x00, // ProxiedAuthzNotification ::= anonymous
+        } );
+
+        String expected = Strings.dumpBytes( bb.array() );
+        bb.flip();
+
+        ProxiedAuthzDecorator decorator = new ProxiedAuthzDecorator( codec );
+
+        ProxiedAuthz proxiedAuthz = ( ProxiedAuthz ) decorator.getDecorated();
+        proxiedAuthz.setAuthzId( "" );
+        bb = decorator.encode( ByteBuffer.allocate( decorator.computeLength() ) );
+        String decoded = Strings.dumpBytes( bb.array() );
+        assertEquals( expected, decoded );
+    }
+}



Mime
View raw message