From jboss-remoting-commits at lists.jboss.org Wed Oct 29 03:06:56 2008 Content-Type: multipart/mixed; boundary="===============3170290354910634554==" 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: r4631 - remoting2/branches/2.x/src/tests/org/jboss/test/remoting/transport/http/proxy. Date: Wed, 29 Oct 2008 03:06:55 -0400 Message-ID: --===============3170290354910634554== Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Author: ron.sigal(a)jboss.com Date: 2008-10-29 03:06:55 -0400 (Wed, 29 Oct 2008) New Revision: 4631 Added: remoting2/branches/2.x/src/tests/org/jboss/test/remoting/transport/http/= proxy/ProxyAuthenticationTestCase.java Log: JBREM-1051: New unit tests. Added: remoting2/branches/2.x/src/tests/org/jboss/test/remoting/transport/h= ttp/proxy/ProxyAuthenticationTestCase.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/transport/http= /proxy/ProxyAuthenticationTestCase.java (rev 0) +++ remoting2/branches/2.x/src/tests/org/jboss/test/remoting/transport/http= /proxy/ProxyAuthenticationTestCase.java 2008-10-29 07:06:55 UTC (rev 4631) @@ -0,0 +1,296 @@ +/* +* JBoss, Home of Professional Open Source +* Copyright 2005, 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.transport.http.proxy; + +import java.io.DataOutputStream; +import java.io.EOFException; +import java.io.InputStreamReader; +import java.net.InetAddress; +import java.net.ServerSocket; +import java.net.Socket; +import java.util.HashMap; +import java.util.Map; + +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.logging.XLevel; +import org.jboss.remoting.Client; +import org.jboss.remoting.InvokerLocator; +import org.jboss.remoting.util.SecurityUtility; +import org.jboss.util.Base64; + + +/** + * Unit test for JBREM-1051. + * = + * @author Ron Sigal + * @version $Revision: 1.1 $ + *

+ * Copyright Oct 29, 2008 + *

+ */ +public class ProxyAuthenticationTestCase extends TestCase +{ + private static Logger log =3D Logger.getLogger(ProxyAuthenticationTestC= ase.class); + = + private static boolean firstTime =3D true; + private static String syspropAuth =3D "Basic " + Base64.encodeBytes("s= ysprop:abc".getBytes()); + private static String metadataAuth =3D "Basic " + Base64.encodeBytes("m= etadata:xyz".getBytes()); + = + protected TestHttpServer server; + + = + public void setUp() throws Exception + { + if (firstTime) + { + firstTime =3D false; + Logger.getLogger("org.jboss.remoting").setLevel(XLevel.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); = + }; + } + = + = + /** + * Tests behavior with proxy and authorization information stored in sy= stem properties. + */ + public void testProxySyspropAuthSysprop() throws Throwable + { + log.info("entering " + getName()); + = + // Start server. + setupServer(); + = + // Set system properties. + System.setProperty("http.proxyHost", server.host); + System.setProperty("http.proxyPort", Integer.toString(server.port)); + System.setProperty("proxySet", "true"); + System.setProperty("http.proxy.username", "sysprop"); + System.setProperty("http.proxy.password", "abc"); + = + // Create invocation metadata map. + HashMap metadata =3D new HashMap(); + metadata.put(Client.RAW, "true"); + = + // Run test. + doTest(metadata, syspropAuth); + log.info(getName() + " PASSES"); + } + = + = + /** + * Tests behavior with proxy information stored in system properties + * and authorization information stored in system properties and in ove= rriding + * invocation metadata map.. + */ + public void testProxySyspropAuthMeta() throws Throwable + { + log.info("entering " + getName()); + = + // Start server. + setupServer(); + = + // Set system properties. + SecurityUtility.setSystemProperty("http.proxyHost", server.host); + SecurityUtility.setSystemProperty("http.proxyPort", Integer.toString= (server.port)); + SecurityUtility.setSystemProperty("proxySet", "true"); + SecurityUtility.setSystemProperty("http.proxy.username", "sysprop"); + SecurityUtility.setSystemProperty("http.proxy.password", "abc"); + = + // Create invocation metadata map. + HashMap metadata =3D new HashMap(); + metadata.put(Client.RAW, "true"); + metadata.put("http.proxy.username", "metadata"); + metadata.put("http.proxy.password", "xyz"); + = + // Run test. + doTest(metadata, metadataAuth); + log.info(getName() + " PASSES"); + } + = + = + /** + * Tests behavior with proxy information stored in invocation metadata = map + * and authorization information stored in system properties. + */ + public void testProxyMetaAuthSysprop() throws Throwable + { + log.info("entering " + getName()); + = + // Start server. + setupServer(); + = + // Set system properties. + SecurityUtility.setSystemProperty("http.proxy.username", "sysprop"); + SecurityUtility.setSystemProperty("http.proxy.password", "abc"); = + = + // Create invocation metadata map. + HashMap metadata =3D new HashMap(); + metadata.put(Client.RAW, "true"); + metadata.put("http.proxyHost", server.host); + metadata.put("http.proxyPort", Integer.toString(server.port)); + = + // Run test. + doTest(metadata, syspropAuth); + log.info(getName() + " PASSES"); + } + = + = + /** + * Tests behavior with proxy information stored in invocation metadata = map + * and authorization information stored in system properties and overri= ding + * invocation metadata map. + */ + public void testProxyMetaAuthMeta() throws Throwable + { + log.info("entering " + getName()); + = + // Start server. + setupServer(); + = + // Set system properties. + SecurityUtility.setSystemProperty("http.proxy.username", "sysprop"); + SecurityUtility.setSystemProperty("http.proxy.password", "abc"); = + = + // Create invocation metadata map. + HashMap metadata =3D new HashMap(); + metadata.put(Client.RAW, "true"); + metadata.put("http.proxyHost", server.host); + metadata.put("http.proxyPort", Integer.toString(server.port)); + metadata.put("http.proxy.username", "metadata"); + metadata.put("http.proxy.password", "xyz"); + = + // Run test. + doTest(metadata, metadataAuth); + log.info(getName() + " PASSES"); + } + = + = + protected void setupServer() throws Exception + { + server =3D new TestHttpServer(); + server.start(); + synchronized (TestHttpServer.class) + { + TestHttpServer.class.wait(); + } + log.info("started server"); + } + = + = + protected void doTest(Map metadata, String auth) throws Throwable + { = + // Create client. + String locatorURI =3D "http://" + server.host + ":" + server.port; + log.info("connecting to " + locatorURI); + InvokerLocator clientLocator =3D new InvokerLocator(locatorURI); + HashMap config =3D new HashMap(); + config.put(InvokerLocator.FORCE_REMOTE, "true"); + Client client =3D new Client(clientLocator, config); + client.connect(); + log.info("client is connected"); + client.invoke("abc", metadata); + = + // Verify correct authorization was sent. + assertEquals(auth, server.auth); + client.disconnect(); + } + = + = + static class TestHttpServer extends Thread + { + public String host; + public int port; + public String auth; + = + public void run() + { = + try + { + log.info("starting HTTP server"); + InetAddress localHost =3D InetAddress.getLocalHost(); + final ServerSocket ss =3D new ServerSocket(0, 100, localHost); + host =3D localHost.getHostAddress(); + port =3D ss.getLocalPort(); + synchronized (TestHttpServer.class) + { + TestHttpServer.class.notify(); + } + Socket s =3D ss.accept(); + = + InputStreamReader ir =3D new InputStreamReader(s.getInputStrea= m()); + char[] cbuf =3D new char[1024]; + int len =3D ir.read(cbuf); + String request =3D String.copyValueOf(cbuf, 0, len); + log.info("Request:"); + System.out.println(); + System.out.println(request); + System.out.println(); + auth =3D getAuth(request); + = + DataOutputStream dos =3D new DataOutputStream(s.getOutputStrea= m()); + dos.writeBytes("HTTP/1.1 200 OK" + "\r\n"); + dos.writeBytes("Server: testServer"); + dos.writeBytes("Content-Type: text/html" + "\r\n"); + dos.writeBytes("Content-Length: 0\r\n"); + dos.writeBytes("Connection: close\r\n"); + dos.writeBytes("\r\n"); + = + ir.close(); + dos.close(); + s.close(); + ss.close(); + } + catch (EOFException e1) + { + log.info("end of file"); + } = + catch (Exception e2) + { + log.error("error", e2); + } + } + = + private String getAuth(String request) + { + String auth =3D null; + String[] tokens =3D request.split("[\r\n]+"); + for (int i =3D 0; i < tokens.length; i++) + { + if (tokens[i].startsWith("Proxy-Authorization")) + { + auth =3D tokens[i].split(":[ ]*")[1]; + break; + } + } + return auth; + } + } +} \ No newline at end of file --===============3170290354910634554==--