1 /*
   2  * Copyright (c) 2013, 2018, 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 java.lang.reflect.Method;
  25 import java.lang.reflect.Modifier;
  26 import java.util.ArrayList;
  27 import java.util.List;
  28 import java.util.concurrent.atomic.AtomicReference;
  29 
  30 import jdk.test.lib.process.ProcessTools;
  31 import jdk.testlibrary.Utils;
  32 
  33 /**
  34  * @test
  35  * @bug 5016507 6173612 6319776 6342019 6484550 8004926
  36  * @summary Start a managed VM and test that a management tool can connect
  37  *          without connection or username/password details.
  38  *          TestManager will attempt a connection to the address obtained from
  39  *          both agent properties and jvmstat buffer.
  40  *
  41  * @library /lib/testlibrary
  42  * @library /test/lib
  43  * @modules java.management
  44  *          jdk.attach
  45  *          jdk.management.agent/jdk.internal.agent
  46  *
  47  * @build jdk.testlibrary.* TestManager TestApplication
  48  * @run main/othervm/timeout=300 LocalManagementTest
  49  */
  50 public class LocalManagementTest {
  51     private static final String TEST_CLASSPATH = System.getProperty("test.class.path");
  52 
  53     public static void main(String[] args) throws Exception {
  54         int failures = 0;
  55         for(Method m : LocalManagementTest.class.getDeclaredMethods()) {
  56             if (Modifier.isStatic(m.getModifiers()) &&
  57                 m.getName().startsWith("test")) {
  58                 m.setAccessible(true);
  59                 try {
  60                     System.out.println(m.getName());
  61                     System.out.println("==========");
  62                     Boolean rslt = (Boolean)m.invoke(null);
  63                     if (!rslt) {
  64                         System.err.println(m.getName() + " failed");
  65                         failures++;
  66                     }
  67                 } catch (Exception e) {
  68                     e.printStackTrace();
  69                     failures++;
  70                 }
  71             }
  72         }
  73         if (failures > 0) {
  74             throw new Error("Test failed");
  75         }
  76     }
  77 
  78     @SuppressWarnings("unused")
  79     private static boolean test1() throws Exception {
  80         return doTest("1", "-Dcom.sun.management.jmxremote");
  81     }
  82 
  83     /**
  84      * no args (blank) - manager should attach and start agent
  85      */
  86     @SuppressWarnings("unused")
  87     private static boolean test3() throws Exception {
  88         return doTest("3", null);
  89     }
  90 
  91     /**
  92      * use DNS-only name service
  93      */
  94     @SuppressWarnings("unused")
  95     private static boolean test5() throws Exception {
  96         return doTest("5", "-Dsun.net.spi.namservice.provider.1=\"dns,sun\"");
  97     }
  98 
  99     private static boolean doTest(String testId, String arg) throws Exception {
 100         List<String> args = new ArrayList<>();
 101         args.add("-XX:+UsePerfData");
 102         args.addAll(Utils.getVmOptions());
 103         args.add("-cp");
 104         args.add(TEST_CLASSPATH);
 105 
 106         if (arg != null) {
 107             args.add(arg);
 108         }
 109         args.add("TestApplication");
 110         ProcessBuilder server = ProcessTools.createJavaProcessBuilder(
 111             args.toArray(new String[args.size()])
 112         );
 113 
 114         Process serverPrc = null, clientPrc = null;
 115         try {
 116             final AtomicReference<String> port = new AtomicReference<>();
 117 
 118             serverPrc = ProcessTools.startProcess(
 119                 "TestApplication(" + testId + ")",
 120                 server,
 121                 (String line) -> {
 122                     if (line.startsWith("port:")) {
 123                          port.set(line.split("\\:")[1]);
 124                     } else if (line.startsWith("waiting")) {
 125                         return true;
 126                     }
 127                     return false;
 128                 }
 129             );
 130 
 131             System.out.println("Attaching test manager:");
 132             System.out.println("=========================");
 133             System.out.println("  PID           : " + serverPrc.pid());
 134             System.out.println("  shutdown port : " + port.get());
 135 
 136             ProcessBuilder client = ProcessTools.createJavaProcessBuilder(
 137                 "-cp",
 138                 TEST_CLASSPATH,
 139                 "--add-exports", "jdk.management.agent/jdk.internal.agent=ALL-UNNAMED",
 140                 "TestManager",
 141                 String.valueOf(serverPrc.pid()),
 142                 port.get(),
 143                 "true"
 144             );
 145 
 146             clientPrc = ProcessTools.startProcess(
 147                 "TestManager",
 148                 client,
 149                 (String line) -> line.startsWith("Starting TestManager for PID")
 150             );
 151 
 152             int clientExitCode = clientPrc.waitFor();
 153             int serverExitCode = serverPrc.waitFor();
 154             return clientExitCode == 0 && serverExitCode == 0;
 155         } finally {
 156             if (clientPrc != null) {
 157                 System.out.println("Stopping process " + clientPrc);
 158                 clientPrc.destroy();
 159                 clientPrc.waitFor();
 160             }
 161             if (serverPrc != null) {
 162                 System.out.println("Stopping process " + serverPrc);
 163                 serverPrc.destroy();
 164                 serverPrc.waitFor();
 165             }
 166         }
 167     }
 168 }