Path: blob/master/test/jdk/javax/net/ssl/HttpsURLConnection/GetResponseCode.java
41152 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 448218726* @summary HttpsClient tests are failing for build 7127* @run main/othervm GetResponseCode28*29* SunJSSE does not support dynamic system properties, no way to re-use30* system properties in samevm/agentvm mode.31*32* @author Yingxian Wang33*/34import java.io.*;35import java.net.*;36import javax.net.ssl.*;37import java.security.cert.Certificate;3839public class GetResponseCode implements HostnameVerifier {40/*41* =============================================================42* Set the various variables needed for the tests, then43* specify what tests to run on each side.44*/4546/*47* Should we run the client or server in a separate thread?48* Both sides can throw exceptions, but do you have a preference49* as to which side should be the main thread.50*/51static boolean separateServerThread = true;5253/*54* Where do we find the keystores?55*/56static String pathToStores = "../etc";57static String keyStoreFile = "keystore";58static String trustStoreFile = "truststore";59static String passwd = "passphrase";6061/*62* Is the server ready to serve?63*/64volatile static boolean serverReady = false;6566/*67* Turn on SSL debugging?68*/69static boolean debug = false;7071/*72* If the client or server is doing some kind of object creation73* that the other side depends on, and that thread prematurely74* exits, you may experience a hang. The test harness will75* terminate all hung threads after its timeout has expired,76* currently 3 minutes by default, but you might try to be77* smart about it....78*/7980/*81* Define the server side of the test.82*83* If the server prematurely exits, serverReady will be set to true84* to avoid infinite hangs.85*/86void doServerSide() throws Exception {87SSLServerSocketFactory sslssf =88(SSLServerSocketFactory) SSLServerSocketFactory.getDefault();89SSLServerSocket sslServerSocket =90(SSLServerSocket) sslssf.createServerSocket(serverPort);91serverPort = sslServerSocket.getLocalPort();9293/*94* Signal Client, we're ready for his connect.95*/96serverReady = true;9798SSLSocket sslSocket = (SSLSocket) sslServerSocket.accept();99OutputStream sslOS = sslSocket.getOutputStream();100BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(sslOS));101bw.write("HTTP/1.1 200 OK\r\n\r\n\r\n");102bw.flush();103Thread.sleep(5000);104sslSocket.close();105}106107/*108* Define the client side of the test.109*110* If the server prematurely exits, serverReady will be set to true111* to avoid infinite hangs.112*/113void doClientSide() throws Exception {114115/*116* Wait for server to get started.117*/118while (!serverReady) {119Thread.sleep(50);120}121122URL url = new URL("https://localhost:"+serverPort+"/index.html");123HttpsURLConnection urlc = (HttpsURLConnection)url.openConnection();124urlc.setHostnameVerifier(this);125urlc.getInputStream();126127if (urlc.getResponseCode() == -1) {128throw new RuntimeException("getResponseCode() returns -1");129}130}131132/*133* =============================================================134* The remainder is just support stuff135*/136137// use any free port by default138volatile int serverPort = 0;139140volatile Exception serverException = null;141volatile Exception clientException = null;142143public static void main(String[] args) throws Exception {144String keyFilename =145System.getProperty("test.src", "./") + "/" + pathToStores +146"/" + keyStoreFile;147String trustFilename =148System.getProperty("test.src", "./") + "/" + pathToStores +149"/" + trustStoreFile;150151System.setProperty("javax.net.ssl.keyStore", keyFilename);152System.setProperty("javax.net.ssl.keyStorePassword", passwd);153System.setProperty("javax.net.ssl.trustStore", trustFilename);154System.setProperty("javax.net.ssl.trustStorePassword", passwd);155156if (debug)157System.setProperty("javax.net.debug", "all");158159/*160* Start the tests.161*/162new GetResponseCode();163}164165Thread clientThread = null;166Thread serverThread = null;167168/*169* Primary constructor, used to drive remainder of the test.170*171* Fork off the other side, then do your work.172*/173GetResponseCode() throws Exception {174if (separateServerThread) {175startServer(true);176startClient(false);177} else {178startClient(true);179startServer(false);180}181182/*183* Wait for other side to close down.184*/185if (separateServerThread) {186serverThread.join();187} else {188clientThread.join();189}190191/*192* When we get here, the test is pretty much over.193*194* If the main thread excepted, that propagates back195* immediately. If the other thread threw an exception, we196* should report back.197*/198if (serverException != null)199throw serverException;200if (clientException != null)201throw clientException;202}203204void startServer(boolean newThread) throws Exception {205if (newThread) {206serverThread = new Thread() {207public void run() {208try {209doServerSide();210} catch (Exception e) {211/*212* Our server thread just died.213*214* Release the client, if not active already...215*/216System.err.println("Server died...");217serverReady = true;218serverException = e;219}220}221};222serverThread.start();223} else {224doServerSide();225}226}227228void startClient(boolean newThread) throws Exception {229if (newThread) {230clientThread = new Thread() {231public void run() {232try {233doClientSide();234} catch (Exception e) {235/*236* Our client thread just died.237*/238System.err.println("Client died...");239clientException = e;240}241}242};243clientThread.start();244} else {245doClientSide();246}247}248249// Simple test method to blindly agree that hostname and certname match250public boolean verify(String hostname, SSLSession session) {251return true;252}253254}255256257