Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/ssl/SSLContextImpl/BadKSProvider.java
38853 views
/*1* Copyright (c) 2003, 2011, 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 491914726* @summary Support for token-based KeyStores27* @run main/othervm BadKSProvider28*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.*;3637public class BadKSProvider {3839/*40* =============================================================41* Set the various variables needed for the tests, then42* specify what tests to run on each side.43*/4445/*46* Should we run the client or server in a separate thread?47* Both sides can throw exceptions, but do you have a preference48* as to which side should be the main thread.49*/50static boolean separateServerThread = false;5152/*53* Where do we find the keystores?54*/55static String pathToStores = "../../../../javax/net/ssl/etc";56static String keyStoreFile = "keystore";57static String trustStoreFile = "truststore";58static String passwd = "passphrase";5960/*61* Is the server ready to serve?62*/63volatile static boolean serverReady = false;6465/*66* Turn on SSL debugging?67*/68static boolean debug = false;6970/*71* If the client or server is doing some kind of object creation72* that the other side depends on, and that thread prematurely73* exits, you may experience a hang. The test harness will74* terminate all hung threads after its timeout has expired,75* currently 3 minutes by default, but you might try to be76* smart about it....77*/7879/*80* Define the server side of the test.81*82* If the server prematurely exits, serverReady will be set to true83* to avoid infinite hangs.84*/85void doServerSide() throws Exception {86SSLServerSocketFactory sslssf =87(SSLServerSocketFactory) SSLServerSocketFactory.getDefault();88SSLServerSocket sslServerSocket =89(SSLServerSocket) sslssf.createServerSocket(serverPort);9091serverPort = sslServerSocket.getLocalPort();9293/*94* Signal Client, we're ready for his connect.95*/96serverReady = true;9798SSLSocket sslSocket = (SSLSocket) sslServerSocket.accept();99InputStream sslIS = sslSocket.getInputStream();100OutputStream sslOS = sslSocket.getOutputStream();101102sslIS.read();103sslOS.write(85);104sslOS.flush();105106sslSocket.close();107}108109/*110* Define the client side of the test.111*112* If the server prematurely exits, serverReady will be set to true113* to avoid infinite hangs.114*/115void doClientSide() throws Exception {116117/*118* Wait for server to get started.119*/120while (!serverReady) {121Thread.sleep(50);122}123124SSLSocketFactory sslsf =125(SSLSocketFactory) SSLSocketFactory.getDefault();126SSLSocket sslSocket = (SSLSocket)127sslsf.createSocket("localhost", serverPort);128129InputStream sslIS = sslSocket.getInputStream();130OutputStream sslOS = sslSocket.getOutputStream();131132sslOS.write(280);133sslOS.flush();134sslIS.read();135136sslSocket.close();137}138139/*140* =============================================================141* The remainder is just support stuff142*/143144// use any free port by default145volatile int serverPort = 0;146147volatile Exception serverException = null;148volatile Exception clientException = null;149150public static void main(String[] args) throws Exception {151String keyFilename =152System.getProperty("test.src", "./") + "/" + pathToStores +153"/" + keyStoreFile;154String trustFilename =155System.getProperty("test.src", "./") + "/" + pathToStores +156"/" + trustStoreFile;157158// first test a good provider name159160System.setProperty("javax.net.ssl.keyStore", keyFilename);161System.setProperty("javax.net.ssl.keyStoreProvider", "BAD-PROVIDER");162System.setProperty("javax.net.ssl.keyStorePassword", passwd);163System.setProperty("javax.net.ssl.trustStore", trustFilename);164System.setProperty("javax.net.ssl.trustStoreProvider", "SUN");165System.setProperty("javax.net.ssl.trustStorePassword", passwd);166167if (debug)168System.setProperty("javax.net.debug", "ssl,defaultctx");169170try {171new BadKSProvider();172throw new SecurityException("expected no-such-provider exception");173} catch (SocketException se) {174175// catching the exception is ok,176// but let's try to confirm it is the right exception.177//178// XXX this test must be updated if the exception message changes179180Throwable cause = se.getCause();181if (cause instanceof java.security.NoSuchAlgorithmException == false) {182se.printStackTrace();183throw new Exception("Unexpected exception" + se);184}185186cause = cause.getCause();187if (cause instanceof java.security.NoSuchProviderException == false) {188se.printStackTrace();189throw new Exception("Unexpected exception" + se);190}191192System.out.println("OK");193}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*/204BadKSProvider() throws Exception {205try {206if (separateServerThread) {207startServer(true);208startClient(false);209} else {210startClient(true);211startServer(false);212}213} catch (Exception e) {214//swallow for now. Show later215}216217/*218* Wait for other side to close down.219*/220if (separateServerThread) {221serverThread.join();222} else {223clientThread.join();224}225226/*227* When we get here, the test is pretty much over.228* Which side threw the error?229*/230Exception local;231Exception remote;232String whichRemote;233234if (separateServerThread) {235remote = serverException;236local = clientException;237whichRemote = "server";238} else {239remote = clientException;240local = serverException;241whichRemote = "client";242}243244/*245* If both failed, return the curthread's exception, but also246* print the remote side Exception247*/248if ((local != null) && (remote != null)) {249System.out.println(whichRemote + " also threw:");250//remote.printStackTrace();251System.out.println();252throw local;253}254255if (remote != null) {256throw remote;257}258259if (local != null) {260throw local;261}262}263264void startServer(boolean newThread) throws Exception {265if (newThread) {266serverThread = new Thread() {267public void run() {268try {269doServerSide();270} catch (Exception e) {271/*272* Our server thread just died.273*274* Release the client, if not active already...275*/276System.err.println("Server died...");277serverReady = true;278serverException = e;279}280}281};282serverThread.start();283} else {284try {285doServerSide();286} catch (Exception e) {287serverException = e;288} finally {289serverReady = true;290}291}292}293294void startClient(boolean newThread) throws Exception {295if (newThread) {296clientThread = new Thread() {297public void run() {298try {299doClientSide();300} catch (Exception e) {301/*302* Our client thread just died.303*/304System.err.println("Client died...");305clientException = e;306}307}308};309clientThread.start();310} else {311try {312doClientSide();313} catch (Exception e) {314clientException = e;315}316}317}318}319320321