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