Path: blob/master/test/jdk/java/security/CodeSigner/Serialize.java
41149 views
/*1* Copyright (c) 2010, 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 679985426* @summary CodeSigner.hashCode() does not work with serialization27*/2829import java.io.*;30import java.security.CodeSigner;31import java.security.Timestamp;32import java.security.cert.*;33import java.util.Collections;34import java.util.Date;3536public class Serialize {3738public static void main(String[] args) throws Exception {3940// Create a certpath consisting of one certificate41File f = new File(System.getProperty("test.src", "."), "cert_file");42FileInputStream fis = new FileInputStream(f);43CertificateFactory cf = CertificateFactory.getInstance("X.509");44Certificate c = cf.generateCertificate(fis);45fis.close();46CertPath cp = cf.generateCertPath(Collections.singletonList(c));4748// Create a code signer49CodeSigner cs = new CodeSigner(cp, new Timestamp(new Date(), cp));5051// Serialize the code signer52ByteArrayOutputStream byteOut = new ByteArrayOutputStream();53ObjectOutputStream out = new ObjectOutputStream(byteOut);54out.writeObject(cs);55out.close();5657// Deserialize the code signer58byte[] data = byteOut.toByteArray();59CodeSigner cs2 = (CodeSigner) new ObjectInputStream(60new ByteArrayInputStream(data)).readObject();6162// Test for equality63if (!cs.equals(cs2) || cs.hashCode() != cs2.hashCode()) {64throw new Exception("CodeSigner serialization test FAILED");65}66}67}686970