test/jdk/java/security/SecureRandom/SerializedSeedTest.java
author prr
Fri, 25 May 2018 12:12:24 -0700
changeset 50347 b2f046ae8eb6
parent 47216 71c04702a3d5
child 51840 dc15e45122b2
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
38423
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
     1
/*
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
     2
 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
     4
 *
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
     7
 * published by the Free Software Foundation.
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
     8
 *
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    13
 * accompanied this code).
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    14
 *
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    18
 *
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    21
 * questions.
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    22
 */
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    23
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    24
/*
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    25
 * @test
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    26
 * @bug 8141039
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    27
 * @library /lib/testlibrary
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    28
 * @summary When random number is generated through the a SecureRandom instance
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    29
 *          as well from it's serialized instance in the same time then the
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    30
 *          generated random numbers should be different when one or both are
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    31
 *          reseeded.
38447
435b76ccc9e3 8157417: Some of SecureRandom test might get timed out in linux
ssahoo
parents: 38423
diff changeset
    32
 * @run main/othervm -Djava.security.egd=file:/dev/urandom SerializedSeedTest
38423
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    33
 */
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    34
import java.io.ByteArrayOutputStream;
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    35
import java.io.IOException;
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    36
import java.io.ObjectInputStream;
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    37
import java.io.ObjectOutputStream;
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    38
import java.io.ByteArrayInputStream;
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    39
import java.security.NoSuchAlgorithmException;
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    40
import java.security.SecureRandom;
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    41
import java.security.Security;
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    42
import jdk.testlibrary.Asserts;
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    43
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    44
public class SerializedSeedTest {
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    45
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    46
    private static final byte[] SEED = "seed".getBytes();
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    47
    private static final String DRBG_CONFIG = "securerandom.drbg.config";
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    48
    private static final String DRBG_CONFIG_VALUE
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    49
            = Security.getProperty(DRBG_CONFIG);
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    50
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    51
    public static void main(String[] args) {
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    52
        boolean success = true;
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    53
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    54
        for (String mech : new String[]{
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    55
            "SHA1PRNG", "Hash_DRBG", "HMAC_DRBG", "CTR_DRBG"}) {
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    56
            System.out.printf(
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    57
                    "%nRunning test for SecureRandom mechanism: '%s'", mech);
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    58
            try {
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    59
                // Serialize without seed and compare generated random numbers
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    60
                // produced through original and serialized instances.
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    61
                SecureRandom orig = getSRInstance(mech);
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    62
                SecureRandom copy = deserializedCopy(orig);
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    63
                System.out.printf("%nSerialize without seed. Generated random"
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    64
                        + " numbers should be different.");
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    65
                check(orig, copy, false, mech);
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    66
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    67
                // Serialize after default seed and compare generated random
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    68
                // numbers produced through original and serialized instances.
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    69
                orig = getSRInstance(mech);
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    70
                orig.nextInt(); // Default seeded
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    71
                copy = deserializedCopy(orig);
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    72
                System.out.printf("%nSerialize after default seed. Generated"
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    73
                        + " random numbers should be same till 20-bytes.");
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    74
                check(orig, copy, !isDRBG(mech), mech);
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    75
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    76
                // Serialize after explicit seed and compare generated random
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    77
                // numbers produced through original and serialized instances.
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    78
                orig = getSRInstance(mech);
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    79
                orig.setSeed(SEED); // Explicitly seeded
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    80
                copy = deserializedCopy(orig);
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    81
                System.out.printf("%nSerialize after explicit seed. Generated "
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    82
                        + "random numbers should be same till 20-bytes.");
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    83
                check(orig, copy, !isDRBG(mech), mech);
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    84
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    85
                // Serialize without seed but original is explicitly seeded
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    86
                // before generating any random number. Then compare generated
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    87
                // random numbers produced through original and serialized
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    88
                // instances.
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    89
                orig = getSRInstance(mech);
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    90
                copy = deserializedCopy(orig);
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    91
                orig.setSeed(SEED); // Explicitly seeded
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    92
                System.out.printf("%nSerialize without seed. When original is "
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    93
                        + "explicitly seeded before generating random numbers,"
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    94
                        + " Generated random numbers should be different.");
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    95
                check(orig, copy, false, mech);
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    96
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    97
                // Serialize after default seed but original is explicitly
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    98
                // seeded before generating any random number. Then compare
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    99
                // generated random numbers produced through original and
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
   100
                // serialized instances.
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
   101
                orig = getSRInstance(mech);
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
   102
                orig.nextInt(); // Default seeded
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
   103
                copy = deserializedCopy(orig);
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
   104
                orig.setSeed(SEED); // Explicitly seeded
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
   105
                System.out.printf("%nSerialize after default seed but original "
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
   106
                        + "is explicitly seeded before generating random number"
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
   107
                        + ". Generated random numbers should be different.");
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
   108
                check(orig, copy, false, mech);
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
   109
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
   110
                // Serialize after explicit seed but original is explicitly
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
   111
                // seeded again before generating random number. Then compare
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
   112
                // generated random numbers produced through original and
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
   113
                // serialized instances.
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
   114
                orig = getSRInstance(mech);
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
   115
                orig.setSeed(SEED); // Explicitly seeded
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
   116
                copy = deserializedCopy(orig);
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
   117
                orig.setSeed(SEED); // Explicitly seeded
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
   118
                System.out.printf("%nSerialize after explicit seed but "
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
   119
                        + "original is explicitly seeded again before "
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
   120
                        + "generating random number. Generated random "
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
   121
                        + "numbers should be different.");
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
   122
                check(orig, copy, false, mech);
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
   123
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
   124
            } catch (Exception e) {
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
   125
                e.printStackTrace(System.out);
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
   126
                success = false;
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
   127
            } finally {
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
   128
                Security.setProperty(DRBG_CONFIG, DRBG_CONFIG_VALUE);
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
   129
            }
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
   130
            System.out.printf("%n------Completed Test for %s------", mech);
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
   131
        }
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
   132
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
   133
        if (!success) {
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
   134
            throw new RuntimeException("At least one test failed.");
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
   135
        }
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
   136
    }
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
   137
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
   138
    /**
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
   139
     * Find if the mechanism is a DRBG mechanism.
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
   140
     * @param mech Mechanism name
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
   141
     * @return True for DRBG mechanism else False
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
   142
     */
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
   143
    private static boolean isDRBG(String mech) {
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
   144
        return mech.contains("_DRBG");
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
   145
    }
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
   146
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
   147
    /**
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
   148
     * Verify the similarity of random numbers generated though both original
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
   149
     * as well as deserialized instance.
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
   150
     */
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
   151
    private static void check(SecureRandom orig, SecureRandom copy,
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
   152
            boolean equal, String mech) {
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
   153
        int o = orig.nextInt();
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
   154
        int c = copy.nextInt();
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
   155
        System.out.printf("%nRandom number generated for mechanism: '%s' "
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
   156
                + "from original instance as: '%s' and from serialized "
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
   157
                + "instance as: '%s'", mech, o, c);
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
   158
        if (equal) {
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
   159
            Asserts.assertEquals(o, c, mech);
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
   160
        } else {
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
   161
            Asserts.assertNotEquals(o, c, mech);
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
   162
        }
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
   163
    }
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
   164
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
   165
    /**
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
   166
     * Get a copy of SecureRandom instance through deserialization.
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
   167
     * @param orig Original SecureRandom instance
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
   168
     * @return Deserialized SecureRandom instance
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
   169
     * @throws IOException
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
   170
     * @throws ClassNotFoundException
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
   171
     */
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
   172
    private static SecureRandom deserializedCopy(SecureRandom orig)
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
   173
            throws IOException, ClassNotFoundException {
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
   174
        return deserialize(serialize(orig));
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
   175
    }
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
   176
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
   177
    /**
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
   178
     * Deserialize the SecureRandom object.
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
   179
     */
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
   180
    private static SecureRandom deserialize(byte[] serialized)
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
   181
            throws IOException, ClassNotFoundException {
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
   182
        SecureRandom sr = null;
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
   183
        try (ByteArrayInputStream bis = new ByteArrayInputStream(serialized);
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
   184
                ObjectInputStream ois = new ObjectInputStream(bis)) {
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
   185
            sr = (SecureRandom) ois.readObject();
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
   186
        }
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
   187
        return sr;
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
   188
    }
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
   189
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
   190
    /**
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
   191
     * Serialize the given SecureRandom object.
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
   192
     */
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
   193
    private static byte[] serialize(SecureRandom sr) throws IOException {
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
   194
        try (ByteArrayOutputStream bos = new ByteArrayOutputStream();
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
   195
                ObjectOutputStream oos = new ObjectOutputStream(bos)) {
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
   196
            oos.writeObject(sr);
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
   197
            return bos.toByteArray();
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
   198
        }
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
   199
    }
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
   200
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
   201
    /**
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
   202
     * Create a SecureRandom instance for a given mechanism.
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
   203
     */
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
   204
    private static SecureRandom getSRInstance(String mech)
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
   205
            throws NoSuchAlgorithmException {
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
   206
        if (!isDRBG(mech)) {
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
   207
            return SecureRandom.getInstance(mech);
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
   208
        } else {
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
   209
            Security.setProperty(DRBG_CONFIG, mech);
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
   210
            return SecureRandom.getInstance("DRBG");
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
   211
        }
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
   212
    }
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
   213
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
   214
}