From jboss-remoting-commits at lists.jboss.org Sun Dec 20 21:16:24 2009
Content-Type: multipart/mixed; boundary="===============3170060955768451184=="
MIME-Version: 1.0
From: jboss-remoting-commits at lists.jboss.org
To: jboss-remoting-commits at lists.jboss.org
Subject: [jboss-remoting-commits] JBoss Remoting SVN: r5619 -
remoting2/branches/2.x/src/tests/org/jboss/test/remoting/ipv6.
Date: Sun, 20 Dec 2009 21:16:24 -0500
Message-ID: <200912210216.nBL2GOuR024866@svn01.web.mwc.hst.phx2.redhat.com>
--===============3170060955768451184==
Content-Type: text/plain; charset="utf-8"
MIME-Version: 1.0
Content-Transfer-Encoding: quoted-printable
Author: ron.sigal(a)jboss.com
Date: 2009-12-20 21:16:24 -0500 (Sun, 20 Dec 2009)
New Revision: 5619
Added:
remoting2/branches/2.x/src/tests/org/jboss/test/remoting/ipv6/IPv6HostWi=
thNoBracketsTestCase.java
Log:
JBREM-1164: New unit tests.
Added: remoting2/branches/2.x/src/tests/org/jboss/test/remoting/ipv6/IPv6Ho=
stWithNoBracketsTestCase.java
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- remoting2/branches/2.x/src/tests/org/jboss/test/remoting/ipv6/IPv6HostW=
ithNoBracketsTestCase.java (rev 0)
+++ remoting2/branches/2.x/src/tests/org/jboss/test/remoting/ipv6/IPv6HostW=
ithNoBracketsTestCase.java 2009-12-21 02:16:24 UTC (rev 5619)
@@ -0,0 +1,404 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2009, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.test.remoting.ipv6;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.net.InetAddress;
+import java.net.UnknownHostException;
+import java.security.AccessController;
+import java.security.PrivilegedActionException;
+import java.security.PrivilegedExceptionAction;
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.management.MBeanServer;
+import javax.xml.parsers.DocumentBuilderFactory;
+
+import junit.framework.TestCase;
+
+import org.apache.log4j.ConsoleAppender;
+import org.apache.log4j.Level;
+import org.apache.log4j.Logger;
+import org.apache.log4j.PatternLayout;
+import org.jboss.remoting.InvocationRequest;
+import org.jboss.remoting.InvokerLocator;
+import org.jboss.remoting.ServerConfiguration;
+import org.jboss.remoting.ServerInvocationHandler;
+import org.jboss.remoting.ServerInvoker;
+import org.jboss.remoting.callback.InvokerCallbackHandler;
+import org.jboss.remoting.transport.Connector;
+import org.jboss.remoting.util.SecurityUtility;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+
+
+/**
+ * Unit tests for JBREM-1164.
+ * =
+ * @author Ben Clare
+ * @author Ron Sigal
+ * =
+ * @version $Rev$
+ *
+ * Copyright Dec 20, 2009
+ *
+ */
+public class IPv6HostWithNoBracketsTestCase extends TestCase
+{
+ private static Logger log =3D Logger.getLogger(IPv6HostWithNoBracketsTe=
stCase.class);
+ private static boolean firstTime =3D true;
+ private static int port =3D 8080;
+
+
+ public void setUp() throws Exception
+ {
+ if (firstTime)
+ {
+ firstTime =3D false;
+ Logger.getLogger("org.jboss.remoting").setLevel(Level.INFO);
+ Logger.getLogger("org.jboss.test.remoting").setLevel(Level.INFO);
+ String pattern =3D "[%d{ABSOLUTE}] [%t] %5p (%F:%L) - %m%n";
+ PatternLayout layout =3D new PatternLayout(pattern);
+ ConsoleAppender consoleAppender =3D new ConsoleAppender(layout);
+ Logger.getRootLogger().addAppender(consoleAppender); =
+ }
+ }
+
+
+ public void tearDown()
+ {
+ }
+
+
+ public void testXMLWithoutBrackets() throws Throwable
+ {
+ log.info("entering " + getName());
+
+ assertTrue(doXMLTest("::", ++port));
+ assertTrue(doXMLTest("::1", ++port));
+ assertTrue(doXMLTest("0:0:0:0:0:0:0:1", ++port));
+ assertTrue(doXMLTest("0:0:0:0:0:0:127.0.0.1", ++port));
+ assertTrue(doXMLTest("3ffe:1900:4545:3:200:f8ff:fe21:67cf", ++port));
+ assertTrue(doXMLTest("3ffe:1900:4545:3:200:f8ff:fe21:67cf%5", ++port=
));
+
+ log.info(getName() + " PASSES");
+ }
+
+
+ public void testXMLWithBrackets() throws Throwable
+ {
+ log.info("entering " + getName());
+
+ assertTrue(doXMLTest("[::]", ++port));
+ assertTrue(doXMLTest("[::1]", ++port));
+ assertTrue(doXMLTest("[0:0:0:0:0:0:0:1]", ++port));
+ assertTrue(doXMLTest("[0:0:0:0:0:0:127.0.0.1]", ++port));
+ assertTrue(doXMLTest("[3ffe:1900:4545:3:200:f8ff:fe21:67cf]", ++port=
));
+ assertTrue(doXMLTest("[3ffe:1900:4545:3:200:f8ff:fe21:67cf%5]", ++po=
rt));
+
+ log.info(getName() + " PASSES");
+ }
+
+
+ public void testXMLWithoutHost() throws Exception
+ {
+ log.info("entering " + getName());
+
+ String xml =3D new StringBuffer()
+ .append("\n")
+ .append(" \n")
+ .append(" \n")
+ .append(" \n")
+ .append(" " + port + "\n")
+ .append(" 10000<=
/attribute>\n")
+ .append(" \n")
+ .append(" \n")
+ .append(" " + SampleInvocationHandl=
er.class.getName() + "\n")
+ .append(" \n")
+ .append(" \n")
+ .append(" \n")
+ .append("\n").toString();
+ Connector connector =3D new Connector();
+ ByteArrayInputStream bais =3D new ByteArrayInputStream(xml.getBytes(=
));
+ Document doc =3D DocumentBuilderFactory.newInstance().newDocumentBui=
lder().parse(bais);
+ Element element =3D doc.getDocumentElement();
+ connector.setConfiguration(element);
+ try
+ {
+ connector.create();
+ InvokerLocator expected =3D new InvokerLocator("socket://" + getL=
ocalHost() + ":" + port + "/?timeout=3D10000");
+ InvokerLocator actual =3D connector.getLocator();
+ log.info("Expected: " + expected);
+ log.info("Actual: " + actual);InetAddress.getLocalHost();
+ assertEquals(expected, actual);
+ }
+ catch (Exception e)
+ {
+ log.error("Exception caught " + e.getMessage());
+ }
+ finally
+ {
+ connector.stop();
+ }
+
+ log.info(getName() + " PASSES");
+ }
+
+
+ =
+ public void testServerConfigurationWithoutBrackets() throws Throwable
+ {
+ log.info("entering " + getName());
+
+ assertTrue(doServerConfigurationTest("::", ++port));
+ assertTrue(doServerConfigurationTest("::1", ++port));
+ assertTrue(doServerConfigurationTest("0:0:0:0:0:0:0:1", ++port));
+ assertTrue(doServerConfigurationTest("0:0:0:0:0:0:127.0.0.1", ++port=
));
+ assertTrue(doServerConfigurationTest("3ffe:1900:4545:3:200:f8ff:fe21=
:67cf", ++port));
+ assertTrue(doServerConfigurationTest("3ffe:1900:4545:3:200:f8ff:fe21=
:67cf%5", ++port));
+
+ log.info(getName() + " PASSES");
+ }
+
+
+ public void testServerConfigurationWithBrackets() throws Throwable
+ {
+ log.info("entering " + getName());
+
+ assertTrue(doServerConfigurationTest("[::]", ++port));
+ assertTrue(doServerConfigurationTest("[::1]", ++port));
+ assertTrue(doServerConfigurationTest("[0:0:0:0:0:0:0:1]", ++port));
+ assertTrue(doServerConfigurationTest("[0:0:0:0:0:0:127.0.0.1]", ++po=
rt));
+ assertTrue(doServerConfigurationTest("[3ffe:1900:4545:3:200:f8ff:fe2=
1:67cf]", ++port));
+ assertTrue(doServerConfigurationTest("[3ffe:1900:4545:3:200:f8ff:fe2=
1:67cf%5]", ++port));
+
+ log.info(getName() + " PASSES");
+ }
+ =
+ =
+ public void testServerConfigurationWithoutHost() throws Throwable
+ {
+ log.info("entering " + getName());
+ =
+ // Create ServerConfiguration.
+ ServerConfiguration config =3D new ServerConfiguration("socket");
+ Map locatorConfig =3D new HashMap();
+
+ // Add invokerLocatorParameters.
+ locatorConfig.put("serverBindPort", Integer.toString(port));
+ locatorConfig.put("timeout", "10000");
+ config.setInvokerLocatorParameters(locatorConfig);
+
+ // Add invocation handler.
+ Map handlers =3D new HashMap();
+ ServerInvocationHandler handler =3D new SampleInvocationHandler();
+ handlers.put("test", handler); =
+ config.setInvocationHandlers(handlers);
+
+ // Create Connector.
+ Connector connector =3D new Connector();
+ try
+ {
+ connector.setServerConfiguration(config);
+ connector.create();
+ InvokerLocator expected =3D new InvokerLocator("socket://" + getL=
ocalHost() + ":" + port + "/?timeout=3D10000");
+ InvokerLocator actual =3D connector.getLocator();
+ log.info("Expected: " + expected);
+ log.info("Actual: " + actual);
+ assertEquals(expected, actual);
+ }
+ catch (Exception e)
+ {
+ log.error("Exception caught ", e);
+ }
+ finally
+ {
+ connector.stop();
+ }
+ =
+ log.info(getName() + " PASSES");
+ }
+
+
+ protected boolean doXMLTest(String host, int port) throws Exception
+ {
+ String xml =3D new StringBuffer()
+ .append("\n")
+ .append(" \n")
+ .append(" \n")
+ .append(" \n")
+ .append(" " + host + "\n")
+ .append(" " + port + "\n")
+ .append(" 10000<=
/attribute>\n")
+ .append(" \n")
+ .append(" \n")
+ .append(" " + SampleInvocationHandl=
er.class.getName() + "\n")
+ .append(" \n")
+ .append(" \n")
+ .append(" \n")
+ .append("\n").toString();
+ Connector connector =3D new Connector();
+ ByteArrayInputStream bais =3D new ByteArrayInputStream(xml.getBytes(=
));
+ Document doc =3D DocumentBuilderFactory.newInstance().newDocumentBui=
lder().parse(bais);
+ Element element =3D doc.getDocumentElement();
+ connector.setConfiguration(element);
+ try
+ {
+ connector.create();
+ InvokerLocator expected =3D new InvokerLocator("socket://" + fixH=
ostnameForURL(host) + ":" + port + "/?timeout=3D10000");
+ InvokerLocator actual =3D connector.getLocator();
+ log.info("Expected: " + expected);
+ log.info("Actual: " + actual);
+ assertEquals(expected, actual);
+ return true;
+ }
+ catch (Exception e)
+ {
+ log.error("Exception caught ", e);
+ return false;
+ }
+ finally
+ {
+ connector.stop();
+ }
+ }
+
+
+ protected boolean doServerConfigurationTest(String host, int port) thro=
ws Exception
+ {
+ // Create ServerConfiguration.
+ ServerConfiguration config =3D new ServerConfiguration("socket");
+ Map locatorConfig =3D new HashMap();
+
+ // Add invokerLocatorParameters.
+ locatorConfig.put("serverBindAddress", host);
+ locatorConfig.put("serverBindPort", Integer.toString(port));
+ locatorConfig.put("timeout", "10000");
+ config.setInvokerLocatorParameters(locatorConfig);
+
+ // Add invocation handler.
+ Map handlers =3D new HashMap();
+ ServerInvocationHandler handler =3D new SampleInvocationHandler();
+ handlers.put("test", handler); =
+ config.setInvocationHandlers(handlers);
+
+ // Create Connector.
+ Connector connector =3D new Connector();
+ try
+ {
+ connector.setServerConfiguration(config);
+ connector.create();
+ InvokerLocator expected =3D new InvokerLocator("socket://" + fixH=
ostnameForURL(host) + ":" + port + "/?timeout=3D10000");
+ InvokerLocator actual =3D connector.getLocator();
+ log.info("Expected: " + expected);
+ log.info("Actual: " + actual);
+ assertEquals(expected, actual);
+ return true;
+ }
+ catch (Exception e)
+ {
+ log.error("Exception caught ", e);
+ return false;
+ }
+ finally
+ {
+ connector.stop();
+ }
+ }
+
+
+ static protected String fixHostnameForURL(String host)
+ {
+ if (host =3D=3D null)
+ return host ;
+
+ // if the hostname is an IPv6 literal, enclose it in brackets
+ if (host.indexOf(':') !=3D -1 && host.indexOf("[") =3D=3D -1)
+ {
+ System.out.println("HOST: [" + host + "]");
+ return "[" + host + "]" ;
+ }
+ else
+ {
+ return host;
+ }
+ } =
+
+
+ static protected String getLocalHost() throws UnknownHostException
+ {
+ if (SecurityUtility.skipAccessControl())
+ {
+ try
+ {
+ System.setProperty("java.net.preferIPv6Addresses", "true");
+ return InetAddress.getLocalHost().getHostAddress();
+ }
+ catch (IOException e)
+ {
+ return InetAddress.getByName("127.0.0.1").getHostAddress();
+ }
+ }
+
+ try
+ {
+ return (String) AccessController.doPrivileged( new PrivilegedExce=
ptionAction()
+ {
+ public Object run() throws IOException
+ {
+ try
+ {
+ System.setProperty("java.net.preferIPv6Addresses", "true=
");
+ return InetAddress.getLocalHost().getHostAddress();
+ }
+ catch (IOException e)
+ {
+ return InetAddress.getByName("127.0.0.1").getHostAddress=
();
+ }
+ }
+ });
+ }
+ catch (PrivilegedActionException e)
+ {
+ throw (UnknownHostException) e.getCause();
+ }
+ }
+
+
+ public static class SampleInvocationHandler implements ServerInvocation=
Handler
+ {
+ public Object invoke(InvocationRequest invocation) throws Throwable
+ {
+ return invocation.getParameter();
+ }
+
+ public void addListener(InvokerCallbackHandler callbackHandler) {}
+ public void removeListener(InvokerCallbackHandler callbackHandler) {}
+ public void setMBeanServer(MBeanServer server) {}
+ public void setInvoker(ServerInvoker invoker) {}
+ }
+}
\ No newline at end of file
--===============3170060955768451184==--