1 /*
2  * Copyright (c) 2001, 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 package nsk.jdwp.VirtualMachine.HoldEvents;
25 
26 import java.io.*;
27 
28 import nsk.share.*;
29 import nsk.share.jpda.*;
30 import nsk.share.jdwp.*;
31 
32 /**
33  * Test for JDWP command: VirtualMachine.HoldEvents.
34  *
35  * See holdevents001.README for description of test execution.
36  *
37  * This class represents debugger part of the test.
38  * Test is executed by invoking method runIt().
39  * JDWP command is tested in the method testCommand().
40  *
41  * @see #runIt()
42  * @see #testCommand()
43  */
44 public class holdevents001 {
45 
46     // exit status constants
47     static final int JCK_STATUS_BASE = 95;
48     static final int PASSED = 0;
49     static final int FAILED = 2;
50 
51     // communication signals constants
52     static final String READY = "ready";
53     static final String QUIT = "quit";
54 
55     // package and classes names constants
56     static final String PACKAGE_NAME = "nsk.jdwp.VirtualMachine.HoldEvents";
57     static final String TEST_CLASS_NAME = PACKAGE_NAME + "." + "holdevents001";
58     static final String DEBUGEE_CLASS_NAME = TEST_CLASS_NAME + "a";
59 
60     // tested JDWP command constants
61     static final String JDWP_COMMAND_NAME = "VirtualMachine.HoldEvents";
62     static final int JDWP_COMMAND_ID = JDWP.Command.VirtualMachine.HoldEvents;
63 
64     // usual scaffold objects
65     ArgumentHandler argumentHandler = null;
66     Log log = null;
67     Binder binder = null;
68     Debugee debugee = null;
69     Transport transport = null;
70     IOPipe pipe = null;
71 
72     // test passed or not
73     boolean success = true;
74 
75     // -------------------------------------------------------------------
76 
77     /**
78      * Start test from command line.
79      */
main(String argv[])80     public static void main (String argv[]) {
81         System.exit(run(argv,System.out) + JCK_STATUS_BASE);
82     }
83 
84     /**
85      * Start JCK-compilant test.
86      */
run(String argv[], PrintStream out)87     public static int run(String argv[], PrintStream out) {
88         return new holdevents001().runIt(argv, out);
89     }
90 
91     // -------------------------------------------------------------------
92 
93     /**
94      * Perform test execution.
95      */
runIt(String argv[], PrintStream out)96     public int runIt(String argv[], PrintStream out) {
97 
98         // make log for debugger messages
99         argumentHandler = new ArgumentHandler(argv);
100         log = new Log(out, argumentHandler);
101 
102         // execute test and display results
103         try {
104             log.display("\n>>> Preparing debugee for testing \n");
105 
106             // launch debuggee
107             binder = new Binder(argumentHandler, log);
108             log.display("Launching debugee");
109             debugee = binder.bindToDebugee(DEBUGEE_CLASS_NAME);
110             transport = debugee.getTransport();
111             pipe = debugee.createIOPipe();
112 
113             // make debuggee ready for testing
114             prepareDebugee();
115 
116             // work with prepared debuggee
117             try {
118 
119                 // perform testing JDWP command
120                 log.display("\n>>> Testing JDWP command \n");
121                 testCommand();
122 
123             } finally {
124 
125                 log.display("\n>>> Finishing test \n");
126 
127                 // finally release events
128                 log.display("Releasing events into debuggee");
129                 releaseEvents();
130 
131                 // quit debugee
132                 quitDebugee();
133             }
134 
135         } catch (Failure e) {
136             log.complain("TEST FAILED: " + e.getMessage());
137             success = false;
138         } catch (Exception e) {
139             e.printStackTrace(out);
140             log.complain("Caught unexpected exception while running the test:\n\t" + e);
141             success = false;
142         }
143 
144         if (!success) {
145             log.complain("TEST FAILED");
146             return FAILED;
147         }
148 
149         out.println("TEST PASSED");
150         return PASSED;
151 
152     }
153 
154     /**
155      * Prepare debugee for testing and waiting for ready signal.
156      */
prepareDebugee()157     void prepareDebugee() {
158         // wait for VM_INIT event from debugee
159         log.display("Waiting for VM_INIT event");
160         debugee.waitForVMInit();
161 
162         // query debugee for VM-dependent ID sizes
163         log.display("Querying for IDSizes");
164         debugee.queryForIDSizes();
165 
166         // resume initially suspended debugee
167         log.display("Resuming debugee VM");
168         debugee.resume();
169 
170         // wait for READY signal from debugee
171         log.display("Waiting for signal from debugee: " + READY);
172         String signal = pipe.readln();
173         log.display("Received signal from debugee: " + signal);
174         if (! signal.equals(READY)) {
175             throw new TestBug("Unexpected signal received from debugee: " + signal
176                             + " (expected: " + READY + ")");
177         }
178     }
179 
180     /**
181      * Sending debugee signal to quit and waiting for it exits.
182      */
quitDebugee()183     void quitDebugee() {
184         // send debugee signal to quit
185         log.display("Sending signal to debugee: " + QUIT);
186         pipe.println(QUIT);
187 
188         // wait for debugee exits
189         log.display("Waiting for debugee exits");
190         int code = debugee.waitFor();
191 
192         // analize debugee exit status code
193         if (code == JCK_STATUS_BASE + PASSED) {
194             log.display("Debugee PASSED with exit code: " + code);
195         } else {
196             log.complain("Debugee FAILED with exit code: " + code);
197             success = false;
198         }
199     }
200 
201     /**
202      * Release events into debuggee.
203      */
releaseEvents()204     void releaseEvents() {
205         CommandPacket command = new CommandPacket(JDWP.Command.VirtualMachine.ReleaseEvents);
206         ReplyPacket reply = debugee.receiveReplyFor(command);
207     }
208 
209     /**
210      * Perform testing JDWP command.
211      */
testCommand()212     void testCommand() {
213         // create command packet and fill requred out data
214         log.display("Create command packet:");
215         log.display("Command: " + JDWP_COMMAND_NAME);
216         CommandPacket command = new CommandPacket(JDWP_COMMAND_ID);
217         command.setLength();
218 
219         // send command packet to debugee
220         try {
221             log.display("Sending command packet:\n" + command);
222             transport.write(command);
223         } catch (IOException e) {
224             log.complain("Unable to send command packet:\n\t" + e);
225             success = false;
226             return;
227         }
228 
229         ReplyPacket reply = new ReplyPacket();
230 
231         // receive reply packet from debugee
232         try {
233             log.display("Waiting for reply packet");
234             transport.read(reply);
235             log.display("Reply packet received:\n" + reply);
236         } catch (IOException e) {
237             log.complain("Unable to read reply packet:\n\t" + e);
238             success = false;
239             return;
240         }
241 
242         // check reply packet header
243         try{
244             log.display("Checking reply packet header");
245             reply.checkHeader(command.getPacketID());
246         } catch (BoundException e) {
247             log.complain("Bad header of reply packet:\n\t" + e.getMessage());
248             success = false;
249             return;
250         }
251 
252         // start parsing reply packet data
253         log.display("Parsing reply packet:");
254         reply.resetPosition();
255 
256         // no data in reply packet
257 
258         // check for extra data in reply packet
259         if (!reply.isParsed()) {
260             log.complain("Extra trailing bytes found in reply packet at: "
261                         + reply.offsetString());
262             success = false;
263         }
264     }
265 
266 }
267