Path: blob/master/test/jdk/sun/security/ssl/SSLContextImpl/CustomizedDTLSDefaultProtocols.java
41152 views
/*1* Copyright (c) 2018, 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 -Djdk.tls.client.protocols="DTLSv1.0"31* CustomizedDTLSDefaultProtocols32*/333435import java.security.Security;36import java.util.Arrays;37import java.util.HashSet;38import java.util.Set;3940import javax.net.SocketFactory;41import javax.net.ssl.KeyManager;42import javax.net.ssl.SSLContext;43import javax.net.ssl.SSLEngine;44import javax.net.ssl.SSLParameters;45import javax.net.ssl.SSLServerSocket;46import javax.net.ssl.SSLServerSocketFactory;47import javax.net.ssl.SSLSocket;48import javax.net.ssl.TrustManager;4950public class CustomizedDTLSDefaultProtocols {5152enum ContextVersion {53TLS_CV_01("DTLS",54new String[] {"DTLSv1.0"}),55TLS_CV_02("DTLSv1.0",56new String[] {"DTLSv1.0"}),57TLS_CV_03("DTLSv1.2",58new String[] {"DTLSv1.0", "DTLSv1.2"});5960final String contextVersion;61final String[] enabledProtocols;62final static String[] supportedProtocols = new String[] {63"DTLSv1.0", "DTLSv1.2"};6465ContextVersion(String contextVersion, String[] enabledProtocols) {66this.contextVersion = contextVersion;67this.enabledProtocols = enabledProtocols;68}69}7071private static boolean checkProtocols(String[] target, String[] expected) {72boolean success = true;73if (target.length == 0) {74System.out.println("\tError: No protocols");75success = false;76}7778if (!protocolEquals(target, expected)) {79System.out.println("\tError: Expected to get protocols " +80Arrays.toString(expected));81success = false;82}83System.out.println("\t Protocols found " + Arrays.toString(target));8485return success;86}8788private static boolean protocolEquals(89String[] actualProtocols,90String[] expectedProtocols) {91if (actualProtocols.length != expectedProtocols.length) {92return false;93}9495Set<String> set = new HashSet<>(Arrays.asList(expectedProtocols));96for (String actual : actualProtocols) {97if (set.add(actual)) {98return false;99}100}101102return true;103}104105private static boolean checkCipherSuites(String[] target) {106boolean success = true;107if (target.length == 0) {108System.out.println("\tError: No cipher suites");109success = false;110}111112return success;113}114115public static void main(String[] args) throws Exception {116// reset the security property to make sure that the algorithms117// and keys used in this test are not disabled.118Security.setProperty("jdk.tls.disabledAlgorithms", "");119120boolean failed = false;121for (ContextVersion cv : ContextVersion.values()) {122System.out.println("Checking SSLContext of " + cv.contextVersion);123SSLContext context = SSLContext.getInstance(cv.contextVersion);124125// Default SSLContext is initialized automatically.126if (!cv.contextVersion.equals("Default")) {127// Use default TK, KM and random.128context.init((KeyManager[])null, (TrustManager[])null, null);129}130131//132// Check SSLContext133//134// Check default SSLParameters of SSLContext135System.out.println("\tChecking default SSLParameters");136SSLParameters parameters = context.getDefaultSSLParameters();137138String[] protocols = parameters.getProtocols();139failed |= !checkProtocols(protocols, cv.enabledProtocols);140141String[] ciphers = parameters.getCipherSuites();142failed |= !checkCipherSuites(ciphers);143144// Check supported SSLParameters of SSLContext145System.out.println("\tChecking supported SSLParameters");146parameters = context.getSupportedSSLParameters();147148protocols = parameters.getProtocols();149failed |= !checkProtocols(protocols, cv.supportedProtocols);150151ciphers = parameters.getCipherSuites();152failed |= !checkCipherSuites(ciphers);153154//155// Check SSLEngine156//157// Check SSLParameters of SSLEngine158System.out.println();159System.out.println("\tChecking SSLEngine of this SSLContext");160System.out.println("\tChecking SSLEngine.getSSLParameters()");161SSLEngine engine = context.createSSLEngine();162engine.setUseClientMode(true);163parameters = engine.getSSLParameters();164165protocols = parameters.getProtocols();166failed |= !checkProtocols(protocols, cv.enabledProtocols);167168ciphers = parameters.getCipherSuites();169failed |= !checkCipherSuites(ciphers);170171System.out.println("\tChecking SSLEngine.getEnabledProtocols()");172protocols = engine.getEnabledProtocols();173failed |= !checkProtocols(protocols, cv.enabledProtocols);174175System.out.println("\tChecking SSLEngine.getEnabledCipherSuites()");176ciphers = engine.getEnabledCipherSuites();177failed |= !checkCipherSuites(ciphers);178179System.out.println("\tChecking SSLEngine.getSupportedProtocols()");180protocols = engine.getSupportedProtocols();181failed |= !checkProtocols(protocols, cv.supportedProtocols);182183System.out.println(184"\tChecking SSLEngine.getSupportedCipherSuites()");185ciphers = engine.getSupportedCipherSuites();186failed |= !checkCipherSuites(ciphers);187188//189// Check SSLSocket190//191// Check SSLParameters of SSLSocket192System.out.println();193System.out.println("\tChecking SSLSocket of this SSLContext");194try {195context.getSocketFactory();196failed = true;197System.out.println("SSLSocket returned a socket for DTLS");198} catch (UnsupportedOperationException e) {199System.out.println("\t " + e.getMessage());200}201202//203// Check SSLServerSocket204//205// Check SSLParameters of SSLServerSocket206System.out.println();207System.out.println("\tChecking SSLServerSocket of this SSLContext");208try {209context.getServerSocketFactory();210failed = true;211System.out.println("SSLServerSocket returned a socket for DTLS");212} catch (UnsupportedOperationException e) {213System.out.println("\t " + e.getMessage());214}215}216217if (failed) {218throw new Exception("Run into problems, see log for more details");219} else {220System.out.println("\t... Success");221}222}223}224225226