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/01/27 05:49:08 UTC

[directory-ldap-api] 01/02: o Added tests for the EndTransactionRequest class o Added the encoding for that operation

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

commit ff794ccc60ce46ee293652c02f493d5dbb4c01ef
Author: Emmanuel Lécharny <el...@symas.com>
AuthorDate: Sat Jan 27 06:21:17 2018 +0100

    o Added tests for the EndTransactionRequest class
    o Added the encoding for that operation
---
 .../EndTransactionRequestDecorator.java            |  68 +++++++
 .../EndTransactionResponseDecorator.java           |   3 +
 .../endTransaction/EndTransactionRequestTest.java  | 223 +++++++++++++++++++++
 .../pwdModify/PasswordModifyResponseTest.java      |   1 -
 4 files changed, 294 insertions(+), 1 deletion(-)

diff --git a/ldap/extras/codec/src/main/java/org/apache/directory/api/ldap/extras/extended/ads_impl/endTransaction/EndTransactionRequestDecorator.java b/ldap/extras/codec/src/main/java/org/apache/directory/api/ldap/extras/extended/ads_impl/endTransaction/EndTransactionRequestDecorator.java
index 985ff84..68bc709 100644
--- a/ldap/extras/codec/src/main/java/org/apache/directory/api/ldap/extras/extended/ads_impl/endTransaction/EndTransactionRequestDecorator.java
+++ b/ldap/extras/codec/src/main/java/org/apache/directory/api/ldap/extras/extended/ads_impl/endTransaction/EndTransactionRequestDecorator.java
@@ -20,7 +20,13 @@
 package org.apache.directory.api.ldap.extras.extended.ads_impl.endTransaction;
 
 
+import java.nio.ByteBuffer;
+
 import org.apache.directory.api.asn1.DecoderException;
+import org.apache.directory.api.asn1.EncoderException;
+import org.apache.directory.api.asn1.ber.tlv.BerValue;
+import org.apache.directory.api.asn1.ber.tlv.TLV;
+import org.apache.directory.api.asn1.ber.tlv.UniversalTag;
 import org.apache.directory.api.i18n.I18n;
 import org.apache.directory.api.ldap.codec.api.ExtendedRequestDecorator;
 import org.apache.directory.api.ldap.codec.api.LdapApiService;
@@ -43,6 +49,9 @@ public class EndTransactionRequestDecorator extends ExtendedRequestDecorator<End
     /** The internal EndTransaction request */
     private EndTransactionRequest endTransactionRequest;
 
+    /** stores the length of the request*/
+    private int requestLength = 0;
+
 
     /**
      * Creates a new instance of EndTransactionRequestDecorator.
@@ -135,4 +144,63 @@ public class EndTransactionRequestDecorator extends ExtendedRequestDecorator<End
             throw new RuntimeException( e );
         }
     }
+
+
+    /**
+     * Compute the EndTransactionRequest extended operation length
+     * <pre>
+     * 0x30 L1 
+     *   | 
+     *   +-- 0x01 0x01 commit 
+     *   +-- 0x04 L2 identifier] 
+     * </pre>
+     */
+    /* No qualifier */int computeLengthInternal()
+    {
+        requestLength = 0;
+        
+        if ( !endTransactionRequest.getCommit() )
+        {
+            requestLength = 1 + 1 + 1; // Commit
+        }
+
+        if ( endTransactionRequest.getTransactionId() != null )
+        {
+            int len = endTransactionRequest.getTransactionId().length;
+            requestLength += 1 + TLV.getNbBytes( len ) + len;
+        }
+
+        return 1 + TLV.getNbBytes( requestLength ) + requestLength;
+    }
+
+
+    /**
+     * Encodes the EndTransactionRequest extended operation.
+     * 
+     * @return A ByteBuffer that contains the encoded PDU
+     * @throws org.apache.directory.api.asn1.EncoderException If anything goes wrong.
+     */
+    /* No qualifier */ByteBuffer encodeInternal() throws EncoderException
+    {
+        ByteBuffer bb = ByteBuffer.allocate( computeLengthInternal() );
+
+        bb.put( UniversalTag.SEQUENCE.getValue() );
+        bb.put( TLV.getBytes( requestLength ) );
+        
+        // The commit flag, if it's not true
+        if ( ! getCommit() )
+        {
+            BerValue.encode( bb, false );
+        }
+
+        // The identifier
+        byte[] identifier = endTransactionRequest.getTransactionId();
+
+        if ( identifier != null )
+        {
+            BerValue.encode( bb, identifier  );
+        }
+
+        return bb;
+    }
 }
diff --git a/ldap/extras/codec/src/main/java/org/apache/directory/api/ldap/extras/extended/ads_impl/endTransaction/EndTransactionResponseDecorator.java b/ldap/extras/codec/src/main/java/org/apache/directory/api/ldap/extras/extended/ads_impl/endTransaction/EndTransactionResponseDecorator.java
index 860c225..0d194c2 100644
--- a/ldap/extras/codec/src/main/java/org/apache/directory/api/ldap/extras/extended/ads_impl/endTransaction/EndTransactionResponseDecorator.java
+++ b/ldap/extras/codec/src/main/java/org/apache/directory/api/ldap/extras/extended/ads_impl/endTransaction/EndTransactionResponseDecorator.java
@@ -39,6 +39,9 @@ public class EndTransactionResponseDecorator extends ExtendedResponseDecorator<E
     /** The endTransaction response */
     private EndTransactionResponse endTransactionResponse;
 
+    /** stores the length of the request*/
+    private int requestLength = 0;
+
     /**
      * Creates a new instance of EndTransactionResponseDecorator.
      *
diff --git a/ldap/extras/codec/src/test/java/org/apache/directory/api/ldap/extras/extended/ads_impl/endTransaction/EndTransactionRequestTest.java b/ldap/extras/codec/src/test/java/org/apache/directory/api/ldap/extras/extended/ads_impl/endTransaction/EndTransactionRequestTest.java
new file mode 100644
index 0000000..7f03d4a
--- /dev/null
+++ b/ldap/extras/codec/src/test/java/org/apache/directory/api/ldap/extras/extended/ads_impl/endTransaction/EndTransactionRequestTest.java
@@ -0,0 +1,223 @@
+/*
+ *  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.extended.ads_impl.endTransaction;
+
+
+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 org.apache.directory.api.asn1.DecoderException;
+import org.apache.directory.api.asn1.EncoderException;
+import org.apache.directory.api.asn1.ber.Asn1Decoder;
+import org.apache.directory.api.ldap.extras.extended.endTransaction.EndTransactionRequest;
+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 EndTransactionRequest codec
+ * 
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+@RunWith(ConcurrentJunitRunner.class)
+@Concurrency()
+public class EndTransactionRequestTest
+{
+    /**
+     * Test the decoding of a EndTransactionRequest with nothing in it
+     */
+    @Test( expected=DecoderException.class)
+    public void testDecodeEndTransactionRequestEmpty() throws DecoderException
+    {
+        Asn1Decoder decoder = new Asn1Decoder();
+        ByteBuffer bb = ByteBuffer.allocate( 0x02 );
+        bb.put( new byte[]
+            { 0x30, 0x00, // EndTransactionRequest ::= SEQUENCE {
+            } );
+
+        EndTransactionRequestContainer container = new EndTransactionRequestContainer();
+
+        decoder.decode( bb, container );
+    }
+
+
+    /**
+     * Test the decoding of a EndTransactionRequest with an commit but no identifier
+     */
+    @Test( expected=DecoderException.class )
+    public void testEndTransactionRequestCommitNoIdentifier() throws DecoderException
+    {
+        Asn1Decoder decoder = new Asn1Decoder();
+        ByteBuffer bb = ByteBuffer.allocate( 0x05 );
+        bb.put( new byte[]
+            { 0x30, 0x03,              // EndTransactionRequest ::= SEQUENCE {
+                0x01, 0x01, 0x00       // Commit, TRUE
+        } );
+
+        EndTransactionRequestContainer container = new EndTransactionRequestContainer();
+
+        decoder.decode( bb, container );
+    }
+
+
+    /**
+     * Test the decoding of a EndTransactionRequest with an identifier but no commit
+     * @throws EncoderException 
+     */
+    @Test
+    public void testEndTransactionRequestNoCommitIdentifier() throws EncoderException
+    {
+        Asn1Decoder decoder = new Asn1Decoder();
+        ByteBuffer bb = ByteBuffer.allocate( 0x08 );
+        bb.put( new byte[]
+            { 0x30, 0x06,                       // EndTransactionRequest ::= SEQUENCE {
+                0x04, 0x04, 't', 'e', 's', 't'  // identifier (test)
+        } );
+
+        String decodedPdu = Strings.dumpBytes( bb.array() );
+        bb.flip();
+
+        EndTransactionRequestContainer container = new EndTransactionRequestContainer();
+
+        try
+        {
+            decoder.decode( bb, container );
+        }
+        catch ( DecoderException de )
+        {
+            de.printStackTrace();
+            fail( de.getMessage() );
+        }
+        
+        EndTransactionRequest endTransactionRequest = container.getEndTransactionRequest();
+        assertTrue( endTransactionRequest.getCommit() );
+        assertEquals( "test", Strings.utf8ToString( endTransactionRequest.getTransactionId() ) );
+
+        // Check the length
+        assertEquals( 0x08, ( ( EndTransactionRequestDecorator ) endTransactionRequest ).computeLengthInternal() );
+
+        // Check the encoding
+        ByteBuffer bb1 = ( ( EndTransactionRequestDecorator ) endTransactionRequest ).encodeInternal();
+
+        String encodedPdu = Strings.dumpBytes( bb1.array() );
+
+        assertEquals( encodedPdu, decodedPdu );
+    }
+
+
+    /**
+     * Test the decoding of a EndTransactionRequest with an identifier and a commit
+     * @throws EncoderException 
+     */
+    @Test
+    public void testEndTransactionRequesoCommitIdentifier() throws EncoderException
+    {
+        Asn1Decoder decoder = new Asn1Decoder();
+        ByteBuffer bb = ByteBuffer.allocate( 0x0B );
+        bb.put( new byte[]
+            { 0x30, 0x09,                       // EndTransactionRequest ::= SEQUENCE {
+                0x01, 0x01, 0x00,               // Commit, FALSE
+                0x04, 0x04, 't', 'e', 's', 't'  // identifier (test)
+        } );
+
+        String decodedPdu = Strings.dumpBytes( bb.array() );
+        bb.flip();
+
+        EndTransactionRequestContainer container = new EndTransactionRequestContainer();
+
+        try
+        {
+            decoder.decode( bb, container );
+        }
+        catch ( DecoderException de )
+        {
+            de.printStackTrace();
+            fail( de.getMessage() );
+        }
+        
+        EndTransactionRequest endTransactionRequest = container.getEndTransactionRequest();
+        assertFalse( endTransactionRequest.getCommit() );
+        assertEquals( "test", Strings.utf8ToString( endTransactionRequest.getTransactionId() ) );
+
+        // Check the length
+        assertEquals( 0x0B, ( ( EndTransactionRequestDecorator ) endTransactionRequest ).computeLengthInternal() );
+
+        // Check the encoding
+        ByteBuffer bb1 = ( ( EndTransactionRequestDecorator ) endTransactionRequest ).encodeInternal();
+
+        String encodedPdu = Strings.dumpBytes( bb1.array() );
+
+        assertEquals( encodedPdu, decodedPdu );
+    }
+
+
+    /**
+     * Test the decoding of a EndTransactionRequest with an empty identifier and a commit
+     * @throws EncoderException 
+     */
+    @Test
+    public void testEndTransactionRequesoCommitEmptyIdentifier() throws EncoderException
+    {
+        Asn1Decoder decoder = new Asn1Decoder();
+        ByteBuffer bb = ByteBuffer.allocate( 0x07 );
+        bb.put( new byte[]
+            { 0x30, 0x05,                       // EndTransactionRequest ::= SEQUENCE {
+                0x01, 0x01, 0x00,               // Commit, FALSE
+                0x04, 0x00                      // identifier (empty)
+        } );
+
+        String decodedPdu = Strings.dumpBytes( bb.array() );
+        bb.flip();
+
+        EndTransactionRequestContainer container = new EndTransactionRequestContainer();
+
+        try
+        {
+            decoder.decode( bb, container );
+        }
+        catch ( DecoderException de )
+        {
+            de.printStackTrace();
+            fail( de.getMessage() );
+        }
+        
+        EndTransactionRequest endTransactionRequest = container.getEndTransactionRequest();
+        assertFalse( endTransactionRequest.getCommit() );
+        assertEquals( 0, endTransactionRequest.getTransactionId().length );
+
+        // Check the length
+        assertEquals( 0x07, ( ( EndTransactionRequestDecorator ) endTransactionRequest ).computeLengthInternal() );
+
+        // Check the encoding
+        ByteBuffer bb1 = ( ( EndTransactionRequestDecorator ) endTransactionRequest ).encodeInternal();
+
+        String encodedPdu = Strings.dumpBytes( bb1.array() );
+
+        assertEquals( encodedPdu, decodedPdu );
+    }
+}
diff --git a/ldap/extras/codec/src/test/java/org/apache/directory/api/ldap/extras/extended/ads_impl/pwdModify/PasswordModifyResponseTest.java b/ldap/extras/codec/src/test/java/org/apache/directory/api/ldap/extras/extended/ads_impl/pwdModify/PasswordModifyResponseTest.java
index 3f8767e..baec9e3 100644
--- a/ldap/extras/codec/src/test/java/org/apache/directory/api/ldap/extras/extended/ads_impl/pwdModify/PasswordModifyResponseTest.java
+++ b/ldap/extras/codec/src/test/java/org/apache/directory/api/ldap/extras/extended/ads_impl/pwdModify/PasswordModifyResponseTest.java
@@ -28,7 +28,6 @@ import static org.junit.Assert.fail;
 import java.nio.ByteBuffer;
 
 import org.apache.directory.api.asn1.DecoderException;
-import org.apache.directory.api.asn1.EncoderException;
 import org.apache.directory.api.asn1.ber.Asn1Decoder;
 import org.apache.directory.api.ldap.extras.extended.ads_impl.pwdModify.PasswordModifyResponseContainer;
 import org.apache.directory.api.ldap.extras.extended.ads_impl.pwdModify.PasswordModifyResponseDecorator;

-- 
To stop receiving notification emails like this one, please contact
elecharny@apache.org.