325N/A/*
325N/A * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved.
325N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
325N/A *
325N/A * This code is free software; you can redistribute it and/or modify it
325N/A * under the terms of the GNU General Public License version 2 only, as
325N/A * published by the Free Software Foundation.
325N/A *
325N/A * This code is distributed in the hope that it will be useful, but WITHOUT
325N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
325N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
325N/A * version 2 for more details (a copy is included in the LICENSE file that
325N/A * accompanied this code).
325N/A *
325N/A * You should have received a copy of the GNU General Public License version
325N/A * 2 along with this work; if not, write to the Free Software Foundation,
325N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
325N/A *
325N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
325N/A * or visit www.oracle.com if you need additional information or have any
325N/A * questions.
325N/A */
325N/A
325N/A/* @test
325N/A * @bug 4183202
325N/A * @summary rmid and rmiregistry could allow alternate security manager
325N/A * @author Laird Dornin
325N/A *
325N/A * @library ../../testlibrary
325N/A * @build TestLibrary JavaVM RMID TestSecurityManager
325N/A * @run main/othervm AltSecurityManager
325N/A */
325N/A
325N/A/**
325N/A * Ensure that a user is able to specify alternate security managers to
325N/A * be used in rmiregistry and rmid. Test specifies a security manager
325N/A * that throws a runtime exception in its checkListen method, this
325N/A * will cause rmiregistry and rmid to exit early because those
325N/A * utilities will be unable to export any remote objects; test fails
325N/A * if registry and rmid take too long to exit.
325N/A */
325N/Apublic class AltSecurityManager implements Runnable {
325N/A private final int regPort;
325N/A // variable to hold registry and rmid children
325N/A static JavaVM vm = null;
325N/A
325N/A // names of utilities
325N/A static String utilityToStart = null;
325N/A static final String REGISTRY_IMPL = "sun.rmi.registry.RegistryImpl";
325N/A static final String ACTIVATION = "sun.rmi.server.Activation";
325N/A
325N/A // children should exit in at least this time.
325N/A static long TIME_OUT = 15000;
325N/A
325N/A public AltSecurityManager(int port) {
325N/A if (port <= 0) {
325N/A TestLibrary.bomb("Port must be greater then 0.");
325N/A }
325N/A
325N/A this.regPort = port;
325N/A }
325N/A
325N/A public void run() {
325N/A try {
325N/A if (utilityToStart.equals(REGISTRY_IMPL)) {
325N/A vm = new JavaVM(utilityToStart,
325N/A " -Djava.security.manager=TestSecurityManager",
325N/A Integer.toString(regPort));
325N/A } else if (utilityToStart.contains(ACTIVATION)) {
325N/A vm = new JavaVM(utilityToStart,
325N/A " -Djava.security.manager=TestSecurityManager",
325N/A "-port " + Integer.toString(regPort));
325N/A } else {
325N/A TestLibrary.bomb("Utility to start must be " + REGISTRY_IMPL +
325N/A " or " + ACTIVATION);
325N/A }
325N/A
325N/A System.err.println("starting " + utilityToStart);
325N/A vm.execute();
325N/A
325N/A } catch (Exception e) {
325N/A TestLibrary.bomb(e);
325N/A }
325N/A }
325N/A
325N/A /**
325N/A * Wait to make sure that the registry and rmid exit after
325N/A * their security manager is set.
325N/A */
325N/A public static void ensureExit(String utility) throws Exception {
325N/A utilityToStart = utility;
325N/A
325N/A try {
325N/A int port = TestLibrary.getUnusedRandomPort();
325N/A Thread thread = new Thread(new AltSecurityManager(port));
325N/A System.err.println("expecting RuntimeException for " +
325N/A "checkListen in child process");
325N/A long start = System.currentTimeMillis();
325N/A thread.start();
325N/A thread.join(TIME_OUT);
325N/A
325N/A long time = System.currentTimeMillis() - start;
325N/A System.err.println("waited " + time + " millis for " +
325N/A utilityToStart + " to die");
325N/A
325N/A if (time >= TIME_OUT) {
325N/A
325N/A // dont pollute other tests; increase the likelihood
325N/A // that rmid will go away if it did not exit already.
325N/A if (utility.equals(ACTIVATION)) {
325N/A RMID.shutdown(port);
325N/A }
325N/A
325N/A TestLibrary.bomb(utilityToStart +
325N/A " took too long to die...");
325N/A } else {
325N/A System.err.println(utilityToStart +
325N/A " terminated on time");
325N/A }
325N/A } finally {
325N/A vm.destroy();
325N/A vm = null;
325N/A }
325N/A }
325N/A
325N/A public static void main(String[] args) {
325N/A try {
325N/A System.err.println("\nRegression test for bug 4183202\n");
325N/A
325N/A // make sure the registry exits early.
325N/A ensureExit(REGISTRY_IMPL);
325N/A
325N/A // make sure rmid exits early
325N/A ensureExit(ACTIVATION);
325N/A
325N/A System.err.println("test passed");
325N/A
325N/A } catch (Exception e) {
325N/A TestLibrary.bomb(e);
325N/A } finally {
325N/A RMID.removeLog();
325N/A }
325N/A }
325N/A}
325N/A