Search in sources :

Example 6 with RecordStore

use of org.apache.directory.server.dns.store.RecordStore 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 7 with RecordStore

use of org.apache.directory.server.dns.store.RecordStore 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 8 with RecordStore

use of org.apache.directory.server.dns.store.RecordStore in project netty by netty.

the class DnsNameResolverTest method testNameServerCache.

/**
 * This test will start an second DNS test server which returns fixed results that can be easily verified as
 * originating from the second DNS test server. The resolver will put {@link DnsServerAddressStreamProvider} under
 * test to ensure that some hostnames can be directed toward both the primary and secondary DNS test servers
 * simultaneously.
 */
@Test
public void testNameServerCache() throws IOException, InterruptedException {
    final String overriddenIP = "12.34.12.34";
    final TestDnsServer dnsServer2 = new TestDnsServer(new RecordStore() {

        @Override
        public Set<ResourceRecord> getRecords(QuestionRecord question) {
            switch(question.getRecordType()) {
                case A:
                    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;
            }
        }
    });
    dnsServer2.start();
    try {
        final Set<String> overriddenHostnames = new HashSet<String>();
        for (String name : DOMAINS) {
            if (EXCLUSIONS_RESOLVE_A.contains(name)) {
                continue;
            }
            if (PlatformDependent.threadLocalRandom().nextBoolean()) {
                overriddenHostnames.add(name);
            }
        }
        DnsNameResolver resolver = newResolver(false, new DnsServerAddressStreamProvider() {

            @Override
            public DnsServerAddressStream nameServerAddressStream(String hostname) {
                return overriddenHostnames.contains(hostname) ? sequential(dnsServer2.localAddress()).stream() : null;
            }
        }).build();
        try {
            final Map<String, InetAddress> resultA = testResolve0(resolver, EXCLUSIONS_RESOLVE_A, AAAA);
            for (Entry<String, InetAddress> resolvedEntry : resultA.entrySet()) {
                if (resolvedEntry.getValue().isLoopbackAddress()) {
                    continue;
                }
                if (overriddenHostnames.contains(resolvedEntry.getKey())) {
                    assertEquals(overriddenIP, resolvedEntry.getValue().getHostAddress(), "failed to resolve " + resolvedEntry.getKey());
                } else {
                    assertNotEquals(overriddenIP, resolvedEntry.getValue().getHostAddress(), "failed to resolve " + resolvedEntry.getKey());
                }
            }
        } finally {
            resolver.close();
        }
    } finally {
        dnsServer2.stop();
    }
}
Also used : QuestionRecord(org.apache.directory.server.dns.messages.QuestionRecord) Set(java.util.Set) LinkedHashSet(java.util.LinkedHashSet) HashSet(java.util.HashSet) RecordStore(org.apache.directory.server.dns.store.RecordStore) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) InetAddress(java.net.InetAddress) LinkedHashSet(java.util.LinkedHashSet) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 9 with RecordStore

use of org.apache.directory.server.dns.store.RecordStore in project netty by netty.

the class DnsNameResolverTest method testCNAMERecursiveResolve.

private static void testCNAMERecursiveResolve(boolean ipv4Preferred) throws IOException {
    final String firstName = "firstname.com";
    final String secondName = "secondname.com";
    final String lastName = "lastname.com";
    final String ipv4Addr = "1.2.3.4";
    final String ipv6Addr = "::1";
    TestDnsServer dnsServer2 = new TestDnsServer(new RecordStore() {

        @Override
        public Set<ResourceRecord> getRecords(QuestionRecord question) {
            ResourceRecordModifier rm = new ResourceRecordModifier();
            rm.setDnsClass(RecordClass.IN);
            rm.setDnsName(question.getDomainName());
            rm.setDnsTtl(100);
            rm.setDnsType(RecordType.CNAME);
            if (question.getDomainName().equals(firstName)) {
                rm.put(DnsAttribute.DOMAIN_NAME, secondName);
            } else if (question.getDomainName().equals(secondName)) {
                rm.put(DnsAttribute.DOMAIN_NAME, lastName);
            } else if (question.getDomainName().equals(lastName)) {
                rm.setDnsType(question.getRecordType());
                switch(question.getRecordType()) {
                    case A:
                        rm.put(DnsAttribute.IP_ADDRESS, ipv4Addr);
                        break;
                    case AAAA:
                        rm.put(DnsAttribute.IP_ADDRESS, ipv6Addr);
                        break;
                    default:
                        return null;
                }
            } else {
                return null;
            }
            return Collections.singleton(rm.getEntry());
        }
    });
    dnsServer2.start();
    DnsNameResolver resolver = null;
    try {
        DnsNameResolverBuilder builder = newResolver().recursionDesired(true).maxQueriesPerResolve(16).nameServerProvider(new SingletonDnsServerAddressStreamProvider(dnsServer2.localAddress()));
        if (ipv4Preferred) {
            builder.resolvedAddressTypes(ResolvedAddressTypes.IPV4_PREFERRED);
        } else {
            builder.resolvedAddressTypes(ResolvedAddressTypes.IPV6_PREFERRED);
        }
        resolver = builder.build();
        InetAddress resolvedAddress = resolver.resolve(firstName).syncUninterruptibly().getNow();
        if (ipv4Preferred) {
            assertEquals(ipv4Addr, resolvedAddress.getHostAddress());
        } else {
            assertEquals(ipv6Addr, NetUtil.toAddressString(resolvedAddress));
        }
        assertEquals(firstName, resolvedAddress.getHostName());
    } 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) RecordStore(org.apache.directory.server.dns.store.RecordStore) ResourceRecordModifier(org.apache.directory.server.dns.messages.ResourceRecordModifier) InetAddress(java.net.InetAddress)

Example 10 with RecordStore

use of org.apache.directory.server.dns.store.RecordStore 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

QuestionRecord (org.apache.directory.server.dns.messages.QuestionRecord)19 RecordStore (org.apache.directory.server.dns.store.RecordStore)19 HashSet (java.util.HashSet)18 LinkedHashSet (java.util.LinkedHashSet)18 Set (java.util.Set)18 Test (org.junit.jupiter.api.Test)12 InetAddress (java.net.InetAddress)10 ResourceRecord (org.apache.directory.server.dns.messages.ResourceRecord)10 DefaultDnsQuestion (io.netty.handler.codec.dns.DefaultDnsQuestion)5 ResourceRecordModifier (org.apache.directory.server.dns.messages.ResourceRecordModifier)5 InetSocketAddress (java.net.InetSocketAddress)4 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)4 HashMap (java.util.HashMap)3 LinkedHashMap (java.util.LinkedHashMap)3 Map (java.util.Map)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 DnsException (org.apache.directory.server.dns.DnsException)3 DnsMessage (org.apache.directory.server.dns.messages.DnsMessage)3 DnsResponse (io.netty.handler.codec.dns.DnsResponse)2 DatagramSocket (java.net.DatagramSocket)2