Path: blob/master/test/jdk/sun/security/provider/SecureRandom/AbstractDrbg/SpecTest.java
41161 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*/2223/* @test24* @bug 8051408 8157308 813018125* @modules java.base/sun.security.provider26* @build java.base/sun.security.provider.S27* @run main SpecTest28* @summary check the AbstractDrbg API etc29*/3031import java.security.*;32import sun.security.provider.S;33import static java.security.DrbgParameters.Capability.*;3435/**36* This test makes sure the AbstractDrbg API works as specified. It also37* checks the SecureRandom API.38*39* The implementations must be patched into java.base/sun.security.provider40* because AbstractDrbg is not a public interface.41*/42public class SpecTest {4344public static void main(String args[]) throws Exception {4546// getInstance from a provider.4748Provider p = new All("A", "0", "");49byte[] bytes = new byte[100];5051// A non-DRBG52iae(() -> SecureRandom.getInstance("S1", null, p));53nsae(() -> SecureRandom.getInstance("S1",54new SecureRandomParameters() {}, p));5556SecureRandom s1 = SecureRandom.getInstance("S1", p);57if (s1.getParameters() != null) {58throw new Exception();59}6061iae(() -> s1.nextBytes(bytes, null));62uoe(() -> s1.nextBytes(bytes, new SecureRandomParameters() {}));63uoe(() -> s1.reseed());64iae(() -> s1.reseed(null));65uoe(() -> s1.reseed(new SecureRandomParameters() {}));6667// A weak DRBG68iae(() -> SecureRandom.getInstance("S2", null, p));69nsae(() -> SecureRandom.getInstance("S2",70new SecureRandomParameters() {}, p));71nsae(() -> SecureRandom.getInstance("S2",72DrbgParameters.instantiation(256, NONE, null), p));73nsae(() -> SecureRandom.getInstance("S2",74DrbgParameters.instantiation(-1, PR_AND_RESEED, null), p));75nsae(() -> SecureRandom.getInstance("S2",76DrbgParameters.instantiation(-1, RESEED_ONLY, null), p));7778SecureRandom s2 = SecureRandom.getInstance("S2",79DrbgParameters.instantiation(-1, NONE, null), p);80equals(s2, "S2,SQUEEZE,128,none");81equals(s2.getParameters(), "128,none,null");8283npe(() -> s2.nextBytes(null));84iae(() -> s2.nextBytes(bytes, null));85iae(() -> s2.nextBytes(bytes, new SecureRandomParameters() {}));86uoe(() -> s2.reseed());87iae(() -> s2.reseed(null));8889iae(() -> s2.nextBytes(bytes,90DrbgParameters.nextBytes(-1, false, new byte[101])));91s2.nextBytes(new byte[101],92DrbgParameters.nextBytes(-1, false, new byte[100]));93s2.nextBytes(bytes,94DrbgParameters.nextBytes(-1, false, new byte[100]));9596// A strong DRBG97iae(() -> SecureRandom.getInstance("S3", null, p));98nsae(() -> SecureRandom.getInstance("S3",99new SecureRandomParameters() {}, p));100SecureRandom.getInstance("S3",101DrbgParameters.instantiation(192, PR_AND_RESEED, null), p);102103SecureRandom s3 = SecureRandom.getInstance("S3", p);104equals(s3, "S3,SQUEEZE,128,reseed_only");105equals(s3.getParameters(), "128,reseed_only,null");106107iae(() -> s3.nextBytes(bytes,108DrbgParameters.nextBytes(192, false, null)));109iae(() -> s3.nextBytes(bytes,110DrbgParameters.nextBytes(112, true, null)));111iae(() -> s3.reseed(new SecureRandomParameters() {}));112113SecureRandom s32 = SecureRandom.getInstance(114"S3", DrbgParameters.instantiation(192, PR_AND_RESEED, null), p);115equals(s32, "S3,SQUEEZE,192,pr_and_reseed");116equals(s32.getParameters(), "192,pr_and_reseed,null");117118s32.nextBytes(bytes, DrbgParameters.nextBytes(192, false, null));119s32.nextBytes(bytes, DrbgParameters.nextBytes(112, true, null));120s32.reseed();121s32.reseed(DrbgParameters.reseed(true, new byte[100]));122123// getInstance from competitive providers.124125Provider l = new Legacy("L", "0", "");126Provider w = new Weak("W", "0", "");127Provider s = new Strong("S", "0", "");128129Security.addProvider(l);130Security.addProvider(w);131Security.addProvider(s);132133SecureRandom s4;134135try {136s4 = SecureRandom.getInstance("S");137if (s4.getProvider() != l) {138throw new Exception();139}140141nsae(() -> SecureRandom.getInstance(142"S", DrbgParameters.instantiation(256, NONE, null)));143144s4 = SecureRandom.getInstance(145"S", DrbgParameters.instantiation(192, NONE, null));146if (s4.getProvider() != s) {147throw new Exception();148}149150s4 = SecureRandom.getInstance(151"S", DrbgParameters.instantiation(128, PR_AND_RESEED, null));152if (s4.getProvider() != s) {153throw new Exception();154}155156s4 = SecureRandom.getInstance(157"S", DrbgParameters.instantiation(128, RESEED_ONLY, null));158if (s4.getProvider() != s) {159throw new Exception();160}161162s4 = SecureRandom.getInstance(163"S", DrbgParameters.instantiation(128, NONE, null));164if (s4.getProvider() != w) {165throw new Exception();166}167} finally {168Security.removeProvider("L");169Security.removeProvider("W");170Security.removeProvider("S");171}172}173174public static class All extends Provider {175protected All(String name, String version, String info) {176super(name, version, info);177put("SecureRandom.S1", S.S1.class.getName());178put("SecureRandom.S2", S.S2.class.getName());179put("SecureRandom.S3", S.S3.class.getName());180}181}182183// Providing S with no params support184public static class Legacy extends Provider {185protected Legacy(String name, String version, String info) {186super(name, version, info);187put("SecureRandom.S", S.S1.class.getName());188}189}190191public static class Weak extends Provider {192protected Weak(String name, String version, String info) {193super(name, version, info);194put("SecureRandom.S", S.S2.class.getName());195}196}197198public static class Strong extends Provider {199protected Strong(String name, String version, String info) {200super(name, version, info);201put("SecureRandom.S", S.S3.class.getName());202}203}204205static void nsae(RunnableWithException r) throws Exception {206checkException(r, NoSuchAlgorithmException.class);207}208209static void iae(RunnableWithException r) throws Exception {210checkException(r, IllegalArgumentException.class);211}212213static void uoe(RunnableWithException r) throws Exception {214checkException(r, UnsupportedOperationException.class);215}216217static void npe(RunnableWithException r) throws Exception {218checkException(r, NullPointerException.class);219}220221interface RunnableWithException {222void run() throws Exception;223}224225static void checkException(RunnableWithException r, Class ex)226throws Exception {227try {228r.run();229} catch (Exception e) {230if (ex.isAssignableFrom(e.getClass())) {231return;232}233throw e;234}235throw new Exception("No exception thrown");236}237238static void equals(Object o, String s) throws Exception {239if (!o.toString().equals(s)) {240throw new Exception(o.toString() + " is not " + s);241}242}243}244245246