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.
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.
22
package org.jboss.test.remoting.ipv6;
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;
34
import javax.management.MBeanServer;
35
import javax.xml.parsers.DocumentBuilderFactory;
37
import junit.framework.TestCase;
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;
56
* Unit tests for JBREM-1164.
58
* @author <a href="mailto:bclare@tpg.com.au">Ben Clare</a>
59
* @author <a href="mailto:ron.sigal@jboss.com">Ron Sigal</a>
63
* Copyright Dec 20, 2009
66
public class IPv6HostWithNoBracketsTestCase extends TestCase
68
private static Logger log = Logger.getLogger(IPv6HostWithNoBracketsTestCase.class);
69
private static boolean firstTime = true;
70
private static int port = 8080;
73
public void setUp() throws Exception
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);
88
public void tearDown()
93
public void testXMLWithoutBrackets() throws Throwable
95
log.info("entering " + getName());
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"))
105
log.info("java version is " + version + ". Skipping test");
109
assertTrue(doXMLTest("3ffe:1900:4545:3:200:f8ff:fe21:67cf%5", ++port));
112
log.info(getName() + " PASSES");
116
public void testXMLWithBrackets() throws Throwable
118
log.info("entering " + getName());
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"))
128
log.info("java version is " + version + ". Skipping test");
132
assertTrue(doXMLTest("[3ffe:1900:4545:3:200:f8ff:fe21:67cf%5]", ++port));
135
log.info(getName() + " PASSES");
139
public void testXMLWithoutHost() throws Exception
141
log.info("entering " + getName());
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);
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);
175
log.error("Exception caught " + e.getMessage());
182
log.info(getName() + " PASSES");
187
public void testServerConfigurationWithoutBrackets() throws Throwable
189
log.info("entering " + getName());
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"))
199
log.info("java version is " + version + ". Skipping test");
203
assertTrue(doServerConfigurationTest("3ffe:1900:4545:3:200:f8ff:fe21:67cf%5", ++port));
206
log.info(getName() + " PASSES");
210
public void testServerConfigurationWithBrackets() throws Throwable
212
log.info("entering " + getName());
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"))
222
log.info("java version is " + version + ". Skipping test");
226
assertTrue(doServerConfigurationTest("[3ffe:1900:4545:3:200:f8ff:fe21:67cf%5]", ++port));
229
log.info(getName() + " PASSES");
233
public void testServerConfigurationWithoutHost() throws Throwable
235
log.info("entering " + getName());
237
// Create ServerConfiguration.
238
ServerConfiguration config = new ServerConfiguration("socket");
239
Map locatorConfig = new HashMap();
241
// Add invokerLocatorParameters.
242
locatorConfig.put("serverBindPort", Integer.toString(port));
243
locatorConfig.put("timeout", "10000");
244
config.setInvokerLocatorParameters(locatorConfig);
246
// Add invocation handler.
247
Map handlers = new HashMap();
248
ServerInvocationHandler handler = new SampleInvocationHandler();
249
handlers.put("test", handler);
250
config.setInvocationHandlers(handlers);
253
Connector connector = new Connector();
256
connector.setServerConfiguration(config);
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);
266
log.error("Exception caught ", e);
273
log.info(getName() + " PASSES");
277
protected boolean doXMLTest(String host, int port) throws Exception
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);
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);
313
log.error("Exception caught ", e);
323
protected boolean doServerConfigurationTest(String host, int port) throws Exception
325
// Create ServerConfiguration.
326
ServerConfiguration config = new ServerConfiguration("socket");
327
Map locatorConfig = new HashMap();
329
// Add invokerLocatorParameters.
330
locatorConfig.put("serverBindAddress", host);
331
locatorConfig.put("serverBindPort", Integer.toString(port));
332
locatorConfig.put("timeout", "10000");
333
config.setInvokerLocatorParameters(locatorConfig);
335
// Add invocation handler.
336
Map handlers = new HashMap();
337
ServerInvocationHandler handler = new SampleInvocationHandler();
338
handlers.put("test", handler);
339
config.setInvocationHandlers(handlers);
342
Connector connector = new Connector();
345
connector.setServerConfiguration(config);
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);
356
log.error("Exception caught ", e);
366
static protected String fixHostnameForURL(String host)
371
// if the hostname is an IPv6 literal, enclose it in brackets
372
if (host.indexOf(':') != -1 && host.indexOf("[") == -1)
374
System.out.println("HOST: [" + host + "]");
375
return "[" + host + "]" ;
384
static protected String getLocalHost() throws UnknownHostException
386
if (SecurityUtility.skipAccessControl())
390
System.setProperty("java.net.preferIPv6Addresses", "true");
391
return InetAddress.getLocalHost().getHostAddress();
393
catch (IOException e)
395
return InetAddress.getByName("127.0.0.1").getHostAddress();
401
return (String) AccessController.doPrivileged( new PrivilegedExceptionAction()
403
public Object run() throws IOException
407
System.setProperty("java.net.preferIPv6Addresses", "true");
408
return InetAddress.getLocalHost().getHostAddress();
410
catch (IOException e)
412
return InetAddress.getByName("127.0.0.1").getHostAddress();
417
catch (PrivilegedActionException e)
419
throw (UnknownHostException) e.getCause();
424
public static class SampleInvocationHandler implements ServerInvocationHandler
426
public Object invoke(InvocationRequest invocation) throws Throwable
428
return invocation.getParameter();
431
public void addListener(InvokerCallbackHandler callbackHandler) {}
432
public void removeListener(InvokerCallbackHandler callbackHandler) {}
433
public void setMBeanServer(MBeanServer server) {}
434
public void setInvoker(ServerInvoker invoker) {}
b'\\ No newline at end of file'