1 /*
   2  * Copyright (c) 2003, 2018, 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  * @library /test/lib
  29  *
  30  * @build ExclusiveBind HelloWorld
  31  * @run driver ExclusiveBind
  32  */
  33 
  34 import jdk.test.lib.process.ProcessTools;
  35 import lib.jdb.Debuggee;
  36 
  37 public class ExclusiveBind {
  38     /*
  39      * - Launch a debuggee with server=y,suspend=y
  40      * - Parse listening port
  41      * - Launch a second debuggee in server=y,suspend=n with the parsed port
  42      * - Second debuggee should fail with an error (address already in use)
  43      * - For clean-up we attach to the first debuggee and resume it.
  44      */
  45     public static void main(String args[]) throws Exception {
  46         // launch the first debuggee
  47         try (Debuggee process1 = Debuggee.launcher("HelloWorld").launch("process1")) {
  48             // launch a second debuggee with the same address
  49             ProcessBuilder process2 = Debuggee.launcher("HelloWorld")
  50                     .setSuspended(false)
  51                     .setAddress(process1.getAddress())
  52                     .prepare();
  53 
  54             // get exit status from second debuggee
  55             int exitCode = ProcessTools.startProcess("process2", process2).waitFor();
  56 
  57             // if the second debuggee ran to completion then we've got a problem
  58             if (exitCode == 0) {
  59                 throw new RuntimeException("Test failed - second debuggee didn't fail to bind");
  60             } else {
  61                 System.out.println("Test passed - second debuggee correctly failed to bind");
  62             }
  63         }
  64     }
  65 }