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