Path: blob/master/test/jdk/java/net/ProxySelector/B8035158.java
41149 views
/*1* Copyright (c) 2014, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 8035158 8145732 8144300 824113826* @run main/othervm B803515827*/2829import java.net.Proxy;30import java.net.ProxySelector;31import java.net.URI;32import java.util.*;33import java.util.concurrent.Callable;3435public class B8035158 {3637public static void main(String[] args) {38for (TestCase t : emptyNonProxiesHosts()) t.run();39for (TestCase t : nonEmptyNonProxiesHosts()) t.run();40for (TestCase t : misc()) t.run();41}4243// Setting http.nonProxyHosts to an empty string has an effect of44// not including default hosts to the list of exceptions45// (i.e. if you want everything to be connected directly rather than46// through proxy, you should set this property to an empty string)47private static Collection<TestCase> emptyNonProxiesHosts() {48List<TestCase> tests = new LinkedList<>();49String[] loopbacks = {"localhost", "[::1]", "[::0]", "0.0.0.0",50"127.0.0.0", "127.0.0.1", "127.0.1.0", "127.0.1.1",51"127.1.0.0", "127.1.0.1", "127.1.1.0", "127.1.1.1"};52Map<String, String> properties = new HashMap<>();53properties.put("http.proxyHost", "http://proxy.example.com");54properties.put("http.nonProxyHosts", "");55for (String s : loopbacks) {56tests.add(new TestCase(properties, "http://" + s, true));57}58return tests;59}6061// No matter what is set into the http.nonProxyHosts (as far as it is not62// an empty string) loopback address aliases must be always connected63// directly64private static Collection<TestCase> nonEmptyNonProxiesHosts() {65List<TestCase> tests = new LinkedList<>();66String[] nonProxyHosts = {67"google.com",68"localhost", "[::1]", "[::0]", "0.0.0.0",69"127.0.0.0", "127.0.0.1", "127.0.1.0", "127.0.1.1",70"127.1.0.0", "127.1.0.1", "127.1.1.0", "127.1.1.1"};71String[] loopbacks = {"localhost", "[::1]", "[::0]", "0.0.0.0",72"127.0.0.0", "127.0.0.1", "127.0.1.0", "127.0.1.1",73"127.1.0.0", "127.1.0.1", "127.1.1.0", "127.1.1.1"};74for (String h : nonProxyHosts) {75for (String s : loopbacks) {76Map<String, String> properties = new HashMap<>();77properties.put("http.proxyHost", "http://proxy.example.com");78properties.put("http.nonProxyHosts", h);79tests.add(new TestCase(properties, "http://" + s, false));80}81}82return tests;83}8485// unsorted tests86private static Collection<TestCase> misc() {87List<TestCase> t = new LinkedList<>();88t.add(new TestCase("oracle.com", "http://137.254.16.101", true));89t.add(new TestCase("google.com", "http://74.125.200.101", true));9091t.add(new TestCase("google.com|google.ie", "http://google.co.uk",92true));93t.add(new TestCase("google.com|google.ie", "http://google.com",94false));95t.add(new TestCase("google.com|google.ie", "http://google.ie",96false));97t.add(new TestCase("google.com|google.com|google.ie",98"http://google.ie", false));99100t.add(new TestCase("google.com|bing.com|yahoo.com",101"http://127.0.0.1", false));102t.add(new TestCase("google.com|bing.com|yahoo.com",103"http://google.com", false));104t.add(new TestCase("google.com|bing.com|yahoo.com",105"http://bing.com", false));106t.add(new TestCase("google.com|bing.com|yahoo.com",107"http://yahoo.com", false));108109t.add(new TestCase("google.com|bing.com", "http://google.com", false));110t.add(new TestCase("google.com|bing.com", "http://bing.com", false));111t.add(new TestCase("google.com|bing.com", "http://yahoo.com",112true));113t.add(new TestCase("google.com|bing.co*", "http://google.com", false));114t.add(new TestCase("google.com|bing.co*", "http://bing.com", false));115t.add(new TestCase("google.com|bing.co*", "http://yahoo.com",116true));117t.add(new TestCase("google.com|*ing.com", "http://google.com", false));118t.add(new TestCase("google.com|*ing.com", "http://bing.com", false));119t.add(new TestCase("google.com|*ing.com", "http://yahoo.com",120true));121t.add(new TestCase("google.co*|bing.com", "http://google.com", false));122t.add(new TestCase("google.co*|bing.com", "http://bing.com", false));123t.add(new TestCase("google.co*|bing.com", "http://yahoo.com",124true));125t.add(new TestCase("google.co*|bing.co*", "http://google.com", false));126t.add(new TestCase("google.co*|bing.co*", "http://bing.com", false));127t.add(new TestCase("google.co*|bing.co*", "http://yahoo.com",128true));129t.add(new TestCase("google.co*|*ing.com", "http://google.com", false));130t.add(new TestCase("google.co*|*ing.com", "http://bing.com", false));131t.add(new TestCase("google.co*|*ing.com", "http://yahoo.com",132true));133t.add(new TestCase("*oogle.com|bing.com", "http://google.com", false));134t.add(new TestCase("*oogle.com|bing.com", "http://bing.com", false));135t.add(new TestCase("*oogle.com|bing.com", "http://yahoo.com",136true));137t.add(new TestCase("*oogle.com|bing.co*", "http://google.com", false));138t.add(new TestCase("*oogle.com|bing.co*", "http://bing.com", false));139t.add(new TestCase("*oogle.com|bing.co*", "http://yahoo.com",140true));141t.add(new TestCase("*oogle.com|*ing.com", "http://google.com", false));142t.add(new TestCase("*oogle.com|*ing.com", "http://bing.com", false));143t.add(new TestCase("*oogle.com|*ing.com", "http://yahoo.com",144true));145146t.add(new TestCase("google.com|bing.com|yahoo.com", "http://google.com", false));147t.add(new TestCase("google.com|bing.com|yahoo.com", "http://bing.com", false));148t.add(new TestCase("google.com|bing.com|yahoo.com", "http://yahoo.com", false));149t.add(new TestCase("google.com|bing.com|yahoo.com",150"http://duckduckgo.com", true));151152t.add(new TestCase("p-proxy.com", "http://p-proxy.com", false));153t.add(new TestCase("google.co*|google.ie", "http://google.co.uk",154false));155t.add(new TestCase("*google.*", "http://google.co.uk",156false));157t.add(new TestCase("*", "http://google.co.uk",false));158t.add(new TestCase("localhost|*", "http://google.co.uk",false));159t.add(new TestCase("*|oracle.com", "http://google.co.uk",false));160t.add(new TestCase("*|oracle.com|*", "http://google.co.uk",false));161t.add(new TestCase("*|*", "http://google.co.uk",false));162163164t.add(new TestCase("*oracle.com", "http://my.oracle.com", false));165t.add(new TestCase("google.com|bing.com|yahoo.com", "http://127.0.0.1", false));166t.add(new TestCase("google.com|bing.com|yahoo.com", "http://yahoo.com", false));167168t.add(new TestCase("localhost|host.example.com", "http://localhost",169false));170t.add(new TestCase("localhost|host.example.com",171"http://host.example.com", false));172t.add(new TestCase("localhost|host.example.com",173"http://google.com", true));174return t;175}176177178private static <T> T withSystemPropertiesSet(179Map<String, String> localProperties,180Callable<? extends T> code) {181Map<String, String> backup = new HashMap<>();182try {183backupAndSetProperties(localProperties, backup);184return code.call();185} catch (Exception e) {186throw new RuntimeException(e);187} finally {188restoreProperties(backup);189}190}191192private static void backupAndSetProperties(193Map<String, String> localProperties,194Map<String, String> oldProperties) {195for (Map.Entry<String, String> e : localProperties.entrySet()) {196String oldValue = System.setProperty(e.getKey(), e.getValue());197oldProperties.put(e.getKey(), oldValue);198}199}200201private static void restoreProperties(Map<String, String> oldProperties) {202for (Map.Entry<String, String> e : oldProperties.entrySet()) {203String oldValue = e.getValue();204String key = e.getKey();205if (oldValue == null)206System.getProperties().remove(key);207else208System.setProperty(key, oldValue);209}210}211212private static class TestCase {213214final Map<String, String> localProperties;215final String urlhost;216final boolean expectedProxying;217218TestCase(String nonProxyHosts, String urlhost,219boolean expectedProxying) {220this(nonProxyHosts, "proxy.example.com", urlhost,221expectedProxying);222}223224TestCase(String nonProxyHosts, String proxyHost, String urlhost,225boolean expectedProxying) {226this(new HashMap<String, String>() {227{228put("http.nonProxyHosts", nonProxyHosts);229put("http.proxyHost", proxyHost);230}231}, urlhost, expectedProxying);232}233234TestCase(Map<String, String> localProperties, String urlhost,235boolean expectedProxying) {236this.localProperties = localProperties;237this.urlhost = urlhost;238this.expectedProxying = expectedProxying;239}240241void run() {242System.out.printf("urlhost=%s properties=%s: proxied? %s%n",243urlhost, localProperties, expectedProxying);244245List<Proxy> proxies = withSystemPropertiesSet(localProperties,246() -> ProxySelector.getDefault().select(247URI.create(urlhost))248);249250verify(proxies);251}252253void verify(List<? extends Proxy> proxies) {254255boolean actualProxying = !(proxies.size() == 1 &&256proxies.get(0).type() == Proxy.Type.DIRECT);257258if (actualProxying != expectedProxying)259throw new AssertionError(String.format(260"Expected %s connection for %s (given " +261"properties=%s). Here's the list of proxies " +262"returned: %s",263expectedProxying ? "proxied" : "direct", urlhost,264localProperties, proxies265));266}267}268}269270271