Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/com/sun/jdi/BadHandshakeTest.java
38855 views
/*1* Copyright (c) 2005, 2014, 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*/2223/* @test24* @bug 6306165 643256725* @summary Check that a bad handshake doesn't cause a debuggee to abort26* @library /lib/testlibrary27*28* @build jdk.testlibrary.* VMConnection BadHandshakeTest Exit029* @run main BadHandshakeTest30*31*/32import java.net.Socket;33import java.net.InetAddress;34import com.sun.jdi.Bootstrap;35import com.sun.jdi.VirtualMachine;36import com.sun.jdi.event.*;37import com.sun.jdi.connect.Connector;38import com.sun.jdi.connect.AttachingConnector;39import java.util.Map;40import java.util.List;41import java.util.Iterator;42import java.util.concurrent.TimeUnit;43import java.util.concurrent.atomic.AtomicBoolean;4445import jdk.testlibrary.Utils;46import jdk.testlibrary.ProcessTools;4748public class BadHandshakeTest {49/*50* Find a connector by name51*/52private static Connector findConnector(String name) {53List connectors = Bootstrap.virtualMachineManager().allConnectors();54Iterator iter = connectors.iterator();55while (iter.hasNext()) {56Connector connector = (Connector)iter.next();57if (connector.name().equals(name)) {58return connector;59}60}61return null;62}6364/*65* Launch a server debuggee with the given address66*/67private static Process launch(String address, String class_name) throws Exception {68String[] args = VMConnection.insertDebuggeeVMOptions(new String[] {69"-agentlib:jdwp=transport=dt_socket" +70",server=y" + ",suspend=y" + ",address=" + address,71class_name72});7374ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(args);7576final AtomicBoolean success = new AtomicBoolean();77Process p = ProcessTools.startProcess(78class_name,79pb,80(line) -> {81// The first thing that will get read is82// Listening for transport dt_socket at address: xxxxx83// which shows the debuggee is ready to accept connections.84success.set(line.contains("Listening for transport dt_socket at address:"));85return true;86},87Integer.MAX_VALUE,88TimeUnit.MILLISECONDS89);9091return success.get() ? p : null;92}9394/*95* - pick a TCP port96* - Launch a server debuggee: server=y,suspend=y,address=${port}97* - run it to VM death98* - verify we saw no error99*/100public static void main(String args[]) throws Exception {101int port = Utils.getFreePort();102103String address = String.valueOf(port);104105// launch the server debuggee106Process process = launch(address, "Exit0");107if (process == null) {108throw new RuntimeException("Unable to start debugee");109}110111// Connect to the debuggee and handshake with garbage112Socket s = new Socket(InetAddress.getLocalHost(), port);113s.getOutputStream().write("Here's a poke in the eye".getBytes("UTF-8"));114s.close();115116// Re-connect and to a partial handshake - don't disconnect117s = new Socket(InetAddress.getLocalHost(), port);118s.getOutputStream().write("JDWP-".getBytes("UTF-8"));119120121// attach to server debuggee and resume it so it can exit122AttachingConnector conn = (AttachingConnector)findConnector("com.sun.jdi.SocketAttach");123Map conn_args = conn.defaultArguments();124Connector.IntegerArgument port_arg =125(Connector.IntegerArgument)conn_args.get("port");126port_arg.setValue(port);127VirtualMachine vm = conn.attach(conn_args);128129// The first event is always a VMStartEvent, and it is always in130// an EventSet by itself. Wait for it.131EventSet evtSet = vm.eventQueue().remove();132for (Event event: evtSet) {133if (event instanceof VMStartEvent) {134break;135}136throw new RuntimeException("Test failed - debuggee did not start properly");137}138139vm.eventRequestManager().deleteAllBreakpoints();140vm.resume();141142process.waitFor();143}144145}146147148