author | prr |
Fri, 25 May 2018 12:12:24 -0700 | |
changeset 50347 | b2f046ae8eb6 |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
42161
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
diff
changeset
|
1 |
/* |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
diff
changeset
|
2 |
* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved. |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
diff
changeset
|
4 |
* |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
diff
changeset
|
8 |
* |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
diff
changeset
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
diff
changeset
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
diff
changeset
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
diff
changeset
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
diff
changeset
|
13 |
* accompanied this code). |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
diff
changeset
|
14 |
* |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License version |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
diff
changeset
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
diff
changeset
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
diff
changeset
|
18 |
* |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
diff
changeset
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
diff
changeset
|
20 |
* or visit www.oracle.com if you need additional information or have any |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
diff
changeset
|
21 |
* questions. |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
diff
changeset
|
22 |
*/ |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
diff
changeset
|
23 |
|
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
diff
changeset
|
24 |
import java.security.Provider; |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
diff
changeset
|
25 |
import java.security.SecureRandom; |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
diff
changeset
|
26 |
import java.security.SecureRandomSpi; |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
diff
changeset
|
27 |
import java.util.Map; |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
diff
changeset
|
28 |
|
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
diff
changeset
|
29 |
/* |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
diff
changeset
|
30 |
* @test |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
diff
changeset
|
31 |
* @bug 7004967 |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
diff
changeset
|
32 |
* @summary SecureRandom should be more explicit about threading |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
diff
changeset
|
33 |
*/ |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
diff
changeset
|
34 |
public class ThreadSafe { |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
diff
changeset
|
35 |
public static void main(String[] args) throws Exception { |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
diff
changeset
|
36 |
Provider p = new P(); |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
diff
changeset
|
37 |
NoSync.test(SecureRandom.getInstance("S1", p), 5, 5); |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
diff
changeset
|
38 |
try { |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
diff
changeset
|
39 |
NoSync.test(SecureRandom.getInstance("S2", p), 5, 5); |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
diff
changeset
|
40 |
throw new Exception("Failed"); |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
diff
changeset
|
41 |
} catch (RuntimeException re) { |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
diff
changeset
|
42 |
// Good |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
diff
changeset
|
43 |
} |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
diff
changeset
|
44 |
NoSync.test(SecureRandom.getInstance("S3", p), 5, 5); |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
diff
changeset
|
45 |
try { |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
diff
changeset
|
46 |
NoSync.test(SecureRandom.getInstance("S4", p), 5, 5); |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
diff
changeset
|
47 |
throw new Exception("Failed"); |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
diff
changeset
|
48 |
} catch (RuntimeException re) { |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
diff
changeset
|
49 |
// Good |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
diff
changeset
|
50 |
} |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
diff
changeset
|
51 |
} |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
diff
changeset
|
52 |
|
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
diff
changeset
|
53 |
public static class P extends Provider { |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
diff
changeset
|
54 |
public P() { |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
diff
changeset
|
55 |
|
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
diff
changeset
|
56 |
super("P", 1.0d, "Haha"); |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
diff
changeset
|
57 |
|
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
diff
changeset
|
58 |
// Good. No attribute. |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
diff
changeset
|
59 |
put("SecureRandom.S1", S.class.getName()); |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
diff
changeset
|
60 |
|
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
diff
changeset
|
61 |
// Bad. Boasting ThreadSafe but isn't |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
diff
changeset
|
62 |
put("SecureRandom.S2", S.class.getName()); |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
diff
changeset
|
63 |
put("SecureRandom.S2 ThreadSafe", "true"); |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
diff
changeset
|
64 |
|
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
diff
changeset
|
65 |
// Good. No attribute. |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
diff
changeset
|
66 |
putService(new Service(this, "SecureRandom", "S3", |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
diff
changeset
|
67 |
S.class.getName(), null, null)); |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
diff
changeset
|
68 |
|
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
diff
changeset
|
69 |
// Bad. Boasting ThreadSafe but isn't |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
diff
changeset
|
70 |
putService(new Service(this, "SecureRandom", "S4", |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
diff
changeset
|
71 |
S.class.getName(), null, Map.of("ThreadSafe", "true"))); |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
diff
changeset
|
72 |
} |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
diff
changeset
|
73 |
} |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
diff
changeset
|
74 |
|
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
diff
changeset
|
75 |
// This implementation is not itself thread safe. |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
diff
changeset
|
76 |
public static class S extends SecureRandomSpi { |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
diff
changeset
|
77 |
@java.lang.Override |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
diff
changeset
|
78 |
protected void engineSetSeed(byte[] seed) { |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
diff
changeset
|
79 |
return; |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
diff
changeset
|
80 |
} |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
diff
changeset
|
81 |
|
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
diff
changeset
|
82 |
private volatile boolean inCall = false; |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
diff
changeset
|
83 |
@Override |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
diff
changeset
|
84 |
protected void engineNextBytes(byte[] bytes) { |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
diff
changeset
|
85 |
if (inCall) { |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
diff
changeset
|
86 |
throw new RuntimeException("IN CALL"); |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
diff
changeset
|
87 |
} |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
diff
changeset
|
88 |
inCall = true; |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
diff
changeset
|
89 |
try { |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
diff
changeset
|
90 |
Thread.sleep(100); |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
diff
changeset
|
91 |
} catch (Exception e) { |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
diff
changeset
|
92 |
// OK |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
diff
changeset
|
93 |
} |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
diff
changeset
|
94 |
inCall = false; |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
diff
changeset
|
95 |
} |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
diff
changeset
|
96 |
|
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
diff
changeset
|
97 |
@Override |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
diff
changeset
|
98 |
protected byte[] engineGenerateSeed(int numBytes) { |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
diff
changeset
|
99 |
return new byte[numBytes]; |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
diff
changeset
|
100 |
} |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
diff
changeset
|
101 |
} |
5b0b84715c06
7004967: SecureRandom should be more explicit about threading
weijun
parents:
diff
changeset
|
102 |
} |