Path: blob/master/test/jdk/com/sun/jdi/BadHandshakeTest.java
41152 views
/*1* Copyright (c) 2005, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.net.ConnectException;24import java.net.Socket;2526import com.sun.jdi.Bootstrap;27import com.sun.jdi.VirtualMachine;28import com.sun.jdi.event.*;29import com.sun.jdi.connect.Connector;30import com.sun.jdi.connect.AttachingConnector;31import com.sun.jdi.connect.Connector.Argument;3233import java.util.Map;34import java.util.List;35import java.util.Iterator;36import java.util.concurrent.TimeUnit;3738import lib.jdb.Debuggee;3940/* @test41* @bug 6306165 643256742* @summary Check that a bad handshake doesn't cause a debuggee to abort43* @library /test/lib44*45* @modules java.management46* jdk.jdi47* @build BadHandshakeTest Exit048* @run driver BadHandshakeTest49*/50public class BadHandshakeTest {5152/*53* Find a connector by name54*/55private static Connector findConnector(String name) {56List<Connector> connectors = Bootstrap.virtualMachineManager().allConnectors();57Iterator<Connector> iter = connectors.iterator();58while (iter.hasNext()) {59Connector connector = iter.next();60if (connector.name().equals(name)) {61return connector;62}63}64return null;65}6667private static void log(Object s) {68System.out.println(String.valueOf(s));69}7071public static void main(String args[]) throws Exception {72// Launch the server debugee73log("Starting debuggee...");74try (Debuggee debuggee = Debuggee.launcher("Exit0").launch()) {75log("Debuggee started.");76int port = Integer.parseInt(debuggee.getAddress());77log("Debuggee port: " + port);7879log("testcase 1...");80// Connect to the debuggee and handshake with garbage81Socket s = new Socket("localhost", port);82s.getOutputStream().write("Here's a poke in the eye".getBytes("UTF-8"));83s.close();8485log("testcase 2...");86// Re-connect and do a partial handshake - don't disconnect87// Re-connect just after disconnect may cause "connection refused" error (see JDK-8192057)88Exception error = null;89long retryDelay = 20;90for (int retry = 0; retry < 5; retry++) {91if (error != null) {92try {93Thread.sleep(retryDelay);94} catch (InterruptedException ex) {95// ignore96}97retryDelay *= 2;98error = null;99}100try {101log("retry: " + retry);102s = new Socket("localhost", port);103s.getOutputStream().write("JDWP-".getBytes("UTF-8"));104break;105} catch (ConnectException ex) {106log("got exception: " + ex.toString());107error = ex;108}109}110if (error != null) {111throw error;112}113114log("final attach...");115// Attach to server debuggee to ensure it's still available to attach and resume it so it can exit116AttachingConnector conn = (AttachingConnector)findConnector("com.sun.jdi.SocketAttach");117retryDelay = 20;118for (int retry = 0; retry < 5; retry++) {119if (error != null) {120try {121Thread.sleep(retryDelay);122} catch (InterruptedException ex) {123// ignore124}125retryDelay *= 2;126error = null;127}128try {129log("retry: " + retry);130Map<String, Argument> conn_args = conn.defaultArguments();131Connector.IntegerArgument port_arg =132(Connector.IntegerArgument)conn_args.get("port");133port_arg.setValue(port);134VirtualMachine vm = conn.attach(conn_args);135136// The first event is always a VMStartEvent, and it is always in137// an EventSet by itself. Wait for it.138EventSet evtSet = vm.eventQueue().remove();139for (Event event : evtSet) {140if (event instanceof VMStartEvent) {141break;142}143throw new RuntimeException("Test failed - debuggee did not start properly");144}145146vm.eventRequestManager().deleteAllBreakpoints();147vm.resume();148break;149} catch (ConnectException ex) {150log("got exception: " + ex.toString());151error = ex;152}153}154if (error != null) {155throw error;156}157158// give the debuggee some time to exit before forcibly terminating it159debuggee.waitFor(10, TimeUnit.SECONDS);160}161}162}163164165