test/java/rmi/registry/altSecurityManager/AltSecurityManager.java

Print this page


   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 }
   1 /*
   2  * Copyright (c) 1999, 2012, 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 RMID
  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     private final int regPort;
  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 final String REGISTRY_IMPL = "sun.rmi.registry.RegistryImpl";
  51     static final String ACTIVATION = "sun.rmi.server.Activation";
  52 
  53     // children should exit in at least this time.
  54     static long TIME_OUT = 15000;
  55 
  56     public AltSecurityManager(int port) {
  57         if (port <= 0) {
  58             TestLibrary.bomb("Port must be greater then 0.");
  59         }
  60 
  61         this.regPort = port;
  62     }
  63 
  64     public void run() {
  65         try {
  66             if (utilityToStart.equals(REGISTRY_IMPL)) {
  67                 vm = new JavaVM(utilityToStart,
  68                         " -Djava.security.manager=TestSecurityManager",
  69                         Integer.toString(regPort));
  70             } else if (utilityToStart.contains(ACTIVATION)) {
  71                 vm = new JavaVM(utilityToStart,
  72                         " -Djava.security.manager=TestSecurityManager",
  73                         "-port " + Integer.toString(regPort));
  74             } else {
  75                 TestLibrary.bomb("Utility to start must be " + REGISTRY_IMPL +
  76                         " or " + ACTIVATION);
  77             }
  78 
  79             System.err.println("starting " + utilityToStart);
  80             vm.start();
  81             vm.getVM().waitFor();
  82 
  83         } catch (Exception e) {
  84             TestLibrary.bomb(e);
  85         }
  86     }
  87 
  88     /**
  89      * Wait to make sure that the registry and rmid exit after
  90      * their security manager is set.
  91      */
  92     public static void ensureExit(String utility) throws Exception {
  93         utilityToStart = utility;
  94 
  95         try {
  96             int port = TestLibrary.getUnusedRandomPort();
  97             Thread thread = new Thread(new AltSecurityManager(port));
  98             System.err.println("expecting RuntimeException for " +
  99                                "checkListen in child process");
 100             long start = System.currentTimeMillis();
 101             thread.start();
 102             thread.join(TIME_OUT);
 103 
 104             long time = System.currentTimeMillis() - start;
 105             System.err.println("waited " + time + " millis for " +
 106                                utilityToStart + " to die");
 107 
 108             if (time >= TIME_OUT) {
 109 
 110                 // dont pollute other tests; increase the likelihood
 111                 // that rmid will go away if it did not exit already.
 112                 if (utility.equals(ACTIVATION)) {
 113                     RMID.shutdown(port);
 114                 }
 115 
 116                 TestLibrary.bomb(utilityToStart +
 117                                  " took too long to die...");
 118             } else {
 119                 System.err.println(utilityToStart +
 120                                    " terminated on time");
 121             }
 122         } finally {
 123             vm.destroy();
 124             vm = null;
 125         }
 126     }
 127 
 128     public static void main(String[] args) {
 129         try {
 130             System.err.println("\nRegression test for bug 4183202\n");
 131 
 132             // make sure the registry exits early.
 133             ensureExit(REGISTRY_IMPL);
 134 
 135             // make sure rmid exits early
 136             ensureExit(ACTIVATION);
 137 
 138             System.err.println("test passed");
 139 
 140         } catch (Exception e) {
 141             TestLibrary.bomb(e);
 142         } finally {
 143             RMID.removeLog();
 144         }
 145     }
 146 }