Search in sources :

Example 6 with ResourceRecordModifier

use of org.apache.directory.server.dns.messages.ResourceRecordModifier in project netty by netty.

the class DnsNameResolverTest method testAllNameServers.

@Test
public void testAllNameServers() throws IOException {
    final String domain = "netty.io";
    final String ipv4Addr = "1.2.3.4";
    final AtomicInteger server2Counter = new AtomicInteger();
    final TestDnsServer dnsServer2 = new TestDnsServer(new RecordStore() {

        @Override
        public Set<ResourceRecord> getRecords(QuestionRecord question) {
            server2Counter.incrementAndGet();
            ResourceRecordModifier rm = new ResourceRecordModifier();
            rm.setDnsClass(RecordClass.IN);
            rm.setDnsName(question.getDomainName());
            rm.setDnsTtl(100);
            rm.setDnsType(question.getRecordType());
            rm.put(DnsAttribute.IP_ADDRESS, ipv4Addr);
            return Collections.singleton(rm.getEntry());
        }
    });
    dnsServer2.start();
    final AtomicInteger server3Counter = new AtomicInteger();
    final TestDnsServer dnsServer3 = new TestDnsServer(new RecordStore() {

        @Override
        public Set<ResourceRecord> getRecords(QuestionRecord question) {
            server3Counter.incrementAndGet();
            ResourceRecordModifier rm = new ResourceRecordModifier();
            rm.setDnsClass(RecordClass.IN);
            rm.setDnsName(question.getDomainName());
            rm.setDnsTtl(100);
            rm.setDnsType(question.getRecordType());
            rm.put(DnsAttribute.IP_ADDRESS, ipv4Addr);
            return Collections.singleton(rm.getEntry());
        }
    });
    dnsServer3.start();
    DnsNameResolver resolver = null;
    try {
        resolver = newResolver().resolveCache(NoopDnsCache.INSTANCE).cnameCache(NoopDnsCnameCache.INSTANCE).recursionDesired(true).maxQueriesPerResolve(16).nameServerProvider(new DnsServerAddressStreamProvider() {

            private final DnsServerAddresses addresses = DnsServerAddresses.rotational(dnsServer2.localAddress(), dnsServer3.localAddress());

            @Override
            public DnsServerAddressStream nameServerAddressStream(String hostname) {
                return addresses.stream();
            }
        }).resolvedAddressTypes(ResolvedAddressTypes.IPV4_ONLY).build();
        assertResolvedAddress(resolver.resolve(domain).syncUninterruptibly().getNow(), ipv4Addr, domain);
        assertEquals(1, server2Counter.get());
        assertEquals(0, server3Counter.get());
        assertResolvedAddress(resolver.resolve(domain).syncUninterruptibly().getNow(), ipv4Addr, domain);
        assertEquals(1, server2Counter.get());
        assertEquals(1, server3Counter.get());
        assertResolvedAddress(resolver.resolve(domain).syncUninterruptibly().getNow(), ipv4Addr, domain);
        assertEquals(2, server2Counter.get());
        assertEquals(1, server3Counter.get());
        assertResolvedAddress(resolver.resolve(domain).syncUninterruptibly().getNow(), ipv4Addr, domain);
        assertEquals(2, server2Counter.get());
        assertEquals(2, server3Counter.get());
    } finally {
        dnsServer2.stop();
        dnsServer3.stop();
        if (resolver != null) {
            resolver.close();
        }
    }
}
Also used : QuestionRecord(org.apache.directory.server.dns.messages.QuestionRecord) Set(java.util.Set) LinkedHashSet(java.util.LinkedHashSet) HashSet(java.util.HashSet) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RecordStore(org.apache.directory.server.dns.store.RecordStore) ResourceRecordModifier(org.apache.directory.server.dns.messages.ResourceRecordModifier) Test(org.junit.jupiter.api.Test)

Example 7 with ResourceRecordModifier

use of org.apache.directory.server.dns.messages.ResourceRecordModifier in project netty by netty.

the class TestDnsServer method newAddressRecord.

protected static ResourceRecord newAddressRecord(String name, RecordType type, String address) {
    ResourceRecordModifier rm = new ResourceRecordModifier();
    rm.setDnsClass(RecordClass.IN);
    rm.setDnsName(name);
    rm.setDnsTtl(100);
    rm.setDnsType(type);
    rm.put(DnsAttribute.IP_ADDRESS, address);
    return rm.getEntry();
}
Also used : ResourceRecordModifier(org.apache.directory.server.dns.messages.ResourceRecordModifier)

Aggregations

ResourceRecordModifier (org.apache.directory.server.dns.messages.ResourceRecordModifier)7 HashSet (java.util.HashSet)5 LinkedHashSet (java.util.LinkedHashSet)5 Set (java.util.Set)5 QuestionRecord (org.apache.directory.server.dns.messages.QuestionRecord)5 RecordStore (org.apache.directory.server.dns.store.RecordStore)5 Test (org.junit.jupiter.api.Test)3 InetAddress (java.net.InetAddress)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 ReflectiveChannelFactory (io.netty.channel.ReflectiveChannelFactory)1 DefaultDnsQuestion (io.netty.handler.codec.dns.DefaultDnsQuestion)1 InetSocketAddress (java.net.InetSocketAddress)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 DnsException (org.apache.directory.server.dns.DnsException)1 ResourceRecord (org.apache.directory.server.dns.messages.ResourceRecord)1 Timeout (org.junit.jupiter.api.Timeout)1