Skip to content

Commit e213043

Browse files
committed
Remove org.openqa.selenium.net.INetAddress, an unnecessary abstraction
around java.net.InetAddress. Not only is INetAddress just not necessary, its constructor triggers a DNS lookup. These DNS lookups cannot be avoided when creating a FirefoxDriver instance, as the code is referenced through a static initializer in org.openqa.selenium.firefox.internal.NewProfileExtensionConnection. By removing INetAddress, not only is the code simpler, but the time to start the FirefoxDriver for the first time on Windows is reduced by up to 30s (depending on the machine's network config)
1 parent 43bff49 commit e213043

File tree

7 files changed

+181
-205
lines changed

7 files changed

+181
-205
lines changed

java/client/src/org/openqa/selenium/net/INetAddress.java

Lines changed: 0 additions & 57 deletions
This file was deleted.

java/client/src/org/openqa/selenium/net/NetworkInterface.java

Lines changed: 29 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,26 @@
1616
*/
1717
package org.openqa.selenium.net;
1818

19+
import static java.util.Collections.list;
20+
21+
import com.google.common.collect.Iterables;
22+
23+
import java.net.Inet6Address;
1924
import java.net.InetAddress;
2025
import java.net.SocketException;
21-
import java.util.ArrayList;
2226
import java.util.Arrays;
23-
import java.util.Collections;
24-
import java.util.Enumeration;
2527
import java.util.Iterator;
26-
import java.util.List;
2728
import java.util.logging.Level;
2829
import java.util.logging.Logger;
2930

3031
public class NetworkInterface {
3132

3233
private final String name;
33-
private final Iterable<INetAddress> inetAddresses;
34+
private final Iterable<InetAddress> inetAddresses;
3435
private boolean isLoopback;
3536

3637
public NetworkInterface(java.net.NetworkInterface networkInterface) {
37-
this(networkInterface.getName(), asIterableAddr(networkInterface.getInetAddresses()));
38+
this(networkInterface.getName(), list(networkInterface.getInetAddresses()));
3839
try {
3940
// Issue 1181 : determine whether this NetworkInterface instance is loopback
4041
// from java.net.NetworkInterface API
@@ -44,18 +45,18 @@ public NetworkInterface(java.net.NetworkInterface networkInterface) {
4445
// If an SocketException is caught, determine whether this NetworkInterface
4546
// instance is loopback from computation from its inetAddresses
4647
this.isLoopback =
47-
isLoopBackFromINetAddresses(asIterableAddr(networkInterface.getInetAddresses()));
48+
isLoopBackFromINetAddresses(list(networkInterface.getInetAddresses()));
4849
}
4950
}
5051

51-
NetworkInterface(String name, Iterable<INetAddress> inetAddresses) {
52+
NetworkInterface(String name, Iterable<InetAddress> inetAddresses) {
5253
this.name = name;
53-
this.inetAddresses = inetAddresses;
54+
this.inetAddresses = Iterables.unmodifiableIterable(inetAddresses);
5455
}
5556

56-
NetworkInterface(String name, INetAddress... inetAddresses) {
57+
NetworkInterface(String name, InetAddress... inetAddresses) {
5758
this(name, Arrays.asList(inetAddresses));
58-
this.isLoopback = isLoopBackFromINetAddresses(Arrays.asList(inetAddresses));
59+
this.isLoopback = isLoopBackFromINetAddresses(this.inetAddresses);
5960
}
6061

6162
public boolean isIp4AddressBindingOnly() {
@@ -66,63 +67,59 @@ public boolean isLoopBack() {
6667
return isLoopback;
6768
}
6869

69-
public final boolean isLoopBackFromINetAddresses(Iterable<INetAddress> inetAddresses) {
70+
private boolean isLoopBackFromINetAddresses(Iterable<InetAddress> inetAddresses) {
7071
// Let's hope there's no such thing as network interfaces with mixed addresses ;)
71-
Iterator<INetAddress> iterator = inetAddresses.iterator();
72+
Iterator<InetAddress> iterator = inetAddresses.iterator();
7273
return iterator.hasNext() && iterator.next().isLoopbackAddress();
7374
}
7475

75-
public INetAddress getIp4LoopbackOnly() {
76+
public InetAddress getIp4LoopbackOnly() {
7677
// Goes by the wildly unscientific assumption that if there are more than one set of
7778
// loopback addresses, firefox will bind to the last one we get.
7879
// An alternate theory if this fails is that firefox prefers 127.0.0.1
7980
// Most "normal" boxes don't have multiple addresses so we'll just refine this
8081
// algorithm until it works.
8182
// See NetworkUtilsTest#testOpenSuseBoxIssue1181
82-
INetAddress lastFound = null;
83+
InetAddress lastFound = null;
8384
// Issue 1181
8485
if (!isLoopback) {
8586
return lastFound;
8687
}
87-
for (INetAddress inetAddress : inetAddresses) {
88-
if (inetAddress.isLoopbackAddress() && inetAddress.isIPv4Address()) {
88+
for (InetAddress inetAddress : inetAddresses) {
89+
if (inetAddress.isLoopbackAddress() && !isIpv6(inetAddress)) {
8990
lastFound = inetAddress;
9091
}
9192
}
9293
return lastFound;
9394
}
9495

95-
public INetAddress getIp4NonLoopBackOnly() {
96-
for (INetAddress inetAddress : inetAddresses) {
97-
if (!inetAddress.isLoopbackAddress() && inetAddress.isIPv4Address()) {
96+
static boolean isIpv6(InetAddress address) {
97+
return address instanceof Inet6Address;
98+
}
99+
100+
public InetAddress getIp4NonLoopBackOnly() {
101+
for (InetAddress inetAddress : inetAddresses) {
102+
if (!inetAddress.isLoopbackAddress() && !isIpv6(inetAddress)) {
98103
return inetAddress;
99104
}
100105
}
101106
return null;
102107
}
103108

104-
public INetAddress getIp6Address() {
105-
for (INetAddress inetAddress : inetAddresses) {
106-
if (inetAddress.isIPv6Address()) {
109+
public InetAddress getIp6Address() {
110+
for (InetAddress inetAddress : inetAddresses) {
111+
if (isIpv6(inetAddress)) {
107112
return inetAddress;
108113
}
109114
}
110115
return null;
111116
}
112117

113-
public Iterable<INetAddress> getInetAddresses() {
118+
public Iterable<InetAddress> getInetAddresses() {
114119
return inetAddresses;
115120
}
116121

117122
public String getName() {
118123
return name;
119124
}
120-
121-
static Iterable<INetAddress> asIterableAddr(Enumeration<InetAddress> tEnumeration) {
122-
List<INetAddress> result = new ArrayList<INetAddress>();
123-
while (tEnumeration.hasMoreElements()) {
124-
result.add(new INetAddress(tEnumeration.nextElement()));
125-
}
126-
return Collections.unmodifiableList(result);
127-
}
128125
}

java/client/src/org/openqa/selenium/net/NetworkUtils.java

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,13 @@
1515
*/
1616
package org.openqa.selenium.net;
1717

18+
import static org.openqa.selenium.net.NetworkInterface.isIpv6;
19+
1820
import org.openqa.selenium.Platform;
1921
import org.openqa.selenium.WebDriverException;
2022

23+
import java.net.Inet6Address;
24+
import java.net.InetAddress;
2125
import java.util.ArrayList;
2226
import java.util.Collections;
2327
import java.util.List;
@@ -35,7 +39,7 @@ public NetworkUtils() {
3539
}
3640

3741
public String getPrivateLocalAddress() {
38-
List<INetAddress> addresses = getLocalInterfaceAddress();
42+
List<InetAddress> addresses = getLocalInterfaceAddress();
3943
if (addresses.isEmpty()) {
4044
return "127.0.0.1";
4145
}
@@ -47,7 +51,7 @@ public String getPrivateLocalAddress() {
4751
* Used by the mobile emulators that refuse to access localhost or 127.0.0.1 The IP4/IP6
4852
* requirements of this method are as-of-yet unspecified, but we return the string that is
4953
* associated with the IP4 interface
50-
*
54+
*
5155
* @return A String representing the host name or non-loopback IP4 address of this machine.
5256
*/
5357
public String getNonLoopbackAddressOfThisMachine() {
@@ -56,12 +60,12 @@ public String getNonLoopbackAddressOfThisMachine() {
5660

5761
/**
5862
* Returns a non-loopback IP4 hostname of the local host.
59-
*
63+
*
6064
* @return A string hostName
6165
*/
62-
public INetAddress getIp4NonLoopbackAddressOfThisMachine() {
66+
public InetAddress getIp4NonLoopbackAddressOfThisMachine() {
6367
for (NetworkInterface iface : networkInterfaceProvider.getNetworkInterfaces()) {
64-
final INetAddress ip4NonLoopback = iface.getIp4NonLoopBackOnly();
68+
final InetAddress ip4NonLoopback = iface.getIp4NonLoopBackOnly();
6569
if (ip4NonLoopback != null) {
6670
return ip4NonLoopback;
6771
}
@@ -73,7 +77,7 @@ public INetAddress getIp4NonLoopbackAddressOfThisMachine() {
7377
* Returns a single address that is guaranteed to resolve to an ipv4 representation of localhost
7478
* This may either be a hostname or an ip address, dependending if we can guarantee what that the
7579
* hostname will resolve to ip4.
76-
*
80+
*
7781
* @return The address part og such an address
7882
*/
7983
public String obtainLoopbackIp4Address() {
@@ -90,7 +94,7 @@ public String obtainLoopbackIp4Address() {
9094
if (Platform.getCurrent().is(Platform.UNIX)) {
9195
NetworkInterface linuxLoopback = networkInterfaceProvider.getLoInterface();
9296
if (linuxLoopback != null) {
93-
final INetAddress netAddress = linuxLoopback.getIp4LoopbackOnly();
97+
final InetAddress netAddress = linuxLoopback.getIp4LoopbackOnly();
9498
if (netAddress != null) {
9599
return netAddress.getHostAddress();
96100
}
@@ -104,10 +108,10 @@ public String obtainLoopbackIp4Address() {
104108
}
105109

106110

107-
private INetAddress grabFirstNetworkAddress() {
111+
private InetAddress grabFirstNetworkAddress() {
108112
NetworkInterface firstInterface =
109113
networkInterfaceProvider.getNetworkInterfaces().iterator().next();
110-
INetAddress firstAddress = null;
114+
InetAddress firstAddress = null;
111115
if (firstInterface != null) {
112116
firstAddress = firstInterface.getInetAddresses().iterator().next();
113117
}
@@ -121,7 +125,7 @@ private INetAddress grabFirstNetworkAddress() {
121125

122126
public String getIpOfLoopBackIp4() {
123127
for (NetworkInterface iface : networkInterfaceProvider.getNetworkInterfaces()) {
124-
final INetAddress netAddress = iface.getIp4LoopbackOnly();
128+
final InetAddress netAddress = iface.getIp4LoopbackOnly();
125129
if (netAddress != null) {
126130
return netAddress.getHostAddress();
127131
}
@@ -138,13 +142,13 @@ private NetworkInterface getLoopBackAndIp4Only() {
138142
return null;
139143
}
140144

141-
private List<INetAddress> getLocalInterfaceAddress() {
142-
List<INetAddress> localAddresses = new ArrayList<INetAddress>();
145+
private List<InetAddress> getLocalInterfaceAddress() {
146+
List<InetAddress> localAddresses = new ArrayList<InetAddress>();
143147

144148
for (NetworkInterface iface : networkInterfaceProvider.getNetworkInterfaces()) {
145-
for (INetAddress addr : iface.getInetAddresses()) {
149+
for (InetAddress addr : iface.getInetAddresses()) {
146150
// filter out Inet6 Addr Entries
147-
if (addr.isLoopbackAddress() && !addr.isIPv6Address()) {
151+
if (addr.isLoopbackAddress() && !isIpv6(addr)) {
148152
localAddresses.add(addr);
149153
}
150154
}
@@ -156,8 +160,8 @@ private List<INetAddress> getLocalInterfaceAddress() {
156160
if (Platform.getCurrent().is(Platform.UNIX)) {
157161
NetworkInterface linuxLoopback = networkInterfaceProvider.getLoInterface();
158162
if (linuxLoopback != null) {
159-
for (INetAddress inetAddress : linuxLoopback.getInetAddresses()) {
160-
if (!inetAddress.isIPv6Address()) {
163+
for (InetAddress inetAddress : linuxLoopback.getInetAddresses()) {
164+
if (!isIpv6(inetAddress)) {
161165
localAddresses.add(inetAddress);
162166
}
163167
}
@@ -197,8 +201,8 @@ private static void dumpToConsole(StringBuilder result, NetworkInterface inNetwo
197201
dumpAddresses(result, inNetworkInterface.getInetAddresses());
198202
}
199203

200-
private static void dumpAddresses(StringBuilder result, Iterable<INetAddress> inetAddresses) {
201-
for (INetAddress address : inetAddresses) {
204+
private static void dumpAddresses(StringBuilder result, Iterable<InetAddress> inetAddresses) {
205+
for (InetAddress address : inetAddresses) {
202206
result.append(" address.getHostName() = ");
203207
result.append(address.getHostName());
204208
result.append("\n");

java/client/test/org/openqa/selenium/SmallTests.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.openqa.selenium.io.ZipTest;
2929
import org.openqa.selenium.logging.LoggingTest;
3030
import org.openqa.selenium.logging.PerformanceLoggingMockTest;
31+
import org.openqa.selenium.net.LinuxEphemeralPortRangeDetectorTest;
3132
import org.openqa.selenium.net.NetworkUtilsTest;
3233
import org.openqa.selenium.os.CommandLineTest;
3334
import org.openqa.selenium.os.WindowsUtilsUnitTest;
@@ -46,6 +47,7 @@
4647
IgnoreComparitorUnitTest.class,
4748
IndividualKeyboardActionsTest.class,
4849
IndividualMouseActionsTest.class,
50+
LinuxEphemeralPortRangeDetectorTest.class,
4951
LoggingTest.class,
5052
NetworkUtilsTest.class,
5153
OutputTypeTest.class,

java/client/test/org/openqa/selenium/net/LinuxEphemeralPortRangeDetectorTest.java

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,29 +15,38 @@
1515
limitations under the License.
1616
*/
1717

18+
import static org.junit.Assert.assertEquals;
19+
import static org.junit.Assert.assertTrue;
20+
import static org.openqa.selenium.Platform.LINUX;
21+
1822
import org.junit.Assume;
23+
import org.junit.BeforeClass;
1924
import org.junit.Test;
25+
import org.junit.runner.RunWith;
26+
import org.junit.runners.JUnit4;
2027
import org.openqa.selenium.Platform;
2128

2229
import java.io.StringReader;
2330

24-
import static org.junit.Assert.assertEquals;
25-
import static org.junit.Assert.assertTrue;
26-
import static org.openqa.selenium.Platform.LINUX;
27-
31+
@RunWith(JUnit4.class)
2832
public class LinuxEphemeralPortRangeDetectorTest {
2933

34+
@BeforeClass
35+
public static void requiresLinux() {
36+
Assume.assumeTrue(Platform.getCurrent().is(LINUX));
37+
}
38+
3039
@Test
3140
public void decodeEphemeralPorts() throws Exception {
3241
String range ="1234 65533";
33-
EphemeralPortRangeDetector ephemeralEphemeralPortDetector = new LinuxEphemeralPortRangeDetector(new StringReader(range));
42+
EphemeralPortRangeDetector ephemeralEphemeralPortDetector =
43+
new LinuxEphemeralPortRangeDetector(new StringReader(range));
3444
assertEquals( 1234, ephemeralEphemeralPortDetector.getLowestEphemeralPort());
3545
assertEquals( 65533, ephemeralEphemeralPortDetector.getHighestEphemeralPort());
3646
}
3747

3848
@Test
3949
public void currentValues(){
40-
Assume.assumeTrue(Platform.getCurrent().is(LINUX));
4150
LinuxEphemeralPortRangeDetector detector = LinuxEphemeralPortRangeDetector.getInstance();
4251
assertTrue( detector.getLowestEphemeralPort() > 1024);
4352
assertTrue( detector.getHighestEphemeralPort() < 65536);

0 commit comments

Comments
 (0)