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 4116082
  26  *
  27  * @summary synopsis: rmid should not destroy group when it reports
  28  * inactiveGroup
  29  * @author Ann Wollrath
  30  *
  31  * @library ../../../testlibrary
  32  * @modules java.rmi/sun.rmi.registry
  33  *          java.rmi/sun.rmi.server
  34  *          java.rmi/sun.rmi.transport
  35  *          java.rmi/sun.rmi.transport.tcp
  36  *          java.base/sun.nio.ch
  37  * @build TestLibrary RMID RMIDSelectorProvider ActivationLibrary ActivateMe InactiveGroup_Stub
  38  * @run main/othervm/policy=security.policy/timeout=240 InactiveGroup
  39  */
  40 
  41 import java.io.*;
  42 import java.rmi.*;
  43 import java.rmi.activation.*;
  44 import java.rmi.server.*;
  45 import java.rmi.registry.*;
  46 import java.util.Properties;
  47 
  48 public class InactiveGroup
  49         implements ActivateMe, Runnable
  50 {
  51 
  52     private ActivationID id;
  53 
  54     public InactiveGroup(ActivationID id, MarshalledObject obj)
  55         throws ActivationException, RemoteException
  56     {
  57         this.id = id;
  58         Activatable.exportObject(this, id, 0);
  59     }
  60 
  61     public InactiveGroup() throws RemoteException {
  62         UnicastRemoteObject.exportObject(this, 0);
  63     }
  64 
  65     public void ping()
  66     {}
  67 
  68     public ActivateMe getUnicastVersion() throws RemoteException {
  69         return new InactiveGroup();
  70     }
  71 
  72     public ActivationID getID() {
  73         return id;
  74     }
  75 
  76     /**
  77      * Spawns a thread to deactivate the object.
  78      */
  79     public void shutdown() throws Exception
  80     {
  81         (new Thread(this,"InactiveGroup")).start();
  82     }
  83 
  84     /**
  85      * Thread to deactivate object. First attempts to make object
  86      * inactive (via the inactive method).  If that fails (the
  87      * object may still have pending/executing calls), then
  88      * unexport the object forcibly.
  89      */
  90     public void run()
  91     {
  92         ActivationLibrary.deactivate(this, getID());
  93     }
  94 
  95     public static void main(String[] args) {
  96 
  97         System.out.println("\nRegression test for bug 4116082\n");
  98 
  99         TestLibrary.suggestSecurityManager("java.rmi.RMISecurityManager");
 100 
 101         RMID rmid = null;
 102 
 103         try {
 104             RMID.removeLog();
 105             rmid = RMID.createRMIDOnEphemeralPort();
 106             rmid.start();
 107 
 108             /* Cause activation groups to have a security policy that will
 109              * allow security managers to be downloaded and installed
 110              */
 111             Properties p = new Properties();
 112             // this test must always set policies/managers in its
 113             // activation groups
 114             p.put("java.security.policy",
 115                   TestParams.defaultGroupPolicy);
 116             p.put("java.security.manager",
 117                   TestParams.defaultSecurityManager);
 118 
 119             /*
 120              * Create descriptor and activate object in a separate VM.
 121              */
 122             System.err.println("Creating descriptor");
 123             ActivationGroupDesc groupDesc =
 124                 new ActivationGroupDesc(p, null);
 125             ActivationGroupID groupID =
 126                 ActivationGroup.getSystem().registerGroup(groupDesc);
 127             ActivationDesc desc =
 128                 new ActivationDesc(groupID, "InactiveGroup", null, null);
 129 
 130             System.err.println("Registering descriptor");
 131             ActivateMe activatableObj = (ActivateMe) Activatable.register(desc);
 132 
 133             System.err.println("Activate object via method call");
 134             activatableObj.ping();
 135 
 136             /*
 137              * Create a unicast object in the activatable object's VM.
 138              */
 139             System.err.println("Obtain unicast object");
 140             ActivateMe unicastObj = activatableObj.getUnicastVersion();
 141 
 142             /*
 143              * Make activatable object (and therefore group) inactive.
 144              */
 145             System.err.println("Make activatable object inactive");
 146             activatableObj.shutdown();
 147 
 148             /*
 149              * Ping the unicast object a few times to make sure that the
 150              * activation group's process hasn't gone away.
 151              */
 152             System.err.println("Ping unicast object for existence");
 153             // set timeout 5 seconds
 154             final long stopTime = System.currentTimeMillis() + 5000;
 155             while (System.currentTimeMillis() < stopTime) {
 156                 unicastObj.ping();
 157                 Thread.sleep(500);
 158             }
 159 
 160             /*
 161              * Now, reactivate the activatable object; the unicast object
 162              * should no longer be accessible, since reactivating the
 163              * activatable object should kill the previous group's VM
 164              * and the unicast object along with it.
 165              */
 166             System.err.println("Reactivate activatable obj");
 167             activatableObj.ping();
 168 
 169             try {
 170                 System.err.println("Ping unicast object again");
 171                 unicastObj.ping();
 172             } catch (Exception thisShouldFail) {
 173                 System.err.println("Test passed: couldn't reach unicast obj: " +
 174                                    thisShouldFail.getMessage());
 175                 return;
 176             }
 177 
 178             TestLibrary.bomb("Test failed: unicast obj accessible after group reactivates",
 179                  null);
 180 
 181         } catch (Exception e) {
 182             TestLibrary.bomb("test failed", e);
 183         } finally {
 184             rmid.cleanup();
 185         }
 186     }
 187 }