|
1 /* |
|
2 * Copyright (c) 2005, 2015, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. |
|
8 * |
|
9 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 * version 2 for more details (a copy is included in the LICENSE file that |
|
13 * accompanied this code). |
|
14 * |
|
15 * You should have received a copy of the GNU General Public License version |
|
16 * 2 along with this work; if not, write to the Free Software Foundation, |
|
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 * |
|
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
20 * or visit www.oracle.com if you need additional information or have any |
|
21 * questions. |
|
22 */ |
|
23 |
|
24 import com.sun.tools.attach.VirtualMachine; |
|
25 import com.sun.tools.attach.AttachNotSupportedException; |
|
26 import java.io.File; |
|
27 import jdk.testlibrary.OutputAnalyzer; |
|
28 import jdk.testlibrary.ProcessTools; |
|
29 import jdk.testlibrary.ProcessThread; |
|
30 |
|
31 /* |
|
32 * @test |
|
33 * @bug 6173612 6273707 6277253 6335921 6348630 6342019 6381757 |
|
34 * @summary Basic unit tests for the VM attach mechanism. Unit test for Attach |
|
35 * API - this checks that a SecurityException is thrown as expected. |
|
36 * |
|
37 * @library /lib/testlibrary |
|
38 * @modules jdk.attach |
|
39 * jdk.jartool/sun.tools.jar |
|
40 * |
|
41 * @run build jdk.testlibrary.* Application |
|
42 * @run main PermissionTest |
|
43 */ |
|
44 public class PermissionTest { |
|
45 |
|
46 /* |
|
47 * The actual test is in the nested class TestMain. |
|
48 * The responsibility of this class is to: |
|
49 * 1. Start the Application class in a separate process. |
|
50 * 2. Find the pid and shutdown port of the running Application. |
|
51 * 3. Run the tests in TstMain that will attach to the Application. |
|
52 * 4. Shut down the Application. |
|
53 */ |
|
54 public static void main(String args[]) throws Throwable { |
|
55 ProcessThread processThread = null; |
|
56 try { |
|
57 processThread = RunnerUtil.startApplication(); |
|
58 runTests(processThread.getPid()); |
|
59 } catch (Throwable t) { |
|
60 System.out.println("TestPermission got unexpected exception: " + t); |
|
61 t.printStackTrace(); |
|
62 throw t; |
|
63 } finally { |
|
64 // Make sure the Application process is stopped. |
|
65 RunnerUtil.stopApplication(processThread); |
|
66 } |
|
67 } |
|
68 |
|
69 /** |
|
70 * Runs the actual test the nested class TestMain. |
|
71 * The test is run in a separate process because we need to add to the classpath. |
|
72 */ |
|
73 private static void runTests(long pid) throws Throwable { |
|
74 final String sep = File.separator; |
|
75 |
|
76 String classpath = |
|
77 System.getProperty("test.class.path", ""); |
|
78 String testSrc = System.getProperty("test.src", "") + sep; |
|
79 |
|
80 // Use a policy that will NOT allow attach. Test will verify exception. |
|
81 String[] args = { |
|
82 "-classpath", |
|
83 classpath, |
|
84 "-Djava.security.manager", |
|
85 String.format("-Djava.security.policy=%sjava.policy.deny", testSrc), |
|
86 "PermissionTest$TestMain", |
|
87 Long.toString(pid), |
|
88 "true" }; |
|
89 OutputAnalyzer output = ProcessTools.executeTestJvm(args); |
|
90 output.shouldHaveExitValue(0); |
|
91 |
|
92 // Use a policy that will allow attach. |
|
93 args = new String[] { |
|
94 "-classpath", |
|
95 classpath, |
|
96 "-Djava.security.manager", |
|
97 String.format("-Djava.security.policy=%sjava.policy.allow", testSrc), |
|
98 "PermissionTest$TestMain", |
|
99 Long.toString(pid), |
|
100 "false" }; |
|
101 output = ProcessTools.executeTestJvm(args); |
|
102 output.shouldHaveExitValue(0); |
|
103 } |
|
104 |
|
105 /** |
|
106 * This is the actual test code. It will attach to the Application and verify |
|
107 * that we get a SecurityException when that is expected. |
|
108 */ |
|
109 public static class TestMain { |
|
110 public static void main(String args[]) throws Exception { |
|
111 SecurityManager sm = System.getSecurityManager(); |
|
112 if (sm == null) { |
|
113 throw new RuntimeException("Test configuration error - no security manager set"); |
|
114 } |
|
115 |
|
116 String pid = args[0]; |
|
117 boolean shouldFail = Boolean.parseBoolean(args[1]); |
|
118 |
|
119 try { |
|
120 VirtualMachine.attach(pid).detach(); |
|
121 if (shouldFail) { |
|
122 throw new RuntimeException("SecurityException should be thrown"); |
|
123 } |
|
124 System.out.println(" - attached to target VM as expected."); |
|
125 } catch (Exception x) { |
|
126 // AttachNotSupportedException thrown when no providers can be loaded |
|
127 if (shouldFail && ((x instanceof AttachNotSupportedException) || |
|
128 (x instanceof SecurityException))) { |
|
129 System.out.println(" - exception thrown as expected."); |
|
130 } else { |
|
131 throw x; |
|
132 } |
|
133 } |
|
134 } |
|
135 } |
|
136 } |