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 87 { 88 String n = desc.getName(); 89 if (n.equals("B")) { 90 return Test.bcl; 91 } else if (n.equals("D")) { 92 return Test.dcl; 93 } else { 94 return super.resolveClass(desc); 95 } 96 } 97 }