1 /* 2 * Copyright (c) 2002, 2010, 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 * @bug 4765255 26 * @summary Verify proper functioning of package equality checks used to 27 * determine accessibility of superclass constructor and inherited 28 * writeReplace/readResolve methods. 29 */ 30 31 import java.io.*; 32 import java.net.*; 33 34 public class Test { 35 36 static Class bcl; 37 static Class dcl; 38 39 public static void main(String[] args) throws Exception { 40 ClassLoader ldr = 41 new URLClassLoader(new URL[]{ new URL("file:foo.jar") }, 42 Test.class.getClassLoader()); 43 bcl = Class.forName("B", true, ldr); 44 dcl = Class.forName("D", true, ldr); 45 46 Object b = bcl.newInstance(); 47 try { 48 swizzle(b); 49 throw new Error("expected InvalidClassException for class B"); 50 } catch (InvalidClassException e) { 51 System.out.println("caught " + e); 52 e.printStackTrace(); 53 } 54 if (A.packagePrivateConstructorInvoked) { 55 throw new Error("package private constructor of A invoked"); 56 } 57 58 Object d = dcl.newInstance(); 59 swizzle(d); 60 } 61 62 static void swizzle(Object obj) throws Exception { 63 ByteArrayOutputStream bout = new ByteArrayOutputStream(); 64 ObjectOutputStream oout = new ObjectOutputStream(bout); 65 oout.writeObject(obj); 66 oout.close(); 67 ByteArrayInputStream bin = 68 new ByteArrayInputStream(bout.toByteArray()); 69 new TestObjectInputStream(bin).readObject(); 70 } 71 } 72 73 class TestObjectInputStream extends ObjectInputStream { 74 TestObjectInputStream(InputStream in) throws IOException { 75 super(in); 76 } 77 78 protected Class resolveClass(ObjectStreamClass desc) 79 throws IOException, ClassNotFoundException | 1 /* 2 * Copyright (c) 2002, 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 4765255 27 * @library /lib/testlibrary 28 * @build JarUtils A B C D Driver Test 29 * @run driver Driver 30 * @run main Test 31 * @summary Verify proper functioning of package equality checks used to 32 * determine accessibility of superclass constructor and inherited 33 * writeReplace/readResolve methods. 34 */ 35 36 import java.io.*; 37 import java.net.*; 38 import java.nio.file.*; 39 40 public class Test { 41 42 static Class bcl; 43 static Class dcl; 44 45 public static void main(String[] args) throws Exception { 46 URLClassLoader ldr = 47 new URLClassLoader(new URL[]{ new URL("file:foo.jar") }, 48 Test.class.getClassLoader()); 49 bcl = Class.forName("B", true, ldr); 50 dcl = Class.forName("D", true, ldr); 51 52 Object b = bcl.newInstance(); 53 try { 54 swizzle(b); 55 throw new Error("expected InvalidClassException for class B"); 56 } catch (InvalidClassException e) { 57 System.out.println("caught " + e); 58 e.printStackTrace(); 59 } 60 if (A.packagePrivateConstructorInvoked) { 61 throw new Error("package private constructor of A invoked"); 62 } 63 64 Object d = dcl.newInstance(); 65 swizzle(d); 66 ldr.close(); 67 } 68 69 static void swizzle(Object obj) throws Exception { 70 ByteArrayOutputStream bout = new ByteArrayOutputStream(); 71 ObjectOutputStream oout = new ObjectOutputStream(bout); 72 oout.writeObject(obj); 73 oout.close(); 74 ByteArrayInputStream bin = 75 new ByteArrayInputStream(bout.toByteArray()); 76 new TestObjectInputStream(bin).readObject(); 77 } 78 } 79 80 class TestObjectInputStream extends ObjectInputStream { 81 TestObjectInputStream(InputStream in) throws IOException { 82 super(in); 83 } 84 85 protected Class resolveClass(ObjectStreamClass desc) 86 throws IOException, ClassNotFoundException |