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 PackageAccessTest 29 * @run main PackageAccessTest 30 * @summary Verify proper functioning of package equality checks used to 31 * determine accessibility of superclass constructor and inherited 32 * writeReplace/readResolve methods. 33 */ 34 35 import java.io.ByteArrayInputStream; 36 import java.io.ByteArrayOutputStream; 37 import java.io.InputStream; 38 import java.io.IOException; 39 import java.io.ObjectInputStream; 40 import java.io.ObjectOutputStream; 41 import java.io.ObjectStreamClass; 42 import java.io.InvalidClassException; 43 import java.net.URL; 44 import java.net.URLClassLoader; 45 import java.nio.file.Files; 46 import java.nio.file.Path; 47 import java.nio.file.Paths; 48 49 public class PackageAccessTest { 50 51 static Class bcl; 52 static Class dcl; 53 54 public static void main(String[] args) throws Exception { 55 setup(); 56 57 try (URLClassLoader ldr = 58 new URLClassLoader(new URL[]{ new URL("file:foo.jar") }, 59 PackageAccessTest.class.getClassLoader())) { 60 bcl = Class.forName("B", true, ldr); 61 dcl = Class.forName("D", true, ldr); 62 63 Object b = bcl.newInstance(); 64 try { 65 swizzle(b); 66 throw new Error("expected InvalidClassException for class B"); 67 } catch (InvalidClassException e) { 68 System.out.println("caught " + e); 69 e.printStackTrace(); 70 } 71 if (A.packagePrivateConstructorInvoked) { 72 throw new Error("package private constructor of A invoked"); 73 } 74 75 Object d = dcl.newInstance(); 76 swizzle(d); 77 } 78 } 79 80 static void swizzle(Object obj) throws Exception { 81 ByteArrayOutputStream bout = new ByteArrayOutputStream(); 82 ObjectOutputStream oout = new ObjectOutputStream(bout); 83 oout.writeObject(obj); 84 oout.close(); 85 ByteArrayInputStream bin = 86 new ByteArrayInputStream(bout.toByteArray()); 87 new TestObjectInputStream(bin).readObject(); 88 } 89 90 static void setup() throws Exception { 91 Path classes = Paths.get(System.getProperty("test.classes", "")); 92 JarUtils.createJarFile(Paths.get("foo.jar"), classes, 93 classes.resolve("B.class"), classes.resolve("D.class")); 94 Files.delete(classes.resolve("B.class")); 95 Files.delete(classes.resolve("D.class")); 96 } 97 } 98 99 class TestObjectInputStream extends ObjectInputStream { 100 TestObjectInputStream(InputStream in) throws IOException { 101 super(in); 102 } 103 104 protected Class resolveClass(ObjectStreamClass desc) 105 throws IOException, ClassNotFoundException 106 { 107 String n = desc.getName(); 108 if (n.equals("B")) { 109 return PackageAccessTest.bcl; 110 } else if (n.equals("D")) { 111 return PackageAccessTest.dcl; 112 } else { 113 return super.resolveClass(desc); 114 } 115 } 116 }