io.netty.resolver.ResolvedAddressTypes Java Examples

The following examples show how to use io.netty.resolver.ResolvedAddressTypes. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DnsAddressEndpointGroupTest.java    From armeria with Apache License 2.0 6 votes vote down vote up
@Test
public void cname() throws Exception {
    try (TestDnsServer server = new TestDnsServer(ImmutableMap.of(
            new DefaultDnsQuestion("a.com.", A),
            new DefaultDnsResponse(0).addRecord(ANSWER, newBadAddressRecord("a.com.", true))
                                     .addRecord(ANSWER, newCnameRecord("a.com.", "b.com."))
                                     .addRecord(ANSWER, newAddressRecord("b.com.", "1.1.1.1")),
            new DefaultDnsQuestion("a.com.", AAAA),
            new DefaultDnsResponse(0).addRecord(ANSWER, newBadAddressRecord("a.com.", false))
                                     .addRecord(ANSWER, newCnameRecord("a.com.", "b.com."))
                                     .addRecord(ANSWER, newAddressRecord("b.com.", "::1"))
    ))) {
        try (DnsAddressEndpointGroup group =
                     DnsAddressEndpointGroup.builder("a.com")
                                            .port(8080)
                                            .serverAddresses(server.addr())
                                            .resolvedAddressTypes(ResolvedAddressTypes.IPV4_PREFERRED)
                                            .build()) {

            assertThat(group.whenReady().get()).containsExactly(
                    Endpoint.of("a.com", 8080).withIpAddr("1.1.1.1"),
                    Endpoint.of("a.com", 8080).withIpAddr("::1"));
        }
    }
}
 
Example #2
Source File: RefreshingAddressResolverTest.java    From armeria with Apache License 2.0 6 votes vote down vote up
@Test
void preferredOrderIpv6() throws Exception {
    try (TestDnsServer server = new TestDnsServer(
            ImmutableMap.of(
                    new DefaultDnsQuestion("foo.com.", A),
                    new DefaultDnsResponse(0).addRecord(ANSWER, newAddressRecord("foo.com.", "1.1.1.1")),
                    new DefaultDnsQuestion("foo.com.", AAAA),
                    new DefaultDnsResponse(0).addRecord(ANSWER, newAddressRecord("foo.com.", "::1", 1))),
            new DelayHandler(AAAA))
    ) {
        final EventLoop eventLoop = eventLoopExtension.get();
        final DnsResolverGroupBuilder builder = builder(server)
                .resolvedAddressTypes(ResolvedAddressTypes.IPV6_PREFERRED);
        try (RefreshingAddressResolverGroup group = builder.build(eventLoop)) {
            final AddressResolver<InetSocketAddress> resolver = group.getResolver(eventLoop);
            final Future<InetSocketAddress> future = resolver.resolve(
                    InetSocketAddress.createUnresolved("foo.com", 36462));
            await().until(future::isSuccess);
            assertThat(future.getNow().getAddress().getHostAddress()).isEqualTo("0:0:0:0:0:0:0:1");
        }
    }
}
 
Example #3
Source File: RefreshingAddressResolverTest.java    From armeria with Apache License 2.0 6 votes vote down vote up
@Test
void preferredOrderIpv4() throws Exception {
    try (TestDnsServer server = new TestDnsServer(
            ImmutableMap.of(
                    new DefaultDnsQuestion("foo.com.", A),
                    new DefaultDnsResponse(0).addRecord(ANSWER, newAddressRecord("foo.com.", "1.1.1.1")),
                    new DefaultDnsQuestion("foo.com.", AAAA),
                    new DefaultDnsResponse(0).addRecord(ANSWER, newAddressRecord("foo.com.", "::1", 1))),
            new DelayHandler(A))
    ) {
        final EventLoop eventLoop = eventLoopExtension.get();
        final DnsResolverGroupBuilder builder = builder(server)
                .resolvedAddressTypes(ResolvedAddressTypes.IPV4_PREFERRED);
        try (RefreshingAddressResolverGroup group = builder.build(eventLoop)) {
            final AddressResolver<InetSocketAddress> resolver = group.getResolver(eventLoop);
            final Future<InetSocketAddress> future = resolver.resolve(
                    InetSocketAddress.createUnresolved("foo.com", 36462));
            await().until(future::isSuccess);
            assertThat(future.getNow().getAddress().getHostAddress()).isEqualTo("1.1.1.1");
        }
    }
}
 
Example #4
Source File: RefreshingAddressResolverTest.java    From armeria with Apache License 2.0 6 votes vote down vote up
@Test
void returnPartialDnsQuestions() throws Exception {
    // Returns IPv6 correctly and make IPv4 timeout.
    try (TestDnsServer server = new TestDnsServer(
            ImmutableMap.of(
                    new DefaultDnsQuestion("foo.com.", AAAA),
                    new DefaultDnsResponse(0).addRecord(ANSWER, newAddressRecord("foo.com.", "::1", 1))))
    ) {
        final EventLoop eventLoop = eventLoopExtension.get();
        final DnsResolverGroupBuilder builder = builder(server)
                .queryTimeoutMillis(1000)
                .resolvedAddressTypes(ResolvedAddressTypes.IPV4_PREFERRED);
        try (RefreshingAddressResolverGroup group = builder.build(eventLoop)) {
            final AddressResolver<InetSocketAddress> resolver = group.getResolver(eventLoop);
            final Future<InetSocketAddress> future = resolver.resolve(
                    InetSocketAddress.createUnresolved("foo.com", 36462));
            await().until(future::isDone);
            assertThat(future.getNow().getAddress().getHostAddress()).isEqualTo("0:0:0:0:0:0:0:1");
        }
    }
}
 
Example #5
Source File: RefreshingAddressResolverTest.java    From armeria with Apache License 2.0 6 votes vote down vote up
@Test
void returnDnsQuestionsWhenAllQueryTimeout() throws Exception {
    try (TestDnsServer server1 = new TestDnsServer(ImmutableMap.of(), new AlwaysTimeoutHandler());
         TestDnsServer server2 = new TestDnsServer(ImmutableMap.of(), new AlwaysTimeoutHandler())) {
        final EventLoop eventLoop = eventLoopExtension.get();
        final DnsResolverGroupBuilder builder = builder(server1, server2)
                .queryTimeoutMillis(1000)
                .resolvedAddressTypes(ResolvedAddressTypes.IPV4_PREFERRED);
        try (RefreshingAddressResolverGroup group = builder.build(eventLoop)) {
            final AddressResolver<InetSocketAddress> resolver = group.getResolver(eventLoop);
            final Future<InetSocketAddress> future = resolver.resolve(
                    InetSocketAddress.createUnresolved("foo.com", 36462));
            await().until(future::isDone);
            assertThat(future.cause()).isInstanceOf(DnsTimeoutException.class);
        }
    }
}
 
Example #6
Source File: DnsAddressEndpointGroup.java    From armeria with Apache License 2.0 6 votes vote down vote up
DnsAddressEndpointGroup(EndpointSelectionStrategy selectionStrategy, EventLoop eventLoop,
                        int minTtl, int maxTtl, long queryTimeoutMillis,
                        DnsServerAddressStreamProvider serverAddressStreamProvider,
                        Backoff backoff, @Nullable ResolvedAddressTypes resolvedAddressTypes,
                        String hostname, int port) {

    super(selectionStrategy, eventLoop, minTtl, maxTtl, queryTimeoutMillis, serverAddressStreamProvider,
          backoff, newQuestions(hostname, resolvedAddressTypes),
          resolverBuilder -> {
              if (resolvedAddressTypes != null) {
                  resolverBuilder.resolvedAddressTypes(resolvedAddressTypes);
              }
          });

    this.hostname = hostname;
    this.port = port;
    start();
}
 
Example #7
Source File: RefreshingAddressResolverGroup.java    From armeria with Apache License 2.0 6 votes vote down vote up
RefreshingAddressResolverGroup(Consumer<DnsNameResolverBuilder> resolverConfigurator,
                               int minTtl, int maxTtl, int negativeTtl, long queryTimeoutMillis,
                               Backoff refreshBackoff,
                               @Nullable ResolvedAddressTypes resolvedAddressTypes) {
    this.resolverConfigurator = resolverConfigurator;
    this.minTtl = minTtl;
    this.maxTtl = maxTtl;
    this.negativeTtl = negativeTtl;
    this.queryTimeoutMillis = queryTimeoutMillis;
    this.refreshBackoff = refreshBackoff;
    if (resolvedAddressTypes == null) {
        dnsRecordTypes = defaultDnsRecordTypes;
    } else {
        dnsRecordTypes = dnsRecordTypes(resolvedAddressTypes);
    }
}
 
Example #8
Source File: RefreshingAddressResolverGroup.java    From armeria with Apache License 2.0 6 votes vote down vote up
private static ImmutableList<DnsRecordType> dnsRecordTypes(ResolvedAddressTypes resolvedAddressTypes) {
    final Builder<DnsRecordType> builder = ImmutableList.builder();
    switch (resolvedAddressTypes) {
        case IPV4_ONLY:
            builder.add(DnsRecordType.A);
            break;
        case IPV4_PREFERRED:
            builder.add(DnsRecordType.A);
            builder.add(DnsRecordType.AAAA);
            break;
        case IPV6_PREFERRED:
            builder.add(DnsRecordType.AAAA);
            builder.add(DnsRecordType.A);
            break;
    }
    return builder.build();
}
 
Example #9
Source File: DnsNameResolverBuilder.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
/**
 * Compute a {@link ResolvedAddressTypes} from some {@link InternetProtocolFamily}s.
 * An empty input will return the default value, based on "java.net" System properties.
 * Valid inputs are (), (IPv4), (IPv6), (Ipv4, IPv6) and (IPv6, IPv4).
 * @param internetProtocolFamilies a valid sequence of {@link InternetProtocolFamily}s
 * @return a {@link ResolvedAddressTypes}
 */
public static ResolvedAddressTypes computeResolvedAddressTypes(InternetProtocolFamily... internetProtocolFamilies) {
    if (internetProtocolFamilies == null || internetProtocolFamilies.length == 0) {
        return DnsNameResolver.DEFAULT_RESOLVE_ADDRESS_TYPES;
    }
    if (internetProtocolFamilies.length > 2) {
        throw new IllegalArgumentException("No more than 2 InternetProtocolFamilies");
    }

    switch(internetProtocolFamilies[0]) {
        case IPv4:
            return (internetProtocolFamilies.length >= 2
                    && internetProtocolFamilies[1] == InternetProtocolFamily.IPv6) ?
                    ResolvedAddressTypes.IPV4_PREFERRED: ResolvedAddressTypes.IPV4_ONLY;
        case IPv6:
            return (internetProtocolFamilies.length >= 2
                    && internetProtocolFamilies[1] == InternetProtocolFamily.IPv4) ?
                    ResolvedAddressTypes.IPV6_PREFERRED: ResolvedAddressTypes.IPV6_ONLY;
        default:
            throw new IllegalArgumentException(
                    "Couldn't resolve ResolvedAddressTypes from InternetProtocolFamily array");
    }
}
 
Example #10
Source File: DnsAddressEndpointGroupTest.java    From armeria with Apache License 2.0 6 votes vote down vote up
@Test
public void ipV4Only() throws Exception {
    try (TestDnsServer server = new TestDnsServer(ImmutableMap.of(
            new DefaultDnsQuestion("foo.com.", A),
            new DefaultDnsResponse(0).addRecord(ANSWER, newAddressRecord("foo.com.", "1.1.1.1"))
                                     .addRecord(ANSWER, newAddressRecord("unrelated.com", "1.2.3.4")),
            new DefaultDnsQuestion("foo.com.", AAAA),
            new DefaultDnsResponse(0).addRecord(ANSWER, newAddressRecord("foo.com.", "::1"))
    ))) {
        try (DnsAddressEndpointGroup group =
                     DnsAddressEndpointGroup.builder("foo.com")
                                            .port(8080)
                                            .serverAddresses(server.addr())
                                            .resolvedAddressTypes(ResolvedAddressTypes.IPV4_ONLY)
                                            .build()) {

            assertThat(group.whenReady().get()).containsExactly(
                    Endpoint.of("foo.com", 8080).withIpAddr("1.1.1.1"));
        }
    }
}
 
Example #11
Source File: DnsAddressEndpointGroupTest.java    From armeria with Apache License 2.0 6 votes vote down vote up
@Test
public void ipV6Only() throws Exception {
    try (TestDnsServer server = new TestDnsServer(ImmutableMap.of(
            new DefaultDnsQuestion("bar.com.", A),
            new DefaultDnsResponse(0).addRecord(ANSWER, newAddressRecord("bar.com.", "1.1.1.1")),
            new DefaultDnsQuestion("bar.com.", AAAA),
            new DefaultDnsResponse(0).addRecord(ANSWER, newAddressRecord("bar.com.", "::1"))
                                     .addRecord(ANSWER, newAddressRecord("bar.com.", "::1234:5678:90ab"))
                                     .addRecord(ANSWER, newAddressRecord("bar.com.",
                                                                         "2404:6800:4004:806::2013"))
    ))) {
        try (DnsAddressEndpointGroup group =
                     DnsAddressEndpointGroup.builder("bar.com")
                                            .port(8080)
                                            .serverAddresses(server.addr())
                                            .resolvedAddressTypes(ResolvedAddressTypes.IPV6_ONLY)
                                            .build()) {

            assertThat(group.whenReady().get(10, TimeUnit.SECONDS)).containsExactly(
                    Endpoint.of("bar.com", 8080).withIpAddr("2404:6800:4004:806::2013"),
                    Endpoint.of("bar.com", 8080).withIpAddr("::1"),
                    Endpoint.of("bar.com", 8080).withIpAddr("::1234:5678:90ab"));
        }
    }
}
 
Example #12
Source File: DnsAddressEndpointGroupTest.java    From armeria with Apache License 2.0 6 votes vote down vote up
@Test
public void ipV4AndIpV6() throws Exception {
    try (TestDnsServer server = new TestDnsServer(ImmutableMap.of(
            new DefaultDnsQuestion("baz.com.", A),
            new DefaultDnsResponse(0).addRecord(ANSWER, newAddressRecord("baz.com.", "1.1.1.1")),
            new DefaultDnsQuestion("baz.com.", AAAA),
            new DefaultDnsResponse(0).addRecord(ANSWER, newAddressRecord("baz.com.", "::1"))
    ))) {
        try (DnsAddressEndpointGroup group =
                     DnsAddressEndpointGroup.builder("baz.com")
                                            .port(8080)
                                            .serverAddresses(server.addr())
                                            .resolvedAddressTypes(ResolvedAddressTypes.IPV4_PREFERRED)
                                            .build()) {

            assertThat(group.whenReady().get()).containsExactly(
                    Endpoint.of("baz.com", 8080).withIpAddr("1.1.1.1"),
                    Endpoint.of("baz.com", 8080).withIpAddr("::1"));
        }
    }
}
 
Example #13
Source File: DnsAddressEndpointGroupTest.java    From armeria with Apache License 2.0 6 votes vote down vote up
@Test
public void mixedLoopbackAddresses() throws Exception {
    try (TestDnsServer server = new TestDnsServer(ImmutableMap.of(
            new DefaultDnsQuestion("foo.com.", A),
            new DefaultDnsResponse(0).addRecord(ANSWER, newAddressRecord("foo.com.", "127.0.0.1")),
            new DefaultDnsQuestion("foo.com.", AAAA),
            new DefaultDnsResponse(0).addRecord(ANSWER, newAddressRecord("foo.com.", "::1"))
    ))) {
        try (DnsAddressEndpointGroup group =
                     DnsAddressEndpointGroup.builder("foo.com")
                                            .port(8080)
                                            .serverAddresses(server.addr())
                                            .resolvedAddressTypes(ResolvedAddressTypes.IPV4_PREFERRED)
                                            .build()) {

            assertThat(group.whenReady().get()).containsExactly(
                    Endpoint.of("foo.com", 8080).withIpAddr("127.0.0.1"));
        }
    }
}
 
Example #14
Source File: DnsAddressEndpointGroupTest.java    From armeria with Apache License 2.0 6 votes vote down vote up
@Test
public void ipV4MappedOrCompatibleAddresses() throws Exception {
    try (TestDnsServer server = new TestDnsServer(ImmutableMap.of(
            new DefaultDnsQuestion("bar.com.", AAAA),
            new DefaultDnsResponse(0).addRecord(ANSWER, newCompatibleAddressRecord("bar.com.", "1.1.1.1"))
                                     .addRecord(ANSWER, newCompatibleAddressRecord("bar.com.", "1.1.1.2"))
                                     .addRecord(ANSWER, newMappedAddressRecord("bar.com.", "1.1.1.1"))
                                     .addRecord(ANSWER, newMappedAddressRecord("bar.com.", "1.1.1.3"))
    ))) {
        try (DnsAddressEndpointGroup group =
                     DnsAddressEndpointGroup.builder("bar.com")
                                            .port(8080)
                                            .serverAddresses(server.addr())
                                            .resolvedAddressTypes(ResolvedAddressTypes.IPV6_ONLY)
                                            .build()) {

            assertThat(group.whenReady().get()).containsExactly(
                    Endpoint.of("bar.com", 8080).withIpAddr("1.1.1.1"),
                    Endpoint.of("bar.com", 8080).withIpAddr("1.1.1.2"),
                    Endpoint.of("bar.com", 8080).withIpAddr("1.1.1.3"));
        }
    }
}
 
Example #15
Source File: DnsAddressEndpointGroupTest.java    From armeria with Apache License 2.0 6 votes vote down vote up
@EnumSource(value = ResolvedAddressTypes.class, names = { "IPV4_PREFERRED", "IPV6_PREFERRED" })
@ParameterizedTest
public void partialIpV6Response(ResolvedAddressTypes resolvedAddressTypes) throws Exception {
    try (TestDnsServer server = new TestDnsServer(ImmutableMap.of(
            // Respond AAAA record only.
            // Respond with NXDOMAIN for A.
            new DefaultDnsQuestion("partial.com.", AAAA),
            new DefaultDnsResponse(0).addRecord(ANSWER, newAddressRecord("partial.com", "::1"))
    ))) {
        try (DnsAddressEndpointGroup group =
                     DnsAddressEndpointGroup.builder("partial.com")
                                            .serverAddresses(server.addr())
                                            .resolvedAddressTypes(resolvedAddressTypes)
                                            .backoff(Backoff.fixed(500))
                                            .build()) {

            assertThat(group.whenReady().get()).containsExactly(
                    Endpoint.of("partial.com").withIpAddr("::1"));
        }
    }
}
 
Example #16
Source File: DnsAddressEndpointGroupTest.java    From armeria with Apache License 2.0 6 votes vote down vote up
@EnumSource(value = ResolvedAddressTypes.class, names = { "IPV4_PREFERRED", "IPV6_PREFERRED" })
@ParameterizedTest
public void partialIpV4Response(ResolvedAddressTypes resolvedAddressTypes) throws Exception {
    try (TestDnsServer server = new TestDnsServer(ImmutableMap.of(
            // Respond A record only.
            // Respond with NXDOMAIN for AAAA.
            new DefaultDnsQuestion("partial.com.", A),
            new DefaultDnsResponse(0).addRecord(ANSWER, newAddressRecord("partial.com", "1.1.1.1"))
    ))) {
        try (DnsAddressEndpointGroup group =
                     DnsAddressEndpointGroup.builder("partial.com")
                                            .serverAddresses(server.addr())
                                            .resolvedAddressTypes(resolvedAddressTypes)
                                            .backoff(Backoff.fixed(500))
                                            .build()) {

            assertThat(group.whenReady().get()).containsExactly(
                    Endpoint.of("partial.com").withIpAddr("1.1.1.1"));
        }
    }
}
 
Example #17
Source File: DnsAddressEndpointGroupTest.java    From armeria with Apache License 2.0 6 votes vote down vote up
@Test
public void noPort() throws Exception {
    try (TestDnsServer server = new TestDnsServer(ImmutableMap.of(
            new DefaultDnsQuestion("no-port.com.", A),
            new DefaultDnsResponse(0).addRecord(ANSWER, newAddressRecord("no-port.com", "1.1.1.1"))
    ))) {
        try (DnsAddressEndpointGroup group =
                     DnsAddressEndpointGroup.builder("no-port.com")
                                            .serverAddresses(server.addr())
                                            .resolvedAddressTypes(ResolvedAddressTypes.IPV4_ONLY)
                                            .build()) {

            assertThat(group.whenReady().get()).containsExactly(
                    Endpoint.of("no-port.com").withIpAddr("1.1.1.1"));
        }
    }
}
 
Example #18
Source File: DefaultDnsClient.java    From servicetalk with Apache License 2.0 5 votes vote down vote up
private static ResolvedAddressTypes toNettyType(final DnsResolverAddressTypes dnsResolverAddressTypes) {
    switch (dnsResolverAddressTypes) {
        case IPV4_ONLY:
            return ResolvedAddressTypes.IPV4_ONLY;
        case IPV6_ONLY:
            return ResolvedAddressTypes.IPV6_ONLY;
        case IPV6_PREFERRED:
            return ResolvedAddressTypes.IPV6_PREFERRED;
        case IPV4_PREFERRED:
            return ResolvedAddressTypes.IPV4_PREFERRED;
        default:
            throw new Error();
    }
}
 
Example #19
Source File: HttpRequestUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static boolean isLocalHost(String host, boolean onlyAnyOrLoopback, boolean hostsOnly) {
  if (NetUtil.isLocalhost(host)) {
    return true;
  }

  // if IP address, it is safe to use getByName (not affected by DNS rebinding)
  if (onlyAnyOrLoopback && !InetAddresses.isInetAddress(host)) {
    return false;
  }

  ThrowableNotNullFunction<InetAddress, Boolean, SocketException> isLocal =
          inetAddress -> inetAddress.isAnyLocalAddress() || inetAddress.isLoopbackAddress() || NetworkInterface.getByInetAddress(inetAddress) != null;

  try {
    InetAddress address = InetAddress.getByName(host);
    if (!isLocal.fun(address)) {
      return false;
    }
    // be aware - on windows hosts file doesn't contain localhost
    // hosts can contain remote addresses, so, we check it
    if (hostsOnly && !InetAddresses.isInetAddress(host)) {
      InetAddress hostInetAddress = HostsFileEntriesResolver.DEFAULT.address(host, ResolvedAddressTypes.IPV4_PREFERRED);
      return hostInetAddress != null && isLocal.fun(hostInetAddress);
    }
    else {
      return true;
    }
  }
  catch (IOException ignored) {
    return false;
  }
}
 
Example #20
Source File: DnsNameResolverTest.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Test
public void testResolveAorAAAA() throws Exception {
    DnsNameResolver resolver = newResolver(ResolvedAddressTypes.IPV4_PREFERRED).build();
    try {
        testResolve0(resolver, EXCLUSIONS_RESOLVE_A, AAAA);
    } finally {
        resolver.close();
    }
}
 
Example #21
Source File: DefaultDnsNameResolver.java    From armeria with Apache License 2.0 5 votes vote down vote up
public DefaultDnsNameResolver(DnsNameResolver delegate, EventLoop eventLoop, long queryTimeoutMillis) {
    this.delegate = requireNonNull(delegate, "delegate");
    this.eventLoop = requireNonNull(eventLoop, "eventLoop");
    checkArgument(queryTimeoutMillis >= 0, "queryTimeoutMillis: %s (expected: >= 0)", queryTimeoutMillis);
    this.queryTimeoutMillis = queryTimeoutMillis;

    if (delegate.resolvedAddressTypes() == ResolvedAddressTypes.IPV6_PREFERRED) {
        preferredOrder = Ordering.explicit(DnsRecordType.AAAA, DnsRecordType.A);
    } else {
        preferredOrder = Ordering.explicit(DnsRecordType.A, DnsRecordType.AAAA);
    }
}
 
Example #22
Source File: DnsAddressEndpointGroup.java    From armeria with Apache License 2.0 5 votes vote down vote up
private static List<DnsQuestion> newQuestions(
        String hostname, @Nullable ResolvedAddressTypes resolvedAddressTypes) {

    if (resolvedAddressTypes == null) {
        if (NetUtil.isIpV4StackPreferred() || !anyInterfaceSupportsIpV6()) {
            resolvedAddressTypes = ResolvedAddressTypes.IPV4_ONLY;
        } else {
            resolvedAddressTypes = ResolvedAddressTypes.IPV4_PREFERRED;
        }
    }

    final ImmutableList.Builder<DnsQuestion> builder = ImmutableList.builder();
    switch (resolvedAddressTypes) {
        case IPV4_ONLY:
        case IPV4_PREFERRED:
        case IPV6_PREFERRED:
            builder.add(DnsQuestionWithoutTrailingDot.of(hostname, DnsRecordType.A));
            break;
    }
    switch (resolvedAddressTypes) {
        case IPV6_ONLY:
        case IPV4_PREFERRED:
        case IPV6_PREFERRED:
            builder.add(DnsQuestionWithoutTrailingDot.of(hostname, DnsRecordType.AAAA));
            break;
    }
    return builder.build();
}
 
Example #23
Source File: DnsNameResolverTest.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Test
public void testResolveAAAAorA() throws Exception {
    DnsNameResolver resolver = newResolver(ResolvedAddressTypes.IPV6_PREFERRED).build();
    try {
        testResolve0(resolver, EXCLUSIONS_RESOLVE_A, A);
    } finally {
        resolver.close();
    }
}
 
Example #24
Source File: DnsNameResolverTest.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Test
public void testResolveA() throws Exception {
    DnsNameResolver resolver = newResolver(ResolvedAddressTypes.IPV4_ONLY)
            // Cache for eternity
            .ttl(Integer.MAX_VALUE, Integer.MAX_VALUE)
            .build();
    try {
        final Map<String, InetAddress> resultA = testResolve0(resolver, EXCLUSIONS_RESOLVE_A, null);

        // Now, try to resolve again to see if it's cached.
        // This test works because the DNS servers usually randomizes the order of the records in a response.
        // If cached, the resolved addresses must be always same, because we reuse the same response.

        final Map<String, InetAddress> resultB = testResolve0(resolver, EXCLUSIONS_RESOLVE_A, null);

        // Ensure the result from the cache is identical from the uncached one.
        assertThat(resultB.size(), is(resultA.size()));
        for (Entry<String, InetAddress> e: resultA.entrySet()) {
            InetAddress expected = e.getValue();
            InetAddress actual = resultB.get(e.getKey());
            if (!actual.equals(expected)) {
                // Print the content of the cache when test failure is expected.
                System.err.println("Cache for " + e.getKey() + ": " + resolver.resolveAll(e.getKey()).getNow());
            }
            assertThat(actual, is(expected));
        }
    } finally {
        resolver.close();
    }
}
 
Example #25
Source File: DnsNameResolverTest.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Test
public void testResolveAAAA() throws Exception {
    DnsNameResolver resolver = newResolver(ResolvedAddressTypes.IPV6_ONLY).build();
    try {
        testResolve0(resolver, EXCLUSIONS_RESOLVE_AAAA, null);
    } finally {
        resolver.close();
    }
}
 
Example #26
Source File: DnsNameResolverTest.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Test
public void testNonCachedResolve() throws Exception {
    DnsNameResolver resolver = newNonCachedResolver(ResolvedAddressTypes.IPV4_ONLY).build();
    try {
        testResolve0(resolver, EXCLUSIONS_RESOLVE_A, null);
    } finally {
        resolver.close();
    }
}
 
Example #27
Source File: RefreshingAddressResolverTest.java    From armeria with Apache License 2.0 5 votes vote down vote up
private static DnsResolverGroupBuilder builder(TestDnsServer... servers) {
    final DnsServerAddressStreamProvider dnsServerAddressStreamProvider =
            hostname -> DnsServerAddresses.sequential(
                    Stream.of(servers).map(TestDnsServer::addr).collect(toImmutableList())).stream();
    return new DnsResolverGroupBuilder()
            .dnsServerAddressStreamProvider(dnsServerAddressStreamProvider)
            .resolvedAddressTypes(ResolvedAddressTypes.IPV4_ONLY)
            .traceEnabled(false);
}
 
Example #28
Source File: DnsNameResolverTest.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
private static void testResolveAll0(ResolvedAddressTypes addressTypes, InetAddress expectedAddr, String name) {
    DnsNameResolver resolver = newResolver(addressTypes).build();
    try {
        List<InetAddress> addresses = resolver.resolveAll(name).syncUninterruptibly().getNow();
        assertEquals(1, addresses.size());
        assertEquals(expectedAddr, addresses.get(0));

        // We are resolving the local address, so we shouldn't make any queries.
        assertNoQueriesMade(resolver);
    } finally {
        resolver.close();
    }
}
 
Example #29
Source File: DnsNameResolverTest.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
public void testNonCachedResolveEmptyHostName(String inetHost) throws Exception {
    DnsNameResolver resolver = newNonCachedResolver(ResolvedAddressTypes.IPV4_ONLY).build();
    try {
        InetAddress addr = resolver.resolve(inetHost).syncUninterruptibly().getNow();
        assertEquals(SocketUtils.addressByName(inetHost), addr);
    } finally {
        resolver.close();
    }
}
 
Example #30
Source File: DnsNameResolverTest.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
private static void testNonCachedResolveAllEmptyHostName(String inetHost) throws UnknownHostException {
    DnsNameResolver resolver = newNonCachedResolver(ResolvedAddressTypes.IPV4_ONLY).build();
    try {
        List<InetAddress> addrs = resolver.resolveAll(inetHost).syncUninterruptibly().getNow();
        assertEquals(Arrays.asList(
                SocketUtils.allAddressesByName(inetHost)), addrs);
    } finally {
        resolver.close();
    }
}