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.util.Properties;
  27 import java.io.File;
  28 import jdk.testlibrary.OutputAnalyzer;
  29 import jdk.testlibrary.ProcessTools;
  30 import jdk.testlibrary.ProcessThread;
  31 
  32 /*
  33  * @test
  34  * @bug 6173612 6273707 6277253 6335921 6348630 6342019 6381757
  35  * @summary Basic unit tests for the VM attach mechanism.
  36  * @library /lib/testlibrary
  37  * @run build Application Shutdown
  38  * @run main PermissionTest
  39  *
  40  * Unit test for Attach API -
  41  * this checks that a SecurityException is thrown as expected.
  42  */
  43 public class PermissionTest {
  44 
  45     /*
  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      */
  53     public static void main(String args[]) throws Throwable {
  54         final String pidFile ="TestPermission.Application.pid";
  55         ProcessThread processThread = null;
  56         RunnerUtil.ProcessInfo info = null;
  57         try {
  58             processThread = RunnerUtil.startApplication(pidFile);
  59             info = RunnerUtil.readProcessInfo(pidFile);
  60             runTests(info.pid);
  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(info.shutdownPort, 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(int pid) throws Throwable {
  76         final String sep = File.separator;
  77 
  78         // Need to add jdk/lib/tools.jar to classpath.
  79         String classpath =
  80             System.getProperty("test.class.path", "") + File.pathSeparator +
  81             System.getProperty("test.jdk", ".") + sep + "lib" + sep + "tools.jar";
  82         String testSrc = System.getProperty("test.src", "") + sep;
  83 
  84         // Use a policy that will NOT allow attach. Test will verify exception.
  85         String[] args = {
  86             "-classpath",
  87             classpath,
  88             "-Djava.security.manager",
  89             String.format("-Djava.security.policy=%sjava.policy.deny", testSrc),
  90             "PermissionTest$TestMain",
  91             Integer.toString(pid),
  92             "true" };
  93         OutputAnalyzer output = ProcessTools.executeTestJvm(args);
  94         output.shouldHaveExitValue(0);
  95 
  96         // Use a policy that will allow attach.
  97         args = new String[] {
  98             "-classpath",
  99             classpath,
 100             "-Djava.security.manager",
 101             String.format("-Djava.security.policy=%sjava.policy.allow", testSrc),
 102             "PermissionTest$TestMain",
 103             Integer.toString(pid),
 104             "false" };
 105         output = ProcessTools.executeTestJvm(args);
 106         output.shouldHaveExitValue(0);
 107     }
 108 
 109     /**
 110      * This is the actual test code. It will attach to the Application and verify
 111      * that we get a SecurityException when that is expected.
 112      */
 113     public static class TestMain {
 114         public static void main(String args[]) throws Exception {
 115             SecurityManager sm = System.getSecurityManager();
 116             if (sm == null) {
 117                 throw new RuntimeException("Test configuration error - no security manager set");
 118             }
 119 
 120             String pid = args[0];
 121             boolean shouldFail = Boolean.parseBoolean(args[1]);
 122 
 123             try {
 124                 VirtualMachine.attach(pid).detach();
 125                 if (shouldFail) {
 126                     throw new RuntimeException("SecurityException should be thrown");
 127                 }
 128                 System.out.println(" - attached to target VM as expected.");
 129             } catch (Exception x) {
 130                 // AttachNotSupportedException thrown when no providers can be loaded
 131                 if (shouldFail && ((x instanceof AttachNotSupportedException) ||
 132                     (x instanceof SecurityException))) {
 133                     System.out.println(" - exception thrown as expected.");
 134                 } else {
 135                     throw x;
 136                 }
 137             }
 138         }
 139     }
 140 }