[jboss-cvs] JBossRemoting/src/tests/org/jboss/test/remoting/marshall/encrypt ...

Anil Saldhana anil.saldhana at jboss.com
Wed Aug 16 15:18:31 EDT 2006


  User: asaldhana
  Date: 06/08/16 15:18:31

  Added:       src/tests/org/jboss/test/remoting/marshall/encrypt    
                        EncryptingMarshallerTestCase.java
                        EncryptingMarshallerTestClient.java
                        EncryptingMarshallerTestServer.java
                        EncryptionStandaloneTestCase.java
  Log:
  JBAS-419: code,tests and keys for encrypting the streams
  
  Revision  Changes    Path
  1.1      date: 2006/08/16 19:18:30;  author: asaldhana;  state: Exp;JBossRemoting/src/tests/org/jboss/test/remoting/marshall/encrypt/EncryptingMarshallerTestCase.java
  
  Index: EncryptingMarshallerTestCase.java
  ===================================================================
  /*
  * JBoss, Home of Professional Open Source
  * Copyright 2005, JBoss Inc., and individual contributors as indicated
  * by the @authors tag. See the copyright.txt in the distribution for a
  * full listing of individual contributors.
  *
  * This is free software; you can redistribute it and/or modify it
  * under the terms of the GNU Lesser General Public License as
  * published by the Free Software Foundation; either version 2.1 of
  * the License, or (at your option) any later version.
  *
  * This software is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this software; if not, write to the Free
  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  */
  
  package org.jboss.test.remoting.marshall.encrypt;
  
  import org.apache.log4j.Level;
  import org.jboss.jrunit.harness.TestDriver;
  
  
  /**
   * A EncryptingMarshallerTestCase.
   *
   * @author Anil.Saldhana at jboss.org
   * @version $Revision: 1.1 $ 
   */
  
  public class EncryptingMarshallerTestCase extends TestDriver
  {
     public void declareTestClasses()
     {
        addTestClasses(EncryptingMarshallerTestClient.class.getName(),
                       1,
                       EncryptingMarshallerTestServer.class.getName());
  
     }
   
     protected Level getTestLogLevel()
     {
        return Level.ERROR;
     }
   
     /*protected String getClientJVMArguments()
      { 
         return "-Xdebug -Xrunjdwp:transport=dt_socket,address=8787,server=y,suspend=y";
      } */
      
      /*protected String getServerJVMArguments()
      { 
        return "-Xdebug -Xrunjdwp:transport=dt_socket,address=8686,server=y,suspend=y";
      } */ 
  }
  
  
  
  
  1.1      date: 2006/08/16 19:18:30;  author: asaldhana;  state: Exp;JBossRemoting/src/tests/org/jboss/test/remoting/marshall/encrypt/EncryptingMarshallerTestClient.java
  
  Index: EncryptingMarshallerTestClient.java
  ===================================================================
  /*
  * JBoss, Home of Professional Open Source
  * Copyright 2005, JBoss Inc., and individual contributors as indicated
  * by the @authors tag. See the copyright.txt in the distribution for a
  * full listing of individual contributors.
  *
  * This is free software; you can redistribute it and/or modify it
  * under the terms of the GNU Lesser General Public License as
  * published by the Free Software Foundation; either version 2.1 of
  * the License, or (at your option) any later version.
  *
  * This software is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this software; if not, write to the Free
  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  */
  
  package org.jboss.test.remoting.marshall.encrypt;
  
  import junit.framework.TestCase;
  import org.jboss.remoting.Client;
  import org.jboss.remoting.InvokerLocator; 
  import org.jboss.remoting.marshal.MarshalFactory;
  import org.jboss.remoting.marshal.Marshaller;
  import org.jboss.remoting.marshal.UnMarshaller; 
  import org.jboss.remoting.marshal.encryption.EncryptingMarshaller;
  import org.jboss.remoting.marshal.encryption.EncryptingUnMarshaller;  
  
  import java.io.ByteArrayInputStream;
  import java.io.ByteArrayOutputStream;
  import java.io.IOException; 
  
  /**
   * Test Client for the marshaller that can do encryption
   * @author Anil.Saldhana at jboss.org
   */
  public class EncryptingMarshallerTestClient extends TestCase
  {
     private Marshaller marshaller;
     private UnMarshaller unmarshaller;
  
     protected void setUp() throws Exception
     {
        super.setUp();
     }
  
  
     protected void tearDown() throws Exception
     {
        super.tearDown();
        marshaller = null;
        unmarshaller = null;
     } 
     
     /*public void testJavaInvocations()
     {
        try
        {
           Thread.sleep(5000); // Wait for server to start.
           MarshalFactory.addMarshaller("encrypt", 
                 new EncryptingMarshaller(), 
                 new EncryptingUnMarshaller());
           String locatorURI = "socket://localhost:5300/?datatype=encrypt";
           InvokerLocator locator = new InvokerLocator(locatorURI);
           Client remotingClient = new Client(locator);
           remotingClient.connect();
           String request1 = "First request";
           Object response1 = remotingClient.invoke(request1);
           assertTrue(request1.equals(response1));
           Object request2 = new Integer(17);
           Object response2 = remotingClient.invoke(request2);
           assertTrue(request2.equals(response2));
        }
        catch(Throwable e)
        {
           e.printStackTrace();
           fail();
        }
     }*/
     
     public void testJBossInvocations()
     {
        try
        {
           Thread.sleep(5000); // Wait for server to start.
           MarshalFactory.addMarshaller("encrypt", 
                 new EncryptingMarshaller(), 
                 new EncryptingUnMarshaller());
           String locatorURI = "socket://localhost:5400/?datatype=encrypt&"+
              InvokerLocator.SERIALIZATIONTYPE+"=jboss";
           InvokerLocator locator = new InvokerLocator(locatorURI); 
           Client remotingClient = new Client(locator);
           remotingClient.connect();
           String request1 = "First request";
           Object response1 = remotingClient.invoke(request1);
           assertTrue(request1.equals(response1));
           Object request2 = new Integer(17);
           Object response2 = remotingClient.invoke(request2);
           assertTrue(request2.equals(response2));
        }
        catch(Throwable e)
        {
           e.printStackTrace();
           fail();
        }
     }  
  
  
     protected void runOneTest() throws IOException, ClassNotFoundException
     {
        String testData = "This is some test data";
        Object param = new String(testData);
  
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        marshaller.write(param, output);
        byte[] byteArray = new byte[output.size()];
        byteArray = output.toByteArray();
        ByteArrayInputStream input = new ByteArrayInputStream(byteArray);
        Object result = unmarshaller.read(input, null);
  
        System.out.println("Result: " + result);
        assertEquals(testData, result);
     }
  }
  
  
  
  1.1      date: 2006/08/16 19:18:30;  author: asaldhana;  state: Exp;JBossRemoting/src/tests/org/jboss/test/remoting/marshall/encrypt/EncryptingMarshallerTestServer.java
  
  Index: EncryptingMarshallerTestServer.java
  ===================================================================
  /*
  * JBoss, Home of Professional Open Source
  * Copyright 2005, JBoss Inc., and individual contributors as indicated
  * by the @authors tag. See the copyright.txt in the distribution for a
  * full listing of individual contributors.
  *
  * This is free software; you can redistribute it and/or modify it
  * under the terms of the GNU Lesser General Public License as
  * published by the Free Software Foundation; either version 2.1 of
  * the License, or (at your option) any later version.
  *
  * This software is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this software; if not, write to the Free
  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  */
  
  package org.jboss.test.remoting.marshall.encrypt; 
   
  import javax.management.MBeanServer;
  import org.jboss.jrunit.extensions.ServerTestCase;
  import org.jboss.remoting.InvocationRequest;
  import org.jboss.remoting.InvokerLocator;
  import org.jboss.remoting.ServerInvocationHandler;
  import org.jboss.remoting.ServerInvoker;
  import org.jboss.remoting.callback.InvokerCallbackHandler;
  import org.jboss.remoting.marshal.MarshalFactory;
  import org.jboss.remoting.marshal.encryption.EncryptingMarshaller;
  import org.jboss.remoting.marshal.encryption.EncryptingUnMarshaller;
  import org.jboss.remoting.transport.Connector;
  
  
  /**
   * A EncryptingMarshallerTestServer.
   *
   * @author Anil.Saldhana at jboss.org
   * @version $Revision: 1.1 $ 
   */
  
  public class EncryptingMarshallerTestServer extends ServerTestCase
  {
     private static final Object lock = new Object(); 
  
     public void setUp()
     {
        String javauri = "socket://localhost:5300/?datatype=encrypt";
        String jbosslocatorURI = "socket://localhost:5400/?datatype=encrypt"
                                        + "&"+ InvokerLocator.SERIALIZATIONTYPE+"=jboss";
        EncryptionServerThread jbossThread = 
           new EncryptionServerThread(jbosslocatorURI,2);
        EncryptionServerThread javaThread = 
           new EncryptionServerThread(javauri,2);
        
        //Start the two threads - one for JBoss Invocation and one for Java invocation
        jbossThread.start();
        javaThread.start(); 
     } 
  
      
  
     public static void main(String[] args)
     {
        try
        {
           new EncryptingMarshallerTestServer().setUp();
        }
        catch (Exception e)
        { 
           e.printStackTrace();
        }
     }
     
     public class EncryptionServerThread extends Thread
     {
        private String locatorURI = null;
        private int counter = 0;
        public EncryptionServerThread(String url, int counter)
        {
           this.counter = counter;
           this.locatorURI = url;  
        }
        
        public void run()
        {
           try
           {
              
              MarshalFactory.addMarshaller("encrypt", 
                                           new EncryptingMarshaller(), 
                                           new EncryptingUnMarshaller()); 
              InvokerLocator locator = new InvokerLocator(locatorURI);
              System.out.println("Starting remoting server with locator uri of: " + locatorURI);
              Connector connector = new Connector();
              connector.setInvokerLocator(locator.getLocatorURI());
              connector.create();
              JavaInvocationHandler invocationHandler = new JavaInvocationHandler(counter);
              connector.addInvocationHandler("sample", invocationHandler);
              connector.start();
  
              synchronized(lock)
              {
                 lock.wait();
              }
  
              connector.stop();
              connector.destroy();
           }
           catch(Exception e)
           {
              e.printStackTrace();
           }
        } 
     }
     
     
     /**
      * Noop Invocation Handler
      * @author <a href="anil.saldhana at jboss.com">Anil Saldhana</a>
      * @version $Revision: 1.1 $
      */
     public abstract class AdapterInvocationHandler implements ServerInvocationHandler
     { 
        public void addListener(InvokerCallbackHandler callbackHandler)
        { 
        }
  
        public abstract Object invoke(InvocationRequest invocation) throws Throwable;
  
        public void removeListener(InvokerCallbackHandler callbackHandler)
        { 
        }
  
        public void setInvoker(ServerInvoker invoker)
        {  
        }
  
        public void setMBeanServer(MBeanServer server)
        { 
        } 
     }
  
  
     /**
      * Simple invocation handler implementation.
      * This is the code that will be called with the invocation payload from the client.
      */
     public class JavaInvocationHandler extends AdapterInvocationHandler
     { 
        private int counter = 0;
        public JavaInvocationHandler(int counter)
        {
           this.counter = counter; 
        }
  
        /**
         * called to handle a specific invocation
         *
         * @param invocation
         * @return
         * @throws Throwable
         */
        public Object invoke(InvocationRequest invocation) throws Throwable
        {
           System.out.println("INVOCATION HAS REACHED:"+invocation+"::counter="+counter);
           if(++counter == 2)
           {
              System.out.println("COUNTER=2");
              synchronized(lock)
              {
                 lock.notify();
              }
           }
           Object obj = invocation.getParameter();;
           System.out.println("RETURNING "+obj);
           return obj;
        } 
     }
  }
  
  
  
  
  1.1      date: 2006/08/16 19:18:30;  author: asaldhana;  state: Exp;JBossRemoting/src/tests/org/jboss/test/remoting/marshall/encrypt/EncryptionStandaloneTestCase.java
  
  Index: EncryptionStandaloneTestCase.java
  ===================================================================
  /*
    * JBoss, Home of Professional Open Source
    * Copyright 2005, JBoss Inc., and individual contributors as indicated
    * by the @authors tag. See the copyright.txt in the distribution for a
    * full listing of individual contributors.
    *
    * This is free software; you can redistribute it and/or modify it
    * under the terms of the GNU Lesser General Public License as
    * published by the Free Software Foundation; either version 2.1 of
    * the License, or (at your option) any later version.
    *
    * This software is distributed in the hope that it will be useful,
    * but WITHOUT ANY WARRANTY; without even the implied warranty of
    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    * Lesser General Public License for more details.
    *
    * You should have received a copy of the GNU Lesser General Public
    * License along with this software; if not, write to the Free
    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
    */
  package org.jboss.test.remoting.marshall.encrypt;
  
  import java.io.ByteArrayInputStream;
  import java.io.ByteArrayOutputStream;
  import java.io.IOException;
  
  import org.jboss.remoting.marshal.MarshalFactory;
  import org.jboss.remoting.marshal.Marshaller;
  import org.jboss.remoting.marshal.UnMarshaller;
  import org.jboss.remoting.marshal.encryption.EncryptingMarshaller;
  import org.jboss.remoting.marshal.encryption.EncryptingUnMarshaller;
  import org.jboss.remoting.marshal.http.HTTPMarshaller;
  import org.jboss.remoting.marshal.http.HTTPUnMarshaller;
  import org.jboss.remoting.marshal.serializable.SerializableMarshaller;
  import org.jboss.remoting.marshal.serializable.SerializableUnMarshaller;
  
  import junit.framework.TestCase;
  
  //$Id: EncryptionStandaloneTestCase.java,v 1.1 2006/08/16 19:18:30 asaldhana Exp $
  
  /**
   *  Tests Remoting Encryption facilities
   *  @author <a href="mailto:Anil.Saldhana at jboss.org">Anil Saldhana</a>
   *  @since  Aug 16, 2006 
   *  @version $Revision: 1.1 $
   */
  public class EncryptionStandaloneTestCase extends TestCase
  {
     private Marshaller marshaller;
     private UnMarshaller unmarshaller;
     
     public void testSerializable() throws IOException, ClassNotFoundException
     {  
        //Test default DES
        runAlgoTest(null);
        //TEST AES 
        runAlgoTest("AES"); 
        //TEST Blowfish
        runAlgoTest("Blowfish");  
        //Test TripleDES
        runAlgoTest("DESede"); 
     }
  
  
     public void testWrappedSerializable() throws IOException, ClassNotFoundException
     { 
        //Test default DES
        runWrappedSerializableTest(null);
        //TEST AES 
        runWrappedSerializableTest("AES");  
        
        //TEST Blowfish
        runWrappedSerializableTest("Blowfish"); 
        
        //Test TripleDES
        runWrappedSerializableTest("DESede");
     }
  
  
     public void testHTTP() throws IOException, ClassNotFoundException
     { 
        //Test default DES
        runHttpTest(null);
        //TEST AES 
        runHttpTest("AES");   
        //TEST Blowfish
        runHttpTest("Blowfish");  
        //Test TripleDES
        runHttpTest("DESede");
     }
     
  
     protected void runOneTest() throws IOException, ClassNotFoundException
     {
        String testData = "This is some test data";
        Object param = new String(testData);
  
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        marshaller.write(param, output);
        byte[] byteArray = new byte[output.size()];
        byteArray = output.toByteArray();
        ByteArrayInputStream input = new ByteArrayInputStream(byteArray);
        Object result = unmarshaller.read(input, null);
  
        System.out.println("Result: " + result);
        assertEquals(testData, result);
     }
     
     private void runAlgoTest(String algo) 
     throws IOException, ClassNotFoundException
     {
        EncryptingMarshaller em = new EncryptingMarshaller();
        EncryptingUnMarshaller um = new EncryptingUnMarshaller();
        if(algo != null)
        {
           em.setCipherAlgorithm(algo);
           um.setCipherAlgorithm(algo);
        }
        MarshalFactory.addMarshaller(EncryptingMarshaller.DATATYPE,em, um); 
  
        marshaller = MarshalFactory.getMarshaller(EncryptingMarshaller.DATATYPE);
        unmarshaller = MarshalFactory.getUnMarshaller(EncryptingMarshaller.DATATYPE);
        runOneTest(); 
     }
     
     private void runHttpTest(String algo) 
     throws IOException, ClassNotFoundException
     {
        String datatype = "encryptedHTTP"; 
        Marshaller m = MarshalFactory.getMarshaller(HTTPMarshaller.DATATYPE);
        UnMarshaller u = MarshalFactory.getUnMarshaller(HTTPUnMarshaller.DATATYPE);
        EncryptingMarshaller em =  new EncryptingMarshaller(m);
        EncryptingUnMarshaller um =  new EncryptingUnMarshaller(u);
        if(algo != null)
        {
           em.setCipherAlgorithm(algo);
           um.setCipherAlgorithm(algo);
        }
        MarshalFactory.addMarshaller(datatype,em, um); 
        marshaller = MarshalFactory.getMarshaller(datatype);
        unmarshaller = MarshalFactory.getUnMarshaller(datatype);
        runOneTest();
     } 
     
     private void runWrappedSerializableTest(String algo) 
     throws IOException, ClassNotFoundException
     { 
        String datatype = "encryptedSerializable";
        String sd = SerializableMarshaller.DATATYPE;
        String sud = SerializableUnMarshaller.DATATYPE;
        EncryptingMarshaller em = 
           new EncryptingMarshaller(MarshalFactory.getMarshaller(sd));
        EncryptingUnMarshaller um = 
           new EncryptingUnMarshaller(MarshalFactory.getUnMarshaller(sud));
        if(algo != null)
        {
           em.setCipherAlgorithm(algo);
           um.setCipherAlgorithm(algo);
        }
        MarshalFactory.addMarshaller(datatype,em, um); 
        
        marshaller = MarshalFactory.getMarshaller(datatype);
        unmarshaller = MarshalFactory.getUnMarshaller(datatype);
     } 
  }
  
  
  



More information about the jboss-cvs-commits mailing list