1 /*
   2  * Copyright (c) 2014, 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.AttachOperationFailedException;
  25 import com.sun.tools.attach.VirtualMachine;
  26 
  27 import java.io.File;
  28 import java.io.FileWriter;
  29 import java.util.Properties;
  30 import java.util.HashMap;
  31 
  32 import javax.management.remote.JMXServiceURL;
  33 import javax.management.remote.JMXConnector;
  34 import javax.management.remote.JMXConnectorFactory;
  35 
  36 import jdk.testlibrary.ProcessThread;
  37 import jdk.testlibrary.Utils;
  38 
  39 /*
  40  * @test
  41  * @summary Test for VirtualMachine.startManagementAgent and VirtualMachine.startLocalManagementAgent
  42  * @library /lib/testlibrary
  43  * @modules java.management
  44  *          jdk.attach
  45  *          jdk.jartool/sun.tools.jar
  46  * @run build Application SimpleProvider jdk.testlibrary.*
  47  * @run main/timeout=300 StartManagementAgent
  48  */
  49 
  50 /*
  51  * This test is not meant to test all possible configuration parameters to
  52  * the JMX agent, there are other tests for that. This test makes sure it is
  53  * possible to start the agent via attach.
  54  */
  55 public class StartManagementAgent {
  56     public static void main(String[] args) throws Throwable {
  57         ProcessThread processThread = null;
  58         try {
  59             System.out.println("Starting test application");
  60             processThread = RunnerUtil.startApplication();
  61             System.out.println("Application started");
  62             runTests(processThread.getPid());
  63         } catch (Throwable t) {
  64             System.out.println("StartManagementAgent got unexpected exception: " + t);
  65             t.printStackTrace();
  66             throw t;
  67         } finally {
  68             // Make sure the Application process is stopped.
  69             RunnerUtil.stopApplication(processThread);
  70         }
  71     }
  72 
  73     private static void basicTests(VirtualMachine vm) throws Exception {
  74 
  75         // Try calling with null argument
  76         boolean exception = false;
  77         try {
  78             System.out.println("Starting management agent with null");
  79             vm.startManagementAgent(null);
  80         } catch (NullPointerException e) {
  81             exception = true;
  82         }
  83         if (!exception) {
  84             throw new Exception("startManagementAgent(null) should throw NPE");
  85         }
  86 
  87         // Try calling with a property value with a space in it
  88         Properties p = new Properties();
  89         File f = new File("file with space");
  90         try (FileWriter fw = new FileWriter(f)) {
  91             fw.write("com.sun.management.jmxremote.port=apa");
  92         }
  93         p.put("com.sun.management.config.file", f.getAbsolutePath());
  94         try {
  95             System.out.println("Starting management agent with bogus port");
  96             vm.startManagementAgent(p);
  97         } catch(AttachOperationFailedException ex) {
  98             // We expect parsing of "apa" above to fail, but if the file path
  99             // can't be read we get a different exception message
 100             if (!ex.getMessage().contains("Invalid com.sun.management.jmxremote.port number")) {
 101                 throw ex;
 102             }
 103             ex.printStackTrace(System.err);
 104         } catch (Throwable t) {
 105             t.printStackTrace(System.err);
 106             throw t;
 107         }
 108     }
 109 
 110     private static final String LOCAL_CONNECTOR_ADDRESS_PROP =
 111         "com.sun.management.jmxremote.localConnectorAddress";
 112 
 113     private static final int MAX_RETRIES = 10;
 114 
 115     public static void runTests(long pid) throws Exception {
 116         VirtualMachine vm = VirtualMachine.attach(""+pid);
 117         try {
 118 
 119             basicTests(vm);
 120 
 121             testLocalAgent(vm);
 122 
 123             // we retry the remote case several times in case the error
 124             // was caused by a port conflict
 125             int i = 0;
 126             boolean success = false;
 127             do {
 128                 try {
 129                     System.err.println("Trying remote agent. Try #" + i);
 130                     testRemoteAgent(vm);
 131                     System.err.println("Successfully connected to remote agent");
 132                     success = true;
 133                 } catch(Exception ex) {
 134                     System.err.println("testRemoteAgent failed with exception:");
 135                     ex.printStackTrace();
 136                     System.err.println("Retrying.");
 137                 }
 138                 i++;
 139             } while(!success && i < MAX_RETRIES);
 140             if (!success) {
 141                 throw new Exception("testRemoteAgent failed after " + MAX_RETRIES + " tries");
 142             }
 143         } finally {
 144             System.err.println("Detaching from VM ...");
 145             vm.detach();
 146             System.err.println("Detached");
 147         }
 148     }
 149 
 150     public static void testLocalAgent(VirtualMachine vm) throws Exception {
 151         System.out.println("Getting VM properties");
 152         Properties agentProps = vm.getAgentProperties();
 153         String address = (String) agentProps.get(LOCAL_CONNECTOR_ADDRESS_PROP);
 154         if (address != null) {
 155             throw new Exception("Local management agent already started");
 156         }
 157 
 158         System.out.println("Starting local agent");
 159 
 160         String result = vm.startLocalManagementAgent();
 161 
 162         System.out.println("Agent started");
 163 
 164         // try to parse the return value as a JMXServiceURL
 165         new JMXServiceURL(result);
 166 
 167         agentProps = vm.getAgentProperties();
 168         address = (String) agentProps.get(LOCAL_CONNECTOR_ADDRESS_PROP);
 169         if (address == null) {
 170             throw new Exception("Local management agent could not be started");
 171         }
 172     }
 173 
 174     public static void testRemoteAgent(VirtualMachine vm) throws Exception {
 175         int port = Utils.getFreePort();
 176 
 177         // try to connect - should fail
 178         tryConnect(port, false);
 179 
 180         // start agent
 181         System.out.println("Starting agent on port: " + port);
 182         Properties mgmtProps = new Properties();
 183         mgmtProps.put("com.sun.management.jmxremote.port", port);
 184         mgmtProps.put("com.sun.management.jmxremote.authenticate", "false");
 185         mgmtProps.put("com.sun.management.jmxremote.ssl", "false");
 186 
 187         System.err.println("Starting management agent ...");
 188         vm.startManagementAgent(mgmtProps);
 189         System.err.println("Started");
 190 
 191         // try to connect - should work
 192         tryConnect(port, true);
 193 
 194         // try to start again - should fail
 195         boolean exception = false;
 196         try {
 197             System.err.println("Starting management agent second time ...");
 198             vm.startManagementAgent(mgmtProps);
 199             System.err.println("Started");
 200         } catch(AttachOperationFailedException ex) {
 201             // expected
 202             System.err.println("Got expected exception: " + ex.getMessage());
 203             exception = true;
 204         }
 205         if (!exception) {
 206             throw new Exception("Expected the second call to vm.startManagementAgent() to fail");
 207         }
 208     }
 209 
 210     private static void tryConnect(int port, boolean shouldSucceed) throws Exception {
 211         String jmxUrlStr =
 212             String.format(
 213                 "service:jmx:rmi:///jndi/rmi://localhost:%d/jmxrmi",
 214                 port);
 215         JMXServiceURL url = new JMXServiceURL(jmxUrlStr);
 216         HashMap<String, ?> env = new HashMap<>();
 217 
 218         boolean succeeded;
 219         try {
 220             System.err.println("Trying to connect to " + jmxUrlStr);
 221             JMXConnector c = JMXConnectorFactory.connect(url, env);
 222             System.err.println("Connected, getting MBeanServerConnection");
 223             c.getMBeanServerConnection();
 224             System.err.println("Success");
 225             succeeded = true;
 226         } catch(Exception ex) {
 227             ex.printStackTrace(System.err);
 228             succeeded = false;
 229         }
 230         if (succeeded && !shouldSucceed) {
 231             throw new Exception("Could connect to agent, but should not have been possible");
 232         }
 233         if (!succeeded && shouldSucceed) {
 234             throw new Exception("Could not connect to agent");
 235         }
 236     }
 237 }