test/jdk/com/oracle/security/ucrypto/TestDigest.java
author aefimov
Thu, 07 Nov 2019 18:46:06 +0000
branchaefimov-dns-client-branch
changeset 58971 465a15dd6bed
parent 47216 71c04702a3d5
permissions -rw-r--r--
aefimov-dns-client-branch: Cleanup and address ANY type failures
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
     1
/*
37909
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 30046
diff changeset
     2
 * Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved.
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
     4
 *
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
     7
 * published by the Free Software Foundation.
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
     8
 *
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    13
 * accompanied this code).
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    14
 *
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    18
 *
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    21
 * questions.
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    22
 */
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    23
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    24
/*
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    25
 * @test
37909
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 30046
diff changeset
    26
 * @bug     7088989 8000415
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    27
 * @summary Ensure the various message digests works correctly
30046
cf2c86e1819e 8078334: Mark regression tests using randomness
darcy
parents: 27182
diff changeset
    28
 * @key randomness
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    29
 */
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    30
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    31
import java.io.*;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    32
import java.security.*;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    33
import java.security.spec.*;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    34
import java.util.*;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    35
import javax.crypto.*;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    36
import javax.crypto.spec.*;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    37
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    38
public class TestDigest extends UcryptoTest {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    39
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    40
    private static final String[] MD_ALGOS = {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    41
        "MD5",
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    42
        "SHA",
37909
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 30046
diff changeset
    43
        "SHA-224",
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    44
        "SHA-256",
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    45
        "SHA-384",
37909
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 30046
diff changeset
    46
        "SHA-512",
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 30046
diff changeset
    47
        "SHA3-224",
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 30046
diff changeset
    48
        "SHA3-256",
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 30046
diff changeset
    49
        "SHA3-384",
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 30046
diff changeset
    50
        "SHA3-512"
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    51
    };
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    52
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    53
    public static void main(String[] args) throws Exception {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    54
        main(new TestDigest(), null);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    55
    }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    56
37909
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 30046
diff changeset
    57
    public void doTest(Provider p) throws Exception {
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    58
        boolean testPassed = true;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    59
        byte[] msg = new byte[200];
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    60
        (new SecureRandom()).nextBytes(msg);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    61
        String interopProvName = "SUN";
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    62
37909
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 30046
diff changeset
    63
        MessageDigest md, md2;
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 30046
diff changeset
    64
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    65
        for (String a : MD_ALGOS) {
37909
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 30046
diff changeset
    66
            System.out.println("Testing " + a);
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    67
            try {
37909
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 30046
diff changeset
    68
                md = MessageDigest.getInstance(a, p);
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 30046
diff changeset
    69
            } catch (NoSuchAlgorithmException nsae) {
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 30046
diff changeset
    70
                System.out.println("=> Skip, unsupported");
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 30046
diff changeset
    71
                continue;
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 30046
diff changeset
    72
            }
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 30046
diff changeset
    73
            try {
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    74
                md2 = MessageDigest.getInstance(a, interopProvName);
37909
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 30046
diff changeset
    75
            } catch (NoSuchAlgorithmException nsae) {
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 30046
diff changeset
    76
                System.out.println("=> Skip, no interop provider found");
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 30046
diff changeset
    77
                continue;
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 30046
diff changeset
    78
            }
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    79
37909
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 30046
diff changeset
    80
            // Test Interoperability for update+digest calls
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 30046
diff changeset
    81
            for (int i = 0; i < 3; i++) {
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 30046
diff changeset
    82
                md.update(msg);
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 30046
diff changeset
    83
                byte[] digest = md.digest();
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 30046
diff changeset
    84
                md2.update(msg);
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 30046
diff changeset
    85
                byte[] digest2 = md2.digest();
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 30046
diff changeset
    86
                if (!Arrays.equals(digest, digest2)) {
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 30046
diff changeset
    87
                    System.out.println("DIFF1 FAILED at iter " + i);
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 30046
diff changeset
    88
                    testPassed = false;
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 30046
diff changeset
    89
                } else {
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 30046
diff changeset
    90
                    System.out.println("...diff1 test passed");
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 30046
diff changeset
    91
                }
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 30046
diff changeset
    92
            }
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    93
37909
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 30046
diff changeset
    94
            // Test Interoperability for digest calls
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 30046
diff changeset
    95
            md = MessageDigest.getInstance(a, p);
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 30046
diff changeset
    96
            md2 = MessageDigest.getInstance(a, interopProvName);
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    97
37909
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 30046
diff changeset
    98
            for (int i = 0; i < 3; i++) {
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    99
                byte[] digest = md.digest();
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   100
                byte[] digest2 = md2.digest();
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   101
                if (!Arrays.equals(digest, digest2)) {
37909
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 30046
diff changeset
   102
                    System.out.println("DIFF2 FAILED at iter " + i);
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   103
                    testPassed = false;
37909
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 30046
diff changeset
   104
                } else {
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 30046
diff changeset
   105
                    System.out.println("...diff2 test passed");
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   106
                }
37909
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 30046
diff changeset
   107
            }
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 30046
diff changeset
   108
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 30046
diff changeset
   109
            // Test Cloning functionality if supported
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 30046
diff changeset
   110
            md = MessageDigest.getInstance(a, p);
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 30046
diff changeset
   111
            try {
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 30046
diff changeset
   112
                md2 = (MessageDigest) md.clone(); // clone right after construction
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 30046
diff changeset
   113
            } catch (CloneNotSupportedException cnse) {
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 30046
diff changeset
   114
                System.out.println("...no clone support");
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 30046
diff changeset
   115
                continue;
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 30046
diff changeset
   116
            }
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 30046
diff changeset
   117
            byte[] digest = md.digest();
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 30046
diff changeset
   118
            byte[] digest2 = md2.digest();
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 30046
diff changeset
   119
            if (!Arrays.equals(digest, digest2)) {
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 30046
diff changeset
   120
                System.out.println("DIFF-3.1 FAILED");
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   121
                testPassed = false;
37909
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 30046
diff changeset
   122
            } else {
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 30046
diff changeset
   123
                System.out.println("...diff3.1 tests passed");
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 30046
diff changeset
   124
            }
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 30046
diff changeset
   125
            md.update(msg);
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 30046
diff changeset
   126
            md2 = (MessageDigest) md.clone(); // clone again after update call
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 30046
diff changeset
   127
            digest = md.digest();
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 30046
diff changeset
   128
            digest2 = md2.digest();
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 30046
diff changeset
   129
            if (!Arrays.equals(digest, digest2)) {
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 30046
diff changeset
   130
                System.out.println("DIFF-3.2 FAILED");
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 30046
diff changeset
   131
                testPassed = false;
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 30046
diff changeset
   132
            } else {
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 30046
diff changeset
   133
                System.out.println("...diff3.2 tests passed");
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 30046
diff changeset
   134
            }
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 30046
diff changeset
   135
            md2 = (MessageDigest) md.clone(); // clone after digest
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 30046
diff changeset
   136
            digest = md.digest();
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 30046
diff changeset
   137
            digest2 = md2.digest();
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 30046
diff changeset
   138
            if (!Arrays.equals(digest, digest2)) {
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 30046
diff changeset
   139
                System.out.println("DIFF-3.3 FAILED");
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 30046
diff changeset
   140
                testPassed = false;
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 30046
diff changeset
   141
            } else {
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 30046
diff changeset
   142
                System.out.println("...diff3.3 tests passed");
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   143
            }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   144
        }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   145
        if (!testPassed) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   146
            throw new RuntimeException("One or more MD test failed!");
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   147
        } else {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   148
            System.out.println("MD Tests Passed");
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   149
        }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   150
    }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   151
}