Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/net/www/protocol/https/NewImpl/JavaxHTTPSConnection.java
38889 views
/*1* Copyright (c) 2001, 2011, 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 JavaxHTTPSConnection28*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 java.security.cert.*;37import javax.net.ssl.*;3839/**40* See if we can obtain a javax.net.ssl.HttpsURLConnection,41* and then play with it a bit.42*/43public class JavaxHTTPSConnection {4445/*46* =============================================================47* Set the various variables needed for the tests, then48* specify what tests to run on each side.49*/5051/*52* Should we run the client or server in a separate thread?53* Both sides can throw exceptions, but do you have a preference54* as to which side should be the main thread.55*/56static boolean separateServerThread = true;5758/*59* Where do we find the keystores?60*/61static String pathToStores = "../../../../../../javax/net/ssl/etc";62static String keyStoreFile = "keystore";63static String trustStoreFile = "truststore";64static String passwd = "passphrase";6566/*67* Is the server ready to serve?68*/69volatile static boolean serverReady = false;7071/*72* Turn on SSL debugging?73*/74static boolean debug = false;7576/*77* If the client or server is doing some kind of object creation78* that the other side depends on, and that thread prematurely79* exits, you may experience a hang. The test harness will80* terminate all hung threads after its timeout has expired,81* currently 3 minutes by default, but you might try to be82* smart about it....83*/8485/**86* Returns the path to the file obtained from87* parsing the HTML header.88*/89private static String getPath(DataInputStream in)90throws IOException91{92String line = in.readLine();93String path = "";94// extract class from GET line95if (line.startsWith("GET /")) {96line = line.substring(5, line.length()-1).trim();97int index = line.indexOf(' ');98if (index != -1) {99path = line.substring(0, index);100}101}102103// eat the rest of header104do {105line = in.readLine();106} while ((line.length() != 0) &&107(line.charAt(0) != '\r') && (line.charAt(0) != '\n'));108109if (path.length() != 0) {110return path;111} else {112throw new IOException("Malformed Header");113}114}115116/**117* Returns an array of bytes containing the bytes for118* the file represented by the argument <b>path</b>.119*120* In our case, we just pretend to send something back.121*122* @return the bytes for the file123* @exception FileNotFoundException if the file corresponding124* to <b>path</b> could not be loaded.125*/126private byte[] getBytes(String path)127throws IOException128{129return "Hello world, I am here".getBytes();130}131132/*133* Define the server side of the test.134*135* If the server prematurely exits, serverReady will be set to true136* to avoid infinite hangs.137*/138void doServerSide() throws Exception {139140SSLServerSocketFactory sslssf =141(SSLServerSocketFactory) SSLServerSocketFactory.getDefault();142SSLServerSocket sslServerSocket =143(SSLServerSocket) sslssf.createServerSocket(serverPort);144serverPort = sslServerSocket.getLocalPort();145146/*147* Signal Client, we're ready for his connect.148*/149serverReady = true;150151SSLSocket sslSocket = (SSLSocket) sslServerSocket.accept();152DataOutputStream out =153new DataOutputStream(sslSocket.getOutputStream());154155try {156// get path to class file from header157DataInputStream in =158new DataInputStream(sslSocket.getInputStream());159String path = getPath(in);160// retrieve bytecodes161byte[] bytecodes = getBytes(path);162// send bytecodes in response (assumes HTTP/1.0 or later)163try {164out.writeBytes("HTTP/1.0 200 OK\r\n");165out.writeBytes("Content-Length: " + bytecodes.length + "\r\n");166out.writeBytes("Content-Type: text/html\r\n\r\n");167out.write(bytecodes);168out.flush();169} catch (IOException ie) {170ie.printStackTrace();171return;172}173174} catch (Exception e) {175e.printStackTrace();176// write out error response177out.writeBytes("HTTP/1.0 400 " + e.getMessage() + "\r\n");178out.writeBytes("Content-Type: text/html\r\n\r\n");179out.flush();180} finally {181// close the socket182System.out.println("Server closing socket");183sslSocket.close();184serverReady = false;185}186}187188/*189* Define the client side of the test.190*191* If the server prematurely exits, serverReady will be set to true192* to avoid infinite hangs.193*/194void doClientSide() throws Exception {195HostnameVerifier reservedHV =196HttpsURLConnection.getDefaultHostnameVerifier();197try {198/*199* Wait for server to get started.200*/201while (!serverReady) {202Thread.sleep(50);203}204205HttpsURLConnection.setDefaultHostnameVerifier(new NameVerifier());206URL url = new URL("https://" + "localhost:" + serverPort +207"/etc/hosts");208URLConnection urlc = url.openConnection();209210if (!(urlc instanceof javax.net.ssl.HttpsURLConnection)) {211throw new Exception("URLConnection ! instanceof " +212"javax.net.ssl.HttpsURLConnection");213}214215BufferedReader in = null;216try {217in = new BufferedReader(new InputStreamReader(218urlc.getInputStream()));219String inputLine;220System.out.print("Client reading... ");221while ((inputLine = in.readLine()) != null)222System.out.println(inputLine);223224System.out.println("Cipher Suite: " +225((HttpsURLConnection)urlc).getCipherSuite());226Certificate[] certs =227((HttpsURLConnection)urlc).getServerCertificates();228for (int i = 0; i < certs.length; i++) {229System.out.println(certs[0]);230}231232in.close();233} catch (SSLException e) {234if (in != null)235in.close();236throw e;237}238System.out.println("Client reports: SUCCESS");239} finally {240HttpsURLConnection.setDefaultHostnameVerifier(reservedHV);241}242}243244static class NameVerifier implements HostnameVerifier {245public boolean verify(String hostname, SSLSession session) {246System.out.println(247"HostnameVerifier: returning true");248return true;249}250}251252/*253* =============================================================254* The remainder is just support stuff255*/256257// use any free port by default258volatile int serverPort = 0;259260volatile Exception serverException = null;261volatile Exception clientException = null;262263public static void main(String[] args) throws Exception {264String keyFilename =265System.getProperty("test.src", "./") + "/" + pathToStores +266"/" + keyStoreFile;267String trustFilename =268System.getProperty("test.src", "./") + "/" + pathToStores +269"/" + trustStoreFile;270271System.setProperty("javax.net.ssl.keyStore", keyFilename);272System.setProperty("javax.net.ssl.keyStorePassword", passwd);273System.setProperty("javax.net.ssl.trustStore", trustFilename);274System.setProperty("javax.net.ssl.trustStorePassword", passwd);275276if (debug)277System.setProperty("javax.net.debug", "all");278279/*280* Start the tests.281*/282new JavaxHTTPSConnection();283}284285Thread clientThread = null;286Thread serverThread = null;287288/*289* Primary constructor, used to drive remainder of the test.290*291* Fork off the other side, then do your work.292*/293JavaxHTTPSConnection() throws Exception {294if (separateServerThread) {295startServer(true);296startClient(false);297} else {298startClient(true);299startServer(false);300}301302/*303* Wait for other side to close down.304*/305if (separateServerThread) {306serverThread.join();307} else {308clientThread.join();309}310311/*312* When we get here, the test is pretty much over.313*314* If the main thread excepted, that propagates back315* immediately. If the other thread threw an exception, we316* should report back.317*/318if (serverException != null) {319System.out.print("Server Exception:");320throw serverException;321}322if (clientException != null) {323System.out.print("Client Exception:");324throw clientException;325}326}327328void startServer(boolean newThread) throws Exception {329if (newThread) {330serverThread = new Thread() {331public void run() {332try {333doServerSide();334} catch (Exception e) {335/*336* Our server thread just died.337*338* Release the client, if not active already...339*/340System.err.println("Server died...");341serverReady = true;342serverException = e;343}344}345};346serverThread.start();347} else {348doServerSide();349}350}351352void startClient(boolean newThread) throws Exception {353if (newThread) {354clientThread = new Thread() {355public void run() {356try {357doClientSide();358} catch (Exception e) {359/*360* Our client thread just died.361*/362System.err.println("Client died...");363clientException = e;364}365}366};367clientThread.start();368} else {369doClientSide();370}371}372}373374375