Path: blob/master/test/jdk/java/security/cert/pkix/policyChanges/TestPolicy.java
41154 views
/*1* Copyright (c) 2002, 2012, 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// This test case relies on updated static security property, no way to re-use24// security property in samevm/agentvm mode.2526/**27* @test28* @bug 468479329* @summary verify that the RFC3280 policy processing changes are30* implemented correctly31* @run main/othervm TestPolicy32* @author Andreas Sterbenz33*/3435import java.io.*;36import java.util.*;3738import java.security.Security;39import java.security.cert.*;4041public class TestPolicy {4243private final static String BASE = System.getProperty("test.src");4445private static CertificateFactory factory;4647private static X509Certificate loadCertificate(String name) throws Exception {48InputStream in = new FileInputStream(new File(BASE, name));49X509Certificate cert = (X509Certificate)factory.generateCertificate(in);50in.close();51return cert;52}5354private static class TestCase {55final String resultTree;56final Set initialPolicies;57TestCase(String resultTree, String p1, String p2, String p3) {58this.resultTree = resultTree;59this.initialPolicies = new HashSet();60initialPolicies.add(p1);61initialPolicies.add(p2);62initialPolicies.add(p3);63initialPolicies.remove(null);64}65public String toString() {66return initialPolicies.toString();67}68}6970private final static TestCase[] TEST_CASES = new TestCase[] {71new 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),72new TestCase("2.5.29.32.0[1.2.0[1.2.0]]", "1.2.0", null, null),73new TestCase("2.5.29.32.0[2.5.29.32.0[1.2.1]]", "1.2.1", null, null),74new 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),75new TestCase("2.5.29.32.0[2.5.29.32.0[1.2.1, 1.2.2]]", "1.2.1", "1.2.2", null),76new 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"),77};7879public static void main(String[] args) throws Exception {80// reset the security property to make sure that the algorithms81// and keys used in this test are not disabled.82Security.setProperty("jdk.certpath.disabledAlgorithms", "MD2");8384factory = CertificateFactory.getInstance("X.509");8586X509Certificate anchor = loadCertificate("anchor.cer");87X509Certificate ca = loadCertificate("ca.cer");88X509Certificate ee = loadCertificate("ee.cer");8990for (int i = 0; i < TEST_CASES.length; i++) {91TestCase testCase = TEST_CASES[i];92System.out.println("*** Running test: " + testCase);93CertPathValidator validator = CertPathValidator.getInstance("PKIX");9495PKIXParameters params = new PKIXParameters(Collections.singleton(new TrustAnchor(anchor, null)));96params.setRevocationEnabled(false);97params.setInitialPolicies(testCase.initialPolicies);9899CertPath path = factory.generateCertPath(Arrays.asList(new X509Certificate[] {ee, ca}));100101PKIXCertPathValidatorResult result = (PKIXCertPathValidatorResult)validator.validate(path, params);102103PolicyNode tree = result.getPolicyTree();104System.out.println(tree);105106String resultTree = toString(tree);107if (resultTree.equals(testCase.resultTree) == false) {108System.out.println("Mismatch");109System.out.println("Should: " + testCase.resultTree);110System.out.println("Is: " + resultTree);111throw new Exception("Test failed: " + testCase);112}113}114}115116private static String toString(PolicyNode tree) {117if (tree == null) {118return "";119}120Iterator t = tree.getChildren();121if (t.hasNext() == false) {122return tree.getValidPolicy();123}124StringBuffer sb = new StringBuffer();125List list = new ArrayList();126while (t.hasNext()) {127PolicyNode next = (PolicyNode)t.next();128list.add(toString(next));129}130Collections.sort(list);131return tree.getValidPolicy() + list;132}133}134135136