From jboss-remoting-commits at lists.jboss.org Tue Aug 18 22:05:51 2009 Content-Type: multipart/mixed; boundary="===============4277747182238847952==" MIME-Version: 1.0 From: jboss-remoting-commits at lists.jboss.org To: jboss-remoting-commits at lists.jboss.org Subject: [jboss-remoting-commits] JBoss Remoting SVN: r5374 - remoting2/branches/2.2/src/tests/org/jboss/test/remoting/transport/socket/timeout. Date: Tue, 18 Aug 2009 22:05:51 -0400 Message-ID: <200908190205.n7J25pTb006593@svn01.web.mwc.hst.phx2.redhat.com> --===============4277747182238847952== Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Author: ron.sigal(a)jboss.com Date: 2009-08-18 22:05:50 -0400 (Tue, 18 Aug 2009) New Revision: 5374 Added: remoting2/branches/2.2/src/tests/org/jboss/test/remoting/transport/socke= t/timeout/SSLWriteTimeoutTestParent.java remoting2/branches/2.2/src/tests/org/jboss/test/remoting/transport/socke= t/timeout/SocketWriteTimeoutTestCase.java remoting2/branches/2.2/src/tests/org/jboss/test/remoting/transport/socke= t/timeout/WriteTimeoutTestParent.java Log: JBREM-1120: New unit tests. Added: remoting2/branches/2.2/src/tests/org/jboss/test/remoting/transport/s= ocket/timeout/SSLWriteTimeoutTestParent.java =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- remoting2/branches/2.2/src/tests/org/jboss/test/remoting/transport/sock= et/timeout/SSLWriteTimeoutTestParent.java (rev 0) +++ remoting2/branches/2.2/src/tests/org/jboss/test/remoting/transport/sock= et/timeout/SSLWriteTimeoutTestParent.java 2009-08-19 02:05:50 UTC (rev 5374) @@ -0,0 +1,580 @@ +/* +* 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.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.lang.reflect.Constructor; +import java.net.InetAddress; +import java.net.InetSocketAddress; +import java.net.ServerSocket; +import java.net.Socket; +import java.net.SocketAddress; +import java.net.UnknownHostException; +import java.util.Map; + +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 org.apache.log4j.Logger; +import org.jboss.remoting.InvokerLocator; +import org.jboss.remoting.security.SSLSocketBuilder; +import org.jboss.remoting.transport.Connector; + + +/** + * Unit tests for JBREM-1120. + * = + * @author Ron Sigal + * @version $Rev$ + *

+ * Copyright Apr 22, 2009 + *

+ */ +public abstract class SSLWriteTimeoutTestParent extends WriteTimeoutTestPa= rent +{ + private static Logger log =3D Logger.getLogger(SSLWriteTimeoutTestParen= t.class); + = + private static boolean firstTime =3D true; + = + protected static int SECONDARY_SERVER_SOCKET_PORT =3D 8765; + protected static String SECONDARY_SERVER_SOCKET_PORT_STRING =3D "8765"; + = + 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 =3D getClass().getResource("../.keystore"= ).getFile(); + System.setProperty("javax.net.ssl.keyStore", keyStoreFilePath); + System.setProperty("javax.net.ssl.keyStorePassword", "unit-tests-= server"); + String trustStoreFilePath =3D getClass().getResource("../.trustst= ore").getFile(); + System.setProperty("javax.net.ssl.trustStore", trustStoreFilePath= ); + System.setProperty("javax.net.ssl.trustStorePassword", "unit-test= s-client"); + } + super.setUp(); + } + = + = + protected String getServerSocketFactoryClassName() + { + return SSLTestServerSocketFactory.class.getName(); + } + = + protected Constructor getServerSocketFactoryConstructor() throws NoSuch= MethodException + { + return SSLTestServerSocketFactory.class.getConstructor(new Class[]{i= nt.class, int.class}); + } + = + protected String getSocketFactoryClassName() + { + return SSLTestSocketFactory.class.getName(); + } + = + protected Constructor getSocketFactoryConstructor() throws NoSuchMethod= Exception + { + return SSLTestSocketFactory.class.getConstructor(new Class[]{int.cla= ss, int.class}); + } + = + static public class SSLTestServerSocketFactory extends ServerSocketFact= ory + { + int timeout; + ServerSocketFactory factory; + int initialWrites; + = + public SSLTestServerSocketFactory() throws IOException + { + this.timeout =3D 5000; + this.initialWrites =3D -1; + setupFactory(); + } = + public SSLTestServerSocketFactory(int timeout, int initialWrites) th= rows IOException + { + this.timeout =3D timeout; + this.initialWrites =3D initialWrites; + setupFactory(); + } + public ServerSocket createServerSocket() throws IOException + { + ServerSocket ss =3D null; + if (callbackTest) + { + ss =3D SSLServerSocketFactory.getDefault().createServerSocket(= ); + } + else + { + ss =3D new SSLTestServerSocket(timeout, initialWrites, ((SSLSe= rverSocket) factory.createServerSocket())); + } + log.info("returning: " + ss); + return ss; + } + public ServerSocket createServerSocket(int port) throws IOException + { + ServerSocket ss =3D null; + if (callbackTest && port !=3D secondaryServerSocketPort) + { + ss =3D SSLServerSocketFactory.getDefault().createServerSocket(= port); + } + else + { + ss =3D new SSLTestServerSocket(port, timeout, initialWrites, (= (SSLServerSocket) factory.createServerSocket())); + } + log.info("returning: " + ss); + return ss; + } + + public ServerSocket createServerSocket(int port, int backlog) throws= IOException + { + ServerSocket ss =3D null; + if (callbackTest && port !=3D secondaryServerSocketPort) + { + ss =3D SSLServerSocketFactory.getDefault().createServerSocket(= port, backlog); + } + else + { + ss =3D new SSLTestServerSocket(port, backlog, timeout, initial= Writes, ((SSLServerSocket) factory.createServerSocket())); + } + log.info("returning: " + ss); + return ss; + } + + public ServerSocket createServerSocket(int port, int backlog, InetAd= dress ifAddress) throws IOException + { + ServerSocket ss =3D null; + if (callbackTest && port !=3D secondaryServerSocketPort) + { + ss =3D SSLServerSocketFactory.getDefault().createServerSocket(= port, backlog, ifAddress); + } + else + { + ss =3D new SSLTestServerSocket(port, backlog, ifAddress, timeo= ut, initialWrites, ((SSLServerSocket) factory.createServerSocket())); + } + log.info("returning: " + ss); + return ss; + } + = + protected void setupFactory() throws IOException + { + SSLSocketBuilder sslSocketBuilder =3D new SSLSocketBuilder(); + sslSocketBuilder.setUseSSLServerSocketFactory(false); + factory =3D sslSocketBuilder.createSSLServerSocketFactory(); + } + } + = + = + static class SSLTestServerSocket extends SSLServerSocket + { + int timeout; + int initialWrites; + SSLServerSocket serverSocket; + + public SSLTestServerSocket(int timeout, int initialWrites, SSLServer= Socket serverSocket) throws IOException + { + super(); + this.timeout =3D timeout; + this.initialWrites =3D initialWrites; + this.serverSocket =3D serverSocket; + } + public SSLTestServerSocket(int port, int timeout, int initialWrites,= SSLServerSocket serverSocket) throws IOException + { + super(port); + this.timeout =3D timeout; + this.initialWrites =3D initialWrites; + this.serverSocket =3D serverSocket; + bind(new InetSocketAddress(port), 50); + } + public SSLTestServerSocket(int port, int backlog, int timeout, int i= nitialWrites, SSLServerSocket serverSocket) throws IOException + { + super(port, backlog); + this.timeout =3D timeout; + this.initialWrites =3D initialWrites; + this.serverSocket =3D serverSocket; + bind(new InetSocketAddress(port), backlog); + } + public SSLTestServerSocket(int port, int backlog, InetAddress bindAd= dr, int timeout, int initialWrites, SSLServerSocket serverSocket) throws IO= Exception + { + super(port, backlog, bindAddr); + this.timeout =3D timeout; + this.initialWrites =3D initialWrites; + this.serverSocket =3D serverSocket; + bind(new InetSocketAddress(bindAddr, port), 50); + } + public Socket accept() throws IOException + { + SSLSocket s1 =3D (SSLSocket) serverSocket.accept(); + Socket s2 =3D new SSLTestSocket(timeout, initialWrites, s1); + return s2; + } + public void bind(SocketAddress endpoint, int backlog) throws IOExcep= tion + { + log.info("serverSocket: " + serverSocket); + if (serverSocket !=3D null) log.info("bound: " + serverSocket.isB= ound()); + if (serverSocket !=3D null && !serverSocket.isBound()) + { + log.info("binding " + serverSocket); + serverSocket.bind(endpoint, backlog); + } + } + 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 InetAddress getInetAddress() + { + return serverSocket.getInetAddress(); + } + 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; + int initialWrites; + SocketFactory factory; + = + public SSLTestSocketFactory() throws IOException + { + timeout =3D 5000; + initialWrites =3D -1; + setupFactory(); + } + public SSLTestSocketFactory(int timeout, int initialWrites) throws I= OException + { + this.timeout =3D timeout; + this.initialWrites =3D initialWrites; + setupFactory(); + } + public Socket createSocket() throws IOException + { + log.info("callbackTest: " + callbackTest); + Socket s =3D null; + if (callbackTest) + { + s =3D factory.createSocket(); + } + else + { + s =3D new TestSocket(timeout, initialWrites); + s =3D new SSLTestSocket(timeout, initialWrites, ((SSLSocket) f= actory.createSocket())); + } + log.info(this + " returning " + s); + return s; + } + public Socket createSocket(String arg0, int arg1) throws IOException= , UnknownHostException + { + log.info("callbackTest: " + callbackTest); + Socket s =3D null; + if (callbackTest && arg1 !=3D secondaryServerSocketPort) + { + s =3D factory.createSocket(arg0, arg1); + } + else + { + s =3D new TestSocket(timeout, initialWrites); + s =3D new SSLTestSocket(arg0, arg1, timeout, initialWrites, ((= SSLSocket) factory.createSocket())); + } + log.info(this + " returning " + s); + return s; + } + + public Socket createSocket(InetAddress arg0, int arg1) throws IOExce= ption + { + log.info("callbackTest: " + callbackTest); + Socket s =3D null; + if (callbackTest && arg1 !=3D secondaryServerSocketPort) + { + s =3D factory.createSocket(arg0, arg1); + } + else + { + s =3D new TestSocket(timeout, initialWrites); + s =3D new SSLTestSocket(arg0, arg1, timeout, initialWrites, ((= SSLSocket) factory.createSocket())); + } + log.info(this + " returning " + s); + return s; + } + + public Socket createSocket(String arg0, int arg1, InetAddress arg2, = int arg3) throws IOException, UnknownHostException + { + log.info("callbackTest: " + callbackTest); + Socket s =3D null; + if (callbackTest && arg1 !=3D secondaryServerSocketPort) + { + s =3D factory.createSocket(arg0, arg1); + } + else + { + s =3D new TestSocket(timeout, initialWrites); + s =3D new SSLTestSocket(arg0, arg1, arg2, arg3, timeout, initi= alWrites, ((SSLSocket) factory.createSocket())); + } + log.info(this + " returning " + s); + return s; + } + + public Socket createSocket(InetAddress arg0, int arg1, InetAddress a= rg2, int arg3) throws IOException + { + log.info("callbackTest: " + callbackTest); + Socket s =3D null; + if (callbackTest && arg1 !=3D secondaryServerSocketPort) + { + s =3D factory.createSocket(arg0, arg1); + } + else + { + s =3D new TestSocket(timeout, initialWrites); + s =3D new SSLTestSocket(arg0, arg1, arg2, arg3, timeout, initi= alWrites, ((SSLSocket) factory.createSocket())); + } + log.info(this + " returning " + s); + return s; + } + = + protected void setupFactory() throws IOException + { + SSLSocketBuilder sslSocketBuilder =3D new SSLSocketBuilder(); + sslSocketBuilder.setUseSSLServerSocketFactory(false); + factory =3D sslSocketBuilder.createSSLSocketFactory(); + } + } + = + static class SSLTestSocket extends SSLSocket + { + int timeout; + int initialWrites; + SSLSocket socket; + SocketAddress endpoint; + = + public SSLTestSocket(int timeout, int initialWrites, SSLSocket socke= t) + { + this.timeout =3D timeout; + this.initialWrites =3D initialWrites; + this.socket =3D socket; + } + public SSLTestSocket(String host, int port, int timeout, int initial= Writes, SSLSocket socket) throws UnknownHostException, IOException + { + super(host, port); + this.timeout =3D timeout; + this.initialWrites =3D initialWrites; + this.socket =3D socket; + connect(new InetSocketAddress(host, port), timeout); + } + public SSLTestSocket(InetAddress address, int port, int timeout, int= initialWrites, SSLSocket socket) throws IOException + { + super(address, port); + this.timeout =3D timeout; + this.initialWrites =3D initialWrites; + this.socket =3D socket; + connect(new InetSocketAddress(address, port), timeout); + } + public SSLTestSocket(String host, int port, InetAddress localAddr, i= nt localPort, int timeout, int initialWrites, SSLSocket socket) throws IOEx= ception + { + super(host, port, localAddr, localPort); + this.timeout =3D timeout; + this.initialWrites =3D initialWrites; + this.socket =3D socket; + bind(new InetSocketAddress(localAddr, localPort)); + connect(new InetSocketAddress(host, port), timeout); + } + public SSLTestSocket(InetAddress address, int port, InetAddress loca= lAddr, int localPort, int timeout, int initialWrites, SSLSocket socket) thr= ows IOException + { + super(address, port, localAddr, localPort); + this.timeout =3D timeout; + this.initialWrites =3D initialWrites; + this.socket =3D socket; + bind(new InetSocketAddress(localAddr, localPort)); + connect(new InetSocketAddress(address, port), timeout); + } + public String toString() + { + return "SSLTestSocket[" + socket.toString() + "]"; + } + public InputStream getInputStream() throws IOException + { + return socket.getInputStream(); + } + public OutputStream getOutputStream() throws IOException + { + return new TestOutputStream(socket.getOutputStream(), timeout, in= itialWrites); + } + public void addHandshakeCompletedListener(HandshakeCompletedListener= listener) + { + socket.addHandshakeCompletedListener(listener); + } + public void bind(SocketAddress bindpoint) throws IOException + { + if (socket !=3D null) + socket.bind(bindpoint); + } + public void connect(SocketAddress endpoint) throws IOException + { + if (socket !=3D null) + socket.connect(endpoint); + } + public void connect(SocketAddress endpoint, int timeout) throws IOEx= ception + { + socket.connect(endpoint, timeout); + } + public boolean getEnableSessionCreation() + { + return socket.getEnableSessionCreation(); + } + public String[] getEnabledCipherSuites() + { + return socket.getEnabledCipherSuites(); + } + public String[] getEnabledProtocols() + { + return socket.getEnabledProtocols(); + } + public InetAddress getInetAddress() + { + return socket.getInetAddress(); + } + 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(HandshakeCompletedListe= ner 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 Added: remoting2/branches/2.2/src/tests/org/jboss/test/remoting/transport/s= ocket/timeout/SocketWriteTimeoutTestCase.java =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- remoting2/branches/2.2/src/tests/org/jboss/test/remoting/transport/sock= et/timeout/SocketWriteTimeoutTestCase.java (rev 0) +++ remoting2/branches/2.2/src/tests/org/jboss/test/remoting/transport/sock= et/timeout/SocketWriteTimeoutTestCase.java 2009-08-19 02:05:50 UTC (rev 537= 4) @@ -0,0 +1,39 @@ +/* +* 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; + +/** + * Unit tests for JBREM-1120. + * = + * @author Ron Sigal + * @version $Rev$ + *

+ * Copyright Apr 22, 2009 + *

+ */ +public class SocketWriteTimeoutTestCase extends WriteTimeoutTestParent +{ + protected String getTransport() + { + return "socket"; + } +} Added: remoting2/branches/2.2/src/tests/org/jboss/test/remoting/transport/s= ocket/timeout/WriteTimeoutTestParent.java =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- remoting2/branches/2.2/src/tests/org/jboss/test/remoting/transport/sock= et/timeout/WriteTimeoutTestParent.java (rev 0) +++ remoting2/branches/2.2/src/tests/org/jboss/test/remoting/transport/sock= et/timeout/WriteTimeoutTestParent.java 2009-08-19 02:05:50 UTC (rev 5374) @@ -0,0 +1,845 @@ +/* +* 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.IOException; +import java.io.OutputStream; +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; +import java.net.InetAddress; +import java.net.ServerSocket; +import java.net.Socket; +import java.net.UnknownHostException; +import java.rmi.MarshalException; +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 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.logging.XLevel; +import org.jboss.remoting.CannotConnectException; +import org.jboss.remoting.Client; +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.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; + + +/** + * Unit tests for JBREM-1120. + * = + * @author Ron Sigal + * @version $Rev$ + *

+ * Copyright Apr 22, 2009 + *

+ */ +public abstract class WriteTimeoutTestParent extends TestCase +{ + private static Logger log =3D Logger.getLogger(WriteTimeoutTestParent.c= lass); + = + private static boolean firstTime =3D true; + protected static int secondaryServerSocketPort; + protected static boolean callbackTest; + = + protected String host; + protected int port; + protected String locatorURI; + protected InvokerLocator serverLocator; + protected Connector connector; + protected TestInvocationHandler invocationHandler; + + = + public void setUp() throws Exception + { + if (firstTime) + { + firstTime =3D false; + Logger.getLogger("org.jboss.remoting").setLevel(XLevel.INFO); + Logger.getLogger("org.jboss.test.remoting").setLevel(Level.INFO); + String pattern =3D "[%d{ABSOLUTE}] [%t] %5p (%F:%L) - %m%n"; + PatternLayout layout =3D new PatternLayout(pattern); + ConsoleAppender consoleAppender =3D new ConsoleAppender(layout); + Logger.getRootLogger().addAppender(consoleAppender); = + } + = + TestOutputStream.counter =3D 0; + callbackTest =3D false; + } + + = + public void tearDown() + { + } + = + = + public void testClientWriteTimeout() throws Throwable + { + log.info("entering " + getName()); + = + // Start server. + setupServer(false, false, "", -1, -1); + = + // Create client. + InvokerLocator clientLocator =3D new InvokerLocator(locatorURI); + HashMap clientConfig =3D new HashMap(); + clientConfig.put(InvokerLocator.FORCE_REMOTE, "true"); + clientConfig.put(SocketWrapper.WRITE_TIMEOUT, "1000"); + SocketFactory sf =3D (SocketFactory) getSocketFactoryConstructor().n= ewInstance(new Object[]{new Integer(5000), new Integer(1)}); + clientConfig.put(Remoting.CUSTOM_SOCKET_FACTORY, sf); + addExtraClientConfig(clientConfig); + Client client =3D new Client(clientLocator, clientConfig); + client.connect(); + log.info("client is connected"); + = + log.info("**************************************"); + log.info("*** WorkerThread error is expected ***"); + log.info("**************************************"); + = + // Test client side write timeout. + try + { + client.invoke("abc"); + } + catch (MarshalException e) + { + log.info(e.getMessage()); + assertNotNull(e.getMessage()); + log.info("e.getMessage(): " + e.getMessage()); + assertTrue(e.getMessage().startsWith("Failed to communicate. Pro= blem during marshalling/unmarshalling")); + assertTrue(e.getCause() instanceof IOException); + IOException ioe =3D (IOException) e.getCause(); + assertEquals("closed", ioe.getMessage()); + log.info("got expected Exception"); + } + catch (Throwable t) + { + log.error("got unexpected Exception", t); + fail("got unexpected Exception"); + } + = + client.disconnect(); + shutdownServer(); + log.info(getName() + " PASSES"); + } + = + = + public void testServerWriteTimeout() throws Throwable + { + log.info("entering " + getName()); + = + // Start server. + setupServer(true, false, "1000", 5000, 1); + = + // Create client. + InvokerLocator clientLocator =3D new InvokerLocator(locatorURI); + HashMap clientConfig =3D new HashMap(); + clientConfig.put(InvokerLocator.FORCE_REMOTE, "true"); + clientConfig.put("numberOfCallRetries", "1"); + clientConfig.put("timeout", "10000"); + addExtraClientConfig(clientConfig); + Client client =3D new Client(clientLocator, clientConfig); + client.connect(); + log.info("client is connected"); + = + log.info("**************************************"); + log.info("*** WorkerThread error is expected ***"); + log.info("**************************************"); + = + // Test server side write timeout. + try + { + client.invoke("abc"); + } + catch (MarshalException e) + { + log.info(e.getMessage()); +// assertNotNull(e.getMessage()); +// assertTrue(e.getMessage().startsWith("Unable to perform invocat= ion")); +// assertTrue(e.getCause() instanceof EOFException); + log.info("got expected Exception"); + } + catch (Throwable t) + { + log.error("got unexpected Exception", t); + fail("got unexpected Exception"); + } + = + // Test server invoker state. + Thread.sleep(4000); + SocketServerInvoker serverInvoker =3D (SocketServerInvoker) connecto= r.getServerInvoker(); + assertEquals(0, serverInvoker.getCurrentClientPoolSize()); + assertEquals(1, serverInvoker.getCurrentThreadPoolSize()); + log.info("used ServerThread has returned to threadPool"); + = + client.disconnect(); + shutdownServer(); + log.info(getName() + " PASSES"); + } + = + = + public void testClientCallbackWriteTimeout() throws Throwable + { + log.info("entering " + getName()); + = + // Start server. + if (isBisocket(getTransport())) + { + callbackTest =3D true; + } + setupServer(false, false, "", -1, -1); + = + // Create client. + InvokerLocator clientLocator =3D new InvokerLocator(locatorURI); + HashMap clientConfig =3D new HashMap(); + clientConfig.put(InvokerLocator.FORCE_REMOTE, "true"); + clientConfig.put(SocketWrapper.WRITE_TIMEOUT, "1000"); + if (isBisocket(getTransport())) + { + SocketFactory sf =3D (SocketFactory) getSocketFactoryConstructor(= ).newInstance(new Object[]{new Integer(5000), new Integer(1)}); + clientConfig.put(Remoting.CUSTOM_SOCKET_FACTORY, sf); + } + else + { + ServerSocketFactory ssf =3D (ServerSocketFactory) getServerSocket= FactoryConstructor().newInstance(new Object[]{new Integer(5000), new Intege= r(-1)}); + clientConfig.put(Remoting.CUSTOM_SERVER_SOCKET_FACTORY, ssf); + } + addExtraClientConfig(clientConfig); + Client client =3D new Client(clientLocator, clientConfig); + client.connect(); + log.info("client is connected"); + = + // Test connection + client.invoke("abc"); + log.info("connection is good"); + = + // Test client callback write timeout. + log.info("registering callback handler"); + log.info("**************************************"); + log.info("*** WorkerThread error is expected ***"); + log.info("**************************************"); + TestCallbackHandler callbackHandler =3D new TestCallbackHandler(); + HashMap metadata =3D new HashMap(); + if (isBisocket(getTransport())) + { +// metadata.put(SocketWrapper.WRITE_TIMEOUT, "1000"); + metadata.put(Remoting.SOCKET_FACTORY_NAME, getSocketFactoryClassN= ame()); + metadata.put("numberOfCallRetries", "1"); + metadata.put(Bisocket.IS_CALLBACK_SERVER, "true"); + metadata.put(Bisocket.PING_FREQUENCY, "11111111"); = + } + else + { +// metadata.put(SocketWrapper.WRITE_TIMEOUT, "1000"); + metadata.put(ServerInvoker.SERVER_SOCKET_FACTORY, getServerSocket= FactoryClassName()); + metadata.put("numberOfCallRetries", "1"); + } + client.addListener(callbackHandler, metadata, null, true); + log.info("called Client.addListener()"); + = + // Test server invoker state. + // Wait for local ServerThread to time out. Might take a while in b= isocket transports, since + // the request to get a socket for the callback client invoker needs= its own write on the + // control socket. + Thread.sleep(20000); + log.info("back from sleep"); + Set callbackConnectors =3D client.getCallbackConnectors(callbackHand= ler); + assertEquals(1, callbackConnectors.size()); + Connector callbackConnector =3D (Connector) callbackConnectors.itera= tor().next(); + SocketServerInvoker serverInvoker =3D (SocketServerInvoker) callback= Connector.getServerInvoker(); + assertEquals(0, serverInvoker.getCurrentClientPoolSize()); + assertEquals(1, serverInvoker.getCurrentThreadPoolSize()); + log.info("used ServerThread has returned to threadPool"); + + client.disconnect(); + shutdownServer(); + log.info(getName() + " PASSES"); + } + = + = + public void testServerCallbackWriteTimeout() throws Throwable + { + log.info("entering " + getName()); + = + // Start server. + if (isBisocket(getTransport())) + { + callbackTest =3D true; + setupServer(true, false, "1000", 5000, 1); + } + else + { + setupServer(false, true, "1000", 5000, 1); + } + = + // Create client. + InvokerLocator clientLocator =3D new InvokerLocator(locatorURI); + HashMap clientConfig =3D new HashMap(); + clientConfig.put(InvokerLocator.FORCE_REMOTE, "true"); + clientConfig.put("numberOfCallRetries", "1"); + addExtraClientConfig(clientConfig); + Client client =3D new Client(clientLocator, clientConfig); + client.connect(); + log.info("client is connected"); + = + // Test connection + client.invoke("abc"); + log.info("connection is good"); + = + // Test server callback write timeout. + log.info("registering callback handler"); + log.info("**************************************"); + log.info("*** WorkerThread error is expected ***"); + log.info("**************************************"); + HashMap metadata =3D new HashMap(); + if (isBisocket(getTransport())) + { + metadata.put(Bisocket.IS_CALLBACK_SERVER, "true"); + } + TestCallbackHandler callbackHandler =3D new TestCallbackHandler(); + client.addListener(callbackHandler, metadata, null, true); + log.info("added listener"); + = + // Test server invoker state. + Thread.sleep(20000); + log.info("waking up"); + Throwable t =3D invocationHandler.t; + assertTrue(t instanceof HandleCallbackException); + log.info("t.getCause:", t.getCause()); + if (t.getCause() instanceof MarshalException) + { + MarshalException e =3D (MarshalException) t.getCause(); + assertNotNull(e.getMessage()); + assertTrue(e.getMessage().startsWith("Failed to communicate. Pro= blem during marshalling/unmarshalling")); + assertTrue(e.getCause() instanceof IOException); + IOException ioe =3D (IOException) e.getCause(); + assertEquals("closed", ioe.getMessage()); + } + else + { + assertTrue(t.getCause() instanceof CannotConnectException); + log.info("t.getCause().getCause(): ", t.getCause().getCause()); + assertTrue(t.getCause().getCause() instanceof InvocationTargetExc= eption); + log.info("t.getCause().getCause().getCause(): ", t.getCause().get= Cause().getCause()); +// assertTrue(t.getCause().getCause().getCause() instanceof SSLPro= tocolException); + assertTrue(t.getCause().getCause().getCause() instanceof IOExcept= ion); + assertEquals("closed", t.getCause().getCause().getCause().getMess= age()); + } + log.info("got expected Exception"); + = + client.disconnect(); + shutdownServer(); + log.info(getName() + " PASSES"); + } + = + = + protected abstract String getTransport(); + = + protected boolean isBisocket(String transport) + { + return transport.indexOf("bisocket") >=3D 0; + } + = + protected String getServerSocketFactoryClassName() + { + return TestServerSocketFactory.class.getName(); + } + = + protected Constructor getServerSocketFactoryConstructor() throws NoSuch= MethodException + { + return TestServerSocketFactory.class.getConstructor(new Class[]{int.= class, int.class}); + } + = + protected String getSocketFactoryClassName() + { + return TestSocketFactory.class.getName(); + } + = + protected Constructor getSocketFactoryConstructor() throws NoSuchMethod= Exception + { + return TestSocketFactory.class.getConstructor(new Class[]{int.class,= int.class}); + } + = + protected void addExtraClientConfig(Map config) {} + protected void addExtraServerConfig(Map config) {} + = + + protected void setupServer(boolean setWriteTimeout, boolean setCallback= WriteTimeout, + String writeTimeout, int blockingTime, int i= nitialWrites) throws Exception + { + host =3D InetAddress.getLocalHost().getHostAddress(); + port =3D PortUtil.findFreePort(host); + locatorURI =3D getTransport() + "://" + host + ":" + port; + String metadata =3D System.getProperty("remoting.metadata"); + if (metadata !=3D null) + { + locatorURI +=3D "/?" + metadata; + } + serverLocator =3D new InvokerLocator(locatorURI); + log.info("Starting remoting server with locator uri of: " + locatorU= RI); + HashMap config =3D new HashMap(); + config.put(InvokerLocator.FORCE_REMOTE, "true"); + if (isBisocket(getTransport())) + { + secondaryServerSocketPort =3D PortUtil.findFreePort(host); + config.put(Bisocket.SECONDARY_BIND_PORT, Integer.toString(seconda= ryServerSocketPort)); + config.put(Bisocket.PING_FREQUENCY, "11111111"); = + } + if (setWriteTimeout) + { + config.put(SocketWrapper.WRITE_TIMEOUT, writeTimeout); + ServerSocketFactory ssf =3D (ServerSocketFactory) getServerSocket= FactoryConstructor().newInstance(new Object[]{new Integer(blockingTime), ne= w Integer(initialWrites)}); + config.put(Remoting.CUSTOM_SERVER_SOCKET_FACTORY, ssf); + } + if (setCallbackWriteTimeout) + { + config.put(SocketWrapper.WRITE_TIMEOUT, writeTimeout); + SocketFactory sf =3D (SocketFactory) getSocketFactoryConstructor(= ).newInstance(new Object[]{new Integer(blockingTime), new Integer(initialWr= ites)}); + config.put(Remoting.CUSTOM_SOCKET_FACTORY, sf); + } + if (callbackTest) + { + config.put("numberOfCallRetries", "1"); + } + addExtraServerConfig(config); + connector =3D new Connector(serverLocator, config); + connector.create(); + invocationHandler =3D new TestInvocationHandler(); + connector.addInvocationHandler("test", invocationHandler); + connector.start(); + } + = + = + protected void shutdownServer() throws Exception + { + if (connector !=3D null) + connector.stop(); + } + = + = + static class TestInvocationHandler implements ServerInvocationHandler + { + Throwable t; + + public void addListener(final InvokerCallbackHandler callbackHandler) + { + new Thread() + { + public void run() + { + try + { + log.info("sending callback"); + callbackHandler.handleCallback(new Callback("callback")); + } + catch (Throwable t) + { + log.info("throwable: ", t); + TestInvocationHandler.this.t =3D t; + } + } + }.start(); + } + public Object invoke(final InvocationRequest invocation) throws Thro= wable + { + return invocation.getParameter(); + } + public void removeListener(InvokerCallbackHandler callbackHandler) {} + public void setMBeanServer(MBeanServer server) {} + public void setInvoker(ServerInvoker invoker) {} + } + = + = + static class TestCallbackHandler implements InvokerCallbackHandler + { + public void handleCallback(Callback callback) throws HandleCallbackE= xception + { + log.info("received callback"); + } = + } + = + = + static public class TestServerSocketFactory extends ServerSocketFactory + { + int timeout; + int initialWrites; + = + public TestServerSocketFactory() + { + this.timeout =3D 5000; + this.initialWrites =3D -1; + } = + public TestServerSocketFactory(int timeout, int initialWrites) + { + this.timeout =3D timeout; + this.initialWrites =3D initialWrites; + } + public ServerSocket createServerSocket() throws IOException + { + ServerSocket ss =3D null; + if (callbackTest) + { + ss =3D ServerSocketFactory.getDefault().createServerSocket(); + } + else + { + ss =3D new TestServerSocket(timeout, initialWrites); + } + log.info("returning: " + ss); + return ss; + } + public ServerSocket createServerSocket(int port) throws IOException + { + ServerSocket ss =3D null; + if (callbackTest && port !=3D secondaryServerSocketPort) + { + ss =3D ServerSocketFactory.getDefault().createServerSocket(por= t); + } + else + { + ss =3D new TestServerSocket(port, timeout, initialWrites); + } + log.info("returning: " + ss); + return ss; + } + + public ServerSocket createServerSocket(int port, int backlog) throws= IOException + { + ServerSocket ss =3D null; + if (callbackTest && port !=3D secondaryServerSocketPort) + { + ss =3D ServerSocketFactory.getDefault().createServerSocket(por= t, backlog); + } + else + { + ss =3D new TestServerSocket(port, backlog, timeout, initialWri= tes); + } + log.info("returning: " + ss); + return ss; + } + + public ServerSocket createServerSocket(int port, int backlog, InetAd= dress ifAddress) throws IOException + { + ServerSocket ss =3D null; + if (callbackTest && port !=3D secondaryServerSocketPort) + { + ss =3D ServerSocketFactory.getDefault().createServerSocket(por= t, backlog, ifAddress); + } + else + { + ss =3D new TestServerSocket(port, backlog, ifAddress, timeout,= initialWrites); + } + log.info("returning: " + ss); + return ss; + } + } + = + = + static class TestServerSocket extends ServerSocket + { + int timeout; + int initialWrites; + + public TestServerSocket(int timeout, int initialWrites) throws IOExc= eption + { + super(); + this.timeout =3D timeout; + this.initialWrites =3D initialWrites; + } + public TestServerSocket(int port, int timeout, int initialWrites) th= rows IOException + { + super(port); + this.timeout =3D timeout; + this.initialWrites =3D initialWrites; + } + public TestServerSocket(int port, int backlog, int timeout, int init= ialWrites) throws IOException + { + super(port, backlog); + this.timeout =3D timeout; + this.initialWrites =3D initialWrites; + } + public TestServerSocket(int port, int backlog, InetAddress bindAddr,= int timeout, int initialWrites) throws IOException + { + super(port, backlog, bindAddr); + this.timeout =3D timeout; + this.initialWrites =3D initialWrites; + } + public Socket accept() throws IOException + { + Socket s =3D new TestSocket(timeout, initialWrites); + implAccept(s); + return s; + } + public String toString() + { + return "TestServerSocket[" + getLocalPort() + "]"; + } + } + = + = + public static class TestSocketFactory extends SocketFactory + { + int timeout; + int initialWrites =3D -1; + = + public TestSocketFactory() + { + timeout =3D 5000; + } + public TestSocketFactory(int timeout, int initialWrites) + { + this.timeout =3D timeout; + this.initialWrites =3D initialWrites; + } + public Socket createSocket() + { + log.info("callbackTest: " + callbackTest); + Socket s =3D null; + if (callbackTest) + { + s =3D new Socket(); + } + else + { + s =3D new TestSocket(timeout, initialWrites); + } + log.info(this + " returning " + s); + return s; + } + public Socket createSocket(String arg0, int arg1) throws IOException= , UnknownHostException + { + log.info("callbackTest: " + callbackTest + ", port: " + arg1); + Socket s =3D null; + if (callbackTest && arg1 !=3D secondaryServerSocketPort) + { + s =3D new Socket(arg0, arg1); + } + else + { + s =3D new TestSocket(arg0, arg1, timeout, initialWrites); + } + log.info(this + " returning " + s); + return s; + } + + public Socket createSocket(InetAddress arg0, int arg1) throws IOExce= ption + { + log.info("callbackTest: " + callbackTest + ", port: " + arg1); + Socket s =3D null; + if (callbackTest && arg1 !=3D secondaryServerSocketPort) + { + s =3D new Socket(arg0, arg1); + } + else + { + s =3D new TestSocket(arg0, arg1, timeout, initialWrites); + } + log.info(this + " returning " + s); + return s; + } + + public Socket createSocket(String arg0, int arg1, InetAddress arg2, = int arg3) throws IOException, UnknownHostException + { + log.info("callbackTest: " + callbackTest + ", port: " + arg1); + Socket s =3D null; + if (callbackTest && arg1 !=3D secondaryServerSocketPort) + { + s =3D new Socket(arg0, arg1); + } + else + { + s =3D new TestSocket(arg0, arg1, arg2, arg3, timeout, initialW= rites); + } + log.info(this + " returning " + s); + return s; + } + + public Socket createSocket(InetAddress arg0, int arg1, InetAddress a= rg2, int arg3) throws IOException + { + log.info("callbackTest: " + callbackTest + ", port: " + arg1); + Socket s =3D null; + if (callbackTest && arg1 !=3D secondaryServerSocketPort) + { + s =3D new Socket(arg0, arg1); + } + else + { + s =3D new TestSocket(arg0, arg1, arg2, arg3, timeout, initialW= rites); + } + log.info(this + " returning " + s); + return s; + } + } + = + static class TestSocket extends Socket + { + int timeout; + int initialWrites; + = + public TestSocket(int timeout, int initialWrites) + { + this.timeout =3D timeout; + this.initialWrites =3D initialWrites; + } + public TestSocket(String host, int port, int timeout, int initialWri= tes) throws UnknownHostException, IOException + { + super(host, port); + this.timeout =3D timeout; + this.initialWrites =3D initialWrites; + } + public TestSocket(InetAddress address, int port, int timeout, int in= itialWrites) throws IOException + { + super(address, port); + this.timeout =3D timeout; + this.initialWrites =3D initialWrites; + } + public TestSocket(String host, int port, InetAddress localAddr, int = localPort, int timeout, int initialWrites) throws IOException + { + super(host, port, localAddr, localPort); + this.timeout =3D timeout; + this.initialWrites =3D initialWrites; + } + public TestSocket(InetAddress address, int port, InetAddress localAd= dr, int localPort, int timeout, int initialWrites) throws IOException + { + super(address, port, localAddr, localPort); + this.timeout =3D timeout; + this.initialWrites =3D initialWrites; + } + public OutputStream getOutputStream() throws IOException + { + return new TestOutputStream(super.getOutputStream(), timeout, ini= tialWrites); + } + public String toString() + { + return "TestSocket[" + getLocalPort() + "->" + getPort() + "]"; + } + } + = + static class TestOutputStream extends OutputStream + { + OutputStream os; + int timeout; + boolean closed; + int initialWrites; + boolean doWait =3D true; + public static int counter; + = + public TestOutputStream(OutputStream os, int timeout, int initialWri= tes) + { + this.os =3D os; + this.timeout =3D timeout; + this.initialWrites =3D initialWrites; + } + public void close()throws IOException + { + closed =3D true; + super.close(); + log.info(this + " closed"); + } + public void write(int b) throws IOException + { + System.out.print("b: " + b); + if (closed) + { + log.info("TestOutputStream closed, cannot write"); + throw new IOException("closed"); + } + if (doWait && ++counter > initialWrites) + { + try + { + log.info("TestOutputStream.write() sleeping: " + timeout); + Thread.sleep(timeout); + } + catch (InterruptedException e) + { + e.printStackTrace(); + } + } + os.write(b); + } + public void write(byte b[], int off, int len) throws IOException + { + System.out.print("b: "); + for (int i =3D 0; i < len; i++) + { + System.out.print(b[i] + " "); + } + System.out.println(""); + if (closed) + { + log.info("TestOutputStream closed, cannot write"); + throw new IOException("closed"); + } + log.info("TestOutputStream: counter =3D " + counter + ", initialW= rites =3D " + initialWrites); + if (++counter > initialWrites) + { + try + { + log.info("TestOutputStream.write() sleeping: " + timeout); + Thread.sleep(timeout); + } + catch (InterruptedException e) + { + e.printStackTrace(); + } + } + if (closed) + { + log.info("TestOutputStream closed, cannot write"); + throw new IOException("closed"); + } + try + { + log.info(this + " calling write()"); + doWait =3D false; + os.write(b, off, len); + os.flush(); + doWait =3D true; + log.info(this + " back from write()"); + } + catch (IOException e) + { + log.info("exception: ", e); + throw e; + } + } + } +} \ No newline at end of file --===============4277747182238847952==--