Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/net/www/protocol/https/HttpsURLConnection/HttpsSocketFacTest.java
38889 views
/*1* Copyright (c) 2010, 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 661495726* @summary HttpsURLConnection not using the set SSLSocketFactory for creating all its Sockets27* @run main/othervm HttpsSocketFacTest28*29* SunJSSE does not support dynamic system properties, no way to re-use30* system properties in samevm/agentvm mode.31*/3233import javax.net.SocketFactory;34import javax.net.ssl.HostnameVerifier;35import javax.net.ssl.HttpsURLConnection;36import javax.net.ssl.SSLContext;37import javax.net.ssl.SSLSession;38import javax.net.ssl.SSLSocketFactory;39import java.security.NoSuchAlgorithmException;40import java.net.InetAddress;41import java.net.InetSocketAddress;42import java.net.Socket;43import java.net.URL;44import java.io.BufferedWriter;45import java.io.InputStream;46import java.io.IOException;47import java.io.OutputStreamWriter;48import com.sun.net.httpserver.HttpExchange;49import com.sun.net.httpserver.HttpHandler;50import com.sun.net.httpserver.HttpsConfigurator;5152/*53* This class tests that the HTTPS protocol handler is using its socket factory for54* creating new Sockets. It does this by wrapping the default SSLSocketFactory with55* its own socket factory, SimpleSSLSocketFactory, and verifying that when a https56* connection is made one of the socket factories createSocket methods, that57* actually creates a Socket, is being invoked by the protocol handler.58*/5960public class HttpsSocketFacTest61{62/*63* Where do we find the keystores?64*/65static String pathToStores = "../../../../../../javax/net/ssl/etc";66static String keyStoreFile = "keystore";67static String trustStoreFile = "truststore";68static String passwd = "passphrase";6970com.sun.net.httpserver.HttpsServer httpsServer;71MyHandler httpHandler;7273public static void main(String[] args) {74String keyFilename =75System.getProperty("test.src", "./") + "/" + pathToStores +76"/" + keyStoreFile;77String trustFilename =78System.getProperty("test.src", "./") + "/" + pathToStores +79"/" + trustStoreFile;8081System.setProperty("javax.net.ssl.keyStore", keyFilename);82System.setProperty("javax.net.ssl.keyStorePassword", passwd);83System.setProperty("javax.net.ssl.trustStore", trustFilename);84System.setProperty("javax.net.ssl.trustStorePassword", passwd);8586new HttpsSocketFacTest();87}8889public HttpsSocketFacTest() {90try {91startHttpsServer();92doClient();93} catch (NoSuchAlgorithmException e) {94e.printStackTrace();95} catch (IOException ioe) {96ioe.printStackTrace();97} finally {98httpsServer.stop(1);99}100}101102void doClient() throws IOException {103InetSocketAddress address = httpsServer.getAddress();104URL url = new URL("https://localhost:" + address.getPort() + "/test6614957/");105System.out.println("trying to connect to " + url + "...");106107HttpsURLConnection uc = (HttpsURLConnection) url.openConnection();108SimpleSSLSocketFactory sssf = new SimpleSSLSocketFactory();109uc.setSSLSocketFactory(sssf);110uc.setHostnameVerifier(new AllHostnameVerifier());111InputStream is = uc.getInputStream();112113byte[] ba = new byte[1024];114int read = 0;115while ((read = is.read(ba)) != -1) {116System.out.println(new String(ba, 0, read));117}118119System.out.println("SimpleSSLSocketFactory.socketCreated = " + sssf.socketCreated);120System.out.println("SimpleSSLSocketFactory.socketWrapped = " + sssf.socketWrapped);121122if (!sssf.socketCreated)123throw new RuntimeException("Failed: Socket Factory not being called to create Socket");124}125126/**127* Https Server128*/129public void startHttpsServer() throws IOException, NoSuchAlgorithmException {130httpsServer = com.sun.net.httpserver.HttpsServer.create(new InetSocketAddress(0), 0);131httpsServer.createContext("/test6614957/", new MyHandler());132httpsServer.setHttpsConfigurator(new HttpsConfigurator(SSLContext.getDefault()));133httpsServer.start();134}135136class MyHandler implements HttpHandler {137private String message = "This is a message!";138139@Override140public void handle(HttpExchange t) throws IOException {141t.sendResponseHeaders(200, message.length());142BufferedWriter writer = new BufferedWriter( new OutputStreamWriter(t.getResponseBody(), "ISO8859-1"));143writer.write(message, 0, message.length());144writer.close();145t.close();146}147}148149/**150* Simple wrapper on default SSLSocketFactory151*/152class SimpleSSLSocketFactory extends SSLSocketFactory153{154/*155* true if this factory has been used to create a new Socket, i.e.156* one of the SocketFactory methods has been called.157*/158boolean socketCreated = false;159160/*161* true if this factory has been used to wrap a Socket, i.e.162* the SSLSocketFactory method,163* createSocket(Socket, String, int, boolean), has been called.164*/165boolean socketWrapped = false;166167// methods for SocketFactory168@Override169public Socket createSocket() throws IOException {170socketCreated = true;171return SocketFactory.getDefault().createSocket();172}173174@Override175public Socket createSocket(InetAddress host, int port) throws IOException {176socketCreated = true;177return SocketFactory.getDefault().createSocket(host, port);178}179180@Override181public Socket createSocket(InetAddress address, int port, InetAddress localAddress,182int localPort) throws IOException {183socketCreated = true;184return SocketFactory.getDefault().createSocket(address, port, localAddress, localPort);185}186187@Override188public Socket createSocket(String host, int port) throws IOException {189socketCreated = true;190return SocketFactory.getDefault().createSocket(host, port);191}192193@Override194public Socket createSocket(String host, int port, InetAddress localHost,195int localPort) throws IOException {196socketCreated = true;197return SocketFactory.getDefault().createSocket(host, port, localHost, localPort);198}199200// methods from SSLSocketFactory201@Override202public Socket createSocket(Socket s, String host, int port,203boolean autoClose) throws IOException {204socketWrapped = true;205return ((SSLSocketFactory) SSLSocketFactory.getDefault()).createSocket206(s, host, port, autoClose);207}208209@Override210public String[] getDefaultCipherSuites() {211return ((SSLSocketFactory) SSLSocketFactory.getDefault()).getDefaultCipherSuites();212}213214@Override215public String[] getSupportedCipherSuites() {216return ((SSLSocketFactory) SSLSocketFactory.getDefault()).getSupportedCipherSuites();217}218}219220class AllHostnameVerifier implements HostnameVerifier221{222@Override223public boolean verify(String hostname, SSLSession session) {224return true;225}226}227}228229230