Path: blob/master/test/jdk/java/net/Socks/BadProxySelector.java
41149 views
/*1* Copyright (c) 2015, 2019, 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 717836226* @run main/othervm BadProxySelector27*/2829import java.net.InetAddress;30import java.net.InetSocketAddress;31import java.net.Proxy;32import java.net.ProxySelector;33import java.net.Socket;34import java.net.SocketAddress;35import java.net.ServerSocket;36import java.net.URI;37import java.util.ArrayList;38import java.util.List;39import java.io.*;4041public class BadProxySelector {42public static void main(String[] args) throws Exception {43ProxySelector.setDefault(new HTTPProxySelector());44try (ServerSocket ss = new ServerSocket(0, 0, InetAddress.getLocalHost());45Socket s1 = new Socket(ss.getInetAddress(), ss.getLocalPort());46Socket s2 = ss.accept()) {47}4849ProxySelector.setDefault(new NullHTTPProxySelector());50try (ServerSocket ss = new ServerSocket(0, 0, InetAddress.getLocalHost());51Socket s1 = new Socket(ss.getInetAddress(), ss.getLocalPort());52Socket s2 = ss.accept()) {53}54}5556// always returns bogus HTTP proxies57private static class HTTPProxySelector extends ProxySelector {58@Override59public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {}6061@Override62public List<Proxy> select(URI uri) {63System.out.println(this.getClass().getSimpleName() + " called for " + uri);64List<Proxy> proxies = new ArrayList<>();65proxies.add(new Proxy(Proxy.Type.HTTP,66new InetSocketAddress("localhost", 0)));67proxies.add(new Proxy(Proxy.Type.HTTP,68new InetSocketAddress("localhost", 0)));69return proxies;70}71}7273private static class NullHTTPProxySelector extends ProxySelector {74@Override75public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {}7677@Override78public List<Proxy> select(URI uri) {79System.out.println(this.getClass().getSimpleName() + " called for " + uri);80List<Proxy> proxies = new ArrayList<>();81proxies.add(null);82proxies.add(new Proxy(Proxy.Type.HTTP,83new InetSocketAddress("localhost", 0)));84return proxies;85}86}87}888990