Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/pkcs11/MessageDigest/TestCloning.java
38855 views
/*1* Copyright (c) 2012, 2016, 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 641489926* @summary Ensure the cloning functionality works.27* @author Valerie Peng28* @library ..29* @key randomness30* @run main/othervm TestCloning31* @run main/othervm TestCloning sm32*/3334import java.security.MessageDigest;35import java.security.Provider;36import java.util.Arrays;37import java.util.Random;3839public class TestCloning extends PKCS11Test {4041private static final String[] ALGOS = {42"MD2", "MD5", "SHA1", "SHA-224", "SHA-256", "SHA-384", "SHA-512"43};4445public static void main(String[] args) throws Exception {46main(new TestCloning(), args);47}4849private static final byte[] data1 = new byte[10];50private static final byte[] data2 = new byte[10*1024];515253@Override54public void main(Provider p) throws Exception {55Random r = new Random();56byte[] data1 = new byte[10];57byte[] data2 = new byte[2*1024];58r.nextBytes(data1);59r.nextBytes(data2);60System.out.println("Testing against provider " + p.getName());61for (int i = 0; i < ALGOS.length; i++) {62if (p.getService("MessageDigest", ALGOS[i]) == null) {63System.out.println(ALGOS[i] + " is not supported, skipping");64continue;65} else {66System.out.println("Testing " + ALGOS[i] + " of " + p.getName());67MessageDigest md = MessageDigest.getInstance(ALGOS[i], p);68try {69md = testCloning(md, p);70// repeat the test again after generating digest once71for (int j = 0; j < 10; j++) {72md = testCloning(md, p);73}74} catch (Exception ex) {75if (ALGOS[i] == "MD2" &&76p.getName().equalsIgnoreCase("SunPKCS11-NSS")) {77// known bug in NSS; ignore for now78System.out.println("Ignore Known bug in MD2 of NSS");79continue;80}81throw ex;82}83}84}85}8687private static MessageDigest testCloning(MessageDigest mdObj, Provider p)88throws Exception {8990// copy#0: clone at state BLANK w/o any data91MessageDigest mdCopy0 = (MessageDigest) mdObj.clone();9293// copy#1: clone again at state BUFFERED w/ very short data94mdObj.update(data1);95mdCopy0.update(data1);96MessageDigest mdCopy1 = (MessageDigest) mdObj.clone();9798// copy#2: clone again after updating it w/ long data to trigger99// the state into INIT100mdObj.update(data2);101mdCopy0.update(data2);102mdCopy1.update(data2);103MessageDigest mdCopy2 = (MessageDigest) mdObj.clone();104105// copy#3: clone again after updating it w/ very short data106mdObj.update(data1);107mdCopy0.update(data1);108mdCopy1.update(data1);109mdCopy2.update(data1);110MessageDigest mdCopy3 = (MessageDigest) mdObj.clone();111112// copy#4: clone again after updating it w/ long data113mdObj.update(data2);114mdCopy0.update(data2);115mdCopy1.update(data2);116mdCopy2.update(data2);117mdCopy3.update(data2);118MessageDigest mdCopy4 = (MessageDigest) mdObj.clone();119120// check digest equalities121byte[] answer = mdObj.digest();122byte[] result0 = mdCopy0.digest();123byte[] result1 = mdCopy1.digest();124byte[] result2 = mdCopy2.digest();125byte[] result3 = mdCopy3.digest();126byte[] result4 = mdCopy4.digest();127128129check(answer, result0, "copy0");130check(answer, result1, "copy1");131check(answer, result2, "copy2");132check(answer, result3, "copy3");133check(answer, result4, "copy4");134135return mdCopy3;136}137138private static void check(byte[] d1, byte[] d2, String copyName)139throws Exception {140if (Arrays.equals(d1, d2) == false) {141throw new RuntimeException(copyName + " digest mismatch!");142}143}144}145146147148