1 /*
   2  * Copyright (c) 1999, 2013, 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 4183202
  26  * @summary rmid and rmiregistry could allow alternate security manager
  27  * @author Laird Dornin
  28  *
  29  * @library ../../testlibrary
  30  * @build TestLibrary JavaVM RMID TestSecurityManager
  31  * @run main/othervm AltSecurityManager
  32  */
  33 
  34 /**
  35  * Ensure that a user is able to specify alternate security managers to
  36  * be used in rmiregistry and rmid.  Test specifies a security manager
  37  * that throws a runtime exception in its checkListen method, this
  38  * will cause rmiregistry and rmid to exit early because those
  39  * utilities will be unable to export any remote objects; test fails
  40  * if registry and rmid take too long to exit.
  41  */
  42 public class AltSecurityManager implements Runnable {
  43     private final int regPort;
  44     // variable to hold registry and rmid children
  45     static JavaVM vm = null;
  46 
  47     // names of utilities
  48     static String utilityToStart = null;
  49     static final String REGISTRY_IMPL = "sun.rmi.registry.RegistryImpl";
  50     static final String ACTIVATION = "sun.rmi.server.Activation";
  51 
  52     // children should exit in at least this time.
  53     static long TIME_OUT = 15000;
  54 
  55     public AltSecurityManager(int port) {
  56         if (port <= 0) {
  57             TestLibrary.bomb("Port must be greater then 0.");
  58         }
  59 
  60         this.regPort = port;
  61     }
  62 
  63     public void run() {
  64         try {
  65             if (utilityToStart.equals(REGISTRY_IMPL)) {
  66                 vm = new JavaVM(utilityToStart,
  67                         " -Djava.security.manager=TestSecurityManager",
  68                         Integer.toString(regPort));
  69             } else if (utilityToStart.contains(ACTIVATION)) {
  70                 vm = new JavaVM(utilityToStart,
  71                         " -Djava.security.manager=TestSecurityManager",
  72                         "-port " + Integer.toString(regPort));
  73             } else {
  74                 TestLibrary.bomb("Utility to start must be " + REGISTRY_IMPL +
  75                         " or " + ACTIVATION);
  76             }
  77 
  78             System.err.println("starting " + utilityToStart);
  79             vm.execute();
  80 
  81         } catch (Exception e) {
  82             TestLibrary.bomb(e);
  83         }
  84     }
  85 
  86     /**
  87      * Wait to make sure that the registry and rmid exit after
  88      * their security manager is set.
  89      */
  90     public static void ensureExit(String utility) throws Exception {
  91         utilityToStart = utility;
  92 
  93         try {
  94             int port = TestLibrary.getUnusedRandomPort();
  95             Thread thread = new Thread(new AltSecurityManager(port));
  96             System.err.println("expecting RuntimeException for " +
  97                                "checkListen in child process");
  98             long start = System.currentTimeMillis();
  99             thread.start();
 100             thread.join(TIME_OUT);
 101 
 102             long time = System.currentTimeMillis() - start;
 103             System.err.println("waited " + time + " millis for " +
 104                                utilityToStart + " to die");
 105 
 106             if (time >= TIME_OUT) {
 107 
 108                 // dont pollute other tests; increase the likelihood
 109                 // that rmid will go away if it did not exit already.
 110                 if (utility.equals(ACTIVATION)) {
 111                     RMID.shutdown(port);
 112                 }
 113 
 114                 TestLibrary.bomb(utilityToStart +
 115                                  " took too long to die...");
 116             } else {
 117                 System.err.println(utilityToStart +
 118                                    " terminated on time");
 119             }
 120         } finally {
 121             vm.destroy();
 122             vm = null;
 123         }
 124     }
 125 
 126     public static void main(String[] args) {
 127         try {
 128             System.err.println("\nRegression test for bug 4183202\n");
 129 
 130             // make sure the registry exits early.
 131             ensureExit(REGISTRY_IMPL);
 132 
 133             // make sure rmid exits early
 134             ensureExit(ACTIVATION);
 135 
 136             System.err.println("test passed");
 137 
 138         } catch (Exception e) {
 139             TestLibrary.bomb(e);
 140         } finally {
 141             RMID.removeLog();
 142         }
 143     }
 144 }