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