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