diff --git a/resolver-dns/src/test/java/io/netty/resolver/dns/DnsNameResolverTest.java b/resolver-dns/src/test/java/io/netty/resolver/dns/DnsNameResolverTest.java index 0ffe40412a..c7a416da8c 100644 --- a/resolver-dns/src/test/java/io/netty/resolver/dns/DnsNameResolverTest.java +++ b/resolver-dns/src/test/java/io/netty/resolver/dns/DnsNameResolverTest.java @@ -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 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 getRecords(QuestionRecord question) { switch (question.getRecordType()) { case A: - rm.put(DnsAttribute.IP_ADDRESS, overriddenIP); - break; + Map attr = new HashMap(); + attr.put(DnsAttribute.IP_ADDRESS.toLowerCase(Locale.US), overriddenIP); + return Collections.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 getRecords(QuestionRecord question) throws DnsException { + public Set 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(Arrays.asList("notnetty.com"))); - final TestDnsServer dnsServer2 = new TestDnsServer(new HashSet(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(Arrays.asList("notnetty.com"))); - final TestDnsServer dnsServer2 = new TestDnsServer(new HashSet(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>> futures, - String hostname) throws Exception { + String hostname) { futures.put(hostname, resolver.query(new DefaultDnsQuestion(hostname, DnsRecordType.MX))); } diff --git a/resolver-dns/src/test/java/io/netty/resolver/dns/TestDnsServer.java b/resolver-dns/src/test/java/io/netty/resolver/dns/TestDnsServer.java index 7cbe3ad86a..dd351ae242 100644 --- a/resolver-dns/src/test/java/io/netty/resolver/dns/TestDnsServer.java +++ b/resolver-dns/src/test/java/io/netty/resolver/dns/TestDnsServer.java @@ -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> domainMap; - public MapRecordStoreA(Set domains, int length) { + MapRecordStoreA(Set domains, int length) { domainMap = new HashMap>(domains.size()); for (String domain : domains) { List addresses = new ArrayList(length); @@ -183,7 +182,7 @@ class TestDnsServer extends DnsServer { } } - public MapRecordStoreA(Set domains) { + MapRecordStoreA(Set domains) { this(domains, 1); } @@ -196,25 +195,15 @@ class TestDnsServer extends DnsServer { } @Override - public Set getRecords(QuestionRecord questionRecord) throws DnsException { + public Set getRecords(QuestionRecord questionRecord) { String name = questionRecord.getDomainName(); List addresses = domainMap.get(name); if (addresses != null && questionRecord.getRecordType() == RecordType.A) { Set records = new LinkedHashSet(); for (String address : addresses) { - HashMap attributes = new HashMap(); + Map attributes = new HashMap(); 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 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 attr = new HashMap(); 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.singleton( + new TestResourceRecord(name, questionRecord.getRecordType(), attr)); } return null; } } + + static final class TestResourceRecord extends ResourceRecordImpl { + + TestResourceRecord(String domainName, RecordType recordType, Map 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; + } + } }