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

« back to all changes in this revision

Viewing changes to tests/org/jboss/test/remoting/ipv6/IPv6HostWithNoBracketsTestCase.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.ipv6;
23
 
 
24
 
import java.io.ByteArrayInputStream;
25
 
import java.io.IOException;
26
 
import java.net.InetAddress;
27
 
import java.net.UnknownHostException;
28
 
import java.security.AccessController;
29
 
import java.security.PrivilegedActionException;
30
 
import java.security.PrivilegedExceptionAction;
31
 
import java.util.HashMap;
32
 
import java.util.Map;
33
 
 
34
 
import javax.management.MBeanServer;
35
 
import javax.xml.parsers.DocumentBuilderFactory;
36
 
 
37
 
import junit.framework.TestCase;
38
 
 
39
 
import org.apache.log4j.ConsoleAppender;
40
 
import org.apache.log4j.Level;
41
 
import org.apache.log4j.Logger;
42
 
import org.apache.log4j.PatternLayout;
43
 
import org.jboss.remoting.InvocationRequest;
44
 
import org.jboss.remoting.InvokerLocator;
45
 
import org.jboss.remoting.ServerConfiguration;
46
 
import org.jboss.remoting.ServerInvocationHandler;
47
 
import org.jboss.remoting.ServerInvoker;
48
 
import org.jboss.remoting.callback.InvokerCallbackHandler;
49
 
import org.jboss.remoting.transport.Connector;
50
 
import org.jboss.remoting.util.SecurityUtility;
51
 
import org.w3c.dom.Document;
52
 
import org.w3c.dom.Element;
53
 
 
54
 
 
55
 
/**
56
 
 * Unit tests for JBREM-1164.
57
 
 * 
58
 
 * @author <a href="mailto:bclare@tpg.com.au">Ben Clare</a>
59
 
 * @author <a href="mailto:ron.sigal@jboss.com">Ron Sigal</a>
60
 
 * 
61
 
 * @version $Rev$
62
 
 * <p>
63
 
 * Copyright Dec 20, 2009
64
 
 * </p>
65
 
 */
66
 
public class IPv6HostWithNoBracketsTestCase extends TestCase
67
 
{
68
 
   private static Logger log = Logger.getLogger(IPv6HostWithNoBracketsTestCase.class);
69
 
   private static boolean firstTime = true;
70
 
   private static int port = 8080;
71
 
 
72
 
 
73
 
   public void setUp() throws Exception
74
 
   {
75
 
      if (firstTime)
76
 
      {
77
 
         firstTime = false;
78
 
         Logger.getLogger("org.jboss.remoting").setLevel(Level.INFO);
79
 
         Logger.getLogger("org.jboss.test.remoting").setLevel(Level.INFO);
80
 
         String pattern = "[%d{ABSOLUTE}] [%t] %5p (%F:%L) - %m%n";
81
 
         PatternLayout layout = new PatternLayout(pattern);
82
 
         ConsoleAppender consoleAppender = new ConsoleAppender(layout);
83
 
         Logger.getRootLogger().addAppender(consoleAppender);  
84
 
      }
85
 
   }
86
 
 
87
 
 
88
 
   public void tearDown()
89
 
   {
90
 
   }
91
 
 
92
 
 
93
 
   public void testXMLWithoutBrackets() throws Throwable
94
 
   {
95
 
      log.info("entering " + getName());
96
 
 
97
 
      assertTrue(doXMLTest("::", ++port));
98
 
      assertTrue(doXMLTest("::1", ++port));
99
 
      assertTrue(doXMLTest("0:0:0:0:0:0:0:1", ++port));
100
 
      assertTrue(doXMLTest("0:0:0:0:0:0:127.0.0.1", ++port));
101
 
      assertTrue(doXMLTest("3ffe:1900:4545:3:200:f8ff:fe21:67cf", ++port));
102
 
      String version = System.getProperty("java.version");
103
 
      if (version.startsWith("1.4"))
104
 
      {
105
 
         log.info("java version is " + version + ". Skipping test");
106
 
      }
107
 
      else
108
 
      {
109
 
         assertTrue(doXMLTest("3ffe:1900:4545:3:200:f8ff:fe21:67cf%5", ++port));
110
 
      }
111
 
 
112
 
      log.info(getName() + " PASSES");
113
 
   }
114
 
 
115
 
 
116
 
   public void testXMLWithBrackets() throws Throwable
117
 
   {
118
 
      log.info("entering " + getName());
119
 
 
120
 
      assertTrue(doXMLTest("[::]", ++port));
121
 
      assertTrue(doXMLTest("[::1]", ++port));
122
 
      assertTrue(doXMLTest("[0:0:0:0:0:0:0:1]", ++port));
123
 
      assertTrue(doXMLTest("[0:0:0:0:0:0:127.0.0.1]", ++port));
124
 
      assertTrue(doXMLTest("[3ffe:1900:4545:3:200:f8ff:fe21:67cf]", ++port));
125
 
      String version = System.getProperty("java.version");
126
 
      if (version.startsWith("1.4"))
127
 
      {
128
 
         log.info("java version is " + version + ". Skipping test");
129
 
      }
130
 
      else
131
 
      {
132
 
         assertTrue(doXMLTest("[3ffe:1900:4545:3:200:f8ff:fe21:67cf%5]", ++port));
133
 
      }
134
 
 
135
 
      log.info(getName() + " PASSES");
136
 
   }
137
 
 
138
 
 
139
 
   public void testXMLWithoutHost() throws Exception
140
 
   {
141
 
      log.info("entering " + getName());
142
 
 
143
 
      String xml = new StringBuffer()
144
 
      .append("<mbean code=\"org.jboss.remoting.transport.Connector\"\n")
145
 
      .append(" name=\"jboss.messaging:service=Connector,transport=socket\"\n")
146
 
      .append(" display-name=\"Connector\">\n")
147
 
      .append(" <attribute name=\"Configuration\">\n")
148
 
      .append("  <config>\n")
149
 
      .append("   <invoker transport=\"socket\">\n")
150
 
      .append("    <attribute name=\"" + ServerInvoker.SERVER_BIND_PORT_KEY + "\">" + port + "</attribute>\n")
151
 
      .append("    <attribute name=\"timeout\" isParam=\"true\">10000</attribute>\n")
152
 
      .append("   </invoker>\n")
153
 
      .append("   <handlers>\n")
154
 
      .append("    <handler subsystem=\"test\">" + SampleInvocationHandler.class.getName() + "</handler>\n")
155
 
      .append("   </handlers>\n")
156
 
      .append("  </config>\n")
157
 
      .append(" </attribute>\n")
158
 
      .append("</mbean>\n").toString();
159
 
      Connector connector = new Connector();
160
 
      ByteArrayInputStream bais = new ByteArrayInputStream(xml.getBytes());
161
 
      Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(bais);
162
 
      Element element =  doc.getDocumentElement();
163
 
      connector.setConfiguration(element);
164
 
      try
165
 
      {
166
 
         connector.create();
167
 
         InvokerLocator expected = new InvokerLocator("socket://" + getLocalHost() + ":" + port + "/?timeout=10000");
168
 
         InvokerLocator actual = connector.getLocator();
169
 
         log.info("Expected: " + expected);
170
 
         log.info("Actual:   " + actual);InetAddress.getLocalHost();
171
 
         assertEquals(expected, actual);
172
 
      }
173
 
      catch (Exception e)
174
 
      {
175
 
         log.error("Exception caught " + e.getMessage());
176
 
      }
177
 
      finally
178
 
      {
179
 
         connector.stop();
180
 
      }
181
 
 
182
 
      log.info(getName() + " PASSES");
183
 
   }
184
 
 
185
 
 
186
 
   
187
 
   public void testServerConfigurationWithoutBrackets() throws Throwable
188
 
   {
189
 
      log.info("entering " + getName());
190
 
 
191
 
      assertTrue(doServerConfigurationTest("::", ++port));
192
 
      assertTrue(doServerConfigurationTest("::1", ++port));
193
 
      assertTrue(doServerConfigurationTest("0:0:0:0:0:0:0:1", ++port));
194
 
      assertTrue(doServerConfigurationTest("0:0:0:0:0:0:127.0.0.1", ++port));
195
 
      assertTrue(doServerConfigurationTest("3ffe:1900:4545:3:200:f8ff:fe21:67cf", ++port));
196
 
      String version = System.getProperty("java.version");
197
 
      if (version.startsWith("1.4"))
198
 
      {
199
 
         log.info("java version is " + version + ". Skipping test");
200
 
      }
201
 
      else
202
 
      {
203
 
         assertTrue(doServerConfigurationTest("3ffe:1900:4545:3:200:f8ff:fe21:67cf%5", ++port));
204
 
      }
205
 
      
206
 
      log.info(getName() + " PASSES");
207
 
   }
208
 
 
209
 
 
210
 
   public void testServerConfigurationWithBrackets() throws Throwable
211
 
   {
212
 
      log.info("entering " + getName());
213
 
 
214
 
      assertTrue(doServerConfigurationTest("[::]", ++port));
215
 
      assertTrue(doServerConfigurationTest("[::1]", ++port));
216
 
      assertTrue(doServerConfigurationTest("[0:0:0:0:0:0:0:1]", ++port));
217
 
      assertTrue(doServerConfigurationTest("[0:0:0:0:0:0:127.0.0.1]", ++port));
218
 
      assertTrue(doServerConfigurationTest("[3ffe:1900:4545:3:200:f8ff:fe21:67cf]", ++port));
219
 
      String version = System.getProperty("java.version");
220
 
      if (version.startsWith("1.4"))
221
 
      {
222
 
         log.info("java version is " + version + ". Skipping test");
223
 
      }
224
 
      else
225
 
      {
226
 
         assertTrue(doServerConfigurationTest("[3ffe:1900:4545:3:200:f8ff:fe21:67cf%5]", ++port));
227
 
      }
228
 
      
229
 
      log.info(getName() + " PASSES");
230
 
   }
231
 
   
232
 
   
233
 
   public void testServerConfigurationWithoutHost() throws Throwable
234
 
   {
235
 
      log.info("entering " + getName());
236
 
      
237
 
      // Create ServerConfiguration.
238
 
      ServerConfiguration config = new ServerConfiguration("socket");
239
 
      Map locatorConfig = new HashMap();
240
 
 
241
 
      // Add invokerLocatorParameters.
242
 
      locatorConfig.put("serverBindPort", Integer.toString(port));
243
 
      locatorConfig.put("timeout", "10000");
244
 
      config.setInvokerLocatorParameters(locatorConfig);
245
 
 
246
 
      // Add invocation handler.
247
 
      Map handlers = new HashMap();
248
 
      ServerInvocationHandler handler = new SampleInvocationHandler();
249
 
      handlers.put("test", handler); 
250
 
      config.setInvocationHandlers(handlers);
251
 
 
252
 
      // Create Connector.
253
 
      Connector connector = new Connector();
254
 
      try
255
 
      {
256
 
         connector.setServerConfiguration(config);
257
 
         connector.create();
258
 
         InvokerLocator expected = new InvokerLocator("socket://" + getLocalHost() + ":" + port + "/?timeout=10000");
259
 
         InvokerLocator actual = connector.getLocator();
260
 
         log.info("Expected: " + expected);
261
 
         log.info("Actual:   " + actual);
262
 
         assertEquals(expected, actual);
263
 
      }
264
 
      catch (Exception e)
265
 
      {
266
 
         log.error("Exception caught ", e);
267
 
      }
268
 
      finally
269
 
      {
270
 
         connector.stop();
271
 
      }
272
 
      
273
 
      log.info(getName() + " PASSES");
274
 
   }
275
 
 
276
 
 
277
 
   protected boolean doXMLTest(String host, int port) throws Exception
278
 
   {
279
 
      String xml = new StringBuffer()
280
 
      .append("<mbean code=\"org.jboss.remoting.transport.Connector\"\n")
281
 
      .append(" name=\"jboss.messaging:service=Connector,transport=socket\"\n")
282
 
      .append(" display-name=\"Connector\">\n")
283
 
      .append(" <attribute name=\"Configuration\">\n")
284
 
      .append("  <config>\n")
285
 
      .append("   <invoker transport=\"socket\">\n")
286
 
      .append("    <attribute name=\"" + ServerInvoker.SERVER_BIND_ADDRESS_KEY + "\">" + host + "</attribute>\n")
287
 
      .append("    <attribute name=\"" + ServerInvoker.SERVER_BIND_PORT_KEY + "\">" + port + "</attribute>\n")
288
 
      .append("    <attribute name=\"timeout\" isParam=\"true\">10000</attribute>\n")
289
 
      .append("   </invoker>\n")
290
 
      .append("   <handlers>\n")
291
 
      .append("    <handler subsystem=\"test\">" + SampleInvocationHandler.class.getName() + "</handler>\n")
292
 
      .append("   </handlers>\n")
293
 
      .append("  </config>\n")
294
 
      .append(" </attribute>\n")
295
 
      .append("</mbean>\n").toString();
296
 
      Connector connector = new Connector();
297
 
      ByteArrayInputStream bais = new ByteArrayInputStream(xml.getBytes());
298
 
      Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(bais);
299
 
      Element element =  doc.getDocumentElement();
300
 
      connector.setConfiguration(element);
301
 
      try
302
 
      {
303
 
         connector.create();
304
 
         InvokerLocator expected = new InvokerLocator("socket://" + fixHostnameForURL(host) + ":" + port + "/?timeout=10000");
305
 
         InvokerLocator actual = connector.getLocator();
306
 
         log.info("Expected: " + expected);
307
 
         log.info("Actual:   " + actual);
308
 
         assertEquals(expected, actual);
309
 
         return true;
310
 
      }
311
 
      catch (Exception e)
312
 
      {
313
 
         log.error("Exception caught ", e);
314
 
         return false;
315
 
      }
316
 
      finally
317
 
      {
318
 
         connector.stop();
319
 
      }
320
 
   }
321
 
 
322
 
 
323
 
   protected boolean doServerConfigurationTest(String host, int port) throws Exception
324
 
   {
325
 
      // Create ServerConfiguration.
326
 
      ServerConfiguration config = new ServerConfiguration("socket");
327
 
      Map locatorConfig = new HashMap();
328
 
 
329
 
      // Add invokerLocatorParameters.
330
 
      locatorConfig.put("serverBindAddress", host);
331
 
      locatorConfig.put("serverBindPort", Integer.toString(port));
332
 
      locatorConfig.put("timeout", "10000");
333
 
      config.setInvokerLocatorParameters(locatorConfig);
334
 
 
335
 
      // Add invocation handler.
336
 
      Map handlers = new HashMap();
337
 
      ServerInvocationHandler handler = new SampleInvocationHandler();
338
 
      handlers.put("test", handler); 
339
 
      config.setInvocationHandlers(handlers);
340
 
 
341
 
      // Create Connector.
342
 
      Connector connector = new Connector();
343
 
      try
344
 
      {
345
 
         connector.setServerConfiguration(config);
346
 
         connector.create();
347
 
         InvokerLocator expected = new InvokerLocator("socket://" + fixHostnameForURL(host) + ":" + port + "/?timeout=10000");
348
 
         InvokerLocator actual = connector.getLocator();
349
 
         log.info("Expected: " + expected);
350
 
         log.info("Actual:   " + actual);
351
 
         assertEquals(expected, actual);
352
 
         return true;
353
 
      }
354
 
      catch (Exception e)
355
 
      {
356
 
         log.error("Exception caught ", e);
357
 
         return false;
358
 
      }
359
 
      finally
360
 
      {
361
 
         connector.stop();
362
 
      }
363
 
   }
364
 
 
365
 
 
366
 
   static protected String fixHostnameForURL(String host)
367
 
   {
368
 
      if (host == null)
369
 
         return host ;
370
 
 
371
 
      // if the hostname is an IPv6 literal, enclose it in brackets
372
 
      if (host.indexOf(':') != -1 && host.indexOf("[") == -1)
373
 
      {
374
 
         System.out.println("HOST: [" + host + "]");
375
 
         return "[" + host + "]" ;
376
 
      }
377
 
      else
378
 
      {
379
 
         return host;
380
 
      }
381
 
   } 
382
 
 
383
 
 
384
 
   static protected String getLocalHost() throws UnknownHostException
385
 
   {
386
 
      if (SecurityUtility.skipAccessControl())
387
 
      {
388
 
         try
389
 
         {
390
 
            System.setProperty("java.net.preferIPv6Addresses", "true");
391
 
            return InetAddress.getLocalHost().getHostAddress();
392
 
         }
393
 
         catch (IOException e)
394
 
         {
395
 
            return InetAddress.getByName("127.0.0.1").getHostAddress();
396
 
         }
397
 
      }
398
 
 
399
 
      try
400
 
      {
401
 
         return (String) AccessController.doPrivileged( new PrivilegedExceptionAction()
402
 
         {
403
 
            public Object run() throws IOException
404
 
            {
405
 
               try
406
 
               {
407
 
                  System.setProperty("java.net.preferIPv6Addresses", "true");
408
 
                  return InetAddress.getLocalHost().getHostAddress();
409
 
               }
410
 
               catch (IOException e)
411
 
               {
412
 
                  return InetAddress.getByName("127.0.0.1").getHostAddress();
413
 
               }
414
 
            }
415
 
         });
416
 
      }
417
 
      catch (PrivilegedActionException e)
418
 
      {
419
 
         throw (UnknownHostException) e.getCause();
420
 
      }
421
 
   }
422
 
 
423
 
 
424
 
   public static class SampleInvocationHandler implements ServerInvocationHandler
425
 
   {
426
 
      public Object invoke(InvocationRequest invocation) throws Throwable
427
 
      {
428
 
         return invocation.getParameter();
429
 
      }
430
 
 
431
 
      public void addListener(InvokerCallbackHandler callbackHandler) {}
432
 
      public void removeListener(InvokerCallbackHandler callbackHandler) {}
433
 
      public void setMBeanServer(MBeanServer server) {}
434
 
      public void setInvoker(ServerInvoker invoker) {}
435
 
   }
436
 
}
 
 
b'\\ No newline at end of file'