Path: blob/master/test/jdk/sun/security/ssl/spi/ProviderInit.java
41152 views
/*1* Copyright (c) 2002, 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* @test 1.3 01/03/0825* @bug 452255026* @summary SSLContext TrustMananagerFactory and KeyManagerFactory27* should throw if not init28* @run main/othervm ProviderInit29*30* SunJSSE does not support dynamic system properties, no way to re-use31* system properties in samevm/agentvm mode.32* @author Jaya Hangal33*/3435/**36* This test case makes sure that the providers throw37* IllegalStateException when used without getting initialized.38* The relevant tests are in doClientSide method.39*/4041import java.io.*;42import java.net.*;43import javax.net.ssl.*;4445public class ProviderInit {4647/*48* =============================================================49* Set the various variables needed for the tests, then50* specify what tests to run on each side.51*/5253/*54* Should we run the client or server in a separate thread?55* Both sides can throw exceptions, but do you have a preference56* as to which side should be the main thread.57*/58static boolean separateServerThread = true;5960/*61* Where do we find the keystores?62*/63static String pathToStores = "../../../../javax/net/ssl/etc";64static String keyStoreFile = "keystore";65static String trustStoreFile = "truststore";66static String passwd = "passphrase";6768/*69* Is the server ready to serve?70*/71volatile static boolean serverReady = false;7273/*74* Turn on SSL debugging?75*/76static boolean debug = false;7778/*79* If the client or server is doing some kind of object creation80* that the other side depends on, and that thread prematurely81* exits, you may experience a hang. The test harness will82* terminate all hung threads after its timeout has expired,83* currently 3 minutes by default, but you might try to be84* smart about it....85*/8687/*88* Define the server side of the test.89*90* If the server prematurely exits, serverReady will be set to true91* to avoid infinite hangs.92*/93void doServerSide() throws Exception {9495SSLContext context = SSLContext.getInstance("TLS");96SSLServerSocketFactory sslssf =97(SSLServerSocketFactory) SSLServerSocketFactory.getDefault();98SSLServerSocket sslServerSocket =99(SSLServerSocket) sslssf.createServerSocket(serverPort);100serverPort = sslServerSocket.getLocalPort();101/*102* Signal Client, we're ready for his connect.103*/104serverReady = true;105106SSLSocket sslSocket = (SSLSocket) sslServerSocket.accept();107sslSocket.setNeedClientAuth(true);108InputStream sslIS = sslSocket.getInputStream();109OutputStream sslOS = sslSocket.getOutputStream();110111sslIS.read();112sslOS.write(85);113sslOS.flush();114115sslSocket.close();116}117118/*119* Define the client side of the test.120*121* If the server prematurely exits, serverReady will be set to true122* to avoid infinite hangs.123*/124void doClientSide() throws Exception {125/*126* Wait for server to get started.127*/128while (!serverReady) {129Thread.sleep(50);130}131132SSLContext context = SSLContext.getInstance("TLS");133SSLSocketFactory sslsf = null;134135/*136* Try using SSLContext without calling init()137* A call to getSocketFactory() will throw an exception138*/139try {140sslsf =141(SSLSocketFactory) context.getSocketFactory();142communicate(sslsf, false);143} catch (IllegalStateException e) {144System.out.println("Caught the right exception" + e);145}146147/*148* Try using TrustManagerFactory without calling init()149* A call to getTrustManagers() will throw an exception150*/151try {152TrustManagerFactory tmf = TrustManagerFactory.getInstance(153"sunX509");154TrustManager[] tms = tmf.getTrustManagers();155context.init(null, tms, null);156sslsf =157(SSLSocketFactory) context.getSocketFactory();158communicate(sslsf, false);159} catch (IllegalStateException e) {160System.out.println("Caught the right exception" + e);161}162163/*164* Try using KeyManagerFactory without calling init()165* A call to getKeyManagers() will throw an exception166*/167try {168KeyManagerFactory kmf = KeyManagerFactory.getInstance("sunX509");169KeyManager kms[] = kmf.getKeyManagers();170context.init(kms, null, null);171sslsf =172(SSLSocketFactory) context.getSocketFactory();173communicate(sslsf, true);174} catch (Exception e) {175System.out.println("Caught the right exception" + e);176sslsf = (SSLSocketFactory) SSLSocketFactory.getDefault();177communicate(sslsf, false);178}179}180181void communicate(SSLSocketFactory sslsf, boolean needClientAuth)182throws Exception {183184SSLSocket sslSocket = (SSLSocket) sslsf.createSocket(185"localhost", serverPort);186sslSocket.setNeedClientAuth(needClientAuth);187InputStream sslIS = sslSocket.getInputStream();188OutputStream sslOS = sslSocket.getOutputStream();189190sslOS.write(280);191sslOS.flush();192sslIS.read();193sslSocket.close();194}195196/*197* =============================================================198* The remainder is just support stuff199*/200201// use any free port by default202volatile int serverPort = 0;203204volatile Exception serverException = null;205volatile Exception clientException = null;206207public static void main(String[] args) throws Exception {208String keyFilename =209System.getProperty("test.src", "./") + "/" + pathToStores +210"/" + keyStoreFile;211String trustFilename =212System.getProperty("test.src", "./") + "/" + pathToStores +213"/" + trustStoreFile;214215System.setProperty("javax.net.ssl.keyStore", keyFilename);216System.setProperty("javax.net.ssl.keyStorePassword", passwd);217System.setProperty("javax.net.ssl.trustStore", trustFilename);218System.setProperty("javax.net.ssl.trustStorePassword", passwd);219220if (debug)221System.setProperty("javax.net.debug", "all");222223/*224* Start the tests.225*/226new ProviderInit();227}228229Thread clientThread = null;230Thread serverThread = null;231232/*233* Primary constructor, used to drive remainder of the test.234*235* Fork off the other side, then do your work.236*/237ProviderInit() throws Exception {238if (separateServerThread) {239startServer(true);240startClient(false);241} else {242startClient(true);243startServer(false);244}245246/*247* Wait for other side to close down.248*/249if (separateServerThread) {250serverThread.join();251} else {252clientThread.join();253}254255/*256* When we get here, the test is pretty much over.257*258* If the main thread excepted, that propagates back259* immediately. If the other thread threw an exception, we260* should report back.261*/262if (serverException != null)263throw serverException;264if (clientException != null)265throw clientException;266}267268void startServer(boolean newThread) throws Exception {269if (newThread) {270serverThread = new Thread() {271public void run() {272try {273doServerSide();274} catch (Exception e) {275/*276* Our server thread just died.277*278* Release the client, if not active already...279*/280System.err.println("Server died..." + e);281e.printStackTrace();282serverReady = true;283serverException = e;284}285}286};287serverThread.start();288} else {289doServerSide();290}291}292293void startClient(boolean newThread) throws Exception {294if (newThread) {295clientThread = new Thread() {296public void run() {297try {298doClientSide();299} catch (Exception e) {300/*301* Our client thread just died.302*/303System.err.println("Client died...");304clientException = e;305}306}307};308clientThread.start();309} else {310doClientSide();311}312}313}314315316