|
| 1 | +/* |
| 2 | + * Copyright (c) 2023, 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 | +/* |
| 25 | + * @test |
| 26 | + * @bug 8155191 |
| 27 | + * @summary check NPE is thrown for various methods of SecureRandom class, |
| 28 | + * e.g. SecureRandom(byte[]), nextBytes(byte[]), and setSeed(byte[]). |
| 29 | + * @run main NextBytesNull |
| 30 | + */ |
| 31 | + |
| 32 | +import java.security.NoSuchAlgorithmException; |
| 33 | +import java.security.Provider; |
| 34 | +import java.security.SecureRandom; |
| 35 | +import java.security.SecureRandomSpi; |
| 36 | + |
| 37 | +public class NextBytesNull { |
| 38 | + |
| 39 | + public static void main(String[] args) throws Exception { |
| 40 | + String test = "SecureRandom(null)"; |
| 41 | + try { |
| 42 | + new SecureRandom(null); |
| 43 | + throw new RuntimeException("Error: NPE not thrown for " + test); |
| 44 | + } catch (NullPointerException e) { |
| 45 | + System.out.println("OK, expected NPE thrown for " + test); |
| 46 | + } |
| 47 | + |
| 48 | + // verify with an Spi impl which does not throw NPE |
| 49 | + SecureRandom sr = SecureRandom.getInstance("S1", new P()); |
| 50 | + try { |
| 51 | + sr.nextBytes(null); |
| 52 | + throw new RuntimeException("Error: NPE not thrown"); |
| 53 | + } catch (NullPointerException npe) { |
| 54 | + System.out.println("OK, expected NPE thrown for " + test); |
| 55 | + } |
| 56 | + try { |
| 57 | + sr.setSeed(null); |
| 58 | + throw new RuntimeException("Error: NPE not thrown for " + test); |
| 59 | + } catch (NullPointerException npe) { |
| 60 | + System.out.println("OK, expected NPE thrown for " + test); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + public static final class P extends Provider { |
| 65 | + public P() { |
| 66 | + super("P", 1.0d, "Test Provider without Null Check"); |
| 67 | + put("SecureRandom.S1", S.class.getName()); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + public static final class S extends SecureRandomSpi { |
| 72 | + @Override |
| 73 | + protected void engineSetSeed(byte[] seed) { |
| 74 | + } |
| 75 | + @Override |
| 76 | + protected void engineNextBytes(byte[] bytes) { |
| 77 | + } |
| 78 | + @Override |
| 79 | + protected byte[] engineGenerateSeed(int numBytes) { |
| 80 | + return new byte[numBytes]; |
| 81 | + } |
| 82 | + } |
| 83 | +} |
1 commit comments
openjdk-notifier[bot] commentedon May 12, 2023
Review
Issues