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.
23
package org.jboss.test.remoting.transport.socket.ssl.custom;
25
import junit.framework.TestCase;
26
import org.jboss.logging.Logger;
27
import org.jboss.remoting.Client;
28
import org.jboss.remoting.InvocationRequest;
29
import org.jboss.remoting.InvokerLocator;
30
import org.jboss.remoting.RemoteClientInvoker;
31
import org.jboss.remoting.ServerInvocationHandler;
32
import org.jboss.remoting.ServerInvoker;
33
import org.jboss.remoting.callback.Callback;
34
import org.jboss.remoting.callback.HandleCallbackException;
35
import org.jboss.remoting.callback.InvokerCallbackHandler;
36
import org.jboss.remoting.invocation.NameBasedInvocation;
37
import org.jboss.remoting.security.CustomSSLSocketFactory;
38
import org.jboss.remoting.security.SSLSocketBuilder;
39
import org.jboss.remoting.transport.Connector;
40
import org.jboss.test.remoting.transport.socket.ssl.SSLInvokerConstants;
42
import javax.management.MBeanServer;
43
import javax.net.ServerSocketFactory;
44
import java.io.IOException;
45
import java.security.KeyManagementException;
46
import java.security.KeyStoreException;
47
import java.security.NoSuchAlgorithmException;
48
import java.security.UnrecoverableKeyException;
49
import java.security.cert.CertificateException;
50
import java.util.ArrayList;
51
import java.util.HashMap;
52
import java.util.List;
57
* @author <a href="mailto:tom@jboss.com">Tom Elrod</a>
58
* @version $Revision: 2901 $
60
* Copyright (c) Mar 23, 2006
63
public class InvokerClientTest extends TestCase implements SSLInvokerConstants
65
private static final Logger log = Logger.getLogger(InvokerClientTest.class);
67
protected Client client;
73
Map config = new HashMap();
74
config.put(SSLSocketBuilder.REMOTING_TRUST_STORE_TYPE, "JKS");
75
String trustStoreFilePath = this.getClass().getResource("../.truststore").getFile();
76
config.put(SSLSocketBuilder.REMOTING_TRUST_STORE_FILE_PATH, trustStoreFilePath);
77
config.put(SSLSocketBuilder.REMOTING_TRUST_STORE_PASSWORD, "unit-tests-client");
79
InvokerLocator locator = new InvokerLocator(getTransport() + "://localhost:" + port);
80
client = new Client(locator, config);
85
log.error(e.getMessage(), e);
89
public void testRemoteCall() throws Throwable
91
log.debug("running testRemoteCall()");
92
log.debug("client.getInvoker().getLocator()" + client.getInvoker().getLocator());
94
// simple invoke, should return bar
95
Object ret = makeInvocation("foo", "bar");
96
assertTrue("Result of testRemoteCall() invocation of foo.", "bar".equals(ret));
105
assertEquals("bar", ret);
109
public void testSocketFactoryParameters()
113
Map config = new HashMap();
114
config.put(SSLSocketBuilder.REMOTING_TRUST_STORE_TYPE, "JKS");
115
String trustStoreFilePath = this.getClass().getResource("../.truststore").getFile();
116
config.put(SSLSocketBuilder.REMOTING_TRUST_STORE_FILE_PATH, trustStoreFilePath);
117
config.put(SSLSocketBuilder.REMOTING_TRUST_STORE_PASSWORD, "unit-tests-client");
119
config.put(SSLSocketBuilder.REMOTING_KEY_STORE_TYPE, "JKS");
120
String keyStoreFilePath = this.getClass().getResource("../.keystore").getFile();
121
config.put(SSLSocketBuilder.REMOTING_KEY_STORE_FILE_PATH, keyStoreFilePath);
122
config.put(SSLSocketBuilder.REMOTING_KEY_STORE_PASSWORD, "unit-tests-server");
124
InvokerLocator locator = new InvokerLocator(getTransport() + "://localhost:" + port);
125
client = new Client(locator, config);
128
// Test that parameters are properly set.
129
RemoteClientInvoker clientInvoker = (RemoteClientInvoker) client.getInvoker();
130
CustomSSLSocketFactory socketFactory = (CustomSSLSocketFactory) clientInvoker.getSocketFactory();
131
assertTrue(socketFactory.getSSLSocketBuilder().getTrustStoreType().equals("JKS"));
132
// assertTrue(socketFactory.gett.equals("JKS"));
143
public void testCallbacks()
147
InvokerLocator locator = new InvokerLocator(getTransport() + "://localhost:" + callbackPort);
148
Connector callbackConnector = new Connector(locator.getLocatorURI());
149
callbackConnector.setServerSocketFactory(createServerSocketFactory());
150
callbackConnector.create();
151
callbackConnector.addInvocationHandler("sample", new SampleInvocationHandler());
152
callbackConnector.start();
154
CallbackHandler callbackHandler = new CallbackHandler();
155
String callbackHandleObject = "myCallbackHandleObject";
156
client.addListener(callbackHandler, locator, callbackHandleObject);
157
solicitCallback("abc");
159
// need to wait for brief moment so server can callback
162
// remove callback handler from server
163
client.removeListener(callbackHandler);
165
// shut down callback server
166
callbackConnector.stop();
167
callbackConnector.destroy();
168
callbackConnector = null;
170
List callbacks = callbackHandler.getCallbacks();
171
assertEquals(callbacks.size(), 1);
172
// assertEquals(callbacks.get(0), "abc");
182
protected String getTransport()
187
protected Object makeInvocation(String method, String param) throws Throwable
189
Object ret = client.invoke(new NameBasedInvocation(method,
191
new String[]{String.class.getName()}),
197
protected Object solicitCallback(String param) throws Throwable
199
Object ret = client.invoke(new NameBasedInvocation("test",
201
new String[]{String.class.getName()}),
207
public void setUp() throws Exception
212
public void tearDown() throws Exception
222
protected ServerSocketFactory createServerSocketFactory()
223
throws NoSuchAlgorithmException, KeyManagementException, IOException,
224
CertificateException, UnrecoverableKeyException, KeyStoreException
226
ServerSocketFactory serverSocketFactory = null;
228
SSLSocketBuilder server = new SSLSocketBuilder();
229
server.setUseSSLServerSocketFactory(false);
231
server.setSecureSocketProtocol("SSL");
232
server.setTrustStoreAlgorithm("SunX509");
234
server.setTrustStoreType("JKS");
235
String trustStoreFilePath = this.getClass().getResource("../.truststore").getFile();
236
server.setTrustStoreURL(trustStoreFilePath);
237
server.setTrustStorePassword("unit-tests-client");
238
// server.setUseClientMode(true);
239
server.setServerSocketUseClientMode(true);
240
server.setSocketUseClientMode(false);
242
* This is optional since if not set, will use
243
* the key store password (and are the same in this case)
245
//server.setKeyPassword("unit-tests-server");
247
serverSocketFactory = server.createSSLServerSocketFactory();
249
return serverSocketFactory;
253
public static class SampleInvocationHandler implements ServerInvocationHandler
255
public SampleInvocationHandler()
259
public Object invoke(InvocationRequest invocation) throws Throwable
261
return new Integer(0);
264
public void addListener(InvokerCallbackHandler callbackHandler)
266
System.out.println("entering addListener()");
270
Callback callback = new Callback(new Integer(1));
271
callbackHandler.handleCallback(callback);
272
System.out.println("sent first callback");
273
callback = new Callback(new Integer(2));
274
callbackHandler.handleCallback(callback);
275
System.out.println("sent second callback");
283
public void removeListener(InvokerCallbackHandler callbackHandler)
287
public void setMBeanServer(MBeanServer server)
289
// NO OP as do not need reference to MBeanServer for this handler
292
public void setInvoker(ServerInvoker invoker)
294
// NO OP as do not need reference back to the server invoker
299
public static class CallbackHandler implements InvokerCallbackHandler
301
private ArrayList callbacks = new ArrayList();
303
public CallbackHandler()
307
public ArrayList getCallbacks()
313
* Will take the callback and print out its values.
316
* @throws org.jboss.remoting.callback.HandleCallbackException
319
public void handleCallback(Callback callback) throws HandleCallbackException
321
log.info("Received push callback.");
322
log.info("Received callback value of: " + callback.getCallbackObject());
323
log.info("Received callback server invoker of: " + callback.getServerLocator());
324
callbacks.add(callback);