Path: blob/master/test/jdk/sun/security/ssl/SSLContextImpl/DefaultDTLSEnabledProtocols.java
41152 views
/*1* Copyright (c) 2013, 2020, 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// SunJSSE does not support dynamic system properties, no way to re-use24// system properties in samevm/agentvm mode.2526/*27* @test28* @bug 823747429* @summary Test jdk.tls.client.protocols with DTLS30* @run main/othervm DefaultDTLSEnabledProtocols31*/3233import java.security.Security;34import java.util.Arrays;35import java.util.HashSet;36import java.util.Set;3738import javax.net.SocketFactory;39import javax.net.ssl.KeyManager;40import javax.net.ssl.SSLContext;41import javax.net.ssl.SSLEngine;42import javax.net.ssl.SSLParameters;43import javax.net.ssl.SSLServerSocket;44import javax.net.ssl.SSLServerSocketFactory;45import javax.net.ssl.SSLSocket;46import javax.net.ssl.TrustManager;4748public class DefaultDTLSEnabledProtocols {49static enum ContextVersion {50TLS_CV_01("DTLS",51new String[] {"DTLSv1.0", "DTLSv1.2"}),52TLS_CV_02("DTLSv1.0",53new String[] {"DTLSv1.0"}),54TLS_CV_03("DTLSv1.2",55new String[] {"DTLSv1.0", "DTLSv1.2"});5657final String contextVersion;58final String[] enabledProtocols;59final static String[] supportedProtocols = new String[] {60"DTLSv1.0", "DTLSv1.2"};6162ContextVersion(String contextVersion, String[] enabledProtocols) {63this.contextVersion = contextVersion;64this.enabledProtocols = enabledProtocols;65}66}6768private static boolean checkProtocols(String[] target, String[] expected) {69boolean success = true;70if (target.length == 0) {71System.out.println("\tError: No protocols");72success = false;73}7475if (!protocolEquals(target, expected)) {76System.out.println("\tError: Expected to get protocols " +77Arrays.toString(expected));78success = false;79}80System.out.println("\t Protocols found " + Arrays.toString(target));8182return success;83}8485private static boolean protocolEquals(86String[] actualProtocols,87String[] expectedProtocols) {88if (actualProtocols.length != expectedProtocols.length) {89return false;90}9192Set<String> set = new HashSet<>(Arrays.asList(expectedProtocols));93for (String actual : actualProtocols) {94if (set.add(actual)) {95return false;96}97}9899return true;100}101102private static boolean checkCipherSuites(String[] target) {103boolean success = true;104if (target.length == 0) {105System.out.println("\tError: No cipher suites");106success = false;107}108109return success;110}111112public static void main(String[] args) throws Exception {113// reset the security property to make sure that the algorithms114// and keys used in this test are not disabled.115Security.setProperty("jdk.tls.disabledAlgorithms", "");116117boolean failed = false;118for (ContextVersion cv : ContextVersion.values()) {119System.out.println("Checking SSLContext of " + cv.contextVersion);120SSLContext context = SSLContext.getInstance(cv.contextVersion);121122// Default SSLContext is initialized automatically.123if (!cv.contextVersion.equals("Default")) {124// Use default TK, KM and random.125context.init((KeyManager[])null, (TrustManager[])null, null);126}127128//129// Check SSLContext130//131// Check default SSLParameters of SSLContext132System.out.println("\tChecking default SSLParameters");133SSLParameters parameters = context.getDefaultSSLParameters();134135String[] protocols = parameters.getProtocols();136failed |= !checkProtocols(protocols, cv.enabledProtocols);137138String[] ciphers = parameters.getCipherSuites();139failed |= !checkCipherSuites(ciphers);140141// Check supported SSLParameters of SSLContext142System.out.println("\tChecking supported SSLParameters");143parameters = context.getSupportedSSLParameters();144145protocols = parameters.getProtocols();146failed |= !checkProtocols(protocols, cv.supportedProtocols);147148ciphers = parameters.getCipherSuites();149failed |= !checkCipherSuites(ciphers);150151//152// Check SSLEngine153//154// Check SSLParameters of SSLEngine155System.out.println();156System.out.println("\tChecking SSLEngine of this SSLContext");157System.out.println("\tChecking SSLEngine.getSSLParameters()");158SSLEngine engine = context.createSSLEngine();159engine.setUseClientMode(true);160parameters = engine.getSSLParameters();161162protocols = parameters.getProtocols();163failed |= !checkProtocols(protocols, cv.enabledProtocols);164165ciphers = parameters.getCipherSuites();166failed |= !checkCipherSuites(ciphers);167168System.out.println("\tChecking SSLEngine.getEnabledProtocols()");169protocols = engine.getEnabledProtocols();170failed |= !checkProtocols(protocols, cv.enabledProtocols);171172System.out.println("\tChecking SSLEngine.getEnabledCipherSuites()");173ciphers = engine.getEnabledCipherSuites();174failed |= !checkCipherSuites(ciphers);175176System.out.println("\tChecking SSLEngine.getSupportedProtocols()");177protocols = engine.getSupportedProtocols();178failed |= !checkProtocols(protocols, cv.supportedProtocols);179180System.out.println(181"\tChecking SSLEngine.getSupportedCipherSuites()");182ciphers = engine.getSupportedCipherSuites();183failed |= !checkCipherSuites(ciphers);184185//186// Check SSLSocket187//188// Check SSLParameters of SSLSocket189System.out.println();190System.out.println("\tChecking SSLSocket of this SSLContext");191try {192context.getSocketFactory();193failed = true;194System.out.println("SSLSocket returned a socket for DTLS");195} catch (UnsupportedOperationException e) {196System.out.println("\t " + e.getMessage());197}198199//200// Check SSLServerSocket201//202// Check SSLParameters of SSLServerSocket203System.out.println();204System.out.println("\tChecking SSLServerSocket of this SSLContext");205try {206context.getServerSocketFactory();207failed = true;208System.out.println("SSLServerSocket returned a socket for DTLS");209} catch (UnsupportedOperationException e) {210System.out.println("\t " + e.getMessage());211}212}213214if (failed) {215throw new Exception("Run into problems, see log for more details");216} else {217System.out.println("\t... Success");218}219}220}221222223