325N/A/**
325N/A * @test
325N/A * @bug 4946388
325N/A * @summary Unit test for CertificateRevokedException
325N/A */
325N/A
325N/Aimport java.io.ByteArrayInputStream;
325N/Aimport java.io.ByteArrayOutputStream;
325N/Aimport java.io.ObjectInputStream;
325N/Aimport java.io.ObjectOutputStream;
325N/Aimport java.security.cert.CertificateRevokedException;
325N/Aimport java.security.cert.CRLReason;
325N/Aimport java.security.cert.Extension;
325N/Aimport java.util.Calendar;
325N/Aimport java.util.Date;
325N/Aimport java.util.HashMap;
325N/Aimport java.util.Map;
325N/Aimport javax.security.auth.x500.X500Principal;
325N/A
325N/Aimport sun.security.x509.InvalidityDateExtension;
325N/A
325N/Apublic class Basic {
325N/A
325N/A public static void main(String[] args) throws Exception {
325N/A
325N/A // test ctor for NPE
325N/A CertificateRevokedException cre = null;
325N/A try {
325N/A cre = new CertificateRevokedException(null, null, null, null);
325N/A throw new Exception("Did not throw expected NullPointerException");
325N/A } catch (NullPointerException npe) {}
325N/A
325N/A // test getRevocationDate returns clone
325N/A Date date = Calendar.getInstance().getTime();
325N/A long time = date.getTime();
325N/A Map<String, Extension> extensions = new HashMap<String, Extension>();
325N/A Date invDate = new Date(date.getTime());
325N/A extensions.put("2.5.29.24", new InvalidityDateExtension(invDate));
325N/A cre = new CertificateRevokedException(date, CRLReason.UNSPECIFIED,
325N/A new X500Principal("CN=TestCA"), extensions);
325N/A Date date2 = cre.getRevocationDate();
325N/A if (date2 == date) {
325N/A throw new Exception("getRevocationDate does not return copy");
325N/A }
325N/A
325N/A // test getRevocationDate returns the same date as specified in ctor
325N/A if (!date.equals(date2)) {
325N/A throw new Exception("getRevocationDate returns different date");
325N/A }
325N/A
325N/A // test ctor copies date
325N/A date.setTime(777);
325N/A date2 = cre.getRevocationDate();
325N/A if (date2.getTime() != time) {
325N/A throw new Exception("Constructor did not copy date parameter");
325N/A }
325N/A
325N/A // test getReason returns same reason as specified in ctor
325N/A CRLReason reason = cre.getRevocationReason();
325N/A if (reason != CRLReason.UNSPECIFIED) {
325N/A throw new Exception("getRevocationReason returns different reason");
325N/A }
325N/A
325N/A // test getAuthorityName returns same name as specified in ctor
325N/A if (!cre.getAuthorityName().equals(new X500Principal("CN=TestCA"))) {
325N/A throw new Exception("getAuthorityName returns different name");
325N/A }
325N/A
325N/A // test getInvalidityDate returns invalidity date
325N/A Date invalidity = cre.getInvalidityDate();
325N/A if (invalidity == null) {
325N/A throw new Exception("getInvalidityDate returned null");
325N/A }
325N/A if (invalidity.getTime() != time) {
325N/A throw new Exception("getInvalidityDate returned wrong date");
325N/A }
// test getInvalidityDate copies date
invDate.setTime(777);
if (invalidity.getTime() != time) {
throw new Exception("getInvalidityDate did not return copy of date");
}
// test serialization
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(cre);
oos.close();
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
ObjectInputStream ois = new ObjectInputStream(bais);
CertificateRevokedException cre2 =
(CertificateRevokedException) ois.readObject();
if (cre2.getRevocationDate().getTime() != time) {
throw new Exception("deserialized exception returns different date");
}
if (cre2.getRevocationReason() != CRLReason.UNSPECIFIED) {
throw new Exception("deserialized exception returns different reason");
}
if (!cre2.getAuthorityName().equals(new X500Principal("CN=TestCA"))) {
throw new Exception("deserialized exception returns different name");
}
if (!cre2.getExtensions().keySet().equals(cre.getExtensions().keySet())) {
throw new Exception("deserialized exception returns different extensions");
}
}
}