Path: blob/master/test/jdk/java/security/SecureRandom/DrbgParametersSpec.java
41149 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 815853425* @summary Make sure DrbgParameters coded as specified26* @library /test/lib27* @build jdk.test.lib.Asserts28* @run main DrbgParametersSpec29*/3031import jdk.test.lib.Asserts;3233import java.security.DrbgParameters;34import java.util.Arrays;3536import static java.security.DrbgParameters.Capability.*;3738public class DrbgParametersSpec {3940public static void main(String args[]) throws Exception {4142byte[] p, np1, np2;4344// Capability45Asserts.assertTrue(PR_AND_RESEED.supportsPredictionResistance());46Asserts.assertTrue(PR_AND_RESEED.supportsReseeding());47Asserts.assertFalse(RESEED_ONLY.supportsPredictionResistance());48Asserts.assertTrue(RESEED_ONLY.supportsReseeding());49Asserts.assertFalse(NONE.supportsPredictionResistance());50Asserts.assertFalse(NONE.supportsReseeding());5152// Instantiation53p = "Instantiation".getBytes();54DrbgParameters.Instantiation ins = DrbgParameters55.instantiation(192, RESEED_ONLY, p);56Asserts.assertTrue(ins.getStrength() == 192);57Asserts.assertTrue(ins.getCapability() == RESEED_ONLY);58np1 = ins.getPersonalizationString();59np2 = ins.getPersonalizationString();60// Getter outputs have same content but not the same object61Asserts.assertTrue(Arrays.equals(np1, p));62Asserts.assertTrue(Arrays.equals(np2, p));63Asserts.assertNE(np1, np2);64// Changes to original input has no affect on object65p[0] = 'X';66np2 = ins.getPersonalizationString();67Asserts.assertTrue(Arrays.equals(np1, np2));6869ins = DrbgParameters.instantiation(-1, NONE, null);70Asserts.assertNull(ins.getPersonalizationString());7172iae(() -> DrbgParameters.instantiation(-2, NONE, null));73npe(() -> DrbgParameters.instantiation(-1, null, null));7475// NextBytes76p = "NextBytes".getBytes();77DrbgParameters.NextBytes nb = DrbgParameters78.nextBytes(192, true, p);79Asserts.assertTrue(nb.getStrength() == 192);80Asserts.assertTrue(nb.getPredictionResistance());81np1 = nb.getAdditionalInput();82np2 = nb.getAdditionalInput();83// Getter outputs have same content but not the same object84Asserts.assertTrue(Arrays.equals(np1, p));85Asserts.assertTrue(Arrays.equals(np2, p));86Asserts.assertNE(np1, np2);87// Changes to original input has no affect on object88p[0] = 'X';89np2 = nb.getAdditionalInput();90Asserts.assertTrue(Arrays.equals(np1, np2));9192iae(() -> DrbgParameters.nextBytes(-2, false, null));9394// Reseed95p = "Reseed".getBytes();96DrbgParameters.Reseed rs = DrbgParameters97.reseed(true, p);98Asserts.assertTrue(rs.getPredictionResistance());99np1 = rs.getAdditionalInput();100np2 = rs.getAdditionalInput();101// Getter outputs have same content but not the same object102Asserts.assertTrue(Arrays.equals(np1, p));103Asserts.assertTrue(Arrays.equals(np2, p));104Asserts.assertNE(np1, np2);105// Changes to original input has no affect on object106p[0] = 'X';107np2 = rs.getAdditionalInput();108Asserts.assertTrue(Arrays.equals(np1, np2));109}110111static void iae(RunnableWithException r) throws Exception {112checkException(r, IllegalArgumentException.class);113}114115static void npe(RunnableWithException r) throws Exception {116checkException(r, NullPointerException.class);117}118119interface RunnableWithException {120void run() throws Exception;121}122123static void checkException(RunnableWithException r, Class ex)124throws Exception {125try {126r.run();127} catch (Exception e) {128if (ex.isAssignableFrom(e.getClass())) {129return;130}131throw e;132}133throw new Exception("No exception thrown");134}135}136137138