Path: blob/master/test/jdk/sun/security/pkcs11/MessageDigest/TestCloning.java
41153 views
/*1* Copyright (c) 2012, 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*/2223/*24* @test25* @bug 6414899 824233226* @summary Ensure the cloning functionality works.27* @author Valerie Peng28* @library /test/lib ..29* @key randomness30* @modules jdk.crypto.cryptoki31* @run main/othervm TestCloning32* @run main/othervm -Djava.security.manager=allow TestCloning sm33*/3435import java.security.MessageDigest;36import java.security.Provider;37import java.util.Arrays;38import java.util.Random;39import java.util.List;4041public class TestCloning extends PKCS11Test {4243public static void main(String[] args) throws Exception {44main(new TestCloning(), args);45}4647private static final byte[] data1 = new byte[10];48private static final byte[] data2 = new byte[10*1024];4950@Override51public void main(Provider p) throws Exception {52List<String> ALGS = getSupportedAlgorithms("MessageDigest", "SHA", p);53Random r = new Random();54byte[] data1 = new byte[10];55byte[] data2 = new byte[2*1024];56r.nextBytes(data1);57r.nextBytes(data2);58System.out.println("Testing against provider " + p.getName());59for (String alg : ALGS) {60System.out.println("Testing " + alg);61MessageDigest md = MessageDigest.getInstance(alg, p);62md = testCloning(md, p);63// repeat the test again after generating digest once64for (int j = 0; j < 10; j++) {65md = testCloning(md, p);66}67}68}6970private static MessageDigest testCloning(MessageDigest mdObj, Provider p)71throws Exception {72// copy#0: clone at state BLANK w/o any data73MessageDigest mdCopy0 = (MessageDigest) mdObj.clone();7475// copy#1: clone again at state BUFFERED w/ very short data76mdObj.update(data1);77mdCopy0.update(data1);78MessageDigest mdCopy1 = (MessageDigest) mdObj.clone();7980// copy#2: clone again after updating it w/ long data to trigger81// the state into INIT82mdObj.update(data2);83mdCopy0.update(data2);84mdCopy1.update(data2);85MessageDigest mdCopy2 = (MessageDigest) mdObj.clone();8687// copy#3: clone again after updating it w/ very short data88mdObj.update(data1);89mdCopy0.update(data1);90mdCopy1.update(data1);91mdCopy2.update(data1);92MessageDigest mdCopy3 = (MessageDigest) mdObj.clone();9394// copy#4: clone again after updating it w/ long data95mdObj.update(data2);96mdCopy0.update(data2);97mdCopy1.update(data2);98mdCopy2.update(data2);99mdCopy3.update(data2);100MessageDigest mdCopy4 = (MessageDigest) mdObj.clone();101102// check digest equalities103byte[] answer = mdObj.digest();104byte[] result0 = mdCopy0.digest();105byte[] result1 = mdCopy1.digest();106byte[] result2 = mdCopy2.digest();107byte[] result3 = mdCopy3.digest();108byte[] result4 = mdCopy4.digest();109110111check(answer, result0, "copy0");112check(answer, result1, "copy1");113check(answer, result2, "copy2");114check(answer, result3, "copy3");115check(answer, result4, "copy4");116117return mdCopy3;118}119120private static void check(byte[] d1, byte[] d2, String copyName)121throws Exception {122if (Arrays.equals(d1, d2) == false) {123throw new RuntimeException(copyName + " digest mismatch!");124}125}126}127128129130