Path: blob/master/test/jdk/javax/net/ssl/sanity/pluggability/CheckSSLContextExport.java
41154 views
/*1* Copyright (c) 2002, 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 4635454 6208022 813018126* @summary Check pluggability of SSLContext class.27*/28import java.security.*;29import java.net.*;30import javax.net.ssl.*;3132public class CheckSSLContextExport extends Provider {33private static String info = "test provider for JSSE pluggability";3435public CheckSSLContextExport(String protocols[]) {36super("TestJSSEPluggability", "1.0", info);37for (int i=0; i<protocols.length; i++) {38put("SSLContext." + protocols[i], "MySSLContextImpl");39}40}4142public static void test(String protocol) throws Exception {43SSLContext mySSLContext = SSLContext.getInstance(protocol);4445String providerName = mySSLContext.getProvider().getName();46if (!providerName.equals("TestJSSEPluggability")) {47System.out.println(providerName + "'s SSLContext is used");48throw new Exception("...used the wrong provider: " + providerName);49}50for (int i = 0; i < 2; i++) {51boolean standardCiphers = true;5253switch (i) {54case 0:55// non-standard cipher suites56standardCiphers = false;57MySSLContextImpl.useCustomCipherSuites();58break;59case 1:60standardCiphers = true;61MySSLContextImpl.useStandardCipherSuites();62break;63default:64throw new Exception("Internal Test Error!");65}66System.out.println("Testing with " +67(standardCiphers ? "standard" : "custom") + " cipher suites");68for (int j = 0; j < 4; j++) {69String clsName = null;70try {71switch (j) {72case 0:73SSLSocketFactory sf = mySSLContext.getSocketFactory();74clsName = sf.getClass().getName();75break;76case 1:77SSLServerSocketFactory ssf =78mySSLContext.getServerSocketFactory();79clsName = ssf.getClass().getName();80break;81case 2:82SSLEngine se = mySSLContext.createSSLEngine();83clsName = se.getClass().getName();84break;85case 3:86SSLEngine se2 = mySSLContext.createSSLEngine(null, 0);87clsName = se2.getClass().getName();88break;89default:90throw new Exception("Internal Test Error!");91}92if (!clsName.startsWith("MySSL")) {93throw new Exception("test#" + j +94": wrong impl is used");95} else {96System.out.println("test#" + j +97": accepted valid impl");98}99} catch (RuntimeException re) {100// pass it on101throw re;102}103}104}105}106107public static void main(String[] argv) throws Exception {108String protocols[] = { "SSL", "TLS" };109Provider extraProvider = new CheckSSLContextExport(protocols);110Security.insertProviderAt(extraProvider, 1);111try {112for (int i = 0; i < protocols.length; i++) {113System.out.println("Testing " + protocols[i] + "'s SSLContext");114test(protocols[i]);115}116System.out.println("Test Passed");117} finally {118Security.removeProvider(extraProvider.getName());119}120}121}122123124