Path: blob/master/test/jdk/sun/net/www/protocol/https/HttpsURLConnection/HttpsPost.java
41161 views
/*1* Copyright (c) 2001, 2019, 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 442307426* @summary Need to rebase all the duplicated classes from Merlin.27* This test will check out http POST28* @library /test/lib29* @run main/othervm HttpsPost30* @run main/othervm -Djava.net.preferIPv6Addresses=true HttpsPost31*32* SunJSSE does not support dynamic system properties, no way to re-use33* system properties in samevm/agentvm mode.34*/3536import java.io.*;37import java.net.*;38import javax.net.ssl.*;39import jdk.test.lib.net.URIBuilder;4041public class HttpsPost {4243/*44* =============================================================45* Set the various variables needed for the tests, then46* specify what tests to run on each side.47*/4849/*50* Should we run the client or server in a separate thread?51* Both sides can throw exceptions, but do you have a preference52* as to which side should be the main thread.53*/54static boolean separateServerThread = true;5556/*57* Where do we find the keystores?58*/59static String pathToStores = "../../../../../../javax/net/ssl/etc";60static String keyStoreFile = "keystore";61static String trustStoreFile = "truststore";62static String passwd = "passphrase";6364/*65* Is the server ready to serve?66*/67volatile static boolean serverReady = false;6869/*70* Is the connection ready to close?71*/72volatile static boolean closeReady = false;7374/*75* Turn on SSL debugging?76*/77static boolean debug = false;7879/*80* Message posted81*/82static String postMsg = "Testing HTTP post on a https server";8384/*85* If the client or server is doing some kind of object creation86* that the other side depends on, and that thread prematurely87* exits, you may experience a hang. The test harness will88* terminate all hung threads after its timeout has expired,89* currently 3 minutes by default, but you might try to be90* smart about it....91*/9293/*94* Define the server side of the test.95*96* If the server prematurely exits, serverReady will be set to true97* to avoid infinite hangs.98*/99void doServerSide() throws Exception {100InetAddress loopback = InetAddress.getLoopbackAddress();101SSLServerSocketFactory sslssf =102(SSLServerSocketFactory) SSLServerSocketFactory.getDefault();103SSLServerSocket sslServerSocket =104(SSLServerSocket) sslssf.createServerSocket(serverPort, 0, loopback);105serverPort = sslServerSocket.getLocalPort();106107System.out.println("Starting server at: "108+ sslServerSocket.getInetAddress()109+ ":" + serverPort);110/*111* Signal Client, we're ready for his connect.112*/113serverReady = true;114115SSLSocket sslSocket = (SSLSocket) sslServerSocket.accept();116try {117InputStream sslIS = sslSocket.getInputStream();118OutputStream sslOS = sslSocket.getOutputStream();119BufferedReader br =120new BufferedReader(new InputStreamReader(sslIS));121PrintStream ps = new PrintStream(sslOS);122123// process HTTP POST request from client124System.out.println("status line: "+br.readLine());125String msg = null;126while ((msg = br.readLine()) != null && msg.length() > 0);127128msg = br.readLine();129if (msg.equals(postMsg)) {130ps.println("HTTP/1.1 200 OK\n\n");131} else {132ps.println("HTTP/1.1 500 Not OK\n\n");133}134ps.flush();135136// close the socket137while (!closeReady) {138Thread.sleep(50);139}140} finally {141sslSocket.close();142sslServerSocket.close();143}144}145146/*147* Define the client side of the test.148*149* If the server prematurely exits, serverReady will be set to true150* to avoid infinite hangs.151*/152void doClientSide() throws Exception {153HostnameVerifier reservedHV =154HttpsURLConnection.getDefaultHostnameVerifier();155try {156/*157* Wait for server to get started.158*/159while (!serverReady) {160Thread.sleep(50);161}162163// Send HTTP POST request to server164URL url = URIBuilder.newBuilder()165.scheme("https")166.loopback()167.port(serverPort)168.toURL();169170System.out.println("Client connecting to: " + url);171HttpsURLConnection.setDefaultHostnameVerifier(new NameVerifier());172HttpsURLConnection http = (HttpsURLConnection)url.openConnection(Proxy.NO_PROXY);173http.setDoOutput(true);174175http.setRequestMethod("POST");176PrintStream ps = new PrintStream(http.getOutputStream());177try {178ps.println(postMsg);179ps.flush();180if (http.getResponseCode() != 200) {181throw new RuntimeException("test Failed");182}183} finally {184ps.close();185http.disconnect();186closeReady = true;187}188} finally {189HttpsURLConnection.setDefaultHostnameVerifier(reservedHV);190}191}192193static class NameVerifier implements HostnameVerifier {194public boolean verify(String hostname, SSLSession session) {195return true;196}197}198199/*200* =============================================================201* The remainder is just support stuff202*/203204// use any free port by default205volatile int serverPort = 0;206207volatile Exception serverException = null;208volatile Exception clientException = null;209210public static void main(String[] args) throws Exception {211String keyFilename =212System.getProperty("test.src", "./") + "/" + pathToStores +213"/" + keyStoreFile;214String trustFilename =215System.getProperty("test.src", "./") + "/" + pathToStores +216"/" + trustStoreFile;217218System.setProperty("javax.net.ssl.keyStore", keyFilename);219System.setProperty("javax.net.ssl.keyStorePassword", passwd);220System.setProperty("javax.net.ssl.trustStore", trustFilename);221System.setProperty("javax.net.ssl.trustStorePassword", passwd);222223if (debug)224System.setProperty("javax.net.debug", "all");225226/*227* Start the tests.228*/229new HttpsPost();230}231232Thread clientThread = null;233Thread serverThread = null;234235/*236* Primary constructor, used to drive remainder of the test.237*238* Fork off the other side, then do your work.239*/240HttpsPost() throws Exception {241if (separateServerThread) {242startServer(true);243startClient(false);244} else {245startClient(true);246startServer(false);247}248249/*250* Wait for other side to close down.251*/252if (separateServerThread) {253serverThread.join();254} else {255clientThread.join();256}257258/*259* When we get here, the test is pretty much over.260*261* If the main thread excepted, that propagates back262* immediately. If the other thread threw an exception, we263* should report back.264*/265if (serverException != null)266throw serverException;267if (clientException != null)268throw clientException;269}270271void startServer(boolean newThread) throws Exception {272if (newThread) {273serverThread = new Thread() {274public void run() {275try {276doServerSide();277} catch (Exception e) {278/*279* Our server thread just died.280*281* Release the client, if not active already...282*/283System.err.println("Server died...");284serverReady = true;285serverException = e;286}287}288};289serverThread.start();290} else {291doServerSide();292}293}294295void startClient(boolean newThread) throws Exception {296if (newThread) {297clientThread = new Thread() {298public void run() {299try {300doClientSide();301} catch (Exception e) {302/*303* Our client thread just died.304*/305System.err.println("Client died...");306clientException = e;307}308}309};310clientThread.start();311} else {312doClientSide();313}314}315}316317318