Path: blob/master/test/jdk/javax/net/ssl/HttpsURLConnection/HttpsSession.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 HttpsSession32*/33import com.sun.net.httpserver.*;34import java.net.*;35import java.io.*;36import javax.net.ssl.*;37import java.util.concurrent.*;38import java.util.Objects;39import jdk.test.lib.net.SimpleSSLContext;4041public class HttpsSession {4243static SSLContext sslContext;4445public static void main(String[] args) throws Exception {46HttpsServer httpsServer = null;47ExecutorService executor = null;48try {49httpsServer = HttpsServer.create(new InetSocketAddress(0), 0);50HttpContext c2 =51httpsServer.createContext("/test", new HttpsHandler());5253executor = Executors.newCachedThreadPool();54httpsServer.setExecutor(executor);5556sslContext = new SimpleSSLContext().get();57httpsServer.setHttpsConfigurator(new HttpsConfigurator(sslContext));58httpsServer.start();5960int httpsPort = httpsServer.getAddress().getPort();61System.out.println(62"Server address: " + httpsServer.getAddress());6364runTest(httpsPort);65} finally {66if (httpsServer != null) {67httpsServer.stop(2);68}69if (executor != null) {70executor.shutdown();71}72}73}7475private static class HttpsHandler implements HttpHandler {76public void handle(HttpExchange httpExchange) throws IOException {77InputStream is = httpExchange.getRequestBody();7879while (is.read() != -1) {80// read to EOF81}82is.close();8384httpExchange.sendResponseHeaders(200, 0);85httpExchange.close();86}87}8889static void runTest(int port) throws Exception {90URL url = new URL(91String.format("https://localhost:%s/test/", port));92HttpsURLConnection urlc =93(HttpsURLConnection)url.openConnection();9495urlc.setSSLSocketFactory(sslContext.getSocketFactory());96urlc.setHostnameVerifier(new HostnameVerifier() {97public boolean verify(String s, SSLSession s1) {98return true;99}100});101102try {103urlc.getSSLSession();104throw new Exception(105"HttpsURLConnection.getSSLSession() should throw " +106"IllegalStateException before the connection established");107} catch (IllegalStateException ise) {108// That's the expected behavior, continue.109}110111try (InputStream is = urlc.getInputStream()) {112while (is.read() != -1) {113// read to EOF114}115116SSLSession session = urlc.getSSLSession().orElseThrow();117if (!Objects.equals(urlc.getCipherSuite(),118session.getCipherSuite())) {119throw new Exception(120"Incorrect SSLSession for HTTPsURLConnection: " +121urlc.getCipherSuite() + "/" + session.getCipherSuite());122}123}124}125}126127128