jdk/test/java/security/cert/CertPathValidatorException/Serial.java
changeset 1238 6d1f4b722acd
child 5506 202f599c92aa
equal deleted inserted replaced
1096:7906d13db4eb 1238:6d1f4b722acd
       
     1 /*
       
     2  * Copyright 2008 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    20  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    21  * have any questions.
       
    22  */
       
    23 
       
    24 /*
       
    25  * @test
       
    26  * @bug 6465942
       
    27  * @summary Test deserialization of CertPathValidatorException
       
    28  */
       
    29 
       
    30 import java.io.ByteArrayInputStream;
       
    31 import java.io.ByteArrayOutputStream;
       
    32 import java.io.File;
       
    33 import java.io.FileInputStream;
       
    34 //import java.io.FileOutputStream;
       
    35 import java.io.ObjectInputStream;
       
    36 import java.io.ObjectOutputStream;
       
    37 import java.security.cert.Certificate;
       
    38 import java.security.cert.CertificateFactory;
       
    39 import java.security.cert.CertPath;
       
    40 import java.security.cert.CertPathValidatorException;
       
    41 import java.security.cert.CertPathValidatorException.BasicReason;
       
    42 import java.util.Collections;
       
    43 
       
    44 /**
       
    45  * This class tests to see if CertPathValidatorException can be serialized and
       
    46  * deserialized properly.
       
    47  */
       
    48 public class Serial {
       
    49     private static volatile boolean failed = false;
       
    50     public static void main(String[] args) throws Exception {
       
    51 
       
    52         File f = new File(System.getProperty("test.src", "."), "cert_file");
       
    53         FileInputStream fis = new FileInputStream(f);
       
    54         CertificateFactory cf = CertificateFactory.getInstance("X.509");
       
    55         Certificate c = cf.generateCertificate(fis);
       
    56         fis.close();
       
    57         CertPath cp = cf.generateCertPath(Collections.singletonList(c));
       
    58 
       
    59         CertPathValidatorException cpve1 =
       
    60             new CertPathValidatorException
       
    61                 ("Test", new Exception("Expired"), cp, 0, BasicReason.EXPIRED);
       
    62         ByteArrayOutputStream baos = new ByteArrayOutputStream();
       
    63 //        FileOutputStream fos = new FileOutputStream("jdk7.serial");
       
    64         ObjectOutputStream oos = new ObjectOutputStream(baos);
       
    65 //        ObjectOutputStream foos = new ObjectOutputStream(fos);
       
    66         oos.writeObject(cpve1);
       
    67 //        foos.writeObject(cpve1);
       
    68         ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
       
    69         ObjectInputStream ois = new ObjectInputStream(bais);
       
    70         CertPathValidatorException cpve2 =
       
    71             (CertPathValidatorException) ois.readObject();
       
    72         check(!cpve1.getMessage().equals(cpve2.getMessage()),
       
    73             "CertPathValidatorException messages not equal");
       
    74         check(!cpve1.getCause().getMessage().equals(cpve2.getCause().getMessage()),
       
    75             "CertPathValidatorException causes not equal");
       
    76         check(!cpve1.getCertPath().equals(cpve2.getCertPath()),
       
    77             "CertPathValidatorException certpaths not equal");
       
    78         check(cpve1.getIndex() != cpve2.getIndex(),
       
    79             "CertPathValidatorException indexes not equal");
       
    80         check(cpve1.getReason() != cpve2.getReason(),
       
    81             "CertPathValidatorException reasons not equal");
       
    82         oos.close();
       
    83         ois.close();
       
    84 
       
    85         f = new File(System.getProperty("test.src", "."), "jdk6.serial");
       
    86         fis = new FileInputStream(f);
       
    87         ois = new ObjectInputStream(fis);
       
    88         cpve2 = (CertPathValidatorException) ois.readObject();
       
    89         check(!cpve1.getMessage().equals(cpve2.getMessage()),
       
    90             "CertPathValidatorException messages not equal");
       
    91         check(!cpve1.getCause().getMessage().equals(cpve2.getCause().getMessage()),
       
    92             "CertPathValidatorException causes not equal");
       
    93         check(!cpve1.getCertPath().equals(cpve2.getCertPath()),
       
    94             "CertPathValidatorException certpaths not equal");
       
    95         check(cpve1.getIndex() != cpve2.getIndex(),
       
    96             "CertPathValidatorException indexes not equal");
       
    97 //      System.out.println(cpve2.getReason());
       
    98         check(cpve2.getReason() != BasicReason.UNSPECIFIED,
       
    99             "CertPathValidatorException reasons not equal");
       
   100         oos.close();
       
   101         ois.close();
       
   102         if (failed) {
       
   103             throw new Exception("Some tests FAILED");
       
   104         }
       
   105     }
       
   106 
       
   107     private static void check(boolean expr, String message) {
       
   108         if (expr) {
       
   109             failed = true;
       
   110             System.err.println("FAILED: " + message);
       
   111         }
       
   112     }
       
   113 }