BadHandshakeTest.java revision 0
1245N/A/*
2362N/A * Copyright 2005-2006 Sun Microsystems, Inc. All Rights Reserved.
1245N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1245N/A *
1245N/A * This code is free software; you can redistribute it and/or modify it
1245N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation.
1245N/A *
2362N/A * This code is distributed in the hope that it will be useful, but WITHOUT
1245N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1245N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1245N/A * version 2 for more details (a copy is included in the LICENSE file that
1245N/A * accompanied this code).
1245N/A *
1245N/A * You should have received a copy of the GNU General Public License version
1245N/A * 2 along with this work; if not, write to the Free Software Foundation,
1245N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1245N/A *
1245N/A * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
1245N/A * CA 95054 USA or visit www.sun.com if you need additional information or
2362N/A * have any questions.
2362N/A */
2362N/A
1245N/A/* @test
1245N/A * @bug 6306165
1245N/A * @summary Check that a bad handshake doesn't cause a debuggee to abort
1245N/A *
1245N/A * @build VMConnection BadHandshakeTest Exit0
1245N/A * @run main BadHandshakeTest
1245N/A *
1245N/A */
1245N/Aimport java.io.InputStream;
1245N/Aimport java.io.IOException;
1245N/Aimport java.io.File;
1245N/Aimport java.io.BufferedInputStream;
1245N/Aimport java.net.ServerSocket;
1245N/Aimport java.net.Socket;
1245N/Aimport java.net.InetAddress;
1245N/Aimport com.sun.jdi.Bootstrap;
1245N/Aimport com.sun.jdi.VirtualMachine;
1245N/Aimport com.sun.jdi.event.*;
1245N/Aimport com.sun.jdi.connect.Connector;
1245N/Aimport com.sun.jdi.connect.AttachingConnector;
1245N/Aimport java.util.Map;
1245N/Aimport java.util.List;
1245N/Aimport java.util.Iterator;
1245N/A
1245N/Apublic class BadHandshakeTest {
1245N/A
1245N/A static volatile boolean ready = false;
1245N/A
1245N/A /*
1245N/A * Helper class to redirect process output/error
1245N/A */
1245N/A static class IOHandler implements Runnable {
1245N/A InputStream in;
1245N/A
1245N/A IOHandler(InputStream in) {
1245N/A this.in = in;
1245N/A }
1245N/A
1245N/A static void handle(InputStream in) {
1245N/A IOHandler handler = new IOHandler(in);
1245N/A Thread thr = new Thread(handler);
1245N/A thr.setDaemon(true);
1245N/A thr.start();
1245N/A }
1245N/A
1245N/A public void run() {
1245N/A try {
1245N/A byte b[] = new byte[100];
1245N/A for (;;) {
1245N/A int n = in.read(b, 0, 100);
1245N/A // The first thing that will get read is
1245N/A // Listening for transport dt_socket at address: xxxxx
1245N/A // which shows the debuggee is ready to accept connections.
1245N/A ready = true;
1245N/A if (n < 0) {
1245N/A break;
1245N/A }
1245N/A String s = new String(b, 0, n, "UTF-8");
1245N/A System.out.print(s);
1245N/A }
1245N/A } catch (IOException ioe) {
1245N/A ioe.printStackTrace();
1245N/A }
1245N/A }
1245N/A
1245N/A }
1245N/A
1245N/A /*
1245N/A * Find a connector by name
1245N/A */
1245N/A private static Connector findConnector(String name) {
1245N/A List connectors = Bootstrap.virtualMachineManager().allConnectors();
1245N/A Iterator iter = connectors.iterator();
1245N/A while (iter.hasNext()) {
1245N/A Connector connector = (Connector)iter.next();
1245N/A if (connector.name().equals(name)) {
1245N/A return connector;
1245N/A }
1245N/A }
1245N/A return null;
}
/*
* Launch a server debuggee with the given address
*/
private static Process launch(String address, String class_name) throws IOException {
String exe = System.getProperty("java.home")
+ File.separator + "bin" + File.separator;
String arch = System.getProperty("os.arch");
if (arch.equals("sparcv9")) {
exe += "sparcv9/java";
} else {
exe += "java";
}
String cmd = exe + " " + VMConnection.getDebuggeeVMOptions() +
" -agentlib:jdwp=transport=dt_socket" +
",server=y" + ",suspend=y" + ",address=" + address +
" " + class_name;
System.out.println("Starting: " + cmd);
Process p = Runtime.getRuntime().exec(cmd);
IOHandler.handle(p.getInputStream());
IOHandler.handle(p.getErrorStream());
return p;
}
/*
* - pick a TCP port
* - Launch a server debuggee: server=y,suspend=y,address=${port}
* - run it to VM death
* - verify we saw no error
*/
public static void main(String args[]) throws Exception {
// find a free port
ServerSocket ss = new ServerSocket(0);
int port = ss.getLocalPort();
ss.close();
String address = String.valueOf(port);
// launch the server debuggee
Process process = launch(address, "Exit0");
// wait for the debugge to be ready
while (!ready) {
try {
Thread.sleep(1000);
} catch(Exception ee) {
throw ee;
}
}
// Connect to the debuggee and handshake with garbage
Socket s = new Socket(InetAddress.getLocalHost(), port);
s.getOutputStream().write("Here's a poke in the eye".getBytes("UTF-8"));
s.close();
// Re-connect and to a partial handshake - don't disconnect
s = new Socket(InetAddress.getLocalHost(), port);
s.getOutputStream().write("JDWP-".getBytes("UTF-8"));
// attach to server debuggee and resume it so it can exit
AttachingConnector conn = (AttachingConnector)findConnector("com.sun.jdi.SocketAttach");
Map conn_args = conn.defaultArguments();
Connector.IntegerArgument port_arg =
(Connector.IntegerArgument)conn_args.get("port");
port_arg.setValue(port);
VirtualMachine vm = conn.attach(conn_args);
// The first event is always a VMStartEvent, and it is always in
// an EventSet by itself. Wait for it.
EventSet evtSet = vm.eventQueue().remove();
for (Event event: evtSet) {
if (event instanceof VMStartEvent) {
break;
}
throw new RuntimeException("Test failed - debuggee did not start properly");
}
vm.eventRequestManager().deleteAllBreakpoints();
vm.resume();
process.waitFor();
}
}