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

« back to all changes in this revision

Viewing changes to tests/org/jboss/test/remoting/transport/socket/timeout/SSLWriteTimeoutTestParent.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 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.
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.transport.socket.timeout;
23
 
 
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;
34
 
import java.util.Map;
35
 
 
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;
43
 
 
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;
48
 
 
49
 
 
50
 
/**
51
 
 * Unit tests for JBREM-1120.
52
 
 * 
53
 
 * @author <a href="ron.sigal@jboss.com">Ron Sigal</a>
54
 
 * @version $Rev$
55
 
 * <p>
56
 
 * Copyright Apr 22, 2009
57
 
 * </p>
58
 
 */
59
 
public abstract class SSLWriteTimeoutTestParent extends WriteTimeoutTestParent
60
 
{
61
 
   private static Logger log = Logger.getLogger(SSLWriteTimeoutTestParent.class);
62
 
   
63
 
   private static boolean firstTime = true;
64
 
   
65
 
   protected static int SECONDARY_SERVER_SOCKET_PORT = 8765;
66
 
   protected static String SECONDARY_SERVER_SOCKET_PORT_STRING = "8765";
67
 
   
68
 
   protected String host;
69
 
   protected int port;
70
 
   protected String locatorURI;
71
 
   protected InvokerLocator serverLocator;
72
 
   protected Connector connector;
73
 
   protected TestInvocationHandler invocationHandler;
74
 
   
75
 
   protected void addExtraClientConfig(Map config) {}
76
 
   protected void addExtraServerConfig(Map config) {}
77
 
   
78
 
   
79
 
   public void setUp() throws Exception
80
 
   {
81
 
      if (firstTime)
82
 
      {
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");
89
 
      }
90
 
      super.setUp();
91
 
   }
92
 
   
93
 
   
94
 
   protected String getServerSocketFactoryClassName()
95
 
   {
96
 
      return SSLTestServerSocketFactory.class.getName();
97
 
   }
98
 
   
99
 
   protected Constructor getServerSocketFactoryConstructor() throws NoSuchMethodException
100
 
   {
101
 
      return SSLTestServerSocketFactory.class.getConstructor(new Class[]{int.class, int.class});
102
 
   }
103
 
   
104
 
   protected String getSocketFactoryClassName()
105
 
   {
106
 
      return SSLTestSocketFactory.class.getName();
107
 
   }
108
 
   
109
 
   protected Constructor getSocketFactoryConstructor() throws NoSuchMethodException
110
 
   {
111
 
      return SSLTestSocketFactory.class.getConstructor(new Class[]{int.class, int.class});
112
 
   }
113
 
   
114
 
   static public class SSLTestServerSocketFactory extends ServerSocketFactory
115
 
   {
116
 
      int timeout;
117
 
      ServerSocketFactory factory;
118
 
      int initialWrites;
119
 
      
120
 
      public SSLTestServerSocketFactory() throws IOException
121
 
      {
122
 
         this.timeout = 5000;
123
 
         this.initialWrites = -1;
124
 
         setupFactory();
125
 
      }      
126
 
      public SSLTestServerSocketFactory(int timeout, int initialWrites) throws IOException
127
 
      {
128
 
         this.timeout = timeout;
129
 
         this.initialWrites = initialWrites;
130
 
         setupFactory();
131
 
      }
132
 
      public ServerSocket createServerSocket() throws IOException
133
 
      {
134
 
         ServerSocket ss = null;
135
 
         if (callbackTest)
136
 
         {
137
 
            ss = SSLServerSocketFactory.getDefault().createServerSocket();
138
 
         }
139
 
         else
140
 
         {
141
 
            ss = new SSLTestServerSocket(timeout, initialWrites, ((SSLServerSocket) factory.createServerSocket()));
142
 
         }
143
 
         log.info("returning: " + ss);
144
 
         return ss;
145
 
      }
146
 
      public ServerSocket createServerSocket(int port) throws IOException
147
 
      {
148
 
         ServerSocket ss = null;
149
 
         if (callbackTest && port != secondaryServerSocketPort)
150
 
         {
151
 
            ss = SSLServerSocketFactory.getDefault().createServerSocket(port);
152
 
         }
153
 
         else
154
 
         {
155
 
            ss = new SSLTestServerSocket(port, timeout, initialWrites, ((SSLServerSocket) factory.createServerSocket()));
156
 
         }
157
 
         log.info("returning: " + ss);
158
 
         return ss;
159
 
      }
160
 
 
161
 
      public ServerSocket createServerSocket(int port, int backlog) throws IOException
162
 
      {
163
 
         ServerSocket ss = null;
164
 
         if (callbackTest && port != secondaryServerSocketPort)
165
 
         {
166
 
            ss = SSLServerSocketFactory.getDefault().createServerSocket(port, backlog);
167
 
         }
168
 
         else
169
 
         {
170
 
            ss = new SSLTestServerSocket(port, backlog, timeout, initialWrites, ((SSLServerSocket) factory.createServerSocket()));
171
 
         }
172
 
         log.info("returning: " + ss);
173
 
         return ss;
174
 
      }
175
 
 
176
 
      public ServerSocket createServerSocket(int port, int backlog, InetAddress ifAddress) throws IOException
177
 
      {
178
 
         ServerSocket ss = null;
179
 
         if (callbackTest && port != secondaryServerSocketPort)
180
 
         {
181
 
            ss = SSLServerSocketFactory.getDefault().createServerSocket(port, backlog, ifAddress);
182
 
         }
183
 
         else
184
 
         {
185
 
            ss = new SSLTestServerSocket(port, backlog, ifAddress, timeout, initialWrites, ((SSLServerSocket) factory.createServerSocket()));
186
 
         }
187
 
         log.info("returning: " + ss);
188
 
         return ss;
189
 
      }
190
 
      
191
 
      protected void setupFactory() throws IOException
192
 
      {
193
 
         SSLSocketBuilder sslSocketBuilder = new SSLSocketBuilder();
194
 
         sslSocketBuilder.setUseSSLServerSocketFactory(false);
195
 
         factory = sslSocketBuilder.createSSLServerSocketFactory();
196
 
      }
197
 
   }
198
 
   
199
 
   
200
 
   static class SSLTestServerSocket extends SSLServerSocket
201
 
   {
202
 
      int timeout;
203
 
      int initialWrites;
204
 
      SSLServerSocket serverSocket;
205
 
 
206
 
      public SSLTestServerSocket(int timeout, int initialWrites, SSLServerSocket serverSocket) throws IOException
207
 
      {
208
 
         super();
209
 
         this.timeout = timeout;
210
 
         this.initialWrites = initialWrites;
211
 
         this.serverSocket = serverSocket;
212
 
      }
213
 
      public SSLTestServerSocket(int port, int timeout, int initialWrites, SSLServerSocket serverSocket) throws IOException
214
 
      {
215
 
         super(port);
216
 
         this.timeout = timeout;
217
 
         this.initialWrites = initialWrites;
218
 
         this.serverSocket = serverSocket;
219
 
         bind(new InetSocketAddress(port), 50);
220
 
      }
221
 
      public SSLTestServerSocket(int port, int backlog, int timeout, int initialWrites, SSLServerSocket serverSocket) throws IOException
222
 
      {
223
 
         super(port, backlog);
224
 
         this.timeout = timeout;
225
 
         this.initialWrites = initialWrites;
226
 
         this.serverSocket = serverSocket;
227
 
         bind(new InetSocketAddress(port), backlog);
228
 
      }
229
 
      public SSLTestServerSocket(int port, int backlog, InetAddress bindAddr, int timeout, int initialWrites, SSLServerSocket serverSocket) throws IOException
230
 
      {
231
 
         super(port, backlog, bindAddr);
232
 
         this.timeout = timeout;
233
 
         this.initialWrites = initialWrites;
234
 
         this.serverSocket = serverSocket;
235
 
         bind(new InetSocketAddress(bindAddr, port), 50);
236
 
      }
237
 
      public Socket accept() throws IOException
238
 
      {
239
 
         SSLSocket s1 = (SSLSocket) serverSocket.accept();
240
 
         Socket s2 = new SSLTestSocket(timeout, initialWrites, s1);
241
 
         return s2;
242
 
      }
243
 
      public void bind(SocketAddress endpoint, int backlog) throws IOException
244
 
      {
245
 
         log.info("serverSocket: " + serverSocket);
246
 
         if (serverSocket != null) log.info("bound: " + serverSocket.isBound());
247
 
         if (serverSocket != null && !serverSocket.isBound())
248
 
         {
249
 
            log.info("binding " + serverSocket);
250
 
            serverSocket.bind(endpoint, backlog);
251
 
         }
252
 
      }
253
 
      public String toString()
254
 
      {
255
 
         return "SSLTestServerSocket[" + serverSocket.toString() + "]";
256
 
      }
257
 
      public boolean getEnableSessionCreation()
258
 
      {
259
 
         return serverSocket.getEnableSessionCreation();
260
 
      }
261
 
      public String[] getEnabledCipherSuites()
262
 
      {
263
 
         return serverSocket.getEnabledCipherSuites();
264
 
      }
265
 
      public String[] getEnabledProtocols()
266
 
      {
267
 
         return serverSocket.getEnabledProtocols();
268
 
      }
269
 
      public boolean getNeedClientAuth()
270
 
      {
271
 
         return serverSocket.getNeedClientAuth();
272
 
      }
273
 
      public String[] getSupportedCipherSuites()
274
 
      {
275
 
         return serverSocket.getSupportedCipherSuites();
276
 
      }
277
 
      public String[] getSupportedProtocols()
278
 
      {
279
 
         return serverSocket.getSupportedProtocols();
280
 
      }
281
 
      public boolean getUseClientMode()
282
 
      {
283
 
         return serverSocket.getUseClientMode();
284
 
      }
285
 
      public boolean getWantClientAuth()
286
 
      {
287
 
         return serverSocket.getWantClientAuth();
288
 
      }
289
 
      public void setEnableSessionCreation(boolean arg0)
290
 
      {
291
 
         serverSocket.setEnableSessionCreation(arg0);
292
 
      }
293
 
      public void setEnabledCipherSuites(String[] arg0)
294
 
      {
295
 
         serverSocket.setEnabledCipherSuites(arg0);
296
 
      }
297
 
      public void setEnabledProtocols(String[] arg0)
298
 
      {
299
 
         serverSocket.setEnabledProtocols(arg0);
300
 
      }
301
 
      public void setNeedClientAuth(boolean arg0)
302
 
      {
303
 
         serverSocket.setNeedClientAuth(arg0);
304
 
      }
305
 
      public void setUseClientMode(boolean arg0)
306
 
      {
307
 
         serverSocket.setUseClientMode(arg0);
308
 
      }
309
 
      public void setWantClientAuth(boolean arg0)
310
 
      {
311
 
         serverSocket.setWantClientAuth(arg0);
312
 
      }
313
 
   }
314
 
   
315
 
   
316
 
   public static class SSLTestSocketFactory extends SocketFactory
317
 
   {
318
 
      int timeout;
319
 
      int initialWrites;
320
 
      SocketFactory factory;
321
 
      
322
 
      public SSLTestSocketFactory() throws IOException
323
 
      {
324
 
         timeout = 5000;
325
 
         initialWrites = -1;
326
 
         setupFactory();
327
 
      }
328
 
      public SSLTestSocketFactory(int timeout, int initialWrites) throws IOException
329
 
      {
330
 
         this.timeout = timeout;
331
 
         this.initialWrites = initialWrites;
332
 
         setupFactory();
333
 
      }
334
 
      public Socket createSocket() throws IOException
335
 
      {
336
 
         log.info("callbackTest: " + callbackTest);
337
 
         Socket s = null;
338
 
         if (callbackTest)
339
 
         {
340
 
            s = factory.createSocket();
341
 
         }
342
 
         else
343
 
         {
344
 
            s = new TestSocket(timeout, initialWrites);
345
 
            s = new SSLTestSocket(timeout, initialWrites, ((SSLSocket) factory.createSocket()));
346
 
         }
347
 
         log.info(this + " returning " + s);
348
 
         return s;
349
 
      }
350
 
      public Socket createSocket(String arg0, int arg1) throws IOException, UnknownHostException
351
 
      {
352
 
         log.info("callbackTest: " + callbackTest);
353
 
         Socket s = null;
354
 
         if (callbackTest && arg1 != secondaryServerSocketPort)
355
 
         {
356
 
            s = factory.createSocket(arg0, arg1);
357
 
         }
358
 
         else
359
 
         {
360
 
            s = new TestSocket(timeout, initialWrites);
361
 
            s = new SSLTestSocket(arg0, arg1, timeout, initialWrites, ((SSLSocket) factory.createSocket()));
362
 
         }
363
 
         log.info(this + " returning " + s);
364
 
         return s;
365
 
      }
366
 
 
367
 
      public Socket createSocket(InetAddress arg0, int arg1) throws IOException
368
 
      {
369
 
         log.info("callbackTest: " + callbackTest);
370
 
         Socket s = null;
371
 
         if (callbackTest && arg1 != secondaryServerSocketPort)
372
 
         {
373
 
            s = factory.createSocket(arg0, arg1);
374
 
         }
375
 
         else
376
 
         {
377
 
            s = new TestSocket(timeout, initialWrites);
378
 
            s = new SSLTestSocket(arg0, arg1, timeout, initialWrites, ((SSLSocket) factory.createSocket()));
379
 
         }
380
 
         log.info(this + " returning " + s);
381
 
         return s;
382
 
      }
383
 
 
384
 
      public Socket createSocket(String arg0, int arg1, InetAddress arg2, int arg3) throws IOException, UnknownHostException
385
 
      {
386
 
         log.info("callbackTest: " + callbackTest);
387
 
         Socket s = null;
388
 
         if (callbackTest && arg1 != secondaryServerSocketPort)
389
 
         {
390
 
            s = factory.createSocket(arg0, arg1);
391
 
         }
392
 
         else
393
 
         {
394
 
            s = new TestSocket(timeout, initialWrites);
395
 
            s = new SSLTestSocket(arg0, arg1, arg2, arg3, timeout, initialWrites, ((SSLSocket) factory.createSocket()));
396
 
         }
397
 
         log.info(this + " returning " + s);
398
 
         return s;
399
 
      }
400
 
 
401
 
      public Socket createSocket(InetAddress arg0, int arg1, InetAddress arg2, int arg3) throws IOException
402
 
      {
403
 
         log.info("callbackTest: " + callbackTest);
404
 
         Socket s = null;
405
 
         if (callbackTest && arg1 != secondaryServerSocketPort)
406
 
         {
407
 
            s = factory.createSocket(arg0, arg1);
408
 
         }
409
 
         else
410
 
         {
411
 
            s = new TestSocket(timeout, initialWrites);
412
 
            s = new SSLTestSocket(arg0, arg1, arg2, arg3, timeout, initialWrites, ((SSLSocket) factory.createSocket()));
413
 
         }
414
 
         log.info(this + " returning " + s);
415
 
         return s;
416
 
      }
417
 
      
418
 
      protected void setupFactory() throws IOException
419
 
      {
420
 
         SSLSocketBuilder sslSocketBuilder = new SSLSocketBuilder();
421
 
         sslSocketBuilder.setUseSSLServerSocketFactory(false);
422
 
         factory = sslSocketBuilder.createSSLSocketFactory();
423
 
      }
424
 
   }
425
 
   
426
 
   static class SSLTestSocket extends SSLSocket
427
 
   {
428
 
      int timeout;
429
 
      int initialWrites;
430
 
      SSLSocket socket;
431
 
      SocketAddress endpoint;
432
 
      
433
 
      public SSLTestSocket(int timeout, int initialWrites, SSLSocket socket)
434
 
      {
435
 
         this.timeout = timeout;
436
 
         this.initialWrites = initialWrites;
437
 
         this.socket = socket;
438
 
      }
439
 
      public SSLTestSocket(String host, int port, int timeout, int initialWrites, SSLSocket socket) throws UnknownHostException, IOException
440
 
      {
441
 
         super(host, port);
442
 
         this.timeout = timeout;
443
 
         this.initialWrites = initialWrites;
444
 
         this.socket = socket;
445
 
         connect(new InetSocketAddress(host, port), timeout);
446
 
      }
447
 
      public SSLTestSocket(InetAddress address, int port, int timeout, int initialWrites, SSLSocket socket) throws IOException
448
 
      {
449
 
         super(address, port);
450
 
         this.timeout = timeout;
451
 
         this.initialWrites = initialWrites;
452
 
         this.socket = socket;
453
 
         connect(new InetSocketAddress(address, port), timeout);
454
 
      }
455
 
      public SSLTestSocket(String host, int port, InetAddress localAddr, int localPort, int timeout, int initialWrites, SSLSocket socket) throws IOException
456
 
      {
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);
463
 
      }
464
 
      public SSLTestSocket(InetAddress address, int port, InetAddress localAddr, int localPort, int timeout, int initialWrites, SSLSocket socket) throws IOException
465
 
      {
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);
472
 
      }
473
 
      public String toString()
474
 
      {
475
 
         return "SSLTestSocket[" + socket.toString() + "]";
476
 
      }
477
 
      public InputStream getInputStream() throws IOException
478
 
      {
479
 
         return socket.getInputStream();
480
 
      }
481
 
      public OutputStream getOutputStream() throws IOException
482
 
      {
483
 
         return new TestOutputStream(socket.getOutputStream(), timeout, initialWrites);
484
 
      }
485
 
      public void addHandshakeCompletedListener(HandshakeCompletedListener listener)
486
 
      {
487
 
         socket.addHandshakeCompletedListener(listener);
488
 
      }
489
 
      public void bind(SocketAddress bindpoint) throws IOException
490
 
      {
491
 
         if (socket != null)
492
 
            socket.bind(bindpoint);
493
 
      }
494
 
      public void connect(SocketAddress endpoint) throws IOException
495
 
      {
496
 
         if (socket != null)
497
 
            socket.connect(endpoint);
498
 
      }
499
 
      public void connect(SocketAddress endpoint, int timeout) throws IOException
500
 
      {
501
 
         socket.connect(endpoint, timeout);
502
 
      }
503
 
      public boolean getEnableSessionCreation()
504
 
      {
505
 
         return socket.getEnableSessionCreation();
506
 
      }
507
 
      public String[] getEnabledCipherSuites()
508
 
      {
509
 
         return socket.getEnabledCipherSuites();
510
 
      }
511
 
      public String[] getEnabledProtocols()
512
 
      {
513
 
         return socket.getEnabledProtocols();
514
 
      }
515
 
      public InetAddress getInetAddress()
516
 
      {
517
 
         return socket.getInetAddress();
518
 
      }
519
 
      public boolean getNeedClientAuth()
520
 
      {
521
 
         return socket.getNeedClientAuth();
522
 
      }
523
 
      public SSLSession getSession()
524
 
      {
525
 
         return socket.getSession();
526
 
      }
527
 
      public String[] getSupportedCipherSuites()
528
 
      {
529
 
         return socket.getSupportedCipherSuites();
530
 
      }
531
 
      public String[] getSupportedProtocols()
532
 
      {
533
 
         return socket.getSupportedProtocols();
534
 
      }
535
 
      public boolean getUseClientMode()
536
 
      {
537
 
         return socket.getUseClientMode();
538
 
      }
539
 
      public boolean getWantClientAuth()
540
 
      {
541
 
         return socket.getWantClientAuth();
542
 
      }
543
 
      public void removeHandshakeCompletedListener(HandshakeCompletedListener listener)
544
 
      {
545
 
         socket.removeHandshakeCompletedListener(listener);
546
 
      }
547
 
      public void setEnableSessionCreation(boolean flag)
548
 
      {
549
 
         socket.setEnableSessionCreation(flag);
550
 
      }
551
 
      public void setEnabledCipherSuites(String[] suites)
552
 
      {
553
 
         socket.setEnabledCipherSuites(suites);
554
 
      }
555
 
      public void setEnabledProtocols(String[] protocols)
556
 
      {
557
 
         socket.setEnabledProtocols(protocols);
558
 
      }
559
 
      public void setNeedClientAuth(boolean need)
560
 
      {
561
 
         socket.setNeedClientAuth(need);
562
 
      }
563
 
      public void setUseClientMode(boolean mode)
564
 
      {
565
 
         socket.setUseClientMode(mode);
566
 
      }
567
 
      public void setWantClientAuth(boolean want)
568
 
      {
569
 
         socket.setWantClientAuth(want);
570
 
      }
571
 
      public void startHandshake() throws IOException
572
 
      {
573
 
         socket.startHandshake();
574
 
      }
575
 
   }
576
 
}
 
 
b'\\ No newline at end of file'