1 /* 2 * Copyright (c) 2018, 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 8212261 27 * @library /test/lib 28 * @modules jdk.httpserver 29 * @build jdk.test.lib.net.SimpleSSLContext 30 * @run main/othervm HttpsSession 31 */ 32 import com.sun.net.httpserver.*; 33 import java.net.*; 34 import java.io.*; 35 import javax.net.ssl.*; 36 import java.util.concurrent.*; 37 import java.util.Objects; 38 import jdk.test.lib.net.SimpleSSLContext; 39 40 public class HttpsSession { 41 42 static SSLContext sslContext; 43 44 public static void main(String[] args) throws Exception { 45 HttpsServer httpsServer = null; 46 ExecutorService executor = null; 47 try { 48 httpsServer = HttpsServer.create(new InetSocketAddress(0), 0); 49 HttpContext c2 = 50 httpsServer.createContext("/test", new HttpsHandler()); 51 52 executor = Executors.newCachedThreadPool(); 53 httpsServer.setExecutor(executor); 54 55 sslContext = new SimpleSSLContext().get(); 56 httpsServer.setHttpsConfigurator(new HttpsConfigurator(sslContext)); 57 httpsServer.start(); 58 59 int httpsPort = httpsServer.getAddress().getPort(); 60 System.out.println( 61 "Server address: " + httpsServer.getAddress()); 62 63 runTest(httpsPort); 64 } finally { 65 if (httpsServer != null) { 66 httpsServer.stop(2); 67 } 68 if (executor != null) { 69 executor.shutdown(); 70 } 71 } 72 } 73 74 private static class HttpsHandler implements HttpHandler { 75 int invocation = 1; 76 77 public void handle(HttpExchange httpExchange) throws IOException { 78 InputStream is = httpExchange.getRequestBody(); 79 80 while (is.read() != -1) { 81 // read to EOF 82 } 83 is.close(); 84 85 httpExchange.sendResponseHeaders(200, 0); 86 httpExchange.close(); 87 } 88 } 89 90 static void runTest(int port) throws Exception { 91 URL url = new URL( 92 String.format("https://localhost:%s/test/", port)); 93 HttpsURLConnection urlc = 94 (HttpsURLConnection)url.openConnection(); 95 96 urlc.setSSLSocketFactory(sslContext.getSocketFactory()); 97 urlc.setHostnameVerifier(new HostnameVerifier() { 98 public boolean verify(String s, SSLSession s1) { 99 return true; 100 } 101 }); 102 103 try (InputStream is = urlc.getInputStream()) { 104 while (is.read() != -1) { 105 // read to EOF 106 } 107 108 SSLSession session = urlc.getSSLSession(); 109 if (!Objects.equals(urlc.getCipherSuite(), session.getCipherSuite())) { 110 throw new Exception( 111 "Incorrect SSLSession for HTTPsURLConnection: " + 112 urlc.getCipherSuite() + "/" + session.getCipherSuite()); 113 } 114 } 115 } 116 }