Path: blob/master/test/jdk/java/security/MessageDigest/TestSameLength.java
41149 views
/*1* Copyright (c) 2015, 2020, 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;24import java.nio.ByteBuffer;25import java.security.MessageDigest;26import java.security.NoSuchAlgorithmException;27import java.security.Security;28import jdk.test.lib.RandomFactory;2930/**31* @test32* @bug 8050371 815605933* @summary Check md.getDigestLength() equal digest output length with various34* algorithm/dataLen/(update,digest methods).35* @author Kevin Liu36* @key randomness37* @library /test/lib38* @build jdk.test.lib.RandomFactory39* @run main TestSameLength40*/4142public class TestSameLength {4344public static void main(String[] args) throws Exception {45TestSameLength test = new TestSameLength();46test.run();47}4849private void run() throws Exception {50String[] algorithmArr = { "SHA", "Sha", "SHA-1", "sha-1", "SHA1",51"sha1", "MD5", "md5", "SHA-224", "SHA-256", "SHA-384",52"SHA-512", "SHA3-224", "SHA3-256", "SHA3-384", "SHA3-512" };53int[] nUpdatesArr = { 0, 1, 2, 3 };54int[] dataLenArr = { 1, 50, 2500, 125000, 6250000 };5556for (String algorithm : algorithmArr) {57for (UpdateMethod update : UpdateMethod.values()) {58for (int dataLen : dataLenArr) {59if (!runTest(algorithm, dataLen, update)) {60throw new RuntimeException(61"Test failed at algorithm/dataLen/numUpdate:"62+ algorithm + "/" + dataLen + "/"63+ update.toString());64}65}66}67}6869out.println("All "70+ algorithmArr.length * nUpdatesArr.length * dataLenArr.length71+ " tests Passed");72}7374private boolean runTest(String algo, long dataLen, UpdateMethod whichUpdate)75throws Exception {76try {77// Do initialization78byte[] data = new byte[(int) dataLen];79RandomFactory.getRandom().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 (NoSuchAlgorithmException nae) {91throw nae;92} catch (Exception ex) {93System.err.println("Testing: " + algo + "/" + dataLen + "/"94+ whichUpdate.toString()95+ " failed with unexpected exception");96ex.printStackTrace();97throw ex;98}99}100101private static enum UpdateMethod {102UPDATE_BYTE {103@Override104public void updateDigest(byte[] data, MessageDigest md,105long dataLen) {106107for (int i = 0; i < dataLen; i++) {108md.update(data[i]);109}110}111},112113UPDATE_BUFFER {114@Override115public void updateDigest(byte[] data, MessageDigest md,116long dataLen) {117118md.update(data);119}120},121122UPDATE_BUFFER_LEN {123@Override124public void updateDigest(byte[] data, MessageDigest md,125long dataLen) {126127for (int i = 0; i < dataLen; i++) {128md.update(data, i, 1);129}130}131},132133UPDATE_BYTE_BUFFER {134@Override135public void updateDigest(byte[] data, MessageDigest md,136long dataLen) {137138md.update(ByteBuffer.wrap(data));139}140};141142public abstract void updateDigest(byte[] data, MessageDigest md,143long dataLen);144}145}146147148