test/jdk/java/security/SecureRandom/Serialize.java
changeset 47216 71c04702a3d5
parent 38437 0401899aa994
equal deleted inserted replaced
47215:4ebc2e2fb97c 47216:71c04702a3d5
       
     1 /*
       
     2  * Copyright (c) 1998, 2016, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.
       
     8  *
       
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    12  * version 2 for more details (a copy is included in the LICENSE file that
       
    13  * accompanied this code).
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License version
       
    16  * 2 along with this work; if not, write to the Free Software Foundation,
       
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    18  *
       
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    20  * or visit www.oracle.com if you need additional information or have any
       
    21  * questions.
       
    22  */
       
    23 
       
    24 /* @test
       
    25  * @bug 4102896
       
    26  * @summary Make sure that a SecureRandom object can be serialized
       
    27  * @run main/othervm Serialize
       
    28  */
       
    29 
       
    30 import java.security.*;
       
    31 import java.io.*;
       
    32 
       
    33 public class Serialize {
       
    34 
       
    35     public static void main(String args[]) throws Exception {
       
    36         System.setProperty("java.security.egd", "file:/dev/urandom");
       
    37 
       
    38         for (String alg: new String[]{
       
    39                 "SHA1PRNG", "DRBG", "Hash_DRBG", "HMAC_DRBG", "CTR_DRBG",
       
    40                 "Hash_DRBG,SHA-512,192,pr_and_reseed"}) {
       
    41 
       
    42             System.out.println("Testing " + alg);
       
    43             SecureRandom s1;
       
    44 
       
    45             // A SecureRandom can be s11ned and des11ned at any time.
       
    46 
       
    47             // Brand new.
       
    48             s1 = getInstance(alg);
       
    49             revive(s1).nextInt();
       
    50             if (alg.contains("DRBG")) {
       
    51                 revive(s1).reseed();
       
    52             }
       
    53 
       
    54             // Used
       
    55             s1 = getInstance(alg);
       
    56             s1.nextInt();    // state set
       
    57             revive(s1).nextInt();
       
    58             if (alg.contains("DRBG")) {
       
    59                 revive(s1).reseed();
       
    60             }
       
    61 
       
    62             // Automatically reseeded
       
    63             s1 = getInstance(alg);
       
    64             if (alg.contains("DRBG")) {
       
    65                 s1.reseed();
       
    66             }
       
    67             revive(s1).nextInt();
       
    68             if (alg.contains("DRBG")) {
       
    69                 revive(s1).reseed();
       
    70             }
       
    71 
       
    72             // Manually seeded
       
    73             s1 = getInstance(alg);
       
    74             s1.setSeed(1L);
       
    75             revive(s1).nextInt();
       
    76             if (alg.contains("DRBG")) {
       
    77                 revive(s1).reseed();
       
    78             }
       
    79         }
       
    80     }
       
    81 
       
    82     private static SecureRandom getInstance(String alg) throws Exception {
       
    83         if (alg.equals("SHA1PRNG") || alg.equals("DRBG")) {
       
    84             return SecureRandom.getInstance(alg);
       
    85         } else {
       
    86             String old = Security.getProperty("securerandom.drbg.config");
       
    87             try {
       
    88                 Security.setProperty("securerandom.drbg.config", alg);
       
    89                 return SecureRandom.getInstance("DRBG");
       
    90             } finally {
       
    91                 Security.setProperty("securerandom.drbg.config", old);
       
    92             }
       
    93         }
       
    94     }
       
    95 
       
    96     private static SecureRandom revive(SecureRandom oldOne) throws Exception {
       
    97         ByteArrayOutputStream bout = new ByteArrayOutputStream();
       
    98         new ObjectOutputStream(bout).writeObject(oldOne);
       
    99         SecureRandom newOne = (SecureRandom) new ObjectInputStream(
       
   100                 new ByteArrayInputStream(bout.toByteArray())).readObject();
       
   101         if (!oldOne.toString().equals(newOne.toString())) {
       
   102             throw new Exception(newOne + " is not " + oldOne);
       
   103         }
       
   104         return newOne;
       
   105     }
       
   106 }