Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jdwp/VirtualMachine/SetDefaultStratum/setdefstrat001.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.VirtualMachine.SetDefaultStratum;2425import java.io.*;2627import nsk.share.*;28import nsk.share.jpda.*;29import nsk.share.jdwp.*;3031/**32* Test for JDWP command: VirtualMachine.SetDefaultStratum.33*34* See setdefstrat001.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 #testCommand()42*/43public class setdefstrat001 {4445// exit status constants46static final int JCK_STATUS_BASE = 95;47static final int PASSED = 0;48static final int FAILED = 2;4950// communication signals constants51static final String READY = "ready";52static final String QUIT = "quit";5354// package and classes names constants55static final String PACKAGE_NAME = "nsk.jdwp.VirtualMachine.SetDefaultStratum";56static final String TEST_CLASS_NAME = PACKAGE_NAME + "." + "setdefstrat001";57static final String DEBUGEE_CLASS_NAME = TEST_CLASS_NAME + "a";5859// VM capability constatnts60static final int VM_CAPABILITY_NUMBER = JDWP.Capability.CAN_SET_DEFAULT_STRATUM;61static final String VM_CAPABILITY_NAME = "canSetDefaultStratum";6263// tested JDWP command constants64static final String JDWP_COMMAND_NAME = "VirtualMachine.SetDefaultStratum";65static final int JDWP_COMMAND_ID = JDWP.Command.VirtualMachine.SetDefaultStratum;6667// new value for default startum68static final String NEW_DEFAULT_STRATUM = "test";6970// usual scaffold objects71ArgumentHandler argumentHandler = null;72Log log = null;73Binder binder = null;74Debugee debugee = null;75Transport transport = null;76IOPipe pipe = null;7778// test passed or not79boolean success = true;8081// -------------------------------------------------------------------8283/**84* Start test from command line.85*/86public static void main (String argv[]) {87System.exit(run(argv,System.out) + JCK_STATUS_BASE);88}8990/**91* Start JCK-compilant test.92*/93public static int run(String argv[], PrintStream out) {94return new setdefstrat001().runIt(argv, out);95}9697// -------------------------------------------------------------------9899/**100* Perform test execution.101*/102public int runIt(String argv[], PrintStream out) {103104// make log for debugger messages105argumentHandler = new ArgumentHandler(argv);106log = new Log(out, argumentHandler);107108// execute test and display results109try {110log.display("\n>>> Preparing debugee for testing \n");111112// launch debuggee113binder = new Binder(argumentHandler, log);114log.display("Launching debugee");115debugee = binder.bindToDebugee(DEBUGEE_CLASS_NAME);116transport = debugee.getTransport();117pipe = debugee.createIOPipe();118119// make debuggee ready for testing120prepareDebugee();121122// work with prepared debuggee123try {124log.display("\n>>> Checking VM capability \n");125126// check for VM capability127log.display("Checking VM capability: " + VM_CAPABILITY_NAME);128if (!debugee.getNewCapability(VM_CAPABILITY_NUMBER, VM_CAPABILITY_NAME)) {129out.println("TEST PASSED: unsupported VM capability: "130+ VM_CAPABILITY_NAME);131return PASSED;132}133134// perform testing JDWP command135log.display("\n>>> Testing JDWP command \n");136testCommand();137138} finally {139// quit debugee140log.display("\n>>> Finishing test \n");141quitDebugee();142}143144} catch (Failure e) {145log.complain("TEST FAILED: " + e.getMessage());146success = false;147} catch (Exception e) {148e.printStackTrace(out);149log.complain("Caught unexpected exception while running the test:\n\t" + e);150success = false;151}152153if (!success) {154log.complain("TEST FAILED");155return FAILED;156}157158out.println("TEST PASSED");159return PASSED;160161}162163/**164* Prepare debugee for testing and waiting for ready signal.165*/166void prepareDebugee() {167// wait for VM_INIT event from debugee168log.display("Waiting for VM_INIT event");169debugee.waitForVMInit();170171// query debugee for VM-dependent ID sizes172log.display("Querying for IDSizes");173debugee.queryForIDSizes();174175// resume initially suspended debugee176log.display("Resuming debugee VM");177debugee.resume();178179// wait for READY signal from debugee180log.display("Waiting for signal from debugee: " + READY);181String signal = pipe.readln();182log.display("Received signal from debugee: " + signal);183if (! signal.equals(READY)) {184throw new TestBug("Unexpected signal received from debugee: " + signal185+ " (expected: " + READY + ")");186}187}188189/**190* Sending debugee signal to quit and waiting for it exits.191*/192void quitDebugee() {193// send debugee signal to quit194log.display("Sending signal to debugee: " + QUIT);195pipe.println(QUIT);196197// wait for debugee exits198log.display("Waiting for debugee exits");199int code = debugee.waitFor();200201// analize debugee exit status code202if (code == JCK_STATUS_BASE + PASSED) {203log.display("Debugee PASSED with exit code: " + code);204} else {205log.complain("Debugee FAILED with exit code: " + code);206success = false;207}208}209210/**211* Perform testing JDWP command.212*/213void testCommand() {214// create command packet and fill requred out data215log.display("Create command packet:");216log.display("Command: " + JDWP_COMMAND_NAME);217CommandPacket command = new CommandPacket(JDWP_COMMAND_ID);218log.display(" stratumID: " + NEW_DEFAULT_STRATUM);219command.addString(NEW_DEFAULT_STRATUM);220command.setLength();221222// send command packet to debugee223try {224log.display("Sending command packet:\n" + command);225transport.write(command);226} catch (IOException e) {227log.complain("Unable to send command packet:\n\t" + e);228success = false;229return;230}231232ReplyPacket reply = new ReplyPacket();233234// receive reply packet from debugee235try {236log.display("Waiting for reply packet");237transport.read(reply);238log.display("Reply packet received:\n" + reply);239} catch (IOException e) {240log.complain("Unable to read reply packet:\n\t" + e);241success = false;242return;243}244245// check reply packet header246try{247log.display("Checking reply packet header");248reply.checkHeader(command.getPacketID());249} catch (BoundException e) {250log.complain("Bad header of reply packet:\n\t" + e.getMessage());251success = false;252return;253}254255// start parsing reply packet data256log.display("Parsing reply packet:");257reply.resetPosition();258259// no data in reply packet260261// check for extra data in reply packet262if (!reply.isParsed()) {263log.complain("Extra trailing bytes found in reply packet at: "264+ reply.offsetString());265success = false;266}267}268}269270271