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