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