DnsNameResolver.resolveAll(DnsQuestion) should not try to filter duplicates (#9141)

Motivation:

https://github.com/netty/netty/pull/9021 did apply some changes to filter out duplicates InetAddress when calling resolveAll(...) to mimic JDK behaviour. Unfortunally this also introduced a regression as we should not filter duplicates when the user explicit calls resolveAll(DnsQuestion).

Modifications:

- Only filter duplicates if resolveAll(String) is used
- Add unit test

Result:

Fixes regressions introduces by https://github.com/netty/netty/pull/9021
This commit is contained in:
Norman Maurer 2019-05-13 06:59:06 +02:00
parent d3a13a0d6a
commit debc1cbc0a
4 changed files with 60 additions and 1 deletions

View File

@ -67,6 +67,12 @@ final class DnsAddressResolveContext extends DnsResolveContext<InetAddress> {
return completeEarlyIfPossible && parent.preferredAddressType().addressType() == resolved.getClass(); return completeEarlyIfPossible && parent.preferredAddressType().addressType() == resolved.getClass();
} }
@Override
boolean isDuplicateAllowed() {
// We don't want include duplicates to mimic JDK behaviour.
return false;
}
@Override @Override
void cache(String hostname, DnsRecord[] additionals, void cache(String hostname, DnsRecord[] additionals,
DnsRecord result, InetAddress convertedResult) { DnsRecord result, InetAddress convertedResult) {

View File

@ -63,6 +63,11 @@ final class DnsRecordResolveContext extends DnsResolveContext<DnsRecord> {
return false; return false;
} }
@Override
boolean isDuplicateAllowed() {
return true;
}
@Override @Override
void cache(String hostname, DnsRecord[] additionals, DnsRecord result, DnsRecord convertedResult) { void cache(String hostname, DnsRecord[] additionals, DnsRecord result, DnsRecord convertedResult) {
// Do not cache. // Do not cache.

View File

@ -159,6 +159,12 @@ abstract class DnsResolveContext<T> {
abstract boolean isCompleteEarly(T resolved); abstract boolean isCompleteEarly(T resolved);
/**
* Returns {@code true} if we should allow duplicates in the result or {@code false} if no duplicates should
* be included.
*/
abstract boolean isDuplicateAllowed();
/** /**
* Caches a successful resolution. * Caches a successful resolution.
*/ */
@ -699,7 +705,7 @@ abstract class DnsResolveContext<T> {
if (finalResult == null) { if (finalResult == null) {
finalResult = new ArrayList<>(8); finalResult = new ArrayList<>(8);
finalResult.add(converted); finalResult.add(converted);
} else if (!finalResult.contains(converted)) { } else if (isDuplicateAllowed() || !finalResult.contains(converted)) {
finalResult.add(converted); finalResult.add(converted);
} else { } else {
shouldRelease = true; shouldRelease = true;

View File

@ -2567,6 +2567,48 @@ public class DnsNameResolverTest {
} }
} }
@Test
public void testIncludeDuplicates() throws IOException {
final String name = "netty.io";
final String ipv4Addr = "1.2.3.4";
TestDnsServer dnsServer2 = new TestDnsServer(new RecordStore() {
@Override
public Set<ResourceRecord> getRecords(QuestionRecord question) {
Set<ResourceRecord> records = new LinkedHashSet<ResourceRecord>(2);
String qName = question.getDomainName().toLowerCase();
records.add(new TestDnsServer.TestResourceRecord(qName,
RecordType.A, Collections.<String, Object>singletonMap(
DnsAttribute.IP_ADDRESS.toLowerCase(), ipv4Addr)));
records.add(new TestDnsServer.TestResourceRecord(qName,
RecordType.A, Collections.<String, Object>singletonMap(
DnsAttribute.IP_ADDRESS.toLowerCase(), ipv4Addr)));
return records;
}
});
dnsServer2.start();
DnsNameResolver resolver = null;
try {
DnsNameResolverBuilder builder = newResolver()
.recursionDesired(true)
.maxQueriesPerResolve(16)
.nameServerProvider(new SingletonDnsServerAddressStreamProvider(dnsServer2.localAddress()));
builder.resolvedAddressTypes(ResolvedAddressTypes.IPV4_ONLY);
resolver = builder.build();
List<DnsRecord> resolvedAddresses = resolver.resolveAll(new DefaultDnsQuestion(name, A))
.syncUninterruptibly().getNow();
assertEquals(2, resolvedAddresses.size());
for (DnsRecord record: resolvedAddresses) {
ReferenceCountUtil.release(record);
}
} finally {
dnsServer2.stop();
if (resolver != null) {
resolver.close();
}
}
}
@Test @Test
public void testDropAAAA() throws IOException { public void testDropAAAA() throws IOException {
String host = "somehost.netty.io"; String host = "somehost.netty.io";