1 /** 2 * @test 3 * @bug 4946388 4 * @summary Unit test for CertificateRevokedException 5 */ 6 7 import java.io.ByteArrayInputStream; 8 import java.io.ByteArrayOutputStream; 9 import java.io.ObjectInputStream; 10 import java.io.ObjectOutputStream; 11 import java.security.cert.CertificateRevokedException; 12 import java.security.cert.CRLReason; 13 import java.security.cert.Extension; 14 import java.util.Calendar; 15 import java.util.Date; 16 import java.util.HashMap; 17 import java.util.Map; 18 import javax.security.auth.x500.X500Principal; 19 20 import sun.security.x509.InvalidityDateExtension; 21 22 public class Basic { 23 24 public static void main(String[] args) throws Exception { 25 26 // test ctor for NPE 27 CertificateRevokedException cre = null; 28 try { 29 cre = new CertificateRevokedException(null, null, null, null); 30 throw new Exception("Did not throw expected NullPointerException"); 31 } catch (NullPointerException npe) {} 32 33 // test getRevocationDate returns clone 34 Date date = Calendar.getInstance().getTime(); 35 long time = date.getTime(); 36 Map<String, Extension> extensions = new HashMap<String, Extension>(); 37 Date invDate = new Date(date.getTime()); 38 extensions.put("2.5.29.24", new InvalidityDateExtension(invDate)); 39 cre = new CertificateRevokedException(date, CRLReason.UNSPECIFIED, 40 new X500Principal("CN=TestCA"), extensions); 41 Date date2 = cre.getRevocationDate(); 42 if (date2 == date) { 43 throw new Exception("getRevocationDate does not return copy"); 44 } 45 46 // test getRevocationDate returns the same date as specified in ctor 47 if (!date.equals(date2)) { 48 throw new Exception("getRevocationDate returns different date"); 49 } 50 51 // test ctor copies date 52 date.setTime(777); 53 date2 = cre.getRevocationDate(); 54 if (date2.getTime() != time) { 55 throw new Exception("Constructor did not copy date parameter"); 56 } 57 58 // test getReason returns same reason as specified in ctor 59 CRLReason reason = cre.getRevocationReason(); 60 if (reason != CRLReason.UNSPECIFIED) { 61 throw new Exception("getRevocationReason returns different reason"); 62 } 63 64 // test getAuthorityName returns same name as specified in ctor 65 if (!cre.getAuthorityName().equals(new X500Principal("CN=TestCA"))) { 66 throw new Exception("getAuthorityName returns different name"); 67 } 68 69 // test getInvalidityDate returns invalidity date 70 Date invalidity = cre.getInvalidityDate(); 71 if (invalidity == null) { 72 throw new Exception("getInvalidityDate returned null"); 73 } 74 if (invalidity.getTime() != time) { 75 throw new Exception("getInvalidityDate returned wrong date"); 76 } 77 // test getInvalidityDate copies date 78 invDate.setTime(777); 79 if (invalidity.getTime() != time) { 80 throw new Exception("getInvalidityDate did not return copy of date"); 81 } 82 83 // test serialization 84 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 85 ObjectOutputStream oos = new ObjectOutputStream(baos); 86 oos.writeObject(cre); 87 oos.close(); 88 89 ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); 90 ObjectInputStream ois = new ObjectInputStream(bais); 91 CertificateRevokedException cre2 = 92 (CertificateRevokedException) ois.readObject(); 93 94 if (cre2.getRevocationDate().getTime() != time) { 95 throw new Exception("deserialized exception returns different date"); 96 } 97 if (cre2.getRevocationReason() != CRLReason.UNSPECIFIED) { 98 throw new Exception("deserialized exception returns different reason"); 99 } 100 if (!cre2.getAuthorityName().equals(new X500Principal("CN=TestCA"))) { 101 throw new Exception("deserialized exception returns different name"); 102 } 103 if (!cre2.getExtensions().keySet().equals(cre.getExtensions().keySet())) { 104 throw new Exception("deserialized exception returns different extensions"); 105 } 106 } 107 }