Path: blob/master/test/jdk/java/security/cert/CertPathValidatorException/Serial.java
41154 views
/*1* Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 646594226* @summary Test deserialization of CertPathValidatorException27*/2829import java.io.ByteArrayInputStream;30import java.io.ByteArrayOutputStream;31import java.io.File;32import java.io.FileInputStream;33//import java.io.FileOutputStream;34import java.io.ObjectInputStream;35import java.io.ObjectOutputStream;36import java.security.cert.Certificate;37import java.security.cert.CertificateFactory;38import java.security.cert.CertPath;39import java.security.cert.CertPathValidatorException;40import java.security.cert.CertPathValidatorException.BasicReason;41import java.util.Collections;4243/**44* This class tests to see if CertPathValidatorException can be serialized and45* deserialized properly.46*/47public class Serial {48private static volatile boolean failed = false;49public static void main(String[] args) throws Exception {5051File f = new File(System.getProperty("test.src", "."), "cert_file");52FileInputStream fis = new FileInputStream(f);53CertificateFactory cf = CertificateFactory.getInstance("X.509");54Certificate c = cf.generateCertificate(fis);55fis.close();56CertPath cp = cf.generateCertPath(Collections.singletonList(c));5758CertPathValidatorException cpve1 =59new CertPathValidatorException60("Test", new Exception("Expired"), cp, 0, BasicReason.EXPIRED);61ByteArrayOutputStream baos = new ByteArrayOutputStream();62// FileOutputStream fos = new FileOutputStream("jdk7.serial");63ObjectOutputStream oos = new ObjectOutputStream(baos);64// ObjectOutputStream foos = new ObjectOutputStream(fos);65oos.writeObject(cpve1);66// foos.writeObject(cpve1);67ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());68ObjectInputStream ois = new ObjectInputStream(bais);69CertPathValidatorException cpve2 =70(CertPathValidatorException) ois.readObject();71check(!cpve1.getMessage().equals(cpve2.getMessage()),72"CertPathValidatorException messages not equal");73check(!cpve1.getCause().getMessage().equals(cpve2.getCause().getMessage()),74"CertPathValidatorException causes not equal");75check(!cpve1.getCertPath().equals(cpve2.getCertPath()),76"CertPathValidatorException certpaths not equal");77check(cpve1.getIndex() != cpve2.getIndex(),78"CertPathValidatorException indexes not equal");79check(cpve1.getReason() != cpve2.getReason(),80"CertPathValidatorException reasons not equal");81oos.close();82ois.close();8384f = new File(System.getProperty("test.src", "."), "jdk6.serial");85fis = new FileInputStream(f);86ois = new ObjectInputStream(fis);87cpve2 = (CertPathValidatorException) ois.readObject();88check(!cpve1.getMessage().equals(cpve2.getMessage()),89"CertPathValidatorException messages not equal");90check(!cpve1.getCause().getMessage().equals(cpve2.getCause().getMessage()),91"CertPathValidatorException causes not equal");92check(!cpve1.getCertPath().equals(cpve2.getCertPath()),93"CertPathValidatorException certpaths not equal");94check(cpve1.getIndex() != cpve2.getIndex(),95"CertPathValidatorException indexes not equal");96// System.out.println(cpve2.getReason());97check(cpve2.getReason() != BasicReason.UNSPECIFIED,98"CertPathValidatorException reasons not equal");99oos.close();100ois.close();101if (failed) {102throw new Exception("Some tests FAILED");103}104}105106private static void check(boolean expr, String message) {107if (expr) {108failed = true;109System.err.println("FAILED: " + message);110}111}112}113114115