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 2018/12/05 04:40:14 UTC

[directory-ldap-api] branch master updated: o Fixed the reverse encoding of controls o Added some check for message with controls

This is an automated email from the ASF dual-hosted git repository.

elecharny pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/directory-ldap-api.git


The following commit(s) were added to refs/heads/master by this push:
     new 0289866  o Fixed the reverse encoding of controls o Added some check for message with controls
0289866 is described below

commit 028986684252b31358cd2c3bf022df6410bfa2e1
Author: elecharny <el...@emmanuels-mbp>
AuthorDate: Wed Dec 5 05:40:12 2018 +0100

    o Fixed the reverse encoding of controls
    o Added some check for message with controls
---
 .../directory/api/ldap/codec/api/LdapEncoder.java  |  6 +++
 .../api/ldap/codec/abandon/AbandonRequestTest.java | 25 ++++-------
 .../api/ldap/codec/add/AddRequestTest.java         |  9 ++--
 .../api/ldap/codec/add/AddResponseTest.java        | 52 +++++++++++++---------
 4 files changed, 49 insertions(+), 43 deletions(-)

diff --git a/ldap/codec/core/src/main/java/org/apache/directory/api/ldap/codec/api/LdapEncoder.java b/ldap/codec/core/src/main/java/org/apache/directory/api/ldap/codec/api/LdapEncoder.java
index 8bc2226..2ca242e 100644
--- a/ldap/codec/core/src/main/java/org/apache/directory/api/ldap/codec/api/LdapEncoder.java
+++ b/ldap/codec/core/src/main/java/org/apache/directory/api/ldap/codec/api/LdapEncoder.java
@@ -196,6 +196,12 @@ public final class LdapEncoder
 
             // the value, if any
             controlFactory.encodeValue( buffer, control );
+            
+            if ( buffer.getPos() != start )
+            {
+                // The control value sequence, as an OctetString
+                BerValue.encodeSequence( buffer, ( byte ) UniversalTag.OCTET_STRING.getValue(), start );
+            }
 
             // The criticality
             if ( control.isCritical() )
diff --git a/ldap/codec/core/src/test/java/org/apache/directory/api/ldap/codec/abandon/AbandonRequestTest.java b/ldap/codec/core/src/test/java/org/apache/directory/api/ldap/codec/abandon/AbandonRequestTest.java
index e9847d2..b2c84d1 100644
--- a/ldap/codec/core/src/test/java/org/apache/directory/api/ldap/codec/abandon/AbandonRequestTest.java
+++ b/ldap/codec/core/src/test/java/org/apache/directory/api/ldap/codec/abandon/AbandonRequestTest.java
@@ -20,13 +20,12 @@
 package org.apache.directory.api.ldap.codec.abandon;
 
 
+import static org.junit.Assert.assertArrayEquals;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertTrue;
-import static org.junit.Assert.fail;
 
 import java.nio.ByteBuffer;
-import java.util.Arrays;
 import java.util.Map;
 
 import org.apache.directory.api.asn1.DecoderException;
@@ -119,26 +118,26 @@ public class AbandonRequestTest extends AbstractCodecServiceTest
 
         assertEquals( 4, controls.size() );
 
-        CodecControl<? extends Control> control = ( org.apache.directory.api.ldap.codec.api.CodecControl<?> ) controls
+        CodecControl<? extends Control> control = ( CodecControl<?> ) controls
             .get( "1.3.6.1.5.5.1" );
         assertEquals( "1.3.6.1.5.5.1", control.getOid() );
         assertEquals( "0x61 0x62 0x63 0x64 0x65 0x66 ", Strings.dumpBytes( control.getValue() ) );
         assertTrue( control.isCritical() );
         internalAbandonRequest.addControl( control );
 
-        control = ( org.apache.directory.api.ldap.codec.api.CodecControl<?> ) controls.get( "1.3.6.1.5.5.2" );
+        control = ( CodecControl<?> ) controls.get( "1.3.6.1.5.5.2" );
         assertEquals( "1.3.6.1.5.5.2", control.getOid() );
         assertEquals( "0x67 0x68 0x69 0x6A 0x6B 0x6C ", Strings.dumpBytes( control.getValue() ) );
         assertFalse( control.isCritical() );
         internalAbandonRequest.addControl( control );
 
-        control = ( org.apache.directory.api.ldap.codec.api.CodecControl<?> ) controls.get( "1.3.6.1.5.5.3" );
+        control = ( CodecControl<?> ) controls.get( "1.3.6.1.5.5.3" );
         assertEquals( "1.3.6.1.5.5.3", control.getOid() );
         assertEquals( "", Strings.dumpBytes( control.getValue() ) );
         assertTrue( control.isCritical() );
         internalAbandonRequest.addControl( control );
 
-        control = ( org.apache.directory.api.ldap.codec.api.CodecControl<?> ) controls.get( "1.3.6.1.5.5.4" );
+        control = ( CodecControl<?> ) controls.get( "1.3.6.1.5.5.4" );
         assertEquals( "1.3.6.1.5.5.4", control.getOid() );
         assertEquals( "", Strings.dumpBytes( control.getValue() ) );
         assertFalse( control.isCritical() );
@@ -152,15 +151,7 @@ public class AbandonRequestTest extends AbstractCodecServiceTest
 
         // 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() );
-        }
+        ldapDecoder.decode( bb, ldapMessageContainer );
 
         AbandonRequest abandonRequest2 = ldapMessageContainer.getMessage();
         assertEquals( abandonRequest, abandonRequest2 );
@@ -210,13 +201,13 @@ public class AbandonRequestTest extends AbstractCodecServiceTest
         // Check the length
         assertEquals( 0x0A, bb.limit() );
 
-        assertTrue( Arrays.equals( stream.array(), bb.array() ) );
+        assertArrayEquals( stream.array(), bb.array() );
 
         // Check the reverse encoding
         Asn1Buffer buffer = new Asn1Buffer();
         LdapEncoder.encodeMessageReverse( buffer, codec, internalAbandonRequest );
 
-        assertTrue( Arrays.equals( stream.array(), buffer.getBytes().array() ) );
+        assertArrayEquals( stream.array(), buffer.getBytes().array() );
     }
 
 
diff --git a/ldap/codec/core/src/test/java/org/apache/directory/api/ldap/codec/add/AddRequestTest.java b/ldap/codec/core/src/test/java/org/apache/directory/api/ldap/codec/add/AddRequestTest.java
index 2fadf24..0840988 100644
--- a/ldap/codec/core/src/test/java/org/apache/directory/api/ldap/codec/add/AddRequestTest.java
+++ b/ldap/codec/core/src/test/java/org/apache/directory/api/ldap/codec/add/AddRequestTest.java
@@ -20,11 +20,11 @@
 package org.apache.directory.api.ldap.codec.add;
 
 
+import static org.junit.Assert.assertArrayEquals;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertTrue;
 
 import java.nio.ByteBuffer;
-import java.util.Arrays;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Map;
@@ -196,7 +196,7 @@ public class AddRequestTest extends AbstractCodecServiceTest
 
         LdapEncoder.encodeMessageReverse( buffer, codec, request );
 
-        assertTrue( Arrays.equals( stream.array(), buffer.getBytes().array() ) );
+        assertArrayEquals( stream.array(), buffer.getBytes().array() );
     }
 
 
@@ -609,6 +609,7 @@ public class AddRequestTest extends AbstractCodecServiceTest
         String encodedPdu = Strings.dumpBytes( bb.array() );
 
         assertEquals( encodedPdu, decodedPdu );
+        
         // Check encode reverse
         Asn1Buffer buffer = new Asn1Buffer();
 
@@ -618,7 +619,7 @@ public class AddRequestTest extends AbstractCodecServiceTest
 
         LdapEncoder.encodeMessageReverse( buffer, codec, request );
 
-        assertTrue( Arrays.equals( stream.array(), buffer.getBytes().array() ) );
+        assertArrayEquals( stream.array(), buffer.getBytes().array() );
     }
 
 
@@ -719,6 +720,6 @@ public class AddRequestTest extends AbstractCodecServiceTest
 
         LdapEncoder.encodeMessageReverse( buffer, codec, request );
 
-        assertTrue( Arrays.equals( stream.array(), buffer.getBytes().array() ) );
+        assertArrayEquals( stream.array(), buffer.getBytes().array() );
     }
 }
diff --git a/ldap/codec/core/src/test/java/org/apache/directory/api/ldap/codec/add/AddResponseTest.java b/ldap/codec/core/src/test/java/org/apache/directory/api/ldap/codec/add/AddResponseTest.java
index fe16ca2..391306f 100644
--- a/ldap/codec/core/src/test/java/org/apache/directory/api/ldap/codec/add/AddResponseTest.java
+++ b/ldap/codec/core/src/test/java/org/apache/directory/api/ldap/codec/add/AddResponseTest.java
@@ -20,11 +20,10 @@
 package org.apache.directory.api.ldap.codec.add;
 
 
+import static org.junit.Assert.assertArrayEquals;
 import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertTrue;
 
 import java.nio.ByteBuffer;
-import java.util.Arrays;
 import java.util.Map;
 
 import org.apache.directory.api.asn1.DecoderException;
@@ -84,7 +83,6 @@ public class AddResponseTest extends AbstractCodecServiceTest
                                             // }
         } );
 
-        String decodedPdu = Strings.dumpBytes( stream.array() );
         stream.flip();
 
         // Allocate a LdapMessage Container
@@ -106,9 +104,7 @@ public class AddResponseTest extends AbstractCodecServiceTest
         // Check the length
         assertEquals( 0x0E, bb.limit() );
 
-        String encodedPdu = Strings.dumpBytes( bb.array() );
-
-        assertEquals( encodedPdu, decodedPdu );
+        assertArrayEquals( stream.array(), bb.array() );
 
         // Check encode reverse
         Asn1Buffer buffer = new Asn1Buffer();
@@ -118,7 +114,7 @@ public class AddResponseTest extends AbstractCodecServiceTest
 
         LdapEncoder.encodeMessageReverse( buffer, codec, response );
 
-        assertTrue( Arrays.equals( stream.array(), buffer.getBytes().array() ) );
+        assertArrayEquals( stream.array(), buffer.getBytes().array() );
     }
 
 
@@ -157,11 +153,11 @@ public class AddResponseTest extends AbstractCodecServiceTest
     {
         Asn1Decoder ldapDecoder = new Asn1Decoder();
 
-        ByteBuffer stream = ByteBuffer.allocate( 0x2B );
+        ByteBuffer stream = ByteBuffer.allocate( 0x32 );
 
         stream.put( new byte[]
             {
-                0x30, 0x29,                     // LDAPMessage ::=SEQUENCE {
+                0x30, 0x30,                     // LDAPMessage ::=SEQUENCE {
                   0x02, 0x01, 0x01,             // messageID MessageID
                   0x69, 0x07,                   // CHOICE { ..., addResponse AddResponse, ...
                                                 // AddResponse ::= [APPLICATION 9] LDAPResult
@@ -173,14 +169,17 @@ public class AddResponseTest extends AbstractCodecServiceTest
                     0x04, 0x00,                 // errorMessage LDAPString,
                                                 // referral [3] Referral OPTIONAL }
                                                 // }
-                    ( byte ) 0xA0, 0x1B,        // A control
-                      0x30, 0x19,
-                        0x04, 0x17,
-                          '2', '.', '1', '6', '.', '8', '4', '0', '.', '1', '.',
-                          '1', '1', '3', '7', '3', '0', '.', '3', '.', '4', '.', '2'
+                  ( byte ) 0xA0, 0x22,          // A control
+                    0x30, 0x20,
+                      0x04, 0x17,               // EntryChange response control
+                        '2', '.', '1', '6', '.', '8', '4', '0', '.', '1', '.', 
+                        '1', '1', '3', '7', '3', '0', '.', '3', '.', '4', '.', '7',
+                      0x04, 0x05,               // Control value
+                        0x30, 0x03,               // EntryChangeNotification ::= SEQUENCE {
+                          0x0A, 0x01, 0x01        //     changeType ENUMERATED {
+                                                //         add             (1),
             } );
 
-        String decodedPdu = Strings.dumpBytes( stream.array() );
         stream.flip();
 
         // Allocate a LdapMessage Container
@@ -203,19 +202,28 @@ public class AddResponseTest extends AbstractCodecServiceTest
         assertEquals( 1, controls.size() );
 
         @SuppressWarnings("unchecked")
-        CodecControl<Control> control = ( org.apache.directory.api.ldap.codec.api.CodecControl<Control> ) controls
-            .get( "2.16.840.1.113730.3.4.2" );
-        assertEquals( "2.16.840.1.113730.3.4.2", control.getOid() );
-        assertEquals( "", Strings.dumpBytes( control.getValue() ) );
+        CodecControl<Control> control = ( CodecControl<Control> ) controls
+            .get( "2.16.840.1.113730.3.4.7" );
+        assertEquals( "2.16.840.1.113730.3.4.7", control.getOid() );
+        assertEquals( "0x30 0x03 0x0A 0x01 0x01 ", Strings.dumpBytes( control.getValue() ) );
 
         /** The encoder instance */
         ByteBuffer bb = LdapEncoder.encodeMessage( codec, addResponse );
 
         // Check the length
-        assertEquals( 0x02B, bb.limit() );
+        assertEquals( 0x32, bb.limit() );
+
+        assertArrayEquals( stream.array(), bb.array() );
+
+        // Check encode reverse
+        Asn1Buffer buffer = new Asn1Buffer();
 
-        String encodedPdu = Strings.dumpBytes( bb.array() );
+        AddResponse response = new AddResponseImpl( addResponse.getMessageId() );
+        response.addControl( control );
+        response.getLdapResult().setResultCode( ResultCodeEnum.SUCCESS );
+
+        LdapEncoder.encodeMessageReverse( buffer, codec, response );
 
-        assertEquals( encodedPdu, decodedPdu );
+        assertArrayEquals( stream.array(), buffer.getBytes().array() );
     }
 }