HttpsSocketFacTest.java revision 14606:bc3775e25b52
15116SN/A/* 211551Sserb * Copyright (c) 2010, 2016, Oracle and/or its affiliates. All rights reserved. 35116SN/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 45116SN/A * 55116SN/A * This code is free software; you can redistribute it and/or modify it 65116SN/A * under the terms of the GNU General Public License version 2 only, as 75116SN/A * published by the Free Software Foundation. 85116SN/A * 95116SN/A * This code is distributed in the hope that it will be useful, but WITHOUT 105116SN/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 115116SN/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 125116SN/A * version 2 for more details (a copy is included in the LICENSE file that 135116SN/A * accompanied this code). 145116SN/A * 155116SN/A * You should have received a copy of the GNU General Public License version 165116SN/A * 2 along with this work; if not, write to the Free Software Foundation, 175116SN/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 185116SN/A * 195116SN/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 205116SN/A * or visit www.oracle.com if you need additional information or have any 215116SN/A * questions. 225116SN/A */ 235116SN/A 245116SN/A/* 255116SN/A * @test 265116SN/A * @bug 6614957 275116SN/A * @summary HttpsURLConnection not using the set SSLSocketFactory for creating all its Sockets 285116SN/A * @modules jdk.httpserver 295116SN/A * @run main/othervm HttpsSocketFacTest 305116SN/A * 315116SN/A * SunJSSE does not support dynamic system properties, no way to re-use 325116SN/A * system properties in samevm/agentvm mode. 335116SN/A */ 345116SN/A 355116SN/Aimport javax.net.SocketFactory; 365116SN/Aimport javax.net.ssl.HostnameVerifier; 375116SN/Aimport javax.net.ssl.HttpsURLConnection; 385116SN/Aimport javax.net.ssl.SSLContext; 395116SN/Aimport javax.net.ssl.SSLSession; 405116SN/Aimport javax.net.ssl.SSLSocketFactory; 415116SN/Aimport java.security.NoSuchAlgorithmException; 425116SN/Aimport java.net.InetAddress; 435116SN/Aimport java.net.InetSocketAddress; 445116SN/Aimport java.net.Socket; 455116SN/Aimport java.net.URL; 465116SN/Aimport java.io.BufferedWriter; 475116SN/Aimport java.io.InputStream; 485116SN/Aimport java.io.IOException; 495116SN/Aimport java.io.OutputStreamWriter; 505116SN/Aimport com.sun.net.httpserver.HttpExchange; 515116SN/Aimport com.sun.net.httpserver.HttpHandler; 525116SN/Aimport com.sun.net.httpserver.HttpsConfigurator; 535116SN/A 545116SN/A/* 555116SN/A * This class tests that the HTTPS protocol handler is using its socket factory for 565116SN/A * creating new Sockets. It does this by wrapping the default SSLSocketFactory with 575116SN/A * its own socket factory, SimpleSSLSocketFactory, and verifying that when a https 585116SN/A * connection is made one of the socket factories createSocket methods, that 595116SN/A * actually creates a Socket, is being invoked by the protocol handler. 605116SN/A */ 615116SN/A 625116SN/Apublic class HttpsSocketFacTest 635116SN/A{ 645116SN/A /* 655116SN/A * Where do we find the keystores? 665116SN/A */ 675116SN/A static String pathToStores = "../../../../../../javax/net/ssl/etc"; 685116SN/A static String keyStoreFile = "keystore"; 695116SN/A static String trustStoreFile = "truststore"; 705116SN/A static String passwd = "passphrase"; 715116SN/A 725116SN/A com.sun.net.httpserver.HttpsServer httpsServer; 735116SN/A MyHandler httpHandler; 745116SN/A 755116SN/A public static void main(String[] args) { 765116SN/A String keyFilename = 775116SN/A System.getProperty("test.src", "./") + "/" + pathToStores + 785116SN/A "/" + keyStoreFile; 795116SN/A String trustFilename = 805116SN/A System.getProperty("test.src", "./") + "/" + pathToStores + 815116SN/A "/" + trustStoreFile; 825116SN/A 835116SN/A System.setProperty("javax.net.ssl.keyStore", keyFilename); 845116SN/A System.setProperty("javax.net.ssl.keyStorePassword", passwd); 855116SN/A System.setProperty("javax.net.ssl.trustStore", trustFilename); 865116SN/A System.setProperty("javax.net.ssl.trustStorePassword", passwd); 875116SN/A 885116SN/A new HttpsSocketFacTest(); 895116SN/A } 905116SN/A 915116SN/A public HttpsSocketFacTest() { 925116SN/A try { 935116SN/A startHttpsServer(); 945116SN/A doClient(); 955116SN/A } catch (NoSuchAlgorithmException e) { 965116SN/A e.printStackTrace(); 975116SN/A } catch (IOException ioe) { 985116SN/A ioe.printStackTrace(); 995116SN/A } finally { 1005116SN/A httpsServer.stop(1); 1015116SN/A } 1025116SN/A } 1035116SN/A 1045116SN/A void doClient() throws IOException { 1055116SN/A InetSocketAddress address = httpsServer.getAddress(); 1065116SN/A URL url = new URL("https://localhost:" + address.getPort() + "/test6614957/"); 1075116SN/A System.out.println("trying to connect to " + url + "..."); 1085116SN/A 1095116SN/A HttpsURLConnection uc = (HttpsURLConnection) url.openConnection(); 1105116SN/A SimpleSSLSocketFactory sssf = new SimpleSSLSocketFactory(); 1115116SN/A uc.setSSLSocketFactory(sssf); 1125116SN/A uc.setHostnameVerifier(new AllHostnameVerifier()); 1135116SN/A InputStream is = uc.getInputStream(); 1145116SN/A 1155116SN/A byte[] ba = new byte[1024]; 1165116SN/A int read = 0; 1175116SN/A while ((read = is.read(ba)) != -1) { 1185116SN/A System.out.println(new String(ba, 0, read)); 1195116SN/A } 1205116SN/A 1215116SN/A System.out.println("SimpleSSLSocketFactory.socketCreated = " + sssf.socketCreated); 1225116SN/A System.out.println("SimpleSSLSocketFactory.socketWrapped = " + sssf.socketWrapped); 1235116SN/A 1245116SN/A if (!sssf.socketCreated) 1255116SN/A throw new RuntimeException("Failed: Socket Factory not being called to create Socket"); 1265116SN/A } 1275116SN/A 1285116SN/A /** 1295116SN/A * Https Server 1305116SN/A */ 1315116SN/A public void startHttpsServer() throws IOException, NoSuchAlgorithmException { 1325116SN/A httpsServer = com.sun.net.httpserver.HttpsServer.create(new InetSocketAddress(0), 0); 1335116SN/A httpsServer.createContext("/test6614957/", new MyHandler()); 1345116SN/A httpsServer.setHttpsConfigurator(new HttpsConfigurator(SSLContext.getDefault())); 1355116SN/A httpsServer.start(); 1365116SN/A } 1375116SN/A 1385116SN/A class MyHandler implements HttpHandler { 1395116SN/A private String message = "This is a message!"; 1405116SN/A 1415116SN/A @Override 1425116SN/A public void handle(HttpExchange t) throws IOException { 1435116SN/A t.sendResponseHeaders(200, message.length()); 1445116SN/A BufferedWriter writer = new BufferedWriter( new OutputStreamWriter(t.getResponseBody(), "ISO8859-1")); 1455116SN/A writer.write(message, 0, message.length()); 1465116SN/A writer.close(); 1475116SN/A t.close(); 1485116SN/A } 1495116SN/A } 1505116SN/A 1515116SN/A /** 1525116SN/A * Simple wrapper on default SSLSocketFactory 1535116SN/A */ 1545116SN/A class SimpleSSLSocketFactory extends SSLSocketFactory 1555116SN/A { 1565116SN/A /* 1575116SN/A * true if this factory has been used to create a new Socket, i.e. 1585116SN/A * one of the SocketFactory methods has been called. 1595116SN/A */ 1605116SN/A boolean socketCreated = false; 1615116SN/A 1625116SN/A /* 1635116SN/A * true if this factory has been used to wrap a Socket, i.e. 1645116SN/A * the SSLSocketFactory method, 1655116SN/A * createSocket(Socket, String, int, boolean), has been called. 1665116SN/A */ 1675116SN/A boolean socketWrapped = false; 1685116SN/A 1695116SN/A // methods for SocketFactory 1705116SN/A @Override 1715116SN/A public Socket createSocket() throws IOException { 1725116SN/A socketCreated = true; 1735116SN/A return SocketFactory.getDefault().createSocket(); 1745116SN/A } 1755116SN/A 1765116SN/A @Override 1775116SN/A public Socket createSocket(InetAddress host, int port) throws IOException { 1785116SN/A socketCreated = true; 1795116SN/A return SocketFactory.getDefault().createSocket(host, port); 1805116SN/A } 1815116SN/A 1825116SN/A @Override 1835116SN/A public Socket createSocket(InetAddress address, int port, InetAddress localAddress, 1845116SN/A int localPort) throws IOException { 1855116SN/A socketCreated = true; 1865116SN/A return SocketFactory.getDefault().createSocket(address, port, localAddress, localPort); 1875116SN/A } 1885116SN/A 1895116SN/A @Override 1905116SN/A public Socket createSocket(String host, int port) throws IOException { 1915116SN/A socketCreated = true; 1925116SN/A return SocketFactory.getDefault().createSocket(host, port); 1935116SN/A } 1945116SN/A 1955116SN/A @Override 1965116SN/A public Socket createSocket(String host, int port, InetAddress localHost, 1975116SN/A int localPort) throws IOException { 1985116SN/A socketCreated = true; 1995116SN/A return SocketFactory.getDefault().createSocket(host, port, localHost, localPort); 2005116SN/A } 2015116SN/A 2025116SN/A // methods from SSLSocketFactory 2035116SN/A @Override 2045116SN/A public Socket createSocket(Socket s, String host, int port, 2055116SN/A boolean autoClose) throws IOException { 2065116SN/A socketWrapped = true; 2075116SN/A return ((SSLSocketFactory) SSLSocketFactory.getDefault()).createSocket 2085116SN/A (s, host, port, autoClose); 2095116SN/A } 2105116SN/A 2115116SN/A @Override 2125116SN/A public String[] getDefaultCipherSuites() { 2135116SN/A return ((SSLSocketFactory) SSLSocketFactory.getDefault()).getDefaultCipherSuites(); 2145116SN/A } 2155116SN/A 2165116SN/A @Override 2175116SN/A public String[] getSupportedCipherSuites() { 2185116SN/A return ((SSLSocketFactory) SSLSocketFactory.getDefault()).getSupportedCipherSuites(); 2195116SN/A } 2205116SN/A } 2215116SN/A 2225116SN/A class AllHostnameVerifier implements HostnameVerifier 2235116SN/A { 2245116SN/A @Override 2255116SN/A public boolean verify(String hostname, SSLSession session) { 2265116SN/A return true; 2275116SN/A } 2285116SN/A } 2295116SN/A} 2305116SN/A