Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Check that a port truly is free before using it #11085

Merged
merged 5 commits into from
Oct 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions java/src/org/openqa/selenium/net/PortProber.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,16 +94,23 @@ private static int createAcceptablePort() {
}
}

private static int checkPortIsFree(int port) {
private static boolean isFree(String bindHost, int port) {
try (ServerSocket socket = new ServerSocket()) {
socket.setReuseAddress(true);
socket.bind(new InetSocketAddress("localhost", port));
return socket.getLocalPort();
} catch (IOException e) {
return -1;
socket.bind(new InetSocketAddress(bindHost, port));
return true;
} catch (Exception e) {
return false;
}
}

static int checkPortIsFree(int port) {
if (isFree("localhost", port) && isFree("0.0.0.0", port) && isFree("::1", port)) {
return port;
}
return -1;
}

public static void waitForPortUp(int port, int timeout, TimeUnit unit) {
long end = System.currentTimeMillis() + unit.toMillis(timeout);
while (System.currentTimeMillis() < end) {
Expand Down
47 changes: 47 additions & 0 deletions java/test/org/openqa/selenium/net/PortProberTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package org.openqa.selenium.net;

import static org.assertj.core.api.Assertions.assertThat;

import java.net.InetSocketAddress;
import java.net.ServerSocket;

import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;

@Tag("UnitTests")
public class PortProberTest {

private static final int TEST_PORT = 12345;

@Test
void checkPortIsFree_checksIpv4Localhost() throws Exception {
try (ServerSocket socket = new ServerSocket()) {
socket.bind(new InetSocketAddress("localhost", TEST_PORT));
assertThat(PortProber.checkPortIsFree(TEST_PORT)).isEqualTo(-1);
}
}

@Test
void checkPortIsFree_checksIpv4AllInterfaces() throws Exception {
try (ServerSocket socket = new ServerSocket()) {
socket.bind(new InetSocketAddress("0.0.0.0", TEST_PORT));
assertThat(PortProber.checkPortIsFree(TEST_PORT)).isEqualTo(-1);
}
}

@Test
void checkPortIsFree_checksIpv6Localhost() throws Exception {
try (ServerSocket socket = new ServerSocket()) {
socket.bind(new InetSocketAddress("::1", TEST_PORT));
assertThat(PortProber.checkPortIsFree(TEST_PORT)).isEqualTo(-1);
}
}

@Test
void checkPortIsFree_checksIpv6AllInterfaces() throws Exception {
try (ServerSocket socket = new ServerSocket()) {
socket.bind(new InetSocketAddress("::", TEST_PORT));
assertThat(PortProber.checkPortIsFree(TEST_PORT)).isEqualTo(-1);
}
}
}