Path: blob/master/test/jdk/java/io/Serializable/packageAccess/PackageAccessTest.java
41153 views
/*1* Copyright (c) 2002, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 476525526* @library /test/lib27* @build jdk.test.lib.util.JarUtils A B C D PackageAccessTest28* @run main PackageAccessTest29* @summary Verify proper functioning of package equality checks used to30* determine accessibility of superclass constructor and inherited31* writeReplace/readResolve methods.32*/3334import java.io.ByteArrayInputStream;35import java.io.ByteArrayOutputStream;36import java.io.InputStream;37import java.io.IOException;38import java.io.ObjectInputStream;39import java.io.ObjectOutputStream;40import java.io.ObjectStreamClass;41import java.io.InvalidClassException;42import java.net.URL;43import java.net.URLClassLoader;44import java.nio.file.Files;45import java.nio.file.Path;46import java.nio.file.Paths;4748import jdk.test.lib.util.JarUtils;4950public class PackageAccessTest {5152static Class<?> bcl;53static Class<?> dcl;5455public static void main(String[] args) throws Exception {56setup();5758try (URLClassLoader ldr =59new URLClassLoader(new URL[]{ new URL("file:foo.jar") },60PackageAccessTest.class.getClassLoader())) {61bcl = Class.forName("B", true, ldr);62dcl = Class.forName("D", true, ldr);6364Object b = bcl.getConstructor().newInstance();65try {66swizzle(b);67throw new Error("expected InvalidClassException for class B");68} catch (InvalidClassException e) {69System.out.println("caught " + e);70e.printStackTrace();71}72if (A.packagePrivateConstructorInvoked) {73throw new Error("package private constructor of A invoked");74}7576Object d = dcl.getConstructor().newInstance();77swizzle(d);78}79}8081static void swizzle(Object obj) throws Exception {82ByteArrayOutputStream bout = new ByteArrayOutputStream();83ObjectOutputStream oout = new ObjectOutputStream(bout);84oout.writeObject(obj);85oout.close();86ByteArrayInputStream bin =87new ByteArrayInputStream(bout.toByteArray());88new TestObjectInputStream(bin).readObject();89}9091static void setup() throws Exception {92Path classes = Paths.get(System.getProperty("test.classes", ""));93JarUtils.createJarFile(Paths.get("foo.jar"), classes,94classes.resolve("B.class"), classes.resolve("D.class"));95Files.delete(classes.resolve("B.class"));96Files.delete(classes.resolve("D.class"));97}98}99100class TestObjectInputStream extends ObjectInputStream {101TestObjectInputStream(InputStream in) throws IOException {102super(in);103}104105protected Class<?> resolveClass(ObjectStreamClass desc)106throws IOException, ClassNotFoundException107{108String n = desc.getName();109if (n.equals("B")) {110return PackageAccessTest.bcl;111} else if (n.equals("D")) {112return PackageAccessTest.dcl;113} else {114return super.resolveClass(desc);115}116}117}118119120