~ubuntu-branches/ubuntu/wily/libjboss-remoting-java/wily

« back to all changes in this revision

Viewing changes to src/tests/org/jboss/test/remoting/transport/socket/ssl/custom/InvokerClientTest.java

  • Committer: Package Import Robot
  • Author(s): Torsten Werner
  • Date: 2011-09-09 14:01:03 UTC
  • mfrom: (1.1.6 upstream)
  • Revision ID: package-import@ubuntu.com-20110909140103-hqokx61534tas9rg
Tags: 2.5.3.SP1-1
* Newer but not newest upstream release. Do not build samples.
* Change debian/watch to upstream's svn repo.
* Add patch to fix compile error caused by tomcat update.
  (Closes: #628303)
* Switch to source format 3.0.
* Switch to debhelper level 7.
* Remove useless Depends.
* Update Standards-Version: 3.9.2.
* Update README.source.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
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.
 
6
 *
 
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.
 
11
 *
 
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.
 
16
 *
 
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.
 
21
 */
 
22
 
 
23
package org.jboss.test.remoting.transport.socket.ssl.custom;
 
24
 
 
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;
 
41
 
 
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;
 
53
import java.util.Map;
 
54
 
 
55
 
 
56
/**
 
57
 * @author <a href="mailto:tom@jboss.com">Tom Elrod</a>
 
58
 * @version $Revision: 2901 $
 
59
 * <p>
 
60
 * Copyright (c) Mar 23, 2006
 
61
 * </p>
 
62
 */
 
63
public class InvokerClientTest extends TestCase implements SSLInvokerConstants
 
64
{
 
65
   private static final Logger log = Logger.getLogger(InvokerClientTest.class);
 
66
   
 
67
   protected Client client;
 
68
   
 
69
   public void init()
 
70
   {
 
71
      try
 
72
      {
 
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");
 
78
         
 
79
         InvokerLocator locator = new InvokerLocator(getTransport() + "://localhost:" + port);
 
80
         client = new Client(locator, config);
 
81
         client.connect();
 
82
      }
 
83
      catch(Exception e)
 
84
      {
 
85
         log.error(e.getMessage(), e);
 
86
      }
 
87
   }
 
88
   
 
89
   public void testRemoteCall() throws Throwable
 
90
   {
 
91
      log.debug("running testRemoteCall()");
 
92
      log.debug("client.getInvoker().getLocator()" + client.getInvoker().getLocator());
 
93
      
 
94
      // simple invoke, should return bar
 
95
      Object ret = makeInvocation("foo", "bar");
 
96
      assertTrue("Result of testRemoteCall() invocation of foo.", "bar".equals(ret));
 
97
      if("bar".equals(ret))
 
98
      {
 
99
         log.debug("PASS");
 
100
      }
 
101
      else
 
102
      {
 
103
         log.debug("FAILED");
 
104
      }
 
105
      assertEquals("bar", ret);
 
106
      
 
107
   }
 
108
   
 
109
   public void testSocketFactoryParameters()
 
110
   {
 
111
      try
 
112
      {
 
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");
 
118
         
 
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");
 
123
         
 
124
         InvokerLocator locator = new InvokerLocator(getTransport() + "://localhost:" + port);
 
125
         client = new Client(locator, config);
 
126
         client.connect();
 
127
         
 
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"));
 
133
      }
 
134
      catch (Throwable t)
 
135
      {
 
136
         log.error(t);
 
137
         t.printStackTrace();
 
138
         fail();
 
139
      }
 
140
      
 
141
   }
 
142
   
 
143
   public void testCallbacks()
 
144
   {
 
145
      try
 
146
      {
 
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();
 
153
 
 
154
         CallbackHandler callbackHandler = new CallbackHandler();
 
155
         String callbackHandleObject = "myCallbackHandleObject";
 
156
         client.addListener(callbackHandler, locator, callbackHandleObject);
 
157
         solicitCallback("abc");
 
158
         
 
159
         // need to wait for brief moment so server can callback
 
160
         Thread.sleep(2000);
 
161
         
 
162
         // remove callback handler from server
 
163
         client.removeListener(callbackHandler);
 
164
         
 
165
         // shut down callback server
 
166
         callbackConnector.stop();
 
167
         callbackConnector.destroy();
 
168
         callbackConnector = null;
 
169
         
 
170
         List callbacks = callbackHandler.getCallbacks();
 
171
         assertEquals(callbacks.size(), 1);
 
172
//         assertEquals(callbacks.get(0), "abc");
 
173
      }
 
174
      catch (Throwable t)
 
175
      {
 
176
         log.error(t);
 
177
         t.printStackTrace();
 
178
         fail();
 
179
      }
 
180
   }
 
181
   
 
182
   protected String getTransport()
 
183
   {
 
184
      return transport;
 
185
   }
 
186
   
 
187
   protected Object makeInvocation(String method, String param) throws Throwable
 
188
   {
 
189
      Object ret = client.invoke(new NameBasedInvocation(method,
 
190
            new Object[]{param},
 
191
            new String[]{String.class.getName()}),
 
192
            null);
 
193
      
 
194
      return ret;
 
195
   }
 
196
   
 
197
   protected Object solicitCallback(String param) throws Throwable
 
198
   {
 
199
      Object ret = client.invoke(new NameBasedInvocation("test",
 
200
            new Object[]{param},
 
201
            new String[]{String.class.getName()}),
 
202
            null);
 
203
      
 
204
      return ret;
 
205
   }
 
206
   
 
207
   public void setUp() throws Exception
 
208
   {
 
209
      init();
 
210
   }
 
211
   
 
212
   public void tearDown() throws Exception
 
213
   {
 
214
      if(client != null)
 
215
      {
 
216
         client.disconnect();
 
217
         client = null;
 
218
      }
 
219
   }
 
220
   
 
221
   
 
222
   protected ServerSocketFactory createServerSocketFactory()
 
223
   throws NoSuchAlgorithmException, KeyManagementException, IOException,
 
224
   CertificateException, UnrecoverableKeyException, KeyStoreException
 
225
   {
 
226
      ServerSocketFactory serverSocketFactory = null;
 
227
      
 
228
      SSLSocketBuilder server = new SSLSocketBuilder();
 
229
      server.setUseSSLServerSocketFactory(false);
 
230
      
 
231
      server.setSecureSocketProtocol("SSL");
 
232
      server.setTrustStoreAlgorithm("SunX509");
 
233
      
 
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);
 
241
      /*
 
242
       * This is optional since if not set, will use
 
243
       * the key store password (and are the same in this case)
 
244
       */
 
245
      //server.setKeyPassword("unit-tests-server");
 
246
      
 
247
      serverSocketFactory = server.createSSLServerSocketFactory();
 
248
      
 
249
      return serverSocketFactory;
 
250
   }
 
251
   
 
252
   
 
253
   public static class SampleInvocationHandler implements ServerInvocationHandler
 
254
   {
 
255
      public SampleInvocationHandler()
 
256
      {   
 
257
      }
 
258
      
 
259
      public Object invoke(InvocationRequest invocation) throws Throwable
 
260
      {
 
261
         return new Integer(0);
 
262
      }
 
263
      
 
264
      public void addListener(InvokerCallbackHandler callbackHandler)
 
265
      {
 
266
         System.out.println("entering addListener()");
 
267
         
 
268
         try
 
269
         {
 
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");
 
276
         }
 
277
         catch(Exception e)
 
278
         {
 
279
            e.printStackTrace();
 
280
         }
 
281
      }
 
282
      
 
283
      public void removeListener(InvokerCallbackHandler callbackHandler)
 
284
      {
 
285
      }
 
286
      
 
287
      public void setMBeanServer(MBeanServer server)
 
288
      {
 
289
         // NO OP as do not need reference to MBeanServer for this handler
 
290
      }
 
291
      
 
292
      public void setInvoker(ServerInvoker invoker)
 
293
      {
 
294
         // NO OP as do not need reference back to the server invoker
 
295
      }
 
296
   }
 
297
   
 
298
   
 
299
   public static class CallbackHandler implements InvokerCallbackHandler
 
300
   {
 
301
      private ArrayList callbacks = new ArrayList();
 
302
      
 
303
      public CallbackHandler()
 
304
      {
 
305
      }
 
306
      
 
307
      public ArrayList getCallbacks()
 
308
      {
 
309
         return callbacks;
 
310
      }
 
311
      
 
312
      /**
 
313
       * Will take the callback and print out its values.
 
314
       *
 
315
       * @param callback
 
316
       * @throws org.jboss.remoting.callback.HandleCallbackException
 
317
       *
 
318
       */
 
319
      public void handleCallback(Callback callback) throws HandleCallbackException
 
320
      {
 
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);
 
325
      }
 
326
   }
 
327
}