1 /* 2 * Copyright (c) 2015, 2020, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 import static java.lang.System.out; 25 import java.security.NoSuchAlgorithmException; 26 import java.security.SecureRandom; 27 28 /** 29 * @test 30 * @bug 8048356 31 * @summary Assert default provider used on all OS for SecureRandom 32 */ 33 public class DefaultProvider { 34 35 private static final String OS_NAME = System.getProperty("os.name"); 36 private static final String WINDOWS = "Windows"; 37 38 public static void main(String[] args) throws NoSuchAlgorithmException { 39 out.println("Operating System: " + OS_NAME); 40 41 /* Test default provider used with constructor */ 42 out.println("TEST: Default provider with constructor"); 43 SecureRandom secureRandom = new SecureRandom(); 44 String provider = secureRandom.getProvider().getName(); 45 if (!provider.equals("SUN")) { 46 throw new RuntimeException("Unexpected provider name: " 47 + provider); 48 } 49 out.println("Passed, default provider with constructor: " + provider); 50 51 /* Test default provider with getInstance(String algorithm) */ 52 out.println("TEST: SHA1PRNG supported on all platforms by SUN provider"); 53 String algorithm = "SHA1PRNG"; 54 provider = "SUN"; 55 56 SecureRandom instance = SecureRandom.getInstance(algorithm); 57 assertInstance(instance, algorithm, provider); 58 out.println("Passed."); 59 60 if (!OS_NAME.startsWith(WINDOWS)) { 61 out.println("TEST: NativePRNG supported on all platforms" 62 + "(except Windows), by SUN provider"); 63 algorithm = "NativePRNG"; 64 provider = "SUN"; 65 } else { 66 out.println( 67 "TEST: Windows-PRNG supported on windows by SunMSCAPI provider"); 68 algorithm = "Windows-PRNG"; 69 provider = "SunMSCAPI"; 70 } 71 instance = SecureRandom.getInstance(algorithm); 72 assertInstance(instance, algorithm, provider); 73 out.println("Passed."); 74 } 75 76 private static void assertInstance(SecureRandom instance, 77 String expectedAlgorithm, 78 String expectedProvider) { 79 if (instance != null) { 80 if (!expectedAlgorithm.equalsIgnoreCase(instance.getAlgorithm())) { 81 throw new RuntimeException("Expected algorithm:" 82 + expectedAlgorithm + " actual: " + instance.getAlgorithm()); 83 } 84 85 if (!expectedProvider.equalsIgnoreCase(instance.getProvider().getName())) { 86 throw new RuntimeException("Expected provider: " 87 + expectedProvider + " actual: " 88 + instance.getProvider().getName()); 89 } 90 } else { 91 throw new RuntimeException("Secure instance is not created"); 92 } 93 } 94 }