585N/A/*
2362N/A * Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved.
585N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
585N/A *
585N/A * This code is free software; you can redistribute it and/or modify it
585N/A * under the terms of the GNU General Public License version 2 only, as
585N/A * published by the Free Software Foundation.
585N/A *
585N/A * This code is distributed in the hope that it will be useful, but WITHOUT
585N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
585N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
585N/A * version 2 for more details (a copy is included in the LICENSE file that
585N/A * accompanied this code).
585N/A *
585N/A * You should have received a copy of the GNU General Public License version
585N/A * 2 along with this work; if not, write to the Free Software Foundation,
585N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
585N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
585N/A */
585N/A
585N/A/*
585N/A * @test
585N/A * @bug 6465942
585N/A * @summary Test deserialization of CertPathValidatorException
585N/A */
585N/A
585N/Aimport java.io.ByteArrayInputStream;
585N/Aimport java.io.ByteArrayOutputStream;
585N/Aimport java.io.File;
585N/Aimport java.io.FileInputStream;
585N/A//import java.io.FileOutputStream;
585N/Aimport java.io.ObjectInputStream;
585N/Aimport java.io.ObjectOutputStream;
585N/Aimport java.security.cert.Certificate;
585N/Aimport java.security.cert.CertificateFactory;
585N/Aimport java.security.cert.CertPath;
585N/Aimport java.security.cert.CertPathValidatorException;
585N/Aimport java.security.cert.CertPathValidatorException.BasicReason;
585N/Aimport java.util.Collections;
585N/A
585N/A/**
585N/A * This class tests to see if CertPathValidatorException can be serialized and
585N/A * deserialized properly.
585N/A */
585N/Apublic class Serial {
585N/A private static volatile boolean failed = false;
585N/A public static void main(String[] args) throws Exception {
585N/A
585N/A File f = new File(System.getProperty("test.src", "."), "cert_file");
585N/A FileInputStream fis = new FileInputStream(f);
585N/A CertificateFactory cf = CertificateFactory.getInstance("X.509");
585N/A Certificate c = cf.generateCertificate(fis);
585N/A fis.close();
585N/A CertPath cp = cf.generateCertPath(Collections.singletonList(c));
585N/A
585N/A CertPathValidatorException cpve1 =
585N/A new CertPathValidatorException
585N/A ("Test", new Exception("Expired"), cp, 0, BasicReason.EXPIRED);
585N/A ByteArrayOutputStream baos = new ByteArrayOutputStream();
585N/A// FileOutputStream fos = new FileOutputStream("jdk7.serial");
585N/A ObjectOutputStream oos = new ObjectOutputStream(baos);
585N/A// ObjectOutputStream foos = new ObjectOutputStream(fos);
585N/A oos.writeObject(cpve1);
585N/A// foos.writeObject(cpve1);
585N/A ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
585N/A ObjectInputStream ois = new ObjectInputStream(bais);
585N/A CertPathValidatorException cpve2 =
585N/A (CertPathValidatorException) ois.readObject();
585N/A check(!cpve1.getMessage().equals(cpve2.getMessage()),
585N/A "CertPathValidatorException messages not equal");
585N/A check(!cpve1.getCause().getMessage().equals(cpve2.getCause().getMessage()),
585N/A "CertPathValidatorException causes not equal");
585N/A check(!cpve1.getCertPath().equals(cpve2.getCertPath()),
585N/A "CertPathValidatorException certpaths not equal");
585N/A check(cpve1.getIndex() != cpve2.getIndex(),
585N/A "CertPathValidatorException indexes not equal");
585N/A check(cpve1.getReason() != cpve2.getReason(),
585N/A "CertPathValidatorException reasons not equal");
585N/A oos.close();
585N/A ois.close();
585N/A
585N/A f = new File(System.getProperty("test.src", "."), "jdk6.serial");
585N/A fis = new FileInputStream(f);
585N/A ois = new ObjectInputStream(fis);
585N/A cpve2 = (CertPathValidatorException) ois.readObject();
585N/A check(!cpve1.getMessage().equals(cpve2.getMessage()),
585N/A "CertPathValidatorException messages not equal");
585N/A check(!cpve1.getCause().getMessage().equals(cpve2.getCause().getMessage()),
585N/A "CertPathValidatorException causes not equal");
585N/A check(!cpve1.getCertPath().equals(cpve2.getCertPath()),
585N/A "CertPathValidatorException certpaths not equal");
585N/A check(cpve1.getIndex() != cpve2.getIndex(),
585N/A "CertPathValidatorException indexes not equal");
585N/A// System.out.println(cpve2.getReason());
585N/A check(cpve2.getReason() != BasicReason.UNSPECIFIED,
585N/A "CertPathValidatorException reasons not equal");
585N/A oos.close();
585N/A ois.close();
585N/A if (failed) {
585N/A throw new Exception("Some tests FAILED");
585N/A }
585N/A }
585N/A
585N/A private static void check(boolean expr, String message) {
585N/A if (expr) {
585N/A failed = true;
585N/A System.err.println("FAILED: " + message);
585N/A }
585N/A }
585N/A}