1 /* |
1 /* |
2 * Copyright (c) 2013, 2017, Oracle and/or its affiliates. All rights reserved. |
2 * Copyright (c) 2013, 2018, Oracle and/or its affiliates. All rights reserved. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 * |
4 * |
5 * This code is free software; you can redistribute it and/or modify it |
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 |
6 * under the terms of the GNU General Public License version 2 only, as |
7 * published by the Free Software Foundation. |
7 * published by the Free Software Foundation. |
24 package jdk.test.lib; |
24 package jdk.test.lib; |
25 |
25 |
26 import java.io.File; |
26 import java.io.File; |
27 import java.io.IOException; |
27 import java.io.IOException; |
28 import java.net.InetAddress; |
28 import java.net.InetAddress; |
|
29 import java.net.InetSocketAddress; |
29 import java.net.MalformedURLException; |
30 import java.net.MalformedURLException; |
30 import java.net.ServerSocket; |
31 import java.net.ServerSocket; |
31 import java.net.URL; |
32 import java.net.URL; |
32 import java.net.URLClassLoader; |
33 import java.net.URLClassLoader; |
33 import java.net.UnknownHostException; |
34 import java.net.UnknownHostException; |
34 import java.nio.file.Files; |
35 import java.nio.file.Files; |
35 import java.nio.file.Path; |
36 import java.nio.file.Path; |
36 import java.nio.file.Paths; |
37 import java.nio.file.Paths; |
|
38 import java.nio.channels.SocketChannel; |
37 import java.util.ArrayList; |
39 import java.util.ArrayList; |
38 import java.util.Arrays; |
40 import java.util.Arrays; |
39 import java.util.Collection; |
41 import java.util.Collection; |
40 import java.util.Collections; |
42 import java.util.Collections; |
41 import java.util.Iterator; |
43 import java.util.Iterator; |
258 } |
260 } |
259 return cmd.toString(); |
261 return cmd.toString(); |
260 } |
262 } |
261 |
263 |
262 /** |
264 /** |
|
265 * Returns the socket address of an endpoint that refuses connections. The |
|
266 * endpoint is an InetSocketAddress where the address is the loopback address |
|
267 * and the port is a system port (1-1023 range). |
|
268 * This method is a better choice than getFreePort for tests that need |
|
269 * an endpoint that refuses connections. |
|
270 */ |
|
271 public static InetSocketAddress refusingEndpoint() { |
|
272 InetAddress lb = InetAddress.getLoopbackAddress(); |
|
273 int port = 1; |
|
274 while (port < 1024) { |
|
275 InetSocketAddress sa = new InetSocketAddress(lb, port); |
|
276 try { |
|
277 SocketChannel.open(sa).close(); |
|
278 } catch (IOException ioe) { |
|
279 return sa; |
|
280 } |
|
281 port++; |
|
282 } |
|
283 throw new RuntimeException("Unable to find system port that is refusing connections"); |
|
284 } |
|
285 |
|
286 /** |
263 * Returns the free port on the local host. |
287 * Returns the free port on the local host. |
264 * The function will spin until a valid port number is found. |
|
265 * |
288 * |
266 * @return The port number |
289 * @return The port number |
267 * @throws InterruptedException if any thread has interrupted the current thread |
|
268 * @throws IOException if an I/O error occurs when opening the socket |
290 * @throws IOException if an I/O error occurs when opening the socket |
269 */ |
291 */ |
270 public static int getFreePort() throws InterruptedException, IOException { |
292 public static int getFreePort() throws IOException { |
271 int port = -1; |
293 try (ServerSocket serverSocket = |
272 |
294 new ServerSocket(0, 5, InetAddress.getLoopbackAddress());) { |
273 while (port <= 0) { |
295 return serverSocket.getLocalPort(); |
274 Thread.sleep(100); |
296 } |
275 |
|
276 ServerSocket serverSocket = null; |
|
277 try { |
|
278 serverSocket = new ServerSocket(0); |
|
279 port = serverSocket.getLocalPort(); |
|
280 } finally { |
|
281 serverSocket.close(); |
|
282 } |
|
283 } |
|
284 |
|
285 return port; |
|
286 } |
297 } |
287 |
298 |
288 /** |
299 /** |
289 * Returns the name of the local host. |
300 * Returns the name of the local host. |
290 * |
301 * |