Author: ron.sigal(a)jboss.com
Date: 2009-04-23 02:27:42 -0400 (Thu, 23 Apr 2009)
New Revision: 5090
Added:
remoting2/branches/2.x/src/tests/org/jboss/test/remoting/transport/socket/timeout/SSLWriteTimeoutTestParent.java
Log:
JBREM-1120: New unit tests.
Added:
remoting2/branches/2.x/src/tests/org/jboss/test/remoting/transport/socket/timeout/SSLWriteTimeoutTestParent.java
===================================================================
---
remoting2/branches/2.x/src/tests/org/jboss/test/remoting/transport/socket/timeout/SSLWriteTimeoutTestParent.java
(rev 0)
+++
remoting2/branches/2.x/src/tests/org/jboss/test/remoting/transport/socket/timeout/SSLWriteTimeoutTestParent.java 2009-04-23
06:27:42 UTC (rev 5090)
@@ -0,0 +1,480 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2009, 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.transport.socket.timeout;
+
+import java.io.EOFException;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.lang.reflect.Constructor;
+import java.net.InetAddress;
+import java.net.ServerSocket;
+import java.net.Socket;
+import java.net.UnknownHostException;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Set;
+
+import javax.management.MBeanServer;
+import javax.net.ServerSocketFactory;
+import javax.net.SocketFactory;
+import javax.net.ssl.HandshakeCompletedListener;
+import javax.net.ssl.SSLServerSocket;
+import javax.net.ssl.SSLServerSocketFactory;
+import javax.net.ssl.SSLSession;
+import javax.net.ssl.SSLSocket;
+
+import junit.framework.TestCase;
+
+import org.apache.log4j.ConsoleAppender;
+import org.apache.log4j.Level;
+import org.apache.log4j.Logger;
+import org.apache.log4j.PatternLayout;
+import org.jboss.remoting.Client;
+import org.jboss.remoting.InvocationFailureException;
+import org.jboss.remoting.InvocationRequest;
+import org.jboss.remoting.InvokerLocator;
+import org.jboss.remoting.Remoting;
+import org.jboss.remoting.ServerInvocationHandler;
+import org.jboss.remoting.ServerInvoker;
+import org.jboss.remoting.callback.Callback;
+import org.jboss.remoting.callback.HandleCallbackException;
+import org.jboss.remoting.callback.InvokerCallbackHandler;
+import org.jboss.remoting.security.SSLSocketBuilder;
+import org.jboss.remoting.transport.Connector;
+import org.jboss.remoting.transport.PortUtil;
+import org.jboss.remoting.transport.bisocket.Bisocket;
+import org.jboss.remoting.transport.socket.SocketServerInvoker;
+import org.jboss.remoting.transport.socket.SocketWrapper;
+import
org.jboss.test.remoting.transport.socket.timeout.WriteTimeoutTestParent.TestServerSocketFactory;
+import
org.jboss.test.remoting.transport.socket.timeout.WriteTimeoutTestParent.TestSocketFactory;
+
+
+/**
+ * Unit tests for JBREM-1120.
+ *
+ * @author <a href="ron.sigal(a)jboss.com">Ron Sigal</a>
+ * @version $Rev$
+ * <p>
+ * Copyright Apr 22, 2009
+ * </p>
+ */
+public abstract class SSLWriteTimeoutTestParent extends WriteTimeoutTestParent
+{
+ private static Logger log = Logger.getLogger(SSLWriteTimeoutTestParent.class);
+
+ private static boolean firstTime = true;
+
+ protected static int SECONDARY_SERVER_SOCKET_PORT = 8765;
+ protected static String SECONDARY_SERVER_SOCKET_PORT_STRING = "8765";
+ protected static boolean callbackTest;
+
+ protected String host;
+ protected int port;
+ protected String locatorURI;
+ protected InvokerLocator serverLocator;
+ protected Connector connector;
+ protected TestInvocationHandler invocationHandler;
+
+ protected void addExtraClientConfig(Map config) {}
+ protected void addExtraServerConfig(Map config) {}
+
+
+ public void setUp() throws Exception
+ {
+ if (firstTime)
+ {
+ String keyStoreFilePath =
getClass().getResource("../.keystore").getFile();
+ System.setProperty("javax.net.ssl.keyStore", keyStoreFilePath);
+ System.setProperty("javax.net.ssl.keyStorePassword",
"unit-tests-server");
+ String trustStoreFilePath =
getClass().getResource("../.truststore").getFile();
+ System.setProperty("javax.net.ssl.trustStore", trustStoreFilePath);
+ System.setProperty("javax.net.ssl.trustStorePassword",
"unit-tests-client");
+ }
+ super.setUp();
+ }
+
+
+ protected String getServerSocketFactoryClassName()
+ {
+ return SSLTestServerSocketFactory.class.getName();
+ }
+
+ protected Constructor getServerSocketFactoryConstructor() throws
NoSuchMethodException
+ {
+ return SSLTestServerSocketFactory.class.getConstructor(new Class[]{int.class});
+ }
+
+ protected String getSocketFactoryClassName()
+ {
+ return SSLTestSocketFactory.class.getName();
+ }
+
+ protected Constructor getSocketFactoryConstructor() throws NoSuchMethodException
+ {
+ return SSLTestSocketFactory.class.getConstructor(new Class[]{int.class});
+ }
+
+ static public class SSLTestServerSocketFactory extends ServerSocketFactory
+ {
+ int timeout;
+ ServerSocketFactory factory;
+
+ public SSLTestServerSocketFactory() throws IOException
+ {
+ this.timeout = 5000;
+ setupFactory();
+ }
+ public SSLTestServerSocketFactory(int timeout) throws IOException
+ {
+ this.timeout = timeout;
+ setupFactory();
+ }
+ public ServerSocket createServerSocket() throws IOException
+ {
+ ServerSocket ss = null;
+ if (callbackTest)
+ {
+ ss = SSLServerSocketFactory.getDefault().createServerSocket();
+ }
+ else
+ {
+ ss = new SSLTestServerSocket(timeout, ((SSLServerSocket)
factory.createServerSocket()));
+ }
+ log.info("returning: " + ss);
+ return ss;
+ }
+ public ServerSocket createServerSocket(int port) throws IOException
+ {
+ ServerSocket ss = null;
+ if (callbackTest && port != SECONDARY_SERVER_SOCKET_PORT)
+ {
+ ss = SSLServerSocketFactory.getDefault().createServerSocket(port);
+ }
+ else
+ {
+ ss = new SSLTestServerSocket(port, timeout, ((SSLServerSocket)
factory.createServerSocket()));
+ }
+ log.info("returning: " + ss);
+ return ss;
+ }
+
+ public ServerSocket createServerSocket(int port, int backlog) throws IOException
+ {
+ ServerSocket ss = null;
+ if (callbackTest && port != SECONDARY_SERVER_SOCKET_PORT)
+ {
+ ss = SSLServerSocketFactory.getDefault().createServerSocket(port, backlog);
+ }
+ else
+ {
+ ss = new SSLTestServerSocket(port, backlog, timeout, ((SSLServerSocket)
factory.createServerSocket()));
+ }
+ log.info("returning: " + ss);
+ return ss;
+ }
+
+ public ServerSocket createServerSocket(int port, int backlog, InetAddress
ifAddress) throws IOException
+ {
+ ServerSocket ss = null;
+ if (callbackTest && port != SECONDARY_SERVER_SOCKET_PORT)
+ {
+ ss = SSLServerSocketFactory.getDefault().createServerSocket(port, backlog,
ifAddress);
+ }
+ else
+ {
+ ss = new SSLTestServerSocket(port, backlog, ifAddress, timeout,
((SSLServerSocket) factory.createServerSocket()));
+ }
+ log.info("returning: " + ss);
+ return ss;
+ }
+
+ protected void setupFactory() throws IOException
+ {
+ SSLSocketBuilder sslSocketBuilder = new SSLSocketBuilder();
+ sslSocketBuilder.setUseSSLServerSocketFactory(false);
+ factory = sslSocketBuilder.createSSLServerSocketFactory();
+ }
+ }
+
+
+ static class SSLTestServerSocket extends SSLServerSocket
+ {
+ int timeout;
+ SSLServerSocket serverSocket;
+
+ public SSLTestServerSocket(int timeout, SSLServerSocket serverSocket) throws
IOException
+ {
+ super();
+ this.timeout = timeout;
+ this.serverSocket = serverSocket;
+ }
+ public SSLTestServerSocket(int port, int timeout, SSLServerSocket serverSocket)
throws IOException
+ {
+ super(port);
+ this.timeout = timeout;
+ this.serverSocket = serverSocket;
+ }
+ public SSLTestServerSocket(int port, int backlog, int timeout, SSLServerSocket
serverSocket) throws IOException
+ {
+ super(port, backlog);
+ this.timeout = timeout;
+ this.serverSocket = serverSocket;
+ }
+ public SSLTestServerSocket(int port, int backlog, InetAddress bindAddr, int
timeout, SSLServerSocket serverSocket) throws IOException
+ {
+ super(port, backlog, bindAddr);
+ this.timeout = timeout;
+ this.serverSocket = serverSocket;
+ }
+ public Socket accept() throws IOException
+ {
+ SSLSocket s1 = (SSLSocket) serverSocket.accept();
+ Socket s2 = new SSLTestSocket(timeout, s1);
+ implAccept(s2);
+ return s2;
+ }
+ public String toString()
+ {
+ return "SSLTestServerSocket[" + serverSocket.toString() +
"]";
+ }
+ public boolean getEnableSessionCreation()
+ {
+ return serverSocket.getEnableSessionCreation();
+ }
+ public String[] getEnabledCipherSuites()
+ {
+ return serverSocket.getEnabledCipherSuites();
+ }
+ public String[] getEnabledProtocols()
+ {
+ return serverSocket.getEnabledProtocols();
+ }
+ public boolean getNeedClientAuth()
+ {
+ return serverSocket.getNeedClientAuth();
+ }
+ public String[] getSupportedCipherSuites()
+ {
+ return serverSocket.getSupportedCipherSuites();
+ }
+ public String[] getSupportedProtocols()
+ {
+ return serverSocket.getSupportedProtocols();
+ }
+ public boolean getUseClientMode()
+ {
+ return serverSocket.getUseClientMode();
+ }
+ public boolean getWantClientAuth()
+ {
+ return serverSocket.getWantClientAuth();
+ }
+ public void setEnableSessionCreation(boolean arg0)
+ {
+ serverSocket.setEnableSessionCreation(arg0);
+ }
+ public void setEnabledCipherSuites(String[] arg0)
+ {
+ serverSocket.setEnabledCipherSuites(arg0);
+ }
+ public void setEnabledProtocols(String[] arg0)
+ {
+ serverSocket.setEnabledProtocols(arg0);
+ }
+ public void setNeedClientAuth(boolean arg0)
+ {
+ serverSocket.setNeedClientAuth(arg0);
+ }
+ public void setUseClientMode(boolean arg0)
+ {
+ serverSocket.setUseClientMode(arg0);
+ }
+ public void setWantClientAuth(boolean arg0)
+ {
+ serverSocket.setWantClientAuth(arg0);
+ }
+ }
+
+
+ public static class SSLTestSocketFactory extends SocketFactory
+ {
+ int timeout;
+ SocketFactory factory;
+
+ public SSLTestSocketFactory() throws IOException
+ {
+ timeout = 5000;
+ setupFactory();
+ }
+ public SSLTestSocketFactory(int timeout) throws IOException
+ {
+ this.timeout = timeout;
+ setupFactory();
+ }
+ public Socket createSocket() throws IOException
+ {
+ return new SSLTestSocket(timeout, ((SSLSocket) factory.createSocket()));
+ }
+ public Socket createSocket(String arg0, int arg1) throws IOException,
UnknownHostException
+ {
+ return new SSLTestSocket(arg0, arg1, timeout, ((SSLSocket)
factory.createSocket()));
+ }
+
+ public Socket createSocket(InetAddress arg0, int arg1) throws IOException
+ {
+ return new SSLTestSocket(arg0, arg1, timeout, ((SSLSocket)
factory.createSocket()));
+ }
+
+ public Socket createSocket(String arg0, int arg1, InetAddress arg2, int arg3)
throws IOException, UnknownHostException
+ {
+ return new SSLTestSocket(arg0, arg1, arg2, arg3, timeout, ((SSLSocket)
factory.createSocket()));
+ }
+
+ public Socket createSocket(InetAddress arg0, int arg1, InetAddress arg2, int arg3)
throws IOException
+ {
+ return new SSLTestSocket(arg0, arg1, arg2, arg3, timeout, ((SSLSocket)
factory.createSocket()));
+ }
+
+ protected void setupFactory() throws IOException
+ {
+ SSLSocketBuilder sslSocketBuilder = new SSLSocketBuilder();
+ sslSocketBuilder.setUseSSLServerSocketFactory(false);
+ factory = sslSocketBuilder.createSSLSocketFactory();
+ }
+ }
+
+ static class SSLTestSocket extends SSLSocket
+ {
+ int timeout;
+ SSLSocket socket;
+
+ public SSLTestSocket(int timeout, SSLSocket socket)
+ {
+ this.timeout = timeout;
+ this.socket = socket;
+ }
+ public SSLTestSocket(String host, int port, int timeout, SSLSocket socket) throws
UnknownHostException, IOException
+ {
+ super(host, port);
+ this.timeout = timeout;
+ this.socket = socket;
+ }
+ public SSLTestSocket(InetAddress address, int port, int timeout, SSLSocket socket)
throws IOException
+ {
+ super(address, port);
+ this.timeout = timeout;
+ this.socket = socket;
+ }
+ public SSLTestSocket(String host, int port, InetAddress localAddr, int localPort,
int timeout, SSLSocket socket) throws IOException
+ {
+ super(host, port, localAddr, localPort);
+ this.timeout = timeout;
+ this.socket = socket;
+ }
+ public SSLTestSocket(InetAddress address, int port, InetAddress localAddr, int
localPort, int timeout, SSLSocket socket) throws IOException
+ {
+ super(address, port, localAddr, localPort);
+ this.timeout = timeout;
+ this.socket = socket;
+ }
+ public String toString()
+ {
+ return "SSLTestSocket[" + socket.toString() + "]";
+ }
+ public OutputStream getOutputStream() throws IOException
+ {
+ return new TestOutputStream(socket.getOutputStream(), timeout);
+ }
+ public void addHandshakeCompletedListener(HandshakeCompletedListener listener)
+ {
+ socket.addHandshakeCompletedListener(listener);
+ }
+ public boolean getEnableSessionCreation()
+ {
+ return socket.getEnableSessionCreation();
+ }
+ public String[] getEnabledCipherSuites()
+ {
+ return socket.getEnabledCipherSuites();
+ }
+ public String[] getEnabledProtocols()
+ {
+ return socket.getEnabledProtocols();
+ }
+ public boolean getNeedClientAuth()
+ {
+ return socket.getNeedClientAuth();
+ }
+ public SSLSession getSession()
+ {
+ return socket.getSession();
+ }
+ public String[] getSupportedCipherSuites()
+ {
+ return socket.getSupportedCipherSuites();
+ }
+ public String[] getSupportedProtocols()
+ {
+ return socket.getSupportedProtocols();
+ }
+ public boolean getUseClientMode()
+ {
+ return socket.getUseClientMode();
+ }
+ public boolean getWantClientAuth()
+ {
+ return socket.getWantClientAuth();
+ }
+ public void removeHandshakeCompletedListener(HandshakeCompletedListener listener)
+ {
+ socket.removeHandshakeCompletedListener(listener);
+ }
+ public void setEnableSessionCreation(boolean flag)
+ {
+ socket.setEnableSessionCreation(flag);
+ }
+ public void setEnabledCipherSuites(String[] suites)
+ {
+ socket.setEnabledCipherSuites(suites);
+ }
+ public void setEnabledProtocols(String[] protocols)
+ {
+ socket.setEnabledProtocols(protocols);
+ }
+ public void setNeedClientAuth(boolean need)
+ {
+ socket.setNeedClientAuth(need);
+ }
+ public void setUseClientMode(boolean mode)
+ {
+ socket.setUseClientMode(mode);
+ }
+ public void setWantClientAuth(boolean want)
+ {
+ socket.setWantClientAuth(want);
+ }
+ public void startHandshake() throws IOException
+ {
+ socket.startHandshake();
+ }
+ }
+}
\ No newline at end of file