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