Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/com/sun/tools/attach/BasicTests.java
38855 views
/*1* Copyright (c) 2005, 2011, 2013 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 com.sun.tools.attach.*;24import java.net.ServerSocket;25import java.net.Socket;26import java.io.IOException;27import java.util.Properties;28import java.util.List;29import java.io.File;30import jdk.testlibrary.OutputAnalyzer;31import jdk.testlibrary.JDKToolLauncher;32import jdk.testlibrary.ProcessTools;33import jdk.testlibrary.ProcessThread;3435/*36* @test37* @bug 6173612 6273707 6277253 6335921 6348630 6342019 638175738* @summary Basic unit tests for the VM attach mechanism.39* @library /lib/testlibrary40* @build jdk.testlibrary.* Agent BadAgent RedefineAgent Application Shutdown RedefineDummy RunnerUtil41* @run main BasicTests42*43* This test will perform a number of basic attach tests.44*/45public class BasicTests {4647/*48* The actual test is in the nested class TestMain.49* The responsibility of this class is to:50* 1. Build all needed jars.51* 2. Start the Application class in a separate process.52* 3. Find the pid and shutdown port of the running Application.53* 4. Launches the tests in nested class TestMain that will attach to the Application.54* 5. Shut down the Application.55*/56public static void main(String args[]) throws Throwable {57final String pidFile = "TestsBasic.Application.pid";58ProcessThread processThread = null;59RunnerUtil.ProcessInfo info = null;60try {61buildJars();62processThread = RunnerUtil.startApplication(pidFile);63info = RunnerUtil.readProcessInfo(pidFile);64runTests(info.pid);65} catch (Throwable t) {66System.out.println("TestBasic got unexpected exception: " + t);67t.printStackTrace();68throw t;69} finally {70// Make sure the Application process is stopped.71RunnerUtil.stopApplication(info.shutdownPort, processThread);72}73}7475/**76* Runs the actual tests in nested class TestMain.77* The reason for running the tests in a separate process78* is that we need to modify the class path.79*/80private static void runTests(int pid) throws Throwable {81final String sep = File.separator;8283// Need to add jdk/lib/tools.jar to classpath.84String classpath =85System.getProperty("test.class.path", "") + File.pathSeparator +86System.getProperty("test.jdk", ".") + sep + "lib" + sep + "tools.jar";87String testClassDir = System.getProperty("test.classes", "") + sep;8889// Argumenta : -classpath cp BasicTests$TestMain pid agent badagent redefineagent90String[] args = {91"-classpath",92classpath,93"BasicTests$TestMain",94Integer.toString(pid),95testClassDir + "Agent.jar",96testClassDir + "BadAgent.jar",97testClassDir + "RedefineAgent.jar" };98OutputAnalyzer output = ProcessTools.executeTestJvm(args);99output.shouldHaveExitValue(0);100}101102/**103* Will build all jars needed by the tests.104*/105private static void buildJars() throws Throwable {106String[] jars = {"Agent", "BadAgent", "RedefineAgent", "Application" };107for (String jar : jars) {108buildJar(jar);109}110}111112/**113* Will build a jar with the given name.114* Class file and manifest must already exist.115* @param jarName Name of the jar.116*/117private static void buildJar(String jarName) throws Throwable {118String testClasses = System.getProperty("test.classes", "?");119String testSrc = System.getProperty("test.src", "?");120String jar = String.format("%s/%s.jar", testClasses, jarName);121String manifest = String.format("%s/%s.mf", testSrc, jarName.toLowerCase());122String clazz = String.format("%s.class", jarName);123124// Arguments to the jar command has this format:125// "-cfm TESTCLASSES/Agent.jar TESTSRC/agent.mf -C TESTCLASSES Agent.class"126RunnerUtil.createJar("-cfm", jar, manifest, "-C", testClasses, clazz);127}128129/**130* This is the actual test. It will attach to the running Application131* and perform a number of basic attach tests.132*/133public static class TestMain {134public static void main(String args[]) throws Exception {135String pid = args[0];136String agent = args[1];137String badagent = args[2];138String redefineagent = args[3];139140System.out.println(" - Attaching to application ...");141VirtualMachine vm = VirtualMachine.attach(pid);142143// Test 1 - read the system properties from the target VM and144// check that property is set145System.out.println(" - Test: system properties in target VM");146Properties props = vm.getSystemProperties();147String value = props.getProperty("attach.test");148if (value == null || !value.equals("true")) {149throw new RuntimeException("attach.test property not set");150}151System.out.println(" - attach.test property set as expected");152153// Test 1a - read the agent properties from the target VM.154// By default, the agent property contains "sun.java.command",155// "sun.jvm.flags", and "sun.jvm.args".156// Just sanity check - make sure not empty.157System.out.println(" - Test: agent properties in target VM");158props = vm.getAgentProperties();159if (props == null || props.size() == 0) {160throw new RuntimeException("Agent properties is empty");161}162System.out.println(" - agent properties non-empty as expected");163164// Test 2 - attempt to load an agent that does not exist165System.out.println(" - Test: Load an agent that does not exist");166try {167vm.loadAgent("SilverBullet.jar");168} catch (AgentLoadException x) {169System.out.println(" - AgentLoadException thrown as expected!");170}171172// Test 3 - load an "bad" agent (agentmain throws an exception)173System.out.println(" - Test: Load a bad agent");174System.out.println("INFO: This test will cause error messages "175+ "to appear in the application log about SilverBullet.jar "176+ "not being found and an agent failing to start.");177try {178vm.loadAgent(badagent);179throw new RuntimeException(180"AgentInitializationException not thrown as expected!");181} catch (AgentInitializationException x) {182System.out.println(183" - AgentInitializationException thrown as expected!");184}185186// Test 4 - detach from the VM and attempt a load (should throw IOE)187System.out.println(" - Test: Detach from VM");188System.out.println("INFO: This test will cause error messages "189+ "to appear in the application log about a BadAgent including "190+ "a RuntimeException and an InvocationTargetException.");191vm.detach();192try {193vm.loadAgent(agent);194throw new RuntimeException("loadAgent did not throw an exception!!");195} catch (IOException ioe) {196System.out.println(" - IOException as expected");197}198199// Test 5 - functional "end-to-end" test.200// Create a listener socket. Load Agent.jar into the target VM passing201// it the port number of our listener. When agent loads it should connect202// back to the tool.203204System.out.println(" - Re-attaching to application ...");205vm = VirtualMachine.attach(pid);206207System.out.println(" - Test: End-to-end connection with agent");208209ServerSocket ss = new ServerSocket(0);210int port = ss.getLocalPort();211212System.out.println(" - Loading Agent.jar into target VM ...");213vm.loadAgent(agent, Integer.toString(port));214215System.out.println(" - Waiting for agent to connect back to tool ...");216Socket s = ss.accept();217System.out.println(" - Connected to agent.");218219// Test 5b - functional "end-to-end" test.220// Now with an agent that does redefine.221222System.out.println(" - Re-attaching to application ...");223vm = VirtualMachine.attach(pid);224225System.out.println(" - Test: End-to-end connection with RedefineAgent");226227ServerSocket ss2 = new ServerSocket(0);228int port2 = ss2.getLocalPort();229230System.out.println(" - Loading RedefineAgent.jar into target VM ...");231vm.loadAgent(redefineagent, Integer.toString(port2));232233System.out.println(" - Waiting for RedefineAgent to connect back to tool ...");234Socket s2 = ss2.accept();235System.out.println(" - Connected to RedefineAgent.");236237// Test 6 - list method should list the target VM238System.out.println(" - Test: VirtualMachine.list");239List<VirtualMachineDescriptor> l = VirtualMachine.list();240boolean found = false;241for (VirtualMachineDescriptor vmd: l) {242if (vmd.id().equals(pid)) {243found = true;244break;245}246}247if (found) {248System.out.println(" - " + pid + " found.");249} else {250throw new RuntimeException(pid + " not found in VM list");251}252253// test 7 - basic hashCode/equals tests254System.out.println(" - Test: hashCode/equals");255256VirtualMachine vm1 = VirtualMachine.attach(pid);257VirtualMachine vm2 = VirtualMachine.attach(pid);258if (!vm1.equals(vm2)) {259throw new RuntimeException("virtual machines are not equal");260}261if (vm.hashCode() != vm.hashCode()) {262throw new RuntimeException("virtual machine hashCodes not equal");263}264System.out.println(" - hashCode/equals okay");265266// ---267System.out.println(" - Cleaning up...");268s.close();269ss.close();270s2.close();271ss2.close();272}273}274}275276277