2007-12-01 00:00:00 +00:00
|
|
|
/*
|
2015-12-08 03:49:12 +00:00
|
|
|
* Copyright (c) 2005, 2015, Oracle and/or its affiliates. All rights reserved.
|
2007-12-01 00:00:00 +00:00
|
|
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
|
|
*
|
|
|
|
* This code is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License version 2 only, as
|
|
|
|
* published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
* version 2 for more details (a copy is included in the LICENSE file that
|
|
|
|
* accompanied this code).
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License version
|
|
|
|
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
|
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*
|
2010-05-25 15:58:33 -07:00
|
|
|
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
|
|
* or visit www.oracle.com if you need additional information or have any
|
|
|
|
* questions.
|
2007-12-01 00:00:00 +00:00
|
|
|
*/
|
|
|
|
|
2015-12-08 03:49:12 +00:00
|
|
|
//
|
|
|
|
// SunJSSE does not support dynamic system properties, no way to re-use
|
|
|
|
// system properties in samevm/agentvm mode.
|
|
|
|
//
|
|
|
|
|
2007-12-01 00:00:00 +00:00
|
|
|
/*
|
|
|
|
* @test
|
|
|
|
* @bug 6216082
|
|
|
|
* @summary Redirect problem with HttpsURLConnection using a proxy
|
2015-05-28 10:54:48 -07:00
|
|
|
* @modules java.base/sun.net.www
|
2017-05-19 13:33:47 -07:00
|
|
|
* @library .. /test/lib
|
2017-06-12 12:43:26 -07:00
|
|
|
* @build jdk.test.lib.NetworkConfiguration
|
|
|
|
* jdk.test.lib.Platform
|
|
|
|
* HttpCallback TestHttpsServer ClosedChannelList
|
2017-05-19 13:33:47 -07:00
|
|
|
* HttpTransaction TunnelProxy
|
2015-12-08 03:49:12 +00:00
|
|
|
* @key intermittent
|
2012-09-04 02:24:51 -07:00
|
|
|
* @run main/othervm B6216082
|
2011-08-12 12:26:31 +08:00
|
|
|
*/
|
2007-12-01 00:00:00 +00:00
|
|
|
|
|
|
|
import java.io.*;
|
|
|
|
import java.net.*;
|
|
|
|
import javax.net.ssl.*;
|
|
|
|
import java.util.*;
|
|
|
|
|
2017-05-19 13:33:47 -07:00
|
|
|
import jdk.test.lib.NetworkConfiguration;
|
2017-04-14 10:47:08 +01:00
|
|
|
|
2007-12-01 00:00:00 +00:00
|
|
|
public class B6216082 {
|
|
|
|
static SimpleHttpTransaction httpTrans;
|
2012-09-07 15:30:17 +01:00
|
|
|
static TestHttpsServer server;
|
2007-12-01 00:00:00 +00:00
|
|
|
static TunnelProxy proxy;
|
|
|
|
|
|
|
|
// it seems there's no proxy ever if a url points to 'localhost',
|
|
|
|
// even if proxy related properties are set. so we need to bind
|
|
|
|
// our simple http proxy and http server to a non-loopback address
|
|
|
|
static InetAddress firstNonLoAddress = null;
|
|
|
|
|
|
|
|
public static void main(String[] args) throws Exception {
|
2011-08-12 12:26:31 +08:00
|
|
|
HostnameVerifier reservedHV =
|
|
|
|
HttpsURLConnection.getDefaultHostnameVerifier();
|
|
|
|
try {
|
|
|
|
// XXX workaround for CNFE
|
|
|
|
Class.forName("java.nio.channels.ClosedByInterruptException");
|
2012-09-04 02:24:51 -07:00
|
|
|
if (!setupEnv()) {
|
|
|
|
return;
|
|
|
|
}
|
2007-12-01 00:00:00 +00:00
|
|
|
|
2011-08-12 12:26:31 +08:00
|
|
|
startHttpServer();
|
2007-12-01 00:00:00 +00:00
|
|
|
|
2011-08-12 12:26:31 +08:00
|
|
|
// https.proxyPort can only be set after the TunnelProxy has been
|
|
|
|
// created as it will use an ephemeral port.
|
|
|
|
System.setProperty("https.proxyPort",
|
|
|
|
(new Integer(proxy.getLocalPort())).toString() );
|
2007-12-01 00:00:00 +00:00
|
|
|
|
2011-08-12 12:26:31 +08:00
|
|
|
makeHttpCall();
|
2007-12-01 00:00:00 +00:00
|
|
|
|
2011-08-12 12:26:31 +08:00
|
|
|
if (httpTrans.hasBadRequest) {
|
|
|
|
throw new RuntimeException("Test failed : bad http request");
|
|
|
|
}
|
|
|
|
} finally {
|
2012-09-04 02:24:51 -07:00
|
|
|
if (proxy != null) {
|
|
|
|
proxy.terminate();
|
|
|
|
}
|
|
|
|
if (server != null) {
|
|
|
|
server.terminate();
|
|
|
|
}
|
2011-08-12 12:26:31 +08:00
|
|
|
HttpsURLConnection.setDefaultHostnameVerifier(reservedHV);
|
2007-12-01 00:00:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Where do we find the keystores for ssl?
|
|
|
|
*/
|
2014-03-05 07:24:34 +00:00
|
|
|
static String pathToStores = "../../../../../../javax/net/ssl/etc";
|
2007-12-01 00:00:00 +00:00
|
|
|
static String keyStoreFile = "keystore";
|
|
|
|
static String trustStoreFile = "truststore";
|
|
|
|
static String passwd = "passphrase";
|
2012-09-04 02:24:51 -07:00
|
|
|
public static boolean setupEnv() throws Exception {
|
|
|
|
firstNonLoAddress = getNonLoAddress();
|
|
|
|
if (firstNonLoAddress == null) {
|
|
|
|
System.err.println("The test needs at least one non-loopback address to run. Quit now.");
|
|
|
|
return false;
|
2007-12-01 00:00:00 +00:00
|
|
|
}
|
2012-09-04 02:24:51 -07:00
|
|
|
System.out.println(firstNonLoAddress.getHostAddress());
|
2007-12-01 00:00:00 +00:00
|
|
|
// will use proxy
|
|
|
|
System.setProperty( "https.proxyHost", firstNonLoAddress.getHostAddress());
|
|
|
|
|
|
|
|
// setup properties to do ssl
|
|
|
|
String keyFilename = System.getProperty("test.src", "./") + "/" +
|
|
|
|
pathToStores + "/" + keyStoreFile;
|
|
|
|
String trustFilename = System.getProperty("test.src", "./") + "/" +
|
|
|
|
pathToStores + "/" + trustStoreFile;
|
|
|
|
|
|
|
|
System.setProperty("javax.net.ssl.keyStore", keyFilename);
|
|
|
|
System.setProperty("javax.net.ssl.keyStorePassword", passwd);
|
|
|
|
System.setProperty("javax.net.ssl.trustStore", trustFilename);
|
|
|
|
System.setProperty("javax.net.ssl.trustStorePassword", passwd);
|
|
|
|
HttpsURLConnection.setDefaultHostnameVerifier(new NameVerifier());
|
2012-09-04 02:24:51 -07:00
|
|
|
return true;
|
2007-12-01 00:00:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static InetAddress getNonLoAddress() throws Exception {
|
2017-04-14 10:47:08 +01:00
|
|
|
InetAddress lh = InetAddress.getByName("localhost");
|
|
|
|
NetworkInterface loNIC = NetworkInterface.getByInetAddress(lh);
|
|
|
|
|
|
|
|
NetworkConfiguration nc = NetworkConfiguration.probe();
|
|
|
|
Optional<InetAddress> oaddr = nc.interfaces()
|
|
|
|
.filter(nif -> !nif.getName().equalsIgnoreCase(loNIC.getName()))
|
|
|
|
.flatMap(nif -> nc.addresses(nif))
|
|
|
|
.filter(a -> !a.isLoopbackAddress())
|
|
|
|
.findFirst();
|
2007-12-01 00:00:00 +00:00
|
|
|
|
2017-04-14 10:47:08 +01:00
|
|
|
return oaddr.orElseGet(() -> null);
|
2007-12-01 00:00:00 +00:00
|
|
|
}
|
|
|
|
|
2012-09-04 02:24:51 -07:00
|
|
|
public static void startHttpServer() throws IOException {
|
|
|
|
// Both the https server and the proxy let the
|
|
|
|
// system pick up an ephemeral port.
|
|
|
|
httpTrans = new SimpleHttpTransaction();
|
2012-09-07 15:30:17 +01:00
|
|
|
server = new TestHttpsServer(httpTrans, 1, 10, 0);
|
2012-09-04 02:24:51 -07:00
|
|
|
proxy = new TunnelProxy(1, 10, 0);
|
2007-12-01 00:00:00 +00:00
|
|
|
}
|
|
|
|
|
2012-09-04 02:24:51 -07:00
|
|
|
public static void makeHttpCall() throws Exception {
|
|
|
|
System.out.println("https server listen on: " + server.getLocalPort());
|
|
|
|
System.out.println("https proxy listen on: " + proxy.getLocalPort());
|
|
|
|
URL url = new URL("https" , firstNonLoAddress.getHostAddress(),
|
|
|
|
server.getLocalPort(), "/");
|
|
|
|
HttpURLConnection uc = (HttpURLConnection)url.openConnection();
|
|
|
|
System.out.println(uc.getResponseCode());
|
|
|
|
uc.disconnect();
|
2007-12-01 00:00:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static class NameVerifier implements HostnameVerifier {
|
|
|
|
public boolean verify(String hostname, SSLSession session) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class SimpleHttpTransaction implements HttpCallback {
|
|
|
|
public boolean hasBadRequest = false;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Our http server which simply redirect first call
|
|
|
|
*/
|
|
|
|
public void request(HttpTransaction trans) {
|
|
|
|
try {
|
|
|
|
String path = trans.getRequestURI().getPath();
|
|
|
|
if (path.equals("/")) {
|
|
|
|
// the first call, redirect it
|
|
|
|
String location = "/redirect";
|
|
|
|
trans.addResponseHeader("Location", location);
|
|
|
|
trans.sendResponse(302, "Moved Temporarily");
|
|
|
|
} else {
|
|
|
|
// if the bug exsits, it'll send 2 GET commands
|
|
|
|
// check 2nd GET here
|
|
|
|
String duplicatedGet = trans.getRequestHeader(null);
|
|
|
|
if (duplicatedGet != null &&
|
|
|
|
duplicatedGet.toUpperCase().indexOf("GET") >= 0) {
|
|
|
|
trans.sendResponse(400, "Bad Request");
|
|
|
|
hasBadRequest = true;
|
|
|
|
} else {
|
|
|
|
trans.sendResponse(200, "OK");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (Exception e) {
|
2012-09-04 02:24:51 -07:00
|
|
|
throw new RuntimeException(e);
|
2007-12-01 00:00:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|