jdk/test/java/rmi/registry/altSecurityManager/AltSecurityManager.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 309 bda219d843f6
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/* 
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1999-2007 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/* @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @bug 4183202
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @summary rmid and rmiregistry could allow alternate security manager
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @author Laird Dornin
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * @library ../../testlibrary 
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * @build StreamPipe TestParams TestLibrary JavaVM 
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * @build AltSecurityManager TestSecurityManager
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * @run main/othervm AltSecurityManager
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * Ensure that a user is able to specify alternate security managers to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * be used in rmiregistry and rmid.  Test specifies a security manager
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * that throws a runtime exception in its checkListen method, this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * will cause rmiregistry and rmid to exit early because those
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * utilities will be unable to export any remote objects; test fails
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * if registry and rmid take too long to exit. 
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
public class AltSecurityManager implements Runnable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    // variable to hold registry and rmid children
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    static JavaVM vm = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    // names of utilities
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    static String utilityToStart = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    static String registry = "sun.rmi.registry.RegistryImpl";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    static String rmid = "sun.rmi.server.Activation";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    // children should exit in at least this time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    static long TIME_OUT = 15000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
	try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
	    vm = new JavaVM(utilityToStart,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
			    " -Djava.security.manager=TestSecurityManager",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
			    "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
	    System.err.println("starting " + utilityToStart);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
	    vm.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
	    vm.getVM().waitFor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
	} catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
	    TestLibrary.bomb(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
	}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * Wait to make sure that the registry and rmid exit after
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * their security manager is set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    public static void ensureExit(String utility) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
	utilityToStart = utility;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
	
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
	try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
	    Thread thread = new Thread(new AltSecurityManager());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
	    System.err.println("expecting RuntimeException for " + 
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
			       "checkListen in child process");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
	    long start = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
	    thread.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
	    thread.join(TIME_OUT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
	    
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
	    long time = System.currentTimeMillis() - start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
	    System.err.println("waited " + time + " millis for " + 
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
			       utilityToStart + " to die");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
	    if (time >= TIME_OUT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
		
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
		// dont pollute other tests; increase the likelihood 
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                // that rmid will go away if it did not exit already.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
		if (utility.equals(rmid)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
		    RMID.shutdown();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
		}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
		
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
		TestLibrary.bomb(utilityToStart + 
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
				 " took too long to die...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
	    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
		System.err.println(utilityToStart + 
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
				   " terminated on time");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
	    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
	} finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
	    vm.destroy();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
	    vm = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
	}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    public static void main(String[] args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
	try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
	    System.err.println("\nRegression test for bug 4183202\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
	    // make sure the registry exits early.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
	    ensureExit(registry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
	    // make sure rmid exits early
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
	    ensureExit(rmid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
	    System.err.println("test passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
	} catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
	    TestLibrary.bomb(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
	} finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
	    RMID.removeLog();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
	}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
}