Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/ssl/SSLContextImpl/BadTSProvider.java
38853 views
/*1* Copyright (c) 2003, 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// SunJSSE does not support dynamic system properties, no way to re-use25// system properties in samevm/agentvm mode.26//2728/*29* @test30* @bug 491914731* @summary Support for token-based KeyStores32* @run main/othervm BadTSProvider33*/3435import java.io.*;36import java.net.*;37import java.security.*;38import javax.net.ssl.*;3940public class BadTSProvider {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 = false;5455/*56* Where do we find the keystores?57*/58static String pathToStores = "../../../../javax/net/ssl/etc";59static String keyStoreFile = "keystore";60static String trustStoreFile = "truststore";61static String passwd = "passphrase";6263/*64* Is the server ready to serve?65*/66volatile static boolean serverReady = false;6768/*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() throws Exception {89SSLServerSocketFactory sslssf =90(SSLServerSocketFactory) SSLServerSocketFactory.getDefault();91SSLServerSocket sslServerSocket =92(SSLServerSocket) sslssf.createServerSocket(serverPort);9394serverPort = sslServerSocket.getLocalPort();9596/*97* Signal Client, we're ready for his connect.98*/99serverReady = true;100101SSLSocket sslSocket = (SSLSocket) sslServerSocket.accept();102InputStream sslIS = sslSocket.getInputStream();103OutputStream sslOS = sslSocket.getOutputStream();104105sslIS.read();106sslOS.write(85);107sslOS.flush();108109sslSocket.close();110}111112/*113* Define the client side of the test.114*115* If the server prematurely exits, serverReady will be set to true116* to avoid infinite hangs.117*/118void doClientSide() throws Exception {119120/*121* Wait for server to get started.122*/123while (!serverReady) {124Thread.sleep(50);125}126127SSLSocketFactory sslsf =128(SSLSocketFactory) SSLSocketFactory.getDefault();129SSLSocket sslSocket = (SSLSocket)130sslsf.createSocket("localhost", serverPort);131132InputStream sslIS = sslSocket.getInputStream();133OutputStream sslOS = sslSocket.getOutputStream();134135sslOS.write(280);136sslOS.flush();137sslIS.read();138139sslSocket.close();140}141142/*143* =============================================================144* The remainder is just support stuff145*/146147// use any free port by default148volatile int serverPort = 0;149150volatile Exception serverException = null;151volatile Exception clientException = null;152153public static void main(String[] args) throws Exception {154String keyFilename =155System.getProperty("test.src", "./") + "/" + pathToStores +156"/" + keyStoreFile;157String trustFilename =158System.getProperty("test.src", "./") + "/" + pathToStores +159"/" + trustStoreFile;160161// first test a good provider name162163System.setProperty("javax.net.ssl.keyStore", keyFilename);164System.setProperty("javax.net.ssl.keyStoreProvider", "SUN");165System.setProperty("javax.net.ssl.keyStorePassword", passwd);166System.setProperty("javax.net.ssl.trustStore", trustFilename);167System.setProperty("javax.net.ssl.trustStoreProvider", "BAD-PROVIDER");168System.setProperty("javax.net.ssl.trustStorePassword", passwd);169170if (debug)171System.setProperty("javax.net.debug", "ssl,defaultctx");172173try {174new BadTSProvider();175throw new SecurityException("expected no-such-provider exception");176} catch (SocketException se) {177178// catching the exception is ok,179// but let's try to confirm it is the right exception.180//181// XXX this test must be updated if the exception message changes182183Throwable cause = se.getCause();184if (!(cause instanceof NoSuchAlgorithmException)) {185se.printStackTrace();186throw new Exception("Unexpected exception" + se);187}188189cause = cause.getCause();190if (!(cause instanceof KeyStoreException)) {191se.printStackTrace();192throw new Exception("Unexpected exception" + se);193}194195cause = cause.getCause();196if (!(cause instanceof NoSuchProviderException)) {197se.printStackTrace();198throw new Exception("Unexpected exception" + se);199}200201System.out.println("OK");202}203}204205Thread clientThread = null;206Thread serverThread = null;207208/*209* Primary constructor, used to drive remainder of the test.210*211* Fork off the other side, then do your work.212*/213BadTSProvider() throws Exception {214try {215if (separateServerThread) {216startServer(true);217startClient(false);218} else {219startClient(true);220startServer(false);221}222} catch (Exception e) {223//swallow for now. Show later224}225226/*227* Wait for other side to close down.228*/229if (separateServerThread) {230serverThread.join();231} else {232clientThread.join();233}234235/*236* When we get here, the test is pretty much over.237* Which side threw the error?238*/239Exception local;240Exception remote;241String whichRemote;242243if (separateServerThread) {244remote = serverException;245local = clientException;246whichRemote = "server";247} else {248remote = clientException;249local = serverException;250whichRemote = "client";251}252253/*254* If both failed, return the curthread's exception, but also255* print the remote side Exception256*/257if ((local != null) && (remote != null)) {258System.out.println(whichRemote + " also threw:");259//remote.printStackTrace();260System.out.println();261throw local;262}263264if (remote != null) {265throw remote;266}267268if (local != null) {269throw local;270}271}272273void startServer(boolean newThread) throws Exception {274if (newThread) {275serverThread = new Thread() {276public void run() {277try {278doServerSide();279} catch (Exception e) {280/*281* Our server thread just died.282*283* Release the client, if not active already...284*/285System.err.println("Server died...");286serverReady = true;287serverException = e;288}289}290};291serverThread.start();292} else {293try {294doServerSide();295} catch (Exception e) {296serverException = e;297} finally {298serverReady = true;299}300}301}302303void startClient(boolean newThread) throws Exception {304if (newThread) {305clientThread = new Thread() {306public void run() {307try {308doClientSide();309} catch (Exception e) {310/*311* Our client thread just died.312*/313System.err.println("Client died...");314clientException = e;315}316}317};318clientThread.start();319} else {320try {321doClientSide();322} catch (Exception e) {323clientException = e;324}325}326}327}328329330