1 /* 2 * Copyright (c) 1998, Oracle and/or its affiliates. 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 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. 22 */ 23 24 /* 25 * @test 26 * @bug 4100915 27 * @summary Verify that [write/read]ObjectOverride methods get called. 28 * Test verifies that ALL methods to write an object can 29 * be overridden. Howver, the testing for reading an object 30 * is incomplete. Only test that readObjectOverride is called. 31 * An entire protocol would need to be implemented and written 32 * out before being able to test the input side of the API. 33 * 34 * Also, would be appropriate that this program verify 35 * that if SerializablePermission "enableSubclassImplamentation" 36 * is not in the security policy and security is enabled, that 37 * a security excepiton is thrown when constructing the 38 * ObjectOutputStream subclass. 39 * 40 * 41 * @compile AbstractObjectInputStream.java AbstractObjectOutputStream.java XObjectInputStream.java XObjectOutputStream.java Test.java 42 * @run main Test 43 */ 44 45 import java.io.*; 46 47 /** 48 * Test if customized readObject and writeObject are called. 49 */ 50 class B implements Serializable { 51 public int publicIntField; 52 public static int numWriteObjectCalled = 0; 53 B(int v) { 54 publicIntField = v; 55 } 56 private void writeObject(ObjectOutputStream os) throws IOException { 57 numWriteObjectCalled++; 58 os.defaultWriteObject(); 59 } 60 61 private void readObject(ObjectInputStream is) 62 throws IOException, ClassNotFoundException 63 { 64 is.defaultReadObject(); 65 } 94 } 95 96 }; 97 98 99 class A implements Serializable { 100 public int publicIntField; 101 public long publicLongField; 102 public B publicBField; 103 public B[] publicBArray = { new B(4), new B(6)}; 104 public C publicCField; 105 106 public A() { 107 publicIntField = 3; 108 publicLongField = 10L; 109 publicBField = new B(5); 110 publicCField = new C(); 111 } 112 }; 113 114 public class Test { 115 public static void main(String argv[]) 116 throws IOException, ClassNotFoundException 117 { 118 boolean expectSecurityException = false; 119 120 if (argv.length > 0 && 121 argv[0].compareTo("-expectSecurityException") == 0) 122 expectSecurityException = true; 123 124 ByteArrayOutputStream baos = new ByteArrayOutputStream(20); 125 XObjectOutputStream os = null; 126 try { 127 os = new XObjectOutputStream(baos); 128 if (expectSecurityException) 129 throw new Error("Assertion failure. " + 130 "Expected a security exception on previous line."); 131 } catch (SecurityException e) { 132 if (expectSecurityException) 133 return; 134 else 135 throw e; 136 } 137 os.writeObject(new A()); 138 os.close(); 139 if (B.numWriteObjectCalled != 3) 140 throw new Error("Expected B.writeObject() to be called 3 times;" + 141 " observed only " + B.numWriteObjectCalled + " times"); 142 143 XObjectInputStream is = 144 new XObjectInputStream(new ByteArrayInputStream(baos.toByteArray())); 145 try { 146 A a = (A)is.readObject(); 147 throw new Error("Expected readObjectOverride() to be called and throw IOException(not implemented)"); 148 } catch (IOException e) { 149 } 150 is.close(); 151 } 152 }; | 1 /* 2 * Copyright (c) 1998, 2017, Oracle and/or its affiliates. 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 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. 22 */ 23 24 /* 25 * @test 26 * @bug 4100915 27 * @summary Verify that [write/read]ObjectOverride methods get called. 28 * Test verifies that ALL methods to write an object can 29 * be overridden. However, the testing for reading an object 30 * is incomplete. Only test that readObjectOverride is called. 31 * An entire protocol would need to be implemented and written 32 * out before being able to test the input side of the API. 33 * 34 * Also, would be appropriate that this program verify 35 * that if SerializablePermission "enableSubclassImplementation" 36 * is not in the security policy and security is enabled, that 37 * a security excepiton is thrown when constructing the 38 * ObjectOutputStream subclass. 39 * 40 * 41 * @compile AbstractObjectInputStream.java AbstractObjectOutputStream.java 42 * @compile XObjectInputStream.java XObjectOutputStream.java 43 * @compile SubclassTest.java 44 * @run main SubclassTest 45 * @run main/othervm/policy=Allow.policy SubclassTest -expectSecurityException 46 */ 47 48 import java.io.ByteArrayInputStream; 49 import java.io.ByteArrayOutputStream; 50 import java.io.IOException; 51 import java.io.ObjectInputStream; 52 import java.io.ObjectOutputStream; 53 import java.io.ObjectStreamField; 54 import java.io.Serializable; 55 56 /** 57 * Test if customized readObject and writeObject are called. 58 */ 59 class B implements Serializable { 60 public int publicIntField; 61 public static int numWriteObjectCalled = 0; 62 B(int v) { 63 publicIntField = v; 64 } 65 private void writeObject(ObjectOutputStream os) throws IOException { 66 numWriteObjectCalled++; 67 os.defaultWriteObject(); 68 } 69 70 private void readObject(ObjectInputStream is) 71 throws IOException, ClassNotFoundException 72 { 73 is.defaultReadObject(); 74 } 103 } 104 105 }; 106 107 108 class A implements Serializable { 109 public int publicIntField; 110 public long publicLongField; 111 public B publicBField; 112 public B[] publicBArray = { new B(4), new B(6)}; 113 public C publicCField; 114 115 public A() { 116 publicIntField = 3; 117 publicLongField = 10L; 118 publicBField = new B(5); 119 publicCField = new C(); 120 } 121 }; 122 123 public class SubclassTest { 124 public static void main(String argv[]) 125 throws IOException, ClassNotFoundException 126 { 127 boolean expectSecurityException = false; 128 129 if (argv.length > 0 && 130 argv[0].compareTo("-expectSecurityException") == 0) 131 expectSecurityException = true; 132 133 ByteArrayOutputStream baos = new ByteArrayOutputStream(20); 134 XObjectOutputStream os = null; 135 try { 136 os = new XObjectOutputStream(baos); 137 if (expectSecurityException) 138 throw new Error("Assertion failure. " + 139 "Expected a security exception on previous line."); 140 } catch (SecurityException e) { 141 if (expectSecurityException) { 142 System.err.println("Caught expected security exception."); 143 return; 144 } 145 throw e; 146 } 147 os.writeObject(new A()); 148 os.close(); 149 if (B.numWriteObjectCalled != 3) 150 throw new Error("Expected B.writeObject() to be called 3 times;" + 151 " observed only " + B.numWriteObjectCalled + " times"); 152 153 XObjectInputStream is = 154 new XObjectInputStream(new ByteArrayInputStream(baos.toByteArray())); 155 try { 156 A a = (A)is.readObject(); 157 throw new Error("Expected readObjectOverride() to be called and throw IOException(not implemented)"); 158 } catch (IOException e) { 159 } 160 is.close(); 161 } 162 }; |