Path: blob/master/test/jdk/sun/net/www/protocol/https/HttpsURLConnection/B6216082.java
41161 views
/*1* Copyright (c) 2005, 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// SunJSSE does not support dynamic system properties, no way to re-use25// system properties in samevm/agentvm mode.26//2728/*29* @test30* @bug 621608231* @summary Redirect problem with HttpsURLConnection using a proxy32* @modules java.base/sun.net.www33* @library .. /test/lib34* @build jdk.test.lib.NetworkConfiguration35* jdk.test.lib.Platform36* HttpCallback TestHttpsServer ClosedChannelList37* HttpTransaction TunnelProxy38* @key intermittent39* @run main/othervm B621608240*/4142import java.io.*;43import java.net.*;44import javax.net.ssl.*;45import java.util.*;4647import jdk.test.lib.NetworkConfiguration;4849public class B6216082 {50static SimpleHttpTransaction httpTrans;51static TestHttpsServer server;52static TunnelProxy proxy;5354// it seems there's no proxy ever if a url points to 'localhost',55// even if proxy related properties are set. so we need to bind56// our simple http proxy and http server to a non-loopback address57static InetAddress firstNonLoAddress = null;5859public static void main(String[] args) throws Exception {60HostnameVerifier reservedHV =61HttpsURLConnection.getDefaultHostnameVerifier();62try {63// XXX workaround for CNFE64Class.forName("java.nio.channels.ClosedByInterruptException");65if (!setupEnv()) {66return;67}6869startHttpServer();7071// https.proxyPort can only be set after the TunnelProxy has been72// created as it will use an ephemeral port.73System.setProperty("https.proxyPort",74(new Integer(proxy.getLocalPort())).toString() );7576makeHttpCall();7778if (httpTrans.hasBadRequest) {79throw new RuntimeException("Test failed : bad http request");80}81} finally {82if (proxy != null) {83proxy.terminate();84}85if (server != null) {86server.terminate();87}88HttpsURLConnection.setDefaultHostnameVerifier(reservedHV);89}90}9192/*93* Where do we find the keystores for ssl?94*/95static String pathToStores = "../../../../../../javax/net/ssl/etc";96static String keyStoreFile = "keystore";97static String trustStoreFile = "truststore";98static String passwd = "passphrase";99public static boolean setupEnv() throws Exception {100firstNonLoAddress = getNonLoAddress();101if (firstNonLoAddress == null) {102System.err.println("The test needs at least one non-loopback address to run. Quit now.");103return false;104}105System.out.println(firstNonLoAddress.getHostAddress());106// will use proxy107System.setProperty( "https.proxyHost", firstNonLoAddress.getHostAddress());108109// setup properties to do ssl110String keyFilename = System.getProperty("test.src", "./") + "/" +111pathToStores + "/" + keyStoreFile;112String trustFilename = System.getProperty("test.src", "./") + "/" +113pathToStores + "/" + trustStoreFile;114115System.setProperty("javax.net.ssl.keyStore", keyFilename);116System.setProperty("javax.net.ssl.keyStorePassword", passwd);117System.setProperty("javax.net.ssl.trustStore", trustFilename);118System.setProperty("javax.net.ssl.trustStorePassword", passwd);119HttpsURLConnection.setDefaultHostnameVerifier(new NameVerifier());120return true;121}122123public static InetAddress getNonLoAddress() throws Exception {124InetAddress lh = InetAddress.getByName("localhost");125NetworkInterface loNIC = NetworkInterface.getByInetAddress(lh);126127NetworkConfiguration nc = NetworkConfiguration.probe();128Optional<InetAddress> oaddr = nc.interfaces()129.filter(nif -> !nif.getName().equalsIgnoreCase(loNIC.getName()))130.flatMap(nif -> nc.addresses(nif))131.filter(a -> !a.isLoopbackAddress())132.findFirst();133134return oaddr.orElseGet(() -> null);135}136137public static void startHttpServer() throws IOException {138// Both the https server and the proxy let the139// system pick up an ephemeral port.140httpTrans = new SimpleHttpTransaction();141server = new TestHttpsServer(httpTrans, 1, 10, firstNonLoAddress, 0);142proxy = new TunnelProxy(1, 10, firstNonLoAddress, 0);143}144145public static void makeHttpCall() throws Exception {146System.out.println("https server listen on: " + server.getLocalPort());147System.out.println("https proxy listen on: " + proxy.getLocalPort());148URL url = new URL("https" , firstNonLoAddress.getHostAddress(),149server.getLocalPort(), "/");150HttpURLConnection uc = (HttpURLConnection)url.openConnection();151System.out.println(uc.getResponseCode());152uc.disconnect();153}154155static class NameVerifier implements HostnameVerifier {156public boolean verify(String hostname, SSLSession session) {157return true;158}159}160}161162class SimpleHttpTransaction implements HttpCallback {163public boolean hasBadRequest = false;164165/*166* Our http server which simply redirect first call167*/168public void request(HttpTransaction trans) {169try {170String path = trans.getRequestURI().getPath();171if (path.equals("/")) {172// the first call, redirect it173String location = "/redirect";174trans.addResponseHeader("Location", location);175trans.sendResponse(302, "Moved Temporarily");176} else {177// if the bug exsits, it'll send 2 GET commands178// check 2nd GET here179String duplicatedGet = trans.getRequestHeader(null);180if (duplicatedGet != null &&181duplicatedGet.toUpperCase().indexOf("GET") >= 0) {182trans.sendResponse(400, "Bad Request");183hasBadRequest = true;184} else {185trans.sendResponse(200, "OK");186}187}188} catch (Exception e) {189throw new RuntimeException(e);190}191}192}193194195