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