Path: blob/master/test/jdk/sun/net/www/protocol/https/HttpsURLConnection/HttpsSocketFacTest.java
41161 views
/*1* Copyright (c) 2010, 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 661495726* @summary HttpsURLConnection not using the set SSLSocketFactory for creating all its Sockets27* @modules jdk.httpserver28* @library /test/lib29* @run main/othervm HttpsSocketFacTest30*31* SunJSSE does not support dynamic system properties, no way to re-use32* system properties in samevm/agentvm mode.33*/3435import javax.net.SocketFactory;36import javax.net.ssl.HostnameVerifier;37import javax.net.ssl.HttpsURLConnection;38import javax.net.ssl.SSLContext;39import javax.net.ssl.SSLSession;40import javax.net.ssl.SSLSocketFactory;41import java.net.InetAddress;42import java.net.InetSocketAddress;43import java.net.Socket;44import java.net.URL;45import java.net.Proxy;46import java.security.NoSuchAlgorithmException;47import java.io.BufferedWriter;48import java.io.InputStream;49import java.io.IOException;50import java.io.OutputStreamWriter;51import com.sun.net.httpserver.HttpExchange;52import com.sun.net.httpserver.HttpHandler;53import com.sun.net.httpserver.HttpsConfigurator;54import jdk.test.lib.net.URIBuilder;5556/*57* This class tests that the HTTPS protocol handler is using its socket factory for58* creating new Sockets. It does this by wrapping the default SSLSocketFactory with59* its own socket factory, SimpleSSLSocketFactory, and verifying that when a https60* connection is made one of the socket factories createSocket methods, that61* actually creates a Socket, is being invoked by the protocol handler.62*/6364public class HttpsSocketFacTest65{66/*67* Where do we find the keystores?68*/69static String pathToStores = "../../../../../../javax/net/ssl/etc";70static String keyStoreFile = "keystore";71static String trustStoreFile = "truststore";72static String passwd = "passphrase";7374com.sun.net.httpserver.HttpsServer httpsServer;75MyHandler httpHandler;7677public static void main(String[] args) {78String keyFilename =79System.getProperty("test.src", "./") + "/" + pathToStores +80"/" + keyStoreFile;81String trustFilename =82System.getProperty("test.src", "./") + "/" + pathToStores +83"/" + trustStoreFile;8485System.setProperty("javax.net.ssl.keyStore", keyFilename);86System.setProperty("javax.net.ssl.keyStorePassword", passwd);87System.setProperty("javax.net.ssl.trustStore", trustFilename);88System.setProperty("javax.net.ssl.trustStorePassword", passwd);8990new HttpsSocketFacTest();91}9293public HttpsSocketFacTest() {94try {95startHttpsServer();96doClient();97} catch (NoSuchAlgorithmException e) {98e.printStackTrace();99} catch (IOException ioe) {100ioe.printStackTrace();101} finally {102httpsServer.stop(1);103}104}105106void doClient() throws IOException {107InetSocketAddress address = httpsServer.getAddress();108URL url = URIBuilder.newBuilder()109.scheme("https")110.loopback()111.port(address.getPort())112.path("/test6614957/")113.toURLUnchecked();114System.out.println("trying to connect to " + url + "...");115116HttpsURLConnection uc = (HttpsURLConnection) url.openConnection(Proxy.NO_PROXY);117SimpleSSLSocketFactory sssf = new SimpleSSLSocketFactory();118uc.setSSLSocketFactory(sssf);119uc.setHostnameVerifier(new AllHostnameVerifier());120InputStream is = uc.getInputStream();121122byte[] ba = new byte[1024];123int read = 0;124while ((read = is.read(ba)) != -1) {125System.out.println(new String(ba, 0, read));126}127128System.out.println("SimpleSSLSocketFactory.socketCreated = " + sssf.socketCreated);129System.out.println("SimpleSSLSocketFactory.socketWrapped = " + sssf.socketWrapped);130131if (!sssf.socketCreated)132throw new RuntimeException("Failed: Socket Factory not being called to create Socket");133}134135/**136* Https Server137*/138public void startHttpsServer() throws IOException, NoSuchAlgorithmException {139httpsServer = com.sun.net.httpserver.HttpsServer140.create(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0), 0);141httpsServer.createContext("/test6614957/", new MyHandler());142httpsServer.setHttpsConfigurator(new HttpsConfigurator(SSLContext.getDefault()));143httpsServer.start();144}145146class MyHandler implements HttpHandler {147private String message = "This is a message!";148149@Override150public void handle(HttpExchange t) throws IOException {151t.sendResponseHeaders(200, message.length());152BufferedWriter writer = new BufferedWriter( new OutputStreamWriter(t.getResponseBody(), "ISO8859-1"));153writer.write(message, 0, message.length());154writer.close();155t.close();156}157}158159/**160* Simple wrapper on default SSLSocketFactory161*/162class SimpleSSLSocketFactory extends SSLSocketFactory163{164/*165* true if this factory has been used to create a new Socket, i.e.166* one of the SocketFactory methods has been called.167*/168boolean socketCreated = false;169170/*171* true if this factory has been used to wrap a Socket, i.e.172* the SSLSocketFactory method,173* createSocket(Socket, String, int, boolean), has been called.174*/175boolean socketWrapped = false;176177// methods for SocketFactory178@Override179public Socket createSocket() throws IOException {180socketCreated = true;181return SocketFactory.getDefault().createSocket();182}183184@Override185public Socket createSocket(InetAddress host, int port) throws IOException {186socketCreated = true;187return SocketFactory.getDefault().createSocket(host, port);188}189190@Override191public Socket createSocket(InetAddress address, int port, InetAddress localAddress,192int localPort) throws IOException {193socketCreated = true;194return SocketFactory.getDefault().createSocket(address, port, localAddress, localPort);195}196197@Override198public Socket createSocket(String host, int port) throws IOException {199socketCreated = true;200return SocketFactory.getDefault().createSocket(host, port);201}202203@Override204public Socket createSocket(String host, int port, InetAddress localHost,205int localPort) throws IOException {206socketCreated = true;207return SocketFactory.getDefault().createSocket(host, port, localHost, localPort);208}209210// methods from SSLSocketFactory211@Override212public Socket createSocket(Socket s, String host, int port,213boolean autoClose) throws IOException {214socketWrapped = true;215return ((SSLSocketFactory) SSLSocketFactory.getDefault()).createSocket216(s, host, port, autoClose);217}218219@Override220public String[] getDefaultCipherSuites() {221return ((SSLSocketFactory) SSLSocketFactory.getDefault()).getDefaultCipherSuites();222}223224@Override225public String[] getSupportedCipherSuites() {226return ((SSLSocketFactory) SSLSocketFactory.getDefault()).getSupportedCipherSuites();227}228}229230class AllHostnameVerifier implements HostnameVerifier231{232@Override233public boolean verify(String hostname, SSLSession session) {234return true;235}236}237}238239240