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.AttachingConnector;
|
|
27 |
import com.sun.jdi.connect.Connector;
|
|
28 |
import com.sun.jdi.connect.IllegalConnectorArgumentsException;
|
|
29 |
import lib.jdb.Debuggee;
|
|
30 |
|
|
31 |
import java.io.IOException;
|
|
32 |
import java.net.Inet4Address;
|
|
33 |
import java.net.Inet6Address;
|
|
34 |
import java.net.InetAddress;
|
|
35 |
import java.net.NetworkInterface;
|
|
36 |
import java.net.SocketException;
|
58876
|
37 |
import java.net.UnknownHostException;
|
54884
|
38 |
import java.util.Enumeration;
|
|
39 |
import java.util.Iterator;
|
|
40 |
import java.util.LinkedList;
|
|
41 |
import java.util.List;
|
|
42 |
import java.util.Map;
|
|
43 |
|
|
44 |
/*
|
|
45 |
* @test
|
|
46 |
* @bug 8184770
|
|
47 |
* @summary Tests for JDWP agent listen functionality (including IPv6 support)
|
|
48 |
* @library /test/lib
|
|
49 |
*
|
|
50 |
* @build HelloWorld JdwpListenTest
|
|
51 |
* @run main/othervm JdwpListenTest
|
|
52 |
*/
|
|
53 |
public class JdwpListenTest {
|
|
54 |
|
58876
|
55 |
private static final boolean IsWindows = System.getProperty("os.name").toLowerCase().contains("windows");
|
|
56 |
|
59066
|
57 |
// Set to true to allow testing of attach from wrong address (expected to fail).
|
|
58 |
// It's off by default as it causes test time increase and test interference (see JDK-8231915).
|
|
59 |
private static boolean allowNegativeTesting = false;
|
|
60 |
|
54884
|
61 |
public static void main(String[] args) throws Exception {
|
|
62 |
List<InetAddress> addresses = getAddresses();
|
|
63 |
|
|
64 |
boolean ipv4EnclosedTested = false;
|
|
65 |
boolean ipv6EnclosedTested = false;
|
|
66 |
for (InetAddress listen: addresses) {
|
|
67 |
for (InetAddress attach: addresses) {
|
|
68 |
// can connect only from the same address
|
|
69 |
// IPv6 cannot connect to IPv4 (::1 to 127.0.0.1) and vice versa.
|
58876
|
70 |
// Note: for IPv6 addresses equals() does not compare scopes
|
|
71 |
// (so addresses with symbolic and numeric scopes are equals).
|
54884
|
72 |
listenTest(listen.getHostAddress(), attach.getHostAddress(), attach.equals(listen));
|
|
73 |
}
|
|
74 |
// test that addresses enclosed in square brackets are supported.
|
|
75 |
if (listen instanceof Inet4Address && !ipv4EnclosedTested) {
|
|
76 |
listenTest("[" + listen.getHostAddress() + "]", "[" + listen.getHostAddress() + "]", true);
|
|
77 |
ipv4EnclosedTested = true;
|
|
78 |
}
|
|
79 |
if (listen instanceof Inet6Address && !ipv6EnclosedTested) {
|
|
80 |
listenTest("[" + listen.getHostAddress() + "]", "[" + listen.getHostAddress() + "]", true);
|
|
81 |
ipv6EnclosedTested = true;
|
|
82 |
}
|
|
83 |
}
|
|
84 |
// listen on "*" - should be accessible from any address
|
|
85 |
for (InetAddress attach: addresses) {
|
|
86 |
listenTest("*", attach.getHostAddress(), true);
|
|
87 |
}
|
|
88 |
}
|
|
89 |
|
|
90 |
private static void listenTest(String listenAddress, String connectAddress, boolean expectedResult)
|
|
91 |
throws IOException {
|
58876
|
92 |
log("\nTest: listen at " + listenAddress + ", attaching from " + connectAddress
|
|
93 |
+ ", expected: " + (expectedResult ? "SUCCESS" : "FAILURE"));
|
59066
|
94 |
if (!expectedResult && !allowNegativeTesting) {
|
|
95 |
log("SKIPPED: negative testing is disabled");
|
|
96 |
return;
|
|
97 |
}
|
|
98 |
|
54884
|
99 |
log("Starting listening debuggee at " + listenAddress);
|
|
100 |
try (Debuggee debuggee = Debuggee.launcher("HelloWorld").setAddress(listenAddress + ":0").launch()) {
|
|
101 |
log("Debuggee is listening on " + listenAddress + ":" + debuggee.getAddress());
|
|
102 |
log("Connecting from " + connectAddress + ", expected: " + (expectedResult ? "SUCCESS" : "FAILURE"));
|
|
103 |
try {
|
|
104 |
VirtualMachine vm = attach(connectAddress, debuggee.getAddress());
|
|
105 |
vm.dispose();
|
|
106 |
if (!expectedResult) {
|
|
107 |
throw new RuntimeException("ERROR: attached successfully");
|
|
108 |
}
|
|
109 |
} catch (IOException ex) {
|
|
110 |
if (expectedResult) {
|
|
111 |
throw new RuntimeException("ERROR: failed to attach", ex);
|
|
112 |
}
|
|
113 |
}
|
|
114 |
}
|
59066
|
115 |
log("PASSED");
|
54884
|
116 |
}
|
|
117 |
|
58876
|
118 |
private static void addAddr(List<InetAddress> list, InetAddress addr) {
|
|
119 |
log(" - (" + addr.getClass().getSimpleName() + ") " + addr.getHostAddress());
|
|
120 |
list.add(addr);
|
|
121 |
}
|
|
122 |
|
54884
|
123 |
private static List<InetAddress> getAddresses() {
|
|
124 |
List<InetAddress> result = new LinkedList<>();
|
|
125 |
try {
|
|
126 |
Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
|
|
127 |
while (networkInterfaces.hasMoreElements()) {
|
|
128 |
NetworkInterface iface = networkInterfaces.nextElement();
|
|
129 |
try {
|
|
130 |
if (iface.isUp()) {
|
|
131 |
Enumeration<InetAddress> addresses = iface.getInetAddresses();
|
|
132 |
while (addresses.hasMoreElements()) {
|
|
133 |
InetAddress addr = addresses.nextElement();
|
58876
|
134 |
// Java reports link local addresses with symbolic scope,
|
|
135 |
// but on Windows java.net.NetworkInterface generates its own scope names
|
|
136 |
// which are incompatible with native Windows routines.
|
|
137 |
// So on Windows test only addresses with numeric scope.
|
|
138 |
// On other platforms test both symbolic and numeric scopes.
|
54884
|
139 |
if (addr instanceof Inet6Address) {
|
|
140 |
Inet6Address addr6 = (Inet6Address)addr;
|
58876
|
141 |
NetworkInterface scopeIface = addr6.getScopedInterface();
|
|
142 |
if (scopeIface != null && scopeIface.getName() != null) {
|
|
143 |
// On some test machines VPN creates link local addresses
|
|
144 |
// which we cannot connect to.
|
|
145 |
// Skip them.
|
|
146 |
if (scopeIface.isPointToPoint()) {
|
|
147 |
continue;
|
|
148 |
}
|
|
149 |
|
|
150 |
try {
|
|
151 |
// the same address with numeric scope
|
|
152 |
addAddr(result, Inet6Address.getByAddress(null, addr6.getAddress(), addr6.getScopeId()));
|
|
153 |
} catch (UnknownHostException e) {
|
|
154 |
// cannot happen!
|
|
155 |
throw new RuntimeException("Unexpected", e);
|
|
156 |
}
|
|
157 |
|
|
158 |
if (IsWindows) {
|
|
159 |
// don't add addresses with symbolic scope
|
|
160 |
continue;
|
|
161 |
}
|
54884
|
162 |
}
|
|
163 |
}
|
58876
|
164 |
addAddr(result, addr);
|
54884
|
165 |
}
|
|
166 |
}
|
|
167 |
} catch (SocketException e) {
|
|
168 |
log("Interface " + iface.getDisplayName() + ": failed to get addresses");
|
|
169 |
}
|
|
170 |
}
|
|
171 |
} catch (SocketException e) {
|
|
172 |
log("Interface enumeration error: " + e);
|
|
173 |
}
|
|
174 |
return result;
|
|
175 |
}
|
|
176 |
|
|
177 |
private static String ATTACH_CONNECTOR = "com.sun.jdi.SocketAttach";
|
|
178 |
// cache socket attaching connector
|
|
179 |
private static AttachingConnector attachingConnector;
|
|
180 |
|
|
181 |
private static VirtualMachine attach(String address, String port) throws IOException {
|
|
182 |
if (attachingConnector == null) {
|
|
183 |
attachingConnector = (AttachingConnector)getConnector(ATTACH_CONNECTOR);
|
|
184 |
}
|
|
185 |
Map<String, Connector.Argument> args = attachingConnector.defaultArguments();
|
|
186 |
setConnectorArg(args, "hostname", address);
|
|
187 |
setConnectorArg(args, "port", port);
|
|
188 |
try {
|
|
189 |
return attachingConnector.attach(args);
|
|
190 |
} catch (IllegalConnectorArgumentsException e) {
|
|
191 |
// unexpected.. wrap in RuntimeException
|
|
192 |
throw new RuntimeException(e);
|
|
193 |
}
|
|
194 |
}
|
|
195 |
|
|
196 |
private static Connector getConnector(String name) {
|
|
197 |
List<Connector> connectors = Bootstrap.virtualMachineManager().allConnectors();
|
|
198 |
for (Iterator<Connector> iter = connectors.iterator(); iter.hasNext(); ) {
|
|
199 |
Connector connector = iter.next();
|
|
200 |
if (connector.name().equalsIgnoreCase(name)) {
|
|
201 |
return connector;
|
|
202 |
}
|
|
203 |
}
|
|
204 |
throw new IllegalArgumentException("Connector " + name + " not found");
|
|
205 |
}
|
|
206 |
|
|
207 |
private static void setConnectorArg(Map<String, Connector.Argument> args, String name, String value) {
|
|
208 |
Connector.Argument arg = args.get(name);
|
|
209 |
if (arg == null) {
|
|
210 |
throw new IllegalArgumentException("Argument " + name + " is not defined");
|
|
211 |
}
|
|
212 |
arg.setValue(value);
|
|
213 |
}
|
|
214 |
|
|
215 |
private static void log(Object o) {
|
|
216 |
System.out.println(String.valueOf(o));
|
|
217 |
}
|
|
218 |
|
|
219 |
}
|