Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/security/SecureRandom/GetAlgorithm.java
38811 views
/*1* Copyright (c) 2003, 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 491539226* @summary test that the getAlgorithm() method works correctly27* @author Andreas Sterbenz28*/2930import java.io.*;3132import java.security.*;3334public class GetAlgorithm {3536private final static String BASE = System.getProperty("test.src", ".");3738public static void main(String[] args) throws Exception {39SecureRandom sr;4041sr = new SecureRandom();42if (sr.getAlgorithm().equals("unknown")) {43throw new Exception("Unknown: " + sr.getAlgorithm());44}4546sr = SecureRandom.getInstance("SHA1PRNG");47check("SHA1PRNG", sr);4849// OutputStream out = new FileOutputStream("sha1prng.bin");50// ObjectOutputStream oout = new ObjectOutputStream(out);51// sr.nextInt();52// oout.writeObject(sr);53// oout.flush();54// oout.close();5556sr = new MySecureRandom();57check("unknown", sr);5859InputStream in = new FileInputStream(new File(BASE, "sha1prng-old.bin"));60ObjectInputStream oin = new ObjectInputStream(in);61sr = (SecureRandom)oin.readObject();62oin.close();63check("unknown", sr);6465in = new FileInputStream(new File(BASE, "sha1prng-new.bin"));66oin = new ObjectInputStream(in);67sr = (SecureRandom)oin.readObject();68oin.close();69check("SHA1PRNG", sr);7071System.out.println("All tests passed");72}7374private static void check(String s1, SecureRandom sr) throws Exception {75String s2 = sr.getAlgorithm();76if (s1.equals(s2) == false) {77throw new Exception("Expected " + s1 + ", got " + s2);78}79}8081private static class MySecureRandom extends SecureRandom {8283}8485}868788