jdk/test/sun/security/pkcs11/MessageDigest/TestCloning.java
author stefank
Mon, 04 Apr 2016 09:15:01 +0200
changeset 37241 b9961c99c356
parent 35379 1e8e336ef66b
child 40975 680639c9b307
permissions -rw-r--r--
8152538: UL doesn't inline the LogTagSet::is_level check Reviewed-by: mlarsson, brutisso
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
11507
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
     1
/*
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30046
diff changeset
     2
 * Copyright (c) 2012, 2016, Oracle and/or its affiliates. All rights reserved.
11507
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
     4
 *
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
     7
 * published by the Free Software Foundation.
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
     8
 *
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
    13
 * accompanied this code).
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
    14
 *
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
    18
 *
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
    21
 * questions.
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
    22
 */
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
    23
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
    24
/**
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
    25
 * @test
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
    26
 * @bug 6414899
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
    27
 * @summary Ensure the cloning functionality works.
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
    28
 * @author Valerie Peng
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
    29
 * @library ..
30046
cf2c86e1819e 8078334: Mark regression tests using randomness
darcy
parents: 12685
diff changeset
    30
 * @key randomness
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30046
diff changeset
    31
 * @run main/othervm TestCloning
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30046
diff changeset
    32
 * @run main/othervm TestCloning sm
11507
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
    33
 */
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
    34
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30046
diff changeset
    35
import java.security.MessageDigest;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30046
diff changeset
    36
import java.security.Provider;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30046
diff changeset
    37
import java.util.Arrays;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30046
diff changeset
    38
import java.util.Random;
11507
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
    39
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
    40
public class TestCloning extends PKCS11Test {
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
    41
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
    42
    private static final String[] ALGOS = {
12685
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 11507
diff changeset
    43
        "MD2", "MD5", "SHA1", "SHA-224", "SHA-256", "SHA-384", "SHA-512"
11507
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
    44
    };
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
    45
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
    46
    public static void main(String[] args) throws Exception {
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30046
diff changeset
    47
        main(new TestCloning(), args);
11507
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
    48
    }
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
    49
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
    50
    private static final byte[] data1 = new byte[10];
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
    51
    private static final byte[] data2 = new byte[10*1024];
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
    52
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
    53
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30046
diff changeset
    54
    @Override
11507
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
    55
    public void main(Provider p) throws Exception {
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
    56
        Random r = new Random();
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
    57
        byte[] data1 = new byte[10];
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
    58
        byte[] data2 = new byte[2*1024];
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
    59
        r.nextBytes(data1);
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
    60
        r.nextBytes(data2);
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
    61
        System.out.println("Testing against provider " + p.getName());
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
    62
        for (int i = 0; i < ALGOS.length; i++) {
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
    63
            if (p.getService("MessageDigest", ALGOS[i]) == null) {
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
    64
                System.out.println(ALGOS[i] + " is not supported, skipping");
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
    65
                continue;
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
    66
            } else {
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
    67
                System.out.println("Testing " + ALGOS[i] + " of " + p.getName());
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
    68
                MessageDigest md = MessageDigest.getInstance(ALGOS[i], p);
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
    69
                try {
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
    70
                    md = testCloning(md, p);
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
    71
                    // repeat the test again after generating digest once
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
    72
                    for (int j = 0; j < 10; j++) {
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
    73
                        md = testCloning(md, p);
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
    74
                    }
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
    75
                } catch (Exception ex) {
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
    76
                    if (ALGOS[i] == "MD2" &&
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
    77
                        p.getName().equalsIgnoreCase("SunPKCS11-NSS")) {
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
    78
                        // known bug in NSS; ignore for now
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
    79
                        System.out.println("Ignore Known bug in MD2 of NSS");
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
    80
                        continue;
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
    81
                    }
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
    82
                    throw ex;
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
    83
                }
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
    84
            }
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
    85
        }
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
    86
    }
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
    87
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
    88
    private static MessageDigest testCloning(MessageDigest mdObj, Provider p)
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
    89
        throws Exception {
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
    90
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
    91
        // copy#0: clone at state BLANK w/o any data
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
    92
        MessageDigest mdCopy0 = (MessageDigest) mdObj.clone();
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
    93
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
    94
        // copy#1: clone again at state BUFFERED w/ very short data
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
    95
        mdObj.update(data1);
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
    96
        mdCopy0.update(data1);
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
    97
        MessageDigest mdCopy1 = (MessageDigest) mdObj.clone();
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
    98
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
    99
        // copy#2: clone again after updating it w/ long data to trigger
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
   100
        // the state into INIT
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
   101
        mdObj.update(data2);
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
   102
        mdCopy0.update(data2);
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
   103
        mdCopy1.update(data2);
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
   104
        MessageDigest mdCopy2 = (MessageDigest) mdObj.clone();
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
   105
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
   106
        // copy#3: clone again after updating it w/ very short data
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
   107
        mdObj.update(data1);
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
   108
        mdCopy0.update(data1);
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
   109
        mdCopy1.update(data1);
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
   110
        mdCopy2.update(data1);
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
   111
        MessageDigest mdCopy3 = (MessageDigest) mdObj.clone();
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
   112
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
   113
        // copy#4: clone again after updating it w/ long data
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
   114
        mdObj.update(data2);
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
   115
        mdCopy0.update(data2);
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
   116
        mdCopy1.update(data2);
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
   117
        mdCopy2.update(data2);
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
   118
        mdCopy3.update(data2);
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
   119
        MessageDigest mdCopy4 = (MessageDigest) mdObj.clone();
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
   120
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
   121
        // check digest equalities
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
   122
        byte[] answer = mdObj.digest();
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
   123
        byte[] result0 = mdCopy0.digest();
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
   124
        byte[] result1 = mdCopy1.digest();
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
   125
        byte[] result2 = mdCopy2.digest();
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
   126
        byte[] result3 = mdCopy3.digest();
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
   127
        byte[] result4 = mdCopy4.digest();
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
   128
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
   129
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
   130
        check(answer, result0, "copy0");
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
   131
        check(answer, result1, "copy1");
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
   132
        check(answer, result2, "copy2");
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
   133
        check(answer, result3, "copy3");
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
   134
        check(answer, result4, "copy4");
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
   135
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
   136
        return mdCopy3;
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
   137
    }
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
   138
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
   139
    private static void check(byte[] d1, byte[] d2, String copyName)
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
   140
            throws Exception {
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
   141
        if (Arrays.equals(d1, d2) == false) {
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
   142
            throw new RuntimeException(copyName + " digest mismatch!");
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
   143
        }
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
   144
    }
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
   145
}
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents:
diff changeset
   146