Path: blob/master/test/jdk/java/security/SecureRandom/NoSync.java
41152 views
/*1* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.security.Provider;24import java.security.SecureRandom;25import java.security.Security;26import java.util.Date;27import java.util.concurrent.atomic.AtomicBoolean;2829/*30* @test31* @bug 700496732* @run main/othervm NoSync33* @summary SecureRandom should be more explicit about threading34*/35public class NoSync {36public static void main(String[] args) throws Exception {37for (Provider p : Security.getProviders()) {38for (Provider.Service s : p.getServices()) {39if (s.getType().equals("SecureRandom") &&40!s.getAlgorithm().contains("Block")) {41test(SecureRandom.getInstance(s.getAlgorithm(), p));42}43}44}45Security.setProperty("securerandom.drbg.config", "HMAC_DRBG");46test(SecureRandom.getInstance("DRBG"));47Security.setProperty("securerandom.drbg.config", "CTR_DRBG");48test(SecureRandom.getInstance("DRBG"));49}5051static void test(SecureRandom sr) throws Exception {52test(sr, 20, 3000);53// All out-of-box impl should have the ThreadSafe attribute54String attr = sr.getProvider().getProperty("SecureRandom."55+ sr.getAlgorithm() + " ThreadSafe");56if (!"true".equals(attr)) {57throw new Exception("Not ThreadSafe: " + attr);58}59}6061public static void test(SecureRandom sr, int tnum, int rnum)62throws Exception {6364System.out.println(sr);65System.out.println(sr.getAlgorithm() + " " + sr.getProvider().getName());6667System.out.println(new Date());68boolean reseed = sr.getParameters() != null;69Thread[] threads = new Thread[tnum];70AtomicBoolean failed = new AtomicBoolean(false);71Thread.UncaughtExceptionHandler h = (t, e) -> {72failed.set(true);73e.printStackTrace();74};75for (int i = 0; i < threads.length; i++) {76threads[i] = new Thread() {77@Override78public void run() {79for (int j = 0; j < rnum; j++) {80sr.nextBytes(new byte[j%100+100]);81sr.setSeed((long)j);82if (reseed) {83sr.reseed();84}85}86}87};88threads[i].setUncaughtExceptionHandler(h);89threads[i].start();90}91for (int i = 0; i < threads.length; i++) {92threads[i].join();93}94System.out.println(new Date());95System.out.println();96if (failed.get()) {97throw new RuntimeException("Failed");98}99}100}101102103