Skip to content

Commit 103eb33

Browse files
authored
Remove IP literal check for InetAddressResolver (#1120)
JAVA-4963
1 parent 136cafa commit 103eb33

File tree

3 files changed

+3
-49
lines changed

3 files changed

+3
-49
lines changed

driver-core/src/main/com/mongodb/internal/connection/ServerAddressWithResolver.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public InetSocketAddress getSocketAddress() {
6262

6363
@Override
6464
public List<InetSocketAddress> getSocketAddresses() {
65-
if (resolver == null || isIpLiteral()) {
65+
if (resolver == null) {
6666
return super.getSocketAddresses();
6767
}
6868
try {
@@ -74,11 +74,6 @@ public List<InetSocketAddress> getSocketAddresses() {
7474
}
7575
}
7676

77-
// If this returns true, it's either an IP literal or a malformed hostname. But either way, skip lookup via resolver
78-
private boolean isIpLiteral() {
79-
return getHost().charAt(0) == '[' || Character.digit(getHost().charAt(0), 16) != -1 || (getHost().charAt(0) == ':');
80-
}
81-
8277
@Override
8378
public boolean equals(final Object o) {
8479
if (this == o) {

driver-core/src/main/com/mongodb/spi/dns/InetAddressResolver.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ public interface InetAddressResolver {
3838
* Given the name of a host, returns a list of IP addresses of the requested
3939
* address family associated with a provided hostname.
4040
*
41+
* <p>The host name can be an IP literal, as with {@link InetAddress#getAllByName(String)}</p>
42+
*
4143
* <p>Implementations are encouraged to implement their own caching policies, as there is
4244
* no guarantee that the caller will implement a cache.
4345
*

driver-sync/src/test/functional/com/mongodb/client/AbstractDnsConfigurationTest.java

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -21,30 +21,22 @@
2121
import com.mongodb.MongoException;
2222
import com.mongodb.MongoSocketException;
2323
import com.mongodb.ServerAddress;
24-
import com.mongodb.connection.ServerConnectionState;
2524
import com.mongodb.connection.ServerDescription;
2625
import com.mongodb.event.ClusterDescriptionChangedEvent;
2726
import com.mongodb.event.ClusterListener;
2827
import com.mongodb.spi.dns.DnsClient;
2928
import com.mongodb.spi.dns.DnsException;
3029
import com.mongodb.spi.dns.InetAddressResolver;
3130
import org.junit.jupiter.api.Test;
32-
import org.junit.jupiter.params.ParameterizedTest;
33-
import org.junit.jupiter.params.provider.ValueSource;
3431

3532
import java.net.UnknownHostException;
3633
import java.util.Collections;
3734
import java.util.concurrent.CompletableFuture;
3835
import java.util.concurrent.ExecutionException;
3936
import java.util.concurrent.TimeoutException;
4037

41-
import static com.mongodb.ClusterFixture.getConnectionString;
42-
import static com.mongodb.ClusterFixture.getServerApi;
43-
import static com.mongodb.ClusterFixture.isStandalone;
4438
import static java.util.concurrent.TimeUnit.SECONDS;
4539
import static org.junit.jupiter.api.Assertions.assertEquals;
46-
import static org.junit.jupiter.api.Assertions.assertTrue;
47-
import static org.junit.jupiter.api.Assumptions.assumeTrue;
4840

4941
@SuppressWarnings("try")
5042
public abstract class AbstractDnsConfigurationTest {
@@ -81,41 +73,6 @@ public void clusterDescriptionChanged(final ClusterDescriptionChangedEvent event
8173
}
8274
}
8375

84-
@ParameterizedTest(name = "InetAddressResolver should not be used to resolve IP literal {0}")
85-
@ValueSource(strings = {"127.0.0.1", "::1", "[0:0:0:0:0:0:0:1]"})
86-
public void testInetAddressResolverDoesNotResolveIpLiteral(final String ipLiteral) throws InterruptedException, ExecutionException,
87-
TimeoutException {
88-
assumeTrue(isStandalone());
89-
assumeTrue(getServerApi() == null);
90-
91-
// should not be invoked for IP literals
92-
InetAddressResolver resolver = host -> {
93-
throw new UnknownHostException();
94-
};
95-
96-
CompletableFuture<Boolean> serverConnectedFuture = new CompletableFuture<>();
97-
MongoClientSettings settings = MongoClientSettings.builder()
98-
.applyConnectionString(getConnectionString())
99-
.applyToClusterSettings(builder ->
100-
builder.hosts(Collections.singletonList(new ServerAddress(ipLiteral)))
101-
.addClusterListener(new ClusterListener() {
102-
@Override
103-
public void clusterDescriptionChanged(final ClusterDescriptionChangedEvent event) {
104-
ServerDescription serverDescription = event.getNewDescription().getServerDescriptions().get(0);
105-
// If the resolver that throws an exception is invoked, the state will not be CONNECTED
106-
if (serverDescription.getState() == ServerConnectionState.CONNECTED) {
107-
serverConnectedFuture.complete(true);
108-
}
109-
}
110-
}))
111-
.inetAddressResolver(resolver)
112-
.build();
113-
114-
try (MongoClient ignored = createMongoClient(settings)) {
115-
assertTrue(serverConnectedFuture.get(1, SECONDS));
116-
}
117-
}
118-
11976
@Test
12077
public void testDnsClientConfiguration() throws InterruptedException, ExecutionException, TimeoutException {
12178
DnsException exception = new DnsException("", new Exception());

0 commit comments

Comments
 (0)