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/03/30 22:21:14 UTC
svn commit: r159520 -
directory/sandbox/trunk/asn1-new-codec/src/test/org/apache/asn1/ldap/codec/LdapDecoderTest.java
Author: elecharny
Date: Wed Mar 30 12:21:12 2005
New Revision: 159520
URL: http://svn.apache.org/viewcvs?view=rev&rev=159520
Log:
Added junit tests to the LdapDecoder class. Test a bad length, a bad tag...
Modified:
directory/sandbox/trunk/asn1-new-codec/src/test/org/apache/asn1/ldap/codec/LdapDecoderTest.java
Modified: 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=diff&r1=159519&r2=159520
==============================================================================
--- directory/sandbox/trunk/asn1-new-codec/src/test/org/apache/asn1/ldap/codec/LdapDecoderTest.java (original)
+++ directory/sandbox/trunk/asn1-new-codec/src/test/org/apache/asn1/ldap/codec/LdapDecoderTest.java Wed Mar 30 12:21:12 2005
@@ -258,4 +258,175 @@
// after being reset.
ldapMessageContainer.free();
}
+
+ /**
+ * Test the decoding of a PDU with a bad Length.
+ * The first TLV has a length of 0x32 when the PDU is 0x33 bytes long.
+ */
+ public void testDecodeBadLengthTooSmall()
+ {
+ Asn1Decoder ldapDecoder = new Asn1Decoder();
+
+ ByteBuffer stream = ByteBuffer.allocate( 0x35 );
+ stream.put(
+ new byte[]
+ {
+ // Length should be 0x33...
+ 0x30, 0x32, // 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 )
+ {
+ Assert.assertEquals( "Current Length is above expected Length", de.getMessage() );
+ return;
+ }
+
+ Assert.fail("Should never arrive here...");
+ }
+
+ /**
+ * Test the decoding of a PDU with a bad primitive Length.
+ * The second TLV has a length of 0x02 when the PDU is 0x01 bytes long.
+ */
+ public void testDecodeBadPrimitiveLengthTooBig()
+ {
+ Asn1Decoder ldapDecoder = new Asn1Decoder();
+
+ ByteBuffer stream = ByteBuffer.allocate( 0x35 );
+ stream.put(
+ new byte[]
+ {
+ 0x30, 0x33, // LDAPMessage ::=SEQUENCE {
+ // Length should be 0x01...
+ 0x02, 0x02, 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'
+ } );
+
+ 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 )
+ {
+ Assert.assertEquals( "Universal tag 14 is reserved", de.getMessage() );
+ return;
+ }
+
+ Assert.fail("Should never arrive here...");
+ }
+
+ /**
+ * Test the decoding of a PDU with a bad primitive Length.
+ * The second TLV has a length of 0x02 when the PDU is 0x01 bytes long.
+ */
+ public void testDecodeBadTagTransition()
+ {
+ Asn1Decoder ldapDecoder = new Asn1Decoder();
+
+ ByteBuffer stream = ByteBuffer.allocate( 0x35 );
+ stream.put(
+ new byte[]
+ {
+ 0x30, 0x33, // LDAPMessage ::=SEQUENCE {
+ // Length should be 0x01...
+ 0x02, 0x01, 0x01, // messageID MessageID
+ 0x2D, 0x2D, // 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'
+ } );
+
+ 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 )
+ {
+ Assert.assertEquals( "Bad transition from state PROTOCOL_OP_TAG, tag [2D]", de.getMessage() );
+ return;
+ }
+
+ Assert.fail("Should never arrive here...");
+ }
}