Path: blob/master/test/jdk/sun/net/www/protocol/https/HttpsClient/ProxyAuthTest.java
41161 views
/*1* Copyright (c) 2001, 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 4323990 4413069 816083831* @summary HttpsURLConnection doesn't send Proxy-Authorization on CONNECT32* Incorrect checking of proxy server response33* @modules jdk.crypto.ec34* java.base/sun.net.www35* @library /javax/net/ssl/templates36* @run main/othervm ProxyAuthTest fail37* @run main/othervm -Djdk.http.auth.tunneling.disabledSchemes=Basic38* ProxyAuthTest fail39* @run main/othervm -Djdk.http.auth.tunneling.disabledSchemes=Basic,40* ProxyAuthTest fail41* @run main/othervm -Djdk.http.auth.tunneling.disabledSchemes=BAsIc42* ProxyAuthTest fail43* @run main/othervm -Djdk.http.auth.tunneling.disabledSchemes=Basic,Digest44* ProxyAuthTest fail45* @run main/othervm -Djdk.http.auth.tunneling.disabledSchemes=Unknown,bAsIc46* ProxyAuthTest fail47* @run main/othervm -Djdk.http.auth.tunneling.disabledSchemes=48* ProxyAuthTest succeed49* @run main/othervm50* -Djdk.http.auth.tunneling.disabledSchemes=Digest,NTLM,Negotiate51* ProxyAuthTest succeed52* @run main/othervm -Djdk.http.auth.tunneling.disabledSchemes=UNKNOWN,notKnown53* ProxyAuthTest succeed54*/5556import java.io.BufferedReader;57import java.io.DataOutputStream;58import java.io.IOException;59import java.io.InputStreamReader;60import java.net.Authenticator;61import java.net.InetAddress;62import java.net.InetSocketAddress;63import java.net.PasswordAuthentication;64import java.net.Proxy;65import java.net.URL;66import javax.net.ssl.HostnameVerifier;67import javax.net.ssl.HttpsURLConnection;68import javax.net.ssl.SSLSession;69import javax.net.ssl.SSLSocket;70import javax.net.ssl.SSLContext;71import static java.nio.charset.StandardCharsets.US_ASCII;7273/*74* ProxyAuthTest.java -- includes a simple server that can serve75* Http get request in both clear and secure channel, and a client76* that makes https requests behind the firewall through an77* authentication proxy78*/7980public class ProxyAuthTest extends SSLSocketTemplate {81private static boolean expectSuccess;8283ProxyAuthTest() {84serverAddress = InetAddress.getLoopbackAddress();85}8687/*88* Run the test case.89*/90public static void main(String[] args) throws Exception {91// Get the customized arguments.92parseArguments(args);9394(new ProxyAuthTest()).run();95}9697@Override98protected boolean isCustomizedClientConnection() {99return true;100}101102@Override103protected void runServerApplication(SSLSocket socket) throws Exception {104String response = "Proxy authentication for tunneling succeeded ..";105DataOutputStream out = new DataOutputStream(socket.getOutputStream());106try {107BufferedReader in = new BufferedReader(108new InputStreamReader(socket.getInputStream()));109110// read the request111readRequest(in);112113// retrieve bytecodes114byte[] bytecodes = response.getBytes(US_ASCII);115116// send bytecodes in response (assumes HTTP/1.0 or later)117out.writeBytes("HTTP/1.0 200 OK\r\n");118out.writeBytes("Content-Length: " + bytecodes.length + "\r\n");119out.writeBytes("Content-Type: text/html\r\n\r\n");120out.write(bytecodes);121out.flush();122} catch (IOException e) {123// write out error response124out.writeBytes("HTTP/1.0 400 " + e.getMessage() + "\r\n");125out.writeBytes("Content-Type: text/html\r\n\r\n");126out.flush();127}128}129130@Override131protected void runClientApplication(int serverPort) throws Exception {132/*133* Set the default SSLSocketFactory.134*/135SSLContext context = createClientSSLContext();136HttpsURLConnection.setDefaultSSLSocketFactory(137context.getSocketFactory());138139/*140* setup up a proxy with authentication information141*/142ProxyTunnelServer ps = setupProxy();143144/*145* we want to avoid URLspoofCheck failures in cases where the cert146* DN name does not match the hostname in the URL.147*/148HttpsURLConnection.setDefaultHostnameVerifier(new NameVerifier());149150InetSocketAddress paddr = InetSocketAddress151.createUnresolved(ps.getInetAddress().getHostAddress(),152ps.getPort());153Proxy proxy = new Proxy(Proxy.Type.HTTP, paddr);154155InetAddress serverAddress = this.serverAddress;156String host = serverAddress == null157? "localhost"158: serverAddress.getHostAddress();159if (host.indexOf(':') > -1) host = "[" + host + "]";160URL url = new URL(161"https://" + host + ":" + serverPort + "/index.html");162System.out.println("URL: " + url);163BufferedReader in = null;164HttpsURLConnection uc = (HttpsURLConnection) url.openConnection(proxy);165try {166in = new BufferedReader(new InputStreamReader(uc.getInputStream()));167String inputLine;168System.out.print("Client received from the server: ");169while ((inputLine = in.readLine()) != null) {170System.out.println(inputLine);171}172if (!expectSuccess) {173throw new RuntimeException(174"Expected exception/failure to connect, but succeeded.");175}176} catch (IOException e) {177if (expectSuccess) {178System.out.println("Client side failed: " + e.getMessage());179throw e;180}181182// Assert that the error stream is not accessible from the failed183// tunnel setup.184if (uc.getErrorStream() != null) {185throw new RuntimeException("Unexpected error stream.");186}187188if (!e.getMessage().contains("Unable to tunnel through proxy") ||189!e.getMessage().contains("407")) {190191throw new RuntimeException(192"Expected exception about cannot tunnel, " +193"407, etc, but got", e);194} else {195// Informative196System.out.println(197"Caught expected exception: " + e.getMessage());198}199} finally {200if (in != null) {201in.close();202}203}204}205206207private static void parseArguments(String[] args) {208if (args[0].equals("succeed")) {209expectSuccess = true;210} else {211expectSuccess = false;212}213}214215/**216* read the response, don't care for the syntax of the request-line217* for this testing218*/219private static void readRequest(BufferedReader in) throws IOException {220String line = null;221System.out.println("Server received: ");222do {223if (line != null) {224System.out.println(line);225}226line = in.readLine();227} while ((line.length() != 0) &&228(line.charAt(0) != '\r') && (line.charAt(0) != '\n'));229}230231private static class NameVerifier implements HostnameVerifier {232233@Override234public boolean verify(String hostname, SSLSession session) {235return true;236}237}238239private static ProxyTunnelServer setupProxy() throws IOException {240InetAddress loopback = InetAddress.getLoopbackAddress();241ProxyTunnelServer pserver = new ProxyTunnelServer(loopback);242243/*244* register a system wide authenticator and setup the proxy for245* authentication246*/247Authenticator.setDefault(new TestAuthenticator());248249// register with the username and password250pserver.needUserAuth(true);251pserver.setUserAuth("Test", "test123");252253pserver.start();254return pserver;255}256257private static class TestAuthenticator extends Authenticator {258259@Override260public PasswordAuthentication getPasswordAuthentication() {261return new PasswordAuthentication("Test", "test123".toCharArray());262}263}264}265266267