Path: blob/master/test/jdk/sun/security/x509/AlgorithmId/ExtensibleAlgorithmId.java
66645 views
/*1* Copyright (c) 1998, 2021, 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 4162868 8130181 8242151 826739726* @modules java.base/sun.security.x50927* @modules java.base/sun.security.util28* @run main/othervm ExtensibleAlgorithmId29* @summary Check that AlgorithmId Name-to-OID mapping is extensible and30* up-to-date.31*/3233import java.security.*;34import sun.security.x509.AlgorithmId;3536public class ExtensibleAlgorithmId {3738private static void test(String alg, String expOid) throws Exception {39System.out.println("Testing " + alg + " and " + expOid );40try {41AlgorithmId algid = AlgorithmId.get(alg);42if (expOid == null) {43throw new Exception("Expected NSAE not thrown");44}45if (!expOid.equals(algid.getOID().toString())) {46throw new Exception("Oid mismatch, expected " + expOid +47", got " + algid.getOID().toString());48}49if (!alg.equals(algid.getName())) {50throw new Exception("Name mismatch, expected " + alg +51", got " + algid.getName());52}53// try AlgorithmId.get() using 'expOid' if (alg != expOid)54if (alg != expOid) {55algid = AlgorithmId.get(expOid);56if (!expOid.equals(algid.getOID().toString())) {57throw new Exception("Oid2 mismatch, expected " + expOid +58", got " + algid.getOID().toString());59}60if (!alg.equals(algid.getName())) {61throw new Exception("Name2 mismatch, expected " + alg +62", got " + algid.getName());63}64}65System.out.println(" => passed");66} catch (NoSuchAlgorithmException nsae) {67if (expOid != null) {68nsae.printStackTrace();69throw new Exception("Unexpected NSAE for " + alg);70}71System.out.println(" => expected NSAE thrown");72}73}7475public static void main(String[] args) throws Exception {7677TestProvider p = new TestProvider();78String alias = "Alg.Alias.Signature.OID." + TestProvider.ALG_OID;79String stdAlgName = p.getProperty(alias);80if (stdAlgName == null ||81!stdAlgName.equalsIgnoreCase(TestProvider.ALG_NAME)) {82throw new Exception("Wrong OID");83}8485// scenario#1: test before adding TestProvider86System.out.println("Before adding test provider");87test(TestProvider.ALG_NAME, null);88test(TestProvider.ALG_OID, TestProvider.ALG_OID);89test(TestProvider.ALG_OID2, TestProvider.ALG_OID2);9091Security.addProvider(p);92// scenario#2: test again after adding TestProvider93System.out.println("After adding test provider");94test(TestProvider.ALG_NAME, TestProvider.ALG_OID);95test(TestProvider.ALG_OID2, TestProvider.ALG_OID2);9697Security.removeProvider(p.getName());98// scenario#3: test after removing TestProvider; should be same as99// scenario#1100System.out.println("After removing test provider");101test(TestProvider.ALG_NAME, null);102test(TestProvider.ALG_OID, TestProvider.ALG_OID);103test(TestProvider.ALG_OID2, TestProvider.ALG_OID2);104}105106static class TestProvider extends Provider {107108static String ALG_OID = "1.2.3.4.5.6.7.8.9.0";109static String ALG_OID2 = "0.2.7.6.5.4.3.2.1.0";110static String ALG_NAME = "XYZ";111112public TestProvider() {113super("Dummy", "1.0", "XYZ algorithm");114115put("Signature." + ALG_NAME, "test.xyz");116// preferred OID for name<->oid mapping117put("Alg.Alias.Signature.OID." + ALG_OID, ALG_NAME);118put("Alg.Alias.Signature." + ALG_OID2, ALG_NAME);119}120}121}122123124