Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/security/cert/CertificateRevokedException/Basic.java
47626 views
/*1* Copyright (c) 2007, 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 494638826* @summary Unit test for CertificateRevokedException27*/2829import java.io.ByteArrayInputStream;30import java.io.ByteArrayOutputStream;31import java.io.ObjectInputStream;32import java.io.ObjectOutputStream;33import java.security.cert.CertificateRevokedException;34import java.security.cert.CRLReason;35import java.security.cert.Extension;36import java.util.Calendar;37import java.util.Date;38import java.util.HashMap;39import java.util.Map;40import javax.security.auth.x500.X500Principal;4142import sun.security.x509.InvalidityDateExtension;4344public class Basic {4546public static void main(String[] args) throws Exception {4748// test ctor for NPE49CertificateRevokedException cre = null;50try {51cre = new CertificateRevokedException(null, null, null, null);52throw new Exception("Did not throw expected NullPointerException");53} catch (NullPointerException npe) {}5455// test getRevocationDate returns clone56Date date = Calendar.getInstance().getTime();57long time = date.getTime();58Map<String, Extension> extensions = new HashMap<String, Extension>();59Date invDate = new Date(date.getTime());60extensions.put("2.5.29.24", new InvalidityDateExtension(invDate));61cre = new CertificateRevokedException(date, CRLReason.UNSPECIFIED,62new X500Principal("CN=TestCA"), extensions);63Date date2 = cre.getRevocationDate();64if (date2 == date) {65throw new Exception("getRevocationDate does not return copy");66}6768// test getRevocationDate returns the same date as specified in ctor69if (!date.equals(date2)) {70throw new Exception("getRevocationDate returns different date");71}7273// test ctor copies date74date.setTime(777);75date2 = cre.getRevocationDate();76if (date2.getTime() != time) {77throw new Exception("Constructor did not copy date parameter");78}7980// test getReason returns same reason as specified in ctor81CRLReason reason = cre.getRevocationReason();82if (reason != CRLReason.UNSPECIFIED) {83throw new Exception("getRevocationReason returns different reason");84}8586// test getAuthorityName returns same name as specified in ctor87if (!cre.getAuthorityName().equals(new X500Principal("CN=TestCA"))) {88throw new Exception("getAuthorityName returns different name");89}9091// test getInvalidityDate returns invalidity date92Date invalidity = cre.getInvalidityDate();93if (invalidity == null) {94throw new Exception("getInvalidityDate returned null");95}96if (invalidity.getTime() != time) {97throw new Exception("getInvalidityDate returned wrong date");98}99// test getInvalidityDate copies date100invDate.setTime(777);101if (invalidity.getTime() != time) {102throw new Exception("getInvalidityDate did not return copy of date");103}104105// test serialization106ByteArrayOutputStream baos = new ByteArrayOutputStream();107ObjectOutputStream oos = new ObjectOutputStream(baos);108oos.writeObject(cre);109oos.close();110111ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());112ObjectInputStream ois = new ObjectInputStream(bais);113CertificateRevokedException cre2 =114(CertificateRevokedException) ois.readObject();115116if (cre2.getRevocationDate().getTime() != time) {117throw new Exception("deserialized exception returns different date");118}119if (cre2.getRevocationReason() != CRLReason.UNSPECIFIED) {120throw new Exception("deserialized exception returns different reason");121}122if (!cre2.getAuthorityName().equals(new X500Principal("CN=TestCA"))) {123throw new Exception("deserialized exception returns different name");124}125if (!cre2.getExtensions().keySet().equals(cre.getExtensions().keySet())) {126throw new Exception("deserialized exception returns different extensions");127}128}129}130131132