Path: blob/master/test/jdk/javax/net/ssl/TLS/TLSClientPropertyTest.java
41152 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* @modules java.security.jgss30* java.security.jgss/sun.security.jgss.krb531* java.security.jgss/sun.security.krb5:+open32* java.security.jgss/sun.security.krb5.internal:+open33* java.security.jgss/sun.security.krb5.internal.ccache34* java.security.jgss/sun.security.krb5.internal.crypto35* java.security.jgss/sun.security.krb5.internal.ktab36* java.base/sun.security.util37* @run main/othervm TLSClientPropertyTest NoProperty38* @run main/othervm TLSClientPropertyTest SSLv339* @run main/othervm TLSClientPropertyTest TLSv140* @run main/othervm TLSClientPropertyTest TLSv1141* @run main/othervm TLSClientPropertyTest TLSv1242* @run main/othervm TLSClientPropertyTest TLSv1343* @run main/othervm TLSClientPropertyTest TLS44* @run main/othervm TLSClientPropertyTest WrongProperty45*/4647import java.security.KeyManagementException;48import java.security.NoSuchAlgorithmException;49import java.util.Arrays;50import java.util.List;51import javax.net.ssl.SSLContext;5253/**54* Sets the property jdk.tls.client.protocols to one of this protocols:55* SSLv3,TLSv1,TLSv1.1,TLSv1.2 and TLSV(invalid) or removes this56* property (if any),then validates the default, supported and current57* protocols in the SSLContext.58*/59public class TLSClientPropertyTest {60private final String[] expectedSupportedProtos = new String[] {61"SSLv2Hello", "SSLv3", "TLSv1", "TLSv1.1", "TLSv1.2", "TLSv1.3"62};6364public static void main(String[] args) throws Exception {6566if (args.length < 1) {67throw new RuntimeException(68"Incorrect arguments,expected arguments: testCase");69}7071String[] expectedDefaultProtos;72String testCase = args[0];73String contextProtocol;74switch (testCase) {75case "NoProperty":76if (System.getProperty("jdk.tls.client.protocols") != null) {77System.getProperties().remove("jdk.tls.client.protocols");78}79contextProtocol = null;80expectedDefaultProtos = new String[] {81"TLSv1.2", "TLSv1.3"82};83break;84case "SSLv3":85contextProtocol = "SSLv3";86expectedDefaultProtos = new String[] {87};88break;89case "TLSv1":90contextProtocol = "TLSv1";91expectedDefaultProtos = new String[] {92};93break;94case "TLSv11":95contextProtocol = "TLSv1.1";96expectedDefaultProtos = new String[] {97};98break;99case "TLSv12":100contextProtocol = "TLSv1.2";101expectedDefaultProtos = new String[] {102"TLSv1.2"103};104break;105case "TLSv13":106case "TLS":107contextProtocol = "TLSv1.3";108expectedDefaultProtos = new String[] {109"TLSv1.2", "TLSv1.3"110};111break;112case "WrongProperty":113expectedDefaultProtos = new String[] {};114contextProtocol = "TLSV";115break;116default:117throw new RuntimeException("test case is wrong");118}119if (contextProtocol != null) {120System.setProperty("jdk.tls.client.protocols", contextProtocol);121}122try {123TLSClientPropertyTest test = new TLSClientPropertyTest();124test.test(contextProtocol, expectedDefaultProtos);125if (testCase.equals("WrongProperty")) {126throw new RuntimeException(127"Test failed: NoSuchAlgorithmException " +128"is expected when input wrong protocol");129} else {130System.out.println("Test " + contextProtocol + " passed");131}132} catch (NoSuchAlgorithmException nsae) {133if (testCase.equals("WrongProperty")) {134System.out.println("NoSuchAlgorithmException is expected,"135+ contextProtocol + " test passed");136} else {137throw nsae;138}139}140141}142143/**144* The parameter passed is the user enforced protocol. Does not catch145* NoSuchAlgorithmException, WrongProperty test will use it.146*/147public void test(String expectedContextProto,148String[] expectedDefaultProtos) throws NoSuchAlgorithmException {149150SSLContext context = null;151try {152if (expectedContextProto != null) {153context = SSLContext.getInstance(expectedContextProto);154context.init(null, null, null);155} else {156context = SSLContext.getDefault();157}158printContextDetails(context);159} catch (KeyManagementException ex) {160error(null, ex);161}162163validateContext(expectedContextProto, expectedDefaultProtos, context);164}165166/**167* Simple print utility for SSLContext's protocol details.168*/169private void printContextDetails(SSLContext context) {170System.out.println("Default Protocols: "171+ Arrays.toString(context.getDefaultSSLParameters()172.getProtocols()));173System.out.println("Supported Protocols: "174+ Arrays.toString(context.getSupportedSSLParameters()175.getProtocols()));176System.out.println("Current Protocol : " + context.getProtocol());177178}179180/**181* Error handler.182*/183private void error(String msg, Throwable tble) {184String finalMsg = "FAILED " + (msg != null ? msg : "");185if (tble != null) {186throw new RuntimeException(finalMsg, tble);187}188throw new RuntimeException(finalMsg);189}190191/**192* Validates the SSLContext's protocols against the user enforced protocol.193*/194private void validateContext(String expectedProto,195String[] expectedDefaultProtos, SSLContext context) {196if (expectedProto == null) {197expectedProto = "Default";198}199if (!context.getProtocol().equals(expectedProto)) {200error("Invalid current protocol: " + context.getProtocol()201+ ", Expected:" + expectedProto, null);202}203List<String> actualDefaultProtos = Arrays.asList(context204.getDefaultSSLParameters().getProtocols());205for (String p : expectedDefaultProtos) {206if (!actualDefaultProtos.contains(p)) {207error("Default protocol " + p + "missing", null);208}209}210List<String> actualSupportedProtos = Arrays.asList(context211.getSupportedSSLParameters().getProtocols());212213for (String p : expectedSupportedProtos) {214if (!actualSupportedProtos.contains(p)) {215error("Expected to support protocol:" + p, null);216}217}218}219}220221222