Search in sources :

Example 1 with ResourceRecord

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

the class DnsNameResolverTest method testIncludeDuplicates.

@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();
        }
    }
}
Also used : QuestionRecord(org.apache.directory.server.dns.messages.QuestionRecord) Set(java.util.Set) LinkedHashSet(java.util.LinkedHashSet) HashSet(java.util.HashSet) ResourceRecord(org.apache.directory.server.dns.messages.ResourceRecord) RecordStore(org.apache.directory.server.dns.store.RecordStore) DnsRecord(io.netty.handler.codec.dns.DnsRecord) DefaultDnsQuestion(io.netty.handler.codec.dns.DefaultDnsQuestion) Test(org.junit.jupiter.api.Test)

Example 2 with ResourceRecord

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

the class DnsNameResolverTest method testResolvesPreferredWhenNonPreferredFirst0.

private static void testResolvesPreferredWhenNonPreferredFirst0(ResolvedAddressTypes types) throws Exception {
    final String name = "netty.com";
    // This store is non-compliant, returning records of the wrong type for a query.
    // It works since we don't verify the type of the result when resolving to deal with
    // non-compliant servers in the wild.
    List<Set<ResourceRecord>> records = new ArrayList<Set<ResourceRecord>>();
    final String ipv6Address = "0:0:0:0:0:0:1:1";
    final String ipv4Address = "1.1.1.1";
    if (types == ResolvedAddressTypes.IPV4_PREFERRED) {
        records.add(Collections.singleton(TestDnsServer.newAddressRecord(name, RecordType.AAAA, ipv6Address)));
        records.add(Collections.singleton(TestDnsServer.newAddressRecord(name, RecordType.A, ipv4Address)));
    } else {
        records.add(Collections.singleton(TestDnsServer.newAddressRecord(name, RecordType.A, ipv4Address)));
        records.add(Collections.singleton(TestDnsServer.newAddressRecord(name, RecordType.AAAA, ipv6Address)));
    }
    final Iterator<Set<ResourceRecord>> recordsIterator = records.iterator();
    RecordStore arbitrarilyOrderedStore = new RecordStore() {

        @Override
        public Set<ResourceRecord> getRecords(QuestionRecord questionRecord) {
            return recordsIterator.next();
        }
    };
    TestDnsServer nonCompliantDnsServer = new TestDnsServer(arbitrarilyOrderedStore);
    nonCompliantDnsServer.start();
    try {
        DnsNameResolver resolver = newResolver(types).maxQueriesPerResolve(2).nameServerProvider(new SingletonDnsServerAddressStreamProvider(nonCompliantDnsServer.localAddress())).build();
        InetAddress resolved = resolver.resolve("netty.com").syncUninterruptibly().getNow();
        if (types == ResolvedAddressTypes.IPV4_PREFERRED) {
            assertEquals(ipv4Address, resolved.getHostAddress());
        } else {
            assertEquals(ipv6Address, resolved.getHostAddress());
        }
        InetAddress ipv4InetAddress = InetAddress.getByAddress("netty.com", InetAddress.getByName(ipv4Address).getAddress());
        InetAddress ipv6InetAddress = InetAddress.getByAddress("netty.com", InetAddress.getByName(ipv6Address).getAddress());
        List<InetAddress> resolvedAll = resolver.resolveAll("netty.com").syncUninterruptibly().getNow();
        List<InetAddress> expected = types == ResolvedAddressTypes.IPV4_PREFERRED ? asList(ipv4InetAddress, ipv6InetAddress) : asList(ipv6InetAddress, ipv4InetAddress);
        assertEquals(expected, resolvedAll);
    } finally {
        nonCompliantDnsServer.stop();
    }
}
Also used : QuestionRecord(org.apache.directory.server.dns.messages.QuestionRecord) Set(java.util.Set) LinkedHashSet(java.util.LinkedHashSet) HashSet(java.util.HashSet) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) ResourceRecord(org.apache.directory.server.dns.messages.ResourceRecord) RecordStore(org.apache.directory.server.dns.store.RecordStore) InetAddress(java.net.InetAddress)

Example 3 with ResourceRecord

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

the class DnsNameResolverTest method testQueryTxt.

@Test
public void testQueryTxt() throws Exception {
    final String hostname = "txt.netty.io";
    final String txt1 = "some text";
    final String txt2 = "some more text";
    TestDnsServer server = new TestDnsServer(new RecordStore() {

        @Override
        public Set<ResourceRecord> getRecords(QuestionRecord question) {
            if (question.getDomainName().equals(hostname)) {
                Map<String, Object> map1 = new HashMap<String, Object>();
                map1.put(DnsAttribute.CHARACTER_STRING.toLowerCase(), txt1);
                Map<String, Object> map2 = new HashMap<String, Object>();
                map2.put(DnsAttribute.CHARACTER_STRING.toLowerCase(), txt2);
                Set<ResourceRecord> records = new HashSet<ResourceRecord>();
                records.add(new TestDnsServer.TestResourceRecord(question.getDomainName(), RecordType.TXT, map1));
                records.add(new TestDnsServer.TestResourceRecord(question.getDomainName(), RecordType.TXT, map2));
                return records;
            }
            return Collections.emptySet();
        }
    });
    server.start();
    DnsNameResolver resolver = newResolver(ResolvedAddressTypes.IPV4_ONLY).nameServerProvider(new SingletonDnsServerAddressStreamProvider(server.localAddress())).build();
    try {
        AddressedEnvelope<DnsResponse, InetSocketAddress> envelope = resolver.query(new DefaultDnsQuestion(hostname, DnsRecordType.TXT)).syncUninterruptibly().getNow();
        assertNotNull(envelope.sender());
        DnsResponse response = envelope.content();
        assertNotNull(response);
        assertEquals(DnsResponseCode.NOERROR, response.code());
        int count = response.count(DnsSection.ANSWER);
        assertEquals(2, count);
        List<String> txts = new ArrayList<String>();
        for (int i = 0; i < 2; i++) {
            txts.addAll(decodeTxt(response.recordAt(DnsSection.ANSWER, i)));
        }
        assertTrue(txts.contains(txt1));
        assertTrue(txts.contains(txt2));
        envelope.release();
    } finally {
        resolver.close();
        server.stop();
    }
}
Also used : QuestionRecord(org.apache.directory.server.dns.messages.QuestionRecord) Set(java.util.Set) LinkedHashSet(java.util.LinkedHashSet) HashSet(java.util.HashSet) InetSocketAddress(java.net.InetSocketAddress) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) ResourceRecord(org.apache.directory.server.dns.messages.ResourceRecord) RecordStore(org.apache.directory.server.dns.store.RecordStore) DnsResponse(io.netty.handler.codec.dns.DnsResponse) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) DefaultDnsQuestion(io.netty.handler.codec.dns.DefaultDnsQuestion) Test(org.junit.jupiter.api.Test)

Example 4 with ResourceRecord

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

the class DnsNameResolverTest method testFollowCNAMELoop.

@Test
public void testFollowCNAMELoop() throws IOException {
    TestDnsServer dnsServer2 = new TestDnsServer(new RecordStore() {

        @Override
        public Set<ResourceRecord> getRecords(QuestionRecord question) {
            Set<ResourceRecord> records = new LinkedHashSet<ResourceRecord>(4);
            records.add(new TestDnsServer.TestResourceRecord("x." + question.getDomainName(), RecordType.A, Collections.<String, Object>singletonMap(DnsAttribute.IP_ADDRESS.toLowerCase(), "10.0.0.99")));
            records.add(new TestDnsServer.TestResourceRecord("cname2.netty.io", RecordType.CNAME, Collections.<String, Object>singletonMap(DnsAttribute.DOMAIN_NAME.toLowerCase(), "cname.netty.io")));
            records.add(new TestDnsServer.TestResourceRecord("cname.netty.io", RecordType.CNAME, Collections.<String, Object>singletonMap(DnsAttribute.DOMAIN_NAME.toLowerCase(), "cname2.netty.io")));
            records.add(new TestDnsServer.TestResourceRecord(question.getDomainName(), RecordType.CNAME, Collections.<String, Object>singletonMap(DnsAttribute.DOMAIN_NAME.toLowerCase(), "cname.netty.io")));
            return records;
        }
    });
    dnsServer2.start();
    DnsNameResolver resolver = null;
    try {
        DnsNameResolverBuilder builder = newResolver().recursionDesired(false).resolvedAddressTypes(ResolvedAddressTypes.IPV4_ONLY).maxQueriesPerResolve(16).nameServerProvider(new SingletonDnsServerAddressStreamProvider(dnsServer2.localAddress()));
        resolver = builder.build();
        final DnsNameResolver finalResolver = resolver;
        assertThrows(UnknownHostException.class, new Executable() {

            @Override
            public void execute() throws Throwable {
                finalResolver.resolveAll("somehost.netty.io").syncUninterruptibly().getNow();
            }
        });
    } finally {
        dnsServer2.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) ResourceRecord(org.apache.directory.server.dns.messages.ResourceRecord) RecordStore(org.apache.directory.server.dns.store.RecordStore) Executable(org.junit.jupiter.api.function.Executable) Test(org.junit.jupiter.api.Test)

Example 5 with ResourceRecord

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

the class DnsNameResolverTest method testDropAAAAResolveAllFast.

@Test
@Timeout(value = 2000, unit = TimeUnit.MILLISECONDS)
public void testDropAAAAResolveAllFast() throws IOException {
    final String host = "somehost.netty.io";
    TestDnsServer dnsServer2 = new TestDnsServer(new RecordStore() {

        @Override
        public Set<ResourceRecord> getRecords(QuestionRecord question) throws DnsException {
            String name = question.getDomainName();
            if (name.equals(host)) {
                Set<ResourceRecord> records = new HashSet<ResourceRecord>(2);
                records.add(new TestDnsServer.TestResourceRecord(name, RecordType.A, Collections.<String, Object>singletonMap(DnsAttribute.IP_ADDRESS.toLowerCase(), "10.0.0.1")));
                records.add(new TestDnsServer.TestResourceRecord(name, RecordType.A, Collections.<String, Object>singletonMap(DnsAttribute.IP_ADDRESS.toLowerCase(), "10.0.0.2")));
                return records;
            }
            return null;
        }
    });
    dnsServer2.start(true);
    DnsNameResolver resolver = null;
    try {
        DnsNameResolverBuilder builder = newResolver().recursionDesired(false).queryTimeoutMillis(10000).resolvedAddressTypes(ResolvedAddressTypes.IPV4_PREFERRED).completeOncePreferredResolved(true).maxQueriesPerResolve(16).nameServerProvider(new SingletonDnsServerAddressStreamProvider(dnsServer2.localAddress()));
        resolver = builder.build();
        List<InetAddress> addresses = resolver.resolveAll(host).syncUninterruptibly().getNow();
        assertEquals(2, addresses.size());
        for (InetAddress address : addresses) {
            assertThat(address, instanceOf(Inet4Address.class));
            assertEquals(host, address.getHostName());
        }
    } finally {
        dnsServer2.stop();
        if (resolver != null) {
            resolver.close();
        }
    }
}
Also used : QuestionRecord(org.apache.directory.server.dns.messages.QuestionRecord) Inet4Address(java.net.Inet4Address) Set(java.util.Set) LinkedHashSet(java.util.LinkedHashSet) HashSet(java.util.HashSet) ResourceRecord(org.apache.directory.server.dns.messages.ResourceRecord) DnsException(org.apache.directory.server.dns.DnsException) RecordStore(org.apache.directory.server.dns.store.RecordStore) InetAddress(java.net.InetAddress) Test(org.junit.jupiter.api.Test) Timeout(org.junit.jupiter.api.Timeout)

Aggregations

ResourceRecord (org.apache.directory.server.dns.messages.ResourceRecord)11 QuestionRecord (org.apache.directory.server.dns.messages.QuestionRecord)10 RecordStore (org.apache.directory.server.dns.store.RecordStore)10 HashSet (java.util.HashSet)9 LinkedHashSet (java.util.LinkedHashSet)9 Set (java.util.Set)9 Test (org.junit.jupiter.api.Test)7 InetAddress (java.net.InetAddress)6 DefaultDnsQuestion (io.netty.handler.codec.dns.DefaultDnsQuestion)3 InetSocketAddress (java.net.InetSocketAddress)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)2 Bootstrap (io.netty.bootstrap.Bootstrap)1 Channel (io.netty.channel.Channel)1 ChannelFuture (io.netty.channel.ChannelFuture)1 ChannelInitializer (io.netty.channel.ChannelInitializer)1