Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/security/cert/pkix/policyChanges/TestPolicy.java
41154 views
1
/*
2
* Copyright (c) 2002, 2012, 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
// This test case relies on updated static security property, no way to re-use
25
// security property in samevm/agentvm mode.
26
27
/**
28
* @test
29
* @bug 4684793
30
* @summary verify that the RFC3280 policy processing changes are
31
* implemented correctly
32
* @run main/othervm TestPolicy
33
* @author Andreas Sterbenz
34
*/
35
36
import java.io.*;
37
import java.util.*;
38
39
import java.security.Security;
40
import java.security.cert.*;
41
42
public class TestPolicy {
43
44
private final static String BASE = System.getProperty("test.src");
45
46
private static CertificateFactory factory;
47
48
private static X509Certificate loadCertificate(String name) throws Exception {
49
InputStream in = new FileInputStream(new File(BASE, name));
50
X509Certificate cert = (X509Certificate)factory.generateCertificate(in);
51
in.close();
52
return cert;
53
}
54
55
private static class TestCase {
56
final String resultTree;
57
final Set initialPolicies;
58
TestCase(String resultTree, String p1, String p2, String p3) {
59
this.resultTree = resultTree;
60
this.initialPolicies = new HashSet();
61
initialPolicies.add(p1);
62
initialPolicies.add(p2);
63
initialPolicies.add(p3);
64
initialPolicies.remove(null);
65
}
66
public String toString() {
67
return initialPolicies.toString();
68
}
69
}
70
71
private final static TestCase[] TEST_CASES = new TestCase[] {
72
new TestCase("2.5.29.32.0[1.2.0[1.2.0], 2.5.29.32.0[2.5.29.32.0]]", "2.5.29.32.0", null, null),
73
new TestCase("2.5.29.32.0[1.2.0[1.2.0]]", "1.2.0", null, null),
74
new TestCase("2.5.29.32.0[2.5.29.32.0[1.2.1]]", "1.2.1", null, null),
75
new TestCase("2.5.29.32.0[1.2.0[1.2.0], 2.5.29.32.0[1.2.1]]", "1.2.0", "1.2.1", null),
76
new TestCase("2.5.29.32.0[2.5.29.32.0[1.2.1, 1.2.2]]", "1.2.1", "1.2.2", null),
77
new TestCase("2.5.29.32.0[1.2.0[1.2.0], 2.5.29.32.0[1.2.1, 1.2.2]]", "1.2.0", "1.2.1", "1.2.2"),
78
};
79
80
public static void main(String[] args) throws Exception {
81
// reset the security property to make sure that the algorithms
82
// and keys used in this test are not disabled.
83
Security.setProperty("jdk.certpath.disabledAlgorithms", "MD2");
84
85
factory = CertificateFactory.getInstance("X.509");
86
87
X509Certificate anchor = loadCertificate("anchor.cer");
88
X509Certificate ca = loadCertificate("ca.cer");
89
X509Certificate ee = loadCertificate("ee.cer");
90
91
for (int i = 0; i < TEST_CASES.length; i++) {
92
TestCase testCase = TEST_CASES[i];
93
System.out.println("*** Running test: " + testCase);
94
CertPathValidator validator = CertPathValidator.getInstance("PKIX");
95
96
PKIXParameters params = new PKIXParameters(Collections.singleton(new TrustAnchor(anchor, null)));
97
params.setRevocationEnabled(false);
98
params.setInitialPolicies(testCase.initialPolicies);
99
100
CertPath path = factory.generateCertPath(Arrays.asList(new X509Certificate[] {ee, ca}));
101
102
PKIXCertPathValidatorResult result = (PKIXCertPathValidatorResult)validator.validate(path, params);
103
104
PolicyNode tree = result.getPolicyTree();
105
System.out.println(tree);
106
107
String resultTree = toString(tree);
108
if (resultTree.equals(testCase.resultTree) == false) {
109
System.out.println("Mismatch");
110
System.out.println("Should: " + testCase.resultTree);
111
System.out.println("Is: " + resultTree);
112
throw new Exception("Test failed: " + testCase);
113
}
114
}
115
}
116
117
private static String toString(PolicyNode tree) {
118
if (tree == null) {
119
return "";
120
}
121
Iterator t = tree.getChildren();
122
if (t.hasNext() == false) {
123
return tree.getValidPolicy();
124
}
125
StringBuffer sb = new StringBuffer();
126
List list = new ArrayList();
127
while (t.hasNext()) {
128
PolicyNode next = (PolicyNode)t.next();
129
list.add(toString(next));
130
}
131
Collections.sort(list);
132
return tree.getValidPolicy() + list;
133
}
134
}
135
136