1 /*
   2  * Copyright (c) 2014 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.*;
  25 
  26 import java.nio.file.Files;
  27 import java.nio.file.Path;
  28 import java.util.Properties;
  29 import java.util.List;
  30 import java.io.File;
  31 
  32 import jdk.testlibrary.OutputAnalyzer;
  33 import jdk.testlibrary.ProcessTools;
  34 import jdk.testlibrary.ProcessThread;
  35 
  36 /*
  37  * @test
  38  * @bug 8033104
  39  * @summary Test to make sure attach and jvmstat works correctly when java.io.tmpdir is set
  40  * @library /lib/testlibrary
  41  * @run build jdk.testlibrary.* Application Shutdown RunnerUtil
  42  * @run main TempDirTest
  43  */
  44 
  45 public class TempDirTest {
  46 
  47     public static void main(String args[]) throws Throwable {
  48 
  49         Path clientTmpDir = Files.createTempDirectory("TempDirTest-client");
  50         clientTmpDir.toFile().deleteOnExit();
  51         Path targetTmpDir = Files.createTempDirectory("TempDirTest-target");
  52         targetTmpDir.toFile().deleteOnExit();
  53 
  54         // run the test with all possible combinations of setting java.io.tmpdir
  55         runExperiment(null, null);
  56         runExperiment(clientTmpDir, null);
  57         runExperiment(clientTmpDir, targetTmpDir);
  58         runExperiment(null, targetTmpDir);
  59 
  60     }
  61 
  62     private static int counter = 0;
  63 
  64     /*
  65      * The actual test is in the nested class TestMain.
  66      * The responsibility of this class is to:
  67      * 1. Start the Application class in a separate process.
  68      * 2. Find the pid and shutdown port of the running Application.
  69      * 3. Launches the tests in nested class TestMain that will attach to the Application.
  70      * 4. Shut down the Application.
  71      */
  72     public static void runExperiment(Path clientTmpDir, Path targetTmpDir) throws Throwable {
  73 
  74         System.out.print("### Running tests with overridden tmpdir for");
  75         System.out.print(" client: " + (clientTmpDir == null ? "no" : "yes"));
  76         System.out.print(" target: " + (targetTmpDir == null ? "no" : "yes"));
  77         System.out.println(" ###");
  78 
  79         final String pidFile = "TempDirTest.Application.pid-" + counter++;
  80         ProcessThread processThread = null;
  81         RunnerUtil.ProcessInfo info = null;
  82         try {
  83             String[] tmpDirArg = null;
  84             if (targetTmpDir != null) {
  85                 tmpDirArg = new String[] {"-Djava.io.tmpdir=" + targetTmpDir};
  86             }
  87             processThread = RunnerUtil.startApplication(pidFile, tmpDirArg);
  88             info = RunnerUtil.readProcessInfo(pidFile);
  89             launchTests(info.pid, clientTmpDir);
  90         } catch (Throwable t) {
  91             System.out.println("TempDirTest got unexpected exception: " + t);
  92             t.printStackTrace();
  93             throw t;
  94         } finally {
  95             // Make sure the Application process is stopped.
  96             RunnerUtil.stopApplication(info.shutdownPort, processThread);
  97         }
  98     }
  99 
 100     /**
 101      * Runs the actual tests in nested class TestMain.
 102      * The reason for running the tests in a separate process
 103      * is that we need to modify the class path and
 104      * the -Djava.io.tmpdir property.
 105      */
 106     private static void launchTests(int pid, Path clientTmpDir) throws Throwable {
 107         final String sep = File.separator;
 108 
 109         // Need to add jdk/lib/tools.jar to classpath.
 110         String classpath =
 111             System.getProperty("test.class.path", "") + File.pathSeparator +
 112             System.getProperty("test.jdk", ".") + sep + "lib" + sep + "tools.jar";
 113 
 114         String[] tmpDirArg = null;
 115         if (clientTmpDir != null) {
 116             tmpDirArg = new String [] {"-Djava.io.tmpdir=" + clientTmpDir};
 117         }
 118 
 119         // Arguments : [-Djava.io.tmpdir=] -classpath cp TempDirTest$TestMain pid
 120         String[] args = RunnerUtil.concat(
 121                 tmpDirArg,
 122                 new String[] {
 123                     "-classpath",
 124                     classpath,
 125                     "TempDirTest$TestMain",
 126                     Integer.toString(pid) });
 127         OutputAnalyzer output = ProcessTools.executeTestJvm(args);
 128         output.shouldHaveExitValue(0);
 129     }
 130 
 131     /**
 132      * This is the actual test. It will attach to the running Application
 133      * and perform a number of basic attach tests.
 134      */
 135     public static class TestMain {
 136         public static void main(String args[]) throws Exception {
 137             String pid = args[0];
 138 
 139             // Test 1 - list method should list the target VM
 140             System.out.println(" - Test: VirtualMachine.list");
 141             List<VirtualMachineDescriptor> l = VirtualMachine.list();
 142             boolean found = false;
 143             for (VirtualMachineDescriptor vmd: l) {
 144                 if (vmd.id().equals(pid)) {
 145                     found = true;
 146                     break;
 147                 }
 148             }
 149             if (found) {
 150                 System.out.println(" - " + pid + " found.");
 151             } else {
 152                 throw new RuntimeException(pid + " not found in VM list");
 153             }
 154 
 155             // Test 2 - try to attach and verify connection
 156 
 157             System.out.println(" - Attaching to application ...");
 158             VirtualMachine vm = VirtualMachine.attach(pid);
 159 
 160             System.out.println(" - Test: system properties in target VM");
 161             Properties props = vm.getSystemProperties();
 162             String value = props.getProperty("attach.test");
 163             if (value == null || !value.equals("true")) {
 164                 throw new RuntimeException("attach.test property not set");
 165             }
 166             System.out.println(" - attach.test property set as expected");
 167         }
 168     }
 169 }