1 /*
   2  * Copyright (c) 1998, 2014, 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 /* @test
  25  * @bug 4149366
  26  * @summary The class loader used to load classes for parameter types sent in
  27  * an RMI call to an activatable object should delegate to the class loader
  28  * that loaded the class of the activatable object itself, to maximize the
  29  * likelihood of type compatibility between downloaded parameter types and
  30  * supertypes shared with the activatable object.
  31  * @author Peter Jones (much code taken from Ann Wollrath's activation tests)
  32  *
  33  * @library ../../../testlibrary
  34  * @modules java.rmi/sun.rmi.registry
  35  *          java.rmi/sun.rmi.server
  36  *          java.rmi/sun.rmi.transport
  37  *          java.rmi/sun.rmi.transport.tcp
  38  *          java.base/sun.nio.ch
  39  * @build TestLibrary RMID ActivationLibrary
  40  *     Foo FooReceiverImpl FooReceiverImpl_Stub Bar
  41  * @run main/othervm/policy=security.policy/timeout=240 DownloadParameterClass
  42  */
  43 
  44 import java.io.*;
  45 import java.net.*;
  46 import java.util.*;
  47 import java.rmi.*;
  48 import java.rmi.activation.*;
  49 import java.rmi.server.*;
  50 import java.rmi.registry.*;
  51 
  52 public class DownloadParameterClass {
  53 
  54     public interface FooReceiver extends Remote {
  55 
  56         /*
  57          * The interface can't actually declare that the method takes a
  58          * Foo, because then Foo would have to be in the test's CLASSPATH,
  59          * which might get propagated to the group VM's CLASSPATH, which
  60          * would nullify the test (the Foo supertype must be loaded in the
  61          * group VM only through the class loader that loaded the
  62          * activatable object).
  63          */
  64         public void receiveFoo(Object obj) throws RemoteException;
  65     }
  66 
  67     public static void main(String[] args) {
  68 
  69         System.err.println("\nRegression test for bug 4149366\n");
  70 
  71         /*
  72          * Install classes to be seen by the activatable object's class
  73          * loader in the "codebase1" subdirectory of working directory, and
  74          * install the subtype to be downloaded into the activatable object
  75          * into the "codebase2" subdirectory.
  76          */
  77         URL codebase1 = null;
  78         URL codebase2 = null;
  79         try {
  80             codebase1 = TestLibrary.installClassInCodebase("FooReceiverImpl", "codebase1");
  81             TestLibrary.installClassInCodebase("FooReceiverImpl_Stub", "codebase1");
  82             TestLibrary.installClassInCodebase("Foo", "codebase1");
  83             codebase2 = TestLibrary.installClassInCodebase("Bar", "codebase2");
  84         } catch (MalformedURLException e) {
  85             TestLibrary.bomb("failed to install test classes", e);
  86         }
  87 
  88         TestLibrary.suggestSecurityManager("java.rmi.RMISecurityManager");
  89 
  90         RMID rmid = null;
  91 
  92         try {
  93             RMID.removeLog();
  94             rmid = RMID.createRMIDOnEphemeralPort();
  95             rmid.start();
  96 
  97             /* Cause activation groups to have a security policy that will
  98              * allow security managers to be downloaded and installed
  99              */
 100             Properties p = new Properties();
 101             // this test must always set policies/managers in its
 102             // activation groups
 103             p.put("java.security.policy",
 104                   TestParams.defaultGroupPolicy);
 105             p.put("java.security.manager",
 106                   TestParams.defaultSecurityManager);
 107 
 108             /*
 109              * Create and register descriptors for activatable object in a
 110              * group other than this VM's group, so that another VM will be
 111              * spawned with the object is activated.
 112              */
 113             System.err.println("Creating descriptors");
 114             ActivationGroupDesc groupDesc =
 115                 new ActivationGroupDesc(p, null);
 116             ActivationGroupID groupID =
 117                 ActivationGroup.getSystem().registerGroup(groupDesc);
 118             ActivationDesc objDesc =
 119                 new ActivationDesc(groupID, "FooReceiverImpl",
 120                                    codebase1.toString(), null, false);
 121 
 122             System.err.println("Registering descriptors");
 123             FooReceiver obj = (FooReceiver) Activatable.register(objDesc);
 124 
 125             /*
 126              * Create an instance of the subtype to be downloaded by the
 127              * activatable object.  The codebase must be a path including
 128              * "codebase1" as well as "codebase2" because the supertype
 129              * must be visible here as well; the supertype cannot be
 130              * installed in both codebases (like it would be in a typical
 131              * setup) because of the trivial installation mechanism used
 132              * below, and see the comment above for why it can't be in
 133              * the test's CLASSPATH.
 134              */
 135             Class subtype = RMIClassLoader.loadClass(
 136                 codebase2 + " " + codebase1, "Bar");
 137             Object subtypeInstance = subtype.newInstance();
 138 
 139             obj.receiveFoo(subtypeInstance);
 140 
 141             System.err.println("\nTEST PASSED\n");
 142 
 143         } catch (Exception e) {
 144             TestLibrary.bomb("test failed", e);
 145         } finally {
 146             rmid.cleanup();
 147         }
 148     }
 149 }