Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/net/www/protocol/https/HttpsURLConnection/HttpsCreateSockTest.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 677143226* @summary createSocket() - smpatch fails using 1.6.0_10 because of27* "Unconnected sockets not implemented"28* @run main/othervm HttpsCreateSockTest29*30* SunJSSE does not support dynamic system properties, no way to re-use31* system properties in samevm/agentvm mode.32*/3334import javax.net.SocketFactory;35import javax.net.ssl.HostnameVerifier;36import javax.net.ssl.HttpsURLConnection;37import javax.net.ssl.SSLContext;38import javax.net.ssl.SSLSession;39import javax.net.ssl.SSLSocketFactory;40import java.security.NoSuchAlgorithmException;41import java.net.InetAddress;42import java.net.InetSocketAddress;43import java.net.Socket;44import java.net.URL;45import java.io.BufferedWriter;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 HttpsCreateSockTest61{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 HttpsCreateSockTest();87}8889public HttpsCreateSockTest() {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();104105URL url = new URL("https://localhost:" + address.getPort() + "/");106System.out.println("trying to connect to " + url + "...");107108HttpsURLConnection uc = (HttpsURLConnection) url.openConnection();109uc.setHostnameVerifier(new AllHostnameVerifier());110if (uc instanceof javax.net.ssl.HttpsURLConnection) {111((javax.net.ssl.HttpsURLConnection) uc).setSSLSocketFactory(new SimpleSSLSocketFactory());112System.out.println("Using TestSocketFactory");113}114uc.connect();115System.out.println("CONNECTED " + uc);116System.out.println(uc.getResponseMessage());117uc.disconnect();118}119120/**121* Https Server122*/123public void startHttpsServer() throws IOException, NoSuchAlgorithmException {124httpsServer = com.sun.net.httpserver.HttpsServer.create(new InetSocketAddress(0), 0);125httpsServer.createContext("/", new MyHandler());126httpsServer.setHttpsConfigurator(new HttpsConfigurator(SSLContext.getDefault()));127httpsServer.start();128}129130class MyHandler implements HttpHandler {131private String message = "This is a message!";132133@Override134public void handle(HttpExchange t) throws IOException {135t.sendResponseHeaders(200, message.length());136BufferedWriter writer = new BufferedWriter( new OutputStreamWriter(t.getResponseBody(), "ISO8859-1"));137writer.write(message, 0, message.length());138writer.close();139t.close();140}141}142143/**144* Simple wrapper on default SSLSocketFactory145*/146class SimpleSSLSocketFactory extends SSLSocketFactory147{148/*149* true if this factory has been used to create a new Socket, i.e.150* one of the SocketFactory methods has been called.151*/152boolean socketCreated = false;153154/*155* true if this factory has been used to wrap a Socket, i.e.156* the SSLSocketFactory method,157* createSocket(Socket, String, int, boolean), has been called.158*/159boolean socketWrapped = false;160161@Override162public Socket createSocket(InetAddress host, int port) throws IOException {163socketCreated = true;164return SocketFactory.getDefault().createSocket(host, port);165}166167@Override168public Socket createSocket(InetAddress address, int port, InetAddress localAddress,169int localPort) throws IOException {170socketCreated = true;171return SocketFactory.getDefault().createSocket(address, port, localAddress, localPort);172}173174@Override175public Socket createSocket(String host, int port) throws IOException {176socketCreated = true;177return SocketFactory.getDefault().createSocket(host, port);178}179180@Override181public Socket createSocket(String host, int port, InetAddress localHost,182int localPort) throws IOException {183socketCreated = true;184return SocketFactory.getDefault().createSocket(host, port, localHost, localPort);185}186187// methods from SSLSocketFactory188@Override189public Socket createSocket(Socket s, String host, int port,190boolean autoClose) throws IOException {191socketWrapped = true;192return ((SSLSocketFactory) SSLSocketFactory.getDefault()).createSocket193(s, host, port, autoClose);194}195196@Override197public String[] getDefaultCipherSuites() {198return ((SSLSocketFactory) SSLSocketFactory.getDefault()).getDefaultCipherSuites();199}200201@Override202public String[] getSupportedCipherSuites() {203return ((SSLSocketFactory) SSLSocketFactory.getDefault()).getSupportedCipherSuites();204}205}206207class AllHostnameVerifier implements HostnameVerifier208{209@Override210public boolean verify(String hostname, SSLSession session) {211return true;212}213}214}215216217