test/jdk/java/security/MessageDigest/TestDigestIOStream.java
author ssadetsky
Wed, 03 Apr 2019 15:43:25 -0700 (2019-04-03)
branchJDK-8200758-branch
changeset 57303 eff0cf668c29
parent 47216 71c04702a3d5
permissions -rw-r--r--
8221876:[macOS] JPackage install takes long time Reviewed-by: almatvee
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
26203
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
     1
/*
45288
58be10a068c2 8180805: move RandomFactory to the top level testlibrary
iignatyev
parents: 38761
diff changeset
     2
 * Copyright (c) 2003, 2017, Oracle and/or its affiliates. All rights reserved.
26203
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
     4
 *
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
     7
 * published by the Free Software Foundation.
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
     8
 *
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
    13
 * accompanied this code).
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
    14
 *
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
    18
 *
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
    21
 * questions.
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
    22
 */
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
    23
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
    24
import java.io.ByteArrayInputStream;
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
    25
import java.io.ByteArrayOutputStream;
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
    26
import java.io.PrintStream;
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
    27
import java.security.DigestInputStream;
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
    28
import java.security.DigestOutputStream;
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
    29
import java.security.MessageDigest;
38761
e31b47204f64 8156059: Update/Develop new tests for JEP 287: SHA-3 Hash Algorithms
amjiang
parents: 30046
diff changeset
    30
import java.security.NoSuchAlgorithmException;
e31b47204f64 8156059: Update/Develop new tests for JEP 287: SHA-3 Hash Algorithms
amjiang
parents: 30046
diff changeset
    31
import java.security.Security;
26203
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
    32
import java.util.Arrays;
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
    33
import java.util.Random;
45288
58be10a068c2 8180805: move RandomFactory to the top level testlibrary
iignatyev
parents: 38761
diff changeset
    34
import jdk.test.lib.RandomFactory;
26203
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
    35
import static java.lang.System.out;
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
    36
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
    37
/**
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
    38
 * @test
38761
e31b47204f64 8156059: Update/Develop new tests for JEP 287: SHA-3 Hash Algorithms
amjiang
parents: 30046
diff changeset
    39
 * @bug 8050370 8156059
26203
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
    40
 * @summary MessageDigest tests with DigestIOStream
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
    41
 * @author Kevin Liu
30046
cf2c86e1819e 8078334: Mark regression tests using randomness
darcy
parents: 26203
diff changeset
    42
 * @key randomness
45288
58be10a068c2 8180805: move RandomFactory to the top level testlibrary
iignatyev
parents: 38761
diff changeset
    43
 * @library /test/lib
45467
99c87a16a8e4 8181761: add explicit @build actions for jdk.test.lib classes in all :tier2 tests
iignatyev
parents: 45288
diff changeset
    44
 * @build jdk.test.lib.RandomFactory
38761
e31b47204f64 8156059: Update/Develop new tests for JEP 287: SHA-3 Hash Algorithms
amjiang
parents: 30046
diff changeset
    45
 * @run main/timeout=180 TestDigestIOStream
26203
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
    46
 */
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
    47
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
    48
enum ReadModel {
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
    49
    READ, BUFFER_READ, MIX_READ
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
    50
}
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
    51
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
    52
public class TestDigestIOStream {
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
    53
38761
e31b47204f64 8156059: Update/Develop new tests for JEP 287: SHA-3 Hash Algorithms
amjiang
parents: 30046
diff changeset
    54
    private static final int[] DATA_LEN_ARRAY = { 1, 50, 2500, 125000,
e31b47204f64 8156059: Update/Develop new tests for JEP 287: SHA-3 Hash Algorithms
amjiang
parents: 30046
diff changeset
    55
            6250000 };
e31b47204f64 8156059: Update/Develop new tests for JEP 287: SHA-3 Hash Algorithms
amjiang
parents: 30046
diff changeset
    56
    private static final String[] ALGORITHM_ARRAY = { "MD2", "MD5", "SHA1",
e31b47204f64 8156059: Update/Develop new tests for JEP 287: SHA-3 Hash Algorithms
amjiang
parents: 30046
diff changeset
    57
            "SHA-224", "SHA-256", "SHA-384", "SHA-512", "SHA3-224", "SHA3-256",
e31b47204f64 8156059: Update/Develop new tests for JEP 287: SHA-3 Hash Algorithms
amjiang
parents: 30046
diff changeset
    58
            "SHA3-384", "SHA3-512" };
26203
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
    59
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
    60
    private static byte[] data;
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
    61
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
    62
    private static MessageDigest md = null;
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
    63
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
    64
    public static void main(String argv[]) throws Exception {
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
    65
        TestDigestIOStream test = new TestDigestIOStream();
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
    66
        test.run();
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
    67
    }
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
    68
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
    69
    public void run() throws Exception {
38761
e31b47204f64 8156059: Update/Develop new tests for JEP 287: SHA-3 Hash Algorithms
amjiang
parents: 30046
diff changeset
    70
        for (String algorithm : ALGORITHM_ARRAY) {
e31b47204f64 8156059: Update/Develop new tests for JEP 287: SHA-3 Hash Algorithms
amjiang
parents: 30046
diff changeset
    71
            try {
e31b47204f64 8156059: Update/Develop new tests for JEP 287: SHA-3 Hash Algorithms
amjiang
parents: 30046
diff changeset
    72
                md = MessageDigest.getInstance(algorithm);
e31b47204f64 8156059: Update/Develop new tests for JEP 287: SHA-3 Hash Algorithms
amjiang
parents: 30046
diff changeset
    73
                for (int length : DATA_LEN_ARRAY) {
26203
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
    74
38761
e31b47204f64 8156059: Update/Develop new tests for JEP 287: SHA-3 Hash Algorithms
amjiang
parents: 30046
diff changeset
    75
                    Random rdm = RandomFactory.getRandom();
e31b47204f64 8156059: Update/Develop new tests for JEP 287: SHA-3 Hash Algorithms
amjiang
parents: 30046
diff changeset
    76
                    data = new byte[length];
e31b47204f64 8156059: Update/Develop new tests for JEP 287: SHA-3 Hash Algorithms
amjiang
parents: 30046
diff changeset
    77
                    rdm.nextBytes(data);
26203
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
    78
38761
e31b47204f64 8156059: Update/Develop new tests for JEP 287: SHA-3 Hash Algorithms
amjiang
parents: 30046
diff changeset
    79
                    if (!testMDChange(algorithm, length)) {
e31b47204f64 8156059: Update/Develop new tests for JEP 287: SHA-3 Hash Algorithms
amjiang
parents: 30046
diff changeset
    80
                        throw new RuntimeException("testMDChange failed at:"
e31b47204f64 8156059: Update/Develop new tests for JEP 287: SHA-3 Hash Algorithms
amjiang
parents: 30046
diff changeset
    81
                                + algorithm + "/" + length);
e31b47204f64 8156059: Update/Develop new tests for JEP 287: SHA-3 Hash Algorithms
amjiang
parents: 30046
diff changeset
    82
                    }
e31b47204f64 8156059: Update/Develop new tests for JEP 287: SHA-3 Hash Algorithms
amjiang
parents: 30046
diff changeset
    83
                    if (!testMDShare(algorithm, length)) {
e31b47204f64 8156059: Update/Develop new tests for JEP 287: SHA-3 Hash Algorithms
amjiang
parents: 30046
diff changeset
    84
                        throw new RuntimeException("testMDShare failed at:"
e31b47204f64 8156059: Update/Develop new tests for JEP 287: SHA-3 Hash Algorithms
amjiang
parents: 30046
diff changeset
    85
                                + algorithm + "/" + length);
e31b47204f64 8156059: Update/Develop new tests for JEP 287: SHA-3 Hash Algorithms
amjiang
parents: 30046
diff changeset
    86
                    }
e31b47204f64 8156059: Update/Develop new tests for JEP 287: SHA-3 Hash Algorithms
amjiang
parents: 30046
diff changeset
    87
                    for (ReadModel readModel : ReadModel.values()) {
e31b47204f64 8156059: Update/Develop new tests for JEP 287: SHA-3 Hash Algorithms
amjiang
parents: 30046
diff changeset
    88
                        // test Digest function when digest switch on
e31b47204f64 8156059: Update/Develop new tests for JEP 287: SHA-3 Hash Algorithms
amjiang
parents: 30046
diff changeset
    89
                        if (!testDigestOnOff(algorithm, readModel, true,
e31b47204f64 8156059: Update/Develop new tests for JEP 287: SHA-3 Hash Algorithms
amjiang
parents: 30046
diff changeset
    90
                                length)) {
e31b47204f64 8156059: Update/Develop new tests for JEP 287: SHA-3 Hash Algorithms
amjiang
parents: 30046
diff changeset
    91
                            throw new RuntimeException(
e31b47204f64 8156059: Update/Develop new tests for JEP 287: SHA-3 Hash Algorithms
amjiang
parents: 30046
diff changeset
    92
                                    "testDigestOn failed at:" + algorithm + "/"
e31b47204f64 8156059: Update/Develop new tests for JEP 287: SHA-3 Hash Algorithms
amjiang
parents: 30046
diff changeset
    93
                                            + length + "/" + readModel);
e31b47204f64 8156059: Update/Develop new tests for JEP 287: SHA-3 Hash Algorithms
amjiang
parents: 30046
diff changeset
    94
                        }
e31b47204f64 8156059: Update/Develop new tests for JEP 287: SHA-3 Hash Algorithms
amjiang
parents: 30046
diff changeset
    95
                        // test Digest function when digest switch off
e31b47204f64 8156059: Update/Develop new tests for JEP 287: SHA-3 Hash Algorithms
amjiang
parents: 30046
diff changeset
    96
                        if (!testDigestOnOff(algorithm, readModel, false,
e31b47204f64 8156059: Update/Develop new tests for JEP 287: SHA-3 Hash Algorithms
amjiang
parents: 30046
diff changeset
    97
                                length)) {
e31b47204f64 8156059: Update/Develop new tests for JEP 287: SHA-3 Hash Algorithms
amjiang
parents: 30046
diff changeset
    98
                            throw new RuntimeException(
e31b47204f64 8156059: Update/Develop new tests for JEP 287: SHA-3 Hash Algorithms
amjiang
parents: 30046
diff changeset
    99
                                    "testDigestOff failed at:" + algorithm + "/"
e31b47204f64 8156059: Update/Develop new tests for JEP 287: SHA-3 Hash Algorithms
amjiang
parents: 30046
diff changeset
   100
                                            + length + "/" + readModel);
e31b47204f64 8156059: Update/Develop new tests for JEP 287: SHA-3 Hash Algorithms
amjiang
parents: 30046
diff changeset
   101
                        }
e31b47204f64 8156059: Update/Develop new tests for JEP 287: SHA-3 Hash Algorithms
amjiang
parents: 30046
diff changeset
   102
                    }
26203
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   103
                }
38761
e31b47204f64 8156059: Update/Develop new tests for JEP 287: SHA-3 Hash Algorithms
amjiang
parents: 30046
diff changeset
   104
            } catch (NoSuchAlgorithmException nae) {
e31b47204f64 8156059: Update/Develop new tests for JEP 287: SHA-3 Hash Algorithms
amjiang
parents: 30046
diff changeset
   105
                if (algorithm.startsWith("SHA3") && !isSHA3supported()) {
e31b47204f64 8156059: Update/Develop new tests for JEP 287: SHA-3 Hash Algorithms
amjiang
parents: 30046
diff changeset
   106
                    continue;
e31b47204f64 8156059: Update/Develop new tests for JEP 287: SHA-3 Hash Algorithms
amjiang
parents: 30046
diff changeset
   107
                } else {
e31b47204f64 8156059: Update/Develop new tests for JEP 287: SHA-3 Hash Algorithms
amjiang
parents: 30046
diff changeset
   108
                    throw nae;
26203
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   109
                }
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   110
            }
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   111
        }
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   112
        int testNumber = ALGORITHM_ARRAY.length * ReadModel.values().length
38761
e31b47204f64 8156059: Update/Develop new tests for JEP 287: SHA-3 Hash Algorithms
amjiang
parents: 30046
diff changeset
   113
                * DATA_LEN_ARRAY.length * 2
e31b47204f64 8156059: Update/Develop new tests for JEP 287: SHA-3 Hash Algorithms
amjiang
parents: 30046
diff changeset
   114
                + ALGORITHM_ARRAY.length * DATA_LEN_ARRAY.length * 2;
26203
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   115
        out.println("All " + testNumber + " Tests Passed");
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   116
    }
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   117
38761
e31b47204f64 8156059: Update/Develop new tests for JEP 287: SHA-3 Hash Algorithms
amjiang
parents: 30046
diff changeset
   118
    // SHA-3 hash algorithms are only supported by "SUN" provider
e31b47204f64 8156059: Update/Develop new tests for JEP 287: SHA-3 Hash Algorithms
amjiang
parents: 30046
diff changeset
   119
    // and "OracleUcrypto" provider on Solaris 12.0 or later
e31b47204f64 8156059: Update/Develop new tests for JEP 287: SHA-3 Hash Algorithms
amjiang
parents: 30046
diff changeset
   120
    // This method checks if system supports SHA-3
e31b47204f64 8156059: Update/Develop new tests for JEP 287: SHA-3 Hash Algorithms
amjiang
parents: 30046
diff changeset
   121
    private boolean isSHA3supported() {
e31b47204f64 8156059: Update/Develop new tests for JEP 287: SHA-3 Hash Algorithms
amjiang
parents: 30046
diff changeset
   122
        if (Security.getProvider("SUN") != null) {
e31b47204f64 8156059: Update/Develop new tests for JEP 287: SHA-3 Hash Algorithms
amjiang
parents: 30046
diff changeset
   123
            return true;
e31b47204f64 8156059: Update/Develop new tests for JEP 287: SHA-3 Hash Algorithms
amjiang
parents: 30046
diff changeset
   124
        }
e31b47204f64 8156059: Update/Develop new tests for JEP 287: SHA-3 Hash Algorithms
amjiang
parents: 30046
diff changeset
   125
        if (Security.getProvider("OracleUcrypto") != null
e31b47204f64 8156059: Update/Develop new tests for JEP 287: SHA-3 Hash Algorithms
amjiang
parents: 30046
diff changeset
   126
                && "SunOS".equals(System.getProperty("os.name"))
e31b47204f64 8156059: Update/Develop new tests for JEP 287: SHA-3 Hash Algorithms
amjiang
parents: 30046
diff changeset
   127
                && System.getProperty("os.version").compareTo("5.12") >= 0) {
e31b47204f64 8156059: Update/Develop new tests for JEP 287: SHA-3 Hash Algorithms
amjiang
parents: 30046
diff changeset
   128
            return true;
e31b47204f64 8156059: Update/Develop new tests for JEP 287: SHA-3 Hash Algorithms
amjiang
parents: 30046
diff changeset
   129
        }
e31b47204f64 8156059: Update/Develop new tests for JEP 287: SHA-3 Hash Algorithms
amjiang
parents: 30046
diff changeset
   130
        return false;
e31b47204f64 8156059: Update/Develop new tests for JEP 287: SHA-3 Hash Algorithms
amjiang
parents: 30046
diff changeset
   131
    }
e31b47204f64 8156059: Update/Develop new tests for JEP 287: SHA-3 Hash Algorithms
amjiang
parents: 30046
diff changeset
   132
26203
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   133
    /**
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   134
     * Test DigestInputStream and DigestOutputStream digest function when digest
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   135
     * set on and off
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   136
     *
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   137
     * @param algo
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   138
     *            Message Digest algorithm
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   139
     * @param readModel
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   140
     *            which read method used(READ, BUFFER_READ, MIX_READ)
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   141
     * @param on
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   142
     *            digest switch(on and off)
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   143
     * @param dataLength
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   144
     *            plain test data length.
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   145
     * @exception Exception
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   146
     *                throw unexpected exception
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   147
     */
38761
e31b47204f64 8156059: Update/Develop new tests for JEP 287: SHA-3 Hash Algorithms
amjiang
parents: 30046
diff changeset
   148
    public boolean testDigestOnOff(String algo, ReadModel readModel, boolean on,
e31b47204f64 8156059: Update/Develop new tests for JEP 287: SHA-3 Hash Algorithms
amjiang
parents: 30046
diff changeset
   149
            int dataLength) throws Exception {
26203
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   150
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   151
        // Generate the DigestInputStream/DigestOutputStream object
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   152
        try (ByteArrayInputStream bais = new ByteArrayInputStream(data);
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   153
                DigestInputStream dis = new DigestInputStream(bais,
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   154
                        MessageDigest.getInstance(algo));
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   155
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   156
                DigestOutputStream dos = new DigestOutputStream(baos,
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   157
                        MessageDigest.getInstance(algo));
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   158
                ByteArrayOutputStream baOut = new ByteArrayOutputStream();) {
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   159
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   160
            // Perform the update using all available/possible update methods
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   161
            int k = 0;
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   162
            byte[] buffer = new byte[5];
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   163
            boolean enDigest = true;
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   164
            // Make sure the digest function is on (default)
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   165
            dis.on(enDigest);
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   166
            dos.on(enDigest);
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   167
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   168
            switch (readModel) {
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   169
            case READ: // use only read()
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   170
                while ((k = dis.read()) != -1) {
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   171
                    if (on) {
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   172
                        dos.write(k);
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   173
                    } else {
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   174
                        dos.write(k);
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   175
                        if (enDigest) {
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   176
                            baOut.write(k);
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   177
                        }
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   178
                        enDigest = !enDigest;
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   179
                        dos.on(enDigest);
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   180
                        dis.on(enDigest);
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   181
                    }
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   182
                }
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   183
                break;
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   184
            case BUFFER_READ: // use only read(byte[], int, int)
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   185
                while ((k = dis.read(buffer, 0, buffer.length)) != -1) {
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   186
                    if (on) {
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   187
                        dos.write(buffer, 0, k);
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   188
                    } else {
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   189
                        dos.write(buffer, 0, k);
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   190
                        if (enDigest) {
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   191
                            baOut.write(buffer, 0, k);
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   192
                        }
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   193
                        enDigest = !enDigest;
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   194
                        dis.on(enDigest);
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   195
                        dos.on(enDigest);
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   196
                    }
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   197
                }
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   198
                break;
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   199
            case MIX_READ: // use both read() and read(byte[], int, int)
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   200
                while ((k = dis.read()) != -1) {
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   201
                    if (on) {
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   202
                        dos.write(k);
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   203
                        if ((k = dis.read(buffer, 0, buffer.length)) != -1) {
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   204
                            dos.write(buffer, 0, k);
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   205
                        }
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   206
                    } else {
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   207
                        dos.write(k);
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   208
                        if (enDigest) {
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   209
                            baOut.write(k);
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   210
                        }
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   211
                        enDigest = !enDigest;
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   212
                        dis.on(enDigest);
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   213
                        dos.on(enDigest);
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   214
                        if ((k = dis.read(buffer, 0, buffer.length)) != -1) {
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   215
                            dos.write(buffer, 0, k);
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   216
                            if (enDigest) {
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   217
                                baOut.write(buffer, 0, k);
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   218
                            }
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   219
                            enDigest = !enDigest;
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   220
                            dis.on(enDigest);
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   221
                            dos.on(enDigest);
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   222
                        }
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   223
                    }
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   224
                }
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   225
                break;
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   226
            default:
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   227
                out.println("ERROR: Invalid read/write combination choice!");
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   228
                return false;
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   229
            }
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   230
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   231
            // Get the output and the "correct" digest values
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   232
            byte[] output1 = dis.getMessageDigest().digest();
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   233
            byte[] output2 = dos.getMessageDigest().digest();
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   234
            byte[] standard;
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   235
            if (on) {
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   236
                standard = md.digest(data);
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   237
            } else {
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   238
                byte[] dataDigested = baOut.toByteArray();
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   239
                standard = md.digest(dataDigested);
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   240
            }
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   241
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   242
            // Compare the output byte array value to the input data
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   243
            if (!MessageDigest.isEqual(data, baos.toByteArray())) {
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   244
                out.println("ERROR of " + readModel
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   245
                        + ": output and input data unexpectedly changed");
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   246
                return false;
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   247
            }
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   248
            // Compare generated digest values
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   249
            if (!MessageDigest.isEqual(output1, standard)
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   250
                    || !MessageDigest.isEqual(output2, standard)) {
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   251
                out.println("ERROR" + readModel
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   252
                        + ": generated digest data unexpectedly changed");
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   253
                return false;
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   254
            }
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   255
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   256
            return true;
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   257
        } catch (Exception ex) {
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   258
            out.println("testDigestOnOff failed at:" + algo + "/" + readModel
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   259
                    + "/" + dataLength + " with unexpected exception");
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   260
            throw ex;
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   261
        }
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   262
    }
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   263
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   264
    /**
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   265
     * Test DigestInputStream and DigestOutputStream digest function when Swap
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   266
     * the message digest engines between DigestIn/OutputStream
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   267
     *
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   268
     * @param algo
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   269
     *            Message Digest algorithm
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   270
     * @param dataLength
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   271
     *            plain test data length.
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   272
     * @exception Exception
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   273
     *                throw unexpected exception
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   274
     */
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   275
    public boolean testMDChange(String algo, int dataLength) throws Exception {
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   276
        // Generate the DigestInputStream/DigestOutputStream object
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   277
        MessageDigest mdIn = MessageDigest.getInstance(algo);
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   278
        MessageDigest mdOut = MessageDigest.getInstance(algo);
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   279
        try (ByteArrayInputStream bais = new ByteArrayInputStream(data);
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   280
                DigestInputStream dis = new DigestInputStream(bais, mdIn);
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   281
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   282
                DigestOutputStream dos = new DigestOutputStream(baos, mdOut);) {
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   283
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   284
            // Perform the update using all available/possible update methods
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   285
            int k = 0;
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   286
            byte[] buffer = new byte[10];
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   287
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   288
            // use both read() and read(byte[], int, int)
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   289
            while ((k = dis.read()) != -1) {
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   290
                dos.write(k);
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   291
                if ((k = dis.read(buffer, 0, buffer.length)) != -1) {
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   292
                    dos.write(buffer, 0, k);
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   293
                }
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   294
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   295
                // Swap the message digest engines between
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   296
                // DigestIn/OutputStream objects
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   297
                dis.setMessageDigest(mdOut);
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   298
                dos.setMessageDigest(mdIn);
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   299
                mdIn = dis.getMessageDigest();
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   300
                mdOut = dos.getMessageDigest();
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   301
            }
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   302
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   303
            // Get the output and the "correct" digest values
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   304
            byte[] output1 = mdIn.digest();
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   305
            byte[] output2 = mdOut.digest();
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   306
            byte[] standard = md.digest(data);
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   307
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   308
            // Compare generated digest values
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   309
            return MessageDigest.isEqual(output1, standard)
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   310
                    && MessageDigest.isEqual(output2, standard);
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   311
        } catch (Exception ex) {
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   312
            out.println("testMDChange failed at:" + algo + "/" + dataLength
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   313
                    + " with unexpected exception");
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   314
            throw ex;
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   315
        }
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   316
    }
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   317
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   318
    /**
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   319
     * Test DigestInputStream and DigestOutputStream digest function when use
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   320
     * same message digest object.
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   321
     *
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   322
     * @param algo
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   323
     *            Message Digest algorithm
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   324
     * @param dataLength
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   325
     *            plain test data length.
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   326
     * @exception Exception
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   327
     *                throw unexpected exception
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   328
     */
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   329
    public boolean testMDShare(String algo, int dataLength) throws Exception {
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   330
        MessageDigest mdCommon = MessageDigest.getInstance(algo);
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   331
        // Generate the DigestInputStream/DigestOutputStream object
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   332
        try (ByteArrayInputStream bais = new ByteArrayInputStream(data);
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   333
                DigestInputStream dis = new DigestInputStream(bais, mdCommon);
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   334
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
38761
e31b47204f64 8156059: Update/Develop new tests for JEP 287: SHA-3 Hash Algorithms
amjiang
parents: 30046
diff changeset
   335
                DigestOutputStream dos = new DigestOutputStream(baos,
e31b47204f64 8156059: Update/Develop new tests for JEP 287: SHA-3 Hash Algorithms
amjiang
parents: 30046
diff changeset
   336
                        mdCommon);) {
26203
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   337
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   338
            // Perform the update using all available/possible update methods
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   339
            int k = 0;
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   340
            byte[] buffer = new byte[10];
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   341
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   342
            // use both read() and read(byte[], int, int)
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   343
            while (k < data.length) {
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   344
                int len = dis.read(buffer, 0, buffer.length);
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   345
                if (len != -1) {
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   346
                    k += len;
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   347
                    if (k < data.length) {
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   348
                        dos.write(data[k]);
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   349
                        k++;
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   350
                        dis.skip(1);
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   351
                    }
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   352
                }
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   353
            }
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   354
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   355
            // Get the output and the "correct" digest values
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   356
            byte[] output = mdCommon.digest();
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   357
            byte[] standard = md.digest(data);
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   358
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   359
            // Compare generated digest values
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   360
            return MessageDigest.isEqual(output, standard);
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   361
        } catch (Exception ex) {
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   362
            out.println("TestMDShare failed at:" + algo + "/" + dataLength
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   363
                    + " with unexpected exception");
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   364
            throw ex;
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   365
        }
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   366
    }
0030579ec361 8050370: Need new regressions tests for messageDigest with DigestIOStream
weijun
parents:
diff changeset
   367
}