Path: blob/master/test/jdk/java/security/SecureRandom/DefaultProvider.java
41152 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 static java.lang.System.out;24import java.security.NoSuchAlgorithmException;25import java.security.SecureRandom;2627/**28* @test29* @bug 804835630* @summary Assert default provider used on all OS for SecureRandom31*/32public class DefaultProvider {3334private static final String OS_NAME = System.getProperty("os.name");35private static final String WINDOWS = "Windows";3637public static void main(String[] args) throws NoSuchAlgorithmException {38out.println("Operating System: " + OS_NAME);3940/* Test default provider used with constructor */41out.println("TEST: Default provider with constructor");42SecureRandom secureRandom = new SecureRandom();43String provider = secureRandom.getProvider().getName();44if (!provider.equals("SUN")) {45throw new RuntimeException("Unexpected provider name: "46+ provider);47}48out.println("Passed, default provider with constructor: " + provider);4950/* Test default provider with getInstance(String algorithm) */51out.println("TEST: SHA1PRNG supported on all platforms by SUN provider");52String algorithm = "SHA1PRNG";53provider = "SUN";5455SecureRandom instance = SecureRandom.getInstance(algorithm);56assertInstance(instance, algorithm, provider);57out.println("Passed.");5859if (!OS_NAME.startsWith(WINDOWS)) {60out.println("TEST: NativePRNG supported on all platforms"61+ "(except Windows), by SUN provider");62algorithm = "NativePRNG";63provider = "SUN";64} else {65out.println(66"TEST: Windows-PRNG supported on windows by SunMSCAPI provider");67algorithm = "Windows-PRNG";68provider = "SunMSCAPI";69}70instance = SecureRandom.getInstance(algorithm);71assertInstance(instance, algorithm, provider);72out.println("Passed.");73}7475private static void assertInstance(SecureRandom instance,76String expectedAlgorithm,77String expectedProvider) {78if (instance != null) {79if (!expectedAlgorithm.equalsIgnoreCase(instance.getAlgorithm())) {80throw new RuntimeException("Expected algorithm:"81+ expectedAlgorithm + " actual: " + instance.getAlgorithm());82}8384if (!expectedProvider.equalsIgnoreCase(instance.getProvider().getName())) {85throw new RuntimeException("Expected provider: "86+ expectedProvider + " actual: "87+ instance.getProvider().getName());88}89} else {90throw new RuntimeException("Secure instance is not created");91}92}93}949596