Skip to content
Merged
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
42 changes: 41 additions & 1 deletion java/src/org/openqa/selenium/net/PortProber.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.net.Socket;
import java.net.SocketTimeoutException;
import java.util.Random;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.Platform;

Expand All @@ -36,6 +37,42 @@ public class PortProber {
private static final EphemeralPortRangeDetector ephemeralRangeDetector;
private static final Platform current = Platform.getCurrent();

/**
* Ports that browsers (Firefox, Chromium, WebKit) refuse to connect to. A fixture HTTP server
* bound to one of these will cause every navigation in those browsers to fail with an "unsafe
* port" / "denied port access" error, which surfaces in tests as an intermittent failure whenever
* the random port happens to land on the list. Sourced from Mozilla's {@code gBadPortList} (also
* mirrored, with minor differences, by Chromium's {@code kRestrictedPorts}). Only ports in the
* 1024-65535 range are included; ports below 1024 are already excluded by {@link
* #createAcceptablePort()}.
*/
private static final Set<Integer> BROWSER_RESTRICTED_PORTS =
Set.of(
1719, // h323gatestat
1720, // h323hostcall
1723, // pptp
2049, // nfs
3659, // apple-sasl
4045, // lockd
4190, // sieve
5060, // sip
5061, // sips
6000, // x11
6566, // sane-port
6665, // irc (alternate)
6666, // irc (alternate)
6667, // irc (default)
6668, // irc (alternate)
6669, // irc (alternate)
6679, // irc ssl (alternate)
6697, // irc ssl
10080 // amanda
);

static boolean isBrowserRestrictedPort(int port) {
return BROWSER_RESTRICTED_PORTS.contains(port);
}

static {
if (current.is(Platform.LINUX)) {
ephemeralRangeDetector = LinuxEphemeralPortRangeDetector.getInstance();
Expand All @@ -51,8 +88,11 @@ private PortProber() {
}

public static int findFreePort() {
for (int i = 0; i < 5; i++) {
for (int i = 0; i < 10; i++) {
int seedPort = createAcceptablePort();
if (isBrowserRestrictedPort(seedPort)) {
continue;
}
Comment thread
titusfortner marked this conversation as resolved.
int suggestedPort = checkPortIsFree(seedPort);
if (suggestedPort != -1) {
return suggestedPort;
Expand Down