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