author | ohair |
Wed, 25 Nov 2009 08:24:58 -0800 | |
changeset 4335 | 365eb4449319 |
parent 2 | 90ce3da70b43 |
child 4342 | 9f3aa1172803 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
2 |
* Copyright 2003-2006 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
20 |
* CA 95054 USA or visit www.sun.com if you need additional information or |
|
21 |
* have any 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 |
* @build VMConnection ExclusiveBind HelloWorld |
|
30 |
* @run main ExclusiveBind |
|
31 |
*/ |
|
32 |
import java.io.InputStream; |
|
33 |
import java.io.IOException; |
|
34 |
import java.io.File; |
|
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 |
import java.util.Map; |
|
41 |
import java.util.List; |
|
42 |
import java.util.Iterator; |
|
43 |
||
44 |
public class ExclusiveBind { |
|
45 |
||
46 |
/* |
|
47 |
* Helper class to direct process output to the parent |
|
48 |
* System.out |
|
49 |
*/ |
|
50 |
static class IOHandler implements Runnable { |
|
51 |
InputStream in; |
|
52 |
||
53 |
IOHandler(InputStream in) { |
|
54 |
this.in = in; |
|
55 |
} |
|
56 |
||
57 |
static void handle(InputStream in) { |
|
58 |
IOHandler handler = new IOHandler(in); |
|
59 |
Thread thr = new Thread(handler); |
|
60 |
thr.setDaemon(true); |
|
61 |
thr.start(); |
|
62 |
} |
|
63 |
||
64 |
public void run() { |
|
65 |
try { |
|
66 |
byte b[] = new byte[100]; |
|
67 |
for (;;) { |
|
68 |
int n = in.read(b); |
|
69 |
if (n < 0) return; |
|
70 |
for (int i=0; i<n; i++) { |
|
71 |
System.out.print((char)b[i]); |
|
72 |
} |
|
73 |
} |
|
74 |
} catch (IOException ioe) { } |
|
75 |
} |
|
76 |
||
77 |
} |
|
78 |
||
79 |
/* |
|
80 |
* Find a connector by name |
|
81 |
*/ |
|
82 |
private static Connector findConnector(String name) { |
|
83 |
List connectors = Bootstrap.virtualMachineManager().allConnectors(); |
|
84 |
Iterator iter = connectors.iterator(); |
|
85 |
while (iter.hasNext()) { |
|
86 |
Connector connector = (Connector)iter.next(); |
|
87 |
if (connector.name().equals(name)) { |
|
88 |
return connector; |
|
89 |
} |
|
90 |
} |
|
91 |
return null; |
|
92 |
} |
|
93 |
||
94 |
/* |
|
95 |
* Launch (in server mode) a debuggee with the given address and |
|
96 |
* suspend mode. |
|
97 |
*/ |
|
98 |
private static Process launch(String address, boolean suspend, String class_name) throws IOException { |
|
99 |
String exe = System.getProperty("java.home") + File.separator + "bin" + |
|
100 |
File.separator; |
|
101 |
String arch = System.getProperty("os.arch"); |
|
102 |
if (arch.equals("sparcv9")) { |
|
103 |
exe += "sparcv9/java"; |
|
4335
365eb4449319
6904183: Fix jdk/test/com/sun/jdi tests to run with -samevm
ohair
parents:
2
diff
changeset
|
104 |
} else if (arch.equals("amd64")) { |
365eb4449319
6904183: Fix jdk/test/com/sun/jdi tests to run with -samevm
ohair
parents:
2
diff
changeset
|
105 |
exe += "amd64/java"; |
2 | 106 |
} else { |
107 |
exe += "java"; |
|
108 |
} |
|
109 |
String cmd = exe + " " + VMConnection.getDebuggeeVMOptions() + |
|
110 |
" -agentlib:jdwp=transport=dt_socket,server=y,suspend="; |
|
111 |
if (suspend) { |
|
112 |
cmd += "y"; |
|
113 |
} else { |
|
114 |
cmd += "n"; |
|
115 |
} |
|
116 |
cmd += ",address=" + address + " " + class_name; |
|
117 |
||
118 |
System.out.println("Starting: " + cmd); |
|
119 |
||
120 |
Process p = Runtime.getRuntime().exec(cmd); |
|
121 |
IOHandler.handle(p.getInputStream()); |
|
122 |
IOHandler.handle(p.getErrorStream()); |
|
123 |
||
124 |
return p; |
|
125 |
} |
|
126 |
||
127 |
/* |
|
128 |
* - pick a TCP port |
|
129 |
* - Launch a debuggee in server=y,suspend=y,address=${port} |
|
130 |
* - Launch a second debuggee in server=y,suspend=n with the same port |
|
131 |
* - Second debuggee should fail with an error (address already in use) |
|
132 |
* - For clean-up we attach to the first debuggee and resume it. |
|
133 |
*/ |
|
134 |
public static void main(String args[]) throws Exception { |
|
135 |
// find a free port |
|
136 |
ServerSocket ss = new ServerSocket(0); |
|
137 |
int port = ss.getLocalPort(); |
|
138 |
ss.close(); |
|
139 |
||
140 |
String address = String.valueOf(port); |
|
141 |
||
142 |
// launch the first debuggee |
|
143 |
Process process1 = launch(address, true, "HelloWorld"); |
|
144 |
||
145 |
// give first debuggee time to suspend |
|
146 |
Thread.currentThread().sleep(5000); |
|
147 |
||
148 |
// launch a second debuggee with the same address |
|
149 |
Process process2 = launch(address, false, "HelloWorld"); |
|
150 |
||
151 |
// get exit status from second debuggee |
|
152 |
int exitCode = process2.waitFor(); |
|
153 |
||
154 |
// clean-up - attach to first debuggee and resume it |
|
155 |
AttachingConnector conn = (AttachingConnector)findConnector("com.sun.jdi.SocketAttach"); |
|
156 |
Map conn_args = conn.defaultArguments(); |
|
157 |
Connector.IntegerArgument port_arg = |
|
158 |
(Connector.IntegerArgument)conn_args.get("port"); |
|
159 |
port_arg.setValue(port); |
|
160 |
VirtualMachine vm = conn.attach(conn_args); |
|
161 |
vm.resume(); |
|
162 |
||
163 |
// if the second debuggee ran to completion then we've got a problem |
|
164 |
if (exitCode == 0) { |
|
165 |
throw new RuntimeException("Test failed - second debuggee didn't fail to bind"); |
|
166 |
} else { |
|
167 |
System.out.println("Test passed - second debuggee correctly failed to bind"); |
|
168 |
} |
|
169 |
} |
|
170 |
} |