PageRenderTime 27ms CodeModel.GetById 7ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/ikeji/openjdk7-jdk
Java | 171 lines | 96 code | 20 blank | 55 comment | 16 complexity | 1caca1e0c48bc82dab6253105b852364 MD5 | raw file
  1. /*
  2. * Copyright (c) 2003, 2006, 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. /* @test
  24. * @bug 4531526
  25. * @summary Test that more than one debuggee cannot bind to same port
  26. * at the same time.
  27. *
  28. * @build VMConnection ExclusiveBind HelloWorld
  29. * @run main ExclusiveBind
  30. */
  31. import java.io.InputStream;
  32. import java.io.IOException;
  33. import java.io.File;
  34. import java.net.ServerSocket;
  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. import java.util.Map;
  40. import java.util.List;
  41. import java.util.Iterator;
  42. public class ExclusiveBind {
  43. /*
  44. * Helper class to direct process output to the parent
  45. * System.out
  46. */
  47. static class IOHandler implements Runnable {
  48. InputStream in;
  49. IOHandler(InputStream in) {
  50. this.in = in;
  51. }
  52. static void handle(InputStream in) {
  53. IOHandler handler = new IOHandler(in);
  54. Thread thr = new Thread(handler);
  55. thr.setDaemon(true);
  56. thr.start();
  57. }
  58. public void run() {
  59. try {
  60. byte b[] = new byte[100];
  61. for (;;) {
  62. int n = in.read(b);
  63. if (n < 0) return;
  64. for (int i=0; i<n; i++) {
  65. System.out.print((char)b[i]);
  66. }
  67. }
  68. } catch (IOException ioe) { }
  69. }
  70. }
  71. /*
  72. * Find a connector by name
  73. */
  74. private static Connector findConnector(String name) {
  75. List connectors = Bootstrap.virtualMachineManager().allConnectors();
  76. Iterator iter = connectors.iterator();
  77. while (iter.hasNext()) {
  78. Connector connector = (Connector)iter.next();
  79. if (connector.name().equals(name)) {
  80. return connector;
  81. }
  82. }
  83. return null;
  84. }
  85. /*
  86. * Launch (in server mode) a debuggee with the given address and
  87. * suspend mode.
  88. */
  89. private static Process launch(String address, boolean suspend, String class_name) throws IOException {
  90. String exe = System.getProperty("java.home") + File.separator + "bin" +
  91. File.separator;
  92. String arch = System.getProperty("os.arch");
  93. String osname = System.getProperty("os.name");
  94. if (osname.equals("SunOS") && arch.equals("sparcv9")) {
  95. exe += "sparcv9/java";
  96. } else if (osname.equals("SunOS") && arch.equals("amd64")) {
  97. exe += "amd64/java";
  98. } else {
  99. exe += "java";
  100. }
  101. String cmd = exe + " " + VMConnection.getDebuggeeVMOptions() +
  102. " -agentlib:jdwp=transport=dt_socket,server=y,suspend=";
  103. if (suspend) {
  104. cmd += "y";
  105. } else {
  106. cmd += "n";
  107. }
  108. cmd += ",address=" + address + " " + class_name;
  109. System.out.println("Starting: " + cmd);
  110. Process p = Runtime.getRuntime().exec(cmd);
  111. IOHandler.handle(p.getInputStream());
  112. IOHandler.handle(p.getErrorStream());
  113. return p;
  114. }
  115. /*
  116. * - pick a TCP port
  117. * - Launch a debuggee in server=y,suspend=y,address=${port}
  118. * - Launch a second debuggee in server=y,suspend=n with the same port
  119. * - Second debuggee should fail with an error (address already in use)
  120. * - For clean-up we attach to the first debuggee and resume it.
  121. */
  122. public static void main(String args[]) throws Exception {
  123. // find a free port
  124. ServerSocket ss = new ServerSocket(0);
  125. int port = ss.getLocalPort();
  126. ss.close();
  127. String address = String.valueOf(port);
  128. // launch the first debuggee
  129. Process process1 = launch(address, true, "HelloWorld");
  130. // give first debuggee time to suspend
  131. Thread.currentThread().sleep(5000);
  132. // launch a second debuggee with the same address
  133. Process process2 = launch(address, false, "HelloWorld");
  134. // get exit status from second debuggee
  135. int exitCode = process2.waitFor();
  136. // clean-up - attach to first debuggee and resume it
  137. AttachingConnector conn = (AttachingConnector)findConnector("com.sun.jdi.SocketAttach");
  138. Map conn_args = conn.defaultArguments();
  139. Connector.IntegerArgument port_arg =
  140. (Connector.IntegerArgument)conn_args.get("port");
  141. port_arg.setValue(port);
  142. VirtualMachine vm = conn.attach(conn_args);
  143. vm.resume();
  144. // if the second debuggee ran to completion then we've got a problem
  145. if (exitCode == 0) {
  146. throw new RuntimeException("Test failed - second debuggee didn't fail to bind");
  147. } else {
  148. System.out.println("Test passed - second debuggee correctly failed to bind");
  149. }
  150. }
  151. }