directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From elecha...@apache.org
Subject svn commit: r159439 - directory/sandbox/trunk/asn1-new-codec/src/test/org/apache/asn1/ldap/codec/LdapDecoderTest.java
Date Tue, 29 Mar 2005 22:46:45 GMT
Author: elecharny
Date: Tue Mar 29 14:46:44 2005
New Revision: 159439

URL: http://svn.apache.org/viewcvs?view=rev&rev=159439
Log:
Added a LdapDecoder Junit test class. Created two tests (a full decoding and a partial decoding)

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

Added: directory/sandbox/trunk/asn1-new-codec/src/test/org/apache/asn1/ldap/codec/LdapDecoderTest.java
URL: http://svn.apache.org/viewcvs/directory/sandbox/trunk/asn1-new-codec/src/test/org/apache/asn1/ldap/codec/LdapDecoderTest.java?view=auto&rev=159439
==============================================================================
--- directory/sandbox/trunk/asn1-new-codec/src/test/org/apache/asn1/ldap/codec/LdapDecoderTest.java
(added)
+++ directory/sandbox/trunk/asn1-new-codec/src/test/org/apache/asn1/ldap/codec/LdapDecoderTest.java
Tue Mar 29 14:46:44 2005
@@ -0,0 +1,174 @@
+/*
+ *   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.ans1.ber.LdapDecoderPerf;
+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.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:directory-dev@incubator.apache.org">Apache Directory Project</a>
+ */
+public class LdapDecoderTest extends TestCase {
+	
+    /** Logger */
+    protected static Logger log = Logger.getLogger( LdapDecoderPerf.class );
+
+    static
+    {
+        PropertyConfigurator.configure( "conf/log4j.conf" );
+    }
+
+    /**
+     * Test the decoding of a full PDU
+     */
+    public void testDecodeFull()
+    {
+        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.setGrammar( LdapMessageGrammar.getInstance() );
+        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", ( ( 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 partial PDU
+     */
+    public void testDecodePartial()
+    {
+        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', '=' 
+            } );
+
+        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.setGrammar( LdapMessageGrammar.getInstance() );
+        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( null, br.getName() );
+        Assert.assertEquals( false, ( br.getAuthentication() instanceof SimpleAuthenticationPOJO
) );
+
+        // Free the BindRequest Container. It will be put back in the IPool
+        // after being reset.
+        ldapMessageContainer.free();
+    }
+}



Mime
View raw message