jdk/test/java/security/cert/CertificateRevokedException/Basic.java
author jjg
Mon, 25 Nov 2013 17:42:28 -0800
changeset 21894 3535c1819067
parent 21596 0e3a39f29dbc
child 23010 6dadb192ad81
permissions -rw-r--r--
8028318: [doclint] doclint will reject existing user-written doc comments using custom tags that follow the recommended rules Reviewed-by: darcy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
21596
0e3a39f29dbc 8027696: Incorrect copyright header in the tests
serb
parents: 2
diff changeset
     1
/*
0e3a39f29dbc 8027696: Incorrect copyright header in the tests
serb
parents: 2
diff changeset
     2
 * Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.
0e3a39f29dbc 8027696: Incorrect copyright header in the tests
serb
parents: 2
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0e3a39f29dbc 8027696: Incorrect copyright header in the tests
serb
parents: 2
diff changeset
     4
 *
0e3a39f29dbc 8027696: Incorrect copyright header in the tests
serb
parents: 2
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
0e3a39f29dbc 8027696: Incorrect copyright header in the tests
serb
parents: 2
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
0e3a39f29dbc 8027696: Incorrect copyright header in the tests
serb
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.
0e3a39f29dbc 8027696: Incorrect copyright header in the tests
serb
parents: 2
diff changeset
     8
 *
0e3a39f29dbc 8027696: Incorrect copyright header in the tests
serb
parents: 2
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
0e3a39f29dbc 8027696: Incorrect copyright header in the tests
serb
parents: 2
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0e3a39f29dbc 8027696: Incorrect copyright header in the tests
serb
parents: 2
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
0e3a39f29dbc 8027696: Incorrect copyright header in the tests
serb
parents: 2
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
0e3a39f29dbc 8027696: Incorrect copyright header in the tests
serb
parents: 2
diff changeset
    13
 * accompanied this code).
0e3a39f29dbc 8027696: Incorrect copyright header in the tests
serb
parents: 2
diff changeset
    14
 *
0e3a39f29dbc 8027696: Incorrect copyright header in the tests
serb
parents: 2
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
0e3a39f29dbc 8027696: Incorrect copyright header in the tests
serb
parents: 2
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
0e3a39f29dbc 8027696: Incorrect copyright header in the tests
serb
parents: 2
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0e3a39f29dbc 8027696: Incorrect copyright header in the tests
serb
parents: 2
diff changeset
    18
 *
0e3a39f29dbc 8027696: Incorrect copyright header in the tests
serb
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
0e3a39f29dbc 8027696: Incorrect copyright header in the tests
serb
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
0e3a39f29dbc 8027696: Incorrect copyright header in the tests
serb
parents: 2
diff changeset
    21
 * questions.
0e3a39f29dbc 8027696: Incorrect copyright header in the tests
serb
parents: 2
diff changeset
    22
 */
0e3a39f29dbc 8027696: Incorrect copyright header in the tests
serb
parents: 2
diff changeset
    23
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 4946388
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Unit test for CertificateRevokedException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.ByteArrayInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.ByteArrayOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.ObjectInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.io.ObjectOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.security.cert.CertificateRevokedException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.security.cert.CRLReason;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.security.cert.Extension;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.util.Calendar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.util.Date;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.util.HashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import javax.security.auth.x500.X500Principal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import sun.security.x509.InvalidityDateExtension;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
public class Basic {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        // test ctor for NPE
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        CertificateRevokedException cre = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
            cre = new CertificateRevokedException(null, null, null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
            throw new Exception("Did not throw expected NullPointerException");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        } catch (NullPointerException npe) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        // test getRevocationDate returns clone
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        Date date = Calendar.getInstance().getTime();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        long time = date.getTime();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        Map<String, Extension> extensions = new HashMap<String, Extension>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        Date invDate = new Date(date.getTime());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        extensions.put("2.5.29.24", new InvalidityDateExtension(invDate));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        cre = new CertificateRevokedException(date, CRLReason.UNSPECIFIED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            new X500Principal("CN=TestCA"), extensions);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        Date date2 = cre.getRevocationDate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        if (date2 == date) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            throw new Exception("getRevocationDate does not return copy");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        // test getRevocationDate returns the same date as specified in ctor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        if (!date.equals(date2)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            throw new Exception("getRevocationDate returns different date");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        // test ctor copies date
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        date.setTime(777);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        date2 = cre.getRevocationDate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        if (date2.getTime() != time) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            throw new Exception("Constructor did not copy date parameter");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        // test getReason returns same reason as specified in ctor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        CRLReason reason = cre.getRevocationReason();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        if (reason != CRLReason.UNSPECIFIED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            throw new Exception("getRevocationReason returns different reason");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        // test getAuthorityName returns same name as specified in ctor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        if (!cre.getAuthorityName().equals(new X500Principal("CN=TestCA"))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            throw new Exception("getAuthorityName returns different name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        // test getInvalidityDate returns invalidity date
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        Date invalidity = cre.getInvalidityDate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        if (invalidity == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            throw new Exception("getInvalidityDate returned null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        if (invalidity.getTime() != time) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            throw new Exception("getInvalidityDate returned wrong date");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        // test getInvalidityDate copies date
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        invDate.setTime(777);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        if (invalidity.getTime() != time) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            throw new Exception("getInvalidityDate did not return copy of date");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        // test serialization
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        ObjectOutputStream oos = new ObjectOutputStream(baos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        oos.writeObject(cre);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        oos.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        ObjectInputStream ois = new ObjectInputStream(bais);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        CertificateRevokedException cre2 =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            (CertificateRevokedException) ois.readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        if (cre2.getRevocationDate().getTime() != time) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            throw new Exception("deserialized exception returns different date");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        if (cre2.getRevocationReason() != CRLReason.UNSPECIFIED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            throw new Exception("deserialized exception returns different reason");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        if (!cre2.getAuthorityName().equals(new X500Principal("CN=TestCA"))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            throw new Exception("deserialized exception returns different name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        if (!cre2.getExtensions().keySet().equals(cre.getExtensions().keySet())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            throw new Exception("deserialized exception returns different extensions");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
}