author | vinnie |
Wed, 12 Aug 2015 14:38:09 +0100 | |
changeset 32138 | 23830562d3d1 |
parent 30046 | cf2c86e1819e |
child 35379 | 1e8e336ef66b |
permissions | -rw-r--r-- |
2 | 1 |
/* |
32138
23830562d3d1
8133318: Exclude intermittent failing PKCS11 tests on Solaris SPARC 11.1 and earlier
vinnie
parents:
30046
diff
changeset
|
2 |
* Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 22 |
*/ |
23 |
||
24 |
/** |
|
25 |
* @test |
|
26 |
* @bug 4856966 |
|
27 |
* @summary Test the Signature.update(ByteBuffer) method |
|
28 |
* @author Andreas Sterbenz |
|
29 |
* @library .. |
|
30046 | 30 |
* @key randomness |
2 | 31 |
*/ |
32 |
||
33 |
import java.util.*; |
|
34 |
import java.nio.*; |
|
35 |
||
36 |
import java.security.*; |
|
37 |
||
38 |
public class ByteBuffers extends PKCS11Test { |
|
39 |
||
40 |
public static void main(String[] args) throws Exception { |
|
41 |
main(new ByteBuffers()); |
|
42 |
} |
|
43 |
||
44 |
public void main(Provider p) throws Exception { |
|
32138
23830562d3d1
8133318: Exclude intermittent failing PKCS11 tests on Solaris SPARC 11.1 and earlier
vinnie
parents:
30046
diff
changeset
|
45 |
|
23830562d3d1
8133318: Exclude intermittent failing PKCS11 tests on Solaris SPARC 11.1 and earlier
vinnie
parents:
30046
diff
changeset
|
46 |
/* |
23830562d3d1
8133318: Exclude intermittent failing PKCS11 tests on Solaris SPARC 11.1 and earlier
vinnie
parents:
30046
diff
changeset
|
47 |
* Use Solaris SPARC 11.2 or later to avoid an intermittent failure |
23830562d3d1
8133318: Exclude intermittent failing PKCS11 tests on Solaris SPARC 11.1 and earlier
vinnie
parents:
30046
diff
changeset
|
48 |
* when running SunPKCS11-Solaris provider (8044554) |
23830562d3d1
8133318: Exclude intermittent failing PKCS11 tests on Solaris SPARC 11.1 and earlier
vinnie
parents:
30046
diff
changeset
|
49 |
*/ |
23830562d3d1
8133318: Exclude intermittent failing PKCS11 tests on Solaris SPARC 11.1 and earlier
vinnie
parents:
30046
diff
changeset
|
50 |
if (p.getName().equals("SunPKCS11-Solaris") && |
23830562d3d1
8133318: Exclude intermittent failing PKCS11 tests on Solaris SPARC 11.1 and earlier
vinnie
parents:
30046
diff
changeset
|
51 |
System.getProperty("os.name").equals("SunOS") && |
23830562d3d1
8133318: Exclude intermittent failing PKCS11 tests on Solaris SPARC 11.1 and earlier
vinnie
parents:
30046
diff
changeset
|
52 |
System.getProperty("os.arch").equals("sparcv9") && |
23830562d3d1
8133318: Exclude intermittent failing PKCS11 tests on Solaris SPARC 11.1 and earlier
vinnie
parents:
30046
diff
changeset
|
53 |
System.getProperty("os.version").compareTo("5.11") <= 0 && |
23830562d3d1
8133318: Exclude intermittent failing PKCS11 tests on Solaris SPARC 11.1 and earlier
vinnie
parents:
30046
diff
changeset
|
54 |
getDistro().compareTo("11.2") < 0) { |
23830562d3d1
8133318: Exclude intermittent failing PKCS11 tests on Solaris SPARC 11.1 and earlier
vinnie
parents:
30046
diff
changeset
|
55 |
|
23830562d3d1
8133318: Exclude intermittent failing PKCS11 tests on Solaris SPARC 11.1 and earlier
vinnie
parents:
30046
diff
changeset
|
56 |
System.out.println("SunPKCS11-Solaris provider requires " + |
23830562d3d1
8133318: Exclude intermittent failing PKCS11 tests on Solaris SPARC 11.1 and earlier
vinnie
parents:
30046
diff
changeset
|
57 |
"Solaris SPARC 11.2 or later, skipping"); |
23830562d3d1
8133318: Exclude intermittent failing PKCS11 tests on Solaris SPARC 11.1 and earlier
vinnie
parents:
30046
diff
changeset
|
58 |
return; |
23830562d3d1
8133318: Exclude intermittent failing PKCS11 tests on Solaris SPARC 11.1 and earlier
vinnie
parents:
30046
diff
changeset
|
59 |
} |
23830562d3d1
8133318: Exclude intermittent failing PKCS11 tests on Solaris SPARC 11.1 and earlier
vinnie
parents:
30046
diff
changeset
|
60 |
|
2 | 61 |
Random random = new Random(); |
62 |
int n = 10 * 1024; |
|
63 |
byte[] t = new byte[n]; |
|
64 |
random.nextBytes(t); |
|
65 |
||
66 |
KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA", p); |
|
67 |
kpg.initialize(512); |
|
68 |
KeyPair kp = kpg.generateKeyPair(); |
|
69 |
||
70 |
Signature sig = Signature.getInstance("MD5withRSA", p); |
|
71 |
sig.initSign(kp.getPrivate()); |
|
72 |
sig.update(t); |
|
73 |
byte[] signature = sig.sign(); |
|
74 |
||
75 |
sig.initVerify(kp.getPublic()); |
|
76 |
||
77 |
// test 1: ByteBuffer with an accessible backing array |
|
78 |
ByteBuffer b1 = ByteBuffer.allocate(n + 256); |
|
79 |
b1.position(random.nextInt(256)); |
|
80 |
b1.limit(b1.position() + n); |
|
81 |
ByteBuffer b2 = b1.slice(); |
|
82 |
b2.put(t); |
|
83 |
b2.clear(); |
|
84 |
verify(sig, signature, b2, random); |
|
85 |
||
86 |
// test 2: direct ByteBuffer |
|
87 |
ByteBuffer b3 = ByteBuffer.allocateDirect(t.length); |
|
88 |
b3.put(t); |
|
89 |
b3.clear(); |
|
90 |
verify(sig, signature, b3, random); |
|
91 |
||
92 |
// test 3: ByteBuffer without an accessible backing array |
|
93 |
b2.clear(); |
|
94 |
ByteBuffer b4 = b2.asReadOnlyBuffer(); |
|
95 |
verify(sig, signature, b4, random); |
|
96 |
||
97 |
System.out.println("All tests passed"); |
|
98 |
} |
|
99 |
||
100 |
private static void verify(Signature sig, byte[] signature, ByteBuffer b, Random random) throws Exception { |
|
101 |
int lim = b.limit(); |
|
102 |
b.limit(random.nextInt(lim)); |
|
103 |
sig.update(b); |
|
104 |
if (b.hasRemaining()) { |
|
105 |
throw new Exception("Buffer not consumed"); |
|
106 |
} |
|
107 |
b.limit(lim); |
|
108 |
sig.update(b); |
|
109 |
if (b.hasRemaining()) { |
|
110 |
throw new Exception("Buffer not consumed"); |
|
111 |
} |
|
112 |
if (sig.verify(signature) == false) { |
|
113 |
throw new Exception("Signature did not verify"); |
|
114 |
} |
|
115 |
} |
|
116 |
} |