You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by vt...@apache.org on 2005/02/09 04:36:05 UTC

svn commit: r152984 - in incubator/directory/ldap/trunk/snacc-provider/src/test: ./ PDUGenerator.java bind.xml

Author: vtence
Date: Tue Feb  8 19:36:05 2005
New Revision: 152984

URL: http://svn.apache.org/viewcvs?view=rev&rev=152984
Log:
To generate pre-computed used in snickers ber provider test cases

Added:
    incubator/directory/ldap/trunk/snacc-provider/src/test/
    incubator/directory/ldap/trunk/snacc-provider/src/test/PDUGenerator.java   (with props)
    incubator/directory/ldap/trunk/snacc-provider/src/test/bind.xml   (with props)

Added: incubator/directory/ldap/trunk/snacc-provider/src/test/PDUGenerator.java
URL: http://svn.apache.org/viewcvs/incubator/directory/ldap/trunk/snacc-provider/src/test/PDUGenerator.java?view=auto&rev=152984
==============================================================================
--- incubator/directory/ldap/trunk/snacc-provider/src/test/PDUGenerator.java (added)
+++ incubator/directory/ldap/trunk/snacc-provider/src/test/PDUGenerator.java Tue Feb  8 19:36:05 2005
@@ -0,0 +1,141 @@
+/*
+ * Copyright (c) 2005 Your Corporation. All Rights Reserved.
+ */
+
+import com.thoughtworks.xstream.XStream;
+import org.apache.ldap.common.message.Message;
+import org.apache.ldap.common.message.MessageEncoder;
+import org.apache.ldap.common.message.UnbindRequest;
+import org.apache.ldap.common.message.UnbindRequestImpl;
+import org.apache.ldap.common.message.spi.Provider;
+
+import java.io.FileReader;
+import java.io.IOException;
+import java.io.Reader;
+import java.nio.ByteBuffer;
+import java.util.Properties;
+
+/**
+ * Helper class to generate pre-computed PDU using Snacc.
+ * PDUs are displayed on screen for pasting in your
+ * Snickers test cases.
+ * <p>
+ * Proceed by changing the message() method to return the message
+ * to encode. The main() method will display the corresponded encoded
+ * PDU byte sequence.
+ * <p>
+ * Another option is to specify a file containing an XStreamed message as
+ * an argument. See the bind.xml file for an example.
+ */
+public class PDUGenerator
+{
+    private final MessageEncoder m_encoder;
+
+    public PDUGenerator( Properties env )
+    {
+        m_encoder = new MessageEncoder( env );
+    }
+
+    public String generatePDU( Message msg )
+    {
+        return toString( encode( msg ) );
+    }
+
+    private byte[] encode( Message msg )
+    {
+        ByteBuffer buf = m_encoder.encodeBlocking( msg );
+        byte[] bites = new byte[ buf.remaining() ];
+        buf.get( bites );
+        /**
+         * For some reason snacc appends PDUs with an A0 00
+         * sequence. Get rid of it.
+         */
+        return removeTrailingSequence( bites );
+    }
+
+    private byte[] removeTrailingSequence( byte[] bites )
+    {
+        byte[] trimmed = new byte[ bites.length - 2 ];
+        System.arraycopy( bites, 0, trimmed, 0, trimmed.length );
+        adjustLengthValue( trimmed, -2 );
+        return trimmed;
+    }
+
+    private void adjustLengthValue( byte[] pdu, int adjustment )
+    {
+        pdu[1] = (byte) (pdu[1] + adjustment);
+    }
+
+    private String toString( byte[] bites )
+    {
+        if ( bites.length == 0 ) return "{}";
+
+        StringBuffer sb = new StringBuffer( "{");
+        for ( int i = 0; i < bites.length; i++ )
+        {
+            byte bite = bites[i];
+            sb.append( "0x" );
+            String s = Integer.toHexString( bite ).toUpperCase( );
+            if (s.length() < 2) sb.append( "0" );
+            sb.append( s ).append( ", ");
+        }
+        sb.setLength( sb.length() - 2 );
+        sb.append( "}");
+
+        return sb.toString();
+    }
+
+    public static Message hardCodedMessage()
+    {
+        UnbindRequest req = new UnbindRequestImpl( 44 ) ;
+
+        return req;
+    }
+
+    public static void main( String[] args )
+    {
+        final Message msg;
+        if (fileNameSpecified( args ))
+        {
+            String fileName = args[0];
+            System.out.println( "Reading message from file: " + fileName );
+            msg = readMessageFromFile( fileName );
+        }
+        else
+        {
+            msg = hardCodedMessage();
+        }
+
+        Properties env = new Properties();
+        env.setProperty( Provider.BERLIB_PROVIDER,
+                "org.apache.ldap.common.berlib.snacc.SnaccProvider" );
+        PDUGenerator generator = new PDUGenerator( env );
+
+        System.out.println( generator.generatePDU( msg ) );
+
+    }
+
+    private static boolean fileNameSpecified( String[] args )
+    {
+        return args.length != 0;
+    }
+
+    private static Message readMessageFromFile( String fileName )
+    {
+        XStream xstream = new XStream();
+        Message msg = null;
+        try
+        {
+            Reader reader = new FileReader( fileName );
+            msg = (Message) xstream.fromXML( reader );
+            reader.close();
+        }
+        catch ( IOException e )
+        {
+            e.printStackTrace();
+            System.exit(1);
+        }
+
+        return msg;
+    }
+}

Propchange: incubator/directory/ldap/trunk/snacc-provider/src/test/PDUGenerator.java
------------------------------------------------------------------------------
    svn:executable = *

Added: incubator/directory/ldap/trunk/snacc-provider/src/test/bind.xml
URL: http://svn.apache.org/viewcvs/incubator/directory/ldap/trunk/snacc-provider/src/test/bind.xml?view=auto&rev=152984
==============================================================================
--- incubator/directory/ldap/trunk/snacc-provider/src/test/bind.xml (added)
+++ incubator/directory/ldap/trunk/snacc-provider/src/test/bind.xml Tue Feb  8 19:36:05 2005
@@ -0,0 +1,17 @@
+<org.apache.ldap.common.message.BindRequestImpl>
+    <name>cn=admin,dc=example,dc=com</name>
+    <credentials>cGFzc3dk</credentials>
+    <isSimple>true</isSimple>
+    <isVersion3>true</isVersion3>
+    <m__hasResponse>true</m__hasResponse>
+    <controls/>
+    <id>27</id>
+    <type>
+        <iValue>1073741824</iValue>
+        <iName>BINDREQUEST</iName>
+    </type>
+    <parameters/>
+    <m__isUnlockable>true</m__isUnlockable>
+    <m__isLocked>false</m__isLocked>
+</org.apache.ldap.common.message.BindRequestImpl>
+

Propchange: incubator/directory/ldap/trunk/snacc-provider/src/test/bind.xml
------------------------------------------------------------------------------
    svn:executable = *