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