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 2006/01/16 17:39:42 UTC
svn commit: r369515 - in /directory/trunks/common/ldap/src:
main/java/org/apache/ldap/common/codec/abandon/
main/java/org/apache/ldap/common/codec/add/
test/java/org/apache/ldap/common/codec/add/
Author: elecharny
Date: Mon Jan 16 08:39:32 2006
New Revision: 369515
URL: http://svn.apache.org/viewcvs?rev=369515&view=rev
Log:
- Added some tests for limit cases
- Removed useless Assert. prefix
- Modified the logs
- Fixed the handling of requests with a empty body
Modified:
directory/trunks/common/ldap/src/main/java/org/apache/ldap/common/codec/abandon/AbandonRequestGrammar.java
directory/trunks/common/ldap/src/main/java/org/apache/ldap/common/codec/add/AddRequest.java
directory/trunks/common/ldap/src/main/java/org/apache/ldap/common/codec/add/AddRequestGrammar.java
directory/trunks/common/ldap/src/main/java/org/apache/ldap/common/codec/add/AddResponseGrammar.java
directory/trunks/common/ldap/src/test/java/org/apache/ldap/common/codec/add/AddRequestTest.java
directory/trunks/common/ldap/src/test/java/org/apache/ldap/common/codec/add/AddResponseTest.java
Modified: directory/trunks/common/ldap/src/main/java/org/apache/ldap/common/codec/abandon/AbandonRequestGrammar.java
URL: http://svn.apache.org/viewcvs/directory/trunks/common/ldap/src/main/java/org/apache/ldap/common/codec/abandon/AbandonRequestGrammar.java?rev=369515&r1=369514&r2=369515&view=diff
==============================================================================
--- directory/trunks/common/ldap/src/main/java/org/apache/ldap/common/codec/abandon/AbandonRequestGrammar.java (original)
+++ directory/trunks/common/ldap/src/main/java/org/apache/ldap/common/codec/abandon/AbandonRequestGrammar.java Mon Jan 16 08:39:32 2006
@@ -103,7 +103,7 @@
if ( ( value == null ) || ( value.getData() == null ) )
{
- String msg = "The AbandonRequest messgaeId must not be null";
+ String msg = "The AbandonRequest messageId must not be null";
log.error( msg );
throw new DecoderException( msg );
}
Modified: directory/trunks/common/ldap/src/main/java/org/apache/ldap/common/codec/add/AddRequest.java
URL: http://svn.apache.org/viewcvs/directory/trunks/common/ldap/src/main/java/org/apache/ldap/common/codec/add/AddRequest.java?rev=369515&r1=369514&r2=369515&view=diff
==============================================================================
--- directory/trunks/common/ldap/src/main/java/org/apache/ldap/common/codec/add/AddRequest.java (original)
+++ directory/trunks/common/ldap/src/main/java/org/apache/ldap/common/codec/add/AddRequest.java Mon Jan 16 08:39:32 2006
@@ -283,7 +283,7 @@
if ( log.isDebugEnabled() )
{
- log.debug( "AddRequest PDU length = " + result );
+ log.debug( "AddRequest PDU length = {}", new Integer( result ) );
}
return result;
@@ -397,8 +397,8 @@
if ( log.isDebugEnabled() )
{
- log.debug( "AddRequest encoding : " + StringTools.dumpBytes( buffer.array() ) );
- log.debug( "AddRequest initial value : " + toString() );
+ log.debug( "AddRequest encoding : {}", StringTools.dumpBytes( buffer.array() ) );
+ log.debug( "AddRequest initial value : {}", toString() );
}
return buffer;
Modified: directory/trunks/common/ldap/src/main/java/org/apache/ldap/common/codec/add/AddRequestGrammar.java
URL: http://svn.apache.org/viewcvs/directory/trunks/common/ldap/src/main/java/org/apache/ldap/common/codec/add/AddRequestGrammar.java?rev=369515&r1=369514&r2=369515&view=diff
==============================================================================
--- directory/trunks/common/ldap/src/main/java/org/apache/ldap/common/codec/add/AddRequestGrammar.java (original)
+++ directory/trunks/common/ldap/src/main/java/org/apache/ldap/common/codec/add/AddRequestGrammar.java Mon Jan 16 08:39:32 2006
@@ -85,7 +85,7 @@
LdapStatesEnum.ADD_REQUEST_VALUE, LdapStatesEnum.ADD_REQUEST_ENTRY_TAG,
new GrammarAction( "Init addRequest" )
{
- public void action( IAsn1Container container )
+ public void action( IAsn1Container container ) throws DecoderException
{
LdapMessageContainer ldapMessageContainer = ( LdapMessageContainer )
@@ -93,6 +93,16 @@
LdapMessage ldapMessage =
ldapMessageContainer.getLdapMessage();
+ // We will check that the request is not null
+ TLV tlv = ldapMessageContainer.getCurrentTLV();
+
+ if ( tlv.getLength().getLength() == 0 )
+ {
+ String msg = "The AddRequest must not be null";
+ log.error( msg );
+ throw new DecoderException( msg );
+ }
+
// Now, we can allocate the ModifyRequest Object
// And we associate it to the ldapMessage Object
ldapMessage.setProtocolOP( new AddRequest() );
@@ -144,23 +154,20 @@
catch ( InvalidNameException ine )
{
String msg = "The DN is invalid : " + StringTools.dumpBytes(tlv.getValue().getData()) + " : " + ine.getMessage();
- log.error( msg + " : " + ine.getMessage());
+ log.error( "{} : {}", msg, ine.getMessage());
throw new DecoderException( msg, ine );
}
catch ( NamingException ne )
{
String msg = "The DN is invalid : " + StringTools.dumpBytes(tlv.getValue().getData()) + " : " + ne.getMessage();
- log.error( msg + " : " + ne.getMessage() );
+ log.error( "{} : {}", msg, ne.getMessage() );
throw new DecoderException( msg, ne );
}
addRequest.setEntry( entry );
}
- if ( log.isDebugEnabled() )
- {
- log.debug( "Adding an entry with DN : " + addRequest.getEntry() );
- }
+ log.debug( "Adding an entry with DN : {}", addRequest.getEntry() );
}
} );
@@ -270,14 +277,14 @@
}
catch ( LdapStringEncodingException lsee )
{
- log.error( "The type is invalid : " + StringTools.dumpBytes( tlv.getValue().getData() ) + " : " + lsee.getMessage() );
+ log.error( "The type is invalid : {} : {}", StringTools.dumpBytes( tlv.getValue().getData() ), lsee.getMessage() );
throw new DecoderException( "Invalid attribute type : " + lsee.getMessage() );
}
}
if ( log.isDebugEnabled() )
{
- log.debug( "Adding type " + type );
+ log.debug( "Adding type {}", type );
}
}
} );
@@ -356,17 +363,14 @@
if ( log.isDebugEnabled() )
{
- log.debug( "Adding value " + StringTools.dumpBytes( (byte[])value ) );
+ log.debug( "Adding value {}", StringTools.dumpBytes( (byte[])value ) );
}
}
else
{
value = StringTools.utf8ToString( tlv.getValue().getData() );
- if ( log.isDebugEnabled() )
- {
- log.debug( "Adding value " + value );
- }
+ log.debug( "Adding value {}" + value );
}
addRequest.addAttributeValue( value );
Modified: directory/trunks/common/ldap/src/main/java/org/apache/ldap/common/codec/add/AddResponseGrammar.java
URL: http://svn.apache.org/viewcvs/directory/trunks/common/ldap/src/main/java/org/apache/ldap/common/codec/add/AddResponseGrammar.java?rev=369515&r1=369514&r2=369515&view=diff
==============================================================================
--- directory/trunks/common/ldap/src/main/java/org/apache/ldap/common/codec/add/AddResponseGrammar.java (original)
+++ directory/trunks/common/ldap/src/main/java/org/apache/ldap/common/codec/add/AddResponseGrammar.java Mon Jan 16 08:39:32 2006
@@ -23,6 +23,7 @@
import org.apache.asn1.ber.IAsn1Container;
import org.apache.asn1.ber.tlv.TLV;
import org.apache.asn1.ber.tlv.UniversalTag;
+import org.apache.asn1.codec.DecoderException;
import org.apache.ldap.common.codec.LdapConstants;
import org.apache.ldap.common.codec.LdapMessage;
import org.apache.ldap.common.codec.LdapMessageContainer;
@@ -77,7 +78,7 @@
LdapStatesEnum.ADD_RESPONSE_VALUE, LdapStatesEnum.ADD_RESPONSE_LDAP_RESULT,
new GrammarAction( "Init AddResponse" )
{
- public void action( IAsn1Container container )
+ public void action( IAsn1Container container ) throws DecoderException
{
LdapMessageContainer ldapMessageContainer = ( LdapMessageContainer )
@@ -85,11 +86,20 @@
LdapMessage ldapMessage =
ldapMessageContainer.getLdapMessage();
+ // We will check that the request is not null
+ TLV tlv = ldapMessageContainer.getCurrentTLV();
+
+ if ( tlv.getLength().getLength() == 0 )
+ {
+ String msg = "The AddResponse must not be null";
+ log.error( msg );
+ throw new DecoderException( msg );
+ }
+
// Now, we can allocate the AddRequest Object
AddResponse addResponse = new AddResponse();
// As this is a new Constructed object, we have to init its length
- TLV tlv = ldapMessageContainer.getCurrentTLV();
int expectedLength = tlv.getLength().getLength();
addResponse.setExpectedLength( expectedLength );
addResponse.setCurrentLength( 0 );
@@ -98,10 +108,7 @@
// And we associate it to the ldapMessage Object
ldapMessage.setProtocolOP( addResponse );
- if ( log.isDebugEnabled() )
- {
- log.debug( "Add Response" );
- }
+ log.debug( "Add Response" );
}
} );
Modified: directory/trunks/common/ldap/src/test/java/org/apache/ldap/common/codec/add/AddRequestTest.java
URL: http://svn.apache.org/viewcvs/directory/trunks/common/ldap/src/test/java/org/apache/ldap/common/codec/add/AddRequestTest.java?rev=369515&r1=369514&r2=369515&view=diff
==============================================================================
--- directory/trunks/common/ldap/src/test/java/org/apache/ldap/common/codec/add/AddRequestTest.java (original)
+++ directory/trunks/common/ldap/src/test/java/org/apache/ldap/common/codec/add/AddRequestTest.java Mon Jan 16 08:39:32 2006
@@ -35,7 +35,6 @@
import org.apache.ldap.common.codec.add.AddRequest;
import org.apache.ldap.common.util.StringTools;
-import junit.framework.Assert;
import junit.framework.TestCase;
/**
@@ -57,25 +56,25 @@
new byte[]
{
0x30, 0x57, // LDAPMessage ::= SEQUENCE {
- 0x02, 0x01, 0x01, // messageID MessageID
- 0x68, 0x52, // CHOICE { ..., addRequest AddRequest, ...
+ 0x02, 0x01, 0x01, // messageID MessageID
+ 0x68, 0x52, // CHOICE { ..., addRequest AddRequest, ...
// AddRequest ::= [APPLICATION 8] SEQUENCE {
// entry 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',
+ 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',
// attributes AttributeList }
- 0x30, 0x2E, // AttributeList ::= SEQUENCE OF SEQUENCE {
- 0x30, 0x0c, // attribute 1
- 0x04, 0x01, 'l', // type AttributeDescription,
- 0x31, 0x07, // vals SET OF AttributeValue }
- 0x04, 0x05, 'P', 'a', 'r', 'i', 's',
+ 0x30, 0x2E, // AttributeList ::= SEQUENCE OF SEQUENCE {
+ 0x30, 0x0c, // attribute 1
+ 0x04, 0x01, 'l', // type AttributeDescription,
+ 0x31, 0x07, // vals SET OF AttributeValue }
+ 0x04, 0x05, 'P', 'a', 'r', 'i', 's',
- 0x30, 0x1E, // attribute 2
+ 0x30, 0x1E, // attribute 2
// type AttributeDescription,
- 0x04, 0x05, 'a', 't', 't', 'r', 's',
- 0x31, 0x15, // vals SET OF AttributeValue }
- 0x04, 0x05, 't', 'e', 's', 't', '1',
- 0x04, 0x05, 't', 'e', 's', 't', '2',
- 0x04, 0x05, 't', 'e', 's', 't', '3',
+ 0x04, 0x05, 'a', 't', 't', 'r', 's',
+ 0x31, 0x15, // vals SET OF AttributeValue }
+ 0x04, 0x05, 't', 'e', 's', 't', '1',
+ 0x04, 0x05, 't', 'e', 's', 't', '2',
+ 0x04, 0x05, 't', 'e', 's', 't', '3',
} );
String decodedPdu = StringTools.dumpBytes( stream.array() );
@@ -92,19 +91,19 @@
catch ( DecoderException de )
{
de.printStackTrace();
- Assert.fail( de.getMessage() );
+ fail( de.getMessage() );
}
LdapMessage message = ( ( LdapMessageContainer ) ldapMessageContainer ).getLdapMessage();
AddRequest addRequest = message.getAddRequest();
// Check the decoded message
- Assert.assertEquals( 1, message.getMessageId() );
- Assert.assertEquals( "cn=testModify,ou=users,ou=system", addRequest.getEntry() );
+ assertEquals( 1, message.getMessageId() );
+ assertEquals( "cn=testModify,ou=users,ou=system", addRequest.getEntry() );
Attributes attributes = addRequest.getAttributes();
- Assert.assertEquals( 2, attributes.size() );
+ assertEquals( 2, attributes.size() );
HashSet expectedTypes = new HashSet();
@@ -125,7 +124,7 @@
BasicAttribute attributeValue = (BasicAttribute)attributes.get( "l" );
- Assert.assertTrue( expectedTypes.contains( attributeValue.getID().toLowerCase() ) );
+ assertTrue( expectedTypes.contains( attributeValue.getID().toLowerCase() ) );
NamingEnumeration values = attributeValue.getAll();
HashSet vals = (HashSet)typesVals.get( attributeValue.getID().toLowerCase() );
@@ -134,14 +133,14 @@
{
Object value = values.next();
- Assert.assertTrue( vals.contains( value.toString() ) );
+ assertTrue( vals.contains( value.toString() ) );
vals.remove( value.toString() );
}
attributeValue = (BasicAttribute)attributes.get( "attrs" );
- Assert.assertTrue( expectedTypes.contains( attributeValue.getID().toLowerCase() ) );
+ assertTrue( expectedTypes.contains( attributeValue.getID().toLowerCase() ) );
values = attributeValue.getAll();
vals = (HashSet)typesVals.get( attributeValue.getID().toLowerCase() );
@@ -150,13 +149,13 @@
{
Object value = values.next();
- Assert.assertTrue( vals.contains( value.toString() ) );
+ assertTrue( vals.contains( value.toString() ) );
vals.remove( value.toString() );
}
// Check the length
- Assert.assertEquals(0x59, message.computeLength());
+ assertEquals(0x59, message.computeLength());
// Check the encoding
try
@@ -165,12 +164,407 @@
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 AddRequest with a null body
+ */
+ public void testDecodeAddRequestNullBody() throws NamingException
+ {
+ Asn1Decoder ldapDecoder = new LdapDecoder();
+
+ ByteBuffer stream = ByteBuffer.allocate( 0x39 );
+
+ stream.put(
+ new byte[]
+ {
+ 0x30, 0x37, // LDAPMessage ::= SEQUENCE {
+ 0x02, 0x01, 0x01, // messageID MessageID
+ 0x68, 0x00 // CHOICE { ..., addRequest AddRequest, ...
+ } );
+
+ stream.flip();
+
+ // Allocate a LdapMessage Container
+ IAsn1Container ldapMessageContainer = new LdapMessageContainer();
+
+ // Decode a AddRequest message
+ try
+ {
+ ldapDecoder.decode( stream, ldapMessageContainer );
+ }
+ catch ( DecoderException de )
+ {
+ System.out.println( de.getMessage() );
+ assertTrue( true );
+ return;
+ }
+
+ fail( "We should not reach this point" );
+ }
+
+ /**
+ * Test the decoding of a AddRequest with a null entry
+ */
+ public void testDecodeAddRequestNullEntry() throws NamingException
+ {
+ Asn1Decoder ldapDecoder = new LdapDecoder();
+
+ ByteBuffer stream = ByteBuffer.allocate( 0x39 );
+
+ stream.put(
+ new byte[]
+ {
+ 0x30, 0x37, // LDAPMessage ::= SEQUENCE {
+ 0x02, 0x01, 0x01, // messageID MessageID
+ 0x68, 0x26, // CHOICE { ..., addRequest AddRequest, ...
+ // AddRequest ::= [APPLICATION 8] SEQUENCE {
+ // entry LDAPDN,
+ 0x04, 0x00,
+ // attributes AttributeList }
+ 0x30, 0x2E, // AttributeList ::= SEQUENCE OF SEQUENCE {
+ 0x30, 0x0c, // attribute 1
+ 0x04, 0x01, 'l', // type AttributeDescription,
+ 0x31, 0x07, // vals SET OF AttributeValue }
+ 0x04, 0x05, 'P', 'a', 'r', 'i', 's',
+
+ 0x30, 0x1E, // attribute 2
+ // type AttributeDescription,
+ 0x04, 0x05, 'a', 't', 't', 'r', 's',
+ 0x31, 0x15, // vals SET OF AttributeValue }
+ 0x04, 0x05, 't', 'e', 's', 't', '1',
+ 0x04, 0x05, 't', 'e', 's', 't', '2',
+ 0x04, 0x05, 't', 'e', 's', 't', '3',
+ } );
+
+ stream.flip();
+
+ // Allocate a LdapMessage Container
+ IAsn1Container ldapMessageContainer = new LdapMessageContainer();
+
+ // Decode a AddRequest message
+ try
+ {
+ ldapDecoder.decode( stream, ldapMessageContainer );
+ }
+ catch ( DecoderException de )
+ {
+ System.out.println( de.getMessage() );
+ assertTrue( true );
+ return;
+ }
+
+ fail( "We should not reach this point" );
+ }
+
+ /**
+ * Test the decoding of a AddRequest with a null attributeList
+ */
+ public void testDecodeAddRequestNullAttributes() throws NamingException
+ {
+ Asn1Decoder ldapDecoder = new LdapDecoder();
+
+ ByteBuffer stream = ByteBuffer.allocate( 0x2B );
+
+ stream.put(
+ new byte[]
+ {
+ 0x30, 0x29, // LDAPMessage ::= SEQUENCE {
+ 0x02, 0x01, 0x01, // messageID MessageID
+ 0x68, 0x24, // CHOICE { ..., addRequest AddRequest, ...
+ // AddRequest ::= [APPLICATION 8] SEQUENCE {
+ // entry 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',
+ // attributes AttributeList }
+ 0x30, 0x00, // AttributeList ::= SEQUENCE OF SEQUENCE {
+ } );
+
+ stream.flip();
+
+ // Allocate a LdapMessage Container
+ IAsn1Container ldapMessageContainer = new LdapMessageContainer();
+
+ // Decode a AddRequest message
+ try
+ {
+ ldapDecoder.decode( stream, ldapMessageContainer );
+ }
+ catch ( DecoderException de )
+ {
+ System.out.println( de.getMessage() );
+ assertTrue( true );
+ return;
+ }
+
+ fail( "We should not reach this point" );
+ }
+
+ /**
+ * Test the decoding of a AddRequest with a empty attributeList
+ */
+ public void testDecodeAddRequestNullAttributeList() throws NamingException
+ {
+ Asn1Decoder ldapDecoder = new LdapDecoder();
+
+ ByteBuffer stream = ByteBuffer.allocate( 0x2D );
+
+ stream.put(
+ new byte[]
+ {
+ 0x30, 0x2B, // LDAPMessage ::= SEQUENCE {
+ 0x02, 0x01, 0x01, // messageID MessageID
+ 0x68, 0x26, // CHOICE { ..., addRequest AddRequest, ...
+ // AddRequest ::= [APPLICATION 8] SEQUENCE {
+ // entry 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',
+ // attributes AttributeList }
+ 0x30, 0x02, // AttributeList ::= SEQUENCE OF SEQUENCE {
+ 0x30, 0x00, // AttributeList ::= SEQUENCE OF SEQUENCE {
+ } );
+
+ stream.flip();
+
+ // Allocate a LdapMessage Container
+ IAsn1Container ldapMessageContainer = new LdapMessageContainer();
+
+ // Decode a AddRequest message
+ try
+ {
+ ldapDecoder.decode( stream, ldapMessageContainer );
+ }
+ catch ( DecoderException de )
+ {
+ System.out.println( de.getMessage() );
+ assertTrue( true );
+ return;
+ }
+
+ fail( "We should not reach this point" );
+ }
+
+ /**
+ * Test the decoding of a AddRequest with a empty attributeList
+ */
+ public void testDecodeAddRequestNullType() throws NamingException
+ {
+ Asn1Decoder ldapDecoder = new LdapDecoder();
+
+ ByteBuffer stream = ByteBuffer.allocate( 0x2F );
+
+ stream.put(
+ new byte[]
+ {
+ 0x30, 0x2D, // LDAPMessage ::= SEQUENCE {
+ 0x02, 0x01, 0x01, // messageID MessageID
+ 0x68, 0x28, // CHOICE { ..., addRequest AddRequest, ...
+ // AddRequest ::= [APPLICATION 8] SEQUENCE {
+ // entry 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',
+ // attributes AttributeList }
+ 0x30, 0x04, // AttributeList ::= SEQUENCE OF SEQUENCE {
+ 0x30, 0x02, // attribute 1
+ 0x04, 0x00, // type AttributeDescription,
+ } );
+
+ stream.flip();
+
+ // Allocate a LdapMessage Container
+ IAsn1Container ldapMessageContainer = new LdapMessageContainer();
+
+ // Decode a AddRequest message
+ try
+ {
+ ldapDecoder.decode( stream, ldapMessageContainer );
+ }
+ catch ( DecoderException de )
+ {
+ System.out.println( de.getMessage() );
+ assertTrue( true );
+ return;
+ }
+
+ fail( "We should not reach this point" );
+ }
+
+ /**
+ * Test the decoding of a AddRequest with a empty attributeList
+ */
+ public void testDecodeAddRequestNoVals() throws NamingException
+ {
+ Asn1Decoder ldapDecoder = new LdapDecoder();
+
+ ByteBuffer stream = ByteBuffer.allocate( 0x30 );
+
+ stream.put(
+ new byte[]
+ {
+ 0x30, 0x2E, // LDAPMessage ::= SEQUENCE {
+ 0x02, 0x01, 0x01, // messageID MessageID
+ 0x68, 0x29, // CHOICE { ..., addRequest AddRequest, ...
+ // AddRequest ::= [APPLICATION 8] SEQUENCE {
+ // entry 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',
+ // attributes AttributeList }
+ 0x30, 0x05, // AttributeList ::= SEQUENCE OF SEQUENCE {
+ 0x30, 0x03, // attribute 1
+ 0x04, 0x01, 'A', // type AttributeDescription,
+ } );
+
+ stream.flip();
+
+ // Allocate a LdapMessage Container
+ IAsn1Container ldapMessageContainer = new LdapMessageContainer();
+
+ // Decode a AddRequest message
+ try
+ {
+ ldapDecoder.decode( stream, ldapMessageContainer );
+ }
+ catch ( DecoderException de )
+ {
+ System.out.println( de.getMessage() );
+ assertTrue( true );
+ return;
+ }
+
+ fail( "We should not reach this point" );
+ }
+
+ /**
+ * Test the decoding of a AddRequest with a empty attributeList
+ */
+ public void testDecodeAddRequestNullVals() throws NamingException
+ {
+ Asn1Decoder ldapDecoder = new LdapDecoder();
+
+ ByteBuffer stream = ByteBuffer.allocate( 0x32 );
+
+ stream.put(
+ new byte[]
+ {
+ 0x30, 0x30, // LDAPMessage ::= SEQUENCE {
+ 0x02, 0x01, 0x01, // messageID MessageID
+ 0x68, 0x2B, // CHOICE { ..., addRequest AddRequest, ...
+ // AddRequest ::= [APPLICATION 8] SEQUENCE {
+ // entry 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',
+ // attributes AttributeList }
+ 0x30, 0x07, // AttributeList ::= SEQUENCE OF SEQUENCE {
+ 0x30, 0x05, // attribute 1
+ 0x04, 0x01, 'A', // type AttributeDescription,
+ 0x31, 0x00
+ } );
+
+ stream.flip();
+
+ // Allocate a LdapMessage Container
+ IAsn1Container ldapMessageContainer = new LdapMessageContainer();
+
+ // Decode a AddRequest message
+ try
+ {
+ ldapDecoder.decode( stream, ldapMessageContainer );
+ }
+ catch ( DecoderException de )
+ {
+ System.out.println( de.getMessage() );
+ assertTrue( true );
+ return;
+ }
+
+ fail( "We should not reach this point" );
+ }
+
+ /**
+ * Test the decoding of a AddRequest with a empty attributeList
+ */
+ public void testDecodeAddRequestEmptyAttributeValue() throws NamingException
+ {
+ Asn1Decoder ldapDecoder = new LdapDecoder();
+
+ ByteBuffer stream = ByteBuffer.allocate( 0x34 );
+
+ stream.put(
+ new byte[]
+ {
+ 0x30, 0x32, // LDAPMessage ::= SEQUENCE {
+ 0x02, 0x01, 0x01, // messageID MessageID
+ 0x68, 0x2D, // CHOICE { ..., addRequest AddRequest, ...
+ // AddRequest ::= [APPLICATION 8] SEQUENCE {
+ // entry 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',
+ // attributes AttributeList }
+ 0x30, 0x09, // AttributeList ::= SEQUENCE OF SEQUENCE {
+ 0x30, 0x07, // attribute 1
+ 0x04, 0x01, 'l', // type AttributeDescription,
+ 0x31, 0x02,
+ 0x04, 0x00
+ } );
+
+ String decodedPdu = StringTools.dumpBytes( stream.array() );
+ stream.flip();
+
+ // Allocate a LdapMessage Container
+ IAsn1Container ldapMessageContainer = new LdapMessageContainer();
+
+ // Decode a AddRequest message
+ try
+ {
+ ldapDecoder.decode( stream, ldapMessageContainer );
+ }
+ catch ( DecoderException de )
+ {
+ de.printStackTrace();
+ fail( de.getMessage() );
+ }
+
+ LdapMessage message = ( ( LdapMessageContainer ) ldapMessageContainer ).getLdapMessage();
+ AddRequest addRequest = message.getAddRequest();
+
+ // Check the decoded message
+ assertEquals( 1, message.getMessageId() );
+ assertEquals( "cn=testModify,ou=users,ou=system", addRequest.getEntry() );
+
+ Attributes attributes = addRequest.getAttributes();
+
+ assertEquals( 1, attributes.size() );
+
+ BasicAttribute attributeValue = (BasicAttribute)attributes.get( "l" );
+
+ assertEquals( "l", attributeValue.getID().toLowerCase() );
+
+ NamingEnumeration values = attributeValue.getAll();
+
+ while ( values.hasMore() )
+ {
+ Object value = values.next();
+
+ assertEquals( "", value.toString() );
+ }
+
+ // Check the length
+ assertEquals(0x34, 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() );
}
}
}
Modified: directory/trunks/common/ldap/src/test/java/org/apache/ldap/common/codec/add/AddResponseTest.java
URL: http://svn.apache.org/viewcvs/directory/trunks/common/ldap/src/test/java/org/apache/ldap/common/codec/add/AddResponseTest.java?rev=369515&r1=369514&r2=369515&view=diff
==============================================================================
--- directory/trunks/common/ldap/src/test/java/org/apache/ldap/common/codec/add/AddResponseTest.java (original)
+++ directory/trunks/common/ldap/src/test/java/org/apache/ldap/common/codec/add/AddResponseTest.java Mon Jan 16 08:39:32 2006
@@ -28,7 +28,6 @@
import org.apache.ldap.common.codec.add.AddResponse;
import org.apache.ldap.common.util.StringTools;
-import junit.framework.Assert;
import junit.framework.TestCase;
/**
@@ -75,20 +74,20 @@
catch ( DecoderException de )
{
de.printStackTrace();
- Assert.fail( de.getMessage() );
+ fail( de.getMessage() );
}
// Check the decoded AddResponse
LdapMessage message = ( ( LdapMessageContainer ) ldapMessageContainer ).getLdapMessage();
AddResponse addResponse = message.getAddResponse();
- Assert.assertEquals( 1, message.getMessageId() );
- Assert.assertEquals( 0, addResponse.getLdapResult().getResultCode() );
- Assert.assertEquals( "", addResponse.getLdapResult().getMatchedDN() );
- Assert.assertEquals( "", addResponse.getLdapResult().getErrorMessage() );
+ assertEquals( 1, message.getMessageId() );
+ assertEquals( 0, addResponse.getLdapResult().getResultCode() );
+ assertEquals( "", addResponse.getLdapResult().getMatchedDN() );
+ assertEquals( "", addResponse.getLdapResult().getErrorMessage() );
// Check the length
- Assert.assertEquals(0x0E, message.computeLength());
+ assertEquals(0x0E, message.computeLength());
try
{
@@ -96,13 +95,50 @@
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 AddResponse with no LdapResult
+ */
+ public void testDecodeAddResponseEmptyResult()
+ {
+ Asn1Decoder ldapDecoder = new LdapDecoder();
+
+ ByteBuffer stream = ByteBuffer.allocate( 0x0E );
+
+ stream.put(
+ new byte[]
+ {
+ 0x30, 0x0C, // LDAPMessage ::=SEQUENCE {
+ 0x02, 0x01, 0x01, // messageID MessageID
+ 0x69, 0x00, // CHOICE { ..., addResponse AddResponse, ...
+ } );
+
+ stream.flip();
+
+ // Allocate a LdapMessage Container
+ IAsn1Container ldapMessageContainer = new LdapMessageContainer();
+
+ // Decode a AddResponse message
+ try
+ {
+ ldapDecoder.decode( stream, ldapMessageContainer );
+ }
+ catch ( DecoderException de )
+ {
+ System.out.println( de.getMessage() );
+ assertTrue( true );
+ return;
+ }
+
+ fail( "We should not reach this point" );
}
}