Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jdwp/Event/Composite/composite001.java
41161 views
/*1* Copyright (c) 2001, 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*/2223package nsk.jdwp.Event.Composite;2425import java.io.*;2627import nsk.share.*;28import nsk.share.jpda.*;29import nsk.share.jdwp.*;3031/**32* Test for JDWP command: Event.Composite.33*34* See composite001.README for description of test execution.35*36* This class represents debugger part of the test.37* Test is executed by invoking method runIt().38* JDWP command is tested in the method testCommand().39*40* @see #runIt()41* @see #waitForTestedEvent()42*/43public class composite001 {4445// exit status constants46static final int JCK_STATUS_BASE = 95;47static final int PASSED = 0;48static final int FAILED = 2;4950// package and classes names constants51static final String PACKAGE_NAME = "nsk.jdwp.Event.Composite";52static final String TEST_CLASS_NAME = PACKAGE_NAME + "." + "composite001";53static final String DEBUGEE_CLASS_NAME = TEST_CLASS_NAME + "a";5455// tested JDWP command constants56static final String JDWP_COMMAND_NAME = "Event.Composite";57static final int JDWP_COMMAND_ID = JDWP.Command.Event.Composite;58static final byte TESTED_EVENT_KIND = JDWP.EventKind.VM_START;59static final byte TESTED_EVENT_SUSPEND_POLICY = JDWP.SuspendPolicy.ALL;6061// usual scaffold objects62ArgumentHandler argumentHandler = null;63Log log = null;64Binder binder = null;65Debugee debugee = null;66Transport transport = null;67int waitTime = 0; // minutes68long timeout = 0; // milliseconds69boolean dead = false;70boolean success = true;7172// -------------------------------------------------------------------7374/**75* Start test from command line.76*/77public static void main(String argv[]) {78System.exit(run(argv,System.out) + JCK_STATUS_BASE);79}8081/**82* Start test from JCK-compilant environment.83*/84public static int run(String argv[], PrintStream out) {85return new composite001().runIt(argv, out);86}8788// -------------------------------------------------------------------8990/**91* Perform test execution.92*/93public int runIt(String argv[], PrintStream out) {9495// make log for debugger messages96argumentHandler = new ArgumentHandler(argv);97log = new Log(out, argumentHandler);98waitTime = argumentHandler.getWaitTime();99timeout = waitTime * 60 * 1000;100101// execute test and display results102try {103log.display("\n>>> Starting debugee \n");104105// launch debuggee106binder = new Binder(argumentHandler, log);107log.display("Launching debugee");108debugee = binder.bindToDebugee(DEBUGEE_CLASS_NAME);109transport = debugee.getTransport();110log.display(" ... debugee launched");111log.display("");112113// set timeout for debuggee responces114log.display("Setting timeout for debuggee responces: " + waitTime + " minute(s)");115transport.setReadTimeout(timeout);116log.display(" ... timeout set");117118// wait for debuggee started119log.display("Waiting for VM_INIT event");120debugee.waitForVMInit();121log.display(" ... VM_INIT event received");122123// query debugee for VM-dependent ID sizes124log.display("Querying for IDSizes");125debugee.queryForIDSizes();126log.display(" ... size of VM-dependent types adjusted");127128// test JDWP command129log.display("\n>>> Testing JDWP event \n");130testCommand();131132log.display("\n>>> Finishing debuggee \n");133134// resume debuggee135log.display("Resuming debuggee");136debugee.resume();137log.display(" ... debuggee resumed");138139// wait for debuggee exited140log.display("Waiting for VM_DEATH event");141debugee.waitForVMDeath();142dead = true;143log.display(" ... VM_DEATH event received");144145} catch (Failure e) {146log.complain("TEST FAILED: " + e.getMessage());147success = false;148} catch (Exception e) {149e.printStackTrace(out);150log.complain("Caught unexpected exception while running the test:\n\t" + e);151success = false;152} finally {153// quit debugee154log.display("\n>>> Finishing test \n");155quitDebugee();156}157158// check test results159if (!success) {160log.complain("TEST FAILED");161return FAILED;162}163164out.println("TEST PASSED");165return PASSED;166167}168169/**170* Test JDWP command.171*/172void testCommand() {173// create command packet and fill requred out data174log.display("Create command packet: " + JDWP_COMMAND_NAME);175CommandPacket command = new CommandPacket(JDWP_COMMAND_ID);176log.display(" suspendPolicy: " + TESTED_EVENT_SUSPEND_POLICY);177command.addByte(TESTED_EVENT_SUSPEND_POLICY);178log.display(" events: " + 1);179command.addInt(1);180log.display(" eventKind: " + TESTED_EVENT_KIND);181command.addByte(TESTED_EVENT_KIND);182log.display(" requestID: " + 0);183command.addInt(0);184log.display(" threadID: " + 0);185command.addObjectID(0);186command.setLength();187log.display(" ... command packet created");188189// send command packet to debugee190try {191log.display("Sending command packet:\n" + command);192transport.write(command);193log.display(" ... command packet sent");194} catch (IOException e) {195log.complain("Unable to send command packet:\n\t" + e);196success = false;197return;198}199200ReplyPacket reply = new ReplyPacket();201202// receive reply packet from debugee203try {204log.display("Waiting for reply packet");205transport.read(reply);206log.display(" ... reply packet received:\n" + reply);207} catch (IOException e) {208log.complain("Unable to read reply packet:\n\t" + e);209success = false;210return;211}212213// check reply packet header214log.display("Checking header of reply packet:");215216// extract and check length field217int length = reply.getLength();218log.display(" length: " + length);219if (length != reply.length()) {220log.complain("Unexpected value of length field in header of reply packet: "221+ length + "(actual: " + reply.length() + ")");222success = false;223}224225// extract and check id field226int id = reply.getPacketID();227log.display(" id: " + id);228if (id != command.getPacketID()) {229log.complain("Unexpected value of id field in header of reply packet: "230+ id + "(expected: " + command.getPacketID() + ")");231success = false;232}233234// extract and check flags field235byte flags = reply.getFlags();236log.display(" flags: " + flags);237if (flags != JDWP.Flag.REPLY_PACKET) {238log.complain("Unexpected value of flags field in header of reply packet: "239+ flags + "(expected: " + JDWP.Flag.REPLY_PACKET + ")");240success = false;241}242243// extract and check error field (should be not null!)244int error = reply.getErrorCode();245log.display(" error: " + error);246if (error == JDWP.Error.NONE) {247log.complain("Unexpected null value of error field in header of reply packet: "248+ error + "(expected: not " + JDWP.Error.NONE + ")");249success = false;250}251252log.display(" ... packet header is parsed");253254// start parsing reply packet data255log.display("Parsing reply packet data:");256reply.resetPosition();257258// no out data259log.display(" no out data");260261log.display(" ... packet data is parsed");262263// check for extra data in reply packet264if (!reply.isParsed()) {265log.complain("Extra trailing bytes found in reply packet at: "266+ reply.offsetString());267success = false;268}269}270271/**272* Disconnect debuggee and wait for it exited.273*/274void quitDebugee() {275if (debugee == null)276return;277278// disconnect debugee if not dead279if (!dead) {280try {281log.display("Disconnecting debuggee");282debugee.dispose();283log.display(" ... debuggee disconnected");284} catch (Failure e) {285log.display("Failed to finally disconnect debuggee:\n\t"286+ e.getMessage());287}288}289290// wait for debugee exited291log.display("Waiting for debuggee exit");292int code = debugee.waitFor();293log.display(" ... debuggee exited with exit code: " + code);294295// analize debugee exit status code296if (code != JCK_STATUS_BASE + PASSED) {297log.complain("Debuggee FAILED with exit code: " + code);298success = false;299}300}301302}303304305