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 2005/06/26 22:12:02 UTC
svn commit: r201897 -
/directory/sandbox/trunk/asn1-new-codec/src/test/org/apache/asn1/ldap/codec/ModifyRequestTest.java
Author: elecharny
Date: Sun Jun 26 13:12:01 2005
New Revision: 201897
URL: http://svn.apache.org/viewcvs?rev=201897&view=rev
Log:
Added a new test for the ModifyRequest Ldap Message
Added:
directory/sandbox/trunk/asn1-new-codec/src/test/org/apache/asn1/ldap/codec/ModifyRequestTest.java
Added: directory/sandbox/trunk/asn1-new-codec/src/test/org/apache/asn1/ldap/codec/ModifyRequestTest.java
URL: http://svn.apache.org/viewcvs/directory/sandbox/trunk/asn1-new-codec/src/test/org/apache/asn1/ldap/codec/ModifyRequestTest.java?rev=201897&view=auto
==============================================================================
--- directory/sandbox/trunk/asn1-new-codec/src/test/org/apache/asn1/ldap/codec/ModifyRequestTest.java (added)
+++ directory/sandbox/trunk/asn1-new-codec/src/test/org/apache/asn1/ldap/codec/ModifyRequestTest.java Sun Jun 26 13:12:01 2005
@@ -0,0 +1,250 @@
+/*
+ * 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 java.util.ArrayList;
+import java.util.HashMap;
+import java.util.HashSet;
+
+import javax.naming.NamingEnumeration;
+import javax.naming.NamingException;
+import javax.naming.directory.BasicAttribute;
+
+import org.apache.asn1.DecoderException;
+import org.apache.asn1.ber.Asn1Decoder;
+import org.apache.asn1.ber.containers.IAsn1Container;
+import org.apache.asn1.ldap.pojo.LdapMessage;
+import org.apache.asn1.ldap.pojo.ModifyRequest;
+import org.apache.asn1.ldap.pojo.SearchResultEntry;
+import org.apache.asn1.primitives.OctetString;
+import org.apache.log4j.Logger;
+import org.apache.log4j.PropertyConfigurator;
+
+import junit.framework.Assert;
+import junit.framework.TestCase;
+
+/**
+ * Test the ModifyRequest codec
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public class ModifyRequestTest extends TestCase {
+ /** Logger */
+ protected static Logger log = Logger.getLogger( ModifyRequestTest.class );
+
+ static
+ {
+ PropertyConfigurator.configure( "conf/log4j.conf" );
+ }
+
+ /**
+ * Test the decoding of a ModifyRequest
+ */
+ public void testDecodeModifyRequestSuccess() throws NamingException
+ {
+ Asn1Decoder ldapDecoder = new LdapDecoder();
+
+ ByteBuffer stream = ByteBuffer.allocate( 0x7c );
+
+ stream.put(
+ new byte[]
+ {
+
+
+ 0x30, 0x7a, // LDAPMessage ::=SEQUENCE {
+ 0x02, 0x01, 0x01, // messageID MessageID
+ 0x64, 0x74, // CHOICE { ..., searchResEntry SearchResultEntry, ...
+ // SearchResultEntry ::= [APPLICATION 4] SEQUENCE {
+ // objectName LDAPDN,
+ 0x04, 0x1b, 'o', 'u', '=', 'c', 'o', 'n', 't', 'a', 'c', 't', 's', ',', 'd', 'c', '=', 'i', 'k', 't', 'e', 'k', ',', 'd', 'c', '=', 'c', 'o', 'm',
+ // attributes PartialAttributeList }
+ // PartialAttributeList ::= SEQUENCE OF SEQUENCE {
+ 0x30, 0x55,
+ 0x30, 0x28,
+ // type AttributeDescription,
+ 0x04, 0x0b, 'o', 'b', 'j', 'e', 'c', 't', 'C', 'l', 'a', 's', 's',
+ // vals SET OF AttributeValue }
+ 0x31, 0x19,
+ // AttributeValue ::= OCTET STRING
+ 0x04, 0x03, 't', 'o', 'p',
+ // AttributeValue ::= OCTET STRING
+ 0x04, 0x12, 'o', 'r', 'g', 'a', 'n', 'i', 'z', 'a', 't', 'i', 'o', 'n', 'a', 'l', 'U', 'n', 'i', 't',
+ 0x30, 0x29,
+ // type AttributeDescription,
+ 0x04, 0x0c, 'o', 'b', 'j', 'e', 'c', 't', 'C', 'l', 'a', 's', 's', '2',
+ // vals SET OF AttributeValue }
+ 0x31, 0x19,
+ // AttributeValue ::= OCTET STRING
+ 0x04, 0x03, 't', 'o', 'p',
+ // AttributeValue ::= OCTET STRING
+ 0x04, 0x12, 'o', 'r', 'g', 'a', 'n', 'i', 'z', 'a', 't', 'i', 'o', 'n', 'a', 'l', 'U', 'n', 'i', 't'
+ } );
+
+ stream.flip();
+
+ // Allocate a BindRequest Container
+ IAsn1Container ldapMessageContainer = new LdapMessageContainer();
+
+ try
+ {
+ ldapDecoder.decode( stream, ldapMessageContainer );
+ }
+ catch ( DecoderException de )
+ {
+ de.printStackTrace();
+ Assert.fail( de.getMessage() );
+ }
+
+ LdapMessage message = ( ( LdapMessageContainer ) ldapMessageContainer ).getLdapMessage();
+ SearchResultEntry searchResultEntry = message.getSearchResultEntry();
+
+ Assert.assertEquals( 1, message.getMessageId() );
+ Assert.assertEquals( "ou=contacts,dc=iktek,dc=com", searchResultEntry.getObjectName() );
+
+ ArrayList partialAttributesList = searchResultEntry.getPartialAttributeList();
+
+ Assert.assertEquals( 2, partialAttributesList.size() );
+
+ String[] expectedAttributes = new String[]{"objectClass", "objectClass2"};
+
+ for ( int i = 0; i < partialAttributesList.size(); i++ )
+ {
+ BasicAttribute attributeValue = (BasicAttribute)partialAttributesList.get( i );
+
+ Assert.assertEquals( expectedAttributes[i], attributeValue.getID() );
+
+ NamingEnumeration values = attributeValue.getAll();
+
+ HashSet expectedValues = new HashSet();
+
+ expectedValues.add( "[74][6F][70]" );
+ expectedValues.add( "[6F][72][67][61][6E][69][7A][61][74][69][6F][6E][61][6C][55][6E][69][74]" );
+
+ while ( values.hasMore() )
+ {
+ OctetString value = (OctetString)values.next();
+
+ Assert.assertTrue( expectedValues.contains( value.toString() ) );
+
+ expectedValues.remove( value.toString() );
+ }
+ }
+ }
+ /**
+ * Test the decoding of a ModifyRequest
+ */
+ public void testDecodeModifyRequest2AttrsSuccess() throws NamingException
+ {
+ Asn1Decoder ldapDecoder = new LdapDecoder();
+
+ ByteBuffer stream = ByteBuffer.allocate( 0x56 );
+
+ stream.put(
+ new byte[]
+ {
+
+
+ 0x30, 0x54, // LDAPMessage ::= SEQUENCE {
+ 0x02, 0x01, 0x01, // messageID MessageID
+ 0x66, 0x4f, // CHOICE { ..., modifyRequest ModifyRequest, ...
+ // ModifyRequest ::= [APPLICATION 6] SEQUENCE {
+ // object LDAPDN,
+ 0x04, 0x22, 'c', 'n', '=', 't', 'e', 's', 't', 'M', 'o', 'd', 'i', 'f', 'y', ',', ' ', 'o', 'u', '=', 'u', 's', 'e', 'r', 's', ',', ' ', 'o', 'u', '=', 's', 'y', 's', 't', 'e', 'm',
+ 0x30, 0x29, // modification SEQUENCE OF SEQUENCE {
+ 0x30, 0x11,
+ 0x0A, 0x01, 0x02, // operation ENUMERATED {
+ // add (0),
+ // delete (1),
+ // replace (2) },
+ // modification AttributeTypeAndValues } }
+ 0x30, 0x0c, // AttributeTypeAndValues ::= SEQUENCE {
+ 0x04, 0x01, 'l', // type AttributeDescription,
+ 0x31, 0x07, // vals SET OF AttributeValue }
+ 0x04, 0x05, 'P', 'a', 'r', 'i', 's',
+
+ 0x30, 0x14, // modification SEQUENCE OF *SEQUENCE* {
+ 0x0A, 0x01, 0x02, // operation ENUMERATED {
+ // add (0),
+ // delete (1),
+ // replace (2) },
+ // modification AttributeTypeAndValues } }
+ 0x30, 0x0f, // AttributeTypeAndValues ::= SEQUENCE {
+ 0x04, 0x05, 'a', 't', 't', 'r', 's', // type AttributeDescription,
+ 0x31, 0x06, // vals SET OF AttributeValue }
+ 0x04, 0x04, 't', 'e', 's', 't'
+ } );
+
+ stream.flip();
+
+ // Allocate a ModifyRequest Container
+ IAsn1Container ldapMessageContainer = new LdapMessageContainer();
+
+ try
+ {
+ ldapDecoder.decode( stream, ldapMessageContainer );
+ }
+ catch ( DecoderException de )
+ {
+ de.printStackTrace();
+ Assert.fail( de.getMessage() );
+ }
+
+ LdapMessage message = ( ( LdapMessageContainer ) ldapMessageContainer ).getLdapMessage();
+ ModifyRequest modifyRequest = message.getModifyRequest();
+
+ Assert.assertEquals( 1, message.getMessageId() );
+ Assert.assertEquals( "cn=testModify, ou=users, ou=system", modifyRequest.getObject() );
+
+ ArrayList modifications = modifyRequest.getModifications();
+
+ Assert.assertEquals( 2, modifications.size() );
+
+ HashSet expectedTypes = new HashSet();
+
+ expectedTypes.add("l");
+ expectedTypes.add("attrs");
+
+ HashMap typesVals = new HashMap();
+
+ HashSet lVals = new HashSet();
+ lVals.add("[50][61][72][69][73]");
+ typesVals.put("l", lVals);
+
+ HashSet attrsVals = new HashSet();
+ attrsVals.add("[74][65][73][74]");
+ typesVals.put("attrs", attrsVals);
+
+ BasicAttribute attributeValue = (BasicAttribute)modifications.get( 1 );
+
+ Assert.assertTrue( expectedTypes.contains( attributeValue.getID() ) );
+
+ NamingEnumeration values = attributeValue.getAll();
+ HashSet vals = (HashSet)typesVals.get( attributeValue.getID() );
+
+ while ( values.hasMore() )
+ {
+ OctetString value = (OctetString)values.next();
+
+ Assert.assertTrue( vals.contains( value.toString() ) );
+
+ vals.remove( value.toString() );
+ }
+
+ }
+
+}