Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/pkcs11/MessageDigest/ByteBuffers.java
38855 views
1
/*
2
* Copyright (c) 2003, 2019, Oracle and/or its affiliates. All rights reserved.
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
24
/**
25
* @test
26
* @bug 4856966 8080462
27
* @summary Test the MessageDigest.update(ByteBuffer) method
28
* @author Andreas Sterbenz
29
* @library ..
30
* @key randomness
31
* @run main/othervm ByteBuffers
32
*/
33
34
import java.nio.ByteBuffer;
35
import java.security.*;
36
import java.util.Arrays;
37
import java.util.Random;
38
39
public class ByteBuffers extends PKCS11Test {
40
41
static final String[] ALGS = {
42
"SHA-224", "SHA-256", "SHA-384", "SHA-512", "SHA-512/224", "SHA-512/256"
43
};
44
45
private static Random random = new Random();
46
47
public static void main(String[] args) throws Exception {
48
main(new ByteBuffers(), args);
49
}
50
51
@Override
52
public void main(Provider p) throws Exception {
53
int n = 10 * 1024;
54
byte[] t = new byte[n];
55
random.nextBytes(t);
56
57
for (String alg : ALGS) {
58
runTest(p, alg, t);
59
}
60
}
61
62
private void runTest(Provider p, String alg, byte[] data) throws Exception {
63
System.out.println("Test against " + p.getName() + " and " + alg);
64
MessageDigest md;
65
try {
66
md = MessageDigest.getInstance(alg, p);
67
} catch (NoSuchAlgorithmException e) {
68
System.out.println("Skip " + alg + " due to no support");
69
return;
70
}
71
72
byte[] d1 = md.digest(data);
73
74
int n = data.length;
75
76
// test 1: ByteBuffer with an accessible backing array
77
ByteBuffer b1 = ByteBuffer.allocate(n + 256);
78
b1.position(random.nextInt(256));
79
b1.limit(b1.position() + n);
80
ByteBuffer b2 = b1.slice();
81
b2.put(data);
82
b2.clear();
83
byte[] d2 = digest(md, b2);
84
if (Arrays.equals(d1, d2) == false) {
85
throw new Exception("Test 1 failed");
86
}
87
88
// test 2: direct ByteBuffer
89
ByteBuffer b3 = ByteBuffer.allocateDirect(n);
90
b3.put(data);
91
b3.clear();
92
byte[] d3 = digest(md, b3);
93
if (Arrays.equals(d1, d2) == false) {
94
throw new Exception("Test 2 failed");
95
}
96
97
// test 3: ByteBuffer without an accessible backing array
98
b2.clear();
99
ByteBuffer b4 = b2.asReadOnlyBuffer();
100
byte[] d4 = digest(md, b4);
101
if (Arrays.equals(d1, d2) == false) {
102
throw new Exception("Test 3 failed");
103
}
104
System.out.println("All tests passed");
105
}
106
107
private static byte[] digest(MessageDigest md, ByteBuffer b)
108
throws Exception {
109
int lim = b.limit();
110
b.limit(random.nextInt(lim));
111
md.update(b);
112
if (b.hasRemaining()) {
113
throw new Exception("Buffer not consumed");
114
}
115
b.limit(lim);
116
md.update(b);
117
if (b.hasRemaining()) {
118
throw new Exception("Buffer not consumed");
119
}
120
return md.digest();
121
}
122
}
123
124