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