|
1 /* |
|
2 * Copyright (c) 2016, 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 import java.security.Provider; |
|
25 import java.security.SecureRandom; |
|
26 import java.security.Security; |
|
27 import java.util.Date; |
|
28 import java.util.concurrent.atomic.AtomicBoolean; |
|
29 |
|
30 /* |
|
31 * @test |
|
32 * @bug 7004967 |
|
33 * @run main/othervm NoSync |
|
34 * @summary SecureRandom should be more explicit about threading |
|
35 */ |
|
36 public class NoSync { |
|
37 public static void main(String[] args) throws Exception { |
|
38 for (Provider p : Security.getProviders()) { |
|
39 for (Provider.Service s : p.getServices()) { |
|
40 if (s.getType().equals("SecureRandom") && |
|
41 !s.getAlgorithm().contains("Block")) { |
|
42 test(SecureRandom.getInstance(s.getAlgorithm(), p)); |
|
43 } |
|
44 } |
|
45 } |
|
46 Security.setProperty("securerandom.drbg.config", "HMAC_DRBG"); |
|
47 test(SecureRandom.getInstance("DRBG")); |
|
48 Security.setProperty("securerandom.drbg.config", "CTR_DRBG"); |
|
49 test(SecureRandom.getInstance("DRBG")); |
|
50 } |
|
51 |
|
52 static void test(SecureRandom sr) throws Exception { |
|
53 test(sr, 20, 3000); |
|
54 // All out-of-box impl should have the ThreadSafe attribute |
|
55 String attr = sr.getProvider().getProperty("SecureRandom." |
|
56 + sr.getAlgorithm() + " ThreadSafe"); |
|
57 if (!"true".equals(attr)) { |
|
58 throw new Exception("Not ThreadSafe: " + attr); |
|
59 } |
|
60 } |
|
61 |
|
62 public static void test(SecureRandom sr, int tnum, int rnum) |
|
63 throws Exception { |
|
64 |
|
65 System.out.println(sr); |
|
66 System.out.println(sr.getAlgorithm() + " " + sr.getProvider().getName()); |
|
67 |
|
68 System.out.println(new Date()); |
|
69 boolean reseed = sr.getParameters() != null; |
|
70 Thread[] threads = new Thread[tnum]; |
|
71 AtomicBoolean failed = new AtomicBoolean(false); |
|
72 Thread.UncaughtExceptionHandler h = (t, e) -> { |
|
73 failed.set(true); |
|
74 e.printStackTrace(); |
|
75 }; |
|
76 for (int i = 0; i < threads.length; i++) { |
|
77 threads[i] = new Thread() { |
|
78 @Override |
|
79 public void run() { |
|
80 for (int j = 0; j < rnum; j++) { |
|
81 sr.nextBytes(new byte[j%100+100]); |
|
82 sr.setSeed((long)j); |
|
83 if (reseed) { |
|
84 sr.reseed(); |
|
85 } |
|
86 } |
|
87 } |
|
88 }; |
|
89 threads[i].setUncaughtExceptionHandler(h); |
|
90 threads[i].start(); |
|
91 } |
|
92 for (int i = 0; i < threads.length; i++) { |
|
93 threads[i].join(); |
|
94 } |
|
95 System.out.println(new Date()); |
|
96 System.out.println(); |
|
97 if (failed.get()) { |
|
98 throw new RuntimeException("Failed"); |
|
99 } |
|
100 } |
|
101 } |