Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/net/www/protocol/https/HttpsURLConnection/PostThruProxyWithAuth.java
38889 views
/*1* Copyright (c) 2001, 2016, 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*/2223import java.io.*;24import java.net.*;25import java.security.KeyStore;26import javax.net.*;27import javax.net.ssl.*;2829import jdk.testlibrary.OutputAnalyzer;30import jdk.testlibrary.ProcessTools;3132/*33* @test34* @bug 442307435* @summary This test case is written to test the https POST through a proxy36* with proxy authentication. It includes a simple server that serves37* http POST method requests in secure channel, and a client that38* makes https POST request through a proxy.39* @library /lib/testlibrary40* @compile OriginServer.java ProxyTunnelServer.java41* @run main/othervm -Djdk.http.auth.tunneling.disabledSchemes= PostThruProxyWithAuth42*/43public class PostThruProxyWithAuth {4445private static final String TEST_SRC = System.getProperty("test.src", ".");46private static final int TIMEOUT = 30000;4748/*49* Where do we find the keystores?50*/51static String pathToStores = "../../../../../../javax/net/ssl/etc";52static String keyStoreFile = "keystore";53static String trustStoreFile = "truststore";54static String passwd = "passphrase";5556volatile private static int serverPort = 0;5758/*59* The TestServer implements a OriginServer that60* processes HTTP requests and responses.61*/62static class TestServer extends OriginServer {63public TestServer(ServerSocket ss) throws Exception {64super(ss);65}6667/*68* Returns an array of bytes containing the bytes for69* the data sent in the response.70*71* @return bytes for the data in the response72*/73public byte[] getBytes() {74return75"Https POST thru proxy is successful with proxy authentication".76getBytes();77}78}7980/*81* Main method to create the server and client82*/83public static void main(String args[]) throws Exception {84String keyFilename = TEST_SRC + "/" + pathToStores + "/" + keyStoreFile;85String trustFilename = TEST_SRC + "/" + pathToStores + "/"86+ trustStoreFile;8788System.setProperty("javax.net.ssl.keyStore", keyFilename);89System.setProperty("javax.net.ssl.keyStorePassword", passwd);90System.setProperty("javax.net.ssl.trustStore", trustFilename);91System.setProperty("javax.net.ssl.trustStorePassword", passwd);9293boolean useSSL = true;94/*95* setup the server96*/97try {98ServerSocketFactory ssf = getServerSocketFactory(useSSL);99ServerSocket ss = ssf.createServerSocket(serverPort);100ss.setSoTimeout(TIMEOUT); // 30 seconds101serverPort = ss.getLocalPort();102new TestServer(ss);103} catch (Exception e) {104System.out.println("Server side failed:" +105e.getMessage());106throw e;107}108// trigger the client109try {110doClientSide();111} catch (Exception e) {112System.out.println("Client side failed: " +113e.getMessage());114throw e;115}116}117118private static ServerSocketFactory getServerSocketFactory119(boolean useSSL) throws Exception {120if (useSSL) {121// set up key manager to do server authentication122SSLContext ctx = SSLContext.getInstance("TLS");123KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");124KeyStore ks = KeyStore.getInstance("JKS");125char[] passphrase = passwd.toCharArray();126127ks.load(new FileInputStream(System.getProperty(128"javax.net.ssl.keyStore")), passphrase);129kmf.init(ks, passphrase);130ctx.init(kmf.getKeyManagers(), null, null);131132return ctx.getServerSocketFactory();133} else {134return ServerSocketFactory.getDefault();135}136}137138/*139* Message to be posted140*/141static String postMsg = "Testing HTTP post on a https server";142143static void doClientSide() throws Exception {144/*145* setup up a proxy146*/147SocketAddress pAddr = setupProxy();148149/*150* we want to avoid URLspoofCheck failures in cases where the cert151* DN name does not match the hostname in the URL.152*/153HttpsURLConnection.setDefaultHostnameVerifier(154new NameVerifier());155URL url = new URL("https://" + getHostname() + ":" + serverPort);156157Proxy p = new Proxy(Proxy.Type.HTTP, pAddr);158HttpsURLConnection https = (HttpsURLConnection)url.openConnection(p);159https.setConnectTimeout(TIMEOUT);160https.setReadTimeout(TIMEOUT);161https.setDoOutput(true);162https.setRequestMethod("POST");163PrintStream ps = null;164try {165ps = new PrintStream(https.getOutputStream());166ps.println(postMsg);167ps.flush();168if (https.getResponseCode() != 200) {169throw new RuntimeException("test Failed");170}171ps.close();172// clear the pipe173BufferedReader in = new BufferedReader(174new InputStreamReader(175https.getInputStream()));176String inputLine;177while ((inputLine = in.readLine()) != null)178System.out.println("Client received: " + inputLine);179in.close();180} catch (SSLException e) {181if (ps != null)182ps.close();183throw e;184} catch (SocketTimeoutException e) {185System.out.println("Client can not get response in time: "186+ e.getMessage());187}188}189190static class NameVerifier implements HostnameVerifier {191public boolean verify(String hostname, SSLSession session) {192return true;193}194}195196static SocketAddress setupProxy() throws IOException {197ProxyTunnelServer pserver = new ProxyTunnelServer();198199/*200* register a system wide authenticator and setup the proxy for201* authentication202*/203Authenticator.setDefault(new TestAuthenticator());204205// register with the username and password206pserver.needUserAuth(true);207pserver.setUserAuth("Test", "test123");208209pserver.start();210211return new InetSocketAddress("localhost", pserver.getPort());212}213214public static class TestAuthenticator extends Authenticator {215public PasswordAuthentication getPasswordAuthentication() {216return new PasswordAuthentication("Test",217"test123".toCharArray());218}219}220221private static String getHostname() {222try {223OutputAnalyzer oa = ProcessTools.executeCommand("hostname");224return oa.getOutput().trim();225} catch (Throwable e) {226throw new RuntimeException("Get hostname failed.", e);227}228}229}230231232