HttpsCreateSockTest.java revision 14606:bc3775e25b52
1/* 2 * Copyright (c) 2010, 2016, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24/** 25 * @test 26 * @bug 6771432 27 * @summary createSocket() - smpatch fails using 1.6.0_10 because of 28 * "Unconnected sockets not implemented" 29 * @modules jdk.httpserver 30 * @run main/othervm HttpsCreateSockTest 31 * 32 * SunJSSE does not support dynamic system properties, no way to re-use 33 * system properties in samevm/agentvm mode. 34 */ 35 36import javax.net.SocketFactory; 37import javax.net.ssl.HostnameVerifier; 38import javax.net.ssl.HttpsURLConnection; 39import javax.net.ssl.SSLContext; 40import javax.net.ssl.SSLSession; 41import javax.net.ssl.SSLSocketFactory; 42import java.security.NoSuchAlgorithmException; 43import java.net.InetAddress; 44import java.net.InetSocketAddress; 45import java.net.Socket; 46import java.net.URL; 47import java.io.BufferedWriter; 48import java.io.IOException; 49import java.io.OutputStreamWriter; 50import com.sun.net.httpserver.HttpExchange; 51import com.sun.net.httpserver.HttpHandler; 52import com.sun.net.httpserver.HttpsConfigurator; 53 54/* 55 * This class tests that the HTTPS protocol handler is using its socket factory for 56 * creating new Sockets. It does this by wrapping the default SSLSocketFactory with 57 * its own socket factory, SimpleSSLSocketFactory, and verifying that when a https 58 * connection is made one of the socket factories createSocket methods, that 59 * actually creates a Socket, is being invoked by the protocol handler. 60 */ 61 62public class HttpsCreateSockTest 63{ 64 /* 65 * Where do we find the keystores? 66 */ 67 static String pathToStores = "../../../../../../javax/net/ssl/etc"; 68 static String keyStoreFile = "keystore"; 69 static String trustStoreFile = "truststore"; 70 static String passwd = "passphrase"; 71 72 com.sun.net.httpserver.HttpsServer httpsServer; 73 MyHandler httpHandler; 74 75 public static void main(String[] args) { 76 String keyFilename = 77 System.getProperty("test.src", "./") + "/" + pathToStores + 78 "/" + keyStoreFile; 79 String trustFilename = 80 System.getProperty("test.src", "./") + "/" + pathToStores + 81 "/" + trustStoreFile; 82 83 System.setProperty("javax.net.ssl.keyStore", keyFilename); 84 System.setProperty("javax.net.ssl.keyStorePassword", passwd); 85 System.setProperty("javax.net.ssl.trustStore", trustFilename); 86 System.setProperty("javax.net.ssl.trustStorePassword", passwd); 87 88 new HttpsCreateSockTest(); 89 } 90 91 public HttpsCreateSockTest() { 92 try { 93 startHttpsServer(); 94 doClient(); 95 } catch (NoSuchAlgorithmException e) { 96 e.printStackTrace(); 97 } catch (IOException ioe) { 98 ioe.printStackTrace(); 99 } finally { 100 httpsServer.stop(1); 101 } 102 } 103 104 void doClient() throws IOException { 105 InetSocketAddress address = httpsServer.getAddress(); 106 107 URL url = new URL("https://localhost:" + address.getPort() + "/"); 108 System.out.println("trying to connect to " + url + "..."); 109 110 HttpsURLConnection uc = (HttpsURLConnection) url.openConnection(); 111 uc.setHostnameVerifier(new AllHostnameVerifier()); 112 if (uc instanceof javax.net.ssl.HttpsURLConnection) { 113 ((javax.net.ssl.HttpsURLConnection) uc).setSSLSocketFactory(new SimpleSSLSocketFactory()); 114 System.out.println("Using TestSocketFactory"); 115 } 116 uc.connect(); 117 System.out.println("CONNECTED " + uc); 118 System.out.println(uc.getResponseMessage()); 119 uc.disconnect(); 120 } 121 122 /** 123 * Https Server 124 */ 125 public void startHttpsServer() throws IOException, NoSuchAlgorithmException { 126 httpsServer = com.sun.net.httpserver.HttpsServer.create(new InetSocketAddress(0), 0); 127 httpsServer.createContext("/", new MyHandler()); 128 httpsServer.setHttpsConfigurator(new HttpsConfigurator(SSLContext.getDefault())); 129 httpsServer.start(); 130 } 131 132 class MyHandler implements HttpHandler { 133 private String message = "This is a message!"; 134 135 @Override 136 public void handle(HttpExchange t) throws IOException { 137 t.sendResponseHeaders(200, message.length()); 138 BufferedWriter writer = new BufferedWriter( new OutputStreamWriter(t.getResponseBody(), "ISO8859-1")); 139 writer.write(message, 0, message.length()); 140 writer.close(); 141 t.close(); 142 } 143 } 144 145 /** 146 * Simple wrapper on default SSLSocketFactory 147 */ 148 class SimpleSSLSocketFactory extends SSLSocketFactory 149 { 150 /* 151 * true if this factory has been used to create a new Socket, i.e. 152 * one of the SocketFactory methods has been called. 153 */ 154 boolean socketCreated = false; 155 156 /* 157 * true if this factory has been used to wrap a Socket, i.e. 158 * the SSLSocketFactory method, 159 * createSocket(Socket, String, int, boolean), has been called. 160 */ 161 boolean socketWrapped = false; 162 163 @Override 164 public Socket createSocket(InetAddress host, int port) throws IOException { 165 socketCreated = true; 166 return SocketFactory.getDefault().createSocket(host, port); 167 } 168 169 @Override 170 public Socket createSocket(InetAddress address, int port, InetAddress localAddress, 171 int localPort) throws IOException { 172 socketCreated = true; 173 return SocketFactory.getDefault().createSocket(address, port, localAddress, localPort); 174 } 175 176 @Override 177 public Socket createSocket(String host, int port) throws IOException { 178 socketCreated = true; 179 return SocketFactory.getDefault().createSocket(host, port); 180 } 181 182 @Override 183 public Socket createSocket(String host, int port, InetAddress localHost, 184 int localPort) throws IOException { 185 socketCreated = true; 186 return SocketFactory.getDefault().createSocket(host, port, localHost, localPort); 187 } 188 189 // methods from SSLSocketFactory 190 @Override 191 public Socket createSocket(Socket s, String host, int port, 192 boolean autoClose) throws IOException { 193 socketWrapped = true; 194 return ((SSLSocketFactory) SSLSocketFactory.getDefault()).createSocket 195 (s, host, port, autoClose); 196 } 197 198 @Override 199 public String[] getDefaultCipherSuites() { 200 return ((SSLSocketFactory) SSLSocketFactory.getDefault()).getDefaultCipherSuites(); 201 } 202 203 @Override 204 public String[] getSupportedCipherSuites() { 205 return ((SSLSocketFactory) SSLSocketFactory.getDefault()).getSupportedCipherSuites(); 206 } 207 } 208 209 class AllHostnameVerifier implements HostnameVerifier 210 { 211 @Override 212 public boolean verify(String hostname, SSLSession session) { 213 return true; 214 } 215 } 216} 217