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 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 XObjectInputStream.java XObjectOutputStream.java Test.java 42 * @run main Test 43 * @run main/othervm/policy=Allow.policy Test -expectSecurityException 44 */ 45 46 import java.io.*; 47 48 /** 49 * Test if customized readObject and writeObject are called. 50 */ 51 class B implements Serializable { 52 public int publicIntField; 53 public static int numWriteObjectCalled = 0; 54 B(int v) { 55 publicIntField = v; 56 } 57 private void writeObject(ObjectOutputStream os) throws IOException { 58 numWriteObjectCalled++; 59 os.defaultWriteObject(); 60 } 61 62 private void readObject(ObjectInputStream is) 63 throws IOException, ClassNotFoundException 113 }; 114 115 public class Test { 116 public static void main(String argv[]) 117 throws IOException, ClassNotFoundException 118 { 119 boolean expectSecurityException = false; 120 121 if (argv.length > 0 && 122 argv[0].compareTo("-expectSecurityException") == 0) 123 expectSecurityException = true; 124 125 ByteArrayOutputStream baos = new ByteArrayOutputStream(20); 126 XObjectOutputStream os = null; 127 try { 128 os = new XObjectOutputStream(baos); 129 if (expectSecurityException) 130 throw new Error("Assertion failure. " + 131 "Expected a security exception on previous line."); 132 } catch (SecurityException e) { 133 if (expectSecurityException) { 134 System.err.println("Caught expected security exception."); 135 return; 136 } 137 throw e; 138 } 139 os.writeObject(new A()); 140 os.close(); 141 if (B.numWriteObjectCalled != 3) 142 throw new Error("Expected B.writeObject() to be called 3 times;" + 143 " observed only " + B.numWriteObjectCalled + " times"); 144 145 XObjectInputStream is = 146 new XObjectInputStream(new ByteArrayInputStream(baos.toByteArray())); 147 try { 148 A a = (A)is.readObject(); 149 throw new Error("Expected readObjectOverride() to be called and throw IOException(not implemented)"); 150 } catch (IOException e) { 151 } 152 is.close(); 153 } 154 }; |