2
|
1 |
/*
|
|
2 |
* Copyright 2005-2006 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
20 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
21 |
* have any questions.
|
|
22 |
*/
|
|
23 |
|
|
24 |
/* @test
|
|
25 |
* @bug 6306165
|
|
26 |
* @summary Check that a bad handshake doesn't cause a debuggee to abort
|
|
27 |
*
|
|
28 |
* @build VMConnection BadHandshakeTest Exit0
|
|
29 |
* @run main BadHandshakeTest
|
|
30 |
*
|
|
31 |
*/
|
|
32 |
import java.io.InputStream;
|
|
33 |
import java.io.IOException;
|
|
34 |
import java.io.File;
|
|
35 |
import java.io.BufferedInputStream;
|
|
36 |
import java.net.ServerSocket;
|
|
37 |
import java.net.Socket;
|
|
38 |
import java.net.InetAddress;
|
|
39 |
import com.sun.jdi.Bootstrap;
|
|
40 |
import com.sun.jdi.VirtualMachine;
|
|
41 |
import com.sun.jdi.event.*;
|
|
42 |
import com.sun.jdi.connect.Connector;
|
|
43 |
import com.sun.jdi.connect.AttachingConnector;
|
|
44 |
import java.util.Map;
|
|
45 |
import java.util.List;
|
|
46 |
import java.util.Iterator;
|
|
47 |
|
|
48 |
public class BadHandshakeTest {
|
|
49 |
|
|
50 |
static volatile boolean ready = false;
|
|
51 |
|
|
52 |
/*
|
|
53 |
* Helper class to redirect process output/error
|
|
54 |
*/
|
|
55 |
static class IOHandler implements Runnable {
|
|
56 |
InputStream in;
|
|
57 |
|
|
58 |
IOHandler(InputStream in) {
|
|
59 |
this.in = in;
|
|
60 |
}
|
|
61 |
|
|
62 |
static void handle(InputStream in) {
|
|
63 |
IOHandler handler = new IOHandler(in);
|
|
64 |
Thread thr = new Thread(handler);
|
|
65 |
thr.setDaemon(true);
|
|
66 |
thr.start();
|
|
67 |
}
|
|
68 |
|
|
69 |
public void run() {
|
|
70 |
try {
|
|
71 |
byte b[] = new byte[100];
|
|
72 |
for (;;) {
|
|
73 |
int n = in.read(b, 0, 100);
|
|
74 |
// The first thing that will get read is
|
|
75 |
// Listening for transport dt_socket at address: xxxxx
|
|
76 |
// which shows the debuggee is ready to accept connections.
|
|
77 |
ready = true;
|
|
78 |
if (n < 0) {
|
|
79 |
break;
|
|
80 |
}
|
|
81 |
String s = new String(b, 0, n, "UTF-8");
|
|
82 |
System.out.print(s);
|
|
83 |
}
|
|
84 |
} catch (IOException ioe) {
|
|
85 |
ioe.printStackTrace();
|
|
86 |
}
|
|
87 |
}
|
|
88 |
|
|
89 |
}
|
|
90 |
|
|
91 |
/*
|
|
92 |
* Find a connector by name
|
|
93 |
*/
|
|
94 |
private static Connector findConnector(String name) {
|
|
95 |
List connectors = Bootstrap.virtualMachineManager().allConnectors();
|
|
96 |
Iterator iter = connectors.iterator();
|
|
97 |
while (iter.hasNext()) {
|
|
98 |
Connector connector = (Connector)iter.next();
|
|
99 |
if (connector.name().equals(name)) {
|
|
100 |
return connector;
|
|
101 |
}
|
|
102 |
}
|
|
103 |
return null;
|
|
104 |
}
|
|
105 |
|
|
106 |
/*
|
|
107 |
* Launch a server debuggee with the given address
|
|
108 |
*/
|
|
109 |
private static Process launch(String address, String class_name) throws IOException {
|
|
110 |
String exe = System.getProperty("java.home")
|
|
111 |
+ File.separator + "bin" + File.separator;
|
|
112 |
String arch = System.getProperty("os.arch");
|
|
113 |
if (arch.equals("sparcv9")) {
|
|
114 |
exe += "sparcv9/java";
|
|
115 |
} else {
|
|
116 |
exe += "java";
|
|
117 |
}
|
|
118 |
String cmd = exe + " " + VMConnection.getDebuggeeVMOptions() +
|
|
119 |
" -agentlib:jdwp=transport=dt_socket" +
|
|
120 |
",server=y" + ",suspend=y" + ",address=" + address +
|
|
121 |
" " + class_name;
|
|
122 |
|
|
123 |
System.out.println("Starting: " + cmd);
|
|
124 |
|
|
125 |
Process p = Runtime.getRuntime().exec(cmd);
|
|
126 |
|
|
127 |
IOHandler.handle(p.getInputStream());
|
|
128 |
IOHandler.handle(p.getErrorStream());
|
|
129 |
|
|
130 |
return p;
|
|
131 |
}
|
|
132 |
|
|
133 |
/*
|
|
134 |
* - pick a TCP port
|
|
135 |
* - Launch a server debuggee: server=y,suspend=y,address=${port}
|
|
136 |
* - run it to VM death
|
|
137 |
* - verify we saw no error
|
|
138 |
*/
|
|
139 |
public static void main(String args[]) throws Exception {
|
|
140 |
// find a free port
|
|
141 |
ServerSocket ss = new ServerSocket(0);
|
|
142 |
int port = ss.getLocalPort();
|
|
143 |
ss.close();
|
|
144 |
|
|
145 |
String address = String.valueOf(port);
|
|
146 |
|
|
147 |
// launch the server debuggee
|
|
148 |
Process process = launch(address, "Exit0");
|
|
149 |
|
|
150 |
// wait for the debugge to be ready
|
|
151 |
while (!ready) {
|
|
152 |
try {
|
|
153 |
Thread.sleep(1000);
|
|
154 |
} catch(Exception ee) {
|
|
155 |
throw ee;
|
|
156 |
}
|
|
157 |
}
|
|
158 |
|
|
159 |
// Connect to the debuggee and handshake with garbage
|
|
160 |
Socket s = new Socket(InetAddress.getLocalHost(), port);
|
|
161 |
s.getOutputStream().write("Here's a poke in the eye".getBytes("UTF-8"));
|
|
162 |
s.close();
|
|
163 |
|
|
164 |
// Re-connect and to a partial handshake - don't disconnect
|
|
165 |
s = new Socket(InetAddress.getLocalHost(), port);
|
|
166 |
s.getOutputStream().write("JDWP-".getBytes("UTF-8"));
|
|
167 |
|
|
168 |
|
|
169 |
// attach to server debuggee and resume it so it can exit
|
|
170 |
AttachingConnector conn = (AttachingConnector)findConnector("com.sun.jdi.SocketAttach");
|
|
171 |
Map conn_args = conn.defaultArguments();
|
|
172 |
Connector.IntegerArgument port_arg =
|
|
173 |
(Connector.IntegerArgument)conn_args.get("port");
|
|
174 |
port_arg.setValue(port);
|
|
175 |
VirtualMachine vm = conn.attach(conn_args);
|
|
176 |
|
|
177 |
// The first event is always a VMStartEvent, and it is always in
|
|
178 |
// an EventSet by itself. Wait for it.
|
|
179 |
EventSet evtSet = vm.eventQueue().remove();
|
|
180 |
for (Event event: evtSet) {
|
|
181 |
if (event instanceof VMStartEvent) {
|
|
182 |
break;
|
|
183 |
}
|
|
184 |
throw new RuntimeException("Test failed - debuggee did not start properly");
|
|
185 |
}
|
|
186 |
|
|
187 |
vm.eventRequestManager().deleteAllBreakpoints();
|
|
188 |
vm.resume();
|
|
189 |
|
|
190 |
process.waitFor();
|
|
191 |
}
|
|
192 |
|
|
193 |
}
|