summaryrefslogtreecommitdiffstats
path: root/src/test/java/com/btr/proxy/selector
diff options
context:
space:
mode:
Diffstat (limited to 'src/test/java/com/btr/proxy/selector')
-rw-r--r--src/test/java/com/btr/proxy/selector/fixed/FixedProxyTest.java56
-rw-r--r--src/test/java/com/btr/proxy/selector/java/JavaProxySelectorTest.java28
-rw-r--r--src/test/java/com/btr/proxy/selector/misc/ProtocolDispatchTest.java87
-rw-r--r--src/test/java/com/btr/proxy/selector/misc/ProxyListFallbackSelectorTest.java94
-rw-r--r--src/test/java/com/btr/proxy/selector/pac/JavaxPacScriptParserTest.java119
-rw-r--r--src/test/java/com/btr/proxy/selector/pac/PacPerProtocolTest.java47
-rw-r--r--src/test/java/com/btr/proxy/selector/pac/PacProxyDebugging.java103
-rw-r--r--src/test/java/com/btr/proxy/selector/pac/PacProxySelectorTest.java133
-rw-r--r--src/test/java/com/btr/proxy/selector/pac/PacScriptMethodsTest.java165
-rw-r--r--src/test/java/com/btr/proxy/selector/pac/RhinoPacScriptParserTest.java114
-rw-r--r--src/test/java/com/btr/proxy/selector/pac/UrlPacScriptSourceTest.java34
-rw-r--r--src/test/java/com/btr/proxy/selector/whitelist/NoProxyTest.java108
12 files changed, 1088 insertions, 0 deletions
diff --git a/src/test/java/com/btr/proxy/selector/fixed/FixedProxyTest.java b/src/test/java/com/btr/proxy/selector/fixed/FixedProxyTest.java
new file mode 100644
index 0000000..c4a7d08
--- /dev/null
+++ b/src/test/java/com/btr/proxy/selector/fixed/FixedProxyTest.java
@@ -0,0 +1,56 @@
+package com.btr.proxy.selector.fixed;
+
+import static org.junit.Assert.*;
+
+import java.net.Proxy;
+import java.net.ProxySelector;
+import java.util.List;
+
+import org.junit.Test;
+
+import com.btr.proxy.TestUtil;
+
+
+/*****************************************************************************
+ *
+ * @author Bernd Rosstauscher (proxyvole@rosstauscher.de) Copyright 2009
+ ****************************************************************************/
+
+public class FixedProxyTest {
+
+ /*************************************************************************
+ * Test method
+ ************************************************************************/
+ @Test
+ public void testFixedProxy() {
+ ProxySelector ps = new FixedProxySelector("http_proxy.unit-test.invalid", 8090);
+
+ List<Proxy> result = ps.select(TestUtil.HTTP_TEST_URI);
+ assertEquals(TestUtil.HTTP_TEST_PROXY, result.get(0));
+ }
+
+ /*************************************************************************
+ * Test method
+ ************************************************************************/
+ @Test
+ public void testFixedProxy2() {
+ ProxySelector ps = new FixedProxySelector(TestUtil.HTTP_TEST_PROXY);
+
+ List<Proxy> result = ps.select(TestUtil.HTTP_TEST_URI);
+ assertEquals(TestUtil.HTTP_TEST_PROXY, result.get(0));
+ }
+
+ /*************************************************************************
+ * Test method
+ ************************************************************************/
+ @Test
+ public void testFixedProxy3() {
+ ProxySelector ps = new FixedProxySelector(TestUtil.HTTP_TEST_PROXY);
+
+ List<Proxy> result = ps.select(TestUtil.HTTPS_TEST_URI);
+ assertEquals(TestUtil.HTTP_TEST_PROXY, result.get(0));
+ }
+
+
+}
+
diff --git a/src/test/java/com/btr/proxy/selector/java/JavaProxySelectorTest.java b/src/test/java/com/btr/proxy/selector/java/JavaProxySelectorTest.java
new file mode 100644
index 0000000..5701e25
--- /dev/null
+++ b/src/test/java/com/btr/proxy/selector/java/JavaProxySelectorTest.java
@@ -0,0 +1,28 @@
+package com.btr.proxy.selector.java;
+
+import static org.junit.Assert.*;
+
+import java.net.ProxySelector;
+
+import org.junit.Test;
+
+import com.btr.proxy.search.java.JavaProxySearchStrategy;
+
+/*****************************************************************************
+ * Some unit tests for the Java Proxy search strategy.
+ * @author Bernd Rosstauscher (proxyvole@rosstauscher.de) Copyright 2009
+ ****************************************************************************/
+
+public class JavaProxySelectorTest {
+
+ /*************************************************************************
+ * Test method
+ ************************************************************************/
+ @Test
+ public void withoutSystemPropertyShouldReturnNull() {
+ ProxySelector ps = new JavaProxySearchStrategy().getProxySelector();
+ assertNull(ps);
+ }
+
+}
+
diff --git a/src/test/java/com/btr/proxy/selector/misc/ProtocolDispatchTest.java b/src/test/java/com/btr/proxy/selector/misc/ProtocolDispatchTest.java
new file mode 100644
index 0000000..da27327
--- /dev/null
+++ b/src/test/java/com/btr/proxy/selector/misc/ProtocolDispatchTest.java
@@ -0,0 +1,87 @@
+package com.btr.proxy.selector.misc;
+
+import static org.junit.Assert.*;
+
+import java.net.Proxy;
+import java.util.List;
+
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+import com.btr.proxy.TestUtil;
+import com.btr.proxy.selector.fixed.FixedProxySelector;
+
+/*****************************************************************************
+ * Unit Tests for the ProtocolDispatchSelector
+ *
+ * @author Bernd Rosstauscher (proxyvole@rosstauscher.de) Copyright 2009
+ ****************************************************************************/
+
+public class ProtocolDispatchTest {
+
+ private static ProtocolDispatchSelector ps;
+
+ @BeforeClass
+ public static void setup() {
+ ps = new ProtocolDispatchSelector();
+ ps.setSelector("http", new FixedProxySelector(TestUtil.HTTP_TEST_PROXY));
+ ps.setSelector("https", new FixedProxySelector(TestUtil.HTTPS_TEST_PROXY));
+ ps.setSelector("ftp", new FixedProxySelector(TestUtil.FTP_TEST_PROXY));
+ }
+
+ /*************************************************************************
+ * Test method
+ ************************************************************************/
+ @Test
+ public void testDispatchHttp() {
+ List<Proxy> result = ps.select(TestUtil.HTTP_TEST_URI);
+ assertEquals(TestUtil.HTTP_TEST_PROXY, result.get(0));
+ }
+
+ /*************************************************************************
+ * Test method
+ ************************************************************************/
+ @Test
+ public void testDispatchHttps() {
+ List<Proxy> result = ps.select(TestUtil.HTTPS_TEST_URI);
+ assertEquals(TestUtil.HTTPS_TEST_PROXY, result.get(0));
+ }
+
+ /*************************************************************************
+ * Test method
+ ************************************************************************/
+ @Test
+ public void testDispatchFtp() {
+ List<Proxy> result = ps.select(TestUtil.FTP_TEST_URI);
+ assertEquals(TestUtil.FTP_TEST_PROXY, result.get(0));
+ }
+
+ /*************************************************************************
+ * Test method
+ ************************************************************************/
+ @Test
+ public void testRemove() {
+ ProtocolDispatchSelector px = new ProtocolDispatchSelector();
+ FixedProxySelector selector = new FixedProxySelector(TestUtil.HTTP_TEST_PROXY);
+ px.setSelector("http", selector);
+ assertEquals(selector, px.getSelector("http"));
+ px.removeSelector("http");
+ assertNull(px.getSelector("http"));
+ }
+
+ /*************************************************************************
+ * Test method
+ ************************************************************************/
+ @Test
+ public void testFallback() {
+ ProtocolDispatchSelector px = new ProtocolDispatchSelector();
+ FixedProxySelector selector = new FixedProxySelector(TestUtil.HTTP_TEST_PROXY);
+ px.setFallbackSelector(selector);
+
+ List<Proxy> proxies = px.select(TestUtil.HTTP_TEST_URI);
+
+ assertEquals(TestUtil.HTTP_TEST_PROXY, proxies.get(0));
+ }
+
+}
+
diff --git a/src/test/java/com/btr/proxy/selector/misc/ProxyListFallbackSelectorTest.java b/src/test/java/com/btr/proxy/selector/misc/ProxyListFallbackSelectorTest.java
new file mode 100644
index 0000000..12c172c
--- /dev/null
+++ b/src/test/java/com/btr/proxy/selector/misc/ProxyListFallbackSelectorTest.java
@@ -0,0 +1,94 @@
+package com.btr.proxy.selector.misc;
+
+import static org.junit.Assert.*;
+
+import java.io.IOException;
+import java.net.Proxy;
+import java.net.ProxySelector;
+import java.net.SocketAddress;
+import java.net.URI;
+import java.util.Arrays;
+import java.util.List;
+
+import org.junit.Before;
+import org.junit.Test;
+
+import com.btr.proxy.TestUtil;
+
+/*****************************************************************************
+ * Unit Tests for the ProxyListFallbackSelector
+ *
+ * @author Bernd Rosstauscher (proxyvole@rosstauscher.de) Copyright 2011
+ ****************************************************************************/
+
+public class ProxyListFallbackSelectorTest {
+
+ private ProxyListFallbackSelector selector;
+
+ /*************************************************************************
+ * Setup before tests.
+ ************************************************************************/
+ @Before
+ public void setup() {
+ this.selector = new ProxyListFallbackSelector(
+ new ProxySelector() {
+ @Override
+ public List<Proxy> select(URI uri) {
+ return Arrays.asList(TestUtil.HTTP_TEST_PROXY, TestUtil.HTTPS_TEST_PROXY, Proxy.NO_PROXY);
+ }
+ @Override
+ public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
+ // Not used on the delegate
+ }
+ });
+ }
+
+ /*************************************************************************
+ * Test method
+ ************************************************************************/
+ @Test
+ public void testList() {
+ List<Proxy> result = this.selector.select(TestUtil.HTTP_TEST_URI);
+ assertEquals(TestUtil.HTTP_TEST_PROXY, result.get(0));
+ assertEquals(TestUtil.HTTPS_TEST_PROXY, result.get(1));
+ assertEquals(Proxy.NO_PROXY, result.get(2));
+ }
+
+ /*************************************************************************
+ * Test method
+ ************************************************************************/
+ @Test
+ public void testFailedProxy() {
+ this.selector.connectFailed(
+ TestUtil.HTTP_TEST_URI,
+ TestUtil.HTTP_TEST_PROXY.address(),
+ new IOException("TEST"));
+
+ List<Proxy> result = this.selector.select(TestUtil.HTTP_TEST_URI);
+
+ assertEquals(TestUtil.HTTPS_TEST_PROXY, result.get(0));
+ assertEquals(Proxy.NO_PROXY, result.get(1));
+ }
+
+ /*************************************************************************
+ * Test method
+ * @throws InterruptedException if the test wait period was interrupted
+ ************************************************************************/
+ @Test
+ public void testFailedProxyRetry() throws InterruptedException {
+ this.selector.setRetryAfterMs(100);
+ this.selector.connectFailed(
+ TestUtil.HTTP_TEST_URI,
+ TestUtil.HTTP_TEST_PROXY.address(),
+ new IOException("TEST"));
+
+ List<Proxy> result = this.selector.select(TestUtil.HTTP_TEST_URI);
+ assertEquals(2, result.size());
+
+ Thread.sleep(200);
+ result = this.selector.select(TestUtil.HTTP_TEST_URI);
+ assertEquals(3, result.size());
+ }
+
+}
+
diff --git a/src/test/java/com/btr/proxy/selector/pac/JavaxPacScriptParserTest.java b/src/test/java/com/btr/proxy/selector/pac/JavaxPacScriptParserTest.java
new file mode 100644
index 0000000..ea963f0
--- /dev/null
+++ b/src/test/java/com/btr/proxy/selector/pac/JavaxPacScriptParserTest.java
@@ -0,0 +1,119 @@
+package com.btr.proxy.selector.pac;
+
+import java.net.MalformedURLException;
+import java.util.Calendar;
+
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.Ignore;
+import org.junit.Test;
+
+import com.btr.proxy.TestUtil;
+import com.btr.proxy.util.ProxyException;
+
+/*****************************************************************************
+ * Tests for the javax.script PAC script parser.
+ * @author Bernd Rosstauscher (proxyvole@rosstauscher.de) Copyright 2009
+ ****************************************************************************/
+
+public class JavaxPacScriptParserTest {
+
+ /*************************************************************************
+ * Set calendar for date and time base tests.
+ * Current date for all tests is: 15. December 1994 12:00.00
+ * its a Thursday
+ ************************************************************************/
+ @BeforeClass
+ public static void setup() {
+ Calendar cal = Calendar.getInstance();
+ cal.set(Calendar.YEAR, 1994);
+ cal.set(Calendar.MONTH, Calendar.DECEMBER);
+ cal.set(Calendar.DAY_OF_MONTH, 15);
+ cal.set(Calendar.HOUR_OF_DAY, 12);
+ cal.set(Calendar.MINUTE, 00);
+ cal.set(Calendar.SECOND, 00);
+ cal.set(Calendar.MILLISECOND, 00);
+
+ // TODO Rossi 26.08.2010 need to fake time
+ // JavaxPacScriptParser.setCurrentTime(cal);
+ }
+
+ /*************************************************************************
+ * Cleanup after the tests.
+ ************************************************************************/
+ @AfterClass
+ public static void teadDown() {
+ //JavaxPacScriptParser.setCurrentTime(null);
+ }
+
+ /*************************************************************************
+ * Test method
+ * @throws ProxyException on proxy detection error.
+ * @throws MalformedURLException on URL erros
+ ************************************************************************/
+ @Test
+ public void testScriptExecution() throws ProxyException, MalformedURLException {
+ PacScriptParser p = new JavaxPacScriptParser(new UrlPacScriptSource(toUrl("test1.pac")));
+ p.evaluate(TestUtil.HTTP_TEST_URI.toString(), "host1.unit-test.invalid");
+ }
+
+ /*************************************************************************
+ * Test method
+ * @throws ProxyException on proxy detection error.
+ * @throws MalformedURLException on URL erros
+ ************************************************************************/
+ @Test
+ public void testCommentsInScript() throws ProxyException, MalformedURLException {
+ PacScriptParser p = new JavaxPacScriptParser(new UrlPacScriptSource(toUrl("test2.pac")));
+ p.evaluate(TestUtil.HTTP_TEST_URI.toString(), "host1.unit-test.invalid");
+ }
+
+ /*************************************************************************
+ * Test method
+ * @throws ProxyException on proxy detection error.
+ * @throws MalformedURLException on URL erros
+ ************************************************************************/
+ @Test
+ @Ignore //Test deactivated because it will not run in Java 1.5 and time based test are unstable
+ public void testScriptWeekDayScript() throws ProxyException, MalformedURLException {
+ PacScriptParser p = new JavaxPacScriptParser(new UrlPacScriptSource(toUrl("testWeekDay.pac")));
+ p.evaluate(TestUtil.HTTP_TEST_URI.toString(), "host1.unit-test.invalid");
+ }
+
+ /*************************************************************************
+ * Test method
+ * @throws ProxyException on proxy detection error.
+ * @throws MalformedURLException on URL erros
+ ************************************************************************/
+ @Test
+ @Ignore //Test deactivated because it will not run in Java 1.5 and time based test are unstable
+ public void testDateRangeScript() throws ProxyException, MalformedURLException {
+ PacScriptParser p = new JavaxPacScriptParser(new UrlPacScriptSource(toUrl("testDateRange.pac")));
+ p.evaluate(TestUtil.HTTP_TEST_URI.toString(), "host1.unit-test.invalid");
+ }
+
+ /*************************************************************************
+ * Test method
+ * @throws ProxyException on proxy detection error.
+ * @throws MalformedURLException on URL erros
+ ************************************************************************/
+ @Test
+ @Ignore //Test deactivated because it will not run in Java 1.5 and time based test are unstable
+ public void testTimeRangeScript() throws ProxyException, MalformedURLException {
+ PacScriptParser p = new JavaxPacScriptParser(new UrlPacScriptSource(toUrl("testTimeRange.pac")));
+ p.evaluate(TestUtil.HTTP_TEST_URI.toString(), "host1.unit-test.invalid");
+ }
+
+ /*************************************************************************
+ * Helper method to build the url to the given test file
+ * @param testFile the name of the test file.
+ * @return the URL.
+ * @throws MalformedURLException
+ ************************************************************************/
+
+ private String toUrl(String testFile) throws MalformedURLException {
+ return JavaxPacScriptParserTest.class.getResource("/" + TestUtil.TEST_DATA_FOLDER + "/pac/" + testFile).toString();
+ }
+
+}
+
diff --git a/src/test/java/com/btr/proxy/selector/pac/PacPerProtocolTest.java b/src/test/java/com/btr/proxy/selector/pac/PacPerProtocolTest.java
new file mode 100644
index 0000000..c0b9c67
--- /dev/null
+++ b/src/test/java/com/btr/proxy/selector/pac/PacPerProtocolTest.java
@@ -0,0 +1,47 @@
+package com.btr.proxy.selector.pac;
+
+import static org.junit.Assert.assertEquals;
+
+import java.io.IOException;
+import java.net.MalformedURLException;
+import java.net.Proxy;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.util.List;
+
+import org.junit.Test;
+
+import com.btr.proxy.TestUtil;
+
+
+public class PacPerProtocolTest {
+
+ /*************************************************************************
+ * Test the PAC selector for a given protocol.
+ * @throws IOException of read error.
+ * @throws URISyntaxException on uri syntax error.
+ ************************************************************************/
+ @Test
+ public void testPacForSocket() throws IOException, URISyntaxException {
+
+ new URI("socket://host1.unit-test.invalid/");
+
+ List<Proxy> result = new PacProxySelector(
+ new UrlPacScriptSource(toUrl("test1.pac"))).select(TestUtil.SOCKET_TEST_URI);
+
+ assertEquals(TestUtil.HTTP_TEST_PROXY, result.get(0));
+ }
+
+ /*************************************************************************
+ * Helper method to build the url to the given test file
+ * @param testFile the name of the test file.
+ * @return the URL.
+ * @throws MalformedURLException
+ ************************************************************************/
+
+ private String toUrl(String testFile) throws MalformedURLException {
+ return PacPerProtocolTest.class.getResource("/" + TestUtil.TEST_DATA_FOLDER + "/pac/" + testFile).toString();
+ }
+
+}
+
diff --git a/src/test/java/com/btr/proxy/selector/pac/PacProxyDebugging.java b/src/test/java/com/btr/proxy/selector/pac/PacProxyDebugging.java
new file mode 100644
index 0000000..695bc3a
--- /dev/null
+++ b/src/test/java/com/btr/proxy/selector/pac/PacProxyDebugging.java
@@ -0,0 +1,103 @@
+package com.btr.proxy.selector.pac;
+
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.LineNumberReader;
+import java.net.ProxySelector;
+import java.net.URL;
+import java.net.URLConnection;
+import java.text.MessageFormat;
+
+import com.btr.proxy.search.ProxySearch;
+import com.btr.proxy.util.Logger;
+import com.btr.proxy.util.Logger.LogLevel;
+
+/*****************************************************************************
+ * Test program submitted to test the issue 27 with PAC proxy selector that is
+ * while downloading the PAC file invoking itself. This has lead to a endless
+ * loop. The issue is now solved but I keep this test program for future PAC
+ * testing.
+ *
+ * @author Bernd Rosstauscher (proxyvole@rosstauscher.de) Copyright 2009
+ ****************************************************************************/
+
+public class PacProxyDebugging {
+
+ private static final String TEST_URL = "http://www.asetune.com";
+
+ /*************************************************************************
+ * Setup a console logger.
+ ************************************************************************/
+
+ private void installLogger() {
+ Logger.setBackend(new Logger.LogBackEnd() {
+ public void log(Class<?> clazz, LogLevel loglevel, String msg, Object... params) {
+ System.out.println(loglevel + "\t"+ MessageFormat.format(msg, params));
+ }
+
+ public boolean isLogginEnabled(LogLevel logLevel) {
+ return true;
+ }
+ });
+ }
+
+ /*************************************************************************
+ * Main entry point for the test application.
+ * @param args the command line arguments.
+ ************************************************************************/
+
+ public static void main(String[] args) {
+ // System.setProperty("http.proxyHost", "10.65.12.21");
+ // System.setProperty("http.proxyPort", "8080");
+ // System.setProperty("java.net.useSystemProxies", "true");
+
+ PacProxyDebugging pt = new PacProxyDebugging();
+ pt.installLogger();
+
+ ProxySearch proxySearch = ProxySearch.getDefaultProxySearch();
+
+ // ProxySearch proxySearch = new ProxySearch();
+ // proxySearch.addStrategy(Strategy.JAVA);
+ // proxySearch.addStrategy(Strategy.BROWSER);
+ // proxySearch.addStrategy(Strategy.OS_DEFAULT);
+ // proxySearch.addStrategy(Strategy.ENV_VAR);
+
+ ProxySelector myProxySelector = proxySearch.getProxySelector();
+
+ ProxySelector.setDefault(myProxySelector);
+ System.out.println("Using proxy selector: " + myProxySelector);
+
+ // String webAddress = "http://www.google.com";
+ String webAddress = TEST_URL;
+ try {
+ URL url = new URL(webAddress);
+ // List<Proxy> result = myProxySelector.select(url.toURI());
+ // if (result == null || result.size() == 0)
+ // {
+ // System.out.println("No proxy found for this url.");
+ // return;
+ // }
+ // System.out.println("Proxy Settings found using 'xxx' strategy.\n"
+ // +
+ // "Proxy used for URL is: "+result.get(0));
+
+ System.out.println("Now open a connection to the url: "+ webAddress);
+ System.out.println("==============================================");
+
+ // open the connection and prepare it to POST
+ URLConnection conn = url.openConnection();
+ conn.setConnectTimeout(10 * 1000);
+
+ // Return the response
+ InputStream in = conn.getInputStream();
+ LineNumberReader lr = new LineNumberReader(new InputStreamReader(in));
+ String line;
+ while ((line = lr.readLine()) != null) {
+ System.out.println("response line " + lr.getLineNumber() + ": " + line);
+ }
+ System.out.println("---- END -------------------------------------");
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+}
diff --git a/src/test/java/com/btr/proxy/selector/pac/PacProxySelectorTest.java b/src/test/java/com/btr/proxy/selector/pac/PacProxySelectorTest.java
new file mode 100644
index 0000000..a08f9c1
--- /dev/null
+++ b/src/test/java/com/btr/proxy/selector/pac/PacProxySelectorTest.java
@@ -0,0 +1,133 @@
+package com.btr.proxy.selector.pac;
+
+import static org.junit.Assert.assertEquals;
+
+import java.io.IOException;
+import java.net.InetSocketAddress;
+import java.net.MalformedURLException;
+import java.net.Proxy;
+import java.net.Proxy.Type;
+import java.net.ProxySelector;
+import java.net.SocketAddress;
+import java.net.URI;
+import java.util.List;
+
+import org.junit.Test;
+
+import com.btr.proxy.TestUtil;
+import com.btr.proxy.util.ProxyException;
+
+
+/*****************************************************************************
+ * Tests for the Pac script parser and proxy selector.
+ * @author Bernd Rosstauscher (proxyvole@rosstauscher.de) Copyright 2009
+ ****************************************************************************/
+
+public class PacProxySelectorTest {
+
+ /*************************************************************************
+ * Test method
+ * @throws ProxyException on proxy detection error.
+ * @throws MalformedURLException on URL erros
+ ************************************************************************/
+ @Test
+ public void testScriptExecution() throws ProxyException, MalformedURLException {
+ List<Proxy> result = new PacProxySelector(
+ new UrlPacScriptSource(toUrl("test1.pac"))).select(TestUtil.HTTP_TEST_URI);
+
+ assertEquals(TestUtil.HTTP_TEST_PROXY, result.get(0));
+ }
+
+ /*************************************************************************
+ * Test method
+ * @throws ProxyException on proxy detection error.
+ * @throws MalformedURLException on URL erros
+ ************************************************************************/
+ @Test
+ public void testScriptExecution2() throws ProxyException, MalformedURLException {
+ PacProxySelector pacProxySelector = new PacProxySelector(
+ new UrlPacScriptSource(toUrl("test2.pac")));
+ List<Proxy> result = pacProxySelector.select(TestUtil.HTTP_TEST_URI);
+ assertEquals(Proxy.NO_PROXY, result.get(0));
+
+ result = pacProxySelector.select(TestUtil.HTTPS_TEST_URI);
+ assertEquals(Proxy.NO_PROXY, result.get(0));
+ }
+
+ /*************************************************************************
+ * Test download fix to prevent infinite loop.
+ * @throws ProxyException on proxy detection error.
+ * @throws MalformedURLException on URL erros
+ ************************************************************************/
+ @Test
+ public void pacDownloadFromURLShouldNotUseProxy() throws ProxyException, MalformedURLException {
+ ProxySelector oldOne = ProxySelector.getDefault();
+ try {
+ ProxySelector.setDefault(new ProxySelector() {
+ @Override
+ public List<Proxy> select(URI uri) {
+ throw new IllegalStateException("Should not download via proxy");
+ }
+
+ @Override
+ public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
+ // Not used
+ }
+ });
+
+ PacProxySelector pacProxySelector = new PacProxySelector(
+ new UrlPacScriptSource("http://www.test.invalid/wpad.pac"));
+ pacProxySelector.select(TestUtil.HTTPS_TEST_URI);
+ } finally {
+ ProxySelector.setDefault(oldOne);
+ }
+ }
+
+ /*************************************************************************
+ * Test method
+ * @throws ProxyException on proxy detection error.
+ * @throws MalformedURLException on URL erros
+ ************************************************************************/
+ @Test
+ public void testScriptMuliProxy() throws ProxyException, MalformedURLException {
+ PacProxySelector pacProxySelector = new PacProxySelector(
+ new UrlPacScriptSource(toUrl("testMultiProxy.pac")));
+ List<Proxy> result = pacProxySelector.select(TestUtil.HTTP_TEST_URI);
+ assertEquals(2, result.size());
+ assertEquals(new Proxy(Type.HTTP, InetSocketAddress.createUnresolved("my-proxy.com", 80)), result.get(0));
+ assertEquals(new Proxy(Type.HTTP, InetSocketAddress.createUnresolved("my-proxy2.com", 8080)), result.get(1));
+ }
+
+ /*************************************************************************
+ * Test method for the override local IP feature.
+ * @throws ProxyException on proxy detection error.
+ * @throws MalformedURLException on URL erros
+ ************************************************************************/
+ @Test
+ public void testLocalIPOverride() throws ProxyException, MalformedURLException {
+ System.setProperty(PacScriptMethods.OVERRIDE_LOCAL_IP, "123.123.123.123");
+ try {
+ PacProxySelector pacProxySelector = new PacProxySelector(
+ new UrlPacScriptSource(toUrl("testLocalIP.pac")));
+ List<Proxy> result = pacProxySelector.select(TestUtil.HTTP_TEST_URI);
+ assertEquals(result.get(0), new Proxy(Type.HTTP, InetSocketAddress.createUnresolved("123.123.123.123", 8080)));
+ } finally {
+ System.setProperty(PacScriptMethods.OVERRIDE_LOCAL_IP, "");
+ }
+
+ }
+
+ /*************************************************************************
+ * Helper method to build the url to the given test file
+ * @param testFile the name of the test file.
+ * @return the URL.
+ * @throws MalformedURLException
+ ************************************************************************/
+
+ private String toUrl(String testFile) throws MalformedURLException {
+ return PacProxySelectorTest.class.getResource("/" + TestUtil.TEST_DATA_FOLDER + "/pac/" + testFile).toString();
+ }
+
+
+}
+
diff --git a/src/test/java/com/btr/proxy/selector/pac/PacScriptMethodsTest.java b/src/test/java/com/btr/proxy/selector/pac/PacScriptMethodsTest.java
new file mode 100644
index 0000000..0d69a1e
--- /dev/null
+++ b/src/test/java/com/btr/proxy/selector/pac/PacScriptMethodsTest.java
@@ -0,0 +1,165 @@
+package com.btr.proxy.selector.pac;
+
+import static org.junit.Assert.assertEquals;
+
+import java.net.Inet4Address;
+import java.net.InetAddress;
+import java.net.UnknownHostException;
+import java.util.Calendar;
+
+import org.junit.Test;
+
+import com.btr.proxy.TestUtil;
+
+/*****************************************************************************
+ * Tests for the global PAC script methods that are used as context inside of
+ * the scripts.
+ * @author Bernd Rosstauscher (proxyvole@rosstauscher.de) Copyright 2009
+ ****************************************************************************/
+
+public class PacScriptMethodsTest {
+
+ /*************************************************************************
+ * Get a methods implementation with a calendar for date and time base tests
+ * set to a hardcoded data.
+ * Current date for all tests is: 15. December 1994 12:00.00
+ * its a Thursday
+ ************************************************************************/
+
+ private PacScriptMethods buildParser() {
+ PacScriptMethods result = new PacScriptMethods();
+
+ Calendar cal = Calendar.getInstance();
+ cal.set(Calendar.YEAR, 1994);
+ cal.set(Calendar.MONTH, Calendar.DECEMBER);
+ cal.set(Calendar.DAY_OF_MONTH, 15);
+ cal.set(Calendar.HOUR_OF_DAY, 12);
+ cal.set(Calendar.MINUTE, 00);
+ cal.set(Calendar.SECOND, 00);
+ cal.set(Calendar.MILLISECOND, 00);
+ result.setCurrentTime(cal);
+
+ return result;
+ }
+
+ /*************************************************************************
+ * Test method
+ ************************************************************************/
+ @Test
+ public void testDnsDomainIs() {
+ assertEquals(true, buildParser().dnsDomainIs("host1.unit-test.invalid", "unit-test.invalid"));
+ }
+
+ /*************************************************************************
+ * Test method
+ ************************************************************************/
+ @Test
+ public void testDnsDomainLevels() {
+ assertEquals(2, buildParser().dnsDomainLevels(TestUtil.HTTP_TEST_URI.toString()));
+ }
+
+ /*************************************************************************
+ * Test method
+ * @throws UnknownHostException on resolve error.
+ ************************************************************************/
+ @Test
+ public void testDnsResolve() throws UnknownHostException {
+ InetAddress adr = Inet4Address.getLocalHost();
+ assertEquals(adr.getHostAddress(), buildParser().dnsResolve(adr.getHostName()));
+ }
+
+ /*************************************************************************
+ * Test method
+ ************************************************************************/
+ @Test
+ public void testIsInNet() {
+ assertEquals(true, buildParser().isInNet("192.168.0.122", "192.168.0.0", "255.255.255.0"));
+ }
+
+ /*************************************************************************
+ * Test method
+ ************************************************************************/
+ @Test
+ public void testIsInNet2() {
+ assertEquals(true, buildParser().isInNet("10.13.75.47", "10.13.72.0", "255.255.252.0"));
+ }
+
+ /*************************************************************************
+ * Test method
+ ************************************************************************/
+ @Test
+ public void testIsPlainHostName() {
+ assertEquals(false, buildParser().isPlainHostName("host1.unit-test.invalid"));
+ assertEquals(true, buildParser().isPlainHostName("host1"));
+ }
+
+ /*************************************************************************
+ * Test method
+ * @throws UnknownHostException on resolve error.
+ ************************************************************************/
+ @Test
+ public void testIsResolveable() throws UnknownHostException {
+ InetAddress adr = Inet4Address.getLocalHost();
+ assertEquals(true, buildParser().isResolvable(adr.getHostName()));
+ }
+
+ /*************************************************************************
+ * Test method
+ ************************************************************************/
+ @Test
+ public void testLocalHostOrDomainIs() {
+ assertEquals(true, buildParser().localHostOrDomainIs("host1.unit-test.invalid", "host1.unit-test.invalid"));
+ }
+
+ /*************************************************************************
+ * Test method
+ ************************************************************************/
+ @Test
+ public void testShExpMatch() {
+ assertEquals(true, buildParser().shExpMatch("host1.unit-test.invalid", "host1.unit-test.*"));
+ assertEquals(true, buildParser().shExpMatch("host1.unit-test.invalid", "*.unit-test.invalid"));
+ }
+
+ /*************************************************************************
+ * Test method
+ ************************************************************************/
+ @Test
+ public void testWeekdayRange() {
+ assertEquals(true, buildParser().weekdayRange("MON", "SUN", "GMT"));
+ assertEquals(true, buildParser().weekdayRange("SUN", "SAT", null));
+ assertEquals(false, buildParser().weekdayRange("MON", "WED", null));
+ }
+
+ /*************************************************************************
+ * Test method
+ ************************************************************************/
+ @Test
+ public void testDateRange() {
+ assertEquals(true, buildParser().dateRange(15, "undefined", "undefined", "undefined", "undefined", "undefined", "undefined"));
+ assertEquals(true, buildParser().dateRange(15, "DEC", "undefined", "undefined", "undefined", "undefined", "undefined"));
+ assertEquals(true, buildParser().dateRange(15, "DEC", 1994, "undefined", "undefined", "undefined", "undefined"));
+ assertEquals(true, buildParser().dateRange(15, 17, "undefined", "undefined", "undefined", "undefined", "undefined"));
+ assertEquals(true, buildParser().dateRange("OCT", "JAN", "undefined", "undefined", "undefined", "undefined", "undefined"));
+ assertEquals(true, buildParser().dateRange(1994, 1994, "undefined", "undefined", "undefined", "undefined", "undefined"));
+ assertEquals(true, buildParser().dateRange(1, "DEC", 1994, 1, "JAN", 1995, "GTM"));
+
+ assertEquals(false, buildParser().dateRange(16, "DEC", 1994, 1, "JAN", 1995, "GTM"));
+ }
+
+ /*************************************************************************
+ * Test method
+ ************************************************************************/
+ @Test
+ public void testTimeRange() {
+ assertEquals(true, buildParser().timeRange(12, "undefined", "undefined", "undefined", "undefined", "undefined", "undefined"));
+ assertEquals(true, buildParser().timeRange(11, 13, "undefined", "undefined", "undefined", "undefined", "undefined"));
+ assertEquals(true, buildParser().timeRange(11, 13, "gmt", "undefined", "undefined", "undefined", "undefined"));
+ assertEquals(true, buildParser().timeRange(11, 30, 13, 30, "undefined", "undefined", "undefined"));
+ assertEquals(true, buildParser().timeRange(11, 30, 15, 13, 30, 15, "undefined"));
+ assertEquals(true, buildParser().timeRange(11, 30, 15, 13, 30, 15, "GMT"));
+
+ assertEquals(false, buildParser().timeRange(12, 50, 00, 9, 30, 00, "GMT"));
+ }
+
+}
+
diff --git a/src/test/java/com/btr/proxy/selector/pac/RhinoPacScriptParserTest.java b/src/test/java/com/btr/proxy/selector/pac/RhinoPacScriptParserTest.java
new file mode 100644
index 0000000..f7d245a
--- /dev/null
+++ b/src/test/java/com/btr/proxy/selector/pac/RhinoPacScriptParserTest.java
@@ -0,0 +1,114 @@
+package com.btr.proxy.selector.pac;
+
+import java.net.MalformedURLException;
+import java.util.Calendar;
+
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+import com.btr.proxy.TestUtil;
+import com.btr.proxy.util.ProxyException;
+
+/*****************************************************************************
+ * Tests for the Rhino PAC script parser.
+ * @author Bernd Rosstauscher (proxyvole@rosstauscher.de) Copyright 2009
+ ****************************************************************************/
+
+public class RhinoPacScriptParserTest {
+
+ /*************************************************************************
+ * Set calendar for date and time base tests.
+ * Current date for all tests is: 15. December 1994 12:00.00
+ * its a Thursday
+ ************************************************************************/
+ @BeforeClass
+ public static void setup() {
+ Calendar cal = Calendar.getInstance();
+ cal.set(Calendar.YEAR, 1994);
+ cal.set(Calendar.MONTH, Calendar.DECEMBER);
+ cal.set(Calendar.DAY_OF_MONTH, 15);
+ cal.set(Calendar.HOUR_OF_DAY, 12);
+ cal.set(Calendar.MINUTE, 00);
+ cal.set(Calendar.SECOND, 00);
+ cal.set(Calendar.MILLISECOND, 00);
+
+ RhinoPacScriptParser.setCurrentTime(cal);
+ }
+
+ /*************************************************************************
+ * Cleanup after the tests.
+ ************************************************************************/
+ @AfterClass
+ public static void teadDown() {
+ RhinoPacScriptParser.setCurrentTime(null);
+ }
+
+ /*************************************************************************
+ * Test method
+ * @throws ProxyException on proxy detection error.
+ * @throws MalformedURLException on URL erros
+ ************************************************************************/
+ @Test
+ public void testScriptExecution() throws ProxyException, MalformedURLException {
+ PacScriptParser p = new RhinoPacScriptParser(new UrlPacScriptSource(toUrl("test1.pac")));
+ p.evaluate(TestUtil.HTTP_TEST_URI.toString(), "host1.unit-test.invalid");
+ }
+
+ /*************************************************************************
+ * Test method
+ * @throws ProxyException on proxy detection error.
+ * @throws MalformedURLException on URL erros
+ ************************************************************************/
+ @Test
+ public void testCommentsInScript() throws ProxyException, MalformedURLException {
+ PacScriptParser p = new RhinoPacScriptParser(new UrlPacScriptSource(toUrl("test2.pac")));
+ p.evaluate(TestUtil.HTTP_TEST_URI.toString(), "host1.unit-test.invalid");
+ }
+
+ /*************************************************************************
+ * Test method
+ * @throws ProxyException on proxy detection error.
+ * @throws MalformedURLException on URL erros
+ ************************************************************************/
+ @Test
+ public void testScriptWeekDayScript() throws ProxyException, MalformedURLException {
+ PacScriptParser p = new RhinoPacScriptParser(new UrlPacScriptSource(toUrl("testWeekDay.pac")));
+ p.evaluate(TestUtil.HTTP_TEST_URI.toString(), "host1.unit-test.invalid");
+ }
+
+ /*************************************************************************
+ * Test method
+ * @throws ProxyException on proxy detection error.
+ * @throws MalformedURLException on URL erros
+ ************************************************************************/
+ @Test
+ public void testDateRangeScript() throws ProxyException, MalformedURLException {
+ PacScriptParser p = new RhinoPacScriptParser(new UrlPacScriptSource(toUrl("testDateRange.pac")));
+ p.evaluate(TestUtil.HTTP_TEST_URI.toString(), "host1.unit-test.invalid");
+ }
+
+ /*************************************************************************
+ * Test method
+ * @throws ProxyException on proxy detection error.
+ * @throws MalformedURLException on URL erros
+ ************************************************************************/
+ @Test
+ public void testTimeRangeScript() throws ProxyException, MalformedURLException {
+ PacScriptParser p = new RhinoPacScriptParser(new UrlPacScriptSource(toUrl("testTimeRange.pac")));
+ p.evaluate(TestUtil.HTTP_TEST_URI.toString(), "host1.unit-test.invalid");
+ }
+
+ /*************************************************************************
+ * Helper method to build the url to the given test file
+ * @param testFile the name of the test file.
+ * @return the URL.
+ * @throws MalformedURLException
+ ************************************************************************/
+
+ private String toUrl(String testFile) throws MalformedURLException {
+ return RhinoPacScriptParserTest.class.getResource("/" + TestUtil.TEST_DATA_FOLDER + "/pac/" + testFile).toString();
+ }
+
+}
+
diff --git a/src/test/java/com/btr/proxy/selector/pac/UrlPacScriptSourceTest.java b/src/test/java/com/btr/proxy/selector/pac/UrlPacScriptSourceTest.java
new file mode 100644
index 0000000..61c8cd0
--- /dev/null
+++ b/src/test/java/com/btr/proxy/selector/pac/UrlPacScriptSourceTest.java
@@ -0,0 +1,34 @@
+package com.btr.proxy.selector.pac;
+
+import static org.junit.Assert.assertEquals;
+import org.junit.Test;
+
+/*****************************************************************************
+ * Tests for the UrlPacScriptSource.
+ * @author Bernd Rosstauscher (proxyvole@rosstauscher.de) Copyright 2009
+ ****************************************************************************/
+
+public class UrlPacScriptSourceTest {
+
+ /*************************************************************************
+ * Unit Test
+ ************************************************************************/
+ @Test
+ public void testHttpCharsetParser() {
+ UrlPacScriptSource scriptSource = new UrlPacScriptSource("");
+ String charset = scriptSource.parseCharsetFromHeader("application/x-ns-proxy-autoconfig; charset=UTF-8");
+ assertEquals("UTF-8", charset);
+ }
+
+ /*************************************************************************
+ * Unit Test
+ ************************************************************************/
+ @Test
+ public void testHttpCharsetParserDefault() {
+ UrlPacScriptSource scriptSource = new UrlPacScriptSource("");
+ String charset = scriptSource.parseCharsetFromHeader("application/octet-stream;");
+ assertEquals("ISO-8859-1", charset);
+ }
+
+}
+
diff --git a/src/test/java/com/btr/proxy/selector/whitelist/NoProxyTest.java b/src/test/java/com/btr/proxy/selector/whitelist/NoProxyTest.java
new file mode 100644
index 0000000..9d187ba
--- /dev/null
+++ b/src/test/java/com/btr/proxy/selector/whitelist/NoProxyTest.java
@@ -0,0 +1,108 @@
+package com.btr.proxy.selector.whitelist;
+
+import static org.junit.Assert.*;
+
+import java.net.Proxy;
+import java.net.ProxySelector;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.util.List;
+
+import org.junit.Test;
+
+import com.btr.proxy.TestUtil;
+import com.btr.proxy.selector.fixed.FixedProxySelector;
+
+/*****************************************************************************
+ * Some unit tests for the white list selector.
+ *
+ * @author Bernd Rosstauscher (proxyvole@rosstauscher.de) Copyright 2009
+ ****************************************************************************/
+
+public class NoProxyTest {
+
+ /*************************************************************************
+ * Test method
+ ************************************************************************/
+ @Test
+ public void testWhiteList() {
+ ProxySelector delegate = new FixedProxySelector(TestUtil.HTTP_TEST_PROXY);
+ ProxyBypassListSelector ps = new ProxyBypassListSelector("no_prox.*", delegate);
+
+ assertEquals(delegate.select(TestUtil.HTTP_TEST_URI).get(0), ps.select(TestUtil.HTTP_TEST_URI).get(0));
+ }
+
+ /*************************************************************************
+ * Test method
+ ************************************************************************/
+ @Test
+ public void testWhiteList2() {
+ ProxySelector delegate = new FixedProxySelector(TestUtil.HTTP_TEST_PROXY);
+ ProxyBypassListSelector ps = new ProxyBypassListSelector("*.unit-test.invalid", delegate);
+
+ List<Proxy> result = ps.select(TestUtil.HTTP_TEST_URI);
+ assertEquals(Proxy.NO_PROXY, result.get(0));
+ }
+
+ /*************************************************************************
+ * Test method
+ * @throws URISyntaxException on invalid URL syntax.
+ ************************************************************************/
+ @Test
+ public void testWhiteList3() throws URISyntaxException {
+ ProxySelector delegate = new FixedProxySelector(TestUtil.HTTP_TEST_PROXY);
+ ProxyBypassListSelector ps = new ProxyBypassListSelector("*.unit-test.invalid, localhost, 127.0.0.1", delegate);
+
+ List<Proxy> result = ps.select(new URI("http://localhost:65/getDocument"));
+ assertEquals(Proxy.NO_PROXY, result.get(0));
+
+ result = ps.select(new URI("http://127.0.0.1:65/getDocument"));
+ assertEquals(Proxy.NO_PROXY, result.get(0));
+ }
+
+ /*************************************************************************
+ * Test method
+ ************************************************************************/
+ @Test
+ public void testWhiteList4() {
+ ProxySelector delegate = new FixedProxySelector(TestUtil.HTTP_TEST_PROXY);
+ ProxyBypassListSelector ps = new ProxyBypassListSelector("*.unit-test.invalid, ", delegate);
+
+ List<Proxy> result = ps.select(TestUtil.HTTP_TEST_URI);
+ assertEquals(Proxy.NO_PROXY, result.get(0));
+ }
+
+ /*************************************************************************
+ * Test method
+ * @throws URISyntaxException on invalid URL syntax.
+ ************************************************************************/
+ @Test
+ public void testWhiteList5() throws URISyntaxException {
+ ProxySelector delegate = new FixedProxySelector(TestUtil.HTTP_TEST_PROXY);
+ ProxyBypassListSelector ps = new ProxyBypassListSelector("*.unit-test.invalid localhost 127.0.0.1", delegate);
+
+ List<Proxy> result = ps.select(new URI("http://localhost:65/getDocument"));
+ assertEquals(Proxy.NO_PROXY, result.get(0));
+
+ result = ps.select(new URI("http://127.0.0.1:65/getDocument"));
+ assertEquals(Proxy.NO_PROXY, result.get(0));
+ }
+
+ /*************************************************************************
+ * Test method
+ * @throws URISyntaxException on invalid URL syntax.
+ ************************************************************************/
+ @Test
+ public void testIpRange() throws URISyntaxException {
+ ProxySelector delegate = new FixedProxySelector(TestUtil.HTTP_TEST_PROXY);
+ ProxyBypassListSelector ps = new ProxyBypassListSelector("192.168.0.0/24", delegate);
+
+ List<Proxy> result = ps.select(new URI("http://192.168.0.100:81/test.data"));
+ assertEquals(Proxy.NO_PROXY, result.get(0));
+
+ result = ps.select(new URI("http://192.168.1.100:81/test.data"));
+ assertEquals(delegate.select(TestUtil.HTTP_TEST_URI).get(0), result.get(0));
+ }
+
+}
+