Path: blob/master/test/jdk/java/security/MessageDigest/TestCloneable.java
41149 views
/*1* Copyright (c) 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 824607726* @summary Make sure that digest spi and the resulting digest impl are27* consistent in the impl of Cloneable interface28* @run testng TestCloneable29*/30import java.security.*;31import java.util.Objects;32import org.testng.annotations.DataProvider;33import org.testng.annotations.Test;34import org.testng.Assert;3536public class TestCloneable {3738private static final Class<CloneNotSupportedException> CNSE =39CloneNotSupportedException.class;4041@DataProvider42public Object[][] testData() {43return new Object[][] {44{ "MD2", "SUN" }, { "MD5", "SUN" }, { "SHA-1", "SUN" },45{ "SHA-224", "SUN" }, { "SHA-256", "SUN" },46{ "SHA-384", "SUN" }, { "SHA-512", "SUN" },47{ "SHA3-224", "SUN" }, { "SHA3-256", "SUN" },48{ "SHA3-384", "SUN" }, { "SHA3-512", "SUN" }49};50}5152@Test(dataProvider = "testData")53public void test(String algo, String provName)54throws NoSuchProviderException, NoSuchAlgorithmException,55CloneNotSupportedException {56System.out.print("Testing " + algo + " impl from " + provName);57Provider p = Security.getProvider(provName);58Provider.Service s = p.getService("MessageDigest", algo);59Objects.requireNonNull(s);60MessageDigestSpi spi = (MessageDigestSpi) s.newInstance(null);61MessageDigest md = MessageDigest.getInstance(algo, provName);62if (spi instanceof Cloneable) {63System.out.println(": Cloneable");64Assert.assertTrue(md instanceof Cloneable);65MessageDigest md2 = (MessageDigest) md.clone();66Assert.assertEquals(md2.getAlgorithm(), algo);67Assert.assertEquals(md2.getProvider().getName(), provName);68Assert.assertTrue(md2 instanceof Cloneable);69} else {70System.out.println(": NOT Cloneable");71Assert.assertThrows(CNSE, ()->md.clone());72}73System.out.println("Test Passed");74}75}767778