Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/ssl/ProtocolVersion/HttpsProtocols.java
38853 views
/*1* Copyright (c) 2002, 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 467128926* @summary passing https.protocols from command line doesn't work.27* @run main/othervm -Dhttps.protocols=SSLv3 HttpsProtocols28* @author Brad Wetmore29*/3031import java.io.*;32import java.net.*;33import javax.net.ssl.*;34import java.security.Security;3536public class HttpsProtocols implements HostnameVerifier {3738/*39* =============================================================40* Set the various variables needed for the tests, then41* specify what tests to run on each side.42*/4344/*45* Should we run the client or server in a separate thread?46* Both sides can throw exceptions, but do you have a preference47* as to which side should be the main thread.48*/49static boolean separateServerThread = true;5051/*52* Where do we find the keystores?53*/54static String pathToStores = "../../../../javax/net/ssl/etc";55static String keyStoreFile = "keystore";56static String trustStoreFile = "truststore";57static String passwd = "passphrase";5859/*60* Is the server ready to serve?61*/62volatile static boolean serverReady = false;6364/*65* Turn on SSL debugging?66*/67static boolean debug = false;6869/*70* If the client or server is doing some kind of object creation71* that the other side depends on, and that thread prematurely72* exits, you may experience a hang. The test harness will73* terminate all hung threads after its timeout has expired,74* currently 3 minutes by default, but you might try to be75* smart about it....76*/7778/*79* Define the server side of the test.80*81* If the server prematurely exits, serverReady will be set to true82* to avoid infinite hangs.83*/84void doServerSide() throws Exception {85SSLServerSocketFactory sslssf =86(SSLServerSocketFactory) SSLServerSocketFactory.getDefault();87SSLServerSocket sslServerSocket =88(SSLServerSocket) sslssf.createServerSocket(serverPort);8990serverPort = sslServerSocket.getLocalPort();9192/*93* Signal Client, we're ready for his connect.94*/95serverReady = true;9697SSLSocket sslSocket = (SSLSocket) sslServerSocket.accept();9899DataOutputStream out =100new DataOutputStream(sslSocket.getOutputStream());101102BufferedReader in =103new BufferedReader(new InputStreamReader(104sslSocket.getInputStream()));105106// read the request107readRequest(in);108109byte [] bytecodes = "Hello world".getBytes();110111out.writeBytes("HTTP/1.0 200 OK\r\n");112out.writeBytes("Content-Length: " + bytecodes.length +113"\r\n");114115out.writeBytes("Content-Type: text/html\r\n\r\n");116out.write(bytecodes);117out.flush();118119sslSocket.close();120}121122/**123* read the response, don't care for the syntax of the request-line124*/125private static void readRequest(BufferedReader in)126throws IOException {127String line = null;128do {129line = in.readLine();130System.out.println("Server received: " + line);131} while ((line.length() != 0) &&132(line.charAt(0) != '\r') && (line.charAt(0) != '\n'));133}134135/*136* Define the client side of the test.137*138* If the server prematurely exits, serverReady will be set to true139* to avoid infinite hangs.140*/141void doClientSide() throws Exception {142143/*144* Wait for server to get started.145*/146while (!serverReady) {147Thread.sleep(50);148}149150HostnameVerifier reservedHV =151HttpsURLConnection.getDefaultHostnameVerifier();152try {153HttpsURLConnection.setDefaultHostnameVerifier(this);154155URL url = new URL("https://localhost:" + serverPort + "/");156HttpURLConnection urlc = (HttpURLConnection) url.openConnection();157158System.out.println("response is " + urlc.getResponseCode());159} finally {160HttpsURLConnection.setDefaultHostnameVerifier(reservedHV);161}162}163164public boolean verify(String hostname, SSLSession session) {165return true;166}167168/*169* =============================================================170* The remainder is just support stuff171*/172173// use any free port by default174volatile int serverPort = 0;175176volatile Exception serverException = null;177volatile Exception clientException = null;178179public static void main(String[] args) throws Exception {180// reset the security property to make sure that the algorithms181// and keys used in this test are not disabled.182Security.setProperty("jdk.tls.disabledAlgorithms", "");183184String keyFilename =185System.getProperty("test.src", "./") + "/" + pathToStores +186"/" + keyStoreFile;187String trustFilename =188System.getProperty("test.src", "./") + "/" + pathToStores +189"/" + trustStoreFile;190191System.setProperty("javax.net.ssl.keyStore", keyFilename);192System.setProperty("javax.net.ssl.keyStorePassword", passwd);193System.setProperty("javax.net.ssl.trustStore", trustFilename);194System.setProperty("javax.net.ssl.trustStorePassword", passwd);195196String prop = System.getProperty("https.protocols");197System.out.println("protocols = " + prop);198199if ((prop == null) || (!prop.equals("SSLv3"))) {200throw new Exception("https.protocols not set properly");201}202203if (debug)204System.setProperty("javax.net.debug", "all");205206/*207* Start the tests.208*/209new HttpsProtocols();210}211212Thread clientThread = null;213Thread serverThread = null;214215/*216* Primary constructor, used to drive remainder of the test.217*218* Fork off the other side, then do your work.219*/220HttpsProtocols() throws Exception {221if (separateServerThread) {222startServer(true);223startClient(false);224} else {225startClient(true);226startServer(false);227}228229/*230* Wait for other side to close down.231*/232if (separateServerThread) {233serverThread.join();234} else {235clientThread.join();236}237238/*239* When we get here, the test is pretty much over.240*241* If the main thread excepted, that propagates back242* immediately. If the other thread threw an exception, we243* should report back.244*/245if (serverException != null) {246System.out.print("Server Exception:");247throw serverException;248}249if (clientException != null) {250System.out.print("Client Exception:");251throw clientException;252}253}254255void startServer(boolean newThread) throws Exception {256if (newThread) {257serverThread = new Thread() {258public void run() {259try {260doServerSide();261} catch (Exception e) {262/*263* Our server thread just died.264*265* Release the client, if not active already...266*/267System.err.println("Server died...");268serverReady = true;269serverException = e;270}271}272};273serverThread.start();274} else {275doServerSide();276}277}278279void startClient(boolean newThread) throws Exception {280if (newThread) {281clientThread = new Thread() {282public void run() {283try {284doClientSide();285} catch (Exception e) {286/*287* Our client thread just died.288*/289System.err.println("Client died...");290clientException = e;291}292}293};294clientThread.start();295} else {296doClientSide();297}298}299}300301302