1 /*
   2  * Copyright (c) 2007, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  *
  27  * @summary converted from VM Testbase nsk/jdwp/ReferenceType/Instances/instances001.
  28  * VM Testbase keywords: [quick, jpda, jdwp, feature_jdk6_jpda, vm6, monitoring]
  29  * VM Testbase readme:
  30  * DESCRIPTION
  31  *     This test performs checking for
  32  *         command set: ReferenceType
  33  *         command: Instances
  34  *     Test checks that debuggee accept the command packet and
  35  *     replies with correct reply packet.
  36  *     Test consists of two compoments:
  37  *         debugger: instances001
  38  *         debuggee: instances001a
  39  *     Debugger uses nsk.share support classes to launch debuggee
  40  *     and obtain Transport object, that represents JDWP transport channel.
  41  *     Also communication channel (IOPipe) is established between
  42  *     debugger and debuggee to exchange with execution commands.
  43  *     For maxInstances in [1, 0, Interger.MAX_VALUE]
  44  *     do
  45  *         Debugger obtains referenceTypeID for 'nsk.share.jdwp.ReferenceType.instances.instances001.TestClass'.
  46  *         Then, debugger creates command packet for Instances command with the
  47  *         found referenceTypeID and maxInstances as an arguments, writes packet to the transport
  48  *         channel, and waits for a reply packet.
  49  *         When reply packet is received, debugger parses the packet structure
  50  *         and extracts number of instances and instance's ids.
  51  *         Debugger checks that received number of instances is correct:
  52  *             - if maxInstances=1 only 1 instance should be returned
  53  *             - if maxInstances=0 or maxInstances=Integer.MAX_VALUE all instances should be returned
  54  *     done
  55  *     Also, test performs checks for cases when incorrect data is sent in command.
  56  *     Following cases are tested:
  57  *         - create command with maxInstances < 0, expect ILLEGAL_ARGUMENT error
  58  *         - create command with typeID = -1, expect INVALID_OBJECT error
  59  *         - create command with threadID instead of referenceTypeID, expect INVALID_CLASS error
  60  *     Finally, debugger sends debuggee signal to quit, waits for it exits
  61  *     and exits too with the proper exit code.
  62  *
  63  * @library /vmTestbase /test/hotspot/jtreg/vmTestbase
  64  *          /test/lib
  65  * @run driver jdk.test.lib.FileInstaller . .
  66  * @build nsk.jdwp.ReferenceType.Instances.instances001.instances001
  67  * @run main/othervm/native PropertyResolvingWrapper
  68  *      nsk.jdwp.ReferenceType.Instances.instances001.instances001
  69  *      -arch=${os.family}-${os.simpleArch}
  70  *      -verbose
  71  *      -waittime=5
  72  *      -debugee.vmkind=java
  73  *      -transport.address=dynamic
  74  *      -debugee.vmkeys="-Xmx128M ${test.vm.opts} ${test.java.opts}"
  75  */
  76 
  77 package nsk.jdwp.ReferenceType.Instances.instances001;
  78 
  79 import java.io.*;
  80 import nsk.share.Consts;
  81 import nsk.share.jdwp.*;
  82 import nsk.share.jpda.AbstractDebuggeeTest;
  83 
  84 public class instances001 extends TestDebuggerType1 {
  85     protected String getDebugeeClassName() {
  86         return nsk.jdwp.ReferenceType.Instances.instances001.instances001a.class.getName();
  87     }
  88 
  89     public static void main(String argv[]) {
  90         System.exit(run(argv, System.out) + Consts.JCK_STATUS_BASE);
  91     }
  92 
  93     public static int run(String argv[], PrintStream out) {
  94         return new instances001().runIt(argv, out);
  95     }
  96 
  97     private void testClass(long typeID, int maxInstances, int expectedInstances, boolean expectError, int errorCode) {
  98         try {
  99             int JDWP_COMMAND_ID = JDWP.Command.ReferenceType.Instances;
 100 
 101             log.display("Create command: " + JDWP.commandNames.get(JDWP_COMMAND_ID));
 102             log.display("referenceType = " + typeID);
 103             log.display("maxInstances = " + maxInstances);
 104 
 105             CommandPacket command = new CommandPacket(JDWP_COMMAND_ID);
 106             command.addReferenceTypeID(typeID);
 107             command.addInt(maxInstances);
 108             command.setLength();
 109 
 110             log.display("Sending command packet:\n" + command);
 111             transport.write(command);
 112 
 113             ReplyPacket reply;
 114 
 115             reply = getReply(command, expectError, errorCode);
 116 
 117             if (expectError)
 118                 return;
 119 
 120             int instances = reply.getInt();
 121             log.display("instances = " + instances);
 122 
 123             // check that correct value of 'instances' was received
 124             if (instances != expectedInstances) {
 125                 setSuccess(false);
 126                 log.complain("Unexpected 'instances' value: " + instances + ", expected is " + expectedInstances);
 127             }
 128 
 129             for (int i = 0; i < instances; i++) {
 130                 JDWP.Value value = reply.getValue();
 131                 log.display("tagged-ObjectID = " + value);
 132             }
 133 
 134             if (!reply.isParsed()) {
 135                 setSuccess(false);
 136                 log.complain("Extra trailing bytes found in reply packet at: " + reply.currentPosition());
 137             }
 138         } catch (Exception e) {
 139             setSuccess(false);
 140             log.complain("Caught exception while testing JDWP command: " + e);
 141             e.printStackTrace(log.getOutStream());
 142         }
 143     }
 144 
 145     public void doTest() {
 146         // force GC in debuggee VM to avoid collection of weak references during test execution
 147         forceGC();
 148         pipe.println(instances001a.COMMAND_CREATE_TEST_INSTANCES);
 149 
 150         if (!isDebuggeeReady())
 151             return;
 152 
 153         int expectedInstances = instances001a.expectedCount;
 154 
 155         String testClassName = nsk.jdwp.ReferenceType.Instances.instances001.TestClass.class.getName();
 156 
 157         long typeID = debuggee.getReferenceTypeID(createTypeSignature(testClassName));
 158 
 159 
 160         // create command with maxInstances=1, only 1 instance should be returned
 161         testClass(typeID, 1, 1, false, 0);
 162         // create command with maxInstances=0, all instances should be returned
 163         testClass(typeID, 0, expectedInstances, false, 0);
 164         // create command with maxInstances=Integer.MAX_VALUE, all instances should be returned
 165         testClass(typeID, Integer.MAX_VALUE, expectedInstances, false, 0);
 166 
 167         // create command with maxInstances < 0, expect ILLEGAL_ARGUMENT error
 168         testClass(typeID, -1, expectedInstances, true, JDWP.Error.ILLEGAL_ARGUMENT);
 169 
 170         // create command with typeID = 1, expect INVALID_OBJECT error
 171         testClass(-1, Integer.MAX_VALUE, expectedInstances, true, JDWP.Error.INVALID_OBJECT);
 172 
 173         // create command with threadID instead of referenceTypeID, expect INVALID_CLASS error
 174         testClass(debuggee.getThreadID("main"), Integer.MAX_VALUE, expectedInstances, true, JDWP.Error.INVALID_CLASS);
 175 
 176         // if GC occurs during test the results should be ignored
 177         resetStatusIfGC();
 178     }
 179 }