Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/io/Serializable/defaultDataEnd/DefaultDataEnd.java
38828 views
/*1* Copyright (c) 2001, 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 {33int i1 = 1, i2 = 2;34String s1 = "foo", s2 = "bar";3536private void readObject(ObjectInputStream in)37throws IOException, ClassNotFoundException38{39in.defaultReadObject();40if (in.read() != -1) {41throw new Error();42}43try {44in.readInt();45throw new Error();46} catch (EOFException ex) {47}48try {49in.readObject();50throw new Error();51} catch (OptionalDataException ex) {52if (!ex.eof) {53throw new Error();54}55}56try {57in.readUnshared();58throw new Error();59} catch (OptionalDataException ex) {60if (!ex.eof) {61throw new Error();62}63}64}65}6667class B implements Serializable {68int i1 = 1, i2 = 2;69String s1 = "foo", s2 = "bar";7071private void readObject(ObjectInputStream in)72throws IOException, ClassNotFoundException73{74in.readFields();75try {76in.readObject();77throw new Error();78} catch (OptionalDataException ex) {79if (!ex.eof) {80throw new Error();81}82}83try {84in.readUnshared();85throw new Error();86} catch (OptionalDataException ex) {87if (!ex.eof) {88throw new Error();89}90}91if (in.read() != -1) {92throw new Error();93}94try {95in.readInt();96throw new Error();97} catch (EOFException ex) {98}99}100}101102class C implements Serializable {103private void readObject(ObjectInputStream in)104throws IOException, ClassNotFoundException105{106in.defaultReadObject();107try {108in.readObject();109throw new Error();110} catch (OptionalDataException ex) {111if (!ex.eof) {112throw new Error();113}114}115try {116in.readUnshared();117throw new Error();118} catch (OptionalDataException ex) {119if (!ex.eof) {120throw new Error();121}122}123if (in.read() != -1) {124throw new Error();125}126try {127in.readInt();128throw new Error();129} catch (EOFException ex) {130}131}132}133134public class DefaultDataEnd {135public static void main(String[] args) throws Exception {136ByteArrayOutputStream bout = new ByteArrayOutputStream();137ObjectOutputStream oout = new ObjectOutputStream(bout);138oout.writeObject(new A());139oout.writeObject(new B());140oout.writeObject(new C());141oout.close();142ObjectInputStream oin = new ObjectInputStream(143new ByteArrayInputStream(bout.toByteArray()));144oin.readObject();145oin.readObject();146oin.readObject();147}148}149150151