Path: blob/master/test/jdk/java/security/cert/PKIXRevocationChecker/UnitTest.java
41153 views
/*1* Copyright (c) 2012, 2013, 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 6854712 7171570 8010748 802528726* @summary Basic unit test for PKIXRevocationChecker27*/2829import java.io.ByteArrayInputStream;30import java.io.InputStream;31import java.io.IOException;32import java.io.OutputStream;33import java.net.URI;34import java.security.cert.*;35import java.security.cert.PKIXRevocationChecker.Option;36import java.util.*;3738public class UnitTest {3940public static void main(String[] args) throws Exception {4142CertPathValidator cpv = CertPathValidator.getInstance("PKIX");43CertPathChecker cpc = cpv.getRevocationChecker();44PKIXRevocationChecker prc = (PKIXRevocationChecker)cpc;4546prc.init(false);4748System.out.println("Testing that get methods return null or " +49"empty lists/sets/maps");50requireNull(prc.getOcspResponder(), "getOcspResponder()");51requireNull(prc.getOcspResponderCert(), "getOcspResponderCert()");52requireEmpty(prc.getOcspExtensions(), "getOcspExtensions()");53requireEmpty(prc.getOcspResponses(), "getOcspResponses()");54requireEmpty(prc.getOptions(), "getOptions()");55requireEmpty(prc.getSoftFailExceptions(), "getSoftFailExceptions()");5657System.out.println("Testing that get methods return same parameters " +58"that are passed to set methods");59URI uri = new URI("http://localhost");60prc.setOcspResponder(uri);61requireEquals(uri, prc.getOcspResponder(), "getOcspResponder()");6263X509Certificate cert = getCert();64prc.setOcspResponderCert(cert);65requireEquals(cert, prc.getOcspResponderCert(),66"getOcspResponderCert()");6768List<Extension> exts = new ArrayList<>();69for (String oid : cert.getNonCriticalExtensionOIDs()) {70System.out.println(oid);71exts.add(new ExtensionImpl(oid,72cert.getExtensionValue(oid), false));73}74prc.setOcspExtensions(exts);75requireEquals(exts, prc.getOcspExtensions(), "getOcspExtensions()");7677Set<Option> options = EnumSet.of(Option.ONLY_END_ENTITY);78prc.setOptions(options);79requireEquals(options, prc.getOptions(), "getOptions()");8081System.out.println("Testing that parameters are re-initialized to " +82"default values if null is passed to set methods");83prc.setOcspResponder(null);84requireNull(prc.getOcspResponder(), "getOcspResponder()");85prc.setOcspResponderCert(null);86requireNull(prc.getOcspResponderCert(), "getOcspResponderCert()");87prc.setOcspExtensions(null);88requireEmpty(prc.getOcspExtensions(), "getOcspExtensions()");89prc.setOcspResponses(null);90requireEmpty(prc.getOcspResponses(), "getOcspResponses()");91prc.setOptions(null);92requireEmpty(prc.getOptions(), "getOptions()");9394System.out.println("Testing that getRevocationChecker returns new " +95"instance each time");96CertPathChecker first = cpv.getRevocationChecker();97CertPathChecker second = cpv.getRevocationChecker();98if (first == second) {99throw new Exception("FAILED: CertPathCheckers not new instances");100}101CertPathBuilder cpb = CertPathBuilder.getInstance("PKIX");102first = cpb.getRevocationChecker();103second = cpb.getRevocationChecker();104if (first == second) {105throw new Exception("FAILED: CertPathCheckers not new instances");106}107}108109static void requireNull(Object o, String msg) throws Exception {110if (o != null) {111throw new Exception("FAILED: " + msg + " must return null");112}113}114115static void requireEmpty(Map<?,?> m, String msg) throws Exception {116if (!m.isEmpty()) {117throw new Exception("FAILED: " + msg + " must return an empty map");118}119}120121static void requireEmpty(List<?> l, String msg) throws Exception {122if (!l.isEmpty()) {123throw new Exception("FAILED: " + msg +" must return an empty list");124}125}126127static void requireEmpty(Set<?> s, String msg) throws Exception {128if (!s.isEmpty()) {129throw new Exception("FAILED: " + msg + " must return an empty set");130}131}132133static void requireEquals(Object a, Object b, String msg) throws Exception {134if (!a.equals(b)) {135throw new Exception("FAILED: " + msg + " does not return the " +136"same object that was set");137}138}139140static X509Certificate getCert() throws Exception {141String b64 =142"-----BEGIN CERTIFICATE-----\n" +143"MIIBLTCB2KADAgECAgEDMA0GCSqGSIb3DQEBBAUAMA0xCzAJBgNVBAMTAkNBMB4X\n" +144"DTAyMTEwNzExNTcwM1oXDTIyMTEwNzExNTcwM1owFTETMBEGA1UEAxMKRW5kIEVu\n" +145"dGl0eTBcMA0GCSqGSIb3DQEBAQUAA0sAMEgCQQDVBDfF+uBr5s5jzzDs1njKlZNt\n" +146"h8hHzEt3ASh67Peos+QrDzgpUyFXT6fdW2h7iPf0ifjM8eW2xa+3EnPjjU5jAgMB\n" +147"AAGjGzAZMBcGA1UdIAQQMA4wBgYEVR0gADAEBgIqADANBgkqhkiG9w0BAQQFAANB\n" +148"AFo//WOboCNOCcA1fvcWW9oc4MvV8ZPvFIAbyEbgyFd4id5lGDTRbRPvvNZRvdsN\n" +149"NM2gXYr+f87NHIXc9EF3pzw=\n" +150"-----END CERTIFICATE-----";151152InputStream is = new ByteArrayInputStream(b64.getBytes("UTF-8"));153CertificateFactory cf = CertificateFactory.getInstance("X.509");154return (X509Certificate)cf.generateCertificate(is);155}156157static class ExtensionImpl implements Extension {158private final String oid;159private final byte[] val;160private final boolean critical;161162ExtensionImpl(String oid, byte[] val, boolean critical) {163this.oid = oid;164this.val = val;165this.critical = critical;166}167168public void encode(OutputStream out) throws IOException {169throw new UnsupportedOperationException();170}171172public String getId() {173return oid;174}175176public byte[] getValue() {177return val.clone();178}179180public boolean isCritical() {181return critical;182}183}184}185186187