use of org.apache.directory.server.dns.messages.DnsMessage in project netty by netty.
the class DnsNameResolverTest method testMultipleAdditionalRecordsForSameNSRecord.
private static void testMultipleAdditionalRecordsForSameNSRecord(final boolean reversed) throws Exception {
final String domain = "netty.io";
final String hostname = "test.netty.io";
final String ns1Name = "ns1." + domain;
final InetSocketAddress ns1Address = new InetSocketAddress(InetAddress.getByAddress(ns1Name, new byte[] { 10, 0, 0, 1 }), DefaultDnsServerAddressStreamProvider.DNS_PORT);
final InetSocketAddress ns2Address = new InetSocketAddress(InetAddress.getByAddress(ns1Name, new byte[] { 10, 0, 0, 2 }), DefaultDnsServerAddressStreamProvider.DNS_PORT);
final InetSocketAddress ns3Address = new InetSocketAddress(InetAddress.getByAddress(ns1Name, new byte[] { 10, 0, 0, 3 }), DefaultDnsServerAddressStreamProvider.DNS_PORT);
final InetSocketAddress ns4Address = new InetSocketAddress(InetAddress.getByAddress(ns1Name, new byte[] { 10, 0, 0, 4 }), DefaultDnsServerAddressStreamProvider.DNS_PORT);
TestDnsServer redirectServer = new TestDnsServer(new HashSet<String>(asList(hostname, ns1Name))) {
@Override
protected DnsMessage filterMessage(DnsMessage message) {
for (QuestionRecord record : message.getQuestionRecords()) {
if (record.getDomainName().equals(hostname)) {
message.getAdditionalRecords().clear();
message.getAnswerRecords().clear();
message.getAuthorityRecords().add(TestDnsServer.newNsRecord(domain, ns1Name));
message.getAdditionalRecords().add(newARecord(ns1Address));
message.getAdditionalRecords().add(newARecord(ns2Address));
message.getAdditionalRecords().add(newARecord(ns3Address));
message.getAdditionalRecords().add(newARecord(ns4Address));
return message;
}
}
return message;
}
private ResourceRecord newARecord(InetSocketAddress address) {
return newARecord(address.getHostName(), address.getAddress().getHostAddress());
}
};
redirectServer.start();
EventLoopGroup group = new NioEventLoopGroup(1);
final List<InetSocketAddress> cached = new CopyOnWriteArrayList<InetSocketAddress>();
final AuthoritativeDnsServerCache authoritativeDnsServerCache = new AuthoritativeDnsServerCache() {
@Override
public DnsServerAddressStream get(String hostname) {
return null;
}
@Override
public void cache(String hostname, InetSocketAddress address, long originalTtl, EventLoop loop) {
cached.add(address);
}
@Override
public void clear() {
// NOOP
}
@Override
public boolean clear(String hostname) {
return false;
}
};
final AtomicReference<DnsServerAddressStream> redirectedRef = new AtomicReference<DnsServerAddressStream>();
final DnsNameResolver resolver = new DnsNameResolver(group.next(), new ReflectiveChannelFactory<DatagramChannel>(NioDatagramChannel.class), NoopDnsCache.INSTANCE, authoritativeDnsServerCache, NoopDnsQueryLifecycleObserverFactory.INSTANCE, 2000, ResolvedAddressTypes.IPV4_ONLY, true, 10, true, 4096, false, HostsFileEntriesResolver.DEFAULT, new SingletonDnsServerAddressStreamProvider(redirectServer.localAddress()), DnsNameResolver.DEFAULT_SEARCH_DOMAINS, 0, true) {
@Override
protected DnsServerAddressStream newRedirectDnsServerStream(String hostname, List<InetSocketAddress> nameservers) {
if (reversed) {
Collections.reverse(nameservers);
}
DnsServerAddressStream stream = new SequentialDnsServerAddressStream(nameservers, 0);
redirectedRef.set(stream);
return stream;
}
};
try {
Throwable cause = resolver.resolveAll(hostname).await().cause();
assertTrue(cause instanceof UnknownHostException);
DnsServerAddressStream redirected = redirectedRef.get();
assertNotNull(redirected);
assertEquals(4, redirected.size());
assertEquals(4, cached.size());
if (reversed) {
assertEquals(ns4Address, redirected.next());
assertEquals(ns3Address, redirected.next());
assertEquals(ns2Address, redirected.next());
assertEquals(ns1Address, redirected.next());
} else {
assertEquals(ns1Address, redirected.next());
assertEquals(ns2Address, redirected.next());
assertEquals(ns3Address, redirected.next());
assertEquals(ns4Address, redirected.next());
}
// We should always have the same order in the cache.
assertEquals(ns1Address, cached.get(0));
assertEquals(ns2Address, cached.get(1));
assertEquals(ns3Address, cached.get(2));
assertEquals(ns4Address, cached.get(3));
} finally {
resolver.close();
group.shutdownGracefully(0, 0, TimeUnit.SECONDS);
redirectServer.stop();
}
}
Aggregations