author | rriggs |
Fri, 11 Oct 2019 13:11:56 -0400 | |
changeset 58565 | baa5969ecf34 |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
58565
baa5969ecf34
8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents:
47216
diff
changeset
|
2 |
* Copyright (c) 2001, 2019, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. |
|
8 |
* |
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
13 |
* accompanied this code). |
|
14 |
* |
|
15 |
* You should have received a copy of the GNU General Public License version |
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 |
* |
|
5506 | 19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
20 |
* or visit www.oracle.com if you need additional information or have any |
|
21 |
* questions. |
|
2 | 22 |
*/ |
23 |
||
24 |
/* @test |
|
25 |
* @bug 4360508 |
|
26 |
* @summary Verify that a custom readObject() method reading in data written |
|
27 |
* via default serialization cannot read past the end of the default |
|
28 |
* data. |
|
29 |
*/ |
|
30 |
||
31 |
import java.io.*; |
|
32 |
||
33 |
class A implements Serializable { |
|
58565
baa5969ecf34
8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents:
47216
diff
changeset
|
34 |
private static final long serialVersionUID = 1L; |
baa5969ecf34
8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents:
47216
diff
changeset
|
35 |
|
2 | 36 |
int i1 = 1, i2 = 2; |
37 |
String s1 = "foo", s2 = "bar"; |
|
38 |
||
39 |
private void readObject(ObjectInputStream in) |
|
40 |
throws IOException, ClassNotFoundException |
|
41 |
{ |
|
42 |
in.defaultReadObject(); |
|
43 |
if (in.read() != -1) { |
|
44 |
throw new Error(); |
|
45 |
} |
|
46 |
try { |
|
47 |
in.readInt(); |
|
48 |
throw new Error(); |
|
49 |
} catch (EOFException ex) { |
|
50 |
} |
|
51 |
try { |
|
52 |
in.readObject(); |
|
53 |
throw new Error(); |
|
54 |
} catch (OptionalDataException ex) { |
|
55 |
if (!ex.eof) { |
|
56 |
throw new Error(); |
|
57 |
} |
|
58 |
} |
|
59 |
try { |
|
60 |
in.readUnshared(); |
|
61 |
throw new Error(); |
|
62 |
} catch (OptionalDataException ex) { |
|
63 |
if (!ex.eof) { |
|
64 |
throw new Error(); |
|
65 |
} |
|
66 |
} |
|
67 |
} |
|
68 |
} |
|
69 |
||
70 |
class B implements Serializable { |
|
58565
baa5969ecf34
8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents:
47216
diff
changeset
|
71 |
private static final long serialVersionUID = 1L; |
baa5969ecf34
8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents:
47216
diff
changeset
|
72 |
|
2 | 73 |
int i1 = 1, i2 = 2; |
74 |
String s1 = "foo", s2 = "bar"; |
|
75 |
||
76 |
private void readObject(ObjectInputStream in) |
|
77 |
throws IOException, ClassNotFoundException |
|
78 |
{ |
|
79 |
in.readFields(); |
|
80 |
try { |
|
81 |
in.readObject(); |
|
82 |
throw new Error(); |
|
83 |
} catch (OptionalDataException ex) { |
|
84 |
if (!ex.eof) { |
|
85 |
throw new Error(); |
|
86 |
} |
|
87 |
} |
|
88 |
try { |
|
89 |
in.readUnshared(); |
|
90 |
throw new Error(); |
|
91 |
} catch (OptionalDataException ex) { |
|
92 |
if (!ex.eof) { |
|
93 |
throw new Error(); |
|
94 |
} |
|
95 |
} |
|
96 |
if (in.read() != -1) { |
|
97 |
throw new Error(); |
|
98 |
} |
|
99 |
try { |
|
100 |
in.readInt(); |
|
101 |
throw new Error(); |
|
102 |
} catch (EOFException ex) { |
|
103 |
} |
|
104 |
} |
|
105 |
} |
|
106 |
||
107 |
class C implements Serializable { |
|
58565
baa5969ecf34
8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents:
47216
diff
changeset
|
108 |
private static final long serialVersionUID = 1L; |
baa5969ecf34
8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents:
47216
diff
changeset
|
109 |
|
2 | 110 |
private void readObject(ObjectInputStream in) |
111 |
throws IOException, ClassNotFoundException |
|
112 |
{ |
|
113 |
in.defaultReadObject(); |
|
114 |
try { |
|
115 |
in.readObject(); |
|
116 |
throw new Error(); |
|
117 |
} catch (OptionalDataException ex) { |
|
118 |
if (!ex.eof) { |
|
119 |
throw new Error(); |
|
120 |
} |
|
121 |
} |
|
122 |
try { |
|
123 |
in.readUnshared(); |
|
124 |
throw new Error(); |
|
125 |
} catch (OptionalDataException ex) { |
|
126 |
if (!ex.eof) { |
|
127 |
throw new Error(); |
|
128 |
} |
|
129 |
} |
|
130 |
if (in.read() != -1) { |
|
131 |
throw new Error(); |
|
132 |
} |
|
133 |
try { |
|
134 |
in.readInt(); |
|
135 |
throw new Error(); |
|
136 |
} catch (EOFException ex) { |
|
137 |
} |
|
138 |
} |
|
139 |
} |
|
140 |
||
141 |
public class DefaultDataEnd { |
|
142 |
public static void main(String[] args) throws Exception { |
|
143 |
ByteArrayOutputStream bout = new ByteArrayOutputStream(); |
|
144 |
ObjectOutputStream oout = new ObjectOutputStream(bout); |
|
145 |
oout.writeObject(new A()); |
|
146 |
oout.writeObject(new B()); |
|
147 |
oout.writeObject(new C()); |
|
148 |
oout.close(); |
|
149 |
ObjectInputStream oin = new ObjectInputStream( |
|
150 |
new ByteArrayInputStream(bout.toByteArray())); |
|
151 |
oin.readObject(); |
|
152 |
oin.readObject(); |
|
153 |
oin.readObject(); |
|
154 |
} |
|
155 |
} |