Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/com/sun/jdi/ExclusiveBind.java
38855 views
/*1* Copyright (c) 2003, 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 453152625* @summary Test that more than one debuggee cannot bind to same port26* at the same time.27* @library /lib/testlibrary28*29* @build jdk.testlibrary.* VMConnection ExclusiveBind HelloWorld30* @run main ExclusiveBind31*/32import java.net.ServerSocket;33import com.sun.jdi.Bootstrap;34import com.sun.jdi.VirtualMachine;35import com.sun.jdi.connect.Connector;36import com.sun.jdi.connect.AttachingConnector;3738import java.util.ArrayList;39import java.util.Map;40import java.util.List;41import java.util.Iterator;42import java.util.concurrent.TimeUnit;4344import jdk.testlibrary.ProcessTools;45import jdk.testlibrary.Utils;4647public class ExclusiveBind {48/*49* Find a connector by name50*/51private static Connector findConnector(String name) {52List connectors = Bootstrap.virtualMachineManager().allConnectors();53Iterator iter = connectors.iterator();54while (iter.hasNext()) {55Connector connector = (Connector)iter.next();56if (connector.name().equals(name)) {57return connector;58}59}60return null;61}6263/*64* Launch (in server mode) a debuggee with the given address and65* suspend mode.66*/67private static ProcessBuilder prepareLauncher(String address, boolean suspend, String class_name) throws Exception {68List<String> args = new ArrayList<>();69for(String dbgOption : VMConnection.getDebuggeeVMOptions().split(" ")) {70args.add(dbgOption);71}72String lib = "-agentlib:jdwp=transport=dt_socket,server=y,suspend=";73if (suspend) {74lib += "y";75} else {76lib += "n";77}78lib += ",address=" + address;7980args.add(lib);81args.add(class_name);8283return ProcessTools.createJavaProcessBuilder(args.toArray(new String[args.size()]));84}8586/*87* - pick a TCP port88* - Launch a debuggee in server=y,suspend=y,address=${port}89* - Launch a second debuggee in server=y,suspend=n with the same port90* - Second debuggee should fail with an error (address already in use)91* - For clean-up we attach to the first debuggee and resume it.92*/93public static void main(String args[]) throws Exception {94// find a free port95ServerSocket ss = new ServerSocket(0);96int port = ss.getLocalPort();97ss.close();9899String address = String.valueOf(port);100101// launch the first debuggee102ProcessBuilder process1 = prepareLauncher(address, true, "HelloWorld");103// start the debuggee and wait for the "ready" message104Process p = ProcessTools.startProcess(105"process1",106process1,107line -> line.equals("Listening for transport dt_socket at address: " + address),108Math.round(5000 * Utils.TIMEOUT_FACTOR),109TimeUnit.MILLISECONDS110);111112// launch a second debuggee with the same address113ProcessBuilder process2 = prepareLauncher(address, false, "HelloWorld");114115// get exit status from second debuggee116int exitCode = ProcessTools.startProcess("process2", process2).waitFor();117118// clean-up - attach to first debuggee and resume it119AttachingConnector conn = (AttachingConnector)findConnector("com.sun.jdi.SocketAttach");120Map conn_args = conn.defaultArguments();121Connector.IntegerArgument port_arg =122(Connector.IntegerArgument)conn_args.get("port");123port_arg.setValue(port);124VirtualMachine vm = conn.attach(conn_args);125vm.resume();126127// if the second debuggee ran to completion then we've got a problem128if (exitCode == 0) {129throw new RuntimeException("Test failed - second debuggee didn't fail to bind");130} else {131System.out.println("Test passed - second debuggee correctly failed to bind");132}133}134}135136137