jdk/test/java/security/SecureRandom/ThreadSafe.java
changeset 42161 5b0b84715c06
equal deleted inserted replaced
42160:c229da92b1a9 42161:5b0b84715c06
       
     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.SecureRandomSpi;
       
    27 import java.util.Map;
       
    28 
       
    29 /*
       
    30  * @test
       
    31  * @bug 7004967
       
    32  * @summary SecureRandom should be more explicit about threading
       
    33  */
       
    34 public class ThreadSafe {
       
    35     public static void main(String[] args) throws Exception {
       
    36         Provider p = new P();
       
    37         NoSync.test(SecureRandom.getInstance("S1", p), 5, 5);
       
    38         try {
       
    39             NoSync.test(SecureRandom.getInstance("S2", p), 5, 5);
       
    40             throw new Exception("Failed");
       
    41         } catch (RuntimeException re) {
       
    42             // Good
       
    43         }
       
    44         NoSync.test(SecureRandom.getInstance("S3", p), 5, 5);
       
    45         try {
       
    46             NoSync.test(SecureRandom.getInstance("S4", p), 5, 5);
       
    47             throw new Exception("Failed");
       
    48         } catch (RuntimeException re) {
       
    49             // Good
       
    50         }
       
    51     }
       
    52 
       
    53     public static class P extends Provider {
       
    54         public P() {
       
    55 
       
    56             super("P", 1.0d, "Haha");
       
    57 
       
    58             // Good. No attribute.
       
    59             put("SecureRandom.S1", S.class.getName());
       
    60 
       
    61             // Bad. Boasting ThreadSafe but isn't
       
    62             put("SecureRandom.S2", S.class.getName());
       
    63             put("SecureRandom.S2 ThreadSafe", "true");
       
    64 
       
    65             // Good. No attribute.
       
    66             putService(new Service(this, "SecureRandom", "S3",
       
    67                     S.class.getName(), null, null));
       
    68 
       
    69             // Bad. Boasting ThreadSafe but isn't
       
    70             putService(new Service(this, "SecureRandom", "S4",
       
    71                     S.class.getName(), null, Map.of("ThreadSafe", "true")));
       
    72         }
       
    73     }
       
    74 
       
    75     // This implementation is not itself thread safe.
       
    76     public static class S extends SecureRandomSpi {
       
    77         @java.lang.Override
       
    78         protected void engineSetSeed(byte[] seed) {
       
    79             return;
       
    80         }
       
    81 
       
    82         private volatile boolean inCall = false;
       
    83         @Override
       
    84         protected void engineNextBytes(byte[] bytes) {
       
    85             if (inCall) {
       
    86                 throw new RuntimeException("IN CALL");
       
    87             }
       
    88             inCall = true;
       
    89             try {
       
    90                 Thread.sleep(100);
       
    91             } catch (Exception e) {
       
    92                 // OK
       
    93             }
       
    94             inCall = false;
       
    95         }
       
    96 
       
    97         @Override
       
    98         protected byte[] engineGenerateSeed(int numBytes) {
       
    99             return new byte[numBytes];
       
   100         }
       
   101     }
       
   102 }