1 /*
   2  * Copyright (c) 1998, 2007, 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 4155894
  26  *
  27  * @summary remote references can't be downloaded
  28  * @author Ann Wollrath
  29  *
  30  * test currently needs RMISecurityManager because of
  31  * 4180392
  32  *
  33  * @library ../../testlibrary
  34  * @build UseCustomRef
  35  * @build Ping
  36  * @build UseCustomRef_Stub
  37  * @build UseCustomRef_Skel
  38  * @run main/othervm/policy=security.policy/secure=java.rmi.RMISecurityManager/timeout=120 UseCustomRef
  39  *
  40  * This test was failing to run because the synthetic access
  41  * control context used by the application class loader to find and define
  42  * CustomServerRef does not have accessClassInPackage.sun.rmi.server runtime
  43  * permission necessary to load its superclass sun.rmi.server.UnicastServerRef,
  44  * even though this test's code is granted that permission in its policy file.
  45  * That bug number is 4256530
  46  */
  47 
  48 import java.io.*;
  49 import java.rmi.*;
  50 import java.rmi.server.*;
  51 import java.rmi.registry.*;
  52 import sun.rmi.transport.LiveRef;
  53 
  54 public class UseCustomRef
  55         extends RemoteServer
  56         implements Ping
  57 {
  58 
  59     public UseCustomRef() throws RemoteException {
  60         exportObject();
  61     }
  62 
  63     public void exportObject() throws RemoteException {
  64         ref = new CustomServerRef(new LiveRef(0));
  65         ((ServerRef) ref).exportObject(this, null);
  66     }
  67 
  68     public RemoteRef getRef() { return ref; }
  69 
  70     public void ping() {}
  71 
  72     public void receiveAndPing(Ping p) throws RemoteException {
  73         p.ping();
  74     }
  75 
  76     public static void main(String[] args) {
  77         Ping obj = null;
  78         Registry registry = null;
  79 
  80         try {
  81             /*
  82              * create registry
  83              */
  84             TestLibrary.suggestSecurityManager("java.rmi.RMISecurityManager");
  85 
  86             System.err.println("creating Registry...");
  87             registry = LocateRegistry.createRegistry(TestLibrary.REGISTRY_PORT);
  88 
  89             /*
  90              * create object with custom ref and bind in registry
  91              */
  92             System.err.println("creating UseCustomRef...");
  93             UseCustomRef cr = new UseCustomRef();
  94             RemoteRef ref = cr.getRef();
  95             if (!(ref instanceof CustomServerRef)) {
  96                 TestLibrary.bomb("test failed: reference not " +
  97                                 "instanceof CustomServerRef");
  98             }
  99 
 100             String name = "//:" + TestLibrary.REGISTRY_PORT + "/UseCustomRef";
 101             //      String name = "UseCustomRef";
 102             System.err.println("binding object in registry...");
 103             Naming.rebind(name, cr);
 104 
 105             /*
 106              * look up object and invoke its ping method
 107              */
 108             System.err.println("ping object...");
 109             obj = (Ping) Naming.lookup(name);
 110             obj.ping();
 111 
 112             /*
 113              * pass object with custom ref in remote call
 114              */
 115             System.err.println("pass object in remote call...");
 116             obj.receiveAndPing(cr);
 117 
 118             /*
 119              * write remote object with custom ref to output stream
 120              */
 121             System.err.println("writing remote object to stream...");
 122             ByteArrayOutputStream bout = new ByteArrayOutputStream();
 123             ObjectOutputStream out = new ObjectOutputStream(bout);
 124             out.writeObject(cr);
 125             out.flush();
 126             out.close();
 127 
 128             /*
 129              * read back remote object from output stream
 130              */
 131             System.err.println("reading remote object from stream...");
 132             ObjectInputStream in = new ObjectInputStream(
 133                 new ByteArrayInputStream(bout.toByteArray()));
 134             cr = (UseCustomRef) in.readObject();
 135 
 136             /*
 137              * re-export object and ping
 138              */
 139             System.err.println("re-export object read...");
 140             cr.exportObject();
 141             System.err.println("look up object again...");
 142             Naming.rebind(name, cr);
 143             System.err.println("ping object read...");
 144             obj = (Ping) Naming.lookup(name);
 145             obj.ping();
 146             System.err.println("TEST PASSED");
 147             Naming.unbind(name);
 148             cr = null;
 149 
 150         } catch (Exception e) {
 151             TestLibrary.bomb("test failed with exception: ", e);
 152         } finally {
 153             TestLibrary.unexport(obj);
 154             TestLibrary.unexport(registry);
 155 
 156             registry = null;
 157             obj = null;
 158         }
 159     }
 160 
 161     public static class CustomServerRef
 162         extends sun.rmi.server.UnicastServerRef
 163     {
 164         public CustomServerRef() {}
 165 
 166         public CustomServerRef(LiveRef ref) {
 167             super(ref);
 168         }
 169         /*****
 170         public CustomServerRef(int port,
 171                                RMIClientSocketFactory csf,
 172                                RMIServerSocketFactory ssf)
 173         {
 174             super (new LiveRef(port, csf, ssf));
 175         }
 176         *****/
 177 
 178         public String getRefClass(ObjectOutput out) {
 179             return "";
 180         }
 181 
 182         protected void unmarshalCustomCallData(ObjectInput in)
 183             throws IOException, ClassNotFoundException
 184         {
 185             System.err.println("unmarshalling call data...");
 186             String s = (String) (in.readObject());
 187             System.err.println(s);
 188         }
 189 
 190         protected RemoteRef getClientRef() {
 191             return new CustomRef(ref);
 192         }
 193     }
 194 
 195     public static class CustomRef extends sun.rmi.server.UnicastRef {
 196 
 197         public CustomRef() {
 198         }
 199 
 200         public CustomRef(sun.rmi.transport.LiveRef ref) {
 201             super(ref);
 202         }
 203 
 204         protected void marshalCustomCallData(ObjectOutput out)
 205             throws IOException
 206         {
 207             // this custom data ensures that a custom server
 208             // ref has written the relevant information.
 209             System.err.println("marshalling call data...");
 210             out.writeObject("hello there.");
 211         }
 212 
 213         public String getRefClass(ObjectOutput out) {
 214             return "";
 215         }
 216 
 217     }
 218 }