Path: blob/master/test/jdk/java/security/cert/CertificateRevokedException/Basic.java
41153 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* @modules java.base/sun.security.x50928*/2930import java.io.ByteArrayInputStream;31import java.io.ByteArrayOutputStream;32import java.io.ObjectInputStream;33import java.io.ObjectOutputStream;34import java.security.cert.CertificateRevokedException;35import java.security.cert.CRLReason;36import java.security.cert.Extension;37import java.util.Calendar;38import java.util.Date;39import java.util.HashMap;40import java.util.Map;41import javax.security.auth.x500.X500Principal;4243import sun.security.x509.InvalidityDateExtension;4445public class Basic {4647public static void main(String[] args) throws Exception {4849// test ctor for NPE50CertificateRevokedException cre = null;51try {52cre = new CertificateRevokedException(null, null, null, null);53throw new Exception("Did not throw expected NullPointerException");54} catch (NullPointerException npe) {}5556// test getRevocationDate returns clone57Date date = Calendar.getInstance().getTime();58long time = date.getTime();59Map<String, Extension> extensions = new HashMap<String, Extension>();60Date invDate = new Date(date.getTime());61extensions.put("2.5.29.24", new InvalidityDateExtension(invDate));62cre = new CertificateRevokedException(date, CRLReason.UNSPECIFIED,63new X500Principal("CN=TestCA"), extensions);64Date date2 = cre.getRevocationDate();65if (date2 == date) {66throw new Exception("getRevocationDate does not return copy");67}6869// test getRevocationDate returns the same date as specified in ctor70if (!date.equals(date2)) {71throw new Exception("getRevocationDate returns different date");72}7374// test ctor copies date75date.setTime(777);76date2 = cre.getRevocationDate();77if (date2.getTime() != time) {78throw new Exception("Constructor did not copy date parameter");79}8081// test getReason returns same reason as specified in ctor82CRLReason reason = cre.getRevocationReason();83if (reason != CRLReason.UNSPECIFIED) {84throw new Exception("getRevocationReason returns different reason");85}8687// test getAuthorityName returns same name as specified in ctor88if (!cre.getAuthorityName().equals(new X500Principal("CN=TestCA"))) {89throw new Exception("getAuthorityName returns different name");90}9192// test getInvalidityDate returns invalidity date93Date invalidity = cre.getInvalidityDate();94if (invalidity == null) {95throw new Exception("getInvalidityDate returned null");96}97if (invalidity.getTime() != time) {98throw new Exception("getInvalidityDate returned wrong date");99}100// test getInvalidityDate copies date101invDate.setTime(777);102if (invalidity.getTime() != time) {103throw new Exception("getInvalidityDate did not return copy of date");104}105106// test serialization107ByteArrayOutputStream baos = new ByteArrayOutputStream();108ObjectOutputStream oos = new ObjectOutputStream(baos);109oos.writeObject(cre);110oos.close();111112ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());113ObjectInputStream ois = new ObjectInputStream(bais);114CertificateRevokedException cre2 =115(CertificateRevokedException) ois.readObject();116117if (cre2.getRevocationDate().getTime() != time) {118throw new Exception("deserialized exception returns different date");119}120if (cre2.getRevocationReason() != CRLReason.UNSPECIFIED) {121throw new Exception("deserialized exception returns different reason");122}123if (!cre2.getAuthorityName().equals(new X500Principal("CN=TestCA"))) {124throw new Exception("deserialized exception returns different name");125}126if (!cre2.getExtensions().keySet().equals(cre.getExtensions().keySet())) {127throw new Exception("deserialized exception returns different extensions");128}129}130}131132133