50018
|
1 |
/*
|
54884
|
2 |
* Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
|
50018
|
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 |
package nsk.jdi.ListeningConnector.startListening;
|
|
25 |
|
|
26 |
import com.sun.jdi.Bootstrap;
|
|
27 |
import com.sun.jdi.connect.*;
|
|
28 |
import com.sun.jdi.VirtualMachine;
|
|
29 |
|
|
30 |
import java.io.*;
|
|
31 |
|
|
32 |
import java.net.InetAddress;
|
|
33 |
import java.net.UnknownHostException;
|
|
34 |
|
54884
|
35 |
import java.util.Arrays;
|
50018
|
36 |
import java.util.Iterator;
|
54884
|
37 |
import java.util.LinkedList;
|
50018
|
38 |
import java.util.List;
|
|
39 |
import java.util.Map;
|
54884
|
40 |
import java.util.stream.Collectors;
|
50018
|
41 |
|
|
42 |
import nsk.share.*;
|
|
43 |
import nsk.share.jpda.*;
|
|
44 |
import nsk.share.jdi.*;
|
|
45 |
|
|
46 |
|
|
47 |
/**
|
|
48 |
* The test exercises JDI function <code>ListeningConnector.startListening</code>.
|
|
49 |
* The <b>Socket Listening Connector</b> is using as listening
|
|
50 |
* connector.<br>
|
|
51 |
* The test cases include:
|
|
52 |
* <li> checking that listening address returned by
|
|
53 |
* <code>ListeningConnector.startListening()</code> matches the address
|
|
54 |
* which was set via connector's arguments;
|
|
55 |
* <li> checking that address generated by
|
|
56 |
* <code>ListeningConnector.startListening()</code> is valid i.e.
|
|
57 |
* debugee VM is accessible via this address.
|
|
58 |
*/
|
|
59 |
public class startlis001 {
|
|
60 |
static final int PASSED = 0;
|
|
61 |
static final int FAILED = 2;
|
|
62 |
static final int JCK_STATUS_BASE = 95;
|
|
63 |
static final String CONNECTOR_NAME =
|
|
64 |
"com.sun.jdi.SocketListen";
|
|
65 |
static final String DEBUGEE_CLASS =
|
|
66 |
"nsk.jdi.ListeningConnector.startListening.startlis001t";
|
|
67 |
|
|
68 |
private Log log;
|
|
69 |
|
|
70 |
private VirtualMachine vm;
|
|
71 |
private ListeningConnector connector;
|
|
72 |
private Map<java.lang.String,? extends com.sun.jdi.connect.Connector.Argument> connArgs;
|
|
73 |
private PrintStream out;
|
|
74 |
|
|
75 |
IORedirector outRedirector;
|
|
76 |
IORedirector errRedirector;
|
|
77 |
|
|
78 |
boolean totalRes = true;
|
|
79 |
|
|
80 |
public static void main (String argv[]) {
|
|
81 |
System.exit(run(argv,System.out) + JCK_STATUS_BASE);
|
|
82 |
}
|
|
83 |
|
|
84 |
public static int run(String argv[], PrintStream out) {
|
|
85 |
return new startlis001().runIt(argv, out);
|
|
86 |
}
|
|
87 |
|
|
88 |
private int runIt(String argv[], PrintStream out) {
|
|
89 |
String port;
|
|
90 |
String addr;
|
|
91 |
ArgumentHandler argHandler = new ArgumentHandler(argv);
|
|
92 |
|
|
93 |
// pass if CONNECTOR_NAME is not implemented
|
|
94 |
// on this platform
|
|
95 |
if (argHandler.shouldPass(CONNECTOR_NAME))
|
|
96 |
return PASSED;
|
|
97 |
this.out = out;
|
|
98 |
log = new Log(out, argHandler);
|
|
99 |
|
|
100 |
long timeout = argHandler.getWaitTime() * 60 * 1000;
|
|
101 |
|
54884
|
102 |
/* Check that listening address returned by ListeningConnector.startListening()
|
|
103 |
* matches the address which was set via connector's arguments.
|
|
104 |
* Empty host address causes listening for local connections only (loopback interface).
|
|
105 |
* */
|
|
106 |
String hostname = "localhost";
|
|
107 |
List<String> validAddresses = new LinkedList<>();
|
|
108 |
validAddresses.add(hostname);
|
50018
|
109 |
try {
|
54884
|
110 |
Arrays.stream(InetAddress.getAllByName(hostname))
|
|
111 |
.forEach(address -> validAddresses.add(address.getHostAddress()));
|
50018
|
112 |
} catch (UnknownHostException e) {
|
|
113 |
log.complain("FAILURE: caught UnknownHostException " +
|
54884
|
114 |
e.getMessage());
|
50018
|
115 |
totalRes = false;
|
|
116 |
}
|
54884
|
117 |
|
50018
|
118 |
port = argHandler.getTransportPortIfNotDynamic();
|
|
119 |
|
|
120 |
initConnector(port);
|
|
121 |
if ((addr = startListen()) == null) {
|
|
122 |
log.complain("Test case #1 FAILED: unable to start listening");
|
|
123 |
totalRes = false;
|
54884
|
124 |
} else {
|
|
125 |
String validAddrList = validAddresses.stream()
|
|
126 |
.map(value -> value + ":" + port)
|
|
127 |
.collect(Collectors.joining(" or "));
|
50018
|
128 |
log.display("Test case #1: start listening the address " + addr);
|
54884
|
129 |
log.display("Expected addresses: " + validAddrList);
|
|
130 |
final String listenAddr = addr;
|
|
131 |
boolean isValid = validAddresses.stream()
|
|
132 |
.anyMatch(value -> listenAddr.startsWith(value) && (port == null || listenAddr.endsWith(port)));
|
|
133 |
if (!isValid) {
|
50018
|
134 |
log.complain("Test case #1 FAILED: listening address " + addr +
|
54884
|
135 |
"\ndoes not match expected address:\n" + validAddrList);
|
50018
|
136 |
totalRes = false;
|
|
137 |
}
|
|
138 |
if (!stopListen()) {
|
|
139 |
log.complain("TEST: unable to stop listening #1");
|
|
140 |
totalRes = false;
|
|
141 |
}
|
|
142 |
else
|
|
143 |
log.display("Test case #1 PASSED: listening address matches expected address");
|
|
144 |
}
|
|
145 |
|
54884
|
146 |
/* Check that an address generated by ListeningConnector.startListening()
|
|
147 |
is valid i.e. debugee VM is accessible via this address */
|
50018
|
148 |
initConnector(null);
|
|
149 |
if ((addr = startListen()) == null) {
|
|
150 |
log.complain("Test case #2 FAILED: unable to start listening");
|
|
151 |
return FAILED;
|
|
152 |
}
|
|
153 |
else
|
|
154 |
log.display("Test case #2: start listening the address " + addr);
|
|
155 |
|
|
156 |
String java = argHandler.getLaunchExecPath()
|
|
157 |
+ " " + argHandler.getLaunchOptions();
|
|
158 |
|
|
159 |
String cmd = java +
|
|
160 |
" -Xdebug -Xnoagent -Xrunjdwp:transport=dt_socket,server=n,address=" +
|
|
161 |
addr + " " + DEBUGEE_CLASS;
|
|
162 |
|
|
163 |
Binder binder = new Binder(argHandler, log);
|
|
164 |
Debugee debugee = null;
|
|
165 |
|
|
166 |
log.display("command: " + cmd);
|
|
167 |
try {
|
|
168 |
debugee = binder.startLocalDebugee(cmd);
|
|
169 |
debugee.redirectOutput(log);
|
|
170 |
} catch (Exception e) {
|
|
171 |
stopListen();
|
|
172 |
throw new Failure(e);
|
|
173 |
}
|
|
174 |
|
|
175 |
if ((vm = attachTarget()) == null) {
|
|
176 |
log.complain("Test case #2 FAILED: unable to attach the debugee VM");
|
|
177 |
debugee.close();
|
|
178 |
stopListen();
|
|
179 |
return FAILED;
|
|
180 |
}
|
|
181 |
else
|
|
182 |
log.display("Test case #2 PASSED: successfully attach the debugee VM");
|
|
183 |
|
|
184 |
if (!stopListen()) {
|
|
185 |
log.complain("TEST: unable to stop listening #2");
|
|
186 |
debugee.close();
|
|
187 |
return FAILED;
|
|
188 |
}
|
|
189 |
|
|
190 |
debugee.setupVM(vm);
|
|
191 |
debugee.waitForVMInit(timeout);
|
|
192 |
|
|
193 |
log.display("\nResuming debugee VM");
|
|
194 |
debugee.resume();
|
|
195 |
|
|
196 |
log.display("\nWaiting for debugee VM exit");
|
|
197 |
int code = debugee.waitFor();
|
|
198 |
if (code != (JCK_STATUS_BASE+PASSED)) {
|
|
199 |
log.complain("Debugee VM has crashed: exit code=" +
|
|
200 |
code);
|
|
201 |
return FAILED;
|
|
202 |
}
|
|
203 |
log.display("Debugee VM: exit code=" + code);
|
|
204 |
|
|
205 |
if (totalRes) return PASSED;
|
|
206 |
else return FAILED;
|
|
207 |
}
|
|
208 |
|
|
209 |
private VirtualMachine attachTarget() {
|
|
210 |
try {
|
|
211 |
return connector.accept(connArgs);
|
|
212 |
} catch (IOException e) {
|
|
213 |
log.complain("FAILURE: caught IOException: " +
|
|
214 |
e.getMessage());
|
|
215 |
e.printStackTrace(out);
|
|
216 |
return null;
|
|
217 |
} catch (IllegalConnectorArgumentsException e) {
|
|
218 |
log.complain("FAILURE: Illegal connector arguments: " +
|
|
219 |
e.getMessage());
|
|
220 |
e.printStackTrace(out);
|
|
221 |
return null;
|
|
222 |
} catch (Exception e) {
|
|
223 |
log.complain("FAILURE: Exception: " +
|
|
224 |
e.getMessage());
|
|
225 |
e.printStackTrace(out);
|
|
226 |
return null;
|
|
227 |
}
|
|
228 |
}
|
|
229 |
|
|
230 |
private void initConnector(String port) {
|
|
231 |
Connector.Argument arg;
|
|
232 |
|
|
233 |
connector = (ListeningConnector)
|
|
234 |
findConnector(CONNECTOR_NAME);
|
|
235 |
|
|
236 |
connArgs = connector.defaultArguments();
|
|
237 |
Iterator cArgsValIter = connArgs.keySet().iterator();
|
|
238 |
while (cArgsValIter.hasNext()) {
|
|
239 |
String argKey = (String) cArgsValIter.next();
|
|
240 |
String argVal = null;
|
|
241 |
|
|
242 |
if ((arg = (Connector.Argument) connArgs.get(argKey)) == null) {
|
|
243 |
log.complain("Argument " + argKey.toString() +
|
|
244 |
"is not defined for the connector: " +
|
|
245 |
connector.name());
|
|
246 |
}
|
|
247 |
if (arg.name().equals("port") && port != null)
|
|
248 |
arg.setValue(port);
|
|
249 |
|
|
250 |
log.display("\targument name=" + arg.name());
|
|
251 |
if ((argVal = arg.value()) != null)
|
|
252 |
log.display("\t\tvalue=" + argVal);
|
|
253 |
else log.display("\t\tvalue=NULL");
|
|
254 |
}
|
|
255 |
}
|
|
256 |
|
|
257 |
private String startListen() {
|
|
258 |
try {
|
|
259 |
return connector.startListening(connArgs);
|
|
260 |
} catch (IOException e) {
|
|
261 |
log.complain("FAILURE: caught IOException: " +
|
|
262 |
e.getMessage());
|
|
263 |
e.printStackTrace(out);
|
|
264 |
return null;
|
|
265 |
} catch (IllegalConnectorArgumentsException e) {
|
|
266 |
log.complain("FAILURE: Illegal connector arguments: " +
|
|
267 |
e.getMessage());
|
|
268 |
e.printStackTrace(out);
|
|
269 |
return null;
|
|
270 |
} catch (Exception e) {
|
|
271 |
log.complain("FAILURE: Exception: " + e.getMessage());
|
|
272 |
e.printStackTrace(out);
|
|
273 |
return null;
|
|
274 |
}
|
|
275 |
}
|
|
276 |
|
|
277 |
private boolean stopListen() {
|
|
278 |
try {
|
|
279 |
connector.stopListening(connArgs);
|
|
280 |
} catch (IOException e) {
|
|
281 |
log.complain("FAILURE: caught IOException: " +
|
|
282 |
e.getMessage());
|
|
283 |
e.printStackTrace(out);
|
|
284 |
return false;
|
|
285 |
} catch (IllegalConnectorArgumentsException e) {
|
|
286 |
log.complain("FAILURE: Illegal connector arguments: " +
|
|
287 |
e.getMessage());
|
|
288 |
e.printStackTrace(out);
|
|
289 |
return false;
|
|
290 |
} catch (Exception e) {
|
|
291 |
log.complain("FAILURE: Exception: " + e.getMessage());
|
|
292 |
e.printStackTrace(out);
|
|
293 |
return false;
|
|
294 |
}
|
|
295 |
|
|
296 |
return true;
|
|
297 |
}
|
|
298 |
|
|
299 |
private Connector findConnector(String connectorName) {
|
|
300 |
List connectors = Bootstrap.virtualMachineManager().allConnectors();
|
|
301 |
Iterator iter = connectors.iterator();
|
|
302 |
|
|
303 |
while (iter.hasNext()) {
|
|
304 |
Connector connector = (Connector) iter.next();
|
|
305 |
if (connector.name().equals(connectorName)) {
|
|
306 |
log.display("Connector name=" + connector.name() +
|
|
307 |
"\n\tdescription=" + connector.description() +
|
|
308 |
"\n\ttransport=" + connector.transport().name());
|
|
309 |
return connector;
|
|
310 |
}
|
|
311 |
}
|
|
312 |
throw new Error("No appropriate connector");
|
|
313 |
}
|
|
314 |
}
|