Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/security/SecureRandom/DefaultProvider.java
38821 views
/*1* Copyright (c) 2015, 2016, 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 SUNOS = "SunOS";36private static final String WINDOWS = "Windows";3738public static void main(String[] args) throws NoSuchAlgorithmException {39out.println("Operating System: " + OS_NAME);4041/* Test default provider used with constructor */42out.println("TEST: Default provider with constructor");43SecureRandom secureRandom = new SecureRandom();44String provider = secureRandom.getProvider().getName();45if (!provider.equals("SUN")) {46throw new RuntimeException("Unexpected provider name: "47+ provider);48}49out.println("Passed, default provider with constructor: " + provider);5051/* Test default provider with getInstance(String algorithm) */52out.println("TEST: SHA1PRNG supported on all platforms by SUN provider");53String algorithm = "SHA1PRNG";54provider = "SUN";5556SecureRandom instance = SecureRandom.getInstance(algorithm);57assertInstance(instance, algorithm, provider);58out.println("Passed.");5960if (!OS_NAME.startsWith(WINDOWS)) {61out.println("TEST: NativePRNG supported on all platforms"62+ "(except Windows), by SUN provider");63algorithm = "NativePRNG";64provider = "SUN";65} else {66out.println(67"TEST: Windows-PRNG supported on windows by SunMSCAPI provider");68algorithm = "Windows-PRNG";69provider = "SunMSCAPI";70}71instance = SecureRandom.getInstance(algorithm);72assertInstance(instance, algorithm, provider);73out.println("Passed.");74}7576private static void assertInstance(SecureRandom instance,77String expectedAlgorithm,78String expectedProvider) {79if (instance != null) {80if (!expectedAlgorithm.equalsIgnoreCase(instance.getAlgorithm())) {81throw new RuntimeException("Expected algorithm:"82+ expectedAlgorithm + " actual: " + instance.getAlgorithm());83}8485if (!expectedProvider.equalsIgnoreCase(instance.getProvider().getName())) {86throw new RuntimeException("Expected provider: "87+ expectedProvider + " actual: "88+ instance.getProvider().getName());89}90} else {91throw new RuntimeException("Secure instance is not created");92}93}94}959697