< prev index next >

test/jdk/com/sun/jdi/ExclusiveBind.java

Print this page




  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  * @library /test/lib
  29  *
  30  * @modules java.management
  31  *          jdk.jdi
  32  * @build VMConnection ExclusiveBind HelloWorld
  33  * @run driver ExclusiveBind
  34  */
  35 import java.net.ServerSocket;
  36 import com.sun.jdi.Bootstrap;
  37 import com.sun.jdi.VirtualMachine;
  38 import com.sun.jdi.connect.Connector;
  39 import com.sun.jdi.connect.AttachingConnector;
  40 
  41 import java.util.ArrayList;
  42 import java.util.Map;
  43 import java.util.List;
  44 import java.util.Iterator;
  45 import java.util.concurrent.TimeUnit;


  46 
  47 import jdk.test.lib.process.ProcessTools;
  48 import jdk.test.lib.Utils;
  49 
  50 public class ExclusiveBind {
  51     /*
  52      * Find a connector by name
  53      */
  54     private static Connector findConnector(String name) {
  55         List connectors = Bootstrap.virtualMachineManager().allConnectors();
  56         Iterator iter = connectors.iterator();
  57         while (iter.hasNext()) {
  58             Connector connector = (Connector)iter.next();
  59             if (connector.name().equals(name)) {
  60                 return connector;
  61             }
  62         }
  63         return null;
  64     }
  65 


  72         for(String dbgOption : VMConnection.getDebuggeeVMOptions().split(" ")) {
  73             if (!dbgOption.trim().isEmpty()) {
  74                 args.add(dbgOption);
  75             }
  76         }
  77         String lib = "-agentlib:jdwp=transport=dt_socket,server=y,suspend=";
  78         if (suspend) {
  79             lib += "y";
  80         } else {
  81             lib += "n";
  82         }
  83         lib += ",address=" + address;
  84 
  85         args.add(lib);
  86         args.add(class_name);
  87 
  88         return ProcessTools.createJavaProcessBuilder(args.toArray(new String[args.size()]));
  89     }
  90 
  91     /*
  92      * - pick a TCP port
  93      * - Launch a debuggee in server=y,suspend=y,address=${port}
  94      * - Launch a second debuggee in server=y,suspend=n with the same port
  95      * - Second debuggee should fail with an error (address already in use)
  96      * - For clean-up we attach to the first debuggee and resume it.
  97      */
  98     public static void main(String args[]) throws Exception {
  99         // find a free port
 100         ServerSocket ss = new ServerSocket(0);
 101         int port = ss.getLocalPort();
 102         ss.close();
 103 
 104         String address = String.valueOf(port);
 105 
 106         // launch the first debuggee
 107         ProcessBuilder process1 = prepareLauncher(address, true, "HelloWorld");
 108         // start the debuggee and wait for the "ready" message



 109         Process p = ProcessTools.startProcess(
 110                 "process1",
 111                 process1,
 112                 line -> line.equals("Listening for transport dt_socket at address: " + address),
 113                 Utils.adjustTimeout(5000),
 114                 TimeUnit.MILLISECONDS









 115         );
 116 


 117         // launch a second debuggee with the same address
 118         ProcessBuilder process2 = prepareLauncher(address, false, "HelloWorld");
 119 
 120         // get exit status from second debuggee
 121         int exitCode = ProcessTools.startProcess("process2", process2).waitFor();
 122 
 123         // clean-up - attach to first debuggee and resume it
 124         AttachingConnector conn = (AttachingConnector)findConnector("com.sun.jdi.SocketAttach");
 125         Map conn_args = conn.defaultArguments();
 126         Connector.IntegerArgument port_arg =
 127             (Connector.IntegerArgument)conn_args.get("port");
 128         port_arg.setValue(port);
 129         VirtualMachine vm = conn.attach(conn_args);
 130         vm.resume();
 131 
 132         // if the second debuggee ran to completion then we've got a problem
 133         if (exitCode == 0) {
 134             throw new RuntimeException("Test failed - second debuggee didn't fail to bind");
 135         } else {
 136             System.out.println("Test passed - second debuggee correctly failed to bind");


  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  * @library /test/lib
  29  *
  30  * @modules java.management
  31  *          jdk.jdi
  32  * @build VMConnection ExclusiveBind HelloWorld
  33  * @run driver ExclusiveBind
  34  */

  35 import com.sun.jdi.Bootstrap;
  36 import com.sun.jdi.VirtualMachine;
  37 import com.sun.jdi.connect.Connector;
  38 import com.sun.jdi.connect.AttachingConnector;
  39 
  40 import java.util.ArrayList;
  41 import java.util.Map;
  42 import java.util.List;
  43 import java.util.Iterator;
  44 import java.util.concurrent.TimeUnit;
  45 import java.util.regex.Matcher;
  46 import java.util.regex.Pattern;
  47 
  48 import jdk.test.lib.process.ProcessTools;
  49 import jdk.test.lib.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 


  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      * - Launch a debuggee in server=y,suspend=y,address=0
  94      * - Parse listening port
  95      * - Launch a second debuggee in server=y,suspend=n with the parsed 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         // launch the first debuggee
 101         ProcessBuilder process1 = prepareLauncher("0", true, "HelloWorld");
 102         // start the debuggee and wait for the "ready" message
 103         Pattern listenRegexp = Pattern.compile("Listening for transport \\b(.+)\\b at address: \\b(\\d+)\\b");
 104         // debuggeeListen[0] - transport, debuggeeListen[1] - address
 105         String[] debuggeeListen = new String[2];
 106         Process p = ProcessTools.startProcess(
 107                 "process1",
 108                 process1,
 109                 line -> {  // warm-up predicate
 110                     Matcher m = listenRegexp.matcher(line);
 111                     if (!m.matches()) {
 112                         return false;
 113                     }
 114                     debuggeeListen[0] = m.group(1);
 115                     debuggeeListen[1] = m.group(2);
 116                     return true;
 117                 },
 118 
 119                 Utils.adjustTimeout(30),
 120                 TimeUnit.SECONDS
 121         );
 122 
 123         String address = debuggeeListen[1];
 124         int port = Integer.parseInt(address);
 125         // launch a second debuggee with the same address
 126         ProcessBuilder process2 = prepareLauncher(address, false, "HelloWorld");
 127 
 128         // get exit status from second debuggee
 129         int exitCode = ProcessTools.startProcess("process2", process2).waitFor();
 130 
 131         // clean-up - attach to first debuggee and resume it
 132         AttachingConnector conn = (AttachingConnector)findConnector("com.sun.jdi.SocketAttach");
 133         Map conn_args = conn.defaultArguments();
 134         Connector.IntegerArgument port_arg =
 135             (Connector.IntegerArgument)conn_args.get("port");
 136         port_arg.setValue(port);
 137         VirtualMachine vm = conn.attach(conn_args);
 138         vm.resume();
 139 
 140         // if the second debuggee ran to completion then we've got a problem
 141         if (exitCode == 0) {
 142             throw new RuntimeException("Test failed - second debuggee didn't fail to bind");
 143         } else {
 144             System.out.println("Test passed - second debuggee correctly failed to bind");
< prev index next >