Path: blob/jdk8u272-b10-aarch32-20201026/jdk/test/java/net/Socks/BadProxySelector.java
48795 views
/*1* Copyright (c) 2015, 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.InetSocketAddress;30import java.net.Proxy;31import java.net.ProxySelector;32import java.net.Socket;33import java.net.SocketAddress;34import java.net.ServerSocket;35import java.net.URI;36import java.util.ArrayList;37import java.util.List;38import java.io.*;3940public class BadProxySelector {41public static void main(String[] args) throws Exception {42ProxySelector.setDefault(new HTTPProxySelector());43try (ServerSocket ss = new ServerSocket(0);44Socket s1 = new Socket(ss.getInetAddress(), ss.getLocalPort());45Socket s2 = ss.accept()) {46}4748ProxySelector.setDefault(new NullHTTPProxySelector());49try (ServerSocket ss = new ServerSocket(0);50Socket s1 = new Socket(ss.getInetAddress(), ss.getLocalPort());51Socket s2 = ss.accept()) {52}53}5455// always returns bogus HTTP proxies56private static class HTTPProxySelector extends ProxySelector {57@Override58public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {}5960@Override61public List<Proxy> select(URI uri) {62List<Proxy> proxies = new ArrayList<>();63proxies.add(new Proxy(Proxy.Type.HTTP,64new InetSocketAddress("localhost", 0)));65proxies.add(new Proxy(Proxy.Type.HTTP,66new InetSocketAddress("localhost", 0)));67return proxies;68}69}7071private static class NullHTTPProxySelector extends ProxySelector {72@Override73public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {}7475@Override76public List<Proxy> select(URI uri) {77List<Proxy> proxies = new ArrayList<>();78proxies.add(null);79proxies.add(new Proxy(Proxy.Type.HTTP,80new InetSocketAddress("localhost", 0)));81return proxies;82}83}84}858687