Path: blob/master/test/jdk/java/io/Serializable/classDescHooks/ClassDescHooks.java
41154 views
/*1* Copyright (c) 1999, 2019, 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/* @test24* @bug 422718925* @summary Ensure that class descriptor read, write hooks exist, are backwards26* compatible, and work as advertised.27*/2829import java.io.*;30import java.util.*;3132class Foo implements Serializable {33private static final long serialVersionUID = 1L;34Short s = (short) 1;35Integer i = 2;36Long l = 3L;3738public boolean equals(Object obj) {39if (obj instanceof Foo) {40Foo ofoo = (Foo) obj;41return s.equals(ofoo.s) && i.equals(ofoo.i) && l.equals(ofoo.l);42}43return false;44}4546public int hashCode() {47return i;48}49}5051class CustomOutputStream extends ObjectOutputStream {5253boolean hookCalled = false;5455CustomOutputStream(OutputStream out) throws IOException {56super(out);57useProtocolVersion(PROTOCOL_VERSION_2);58}5960protected void writeClassDescriptor(ObjectStreamClass desc)61throws IOException62{63writeUTF(desc.getName());64hookCalled = true;65}66}6768class CustomInputStream extends ObjectInputStream {6970boolean hookCalled = false;7172CustomInputStream(InputStream in) throws IOException {73super(in);74}7576protected ObjectStreamClass readClassDescriptor()77throws IOException, ClassNotFoundException78{79hookCalled = true;80return ObjectStreamClass.lookup(Class.forName(readUTF()));81}82}8384public class ClassDescHooks implements ObjectStreamConstants {85public static void main(String[] args) throws Exception {86ByteArrayOutputStream bout;87ByteArrayInputStream bin;88ObjectOutputStream oout;89ObjectInputStream oin;90FileInputStream fin;91File foof;92CustomOutputStream cout;93CustomInputStream cin;9495// test for backwards compatibility96bout = new ByteArrayOutputStream();97foof = new File(System.getProperty("test.src", "."), "Foo.ser");98fin = new FileInputStream(foof);99try {100while (fin.available() > 0)101bout.write(fin.read());102} finally {103fin.close();104}105byte[] buf1 = bout.toByteArray();106107bout = new ByteArrayOutputStream();108oout = new ObjectOutputStream(bout);109Foo foo = new Foo();110oout.writeObject(foo);111oout.flush();112byte[] buf2 = bout.toByteArray();113114if (! Arrays.equals(buf1, buf2))115throw new Error("Incompatible stream format (write)");116117Foo foocopy;118fin = new FileInputStream(foof);119try {120oin = new ObjectInputStream(fin);121foocopy = (Foo) oin.readObject();122if (! foo.equals(foocopy))123throw new Error("Incompatible stream format (read)");124} finally {125fin.close();126}127128// make sure write hook not called when old protocol in use129bout = new ByteArrayOutputStream();130cout = new CustomOutputStream(bout);131cout.useProtocolVersion(PROTOCOL_VERSION_1);132cout.writeObject(foo);133if (cout.hookCalled)134throw new Error("write descriptor hook should not be called");135136// write custom class descriptor representations137bout = new ByteArrayOutputStream();138cout = new CustomOutputStream(bout);139cout.writeObject(foo);140cout.flush();141bin = new ByteArrayInputStream(bout.toByteArray());142cin = new CustomInputStream(bin);143foocopy = (Foo) cin.readObject();144if (! cout.hookCalled)145throw new Error("write descriptor hook never called");146if (! cin.hookCalled)147throw new Error("read descriptor hook never called");148if (! foo.equals(foocopy))149throw new Error("serialization failed when hooks active");150}151}152153154