Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/jdk17u
Path: blob/master/test/jdk/sun/security/x509/AlgorithmId/ExtensibleAlgorithmId.java
66645 views
1
/*
2
* Copyright (c) 1998, 2021, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
/*
25
* @test
26
* @bug 4162868 8130181 8242151 8267397
27
* @modules java.base/sun.security.x509
28
* @modules java.base/sun.security.util
29
* @run main/othervm ExtensibleAlgorithmId
30
* @summary Check that AlgorithmId Name-to-OID mapping is extensible and
31
* up-to-date.
32
*/
33
34
import java.security.*;
35
import sun.security.x509.AlgorithmId;
36
37
public class ExtensibleAlgorithmId {
38
39
private static void test(String alg, String expOid) throws Exception {
40
System.out.println("Testing " + alg + " and " + expOid );
41
try {
42
AlgorithmId algid = AlgorithmId.get(alg);
43
if (expOid == null) {
44
throw new Exception("Expected NSAE not thrown");
45
}
46
if (!expOid.equals(algid.getOID().toString())) {
47
throw new Exception("Oid mismatch, expected " + expOid +
48
", got " + algid.getOID().toString());
49
}
50
if (!alg.equals(algid.getName())) {
51
throw new Exception("Name mismatch, expected " + alg +
52
", got " + algid.getName());
53
}
54
// try AlgorithmId.get() using 'expOid' if (alg != expOid)
55
if (alg != expOid) {
56
algid = AlgorithmId.get(expOid);
57
if (!expOid.equals(algid.getOID().toString())) {
58
throw new Exception("Oid2 mismatch, expected " + expOid +
59
", got " + algid.getOID().toString());
60
}
61
if (!alg.equals(algid.getName())) {
62
throw new Exception("Name2 mismatch, expected " + alg +
63
", got " + algid.getName());
64
}
65
}
66
System.out.println(" => passed");
67
} catch (NoSuchAlgorithmException nsae) {
68
if (expOid != null) {
69
nsae.printStackTrace();
70
throw new Exception("Unexpected NSAE for " + alg);
71
}
72
System.out.println(" => expected NSAE thrown");
73
}
74
}
75
76
public static void main(String[] args) throws Exception {
77
78
TestProvider p = new TestProvider();
79
String alias = "Alg.Alias.Signature.OID." + TestProvider.ALG_OID;
80
String stdAlgName = p.getProperty(alias);
81
if (stdAlgName == null ||
82
!stdAlgName.equalsIgnoreCase(TestProvider.ALG_NAME)) {
83
throw new Exception("Wrong OID");
84
}
85
86
// scenario#1: test before adding TestProvider
87
System.out.println("Before adding test provider");
88
test(TestProvider.ALG_NAME, null);
89
test(TestProvider.ALG_OID, TestProvider.ALG_OID);
90
test(TestProvider.ALG_OID2, TestProvider.ALG_OID2);
91
92
Security.addProvider(p);
93
// scenario#2: test again after adding TestProvider
94
System.out.println("After adding test provider");
95
test(TestProvider.ALG_NAME, TestProvider.ALG_OID);
96
test(TestProvider.ALG_OID2, TestProvider.ALG_OID2);
97
98
Security.removeProvider(p.getName());
99
// scenario#3: test after removing TestProvider; should be same as
100
// scenario#1
101
System.out.println("After removing test provider");
102
test(TestProvider.ALG_NAME, null);
103
test(TestProvider.ALG_OID, TestProvider.ALG_OID);
104
test(TestProvider.ALG_OID2, TestProvider.ALG_OID2);
105
}
106
107
static class TestProvider extends Provider {
108
109
static String ALG_OID = "1.2.3.4.5.6.7.8.9.0";
110
static String ALG_OID2 = "0.2.7.6.5.4.3.2.1.0";
111
static String ALG_NAME = "XYZ";
112
113
public TestProvider() {
114
super("Dummy", "1.0", "XYZ algorithm");
115
116
put("Signature." + ALG_NAME, "test.xyz");
117
// preferred OID for name<->oid mapping
118
put("Alg.Alias.Signature.OID." + ALG_OID, ALG_NAME);
119
put("Alg.Alias.Signature." + ALG_OID2, ALG_NAME);
120
}
121
}
122
}
123
124