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/04 10:48:22 UTC

[directory-ldap-api] branch master updated: o Fixed the AdPolicyHints control, it can't be decoded o Added tests for this control

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 dca2e9f  o Fixed the AdPolicyHints control, it can't be decoded o Added tests for this control
dca2e9f is described below

commit dca2e9f3f3659572dd77e454247db9d5a98d66cc
Author: elecharny <el...@emmanuels-mbp>
AuthorDate: Tue Dec 4 11:48:20 2018 +0100

    o Fixed the AdPolicyHints control, it can't be decoded
    o Added tests for this control
---
 .../ldap/extras/controls/ad_impl/StoreFlags.java   |   1 +
 .../controls/ad/AdDirSyncRequestControlTest.java   |  20 ++-
 .../controls/ad/AdDirSyncResponseControlTest.java  |  35 ++++-
 .../controls/ad/AdPolicyHintsControlTest.java      | 162 +++++++++++++++++++++
 4 files changed, 205 insertions(+), 13 deletions(-)

diff --git a/ldap/extras/codec/src/main/java/org/apache/directory/api/ldap/extras/controls/ad_impl/StoreFlags.java b/ldap/extras/codec/src/main/java/org/apache/directory/api/ldap/extras/controls/ad_impl/StoreFlags.java
index 02ee3ba..5537b67 100644
--- a/ldap/extras/codec/src/main/java/org/apache/directory/api/ldap/extras/controls/ad_impl/StoreFlags.java
+++ b/ldap/extras/codec/src/main/java/org/apache/directory/api/ldap/extras/controls/ad_impl/StoreFlags.java
@@ -47,5 +47,6 @@ public class StoreFlags extends AbstractReadInteger<AdPolicyHintsContainer>
     protected void setIntegerValue( int value, AdPolicyHintsContainer adPolicyHintsContainer )
     {
         adPolicyHintsContainer.getDecorator().setFlags( value );
+        adPolicyHintsContainer.setGrammarEndAllowed( true );
     }
 }
\ No newline at end of file
diff --git a/ldap/extras/codec/src/test/java/org/apache/directory/api/ldap/extras/controls/ad/AdDirSyncRequestControlTest.java b/ldap/extras/codec/src/test/java/org/apache/directory/api/ldap/extras/controls/ad/AdDirSyncRequestControlTest.java
index efd6df3..9d4e728 100644
--- a/ldap/extras/codec/src/test/java/org/apache/directory/api/ldap/extras/controls/ad/AdDirSyncRequestControlTest.java
+++ b/ldap/extras/codec/src/test/java/org/apache/directory/api/ldap/extras/controls/ad/AdDirSyncRequestControlTest.java
@@ -55,10 +55,12 @@ public class AdDirSyncRequestControlTest extends AbstractCodecServiceTest
     {
         codec.registerRequestControl( new AdDirSyncRequestFactory( codec ) );
     }
+    
+    
     @Test
     public void testAdDirSyncRequestControl() throws DecoderException, EncoderException
     {
-        ByteBuffer bb = ByteBuffer.allocate( 0x0F );
+        ByteBuffer bb = ByteBuffer.allocate( 0x0E );
 
         bb.put( new byte[]
             {
@@ -82,9 +84,15 @@ public class AdDirSyncRequestControlTest extends AbstractCodecServiceTest
         // test encoding
         ByteBuffer buffer = ( ( AdDirSyncRequestDecorator ) adDirSync ).encode( ByteBuffer
             .allocate( ( ( AdDirSyncRequestDecorator ) adDirSync ).computeLength() ) );
-        String expected = "0x30 0x0C 0x02 0x01 0x01 0x02 0x01 0x00 0x04 0x04 0x78 0x6B 0x63 0x64 ";
-        String decoded = Strings.dumpBytes( buffer.array() );
-        assertEquals( expected, decoded );
+        assertArrayEquals( bb.array(), buffer.array() );
+
+        // Check the reverse encoding
+        Asn1Buffer asn1Buffer = new Asn1Buffer();
+
+        AdDirSyncRequestFactory factory = ( AdDirSyncRequestFactory ) codec.getRequestControlFactories().get( AdDirSyncRequest.OID );
+        factory.encodeValue( asn1Buffer, adDirSync );
+
+        assertArrayEquals( bb.array(),  asn1Buffer.getBytes().array() );
     }
 
 
@@ -114,9 +122,7 @@ public class AdDirSyncRequestControlTest extends AbstractCodecServiceTest
         // test encoding
         ByteBuffer buffer = ( ( AdDirSyncRequestDecorator ) adDirSync ).encode( ByteBuffer
             .allocate( ( ( AdDirSyncRequestDecorator ) adDirSync ).computeLength() ) );
-        String expected = "0x30 0x08 0x02 0x01 0x01 0x02 0x01 0x00 0x04 0x00 ";
-        String decoded = Strings.dumpBytes( buffer.array() );
-        assertEquals( expected, decoded );
+        assertArrayEquals( bb.array(), buffer.array() );
 
         // Check the reverse encoding
         Asn1Buffer asn1Buffer = new Asn1Buffer();
diff --git a/ldap/extras/codec/src/test/java/org/apache/directory/api/ldap/extras/controls/ad/AdDirSyncResponseControlTest.java b/ldap/extras/codec/src/test/java/org/apache/directory/api/ldap/extras/controls/ad/AdDirSyncResponseControlTest.java
index 6d8192f..d0760af 100644
--- a/ldap/extras/codec/src/test/java/org/apache/directory/api/ldap/extras/controls/ad/AdDirSyncResponseControlTest.java
+++ b/ldap/extras/codec/src/test/java/org/apache/directory/api/ldap/extras/controls/ad/AdDirSyncResponseControlTest.java
@@ -20,6 +20,7 @@
 package org.apache.directory.api.ldap.extras.controls.ad;
 
 
+import static org.junit.Assert.assertArrayEquals;
 import static org.junit.Assert.assertEquals;
 
 import java.nio.ByteBuffer;
@@ -27,9 +28,12 @@ import java.util.EnumSet;
 
 import org.apache.directory.api.asn1.DecoderException;
 import org.apache.directory.api.asn1.EncoderException;
+import org.apache.directory.api.asn1.util.Asn1Buffer;
 import org.apache.directory.api.ldap.extras.AbstractCodecServiceTest;
 import org.apache.directory.api.ldap.extras.controls.ad_impl.AdDirSyncResponseDecorator;
+import org.apache.directory.api.ldap.extras.controls.ad_impl.AdDirSyncResponseFactory;
 import org.apache.directory.api.util.Strings;
+import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 
@@ -47,6 +51,13 @@ import com.mycila.junit.concurrent.ConcurrentJunitRunner;
 @Concurrency()
 public class AdDirSyncResponseControlTest extends AbstractCodecServiceTest
 {
+    @Before
+    public void init()
+    {
+        codec.registerRequestControl( new AdDirSyncResponseFactory( codec ) );
+    }
+    
+    
     @Test
     public void testAdDirSyncControl() throws DecoderException, EncoderException
     {
@@ -77,9 +88,15 @@ public class AdDirSyncResponseControlTest extends AbstractCodecServiceTest
         // test encoding
         ByteBuffer buffer = ( ( AdDirSyncResponseDecorator ) adDirSync ).encode( ByteBuffer
             .allocate( ( ( AdDirSyncResponseDecorator ) adDirSync ).computeLength() ) );
-        String expected = "0x30 0x0D 0x02 0x02 0x08 0x01 0x02 0x01 0x00 0x04 0x04 0x78 0x6B 0x63 0x64 ";
-        String decoded = Strings.dumpBytes( buffer.array() );
-        assertEquals( expected, decoded );
+        assertArrayEquals( bb.array(), buffer.array() );
+
+        // Check the reverse encoding
+        Asn1Buffer asn1Buffer = new Asn1Buffer();
+
+        AdDirSyncResponseFactory factory = ( AdDirSyncResponseFactory ) codec.getRequestControlFactories().get( AdDirSyncResponse.OID );
+        factory.encodeValue( asn1Buffer, adDirSync );
+
+        assertArrayEquals( bb.array(),  asn1Buffer.getBytes().array() );
     }
 
 
@@ -109,9 +126,15 @@ public class AdDirSyncResponseControlTest extends AbstractCodecServiceTest
         // test encoding
         ByteBuffer buffer = ( ( AdDirSyncResponseDecorator ) adDirSync ).encode( ByteBuffer
             .allocate( ( ( AdDirSyncResponseDecorator ) adDirSync ).computeLength() ) );
-        String expected = "0x30 0x08 0x02 0x01 0x01 0x02 0x01 0x00 0x04 0x00 ";
-        String decoded = Strings.dumpBytes( buffer.array() );
-        assertEquals( expected, decoded );
+        assertArrayEquals( bb.array(), buffer.array() );
+
+        // Check the reverse encoding
+        Asn1Buffer asn1Buffer = new Asn1Buffer();
+
+        AdDirSyncResponseFactory factory = ( AdDirSyncResponseFactory ) codec.getRequestControlFactories().get( AdDirSyncResponse.OID );
+        factory.encodeValue( asn1Buffer, adDirSync );
+
+        assertArrayEquals( bb.array(),  asn1Buffer.getBytes().array() );
     }
 
 
diff --git a/ldap/extras/codec/src/test/java/org/apache/directory/api/ldap/extras/controls/ad/AdPolicyHintsControlTest.java b/ldap/extras/codec/src/test/java/org/apache/directory/api/ldap/extras/controls/ad/AdPolicyHintsControlTest.java
new file mode 100644
index 0000000..b4cb1b2
--- /dev/null
+++ b/ldap/extras/codec/src/test/java/org/apache/directory/api/ldap/extras/controls/ad/AdPolicyHintsControlTest.java
@@ -0,0 +1,162 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  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.
+ *
+ */
+package org.apache.directory.api.ldap.extras.controls.ad;
+
+
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+
+import java.nio.ByteBuffer;
+
+import org.apache.directory.api.asn1.DecoderException;
+import org.apache.directory.api.asn1.EncoderException;
+import org.apache.directory.api.asn1.util.Asn1Buffer;
+import org.apache.directory.api.ldap.extras.AbstractCodecServiceTest;
+import org.apache.directory.api.ldap.extras.controls.ad_impl.AdPolicyHintsDecorator;
+import org.apache.directory.api.ldap.extras.controls.ad_impl.AdPolicyHintsFactory;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import com.mycila.junit.concurrent.Concurrency;
+import com.mycila.junit.concurrent.ConcurrentJunitRunner;
+
+
+/**
+ *
+ * TestCase for AdPolicyHints Control.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+@RunWith(ConcurrentJunitRunner.class)
+@Concurrency()
+public class AdPolicyHintsControlTest extends AbstractCodecServiceTest
+{
+    @Before
+    public void init()
+    {
+        codec.registerRequestControl( new AdPolicyHintsFactory( codec ) );
+    }
+    
+    
+    @Test( expected=DecoderException.class )
+    public void testAdPolicyHiontsControlNoFlag() throws DecoderException
+    {
+        ByteBuffer bb = ByteBuffer.allocate( 0x02 );
+
+        bb.put( new byte[]
+            {
+                0x30, 0x00                  // PolicyHintsRequestValue ::= SEQUENCE {
+            } );
+
+        bb.flip();
+
+        AdPolicyHints decorator = new AdPolicyHintsDecorator( codec );
+
+        ( ( AdPolicyHintsDecorator ) decorator ).decode( bb.array() );
+    }
+
+
+    @Test( expected=DecoderException.class )
+    public void testAdPolicyHiontsControlEmptyFlag() throws DecoderException
+    {
+        ByteBuffer bb = ByteBuffer.allocate( 0x04 );
+
+        bb.put( new byte[]
+            {
+                0x30, 0x02,                  // PolicyHintsRequestValue ::= SEQUENCE {
+                  0x02, 0x00                 //      Flags    INTEGER
+            } );
+
+        bb.flip();
+
+        AdPolicyHints decorator = new AdPolicyHintsDecorator( codec );
+
+        ( ( AdPolicyHintsDecorator ) decorator ).decode( bb.array() );
+    }
+
+
+    @Test
+    public void testAdPolicyHintsControlLengthConstraintEnforced() throws DecoderException, EncoderException
+    {
+        ByteBuffer bb = ByteBuffer.allocate( 0x05 );
+
+        bb.put( new byte[]
+            {
+                0x30, 0x03,                  // PolicyHintsRequestValue ::= SEQUENCE {
+                  0x02, 0x01, 0x01           //      Flags    INTEGER
+            } );
+
+        bb.flip();
+
+        AdPolicyHints decorator = new AdPolicyHintsDecorator( codec );
+
+        AdPolicyHints adPolicyHints = ( AdPolicyHints ) ( ( AdPolicyHintsDecorator ) decorator ).decode( bb.array() );
+        
+        assertEquals( 1, adPolicyHints.getFlags() );
+
+        // test encoding
+        ByteBuffer buffer = ( ( AdPolicyHintsDecorator ) adPolicyHints ).encode( ByteBuffer
+            .allocate( ( ( AdPolicyHintsDecorator ) adPolicyHints ).computeLength() ) );
+        assertArrayEquals( bb.array(), buffer.array() );
+
+        // Check the reverse encoding
+        Asn1Buffer asn1Buffer = new Asn1Buffer();
+
+        AdPolicyHintsFactory factory = ( AdPolicyHintsFactory ) codec.getRequestControlFactories().get( AdPolicyHints.OID );
+        factory.encodeValue( asn1Buffer, adPolicyHints );
+
+        assertArrayEquals( bb.array(),  asn1Buffer.getBytes().array() );
+    }
+
+
+    @Test
+    public void testAdPolicyHintsControlLengthConstraintNotEnforced() throws DecoderException, EncoderException
+    {
+        ByteBuffer bb = ByteBuffer.allocate( 0x05 );
+
+        bb.put( new byte[]
+            {
+                0x30, 0x03,                  // PolicyHintsRequestValue ::= SEQUENCE {
+                  0x02, 0x01, 0x00           //      Flags    INTEGER
+            } );
+
+        bb.flip();
+
+        AdPolicyHints decorator = new AdPolicyHintsDecorator( codec );
+
+        AdPolicyHints adPolicyHints = ( AdPolicyHints ) ( ( AdPolicyHintsDecorator ) decorator ).decode( bb.array() );
+        
+        assertEquals( 0, adPolicyHints.getFlags() );
+
+        // test encoding
+        ByteBuffer buffer = ( ( AdPolicyHintsDecorator ) adPolicyHints ).encode( ByteBuffer
+            .allocate( ( ( AdPolicyHintsDecorator ) adPolicyHints ).computeLength() ) );
+        assertArrayEquals( bb.array(), buffer.array() );
+
+        // Check the reverse encoding
+        Asn1Buffer asn1Buffer = new Asn1Buffer();
+
+        AdPolicyHintsFactory factory = ( AdPolicyHintsFactory ) codec.getRequestControlFactories().get( AdPolicyHints.OID );
+        factory.encodeValue( asn1Buffer, adPolicyHints );
+
+        assertArrayEquals( bb.array(),  asn1Buffer.getBytes().array() );
+    }
+}