Path: blob/master/test/jdk/javax/net/ssl/HttpsURLConnection/DummyCacheResponse.java
41152 views
/*1* Copyright (c) 2018, 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 821226126* @summary Add SSLSession accessors to HttpsURLConnection and27* SecureCacheResponse28* @library /test/lib29* @modules jdk.httpserver30* @build jdk.test.lib.net.SimpleSSLContext31* @run main/othervm DummyCacheResponse32*/3334import java.io.*;35import java.net.*;36import javax.net.ssl.*;37import java.util.*;38import java.util.concurrent.*;39import java.security.Principal;40import java.security.cert.Certificate;41import jdk.test.lib.net.SimpleSSLContext;42import com.sun.net.httpserver.*;4344public class DummyCacheResponse extends SecureCacheResponse {45static SSLContext sslContext;46private final SSLSession cachedSession;47private final Map<String, List<String>> rqstHeaders;4849public static void main(String[] args) throws Exception {50ResponseCache reservedResponseCache = ResponseCache.getDefault();51HttpsServer httpsServer = null;52ExecutorService executor = null;53try {54ResponseCache.setDefault(new DummyResponseCache());5556httpsServer = HttpsServer.create(new InetSocketAddress(0), 0);57HttpContext c2 =58httpsServer.createContext("/test", new HttpsHandler());5960executor = Executors.newCachedThreadPool();61httpsServer.setExecutor(executor);6263sslContext = new SimpleSSLContext().get();64httpsServer.setHttpsConfigurator(new HttpsConfigurator(sslContext));65httpsServer.start();6667int httpsPort = httpsServer.getAddress().getPort();68System.out.println(69"Server address: " + httpsServer.getAddress());7071// the 1st connection72runTest(httpsPort, false);7374// the 2nd connection that use the cache75runTest(httpsPort, true);76} finally {77if (httpsServer != null) {78httpsServer.stop(2);79}80if (executor != null) {81executor.shutdown();82}8384ResponseCache.setDefault(reservedResponseCache);85}86}8788private static class HttpsHandler implements HttpHandler {89public void handle(HttpExchange httpExchange) throws IOException {90InputStream is = httpExchange.getRequestBody();9192while (is.read() != -1) {93// read to EOF94}95is.close();9697httpExchange.sendResponseHeaders(200, 0);98httpExchange.close();99}100}101102static void runTest(int port, boolean useCache) throws Exception {103URL url = new URL(104String.format("https://localhost:%s/test/", port));105HttpsURLConnection urlc =106(HttpsURLConnection)url.openConnection();107108urlc.setSSLSocketFactory(sslContext.getSocketFactory());109urlc.setHostnameVerifier(new HostnameVerifier() {110public boolean verify(String s, SSLSession s1) {111return true;112}113});114115try (InputStream is = urlc.getInputStream()) {116while (is.read() != -1) {117// read to EOF118}119120SSLSession session = urlc.getSSLSession().orElseThrow();121if (!Objects.equals(urlc.getCipherSuite(),122session.getCipherSuite())) {123throw new Exception(124"Incorrect SSLSession for HTTPsURLConnection: " +125urlc.getCipherSuite() + "/" + session.getCipherSuite());126}127128// Make sure the cache implementation is used.129try {130urlc.getServerCertificates();131if (useCache) {132throw new Exception(133"The SecureCacheResponse impl should be used");134}135} catch (UnsupportedOperationException uoe) {136if (!useCache) {137throw new Exception(138"The SecureCacheResponse impl should not be used");139}140}141}142}143144DummyCacheResponse(SSLSession sslSession,145Map<String, List<String>> rqstHeaders) {146this.rqstHeaders = rqstHeaders;147this.cachedSession = sslSession;148}149150@Override151public String getCipherSuite() {152return cachedSession.getCipherSuite();153}154155@Override156public List<Certificate> getLocalCertificateChain() {157throw new UnsupportedOperationException("Not supported yet.");158}159160@Override161public List<Certificate> getServerCertificateChain()162throws SSLPeerUnverifiedException {163throw new UnsupportedOperationException("Not supported yet.");164}165166@Override167public Principal getPeerPrincipal() throws SSLPeerUnverifiedException {168throw new UnsupportedOperationException("Not supported yet.");169}170171@Override172public Principal getLocalPrincipal() {173throw new UnsupportedOperationException("Not supported yet.");174}175176@Override177public Map<String, List<String>> getHeaders() throws IOException {178return rqstHeaders;179}180181@Override182public InputStream getBody() throws IOException {183return new ByteArrayInputStream(new byte[0]);184}185186@Override187public Optional<SSLSession> getSSLSession() {188return Optional.of(cachedSession);189}190191private static class DummyResponseCache extends ResponseCache {192Map<URI, SSLSession> httpsConnections = new HashMap<>();193194@Override195public CacheResponse get(URI uri, String rqstMethod,196Map<String, List<String>> rqstHeaders) throws IOException {197if (httpsConnections.containsKey(uri)) {198return new DummyCacheResponse(199httpsConnections.get(uri), rqstHeaders);200}201202return null;203}204205@Override206public CacheRequest put(URI uri,207URLConnection conn) throws IOException {208if (conn instanceof HttpsURLConnection) {209HttpsURLConnection httpsConn = (HttpsURLConnection)conn;210httpsConnections.putIfAbsent(211uri, httpsConn.getSSLSession().orElseThrow());212}213214return null;215}216}217}218219220