Path: blob/master/test/jdk/java/security/SecureRandom/EnoughSeedTest.java
41149 views
/*1* Copyright (c) 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 814103926* @library /lib/testlibrary27* @summary Check SecureRandom generate expected seed counts what the caller28* asked for.29* @run main/othervm EnoughSeedTest30*/31import java.security.SecureRandom;32import java.security.Security;33import static java.lang.Math.*;3435public class EnoughSeedTest {3637private static final String DRBG_CONFIG = "securerandom.drbg.config";38private static final String DRBG_CONFIG_VALUE39= Security.getProperty(DRBG_CONFIG);4041public static void main(String[] args) {42System.setProperty("java.security.egd", "file:/dev/urandom");4344boolean success = true;45for (String mech : new String[]{46"SHA1PRNG", "Hash_DRBG", "HMAC_DRBG", "CTR_DRBG"}) {47System.out.printf("%nTest for SecureRandom algorithm: '%s'", mech);48try {49SecureRandom sr = null;50if (!mech.contains("_DRBG")) {51sr = SecureRandom.getInstance(mech);52} else {53Security.setProperty(DRBG_CONFIG, mech);54sr = SecureRandom.getInstance("DRBG");55}5657success &= forEachSeedBytes(sr);58System.out.printf("%nCompleted test for SecureRandom "59+ "mechanism: '%s'", mech);60} catch (Exception e) {61success &= false;62e.printStackTrace(System.out);63} finally {64Security.setProperty(DRBG_CONFIG, DRBG_CONFIG_VALUE);65}66}67if (!success) {68throw new RuntimeException("At least one test failed.");69}70}7172/**73* Generates fixed number of seed bytes through a SecureRandom instance74* to verify it's seed generation status.75* @param sr SecureRandom instance76* @return The test success indicator77*/78private static boolean forEachSeedBytes(SecureRandom sr) {79boolean success = true;80sr.setSeed(1l);81for (int seedByte : new int[]{Integer.MIN_VALUE, -1, 0, 1, 256, 1024,82Short.MAX_VALUE, (int) pow(2, 20)}) {83try {84byte[] seed = sr.generateSeed(seedByte);85if (seed.length != seedByte) {86throw new RuntimeException("Not able to produce expected "87+ "seed size.");88}89} catch (IllegalArgumentException e) {90if (seedByte >= 0) {91throw new RuntimeException("Unknown Exception occured.", e);92}93System.out.printf("%nPASS - Exception expected when required "94+ "seed size requested is negative: %s", seedByte);95}96}97return success;98}99100}101102103