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 2017/04/08 06:35:27 UTC

svn commit: r1790656 - in /directory/shared/trunk/ldap/codec/core/src: main/java/org/apache/directory/api/ldap/codec/actions/controls/InitControls.java test/java/org/apache/directory/api/ldap/codec/LdapControlTest.java

Author: elecharny
Date: Sat Apr  8 06:35:27 2017
New Revision: 1790656

URL: http://svn.apache.org/viewvc?rev=1790656&view=rev
Log:
Fixed a bug with empty controls generated by Samba

Modified:
    directory/shared/trunk/ldap/codec/core/src/main/java/org/apache/directory/api/ldap/codec/actions/controls/InitControls.java
    directory/shared/trunk/ldap/codec/core/src/test/java/org/apache/directory/api/ldap/codec/LdapControlTest.java

Modified: directory/shared/trunk/ldap/codec/core/src/main/java/org/apache/directory/api/ldap/codec/actions/controls/InitControls.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/codec/core/src/main/java/org/apache/directory/api/ldap/codec/actions/controls/InitControls.java?rev=1790656&r1=1790655&r2=1790656&view=diff
==============================================================================
--- directory/shared/trunk/ldap/codec/core/src/main/java/org/apache/directory/api/ldap/codec/actions/controls/InitControls.java (original)
+++ directory/shared/trunk/ldap/codec/core/src/main/java/org/apache/directory/api/ldap/codec/actions/controls/InitControls.java Sat Apr  8 06:35:27 2017
@@ -35,6 +35,8 @@ import org.slf4j.LoggerFactory;
  * <pre>
  *         ... },
  *     controls       [0] Controls OPTIONAL }
+ *     
+ * Controls ::= SEQUENCE OF control Control
  * </pre>
  * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
  */
@@ -64,18 +66,23 @@ public class InitControls extends Gramma
         TLV tlv = container.getCurrentTLV();
         int expectedLength = tlv.getLength();
 
-        // The Length should be null
-        if ( expectedLength == 0 )
+        // The Length can be null
+        if ( expectedLength != 0 )
         {
-            LOG.error( "The length of controls must not be null" );
-
-            // This will generate a PROTOCOL_ERROR
-            throw new DecoderException( "The length of controls must not be null" );
+            if ( IS_DEBUG )
+            {
+                LOG.debug( "A new list of controls has been initialized" );
+            }
         }
-
-        if ( IS_DEBUG )
+        else
         {
-            LOG.debug( "A new list of controls has been initialized" );
+            if ( IS_DEBUG )
+            {
+                LOG.debug( "An empty list of controls has been initialized" );
+            }
         }
+
+        // We can have an END transition
+        container.setGrammarEndAllowed( true );
     }
 }

Modified: directory/shared/trunk/ldap/codec/core/src/test/java/org/apache/directory/api/ldap/codec/LdapControlTest.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/codec/core/src/test/java/org/apache/directory/api/ldap/codec/LdapControlTest.java?rev=1790656&r1=1790655&r2=1790656&view=diff
==============================================================================
--- directory/shared/trunk/ldap/codec/core/src/test/java/org/apache/directory/api/ldap/codec/LdapControlTest.java (original)
+++ directory/shared/trunk/ldap/codec/core/src/test/java/org/apache/directory/api/ldap/codec/LdapControlTest.java Sat Apr  8 06:35:27 2017
@@ -64,114 +64,31 @@ public class LdapControlTest extends Abs
 
         ByteBuffer stream = ByteBuffer.allocate( 0x64 );
         stream.put( new byte[]
-            { 0x30, 0x62, // LDAPMessage ::=SEQUENCE {
-                0x02,
-                0x01,
-                0x03, // messageID MessageID
-                0x50,
-                0x01,
-                0x02, // CHOICE { ..., abandonRequest
-                // AbandonRequest,...
-                ( byte ) 0xA0,
-                0x5A, // controls [0] Controls OPTIONAL }
-                0x30,
-                0x1A, // Control ::= SEQUENCE {
-                // controlType LDAPOID,
-                0x04,
-                0x0D,
-                '1',
-                '.',
-                '3',
-                '.',
-                '6',
-                '.',
-                '1',
-                '.',
-                '5',
-                '.',
-                '5',
-                '.',
-                '1',
-                // criticality BOOLEAN DEFAULT FALSE,
-                0x01,
-                0x01,
-                ( byte ) 0xFF,
-                // controlValue OCTET STRING OPTIONAL }
-                0x04,
-                0x06,
-                'a',
-                'b',
-                'c',
-                'd',
-                'e',
-                'f',
-                0x30,
-                0x17, // Control ::= SEQUENCE {
-                // controlType LDAPOID,
-                0x04,
-                0x0D,
-                '1',
-                '.',
-                '3',
-                '.',
-                '6',
-                '.',
-                '1',
-                '.',
-                '5',
-                '.',
-                '5',
-                '.',
-                '2',
-                // controlValue OCTET STRING OPTIONAL }
-                0x04,
-                0x06,
-                'g',
-                'h',
-                'i',
-                'j',
-                'k',
-                'l',
-                0x30,
-                0x12, // Control ::= SEQUENCE {
-                // controlType LDAPOID,
-                0x04,
-                0x0D,
-                '1',
-                '.',
-                '3',
-                '.',
-                '6',
-                '.',
-                '1',
-                '.',
-                '5',
-                '.',
-                '5',
-                '.',
-                '3',
-                // criticality BOOLEAN DEFAULT FALSE}
-                0x01,
-                0x01,
-                ( byte ) 0xFF,
-                0x30,
-                0x0F, // Control ::= SEQUENCE {
-                // controlType LDAPOID}
-                0x04,
-                0x0D,
-                '1',
-                '.',
-                '3',
-                '.',
-                '6',
-                '.',
-                '1',
-                '.',
-                '5',
-                '.',
-                '5',
-                '.',
-                '4' } );
+            { 
+                0x30, 0x62,                       // LDAPMessage ::=SEQUENCE {
+                  0x02, 0x01, 0x03,               // messageID MessageID
+                    0x50, 0x01, 0x02,             // CHOICE { ..., abandonRequest
+                                                // AbandonRequest,...
+                  ( byte ) 0xA0, 0x5A,            // controls [0] Controls OPTIONAL }
+                    0x30, 0x1A,                   // Control ::= SEQUENCE {
+                      0x04, 0x0D,                 // controlType LDAPOID,
+                        '1', '.', '3', '.', '6', '.', '1', '.', '5', '.', '5', '.', '1',
+                      0x01, 0x01, ( byte ) 0xFF,  // criticality BOOLEAN DEFAULT FALSE, 
+                      0x04, 0x06,                 // controlValue OCTET STRING OPTIONAL }
+                        'a', 'b', 'c', 'd', 'e', 'f',
+                    0x30, 0x17,                   // Control ::= SEQUENCE {
+                      0x04, 0x0D,                 // controlType LDAPOID,
+                        '1', '.', '3', '.', '6', '.', '1', '.', '5', '.', '5', '.', '2',
+                      0x04, 0x06,                 // controlValue OCTET STRING OPTIONAL }
+                        'g', 'h', 'i', 'j', 'k', 'l',
+                    0x30, 0x12,                   // Control ::= SEQUENCE {
+                      0x04, 0x0D,                 // controlType LDAPOID,
+                        '1', '.', '3', '.', '6', '.', '1', '.', '5', '.', '5', '.', '3',
+                      0x01, 0x01, ( byte ) 0xFF,  // criticality BOOLEAN DEFAULT FALSE}
+                    0x30, 0x0F,                   // Control ::= SEQUENCE {
+                      0x04, 0x0D,                 // controlType LDAPOID}
+                        '1', '.', '3', '.', '6', '.', '1', '.', '5', '.', '5', '.', '4' 
+            } );
 
         stream.flip();
 
@@ -240,6 +157,88 @@ public class LdapControlTest extends Abs
 
             // Don't check the PDU, as control are in a Map, and can be in a different order
             // So we decode the generated PDU, and we compare it with the initial message
+            try
+            {
+                ldapDecoder.decode( bb, ldapMessageContainer );
+            }
+            catch ( DecoderException de )
+            {
+                de.printStackTrace();
+                fail( de.getMessage() );
+            }
+
+            AbandonRequest abandonRequest2 = ldapMessageContainer.getMessage();
+
+            assertEquals( abandonRequest, abandonRequest2 );
+        }
+        catch ( EncoderException ee )
+        {
+            ee.printStackTrace();
+            fail( ee.getMessage() );
+        }
+    }
+
+    
+    /**
+     * Test the decoding of a Request with an empty list of controls
+     */
+    @Test
+    public void testDecodeRequestWithEmptyControls()
+    {
+        Asn1Decoder ldapDecoder = new Asn1Decoder();
+
+        ByteBuffer stream = ByteBuffer.allocate( 0x0A );
+        stream.put( new byte[]
+            { 
+                0x30, 0x08,                       // LDAPMessage ::=SEQUENCE {
+                  0x02, 0x01, 0x03,               // messageID MessageID
+                    0x50, 0x01, 0x02,             // CHOICE { ..., abandonRequest
+                                                // AbandonRequest,...
+                  ( byte ) 0xA0, 0x00             // controls [0] Controls OPTIONAL }
+            } );
+
+        stream.flip();
+
+        // Allocate a LdapMessageContainer Container
+        LdapMessageContainer<AbandonRequestDecorator> ldapMessageContainer =
+            new LdapMessageContainer<AbandonRequestDecorator>( codec );
+
+        // Decode the PDU
+        try
+        {
+            ldapDecoder.decode( stream, ldapMessageContainer );
+        }
+        catch ( DecoderException de )
+        {
+            de.printStackTrace();
+            fail( de.getMessage() );
+        }
+
+        // Check that everything is OK
+        AbandonRequestDecorator abandonRequest = ldapMessageContainer.getMessage();
+
+        // Copy the message
+        AbandonRequest internalAbandonRequest = new AbandonRequestImpl( abandonRequest.getAbandoned() );
+        internalAbandonRequest.setMessageId( abandonRequest.getMessageId() );
+
+        assertEquals( 3, abandonRequest.getMessageId() );
+        assertEquals( 2, abandonRequest.getAbandoned() );
+
+        // Check the Controls
+        Map<String, Control> controls = abandonRequest.getControls();
+
+        assertEquals( 0, controls.size() );
+
+        // Check the encoding
+        try
+        {
+            ByteBuffer bb = encoder.encodeMessage( internalAbandonRequest );
+
+            // Check the length, which should be 2 bytes shorter, as we don't encode teh empty control
+            assertEquals( 0x08, bb.limit() );
+
+            // Don't check the PDU, as control are in a Map, and can be in a different order
+            // So we decode the generated PDU, and we compare it with the initial message
             try
             {
                 ldapDecoder.decode( bb, ldapMessageContainer );