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

« back to all changes in this revision

Viewing changes to tests/org/jboss/test/remoting/callback/sslsocketfactory/SSLSocketFactoryTestParent.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
 
package org.jboss.test.remoting.callback.sslsocketfactory;
23
 
 
24
 
import java.net.InetAddress;
25
 
import java.net.ServerSocket;
26
 
import java.util.HashMap;
27
 
import java.util.Map;
28
 
import java.util.Set;
29
 
 
30
 
import javax.management.MBeanServer;
31
 
import javax.net.ServerSocketFactory;
32
 
import javax.net.ssl.SSLServerSocket;
33
 
import javax.net.ssl.SSLServerSocketFactory;
34
 
 
35
 
import junit.framework.TestCase;
36
 
 
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;
53
 
 
54
 
 
55
 
abstract public class SSLSocketFactoryTestParent extends TestCase
56
 
{
57
 
   private static Logger log = Logger.getLogger(SSLSocketFactoryTestParent.class);
58
 
   
59
 
   private static boolean firstTime = true;
60
 
   
61
 
   protected String host;
62
 
   protected int port;
63
 
   protected String locatorURI;
64
 
   protected InvokerLocator serverLocator;
65
 
   protected Connector connector;
66
 
   protected TestInvocationHandler invocationHandler;
67
 
 
68
 
   
69
 
   public void setUp() throws Exception
70
 
   {
71
 
      if (firstTime)
72
 
      {
73
 
         firstTime = false;
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);  
80
 
      }
81
 
   }
82
 
 
83
 
   
84
 
   public void tearDown()
85
 
   {
86
 
   }
87
 
   
88
 
   
89
 
   public void testSSLServerSocketFactory() throws Throwable
90
 
   {
91
 
      log.info("entering " + getName());
92
 
      
93
 
      // Start server.
94
 
      setupServer();
95
 
      
96
 
      // Create client.
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);
106
 
      client.connect();
107
 
      log.info("client is connected");
108
 
      
109
 
      // Test connections.
110
 
      assertEquals("abc", client.invoke("abc"));
111
 
      log.info("connection is good");
112
 
      
113
 
      // Add callback listener.
114
 
      TestCallbackHandler callbackHandler = new TestCallbackHandler();
115
 
      client.addListener(callbackHandler, null, null, true);
116
 
      
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());
129
 
      
130
 
      client.removeListener(callbackHandler);
131
 
      client.disconnect();
132
 
      shutdownServer();
133
 
      log.info(getName() + " PASSES");
134
 
   }
135
 
   
136
 
   
137
 
   abstract protected String getTransport();
138
 
   
139
 
   
140
 
   protected void addExtraClientConfig(Map config) {}
141
 
   protected void addExtraServerConfig(Map config) {}
142
 
   
143
 
 
144
 
   protected void setupServer() throws Exception
145
 
   {
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);
159
 
      connector.create();
160
 
      invocationHandler = new TestInvocationHandler();
161
 
      connector.addInvocationHandler("test", invocationHandler);
162
 
      connector.start();
163
 
   }
164
 
   
165
 
   
166
 
   protected void shutdownServer() throws Exception
167
 
   {
168
 
      if (connector != null)
169
 
         connector.stop();
170
 
   }
171
 
   
172
 
   
173
 
   static class TestInvocationHandler implements ServerInvocationHandler
174
 
   {
175
 
      public void addListener(InvokerCallbackHandler callbackHandler) {}
176
 
      public Object invoke(final InvocationRequest invocation) throws Throwable
177
 
      {
178
 
         return invocation.getParameter();
179
 
      }
180
 
      public void removeListener(InvokerCallbackHandler callbackHandler) {}
181
 
      public void setMBeanServer(MBeanServer server) {}
182
 
      public void setInvoker(ServerInvoker invoker) {}
183
 
   }
184
 
   
185
 
   
186
 
   static class TestCallbackHandler implements InvokerCallbackHandler
187
 
   {
188
 
      public void handleCallback(Callback callback) throws HandleCallbackException
189
 
      {
190
 
         log.info("received callback");
191
 
      }  
192
 
   }
193
 
}
 
 
b'\\ No newline at end of file'