1 /* 2 * Copyright (c) 2005, 2018, 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.test.lib.thread.ProcessThread; 28 import jdk.test.lib.process.OutputAnalyzer; 29 import jdk.test.lib.process.ProcessTools; 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 * @library /test/lib 39 * @modules jdk.attach 40 * jdk.jartool/sun.tools.jar 41 * 42 * @run build jdk.testlibrary.* Application 43 * @run main PermissionTest 44 */ 45 public class PermissionTest { 46 47 /* 48 * The actual test is in the nested class TestMain. 49 * The responsibility of this class is to: 50 * 1. Start the Application class in a separate process. 51 * 2. Find the pid and shutdown port of the running Application. 52 * 3. Run the tests in TstMain that will attach to the Application. 53 * 4. Shut down the Application. 54 */ 55 public static void main(String args[]) throws Throwable { 56 ProcessThread processThread = null; 57 try { 58 processThread = RunnerUtil.startApplication(); 59 runTests(processThread.getPid()); 60 } catch (Throwable t) { 61 System.out.println("TestPermission got unexpected exception: " + t); 62 t.printStackTrace(); 63 throw t; 64 } finally { 65 // Make sure the Application process is stopped. 66 RunnerUtil.stopApplication(processThread); 67 } 68 } 69 70 /** 71 * Runs the actual test the nested class TestMain. 72 * The test is run in a separate process because we need to add to the classpath. 73 */ 74 private static void runTests(long pid) throws Throwable { 75 final String sep = File.separator; 76 77 String classpath = 78 System.getProperty("test.class.path", ""); 79 String testSrc = System.getProperty("test.src", "") + sep; 80 81 // Use a policy that will NOT allow attach. Test will verify exception. 82 String[] args = { 83 "-classpath", 84 classpath, 85 "-Djava.security.manager", 86 String.format("-Djava.security.policy=%sjava.policy.deny", testSrc), 87 "PermissionTest$TestMain", 88 Long.toString(pid), 89 "true" }; 90 OutputAnalyzer output = ProcessTools.executeTestJvm(args); 91 output.shouldHaveExitValue(0); 92 93 // Use a policy that will allow attach. 94 args = new String[] { 95 "-classpath", 96 classpath, 97 "-Djava.security.manager", 98 String.format("-Djava.security.policy=%sjava.policy.allow", testSrc), 99 "PermissionTest$TestMain", 100 Long.toString(pid), 101 "false" }; 102 output = ProcessTools.executeTestJvm(args); 103 output.shouldHaveExitValue(0); 104 } 105 106 /** 107 * This is the actual test code. It will attach to the Application and verify 108 * that we get a SecurityException when that is expected. 109 */ 110 public static class TestMain { 111 public static void main(String args[]) throws Exception { 112 SecurityManager sm = System.getSecurityManager(); 113 if (sm == null) { 114 throw new RuntimeException("Test configuration error - no security manager set"); 115 } 116 117 String pid = args[0]; 118 boolean shouldFail = Boolean.parseBoolean(args[1]); 119 120 try { 121 VirtualMachine.attach(pid).detach(); 122 if (shouldFail) { 123 throw new RuntimeException("SecurityException should be thrown"); 124 } 125 System.out.println(" - attached to target VM as expected."); 126 } catch (Exception x) { 127 // AttachNotSupportedException thrown when no providers can be loaded 128 if (shouldFail && ((x instanceof AttachNotSupportedException) || 129 (x instanceof SecurityException))) { 130 System.out.println(" - exception thrown as expected."); 131 } else { 132 throw x; 133 } 134 } 135 } 136 } 137 }