Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/rmi/registry/altSecurityManager/AltSecurityManager.java
38828 views
/*1* Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24* @bug 418320225* @summary rmid and rmiregistry could allow alternate security manager26* @author Laird Dornin27*28* @library ../../testlibrary29* @build TestLibrary JavaVM RMID TestSecurityManager30* @run main/othervm AltSecurityManager31*/3233/**34* Ensure that a user is able to specify alternate security managers to35* be used in rmiregistry and rmid. Test specifies a security manager36* that throws a runtime exception in its checkListen method, this37* will cause rmiregistry and rmid to exit early because those38* utilities will be unable to export any remote objects; test fails39* if registry and rmid take too long to exit.40*/41public class AltSecurityManager implements Runnable {42private final int regPort;43// variable to hold registry and rmid children44static JavaVM vm = null;4546// names of utilities47static String utilityToStart = null;48static final String REGISTRY_IMPL = "sun.rmi.registry.RegistryImpl";49static final String ACTIVATION = "sun.rmi.server.Activation";5051// children should exit in at least this time.52static long TIME_OUT = 15000;5354public AltSecurityManager(int port) {55if (port <= 0) {56TestLibrary.bomb("Port must be greater then 0.");57}5859this.regPort = port;60}6162public void run() {63try {64if (utilityToStart.equals(REGISTRY_IMPL)) {65vm = new JavaVM(utilityToStart,66" -Djava.security.manager=TestSecurityManager",67Integer.toString(regPort));68} else if (utilityToStart.contains(ACTIVATION)) {69vm = new JavaVM(utilityToStart,70" -Djava.security.manager=TestSecurityManager",71"-port " + Integer.toString(regPort));72} else {73TestLibrary.bomb("Utility to start must be " + REGISTRY_IMPL +74" or " + ACTIVATION);75}7677System.err.println("starting " + utilityToStart);78vm.execute();7980} catch (Exception e) {81TestLibrary.bomb(e);82}83}8485/**86* Wait to make sure that the registry and rmid exit after87* their security manager is set.88*/89public static void ensureExit(String utility) throws Exception {90utilityToStart = utility;9192try {93int port = TestLibrary.getUnusedRandomPort();94Thread thread = new Thread(new AltSecurityManager(port));95System.err.println("expecting RuntimeException for " +96"checkListen in child process");97long start = System.currentTimeMillis();98thread.start();99thread.join(TIME_OUT);100101long time = System.currentTimeMillis() - start;102System.err.println("waited " + time + " millis for " +103utilityToStart + " to die");104105if (time >= TIME_OUT) {106107// dont pollute other tests; increase the likelihood108// that rmid will go away if it did not exit already.109if (utility.equals(ACTIVATION)) {110RMID.shutdown(port);111}112113TestLibrary.bomb(utilityToStart +114" took too long to die...");115} else {116System.err.println(utilityToStart +117" terminated on time");118}119} finally {120vm.destroy();121vm = null;122}123}124125public static void main(String[] args) {126try {127System.err.println("\nRegression test for bug 4183202\n");128129// make sure the registry exits early.130ensureExit(REGISTRY_IMPL);131132// make sure rmid exits early133ensureExit(ACTIVATION);134135System.err.println("test passed");136137} catch (Exception e) {138TestLibrary.bomb(e);139} finally {140RMID.removeLog();141}142}143}144145146