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