2
* JBoss, Home of Professional Open Source
3
* Copyright 2009, JBoss Inc., and individual contributors as indicated
4
* by the @authors tag. See the copyright.txt in the distribution for a
5
* full listing of individual contributors.
7
* This is free software; you can redistribute it and/or modify it
8
* under the terms of the GNU Lesser General Public License as
9
* published by the Free Software Foundation; either version 2.1 of
10
* the License, or (at your option) any later version.
12
* This software is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
* Lesser General Public License for more details.
17
* You should have received a copy of the GNU Lesser General Public
18
* License along with this software; if not, write to the Free
19
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
22
package org.jboss.test.remoting.transport.socket.timeout;
24
import java.io.IOException;
25
import java.io.InputStream;
26
import java.io.OutputStream;
27
import java.lang.reflect.Constructor;
28
import java.net.InetAddress;
29
import java.net.InetSocketAddress;
30
import java.net.ServerSocket;
31
import java.net.Socket;
32
import java.net.SocketAddress;
33
import java.net.UnknownHostException;
36
import javax.net.ServerSocketFactory;
37
import javax.net.SocketFactory;
38
import javax.net.ssl.HandshakeCompletedListener;
39
import javax.net.ssl.SSLServerSocket;
40
import javax.net.ssl.SSLServerSocketFactory;
41
import javax.net.ssl.SSLSession;
42
import javax.net.ssl.SSLSocket;
44
import org.apache.log4j.Logger;
45
import org.jboss.remoting.InvokerLocator;
46
import org.jboss.remoting.security.SSLSocketBuilder;
47
import org.jboss.remoting.transport.Connector;
51
* Unit tests for JBREM-1120.
53
* @author <a href="ron.sigal@jboss.com">Ron Sigal</a>
56
* Copyright Apr 22, 2009
59
public abstract class SSLWriteTimeoutTestParent extends WriteTimeoutTestParent
61
private static Logger log = Logger.getLogger(SSLWriteTimeoutTestParent.class);
63
private static boolean firstTime = true;
65
protected static int SECONDARY_SERVER_SOCKET_PORT = 8765;
66
protected static String SECONDARY_SERVER_SOCKET_PORT_STRING = "8765";
68
protected String host;
70
protected String locatorURI;
71
protected InvokerLocator serverLocator;
72
protected Connector connector;
73
protected TestInvocationHandler invocationHandler;
75
protected void addExtraClientConfig(Map config) {}
76
protected void addExtraServerConfig(Map config) {}
79
public void setUp() throws Exception
83
String keyStoreFilePath = getClass().getResource("../.keystore").getFile();
84
System.setProperty("javax.net.ssl.keyStore", keyStoreFilePath);
85
System.setProperty("javax.net.ssl.keyStorePassword", "unit-tests-server");
86
String trustStoreFilePath = getClass().getResource("../.truststore").getFile();
87
System.setProperty("javax.net.ssl.trustStore", trustStoreFilePath);
88
System.setProperty("javax.net.ssl.trustStorePassword", "unit-tests-client");
94
protected String getServerSocketFactoryClassName()
96
return SSLTestServerSocketFactory.class.getName();
99
protected Constructor getServerSocketFactoryConstructor() throws NoSuchMethodException
101
return SSLTestServerSocketFactory.class.getConstructor(new Class[]{int.class, int.class});
104
protected String getSocketFactoryClassName()
106
return SSLTestSocketFactory.class.getName();
109
protected Constructor getSocketFactoryConstructor() throws NoSuchMethodException
111
return SSLTestSocketFactory.class.getConstructor(new Class[]{int.class, int.class});
114
static public class SSLTestServerSocketFactory extends ServerSocketFactory
117
ServerSocketFactory factory;
120
public SSLTestServerSocketFactory() throws IOException
123
this.initialWrites = -1;
126
public SSLTestServerSocketFactory(int timeout, int initialWrites) throws IOException
128
this.timeout = timeout;
129
this.initialWrites = initialWrites;
132
public ServerSocket createServerSocket() throws IOException
134
ServerSocket ss = null;
137
ss = SSLServerSocketFactory.getDefault().createServerSocket();
141
ss = new SSLTestServerSocket(timeout, initialWrites, ((SSLServerSocket) factory.createServerSocket()));
143
log.info("returning: " + ss);
146
public ServerSocket createServerSocket(int port) throws IOException
148
ServerSocket ss = null;
149
if (callbackTest && port != secondaryServerSocketPort)
151
ss = SSLServerSocketFactory.getDefault().createServerSocket(port);
155
ss = new SSLTestServerSocket(port, timeout, initialWrites, ((SSLServerSocket) factory.createServerSocket()));
157
log.info("returning: " + ss);
161
public ServerSocket createServerSocket(int port, int backlog) throws IOException
163
ServerSocket ss = null;
164
if (callbackTest && port != secondaryServerSocketPort)
166
ss = SSLServerSocketFactory.getDefault().createServerSocket(port, backlog);
170
ss = new SSLTestServerSocket(port, backlog, timeout, initialWrites, ((SSLServerSocket) factory.createServerSocket()));
172
log.info("returning: " + ss);
176
public ServerSocket createServerSocket(int port, int backlog, InetAddress ifAddress) throws IOException
178
ServerSocket ss = null;
179
if (callbackTest && port != secondaryServerSocketPort)
181
ss = SSLServerSocketFactory.getDefault().createServerSocket(port, backlog, ifAddress);
185
ss = new SSLTestServerSocket(port, backlog, ifAddress, timeout, initialWrites, ((SSLServerSocket) factory.createServerSocket()));
187
log.info("returning: " + ss);
191
protected void setupFactory() throws IOException
193
SSLSocketBuilder sslSocketBuilder = new SSLSocketBuilder();
194
sslSocketBuilder.setUseSSLServerSocketFactory(false);
195
factory = sslSocketBuilder.createSSLServerSocketFactory();
200
static class SSLTestServerSocket extends SSLServerSocket
204
SSLServerSocket serverSocket;
206
public SSLTestServerSocket(int timeout, int initialWrites, SSLServerSocket serverSocket) throws IOException
209
this.timeout = timeout;
210
this.initialWrites = initialWrites;
211
this.serverSocket = serverSocket;
213
public SSLTestServerSocket(int port, int timeout, int initialWrites, SSLServerSocket serverSocket) throws IOException
216
this.timeout = timeout;
217
this.initialWrites = initialWrites;
218
this.serverSocket = serverSocket;
219
bind(new InetSocketAddress(port), 50);
221
public SSLTestServerSocket(int port, int backlog, int timeout, int initialWrites, SSLServerSocket serverSocket) throws IOException
223
super(port, backlog);
224
this.timeout = timeout;
225
this.initialWrites = initialWrites;
226
this.serverSocket = serverSocket;
227
bind(new InetSocketAddress(port), backlog);
229
public SSLTestServerSocket(int port, int backlog, InetAddress bindAddr, int timeout, int initialWrites, SSLServerSocket serverSocket) throws IOException
231
super(port, backlog, bindAddr);
232
this.timeout = timeout;
233
this.initialWrites = initialWrites;
234
this.serverSocket = serverSocket;
235
bind(new InetSocketAddress(bindAddr, port), 50);
237
public Socket accept() throws IOException
239
SSLSocket s1 = (SSLSocket) serverSocket.accept();
240
Socket s2 = new SSLTestSocket(timeout, initialWrites, s1);
243
public void bind(SocketAddress endpoint, int backlog) throws IOException
245
log.info("serverSocket: " + serverSocket);
246
if (serverSocket != null) log.info("bound: " + serverSocket.isBound());
247
if (serverSocket != null && !serverSocket.isBound())
249
log.info("binding " + serverSocket);
250
serverSocket.bind(endpoint, backlog);
253
public String toString()
255
return "SSLTestServerSocket[" + serverSocket.toString() + "]";
257
public boolean getEnableSessionCreation()
259
return serverSocket.getEnableSessionCreation();
261
public String[] getEnabledCipherSuites()
263
return serverSocket.getEnabledCipherSuites();
265
public String[] getEnabledProtocols()
267
return serverSocket.getEnabledProtocols();
269
public boolean getNeedClientAuth()
271
return serverSocket.getNeedClientAuth();
273
public String[] getSupportedCipherSuites()
275
return serverSocket.getSupportedCipherSuites();
277
public String[] getSupportedProtocols()
279
return serverSocket.getSupportedProtocols();
281
public boolean getUseClientMode()
283
return serverSocket.getUseClientMode();
285
public boolean getWantClientAuth()
287
return serverSocket.getWantClientAuth();
289
public void setEnableSessionCreation(boolean arg0)
291
serverSocket.setEnableSessionCreation(arg0);
293
public void setEnabledCipherSuites(String[] arg0)
295
serverSocket.setEnabledCipherSuites(arg0);
297
public void setEnabledProtocols(String[] arg0)
299
serverSocket.setEnabledProtocols(arg0);
301
public void setNeedClientAuth(boolean arg0)
303
serverSocket.setNeedClientAuth(arg0);
305
public void setUseClientMode(boolean arg0)
307
serverSocket.setUseClientMode(arg0);
309
public void setWantClientAuth(boolean arg0)
311
serverSocket.setWantClientAuth(arg0);
316
public static class SSLTestSocketFactory extends SocketFactory
320
SocketFactory factory;
322
public SSLTestSocketFactory() throws IOException
328
public SSLTestSocketFactory(int timeout, int initialWrites) throws IOException
330
this.timeout = timeout;
331
this.initialWrites = initialWrites;
334
public Socket createSocket() throws IOException
336
log.info("callbackTest: " + callbackTest);
340
s = factory.createSocket();
344
s = new TestSocket(timeout, initialWrites);
345
s = new SSLTestSocket(timeout, initialWrites, ((SSLSocket) factory.createSocket()));
347
log.info(this + " returning " + s);
350
public Socket createSocket(String arg0, int arg1) throws IOException, UnknownHostException
352
log.info("callbackTest: " + callbackTest);
354
if (callbackTest && arg1 != secondaryServerSocketPort)
356
s = factory.createSocket(arg0, arg1);
360
s = new TestSocket(timeout, initialWrites);
361
s = new SSLTestSocket(arg0, arg1, timeout, initialWrites, ((SSLSocket) factory.createSocket()));
363
log.info(this + " returning " + s);
367
public Socket createSocket(InetAddress arg0, int arg1) throws IOException
369
log.info("callbackTest: " + callbackTest);
371
if (callbackTest && arg1 != secondaryServerSocketPort)
373
s = factory.createSocket(arg0, arg1);
377
s = new TestSocket(timeout, initialWrites);
378
s = new SSLTestSocket(arg0, arg1, timeout, initialWrites, ((SSLSocket) factory.createSocket()));
380
log.info(this + " returning " + s);
384
public Socket createSocket(String arg0, int arg1, InetAddress arg2, int arg3) throws IOException, UnknownHostException
386
log.info("callbackTest: " + callbackTest);
388
if (callbackTest && arg1 != secondaryServerSocketPort)
390
s = factory.createSocket(arg0, arg1);
394
s = new TestSocket(timeout, initialWrites);
395
s = new SSLTestSocket(arg0, arg1, arg2, arg3, timeout, initialWrites, ((SSLSocket) factory.createSocket()));
397
log.info(this + " returning " + s);
401
public Socket createSocket(InetAddress arg0, int arg1, InetAddress arg2, int arg3) throws IOException
403
log.info("callbackTest: " + callbackTest);
405
if (callbackTest && arg1 != secondaryServerSocketPort)
407
s = factory.createSocket(arg0, arg1);
411
s = new TestSocket(timeout, initialWrites);
412
s = new SSLTestSocket(arg0, arg1, arg2, arg3, timeout, initialWrites, ((SSLSocket) factory.createSocket()));
414
log.info(this + " returning " + s);
418
protected void setupFactory() throws IOException
420
SSLSocketBuilder sslSocketBuilder = new SSLSocketBuilder();
421
sslSocketBuilder.setUseSSLServerSocketFactory(false);
422
factory = sslSocketBuilder.createSSLSocketFactory();
426
static class SSLTestSocket extends SSLSocket
431
SocketAddress endpoint;
433
public SSLTestSocket(int timeout, int initialWrites, SSLSocket socket)
435
this.timeout = timeout;
436
this.initialWrites = initialWrites;
437
this.socket = socket;
439
public SSLTestSocket(String host, int port, int timeout, int initialWrites, SSLSocket socket) throws UnknownHostException, IOException
442
this.timeout = timeout;
443
this.initialWrites = initialWrites;
444
this.socket = socket;
445
connect(new InetSocketAddress(host, port), timeout);
447
public SSLTestSocket(InetAddress address, int port, int timeout, int initialWrites, SSLSocket socket) throws IOException
449
super(address, port);
450
this.timeout = timeout;
451
this.initialWrites = initialWrites;
452
this.socket = socket;
453
connect(new InetSocketAddress(address, port), timeout);
455
public SSLTestSocket(String host, int port, InetAddress localAddr, int localPort, int timeout, int initialWrites, SSLSocket socket) throws IOException
457
super(host, port, localAddr, localPort);
458
this.timeout = timeout;
459
this.initialWrites = initialWrites;
460
this.socket = socket;
461
bind(new InetSocketAddress(localAddr, localPort));
462
connect(new InetSocketAddress(host, port), timeout);
464
public SSLTestSocket(InetAddress address, int port, InetAddress localAddr, int localPort, int timeout, int initialWrites, SSLSocket socket) throws IOException
466
super(address, port, localAddr, localPort);
467
this.timeout = timeout;
468
this.initialWrites = initialWrites;
469
this.socket = socket;
470
bind(new InetSocketAddress(localAddr, localPort));
471
connect(new InetSocketAddress(address, port), timeout);
473
public String toString()
475
return "SSLTestSocket[" + socket.toString() + "]";
477
public InputStream getInputStream() throws IOException
479
return socket.getInputStream();
481
public OutputStream getOutputStream() throws IOException
483
return new TestOutputStream(socket.getOutputStream(), timeout, initialWrites);
485
public void addHandshakeCompletedListener(HandshakeCompletedListener listener)
487
socket.addHandshakeCompletedListener(listener);
489
public void bind(SocketAddress bindpoint) throws IOException
492
socket.bind(bindpoint);
494
public void connect(SocketAddress endpoint) throws IOException
497
socket.connect(endpoint);
499
public void connect(SocketAddress endpoint, int timeout) throws IOException
501
socket.connect(endpoint, timeout);
503
public boolean getEnableSessionCreation()
505
return socket.getEnableSessionCreation();
507
public String[] getEnabledCipherSuites()
509
return socket.getEnabledCipherSuites();
511
public String[] getEnabledProtocols()
513
return socket.getEnabledProtocols();
515
public InetAddress getInetAddress()
517
return socket.getInetAddress();
519
public boolean getNeedClientAuth()
521
return socket.getNeedClientAuth();
523
public SSLSession getSession()
525
return socket.getSession();
527
public String[] getSupportedCipherSuites()
529
return socket.getSupportedCipherSuites();
531
public String[] getSupportedProtocols()
533
return socket.getSupportedProtocols();
535
public boolean getUseClientMode()
537
return socket.getUseClientMode();
539
public boolean getWantClientAuth()
541
return socket.getWantClientAuth();
543
public void removeHandshakeCompletedListener(HandshakeCompletedListener listener)
545
socket.removeHandshakeCompletedListener(listener);
547
public void setEnableSessionCreation(boolean flag)
549
socket.setEnableSessionCreation(flag);
551
public void setEnabledCipherSuites(String[] suites)
553
socket.setEnabledCipherSuites(suites);
555
public void setEnabledProtocols(String[] protocols)
557
socket.setEnabledProtocols(protocols);
559
public void setNeedClientAuth(boolean need)
561
socket.setNeedClientAuth(need);
563
public void setUseClientMode(boolean mode)
565
socket.setUseClientMode(mode);
567
public void setWantClientAuth(boolean want)
569
socket.setWantClientAuth(want);
571
public void startHandshake() throws IOException
573
socket.startHandshake();
b'\\ No newline at end of file'