Path: blob/master/test/jdk/sun/net/www/protocol/https/HttpsURLConnection/HttpsCreateSockTest.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 677143226* @summary createSocket() - smpatch fails using 1.6.0_10 because of27* "Unconnected sockets not implemented"28* @modules jdk.httpserver29* @library /test/lib30* @run main/othervm HttpsCreateSockTest31* @run main/othervm -Djava.net.preferIPv6Addresses=true HttpsCreateSockTest32*33* SunJSSE does not support dynamic system properties, no way to re-use34* system properties in samevm/agentvm mode.35*/3637import javax.net.SocketFactory;38import javax.net.ssl.HostnameVerifier;39import javax.net.ssl.HttpsURLConnection;40import javax.net.ssl.SSLContext;41import javax.net.ssl.SSLSession;42import javax.net.ssl.SSLSocketFactory;43import java.security.NoSuchAlgorithmException;44import java.net.InetAddress;45import java.net.InetSocketAddress;46import java.net.Proxy;47import java.net.Socket;48import java.net.URL;49import java.io.BufferedWriter;50import java.io.IOException;51import java.io.OutputStreamWriter;52import com.sun.net.httpserver.HttpExchange;53import com.sun.net.httpserver.HttpHandler;54import com.sun.net.httpserver.HttpsConfigurator;5556import jdk.test.lib.net.URIBuilder;5758/*59* This class tests that the HTTPS protocol handler is using its socket factory for60* creating new Sockets. It does this by wrapping the default SSLSocketFactory with61* its own socket factory, SimpleSSLSocketFactory, and verifying that when a https62* connection is made one of the socket factories createSocket methods, that63* actually creates a Socket, is being invoked by the protocol handler.64*/6566public class HttpsCreateSockTest67{68/*69* Where do we find the keystores?70*/71static String pathToStores = "../../../../../../javax/net/ssl/etc";72static String keyStoreFile = "keystore";73static String trustStoreFile = "truststore";74static String passwd = "passphrase";7576com.sun.net.httpserver.HttpsServer httpsServer;77MyHandler httpHandler;7879public static void main(String[] args) {80String keyFilename =81System.getProperty("test.src", "./") + "/" + pathToStores +82"/" + keyStoreFile;83String trustFilename =84System.getProperty("test.src", "./") + "/" + pathToStores +85"/" + trustStoreFile;8687System.setProperty("javax.net.ssl.keyStore", keyFilename);88System.setProperty("javax.net.ssl.keyStorePassword", passwd);89System.setProperty("javax.net.ssl.trustStore", trustFilename);90System.setProperty("javax.net.ssl.trustStorePassword", passwd);9192new HttpsCreateSockTest();93}9495public HttpsCreateSockTest() {96try {97startHttpsServer();98doClient();99} catch (NoSuchAlgorithmException e) {100e.printStackTrace();101} catch (IOException ioe) {102ioe.printStackTrace();103} finally {104httpsServer.stop(1);105}106}107108void doClient() throws IOException {109InetSocketAddress address = httpsServer.getAddress();110111URL url = URIBuilder.newBuilder()112.scheme("https")113.host(address.getAddress())114.port(address.getPort())115.path("/")116.toURLUnchecked();117System.out.println("trying to connect to " + url + "...");118119HttpsURLConnection uc = (HttpsURLConnection) url.openConnection(Proxy.NO_PROXY);120uc.setHostnameVerifier(new AllHostnameVerifier());121if (uc instanceof javax.net.ssl.HttpsURLConnection) {122((javax.net.ssl.HttpsURLConnection) uc).setSSLSocketFactory(new SimpleSSLSocketFactory());123System.out.println("Using TestSocketFactory");124}125uc.connect();126System.out.println("CONNECTED " + uc);127System.out.println(uc.getResponseMessage());128uc.disconnect();129}130131/**132* Https Server133*/134public void startHttpsServer() throws IOException, NoSuchAlgorithmException {135InetAddress loopback = InetAddress.getLoopbackAddress();136InetSocketAddress address = new InetSocketAddress(loopback, 0);137httpsServer = com.sun.net.httpserver.HttpsServer.create(address, 0);138httpsServer.createContext("/", new MyHandler());139httpsServer.setHttpsConfigurator(new HttpsConfigurator(SSLContext.getDefault()));140httpsServer.start();141}142143class MyHandler implements HttpHandler {144private String message = "This is a message!";145146@Override147public void handle(HttpExchange t) throws IOException {148t.sendResponseHeaders(200, message.length());149BufferedWriter writer = new BufferedWriter( new OutputStreamWriter(t.getResponseBody(), "ISO8859-1"));150writer.write(message, 0, message.length());151writer.close();152t.close();153}154}155156/**157* Simple wrapper on default SSLSocketFactory158*/159class SimpleSSLSocketFactory extends SSLSocketFactory160{161/*162* true if this factory has been used to create a new Socket, i.e.163* one of the SocketFactory methods has been called.164*/165boolean socketCreated = false;166167/*168* true if this factory has been used to wrap a Socket, i.e.169* the SSLSocketFactory method,170* createSocket(Socket, String, int, boolean), has been called.171*/172boolean socketWrapped = false;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