directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From akaras...@apache.org
Subject svn commit: r420309 - /directory/branches/apacheds/optimization/server-unit/src/test/java/org/apache/directory/server/AbandonSearchTest.java
Date Sun, 09 Jul 2006 16:22:31 GMT
Author: akarasulu
Date: Sun Jul  9 09:22:30 2006
New Revision: 420309

URL: http://svn.apache.org/viewvc?rev=420309&view=rev
Log:
checking in test case early ot show it to emmanuel

Added:
    directory/branches/apacheds/optimization/server-unit/src/test/java/org/apache/directory/server/AbandonSearchTest.java

Added: directory/branches/apacheds/optimization/server-unit/src/test/java/org/apache/directory/server/AbandonSearchTest.java
URL: http://svn.apache.org/viewvc/directory/branches/apacheds/optimization/server-unit/src/test/java/org/apache/directory/server/AbandonSearchTest.java?rev=420309&view=auto
==============================================================================
--- directory/branches/apacheds/optimization/server-unit/src/test/java/org/apache/directory/server/AbandonSearchTest.java
(added)
+++ directory/branches/apacheds/optimization/server-unit/src/test/java/org/apache/directory/server/AbandonSearchTest.java
Sun Jul  9 09:22:30 2006
@@ -0,0 +1,274 @@
+/*
+ *   Copyright 2006 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.directory.server;
+
+
+import java.io.IOException;
+import java.net.InetSocketAddress;
+import java.net.SocketAddress;
+import java.net.UnknownHostException;
+import java.nio.ByteBuffer;
+import java.nio.channels.SocketChannel;
+
+import javax.naming.InvalidNameException;
+import javax.naming.NamingException;
+
+import org.apache.directory.server.unit.AbstractServerTest;
+import org.apache.directory.shared.asn1.ber.Asn1Decoder;
+import org.apache.directory.shared.asn1.ber.IAsn1Container;
+import org.apache.directory.shared.asn1.ber.tlv.TLVStateEnum;
+import org.apache.directory.shared.asn1.codec.DecoderException;
+import org.apache.directory.shared.asn1.codec.EncoderException;
+import org.apache.directory.shared.ldap.codec.LdapDecoder;
+import org.apache.directory.shared.ldap.codec.LdapMessage;
+import org.apache.directory.shared.ldap.codec.LdapMessageContainer;
+import org.apache.directory.shared.ldap.codec.LdapResult;
+import org.apache.directory.shared.ldap.codec.abandon.AbandonRequest;
+import org.apache.directory.shared.ldap.codec.bind.BindRequest;
+import org.apache.directory.shared.ldap.codec.bind.BindResponse;
+import org.apache.directory.shared.ldap.codec.bind.LdapAuthentication;
+import org.apache.directory.shared.ldap.codec.bind.SimpleAuthentication;
+import org.apache.directory.shared.ldap.codec.extended.ExtendedResponse;
+import org.apache.directory.shared.ldap.codec.search.PresentFilter;
+import org.apache.directory.shared.ldap.codec.search.SearchRequest;
+import org.apache.directory.shared.ldap.codec.unbind.UnBindRequest;
+import org.apache.directory.shared.ldap.codec.util.LdapResultEnum;
+import org.apache.directory.shared.ldap.codec.util.LdapString;
+import org.apache.directory.shared.ldap.name.LdapDN;
+import org.apache.directory.shared.ldap.util.StringTools;
+
+
+/**
+ * Testcase with different modify operations on a person entry. Each includes a
+ * single add op only. Created to demonstrate DIREVE-241 ("Adding an already
+ * existing attribute value with a modify operation does not cause an error.").
+ * 
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev: 418470 $
+ */
+public class AbandonSearchTest extends AbstractServerTest
+{
+    private SocketChannel channel;
+    private SocketAddress serverAddress;
+    private IAsn1Container ldapMessageContainer = new LdapMessageContainer();
+    private Asn1Decoder ldapDecoder = new LdapDecoder();
+
+
+    /**
+     * Connect to the LDAP server through a socket and establish the Input and
+     * Output Streams. 
+     * 
+     * @throws UnknownHostException
+     *             The hostname or the Address of server could not be found
+     * @throws IOException
+     *             There was a error opening or establishing the socket
+     */
+    private void connect() throws UnknownHostException, IOException
+    {
+        serverAddress = new InetSocketAddress( "localhost", port );
+        channel = SocketChannel.open( serverAddress );
+        channel.configureBlocking( true );
+    }
+
+    
+    private void sendMessage( ByteBuffer bb ) throws IOException
+    {
+        channel.write( bb );
+        bb.clear();
+    }
+
+
+    private LdapMessage readResponse( ByteBuffer bb ) throws IOException, DecoderException,
NamingException
+    {
+        LdapMessage messageResp = null;
+
+        while ( true )
+        {
+            int nbRead = channel.read( bb );
+
+            if ( nbRead == -1 )
+            {
+                break;
+            }
+            else
+            {
+                bb.flip();
+
+                // Decode the PDU
+                ldapDecoder.decode( bb, ldapMessageContainer );
+
+                if ( ldapMessageContainer.getState() == TLVStateEnum.PDU_DECODED )
+                {
+                    messageResp = ( (LdapMessageContainer) ldapMessageContainer ).getLdapMessage();
+
+                    if ( messageResp instanceof BindResponse )
+                    {
+                        BindResponse resp = ( (LdapMessageContainer) ldapMessageContainer
).getLdapMessage().getBindResponse();
+
+                        if ( resp.getLdapResult().getResultCode() != 0 )
+                        {
+                            System.out.println( "Error : " + resp.getLdapResult().getErrorMessage()
);
+                        }
+                    }
+                    else if ( messageResp instanceof ExtendedResponse )
+                    {
+                        ExtendedResponse resp = ( (LdapMessageContainer) ldapMessageContainer
).getLdapMessage()
+                                .getExtendedResponse();
+
+                        if ( resp.getLdapResult().getResultCode() != 0 )
+                        {
+                            System.out.println( "Error : " + resp.getLdapResult().getErrorMessage()
);
+                        }
+                    }
+
+                    ( (LdapMessageContainer) ldapMessageContainer ).clean();
+                    break;
+                }
+                else
+                {
+                    bb.flip();
+                }
+            }
+        }
+
+        return messageResp;
+    }
+
+    
+    /**
+     * Test abandoning a search operation.
+     * 
+     * @throws NamingException
+     */
+    public void testAbandonSearch() throws Exception
+    {
+        // bind first
+        bind( 1 );
+        
+        SearchRequest searchRequest = new SearchRequest();
+        searchRequest.setBaseObject( new LdapDN( "ou=system" ) );
+        PresentFilter filter = new PresentFilter();
+        filter.setAttributeDescription( new LdapString( StringTools.getBytesUtf8( "objectClass"
) ) );
+        searchRequest.setFilter( filter );
+        searchRequest.setMessageId( 2 );
+        searchRequest.setScope( 2 );
+        searchRequest.setSizeLimit( 0 );
+        searchRequest.setTimeLimit( 0 );
+        searchRequest.setTypesOnly( false );
+        searchRequest.addAttribute( new LdapString( StringTools.getBytesUtf8( "*" ) ) );
+        
+        LdapMessage message = new LdapMessage();
+        message.setProtocolOP( searchRequest );
+        message.setMessageId( 2 );
+        
+        // Encode and send the addRequest message
+        ByteBuffer bb = message.encode( null );
+        bb.flip();
+
+        sendMessage( bb );
+
+        bb.clear();
+
+        abandon( 3, 2 );
+        unbind( 4 );
+    }
+
+    
+    private void abandon( int messageId, int toAbandonId ) throws Exception
+    {
+        AbandonRequest abandonRequest = new AbandonRequest();
+        abandonRequest.setAbandonedMessageId( toAbandonId );
+        LdapMessage message = new LdapMessage();
+        message.setProtocolOP( abandonRequest );
+        message.setMessageId( messageId );
+
+        // Encode and send the abandon request
+        ByteBuffer bb = message.encode( null );
+        bb.flip();
+
+        sendMessage( bb );
+
+        bb.clear();
+    }
+    
+
+    /**
+     * Bind to the ldap server
+     * 
+     * @param messageId The message Id
+     */
+    private void bind( int messageId ) throws Exception
+    {
+        BindRequest bindRequest = new BindRequest();
+        LdapMessage message = new LdapMessage();
+        LdapAuthentication authentication = null;
+
+        authentication = new SimpleAuthentication();
+        ( (SimpleAuthentication) authentication ).setSimple( StringTools.getBytesUtf8( "secret"
) );
+        bindRequest.setAuthentication( authentication );
+        bindRequest.setName( new LdapDN( "uid=admin,ou=system" ) );
+        bindRequest.setVersion( 3 );
+        message.setProtocolOP( bindRequest );
+        message.setMessageId( messageId );
+        
+        // Encode and send the bind request
+        ByteBuffer bb = message.encode( null );
+        bb.flip();
+
+        connect();
+        sendMessage( bb );
+
+        bb.clear();
+
+        // Get the bind response
+        LdapMessage response = readResponse( bb );
+
+        LdapResult result = response.getBindResponse().getLdapResult();
+
+        if ( result.getResultCode() == LdapResultEnum.SUCCESS )
+        {
+        }
+        else
+        {
+            fail( "bind should succeed" );
+        }
+    }
+    
+
+    /**
+     * Unbind from the server
+     * 
+     * @param messageId
+     *            The message Id
+     * @throws InvalidNameException
+     * @throws EncoderException
+     * @throws DecoderException
+     * @throws IOException
+     */
+    private void unbind( int messageId ) throws Exception
+    {
+        UnBindRequest unbindRequest = new UnBindRequest();
+        LdapMessage message = new LdapMessage();
+
+        message.setProtocolOP( unbindRequest );
+        message.setMessageId( messageId );
+        ByteBuffer bb = message.encode( null );
+        bb.flip();
+
+        sendMessage( bb );
+    }
+}



Mime
View raw message