1 /* 2 * Copyright (c) 2007, 2013, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /** 25 * @test 26 * @bug 4946388 27 * @summary Unit test for CertificateRevokedException 28 */ 29 30 import java.io.ByteArrayInputStream; 31 import java.io.ByteArrayOutputStream; 32 import java.io.ObjectInputStream; 33 import java.io.ObjectOutputStream; 34 import java.security.cert.CertificateRevokedException; 35 import java.security.cert.CRLReason; 36 import java.security.cert.Extension; 37 import java.util.Calendar; 38 import java.util.Date; 39 import java.util.HashMap; 40 import java.util.Map; 41 import javax.security.auth.x500.X500Principal; 42 43 import sun.security.x509.InvalidityDateExtension; 44 45 public class Basic { 46 47 public static void main(String[] args) throws Exception { 48 49 // test ctor for NPE 50 CertificateRevokedException cre = null; 51 try { 52 cre = new CertificateRevokedException(null, null, null, null); 53 throw new Exception("Did not throw expected NullPointerException"); 54 } catch (NullPointerException npe) {} 55 56 // test getRevocationDate returns clone 57 Date date = Calendar.getInstance().getTime(); 58 long time = date.getTime(); 59 Map<String, Extension> extensions = new HashMap<String, Extension>(); 60 Date invDate = new Date(date.getTime()); 61 extensions.put("2.5.29.24", new InvalidityDateExtension(invDate)); 62 cre = new CertificateRevokedException(date, CRLReason.UNSPECIFIED, 63 new X500Principal("CN=TestCA"), extensions); 64 Date date2 = cre.getRevocationDate(); 65 if (date2 == date) { 66 throw new Exception("getRevocationDate does not return copy"); 67 } 68 69 // test getRevocationDate returns the same date as specified in ctor 70 if (!date.equals(date2)) { 71 throw new Exception("getRevocationDate returns different date"); 72 } 73 74 // test ctor copies date 75 date.setTime(777); 76 date2 = cre.getRevocationDate(); 77 if (date2.getTime() != time) { 78 throw new Exception("Constructor did not copy date parameter"); 79 } 80 81 // test getReason returns same reason as specified in ctor 82 CRLReason reason = cre.getRevocationReason(); 83 if (reason != CRLReason.UNSPECIFIED) { 84 throw new Exception("getRevocationReason returns different reason"); 85 } 86 87 // test getAuthorityName returns same name as specified in ctor 88 if (!cre.getAuthorityName().equals(new X500Principal("CN=TestCA"))) { 89 throw new Exception("getAuthorityName returns different name"); 90 } 91 92 // test getInvalidityDate returns invalidity date 93 Date invalidity = cre.getInvalidityDate(); 94 if (invalidity == null) { 95 throw new Exception("getInvalidityDate returned null"); 96 } 97 if (invalidity.getTime() != time) { 98 throw new Exception("getInvalidityDate returned wrong date"); 99 } 100 // test getInvalidityDate copies date 101 invDate.setTime(777); 102 if (invalidity.getTime() != time) { 103 throw new Exception("getInvalidityDate did not return copy of date"); 104 } 105 106 // test serialization 107 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 108 ObjectOutputStream oos = new ObjectOutputStream(baos); 109 oos.writeObject(cre); 110 oos.close(); 111 112 ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); 113 ObjectInputStream ois = new ObjectInputStream(bais); 114 CertificateRevokedException cre2 = 115 (CertificateRevokedException) ois.readObject(); 116 117 if (cre2.getRevocationDate().getTime() != time) { 118 throw new Exception("deserialized exception returns different date"); 119 } 120 if (cre2.getRevocationReason() != CRLReason.UNSPECIFIED) { 121 throw new Exception("deserialized exception returns different reason"); 122 } 123 if (!cre2.getAuthorityName().equals(new X500Principal("CN=TestCA"))) { 124 throw new Exception("deserialized exception returns different name"); 125 } 126 if (!cre2.getExtensions().keySet().equals(cre.getExtensions().keySet())) { 127 throw new Exception("deserialized exception returns different extensions"); 128 } 129 } 130 }