Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jdwp/ClassLoaderReference/VisibleClasses/visibclasses001.java
41160 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.ClassLoaderReference.VisibleClasses;2425import java.io.*;2627import nsk.share.*;28import nsk.share.jpda.*;29import nsk.share.jdwp.*;3031/**32* Test for JDWP command: ClassLoaderReference.VisibleClasses.33*34* See visibclasses001.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 visibclasses001 {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.ClassLoaderReference.VisibleClasses";55static final String TEST_CLASS_NAME = PACKAGE_NAME + "." + "visibclasses001";56static final String DEBUGEE_CLASS_NAME = TEST_CLASS_NAME + "a";5758// tested JDWP command constants59static final String JDWP_COMMAND_NAME = "ClassLoaderReference.VisibleClasses";60static final int JDWP_COMMAND_ID = JDWP.Command.ClassLoaderReference.VisibleClasses;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// usual scaffold objects67ArgumentHandler argumentHandler = null;68Log log = null;69Binder binder = null;70Debugee debugee = null;71Transport transport = null;72IOPipe pipe = null;7374// test passed or not75boolean success = true;7677// -------------------------------------------------------------------7879/**80* Start test from command line.81*/82public static void main (String argv[]) {83System.exit(run(argv,System.out) + JCK_STATUS_BASE);84}8586/**87* Start JCK-compilant test.88*/89public static int run(String argv[], PrintStream out) {90return new visibclasses001().runIt(argv, out);91}9293// -------------------------------------------------------------------9495/**96* Perform test execution.97*/98public int runIt(String argv[], PrintStream out) {99100// make log for debugger messages101argumentHandler = new ArgumentHandler(argv);102log = new Log(out, argumentHandler);103104// execute test and display results105try {106log.display("\n>>> Preparing debugee for testing \n");107108// launch debugee109binder = new Binder(argumentHandler, log);110log.display("Launching debugee");111debugee = binder.bindToDebugee(DEBUGEE_CLASS_NAME);112transport = debugee.getTransport();113pipe = debugee.createIOPipe();114115// make debuggee ready for testing116prepareDebugee();117118// work with prepared debugee119try {120log.display("\n>>> Obtaining requred data from debugee \n");121122// query debugee for TypeID of tested class123log.display("Getting ReferenceTypeID by signature:\n"124+ " " + TESTED_CLASS_SIGNATURE);125long classID = debugee.getReferenceTypeID(TESTED_CLASS_SIGNATURE);126log.display(" got classID: " + classID);127128// query debugee for TypeIDs of classes been nested129log.display("Getting classLoaderID for tested classes");130long classLoaderID = queryClassLoaderID(classID);131log.display(" got classLoaderID: " + classLoaderID);132133// perform testing JDWP command134log.display("\n>>> Testing JDWP command \n");135testCommand(classLoaderID, classID);136137} finally {138// quit debugee139log.display("\n>>> Finishing test \n");140quitDebugee();141}142143} catch (Failure e) {144log.complain("TEST FAILED: " + e.getMessage());145success = false;146} catch (Exception e) {147e.printStackTrace(out);148log.complain("Caught unexpected exception while running the test:\n\t" + e);149success = false;150}151152if (!success) {153log.complain("TEST FAILED");154return FAILED;155}156157out.println("TEST PASSED");158return PASSED;159160}161162/**163* Prepare debugee for testing and waiting for ready signal.164*/165void prepareDebugee() {166// wait for VM_INIT event from debugee167log.display("Waiting for VM_INIT event");168debugee.waitForVMInit();169170// query debugee for VM-dependent ID sizes171log.display("Querying for IDSizes");172debugee.queryForIDSizes();173174// resume initially suspended debugee175log.display("Resuming debugee VM");176debugee.resume();177178// wait for READY signal from debugee179log.display("Waiting for signal from debugee: " + READY);180String signal = pipe.readln();181log.display("Received signal from debugee: " + signal);182if (! signal.equals(READY)) {183throw new TestBug("Unexpected signal received from debugee: " + signal184+ " (expected: " + READY + ")");185}186}187188/**189* Sending debugee signal to quit and waiting for it exits.190*/191void quitDebugee() {192// send debugee signal to quit193log.display("Sending signal to debugee: " + QUIT);194pipe.println(QUIT);195196// wait for debugee exits197log.display("Waiting for debugee exits");198int code = debugee.waitFor();199200// analize debugee exit status code201if (code == JCK_STATUS_BASE + PASSED) {202log.display("Debugee PASSED with exit code: " + code);203} else {204log.complain("Debugee FAILED with exit code: " + code);205success = false;206}207}208209/**210* Query debugee for classLoaderID for specified classID.211*/212long queryClassLoaderID(long classID) {213CommandPacket command =214new CommandPacket(JDWP.Command.ReferenceType.ClassLoader);215command.addReferenceTypeID(classID);216ReplyPacket reply = debugee.receiveReplyFor(command);217218try {219reply.resetPosition();220221long classLoaderID = reply.getObjectID();222return classLoaderID;223} catch (BoundException e) {224throw new Failure("Unable to parse reply packet for ReferenceType.ClassLoader:\n\t"225+ e);226}227}228229/**230* Perform testing JDWP command for specified classLoaderID.231*/232void testCommand(long classLoaderID, long expectedClassID) {233// create command packet and fill requred out data234log.display("Create command packet:");235log.display("Command: " + JDWP_COMMAND_NAME);236log.display(" classLoaderID: " + classLoaderID);237CommandPacket command = new CommandPacket(JDWP_COMMAND_ID);238command.addObjectID(classLoaderID);239command.setLength();240241// send command packet to debugee242try {243log.display("Sending command packet:\n" + command);244transport.write(command);245} catch (IOException e) {246log.complain("Unable to send command packet:\n\t" + e);247success = false;248return;249}250251ReplyPacket reply = new ReplyPacket();252253// receive reply packet from debugee254try {255log.display("Waiting for reply packet");256transport.read(reply);257log.display("Reply packet received:\n" + reply);258} catch (IOException e) {259log.complain("Unable to read reply packet:\n\t" + e);260success = false;261return;262}263264// check reply packet header265try{266log.display("Checking reply packet header");267reply.checkHeader(command.getPacketID());268} catch (BoundException e) {269log.complain("Bad header of reply packet:\n\t" + e.getMessage());270success = false;271return;272}273274// start parsing reply packet data275log.display("Parsing reply packet:");276reply.resetPosition();277278// extract and check number of nested classes279int classes = 0;280try {281classes = reply.getInt();282log.display(" classes: " + classes);283284} catch (BoundException e) {285log.complain("Unable to extract number of nested classes from reply packet:\n\t"286+ e.getMessage());287success = false;288}289290if (classes < 0) {291log.complain("Negative number of classes in the reply packet:" + classes);292success = false;293} else if (classes == 0) {294log.complain("Zero number of classes in the reply packet:" + classes);295success = false;296}297298boolean found = false;299// extract and check TypeID for each received class300for (int i = 0; i < classes; i++ ) {301log.display(" class #" + i);302303// extract TypeTag byte304byte refTypeTag = (byte)0;305String refTypeTagName = null;306try {307refTypeTag = reply.getByte();308String tag;309switch (refTypeTag) {310case JDWP.TypeTag.CLASS:311refTypeTagName = "CLASS";312break;313case JDWP.TypeTag.INTERFACE:314refTypeTagName = "INTERFACE";315break;316case JDWP.TypeTag.ARRAY:317refTypeTagName = "ARRAY";318break;319default:320refTypeTagName = "UNKNOWN";321break;322}323log.display(" refTypeTag: " + refTypeTag + "=" + refTypeTagName);324} catch (BoundException e) {325log.complain("Unable to extract refTypetag of " + i326+ " class from reply packet:\n\t"327+ e.getMessage());328success = false;329break;330}331332// extract and check TypeID333long typeID = 0;334try {335typeID = reply.getReferenceTypeID();336log.display(" typeID: " + typeID);337338} catch (BoundException e) {339log.complain("Unable to extract TypeID of " + i340+ " nested class from reply packet:\n\t"341+ e.getMessage());342success = false;343break;344}345346if (typeID == expectedClassID) {347log.display("Found expected classID: " + expectedClassID);348found = true;349if (refTypeTag != JDWP.TypeTag.CLASS) {350log.complain("unexpected refTypeTag returned for checked class: "351+ refTypeTag + "=" + refTypeTagName352+ " (expected: " + JDWP.TypeTag.CLASS + "=CLASS)");353success = false;354}355}356}357358// check for extra data in reply packet359if (!reply.isParsed()) {360log.complain("Extra trailing bytes found in reply packet at: " + reply.offsetString());361success = false;362}363364if (!found) {365log.complain("Expected classID not found in the list of visible classes: " + expectedClassID);366success = false;367}368}369370}371372373