Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/net/www/protocol/https/HttpsURLConnection/B6216082.java
38889 views
/*1* Copyright (c) 2005, 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// 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* @library .. /lib33* @build jdk.test.lib.NetworkConfiguration34* jdk.test.lib.Platform35* HttpCallback TestHttpsServer ClosedChannelList36* HttpTransaction TunnelProxy37* @key intermittent38* @run main/othervm B621608239*/4041import java.io.*;42import java.net.*;43import javax.net.ssl.*;44import java.util.*;4546import jdk.test.lib.NetworkConfiguration;4748public class B6216082 {49static SimpleHttpTransaction httpTrans;50static TestHttpsServer server;51static TunnelProxy proxy;5253// it seems there's no proxy ever if a url points to 'localhost',54// even if proxy related properties are set. so we need to bind55// our simple http proxy and http server to a non-loopback address56static InetAddress firstNonLoAddress = null;5758public static void main(String[] args) throws Exception {59HostnameVerifier reservedHV =60HttpsURLConnection.getDefaultHostnameVerifier();61try {62// XXX workaround for CNFE63Class.forName("java.nio.channels.ClosedByInterruptException");64if (!setupEnv()) {65return;66}6768startHttpServer();6970// https.proxyPort can only be set after the TunnelProxy has been71// created as it will use an ephemeral port.72System.setProperty("https.proxyPort",73(new Integer(proxy.getLocalPort())).toString() );7475makeHttpCall();7677if (httpTrans.hasBadRequest) {78throw new RuntimeException("Test failed : bad http request");79}80} finally {81if (proxy != null) {82proxy.terminate();83}84if (server != null) {85server.terminate();86}87HttpsURLConnection.setDefaultHostnameVerifier(reservedHV);88}89}9091/*92* Where do we find the keystores for ssl?93*/94static String pathToStores = "../../../../../../javax/net/ssl/etc";95static String keyStoreFile = "keystore";96static String trustStoreFile = "truststore";97static String passwd = "passphrase";98public static boolean setupEnv() throws Exception {99firstNonLoAddress = getNonLoAddress();100if (firstNonLoAddress == null) {101System.err.println("The test needs at least one non-loopback address to run. Quit now.");102return false;103}104System.out.println(firstNonLoAddress.getHostAddress());105// will use proxy106System.setProperty( "https.proxyHost", firstNonLoAddress.getHostAddress());107108// setup properties to do ssl109String keyFilename = System.getProperty("test.src", "./") + "/" +110pathToStores + "/" + keyStoreFile;111String trustFilename = System.getProperty("test.src", "./") + "/" +112pathToStores + "/" + trustStoreFile;113114System.setProperty("javax.net.ssl.keyStore", keyFilename);115System.setProperty("javax.net.ssl.keyStorePassword", passwd);116System.setProperty("javax.net.ssl.trustStore", trustFilename);117System.setProperty("javax.net.ssl.trustStorePassword", passwd);118HttpsURLConnection.setDefaultHostnameVerifier(new NameVerifier());119return true;120}121122public static InetAddress getNonLoAddress() throws Exception {123InetAddress lh = InetAddress.getByName("localhost");124NetworkInterface loNIC = NetworkInterface.getByInetAddress(lh);125126NetworkConfiguration nc = NetworkConfiguration.probe();127Optional<InetAddress> oaddr = nc.interfaces()128.filter(nif -> !nif.getName().equalsIgnoreCase(loNIC.getName()))129.flatMap(nif -> nc.addresses(nif))130.filter(a -> !a.isLoopbackAddress())131.findFirst();132133return oaddr.orElseGet(() -> null);134}135136public static void startHttpServer() throws IOException {137// Both the https server and the proxy let the138// system pick up an ephemeral port.139httpTrans = new SimpleHttpTransaction();140server = new TestHttpsServer(httpTrans, 1, 10, 0);141proxy = new TunnelProxy(1, 10, 0);142}143144public static void makeHttpCall() throws Exception {145System.out.println("https server listen on: " + server.getLocalPort());146System.out.println("https proxy listen on: " + proxy.getLocalPort());147URL url = new URL("https" , firstNonLoAddress.getHostAddress(),148server.getLocalPort(), "/");149HttpURLConnection uc = (HttpURLConnection)url.openConnection();150System.out.println(uc.getResponseCode());151uc.disconnect();152}153154static class NameVerifier implements HostnameVerifier {155public boolean verify(String hostname, SSLSession session) {156return true;157}158}159}160161class SimpleHttpTransaction implements HttpCallback {162public boolean hasBadRequest = false;163164/*165* Our http server which simply redirect first call166*/167public void request(HttpTransaction trans) {168try {169String path = trans.getRequestURI().getPath();170if (path.equals("/")) {171// the first call, redirect it172String location = "/redirect";173trans.addResponseHeader("Location", location);174trans.sendResponse(302, "Moved Temporarily");175} else {176// if the bug exsits, it'll send 2 GET commands177// check 2nd GET here178String duplicatedGet = trans.getRequestHeader(null);179if (duplicatedGet != null &&180duplicatedGet.toUpperCase().indexOf("GET") >= 0) {181trans.sendResponse(400, "Bad Request");182hasBadRequest = true;183} else {184trans.sendResponse(200, "OK");185}186}187} catch (Exception e) {188throw new RuntimeException(e);189}190}191}192193194