54884
|
1 |
/*
|
|
2 |
* Copyright (c) 2019, 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 |
import com.sun.jdi.Bootstrap;
|
|
25 |
import com.sun.jdi.VirtualMachine;
|
|
26 |
import com.sun.jdi.connect.Connector;
|
|
27 |
import com.sun.jdi.connect.ListeningConnector;
|
|
28 |
import jdk.test.lib.apps.LingeredApp;
|
|
29 |
|
|
30 |
import java.net.Inet4Address;
|
|
31 |
import java.net.Inet6Address;
|
|
32 |
import java.net.InetAddress;
|
|
33 |
import java.net.NetworkInterface;
|
|
34 |
import java.net.SocketException;
|
|
35 |
import java.util.Arrays;
|
|
36 |
import java.util.Enumeration;
|
|
37 |
import java.util.Iterator;
|
|
38 |
import java.util.LinkedList;
|
|
39 |
import java.util.List;
|
|
40 |
import java.util.Map;
|
|
41 |
import java.util.concurrent.Callable;
|
|
42 |
import java.util.concurrent.ExecutorService;
|
|
43 |
import java.util.concurrent.Executors;
|
|
44 |
import java.util.concurrent.TimeUnit;
|
|
45 |
|
|
46 |
/*
|
|
47 |
* @test
|
|
48 |
* @bug 8184770
|
|
49 |
* @summary Tests for JDWP agent attach functionality (including IPv6 support)
|
|
50 |
* @library /test/lib
|
|
51 |
*
|
|
52 |
* @build HelloWorld JdwpAttachTest
|
|
53 |
* @run main/othervm JdwpAttachTest
|
|
54 |
*/
|
|
55 |
public class JdwpAttachTest {
|
|
56 |
|
|
57 |
public static void main(String[] args) throws Exception {
|
|
58 |
List<InetAddress> addresses = getAddresses();
|
|
59 |
|
|
60 |
boolean ipv4EnclosedTested = false;
|
|
61 |
boolean ipv6EnclosedTested = false;
|
|
62 |
for (InetAddress addr: addresses) {
|
|
63 |
// also test that addresses enclosed in square brackets are supported
|
|
64 |
attachTest(addr.getHostAddress(), addr.getHostAddress());
|
|
65 |
// listening on "*" should accept connections from all addresses
|
|
66 |
attachTest("*", addr.getHostAddress());
|
|
67 |
|
|
68 |
// test that addresses enclosed in square brackets are supported.
|
|
69 |
if (addr instanceof Inet4Address && !ipv4EnclosedTested) {
|
|
70 |
attachTest("[" + addr.getHostAddress() + "]", "[" + addr.getHostAddress() + "]");
|
|
71 |
ipv4EnclosedTested = true;
|
|
72 |
}
|
|
73 |
if (addr instanceof Inet6Address && !ipv6EnclosedTested) {
|
|
74 |
attachTest("[" + addr.getHostAddress() + "]", "[" + addr.getHostAddress() + "]");
|
|
75 |
ipv6EnclosedTested = true;
|
|
76 |
}
|
|
77 |
}
|
|
78 |
|
|
79 |
// by using "localhost" or empty hostname
|
|
80 |
// we should be able to attach to both IPv4 and IPv6 addresses (127.0.0.1 & ::1)
|
|
81 |
InetAddress localAddresses[] = InetAddress.getAllByName("localhost");
|
|
82 |
for (int i = 0; i < localAddresses.length; i++) {
|
|
83 |
attachTest(localAddresses[i].getHostAddress(), "");
|
|
84 |
}
|
|
85 |
}
|
|
86 |
|
|
87 |
private static void attachTest(String listenAddress, String connectAddresses)
|
|
88 |
throws Exception {
|
|
89 |
log("Starting listening at " + listenAddress);
|
|
90 |
ListeningConnector connector = getListenConnector();
|
|
91 |
Map<String, Connector.Argument> args = connector.defaultArguments();
|
|
92 |
setConnectorArg(args, "localAddress", listenAddress);
|
|
93 |
setConnectorArg(args, "port", "0");
|
|
94 |
|
|
95 |
String actualAddress = connector.startListening(args);
|
|
96 |
String actualPort = actualAddress.substring(actualAddress.lastIndexOf(':') + 1);
|
|
97 |
String port = args.get("port").value();
|
|
98 |
// port from connector.startListening must be the same as values from arguments
|
|
99 |
if (!port.equals(actualPort)) {
|
|
100 |
throw new RuntimeException("values from connector.startListening (" + actualPort
|
|
101 |
+ " is not equal to values from arguments (" + port + ")");
|
|
102 |
}
|
|
103 |
log("Listening port: " + port);
|
|
104 |
|
|
105 |
log("Attaching from " + connectAddresses);
|
|
106 |
try {
|
|
107 |
ExecutorService executor = Executors.newSingleThreadExecutor();
|
|
108 |
executor.submit((Callable<Exception>)() -> {
|
|
109 |
VirtualMachine vm = connector.accept(args);
|
|
110 |
log("ACCEPTED.");
|
|
111 |
vm.dispose();
|
|
112 |
return null;
|
|
113 |
});
|
|
114 |
executor.shutdown();
|
|
115 |
|
|
116 |
LingeredApp debuggee = LingeredApp.startApp(
|
|
117 |
Arrays.asList("-agentlib:jdwp=transport=dt_socket"
|
|
118 |
+",address=" + connectAddresses + ":" + port
|
|
119 |
+ ",server=n,suspend=n"));
|
|
120 |
debuggee.stopApp();
|
|
121 |
|
|
122 |
executor.awaitTermination(20, TimeUnit.SECONDS);
|
|
123 |
} finally {
|
|
124 |
connector.stopListening(args);
|
|
125 |
}
|
|
126 |
}
|
|
127 |
|
|
128 |
private static List<InetAddress> getAddresses() {
|
|
129 |
List<InetAddress> result = new LinkedList<>();
|
|
130 |
try {
|
|
131 |
Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
|
|
132 |
while (networkInterfaces.hasMoreElements()) {
|
|
133 |
NetworkInterface iface = networkInterfaces.nextElement();
|
|
134 |
try {
|
|
135 |
if (iface.isUp()) {
|
|
136 |
Enumeration<InetAddress> addresses = iface.getInetAddresses();
|
|
137 |
while (addresses.hasMoreElements()) {
|
|
138 |
InetAddress addr = addresses.nextElement();
|
|
139 |
// Java reports link local addresses with named scope,
|
|
140 |
// but Windows sockets routines support only numeric scope id.
|
|
141 |
// skip such addresses.
|
|
142 |
if (addr instanceof Inet6Address) {
|
|
143 |
Inet6Address addr6 = (Inet6Address)addr;
|
|
144 |
if (addr6.getScopedInterface() != null) {
|
|
145 |
continue;
|
|
146 |
}
|
|
147 |
}
|
|
148 |
log(" - (" + addr.getClass().getSimpleName() + ") " + addr.getHostAddress());
|
|
149 |
result.add(addr);
|
|
150 |
}
|
|
151 |
}
|
|
152 |
} catch (SocketException e) {
|
|
153 |
log("Interface " + iface.getDisplayName() + ": failed to get addresses");
|
|
154 |
}
|
|
155 |
}
|
|
156 |
} catch (SocketException e) {
|
|
157 |
log("Interface enumeration error: " + e);
|
|
158 |
}
|
|
159 |
return result;
|
|
160 |
}
|
|
161 |
|
|
162 |
private static String LISTEN_CONNECTOR = "com.sun.jdi.SocketListen";
|
|
163 |
|
|
164 |
private static ListeningConnector getListenConnector() {
|
|
165 |
return (ListeningConnector)getConnector(LISTEN_CONNECTOR);
|
|
166 |
}
|
|
167 |
|
|
168 |
private static Connector getConnector(String name) {
|
|
169 |
List<Connector> connectors = Bootstrap.virtualMachineManager().allConnectors();
|
|
170 |
for (Iterator<Connector> iter = connectors.iterator(); iter.hasNext(); ) {
|
|
171 |
Connector connector = iter.next();
|
|
172 |
if (connector.name().equalsIgnoreCase(name)) {
|
|
173 |
return connector;
|
|
174 |
}
|
|
175 |
}
|
|
176 |
throw new IllegalArgumentException("Connector " + name + " not found");
|
|
177 |
}
|
|
178 |
|
|
179 |
private static void setConnectorArg(Map<String, Connector.Argument> args, String name, String value) {
|
|
180 |
Connector.Argument arg = args.get(name);
|
|
181 |
if (arg == null) {
|
|
182 |
throw new IllegalArgumentException("Argument " + name + " is not defined");
|
|
183 |
}
|
|
184 |
arg.setValue(value);
|
|
185 |
}
|
|
186 |
|
|
187 |
private static void log(Object o) {
|
|
188 |
System.out.println(String.valueOf(o));
|
|
189 |
}
|
|
190 |
|
|
191 |
}
|