1 /*
   2  * Copyright (c) 2003, 2015, 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 4531526
  26  * @summary Test that more than one debuggee cannot bind to same port
  27  *          at the same time.
  28  *
  29  * @library /lib/testlibrary
  30  * @modules java.management
  31  *          jdk.jdi
  32  *
  33  * @build jdk.testlibrary.* VMConnection ExclusiveBind HelloWorld
  34  * @run driver ExclusiveBind
  35  */
  36 import java.net.ServerSocket;
  37 import com.sun.jdi.Bootstrap;
  38 import com.sun.jdi.VirtualMachine;
  39 import com.sun.jdi.connect.Connector;
  40 import com.sun.jdi.connect.AttachingConnector;
  41 
  42 import java.util.ArrayList;
  43 import java.util.Map;
  44 import java.util.List;
  45 import java.util.Iterator;
  46 import java.util.concurrent.TimeUnit;
  47 
  48 import jdk.testlibrary.ProcessTools;
  49 import jdk.testlibrary.Utils;
  50 
  51 public class ExclusiveBind {
  52     /*
  53      * Find a connector by name
  54      */
  55     private static Connector findConnector(String name) {
  56         List connectors = Bootstrap.virtualMachineManager().allConnectors();
  57         Iterator iter = connectors.iterator();
  58         while (iter.hasNext()) {
  59             Connector connector = (Connector)iter.next();
  60             if (connector.name().equals(name)) {
  61                 return connector;
  62             }
  63         }
  64         return null;
  65     }
  66 
  67     /*
  68      * Launch (in server mode) a debuggee with the given address and
  69      * suspend mode.
  70      */
  71     private static ProcessBuilder prepareLauncher(String address, boolean suspend, String class_name) throws Exception {
  72         List<String> args = new ArrayList<>();
  73         for(String dbgOption : VMConnection.getDebuggeeVMOptions().split(" ")) {
  74             if (!dbgOption.trim().isEmpty()) {
  75                 args.add(dbgOption);
  76             }
  77         }
  78         String lib = "-agentlib:jdwp=transport=dt_socket,server=y,suspend=";
  79         if (suspend) {
  80             lib += "y";
  81         } else {
  82             lib += "n";
  83         }
  84         lib += ",address=" + address;
  85 
  86         args.add(lib);
  87         args.add(class_name);
  88 
  89         return ProcessTools.createJavaProcessBuilder(args.toArray(new String[args.size()]));
  90     }
  91 
  92     /*
  93      * - pick a TCP port
  94      * - Launch a debuggee in server=y,suspend=y,address=${port}
  95      * - Launch a second debuggee in server=y,suspend=n with the same port
  96      * - Second debuggee should fail with an error (address already in use)
  97      * - For clean-up we attach to the first debuggee and resume it.
  98      */
  99     public static void main(String args[]) throws Exception {
 100         // find a free port
 101         ServerSocket ss = new ServerSocket(0);
 102         int port = ss.getLocalPort();
 103         ss.close();
 104 
 105         String address = String.valueOf(port);
 106 
 107         // launch the first debuggee
 108         ProcessBuilder process1 = prepareLauncher(address, true, "HelloWorld");
 109         // start the debuggee and wait for the "ready" message
 110         Process p = ProcessTools.startProcess(
 111                 "process1",
 112                 process1,
 113                 line -> line.equals("Listening for transport dt_socket at address: " + address),
 114                 Utils.adjustTimeout(5000),
 115                 TimeUnit.MILLISECONDS
 116         );
 117 
 118         // launch a second debuggee with the same address
 119         ProcessBuilder process2 = prepareLauncher(address, false, "HelloWorld");
 120 
 121         // get exit status from second debuggee
 122         int exitCode = ProcessTools.startProcess("process2", process2).waitFor();
 123 
 124         // clean-up - attach to first debuggee and resume it
 125         AttachingConnector conn = (AttachingConnector)findConnector("com.sun.jdi.SocketAttach");
 126         Map conn_args = conn.defaultArguments();
 127         Connector.IntegerArgument port_arg =
 128             (Connector.IntegerArgument)conn_args.get("port");
 129         port_arg.setValue(port);
 130         VirtualMachine vm = conn.attach(conn_args);
 131         vm.resume();
 132 
 133         // if the second debuggee ran to completion then we've got a problem
 134         if (exitCode == 0) {
 135             throw new RuntimeException("Test failed - second debuggee didn't fail to bind");
 136         } else {
 137             System.out.println("Test passed - second debuggee correctly failed to bind");
 138         }
 139     }
 140 }