1 /* 2 * Copyright (c) 2005, 2013, 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 /* @test 25 * @bug 6306165 6432567 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 + "java"; 112 String cmd = exe + " " + VMConnection.getDebuggeeVMOptions() + 113 " -agentlib:jdwp=transport=dt_socket" + 114 ",server=y" + ",suspend=y" + ",address=" + address + 115 " " + class_name; 116 117 System.out.println("Starting: " + cmd); 118 119 Process p = Runtime.getRuntime().exec(cmd); 120 121 IOHandler.handle(p.getInputStream()); 122 IOHandler.handle(p.getErrorStream()); 123 124 return p; 125 } 126 127 /* 128 * - pick a TCP port 129 * - Launch a server debuggee: server=y,suspend=y,address=${port} 130 * - run it to VM death 131 * - verify we saw no error 132 */ 133 public static void main(String args[]) throws Exception { 134 // find a free port 135 ServerSocket ss = new ServerSocket(0); 136 int port = ss.getLocalPort(); 137 ss.close(); 138 139 String address = String.valueOf(port); 140 141 // launch the server debuggee 142 Process process = launch(address, "Exit0"); 143 144 // wait for the debugge to be ready 145 while (!ready) { 146 try { 147 Thread.sleep(1000); 148 } catch(Exception ee) { 149 throw ee; 150 } 151 } 152 153 // Connect to the debuggee and handshake with garbage 154 Socket s = new Socket(InetAddress.getLocalHost(), port); 155 s.getOutputStream().write("Here's a poke in the eye".getBytes("UTF-8")); 156 s.close(); 157 158 // Re-connect and to a partial handshake - don't disconnect 159 s = new Socket(InetAddress.getLocalHost(), port); 160 s.getOutputStream().write("JDWP-".getBytes("UTF-8")); 161 162 163 // attach to server debuggee and resume it so it can exit 164 AttachingConnector conn = (AttachingConnector)findConnector("com.sun.jdi.SocketAttach"); 165 Map conn_args = conn.defaultArguments(); 166 Connector.IntegerArgument port_arg = 167 (Connector.IntegerArgument)conn_args.get("port"); 168 port_arg.setValue(port); 169 VirtualMachine vm = conn.attach(conn_args); 170 171 // The first event is always a VMStartEvent, and it is always in 172 // an EventSet by itself. Wait for it. 173 EventSet evtSet = vm.eventQueue().remove(); 174 for (Event event: evtSet) { 175 if (event instanceof VMStartEvent) { 176 break; 177 } 178 throw new RuntimeException("Test failed - debuggee did not start properly"); 179 } 180 181 vm.eventRequestManager().deleteAllBreakpoints(); 182 vm.resume(); 183 184 process.waitFor(); 185 } 186 187 }