You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by el...@apache.org on 2010/02/02 15:48:41 UTC

svn commit: r905654 - in /directory/clients/ldap/trunk: ldap-client-api/src/main/java/org/apache/directory/ldap/client/api/ ldap-client-test/src/test/java/org/apache/directory/shared/client/api/operations/ ldap-client-test/src/test/java/org/apache/dire...

Author: elecharny
Date: Tue Feb  2 14:48:37 2010
New Revision: 905654

URL: http://svn.apache.org/viewvc?rev=905654&view=rev
Log:
o Added tests for BindRequest operation
o Added a isAuthenticated() method
o Moved the Bind tests into a dedicated package

Added:
    directory/clients/ldap/trunk/ldap-client-test/src/test/java/org/apache/directory/shared/client/api/operations/bind/
    directory/clients/ldap/trunk/ldap-client-test/src/test/java/org/apache/directory/shared/client/api/operations/bind/SimpleBindRequestTest.java
      - copied, changed from r905544, directory/clients/ldap/trunk/ldap-client-test/src/test/java/org/apache/directory/shared/client/api/operations/ClientBindRequestTest.java
Removed:
    directory/clients/ldap/trunk/ldap-client-test/src/test/java/org/apache/directory/shared/client/api/operations/ClientBindRequestTest.java
Modified:
    directory/clients/ldap/trunk/ldap-client-api/src/main/java/org/apache/directory/ldap/client/api/LdapConnection.java
    directory/clients/ldap/trunk/ldap-client-api/src/main/java/org/apache/directory/ldap/client/api/PoolableLdapConnectionFactory.java

Modified: directory/clients/ldap/trunk/ldap-client-api/src/main/java/org/apache/directory/ldap/client/api/LdapConnection.java
URL: http://svn.apache.org/viewvc/directory/clients/ldap/trunk/ldap-client-api/src/main/java/org/apache/directory/ldap/client/api/LdapConnection.java?rev=905654&r1=905653&r2=905654&view=diff
==============================================================================
--- directory/clients/ldap/trunk/ldap-client-api/src/main/java/org/apache/directory/ldap/client/api/LdapConnection.java (original)
+++ directory/clients/ldap/trunk/ldap-client-api/src/main/java/org/apache/directory/ldap/client/api/LdapConnection.java Tue Feb  2 14:48:37 2010
@@ -197,7 +197,12 @@
     /** list of controls supported by the server */
     private List<String> supportedControls;
 
+    /** The ROOT DSE entry */
     private Entry rootDSE;
+    
+    /** A flag indicating that the BindRequest has been issued and successfully authenticated the user */
+    private boolean authenticated = false;
+    
 
     // ~~~~~~~~~~~~~~~~~ common error messages ~~~~~~~~~~~~~~~~~~~~~~~~~~
 
@@ -212,17 +217,28 @@
 
     //--------------------------- Helper methods ---------------------------//
     /**
-     * Check if the connection is valid : created and connected
+     * Check if we are connected
      *
-     * @return <code>true</code> if the session is valid.
+     * @return <code>true</code> if we are connected.
      */
-    public boolean isBound()
+    public boolean isConnected()
     {
         return ( ldapSession != null ) && ldapSession.isConnected();
     }
 
 
     /**
+     * Check if we are authenticated
+     *
+     * @return <code>true</code> if we are connected.
+     */
+    public boolean isAuthenticated()
+    {
+        return isConnected() && authenticated;
+    }
+
+
+    /**
      * Check that a session is valid, ie we can send requests to the
      * server
      *
@@ -235,7 +251,7 @@
             throw new InvalidConnectionException( "Cannot connect on the server, the connection is null" );
         }
 
-        if ( !isBound() )
+        if ( !isConnected() )
         {
             throw new InvalidConnectionException( "Cannot connect on the server, the connection is invalid" );
         }
@@ -1008,8 +1024,18 @@
             // Get the response, blocking
             BindResponse bindResponse = ( BindResponse ) bindFuture.get( timeout, TimeUnit.MILLISECONDS );
 
-            // Everything is fine, return the response
-            LOG.debug( "Bind successful : {}", bindResponse );
+            if ( bindResponse.getLdapResult().getResultCode() == ResultCodeEnum.SUCCESS )
+            {
+                authenticated = true;
+
+                // Everything is fine, return the response
+                LOG.debug( "Bind successful : {}", bindResponse );
+            }
+            else
+            {
+                // We have had an error
+                LOG.debug( "Bind failed : {}", bindResponse );
+            }
 
             return bindResponse;
         }
@@ -1122,7 +1148,10 @@
      */
     public BindFuture bind( BindRequest bindRequest, BindListener bindListener ) throws LdapException, IOException
     {
-        // First try to connect, if we aren't already connected.
+        // First switch to anonymous state
+        authenticated = false;
+        
+        // try to connect, if we aren't already connected.
         connect();
 
         // If the session has not been establish, or is closed, we get out immediately
@@ -1162,7 +1191,7 @@
 
         // Wait for the message to be sent to the server
         writeFuture.awaitUninterruptibly( timeOut );
-
+        
         return bindFuture;
     }
 
@@ -1529,6 +1558,19 @@
                 // remove the listener from the listener map
                 BindListener bindListener = ( BindListener ) listenerMap.remove( bindResponseCodec.getMessageId() );
 
+                if ( bindResponse.getLdapResult().getResultCode() == ResultCodeEnum.SUCCESS )
+                {
+                    authenticated = true;
+
+                    // Everything is fine, return the response
+                    LOG.debug( "Bind successful : {}", bindResponse );
+                }
+                else
+                {
+                    // We have had an error
+                    LOG.debug( "Bind failed : {}", bindResponse );
+                }
+
                 if ( bindListener != null )
                 {
                     bindListener.bindCompleted( this, bindResponse );

Modified: directory/clients/ldap/trunk/ldap-client-api/src/main/java/org/apache/directory/ldap/client/api/PoolableLdapConnectionFactory.java
URL: http://svn.apache.org/viewvc/directory/clients/ldap/trunk/ldap-client-api/src/main/java/org/apache/directory/ldap/client/api/PoolableLdapConnectionFactory.java?rev=905654&r1=905653&r2=905654&view=diff
==============================================================================
--- directory/clients/ldap/trunk/ldap-client-api/src/main/java/org/apache/directory/ldap/client/api/PoolableLdapConnectionFactory.java (original)
+++ directory/clients/ldap/trunk/ldap-client-api/src/main/java/org/apache/directory/ldap/client/api/PoolableLdapConnectionFactory.java Tue Feb  2 14:48:37 2010
@@ -105,7 +105,7 @@
         LOG.debug( "validating {}", obj );
 
         LdapConnection connection = ( LdapConnection ) obj;
-        return connection.isBound();
+        return connection.isConnected();
     }
 
 }

Copied: directory/clients/ldap/trunk/ldap-client-test/src/test/java/org/apache/directory/shared/client/api/operations/bind/SimpleBindRequestTest.java (from r905544, directory/clients/ldap/trunk/ldap-client-test/src/test/java/org/apache/directory/shared/client/api/operations/ClientBindRequestTest.java)
URL: http://svn.apache.org/viewvc/directory/clients/ldap/trunk/ldap-client-test/src/test/java/org/apache/directory/shared/client/api/operations/bind/SimpleBindRequestTest.java?p2=directory/clients/ldap/trunk/ldap-client-test/src/test/java/org/apache/directory/shared/client/api/operations/bind/SimpleBindRequestTest.java&p1=directory/clients/ldap/trunk/ldap-client-test/src/test/java/org/apache/directory/shared/client/api/operations/ClientBindRequestTest.java&r1=905544&r2=905654&rev=905654&view=diff
==============================================================================
--- directory/clients/ldap/trunk/ldap-client-test/src/test/java/org/apache/directory/shared/client/api/operations/ClientBindRequestTest.java (original)
+++ directory/clients/ldap/trunk/ldap-client-test/src/test/java/org/apache/directory/shared/client/api/operations/bind/SimpleBindRequestTest.java Tue Feb  2 14:48:37 2010
@@ -17,17 +17,19 @@
  *  under the License. 
  *  
  */
-package org.apache.directory.shared.client.api.operations;
+package org.apache.directory.shared.client.api.operations.bind;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
 
 import java.io.IOException;
 
 import org.apache.directory.ldap.client.api.LdapConnection;
 import org.apache.directory.ldap.client.api.exception.LdapException;
-import org.apache.directory.ldap.client.api.future.ResponseFuture;
+import org.apache.directory.ldap.client.api.future.BindFuture;
 import org.apache.directory.ldap.client.api.listener.BindListener;
 import org.apache.directory.ldap.client.api.message.BindRequest;
 import org.apache.directory.ldap.client.api.message.BindResponse;
@@ -45,7 +47,7 @@
 import org.junit.runner.RunWith;
 
 /**
- * Test the BindRequest operation
+ * Test the Simple BindRequest operation
  *
  * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
  * @version $Rev$, $Date$
@@ -57,7 +59,7 @@
         @CreateTransport( protocol = "LDAP" ), 
         @CreateTransport( protocol = "LDAPS" ) 
     })
-public class ClientBindRequestTest extends AbstractLdapTestUnit
+public class SimpleBindRequestTest extends AbstractLdapTestUnit
 {
     private LdapConnection connection;
 
@@ -90,35 +92,25 @@
 
     
     /**
-     * Test a successful synchronous bind request
-     *
-     * @throws IOException
+     * Test a successful synchronous bind request. the server allows it.
      */
     @Test
     public void testSyncBindRequest() throws Exception
     {
-        try
-        {
-            BindResponse bindResponse = connection.bind( "uid=admin,ou=system", "secret" );
-            
-            assertNotNull( bindResponse );
-            assertNotNull( bindResponse.getLdapResult() );
-            assertEquals( ResultCodeEnum.SUCCESS, bindResponse.getLdapResult().getResultCode() );
-            assertEquals( 1, bindResponse.getMessageId() );
+        BindResponse bindResponse = connection.bind( "uid=admin,ou=system", "secret" );
+        
+        assertNotNull( bindResponse );
+        assertNotNull( bindResponse.getLdapResult() );
+        assertEquals( ResultCodeEnum.SUCCESS, bindResponse.getLdapResult().getResultCode() );
+        assertEquals( 1, bindResponse.getMessageId() );
+        assertTrue( connection.isAuthenticated() );
 
-            connection.unBind();
-        }
-        catch ( LdapException le )
-        {
-            fail();
-        }
+        connection.unBind();
     }
 
     
     /**
-     * Test a successful asynchronous bind request
-     *
-     * @throws IOException
+     * Test a successful asynchronous bind request, 10 times.
      */
     @Test
     @Ignore
@@ -127,124 +119,130 @@
         int i = 0;
         int nbLoop = 10;
 
-        try
+        for ( ; i < nbLoop; i++)
         {
-            for ( ; i < nbLoop; i++)
-            {
-                BindRequest bindRequest = new BindRequest();
-                bindRequest.setName( "uid=admin,ou=system" );
-                bindRequest.setCredentials( "secret" );
-                final int loop = i;
+            BindRequest bindRequest = new BindRequest();
+            bindRequest.setName( "uid=admin,ou=system" );
+            bindRequest.setCredentials( "secret" );
+            final int loop = i;
 
-                ResponseFuture bindFuture = connection.bind( bindRequest, new BindListener()
+            BindFuture bindFuture = connection.bind( bindRequest, new BindListener()
+            {
+                public void bindCompleted( LdapConnection connection, BindResponse bindResponse ) throws LdapException
                 {
-                    public void bindCompleted( LdapConnection connection, BindResponse bindResponse ) throws LdapException
-                    {
-                        assertNotNull( bindResponse );
-                        assertEquals( ResultCodeEnum.SUCCESS, bindResponse.getLdapResult().getResultCode() );
-                        assertEquals( 1, bindResponse.getMessageId() );
-                        System.out.println( "Bound " + loop );
-                    }
-                } );
-                
-                Response bindResponse = (Response)bindFuture.get();
-                bindResponse.wait();
-                
-                System.out.println( "Unbinding " + loop );
-                connection.unBind();
-            }
-        }
-        catch ( LdapException le )
-        {
-            le.printStackTrace();
-            fail();
-        }
-        catch( Exception e )
-        {
-            e.printStackTrace();
+                    assertNotNull( bindResponse );
+                    assertEquals( ResultCodeEnum.SUCCESS, bindResponse.getLdapResult().getResultCode() );
+                    assertEquals( 1, bindResponse.getMessageId() );
+                    System.out.println( "Bound " + loop );
+                }
+            } );
+            
+            Response bindResponse = (Response)bindFuture.get();
+            bindResponse.wait();
+            assertTrue( connection.isAuthenticated() );
+
+            connection.unBind();
         }
     }
     
     
+    /**
+     * Test an Anonymous BindRequest
+     */
     @Test
+    @Ignore
     public void testSimpleBindAnonymous() throws Exception
     {
-        try
-        {
-            BindResponse bindResponse = connection.bind();
-            
-            assertNotNull( bindResponse );
-            assertNotNull( bindResponse.getLdapResult() );
-            assertEquals( ResultCodeEnum.SUCCESS, bindResponse.getLdapResult().getResultCode() );
-            assertEquals( 1, bindResponse.getMessageId() );
+        // Try with no parameters
+        BindResponse bindResponse = connection.bind();
+        
+        assertNotNull( bindResponse );
+        assertNotNull( bindResponse.getLdapResult() );
+        assertEquals( ResultCodeEnum.SUCCESS, bindResponse.getLdapResult().getResultCode() );
+        assertEquals( 1, bindResponse.getMessageId() );
+        assertTrue( connection.isAuthenticated() );
+
+        connection.unBind();
+
+        // Try with empty strings
+        bindResponse = connection.bind( "", "" );
+        
+        assertNotNull( bindResponse );
+        assertNotNull( bindResponse.getLdapResult() );
+        assertEquals( ResultCodeEnum.SUCCESS, bindResponse.getLdapResult().getResultCode() );
+        assertEquals( 1, bindResponse.getMessageId() );
+        assertTrue( connection.isAuthenticated() );
+
+        connection.unBind();
+
+        // Try with null parameters
+        bindResponse = connection.bind( (String)null, (String)null );
+        
+        assertNotNull( bindResponse );
+        assertNotNull( bindResponse.getLdapResult() );
+        assertEquals( ResultCodeEnum.SUCCESS, bindResponse.getLdapResult().getResultCode() );
+        assertEquals( 1, bindResponse.getMessageId() );
+        assertTrue( connection.isAuthenticated() );
 
-            connection.unBind();
-        }
-        catch ( LdapException le )
-        {
-            fail();
-        }
+        connection.unBind();
     }
+
     
+    /**
+     * A bind with no name and a password is invalid
+     */
+    @Test
+    public void testSimpleBindNoNamePassword() throws Exception
+    {
+        BindResponse response = connection.bind((String)null, "abc" );
+        LdapResult ldapResult = response.getLdapResult();
+        assertEquals( ResultCodeEnum.INVALID_CREDENTIALS, ldapResult.getResultCode() );
+        assertEquals( 1, response.getMessageId() );
+        assertFalse( connection.isAuthenticated() );
+    }
+
     
+    /**
+     * Test an unauthenticated bind (name, no password)
+     */
     @Test
-    public void testSimpleBindAnonymous2() throws Exception
+    public void testSimpleBindUnauthenticated() throws Exception
     {
-        try
-        {
-            BindResponse bindResponse = connection.bind( "", "" );
-            
-            assertNotNull( bindResponse );
-            assertNotNull( bindResponse.getLdapResult() );
-            assertEquals( ResultCodeEnum.SUCCESS, bindResponse.getLdapResult().getResultCode() );
-            assertEquals( 1, bindResponse.getMessageId() );
+        BindResponse response = connection.bind( "uid=admin,ou=system" );
+        LdapResult ldapResult = response.getLdapResult();
+        assertEquals( ResultCodeEnum.UNWILLING_TO_PERFORM, ldapResult.getResultCode() );
+        assertEquals( 1, response.getMessageId() );
+        assertFalse( connection.isAuthenticated() );
 
-            connection.unBind();
-        }
-        catch ( LdapException le )
-        {
-            fail();
-        }
     }
+
     
-    
+    /**
+     * Test a valid bind
+     */
     @Test
-    public void testSimpleBindAnonymous3() throws Exception
+    public void testSimpleBindValid() throws Exception
     {
-        try
-        {
-            BindResponse bindResponse = connection.bind( (String)null, (String)null );
-            
-            assertNotNull( bindResponse );
-            assertNotNull( bindResponse.getLdapResult() );
-            assertEquals( ResultCodeEnum.SUCCESS, bindResponse.getLdapResult().getResultCode() );
-            assertEquals( 1, bindResponse.getMessageId() );
+        BindResponse response = connection.bind( "uid=admin,ou=system", "secret" );
+        LdapResult ldapResult = response.getLdapResult();
+        assertEquals( ResultCodeEnum.SUCCESS, ldapResult.getResultCode() );
+        assertEquals( 1, response.getMessageId() );
+        assertTrue( connection.isAuthenticated() );
 
-            connection.unBind();
-        }
-        catch ( LdapException le )
-        {
-            fail();
-        }
+        connection.unBind();
     }
-    
+
     
     /**
-     * A bind with no name and a password is invalid
+     * Test a bind with a valid user but a wrong password
      */
     @Test
-    public void testSimpleBindNoNamePassword() throws Exception
+    public void testSimpleBindValidUserWrongPassword() throws Exception
     {
-        try
-        {
-            BindResponse response = connection.bind((String)null, "abc" );
-            LdapResult ldapResult = response.getLdapResult();
-            assertEquals( ResultCodeEnum.INVALID_CREDENTIALS, ldapResult.getResultCode() );
-            assertEquals( 1, response.getMessageId() );
-        }
-        catch ( Exception le )
-        {
-            fail();
-        }
+        BindResponse response = connection.bind( "uid=admin,ou=system", "badpassword" );
+        LdapResult ldapResult = response.getLdapResult();
+        assertEquals( ResultCodeEnum.INVALID_CREDENTIALS, ldapResult.getResultCode() );
+        assertEquals( 1, response.getMessageId() );
+        assertFalse( connection.isAuthenticated() );
     }
 }