Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/security/MessageDigest/TestSameLength.java
38811 views
/*1* Copyright (c) 2015, 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*/2223import static java.lang.System.out;2425import java.nio.ByteBuffer;26import java.security.MessageDigest;27import java.util.Random;2829/**30* @test31* @bug 805037132* @summary Check md.getDigestLength() equal digest output length with various33* algorithm/dataLen/(update,digest methods).34* @author Kevin Liu35* @key randomness36*/3738public class TestSameLength {3940public static void main(String[] args) throws Exception {41TestSameLength test = new TestSameLength();42test.run();43}4445private void run() throws Exception {46String[] algorithmArr = {47"SHA", "Sha", "SHA-1", "sha-1", "SHA1", "sha1", "MD5", "md5",48"SHA-224", "SHA-256", "SHA-384", "SHA-512"49};50int[] nUpdatesArr = {510, 1, 2, 352};53int[] dataLenArr = {541, 50, 2500, 125000, 625000055};5657for (String algorithm: algorithmArr) {58for (UpdateMethod update: UpdateMethod.values()) {59for (int dataLen: dataLenArr) {60if (!runTest(algorithm, dataLen, update)) {61throw new RuntimeException(62"Test failed at algorithm/dataLen/numUpdate:"63+ algorithm + "/" + dataLen + "/"64+ update.toString());65}66}67}68}6970out.println("All " + algorithmArr.length * nUpdatesArr.length71* dataLenArr.length + " tests Passed");72}7374private boolean runTest(String algo, long dataLen,75UpdateMethod whichUpdate) throws Exception {76try {77// Do initialization78byte[] data = new byte[(int) dataLen];79new Random().nextBytes(data);80MessageDigest md = MessageDigest.getInstance(algo);81int outputLen = md.getDigestLength();8283// Perform the update using all available/possible update methods84whichUpdate.updateDigest(data, md, dataLen);85// Get the output86byte[] output = md.digest();8788// Compare input and output89return outputLen == output.length;90} catch (Exception ex) {91System.err.println("Testing: " + algo + "/" + dataLen + "/"92+ whichUpdate.toString()93+ " failed with unexpected exception");94ex.printStackTrace();95throw ex;96}97}9899private static enum UpdateMethod {100UPDATE_BYTE {101@Override102public void updateDigest(byte[] data,103MessageDigest md, long dataLen) {104105for (int i = 0; i < dataLen; i++) {106md.update(data[i]);107}108}109},110111UPDATE_BUFFER {112@Override113public void updateDigest(byte[] data,114MessageDigest md, long dataLen) {115116md.update(data);117}118},119120UPDATE_BUFFER_LEN {121@Override122public void updateDigest(byte[] data,123MessageDigest md, long dataLen) {124125for (int i = 0; i < dataLen; i++) {126md.update(data, i, 1);127}128}129},130131UPDATE_BYTE_BUFFER {132@Override133public void updateDigest(byte[] data,134MessageDigest md, long dataLen) {135136md.update(ByteBuffer.wrap(data));137}138};139140public abstract void updateDigest(byte[] data,141MessageDigest md, long dataLen);142}143}144145146