Cleanup resolver-dns test code. (#7916)

Motivation:

We had some stuff that could be cleaned up in the resolver-dns test classes.

Modifications:

Cleanup code.

Result:

Cleaner code.
This commit is contained in:
Norman Maurer 2018-05-08 08:55:17 +02:00 committed by GitHub
parent 54c3de0f8a
commit 18b170bd7a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 52 additions and 55 deletions

View File

@ -67,20 +67,18 @@ import java.net.UnknownHostException;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Queue;
import java.util.Set;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
@ -379,20 +377,17 @@ public class DnsNameResolverTest {
final String overriddenIP = "12.34.12.34";
final TestDnsServer dnsServer2 = new TestDnsServer(new RecordStore() {
@Override
public Set<ResourceRecord> getRecords(QuestionRecord question) throws DnsException {
ResourceRecordModifier rm = new ResourceRecordModifier();
rm.setDnsClass(RecordClass.IN);
rm.setDnsName(question.getDomainName());
rm.setDnsTtl(100);
rm.setDnsType(question.getRecordType());
public Set<ResourceRecord> getRecords(QuestionRecord question) {
switch (question.getRecordType()) {
case A:
rm.put(DnsAttribute.IP_ADDRESS, overriddenIP);
break;
Map<String, Object> attr = new HashMap<String, Object>();
attr.put(DnsAttribute.IP_ADDRESS.toLowerCase(Locale.US), overriddenIP);
return Collections.<ResourceRecord>singleton(
new TestDnsServer.TestResourceRecord(
question.getDomainName(), question.getRecordType(), attr));
default:
return null;
}
return Collections.singleton(rm.getEntry());
}
});
dnsServer2.start();
@ -496,7 +491,7 @@ public class DnsNameResolverTest {
testNonCachedResolveEmptyHostName(null);
}
public void testNonCachedResolveEmptyHostName(String inetHost) throws Exception {
private static void testNonCachedResolveEmptyHostName(String inetHost) throws Exception {
DnsNameResolver resolver = newNonCachedResolver(ResolvedAddressTypes.IPV4_ONLY).build();
try {
InetAddress addr = resolver.resolve(inetHost).syncUninterruptibly().getNow();
@ -753,16 +748,16 @@ public class DnsNameResolverTest {
}
@Test
public void testCNAMEResolveAllIpv4() throws IOException, InterruptedException {
public void testCNAMEResolveAllIpv4() throws IOException {
testCNAMERecursiveResolve(true);
}
@Test
public void testCNAMEResolveAllIpv6() throws IOException, InterruptedException {
public void testCNAMEResolveAllIpv6() throws IOException {
testCNAMERecursiveResolve(false);
}
private static void testCNAMERecursiveResolve(boolean ipv4Preferred) throws IOException, InterruptedException {
private static void testCNAMERecursiveResolve(boolean ipv4Preferred) throws IOException {
final String firstName = "firstname.com";
final String secondName = "secondname.com";
final String lastName = "lastname.com";
@ -770,7 +765,7 @@ public class DnsNameResolverTest {
final String ipv6Addr = "::1";
TestDnsServer dnsServer2 = new TestDnsServer(new RecordStore() {
@Override
public Set<ResourceRecord> getRecords(QuestionRecord question) throws DnsException {
public Set<ResourceRecord> getRecords(QuestionRecord question) {
ResourceRecordModifier rm = new ResourceRecordModifier();
rm.setDnsClass(RecordClass.IN);
rm.setDnsName(question.getDomainName());
@ -1067,8 +1062,8 @@ public class DnsNameResolverTest {
private static void secondDnsServerShouldBeUsedBeforeCNAME(boolean startDnsServer1) throws IOException {
final String knownHostName = "netty.io";
final TestDnsServer dnsServer1 = new TestDnsServer(new HashSet<String>(Arrays.asList("notnetty.com")));
final TestDnsServer dnsServer2 = new TestDnsServer(new HashSet<String>(Arrays.asList(knownHostName)));
final TestDnsServer dnsServer1 = new TestDnsServer(Collections.singleton("notnetty.com"));
final TestDnsServer dnsServer2 = new TestDnsServer(Collections.singleton(knownHostName));
DnsNameResolver resolver = null;
try {
final InetSocketAddress dnsServer1Address;
@ -1122,8 +1117,8 @@ public class DnsNameResolverTest {
@Test(timeout = DEFAULT_TEST_TIMEOUT_MS)
public void aAndAAAAQueryShouldTryFirstDnsServerBeforeSecond() throws IOException {
final String knownHostName = "netty.io";
final TestDnsServer dnsServer1 = new TestDnsServer(new HashSet<String>(Arrays.asList("notnetty.com")));
final TestDnsServer dnsServer2 = new TestDnsServer(new HashSet<String>(Arrays.asList(knownHostName)));
final TestDnsServer dnsServer1 = new TestDnsServer(Collections.singleton("notnetty.com"));
final TestDnsServer dnsServer2 = new TestDnsServer(Collections.singleton(knownHostName));
DnsNameResolver resolver = null;
try {
dnsServer1.start();
@ -1347,7 +1342,7 @@ public class DnsNameResolverTest {
private static void queryMx(
DnsNameResolver resolver,
Map<String, Future<AddressedEnvelope<DnsResponse, InetSocketAddress>>> futures,
String hostname) throws Exception {
String hostname) {
futures.put(hostname, resolver.query(new DefaultDnsQuestion(hostname, DnsRecordType.MX)));
}

View File

@ -17,7 +17,6 @@ package io.netty.resolver.dns;
import io.netty.util.NetUtil;
import io.netty.util.internal.PlatformDependent;
import org.apache.directory.server.dns.DnsException;
import org.apache.directory.server.dns.DnsServer;
import org.apache.directory.server.dns.io.encoder.DnsMessageEncoder;
import org.apache.directory.server.dns.io.encoder.ResourceRecordEncoder;
@ -27,7 +26,6 @@ import org.apache.directory.server.dns.messages.RecordClass;
import org.apache.directory.server.dns.messages.RecordType;
import org.apache.directory.server.dns.messages.ResourceRecord;
import org.apache.directory.server.dns.messages.ResourceRecordImpl;
import org.apache.directory.server.dns.messages.ResourceRecordModifier;
import org.apache.directory.server.dns.protocol.DnsProtocolHandler;
import org.apache.directory.server.dns.protocol.DnsUdpDecoder;
import org.apache.directory.server.dns.protocol.DnsUdpEncoder;
@ -51,6 +49,7 @@ import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
@ -91,7 +90,7 @@ class TestDnsServer extends DnsServer {
acceptor.setHandler(new DnsProtocolHandler(this, store) {
@Override
public void sessionCreated(IoSession session) throws Exception {
public void sessionCreated(IoSession session) {
// USe our own codec to support AAAA testing
session.getFilterChain()
.addFirst("codec", new ProtocolCodecFilter(new TestDnsProtocolUdpCodecFactory()));
@ -120,7 +119,7 @@ class TestDnsServer extends DnsServer {
private final TestAAAARecordEncoder recordEncoder = new TestAAAARecordEncoder();
@Override
public ProtocolEncoder getEncoder(IoSession session) throws Exception {
public ProtocolEncoder getEncoder(IoSession session) {
return new DnsUdpEncoder() {
@Override
@ -150,7 +149,7 @@ class TestDnsServer extends DnsServer {
}
@Override
public ProtocolDecoder getDecoder(IoSession session) throws Exception {
public ProtocolDecoder getDecoder(IoSession session) {
return new DnsUdpDecoder();
}
@ -160,7 +159,7 @@ class TestDnsServer extends DnsServer {
protected void putResourceRecordData(IoBuffer ioBuffer, ResourceRecord resourceRecord) {
byte[] bytes = BYTES.get(resourceRecord.get(DnsAttribute.IP_ADDRESS));
if (bytes == null) {
throw new IllegalStateException();
throw new IllegalStateException(resourceRecord.get(DnsAttribute.IP_ADDRESS));
}
// encode the ::1
ioBuffer.put(bytes);
@ -168,11 +167,11 @@ class TestDnsServer extends DnsServer {
}
}
public static final class MapRecordStoreA implements RecordStore {
static final class MapRecordStoreA implements RecordStore {
private final Map<String, List<String>> domainMap;
public MapRecordStoreA(Set<String> domains, int length) {
MapRecordStoreA(Set<String> domains, int length) {
domainMap = new HashMap<String, List<String>>(domains.size());
for (String domain : domains) {
List<String> addresses = new ArrayList<String>(length);
@ -183,7 +182,7 @@ class TestDnsServer extends DnsServer {
}
}
public MapRecordStoreA(Set<String> domains) {
MapRecordStoreA(Set<String> domains) {
this(domains, 1);
}
@ -196,25 +195,15 @@ class TestDnsServer extends DnsServer {
}
@Override
public Set<ResourceRecord> getRecords(QuestionRecord questionRecord) throws DnsException {
public Set<ResourceRecord> getRecords(QuestionRecord questionRecord) {
String name = questionRecord.getDomainName();
List<String> addresses = domainMap.get(name);
if (addresses != null && questionRecord.getRecordType() == RecordType.A) {
Set<ResourceRecord> records = new LinkedHashSet<ResourceRecord>();
for (String address : addresses) {
HashMap<String, Object> attributes = new HashMap<String, Object>();
Map<String, Object> attributes = new HashMap<String, Object>();
attributes.put(DnsAttribute.IP_ADDRESS.toLowerCase(), address);
records.add(new ResourceRecordImpl(name, questionRecord.getRecordType(),
RecordClass.IN, 100, attributes) {
@Override
public int hashCode() {
return System.identityHashCode(this);
}
@Override
public boolean equals(Object o) {
return false;
}
});
records.add(new TestResourceRecord(name, questionRecord.getRecordType(), attributes));
}
return records;
}
@ -266,36 +255,49 @@ class TestDnsServer extends DnsServer {
public Set<ResourceRecord> getRecords(QuestionRecord questionRecord) {
String name = questionRecord.getDomainName();
if (domains.contains(name)) {
ResourceRecordModifier rm = new ResourceRecordModifier();
rm.setDnsClass(RecordClass.IN);
rm.setDnsName(name);
rm.setDnsTtl(100);
rm.setDnsType(questionRecord.getRecordType());
Map<String, Object> attr = new HashMap<String, Object>();
switch (questionRecord.getRecordType()) {
case A:
do {
rm.put(DnsAttribute.IP_ADDRESS, nextIp());
attr.put(DnsAttribute.IP_ADDRESS.toLowerCase(Locale.US), nextIp());
} while (PlatformDependent.threadLocalRandom().nextBoolean());
break;
case AAAA:
do {
rm.put(DnsAttribute.IP_ADDRESS, nextIp6());
attr.put(DnsAttribute.IP_ADDRESS.toLowerCase(Locale.US), nextIp6());
} while (PlatformDependent.threadLocalRandom().nextBoolean());
break;
case MX:
int priority = 0;
do {
rm.put(DnsAttribute.DOMAIN_NAME, nextDomain());
rm.put(DnsAttribute.MX_PREFERENCE, String.valueOf(++priority));
attr.put(DnsAttribute.DOMAIN_NAME.toLowerCase(Locale.US), nextDomain());
attr.put(DnsAttribute.MX_PREFERENCE.toLowerCase(Locale.US), String.valueOf(++priority));
} while (PlatformDependent.threadLocalRandom().nextBoolean());
break;
default:
return null;
}
return Collections.singleton(rm.getEntry());
return Collections.<ResourceRecord>singleton(
new TestResourceRecord(name, questionRecord.getRecordType(), attr));
}
return null;
}
}
static final class TestResourceRecord extends ResourceRecordImpl {
TestResourceRecord(String domainName, RecordType recordType, Map<String, Object> attributes) {
super(domainName, recordType, RecordClass.IN, 100, attributes);
}
@Override
public int hashCode() {
return System.identityHashCode(this);
}
@Override
public boolean equals(Object o) {
return o == this;
}
}
}