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 /* @test
  25  * @bug 8051408 8158534
  26  * @summary Make sure DrbgParameters coded as specified
  27  * @library /test/lib/share/classes
  28  */
  29 
  30 import jdk.test.lib.Asserts;
  31 
  32 import java.security.DrbgParameters;
  33 import java.util.Arrays;
  34 
  35 import static java.security.DrbgParameters.Capability.*;
  36 
  37 public class DrbgParametersSpec {
  38 
  39     public static void main(String args[]) throws Exception {
  40 
  41         byte[] p, np1, np2;
  42 
  43         // Capability
  44         Asserts.assertTrue(PR_AND_RESEED.supportsPredictionResistance());
  45         Asserts.assertTrue(PR_AND_RESEED.supportsReseeding());
  46         Asserts.assertFalse(RESEED_ONLY.supportsPredictionResistance());
  47         Asserts.assertTrue(RESEED_ONLY.supportsReseeding());
  48         Asserts.assertFalse(NONE.supportsPredictionResistance());
  49         Asserts.assertFalse(NONE.supportsReseeding());
  50 
  51         // Instantiation
  52         p = "Instantiation".getBytes();
  53         DrbgParameters.Instantiation ins = DrbgParameters
  54                 .instantiation(192, RESEED_ONLY, p);
  55         Asserts.assertTrue(ins.getStrength() == 192);
  56         Asserts.assertTrue(ins.getCapability() == RESEED_ONLY);
  57         np1 = ins.getPersonalizationString();
  58         np2 = ins.getPersonalizationString();
  59         // Getter outputs have same content but not the same object
  60         Asserts.assertTrue(Arrays.equals(np1, p));
  61         Asserts.assertTrue(Arrays.equals(np2, p));
  62         Asserts.assertNE(np1, np2);
  63         // Changes to original input has no affect on object
  64         p[0] = 'X';
  65         np2 = ins.getPersonalizationString();
  66         Asserts.assertTrue(Arrays.equals(np1, np2));
  67 
  68         ins = DrbgParameters.instantiation(-1, NONE, null);
  69         Asserts.assertNull(ins.getPersonalizationString());
  70 
  71         iae(() -> DrbgParameters.instantiation(-2, NONE, null));
  72         npe(() -> DrbgParameters.instantiation(-1, null, null));
  73 
  74         // NextBytes
  75         p = "NextBytes".getBytes();
  76         DrbgParameters.NextBytes nb = DrbgParameters
  77                 .nextBytes(192, true, p);
  78         Asserts.assertTrue(nb.getStrength() == 192);
  79         Asserts.assertTrue(nb.getPredictionResistance());
  80         np1 = nb.getAdditionalInput();
  81         np2 = nb.getAdditionalInput();
  82         // Getter outputs have same content but not the same object
  83         Asserts.assertTrue(Arrays.equals(np1, p));
  84         Asserts.assertTrue(Arrays.equals(np2, p));
  85         Asserts.assertNE(np1, np2);
  86         // Changes to original input has no affect on object
  87         p[0] = 'X';
  88         np2 = nb.getAdditionalInput();
  89         Asserts.assertTrue(Arrays.equals(np1, np2));
  90 
  91         iae(() -> DrbgParameters.nextBytes(-2, false, null));
  92 
  93         // Reseed
  94         p = "Reseed".getBytes();
  95         DrbgParameters.Reseed rs = DrbgParameters
  96                 .reseed(true, p);
  97         Asserts.assertTrue(rs.getPredictionResistance());
  98         np1 = rs.getAdditionalInput();
  99         np2 = rs.getAdditionalInput();
 100         // Getter outputs have same content but not the same object
 101         Asserts.assertTrue(Arrays.equals(np1, p));
 102         Asserts.assertTrue(Arrays.equals(np2, p));
 103         Asserts.assertNE(np1, np2);
 104         // Changes to original input has no affect on object
 105         p[0] = 'X';
 106         np2 = rs.getAdditionalInput();
 107         Asserts.assertTrue(Arrays.equals(np1, np2));
 108     }
 109 
 110     static void iae(RunnableWithException r) throws Exception {
 111         checkException(r, IllegalArgumentException.class);
 112     }
 113 
 114     static void npe(RunnableWithException r) throws Exception {
 115         checkException(r, NullPointerException.class);
 116     }
 117 
 118     interface RunnableWithException {
 119         void run() throws Exception;
 120     }
 121 
 122     static void checkException(RunnableWithException r, Class ex)
 123             throws Exception {
 124         try {
 125             r.run();
 126         } catch (Exception e) {
 127             if (ex.isAssignableFrom(e.getClass())) {
 128                 return;
 129             }
 130             throw e;
 131         }
 132         throw new Exception("No exception thrown");
 133     }
 134 }