Path: blob/master/test/jdk/java/rmi/registry/altSecurityManager/AltSecurityManager.java
41155 views
/*1* Copyright (c) 1999, 2021, 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 rmiregistry could allow alternate security manager26* @author Laird Dornin27*28* @library ../../testlibrary29* @modules java.rmi/sun.rmi.registry30* java.rmi/sun.rmi.server31* java.rmi/sun.rmi.transport32* java.rmi/sun.rmi.transport.tcp33* java.base/sun.nio.ch34* @build TestLibrary RegistryVM RMIRegistryRunner TestSecurityManager35* @run main/othervm AltSecurityManager36*/3738/**39* Ensure that a user is able to specify alternate security managers to40* be used in rmiregistry. Test specifies a security manager41* that throws a runtime exception in its checkListen method, this42* will cause rmiregistry to exit early because those43* utilities will be unable to export any remote objects; test fails44* if registry takes too long to exit.45*/46public class AltSecurityManager implements Runnable {47// variable to hold registry child48static JavaVM vm = null;4950// names of utilities51static String utilityToStart = null;52static final String REGISTRY_IMPL = "sun.rmi.registry.RegistryImpl";5354// children should exit in at least this time.55private static final long TIME_OUT =56(long)(15000 * TestLibrary.getTimeoutFactor());5758public void run() {59try {60if (utilityToStart.equals(REGISTRY_IMPL)) {61vm = RegistryVM.createRegistryVMWithRunner(62"RMIRegistryRunner",63"-Djava.security.manager=TestSecurityManager");64} else {65TestLibrary.bomb("Utility to start must be " + REGISTRY_IMPL);66}6768System.err.println("starting " + utilityToStart);69try {70vm.start();71throw new RuntimeException("Expected exception did not occur!");72} catch (Exception expected) {73int exit = vm.waitFor();74if (exit != TestSecurityManager.EXIT_VALUE) {75throw new RuntimeException(utilityToStart76+ " exit with an unexpected value "77+ exit + ".");78}79System.err.format("Success: starting %s exited with status %d%n",80utilityToStart, TestSecurityManager.EXIT_VALUE);81}8283} catch (Exception e) {84TestLibrary.bomb(e);85}86}8788/**89* Wait to make sure that the registry exits after90* their security manager is set.91*/92public static void ensureExit(String utility) throws Exception {93utilityToStart = utility;9495try {96Thread thread = new Thread(new AltSecurityManager());97System.err.println("expecting RuntimeException for " +98"checkListen in child process");99long start = System.currentTimeMillis();100thread.start();101thread.join(TIME_OUT);102103long time = System.currentTimeMillis() - start;104System.err.println("waited " + time + " millis for " +105utilityToStart + " to die");106107if (time >= TIME_OUT) {108TestLibrary.bomb(utilityToStart +109" took too long to die...");110} else {111System.err.println(utilityToStart +112" terminated on time");113}114} finally {115vm.cleanup();116vm = null;117}118}119120public static void main(String[] args) {121try {122System.err.println("\nRegression test for bug 4183202\n");123124// make sure the registry exits early.125ensureExit(REGISTRY_IMPL);126127System.err.println("test passed");128129} catch (Exception e) {130TestLibrary.bomb(e);131}132}133}134135136