test/jdk/java/security/SecureRandom/EnoughSeedTest.java
author prr
Fri, 25 May 2018 12:12:24 -0700
changeset 50347 b2f046ae8eb6
parent 47216 71c04702a3d5
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 Check SecureRandom generate expected seed counts what the caller
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    29
 *          asked for.
38437
0401899aa994 8157344: Multiple test timeouts after push for JDK-8141039
asmotrak
parents: 38423
diff changeset
    30
 * @run main/othervm EnoughSeedTest
38423
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    31
 */
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    32
import java.security.SecureRandom;
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    33
import java.security.Security;
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    34
import static java.lang.Math.*;
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    35
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    36
public class EnoughSeedTest {
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    37
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    38
    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
    39
    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
    40
            = Security.getProperty(DRBG_CONFIG);
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    41
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    42
    public static void main(String[] args) {
38437
0401899aa994 8157344: Multiple test timeouts after push for JDK-8141039
asmotrak
parents: 38423
diff changeset
    43
        System.setProperty("java.security.egd", "file:/dev/urandom");
38423
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    44
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    45
        boolean success = true;
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    46
        for (String mech : new String[]{
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    47
            "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
    48
            System.out.printf("%nTest for SecureRandom algorithm: '%s'", mech);
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    49
            try {
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    50
                SecureRandom sr = null;
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    51
                if (!mech.contains("_DRBG")) {
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    52
                    sr = SecureRandom.getInstance(mech);
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    53
                } else {
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    54
                    Security.setProperty(DRBG_CONFIG, mech);
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    55
                    sr = SecureRandom.getInstance("DRBG");
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    56
                }
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    57
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    58
                success &= forEachSeedBytes(sr);
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    59
                System.out.printf("%nCompleted test for SecureRandom "
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    60
                        + "mechanism: '%s'", mech);
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    61
            } catch (Exception e) {
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    62
                success &= false;
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    63
                e.printStackTrace(System.out);
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    64
            } finally {
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    65
                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
    66
            }
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    67
        }
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    68
        if (!success) {
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    69
            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
    70
        }
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    71
    }
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    72
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    73
    /**
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    74
     * Generates fixed number of seed bytes through a SecureRandom instance
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    75
     * to verify it's seed generation status.
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    76
     * @param sr SecureRandom instance
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    77
     * @return The test success indicator
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    78
     */
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    79
    private static boolean forEachSeedBytes(SecureRandom sr) {
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    80
        boolean success = true;
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    81
        sr.setSeed(1l);
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    82
        for (int seedByte : new int[]{Integer.MIN_VALUE, -1, 0, 1, 256, 1024,
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    83
            Short.MAX_VALUE, (int) pow(2, 20)}) {
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    84
            try {
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    85
                byte[] seed = sr.generateSeed(seedByte);
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    86
                if (seed.length != seedByte) {
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    87
                    throw new RuntimeException("Not able to produce expected "
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    88
                            + "seed size.");
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    89
                }
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    90
            } catch (IllegalArgumentException e) {
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    91
                if (seedByte >= 0) {
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    92
                    throw new RuntimeException("Unknown Exception occured.", e);
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    93
                }
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    94
                System.out.printf("%nPASS - Exception expected when required "
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    95
                        + "seed size requested is negative: %s", seedByte);
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
        }
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    98
        return success;
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
    99
    }
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
   100
5d6ff7d76bf4 8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff changeset
   101
}