Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/net/www/protocol/https/HttpsClient/ProxyAuthTest.java
38889 views
/*1* Copyright (c) 2001, 2017, 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* @library /javax/net/ssl/templates34* @run main/othervm ProxyAuthTest fail35* @run main/othervm -Djdk.http.auth.tunneling.disabledSchemes=Basic36* ProxyAuthTest fail37* @run main/othervm -Djdk.http.auth.tunneling.disabledSchemes=Basic,38* ProxyAuthTest fail39* @run main/othervm -Djdk.http.auth.tunneling.disabledSchemes=BAsIc40* ProxyAuthTest fail41* @run main/othervm -Djdk.http.auth.tunneling.disabledSchemes=Basic,Digest42* ProxyAuthTest fail43* @run main/othervm -Djdk.http.auth.tunneling.disabledSchemes=Unknown,bAsIc44* ProxyAuthTest fail45* @run main/othervm -Djdk.http.auth.tunneling.disabledSchemes=46* ProxyAuthTest succeed47* @run main/othervm48* -Djdk.http.auth.tunneling.disabledSchemes=Digest,NTLM,Negotiate49* ProxyAuthTest succeed50* @run main/othervm -Djdk.http.auth.tunneling.disabledSchemes=UNKNOWN,notKnown51* ProxyAuthTest succeed52*/5354import java.io.BufferedReader;55import java.io.DataOutputStream;56import java.io.IOException;57import java.io.InputStreamReader;58import java.net.Authenticator;59import java.net.InetSocketAddress;60import java.net.PasswordAuthentication;61import java.net.Proxy;62import java.net.URL;63import javax.net.ssl.HostnameVerifier;64import javax.net.ssl.HttpsURLConnection;65import javax.net.ssl.SSLSession;66import javax.net.ssl.SSLSocket;67import javax.net.ssl.SSLContext;68import static java.nio.charset.StandardCharsets.US_ASCII;6970/*71* ProxyAuthTest.java -- includes a simple server that can serve72* Http get request in both clear and secure channel, and a client73* that makes https requests behind the firewall through an74* authentication proxy75*/7677public class ProxyAuthTest extends SSLSocketTemplate {78private static boolean expectSuccess;7980/*81* Run the test case.82*/83public static void main(String[] args) throws Exception {84// Get the customized arguments.85parseArguments(args);8687(new ProxyAuthTest()).run();88}8990@Override91protected boolean isCustomizedClientConnection() {92return true;93}9495@Override96protected void runServerApplication(SSLSocket socket) throws Exception {97String response = "Proxy authentication for tunneling succeeded ..";98DataOutputStream out = new DataOutputStream(socket.getOutputStream());99try {100BufferedReader in = new BufferedReader(101new InputStreamReader(socket.getInputStream()));102103// read the request104readRequest(in);105106// retrieve bytecodes107byte[] bytecodes = response.getBytes(US_ASCII);108109// send bytecodes in response (assumes HTTP/1.0 or later)110out.writeBytes("HTTP/1.0 200 OK\r\n");111out.writeBytes("Content-Length: " + bytecodes.length + "\r\n");112out.writeBytes("Content-Type: text/html\r\n\r\n");113out.write(bytecodes);114out.flush();115} catch (IOException e) {116// write out error response117out.writeBytes("HTTP/1.0 400 " + e.getMessage() + "\r\n");118out.writeBytes("Content-Type: text/html\r\n\r\n");119out.flush();120}121}122123@Override124protected void runClientApplication(int serverPort) throws Exception {125/*126* Set the default SSLSocketFactory.127*/128SSLContext context = createClientSSLContext();129HttpsURLConnection.setDefaultSSLSocketFactory(130context.getSocketFactory());131132/*133* setup up a proxy with authentication information134*/135ProxyTunnelServer ps = setupProxy();136137/*138* we want to avoid URLspoofCheck failures in cases where the cert139* DN name does not match the hostname in the URL.140*/141HttpsURLConnection.setDefaultHostnameVerifier(new NameVerifier());142143InetSocketAddress paddr =144new InetSocketAddress("localhost", ps.getPort());145Proxy proxy = new Proxy(Proxy.Type.HTTP, paddr);146147URL url = new URL(148"https://" + "localhost:" + serverPort + "/index.html");149BufferedReader in = null;150HttpsURLConnection uc = (HttpsURLConnection) url.openConnection(proxy);151try {152in = new BufferedReader(new InputStreamReader(uc.getInputStream()));153String inputLine;154System.out.print("Client recieved from the server: ");155while ((inputLine = in.readLine()) != null) {156System.out.println(inputLine);157}158if (!expectSuccess) {159throw new RuntimeException(160"Expected exception/failure to connect, but succeeded.");161}162} catch (IOException e) {163if (expectSuccess) {164System.out.println("Client side failed: " + e.getMessage());165throw e;166}167168// Assert that the error stream is not accessible from the failed169// tunnel setup.170if (uc.getErrorStream() != null) {171throw new RuntimeException("Unexpected error stream.");172}173174if (!e.getMessage().contains("Unable to tunnel through proxy") ||175!e.getMessage().contains("407")) {176177throw new RuntimeException(178"Expected exception about cannot tunnel, " +179"407, etc, but got", e);180} else {181// Informative182System.out.println(183"Caught expected exception: " + e.getMessage());184}185} finally {186if (in != null) {187in.close();188}189}190}191192193private static void parseArguments(String[] args) {194if (args[0].equals("succeed")) {195expectSuccess = true;196} else {197expectSuccess = false;198}199}200201/**202* read the response, don't care for the syntax of the request-line203* for this testing204*/205private static void readRequest(BufferedReader in) throws IOException {206String line = null;207System.out.println("Server received: ");208do {209if (line != null) {210System.out.println(line);211}212line = in.readLine();213} while ((line.length() != 0) &&214(line.charAt(0) != '\r') && (line.charAt(0) != '\n'));215}216217private static class NameVerifier implements HostnameVerifier {218219@Override220public boolean verify(String hostname, SSLSession session) {221return true;222}223}224225private static ProxyTunnelServer setupProxy() throws IOException {226ProxyTunnelServer pserver = new ProxyTunnelServer();227228/*229* register a system wide authenticator and setup the proxy for230* authentication231*/232Authenticator.setDefault(new TestAuthenticator());233234// register with the username and password235pserver.needUserAuth(true);236pserver.setUserAuth("Test", "test123");237238pserver.start();239return pserver;240}241242private static class TestAuthenticator extends Authenticator {243244@Override245public PasswordAuthentication getPasswordAuthentication() {246return new PasswordAuthentication("Test", "test123".toCharArray());247}248}249}250251252