directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kayyag...@apache.org
Subject svn commit: r1630177 - in /directory/shared/trunk/ldap/codec/core/src: main/java/org/apache/directory/api/ldap/codec/controls/sort/ test/java/org/apache/directory/api/ldap/codec/sort/
Date Wed, 08 Oct 2014 16:55:32 GMT
Author: kayyagari
Date: Wed Oct  8 16:55:31 2014
New Revision: 1630177

URL: http://svn.apache.org/r1630177
Log:
o fixed wrong tag values (DIRAPI-137)
o added a missing transition
o updated tests

Modified:
    directory/shared/trunk/ldap/codec/core/src/main/java/org/apache/directory/api/ldap/codec/controls/sort/SortRequestDecorator.java
    directory/shared/trunk/ldap/codec/core/src/main/java/org/apache/directory/api/ldap/codec/controls/sort/SortRequestGrammar.java
    directory/shared/trunk/ldap/codec/core/src/test/java/org/apache/directory/api/ldap/codec/sort/SortRequestControlTest.java

Modified: directory/shared/trunk/ldap/codec/core/src/main/java/org/apache/directory/api/ldap/codec/controls/sort/SortRequestDecorator.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/codec/core/src/main/java/org/apache/directory/api/ldap/codec/controls/sort/SortRequestDecorator.java?rev=1630177&r1=1630176&r2=1630177&view=diff
==============================================================================
--- directory/shared/trunk/ldap/codec/core/src/main/java/org/apache/directory/api/ldap/codec/controls/sort/SortRequestDecorator.java
(original)
+++ directory/shared/trunk/ldap/codec/core/src/main/java/org/apache/directory/api/ldap/codec/controls/sort/SortRequestDecorator.java
Wed Oct  8 16:55:31 2014
@@ -31,6 +31,7 @@ import org.apache.directory.api.asn1.ber
 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.asn1.util.Asn1StringUtils;
 import org.apache.directory.api.i18n.I18n;
 import org.apache.directory.api.ldap.codec.api.ControlDecorator;
 import org.apache.directory.api.ldap.codec.api.LdapApiService;
@@ -53,6 +54,9 @@ public class SortRequestDecorator extend
 
     private List<Integer> sortKeyLenList = new ArrayList<Integer>();
 
+    public static int ORDERING_RULE_TAG = 0x80;
+    
+    public static int REVERSE_ORDER_TAG = 0x81;
 
     /**
      * Creates a new instance of SortRequestDecorator.
@@ -100,8 +104,11 @@ public class SortRequestDecorator extend
                 skLen += 1 + TLV.getNbBytes( mrBytes.length ) + mrBytes.length;
             }
 
-            // reverse order flag
-            skLen += 1 + 1 + 1;
+            if ( sk.isReverseOrder() )
+            {
+                // reverse order flag
+                skLen += 1 + 1 + 1;
+            }
 
             sortKeyLenList.add( skLen );
 
@@ -138,12 +145,22 @@ public class SortRequestDecorator extend
 
             BerValue.encode( buffer, sk.getAttributeTypeDesc() );
 
-            if ( sk.getMatchingRuleId() != null )
+            String mrId = sk.getMatchingRuleId();
+            if ( mrId != null )
             {
-                BerValue.encode( buffer, sk.getMatchingRuleId() );
+                buffer.put( (byte)ORDERING_RULE_TAG );
+                byte[] value = Asn1StringUtils.getBytesUtf8( mrId );
+
+                buffer.put( TLV.getBytes( value.length ) );
+                buffer.put( value );
             }
 
-            BerValue.encode( buffer, sk.isReverseOrder() );
+            if ( sk.isReverseOrder() )
+            {
+                buffer.put( (byte)REVERSE_ORDER_TAG );
+                buffer.put( (byte)0x01 );
+                buffer.put( BerValue.TRUE_VALUE );
+            }
         }
 
         return buffer;

Modified: directory/shared/trunk/ldap/codec/core/src/main/java/org/apache/directory/api/ldap/codec/controls/sort/SortRequestGrammar.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/codec/core/src/main/java/org/apache/directory/api/ldap/codec/controls/sort/SortRequestGrammar.java?rev=1630177&r1=1630176&r2=1630177&view=diff
==============================================================================
--- directory/shared/trunk/ldap/codec/core/src/main/java/org/apache/directory/api/ldap/codec/controls/sort/SortRequestGrammar.java
(original)
+++ directory/shared/trunk/ldap/codec/core/src/main/java/org/apache/directory/api/ldap/codec/controls/sort/SortRequestGrammar.java
Wed Oct  8 16:55:31 2014
@@ -34,6 +34,8 @@ import org.apache.directory.api.util.Str
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import static org.apache.directory.api.ldap.codec.controls.sort.SortRequestDecorator.*;
+
 
 /**
  * Grammar used for decoding a SortRequestControl.
@@ -70,10 +72,11 @@ public class SortRequestGrammar extends 
                 {
                     LOG.debug( "AttributeTypeDesc = " + atDesc );
                 }
-                
+
                 SortKey sk = new SortKey( atDesc );
                 container.setCurrentKey( sk );
                 container.getControl().addSortKey( sk );
+                container.setGrammarEndAllowed( true );
             }
 
         };
@@ -93,9 +96,9 @@ public class SortRequestGrammar extends 
                     {
                         LOG.debug( "ReverseOrder = " + reverseOrder );
                     }
-                    
+
                     container.getCurrentKey().setReverseOrder( reverseOrder );
-                    
+
                     container.setGrammarEndAllowed( true );
                 }
                 catch ( BooleanDecoderException e )
@@ -107,7 +110,7 @@ public class SortRequestGrammar extends 
             }
 
         };
-        
+
         // Create the transitions table
         super.transitions = new GrammarTransition[SortRequestStates.END_STATE.ordinal()][256];
 
@@ -125,11 +128,11 @@ public class SortRequestGrammar extends 
             new GrammarTransition<SortRequestContainer>( SortRequestStates.SORT_KEY_SEQUENCE_STATE,
                 SortRequestStates.AT_DESC_STATE,
                 UniversalTag.OCTET_STRING.getValue(), addSortKey );
-        
-        super.transitions[SortRequestStates.AT_DESC_STATE.ordinal()][UniversalTag.OCTET_STRING.getValue()]
=
+
+        super.transitions[SortRequestStates.AT_DESC_STATE.ordinal()][ORDERING_RULE_TAG] =
             new GrammarTransition<SortRequestContainer>( SortRequestStates.AT_DESC_STATE,
                 SortRequestStates.ORDER_RULE_STATE,
-                UniversalTag.OCTET_STRING.getValue(), new GrammarAction<SortRequestContainer>()
+                ORDERING_RULE_TAG, new GrammarAction<SortRequestContainer>()
                 {
 
                     @Override
@@ -142,27 +145,32 @@ public class SortRequestGrammar extends 
                         {
                             LOG.debug( "MatchingRuleOid = " + matchingRuleOid );
                         }
-                        
+
                         container.getCurrentKey().setMatchingRuleId( matchingRuleOid );
                     }
 
                 } );
-        
-        super.transitions[SortRequestStates.ORDER_RULE_STATE.ordinal()][UniversalTag.BOOLEAN.getValue()]
=
+
+        super.transitions[SortRequestStates.ORDER_RULE_STATE.ordinal()][REVERSE_ORDER_TAG]
=
             new GrammarTransition<SortRequestContainer>( SortRequestStates.ORDER_RULE_STATE,
                 SortRequestStates.REVERSE_ORDER_STATE,
-                UniversalTag.BOOLEAN.getValue(), storeReverseOrder );
-        
-        super.transitions[SortRequestStates.AT_DESC_STATE.ordinal()][UniversalTag.BOOLEAN.getValue()]
=
+                REVERSE_ORDER_TAG, storeReverseOrder );
+
+        super.transitions[SortRequestStates.AT_DESC_STATE.ordinal()][REVERSE_ORDER_TAG] =
             new GrammarTransition<SortRequestContainer>( SortRequestStates.AT_DESC_STATE,
                 SortRequestStates.REVERSE_ORDER_STATE,
-                UniversalTag.BOOLEAN.getValue(), storeReverseOrder );
+                REVERSE_ORDER_TAG, storeReverseOrder );
 
         super.transitions[SortRequestStates.REVERSE_ORDER_STATE.ordinal()][UniversalTag.SEQUENCE.getValue()]
=
             new GrammarTransition<SortRequestContainer>( SortRequestStates.REVERSE_ORDER_STATE,
                 SortRequestStates.SORT_KEY_SEQUENCE_STATE,
                 UniversalTag.SEQUENCE.getValue(), null );
 
+        super.transitions[SortRequestStates.AT_DESC_STATE.ordinal()][UniversalTag.SEQUENCE.getValue()]
=
+            new GrammarTransition<SortRequestContainer>( SortRequestStates.AT_DESC_STATE,
+                SortRequestStates.SORT_KEY_SEQUENCE_STATE,
+                UniversalTag.SEQUENCE.getValue(), null );
+
     }
 
 

Modified: directory/shared/trunk/ldap/codec/core/src/test/java/org/apache/directory/api/ldap/codec/sort/SortRequestControlTest.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/codec/core/src/test/java/org/apache/directory/api/ldap/codec/sort/SortRequestControlTest.java?rev=1630177&r1=1630176&r2=1630177&view=diff
==============================================================================
--- directory/shared/trunk/ldap/codec/core/src/test/java/org/apache/directory/api/ldap/codec/sort/SortRequestControlTest.java
(original)
+++ directory/shared/trunk/ldap/codec/core/src/test/java/org/apache/directory/api/ldap/codec/sort/SortRequestControlTest.java
Wed Oct  8 16:55:31 2014
@@ -33,6 +33,7 @@ import org.apache.directory.api.ldap.cod
 import org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest;
 import org.apache.directory.api.ldap.model.message.controls.SortKey;
 import org.apache.directory.api.ldap.model.message.controls.SortRequest;
+import org.apache.directory.api.util.Strings;
 import org.junit.Test;
 
 /**
@@ -51,8 +52,8 @@ public class SortRequestControlTest exte
                0x30, 0x0E,
                 0x30, 0x0C,
                    0x04, 0x02, 'c', 'n',
-                   0x04, 0x03, 'o', 'i', 'd',
-                   0x01, 0x01, 0x00
+                   (byte)0x80, 0x03, 'o', 'i', 'd',
+                   (byte)0x81, 0x01, 0x00
             } );
         buffer.flip();
         
@@ -66,10 +67,13 @@ public class SortRequestControlTest exte
         assertEquals( "oid", sk.getMatchingRuleId() );
         assertFalse( sk.isReverseOrder() );
         
-        ByteBuffer encoded = ByteBuffer.allocate( buffer.capacity() );
+        // default value of false reverseOrder will not be encoded
+        int skipBytes = 3;
+        ByteBuffer encoded = ByteBuffer.allocate( buffer.capacity() - skipBytes );
         decorator.computeLength();
         decorator.encode( encoded );
-        assertTrue( Arrays.equals( buffer.array(), encoded.array() ) );
+        assertFalse( Arrays.equals( buffer.array(), encoded.array() ) );
+        assertEquals( buffer.array().length - skipBytes, encoded.array().length );
     }
     
     
@@ -83,13 +87,13 @@ public class SortRequestControlTest exte
 
                 0x30, 0x0C,
                    0x04, 0x02, 'c', 'n',
-                   0x04, 0x03, 'o', 'i', 'd',
-                   0x01, 0x01, 0x00,
+                   (byte)0x80, 0x03, 'o', 'i', 'd',
+                   (byte)0x81, 0x01, (byte)0xFF,
 
                 0x30, 0x0C,
                    0x04, 0x02, 's', 'n',
-                   0x04, 0x03, 'i', 'o', 'd',
-                   0x01, 0x01, (byte)0xFF
+                   (byte)0x80, 0x03, 'i', 'o', 'd',
+                   (byte)0x81, 0x01, (byte)0xFF
             } );
         buffer.flip();
         
@@ -97,11 +101,11 @@ public class SortRequestControlTest exte
         SortRequest control = ( SortRequest ) decorator.decode( buffer.array() );
         
         assertEquals( 2, control.getSortKeys().size() );
-        
+
         SortKey sk = control.getSortKeys().get( 0 );
         assertEquals( "cn", sk.getAttributeTypeDesc() );
         assertEquals( "oid", sk.getMatchingRuleId() );
-        assertFalse( sk.isReverseOrder() );
+        assertTrue( sk.isReverseOrder() );
         
         sk = control.getSortKeys().get( 1 );
         assertEquals( "sn", sk.getAttributeTypeDesc() );
@@ -123,7 +127,7 @@ public class SortRequestControlTest exte
             {
                0x30, 0x05,
                 0x30, 0x03,
-                   0x01, 0x01, 0x00
+                (byte)0x81, 0x01, 0x00
             } );
         buffer.flip();
         
@@ -141,7 +145,36 @@ public class SortRequestControlTest exte
                0x30, 0x09,
                 0x30, 0x07,
                    0x04, 0x02, 'c', 'n',
-                   0x01, 0x01, 0x00
+                   (byte)0x81, 0x01, (byte)0xFF
+            } );
+        buffer.flip();
+        
+        SortRequestDecorator decorator = new SortRequestDecorator( codec );
+        SortRequest control = ( SortRequest ) decorator.decode( buffer.array() );
+        
+        assertEquals( 1, control.getSortKeys().size() );
+        
+        SortKey sk = control.getSortKeys().get( 0 );
+        assertEquals( "cn", sk.getAttributeTypeDesc() );
+        assertNull( sk.getMatchingRuleId() );
+        assertTrue( sk.isReverseOrder() );
+        
+        ByteBuffer encoded = ByteBuffer.allocate( buffer.capacity() );
+        decorator.computeLength();
+        decorator.encode( encoded );
+        assertTrue( Arrays.equals( buffer.array(), encoded.array() ) );
+    }
+    
+    
+    @Test
+    public void testDecodeControlWithAtDescOnly() throws Exception
+    {
+        ByteBuffer buffer = ByteBuffer.allocate( 8 );
+        buffer.put( new byte[]
+            {
+               0x30, 0x06,
+                0x30, 0x04,
+                0x04, 0x02, 'c', 'n'
             } );
         buffer.flip();
         
@@ -160,4 +193,41 @@ public class SortRequestControlTest exte
         decorator.encode( encoded );
         assertTrue( Arrays.equals( buffer.array(), encoded.array() ) );
     }
+
+    
+    @Test
+    public void testDecodeControlWithMultipleAtDescOnly() throws Exception
+    {
+        ByteBuffer buffer = ByteBuffer.allocate( 0x0E );
+        buffer.put( new byte[]
+            {
+               0x30, 0x0C,
+                0x30, 0x04,
+                 0x04, 0x02, 'c', 'n',
+                0x30, 0x04,
+                 0x04, 0x02, 's', 'n'
+            } );
+        buffer.flip();
+        
+        SortRequestDecorator decorator = new SortRequestDecorator( codec );
+        SortRequest control = ( SortRequest ) decorator.decode( buffer.array() );
+        
+        assertEquals( 2, control.getSortKeys().size() );
+        
+        SortKey sk = control.getSortKeys().get( 0 );
+        assertEquals( "cn", sk.getAttributeTypeDesc() );
+        assertNull( sk.getMatchingRuleId() );
+        assertFalse( sk.isReverseOrder() );
+        
+        sk = control.getSortKeys().get( 1 );
+        assertEquals( "sn", sk.getAttributeTypeDesc() );
+        assertNull( sk.getMatchingRuleId() );
+        assertFalse( sk.isReverseOrder() );
+        
+        ByteBuffer encoded = ByteBuffer.allocate( buffer.capacity() );
+        decorator.computeLength();
+        decorator.encode( encoded );
+        assertTrue( Arrays.equals( buffer.array(), encoded.array() ) );
+    }
+
 }



Mime
View raw message