Path: blob/master/test/jdk/sun/security/ssl/SSLEngineImpl/DelegatedTaskWrongException.java
41152 views
/*1* Copyright (c) 2003, 2014, 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 496945931* @summary Delegated tasks are not reflecting the subclasses of SSLException32* @run main/othervm DelegatedTaskWrongException33*/3435import javax.net.ssl.*;36import javax.net.ssl.SSLEngineResult.*;37import java.io.*;38import java.security.*;39import java.nio.*;4041public class DelegatedTaskWrongException {4243private static boolean debug = false;4445private SSLContext sslc;46private SSLEngine ssle1; // client47private SSLEngine ssle2; // server4849private static String pathToStores = "../../../../javax/net/ssl/etc";50private static String keyStoreFile = "keystore";51private static String trustStoreFile = "truststore";52private static String passwd = "passphrase";5354private static String keyFilename =55System.getProperty("test.src", "./") + "/" + pathToStores +56"/" + keyStoreFile;57private static String trustFilename =58System.getProperty("test.src", "./") + "/" + pathToStores +59"/" + trustStoreFile;6061private ByteBuffer appOut1; // write side of ssle162private ByteBuffer appIn1; // read side of ssle163private ByteBuffer appOut2; // write side of ssle264private ByteBuffer appIn2; // read side of ssle26566private ByteBuffer oneToTwo; // "reliable" transport ssle1->ssle267private ByteBuffer twoToOne; // "reliable" transport ssle2->ssle16869/*70* Majority of the test case is here, setup is done below.71*/72private void createSSLEngines() throws Exception {73ssle1 = sslc.createSSLEngine("client", 1);74ssle1.setUseClientMode(true);7576ssle2 = sslc.createSSLEngine();77ssle2.setUseClientMode(false);7879ssle1.setEnabledProtocols(new String [] { "SSLv3" });80ssle2.setEnabledProtocols(new String [] { "TLSv1" });81}8283private void runTest() throws Exception {84boolean dataDone = false;8586createSSLEngines();87createBuffers();8889SSLEngineResult result1; // ssle1's results from last operation90SSLEngineResult result2; // ssle2's results from last operation9192result1 = ssle1.wrap(appOut1, oneToTwo);93oneToTwo.flip();9495result2 = ssle2.unwrap(oneToTwo, appIn2);9697runDelegatedTasks(result2, ssle2);9899try {100/*101* We should be getting a SSLHandshakeException.102* If this changes, we'll need to update this test.103* Anything else and we fail.104*/105result2 = ssle2.unwrap(oneToTwo, appIn2);106throw new Exception(107"TEST FAILED: Didn't generate any exception");108} catch (SSLHandshakeException e) {109System.out.println("TEST PASSED: Caught right exception");110} catch (SSLException e) {111System.out.println("TEST FAILED: Generated wrong exception");112throw e;113}114}115116public static void main(String args[]) throws Exception {117// reset the security property to make sure that the algorithms118// and keys used in this test are not disabled.119Security.setProperty("jdk.tls.disabledAlgorithms", "");120121DelegatedTaskWrongException test;122123test = new DelegatedTaskWrongException();124125test.createSSLEngines();126127test.runTest();128129System.out.println("Test Passed.");130}131132/*133* **********************************************************134* Majority of the test case is above, below is just setup stuff135* **********************************************************136*/137138public DelegatedTaskWrongException() throws Exception {139sslc = getSSLContext(keyFilename, trustFilename);140}141142/*143* Create an initialized SSLContext to use for this test.144*/145private SSLContext getSSLContext(String keyFile, String trustFile)146throws Exception {147148KeyStore ks = KeyStore.getInstance("JKS");149KeyStore ts = KeyStore.getInstance("JKS");150151char[] passphrase = "passphrase".toCharArray();152153ks.load(new FileInputStream(keyFile), passphrase);154ts.load(new FileInputStream(trustFile), passphrase);155156KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");157kmf.init(ks, passphrase);158159TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");160tmf.init(ts);161162SSLContext sslCtx = SSLContext.getInstance("TLS");163164sslCtx.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);165166return sslCtx;167}168169private void createBuffers() {170// Size the buffers as appropriate.171172SSLSession session = ssle1.getSession();173int appBufferMax = session.getApplicationBufferSize();174int netBufferMax = session.getPacketBufferSize();175176appIn1 = ByteBuffer.allocateDirect(appBufferMax + 50);177appIn2 = ByteBuffer.allocateDirect(appBufferMax + 50);178179oneToTwo = ByteBuffer.allocateDirect(netBufferMax);180twoToOne = ByteBuffer.allocateDirect(netBufferMax);181182appOut1 = ByteBuffer.wrap("Hi Engine2, I'm SSLEngine1".getBytes());183appOut2 = ByteBuffer.wrap("Hello Engine1, I'm SSLEngine2".getBytes());184185log("AppOut1 = " + appOut1);186log("AppOut2 = " + appOut2);187log("");188}189190private static void runDelegatedTasks(SSLEngineResult result,191SSLEngine engine) throws Exception {192193if (result.getHandshakeStatus() == HandshakeStatus.NEED_TASK) {194Runnable runnable;195while ((runnable = engine.getDelegatedTask()) != null) {196log("running delegated task...");197runnable.run();198}199}200}201202private static boolean isEngineClosed(SSLEngine engine) {203return (engine.isOutboundDone() && engine.isInboundDone());204}205206private static void checkTransfer(ByteBuffer a, ByteBuffer b)207throws Exception {208a.flip();209b.flip();210211if (!a.equals(b)) {212throw new Exception("Data didn't transfer cleanly");213} else {214log("Data transferred cleanly");215}216217a.position(a.limit());218b.position(b.limit());219a.limit(a.capacity());220b.limit(b.capacity());221}222223private static void log(String str) {224if (debug) {225System.out.println(str);226}227}228}229230231