Path: blob/master/test/jdk/sun/security/provider/SecureRandom/StrongSecureRandom.java
41154 views
/*1* Copyright (c) 2013, 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*/2223/*24* @test25* @bug 6425477 814103926* @summary Better support for generation of high entropy random numbers27* @run main StrongSecureRandom28*/29import java.security.*;30import java.util.*;3132/**33* This test assumes that the standard Sun providers are installed.34*/35public class StrongSecureRandom {3637private static final String os = System.getProperty("os.name", "unknown");3839private static void testDefaultEgd() throws Exception {40// No SecurityManager installed.41String s = Security.getProperty("securerandom.source");4243System.out.println("Testing: default EGD: " + s);44if (!s.equals("file:/dev/random")) {45throw new Exception("Default is not 'file:/dev/random'");46}47}4849private static void testNativePRNGImpls() throws Exception {50SecureRandom sr;51byte[] ba;5253System.out.println("Testing new NativePRNGImpls");5455if (os.startsWith("Windows")) {56System.out.println("Skip windows testing.");57return;58}5960System.out.println("Testing regular");61sr = SecureRandom.getInstance("NativePRNG");62if (!sr.getAlgorithm().equals("NativePRNG")) {63throw new Exception("sr.getAlgorithm(): " + sr.getAlgorithm());64}65ba = sr.generateSeed(1);66sr.nextBytes(ba);67sr.setSeed(ba);6869System.out.println("Testing NonBlocking");70sr = SecureRandom.getInstance("NativePRNGNonBlocking");71if (!sr.getAlgorithm().equals("NativePRNGNonBlocking")) {72throw new Exception("sr.getAlgorithm(): " + sr.getAlgorithm());73}74ba = sr.generateSeed(1);75sr.nextBytes(ba);76sr.setSeed(ba);7778if (os.equals("Linux")) {79System.out.println("Skip Linux blocking test.");80return;81}8283System.out.println("Testing Blocking");84sr = SecureRandom.getInstance("NativePRNGBlocking");85if (!sr.getAlgorithm().equals("NativePRNGBlocking")) {86throw new Exception("sr.getAlgorithm(): " + sr.getAlgorithm());87}88ba = sr.generateSeed(1);89sr.nextBytes(ba);90sr.setSeed(ba);91}9293private static void testStrongInstance(boolean expected) throws Exception {9495boolean result;9697try {98SecureRandom.getInstanceStrong();99result = true;100} catch (NoSuchAlgorithmException e) {101result = false;102}103104if (expected != result) {105throw new Exception("Received: " + result);106}107}108109/*110* This test assumes that the standard providers are installed.111*/112private static void testProperty(String property, boolean expected)113throws Exception {114115System.out.println("Testing: '" + property + "' " + expected);116final String origStrongAlgoProp117= Security.getProperty("securerandom.strongAlgorithms");118try {119Security.setProperty("securerandom.strongAlgorithms", property);120testStrongInstance(expected);121} finally {122Security.setProperty(123"securerandom.strongAlgorithms", origStrongAlgoProp);124}125}126127private static void testProperties() throws Exception {128// Sets securerandom.strongAlgorithms, and then tests various combos.129testProperty("", false);130131testProperty("SHA1PRNG", true);132testProperty(" SHA1PRNG", true);133testProperty("SHA1PRNG ", true);134testProperty(" SHA1PRNG ", true);135136// Impls are case-insenstive, providers are sensitive.137testProperty("SHA1PRNG:SUN", true);138testProperty("Sha1PRNG:SUN", true);139testProperty("SHA1PRNG:Sun", false);140141testProperty(" SHA1PRNG:SUN", true);142testProperty("SHA1PRNG:SUN ", true);143testProperty(" SHA1PRNG:SUN ", true);144145testProperty(" SHA1PRNG:SUn", false);146testProperty("SHA1PRNG:SUn ", false);147testProperty(" SHA1PRNG:SUn ", false);148149testProperty(",,,SHA1PRNG", true);150testProperty(",,, SHA1PRNG", true);151testProperty(" , , ,SHA1PRNG ", true);152153testProperty(",,,, SHA1PRNG ,,,", true);154testProperty(",,,, SHA1PRNG:SUN ,,,", true);155testProperty(",,,, SHA1PRNG:SUn ,,,", false);156157testProperty(",,,SHA1PRNG:Sun,, SHA1PRNG:SUN", true);158testProperty(",,,Sha1PRNG:Sun, SHA1PRNG:SUN", true);159testProperty(" SHA1PRNG:Sun, Sha1PRNG:Sun,,,,Sha1PRNG:SUN", true);160161testProperty(",,,SHA1PRNG:Sun,, SHA1PRNG:SUn", false);162testProperty(",,,Sha1PRNG:Sun, SHA1PRNG:SUn", false);163testProperty(" SHA1PRNG:Sun, Sha1PRNG:Sun,,,,Sha1PRNG:SUn", false);164165testProperty(166" @#%,%$#:!%^, NativePRNG:Sun, Sha1PRNG:Sun,,Sha1PRNG:SUN",167true);168testProperty(" @#%,%$#!%^, NativePRNG:Sun, Sha1PRNG:Sun,,Sha1PRNG:SUn",169false);170}171172/*173* Linux tends to block, so ignore anything that reads /dev/random.174*/175private static void handleLinuxRead(SecureRandom sr) throws Exception {176if (os.equals("Linux")) {177if (!sr.getAlgorithm().equalsIgnoreCase("NativePRNGBlocking")) {178sr.nextBytes(new byte[34]);179}180} else {181sr.nextBytes(new byte[34]);182sr.generateSeed(34);183sr.setSeed(new byte[34]);184}185}186187/*188* This is duplicating stuff above, but just iterate over all impls189* just in case we missed something.190*/191private static void testAllImpls() throws Exception {192System.out.print("Testing: AllImpls: ");193194Iterator<String> i = Security.getAlgorithms("SecureRandom").iterator();195196while (i.hasNext()) {197String s = i.next();198System.out.print("/" + s);199SecureRandom sr = SecureRandom.getInstance(s);200201handleLinuxRead(sr);202handleLinuxRead(sr);203}204System.out.println("/");205}206207public static void main(String args[]) throws Exception {208testDefaultEgd();209210testNativePRNGImpls();211testAllImpls();212213// test default.214testStrongInstance(true);215testProperties();216}217}218219220