Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/com/sun/tools/attach/PermissionTest.java
38855 views
/*1* Copyright (c) 2005, 2013 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.util.Properties;26import java.io.File;27import jdk.testlibrary.OutputAnalyzer;28import jdk.testlibrary.ProcessTools;29import jdk.testlibrary.ProcessThread;3031/*32* @test33* @bug 6173612 6273707 6277253 6335921 6348630 6342019 638175734* @summary Basic unit tests for the VM attach mechanism.35* @library /lib/testlibrary36* @build jdk.testlibrary.* Application Shutdown37* @run main PermissionTest38*39* Unit test for Attach API -40* this checks that a SecurityException is thrown as expected.41*/42public class PermissionTest {4344/*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*/52public static void main(String args[]) throws Throwable {53final String pidFile ="TestPermission.Application.pid";54ProcessThread processThread = null;55RunnerUtil.ProcessInfo info = null;56try {57processThread = RunnerUtil.startApplication(pidFile);58info = RunnerUtil.readProcessInfo(pidFile);59runTests(info.pid);60} catch (Throwable t) {61System.out.println("TestPermission got unexpected exception: " + t);62t.printStackTrace();63throw t;64} finally {65// Make sure the Application process is stopped.66RunnerUtil.stopApplication(info.shutdownPort, processThread);67}68}6970/**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*/74private static void runTests(int pid) throws Throwable {75final String sep = File.separator;7677// Need to add jdk/lib/tools.jar to classpath.78String classpath =79System.getProperty("test.class.path", "") + File.pathSeparator +80System.getProperty("test.jdk", ".") + sep + "lib" + sep + "tools.jar";81String testSrc = System.getProperty("test.src", "") + sep;8283// Use a policy that will NOT allow attach. Test will verify exception.84String[] args = {85"-classpath",86classpath,87"-Djava.security.manager",88String.format("-Djava.security.policy=%sjava.policy.deny", testSrc),89"PermissionTest$TestMain",90Integer.toString(pid),91"true" };92OutputAnalyzer output = ProcessTools.executeTestJvm(args);93output.shouldHaveExitValue(0);9495// Use a policy that will allow attach.96args = new String[] {97"-classpath",98classpath,99"-Djava.security.manager",100String.format("-Djava.security.policy=%sjava.policy.allow", testSrc),101"PermissionTest$TestMain",102Integer.toString(pid),103"false" };104output = ProcessTools.executeTestJvm(args);105output.shouldHaveExitValue(0);106}107108/**109* This is the actual test code. It will attach to the Application and verify110* that we get a SecurityException when that is expected.111*/112public static class TestMain {113public static void main(String args[]) throws Exception {114SecurityManager sm = System.getSecurityManager();115if (sm == null) {116throw new RuntimeException("Test configuration error - no security manager set");117}118119String pid = args[0];120boolean shouldFail = Boolean.parseBoolean(args[1]);121122try {123VirtualMachine.attach(pid).detach();124if (shouldFail) {125throw new RuntimeException("SecurityException should be thrown");126}127System.out.println(" - attached to target VM as expected.");128} catch (Exception x) {129// AttachNotSupportedException thrown when no providers can be loaded130if (shouldFail && ((x instanceof AttachNotSupportedException) ||131(x instanceof SecurityException))) {132System.out.println(" - exception thrown as expected.");133} else {134throw x;135}136}137}138}139}140141142