Path: blob/master/test/jdk/java/security/SecureRandom/Serialize.java
41152 views
/*1* Copyright (c) 1998, 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/* @test24* @bug 410289625* @summary Make sure that a SecureRandom object can be serialized26* @run main/othervm Serialize27*/2829import java.security.*;30import java.io.*;3132public class Serialize {3334public static void main(String args[]) throws Exception {35System.setProperty("java.security.egd", "file:/dev/urandom");3637for (String alg: new String[]{38"SHA1PRNG", "DRBG", "Hash_DRBG", "HMAC_DRBG", "CTR_DRBG",39"Hash_DRBG,SHA-512,192,pr_and_reseed"}) {4041System.out.println("Testing " + alg);42SecureRandom s1;4344// A SecureRandom can be s11ned and des11ned at any time.4546// Brand new.47s1 = getInstance(alg);48revive(s1).nextInt();49if (alg.contains("DRBG")) {50revive(s1).reseed();51}5253// Used54s1 = getInstance(alg);55s1.nextInt(); // state set56revive(s1).nextInt();57if (alg.contains("DRBG")) {58revive(s1).reseed();59}6061// Automatically reseeded62s1 = getInstance(alg);63if (alg.contains("DRBG")) {64s1.reseed();65}66revive(s1).nextInt();67if (alg.contains("DRBG")) {68revive(s1).reseed();69}7071// Manually seeded72s1 = getInstance(alg);73s1.setSeed(1L);74revive(s1).nextInt();75if (alg.contains("DRBG")) {76revive(s1).reseed();77}78}79}8081private static SecureRandom getInstance(String alg) throws Exception {82if (alg.equals("SHA1PRNG") || alg.equals("DRBG")) {83return SecureRandom.getInstance(alg);84} else {85String old = Security.getProperty("securerandom.drbg.config");86try {87Security.setProperty("securerandom.drbg.config", alg);88return SecureRandom.getInstance("DRBG");89} finally {90Security.setProperty("securerandom.drbg.config", old);91}92}93}9495private static SecureRandom revive(SecureRandom oldOne) throws Exception {96ByteArrayOutputStream bout = new ByteArrayOutputStream();97new ObjectOutputStream(bout).writeObject(oldOne);98SecureRandom newOne = (SecureRandom) new ObjectInputStream(99new ByteArrayInputStream(bout.toByteArray())).readObject();100if (!oldOne.toString().equals(newOne.toString())) {101throw new Exception(newOne + " is not " + oldOne);102}103return newOne;104}105}106107108