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/11/27 23:18:29 UTC

[directory-ldap-api] branch master updated: o Added the reverse encoded value for PagedSearch Control (and the associated tests)

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 8fb2b00  o Added the reverse encoded value for PagedSearch Control (and the associated tests)
8fb2b00 is described below

commit 8fb2b0037f4c86fe87c3c2106783a76410c2e52f
Author: Emmanuel Lecharny <el...@apache.org>
AuthorDate: Wed Nov 28 00:18:27 2018 +0100

    o Added the reverse encoded value for PagedSearch Control (and the
    associated tests)
---
 .../search/pagedSearch/PagedResultsFactory.java    |  19 +++-
 .../search/pagedSearch/PagedSearchControlTest.java | 117 +++++++++++----------
 2 files changed, 75 insertions(+), 61 deletions(-)

diff --git a/ldap/codec/core/src/main/java/org/apache/directory/api/ldap/codec/controls/search/pagedSearch/PagedResultsFactory.java b/ldap/codec/core/src/main/java/org/apache/directory/api/ldap/codec/controls/search/pagedSearch/PagedResultsFactory.java
index ddd0b6f..1d02fdc 100644
--- a/ldap/codec/core/src/main/java/org/apache/directory/api/ldap/codec/controls/search/pagedSearch/PagedResultsFactory.java
+++ b/ldap/codec/core/src/main/java/org/apache/directory/api/ldap/codec/controls/search/pagedSearch/PagedResultsFactory.java
@@ -20,7 +20,9 @@
 package org.apache.directory.api.ldap.codec.controls.search.pagedSearch;
 
 
+import org.apache.directory.api.asn1.ber.tlv.BerValue;
 import org.apache.directory.api.asn1.util.Asn1Buffer;
+import org.apache.directory.api.ldap.codec.api.AbstractControlFactory;
 import org.apache.directory.api.ldap.codec.api.CodecControl;
 import org.apache.directory.api.ldap.codec.api.ControlFactory;
 import org.apache.directory.api.ldap.codec.api.LdapApiService;
@@ -34,7 +36,7 @@ import org.apache.directory.api.ldap.model.message.controls.PagedResults;
  * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
  * @version $Rev$, $Date$
  */
-public class PagedResultsFactory implements ControlFactory<PagedResults>
+public class PagedResultsFactory extends AbstractControlFactory<PagedResults>
 {
     /** The LDAP codec service */
     private LdapApiService codec;
@@ -47,7 +49,7 @@ public class PagedResultsFactory implements ControlFactory<PagedResults>
      */
     public PagedResultsFactory( LdapApiService codec )
     {
-        this.codec = codec;
+        super( codec );
     }
 
 
@@ -81,10 +83,21 @@ public class PagedResultsFactory implements ControlFactory<PagedResults>
     }
 
 
+    /**
+     * {@inheritDoc}
+     */
     @Override
     public void encodeValue( Asn1Buffer buffer, Control control )
     {
-        // TODO Auto-generated method stub
+        int start = buffer.getPos();
+
+        // The cookie
+        BerValue.encodeOctetString( buffer, ( ( PagedResults ) control ).getCookie() );
+
+        // The size
+        BerValue.encodeInteger( buffer, ( ( PagedResults ) control ).getSize() );
 
+        // The sequence
+        BerValue.encodeSequence( buffer, start );
     }
 }
diff --git a/ldap/codec/core/src/test/java/org/apache/directory/api/ldap/codec/controls/search/pagedSearch/PagedSearchControlTest.java b/ldap/codec/core/src/test/java/org/apache/directory/api/ldap/codec/controls/search/pagedSearch/PagedSearchControlTest.java
index 425e6b3..0c0e023 100644
--- a/ldap/codec/core/src/test/java/org/apache/directory/api/ldap/codec/controls/search/pagedSearch/PagedSearchControlTest.java
+++ b/ldap/codec/core/src/test/java/org/apache/directory/api/ldap/codec/controls/search/pagedSearch/PagedSearchControlTest.java
@@ -6,20 +6,21 @@
  *  to you under the Apache License, Version 2.0 (the
  *  "License"); you may not use this file except in compliance
  *  with the License.  You may obtain a copy of the License at
- *  
+ *
  *    http://www.apache.org/licenses/LICENSE-2.0
- *  
+ *
  *  Unless required by applicable law or agreed to in writing,
  *  software distributed under the License is distributed on an
  *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  *  KIND, either express or implied.  See the License for the
  *  specific language governing permissions and limitations
- *  under the License. 
- *  
+ *  under the License.
+ *
  */
 package org.apache.directory.api.ldap.codec.controls.search.pagedSearch;
 
 
+import static org.junit.Assert.assertArrayEquals;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertTrue;
@@ -27,21 +28,21 @@ import static org.junit.Assert.assertTrue;
 import java.nio.ByteBuffer;
 import java.util.Arrays;
 
-import com.mycila.junit.concurrent.Concurrency;
-import com.mycila.junit.concurrent.ConcurrentJunitRunner;
-
 import org.apache.directory.api.asn1.DecoderException;
-import org.apache.directory.api.ldap.codec.controls.search.pagedSearch.PagedResultsDecorator;
+import org.apache.directory.api.asn1.util.Asn1Buffer;
 import org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest;
 import org.apache.directory.api.ldap.model.message.controls.PagedResults;
 import org.apache.directory.api.util.Strings;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 
+import com.mycila.junit.concurrent.Concurrency;
+import com.mycila.junit.concurrent.ConcurrentJunitRunner;
+
 
 /**
  * Test the PagedSearchControlTest codec
- * 
+ *
  * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
  */
 @RunWith(ConcurrentJunitRunner.class)
@@ -55,19 +56,15 @@ public class PagedSearchControlTest extends AbstractCodecServiceTest
     public void testEncodePagedSearchControl() throws Exception
     {
         ByteBuffer bb = ByteBuffer.allocate( 0x0B );
+
         bb.put( new byte[]
             {
-                0x30, 0x09, // realSearchControlValue ::= SEQUENCE {
-                0x02,
-                0x01,
-                0x20, // size INTEGER,
-                0x04,
-                0x04,
-                't',
-                'e',
-                's',
-                't' // cookie OCTET STRING,
-        } );
+                0x30, 0x09,             // realSearchControlValue ::= SEQUENCE {
+                  0x02, 0x01, 0x20,     // size INTEGER,
+                  0x04, 0x04,
+                    't', 'e', 's', 't'  // cookie OCTET STRING,
+            } );
+
         bb.flip();
 
         PagedResultsDecorator decorator = new PagedResultsDecorator( codec );
@@ -88,6 +85,14 @@ public class PagedSearchControlTest extends AbstractCodecServiceTest
         String decoded = Strings.dumpBytes( buffer.array() );
         String expected = Strings.dumpBytes( bb.array() );
         assertEquals( expected, decoded );
+
+        // Check the reverse encoding
+        Asn1Buffer asn1Buffer = new Asn1Buffer();
+
+        PagedResultsFactory factory = ( PagedResultsFactory ) codec.getControlFactories().get( PagedResults.OID );
+        factory.encodeValue( asn1Buffer, ctrl );
+
+        assertArrayEquals( bb.array(),  asn1Buffer.getBytes().array() );
     }
 
 
@@ -100,11 +105,9 @@ public class PagedSearchControlTest extends AbstractCodecServiceTest
         ByteBuffer bb = ByteBuffer.allocate( 0x05 );
         bb.put( new byte[]
             {
-                0x30, 0x03, // realSearchControlValue ::= SEQUENCE {
-                0x02,
-                0x01,
-                0x20 // size INTEGER,
-        } );
+                0x30, 0x03,             // realSearchControlValue ::= SEQUENCE {
+                  0x02, 0x01, 0x20      // size INTEGER,
+            } );
         bb.flip();
 
         PagedResultsDecorator decorator = new PagedResultsDecorator( codec );
@@ -122,13 +125,9 @@ public class PagedSearchControlTest extends AbstractCodecServiceTest
         ByteBuffer bb = ByteBuffer.allocate( 0x08 );
         bb.put( new byte[]
             {
-                0x30, 0x06, // realSearchControlValue ::= SEQUENCE {
-                0x04,
-                0x04,
-                't',
-                'e',
-                's',
-                't' // cookie OCTET STRING,
+                0x30, 0x06,             // realSearchControlValue ::= SEQUENCE {
+                  0x04, 0x04,
+                    't', 'e', 's', 't'  // cookie OCTET STRING,
         } );
         bb.flip();
 
@@ -147,7 +146,7 @@ public class PagedSearchControlTest extends AbstractCodecServiceTest
         ByteBuffer bb = ByteBuffer.allocate( 0x02 );
         bb.put( new byte[]
             {
-                0x30, 0x00, // realSearchControlValue ::= SEQUENCE 
+                0x30, 0x00, // realSearchControlValue ::= SEQUENCE
             } );
         bb.flip();
 
@@ -166,16 +165,10 @@ public class PagedSearchControlTest extends AbstractCodecServiceTest
         ByteBuffer bb = ByteBuffer.allocate( 0x0b );
         bb.put( new byte[]
             {
-                0x30, 0x09, // realSearchControlValue ::= SEQUENCE {
-                0x02,
-                0x01,
-                ( byte ) 0xFF, // size INTEGER,
-                0x04,
-                0x04,
-                't',
-                'e',
-                's',
-                't' // cookie OCTET STRING,
+                0x30, 0x09,                     // realSearchControlValue ::= SEQUENCE {
+                  0x02, 0x01, ( byte ) 0xFF,    // size INTEGER,
+                  0x04, 0x04,
+                    't', 'e', 's', 't'          // cookie OCTET STRING,
         } );
         bb.flip();
 
@@ -197,6 +190,14 @@ public class PagedSearchControlTest extends AbstractCodecServiceTest
         String decoded = Strings.dumpBytes( buffer.array() );
         String expected = Strings.dumpBytes( bb.array() );
         assertEquals( expected, decoded );
+
+        // Check the reverse encoding
+        Asn1Buffer asn1Buffer = new Asn1Buffer();
+
+        PagedResultsFactory factory = ( PagedResultsFactory ) codec.getControlFactories().get( PagedResults.OID );
+        factory.encodeValue( asn1Buffer, ctrl );
+
+        assertArrayEquals( bb.array(),  asn1Buffer.getBytes().array() );
     }
 
 
@@ -209,15 +210,10 @@ public class PagedSearchControlTest extends AbstractCodecServiceTest
         ByteBuffer bb = ByteBuffer.allocate( 0x0a );
         bb.put( new byte[]
             {
-                0x30, 0x08, // realSearchControlValue ::= SEQUENCE {
-                0x02,
-                0x00, // size INTEGER,
-                0x04,
-                0x04,
-                't',
-                'e',
-                's',
-                't' // cookie OCTET STRING,
+                0x30, 0x08,                 // realSearchControlValue ::= SEQUENCE {
+                  0x02, 0x00,               // size INTEGER,
+                  0x04, 0x04,
+                    't', 'e', 's', 't'      // cookie OCTET STRING,
         } );
         bb.flip();
 
@@ -236,12 +232,9 @@ public class PagedSearchControlTest extends AbstractCodecServiceTest
         ByteBuffer bb = ByteBuffer.allocate( 0x07 );
         bb.put( new byte[]
             {
-                0x30, 0x05, // realSearchControlValue ::= SEQUENCE {
-                0x02,
-                0x01,
-                0x20, // size INTEGER,
-                0x04,
-                0x00 // cookie OCTET STRING,
+                0x30, 0x05,                 // realSearchControlValue ::= SEQUENCE {
+                  0x02, 0x01, 0x20,         // size INTEGER,
+                  0x04, 0x00                // cookie OCTET STRING,
         } );
         bb.flip();
 
@@ -260,5 +253,13 @@ public class PagedSearchControlTest extends AbstractCodecServiceTest
         String decoded = Strings.dumpBytes( buffer.array() );
         String expected = Strings.dumpBytes( bb.array() );
         assertEquals( expected, decoded );
+
+        // Check the reverse encoding
+        Asn1Buffer asn1Buffer = new Asn1Buffer();
+
+        PagedResultsFactory factory = ( PagedResultsFactory ) codec.getControlFactories().get( PagedResults.OID );
+        factory.encodeValue( asn1Buffer, ctrl );
+
+        assertArrayEquals( bb.array(),  asn1Buffer.getBytes().array() );
     }
 }
\ No newline at end of file