2
* JBoss, Home of Professional Open Source
3
* Copyright 2005, 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.callback.sslsocketfactory;
24
import java.net.InetAddress;
25
import java.net.ServerSocket;
26
import java.util.HashMap;
30
import javax.management.MBeanServer;
31
import javax.net.ServerSocketFactory;
32
import javax.net.ssl.SSLServerSocket;
33
import javax.net.ssl.SSLServerSocketFactory;
35
import junit.framework.TestCase;
37
import org.apache.log4j.ConsoleAppender;
38
import org.apache.log4j.Level;
39
import org.apache.log4j.Logger;
40
import org.apache.log4j.PatternLayout;
41
import org.jboss.logging.XLevel;
42
import org.jboss.remoting.Client;
43
import org.jboss.remoting.InvocationRequest;
44
import org.jboss.remoting.InvokerLocator;
45
import org.jboss.remoting.ServerInvocationHandler;
46
import org.jboss.remoting.ServerInvoker;
47
import org.jboss.remoting.callback.Callback;
48
import org.jboss.remoting.callback.HandleCallbackException;
49
import org.jboss.remoting.callback.InvokerCallbackHandler;
50
import org.jboss.remoting.security.SSLSocketBuilder;
51
import org.jboss.remoting.transport.Connector;
52
import org.jboss.remoting.transport.PortUtil;
55
abstract public class SSLSocketFactoryTestParent extends TestCase
57
private static Logger log = Logger.getLogger(SSLSocketFactoryTestParent.class);
59
private static boolean firstTime = true;
61
protected String host;
63
protected String locatorURI;
64
protected InvokerLocator serverLocator;
65
protected Connector connector;
66
protected TestInvocationHandler invocationHandler;
69
public void setUp() throws Exception
74
Logger.getLogger("org.jboss.remoting").setLevel(XLevel.INFO);
75
Logger.getLogger("org.jboss.test.remoting").setLevel(Level.INFO);
76
String pattern = "[%d{ABSOLUTE}] [%t] %5p (%F:%L) - %m%n";
77
PatternLayout layout = new PatternLayout(pattern);
78
ConsoleAppender consoleAppender = new ConsoleAppender(layout);
79
Logger.getRootLogger().addAppender(consoleAppender);
84
public void tearDown()
89
public void testSSLServerSocketFactory() throws Throwable
91
log.info("entering " + getName());
97
InvokerLocator clientLocator = new InvokerLocator(locatorURI);
98
HashMap clientConfig = new HashMap();
99
clientConfig.put(SSLSocketBuilder.REMOTING_TRUST_STORE_TYPE, "JKS");
100
String trustStoreFilePath = this.getClass().getResource("truststore").getFile();
101
clientConfig.put(SSLSocketBuilder.REMOTING_TRUST_STORE_FILE_PATH, trustStoreFilePath);
102
clientConfig.put(SSLSocketBuilder.REMOTING_TRUST_STORE_PASSWORD, "unit-tests-client");
103
clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
104
addExtraClientConfig(clientConfig);
105
Client client = new Client(clientLocator, clientConfig);
107
log.info("client is connected");
110
assertEquals("abc", client.invoke("abc"));
111
log.info("connection is good");
113
// Add callback listener.
114
TestCallbackHandler callbackHandler = new TestCallbackHandler();
115
client.addListener(callbackHandler, null, null, true);
117
// Test callback SSLServerSocketFactory.
118
Set callbackConnectors = client.getCallbackConnectors(callbackHandler);
119
assertEquals(1, callbackConnectors.size());
120
Connector callbackConnector = (Connector) callbackConnectors.iterator().next();
121
ServerInvoker callbackServerInvoker = callbackConnector.getServerInvoker();
122
ServerSocketFactory serverSocketFactory = callbackServerInvoker.getServerSocketFactory();
123
assertTrue(serverSocketFactory instanceof SSLServerSocketFactory);
124
InetAddress address = InetAddress.getLocalHost();
125
int port = PortUtil.findFreePort(address.getHostAddress());
126
ServerSocket serverSocket = serverSocketFactory.createServerSocket(port, 100, address);
127
SSLServerSocket sslServerSocket = (SSLServerSocket) serverSocket;
128
assertTrue(sslServerSocket.getUseClientMode());
130
client.removeListener(callbackHandler);
133
log.info(getName() + " PASSES");
137
abstract protected String getTransport();
140
protected void addExtraClientConfig(Map config) {}
141
protected void addExtraServerConfig(Map config) {}
144
protected void setupServer() throws Exception
146
host = InetAddress.getLocalHost().getHostAddress();
147
port = PortUtil.findFreePort(host);
148
locatorURI = getTransport() + "://" + host + ":" + port;
149
serverLocator = new InvokerLocator(locatorURI);
150
log.info("Starting remoting server with locator uri of: " + locatorURI);
151
HashMap config = new HashMap();
152
config.put(SSLSocketBuilder.REMOTING_KEY_STORE_TYPE, "JKS");
153
String keyStoreFilePath = this.getClass().getResource("keystore").getFile();
154
config.put(SSLSocketBuilder.REMOTING_KEY_STORE_FILE_PATH, keyStoreFilePath);
155
config.put(SSLSocketBuilder.REMOTING_KEY_STORE_PASSWORD, "unit-tests-server");
156
config.put(InvokerLocator.FORCE_REMOTE, "true");
157
addExtraServerConfig(config);
158
connector = new Connector(serverLocator, config);
160
invocationHandler = new TestInvocationHandler();
161
connector.addInvocationHandler("test", invocationHandler);
166
protected void shutdownServer() throws Exception
168
if (connector != null)
173
static class TestInvocationHandler implements ServerInvocationHandler
175
public void addListener(InvokerCallbackHandler callbackHandler) {}
176
public Object invoke(final InvocationRequest invocation) throws Throwable
178
return invocation.getParameter();
180
public void removeListener(InvokerCallbackHandler callbackHandler) {}
181
public void setMBeanServer(MBeanServer server) {}
182
public void setInvoker(ServerInvoker invoker) {}
186
static class TestCallbackHandler implements InvokerCallbackHandler
188
public void handleCallback(Callback callback) throws HandleCallbackException
190
log.info("received callback");
b'\\ No newline at end of file'