Search in sources :

Example 1 with QuestionRecord

use of org.apache.directory.server.dns.messages.QuestionRecord 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) throws DnsException {
            ResourceRecordModifier rm = new ResourceRecordModifier();
            rm.setDnsClass(RecordClass.IN);
            rm.setDnsName(question.getDomainName());
            rm.setDnsTtl(100);
            rm.setDnsType(question.getRecordType());
            switch(question.getRecordType()) {
                case A:
                    rm.put(DnsAttribute.IP_ADDRESS, overriddenIP);
                    break;
                default:
                    return null;
            }
            return Collections.singleton(rm.getEntry());
        }
    });
    dnsServer2.start();
    try {
        final Set<String> overridenHostnames = new HashSet<String>();
        for (String name : DOMAINS) {
            if (EXCLUSIONS_RESOLVE_A.contains(name)) {
                continue;
            }
            if (PlatformDependent.threadLocalRandom().nextBoolean()) {
                overridenHostnames.add(name);
            }
        }
        DnsNameResolver resolver = newResolver(false, new DnsServerAddressStreamProvider() {

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

Aggregations

InetAddress (java.net.InetAddress)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1 DnsException (org.apache.directory.server.dns.DnsException)1 QuestionRecord (org.apache.directory.server.dns.messages.QuestionRecord)1 ResourceRecordModifier (org.apache.directory.server.dns.messages.ResourceRecordModifier)1 RecordStore (org.apache.directory.server.dns.store.RecordStore)1 Test (org.junit.Test)1