Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/provider/SecureRandom/StrongSecureRandom.java
38853 views
/*1* Copyright (c) 2013, 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 642547726* @summary Better support for generation of high entropy random numbers27* @run main/othervm 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 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 testSHA1PRNGImpl() throws Exception {50SecureRandom sr;51byte[] ba;5253String urandom = "file:/dev/urandom";5455System.out.println("Testing new SeedGenerator and EGD");5657Security.setProperty("securerandom.source", urandom);58if (!Security.getProperty("securerandom.source").equals(urandom)) {59throw new Exception("Couldn't set securerandom.source");60}6162/*63* Take out a large number of bytes in hopes of blocking.64* Don't expect this to happen, unless something is broken on Linux65*/66sr = SecureRandom.getInstance("SHA1PRNG");67if (!sr.getAlgorithm().equals("SHA1PRNG")) {68throw new Exception("sr.getAlgorithm(): " + sr.getAlgorithm());69}7071ba = sr.generateSeed(4096);72sr.nextBytes(ba);73sr.setSeed(ba);74}7576private static void testNativePRNGImpls() throws Exception {77SecureRandom sr;78byte[] ba;7980System.out.println("Testing new NativePRNGImpls");8182if (os.startsWith("Windows")) {83System.out.println("Skip windows testing.");84return;85}8687System.out.println(" Testing regular");88sr = SecureRandom.getInstance("NativePRNG");89if (!sr.getAlgorithm().equals("NativePRNG")) {90throw new Exception("sr.getAlgorithm(): " + sr.getAlgorithm());91}92ba = sr.generateSeed(1);93sr.nextBytes(ba);94sr.setSeed(ba);9596System.out.println(" Testing NonBlocking");97sr = SecureRandom.getInstance("NativePRNGNonBlocking");98if (!sr.getAlgorithm().equals("NativePRNGNonBlocking")) {99throw new Exception("sr.getAlgorithm(): " + sr.getAlgorithm());100}101ba = sr.generateSeed(1);102sr.nextBytes(ba);103sr.setSeed(ba);104105if (os.equals("Linux")) {106System.out.println("Skip Linux blocking test.");107return;108}109110System.out.println(" Testing Blocking");111sr = SecureRandom.getInstance("NativePRNGBlocking");112if (!sr.getAlgorithm().equals("NativePRNGBlocking")) {113throw new Exception("sr.getAlgorithm(): " + sr.getAlgorithm());114}115ba = sr.generateSeed(1);116sr.nextBytes(ba);117sr.setSeed(ba);118}119120private static void testStrongInstance(boolean expected) throws Exception {121122boolean result;123124try {125SecureRandom.getInstanceStrong();126result = true;127} catch (NoSuchAlgorithmException e) {128result = false;129}130131if (expected != result) {132throw new Exception("Received: " + result);133}134}135136/*137* This test assumes that the standard providers are installed.138*/139private static void testProperty(String property, boolean expected)140throws Exception {141142System.out.println("Testing: '" + property + "' " + expected);143144Security.setProperty("securerandom.strongAlgorithms", property);145testStrongInstance(expected);146}147148private static void testProperties() throws Exception {149// Sets securerandom.strongAlgorithms, and then tests various combos.150testProperty("", false);151152testProperty("SHA1PRNG", true);153testProperty(" SHA1PRNG", true);154testProperty("SHA1PRNG ", true);155testProperty(" SHA1PRNG ", true);156157// Impls are case-insenstive, providers are sensitive.158testProperty("SHA1PRNG:SUN", true);159testProperty("Sha1PRNG:SUN", true);160testProperty("SHA1PRNG:Sun", false);161162testProperty(" SHA1PRNG:SUN", true);163testProperty("SHA1PRNG:SUN ", true);164testProperty(" SHA1PRNG:SUN ", true);165166testProperty(" SHA1PRNG:SUn", false);167testProperty("SHA1PRNG:SUn ", false);168testProperty(" SHA1PRNG:SUn ", false);169170testProperty(",,,SHA1PRNG", true);171testProperty(",,, SHA1PRNG", true);172testProperty(" , , ,SHA1PRNG ", true);173174testProperty(",,,, SHA1PRNG ,,,", true);175testProperty(",,,, SHA1PRNG:SUN ,,,", true);176testProperty(",,,, SHA1PRNG:SUn ,,,", false);177178testProperty(",,,SHA1PRNG:Sun,, SHA1PRNG:SUN", true);179testProperty(",,,Sha1PRNG:Sun, SHA1PRNG:SUN", true);180testProperty(" SHA1PRNG:Sun, Sha1PRNG:Sun,,,,Sha1PRNG:SUN", true);181182testProperty(",,,SHA1PRNG:Sun,, SHA1PRNG:SUn", false);183testProperty(",,,Sha1PRNG:Sun, SHA1PRNG:SUn", false);184testProperty(" SHA1PRNG:Sun, Sha1PRNG:Sun,,,,Sha1PRNG:SUn", false);185186testProperty(187" @#%,%$#:!%^, NativePRNG:Sun, Sha1PRNG:Sun,,Sha1PRNG:SUN",188true);189testProperty(" @#%,%$#!%^, NativePRNG:Sun, Sha1PRNG:Sun,,Sha1PRNG:SUn",190false);191}192193/*194* Linux tends to block, so ignore anything that reads /dev/random.195*/196private static void handleLinuxRead(SecureRandom sr) throws Exception {197if (os.equals("Linux")) {198if (!sr.getAlgorithm().equalsIgnoreCase("NativePRNGBlocking")) {199sr.nextBytes(new byte[34]);200}201} else {202sr.nextBytes(new byte[34]);203sr.generateSeed(34);204sr.setSeed(new byte[34]);205}206}207208/*209* This is duplicating stuff above, but just iterate over all impls210* just in case we missed something.211*/212private static void testAllImpls() throws Exception {213System.out.print("Testing: AllImpls: ");214215Iterator<String> i = Security.getAlgorithms("SecureRandom").iterator();216217while (i.hasNext()) {218String s = i.next();219System.out.print("/" + s);220SecureRandom sr = SecureRandom.getInstance(s);221222handleLinuxRead(sr);223handleLinuxRead(sr);224}225System.out.println("/");226}227228public static void main(String args[]) throws Exception {229testDefaultEgd();230testSHA1PRNGImpl();231testNativePRNGImpls();232testAllImpls();233234// test default.235testStrongInstance(true);236testProperties();237}238}239240241