1 /*
   2  * Copyright (c) 2000, 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 4325590
  27  * @library /test/lib
  28  * @build jdk.test.lib.util.JarUtils A B
  29  * @run main SuperclassDataLossTest
  30  * @summary Verify that superclass data is not lost when incoming superclass
  31  *          descriptor is matched with local class that is not a superclass of
  32  *          the deserialized instance's class.
  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.net.URL;
  43 import java.net.URLClassLoader;
  44 import java.net.MalformedURLException;
  45 import java.nio.file.Files;
  46 import java.nio.file.Path;
  47 import java.nio.file.Paths;
  48 import java.nio.file.StandardCopyOption;
  49 
  50 import jdk.test.lib.util.JarUtils;
  51 
  52 class MixedSuperclassStream extends ObjectInputStream {
  53     private boolean ldr12A;
  54     private URLClassLoader ldr1;
  55     private URLClassLoader ldr2;
  56 
  57     MixedSuperclassStream(InputStream in, URLClassLoader ldr1,
  58             URLClassLoader ldr2, boolean ldr1First) throws IOException {
  59         super(in);
  60         this.ldr1 = ldr1;
  61         this.ldr2 = ldr2;
  62         this.ldr12A = ldr12A;
  63     }
  64 
  65     protected Class resolveClass(ObjectStreamClass desc)
  66         throws IOException, ClassNotFoundException
  67     {
  68         // resolve A's classdesc to class != B's superclass
  69         String name = desc.getName();
  70         if (ldr12A) {
  71             if (name.equals("A")) {
  72                 return Class.forName(name, true, ldr1);
  73             } else if (name.equals("B")) {
  74                 return Class.forName(name, true, ldr2);
  75             }
  76         } else {
  77             if (name.equals("B")) {
  78                 return Class.forName(name, true, ldr1);
  79             } else if (name.equals("A")) {
  80                 return Class.forName(name, true, ldr2);
  81             }
  82         }
  83         return super.resolveClass(desc);
  84     }
  85 }
  86 
  87 public class SuperclassDataLossTest {
  88 
  89     public static void main(String[] args) throws Exception {
  90         try (URLClassLoader ldr1 = new URLClassLoader(new URL[] { new URL("file:cb1.jar") });
  91              URLClassLoader ldr2 = new URLClassLoader(new URL[] { new URL("file:cb2.jar") })) {
  92             setup();
  93 
  94             Runnable a = (Runnable) Class.forName("B", true, ldr1).newInstance();
  95             a.run();
  96 
  97             ByteArrayOutputStream bout = new ByteArrayOutputStream();
  98             ObjectOutputStream oout = new ObjectOutputStream(bout);
  99             oout.writeObject(a);
 100             oout.close();
 101 
 102             test(bout, ldr1, ldr2, true);
 103             test(bout, ldr1, ldr2, false);
 104         }
 105     }
 106 
 107     private static void test(ByteArrayOutputStream bout, URLClassLoader ldr1,
 108                              URLClassLoader ldr2, boolean ldr12A) throws Exception {
 109         ByteArrayInputStream bin =
 110             new ByteArrayInputStream(bout.toByteArray());
 111         ObjectInputStream oin = new MixedSuperclassStream(bin, ldr1, ldr2, ldr12A);
 112         Runnable a = (Runnable) oin.readObject();
 113         a.run();
 114     }
 115 
 116     private static void setup() throws Exception {
 117         Path classes = Paths.get(System.getProperty("test.classes", ""));
 118         JarUtils.createJarFile(Paths.get("cb1.jar"), classes,
 119                 classes.resolve("A.class"), classes.resolve("B.class"));
 120         Files.copy(Paths.get("cb1.jar"), Paths.get("cb2.jar"),
 121                    StandardCopyOption.REPLACE_EXISTING);
 122     }
 123 }