Path: blob/master/test/jdk/com/sun/tools/attach/BasicTests.java
41153 views
/*1* Copyright (c) 2005, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.io.File;24import java.io.IOException;25import java.net.ServerSocket;26import java.net.Socket;27import java.util.List;28import java.util.Properties;2930import jdk.test.lib.thread.ProcessThread;31import jdk.test.lib.process.OutputAnalyzer;32import jdk.test.lib.process.ProcessTools;3334import com.sun.tools.attach.AgentInitializationException;35import com.sun.tools.attach.AgentLoadException;36import com.sun.tools.attach.VirtualMachine;37import com.sun.tools.attach.VirtualMachineDescriptor;3839/*40* @test41* @bug 6173612 6273707 6277253 6335921 6348630 6342019 638175742* @key intermittent43* @summary Basic unit tests for the VM attach mechanism. This test will perform44* a number of basic attach tests.45*46* @library /test/lib47* @modules java.instrument48* jdk.attach49* jdk.jartool/sun.tools.jar50*51* @run build Agent BadAgent RedefineAgent Application RedefineDummy RunnerUtil52* @run main BasicTests53*/54public class BasicTests {5556/*57* The actual test is in the nested class TestMain.58* The responsibility of this class is to:59* 1. Build all needed jars.60* 2. Start the Application class in a separate process.61* 3. Find the pid and shutdown port of the running Application.62* 4. Launches the tests in nested class TestMain that will attach to the Application.63* 5. Shut down the Application.64*/65public static void main(String args[]) throws Throwable {66ProcessThread processThread = null;67try {68buildJars();69processThread = RunnerUtil.startApplication();70runTests(processThread.getPid());71} catch (Throwable t) {72System.out.println("TestBasic got unexpected exception: " + t);73t.printStackTrace();74throw t;75} finally {76// Make sure the Application process is stopped.77RunnerUtil.stopApplication(processThread);78}79}8081/**82* Runs the actual tests in nested class TestMain.83* The reason for running the tests in a separate process84* is that we need to modify the class path.85*/86private static void runTests(long pid) throws Throwable {87final String sep = File.separator;8889// Need to add jdk/lib/tools.jar to classpath.90String classpath =91System.getProperty("test.class.path", "");92String testClassDir = System.getProperty("test.classes", "") + sep;9394// Argumenta : -classpath cp BasicTests$TestMain pid agent badagent redefineagent95String[] args = {96"-classpath",97classpath,98"BasicTests$TestMain",99Long.toString(pid),100testClassDir + "Agent.jar",101testClassDir + "BadAgent.jar",102testClassDir + "RedefineAgent.jar" };103OutputAnalyzer output = ProcessTools.executeTestJvm(args);104output.shouldHaveExitValue(0);105}106107/**108* Will build all jars needed by the tests.109*/110private static void buildJars() throws Throwable {111String[] jars = {"Agent", "BadAgent", "RedefineAgent", "Application" };112for (String jar : jars) {113buildJar(jar);114}115}116117/**118* Will build a jar with the given name.119* Class file and manifest must already exist.120* @param jarName Name of the jar.121*/122private static void buildJar(String jarName) throws Throwable {123String testClasses = System.getProperty("test.classes", "?");124String testSrc = System.getProperty("test.src", "?");125String jar = String.format("%s/%s.jar", testClasses, jarName);126String manifest = String.format("%s/%s.mf", testSrc, jarName.toLowerCase());127String clazz = String.format("%s.class", jarName);128129// Arguments to the jar command has this format:130// "-cfm TESTCLASSES/Agent.jar TESTSRC/agent.mf -C TESTCLASSES Agent.class"131RunnerUtil.createJar("-cfm", jar, manifest, "-C", testClasses, clazz);132}133134/**135* This is the actual test. It will attach to the running Application136* and perform a number of basic attach tests.137*/138public static class TestMain {139public static void main(String args[]) throws Exception {140String pid = args[0];141String agent = args[1];142String badagent = args[2];143String redefineagent = args[3];144145System.out.println(" - Attaching to application ...");146VirtualMachine vm = VirtualMachine.attach(pid);147148// Test 1 - read the system properties from the target VM and149// check that property is set150System.out.println(" - Test: system properties in target VM");151Properties props = vm.getSystemProperties();152String value = props.getProperty("attach.test");153if (value == null || !value.equals("true")) {154throw new RuntimeException("attach.test property not set");155}156System.out.println(" - attach.test property set as expected");157158// Test 1a - read the agent properties from the target VM.159// By default, the agent property contains "sun.java.command",160// "sun.jvm.flags", and "sun.jvm.args".161// Just sanity check - make sure not empty.162System.out.println(" - Test: agent properties in target VM");163props = vm.getAgentProperties();164if (props == null || props.size() == 0) {165throw new RuntimeException("Agent properties is empty");166}167System.out.println(" - agent properties non-empty as expected");168169// Test 2 - attempt to load an agent that does not exist170System.out.println(" - Test: Load an agent that does not exist");171try {172vm.loadAgent("SilverBullet.jar");173} catch (AgentLoadException x) {174System.out.println(" - AgentLoadException thrown as expected!");175}176177// Test 3 - load an "bad" agent (agentmain throws an exception)178System.out.println(" - Test: Load a bad agent");179System.out.println("INFO: This test will cause error messages "180+ "to appear in the application log about SilverBullet.jar "181+ "not being found and an agent failing to start.");182try {183vm.loadAgent(badagent);184throw new RuntimeException(185"AgentInitializationException not thrown as expected!");186} catch (AgentInitializationException x) {187System.out.println(188" - AgentInitializationException thrown as expected!");189}190191// Test 4 - detach from the VM and attempt a load (should throw IOE)192System.out.println(" - Test: Detach from VM");193System.out.println("INFO: This test will cause error messages "194+ "to appear in the application log about a BadAgent including "195+ "a RuntimeException and an InvocationTargetException.");196vm.detach();197try {198vm.loadAgent(agent);199throw new RuntimeException("loadAgent did not throw an exception!!");200} catch (IOException ioe) {201System.out.println(" - IOException as expected");202}203204// Test 5 - functional "end-to-end" test.205// Create a listener socket. Load Agent.jar into the target VM passing206// it the port number of our listener. When agent loads it should connect207// back to the tool.208209System.out.println(" - Re-attaching to application ...");210vm = VirtualMachine.attach(pid);211212System.out.println(" - Test: End-to-end connection with agent");213214ServerSocket ss = new ServerSocket(0);215int port = ss.getLocalPort();216217System.out.println(" - Loading Agent.jar into target VM ...");218vm.loadAgent(agent, Integer.toString(port));219220System.out.println(" - Waiting for agent to connect back to tool ...");221Socket s = ss.accept();222System.out.println(" - Connected to agent.");223224// Test 5b - functional "end-to-end" test.225// Now with an agent that does redefine.226227System.out.println(" - Re-attaching to application ...");228vm = VirtualMachine.attach(pid);229230System.out.println(" - Test: End-to-end connection with RedefineAgent");231232ServerSocket ss2 = new ServerSocket(0);233int port2 = ss2.getLocalPort();234235System.out.println(" - Loading RedefineAgent.jar into target VM ...");236vm.loadAgent(redefineagent, Integer.toString(port2));237238System.out.println(" - Waiting for RedefineAgent to connect back to tool ...");239Socket s2 = ss2.accept();240System.out.println(" - Connected to RedefineAgent.");241242// Test 6 - list method should list the target VM243System.out.println(" - Test: VirtualMachine.list");244List<VirtualMachineDescriptor> l = VirtualMachine.list();245boolean found = false;246for (VirtualMachineDescriptor vmd: l) {247if (vmd.id().equals(pid)) {248found = true;249break;250}251}252if (found) {253System.out.println(" - " + pid + " found.");254} else {255throw new RuntimeException(pid + " not found in VM list");256}257258// test 7 - basic hashCode/equals tests259System.out.println(" - Test: hashCode/equals");260261VirtualMachine vm1 = VirtualMachine.attach(pid);262VirtualMachine vm2 = VirtualMachine.attach(pid);263if (!vm1.equals(vm2)) {264throw new RuntimeException("virtual machines are not equal");265}266if (vm.hashCode() != vm.hashCode()) {267throw new RuntimeException("virtual machine hashCodes not equal");268}269System.out.println(" - hashCode/equals okay");270271// ---272System.out.println(" - Cleaning up...");273s.close();274ss.close();275s2.close();276ss2.close();277}278}279}280281282