Path: blob/master/test/jdk/sun/net/www/protocol/http/TunnelThroughProxy.java
41159 views
/*1* Copyright (c) 2002, 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 462036226* @modules java.base/sun.net.www27* @build ProxyTunnelServer28* @run main/othervm TunnelThroughProxy29* @summary JSSE not returning proper exception on unknown host30*/3132import java.net.*;33import java.io.*;3435public class TunnelThroughProxy {36public static void main(String[] args) throws Exception {37nonexistingHostTest();38getLocalPortTest();39}4041static void nonexistingHostTest() throws Exception {42ProxyTunnelServer proxy = setupProxy(false);43try {44URL u = new URL("https://www.nonexistent-site.com/");45URLConnection uc = u.openConnection();46InputStream is = uc.getInputStream();47is.close();48} catch (Exception e) {49if (!e.getMessage().matches(".*HTTP\\/.*500.*")) {50throw new RuntimeException(e);51}52} finally {53proxy.terminate();54}55}565758static void getLocalPortTest() throws Exception {59ProxyTunnelServer proxy = setupProxy(true);60try {61int proxyPort = proxy.getPort();62InetAddress proxyAddress = proxy.getInetAddress();63ServerSocket server = new ServerSocket(0, 0, InetAddress.getLoopbackAddress());64int serverPort = server.getLocalPort();6566Socket sock;67sock = new Socket(new Proxy(Proxy.Type.HTTP,68new InetSocketAddress(proxyAddress, proxyPort)));69InetSocketAddress dest = new InetSocketAddress(server.getInetAddress(), serverPort);70sock.connect(dest);71int localPort = sock.getLocalPort();72if (localPort == proxyPort)73throw new RuntimeException("Fail: socket has wrong local port");74// check that tunnel really works75Socket sock1 = server.accept();76OutputStream os = sock1.getOutputStream();77os.write(99);78os.flush();79if (sock.getInputStream().read() != 99)80throw new RuntimeException("Tunnel does not work");81} finally {82proxy.terminate();83}84}8586static ProxyTunnelServer setupProxy(boolean makeTunnel) throws IOException {87InetAddress proxyAddress = InetAddress.getLoopbackAddress();88ProxyTunnelServer pserver = new ProxyTunnelServer(proxyAddress);89pserver.doTunnel(makeTunnel);90int proxyPort = pserver.getPort();9192// disable proxy authentication93pserver.needUserAuth(false);94pserver.start();95System.out.printf("Setting https.proxyHost='%s'%n", proxyAddress.getHostAddress());96System.setProperty("https.proxyHost", proxyAddress.getHostAddress());97System.out.printf("Setting https.proxyPort='%s'%n", String.valueOf(proxyPort));98System.setProperty("https.proxyPort", String.valueOf(proxyPort));99return pserver;100}101}102103104