< prev index next >

test/jdk/java/security/MessageDigest/TestDigestIOStream.java

Print this page
rev 59383 : [mq]: final
   1 /*
   2  * Copyright (c) 2003, 2017, 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  */


  51 
  52 public class TestDigestIOStream {
  53 
  54     private static final int[] DATA_LEN_ARRAY = { 1, 50, 2500, 125000,
  55             6250000 };
  56     private static final String[] ALGORITHM_ARRAY = { "MD2", "MD5", "SHA1",
  57             "SHA-224", "SHA-256", "SHA-384", "SHA-512", "SHA3-224", "SHA3-256",
  58             "SHA3-384", "SHA3-512" };
  59 
  60     private static byte[] data;
  61 
  62     private static MessageDigest md = null;
  63 
  64     public static void main(String argv[]) throws Exception {
  65         TestDigestIOStream test = new TestDigestIOStream();
  66         test.run();
  67     }
  68 
  69     public void run() throws Exception {
  70         for (String algorithm : ALGORITHM_ARRAY) {
  71             try {
  72                 md = MessageDigest.getInstance(algorithm);
  73                 for (int length : DATA_LEN_ARRAY) {
  74 
  75                     Random rdm = RandomFactory.getRandom();
  76                     data = new byte[length];
  77                     rdm.nextBytes(data);
  78 
  79                     if (!testMDChange(algorithm, length)) {
  80                         throw new RuntimeException("testMDChange failed at:"
  81                                 + algorithm + "/" + length);
  82                     }
  83                     if (!testMDShare(algorithm, length)) {
  84                         throw new RuntimeException("testMDShare failed at:"
  85                                 + algorithm + "/" + length);
  86                     }
  87                     for (ReadModel readModel : ReadModel.values()) {
  88                         // test Digest function when digest switch on
  89                         if (!testDigestOnOff(algorithm, readModel, true,
  90                                 length)) {
  91                             throw new RuntimeException(
  92                                     "testDigestOn failed at:" + algorithm + "/"
  93                                             + length + "/" + readModel);
  94                         }
  95                         // test Digest function when digest switch off
  96                         if (!testDigestOnOff(algorithm, readModel, false,
  97                                 length)) {
  98                             throw new RuntimeException(
  99                                     "testDigestOff failed at:" + algorithm + "/"
 100                                             + length + "/" + readModel);
 101                         }
 102                     }
 103                 }
 104             } catch (NoSuchAlgorithmException nae) {
 105                 if (algorithm.startsWith("SHA3") && !isSHA3supported()) {
 106                     continue;
 107                 } else {
 108                     throw nae;
 109                 }
 110             }
 111         }
 112         int testNumber = ALGORITHM_ARRAY.length * ReadModel.values().length
 113                 * DATA_LEN_ARRAY.length * 2
 114                 + ALGORITHM_ARRAY.length * DATA_LEN_ARRAY.length * 2;
 115         out.println("All " + testNumber + " Tests Passed");
 116     }
 117 
 118     // SHA-3 hash algorithms are only supported by "SUN" provider
 119     // and "OracleUcrypto" provider on Solaris 12.0 or later
 120     // This method checks if system supports SHA-3
 121     private boolean isSHA3supported() {
 122         if (Security.getProvider("SUN") != null) {
 123             return true;
 124         }
 125         if (Security.getProvider("OracleUcrypto") != null
 126                 && "SunOS".equals(System.getProperty("os.name"))
 127                 && System.getProperty("os.version").compareTo("5.12") >= 0) {
 128             return true;
 129         }
 130         return false;
 131     }
 132 
 133     /**
 134      * Test DigestInputStream and DigestOutputStream digest function when digest
 135      * set on and off
 136      *
 137      * @param algo
 138      *            Message Digest algorithm
 139      * @param readModel
 140      *            which read method used(READ, BUFFER_READ, MIX_READ)
 141      * @param on
 142      *            digest switch(on and off)
 143      * @param dataLength
 144      *            plain test data length.
 145      * @exception Exception
 146      *                throw unexpected exception
 147      */
 148     public boolean testDigestOnOff(String algo, ReadModel readModel, boolean on,
 149             int dataLength) throws Exception {
 150 


   1 /*
   2  * Copyright (c) 2003, 2020, 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  */


  51 
  52 public class TestDigestIOStream {
  53 
  54     private static final int[] DATA_LEN_ARRAY = { 1, 50, 2500, 125000,
  55             6250000 };
  56     private static final String[] ALGORITHM_ARRAY = { "MD2", "MD5", "SHA1",
  57             "SHA-224", "SHA-256", "SHA-384", "SHA-512", "SHA3-224", "SHA3-256",
  58             "SHA3-384", "SHA3-512" };
  59 
  60     private static byte[] data;
  61 
  62     private static MessageDigest md = null;
  63 
  64     public static void main(String argv[]) throws Exception {
  65         TestDigestIOStream test = new TestDigestIOStream();
  66         test.run();
  67     }
  68 
  69     public void run() throws Exception {
  70         for (String algorithm : ALGORITHM_ARRAY) {

  71             md = MessageDigest.getInstance(algorithm);
  72             for (int length : DATA_LEN_ARRAY) {
  73 
  74                 Random rdm = RandomFactory.getRandom();
  75                 data = new byte[length];
  76                 rdm.nextBytes(data);
  77 
  78                 if (!testMDChange(algorithm, length)) {
  79                     throw new RuntimeException("testMDChange failed at:"
  80                             + algorithm + "/" + length);
  81                 }
  82                 if (!testMDShare(algorithm, length)) {
  83                     throw new RuntimeException("testMDShare failed at:"
  84                             + algorithm + "/" + length);
  85                 }
  86                 for (ReadModel readModel : ReadModel.values()) {
  87                     // test Digest function when digest switch on
  88                     if (!testDigestOnOff(algorithm, readModel, true,
  89                             length)) {
  90                         throw new RuntimeException(
  91                                 "testDigestOn failed at:" + algorithm + "/"
  92                                         + length + "/" + readModel);
  93                     }
  94                     // test Digest function when digest switch off
  95                     if (!testDigestOnOff(algorithm, readModel, false,
  96                             length)) {
  97                         throw new RuntimeException(
  98                                 "testDigestOff failed at:" + algorithm + "/"
  99                                         + length + "/" + readModel);
 100                     }
 101                 }
 102             }







 103         }
 104         int testNumber = ALGORITHM_ARRAY.length * ReadModel.values().length
 105                 * DATA_LEN_ARRAY.length * 2
 106                 + ALGORITHM_ARRAY.length * DATA_LEN_ARRAY.length * 2;
 107         out.println("All " + testNumber + " Tests Passed");















 108     }
 109 
 110     /**
 111      * Test DigestInputStream and DigestOutputStream digest function when digest
 112      * set on and off
 113      *
 114      * @param algo
 115      *            Message Digest algorithm
 116      * @param readModel
 117      *            which read method used(READ, BUFFER_READ, MIX_READ)
 118      * @param on
 119      *            digest switch(on and off)
 120      * @param dataLength
 121      *            plain test data length.
 122      * @exception Exception
 123      *                throw unexpected exception
 124      */
 125     public boolean testDigestOnOff(String algo, ReadModel readModel, boolean on,
 126             int dataLength) throws Exception {
 127 


< prev index next >