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