Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/net/www/protocol/https/NewImpl/ComHTTPSConnection.java
38889 views
/*1* Copyright (c) 2001, 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* @test25* @bug 447425526* @summary Can no longer obtain a com.sun.net.ssl.HttpsURLConnection27* @run main/othervm ComHTTPSConnection28*29* SunJSSE does not support dynamic system properties, no way to re-use30* system properties in samevm/agentvm mode.31* @author Brad Wetmore32*/3334import java.io.*;35import java.net.*;36import javax.security.cert.X509Certificate;37import javax.net.ssl.*;38import com.sun.net.ssl.HostnameVerifier;39import com.sun.net.ssl.HttpsURLConnection;4041/**42* See if we can obtain a com.sun.net.ssl.HttpsURLConnection,43* and then play with it a bit.44*/45public class ComHTTPSConnection {4647/*48* =============================================================49* Set the various variables needed for the tests, then50* specify what tests to run on each side.51*/5253/*54* Should we run the client or server in a separate thread?55* Both sides can throw exceptions, but do you have a preference56* as to which side should be the main thread.57*/58static boolean separateServerThread = true;5960/*61* Where do we find the keystores?62*/63static String pathToStores = "../../../../../../javax/net/ssl/etc";64static String keyStoreFile = "keystore";65static String trustStoreFile = "truststore";66static String passwd = "passphrase";6768/*69* Is the server ready to serve?70*/71volatile static boolean serverReady = false;7273/*74* Turn on SSL debugging?75*/76static boolean debug = false;7778/*79* If the client or server is doing some kind of object creation80* that the other side depends on, and that thread prematurely81* exits, you may experience a hang. The test harness will82* terminate all hung threads after its timeout has expired,83* currently 3 minutes by default, but you might try to be84* smart about it....85*/8687/**88* Returns the path to the file obtained from89* parsing the HTML header.90*/91private static String getPath(DataInputStream in)92throws IOException93{94String line = in.readLine();95String path = "";96// extract class from GET line97if (line.startsWith("GET /")) {98line = line.substring(5, line.length()-1).trim();99int index = line.indexOf(' ');100if (index != -1) {101path = line.substring(0, index);102}103}104105// eat the rest of header106do {107line = in.readLine();108} while ((line.length() != 0) &&109(line.charAt(0) != '\r') && (line.charAt(0) != '\n'));110111if (path.length() != 0) {112return path;113} else {114throw new IOException("Malformed Header");115}116}117118/**119* Returns an array of bytes containing the bytes for120* the file represented by the argument <b>path</b>.121*122* In our case, we just pretend to send something back.123*124* @return the bytes for the file125* @exception FileNotFoundException if the file corresponding126* to <b>path</b> could not be loaded.127*/128private byte[] getBytes(String path)129throws IOException130{131return "Hello world, I am here".getBytes();132}133134/*135* Define the server side of the test.136*137* If the server prematurely exits, serverReady will be set to true138* to avoid infinite hangs.139*/140void doServerSide() throws Exception {141142SSLServerSocketFactory sslssf =143(SSLServerSocketFactory) SSLServerSocketFactory.getDefault();144SSLServerSocket sslServerSocket =145(SSLServerSocket) sslssf.createServerSocket(serverPort);146serverPort = sslServerSocket.getLocalPort();147148/*149* Signal Client, we're ready for his connect.150*/151serverReady = true;152153SSLSocket sslSocket = (SSLSocket) sslServerSocket.accept();154DataOutputStream out =155new DataOutputStream(sslSocket.getOutputStream());156157try {158// get path to class file from header159DataInputStream in =160new DataInputStream(sslSocket.getInputStream());161String path = getPath(in);162// retrieve bytecodes163byte[] bytecodes = getBytes(path);164// send bytecodes in response (assumes HTTP/1.0 or later)165try {166out.writeBytes("HTTP/1.0 200 OK\r\n");167out.writeBytes("Content-Length: " + bytecodes.length + "\r\n");168out.writeBytes("Content-Type: text/html\r\n\r\n");169out.write(bytecodes);170out.flush();171} catch (IOException ie) {172ie.printStackTrace();173return;174}175176} catch (Exception e) {177e.printStackTrace();178// write out error response179out.writeBytes("HTTP/1.0 400 " + e.getMessage() + "\r\n");180out.writeBytes("Content-Type: text/html\r\n\r\n");181out.flush();182} finally {183// close the socket184System.out.println("Server closing socket");185sslSocket.close();186serverReady = false;187}188}189190private static class ComSunHTTPSHandlerFactory implements URLStreamHandlerFactory {191private static String SUPPORTED_PROTOCOL = "https";192193public URLStreamHandler createURLStreamHandler(String protocol) {194if (!protocol.equalsIgnoreCase(SUPPORTED_PROTOCOL))195return null;196197return new com.sun.net.ssl.internal.www.protocol.https.Handler();198}199}200201/*202* Define the client side of the test.203*204* If the server prematurely exits, serverReady will be set to true205* to avoid infinite hangs.206*/207void doClientSide() throws Exception {208/*209* Wait for server to get started.210*/211while (!serverReady) {212Thread.sleep(50);213}214215HostnameVerifier reservedHV =216HttpsURLConnection.getDefaultHostnameVerifier();217try {218URL.setURLStreamHandlerFactory(new ComSunHTTPSHandlerFactory());219HttpsURLConnection.setDefaultHostnameVerifier(new NameVerifier());220221URL url = new URL("https://" + "localhost:" + serverPort +222"/etc/hosts");223URLConnection urlc = url.openConnection();224225if (!(urlc instanceof com.sun.net.ssl.HttpsURLConnection)) {226throw new Exception(227"URLConnection ! instanceof " +228"com.sun.net.ssl.HttpsURLConnection");229}230231BufferedReader in = null;232try {233in = new BufferedReader(new InputStreamReader(234urlc.getInputStream()));235String inputLine;236System.out.print("Client reading... ");237while ((inputLine = in.readLine()) != null)238System.out.println(inputLine);239240System.out.println("Cipher Suite: " +241((HttpsURLConnection)urlc).getCipherSuite());242X509Certificate[] certs =243((HttpsURLConnection)urlc).getServerCertificateChain();244for (int i = 0; i < certs.length; i++) {245System.out.println(certs[0]);246}247248in.close();249} catch (SSLException e) {250if (in != null)251in.close();252throw e;253}254System.out.println("Client reports: SUCCESS");255} finally {256HttpsURLConnection.setDefaultHostnameVerifier(reservedHV);257}258}259260static class NameVerifier implements HostnameVerifier {261public boolean verify(String urlHostname,262String certHostname) {263System.out.println(264"CertificateHostnameVerifier: " + urlHostname + " == "265+ certHostname + " returning true");266return true;267}268}269270/*271* =============================================================272* The remainder is just support stuff273*/274275// use any free port by default276volatile int serverPort = 0;277278volatile Exception serverException = null;279volatile Exception clientException = null;280281public static void main(String[] args) throws Exception {282String keyFilename =283System.getProperty("test.src", "./") + "/" + pathToStores +284"/" + keyStoreFile;285String trustFilename =286System.getProperty("test.src", "./") + "/" + pathToStores +287"/" + trustStoreFile;288289System.setProperty("javax.net.ssl.keyStore", keyFilename);290System.setProperty("javax.net.ssl.keyStorePassword", passwd);291System.setProperty("javax.net.ssl.trustStore", trustFilename);292System.setProperty("javax.net.ssl.trustStorePassword", passwd);293294if (debug)295System.setProperty("javax.net.debug", "all");296297/*298* Start the tests.299*/300new ComHTTPSConnection();301}302303Thread clientThread = null;304Thread serverThread = null;305306/*307* Primary constructor, used to drive remainder of the test.308*309* Fork off the other side, then do your work.310*/311ComHTTPSConnection() throws Exception {312if (separateServerThread) {313startServer(true);314startClient(false);315} else {316startClient(true);317startServer(false);318}319320/*321* Wait for other side to close down.322*/323if (separateServerThread) {324serverThread.join();325} else {326clientThread.join();327}328329/*330* When we get here, the test is pretty much over.331*332* If the main thread excepted, that propagates back333* immediately. If the other thread threw an exception, we334* should report back.335*/336if (serverException != null) {337System.out.print("Server Exception:");338throw serverException;339}340if (clientException != null) {341System.out.print("Client Exception:");342throw clientException;343}344}345346void startServer(boolean newThread) throws Exception {347if (newThread) {348serverThread = new Thread() {349public void run() {350try {351doServerSide();352} catch (Exception e) {353/*354* Our server thread just died.355*356* Release the client, if not active already...357*/358System.err.println("Server died...");359serverReady = true;360serverException = e;361}362}363};364serverThread.start();365} else {366doServerSide();367}368}369370void startClient(boolean newThread) throws Exception {371if (newThread) {372clientThread = new Thread() {373public void run() {374try {375doClientSide();376} catch (Exception e) {377/*378* Our client thread just died.379*/380System.err.println("Client died...");381clientException = e;382}383}384};385clientThread.start();386} else {387doClientSide();388}389}390}391392393