Path: blob/master/test/jdk/sun/security/jca/PreferredProviderNegativeTest.java
41149 views
/*1* Copyright (c) 2015, 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*/2223import java.security.Security;24import java.security.NoSuchAlgorithmException;25import javax.crypto.Cipher;26import javax.crypto.NoSuchPaddingException;2728/**29* @test30* @bug 8076359 8133151 815051231* @summary Test for jdk.security.provider.preferred security property32* @run main/othervm PreferredProviderNegativeTest preSet AES false33* @run main/othervm PreferredProviderNegativeTest preSet AES:SunNegative true34* @run main/othervm PreferredProviderNegativeTest afterSet AES:SunJGSS35* @run main/othervm PreferredProviderNegativeTest afterSet AES:SunECNegative36* @run main/othervm PreferredProviderNegativeTest invalidAlg AESInvalid:SunJCE37*/38public class PreferredProviderNegativeTest {3940static final String SEC_PREF_PROP = "jdk.security.provider.preferred";4142/*43* Test security property could be set by valid and invalid provider44* before JCE was loaded45*/46public static void preJCESet(String value, boolean negativeProvider)47throws NoSuchAlgorithmException, NoSuchPaddingException {4849Security.setProperty(SEC_PREF_PROP, value);5051if (!Security.getProperty(SEC_PREF_PROP).equals(value)) {52throw new RuntimeException("Test Failed:The property wasn't set");53}5455String[] arrays = value.split(":");56Cipher cipher = Cipher.getInstance(arrays[0]);57if (negativeProvider) {58if (cipher.getProvider().getName().equals(arrays[1])) {59throw new RuntimeException(60"Test Failed:The provider shouldn't be set.");61}62} else {63if (!cipher.getProvider().getName().equals(arrays[1])) {64throw new RuntimeException("Test Failed:The provider could be "65+ "set by valid provider.");66}67}68System.out.println("Test Pass.");69}7071/*72* Test that the setting of the security property after Cipher.getInstance()73* does not influence previously loaded instances74*/75public static void afterJCESet(String value, String expected)76throws NoSuchAlgorithmException, NoSuchPaddingException {77String[] arrays = value.split(":");78Cipher cipher = Cipher.getInstance(arrays[0]);7980Security.setProperty(SEC_PREF_PROP, value);81if (!cipher.getProvider().getName().equals(expected)) {82throw new RuntimeException("Test Failed:The security property can't"83+ " be updated after JCE load.");84}85System.out.println("Test Pass");86}8788/* Test the security property with negative algorithm */89public static void invalidAlg(String value) throws NoSuchPaddingException {90String[] arrays = value.split(":");9192try {93Security.setProperty(SEC_PREF_PROP, value);94Cipher.getInstance(arrays[0]);95} catch (NoSuchAlgorithmException e) {96System.out.println(97"Test Pass:Got NoSuchAlgorithmException as expired");98return;99}100throw new RuntimeException(101"Test Failed:Expected NoSuchAlgorithmException was not thrown");102}103104public static void main(String[] args)105throws NoSuchAlgorithmException, NoSuchPaddingException {106107String expected;108String value = args[1];109expected = "SunJCE";110111if (args.length >= 2) {112switch (args[0]) {113case "preSet":114boolean negativeProvider = Boolean.valueOf(args[2]);115if (!args[1].contains(":")) {116value += ":" + expected;117}118PreferredProviderNegativeTest.preJCESet(119value, negativeProvider);120break;121case "afterSet":122PreferredProviderNegativeTest.afterJCESet(args[1],123expected);124break;125case "invalidAlg":126PreferredProviderNegativeTest.invalidAlg(args[1]);127break;128}129} else {130throw new RuntimeException(131"Test Failed:Please pass the correct args");132}133}134}135136137