diff options
author | Omair Majid <[email protected]> | 2013-09-23 12:34:25 -0400 |
---|---|---|
committer | Omair Majid <[email protected]> | 2013-09-23 12:34:25 -0400 |
commit | 0b4378d462bf9e44afe71d8c87adf682dd116c50 (patch) | |
tree | c91b0ace61b30e086d54c2a09f3d748a09c8461a | |
parent | 9ba69baff18dd24b207e0a8b6ad76133fe4ea536 (diff) |
Unit tests for JNLPProxySelector
This contains one functional change:
- String host = uri.getSchemeSpecificPart().split(":")[0];
+ String host = uri.getHost();
Given the URI of "socket://example.org", the first line
evaluates to "//example.org", while the second one (correctly)
evaluates to "example.org".
8 files changed, 363 insertions, 15 deletions
@@ -1,3 +1,26 @@ +2013-09-23 Omair Majid <[email protected]> + + * netx/net/sourceforge/jnlp/browser/BrowserAwareProxySelector.java + (BrowserAwareProxySelector): Rename to... + (BrowserAwareProxySelector(DeploymentConfiguration)): New method. + * netx/net/sourceforge/jnlp/config/DeploymentConfiguration.java: Clarify + possible values for KEY_PROXY_TYPE. + * netx/net/sourceforge/jnlp/runtime/JNLPProxySelector.java + (JNLPProxySelector): Rename to... + (JNLPProxySelector(DeploymentConfiguration)): New method. + (parseConfiguration): Rename to... + (parseConfiguration(DeploymentConfiguration)): New method. + (inBypassList): Get host from URI instead of manual hacks. + (getProxiesFromPacResult): Clarify return value. + * netx/net/sourceforge/jnlp/runtime/JNLPRuntime.java + (initialize): Adjust for new BrowserAwareProxySelector constructor. + * plugin/icedteanp/java/sun/applet/PluginMain.java + (init): Adjust for new PluginProxySelector constructor. + * plugin/icedteanp/java/sun/applet/PluginProxySelector.java + (PluginProxySelector): New constructor. + * tests/netx/unit/net/sourceforge/jnlp/runtime/JNLPProxySelectorTest.java: + New file. + 2013-09-20 Omair Majid <[email protected]> * netx/net/sourceforge/jnlp/InformationDesc.java diff --git a/netx/net/sourceforge/jnlp/browser/BrowserAwareProxySelector.java b/netx/net/sourceforge/jnlp/browser/BrowserAwareProxySelector.java index 831aee0..835444b 100644 --- a/netx/net/sourceforge/jnlp/browser/BrowserAwareProxySelector.java +++ b/netx/net/sourceforge/jnlp/browser/BrowserAwareProxySelector.java @@ -52,6 +52,7 @@ import java.util.Arrays; import java.util.List; import java.util.Map; +import net.sourceforge.jnlp.config.DeploymentConfiguration; import net.sourceforge.jnlp.runtime.JNLPProxySelector; import net.sourceforge.jnlp.runtime.JNLPRuntime; import net.sourceforge.jnlp.runtime.PacEvaluator; @@ -92,8 +93,8 @@ public class BrowserAwareProxySelector extends JNLPProxySelector { /** * Create a new instance of this class, reading configuration fropm the browser */ - public BrowserAwareProxySelector() { - super(); + public BrowserAwareProxySelector(DeploymentConfiguration config) { + super(config); try { initFromBrowserConfig(); } catch (IOException e) { diff --git a/netx/net/sourceforge/jnlp/config/DeploymentConfiguration.java b/netx/net/sourceforge/jnlp/config/DeploymentConfiguration.java index fd7cf9b..25aab36 100644 --- a/netx/net/sourceforge/jnlp/config/DeploymentConfiguration.java +++ b/netx/net/sourceforge/jnlp/config/DeploymentConfiguration.java @@ -36,8 +36,8 @@ import java.util.Properties; import java.util.Set; import javax.naming.ConfigurationException; -import net.sourceforge.jnlp.cache.CacheLRUWrapper; +import net.sourceforge.jnlp.cache.CacheLRUWrapper; import net.sourceforge.jnlp.runtime.JNLPRuntime; import net.sourceforge.jnlp.util.FileUtils; @@ -126,6 +126,8 @@ public final class DeploymentConfiguration { /* * Networking */ + + /** the proxy type. possible values are {@code JNLPProxySelector.PROXY_TYPE_*} */ public static final String KEY_PROXY_TYPE = "deployment.proxy.type"; public static final String KEY_PROXY_SAME = "deployment.proxy.same"; public static final String KEY_PROXY_AUTO_CONFIG_URL = "deployment.proxy.auto.config.url"; diff --git a/netx/net/sourceforge/jnlp/runtime/JNLPProxySelector.java b/netx/net/sourceforge/jnlp/runtime/JNLPProxySelector.java index db5a9b7..b8fd25d 100644 --- a/netx/net/sourceforge/jnlp/runtime/JNLPProxySelector.java +++ b/netx/net/sourceforge/jnlp/runtime/JNLPProxySelector.java @@ -82,19 +82,14 @@ public abstract class JNLPProxySelector extends ProxySelector { // FIXME what is this? where should it be used? private String overrideHosts = null; - /** - * Creates a new JNLPProxySelector. - */ - public JNLPProxySelector() { - parseConfiguration(); + public JNLPProxySelector(DeploymentConfiguration config) { + parseConfiguration(config); } /** * Initialize this ProxySelector by reading the configuration */ - private void parseConfiguration() { - DeploymentConfiguration config = JNLPRuntime.getConfiguration(); - + private void parseConfiguration(DeploymentConfiguration config) { proxyType = Integer.valueOf(config.getProperty(DeploymentConfiguration.KEY_PROXY_TYPE)); String autoConfigString = config.getProperty(DeploymentConfiguration.KEY_PROXY_AUTO_CONFIG_URL); @@ -240,7 +235,7 @@ public abstract class JNLPProxySelector extends ProxySelector { return true; } } else if (scheme.equals("socket")) { - String host = uri.getSchemeSpecificPart().split(":")[0]; + String host = uri.getHost(); if (bypassLocal && isLocalHost(host)) { return true; @@ -368,7 +363,8 @@ public abstract class JNLPProxySelector extends ProxySelector { * suitable for java. * @param pacString a string indicating proxies. For example * "PROXY foo.bar:3128; DIRECT" - * @return a list of Proxy objects represeting the parsed string. + * @return a list of Proxy objects representing the parsed string. In + * case of malformed input, an empty list may be returned */ public static List<Proxy> getProxiesFromPacResult(String pacString) { List<Proxy> proxies = new ArrayList<Proxy>(); diff --git a/netx/net/sourceforge/jnlp/runtime/JNLPRuntime.java b/netx/net/sourceforge/jnlp/runtime/JNLPRuntime.java index 816807b..a2fcb7d 100644 --- a/netx/net/sourceforge/jnlp/runtime/JNLPRuntime.java +++ b/netx/net/sourceforge/jnlp/runtime/JNLPRuntime.java @@ -265,7 +265,7 @@ public class JNLPRuntime { // plug in a custom authenticator and proxy selector Authenticator.setDefault(new JNLPAuthenticator()); - ProxySelector.setDefault(new BrowserAwareProxySelector()); + ProxySelector.setDefault(new BrowserAwareProxySelector(config)); // Restrict access to netx classes Security.setProperty("package.access", diff --git a/plugin/icedteanp/java/sun/applet/PluginMain.java b/plugin/icedteanp/java/sun/applet/PluginMain.java index 3ceb119..3bfbfff 100644 --- a/plugin/icedteanp/java/sun/applet/PluginMain.java +++ b/plugin/icedteanp/java/sun/applet/PluginMain.java @@ -231,7 +231,7 @@ public class PluginMain { Authenticator.setDefault(new JNLPAuthenticator()); } // override the proxy selector set by JNLPRuntime - ProxySelector.setDefault(new PluginProxySelector()); + ProxySelector.setDefault(new PluginProxySelector(JNLPRuntime.getConfiguration())); } private static void setCookieHandler(PluginStreamHandler streamHandler) { diff --git a/plugin/icedteanp/java/sun/applet/PluginProxySelector.java b/plugin/icedteanp/java/sun/applet/PluginProxySelector.java index 32360fe..af6efc6 100644 --- a/plugin/icedteanp/java/sun/applet/PluginProxySelector.java +++ b/plugin/icedteanp/java/sun/applet/PluginProxySelector.java @@ -43,6 +43,7 @@ import java.net.URI; import java.util.ArrayList; import java.util.List; +import net.sourceforge.jnlp.config.DeploymentConfiguration; import net.sourceforge.jnlp.runtime.JNLPProxySelector; import net.sourceforge.jnlp.util.TimedHashMap; @@ -59,6 +60,10 @@ public class PluginProxySelector extends JNLPProxySelector { private TimedHashMap<String, Proxy> proxyCache = new TimedHashMap<String, Proxy>(); + public PluginProxySelector(DeploymentConfiguration config) { + super(config); + } + /** * Selects the appropriate proxy (or DIRECT connection method) for the given URI * diff --git a/tests/netx/unit/net/sourceforge/jnlp/runtime/JNLPProxySelectorTest.java b/tests/netx/unit/net/sourceforge/jnlp/runtime/JNLPProxySelectorTest.java new file mode 100644 index 0000000..d522772 --- /dev/null +++ b/tests/netx/unit/net/sourceforge/jnlp/runtime/JNLPProxySelectorTest.java @@ -0,0 +1,321 @@ +/* JNLPProxySelectorTest.java + Copyright (C) 2013 Red Hat, Inc. + +This file is part of IcedTea. + +IcedTea is free software; you can redistribute it and/or +modify it under the terms of the GNU General Public License as published by +the Free Software Foundation, version 2. + +IcedTea 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 +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with IcedTea; see the file COPYING. If not, write to +the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. +*/ + +package net.sourceforge.jnlp.runtime; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertSame; +import static org.junit.Assert.fail; + +import java.net.InetAddress; +import java.net.InetSocketAddress; +import java.net.Proxy; +import java.net.Proxy.Type; +import java.net.SocketAddress; +import java.net.URI; +import java.net.URISyntaxException; +import java.net.UnknownHostException; +import java.util.Arrays; +import java.util.List; + +import net.sourceforge.jnlp.config.DeploymentConfiguration; + +import org.junit.Ignore; +import org.junit.Test; + +public class JNLPProxySelectorTest { + + private static final Proxy BROWSER_PROXY = new Proxy(Type.SOCKS, InetSocketAddress.createUnresolved("foo", 0xF00)); + + class TestProxySelector extends JNLPProxySelector { + public TestProxySelector(DeploymentConfiguration config) { + super(config); + } + + @Override + protected List<Proxy> getFromBrowser(URI uri) { + return Arrays.asList(BROWSER_PROXY); + } + } + + @Test + public void testNoProxy() throws URISyntaxException { + DeploymentConfiguration config = new DeploymentConfiguration(); + config.setProperty(DeploymentConfiguration.KEY_PROXY_TYPE, String.valueOf(JNLPProxySelector.PROXY_TYPE_NONE)); + + JNLPProxySelector selector = new TestProxySelector(config); + List<Proxy> result = selector.select(new URI("http://example.org/")); + + assertEquals(1, result.size()); + assertEquals(Proxy.NO_PROXY, result.get(0)); + } + + @Test + public void testProxyBypassLocal() throws URISyntaxException, UnknownHostException { + final String LOCALHOST = InetAddress.getLocalHost().getHostName(); + + DeploymentConfiguration config = new DeploymentConfiguration(); + config.setProperty(DeploymentConfiguration.KEY_PROXY_TYPE, String.valueOf(JNLPProxySelector.PROXY_TYPE_MANUAL)); + config.setProperty(DeploymentConfiguration.KEY_PROXY_BYPASS_LOCAL, String.valueOf(true)); + + List<Proxy> result; + JNLPProxySelector selector = new TestProxySelector(config); + + result = selector.select(new URI("http://127.0.0.1/")); + + assertEquals(1, result.size()); + assertEquals(Proxy.NO_PROXY, result.get(0)); + + result = selector.select(new URI("http://" + LOCALHOST + "/")); + + assertEquals(1, result.size()); + assertEquals(Proxy.NO_PROXY, result.get(0)); + + result = selector.select(new URI("socket://127.0.0.1/")); + + assertEquals(1, result.size()); + assertEquals(Proxy.NO_PROXY, result.get(0)); + + result = selector.select(new URI("socket://" + LOCALHOST + "/")); + + assertEquals(1, result.size()); + assertEquals(Proxy.NO_PROXY, result.get(0)); + } + + @Test + public void testLocalProxyBypassListIsIgnoredForNonLocal() { + + } + + @Test + public void testProxyBypassList() throws URISyntaxException { + DeploymentConfiguration config = new DeploymentConfiguration(); + config.setProperty(DeploymentConfiguration.KEY_PROXY_TYPE, String.valueOf(JNLPProxySelector.PROXY_TYPE_MANUAL)); + config.setProperty(DeploymentConfiguration.KEY_PROXY_BYPASS_LIST, "example.org"); + + JNLPProxySelector selector = new TestProxySelector(config); + List<Proxy> result; + + result = selector.select(new URI("http://example.org/")); + + assertEquals(1, result.size()); + assertEquals(Proxy.NO_PROXY, result.get(0)); + + result = selector.select(new URI("socket://example.org/")); + + assertEquals(1, result.size()); + assertEquals(Proxy.NO_PROXY, result.get(0)); + } + + @Test + public void testManualHttpProxy() throws URISyntaxException { + String HTTP_HOST = "example.org"; + int HTTP_PORT = 42; + + DeploymentConfiguration config = new DeploymentConfiguration(); + config.setProperty(DeploymentConfiguration.KEY_PROXY_TYPE, String.valueOf(JNLPProxySelector.PROXY_TYPE_MANUAL)); + config.setProperty(DeploymentConfiguration.KEY_PROXY_HTTP_HOST, HTTP_HOST); + config.setProperty(DeploymentConfiguration.KEY_PROXY_HTTP_PORT, String.valueOf(HTTP_PORT)); + + JNLPProxySelector selector = new TestProxySelector(config); + List<Proxy> result = selector.select(new URI("http://example.org/")); + + assertEquals(1, result.size()); + assertEquals(new Proxy(Type.HTTP, new InetSocketAddress(HTTP_HOST, HTTP_PORT)), result.get(0)); + } + + @Test + public void testManualHttpsProxy() throws URISyntaxException { + String HTTPS_HOST = "example.org"; + int HTTPS_PORT = 42; + + DeploymentConfiguration config = new DeploymentConfiguration(); + config.setProperty(DeploymentConfiguration.KEY_PROXY_TYPE, String.valueOf(JNLPProxySelector.PROXY_TYPE_MANUAL)); + config.setProperty(DeploymentConfiguration.KEY_PROXY_HTTPS_HOST, HTTPS_HOST); + config.setProperty(DeploymentConfiguration.KEY_PROXY_HTTPS_PORT, String.valueOf(HTTPS_PORT)); + + JNLPProxySelector selector = new TestProxySelector(config); + List<Proxy> result = selector.select(new URI("https://example.org/")); + + assertEquals(1, result.size()); + assertEquals(new Proxy(Type.HTTP, new InetSocketAddress(HTTPS_HOST, HTTPS_PORT)), result.get(0)); + } + + @Test + public void testManualFtpProxy() throws URISyntaxException { + String FTP_HOST = "example.org"; + int FTP_PORT = 42; + + DeploymentConfiguration config = new DeploymentConfiguration(); + config.setProperty(DeploymentConfiguration.KEY_PROXY_TYPE, String.valueOf(JNLPProxySelector.PROXY_TYPE_MANUAL)); + config.setProperty(DeploymentConfiguration.KEY_PROXY_FTP_HOST, FTP_HOST); + config.setProperty(DeploymentConfiguration.KEY_PROXY_FTP_PORT, String.valueOf(FTP_PORT)); + + JNLPProxySelector selector = new TestProxySelector(config); + List<Proxy> result = selector.select(new URI("ftp://example.org/")); + + assertEquals(1, result.size()); + assertEquals(new Proxy(Type.HTTP, new InetSocketAddress(FTP_HOST, FTP_PORT)), result.get(0)); + } + + @Test + public void testManualSocksProxy() throws URISyntaxException { + String SOCKS_HOST = "example.org"; + int SOCKS_PORT = 42; + + DeploymentConfiguration config = new DeploymentConfiguration(); + config.setProperty(DeploymentConfiguration.KEY_PROXY_TYPE, String.valueOf(JNLPProxySelector.PROXY_TYPE_MANUAL)); + config.setProperty(DeploymentConfiguration.KEY_PROXY_SOCKS4_HOST, SOCKS_HOST); + config.setProperty(DeploymentConfiguration.KEY_PROXY_SOCKS4_PORT, String.valueOf(SOCKS_PORT)); + + JNLPProxySelector selector = new TestProxySelector(config); + List<Proxy> result = selector.select(new URI("socket://example.org/")); + + assertEquals(1, result.size()); + assertEquals(new Proxy(Type.SOCKS, new InetSocketAddress(SOCKS_HOST, SOCKS_PORT)), result.get(0)); + } + + @Test + public void testManualUnknownProtocolProxy() throws URISyntaxException { + DeploymentConfiguration config = new DeploymentConfiguration(); + config.setProperty(DeploymentConfiguration.KEY_PROXY_TYPE, String.valueOf(JNLPProxySelector.PROXY_TYPE_MANUAL)); + + JNLPProxySelector selector = new TestProxySelector(config); + List<Proxy> result = selector.select(new URI("gopher://example.org/")); + + assertEquals(1, result.size()); + assertEquals(Proxy.NO_PROXY, result.get(0)); + } + + @Test + public void testManualSameProxy() throws URISyntaxException { + final String HTTP_HOST = "example.org"; + final int HTTP_PORT = 42; + + DeploymentConfiguration config = new DeploymentConfiguration(); + config.setProperty(DeploymentConfiguration.KEY_PROXY_TYPE, String.valueOf(JNLPProxySelector.PROXY_TYPE_MANUAL)); + config.setProperty(DeploymentConfiguration.KEY_PROXY_HTTP_HOST, HTTP_HOST); + config.setProperty(DeploymentConfiguration.KEY_PROXY_HTTP_PORT, String.valueOf(HTTP_PORT)); + config.setProperty(DeploymentConfiguration.KEY_PROXY_SAME, String.valueOf(true)); + + JNLPProxySelector selector = new TestProxySelector(config); + List<Proxy> result; + + result = selector.select(new URI("http://example.org/")); + + assertEquals(1, result.size()); + assertEquals(new Proxy(Type.HTTP, new InetSocketAddress(HTTP_HOST, HTTP_PORT)), result.get(0)); + + result = selector.select(new URI("socket://example.org/")); + + assertEquals(1, result.size()); + assertEquals(new Proxy(Type.SOCKS, new InetSocketAddress(HTTP_HOST, HTTP_PORT)), result.get(0)); + } + + @Test + public void testBrowserProxy() throws URISyntaxException { + DeploymentConfiguration config = new DeploymentConfiguration(); + config.setProperty(DeploymentConfiguration.KEY_PROXY_TYPE, String.valueOf(JNLPProxySelector.PROXY_TYPE_BROWSER)); + + JNLPProxySelector selector = new TestProxySelector(config); + List<Proxy> result = selector.select(new URI("http://example.org/")); + + assertEquals(1, result.size()); + assertSame(BROWSER_PROXY, result.get(0)); + } + + @Test + public void testMissingProxyAutoConfigUrl() throws URISyntaxException { + DeploymentConfiguration config = new DeploymentConfiguration(); + config.setProperty(DeploymentConfiguration.KEY_PROXY_TYPE, String.valueOf(JNLPProxySelector.PROXY_TYPE_AUTO)); + + JNLPProxySelector selector = new TestProxySelector(config); + List<Proxy> result = selector.select(new URI("http://example.org/")); + + assertEquals(1, result.size()); + assertEquals(Proxy.NO_PROXY, result.get(0)); + } + + // TODO + @Ignore("Need to find a way to inject a custom proxy autoconfig file first") + @Test + public void testProxyAutoConfig() throws URISyntaxException { + DeploymentConfiguration config = new DeploymentConfiguration(); + config.setProperty(DeploymentConfiguration.KEY_PROXY_TYPE, String.valueOf(JNLPProxySelector.PROXY_TYPE_AUTO)); + config.setProperty(DeploymentConfiguration.KEY_PROXY_AUTO_CONFIG_URL, "foobar"); + + JNLPProxySelector selector = new TestProxySelector(config); + List<Proxy> result = selector.select(new URI("http://example.org/")); + + assertEquals(1, result.size()); + } + + // TODO this JNLPProxySelect#getProxiesFromPacResult should be moved into a different class + // TODO this test should be split into different methods + @Test + public void testConvertingProxyAutoConfigResultToProxyObject() { + List<Proxy> result; + + result = JNLPProxySelector.getProxiesFromPacResult("foo bar baz; what is this; dunno"); + assertEquals(0, result.size()); + + result = JNLPProxySelector.getProxiesFromPacResult("DIRECT"); + assertEquals(1, result.size()); + assertEquals(Proxy.NO_PROXY, result.get(0)); + + result = JNLPProxySelector.getProxiesFromPacResult("PROXY foo:42"); + assertEquals(1, result.size()); + assertEquals(new Proxy(Type.HTTP, InetSocketAddress.createUnresolved("foo", 42)), result.get(0)); + + result = JNLPProxySelector.getProxiesFromPacResult("PROXY foo:bar"); + assertEquals(0, result.size()); + + result = JNLPProxySelector.getProxiesFromPacResult("PROXY foo"); + assertEquals(0, result.size()); + + result = JNLPProxySelector.getProxiesFromPacResult("SOCKS foo:42"); + assertEquals(1, result.size()); + assertEquals(new Proxy(Type.SOCKS, InetSocketAddress.createUnresolved("foo", 42)), result.get(0)); + + result = JNLPProxySelector.getProxiesFromPacResult("SOCKS foo:bar"); + assertEquals(0, result.size()); + + result = JNLPProxySelector.getProxiesFromPacResult("SOCKS foo"); + assertEquals(0, result.size()); + + } +} |