Skip to content

Commit 3beefec

Browse files
Artur-diemol
andauthored
fix: Check that a port truly is free before using it (#11085)
* fix: Check that a port truly is free before using it If 0.0.0.0 port 12345 is in use, then 'chromedriver --port=12345 --allowed-ips=1.2.3.4' fails with address already in use. If ::1 port 12345 is in use, then 'chromedriver --port=12345' fails. * Fix sonarcloud issues Co-authored-by: Diego Molina <[email protected]>
1 parent 4cdcffe commit 3beefec

File tree

2 files changed

+59
-5
lines changed

2 files changed

+59
-5
lines changed

java/src/org/openqa/selenium/net/PortProber.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,16 +94,23 @@ private static int createAcceptablePort() {
9494
}
9595
}
9696

97-
private static int checkPortIsFree(int port) {
97+
private static boolean isFree(String bindHost, int port) {
9898
try (ServerSocket socket = new ServerSocket()) {
9999
socket.setReuseAddress(true);
100-
socket.bind(new InetSocketAddress("localhost", port));
101-
return socket.getLocalPort();
102-
} catch (IOException e) {
103-
return -1;
100+
socket.bind(new InetSocketAddress(bindHost, port));
101+
return true;
102+
} catch (Exception e) {
103+
return false;
104104
}
105105
}
106106

107+
static int checkPortIsFree(int port) {
108+
if (isFree("localhost", port) && isFree("0.0.0.0", port) && isFree("::1", port)) {
109+
return port;
110+
}
111+
return -1;
112+
}
113+
107114
public static void waitForPortUp(int port, int timeout, TimeUnit unit) {
108115
long end = System.currentTimeMillis() + unit.toMillis(timeout);
109116
while (System.currentTimeMillis() < end) {
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package org.openqa.selenium.net;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import java.net.InetSocketAddress;
6+
import java.net.ServerSocket;
7+
8+
import org.junit.jupiter.api.Tag;
9+
import org.junit.jupiter.api.Test;
10+
11+
@Tag("UnitTests")
12+
public class PortProberTest {
13+
14+
private static final int TEST_PORT = 12345;
15+
16+
@Test
17+
void checkPortIsFree_checksIpv4Localhost() throws Exception {
18+
try (ServerSocket socket = new ServerSocket()) {
19+
socket.bind(new InetSocketAddress("localhost", TEST_PORT));
20+
assertThat(PortProber.checkPortIsFree(TEST_PORT)).isEqualTo(-1);
21+
}
22+
}
23+
24+
@Test
25+
void checkPortIsFree_checksIpv4AllInterfaces() throws Exception {
26+
try (ServerSocket socket = new ServerSocket()) {
27+
socket.bind(new InetSocketAddress("0.0.0.0", TEST_PORT));
28+
assertThat(PortProber.checkPortIsFree(TEST_PORT)).isEqualTo(-1);
29+
}
30+
}
31+
32+
@Test
33+
void checkPortIsFree_checksIpv6Localhost() throws Exception {
34+
try (ServerSocket socket = new ServerSocket()) {
35+
socket.bind(new InetSocketAddress("::1", TEST_PORT));
36+
assertThat(PortProber.checkPortIsFree(TEST_PORT)).isEqualTo(-1);
37+
}
38+
}
39+
40+
@Test
41+
void checkPortIsFree_checksIpv6AllInterfaces() throws Exception {
42+
try (ServerSocket socket = new ServerSocket()) {
43+
socket.bind(new InetSocketAddress("::", TEST_PORT));
44+
assertThat(PortProber.checkPortIsFree(TEST_PORT)).isEqualTo(-1);
45+
}
46+
}
47+
}

0 commit comments

Comments
 (0)