Path: blob/master/test/jdk/java/io/Serializable/defaultDataEnd/DefaultDataEnd.java
41153 views
/*1* Copyright (c) 2001, 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 436050825* @summary Verify that a custom readObject() method reading in data written26* via default serialization cannot read past the end of the default27* data.28*/2930import java.io.*;3132class A implements Serializable {33private static final long serialVersionUID = 1L;3435int i1 = 1, i2 = 2;36String s1 = "foo", s2 = "bar";3738private void readObject(ObjectInputStream in)39throws IOException, ClassNotFoundException40{41in.defaultReadObject();42if (in.read() != -1) {43throw new Error();44}45try {46in.readInt();47throw new Error();48} catch (EOFException ex) {49}50try {51in.readObject();52throw new Error();53} catch (OptionalDataException ex) {54if (!ex.eof) {55throw new Error();56}57}58try {59in.readUnshared();60throw new Error();61} catch (OptionalDataException ex) {62if (!ex.eof) {63throw new Error();64}65}66}67}6869class B implements Serializable {70private static final long serialVersionUID = 1L;7172int i1 = 1, i2 = 2;73String s1 = "foo", s2 = "bar";7475private void readObject(ObjectInputStream in)76throws IOException, ClassNotFoundException77{78in.readFields();79try {80in.readObject();81throw new Error();82} catch (OptionalDataException ex) {83if (!ex.eof) {84throw new Error();85}86}87try {88in.readUnshared();89throw new Error();90} catch (OptionalDataException ex) {91if (!ex.eof) {92throw new Error();93}94}95if (in.read() != -1) {96throw new Error();97}98try {99in.readInt();100throw new Error();101} catch (EOFException ex) {102}103}104}105106class C implements Serializable {107private static final long serialVersionUID = 1L;108109private void readObject(ObjectInputStream in)110throws IOException, ClassNotFoundException111{112in.defaultReadObject();113try {114in.readObject();115throw new Error();116} catch (OptionalDataException ex) {117if (!ex.eof) {118throw new Error();119}120}121try {122in.readUnshared();123throw new Error();124} catch (OptionalDataException ex) {125if (!ex.eof) {126throw new Error();127}128}129if (in.read() != -1) {130throw new Error();131}132try {133in.readInt();134throw new Error();135} catch (EOFException ex) {136}137}138}139140public class DefaultDataEnd {141public static void main(String[] args) throws Exception {142ByteArrayOutputStream bout = new ByteArrayOutputStream();143ObjectOutputStream oout = new ObjectOutputStream(bout);144oout.writeObject(new A());145oout.writeObject(new B());146oout.writeObject(new C());147oout.close();148ObjectInputStream oin = new ObjectInputStream(149new ByteArrayInputStream(bout.toByteArray()));150oin.readObject();151oin.readObject();152oin.readObject();153}154}155156157