Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/net/www/protocol/https/HttpsURLConnection/HttpsProxyStackOverflow.java
38889 views
/*1* Copyright (c) 2011, 2012, 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 667086826* @summary StackOverFlow with bad authenticated Proxy tunnels27* @run main/othervm HttpsProxyStackOverflow28*29* No way to reserve default Authenticator, need to run in othervm mode.30*/3132import java.io.IOException;33import java.io.InputStream;34import java.net.Authenticator;35import java.net.Proxy;36import java.net.InetSocketAddress;37import java.net.PasswordAuthentication;38import java.net.ServerSocket;39import java.net.Socket;40import java.net.URL;41import javax.net.ssl.HttpsURLConnection;4243public class HttpsProxyStackOverflow {4445public static void main(String[] args) throws IOException {46BadAuthProxyServer server = startServer();47doClient(server);48}4950static void doClient(BadAuthProxyServer server) throws IOException {51// url doesn't matter since we will never make the connection52URL url = new URL("https://anythingwilldo/");53HttpsURLConnection conn = (HttpsURLConnection) url.openConnection(54new Proxy(Proxy.Type.HTTP,55new InetSocketAddress("localhost", server.getPort())));56try (InputStream is = conn.getInputStream()) {57} catch(IOException unused) {58// no real server, IOException is expected.59// failure if StackOverflowError60} finally {61server.done();62}63}6465static BadAuthProxyServer startServer() throws IOException {66Authenticator.setDefault(new Authenticator() {67@Override68protected PasswordAuthentication getPasswordAuthentication() {69return new PasswordAuthentication("xyz", "xyz".toCharArray());70}71});7273BadAuthProxyServer server = new BadAuthProxyServer(new ServerSocket(0));74Thread serverThread = new Thread(server);75serverThread.start();76return server;77}7879static class BadAuthProxyServer implements Runnable {80private ServerSocket ss;81private boolean done;8283BadAuthProxyServer(ServerSocket ss) { this.ss = ss; }8485public void run() {86try {87while (!done) {88Socket s = ss.accept();89s.getOutputStream().write(90("HTTP/1.1 407\nProxy-Authenticate:Basic " +91"realm=\"WallyWorld\"\n\n").getBytes("US-ASCII"));9293s.close();9495s = ss.accept();96s.close();97}98} catch (IOException e) {99// Ignore IOException when the main thread calls done100} finally {101try { ss.close(); } catch (IOException e) {}102}103}104105int getPort() {106return ss.getLocalPort();107}108109void done() {110try { ss.close(); } catch (IOException e) {}111done = true;112}113}114}115116117118