|
| 1 | +/* |
| 2 | + * Copyright 2026 The Netty Project |
| 3 | + * |
| 4 | + * The Netty Project licenses this file to you under the Apache License, |
| 5 | + * version 2.0 (the "License"); you may not use this file except in compliance |
| 6 | + * with the License. You may obtain a copy of the License at: |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | + * License for the specific language governing permissions and limitations |
| 14 | + * under the License. |
| 15 | + */ |
| 16 | +package io.netty.resolver.dns; |
| 17 | + |
| 18 | +import io.netty.channel.EventLoopGroup; |
| 19 | +import io.netty.channel.MultiThreadIoEventLoopGroup; |
| 20 | +import io.netty.channel.nio.NioIoHandler; |
| 21 | +import io.netty.resolver.HostsFileEntriesResolver; |
| 22 | +import io.netty.resolver.ResolvedAddressTypes; |
| 23 | +import io.netty.resolver.dns.DnsNameResolverTest.TestRecursiveCacheDnsQueryLifecycleObserverFactory; |
| 24 | +import io.netty.util.internal.PlatformDependent; |
| 25 | +import org.apache.directory.server.dns.messages.QuestionRecord; |
| 26 | +import org.apache.directory.server.dns.messages.RecordType; |
| 27 | +import org.apache.directory.server.dns.messages.ResourceRecord; |
| 28 | +import org.apache.directory.server.dns.store.RecordStore; |
| 29 | +import org.junit.jupiter.api.AfterAll; |
| 30 | +import org.junit.jupiter.api.BeforeAll; |
| 31 | +import org.junit.jupiter.api.parallel.Isolated; |
| 32 | +import org.junit.jupiter.params.ParameterizedTest; |
| 33 | +import org.junit.jupiter.params.provider.EnumSource; |
| 34 | + |
| 35 | +import java.net.InetAddress; |
| 36 | +import java.util.Collections; |
| 37 | +import java.util.Set; |
| 38 | + |
| 39 | +import static io.netty.resolver.dns.DnsNameResolverTest.DOMAINS_ALL; |
| 40 | +import static io.netty.resolver.dns.DnsNameResolverTest.newResolver; |
| 41 | +import static io.netty.resolver.dns.TestDnsServer.newARecord; |
| 42 | +import static org.assertj.core.api.Assumptions.assumeThat; |
| 43 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 44 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 45 | + |
| 46 | +@Isolated |
| 47 | +public class IsolatedDnsNameResolverTest { |
| 48 | + private static final TestDnsServer dnsServer = new TestDnsServer(DOMAINS_ALL); |
| 49 | + private static final EventLoopGroup group = new MultiThreadIoEventLoopGroup(1, NioIoHandler.newFactory()); |
| 50 | + private static boolean existingLocalhostSetting; |
| 51 | + |
| 52 | + @BeforeAll |
| 53 | + public static void captureSetting() throws Exception { |
| 54 | + existingLocalhostSetting = DnsNameResolver.resolveLocalhostWithoutDns; |
| 55 | + dnsServer.start(); |
| 56 | + } |
| 57 | + |
| 58 | + @AfterAll |
| 59 | + public static void restoreSetting() { |
| 60 | + try { |
| 61 | + dnsServer.stop(); |
| 62 | + group.shutdownGracefully(); |
| 63 | + } finally { |
| 64 | + DnsNameResolver.resolveLocalhostWithoutDns = existingLocalhostSetting; |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + @ParameterizedTest |
| 69 | + @EnumSource(DnsNameResolverChannelStrategy.class) |
| 70 | + public void testResolveDotLocalhostViaDnsWhenDisabled(DnsNameResolverChannelStrategy strategy) throws Exception { |
| 71 | + final String hostname = "myservice.localhost"; |
| 72 | + final String expectedIp = "10.0.0.1"; |
| 73 | + final TestDnsServer customDnsServer = new TestDnsServer(new RecordStore() { |
| 74 | + @Override |
| 75 | + public Set<ResourceRecord> getRecords(QuestionRecord question) { |
| 76 | + if (question.getRecordType() == RecordType.A) { |
| 77 | + String domainName = question.getDomainName(); |
| 78 | + if (domainName.equalsIgnoreCase(hostname) || domainName.equalsIgnoreCase(hostname + '.')) { |
| 79 | + return Collections.singleton(newARecord(hostname, expectedIp)); |
| 80 | + } |
| 81 | + } |
| 82 | + return Collections.emptySet(); |
| 83 | + } |
| 84 | + }); |
| 85 | + customDnsServer.start(); |
| 86 | + DnsNameResolver.resolveLocalhostWithoutDns = false; |
| 87 | + try { |
| 88 | + DnsNameResolver resolver = newResolver(strategy, false, null, customDnsServer, group.next()) |
| 89 | + .resolvedAddressTypes(ResolvedAddressTypes.IPV4_ONLY) |
| 90 | + .hostsFileEntriesResolver(new HostsFileEntriesResolver() { |
| 91 | + @Override |
| 92 | + public InetAddress address(String inetHost, ResolvedAddressTypes resolvedAddressTypes) { |
| 93 | + return null; |
| 94 | + } |
| 95 | + }) |
| 96 | + .build(); |
| 97 | + try { |
| 98 | + InetAddress address = resolver.resolve(hostname).syncUninterruptibly().getNow(); |
| 99 | + assertEquals(expectedIp, address.getHostAddress()); |
| 100 | + |
| 101 | + TestRecursiveCacheDnsQueryLifecycleObserverFactory lifecycleObserverFactory = |
| 102 | + (TestRecursiveCacheDnsQueryLifecycleObserverFactory) |
| 103 | + resolver.dnsQueryLifecycleObserverFactory(); |
| 104 | + assertFalse(lifecycleObserverFactory.observers.isEmpty()); |
| 105 | + } finally { |
| 106 | + resolver.close(); |
| 107 | + } |
| 108 | + } finally { |
| 109 | + customDnsServer.stop(); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + @ParameterizedTest |
| 114 | + @EnumSource(DnsNameResolverChannelStrategy.class) |
| 115 | + public void testResolveLocalhostViaDnsWhenDisabledOnNonWindows(DnsNameResolverChannelStrategy strategy) |
| 116 | + throws Exception { |
| 117 | + assumeThat(PlatformDependent.isWindows()).isFalse(); |
| 118 | + |
| 119 | + final String expectedIp = "10.0.0.2"; |
| 120 | + final TestDnsServer customDnsServer = new TestDnsServer(new RecordStore() { |
| 121 | + @Override |
| 122 | + public Set<ResourceRecord> getRecords(QuestionRecord question) { |
| 123 | + if (question.getRecordType() == RecordType.A) { |
| 124 | + String domainName = question.getDomainName(); |
| 125 | + if (domainName.equalsIgnoreCase("localhost") || domainName.equalsIgnoreCase("localhost.")) { |
| 126 | + return Collections.singleton(newARecord("localhost", expectedIp)); |
| 127 | + } |
| 128 | + } |
| 129 | + return Collections.emptySet(); |
| 130 | + } |
| 131 | + }); |
| 132 | + customDnsServer.start(); |
| 133 | + DnsNameResolver.resolveLocalhostWithoutDns = false; |
| 134 | + try { |
| 135 | + DnsNameResolver resolver = newResolver(strategy, false, null, customDnsServer, group.next()) |
| 136 | + .resolvedAddressTypes(ResolvedAddressTypes.IPV4_ONLY) |
| 137 | + .hostsFileEntriesResolver(new HostsFileEntriesResolver() { |
| 138 | + @Override |
| 139 | + public InetAddress address(String inetHost, ResolvedAddressTypes resolvedAddressTypes) { |
| 140 | + return null; |
| 141 | + } |
| 142 | + }) |
| 143 | + .build(); |
| 144 | + try { |
| 145 | + InetAddress address = resolver.resolve("localhost").syncUninterruptibly().getNow(); |
| 146 | + assertEquals(expectedIp, address.getHostAddress()); |
| 147 | + |
| 148 | + TestRecursiveCacheDnsQueryLifecycleObserverFactory lifecycleObserverFactory = |
| 149 | + (TestRecursiveCacheDnsQueryLifecycleObserverFactory) |
| 150 | + resolver.dnsQueryLifecycleObserverFactory(); |
| 151 | + assertFalse(lifecycleObserverFactory.observers.isEmpty()); |
| 152 | + } finally { |
| 153 | + resolver.close(); |
| 154 | + } |
| 155 | + } finally { |
| 156 | + customDnsServer.stop(); |
| 157 | + } |
| 158 | + } |
| 159 | +} |