Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/net/ssl/TLS/TLSClientPropertyTest.java
38854 views
/*1* Copyright (c) 2014, 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/*24* @test25* @bug 8049432 8069038 8234723 820234326* @summary New tests for TLS property jdk.tls.client.protocols27* @summary javax/net/ssl/TLS/TLSClientPropertyTest.java needs to be28* updated for JDK-806121029* @run main/othervm TLSClientPropertyTest NoProperty30* @run main/othervm TLSClientPropertyTest SSLv331* @run main/othervm TLSClientPropertyTest TLSv132* @run main/othervm TLSClientPropertyTest TLSv1133* @run main/othervm TLSClientPropertyTest TLSv1234* @run main/othervm TLSClientPropertyTest TLSv1335* @run main/othervm TLSClientPropertyTest TLS36* @run main/othervm TLSClientPropertyTest WrongProperty37*/3839import java.security.KeyManagementException;40import java.security.NoSuchAlgorithmException;41import java.util.Arrays;42import java.util.List;43import javax.net.ssl.SSLContext;4445/**46* Sets the property jdk.tls.client.protocols to one of this protocols:47* SSLv3,TLSv1,TLSv1.1,TLSv1.2 and TLSV(invalid) or removes this48* property (if any),then validates the default, supported and current49* protocols in the SSLContext.50*/51public class TLSClientPropertyTest {52private final String[] expectedSupportedProtos = new String[] {53"SSLv2Hello", "SSLv3", "TLSv1", "TLSv1.1", "TLSv1.2", "TLSv1.3"54};5556public static void main(String[] args) throws Exception {5758if (args.length < 1) {59throw new RuntimeException(60"Incorrect arguments,expected arguments: testCase");61}6263String[] expectedDefaultProtos;64String testCase = args[0];65String contextProtocol;66switch (testCase) {67case "NoProperty":68if (System.getProperty("jdk.tls.client.protocols") != null) {69System.getProperties().remove("jdk.tls.client.protocols");70}71contextProtocol = null;72expectedDefaultProtos = new String[] {73"TLSv1.2"74};75break;76case "SSLv3":77contextProtocol = "SSLv3";78expectedDefaultProtos = new String[] {79};80break;81case "TLSv1":82contextProtocol = "TLSv1";83expectedDefaultProtos = new String[] {84};85break;86case "TLSv11":87contextProtocol = "TLSv1.1";88expectedDefaultProtos = new String[] {89};90break;91case "TLSv12":92case "TLS":93contextProtocol = "TLSv1.2";94expectedDefaultProtos = new String[] {95"TLSv1.2"96};97break;98case "TLSv13":99contextProtocol = "TLSv1.3";100expectedDefaultProtos = new String[] {101"TLSv1.2", "TLSv1.3"102};103break;104case "WrongProperty":105expectedDefaultProtos = new String[] {};106contextProtocol = "TLSV";107break;108default:109throw new RuntimeException("test case is wrong");110}111if (contextProtocol != null) {112System.setProperty("jdk.tls.client.protocols", contextProtocol);113}114try {115TLSClientPropertyTest test = new TLSClientPropertyTest();116test.test(contextProtocol, expectedDefaultProtos);117if (testCase.equals("WrongProperty")) {118throw new RuntimeException(119"Test failed: NoSuchAlgorithmException " +120"is expected when input wrong protocol");121} else {122System.out.println("Test " + contextProtocol + " passed");123}124} catch (NoSuchAlgorithmException nsae) {125if (testCase.equals("WrongProperty")) {126System.out.println("NoSuchAlgorithmException is expected,"127+ contextProtocol + " test passed");128} else {129throw nsae;130}131}132133}134135/**136* The parameter passed is the user enforced protocol. Does not catch137* NoSuchAlgorithmException, WrongProperty test will use it.138*/139public void test(String expectedContextProto,140String[] expectedDefaultProtos) throws NoSuchAlgorithmException {141142SSLContext context = null;143try {144if (expectedContextProto != null) {145context = SSLContext.getInstance(expectedContextProto);146context.init(null, null, null);147} else {148context = SSLContext.getDefault();149}150printContextDetails(context);151} catch (KeyManagementException ex) {152error(null, ex);153}154155validateContext(expectedContextProto, expectedDefaultProtos, context);156}157158/**159* Simple print utility for SSLContext's protocol details.160*/161private void printContextDetails(SSLContext context) {162System.out.println("Default Protocols: "163+ Arrays.toString(context.getDefaultSSLParameters()164.getProtocols()));165System.out.println("Supported Protocols: "166+ Arrays.toString(context.getSupportedSSLParameters()167.getProtocols()));168System.out.println("Current Protocol : " + context.getProtocol());169170}171172/**173* Error handler.174*/175private void error(String msg, Throwable tble) {176String finalMsg = "FAILED " + (msg != null ? msg : "");177if (tble != null) {178throw new RuntimeException(finalMsg, tble);179}180throw new RuntimeException(finalMsg);181}182183/**184* Validates the SSLContext's protocols against the user enforced protocol.185*/186private void validateContext(String expectedProto,187String[] expectedDefaultProtos, SSLContext context) {188if (expectedProto == null) {189expectedProto = "Default";190}191if (!context.getProtocol().equals(expectedProto)) {192error("Invalid current protocol: " + context.getProtocol()193+ ", Expected:" + expectedProto, null);194}195List<String> actualDefaultProtos = Arrays.asList(context196.getDefaultSSLParameters().getProtocols());197for (String p : expectedDefaultProtos) {198if (!actualDefaultProtos.contains(p)) {199error("Default protocol " + p + "missing", null);200}201}202List<String> actualSupportedProtos = Arrays.asList(context203.getSupportedSSLParameters().getProtocols());204205for (String p : expectedSupportedProtos) {206if (!actualSupportedProtos.contains(p)) {207error("Expected to support protocol:" + p, null);208}209}210}211}212213214