Author: elecharny
Date: Thu Apr 14 22:29:53 2005
New Revision: 161387

URL: http://svn.apache.org/viewcvs?view=rev&rev=161387
Log:
Created two test cases : BindRequest and BindResponse

Added:
    
directory/sandbox/trunk/asn1-new-codec/src/test/org/apache/asn1/ldap/codec/BindRequestTest.java
    
directory/sandbox/trunk/asn1-new-codec/src/test/org/apache/asn1/ldap/codec/BindResponseTest.java

Added: 
directory/sandbox/trunk/asn1-new-codec/src/test/org/apache/asn1/ldap/codec/BindRequestTest.java
URL: 
http://svn.apache.org/viewcvs/directory/sandbox/trunk/asn1-new-codec/src/test/org/apache/asn1/ldap/codec/BindRequestTest.java?view=auto&rev=161387
==============================================================================
--- 
directory/sandbox/trunk/asn1-new-codec/src/test/org/apache/asn1/ldap/codec/BindRequestTest.java
 (added)
+++ 
directory/sandbox/trunk/asn1-new-codec/src/test/org/apache/asn1/ldap/codec/BindRequestTest.java
 Thu Apr 14 22:29:53 2005
@@ -0,0 +1,308 @@
+/*
+ *   Copyright 2005 The Apache Software Foundation
+ *
+ *   Licensed 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.asn1.ldap.codec;
+
+import java.nio.ByteBuffer;
+
+import org.apache.asn1.ber.Asn1Decoder;
+import org.apache.asn1.ber.containers.IAsn1Container;
+import org.apache.asn1.ldap.pojo.BindRequestPOJO;
+import org.apache.asn1.ldap.pojo.LdapMessagePOJO;
+import org.apache.asn1.ldap.pojo.SaslAuthenticationPOJO;
+import org.apache.asn1.ldap.pojo.SimpleAuthenticationPOJO;
+import org.apache.asn1.util.pools.PoolEnum;
+import org.apache.asn1.util.pools.PoolException;
+import org.apache.log4j.Logger;
+import org.apache.log4j.PropertyConfigurator;
+
+import junit.framework.Assert;
+import junit.framework.TestCase;
+
+/**
+ * @author <a href="mailto:[email protected]";>Apache Directory 
Project</a>
+ */
+public class BindRequestTest extends TestCase {
+    /** Logger */
+    protected static Logger log = Logger.getLogger( BindRequestTest.class );
+
+    static
+    {
+        PropertyConfigurator.configure( "conf/log4j.conf" );
+    }
+
+    /**
+     * Test the decoding of a BindRequest with Simple authentication
+     * and no controls
+     */
+    public void testDecodeBindRequestSimpleNoControls()
+    {
+        Asn1Decoder ldapDecoder = new Asn1Decoder();
+
+        ByteBuffer  stream      = ByteBuffer.allocate( 0x35 );
+        stream.put(
+            new byte[]
+            {
+                0x30, 0x33,            // LDAPMessage ::=SEQUENCE {
+                               0x02, 0x01, 0x01,       //         messageID 
MessageID
+                               0x60, 0x2E,             //        CHOICE { ..., 
bindRequest BindRequest, ...
+                                               // BindRequest ::= 
APPLICATION[0] SEQUENCE {
+                               0x02, 0x01, 0x03,       //        version 
INTEGER (1..127),
+                               0x04, 0x1F,             //        name LDAPDN,
+                               'u', 'i', 'd', '=', 'a', 'k', 'a', 'r', 'a', 
's', 'u', 'l', 'u', ',', 'd', 'c', '=',
+                'e', 'x', 'a', 'm', 'p', 'l', 'e', ',', 'd', 'c', '=', 'c', 
'o', 'm',
+                               ( byte ) 0x80, 0x08, //        authentication 
AuthenticationChoice
+                                     // AuthenticationChoice ::= CHOICE { 
simple [0] OCTET STRING, ...
+                               'p', 'a', 's', 's', 'w', 'o', 'r', 'd'
+            } );
+
+        stream.flip();
+
+        // Allocate a BindRequest Container
+        IAsn1Container ldapMessageContainer = null;
+
+        try
+        {
+            ldapMessageContainer = ( IAsn1Container ) ldapDecoder.allocate(
+                    PoolEnum.LDAP_MESSAGE_CONTAINER_POOL );
+        }
+        catch ( PoolException pe )
+        {
+            Assert.fail("Cannot allocat a LdapMessageContainer : " + 
pe.getMessage());
+        }
+
+        ldapMessageContainer.setPoolManager( ldapDecoder.getPoolManager() );
+
+        try
+        {
+            ldapDecoder.decode( stream, ldapMessageContainer );
+        }
+        catch ( DecoderException de )
+        {
+            de.printStackTrace();
+            Assert.fail( de.getMessage() );
+        }
+       
+        LdapMessagePOJO message = ( ( LdapMessageContainer ) 
ldapMessageContainer ).getLdapMessage();
+        BindRequestPOJO br      = ( BindRequestPOJO ) ( 
message.getProtocolOp() );
+
+        Assert.assertEquals( 1, message.getMessageId() );
+        Assert.assertEquals( 3, br.getVersion() );
+        Assert.assertEquals( "uid=akarasulu,dc=example,dc=com", br.getName() );
+        Assert.assertEquals( true, ( br.getAuthentication() instanceof 
SimpleAuthenticationPOJO ) );
+        Assert.assertEquals( "password", new String(( ( 
SimpleAuthenticationPOJO ) br.getAuthentication() ).getSimple()) );
+
+        // Free the BindRequest Container. It will be put back in the IPool
+        // after being reset.
+        ldapMessageContainer.free();
+    }
+
+    /**
+     * Test the decoding of a BindRequest with Simple authentication
+     * and no controls
+     */
+    public void testDecodeBindRequestNoName()
+    {
+        Asn1Decoder ldapDecoder = new Asn1Decoder();
+
+        ByteBuffer  stream      = ByteBuffer.allocate( 0x15 );
+        stream.put(
+            new byte[]
+            {
+                0x30, 0x13,            // LDAPMessage ::=SEQUENCE {
+                               0x02, 0x01, 0x01,       //         messageID 
MessageID
+                               0x60, 0x0D,             //        CHOICE { ..., 
bindRequest BindRequest, ...
+                                               // BindRequest ::= 
APPLICATION[0] SEQUENCE {
+                               0x02, 0x01, 0x03,       //        version 
INTEGER (1..127),
+                               ( byte ) 0x80, 0x08, //        authentication 
AuthenticationChoice
+                                     // AuthenticationChoice ::= CHOICE { 
simple [0] OCTET STRING, ...
+                               'p', 'a', 's', 's', 'w', 'o', 'r', 'd'
+            } );
+
+        stream.flip();
+
+        // Allocate a BindRequest Container
+        IAsn1Container ldapMessageContainer = null;
+
+        try
+        {
+            ldapMessageContainer = ( IAsn1Container ) ldapDecoder.allocate(
+                    PoolEnum.LDAP_MESSAGE_CONTAINER_POOL );
+        }
+        catch ( PoolException pe )
+        {
+            Assert.fail("Cannot allocat a LdapMessageContainer : " + 
pe.getMessage());
+        }
+
+        ldapMessageContainer.setPoolManager( ldapDecoder.getPoolManager() );
+
+        try
+        {
+            ldapDecoder.decode( stream, ldapMessageContainer );
+        }
+        catch ( DecoderException de )
+        {
+            Assert.assertEquals( "Bad transition from state 
BIND_REQUEST_NAME_TAG, tag [80]", de.getMessage() );
+            return;
+        }
+       
+        Assert.fail("Should never reach this point.");
+    }
+
+    /**
+     * Test the decoding of a BindRequest with Sasl authentication,
+     * no credentials and no controls
+     */
+    public void testDecodeBindRequestSaslNoCredsNoControls()
+    {
+        Asn1Decoder ldapDecoder = new Asn1Decoder();
+
+        ByteBuffer  stream      = ByteBuffer.allocate( 0x38 );
+        stream.put(
+            new byte[]
+            {
+                0x30, 0x36,            // LDAPMessage ::=SEQUENCE {
+                               0x02, 0x01, 0x01,       //         messageID 
MessageID
+                               0x60, 0x31,             //        CHOICE { ..., 
bindRequest BindRequest, ...
+                                               // BindRequest ::= 
APPLICATION[0] SEQUENCE {
+                               0x02, 0x01, 0x03,       //        version 
INTEGER (1..127),
+                               0x04, 0x1F,             //        name LDAPDN,
+                               'u', 'i', 'd', '=', 'a', 'k', 'a', 'r', 'a', 
's', 'u', 'l', 'u', ',', 'd', 'c', '=',
+                'e', 'x', 'a', 'm', 'p', 'l', 'e', ',', 'd', 'c', '=', 'c', 
'o', 'm',
+                               ( byte ) 0x83, 0x0B, //        authentication 
AuthenticationChoice
+                                     // AuthenticationChoice ::= CHOICE { ... 
sasl [3] SaslCredentials }
+                                                                        // 
SaslCredentials ::= SEQUENCE {
+                                                                        //     
 mechanism   LDAPSTRING,
+                                                                        //     
 ...
+                               'K', 'E', 'R', 'B', 'E', 'R', 'O', 'S', '_', 
'V', '4'
+            } );
+
+        stream.flip();
+
+        // Allocate a BindRequest Container
+        IAsn1Container ldapMessageContainer = null;
+
+        try
+        {
+            ldapMessageContainer = ( IAsn1Container ) ldapDecoder.allocate(
+                    PoolEnum.LDAP_MESSAGE_CONTAINER_POOL );
+        }
+        catch ( PoolException pe )
+        {
+            Assert.fail("Cannot allocat a LdapMessageContainer : " + 
pe.getMessage());
+        }
+
+        ldapMessageContainer.setPoolManager( ldapDecoder.getPoolManager() );
+
+        try
+        {
+            ldapDecoder.decode( stream, ldapMessageContainer );
+        }
+        catch ( DecoderException de )
+        {
+            de.printStackTrace();
+            Assert.fail( de.getMessage() );
+        }
+       
+        LdapMessagePOJO message = ( ( LdapMessageContainer ) 
ldapMessageContainer ).getLdapMessage();
+        BindRequestPOJO br      = ( BindRequestPOJO ) ( 
message.getProtocolOp() );
+
+        Assert.assertEquals( 1, message.getMessageId() );
+        Assert.assertEquals( 3, br.getVersion() );
+        Assert.assertEquals( "uid=akarasulu,dc=example,dc=com", br.getName() );
+        Assert.assertEquals( true, ( br.getAuthentication() instanceof 
SaslAuthenticationPOJO ) );
+        Assert.assertEquals( "KERBEROS_V4", ( ( SaslAuthenticationPOJO ) 
br.getAuthentication() ).getMechanism() );
+
+        // Free the BindRequest Container. It will be put back in the IPool
+        // after being reset.
+        ldapMessageContainer.free();
+    }
+
+    /**
+     * Test the decoding of a BindRequest with Sasl authentication,
+     * a credentials and no controls
+     */
+    public void testDecodeBindRequestSaslCredsNoControls()
+    {
+        Asn1Decoder ldapDecoder = new Asn1Decoder();
+
+        ByteBuffer  stream      = ByteBuffer.allocate( 0x41 );
+        stream.put(
+            new byte[]
+            {
+                0x30, 0x3F,            // LDAPMessage ::=SEQUENCE {
+                               0x02, 0x01, 0x01,       //         messageID 
MessageID
+                               0x60, 0x39,             //        CHOICE { ..., 
bindRequest BindRequest, ...
+                                               // BindRequest ::= 
APPLICATION[0] SEQUENCE {
+                               0x02, 0x01, 0x03,       //        version 
INTEGER (1..127),
+                               0x04, 0x1F,             //        name LDAPDN,
+                               'u', 'i', 'd', '=', 'a', 'k', 'a', 'r', 'a', 
's', 'u', 'l', 'u', ',', 'd', 'c', '=',
+                'e', 'x', 'a', 'm', 'p', 'l', 'e', ',', 'd', 'c', '=', 'c', 
'o', 'm',
+                               ( byte ) 0x83, 0x0B, //        authentication 
AuthenticationChoice }
+                                     // AuthenticationChoice ::= CHOICE { ... 
sasl [3] SaslCredentials }
+                                                                        // 
SaslCredentials ::= SEQUENCE {
+                                                                        //     
 mechanism   LDAPSTRING,
+                                                                        //     
 ...
+                               'K', 'E', 'R', 'B', 'E', 'R', 'O', 'S', '_', 
'V', '4',
+                               ( byte ) 0x04, 0x06, // SaslCredentials ::= 
SEQUENCE {        
+                                                                        //     
 ...
+                                                                        //     
 credentials   OCTET STRING OPTIONAL }
+                                                        // 
+                               'a', 'b', 'c', 'd', 'e', 'f'
+            } );
+
+        stream.flip();
+
+        // Allocate a BindRequest Container
+        IAsn1Container ldapMessageContainer = null;
+
+        try
+        {
+            ldapMessageContainer = ( IAsn1Container ) ldapDecoder.allocate(
+                    PoolEnum.LDAP_MESSAGE_CONTAINER_POOL );
+        }
+        catch ( PoolException pe )
+        {
+            Assert.fail("Cannot allocat a LdapMessageContainer : " + 
pe.getMessage());
+        }
+
+        ldapMessageContainer.setPoolManager( ldapDecoder.getPoolManager() );
+
+        try
+        {
+            ldapDecoder.decode( stream, ldapMessageContainer );
+        }
+        catch ( DecoderException de )
+        {
+            de.printStackTrace();
+            Assert.fail( de.getMessage() );
+        }
+       
+        LdapMessagePOJO message = ( ( LdapMessageContainer ) 
ldapMessageContainer ).getLdapMessage();
+        BindRequestPOJO br      = ( BindRequestPOJO ) ( 
message.getProtocolOp() );
+
+        Assert.assertEquals( 1, message.getMessageId() );
+        Assert.assertEquals( 3, br.getVersion() );
+        Assert.assertEquals( "uid=akarasulu,dc=example,dc=com", br.getName() );
+        Assert.assertEquals( true, ( br.getAuthentication() instanceof 
SaslAuthenticationPOJO ) );
+        Assert.assertEquals( "KERBEROS_V4", ( ( SaslAuthenticationPOJO ) 
br.getAuthentication() ).getMechanism() );
+        Assert.assertEquals( "abcdef", new String( ( ( SaslAuthenticationPOJO 
) br.getAuthentication() ).getCredentials() ) );
+
+        // Free the BindRequest Container. It will be put back in the IPool
+        // after being reset.
+        ldapMessageContainer.free();
+    }
+}

Added: 
directory/sandbox/trunk/asn1-new-codec/src/test/org/apache/asn1/ldap/codec/BindResponseTest.java
URL: 
http://svn.apache.org/viewcvs/directory/sandbox/trunk/asn1-new-codec/src/test/org/apache/asn1/ldap/codec/BindResponseTest.java?view=auto&rev=161387
==============================================================================
--- 
directory/sandbox/trunk/asn1-new-codec/src/test/org/apache/asn1/ldap/codec/BindResponseTest.java
 (added)
+++ 
directory/sandbox/trunk/asn1-new-codec/src/test/org/apache/asn1/ldap/codec/BindResponseTest.java
 Thu Apr 14 22:29:53 2005
@@ -0,0 +1,113 @@
+/*
+ *   Copyright 2005 The Apache Software Foundation
+ *
+ *   Licensed 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.asn1.ldap.codec;
+
+import java.nio.ByteBuffer;
+
+import org.apache.asn1.ber.Asn1Decoder;
+import org.apache.asn1.ber.containers.IAsn1Container;
+import org.apache.asn1.ldap.pojo.BindResponsePOJO;
+import org.apache.asn1.ldap.pojo.LdapMessagePOJO;
+import org.apache.asn1.util.pools.PoolEnum;
+import org.apache.asn1.util.pools.PoolException;
+import org.apache.log4j.Logger;
+import org.apache.log4j.PropertyConfigurator;
+
+import junit.framework.Assert;
+import junit.framework.TestCase;
+
+/**
+ * @author <a href="mailto:[email protected]";>Apache Directory 
Project</a>
+ */
+public class BindResponseTest extends TestCase {
+    /** Logger */
+    protected static Logger log = Logger.getLogger( BindResponseTest.class );
+
+    static
+    {
+        PropertyConfigurator.configure( "conf/log4j.conf" );
+    }
+
+    /**
+     * Test the decoding of a BindResponse
+     */
+    public void testDecodeBindResponseSuccess()
+    {
+        Asn1Decoder ldapDecoder = new Asn1Decoder();
+
+        ByteBuffer  stream      = ByteBuffer.allocate( 0x2D );
+        
+        stream.put(
+            new byte[]
+            {
+                0x30, 0x2B,            // LDAPMessage ::=SEQUENCE {
+                               0x02, 0x01, 0x01,       //         messageID 
MessageID
+                               0x61, 0x26,             //        CHOICE { ..., 
bindResponse BindResponse, ...
+                                               // BindResponse ::= 
APPLICATION[1] SEQUENCE {
+                                                                       //      
  COMPONENTS OF LDAPResult,
+                               0x0A, 0x01, 0x00,       //   LDAPResult ::= 
SEQUENCE {
+                                                                       //      
        resultCode ENUMERATED {
+                                                                       //      
                success (0), ...
+                                                                       //      
},
+                               0x04, 0x1F,                     //              
matchedDN    LDAPDN,
+                               'u', 'i', 'd', '=', 'a', 'k', 'a', 'r', 'a', 
's', 'u', 'l', 'u', ',', 'd', 'c', '=',
+                'e', 'x', 'a', 'm', 'p', 'l', 'e', ',', 'd', 'c', '=', 'c', 
'o', 'm',
+                               0x04, 0x00              //      errorMessage 
LDAPString,
+                                                                       //      
        referral     [3] Referral OPTIONAL }
+                                                                       // 
serverSaslCreds [7] OCTET STRING OPTIONAL }
+            } );
+
+        stream.flip();
+
+        // Allocate a BindRequest Container
+        IAsn1Container ldapMessageContainer = null;
+
+        try
+        {
+            ldapMessageContainer = ( IAsn1Container ) ldapDecoder.allocate(
+                    PoolEnum.LDAP_MESSAGE_CONTAINER_POOL );
+        }
+        catch ( PoolException pe )
+        {
+            Assert.fail("Cannot allocat a LdapMessageContainer : " + 
pe.getMessage());
+        }
+
+        ldapMessageContainer.setPoolManager( ldapDecoder.getPoolManager() );
+
+        try
+        {
+            ldapDecoder.decode( stream, ldapMessageContainer );
+        }
+        catch ( DecoderException de )
+        {
+            de.printStackTrace();
+            Assert.fail( de.getMessage() );
+        }
+       
+        LdapMessagePOJO message = ( ( LdapMessageContainer ) 
ldapMessageContainer ).getLdapMessage();
+        BindResponsePOJO br      = ( BindResponsePOJO ) ( 
message.getProtocolOp() );
+
+        Assert.assertEquals( 1, message.getMessageId() );
+        Assert.assertEquals( 0, br.getLdapResult().getResultCode() );
+        Assert.assertEquals( "uid=akarasulu,dc=example,dc=com", 
br.getLdapResult().getMatchedDN() );
+        Assert.assertEquals( "", br.getLdapResult().getErrorMessage() );
+
+        // Free the BindRequest Container. It will be put back in the IPool
+        // after being reset.
+        ldapMessageContainer.free();
+    }
+}


Reply via email to