jdk/test/java/security/cert/X509Certificate/X509BadCertificate.java
author asmotrak
Tue, 21 Jan 2014 16:54:26 -0500
changeset 22563 2e5b6242e863
child 23345 534d74068ee0
permissions -rw-r--r--
8028431: NullPointerException in DerValue.equals(DerValue) Reviewed-by: mullan
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
22563
2e5b6242e863 8028431: NullPointerException in DerValue.equals(DerValue)
asmotrak
parents:
diff changeset
     1
/*
2e5b6242e863 8028431: NullPointerException in DerValue.equals(DerValue)
asmotrak
parents:
diff changeset
     2
 * Copyright (c) 2001, 2013, Oracle and/or its affiliates. All rights reserved.
2e5b6242e863 8028431: NullPointerException in DerValue.equals(DerValue)
asmotrak
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
2e5b6242e863 8028431: NullPointerException in DerValue.equals(DerValue)
asmotrak
parents:
diff changeset
     4
 *
2e5b6242e863 8028431: NullPointerException in DerValue.equals(DerValue)
asmotrak
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
2e5b6242e863 8028431: NullPointerException in DerValue.equals(DerValue)
asmotrak
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
2e5b6242e863 8028431: NullPointerException in DerValue.equals(DerValue)
asmotrak
parents:
diff changeset
     7
 * published by the Free Software Foundation.
2e5b6242e863 8028431: NullPointerException in DerValue.equals(DerValue)
asmotrak
parents:
diff changeset
     8
 *
2e5b6242e863 8028431: NullPointerException in DerValue.equals(DerValue)
asmotrak
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
2e5b6242e863 8028431: NullPointerException in DerValue.equals(DerValue)
asmotrak
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
2e5b6242e863 8028431: NullPointerException in DerValue.equals(DerValue)
asmotrak
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
2e5b6242e863 8028431: NullPointerException in DerValue.equals(DerValue)
asmotrak
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
2e5b6242e863 8028431: NullPointerException in DerValue.equals(DerValue)
asmotrak
parents:
diff changeset
    13
 * accompanied this code).
2e5b6242e863 8028431: NullPointerException in DerValue.equals(DerValue)
asmotrak
parents:
diff changeset
    14
 *
2e5b6242e863 8028431: NullPointerException in DerValue.equals(DerValue)
asmotrak
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
2e5b6242e863 8028431: NullPointerException in DerValue.equals(DerValue)
asmotrak
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
2e5b6242e863 8028431: NullPointerException in DerValue.equals(DerValue)
asmotrak
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2e5b6242e863 8028431: NullPointerException in DerValue.equals(DerValue)
asmotrak
parents:
diff changeset
    18
 *
2e5b6242e863 8028431: NullPointerException in DerValue.equals(DerValue)
asmotrak
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2e5b6242e863 8028431: NullPointerException in DerValue.equals(DerValue)
asmotrak
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
2e5b6242e863 8028431: NullPointerException in DerValue.equals(DerValue)
asmotrak
parents:
diff changeset
    21
 * questions.
2e5b6242e863 8028431: NullPointerException in DerValue.equals(DerValue)
asmotrak
parents:
diff changeset
    22
 */
2e5b6242e863 8028431: NullPointerException in DerValue.equals(DerValue)
asmotrak
parents:
diff changeset
    23
2e5b6242e863 8028431: NullPointerException in DerValue.equals(DerValue)
asmotrak
parents:
diff changeset
    24
/**
2e5b6242e863 8028431: NullPointerException in DerValue.equals(DerValue)
asmotrak
parents:
diff changeset
    25
 * @test
2e5b6242e863 8028431: NullPointerException in DerValue.equals(DerValue)
asmotrak
parents:
diff changeset
    26
 * @bug 8028431
2e5b6242e863 8028431: NullPointerException in DerValue.equals(DerValue)
asmotrak
parents:
diff changeset
    27
 * @summary Make sure that proper CertificateException is thrown
2e5b6242e863 8028431: NullPointerException in DerValue.equals(DerValue)
asmotrak
parents:
diff changeset
    28
 *          when loading bad x509 certificate
2e5b6242e863 8028431: NullPointerException in DerValue.equals(DerValue)
asmotrak
parents:
diff changeset
    29
 * @author Artem Smotrakov
2e5b6242e863 8028431: NullPointerException in DerValue.equals(DerValue)
asmotrak
parents:
diff changeset
    30
 */
2e5b6242e863 8028431: NullPointerException in DerValue.equals(DerValue)
asmotrak
parents:
diff changeset
    31
2e5b6242e863 8028431: NullPointerException in DerValue.equals(DerValue)
asmotrak
parents:
diff changeset
    32
import java.io.File;
2e5b6242e863 8028431: NullPointerException in DerValue.equals(DerValue)
asmotrak
parents:
diff changeset
    33
import java.io.FileInputStream;
2e5b6242e863 8028431: NullPointerException in DerValue.equals(DerValue)
asmotrak
parents:
diff changeset
    34
import java.security.cert.CertificateFactory;
2e5b6242e863 8028431: NullPointerException in DerValue.equals(DerValue)
asmotrak
parents:
diff changeset
    35
import java.security.cert.X509Certificate;
2e5b6242e863 8028431: NullPointerException in DerValue.equals(DerValue)
asmotrak
parents:
diff changeset
    36
import java.security.cert.CertificateException;
2e5b6242e863 8028431: NullPointerException in DerValue.equals(DerValue)
asmotrak
parents:
diff changeset
    37
2e5b6242e863 8028431: NullPointerException in DerValue.equals(DerValue)
asmotrak
parents:
diff changeset
    38
public class X509BadCertificate {
2e5b6242e863 8028431: NullPointerException in DerValue.equals(DerValue)
asmotrak
parents:
diff changeset
    39
2e5b6242e863 8028431: NullPointerException in DerValue.equals(DerValue)
asmotrak
parents:
diff changeset
    40
    public static void main(String[] args) throws Exception {
2e5b6242e863 8028431: NullPointerException in DerValue.equals(DerValue)
asmotrak
parents:
diff changeset
    41
        test("bad-cert-1.pem");
2e5b6242e863 8028431: NullPointerException in DerValue.equals(DerValue)
asmotrak
parents:
diff changeset
    42
    }
2e5b6242e863 8028431: NullPointerException in DerValue.equals(DerValue)
asmotrak
parents:
diff changeset
    43
2e5b6242e863 8028431: NullPointerException in DerValue.equals(DerValue)
asmotrak
parents:
diff changeset
    44
    /**
2e5b6242e863 8028431: NullPointerException in DerValue.equals(DerValue)
asmotrak
parents:
diff changeset
    45
     * Parse X509 certificates.
2e5b6242e863 8028431: NullPointerException in DerValue.equals(DerValue)
asmotrak
parents:
diff changeset
    46
     */
2e5b6242e863 8028431: NullPointerException in DerValue.equals(DerValue)
asmotrak
parents:
diff changeset
    47
    static void test(String filename) throws Exception {
2e5b6242e863 8028431: NullPointerException in DerValue.equals(DerValue)
asmotrak
parents:
diff changeset
    48
        try {
2e5b6242e863 8028431: NullPointerException in DerValue.equals(DerValue)
asmotrak
parents:
diff changeset
    49
            System.out.println("Parse file " + filename);
2e5b6242e863 8028431: NullPointerException in DerValue.equals(DerValue)
asmotrak
parents:
diff changeset
    50
            File f = new File(System.getProperty("test.src", "."), filename);
2e5b6242e863 8028431: NullPointerException in DerValue.equals(DerValue)
asmotrak
parents:
diff changeset
    51
            try (FileInputStream fis = new FileInputStream(f)) {
2e5b6242e863 8028431: NullPointerException in DerValue.equals(DerValue)
asmotrak
parents:
diff changeset
    52
                CertificateFactory cf = CertificateFactory.getInstance("X509");
2e5b6242e863 8028431: NullPointerException in DerValue.equals(DerValue)
asmotrak
parents:
diff changeset
    53
                X509Certificate cert = (X509Certificate)
2e5b6242e863 8028431: NullPointerException in DerValue.equals(DerValue)
asmotrak
parents:
diff changeset
    54
                cf.generateCertificate(fis);
2e5b6242e863 8028431: NullPointerException in DerValue.equals(DerValue)
asmotrak
parents:
diff changeset
    55
            }
2e5b6242e863 8028431: NullPointerException in DerValue.equals(DerValue)
asmotrak
parents:
diff changeset
    56
            throw new Exception("Test failed: " +
2e5b6242e863 8028431: NullPointerException in DerValue.equals(DerValue)
asmotrak
parents:
diff changeset
    57
                "expected CertificateParsingException was not thrown");
2e5b6242e863 8028431: NullPointerException in DerValue.equals(DerValue)
asmotrak
parents:
diff changeset
    58
        } catch (CertificateException e) {
2e5b6242e863 8028431: NullPointerException in DerValue.equals(DerValue)
asmotrak
parents:
diff changeset
    59
            System.out.println("Test passed: expected exception was thrown: " +
2e5b6242e863 8028431: NullPointerException in DerValue.equals(DerValue)
asmotrak
parents:
diff changeset
    60
                e.toString());
2e5b6242e863 8028431: NullPointerException in DerValue.equals(DerValue)
asmotrak
parents:
diff changeset
    61
        }
2e5b6242e863 8028431: NullPointerException in DerValue.equals(DerValue)
asmotrak
parents:
diff changeset
    62
    }
2e5b6242e863 8028431: NullPointerException in DerValue.equals(DerValue)
asmotrak
parents:
diff changeset
    63
}