|
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.Event.Composite; |
|
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: Event.Composite. |
|
34 * |
|
35 * See composite001.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 #waitForTestedEvent() |
|
43 */ |
|
44 public class composite001 { |
|
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 // package and classes names constants |
|
52 static final String PACKAGE_NAME = "nsk.jdwp.Event.Composite"; |
|
53 static final String TEST_CLASS_NAME = PACKAGE_NAME + "." + "composite001"; |
|
54 static final String DEBUGEE_CLASS_NAME = TEST_CLASS_NAME + "a"; |
|
55 |
|
56 // tested JDWP command constants |
|
57 static final String JDWP_COMMAND_NAME = "Event.Composite"; |
|
58 static final int JDWP_COMMAND_ID = JDWP.Command.Event.Composite; |
|
59 static final byte TESTED_EVENT_KIND = JDWP.EventKind.VM_START; |
|
60 static final byte TESTED_EVENT_SUSPEND_POLICY = JDWP.SuspendPolicy.ALL; |
|
61 |
|
62 // usual scaffold objects |
|
63 ArgumentHandler argumentHandler = null; |
|
64 Log log = null; |
|
65 Binder binder = null; |
|
66 Debugee debugee = null; |
|
67 Transport transport = null; |
|
68 int waitTime = 0; // minutes |
|
69 long timeout = 0; // milliseconds |
|
70 boolean dead = false; |
|
71 boolean success = true; |
|
72 |
|
73 // ------------------------------------------------------------------- |
|
74 |
|
75 /** |
|
76 * Start test from command line. |
|
77 */ |
|
78 public static void main(String argv[]) { |
|
79 System.exit(run(argv,System.out) + JCK_STATUS_BASE); |
|
80 } |
|
81 |
|
82 /** |
|
83 * Start test from JCK-compilant environment. |
|
84 */ |
|
85 public static int run(String argv[], PrintStream out) { |
|
86 return new composite001().runIt(argv, out); |
|
87 } |
|
88 |
|
89 // ------------------------------------------------------------------- |
|
90 |
|
91 /** |
|
92 * Perform test execution. |
|
93 */ |
|
94 public int runIt(String argv[], PrintStream out) { |
|
95 |
|
96 // make log for debugger messages |
|
97 argumentHandler = new ArgumentHandler(argv); |
|
98 log = new Log(out, argumentHandler); |
|
99 waitTime = argumentHandler.getWaitTime(); |
|
100 timeout = waitTime * 60 * 1000; |
|
101 |
|
102 // execute test and display results |
|
103 try { |
|
104 log.display("\n>>> Starting debugee \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 log.display(" ... debugee launched"); |
|
112 log.display(""); |
|
113 |
|
114 // set timeout for debuggee responces |
|
115 log.display("Setting timeout for debuggee responces: " + waitTime + " minute(s)"); |
|
116 transport.setReadTimeout(timeout); |
|
117 log.display(" ... timeout set"); |
|
118 |
|
119 // wait for debuggee started |
|
120 log.display("Waiting for VM_INIT event"); |
|
121 debugee.waitForVMInit(); |
|
122 log.display(" ... VM_INIT event received"); |
|
123 |
|
124 // query debugee for VM-dependent ID sizes |
|
125 log.display("Querying for IDSizes"); |
|
126 debugee.queryForIDSizes(); |
|
127 log.display(" ... size of VM-dependent types adjusted"); |
|
128 |
|
129 // test JDWP command |
|
130 log.display("\n>>> Testing JDWP event \n"); |
|
131 testCommand(); |
|
132 |
|
133 log.display("\n>>> Finishing debuggee \n"); |
|
134 |
|
135 // resume debuggee |
|
136 log.display("Resuming debuggee"); |
|
137 debugee.resume(); |
|
138 log.display(" ... debuggee resumed"); |
|
139 |
|
140 // wait for debuggee exited |
|
141 log.display("Waiting for VM_DEATH event"); |
|
142 debugee.waitForVMDeath(); |
|
143 dead = true; |
|
144 log.display(" ... VM_DEATH event received"); |
|
145 |
|
146 } catch (Failure e) { |
|
147 log.complain("TEST FAILED: " + e.getMessage()); |
|
148 success = false; |
|
149 } catch (Exception e) { |
|
150 e.printStackTrace(out); |
|
151 log.complain("Caught unexpected exception while running the test:\n\t" + e); |
|
152 success = false; |
|
153 } finally { |
|
154 // quit debugee |
|
155 log.display("\n>>> Finishing test \n"); |
|
156 quitDebugee(); |
|
157 } |
|
158 |
|
159 // check test results |
|
160 if (!success) { |
|
161 log.complain("TEST FAILED"); |
|
162 return FAILED; |
|
163 } |
|
164 |
|
165 out.println("TEST PASSED"); |
|
166 return PASSED; |
|
167 |
|
168 } |
|
169 |
|
170 /** |
|
171 * Test JDWP command. |
|
172 */ |
|
173 void testCommand() { |
|
174 // create command packet and fill requred out data |
|
175 log.display("Create command packet: " + JDWP_COMMAND_NAME); |
|
176 CommandPacket command = new CommandPacket(JDWP_COMMAND_ID); |
|
177 log.display(" suspendPolicy: " + TESTED_EVENT_SUSPEND_POLICY); |
|
178 command.addByte(TESTED_EVENT_SUSPEND_POLICY); |
|
179 log.display(" events: " + 1); |
|
180 command.addInt(1); |
|
181 log.display(" eventKind: " + TESTED_EVENT_KIND); |
|
182 command.addByte(TESTED_EVENT_KIND); |
|
183 log.display(" requestID: " + 0); |
|
184 command.addInt(0); |
|
185 log.display(" threadID: " + 0); |
|
186 command.addObjectID(0); |
|
187 command.setLength(); |
|
188 log.display(" ... command packet created"); |
|
189 |
|
190 // send command packet to debugee |
|
191 try { |
|
192 log.display("Sending command packet:\n" + command); |
|
193 transport.write(command); |
|
194 log.display(" ... command packet sent"); |
|
195 } catch (IOException e) { |
|
196 log.complain("Unable to send command packet:\n\t" + e); |
|
197 success = false; |
|
198 return; |
|
199 } |
|
200 |
|
201 ReplyPacket reply = new ReplyPacket(); |
|
202 |
|
203 // receive reply packet from debugee |
|
204 try { |
|
205 log.display("Waiting for reply packet"); |
|
206 transport.read(reply); |
|
207 log.display(" ... reply packet received:\n" + reply); |
|
208 } catch (IOException e) { |
|
209 log.complain("Unable to read reply packet:\n\t" + e); |
|
210 success = false; |
|
211 return; |
|
212 } |
|
213 |
|
214 // check reply packet header |
|
215 log.display("Checking header of reply packet:"); |
|
216 |
|
217 // extract and check length field |
|
218 int length = reply.getLength(); |
|
219 log.display(" length: " + length); |
|
220 if (length != reply.length()) { |
|
221 log.complain("Unexpected value of length field in header of reply packet: " |
|
222 + length + "(actual: " + reply.length() + ")"); |
|
223 success = false; |
|
224 } |
|
225 |
|
226 // extract and check id field |
|
227 int id = reply.getPacketID(); |
|
228 log.display(" id: " + id); |
|
229 if (id != command.getPacketID()) { |
|
230 log.complain("Unexpected value of id field in header of reply packet: " |
|
231 + id + "(expected: " + command.getPacketID() + ")"); |
|
232 success = false; |
|
233 } |
|
234 |
|
235 // extract and check flags field |
|
236 byte flags = reply.getFlags(); |
|
237 log.display(" flags: " + flags); |
|
238 if (flags != JDWP.Flag.REPLY_PACKET) { |
|
239 log.complain("Unexpected value of flags field in header of reply packet: " |
|
240 + flags + "(expected: " + JDWP.Flag.REPLY_PACKET + ")"); |
|
241 success = false; |
|
242 } |
|
243 |
|
244 // extract and check error field (should be not null!) |
|
245 int error = reply.getErrorCode(); |
|
246 log.display(" error: " + error); |
|
247 if (error == JDWP.Error.NONE) { |
|
248 log.complain("Unexpected null value of error field in header of reply packet: " |
|
249 + error + "(expected: not " + JDWP.Error.NONE + ")"); |
|
250 success = false; |
|
251 } |
|
252 |
|
253 log.display(" ... packet header is parsed"); |
|
254 |
|
255 // start parsing reply packet data |
|
256 log.display("Parsing reply packet data:"); |
|
257 reply.resetPosition(); |
|
258 |
|
259 // no out data |
|
260 log.display(" no out data"); |
|
261 |
|
262 log.display(" ... packet data is parsed"); |
|
263 |
|
264 // check for extra data in reply packet |
|
265 if (!reply.isParsed()) { |
|
266 log.complain("Extra trailing bytes found in reply packet at: " |
|
267 + reply.offsetString()); |
|
268 success = false; |
|
269 } |
|
270 } |
|
271 |
|
272 /** |
|
273 * Disconnect debuggee and wait for it exited. |
|
274 */ |
|
275 void quitDebugee() { |
|
276 if (debugee == null) |
|
277 return; |
|
278 |
|
279 // disconnect debugee if not dead |
|
280 if (!dead) { |
|
281 try { |
|
282 log.display("Disconnecting debuggee"); |
|
283 debugee.dispose(); |
|
284 log.display(" ... debuggee disconnected"); |
|
285 } catch (Failure e) { |
|
286 log.display("Failed to finally disconnect debuggee:\n\t" |
|
287 + e.getMessage()); |
|
288 } |
|
289 } |
|
290 |
|
291 // wait for debugee exited |
|
292 log.display("Waiting for debuggee exit"); |
|
293 int code = debugee.waitFor(); |
|
294 log.display(" ... debuggee exited with exit code: " + code); |
|
295 |
|
296 // analize debugee exit status code |
|
297 if (code != JCK_STATUS_BASE + PASSED) { |
|
298 log.complain("Debuggee FAILED with exit code: " + code); |
|
299 success = false; |
|
300 } |
|
301 } |
|
302 |
|
303 } |