Path: blob/master/test/jdk/java/security/SecureRandom/GetAlgorithm.java
41149 views
/*1* Copyright (c) 2003, 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 4915392 814103926* @summary test that the getAlgorithm() method works correctly27* @author Andreas Sterbenz28* @run main GetAlgorithm29*/30import java.io.*;31import java.security.*;32import java.util.Arrays;33import java.util.List;3435public class GetAlgorithm {3637private static final String BASE = System.getProperty("test.src", ".");38private static final String DRBG_CONFIG = "securerandom.drbg.config";39private static final String DRBG_CONFIG_VALUE40= Security.getProperty(DRBG_CONFIG);4142public static void main(String[] args) throws Exception {43SecureRandom sr = new SecureRandom();44if (sr.getAlgorithm().equals("unknown")) {45throw new Exception("Unknown: " + sr.getAlgorithm());46}4748for (String mech : new String[]{supportedNativeAlgo(), "SHA1PRNG",49"Hash_DRBG", "HMAC_DRBG", "CTR_DRBG"}) {50if (!mech.contains("_DRBG")) {51check(mech, SecureRandom.getInstance(mech));52} else {53try {54Security.setProperty(DRBG_CONFIG, mech);55check("DRBG", SecureRandom.getInstance("DRBG"));56} finally {57Security.setProperty(DRBG_CONFIG, DRBG_CONFIG_VALUE);58}59}60}61check("unknown", new MySecureRandom());6263InputStream in = new FileInputStream(64new File(BASE, "sha1prng-old.bin"));65ObjectInputStream oin = new ObjectInputStream(in);66sr = (SecureRandom) oin.readObject();67oin.close();68check("unknown", sr);6970in = new FileInputStream(new File(BASE, "sha1prng-new.bin"));71oin = new ObjectInputStream(in);72sr = (SecureRandom) oin.readObject();73oin.close();74check("SHA1PRNG", sr);7576System.out.println("All tests passed");77}7879private static void check(String s1, SecureRandom sr) throws Exception {80String s2 = sr.getAlgorithm();81if (s1.equals(s2) == false) {82throw new Exception("Expected " + s1 + ", got " + s2);83}84}8586private static class MySecureRandom extends SecureRandom {8788}8990/**91* Find the name of supported native mechanism name for current platform.92*/93private static String supportedNativeAlgo() {94String nativeSr = "Windows-PRNG";95try {96SecureRandom.getInstance(nativeSr);97} catch (NoSuchAlgorithmException e) {98nativeSr = "NativePRNG";99}100return nativeSr;101}102103}104105106