1 /* 2 * Copyright (c) 2015, 2016, 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 SUNOS = "SunOS"; 37 private static final String WINDOWS = "Windows"; 38 39 public static void main(String[] args) throws NoSuchAlgorithmException { 40 out.println("Operating System: " + OS_NAME); 41 42 /* Test default provider used with constructor */ 43 out.println("TEST: Default provider with constructor"); 44 SecureRandom secureRandom = new SecureRandom(); 45 String provider = secureRandom.getProvider().getName(); 46 if (!provider.equals("SUN")) { 47 throw new RuntimeException("Unexpected provider name: " 48 + provider); 49 } 50 out.println("Passed, default provider with constructor: " + provider); 51 52 /* Test default provider with getInstance(String algorithm) */ 53 out.println("TEST: SHA1PRNG supported on all platforms by SUN provider"); 54 String algorithm = "SHA1PRNG"; 55 provider = "SUN"; 56 57 SecureRandom instance = SecureRandom.getInstance(algorithm); 58 assertInstance(instance, algorithm, provider); 59 out.println("Passed."); 60 61 if (!OS_NAME.startsWith(WINDOWS)) { 62 out.println("TEST: NativePRNG supported on all platforms" 63 + "(except Windows), by SUN provider"); 64 algorithm = "NativePRNG"; 65 provider = "SUN"; 66 } else { 67 out.println( 68 "TEST: Windows-PRNG supported on windows by SunMSCAPI provider"); 69 algorithm = "Windows-PRNG"; 70 provider = "SunMSCAPI"; 71 } 72 instance = SecureRandom.getInstance(algorithm); 73 assertInstance(instance, algorithm, provider); 74 out.println("Passed."); 75 } 76 77 private static void assertInstance(SecureRandom instance, 78 String expectedAlgorithm, 79 String expectedProvider) { 80 if (instance != null) { 81 if (!expectedAlgorithm.equalsIgnoreCase(instance.getAlgorithm())) { 82 throw new RuntimeException("Expected algorithm:" 83 + expectedAlgorithm + " actual: " + instance.getAlgorithm()); 84 } 85 86 if (!expectedProvider.equalsIgnoreCase(instance.getProvider().getName())) { 87 throw new RuntimeException("Expected provider: " 88 + expectedProvider + " actual: " 89 + instance.getProvider().getName()); 90 } 91 } else { 92 throw new RuntimeException("Secure instance is not created"); 93 } 94 } 95 }