Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jdwp/ArrayReference/Length/length001.java
41162 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.ArrayReference.Length;2425import java.io.*;2627import nsk.share.*;28import nsk.share.jpda.*;29import nsk.share.jdwp.*;3031/**32* Test for JDWP command: ArrayReference.Length.33*34* See length001.README for description of test execution.35*36* Test is executed by invoking method runIt().37* JDWP command is tested in method testCommand().38*39* @see #runIt()40* @see #testCommand()41*/42public class length001 {4344// exit status constants45static final int JCK_STATUS_BASE = 95;46static final int PASSED = 0;47static final int FAILED = 2;4849// communication signals constants50static final String READY = "ready";51static final String QUIT = "quit";5253// package and classes names constants54static final String PACKAGE_NAME = "nsk.jdwp.ArrayReference.Length";55static final String TEST_CLASS_NAME = PACKAGE_NAME + "." + "length001";56static final String DEBUGEE_CLASS_NAME = TEST_CLASS_NAME + "a";5758// tested JDWP command constants59static final String JDWP_COMMAND_NAME = "ArrayReference.Length";60static final int JDWP_COMMAND_ID = JDWP.Command.ArrayReference.Length;6162// tested class name and signature constants63static final String TESTED_CLASS_NAME = DEBUGEE_CLASS_NAME + "$" + "TestedClass";64static final String TESTED_CLASS_SIGNATURE = "L" + TESTED_CLASS_NAME.replace('.', '/') + ";";6566// name of the static field in the tested class with the tested object value67static final String ARRAY_FIELD_NAME = length001a.ARRAY_FIELD_NAME;68static final int ARRAY_LENGTH = length001a.ARRAY_LENGTH;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 length001().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 debugee113binder = 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 debugee123try {124log.display("\n>>> Obtaining requred data from debugee \n");125126// query debugee for TypeID of tested class127log.display("Getting ReferenceTypeID by signature:\n"128+ " " + TESTED_CLASS_SIGNATURE);129long classID = debugee.getReferenceTypeID(TESTED_CLASS_SIGNATURE);130log.display(" got classID: " + classID);131132// query debuggee for arrayID value from static field133log.display("Getting arrayID value from static field: "134+ ARRAY_FIELD_NAME);135long arrayID = queryObjectID(classID,136ARRAY_FIELD_NAME, JDWP.Tag.ARRAY);137log.display(" got arrayID: " + arrayID);138139// perform testing JDWP command140log.display("\n>>> Testing JDWP command \n");141testCommand(arrayID);142143} finally {144// quit debugee145log.display("\n>>> Finishing test \n");146quitDebugee();147}148149} catch (Failure e) {150log.complain("TEST FAILED: " + e.getMessage());151e.printStackTrace(out);152success = false;153} catch (Exception e) {154log.complain("Caught unexpected exception:\n" + e);155e.printStackTrace(out);156success = false;157}158159if (!success) {160log.complain("TEST FAILED");161return FAILED;162}163164out.println("TEST PASSED");165return PASSED;166167}168169/**170* Prepare debugee for testing and waiting for ready signal.171*/172void prepareDebugee() {173// wait for VM_INIT event from debugee174log.display("Waiting for VM_INIT event");175debugee.waitForVMInit();176177// query debugee for VM-dependent ID sizes178log.display("Querying for IDSizes");179debugee.queryForIDSizes();180181// resume initially suspended debugee182log.display("Resuming debugee VM");183debugee.resume();184185// wait for READY signal from debugee186log.display("Waiting for signal from debugee: " + READY);187String signal = pipe.readln();188log.display("Received signal from debugee: " + signal);189if (! signal.equals(READY)) {190throw new TestBug("Unexpected signal received form debugee: " + signal191+ " (expected: " + READY + ")");192}193}194195/**196* Sending debugee signal to quit and waiting for it exits.197*/198void quitDebugee() {199// send debugee signal to quit200log.display("Sending signal to debugee: " + QUIT);201pipe.println(QUIT);202203// wait for debugee exits204log.display("Waiting for debugee exits");205int code = debugee.waitFor();206207// analize debugee exit status code208if (code == JCK_STATUS_BASE + PASSED) {209log.display("Debugee PASSED with exit code: " + code);210} else {211log.complain("Debugee FAILED with exit code: " + code);212success = false;213}214}215216/**217* Query debuggee for objectID value of static class field.218*/219long queryObjectID(long classID, String fieldName, byte tag) {220// get fieledID for static field (declared in the class)221long fieldID = debugee.getClassFieldID(classID, fieldName, true);222// get value of the field223JDWP.Value value = debugee.getStaticFieldValue(classID, fieldID);224225// check that value has THREAD tag226if (value.getTag() != tag) {227throw new Failure("Wrong objectID tag received from field \"" + fieldName228+ "\": " + value.getTag() + " (expected: " + tag + ")");229}230231// extract threadID from the value232long objectID = ((Long)value.getValue()).longValue();233return objectID;234}235236/**237* Perform testing JDWP command for specified arrayID.238*/239void testCommand(long arrayID) {240// create command packet241log.display("Create command packet:");242log.display("Command: " + JDWP_COMMAND_NAME);243CommandPacket command = new CommandPacket(JDWP_COMMAND_ID);244245// add out data to the command packet246log.display(" arrayID: " + arrayID);247command.addObjectID(arrayID);248command.setLength();249250// send command packet to debugee251try {252log.display("Sending command packet:\n" + command);253transport.write(command);254} catch (IOException e) {255log.complain("Unable to send command packet:\n" + e);256success = false;257return;258}259260ReplyPacket reply = new ReplyPacket();261262// receive reply packet from debugee263try {264log.display("Waiting for reply packet");265transport.read(reply);266log.display("Reply packet received:\n" + reply);267} catch (IOException e) {268log.complain("Unable to read reply packet:\n" + e);269success = false;270return;271}272273// check reply packet header274try{275log.display("Checking reply packet header");276reply.checkHeader(command.getPacketID());277} catch (BoundException e) {278log.complain("Bad header of reply packet: " + e.getMessage());279success = false;280}281282// start parsing reply packet data283log.display("Parsing reply packet:");284reply.resetPosition();285286// extract array length287int arrayLength = 0;288try {289arrayLength = reply.getInt();290log.display(" arrayLength: " + arrayLength);291292} catch (BoundException e) {293log.complain("Unable to extract arrayLength from reply packet:\n\t"294+ e.getMessage());295success = false;296}297298// check if arrayLength is as expected299if (arrayLength < 0) {300log.complain("Negative number of arrayLength received:" + arrayLength301+ " (expected: " + ARRAY_LENGTH + ")");302success = false;303} else if (arrayLength != ARRAY_LENGTH) {304log.complain("Unexpected number of values received:" + arrayLength305+ " (expected: " + ARRAY_LENGTH + ")");306success = false;307}308309// check for extra data in reply packet310if (! reply.isParsed()) {311log.complain("Extra trailing bytes found in reply packet at: "312+ "0x" + reply.toHexString(reply.currentDataPosition(), 4));313success = false;314}315}316317}318319320