Return-Path: Delivered-To: apmail-directory-commits-archive@www.apache.org Received: (qmail 38165 invoked from network); 17 Jan 2006 18:35:53 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur.apache.org with SMTP; 17 Jan 2006 18:35:53 -0000 Received: (qmail 63897 invoked by uid 500); 17 Jan 2006 18:35:51 -0000 Delivered-To: apmail-directory-commits-archive@directory.apache.org Received: (qmail 63826 invoked by uid 500); 17 Jan 2006 18:35:50 -0000 Mailing-List: contact commits-help@directory.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@directory.apache.org Delivered-To: mailing list commits@directory.apache.org Received: (qmail 63783 invoked by uid 99); 17 Jan 2006 18:35:50 -0000 Received: from asf.osuosl.org (HELO asf.osuosl.org) (140.211.166.49) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 17 Jan 2006 10:35:50 -0800 X-ASF-Spam-Status: No, hits=-9.4 required=10.0 tests=ALL_TRUSTED,NO_REAL_NAME X-Spam-Check-By: apache.org Received: from [209.237.227.194] (HELO minotaur.apache.org) (209.237.227.194) by apache.org (qpsmtpd/0.29) with SMTP; Tue, 17 Jan 2006 10:35:48 -0800 Received: (qmail 38022 invoked by uid 65534); 17 Jan 2006 18:35:27 -0000 Message-ID: <20060117183527.38021.qmail@minotaur.apache.org> Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r369859 - in /directory/trunks/common/ldap/src: main/java/org/apache/ldap/common/codec/modify/ test/java/org/apache/ldap/common/codec/modify/ Date: Tue, 17 Jan 2006 18:35:26 -0000 To: commits@directory.apache.org From: elecharny@apache.org X-Mailer: svnmailer-1.0.5 X-Virus-Checked: Checked by ClamAV on apache.org X-Spam-Rating: minotaur.apache.org 1.6.2 0/1000/N Author: elecharny Date: Tue Jan 17 10:35:16 2006 New Revision: 369859 URL: http://svn.apache.org/viewcvs?rev=369859&view=rev Log: - Added some tests case - Removed useless Assert prefix - Modified logs - Fixed a bug : an action was lacking for empty vals Modified: directory/trunks/common/ldap/src/main/java/org/apache/ldap/common/codec/modify/ModifyRequestGrammar.java directory/trunks/common/ldap/src/test/java/org/apache/ldap/common/codec/modify/ModifyRequestTest.java directory/trunks/common/ldap/src/test/java/org/apache/ldap/common/codec/modify/ModifyResponseTest.java Modified: directory/trunks/common/ldap/src/main/java/org/apache/ldap/common/codec/modify/ModifyRequestGrammar.java URL: http://svn.apache.org/viewcvs/directory/trunks/common/ldap/src/main/java/org/apache/ldap/common/codec/modify/ModifyRequestGrammar.java?rev=369859&r1=369858&r2=369859&view=diff ============================================================================== --- directory/trunks/common/ldap/src/main/java/org/apache/ldap/common/codec/modify/ModifyRequestGrammar.java (original) +++ directory/trunks/common/ldap/src/main/java/org/apache/ldap/common/codec/modify/ModifyRequestGrammar.java Tue Jan 17 10:35:16 2006 @@ -286,7 +286,7 @@ } catch ( IntegerDecoderException ide ) { - String msg = "Invalid operation ({}), it should be 0, 1 or 2" + StringTools.dumpBytes( tlv.getValue().getData() ); + String msg = "Invalid operation ( " + StringTools.dumpBytes( tlv.getValue().getData() ) + "), it should be 0, 1 or 2"; log.error( msg ); throw new DecoderException( msg ); } @@ -405,10 +405,33 @@ // AttributeTypeAndValues ::= SEQUENCE { // ... // vals SET OF AttributeValue } (Value) - // Nothing to do + // It can be null. super.transitions[LdapStatesEnum.MODIFY_REQUEST_VALS_VALUE][UniversalTag.SET_TAG] = new GrammarTransition( LdapStatesEnum.MODIFY_REQUEST_VALS_VALUE, - LdapStatesEnum.MODIFY_REQUEST_ATTRIBUTE_VALUE_OR_MODIFICATION_TAG, null ); + LdapStatesEnum.MODIFY_REQUEST_ATTRIBUTE_VALUE_OR_MODIFICATION_TAG, + new GrammarAction( "Attribute vals" ) + { + public void action( IAsn1Container container ) + { + + LdapMessageContainer ldapMessageContainer = ( LdapMessageContainer ) + container; + + TLV tlv = + ldapMessageContainer.getCurrentTLV(); + + // If the length is null, we store an empty value + if ( tlv.getLength().getLength() == 0 ) + { + log.debug( "No vals for this attribute" ); + } + + // We can have an END transition + ldapMessageContainer.grammarEndAllowed( true ); + + log.debug( "Some vals are to be decoded" ); + } + } ); // AttributeTypeAndValues ::= SEQUENCE { // ... Modified: directory/trunks/common/ldap/src/test/java/org/apache/ldap/common/codec/modify/ModifyRequestTest.java URL: http://svn.apache.org/viewcvs/directory/trunks/common/ldap/src/test/java/org/apache/ldap/common/codec/modify/ModifyRequestTest.java?rev=369859&r1=369858&r2=369859&view=diff ============================================================================== --- directory/trunks/common/ldap/src/test/java/org/apache/ldap/common/codec/modify/ModifyRequestTest.java (original) +++ directory/trunks/common/ldap/src/test/java/org/apache/ldap/common/codec/modify/ModifyRequestTest.java Tue Jan 17 10:35:16 2006 @@ -18,10 +18,7 @@ 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 javax.naming.directory.ModificationItem; @@ -36,7 +33,6 @@ import org.apache.ldap.common.codec.modify.ModifyRequest; import org.apache.ldap.common.util.StringTools; -import junit.framework.Assert; import junit.framework.TestCase; /** @@ -60,33 +56,33 @@ 0x30, 0x52, // LDAPMessage ::= SEQUENCE { - 0x02, 0x01, 0x01, // messageID MessageID - 0x66, 0x4d, // CHOICE { ..., modifyRequest ModifyRequest, ... + 0x02, 0x01, 0x01, // messageID MessageID + 0x66, 0x4d, // CHOICE { ..., modifyRequest ModifyRequest, ... // ModifyRequest ::= [APPLICATION 6] SEQUENCE { // object LDAPDN, - 0x04, 0x20, '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 { + 0x04, 0x20, '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, 0x00, // 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' + 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, 0x00, // 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' } ); String decodedPdu = StringTools.dumpBytes( stream.array() ); @@ -103,61 +99,38 @@ catch ( DecoderException de ) { de.printStackTrace(); - Assert.fail( de.getMessage() ); + fail( de.getMessage() ); } // Check the decoded PDU LdapMessage message = ( ( LdapMessageContainer ) ldapMessageContainer ).getLdapMessage(); ModifyRequest modifyRequest = message.getModifyRequest(); - Assert.assertEquals( 1, message.getMessageId() ); - Assert.assertEquals( "cn=testModify,ou=users,ou=system", modifyRequest.getObject() ); + assertEquals( 1, message.getMessageId() ); + assertEquals( "cn=testModify,ou=users,ou=system", modifyRequest.getObject() ); ArrayList modifications = modifyRequest.getModifications(); - Assert.assertEquals( 2, modifications.size() ); + assertEquals( 2, modifications.size() ); - HashSet expectedTypes = new HashSet(); - - expectedTypes.add("l"); - expectedTypes.add("attrs"); - - HashMap typesVals = new HashMap(); - - HashSet lVals = new HashSet(); - lVals.add("Paris"); - typesVals.put("l", lVals); - - HashSet attrsVals = new HashSet(); - attrsVals.add("test"); - typesVals.put("attrs", attrsVals); - - ModificationItem modification = (ModificationItem)modifications.get( 1 ); + ModificationItem modification = (ModificationItem)modifications.get( 0 ); BasicAttribute attributeValue = (BasicAttribute)modification.getAttribute(); - Assert.assertTrue( expectedTypes.contains( attributeValue.getID().toLowerCase() ) ); + assertEquals( "l", attributeValue.getID().toLowerCase() ); - NamingEnumeration values = attributeValue.getAll(); - HashSet vals = (HashSet)typesVals.get( attributeValue.getID().toLowerCase() ); + String attrValue = (String)attributeValue.get( 0 ); + assertEquals( "Paris", attrValue ); - while ( values.hasMore() ) - { - Object value = values.next(); + modification = (ModificationItem)modifications.get( 1 ); + attributeValue = (BasicAttribute)modification.getAttribute(); - if ( value instanceof String ) - { - Assert.assertTrue( vals.contains( value ) ); - } - else - { - Assert.assertTrue( vals.contains( StringTools.utf8ToString( (byte[])value ) ) ); - } + assertEquals( "attrs", attributeValue.getID().toLowerCase() ); - vals.remove( value.toString() ); - } + attrValue = (String)attributeValue.get( 0 ); + assertEquals( "test", attrValue ); // Check the length - Assert.assertEquals(0x54, message.computeLength()); + assertEquals(0x54, message.computeLength()); // Check the encoding try @@ -166,12 +139,12 @@ String encodedPdu = StringTools.dumpBytes( bb.array() ); - Assert.assertEquals(encodedPdu, decodedPdu ); + assertEquals(encodedPdu, decodedPdu ); } catch ( EncoderException ee ) { ee.printStackTrace(); - Assert.fail( ee.getMessage() ); + fail( ee.getMessage() ); } } @@ -237,61 +210,38 @@ catch ( DecoderException de ) { de.printStackTrace(); - Assert.fail( de.getMessage() ); + fail( de.getMessage() ); } // Check the decoded PDU LdapMessage message = ( ( LdapMessageContainer ) ldapMessageContainer ).getLdapMessage(); ModifyRequest modifyRequest = message.getModifyRequest(); - Assert.assertEquals( 21, message.getMessageId() ); - Assert.assertEquals( "cn=Tori Amos,ou=playground,dc=apache,dc=org", modifyRequest.getObject() ); + assertEquals( 21, message.getMessageId() ); + assertEquals( "cn=Tori Amos,ou=playground,dc=apache,dc=org", modifyRequest.getObject() ); ArrayList modifications = modifyRequest.getModifications(); - Assert.assertEquals( 2, modifications.size() ); - - HashSet expectedTypes = new HashSet(); - - expectedTypes.add("telephoneNumber"); - expectedTypes.add("cn"); + assertEquals( 2, modifications.size() ); - HashMap typesVals = new HashMap(); - - HashSet telephoneVals = new HashSet(); - telephoneVals.add("1234567890"); - typesVals.put("telephoneNumber", telephoneVals); - - HashSet cnVals = new HashSet(); - cnVals.add("XXX"); - typesVals.put("cn", cnVals); - - ModificationItem modification = (ModificationItem)modifications.get( 1 ); + ModificationItem modification = (ModificationItem)modifications.get( 0 ); BasicAttribute attributeValue = (BasicAttribute)modification.getAttribute(); - Assert.assertTrue( expectedTypes.contains( attributeValue.getID().toLowerCase() ) ); + assertEquals( "telephonenumber", attributeValue.getID().toLowerCase() ); - NamingEnumeration values = attributeValue.getAll(); - HashSet vals = (HashSet)typesVals.get( attributeValue.getID().toLowerCase() ); + String attrValue = (String)attributeValue.get( 0 ); + assertEquals( "1234567890", attrValue ); - while ( values.hasMore() ) - { - Object value = values.next(); + modification = (ModificationItem)modifications.get( 1 ); + attributeValue = (BasicAttribute)modification.getAttribute(); - if ( value instanceof String ) - { - Assert.assertTrue( vals.contains( value ) ); - } - else - { - Assert.assertTrue( vals.contains( StringTools.utf8ToString( (byte[])value ) ) ); - } + assertEquals( "cn", attributeValue.getID().toLowerCase() ); - vals.remove( value.toString() ); - } + attrValue = (String)attributeValue.get( 0 ); + assertEquals( "XXX", attrValue ); // Check the length - Assert.assertEquals(0x8C, message.computeLength()); + assertEquals(0x8C, message.computeLength()); // Check the encoding, by decoding and re-encoding the result try @@ -306,7 +256,7 @@ catch ( DecoderException de ) { de.printStackTrace(); - Assert.fail( de.getMessage() ); + fail( de.getMessage() ); } LdapMessage message2 = ( ( LdapMessageContainer ) ldapMessageContainer ).getLdapMessage(); @@ -315,12 +265,12 @@ ByteBuffer bb2 = message2.encode( null ); String decodedPdu2 = StringTools.dumpBytes( bb2.array() ); - Assert.assertEquals( decodedPdu1, decodedPdu2 ); + assertEquals( decodedPdu1, decodedPdu2 ); } catch ( EncoderException ee ) { ee.printStackTrace(); - Assert.fail( ee.getMessage() ); + fail( ee.getMessage() ); } } @@ -353,7 +303,7 @@ 0x04, 0x0B, 0x64, 0x65, 0x73, 0x63, 0x72, 0x69, 0x70, 0x74, 0x69, 0x6F, 0x6E, - 0x31, 0x00, // Vals = null + 0x31, 0x00, // Vals = null 0x30, 0x25, // Modification 0x0A, 0x01, 0x00, // Operation : Add 0x30, 0x20, // type : telephoneNumber @@ -394,57 +344,47 @@ catch ( DecoderException de ) { de.printStackTrace(); - Assert.fail( de.getMessage() ); + fail( de.getMessage() ); } // Check the decoded PDU LdapMessage message = ( ( LdapMessageContainer ) ldapMessageContainer ).getLdapMessage(); ModifyRequest modifyRequest = message.getModifyRequest(); - Assert.assertEquals( 49, message.getMessageId() ); - Assert.assertEquals( "cn=Tori Amos,ou=playground,dc=apache,dc=org", modifyRequest.getObject() ); + assertEquals( 49, message.getMessageId() ); + assertEquals( "cn=Tori Amos,ou=playground,dc=apache,dc=org", modifyRequest.getObject() ); ArrayList modifications = modifyRequest.getModifications(); - Assert.assertEquals( 3, modifications.size() ); - - HashSet expectedTypes = new HashSet(); - - expectedTypes.add("telephonenumber"); - expectedTypes.add("description"); - - HashMap typesVals = new HashMap(); - - HashSet telephoneVals = new HashSet(); - telephoneVals.add("01234567890"); - typesVals.put("telephonenumber", telephoneVals); + assertEquals( 3, modifications.size() ); - ModificationItem modification = (ModificationItem)modifications.get( 1 ); + ModificationItem modification = (ModificationItem)modifications.get( 0 ); BasicAttribute attributeValue = (BasicAttribute)modification.getAttribute(); - Assert.assertTrue( expectedTypes.contains( attributeValue.getID().toLowerCase() ) ); + assertEquals( "description", attributeValue.getID().toLowerCase() ); + assertEquals( 0, attributeValue.size() ); - NamingEnumeration values = attributeValue.getAll(); - HashSet vals = (HashSet)typesVals.get( attributeValue.getID().toLowerCase() ); + modification = (ModificationItem)modifications.get( 1 ); + attributeValue = (BasicAttribute)modification.getAttribute(); - while ( values.hasMore() ) - { - Object value = values.next(); + String attrValue = (String)attributeValue.get( 0 ); - if ( value instanceof String ) - { - Assert.assertTrue( vals.contains( value ) ); - } - else - { - Assert.assertTrue( vals.contains( StringTools.utf8ToString( (byte[])value ) ) ); - } + assertEquals( "telephonenumber", attributeValue.getID().toLowerCase() ); + + assertEquals( "01234567890", attrValue ); + + modification = (ModificationItem)modifications.get( 2 ); + attributeValue = (BasicAttribute)modification.getAttribute(); + + attrValue = (String)attributeValue.get( 0 ); + + assertEquals( "telephonenumber", attributeValue.getID().toLowerCase() ); - vals.remove( value.toString() ); - } + attrValue = (String)attributeValue.get( 0 ); + assertEquals( "01234567890", attrValue ); // Check the length - Assert.assertEquals(0xB9, message.computeLength()); + assertEquals(0xB9, message.computeLength()); // Check the encoding, by decoding and re-encoding the result try @@ -459,7 +399,7 @@ catch ( DecoderException de ) { de.printStackTrace(); - Assert.fail( de.getMessage() ); + fail( de.getMessage() ); } LdapMessage message2 = ( ( LdapMessageContainer ) ldapMessageContainer ).getLdapMessage(); @@ -468,12 +408,556 @@ ByteBuffer bb2 = message2.encode( null ); String decodedPdu2 = StringTools.dumpBytes( bb2.array() ); - Assert.assertEquals( decodedPdu1, decodedPdu2 ); + assertEquals( decodedPdu1, decodedPdu2 ); + } + catch ( EncoderException ee ) + { + ee.printStackTrace(); + fail( ee.getMessage() ); + } + } + + // Defensive tests + + /** + * Test the decoding of a ModifyRequest with an empty body + */ + public void testDecodeModifyRequestEmptyBody() throws NamingException + { + Asn1Decoder ldapDecoder = new LdapDecoder(); + + ByteBuffer stream = ByteBuffer.allocate( 0x07 ); + + stream.put( + new byte[] + { + 0x30, 0x05, // LdapMessage + 0x02, 0x01, 0x31, // Message ID : 49 + 0x66, 0x00 // ModifyRequest + } ); + + stream.flip(); + + // Allocate a LdapMessage Container + IAsn1Container ldapMessageContainer = new LdapMessageContainer(); + + // Decode a ModifyRequest PDU + try + { + ldapDecoder.decode( stream, ldapMessageContainer ); + fail("We should never reach this point !!!"); + } + catch ( DecoderException de ) + { + assertTrue( true ); + } + } + + /** + * Test the decoding of a ModifyRequest with an empty object + */ + public void testDecodeModifyRequestEmptyObject() throws NamingException + { + Asn1Decoder ldapDecoder = new LdapDecoder(); + + ByteBuffer stream = ByteBuffer.allocate( 0x29 ); + + stream.put( + new byte[] + { + 0x30, 0x27, // LdapMessage + 0x02, 0x01, 0x31, // Message ID : 49 + 0x66, 0x22, // ModifyRequest + 0x04, 0x20, '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' + } ); + + stream.flip(); + + // Allocate a LdapMessage Container + IAsn1Container ldapMessageContainer = new LdapMessageContainer(); + + // Decode a ModifyRequest PDU + try + { + ldapDecoder.decode( stream, ldapMessageContainer ); + fail("We should never reach this point !!!"); + } + catch ( DecoderException de ) + { + assertTrue( true ); + } + } + + /** + * Test the decoding of a ModifyRequest with an empty modification + */ + public void testDecodeModifyRequestEmptyModification() throws NamingException + { + Asn1Decoder ldapDecoder = new LdapDecoder(); + + ByteBuffer stream = ByteBuffer.allocate( 0x2B ); + + stream.put( + new byte[] + { + 0x30, 0x29, // LdapMessage + 0x02, 0x01, 0x31, // Message ID : 49 + 0x66, 0x24, // ModifyRequest + 0x04, 0x20, '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, 0x00 + } ); + + stream.flip(); + + // Allocate a LdapMessage Container + IAsn1Container ldapMessageContainer = new LdapMessageContainer(); + + // Decode a ModifyRequest PDU + try + { + ldapDecoder.decode( stream, ldapMessageContainer ); + fail("We should never reach this point !!!"); + } + catch ( DecoderException de ) + { + assertTrue( true ); + } + } + + /** + * Test the decoding of a ModifyRequest with an empty operation + */ + public void testDecodeModifyRequestEmptyOperation() throws NamingException + { + Asn1Decoder ldapDecoder = new LdapDecoder(); + + ByteBuffer stream = ByteBuffer.allocate( 0x2D ); + + stream.put( + new byte[] + { + 0x30, 0x2B, // LdapMessage + 0x02, 0x01, 0x31, // Message ID : 49 + 0x66, 0x26, // ModifyRequest + 0x04, 0x20, '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, 0x02, + 0x30, 0x00 + } ); + + stream.flip(); + + // Allocate a LdapMessage Container + IAsn1Container ldapMessageContainer = new LdapMessageContainer(); + + // Decode a ModifyRequest PDU + try + { + ldapDecoder.decode( stream, ldapMessageContainer ); + fail("We should never reach this point !!!"); + } + catch ( DecoderException de ) + { + assertTrue( true ); + } + } + + /** + * Test the decoding of a ModifyRequest with an wrong empty operation + */ + public void testDecodeModifyRequestWrongOperationEmpty() throws NamingException + { + Asn1Decoder ldapDecoder = new LdapDecoder(); + + ByteBuffer stream = ByteBuffer.allocate( 0x2F ); + + stream.put( + new byte[] + { + 0x30, 0x2D, // LdapMessage + 0x02, 0x01, 0x31, // Message ID : 49 + 0x66, 0x28, // ModifyRequest + 0x04, 0x20, '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, 0x04, + 0x30, 0x02, + 0x0A, 0x00 + } ); + + stream.flip(); + + // Allocate a LdapMessage Container + IAsn1Container ldapMessageContainer = new LdapMessageContainer(); + + // Decode a ModifyRequest PDU + try + { + ldapDecoder.decode( stream, ldapMessageContainer ); + fail("We should never reach this point !!!"); + } + catch ( DecoderException de ) + { + assertTrue( true ); + } + } + + /** + * Test the decoding of a ModifyRequest with an wrong operation + */ + public void testDecodeModifyRequestWrongOperation() throws NamingException + { + Asn1Decoder ldapDecoder = new LdapDecoder(); + + ByteBuffer stream = ByteBuffer.allocate( 0x30 ); + + stream.put( + new byte[] + { + 0x30, 0x2E, // LdapMessage + 0x02, 0x01, 0x31, // Message ID : 49 + 0x66, 0x29, // ModifyRequest + 0x04, 0x20, '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, 0x05, + 0x30, 0x03, + 0x0A, 0x01, 0x04 + } ); + + stream.flip(); + + // Allocate a LdapMessage Container + IAsn1Container ldapMessageContainer = new LdapMessageContainer(); + + // Decode a ModifyRequest PDU + try + { + ldapDecoder.decode( stream, ldapMessageContainer ); + fail("We should never reach this point !!!"); + } + catch ( DecoderException de ) + { + assertTrue( true ); + } + } + + /** + * Test the decoding of a ModifyRequest with an add operation, and nothing more + */ + public void testDecodeModifyRequestAddOperationEnd() throws NamingException + { + Asn1Decoder ldapDecoder = new LdapDecoder(); + + ByteBuffer stream = ByteBuffer.allocate( 0x30 ); + + stream.put( + new byte[] + { + 0x30, 0x2E, // LdapMessage + 0x02, 0x01, 0x31, // Message ID : 49 + 0x66, 0x29, // ModifyRequest + 0x04, 0x20, '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, 0x05, + 0x30, 0x03, + 0x0A, 0x01, 0x00 + } ); + + stream.flip(); + + // Allocate a LdapMessage Container + IAsn1Container ldapMessageContainer = new LdapMessageContainer(); + + // Decode a ModifyRequest PDU + try + { + ldapDecoder.decode( stream, ldapMessageContainer ); + fail("We should never reach this point !!!"); + } + catch ( DecoderException de ) + { + assertTrue( true ); + } + } + + /** + * Test the decoding of a ModifyRequest with an add operation, and an + * empty modification + */ + public void testDecodeModifyRequestAddOperationEmptyModification() throws NamingException + { + Asn1Decoder ldapDecoder = new LdapDecoder(); + + ByteBuffer stream = ByteBuffer.allocate( 0x32 ); + + stream.put( + new byte[] + { + 0x30, 0x30, // LdapMessage + 0x02, 0x01, 0x31, // Message ID : 49 + 0x66, 0x2B, // ModifyRequest + 0x04, 0x20, '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, 0x07, + 0x30, 0x05, + 0x0A, 0x01, 0x00, + 0x30, 0x00 + } ); + + stream.flip(); + + // Allocate a LdapMessage Container + IAsn1Container ldapMessageContainer = new LdapMessageContainer(); + + // Decode a ModifyRequest PDU + try + { + ldapDecoder.decode( stream, ldapMessageContainer ); + fail("We should never reach this point !!!"); + } + catch ( DecoderException de ) + { + assertTrue( true ); + } + } + + /** + * Test the decoding of a ModifyRequest with an add operation, and a + * modification with an empty type + */ + public void testDecodeModifyRequestAddOperationModificationEmptyType() throws NamingException + { + Asn1Decoder ldapDecoder = new LdapDecoder(); + + ByteBuffer stream = ByteBuffer.allocate( 0x34 ); + + stream.put( + new byte[] + { + 0x30, 0x32, // LdapMessage + 0x02, 0x01, 0x31, // Message ID : 49 + 0x66, 0x2D, // ModifyRequest + 0x04, 0x20, '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, 0x09, + 0x30, 0x07, + 0x0A, 0x01, 0x00, + 0x30, 0x02, + 0x04, 0x00 + } ); + + stream.flip(); + + // Allocate a LdapMessage Container + IAsn1Container ldapMessageContainer = new LdapMessageContainer(); + + // Decode a ModifyRequest PDU + try + { + ldapDecoder.decode( stream, ldapMessageContainer ); + fail("We should never reach this point !!!"); + } + catch ( DecoderException de ) + { + assertTrue( true ); + } + } + + /** + * Test the decoding of a ModifyRequest with an add operation, and a + * modification with a type and no vals + */ + public void testDecodeModifyRequestAddOperationModificationTypeNoVals() throws NamingException + { + Asn1Decoder ldapDecoder = new LdapDecoder(); + + ByteBuffer stream = ByteBuffer.allocate( 0x35 ); + + stream.put( + new byte[] + { + 0x30, 0x33, // LdapMessage + 0x02, 0x01, 0x31, // Message ID : 49 + 0x66, 0x2E, // ModifyRequest + 0x04, 0x20, '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, 0x0A, + 0x30, 0x08, + 0x0A, 0x01, 0x00, + 0x30, 0x03, + 0x04, 0x01, 'l', + } ); + + stream.flip(); + + // Allocate a LdapMessage Container + IAsn1Container ldapMessageContainer = new LdapMessageContainer(); + + // Decode a ModifyRequest PDU + try + { + ldapDecoder.decode( stream, ldapMessageContainer ); + fail("We should never reach this point !!!"); + } + catch ( DecoderException de ) + { + assertTrue( true ); + } + } + + /** + * Test the decoding of a ModifyRequest with an add operation, and a + * modification with a type and an empty vals + */ + public void testDecodeModifyRequestAddOperationModificationTypeEmptyVals() throws NamingException + { + Asn1Decoder ldapDecoder = new LdapDecoder(); + + ByteBuffer stream = ByteBuffer.allocate( 0x37 ); + + stream.put( + new byte[] + { + 0x30, 0x35, // LdapMessage + 0x02, 0x01, 0x31, // Message ID : 49 + 0x66, 0x30, // ModifyRequest + 0x04, 0x20, '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, 0x0C, + 0x30, 0x0A, + 0x0A, 0x01, 0x00, + 0x30, 0x05, + 0x04, 0x01, 'l', + 0x31, 0x00 + } ); + + String decodedPdu = StringTools.dumpBytes( stream.array() ); + stream.flip(); + + // Allocate a LdapMessage Container + IAsn1Container ldapMessageContainer = new LdapMessageContainer(); + + // Decode a ModifyRequest PDU + try + { + ldapDecoder.decode( stream, ldapMessageContainer ); + } + catch ( DecoderException de ) + { + de.printStackTrace(); + fail( de.getMessage() ); + } + + // Check the decoded PDU + LdapMessage message = ( ( LdapMessageContainer ) ldapMessageContainer ).getLdapMessage(); + ModifyRequest modifyRequest = message.getModifyRequest(); + + assertEquals( 49, message.getMessageId() ); + assertEquals( "cn=testModify,ou=users,ou=system", modifyRequest.getObject() ); + + ArrayList modifications = modifyRequest.getModifications(); + + assertEquals( 1, modifications.size() ); + + ModificationItem modification = (ModificationItem)modifications.get( 0 ); + BasicAttribute attributeValue = (BasicAttribute)modification.getAttribute(); + + assertEquals( "l", attributeValue.getID().toLowerCase() ); + assertEquals( 0, attributeValue.size() ); + + // Check the length + assertEquals(0x37, message.computeLength()); + + // Check the encoding + try + { + ByteBuffer bb = message.encode( null ); + + String encodedPdu = StringTools.dumpBytes( bb.array() ); + + assertEquals(encodedPdu, decodedPdu ); + } + catch ( EncoderException ee ) + { + ee.printStackTrace(); + fail( ee.getMessage() ); + } + } + + /** + * Test the decoding of a ModifyRequest with an add operation, and a + * modification with a type and two vals + */ + public void testDecodeModifyRequestAddOperationModificationType2Vals() throws NamingException + { + Asn1Decoder ldapDecoder = new LdapDecoder(); + + ByteBuffer stream = ByteBuffer.allocate( 0x3D ); + + stream.put( + new byte[] + { + 0x30, 0x3B, // LdapMessage + 0x02, 0x01, 0x31, // Message ID : 49 + 0x66, 0x36, // ModifyRequest + 0x04, 0x20, '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, 0x12, + 0x30, 0x10, + 0x0A, 0x01, 0x00, + 0x30, 0x0B, + 0x04, 0x01, 'l', + 0x31, 0x06, + 0x04, 0x01, 'a', + 0x04, 0x01, 'b' + } ); + + String decodedPdu = StringTools.dumpBytes( stream.array() ); + stream.flip(); + + // Allocate a LdapMessage Container + IAsn1Container ldapMessageContainer = new LdapMessageContainer(); + + // Decode a ModifyRequest PDU + try + { + ldapDecoder.decode( stream, ldapMessageContainer ); + } + catch ( DecoderException de ) + { + de.printStackTrace(); + fail( de.getMessage() ); + } + + // Check the decoded PDU + LdapMessage message = ( ( LdapMessageContainer ) ldapMessageContainer ).getLdapMessage(); + ModifyRequest modifyRequest = message.getModifyRequest(); + + assertEquals( 49, message.getMessageId() ); + assertEquals( "cn=testModify,ou=users,ou=system", modifyRequest.getObject() ); + + ArrayList modifications = modifyRequest.getModifications(); + + assertEquals( 1, modifications.size() ); + + ModificationItem modification = (ModificationItem)modifications.get( 0 ); + BasicAttribute attributeValue = (BasicAttribute)modification.getAttribute(); + + assertEquals( "l", attributeValue.getID().toLowerCase() ); + assertEquals( 2, attributeValue.size() ); + + String attrValue = (String)attributeValue.get( 0 ); + assertEquals( "a", attrValue ); + + attrValue = (String)attributeValue.get( 1 ); + assertEquals( "b", attrValue ); + + // Check the length + assertEquals(0x3D, message.computeLength()); + + // Check the encoding + try + { + ByteBuffer bb = message.encode( null ); + + String encodedPdu = StringTools.dumpBytes( bb.array() ); + + assertEquals(encodedPdu, decodedPdu ); } catch ( EncoderException ee ) { ee.printStackTrace(); - Assert.fail( ee.getMessage() ); + fail( ee.getMessage() ); } } } Modified: directory/trunks/common/ldap/src/test/java/org/apache/ldap/common/codec/modify/ModifyResponseTest.java URL: http://svn.apache.org/viewcvs/directory/trunks/common/ldap/src/test/java/org/apache/ldap/common/codec/modify/ModifyResponseTest.java?rev=369859&r1=369858&r2=369859&view=diff ============================================================================== --- directory/trunks/common/ldap/src/test/java/org/apache/ldap/common/codec/modify/ModifyResponseTest.java (original) +++ directory/trunks/common/ldap/src/test/java/org/apache/ldap/common/codec/modify/ModifyResponseTest.java Tue Jan 17 10:35:16 2006 @@ -28,7 +28,6 @@ import org.apache.ldap.common.codec.modify.ModifyResponse; import org.apache.ldap.common.util.StringTools; -import junit.framework.Assert; import junit.framework.TestCase; /** @@ -77,20 +76,20 @@ catch ( DecoderException de ) { de.printStackTrace(); - Assert.fail( de.getMessage() ); + fail( de.getMessage() ); } // Check the decoded ModifyResponse PDU LdapMessage message = ( ( LdapMessageContainer ) ldapMessageContainer ).getLdapMessage(); ModifyResponse modifyResponse = message.getModifyResponse(); - Assert.assertEquals( 1, message.getMessageId() ); - Assert.assertEquals( 0, modifyResponse.getLdapResult().getResultCode() ); - Assert.assertEquals( "", modifyResponse.getLdapResult().getMatchedDN() ); - Assert.assertEquals( "", modifyResponse.getLdapResult().getErrorMessage() ); + assertEquals( 1, message.getMessageId() ); + assertEquals( 0, modifyResponse.getLdapResult().getResultCode() ); + assertEquals( "", modifyResponse.getLdapResult().getMatchedDN() ); + assertEquals( "", modifyResponse.getLdapResult().getErrorMessage() ); // Check the length - Assert.assertEquals(0x0E, message.computeLength()); + assertEquals(0x0E, message.computeLength()); // Check the encoding try @@ -99,12 +98,49 @@ String encodedPdu = StringTools.dumpBytes( bb.array() ); - Assert.assertEquals(encodedPdu, decodedPdu ); + assertEquals(encodedPdu, decodedPdu ); } catch ( EncoderException ee ) { ee.printStackTrace(); - Assert.fail( ee.getMessage() ); + fail( ee.getMessage() ); } + } + + /** + * Test the decoding of a ModifyResponse with no LdapResult + */ + public void testDecodeModifyResponseEmptyResult() + { + Asn1Decoder ldapDecoder = new LdapDecoder(); + + ByteBuffer stream = ByteBuffer.allocate( 0x07 ); + + stream.put( + new byte[] + { + 0x30, 0x05, // LDAPMessage ::=SEQUENCE { + 0x02, 0x01, 0x01, // messageID MessageID + 0x67, 0x00, // CHOICE { ..., modifyResponse ModifyResponse, ... + } ); + + stream.flip(); + + // Allocate a LdapMessage Container + IAsn1Container ldapMessageContainer = new LdapMessageContainer(); + + // Decode a ModifyResponse message + try + { + ldapDecoder.decode( stream, ldapMessageContainer ); + } + catch ( DecoderException de ) + { + System.out.println( de.getMessage() ); + assertTrue( true ); + return; + } + + fail( "We should not reach this point" ); } }