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 /test/lib
  28  * @build jdk.test.lib.util.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 import jdk.test.lib.util.JarUtils;
  50 
  51 public class PackageAccessTest {
  52 
  53     static Class bcl;
  54     static Class dcl;
  55 
  56     public static void main(String[] args) throws Exception {
  57         setup();
  58 
  59         try (URLClassLoader ldr =
  60             new URLClassLoader(new URL[]{ new URL("file:foo.jar") },
  61                     PackageAccessTest.class.getClassLoader())) {
  62             bcl = Class.forName("B", true, ldr);
  63             dcl = Class.forName("D", true, ldr);
  64 
  65             Object b = bcl.newInstance();
  66             try {
  67                 swizzle(b);
  68                 throw new Error("expected InvalidClassException for class B");
  69             } catch (InvalidClassException e) {
  70                 System.out.println("caught " + e);
  71                 e.printStackTrace();
  72             }
  73             if (A.packagePrivateConstructorInvoked) {
  74                 throw new Error("package private constructor of A invoked");
  75             }
  76 
  77             Object d = dcl.newInstance();
  78             swizzle(d);
  79         }
  80     }
  81 
  82     static void swizzle(Object obj) throws Exception {
  83         ByteArrayOutputStream bout = new ByteArrayOutputStream();
  84         ObjectOutputStream oout = new ObjectOutputStream(bout);
  85         oout.writeObject(obj);
  86         oout.close();
  87         ByteArrayInputStream bin =
  88             new ByteArrayInputStream(bout.toByteArray());
  89         new TestObjectInputStream(bin).readObject();
  90     }
  91 
  92     static void setup() throws Exception {
  93         Path classes = Paths.get(System.getProperty("test.classes", ""));
  94         JarUtils.createJarFile(Paths.get("foo.jar"), classes,
  95                 classes.resolve("B.class"), classes.resolve("D.class"));
  96         Files.delete(classes.resolve("B.class"));
  97         Files.delete(classes.resolve("D.class"));
  98     }
  99 }
 100 
 101 class TestObjectInputStream extends ObjectInputStream {
 102     TestObjectInputStream(InputStream in) throws IOException {
 103         super(in);
 104     }
 105 
 106     protected Class resolveClass(ObjectStreamClass desc)
 107         throws IOException, ClassNotFoundException
 108     {
 109         String n = desc.getName();
 110         if (n.equals("B")) {
 111             return PackageAccessTest.bcl;
 112         } else if (n.equals("D")) {
 113             return PackageAccessTest.dcl;
 114         } else {
 115             return super.resolveClass(desc);
 116         }
 117     }
 118 }