jdk/test/java/rmi/registry/altSecurityManager/AltSecurityManager.java
author mli
Wed, 11 Jan 2017 19:36:11 -0800
changeset 43066 bc2234c64fbd
parent 43010 f3c984a6f1d9
permissions -rw-r--r--
8172314: java/rmi/registry/altSecurityManager/AltSecurityManager.java fails with "port in use" Reviewed-by: rriggs
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
309
bda219d843f6 6627823: Missed whitespace normalization files: jdk/test/java/rmi
ohair
parents: 2
diff changeset
     1
/*
43066
bc2234c64fbd 8172314: java/rmi/registry/altSecurityManager/AltSecurityManager.java fails with "port in use"
mli
parents: 43010
diff changeset
     2
 * Copyright (c) 1999, 2017, Oracle and/or its affiliates. All rights reserved.
2
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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    21
 * questions.
2
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
 *
309
bda219d843f6 6627823: Missed whitespace normalization files: jdk/test/java/rmi
ohair
parents: 2
diff changeset
    29
 * @library ../../testlibrary
30820
0d4717a011d3 8081347: Add @modules to jdk_core tests
mchung
parents: 27784
diff changeset
    30
 * @modules java.rmi/sun.rmi.registry
0d4717a011d3 8081347: Add @modules to jdk_core tests
mchung
parents: 27784
diff changeset
    31
 *          java.rmi/sun.rmi.server
0d4717a011d3 8081347: Add @modules to jdk_core tests
mchung
parents: 27784
diff changeset
    32
 *          java.rmi/sun.rmi.transport
0d4717a011d3 8081347: Add @modules to jdk_core tests
mchung
parents: 27784
diff changeset
    33
 *          java.rmi/sun.rmi.transport.tcp
43066
bc2234c64fbd 8172314: java/rmi/registry/altSecurityManager/AltSecurityManager.java fails with "port in use"
mli
parents: 43010
diff changeset
    34
 *          java.base/sun.nio.ch
bc2234c64fbd 8172314: java/rmi/registry/altSecurityManager/AltSecurityManager.java fails with "port in use"
mli
parents: 43010
diff changeset
    35
 * @build TestLibrary RMID RMIDSelectorProvider RegistryVM RMIRegistryRunner
bc2234c64fbd 8172314: java/rmi/registry/altSecurityManager/AltSecurityManager.java fails with "port in use"
mli
parents: 43010
diff changeset
    36
 *        TestSecurityManager
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * @run main/othervm AltSecurityManager
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * Ensure that a user is able to specify alternate security managers to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * be used in rmiregistry and rmid.  Test specifies a security manager
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * that throws a runtime exception in its checkListen method, this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * will cause rmiregistry and rmid to exit early because those
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * utilities will be unable to export any remote objects; test fails
309
bda219d843f6 6627823: Missed whitespace normalization files: jdk/test/java/rmi
ohair
parents: 2
diff changeset
    46
 * if registry and rmid take too long to exit.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
public class AltSecurityManager implements Runnable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    // variable to hold registry and rmid children
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    static JavaVM vm = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    // names of utilities
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    static String utilityToStart = null;
13256
5886d7607acd 7142596: RMI JPRT tests are failing
dmocek
parents: 5506
diff changeset
    54
    static final String REGISTRY_IMPL = "sun.rmi.registry.RegistryImpl";
5886d7607acd 7142596: RMI JPRT tests are failing
dmocek
parents: 5506
diff changeset
    55
    static final String ACTIVATION = "sun.rmi.server.Activation";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    // children should exit in at least this time.
43010
f3c984a6f1d9 8030175: java/rmi/registry/altSecurityManager/AltSecurityManager.java fails due to timeout
mli
parents: 35667
diff changeset
    58
    private static final long TIME_OUT =
f3c984a6f1d9 8030175: java/rmi/registry/altSecurityManager/AltSecurityManager.java fails due to timeout
mli
parents: 35667
diff changeset
    59
            (long)(15000 * TestLibrary.getTimeoutFactor());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    public void run() {
309
bda219d843f6 6627823: Missed whitespace normalization files: jdk/test/java/rmi
ohair
parents: 2
diff changeset
    62
        try {
13256
5886d7607acd 7142596: RMI JPRT tests are failing
dmocek
parents: 5506
diff changeset
    63
            if (utilityToStart.equals(REGISTRY_IMPL)) {
43066
bc2234c64fbd 8172314: java/rmi/registry/altSecurityManager/AltSecurityManager.java fails with "port in use"
mli
parents: 43010
diff changeset
    64
                vm = RegistryVM.createRegistryVMWithRunner(
bc2234c64fbd 8172314: java/rmi/registry/altSecurityManager/AltSecurityManager.java fails with "port in use"
mli
parents: 43010
diff changeset
    65
                        "RMIRegistryRunner",
bc2234c64fbd 8172314: java/rmi/registry/altSecurityManager/AltSecurityManager.java fails with "port in use"
mli
parents: 43010
diff changeset
    66
                        "-Djava.security.manager=TestSecurityManager");
13256
5886d7607acd 7142596: RMI JPRT tests are failing
dmocek
parents: 5506
diff changeset
    67
            } else if (utilityToStart.contains(ACTIVATION)) {
43066
bc2234c64fbd 8172314: java/rmi/registry/altSecurityManager/AltSecurityManager.java fails with "port in use"
mli
parents: 43010
diff changeset
    68
                vm = RMID.createRMIDOnEphemeralPortWithOptions(
bc2234c64fbd 8172314: java/rmi/registry/altSecurityManager/AltSecurityManager.java fails with "port in use"
mli
parents: 43010
diff changeset
    69
                        "-Djava.security.manager=TestSecurityManager");
13256
5886d7607acd 7142596: RMI JPRT tests are failing
dmocek
parents: 5506
diff changeset
    70
            } else {
5886d7607acd 7142596: RMI JPRT tests are failing
dmocek
parents: 5506
diff changeset
    71
                TestLibrary.bomb("Utility to start must be " + REGISTRY_IMPL +
5886d7607acd 7142596: RMI JPRT tests are failing
dmocek
parents: 5506
diff changeset
    72
                        " or " + ACTIVATION);
5886d7607acd 7142596: RMI JPRT tests are failing
dmocek
parents: 5506
diff changeset
    73
            }
5886d7607acd 7142596: RMI JPRT tests are failing
dmocek
parents: 5506
diff changeset
    74
309
bda219d843f6 6627823: Missed whitespace normalization files: jdk/test/java/rmi
ohair
parents: 2
diff changeset
    75
            System.err.println("starting " + utilityToStart);
43066
bc2234c64fbd 8172314: java/rmi/registry/altSecurityManager/AltSecurityManager.java fails with "port in use"
mli
parents: 43010
diff changeset
    76
            try {
bc2234c64fbd 8172314: java/rmi/registry/altSecurityManager/AltSecurityManager.java fails with "port in use"
mli
parents: 43010
diff changeset
    77
                vm.start();
bc2234c64fbd 8172314: java/rmi/registry/altSecurityManager/AltSecurityManager.java fails with "port in use"
mli
parents: 43010
diff changeset
    78
                throw new RuntimeException("Expected exception did not occur!");
bc2234c64fbd 8172314: java/rmi/registry/altSecurityManager/AltSecurityManager.java fails with "port in use"
mli
parents: 43010
diff changeset
    79
            } catch (Exception expected) {
bc2234c64fbd 8172314: java/rmi/registry/altSecurityManager/AltSecurityManager.java fails with "port in use"
mli
parents: 43010
diff changeset
    80
                int exit = vm.waitFor();
bc2234c64fbd 8172314: java/rmi/registry/altSecurityManager/AltSecurityManager.java fails with "port in use"
mli
parents: 43010
diff changeset
    81
                if (exit != TestSecurityManager.EXIT_VALUE) {
bc2234c64fbd 8172314: java/rmi/registry/altSecurityManager/AltSecurityManager.java fails with "port in use"
mli
parents: 43010
diff changeset
    82
                    throw new RuntimeException(utilityToStart
bc2234c64fbd 8172314: java/rmi/registry/altSecurityManager/AltSecurityManager.java fails with "port in use"
mli
parents: 43010
diff changeset
    83
                            + " exit with an unexpected value "
bc2234c64fbd 8172314: java/rmi/registry/altSecurityManager/AltSecurityManager.java fails with "port in use"
mli
parents: 43010
diff changeset
    84
                            + exit + ".");
bc2234c64fbd 8172314: java/rmi/registry/altSecurityManager/AltSecurityManager.java fails with "port in use"
mli
parents: 43010
diff changeset
    85
                }
bc2234c64fbd 8172314: java/rmi/registry/altSecurityManager/AltSecurityManager.java fails with "port in use"
mli
parents: 43010
diff changeset
    86
                System.err.format("Success: starting %s exited with status %d%n",
bc2234c64fbd 8172314: java/rmi/registry/altSecurityManager/AltSecurityManager.java fails with "port in use"
mli
parents: 43010
diff changeset
    87
                                  utilityToStart, TestSecurityManager.EXIT_VALUE);
bc2234c64fbd 8172314: java/rmi/registry/altSecurityManager/AltSecurityManager.java fails with "port in use"
mli
parents: 43010
diff changeset
    88
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
309
bda219d843f6 6627823: Missed whitespace normalization files: jdk/test/java/rmi
ohair
parents: 2
diff changeset
    90
        } catch (Exception e) {
bda219d843f6 6627823: Missed whitespace normalization files: jdk/test/java/rmi
ohair
parents: 2
diff changeset
    91
            TestLibrary.bomb(e);
bda219d843f6 6627823: Missed whitespace normalization files: jdk/test/java/rmi
ohair
parents: 2
diff changeset
    92
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    }
309
bda219d843f6 6627823: Missed whitespace normalization files: jdk/test/java/rmi
ohair
parents: 2
diff changeset
    94
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * Wait to make sure that the registry and rmid exit after
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * their security manager is set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    public static void ensureExit(String utility) throws Exception {
309
bda219d843f6 6627823: Missed whitespace normalization files: jdk/test/java/rmi
ohair
parents: 2
diff changeset
   100
        utilityToStart = utility;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
309
bda219d843f6 6627823: Missed whitespace normalization files: jdk/test/java/rmi
ohair
parents: 2
diff changeset
   102
        try {
43066
bc2234c64fbd 8172314: java/rmi/registry/altSecurityManager/AltSecurityManager.java fails with "port in use"
mli
parents: 43010
diff changeset
   103
            Thread thread = new Thread(new AltSecurityManager());
309
bda219d843f6 6627823: Missed whitespace normalization files: jdk/test/java/rmi
ohair
parents: 2
diff changeset
   104
            System.err.println("expecting RuntimeException for " +
bda219d843f6 6627823: Missed whitespace normalization files: jdk/test/java/rmi
ohair
parents: 2
diff changeset
   105
                               "checkListen in child process");
bda219d843f6 6627823: Missed whitespace normalization files: jdk/test/java/rmi
ohair
parents: 2
diff changeset
   106
            long start = System.currentTimeMillis();
bda219d843f6 6627823: Missed whitespace normalization files: jdk/test/java/rmi
ohair
parents: 2
diff changeset
   107
            thread.start();
bda219d843f6 6627823: Missed whitespace normalization files: jdk/test/java/rmi
ohair
parents: 2
diff changeset
   108
            thread.join(TIME_OUT);
bda219d843f6 6627823: Missed whitespace normalization files: jdk/test/java/rmi
ohair
parents: 2
diff changeset
   109
bda219d843f6 6627823: Missed whitespace normalization files: jdk/test/java/rmi
ohair
parents: 2
diff changeset
   110
            long time = System.currentTimeMillis() - start;
bda219d843f6 6627823: Missed whitespace normalization files: jdk/test/java/rmi
ohair
parents: 2
diff changeset
   111
            System.err.println("waited " + time + " millis for " +
bda219d843f6 6627823: Missed whitespace normalization files: jdk/test/java/rmi
ohair
parents: 2
diff changeset
   112
                               utilityToStart + " to die");
bda219d843f6 6627823: Missed whitespace normalization files: jdk/test/java/rmi
ohair
parents: 2
diff changeset
   113
bda219d843f6 6627823: Missed whitespace normalization files: jdk/test/java/rmi
ohair
parents: 2
diff changeset
   114
            if (time >= TIME_OUT) {
bda219d843f6 6627823: Missed whitespace normalization files: jdk/test/java/rmi
ohair
parents: 2
diff changeset
   115
                TestLibrary.bomb(utilityToStart +
bda219d843f6 6627823: Missed whitespace normalization files: jdk/test/java/rmi
ohair
parents: 2
diff changeset
   116
                                 " took too long to die...");
bda219d843f6 6627823: Missed whitespace normalization files: jdk/test/java/rmi
ohair
parents: 2
diff changeset
   117
            } else {
bda219d843f6 6627823: Missed whitespace normalization files: jdk/test/java/rmi
ohair
parents: 2
diff changeset
   118
                System.err.println(utilityToStart +
bda219d843f6 6627823: Missed whitespace normalization files: jdk/test/java/rmi
ohair
parents: 2
diff changeset
   119
                                   " terminated on time");
bda219d843f6 6627823: Missed whitespace normalization files: jdk/test/java/rmi
ohair
parents: 2
diff changeset
   120
            }
bda219d843f6 6627823: Missed whitespace normalization files: jdk/test/java/rmi
ohair
parents: 2
diff changeset
   121
        } finally {
43066
bc2234c64fbd 8172314: java/rmi/registry/altSecurityManager/AltSecurityManager.java fails with "port in use"
mli
parents: 43010
diff changeset
   122
            vm.cleanup();
309
bda219d843f6 6627823: Missed whitespace normalization files: jdk/test/java/rmi
ohair
parents: 2
diff changeset
   123
            vm = null;
bda219d843f6 6627823: Missed whitespace normalization files: jdk/test/java/rmi
ohair
parents: 2
diff changeset
   124
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    }
309
bda219d843f6 6627823: Missed whitespace normalization files: jdk/test/java/rmi
ohair
parents: 2
diff changeset
   126
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    public static void main(String[] args) {
309
bda219d843f6 6627823: Missed whitespace normalization files: jdk/test/java/rmi
ohair
parents: 2
diff changeset
   128
        try {
bda219d843f6 6627823: Missed whitespace normalization files: jdk/test/java/rmi
ohair
parents: 2
diff changeset
   129
            System.err.println("\nRegression test for bug 4183202\n");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
309
bda219d843f6 6627823: Missed whitespace normalization files: jdk/test/java/rmi
ohair
parents: 2
diff changeset
   131
            // make sure the registry exits early.
13256
5886d7607acd 7142596: RMI JPRT tests are failing
dmocek
parents: 5506
diff changeset
   132
            ensureExit(REGISTRY_IMPL);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
309
bda219d843f6 6627823: Missed whitespace normalization files: jdk/test/java/rmi
ohair
parents: 2
diff changeset
   134
            // make sure rmid exits early
13256
5886d7607acd 7142596: RMI JPRT tests are failing
dmocek
parents: 5506
diff changeset
   135
            ensureExit(ACTIVATION);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
309
bda219d843f6 6627823: Missed whitespace normalization files: jdk/test/java/rmi
ohair
parents: 2
diff changeset
   137
            System.err.println("test passed");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
309
bda219d843f6 6627823: Missed whitespace normalization files: jdk/test/java/rmi
ohair
parents: 2
diff changeset
   139
        } catch (Exception e) {
bda219d843f6 6627823: Missed whitespace normalization files: jdk/test/java/rmi
ohair
parents: 2
diff changeset
   140
            TestLibrary.bomb(e);
bda219d843f6 6627823: Missed whitespace normalization files: jdk/test/java/rmi
ohair
parents: 2
diff changeset
   141
        } finally {
bda219d843f6 6627823: Missed whitespace normalization files: jdk/test/java/rmi
ohair
parents: 2
diff changeset
   142
            RMID.removeLog();
bda219d843f6 6627823: Missed whitespace normalization files: jdk/test/java/rmi
ohair
parents: 2
diff changeset
   143
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
}