Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/net/ProxySelector/B8035158.java
38812 views
/*1* Copyright (c) 2014, 2016, 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*/22/*23* @test24* @bug 8035158 814573225* @run main/othervm B803515826*/2728import java.net.Proxy;29import java.net.ProxySelector;30import java.net.URI;31import java.util.*;32import java.util.concurrent.Callable;3334public class B8035158 {3536public static void main(String[] args) {37for (TestCase t : emptyNonProxiesHosts()) t.run();38for (TestCase t : nonEmptyNonProxiesHosts()) t.run();39for (TestCase t : misc()) t.run();40}4142// Setting http.nonProxyHosts to an empty string has an effect of43// not including default hosts to the list of exceptions44// (i.e. if you want everything to be connected directly rather than45// through proxy, you should set this property to an empty string)46private static Collection<TestCase> emptyNonProxiesHosts() {47List<TestCase> tests = new LinkedList<>();48String[] loopbacks = {"localhost", "[::1]", "[::0]", "0.0.0.0",49"127.0.0.0", "127.0.0.1", "127.0.1.0", "127.0.1.1",50"127.1.0.0", "127.1.0.1", "127.1.1.0", "127.1.1.1"};51Map<String, String> properties = new HashMap<>();52properties.put("http.proxyHost", "http://proxy.example.com");53properties.put("http.nonProxyHosts", "");54for (String s : loopbacks) {55tests.add(new TestCase(properties, "http://" + s, true));56}57return tests;58}5960// No matter what is set into the http.nonProxyHosts (as far as it is not61// an empty string) loopback address aliases must be always connected62// directly63private static Collection<TestCase> nonEmptyNonProxiesHosts() {64List<TestCase> tests = new LinkedList<>();65String[] nonProxyHosts = {66"google.com",67"localhost", "[::1]", "[::0]", "0.0.0.0",68"127.0.0.0", "127.0.0.1", "127.0.1.0", "127.0.1.1",69"127.1.0.0", "127.1.0.1", "127.1.1.0", "127.1.1.1"};70String[] loopbacks = {"localhost", "[::1]", "[::0]", "0.0.0.0",71"127.0.0.0", "127.0.0.1", "127.0.1.0", "127.0.1.1",72"127.1.0.0", "127.1.0.1", "127.1.1.0", "127.1.1.1"};73for (String h : nonProxyHosts) {74for (String s : loopbacks) {75Map<String, String> properties = new HashMap<>();76properties.put("http.proxyHost", "http://proxy.example.com");77properties.put("http.nonProxyHosts", h);78tests.add(new TestCase(properties, "http://" + s, false));79}80}81return tests;82}8384// unsorted tests85private static Collection<TestCase> misc() {86List<TestCase> t = new LinkedList<>();87t.add(new TestCase("oracle.com", "http://137.254.16.101", true));88t.add(new TestCase("google.com", "http://74.125.200.101", true));8990t.add(new TestCase("google.com|google.ie", "http://google.co.uk",91true));92t.add(new TestCase("google.com|google.ie", "http://google.com",93false));94t.add(new TestCase("google.com|google.ie", "http://google.ie",95false));96t.add(new TestCase("google.com|google.com|google.ie",97"http://google.ie", false));9899t.add(new TestCase("google.com|bing.com|yahoo.com",100"http://127.0.0.1", false));101t.add(new TestCase("google.com|bing.com|yahoo.com",102"http://google.com", false));103t.add(new TestCase("google.com|bing.com|yahoo.com",104"http://bing.com", false));105t.add(new TestCase("google.com|bing.com|yahoo.com",106"http://yahoo.com", false));107108t.add(new TestCase("google.com|bing.com", "http://google.com", false));109t.add(new TestCase("google.com|bing.com", "http://bing.com", false));110t.add(new TestCase("google.com|bing.com", "http://yahoo.com",111true));112t.add(new TestCase("google.com|bing.co*", "http://google.com", false));113t.add(new TestCase("google.com|bing.co*", "http://bing.com", false));114t.add(new TestCase("google.com|bing.co*", "http://yahoo.com",115true));116t.add(new TestCase("google.com|*ing.com", "http://google.com", false));117t.add(new TestCase("google.com|*ing.com", "http://bing.com", false));118t.add(new TestCase("google.com|*ing.com", "http://yahoo.com",119true));120t.add(new TestCase("google.co*|bing.com", "http://google.com", false));121t.add(new TestCase("google.co*|bing.com", "http://bing.com", false));122t.add(new TestCase("google.co*|bing.com", "http://yahoo.com",123true));124t.add(new TestCase("google.co*|bing.co*", "http://google.com", false));125t.add(new TestCase("google.co*|bing.co*", "http://bing.com", false));126t.add(new TestCase("google.co*|bing.co*", "http://yahoo.com",127true));128t.add(new TestCase("google.co*|*ing.com", "http://google.com", false));129t.add(new TestCase("google.co*|*ing.com", "http://bing.com", false));130t.add(new TestCase("google.co*|*ing.com", "http://yahoo.com",131true));132t.add(new TestCase("*oogle.com|bing.com", "http://google.com", false));133t.add(new TestCase("*oogle.com|bing.com", "http://bing.com", false));134t.add(new TestCase("*oogle.com|bing.com", "http://yahoo.com",135true));136t.add(new TestCase("*oogle.com|bing.co*", "http://google.com", false));137t.add(new TestCase("*oogle.com|bing.co*", "http://bing.com", false));138t.add(new TestCase("*oogle.com|bing.co*", "http://yahoo.com",139true));140t.add(new TestCase("*oogle.com|*ing.com", "http://google.com", false));141t.add(new TestCase("*oogle.com|*ing.com", "http://bing.com", false));142t.add(new TestCase("*oogle.com|*ing.com", "http://yahoo.com",143true));144145t.add(new TestCase("google.com|bing.com|yahoo.com", "http://google.com", false));146t.add(new TestCase("google.com|bing.com|yahoo.com", "http://bing.com", false));147t.add(new TestCase("google.com|bing.com|yahoo.com", "http://yahoo.com", false));148t.add(new TestCase("google.com|bing.com|yahoo.com",149"http://duckduckgo.com", true));150151t.add(new TestCase("p-proxy.com", "http://p-proxy.com", false));152t.add(new TestCase("google.co*|google.ie", "http://google.co.uk",153false));154155t.add(new TestCase("*oracle.com", "http://my.oracle.com", false));156t.add(new TestCase("google.com|bing.com|yahoo.com", "http://127.0.0.1", false));157t.add(new TestCase("google.com|bing.com|yahoo.com", "http://yahoo.com", false));158159// example from160// http://docs.oracle.com/javase/7/docs/technotes/guides/net/proxies.html161t.add(new TestCase("localhost|host.example.com", "http://localhost",162false));163t.add(new TestCase("localhost|host.example.com",164"http://host.example.com", false));165t.add(new TestCase("localhost|host.example.com",166"http://google.com", true));167return t;168}169170171private static <T> T withSystemPropertiesSet(172Map<String, String> localProperties,173Callable<? extends T> code) {174Map<String, String> backup = new HashMap<>();175try {176backupAndSetProperties(localProperties, backup);177return code.call();178} catch (Exception e) {179throw new RuntimeException(e);180} finally {181restoreProperties(backup);182}183}184185private static void backupAndSetProperties(186Map<String, String> localProperties,187Map<String, String> oldProperties) {188for (Map.Entry<String, String> e : localProperties.entrySet()) {189String oldValue = System.setProperty(e.getKey(), e.getValue());190oldProperties.put(e.getKey(), oldValue);191}192}193194private static void restoreProperties(Map<String, String> oldProperties) {195for (Map.Entry<String, String> e : oldProperties.entrySet()) {196String oldValue = e.getValue();197String key = e.getKey();198if (oldValue == null)199System.getProperties().remove(key);200else201System.setProperty(key, oldValue);202}203}204205private static class TestCase {206207final Map<String, String> localProperties;208final String urlhost;209final boolean expectedProxying;210211TestCase(String nonProxyHosts, String urlhost,212boolean expectedProxying) {213this(nonProxyHosts, "proxy.example.com", urlhost,214expectedProxying);215}216217TestCase(String nonProxyHosts, String proxyHost, String urlhost,218boolean expectedProxying) {219this(new HashMap<String, String>() {220{221put("http.nonProxyHosts", nonProxyHosts);222put("http.proxyHost", proxyHost);223}224}, urlhost, expectedProxying);225}226227TestCase(Map<String, String> localProperties, String urlhost,228boolean expectedProxying) {229this.localProperties = localProperties;230this.urlhost = urlhost;231this.expectedProxying = expectedProxying;232}233234void run() {235System.out.printf("urlhost=%s properties=%s: proxied? %s%n",236urlhost, localProperties, expectedProxying);237238List<Proxy> proxies = withSystemPropertiesSet(localProperties,239() -> ProxySelector.getDefault().select(240URI.create(urlhost))241);242243verify(proxies);244}245246void verify(List<? extends Proxy> proxies) {247248boolean actualProxying = !(proxies.size() == 1 &&249proxies.get(0).type() == Proxy.Type.DIRECT);250251if (actualProxying != expectedProxying)252throw new AssertionError(String.format(253"Expected %s connection for %s (given " +254"properties=%s). Here's the list of proxies " +255"returned: %s",256expectedProxying ? "proxied" : "direct", urlhost,257localProperties, proxies258));259}260}261}262263264