jdk/test/com/sun/tools/attach/PermissionTest.java
author sla
Tue, 03 Dec 2013 17:06:23 +0100
changeset 21968 619035df6988
parent 5506 202f599c92aa
child 24366 57c0a8b7a936
permissions -rw-r--r--
6461635: [TESTBUG] BasicTests.sh test fails intermittently Summary: Transform dummy class instead of BigInteger to avoid complication by -Xshare. Ported from script to java. Reviewed-by: alanb Contributed-by: mattias.tobiasson@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
21968
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
     2
 * Copyright (c) 2005, 2013 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: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
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
import com.sun.tools.attach.VirtualMachine;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
import com.sun.tools.attach.AttachNotSupportedException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
import java.util.Properties;
21968
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    27
import java.io.File;
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    28
import jdk.testlibrary.OutputAnalyzer;
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    29
import jdk.testlibrary.ProcessTools;
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    30
import jdk.testlibrary.ProcessThread;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
21968
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    32
/*
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    33
 * @test
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    34
 * @bug 6173612 6273707 6277253 6335921 6348630 6342019 6381757
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    35
 * @summary Basic unit tests for the VM attach mechanism.
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    36
 * @library /lib/testlibrary
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    37
 * @run build Application Shutdown
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    38
 * @run main PermissionTest
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    39
 *
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    40
 * Unit test for Attach API -
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    41
 * this checks that a SecurityException is thrown as expected.
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    42
 */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
public class PermissionTest {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
21968
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    45
    /*
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    46
     * The actual test is in the nested class TestMain.
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    47
     * The responsibility of this class is to:
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    48
     * 1. Start the Application class in a separate process.
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    49
     * 2. Find the pid and shutdown port of the running Application.
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    50
     * 3. Run the tests in TstMain that will attach to the Application.
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    51
     * 4. Shut down the Application.
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    52
     */
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    53
    public static void main(String args[]) throws Throwable {
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    54
        final String pidFile ="TestPermission.Application.pid";
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    55
        ProcessThread processThread = null;
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    56
        RunnerUtil.ProcessInfo info = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        try {
21968
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    58
            processThread = RunnerUtil.startApplication(pidFile);
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    59
            info = RunnerUtil.readProcessInfo(pidFile);
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    60
            runTests(info.pid);
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    61
        } catch (Throwable t) {
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    62
            System.out.println("TestPermission got unexpected exception: " + t);
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    63
            t.printStackTrace();
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    64
            throw t;
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    65
        } finally {
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    66
            // Make sure the Application process is stopped.
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    67
            RunnerUtil.stopApplication(info.shutdownPort, processThread);
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    68
        }
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    69
    }
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    70
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    71
    /**
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    72
     * Runs the actual test the nested class TestMain.
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    73
     * The test is run in a separate process because we need to add to the classpath.
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    74
     */
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    75
    private static void runTests(int pid) throws Throwable {
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    76
        final String sep = File.separator;
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    77
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    78
        // Need to add jdk/lib/tools.jar to classpath.
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    79
        String classpath =
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    80
            System.getProperty("test.class.path", "") + File.pathSeparator +
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    81
            System.getProperty("test.jdk", ".") + sep + "lib" + sep + "tools.jar";
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    82
        String testSrc = System.getProperty("test.src", "") + sep;
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    83
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    84
        // Use a policy that will NOT allow attach. Test will verify exception.
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    85
        String[] args = {
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    86
            "-classpath",
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    87
            classpath,
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    88
            "-Djava.security.manager",
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    89
            String.format("-Djava.security.policy=%sjava.policy.deny", testSrc),
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    90
            "PermissionTest$TestMain",
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    91
            Integer.toString(pid),
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    92
            "true" };
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    93
        OutputAnalyzer output = ProcessTools.executeTestJvm(args);
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    94
        output.shouldHaveExitValue(0);
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    95
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    96
        // Use a policy that will allow attach.
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    97
        args = new String[] {
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    98
            "-classpath",
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    99
            classpath,
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
   100
            "-Djava.security.manager",
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
   101
            String.format("-Djava.security.policy=%sjava.policy.allow", testSrc),
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
   102
            "PermissionTest$TestMain",
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
   103
            Integer.toString(pid),
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
   104
            "false" };
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
   105
        output = ProcessTools.executeTestJvm(args);
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
   106
        output.shouldHaveExitValue(0);
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
   107
    }
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
   108
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
   109
    /**
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
   110
     * This is the actual test code. It will attach to the Application and verify
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
   111
     * that we get a SecurityException when that is expected.
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
   112
     */
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
   113
    public static class TestMain {
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
   114
        public static void main(String args[]) throws Exception {
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
   115
            SecurityManager sm = System.getSecurityManager();
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
   116
            if (sm == null) {
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
   117
                throw new RuntimeException("Test configuration error - no security manager set");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            }
21968
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
   119
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
   120
            String pid = args[0];
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
   121
            boolean shouldFail = Boolean.parseBoolean(args[1]);
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
   122
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
   123
            try {
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
   124
                VirtualMachine.attach(pid).detach();
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
   125
                if (shouldFail) {
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
   126
                    throw new RuntimeException("SecurityException should be thrown");
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
   127
                }
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
   128
                System.out.println(" - attached to target VM as expected.");
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
   129
            } catch (Exception x) {
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
   130
                // AttachNotSupportedException thrown when no providers can be loaded
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
   131
                if (shouldFail && ((x instanceof AttachNotSupportedException) ||
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
   132
                    (x instanceof SecurityException))) {
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
   133
                    System.out.println(" - exception thrown as expected.");
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
   134
                } else {
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
   135
                    throw x;
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
   136
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
}