jdk/test/com/sun/tools/attach/PermissionTest.java
author ihse
Tue, 09 May 2017 12:57:30 +0200
changeset 45028 b0ea3c0bfb81
parent 44423 306c020eb154
permissions -rw-r--r--
8179889: Fix typographic errors in copyright headers Reviewed-by: erikj, dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
45028
b0ea3c0bfb81 8179889: Fix typographic errors in copyright headers
ihse
parents: 44423
diff changeset
     2
 * Copyright (c) 2005, 2015, 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;
21968
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    26
import java.io.File;
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    27
import jdk.testlibrary.OutputAnalyzer;
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    28
import jdk.testlibrary.ProcessTools;
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    29
import jdk.testlibrary.ProcessThread;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
21968
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    31
/*
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    32
 * @test
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    33
 * @bug 6173612 6273707 6277253 6335921 6348630 6342019 6381757
44423
306c020eb154 8176176: fix @modules in jdk_svc tests
iignatyev
parents: 30820
diff changeset
    34
 * @summary Basic unit tests for the VM attach mechanism. Unit test for Attach
306c020eb154 8176176: fix @modules in jdk_svc tests
iignatyev
parents: 30820
diff changeset
    35
 * API - this checks that a SecurityException is thrown as expected.
306c020eb154 8176176: fix @modules in jdk_svc tests
iignatyev
parents: 30820
diff changeset
    36
 *
21968
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    37
 * @library /lib/testlibrary
44423
306c020eb154 8176176: fix @modules in jdk_svc tests
iignatyev
parents: 30820
diff changeset
    38
 * @modules jdk.attach
30376
2ccf2cf7ea48 8078896: Add @modules as needed to the jdk_svc tests
ykantser
parents: 28760
diff changeset
    39
 *          jdk.jartool/sun.tools.jar
44423
306c020eb154 8176176: fix @modules in jdk_svc tests
iignatyev
parents: 30820
diff changeset
    40
 *
25182
1f6766bac82c 8048193: [tests] Replace JPS and stdout based PID retrieval by Process.getPid()
jbachorik
parents: 24366
diff changeset
    41
 * @run build jdk.testlibrary.* Application
21968
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    42
 * @run main PermissionTest
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    43
 */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
public class PermissionTest {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
21968
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    46
    /*
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    47
     * The actual test is in the nested class TestMain.
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    48
     * The responsibility of this class is to:
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    49
     * 1. Start the Application class in a separate process.
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    50
     * 2. Find the pid and shutdown port of the running Application.
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    51
     * 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
    52
     * 4. Shut down the Application.
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    53
     */
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    54
    public static void main(String args[]) throws Throwable {
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    55
        ProcessThread processThread = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        try {
25182
1f6766bac82c 8048193: [tests] Replace JPS and stdout based PID retrieval by Process.getPid()
jbachorik
parents: 24366
diff changeset
    57
            processThread = RunnerUtil.startApplication();
1f6766bac82c 8048193: [tests] Replace JPS and stdout based PID retrieval by Process.getPid()
jbachorik
parents: 24366
diff changeset
    58
            runTests(processThread.getPid());
21968
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    59
        } catch (Throwable t) {
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    60
            System.out.println("TestPermission got unexpected exception: " + t);
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    61
            t.printStackTrace();
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    62
            throw t;
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    63
        } finally {
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    64
            // Make sure the Application process is stopped.
25182
1f6766bac82c 8048193: [tests] Replace JPS and stdout based PID retrieval by Process.getPid()
jbachorik
parents: 24366
diff changeset
    65
            RunnerUtil.stopApplication(processThread);
21968
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    66
        }
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    67
    }
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
     * Runs the actual test the nested class TestMain.
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    71
     * 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
    72
     */
25182
1f6766bac82c 8048193: [tests] Replace JPS and stdout based PID retrieval by Process.getPid()
jbachorik
parents: 24366
diff changeset
    73
    private static void runTests(long pid) throws Throwable {
21968
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    74
        final String sep = File.separator;
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    75
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    76
        String classpath =
28760
b83d51c77b31 8067945: SVC jdk/test/* should be cleaned from JRE layout dependency
ykantser
parents: 25182
diff changeset
    77
            System.getProperty("test.class.path", "");
21968
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    78
        String testSrc = System.getProperty("test.src", "") + sep;
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    79
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    80
        // 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
    81
        String[] args = {
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    82
            "-classpath",
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    83
            classpath,
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    84
            "-Djava.security.manager",
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    85
            String.format("-Djava.security.policy=%sjava.policy.deny", testSrc),
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    86
            "PermissionTest$TestMain",
25182
1f6766bac82c 8048193: [tests] Replace JPS and stdout based PID retrieval by Process.getPid()
jbachorik
parents: 24366
diff changeset
    87
            Long.toString(pid),
21968
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    88
            "true" };
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    89
        OutputAnalyzer output = ProcessTools.executeTestJvm(args);
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    90
        output.shouldHaveExitValue(0);
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    91
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    92
        // Use a policy that will allow attach.
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    93
        args = new String[] {
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    94
            "-classpath",
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    95
            classpath,
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    96
            "-Djava.security.manager",
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    97
            String.format("-Djava.security.policy=%sjava.policy.allow", testSrc),
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    98
            "PermissionTest$TestMain",
25182
1f6766bac82c 8048193: [tests] Replace JPS and stdout based PID retrieval by Process.getPid()
jbachorik
parents: 24366
diff changeset
    99
            Long.toString(pid),
21968
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
   100
            "false" };
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
   101
        output = ProcessTools.executeTestJvm(args);
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
   102
        output.shouldHaveExitValue(0);
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
   103
    }
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
   104
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
   105
    /**
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
   106
     * 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
   107
     * that we get a SecurityException when that is expected.
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
    public static class TestMain {
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
   110
        public static void main(String args[]) throws Exception {
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
   111
            SecurityManager sm = System.getSecurityManager();
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
   112
            if (sm == null) {
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
   113
                throw new RuntimeException("Test configuration error - no security manager set");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            }
21968
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
   115
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
   116
            String pid = args[0];
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
   117
            boolean shouldFail = Boolean.parseBoolean(args[1]);
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
   118
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
   119
            try {
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
   120
                VirtualMachine.attach(pid).detach();
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
   121
                if (shouldFail) {
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
   122
                    throw new RuntimeException("SecurityException should be thrown");
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
   123
                }
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
   124
                System.out.println(" - attached to target VM as expected.");
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
   125
            } catch (Exception x) {
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
   126
                // AttachNotSupportedException thrown when no providers can be loaded
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
   127
                if (shouldFail && ((x instanceof AttachNotSupportedException) ||
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
   128
                    (x instanceof SecurityException))) {
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
   129
                    System.out.println(" - exception thrown as expected.");
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
   130
                } else {
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
   131
                    throw x;
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
   132
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
}