blob: 4c6f41f01bfd6b22ba0942c4b879e1b00becb9c5 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2002 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 */
23
24/**
25 * @test
26 * @bug 4684793
27 * @summary verify that the RFC3280 policy processing changes are implemented correctly
28 * @author Andreas Sterbenz
29 */
30
31import java.io.*;
32import java.util.*;
33
34import java.security.cert.*;
35
36public class TestPolicy {
37
38 private final static String BASE = System.getProperty("test.src");
39
40 private static CertificateFactory factory;
41
42 private static X509Certificate loadCertificate(String name) throws Exception {
43 InputStream in = new FileInputStream(new File(BASE, name));
44 X509Certificate cert = (X509Certificate)factory.generateCertificate(in);
45 in.close();
46 return cert;
47 }
48
49 private static class TestCase {
50 final String resultTree;
51 final Set initialPolicies;
52 TestCase(String resultTree, String p1, String p2, String p3) {
53 this.resultTree = resultTree;
54 this.initialPolicies = new HashSet();
55 initialPolicies.add(p1);
56 initialPolicies.add(p2);
57 initialPolicies.add(p3);
58 initialPolicies.remove(null);
59 }
60 public String toString() {
61 return initialPolicies.toString();
62 }
63 }
64
65 private final static TestCase[] TEST_CASES = new TestCase[] {
66 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),
67 new TestCase("2.5.29.32.0[1.2.0[1.2.0]]", "1.2.0", null, null),
68 new TestCase("2.5.29.32.0[2.5.29.32.0[1.2.1]]", "1.2.1", null, null),
69 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),
70 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),
71 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"),
72 };
73
74 public static void main(String[] args) throws Exception {
75 factory = CertificateFactory.getInstance("X.509");
76
77 X509Certificate anchor = loadCertificate("anchor.cer");
78 X509Certificate ca = loadCertificate("ca.cer");
79 X509Certificate ee = loadCertificate("ee.cer");
80
81 for (int i = 0; i < TEST_CASES.length; i++) {
82 TestCase testCase = TEST_CASES[i];
83 System.out.println("*** Running test: " + testCase);
84 CertPathValidator validator = CertPathValidator.getInstance("PKIX");
85
86 PKIXParameters params = new PKIXParameters(Collections.singleton(new TrustAnchor(anchor, null)));
87 params.setRevocationEnabled(false);
88 params.setInitialPolicies(testCase.initialPolicies);
89
90 CertPath path = factory.generateCertPath(Arrays.asList(new X509Certificate[] {ee, ca}));
91
92 PKIXCertPathValidatorResult result = (PKIXCertPathValidatorResult)validator.validate(path, params);
93
94 PolicyNode tree = result.getPolicyTree();
95 System.out.println(tree);
96
97 String resultTree = toString(tree);
98 if (resultTree.equals(testCase.resultTree) == false) {
99 System.out.println("Mismatch");
100 System.out.println("Should: " + testCase.resultTree);
101 System.out.println("Is: " + resultTree);
102 throw new Exception("Test failed: " + testCase);
103 }
104 }
105 }
106
107 private static String toString(PolicyNode tree) {
108 if (tree == null) {
109 return "";
110 }
111 Iterator t = tree.getChildren();
112 if (t.hasNext() == false) {
113 return tree.getValidPolicy();
114 }
115 StringBuffer sb = new StringBuffer();
116 List list = new ArrayList();
117 while (t.hasNext()) {
118 PolicyNode next = (PolicyNode)t.next();
119 list.add(toString(next));
120 }
121 Collections.sort(list);
122 return tree.getValidPolicy() + list;
123 }
124}