J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 1999 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 4227189 |
| 26 | * @summary Ensure that class descriptor read, write hooks exist, are backwards |
| 27 | * compatible, and work as advertised. |
| 28 | */ |
| 29 | |
| 30 | import java.io.*; |
| 31 | import java.util.*; |
| 32 | |
| 33 | class Foo implements Serializable { |
| 34 | private static final long serialVersionUID = 1L; |
| 35 | Short s = new Short((short) 1); |
| 36 | Integer i = new Integer(2); |
| 37 | Long l = new Long(3); |
| 38 | |
| 39 | public boolean equals(Object obj) { |
| 40 | if (obj instanceof Foo) { |
| 41 | Foo ofoo = (Foo) obj; |
| 42 | return s.equals(ofoo.s) && i.equals(ofoo.i) && l.equals(ofoo.l); |
| 43 | } |
| 44 | return false; |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | class CustomOutputStream extends ObjectOutputStream { |
| 49 | |
| 50 | boolean hookCalled = false; |
| 51 | |
| 52 | CustomOutputStream(OutputStream out) throws IOException { |
| 53 | super(out); |
| 54 | useProtocolVersion(PROTOCOL_VERSION_2); |
| 55 | } |
| 56 | |
| 57 | protected void writeClassDescriptor(ObjectStreamClass desc) |
| 58 | throws IOException |
| 59 | { |
| 60 | writeUTF(desc.getName()); |
| 61 | hookCalled = true; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | class CustomInputStream extends ObjectInputStream { |
| 66 | |
| 67 | boolean hookCalled = false; |
| 68 | |
| 69 | CustomInputStream(InputStream in) throws IOException { |
| 70 | super(in); |
| 71 | } |
| 72 | |
| 73 | protected ObjectStreamClass readClassDescriptor() |
| 74 | throws IOException, ClassNotFoundException |
| 75 | { |
| 76 | hookCalled = true; |
| 77 | return ObjectStreamClass.lookup(Class.forName(readUTF())); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | public class ClassDescHooks implements ObjectStreamConstants { |
| 82 | public static void main(String[] args) throws Exception { |
| 83 | ByteArrayOutputStream bout; |
| 84 | ByteArrayInputStream bin; |
| 85 | ObjectOutputStream oout; |
| 86 | ObjectInputStream oin; |
| 87 | FileInputStream fin; |
| 88 | File foof; |
| 89 | CustomOutputStream cout; |
| 90 | CustomInputStream cin; |
| 91 | |
| 92 | // test for backwards compatibility |
| 93 | bout = new ByteArrayOutputStream(); |
| 94 | foof = new File(System.getProperty("test.src", "."), "Foo.ser"); |
| 95 | fin = new FileInputStream(foof); |
| 96 | while (fin.available() > 0) |
| 97 | bout.write(fin.read()); |
| 98 | byte[] buf1 = bout.toByteArray(); |
| 99 | |
| 100 | bout = new ByteArrayOutputStream(); |
| 101 | oout = new ObjectOutputStream(bout); |
| 102 | Foo foo = new Foo(); |
| 103 | oout.writeObject(foo); |
| 104 | oout.flush(); |
| 105 | byte[] buf2 = bout.toByteArray(); |
| 106 | |
| 107 | if (! Arrays.equals(buf1, buf2)) |
| 108 | throw new Error("Incompatible stream format (write)"); |
| 109 | |
| 110 | fin = new FileInputStream(foof); |
| 111 | oin = new ObjectInputStream(fin); |
| 112 | Foo foocopy = (Foo) oin.readObject(); |
| 113 | if (! foo.equals(foocopy)) |
| 114 | throw new Error("Incompatible stream format (read)"); |
| 115 | |
| 116 | // make sure write hook not called when old protocol in use |
| 117 | bout = new ByteArrayOutputStream(); |
| 118 | cout = new CustomOutputStream(bout); |
| 119 | cout.useProtocolVersion(PROTOCOL_VERSION_1); |
| 120 | cout.writeObject(foo); |
| 121 | if (cout.hookCalled) |
| 122 | throw new Error("write descriptor hook should not be called"); |
| 123 | |
| 124 | // write custom class descriptor representations |
| 125 | bout = new ByteArrayOutputStream(); |
| 126 | cout = new CustomOutputStream(bout); |
| 127 | cout.writeObject(foo); |
| 128 | cout.flush(); |
| 129 | bin = new ByteArrayInputStream(bout.toByteArray()); |
| 130 | cin = new CustomInputStream(bin); |
| 131 | foocopy = (Foo) cin.readObject(); |
| 132 | if (! cout.hookCalled) |
| 133 | throw new Error("write descriptor hook never called"); |
| 134 | if (! cin.hookCalled) |
| 135 | throw new Error("read descriptor hook never called"); |
| 136 | if (! foo.equals(foocopy)) |
| 137 | throw new Error("serialization failed when hooks active"); |
| 138 | } |
| 139 | } |