Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/net/ssl/SSLSession/SSLCtxAccessToSessCtx.java
38853 views
/*1* Copyright (c) 2001, 2016, 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 447321026* @summary SSLSessionContext should be accessible from SSLContext27* @run main/othervm SSLCtxAccessToSessCtx28*29* SunJSSE does not support dynamic system properties, no way to re-use30* system properties in samevm/agentvm mode.31*/3233import java.io.*;34import java.net.*;35import javax.net.ssl.*;36import java.util.*;37import java.util.concurrent.atomic.AtomicInteger;38import java.security.KeyStore;3940public class SSLCtxAccessToSessCtx {4142/*43* =============================================================44* Set the various variables needed for the tests, then45* specify what tests to run on each side.46*/4748/*49* Should we run the client or server in a separate thread?50* Both sides can throw exceptions, but do you have a preference51* as to which side should be the main thread.52*/53static boolean separateServerThread = true;5455/*56* Where do we find the keystores?57*/58static String pathToStores = "../etc";59static String keyStoreFile = "keystore";60static String trustStoreFile = "truststore";61static String passwd = "passphrase";6263/*64* Is the server ready to serve?65*/66AtomicInteger serverReady = new AtomicInteger(1); // only one port now6768/*69* Turn on SSL debugging?70*/71static boolean debug = false;7273/*74* If the client or server is doing some kind of object creation75* that the other side depends on, and that thread prematurely76* exits, you may experience a hang. The test harness will77* terminate all hung threads after its timeout has expired,78* currently 3 minutes by default, but you might try to be79* smart about it....80*/8182/*83* Define the server side of the test.84*85* If the server prematurely exits, serverReady will be set to true86* to avoid infinite hangs.87*/88void doServerSide(int serverPort) throws Exception {8990SSLServerSocket sslServerSocket =91(SSLServerSocket) sslssf.createServerSocket(serverPort);92int slot = createdPorts.getAndIncrement();93serverPorts[slot] = sslServerSocket.getLocalPort();9495/*96* Signal Client, we're ready for his connect.97*/98serverReady.getAndDecrement();99int read = 0;100SSLSocket sslSocket = (SSLSocket) sslServerSocket.accept();101InputStream sslIS = sslSocket.getInputStream();102OutputStream sslOS = sslSocket.getOutputStream();103read = sslIS.read();104SSLSessionContext sslctxCache = sslctx.getServerSessionContext();105SSLSessionContext sessCache = sslSocket.getSession().106getSessionContext();107if (sessCache != sslctxCache)108throw new Exception("Test failed, session_cache != sslctx_cache");109sslOS.write(85);110sslOS.flush();111sslSocket.close();112}113114/*115* Define the client side of the test.116*117* If the server prematurely exits, serverReady will be set to true118* to avoid infinite hangs.119*/120void doClientSide() throws Exception {121122/*123* Wait for server to get started.124*/125while (serverReady.get() > 0) {126Thread.sleep(50);127}128/*129* first connection to serverPorts[0] -- a new session, session11130* gets created, and is cached.131*/132SSLSocket sslSocket;133sslSocket = (SSLSocket) sslsf.134createSocket("localhost", serverPorts[0]);135InputStream sslIS = sslSocket.getInputStream();136OutputStream sslOS = sslSocket.getOutputStream();137sslOS.write(237);138sslOS.flush();139140SSLSession sess = sslSocket.getSession();141SSLSessionContext sessCache = sess.getSessionContext();142SSLSessionContext sslctxCache = sslctx.getClientSessionContext();143if (sessCache != sslctxCache)144throw new Exception("Test failed, session_cache != sslctx_cache");145146int read = sslIS.read();147sslSocket.close();148}149150/*151* =============================================================152* The remainder is just support stuff153*/154155int serverPorts[] = new int[]{0}; // only one port at present156AtomicInteger createdPorts = new AtomicInteger(0);157static SSLServerSocketFactory sslssf;158static SSLSocketFactory sslsf;159static SSLContext sslctx;160161volatile Exception serverException = null;162volatile Exception clientException = null;163164public static void main(String[] args) throws Exception {165String keyFilename =166System.getProperty("test.src", "./") + "/" + pathToStores +167"/" + keyStoreFile;168String trustFilename =169System.getProperty("test.src", "./") + "/" + pathToStores +170"/" + trustStoreFile;171172System.setProperty("javax.net.ssl.keyStore", keyFilename);173System.setProperty("javax.net.ssl.keyStorePassword", passwd);174System.setProperty("javax.net.ssl.trustStore", trustFilename);175System.setProperty("javax.net.ssl.trustStorePassword", passwd);176177sslctx = SSLContext.getInstance("TLS");178KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");179KeyStore ks = KeyStore.getInstance("JKS");180ks.load(new FileInputStream(keyFilename), passwd.toCharArray());181kmf.init(ks, passwd.toCharArray());182sslctx.init(kmf.getKeyManagers(), null, null);183184sslssf = (SSLServerSocketFactory) sslctx.getServerSocketFactory();185sslsf = (SSLSocketFactory) sslctx.getSocketFactory();186187if (debug)188System.setProperty("javax.net.debug", "all");189190/*191* Start the tests.192*/193new SSLCtxAccessToSessCtx();194}195196Thread clientThread = null;197Thread serverThread = null;198199/*200* Primary constructor, used to drive remainder of the test.201*202* Fork off the other side, then do your work.203*/204SSLCtxAccessToSessCtx() throws Exception {205206/*207* create the SSLServerSocket and SSLSocket factories208*/209if (separateServerThread) {210for (int i = 0; i < serverPorts.length; i++) {211startServer(serverPorts[i], true);212}213startClient(false);214} else {215startClient(true);216for (int i = 0; i < serverPorts.length; i++) {217startServer(serverPorts[i], false);218}219}220221/*222* Wait for other side to close down.223*/224if (separateServerThread) {225serverThread.join();226} else {227clientThread.join();228}229230/*231* When we get here, the test is pretty much over.232*233* If the main thread excepted, that propagates back234* immediately. If the other thread threw an exception, we235* should report back.236*/237if (serverException != null)238throw serverException;239if (clientException != null)240throw clientException;241System.out.println("The Session context tests passed");242}243244void startServer(final int port,245boolean newThread) throws Exception {246if (newThread) {247serverThread = new Thread() {248public void run() {249try {250doServerSide(port);251} catch (Exception e) {252/*253* Our server thread just died.254*255* Release the client, if not active already...256*/257System.err.println("Server died...");258e.printStackTrace();259serverReady.set(0);260serverException = e;261}262}263};264serverThread.start();265} else {266try {267doServerSide(port);268} catch (Exception e) {269serverException = e;270} finally {271serverReady.set(0);272}273}274}275276void startClient(boolean newThread)277throws Exception {278if (newThread) {279clientThread = new Thread() {280public void run() {281try {282doClientSide();283} catch (Exception e) {284/*285* Our client thread just died.286*/287System.err.println("Client died...");288clientException = e;289}290}291};292clientThread.start();293} else {294try {295doClientSide();296} catch (Exception e) {297clientException = e;298}299}300}301}302303304