blob: 332954308dd662312dba3b0aeae0fe9bbaeb0a18 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2005 Sun Microsystems, Inc. All Rights Reserved.
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 *
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 */
23
24/* @test
25 * @bug 6313687
26 * @summary Verify that if the class descriptor for an ordinary object is the
27 * descriptor for an array class, an ObjectStreamException is thrown.
28 *
29 */
30
31import java.io.*;
32
33class TestArray implements Serializable {
34 // size of array
35 private static final int ARR_SIZE = 5;
36 // serializable field
37 private String[] strArr = new String[ARR_SIZE];
38
39 public TestArray() {
40 for (int i = 0; i < ARR_SIZE; i++) {
41 strArr[i] = "test" + i;
42 }
43 }
44}
45
46public class MisplacedArrayClassDesc {
47 public static final void main(String[] args) throws Exception {
48 System.err.println("\nRegression test for CR6313687");
49 TestArray object = new TestArray();
50 try {
51 // Serialize to a byte array
52 ByteArrayOutputStream bos = new ByteArrayOutputStream() ;
53 ObjectOutputStream out = new ObjectOutputStream(bos) ;
54 out.writeObject(object);
55 out.close();
56
57 // Get the bytes of the serialized object
58 byte[] buf = bos.toByteArray();
59 for (int i = 0; i < buf.length; i++) {
60 if (buf[i] == ObjectOutputStream.TC_ARRAY) {
61 buf[i] = ObjectOutputStream.TC_OBJECT;
62 break;
63 }
64 }
65
66 // Deserialize from a byte array
67 ByteArrayInputStream bais = new ByteArrayInputStream(buf);
68 ObjectInputStream in = new ObjectInputStream(bais);
69 TestArray ta = (TestArray) in.readObject();
70 in.close();
71 } catch (InstantiationError e) {
72 throw new Error();
73 } catch (InvalidClassException e) {
74 System.err.println("\nTest passed");
75 return;
76 }
77 throw new Error();
78 }
79}