1 /*
   2  * Copyright (c) 2015, 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 import static java.lang.System.out;
  25 
  26 import java.nio.ByteBuffer;
  27 import java.security.MessageDigest;
  28 import java.util.Random;
  29 
  30 /**
  31  * @test
  32  * @bug 8050371
  33  * @summary Check md.getDigestLength() equal digest output length with various
  34  *          algorithm/dataLen/(update,digest methods).
  35  * @author Kevin Liu
  36  * @key randomness
  37  */
  38 
  39 public class TestSameLength {
  40 
  41     public static void main(String[] args) throws Exception {
  42         TestSameLength test = new TestSameLength();
  43         test.run();
  44     }
  45 
  46     private void run() throws Exception {
  47         String[] algorithmArr = {
  48                 "SHA", "Sha", "SHA-1", "sha-1", "SHA1", "sha1", "MD5", "md5",
  49                 "SHA-224", "SHA-256", "SHA-384", "SHA-512"
  50         };
  51         int[] nUpdatesArr = {
  52                 0, 1, 2, 3
  53         };
  54         int[] dataLenArr = {
  55                 1, 50, 2500, 125000, 6250000
  56         };
  57 
  58         for (String algorithm: algorithmArr) {
  59             for (UpdateMethod update: UpdateMethod.values()) {
  60                 for (int dataLen: dataLenArr) {
  61                     if (!runTest(algorithm, dataLen, update)) {
  62                         throw new RuntimeException(
  63                                 "Test failed at algorithm/dataLen/numUpdate:"
  64                                         + algorithm + "/" + dataLen + "/"
  65                                         + update.toString());
  66                     }
  67                 }
  68             }
  69         }
  70 
  71         out.println("All " + algorithmArr.length * nUpdatesArr.length
  72                 * dataLenArr.length + " tests Passed");
  73     }
  74 
  75     private boolean runTest(String algo, long dataLen,
  76             UpdateMethod whichUpdate) throws Exception {
  77         try {
  78             // Do initialization
  79             byte[] data = new byte[(int) dataLen];
  80             new Random().nextBytes(data);
  81             MessageDigest md = MessageDigest.getInstance(algo);
  82             int outputLen = md.getDigestLength();
  83 
  84             // Perform the update using all available/possible update methods
  85             whichUpdate.updateDigest(data, md, dataLen);
  86             // Get the output
  87             byte[] output = md.digest();
  88 
  89             // Compare input and output
  90             return outputLen == output.length;
  91         } catch (Exception ex) {
  92             System.err.println("Testing: " + algo + "/" + dataLen + "/"
  93                     + whichUpdate.toString()
  94                     + " failed with unexpected exception");
  95             ex.printStackTrace();
  96             throw ex;
  97         }
  98     }
  99 
 100     private static enum UpdateMethod {
 101         UPDATE_BYTE {
 102             @Override
 103             public void updateDigest(byte[] data,
 104                     MessageDigest md, long dataLen) {
 105 
 106                 for (int i = 0; i < dataLen; i++) {
 107                     md.update(data[i]);
 108                 }
 109             }
 110         },
 111 
 112         UPDATE_BUFFER {
 113             @Override
 114             public void updateDigest(byte[] data,
 115                     MessageDigest md, long dataLen) {
 116 
 117                 md.update(data);
 118             }
 119         },
 120 
 121         UPDATE_BUFFER_LEN {
 122             @Override
 123             public void updateDigest(byte[] data,
 124                     MessageDigest md, long dataLen) {
 125 
 126                 for (int i = 0; i < dataLen; i++) {
 127                     md.update(data, i, 1);
 128                 }
 129             }
 130         },
 131 
 132         UPDATE_BYTE_BUFFER {
 133             @Override
 134             public void updateDigest(byte[] data,
 135                     MessageDigest md, long dataLen) {
 136 
 137                 md.update(ByteBuffer.wrap(data));
 138             }
 139         };
 140 
 141         public abstract void updateDigest(byte[] data,
 142                     MessageDigest md, long dataLen);
 143         }
 144 }
--- EOF ---