Basic.java revision 9330:8b1f1c2a400f
1219820Sjeff/* 2219820Sjeff * Copyright (c) 2007, 2013, Oracle and/or its affiliates. All rights reserved. 3219820Sjeff * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4219820Sjeff * 5219820Sjeff * This code is free software; you can redistribute it and/or modify it 6219820Sjeff * under the terms of the GNU General Public License version 2 only, as 7219820Sjeff * published by the Free Software Foundation. 8219820Sjeff * 9219820Sjeff * This code is distributed in the hope that it will be useful, but WITHOUT 10219820Sjeff * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11219820Sjeff * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12219820Sjeff * version 2 for more details (a copy is included in the LICENSE file that 13219820Sjeff * accompanied this code). 14219820Sjeff * 15219820Sjeff * You should have received a copy of the GNU General Public License version 16219820Sjeff * 2 along with this work; if not, write to the Free Software Foundation, 17219820Sjeff * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18219820Sjeff * 19219820Sjeff * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20219820Sjeff * or visit www.oracle.com if you need additional information or have any 21219820Sjeff * questions. 22219820Sjeff */ 23219820Sjeff 24219820Sjeff/** 25219820Sjeff * @test 26219820Sjeff * @bug 4946388 27219820Sjeff * @summary Unit test for CertificateRevokedException 28219820Sjeff */ 29219820Sjeff 30219820Sjeffimport java.io.ByteArrayInputStream; 31219820Sjeffimport java.io.ByteArrayOutputStream; 32219820Sjeffimport java.io.ObjectInputStream; 33219820Sjeffimport java.io.ObjectOutputStream; 34219820Sjeffimport java.security.cert.CertificateRevokedException; 35219820Sjeffimport java.security.cert.CRLReason; 36219820Sjeffimport java.security.cert.Extension; 37219820Sjeffimport java.util.Calendar; 38219820Sjeffimport java.util.Date; 39219820Sjeffimport java.util.HashMap; 40219820Sjeffimport java.util.Map; 41219820Sjeffimport javax.security.auth.x500.X500Principal; 42219820Sjeff 43219820Sjeffimport sun.security.x509.InvalidityDateExtension; 44219820Sjeff 45219820Sjeffpublic class Basic { 46219820Sjeff 47219820Sjeff public static void main(String[] args) throws Exception { 48219820Sjeff 49219820Sjeff // test ctor for NPE 50219820Sjeff CertificateRevokedException cre = null; 51219820Sjeff try { 52219820Sjeff cre = new CertificateRevokedException(null, null, null, null); 53219820Sjeff throw new Exception("Did not throw expected NullPointerException"); 54219820Sjeff } catch (NullPointerException npe) {} 55219820Sjeff 56219820Sjeff // test getRevocationDate returns clone 57219820Sjeff Date date = Calendar.getInstance().getTime(); 58219820Sjeff long time = date.getTime(); 59219820Sjeff Map<String, Extension> extensions = new HashMap<String, Extension>(); 60219820Sjeff Date invDate = new Date(date.getTime()); 61219820Sjeff extensions.put("2.5.29.24", new InvalidityDateExtension(invDate)); 62219820Sjeff cre = new CertificateRevokedException(date, CRLReason.UNSPECIFIED, 63219820Sjeff new X500Principal("CN=TestCA"), extensions); 64219820Sjeff Date date2 = cre.getRevocationDate(); 65219820Sjeff if (date2 == date) { 66219820Sjeff throw new Exception("getRevocationDate does not return copy"); 67219820Sjeff } 68219820Sjeff 69219820Sjeff // test getRevocationDate returns the same date as specified in ctor 70219820Sjeff if (!date.equals(date2)) { 71219820Sjeff throw new Exception("getRevocationDate returns different date"); 72219820Sjeff } 73219820Sjeff 74219820Sjeff // test ctor copies date 75219820Sjeff date.setTime(777); 76219820Sjeff date2 = cre.getRevocationDate(); 77219820Sjeff if (date2.getTime() != time) { 78219820Sjeff throw new Exception("Constructor did not copy date parameter"); 79219820Sjeff } 80219820Sjeff 81219820Sjeff // test getReason returns same reason as specified in ctor 82219820Sjeff CRLReason reason = cre.getRevocationReason(); 83219820Sjeff if (reason != CRLReason.UNSPECIFIED) { 84219820Sjeff throw new Exception("getRevocationReason returns different reason"); 85219820Sjeff } 86219820Sjeff 87219820Sjeff // test getAuthorityName returns same name as specified in ctor 88219820Sjeff if (!cre.getAuthorityName().equals(new X500Principal("CN=TestCA"))) { 89219820Sjeff throw new Exception("getAuthorityName returns different name"); 90219820Sjeff } 91219820Sjeff 92219820Sjeff // test getInvalidityDate returns invalidity date 93219820Sjeff Date invalidity = cre.getInvalidityDate(); 94219820Sjeff if (invalidity == null) { 95219820Sjeff throw new Exception("getInvalidityDate returned null"); 96219820Sjeff } 97219820Sjeff if (invalidity.getTime() != time) { 98219820Sjeff throw new Exception("getInvalidityDate returned wrong date"); 99219820Sjeff } 100219820Sjeff // test getInvalidityDate copies date 101219820Sjeff invDate.setTime(777); 102219820Sjeff if (invalidity.getTime() != time) { 103219820Sjeff throw new Exception("getInvalidityDate did not return copy of date"); 104219820Sjeff } 105219820Sjeff 106219820Sjeff // test serialization 107219820Sjeff ByteArrayOutputStream baos = new ByteArrayOutputStream(); 108219820Sjeff ObjectOutputStream oos = new ObjectOutputStream(baos); 109219820Sjeff oos.writeObject(cre); 110219820Sjeff oos.close(); 111219820Sjeff 112219820Sjeff ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); 113219820Sjeff ObjectInputStream ois = new ObjectInputStream(bais); 114219820Sjeff CertificateRevokedException cre2 = 115219820Sjeff (CertificateRevokedException) ois.readObject(); 116219820Sjeff 117219820Sjeff if (cre2.getRevocationDate().getTime() != time) { 118219820Sjeff throw new Exception("deserialized exception returns different date"); 119219820Sjeff } 120219820Sjeff if (cre2.getRevocationReason() != CRLReason.UNSPECIFIED) { 121219820Sjeff throw new Exception("deserialized exception returns different reason"); 122219820Sjeff } 123219820Sjeff if (!cre2.getAuthorityName().equals(new X500Principal("CN=TestCA"))) { 124219820Sjeff throw new Exception("deserialized exception returns different name"); 125219820Sjeff } 126219820Sjeff if (!cre2.getExtensions().keySet().equals(cre.getExtensions().keySet())) { 127219820Sjeff throw new Exception("deserialized exception returns different extensions"); 128219820Sjeff } 129219820Sjeff } 130219820Sjeff} 131219820Sjeff