Migrate resolver-dns tests to JUnit 5 (#11326)

Motivation:

JUnit 5 is more expressive, extensible, and composable in many ways, and it's better able to run tests in parallel.

Modifications:

Use JUnit5 in tests

Result:

Related to https://github.com/netty/netty/issues/10757
This commit is contained in:
Riley Park 2021-05-27 10:06:07 -07:00 committed by GitHub
parent fd111e22d9
commit cd4249218c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 175 additions and 164 deletions

View File

@ -21,7 +21,7 @@ import io.netty.channel.EventLoopGroup;
import io.netty.channel.local.LocalHandler;
import io.netty.channel.nio.NioHandler;
import io.netty.util.NetUtil;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import java.net.InetAddress;
import java.net.InetSocketAddress;
@ -29,7 +29,8 @@ import java.util.Comparator;
import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;
import static org.junit.Assert.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
public class DefaultAuthoritativeDnsServerCacheTest {

View File

@ -22,7 +22,7 @@ import io.netty.channel.EventLoopGroup;
import io.netty.channel.local.LocalHandler;
import io.netty.channel.nio.NioHandler;
import io.netty.util.NetUtil;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import java.net.InetAddress;
import java.util.List;
@ -30,10 +30,10 @@ import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;
public class DefaultDnsCacheTest {

View File

@ -20,12 +20,14 @@ import io.netty.channel.EventLoop;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.local.LocalHandler;
import io.netty.channel.nio.NioHandler;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;
import static org.junit.Assert.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class DefaultDnsCnameCacheTest {

View File

@ -24,14 +24,14 @@ import io.netty.resolver.AddressResolver;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.FutureListener;
import io.netty.util.concurrent.Promise;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import java.net.SocketAddress;
import java.nio.channels.UnsupportedAddressTypeException;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.instanceOf;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class DnsAddressResolverGroupTest {
@Test

View File

@ -22,8 +22,8 @@ import io.netty.channel.socket.nio.NioDatagramChannel;
import io.netty.handler.codec.dns.DefaultDnsOptEcsRecord;
import io.netty.util.internal.SocketUtils;
import io.netty.util.concurrent.Future;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import java.net.InetAddress;
import java.util.Collections;
@ -34,7 +34,7 @@ public class DnsNameResolverClientSubnetTest {
// See https://www.gsic.uva.es/~jnisigl/dig-edns-client-subnet.html
// Ignore as this needs to query real DNS servers.
@Ignore
@Disabled
@Test
public void testSubnetQuery() throws Exception {
EventLoopGroup group = new MultithreadEventLoopGroup(1, NioHandler.newFactory());

View File

@ -66,11 +66,9 @@ import org.apache.directory.server.dns.store.DnsAttribute;
import org.apache.directory.server.dns.store.RecordStore;
import org.apache.mina.core.buffer.IoBuffer;
import org.hamcrest.Matchers;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import java.io.IOException;
import java.io.InputStream;
@ -108,6 +106,7 @@ import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import org.junit.jupiter.api.Timeout;
import static io.netty.handler.codec.dns.DnsRecordType.A;
import static io.netty.handler.codec.dns.DnsRecordType.AAAA;
@ -123,14 +122,15 @@ import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
public class DnsNameResolverTest {
@ -347,9 +347,6 @@ public class DnsNameResolverTest {
private static final TestDnsServer dnsServer = new TestDnsServer(DOMAINS_ALL);
private static final EventLoopGroup group = new MultithreadEventLoopGroup(1, NioHandler.newFactory());
@Rule
public ExpectedException expectedException = ExpectedException.none();
private static DnsNameResolverBuilder newResolver(boolean decodeToUnicode) {
return newResolver(decodeToUnicode, null);
}
@ -389,12 +386,12 @@ public class DnsNameResolverTest {
.resolvedAddressTypes(resolvedAddressTypes);
}
@BeforeClass
@BeforeAll
public static void init() throws Exception {
dnsServer.start();
}
@AfterClass
@AfterAll
public static void destroy() {
dnsServer.stop();
group.shutdownGracefully();
@ -462,11 +459,11 @@ public class DnsNameResolverTest {
continue;
}
if (overriddenHostnames.contains(resolvedEntry.getKey())) {
assertEquals("failed to resolve " + resolvedEntry.getKey(),
overriddenIP, resolvedEntry.getValue().getHostAddress());
assertEquals(overriddenIP, resolvedEntry.getValue().getHostAddress(),
"failed to resolve " + resolvedEntry.getKey());
} else {
assertNotEquals("failed to resolve " + resolvedEntry.getKey(),
overriddenIP, resolvedEntry.getValue().getHostAddress());
assertNotEquals(overriddenIP, resolvedEntry.getValue().getHostAddress(),
"failed to resolve " + resolvedEntry.getKey());
}
}
} finally {
@ -525,12 +522,14 @@ public class DnsNameResolverTest {
}
}
@Test(timeout = DEFAULT_TEST_TIMEOUT_MS)
@Test
@Timeout(value = DEFAULT_TEST_TIMEOUT_MS, unit = TimeUnit.MILLISECONDS)
public void testNonCachedResolveEmptyHostName() throws Exception {
testNonCachedResolveEmptyHostName("");
}
@Test(timeout = DEFAULT_TEST_TIMEOUT_MS)
@Test
@Timeout(value = DEFAULT_TEST_TIMEOUT_MS, unit = TimeUnit.MILLISECONDS)
public void testNonCachedResolveNullHostName() throws Exception {
testNonCachedResolveEmptyHostName(null);
}
@ -545,12 +544,14 @@ public class DnsNameResolverTest {
}
}
@Test(timeout = DEFAULT_TEST_TIMEOUT_MS)
@Test
@Timeout(value = DEFAULT_TEST_TIMEOUT_MS, unit = TimeUnit.MILLISECONDS)
public void testNonCachedResolveAllEmptyHostName() throws Exception {
testNonCachedResolveAllEmptyHostName("");
}
@Test(timeout = DEFAULT_TEST_TIMEOUT_MS)
@Test
@Timeout(value = DEFAULT_TEST_TIMEOUT_MS, unit = TimeUnit.MILLISECONDS)
public void testNonCachedResolveAllNullHostName() throws Exception {
testNonCachedResolveAllEmptyHostName(null);
}
@ -721,8 +722,8 @@ public class DnsNameResolverTest {
if (observer != null) {
Object o = observer.events.poll();
if (o instanceof QueryCancelledEvent) {
assertTrue("unexpected type: " + observer.question,
observer.question.type() == CNAME || observer.question.type() == AAAA);
assertTrue(observer.question.type() == CNAME || observer.question.type() == AAAA,
"unexpected type: " + observer.question);
} else if (o instanceof QueryWrittenEvent) {
QueryFailedEvent failedEvent = (QueryFailedEvent) observer.events.poll();
} else if (!(o instanceof QueryFailedEvent)) {
@ -1141,12 +1142,14 @@ public class DnsNameResolverTest {
}
}
@Test(timeout = DEFAULT_TEST_TIMEOUT_MS)
@Test
@Timeout(value = DEFAULT_TEST_TIMEOUT_MS, unit = TimeUnit.MILLISECONDS)
public void secondDnsServerShouldBeUsedBeforeCNAMEFirstServerNotStarted() throws IOException {
secondDnsServerShouldBeUsedBeforeCNAME(false);
}
@Test(timeout = DEFAULT_TEST_TIMEOUT_MS)
@Test
@Timeout(value = DEFAULT_TEST_TIMEOUT_MS, unit = TimeUnit.MILLISECONDS)
public void secondDnsServerShouldBeUsedBeforeCNAMEFirstServerFailResolve() throws IOException {
secondDnsServerShouldBeUsedBeforeCNAME(true);
}
@ -1205,7 +1208,8 @@ public class DnsNameResolverTest {
}
}
@Test(timeout = DEFAULT_TEST_TIMEOUT_MS)
@Test
@Timeout(value = DEFAULT_TEST_TIMEOUT_MS, unit = TimeUnit.MILLISECONDS)
public void aAndAAAAQueryShouldTryFirstDnsServerBeforeSecond() throws IOException {
final String knownHostName = "netty.io";
final TestDnsServer dnsServer1 = new TestDnsServer(Collections.singleton("notnetty.com"));
@ -2146,7 +2150,8 @@ public class DnsNameResolverTest {
}
}
@Test(timeout = 3000)
@Test
@Timeout(value = 3000, unit = TimeUnit.MILLISECONDS)
public void testTimeoutNotCached() {
DnsCache cache = new DnsCache() {
@Override
@ -2256,7 +2261,6 @@ public class DnsNameResolverTest {
@Test
public void testFollowCNAMELoop() throws Throwable {
expectedException.expect(UnknownHostException.class);
TestDnsServer dnsServer2 = new TestDnsServer(question -> {
Set<ResourceRecord> records = new LinkedHashSet<>(4);
@ -2278,7 +2282,7 @@ public class DnsNameResolverTest {
return records;
});
dnsServer2.start();
DnsNameResolver resolver = null;
DnsNameResolver[] resolver = new DnsNameResolver[1];
try {
DnsNameResolverBuilder builder = newResolver()
.recursionDesired(false)
@ -2286,22 +2290,23 @@ public class DnsNameResolverTest {
.maxQueriesPerResolve(16)
.nameServerProvider(new SingletonDnsServerAddressStreamProvider(dnsServer2.localAddress()));
resolver = builder.build();
resolver.resolveAll("somehost.netty.io").syncUninterruptibly().getNow();
resolver[0] = builder.build();
final CompletionException completion = assertThrows(CompletionException.class,
() -> resolver[0].resolveAll("somehost.netty.io").syncUninterruptibly().getNow());
assertTrue(completion.getCause() instanceof UnknownHostException);
} catch (CompletionException e) {
throw e.getCause();
} finally {
dnsServer2.stop();
if (resolver != null) {
resolver.close();
if (resolver[0] != null) {
resolver[0].close();
}
}
}
@Test
public void testCNAMELoopInCache() throws Throwable {
expectedException.expect(UnknownHostException.class);
DnsNameResolver resolver = null;
DnsNameResolver[] resolver = new DnsNameResolver[1];
try {
DnsNameResolverBuilder builder = newResolver()
.recursionDesired(false)
@ -2309,33 +2314,33 @@ public class DnsNameResolverTest {
.maxQueriesPerResolve(16)
.nameServerProvider(new SingletonDnsServerAddressStreamProvider(dnsServer.localAddress()));
resolver = builder.build();
resolver[0] = builder.build();
// Add a CNAME loop into the cache
String name = "somehost.netty.io.";
String name2 = "cname.netty.io.";
resolver.cnameCache().cache(name, name2, Long.MAX_VALUE, resolver.executor());
resolver.cnameCache().cache(name2, name, Long.MAX_VALUE, resolver.executor());
resolver.resolve(name).syncUninterruptibly().getNow();
} catch (CompletionException e) {
throw e.getCause();
resolver[0].cnameCache().cache(name, name2, Long.MAX_VALUE, resolver[0].executor());
resolver[0].cnameCache().cache(name2, name, Long.MAX_VALUE, resolver[0].executor());
final CompletionException completion = assertThrows(CompletionException.class,
() -> resolver[0].resolve(name).syncUninterruptibly().getNow());
assertTrue(completion.getCause() instanceof UnknownHostException);
} finally {
if (resolver != null) {
resolver.close();
if (resolver[0] != null) {
resolver[0].close();
}
}
}
@Test
public void testSearchDomainQueryFailureForSingleAddressTypeCompletes() throws Throwable {
expectedException.expect(UnknownHostException.class);
testSearchDomainQueryFailureCompletes(ResolvedAddressTypes.IPV4_ONLY);
assertThrows(UnknownHostException.class,
() -> testSearchDomainQueryFailureCompletes(ResolvedAddressTypes.IPV4_ONLY));
}
@Test
public void testSearchDomainQueryFailureForMultipleAddressTypeCompletes() throws Throwable {
expectedException.expect(UnknownHostException.class);
testSearchDomainQueryFailureCompletes(ResolvedAddressTypes.IPV4_PREFERRED);
assertThrows(UnknownHostException.class,
() -> testSearchDomainQueryFailureCompletes(ResolvedAddressTypes.IPV4_PREFERRED));
}
private void testSearchDomainQueryFailureCompletes(ResolvedAddressTypes types) throws Throwable {
@ -2352,7 +2357,8 @@ public class DnsNameResolverTest {
}
}
@Test(timeout = 2000L)
@Test
@Timeout(value = 2000, unit = TimeUnit.MILLISECONDS)
public void testCachesClearedOnClose() throws Exception {
final CountDownLatch resolveLatch = new CountDownLatch(1);
final CountDownLatch authoritativeLatch = new CountDownLatch(1);
@ -2524,13 +2530,13 @@ public class DnsNameResolverTest {
.cnameCache(new DnsCnameCache() {
@Override
public String get(String hostname) {
assertTrue(hostname, hostname.endsWith("."));
assertTrue(hostname.endsWith("."), hostname);
return cache.get(hostname);
}
@Override
public void cache(String hostname, String cname, long originalTtl, EventLoop loop) {
assertTrue(hostname, hostname.endsWith("."));
assertTrue(hostname.endsWith("."), hostname);
cache.put(hostname, cname);
}
@ -2787,7 +2793,8 @@ public class DnsNameResolverTest {
}
}
@Test(timeout = 2000)
@Test
@Timeout(value = 2000, unit = TimeUnit.MILLISECONDS)
public void testDropAAAAResolveFast() throws IOException {
String host = "somehost.netty.io";
TestDnsServer dnsServer2 = new TestDnsServer(Collections.singleton(host));
@ -2812,7 +2819,8 @@ public class DnsNameResolverTest {
}
}
@Test(timeout = 2000)
@Test
@Timeout(value = 2000, unit = TimeUnit.MILLISECONDS)
public void testDropAAAAResolveAllFast() throws IOException {
final String host = "somehost.netty.io";
TestDnsServer dnsServer2 = new TestDnsServer(question -> {
@ -2855,17 +2863,20 @@ public class DnsNameResolverTest {
}
}
@Test(timeout = 5000)
@Test
@Timeout(value = 5000, unit = TimeUnit.MILLISECONDS)
public void testTruncatedWithoutTcpFallback() throws IOException {
testTruncated0(false, false);
}
@Test(timeout = 5000)
@Test
@Timeout(value = 5000, unit = TimeUnit.MILLISECONDS)
public void testTruncatedWithTcpFallback() throws IOException {
testTruncated0(true, false);
}
@Test(timeout = 5000)
@Test
@Timeout(value = 5000, unit = TimeUnit.MILLISECONDS)
public void testTruncatedWithTcpFallbackBecauseOfMtu() throws IOException {
testTruncated0(true, true);
}
@ -3205,7 +3216,8 @@ public class DnsNameResolverTest {
}
}
@Test(timeout = 2000)
@Test
@Timeout(value = 2000, unit = TimeUnit.MILLISECONDS)
public void testSrvWithCnameNotCached() throws Exception {
final AtomicBoolean alias = new AtomicBoolean();
TestDnsServer dnsServer2 = new TestDnsServer(new RecordStore() {

View File

@ -16,11 +16,11 @@
package io.netty.resolver.dns;
import io.netty.channel.embedded.EmbeddedChannel;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import java.net.UnknownHostException;
import static org.junit.Assert.fail;
import static org.junit.jupiter.api.Assertions.fail;
public class DnsResolveContextTest {

View File

@ -12,16 +12,18 @@
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/package io.netty.resolver.dns;
*/
package io.netty.resolver.dns;
import org.junit.Assert;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertSame;
public class DnsServerAddressStreamProvidersTest {
@Test
public void testUseCorrectProvider() {
Assert.assertSame(DnsServerAddressStreamProviders.unixDefault(),
assertSame(DnsServerAddressStreamProviders.unixDefault(),
DnsServerAddressStreamProviders.platformDefault());
}
}

View File

@ -17,7 +17,7 @@
package io.netty.resolver.dns;
import io.netty.util.NetUtil;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import java.net.InetSocketAddress;
import java.util.Collections;

View File

@ -15,8 +15,8 @@
*/
package io.netty.resolver.dns;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import java.net.Inet4Address;
import java.net.Inet6Address;
@ -28,8 +28,7 @@ import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import static org.junit.Assert.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class NameServerComparatorTest {
@ -44,7 +43,7 @@ public class NameServerComparatorTest {
private static InetSocketAddress UNRESOLVED2;
private static InetSocketAddress UNRESOLVED3;
@BeforeClass
@BeforeAll
public static void before() throws UnknownHostException {
IPV4ADDRESS1 = new InetSocketAddress(InetAddress.getByAddress("ns1", new byte[] { 10, 0, 0, 1 }), 53);
IPV4ADDRESS2 = new InetSocketAddress(InetAddress.getByAddress("ns2", new byte[] { 10, 0, 0, 2 }), 53);

View File

@ -16,8 +16,7 @@
package io.netty.resolver.dns;
import io.netty.channel.socket.InternetProtocolFamily;
import org.junit.Assert;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import java.net.InetAddress;
import java.net.UnknownHostException;
@ -26,6 +25,8 @@ import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class PreferredAddressTypeComparatorTest {
@Test
@ -44,7 +45,7 @@ public class PreferredAddressTypeComparatorTest {
ipv6Address2, ipv4Address3, ipv6Address3);
Collections.sort(addressList, ipv4);
Assert.assertEquals(Arrays.asList(ipv4Address1, ipv4Address2, ipv4Address3, ipv6Address1,
assertEquals(Arrays.asList(ipv4Address1, ipv4Address2, ipv4Address3, ipv6Address1,
ipv6Address2, ipv6Address3), addressList);
}
@ -64,7 +65,7 @@ public class PreferredAddressTypeComparatorTest {
ipv6Address2, ipv4Address3, ipv6Address3);
Collections.sort(addressList, ipv4);
Assert.assertEquals(Arrays.asList(ipv6Address1,
assertEquals(Arrays.asList(ipv6Address1,
ipv6Address2, ipv6Address3, ipv4Address1, ipv4Address2, ipv4Address3), addressList);
}
}

View File

@ -20,9 +20,9 @@ import io.netty.channel.MultithreadEventLoopGroup;
import io.netty.channel.nio.NioHandler;
import io.netty.channel.socket.nio.NioDatagramChannel;
import io.netty.util.concurrent.Future;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.net.InetAddress;
import java.net.UnknownHostException;
@ -38,9 +38,9 @@ import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.core.StringContains.containsString;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class SearchDomainTest {
@ -57,12 +57,12 @@ public class SearchDomainTest {
private EventLoopGroup group;
private DnsNameResolver resolver;
@Before
@BeforeEach
public void before() {
group = new MultithreadEventLoopGroup(1, NioHandler.newFactory());
}
@After
@AfterEach
public void destroy() {
if (dnsServer != null) {
dnsServer.stop();

View File

@ -16,29 +16,26 @@
package io.netty.resolver.dns;
import io.netty.util.CharsetUtil;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.UUID;
import static io.netty.resolver.dns.UnixResolverDnsServerAddressStreamProvider.parseEtcResolverOptions;
import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class UnixResolverDnsServerAddressStreamProviderTest {
@Rule
public final TemporaryFolder folder = new TemporaryFolder();
@Test
public void defaultLookupShouldReturnResultsIfOnlySingleFileSpecified() throws Exception {
File f = buildFile("domain linecorp.local\n" +
public void defaultLookupShouldReturnResultsIfOnlySingleFileSpecified(@TempDir Path tempDir) throws Exception {
File f = buildFile(tempDir, "domain linecorp.local\n" +
"nameserver 127.0.0.2\n" +
"nameserver 127.0.0.3\n");
UnixResolverDnsServerAddressStreamProvider p =
@ -50,8 +47,9 @@ public class UnixResolverDnsServerAddressStreamProviderTest {
}
@Test
public void nameServerAddressStreamShouldBeRotationalWhenRotationOptionsIsPresent() throws Exception {
File f = buildFile("options rotate\n" +
public void nameServerAddressStreamShouldBeRotationalWhenRotationOptionsIsPresent(
@TempDir Path tempDir) throws Exception {
File f = buildFile(tempDir, "options rotate\n" +
"domain linecorp.local\n" +
"nameserver 127.0.0.2\n" +
"nameserver 127.0.0.3\n" +
@ -81,8 +79,9 @@ public class UnixResolverDnsServerAddressStreamProviderTest {
}
@Test
public void nameServerAddressStreamShouldAlwaysStartFromTheTopWhenRotationOptionsIsAbsent() throws Exception {
File f = buildFile("domain linecorp.local\n" +
public void nameServerAddressStreamShouldAlwaysStartFromTheTopWhenRotationOptionsIsAbsent(
@TempDir Path tempDir) throws Exception {
File f = buildFile(tempDir, "domain linecorp.local\n" +
"nameserver 127.0.0.2\n" +
"nameserver 127.0.0.3\n" +
"nameserver 127.0.0.4\n");
@ -106,11 +105,11 @@ public class UnixResolverDnsServerAddressStreamProviderTest {
}
@Test
public void defaultReturnedWhenNoBetterMatch() throws Exception {
File f = buildFile("domain linecorp.local\n" +
public void defaultReturnedWhenNoBetterMatch(@TempDir Path tempDir) throws Exception {
File f = buildFile(tempDir, "domain linecorp.local\n" +
"nameserver 127.0.0.2\n" +
"nameserver 127.0.0.3\n");
File f2 = buildFile("domain squarecorp.local\n" +
File f2 = buildFile(tempDir, "domain squarecorp.local\n" +
"nameserver 127.0.0.4\n" +
"nameserver 127.0.0.5\n");
UnixResolverDnsServerAddressStreamProvider p =
@ -122,11 +121,11 @@ public class UnixResolverDnsServerAddressStreamProviderTest {
}
@Test
public void moreRefinedSelectionReturnedWhenMatch() throws Exception {
File f = buildFile("domain linecorp.local\n" +
public void moreRefinedSelectionReturnedWhenMatch(@TempDir Path tempDir) throws Exception {
File f = buildFile(tempDir, "domain linecorp.local\n" +
"nameserver 127.0.0.2\n" +
"nameserver 127.0.0.3\n");
File f2 = buildFile("domain dc1.linecorp.local\n" +
File f2 = buildFile(tempDir, "domain dc1.linecorp.local\n" +
"nameserver 127.0.0.4\n" +
"nameserver 127.0.0.5\n");
UnixResolverDnsServerAddressStreamProvider p =
@ -138,96 +137,96 @@ public class UnixResolverDnsServerAddressStreamProviderTest {
}
@Test
public void ndotsOptionIsParsedIfPresent() throws IOException {
File f = buildFile("search localdomain\n" +
public void ndotsOptionIsParsedIfPresent(@TempDir Path tempDir) throws IOException {
File f = buildFile(tempDir, "search localdomain\n" +
"nameserver 127.0.0.11\n" +
"options ndots:0\n");
assertEquals(0, parseEtcResolverOptions(f).ndots());
f = buildFile("search localdomain\n" +
f = buildFile(tempDir, "search localdomain\n" +
"nameserver 127.0.0.11\n" +
"options ndots:123 foo:goo\n");
assertEquals(123, parseEtcResolverOptions(f).ndots());
}
@Test
public void defaultValueReturnedIfNdotsOptionsNotPresent() throws IOException {
File f = buildFile("search localdomain\n" +
public void defaultValueReturnedIfNdotsOptionsNotPresent(@TempDir Path tempDir) throws IOException {
File f = buildFile(tempDir, "search localdomain\n" +
"nameserver 127.0.0.11\n");
assertEquals(1, parseEtcResolverOptions(f).ndots());
}
@Test
public void timeoutOptionIsParsedIfPresent() throws IOException {
File f = buildFile("search localdomain\n" +
public void timeoutOptionIsParsedIfPresent(@TempDir Path tempDir) throws IOException {
File f = buildFile(tempDir, "search localdomain\n" +
"nameserver 127.0.0.11\n" +
"options timeout:0\n");
assertEquals(0, parseEtcResolverOptions(f).timeout());
f = buildFile("search localdomain\n" +
f = buildFile(tempDir, "search localdomain\n" +
"nameserver 127.0.0.11\n" +
"options foo:bar timeout:124\n");
assertEquals(124, parseEtcResolverOptions(f).timeout());
}
@Test
public void defaultValueReturnedIfTimeoutOptionsIsNotPresent() throws IOException {
File f = buildFile("search localdomain\n" +
public void defaultValueReturnedIfTimeoutOptionsIsNotPresent(@TempDir Path tempDir) throws IOException {
File f = buildFile(tempDir, "search localdomain\n" +
"nameserver 127.0.0.11\n");
assertEquals(5, parseEtcResolverOptions(f).timeout());
}
@Test
public void attemptsOptionIsParsedIfPresent() throws IOException {
File f = buildFile("search localdomain\n" +
public void attemptsOptionIsParsedIfPresent(@TempDir Path tempDir) throws IOException {
File f = buildFile(tempDir, "search localdomain\n" +
"nameserver 127.0.0.11\n" +
"options attempts:0\n");
assertEquals(0, parseEtcResolverOptions(f).attempts());
f = buildFile("search localdomain\n" +
f = buildFile(tempDir, "search localdomain\n" +
"nameserver 127.0.0.11\n" +
"options foo:bar attempts:12\n");
assertEquals(12, parseEtcResolverOptions(f).attempts());
}
@Test
public void defaultValueReturnedIfAttemptsOptionsIsNotPresent() throws IOException {
File f = buildFile("search localdomain\n" +
public void defaultValueReturnedIfAttemptsOptionsIsNotPresent(@TempDir Path tempDir) throws IOException {
File f = buildFile(tempDir, "search localdomain\n" +
"nameserver 127.0.0.11\n");
assertEquals(16, parseEtcResolverOptions(f).attempts());
}
@Test
public void emptyEtcResolverDirectoryDoesNotThrow() throws IOException {
File f = buildFile("domain linecorp.local\n" +
public void emptyEtcResolverDirectoryDoesNotThrow(@TempDir Path tempDir) throws IOException {
File f = buildFile(tempDir, "domain linecorp.local\n" +
"nameserver 127.0.0.2\n" +
"nameserver 127.0.0.3\n");
UnixResolverDnsServerAddressStreamProvider p =
new UnixResolverDnsServerAddressStreamProvider(f, folder.newFolder().listFiles());
new UnixResolverDnsServerAddressStreamProvider(f, tempDir.resolve("netty-empty").toFile().listFiles());
DnsServerAddressStream stream = p.nameServerAddressStream("somehost");
assertHostNameEquals("127.0.0.2", stream.next());
}
@Test
public void searchDomainsWithOnlyDomain() throws IOException {
File f = buildFile("domain linecorp.local\n" +
public void searchDomainsWithOnlyDomain(@TempDir Path tempDir) throws IOException {
File f = buildFile(tempDir, "domain linecorp.local\n" +
"nameserver 127.0.0.2\n");
List<String> domains = UnixResolverDnsServerAddressStreamProvider.parseEtcResolverSearchDomains(f);
assertEquals(Collections.singletonList("linecorp.local"), domains);
}
@Test
public void searchDomainsWithOnlySearch() throws IOException {
File f = buildFile("search linecorp.local\n" +
public void searchDomainsWithOnlySearch(@TempDir Path tempDir) throws IOException {
File f = buildFile(tempDir, "search linecorp.local\n" +
"nameserver 127.0.0.2\n");
List<String> domains = UnixResolverDnsServerAddressStreamProvider.parseEtcResolverSearchDomains(f);
assertEquals(Collections.singletonList("linecorp.local"), domains);
}
@Test
public void searchDomainsWithMultipleSearch() throws IOException {
File f = buildFile("search linecorp.local\n" +
public void searchDomainsWithMultipleSearch(@TempDir Path tempDir) throws IOException {
File f = buildFile(tempDir, "search linecorp.local\n" +
"search squarecorp.local\n" +
"nameserver 127.0.0.2\n");
List<String> domains = UnixResolverDnsServerAddressStreamProvider.parseEtcResolverSearchDomains(f);
@ -235,24 +234,24 @@ public class UnixResolverDnsServerAddressStreamProviderTest {
}
@Test
public void searchDomainsWithMultipleSearchSeperatedByWhitespace() throws IOException {
File f = buildFile("search linecorp.local squarecorp.local\n" +
public void searchDomainsWithMultipleSearchSeperatedByWhitespace(@TempDir Path tempDir) throws IOException {
File f = buildFile(tempDir, "search linecorp.local squarecorp.local\n" +
"nameserver 127.0.0.2\n");
List<String> domains = UnixResolverDnsServerAddressStreamProvider.parseEtcResolverSearchDomains(f);
assertEquals(Arrays.asList("linecorp.local", "squarecorp.local"), domains);
}
@Test
public void searchDomainsWithMultipleSearchSeperatedByTab() throws IOException {
File f = buildFile("search linecorp.local\tsquarecorp.local\n" +
public void searchDomainsWithMultipleSearchSeperatedByTab(@TempDir Path tempDir) throws IOException {
File f = buildFile(tempDir, "search linecorp.local\tsquarecorp.local\n" +
"nameserver 127.0.0.2\n");
List<String> domains = UnixResolverDnsServerAddressStreamProvider.parseEtcResolverSearchDomains(f);
assertEquals(Arrays.asList("linecorp.local", "squarecorp.local"), domains);
}
@Test
public void searchDomainsPrecedence() throws IOException {
File f = buildFile("domain linecorp.local\n" +
public void searchDomainsPrecedence(@TempDir Path tempDir) throws IOException {
File f = buildFile(tempDir, "domain linecorp.local\n" +
"search squarecorp.local\n" +
"nameserver 127.0.0.2\n");
List<String> domains = UnixResolverDnsServerAddressStreamProvider.parseEtcResolverSearchDomains(f);
@ -260,8 +259,8 @@ public class UnixResolverDnsServerAddressStreamProviderTest {
}
@Test
public void ignoreInvalidEntries() throws Exception {
File f = buildFile("domain netty.local\n" +
public void ignoreInvalidEntries(@TempDir Path tempDir) throws Exception {
File f = buildFile(tempDir, "domain netty.local\n" +
"nameserver nil\n" +
"nameserver 127.0.0.3\n");
UnixResolverDnsServerAddressStreamProvider p =
@ -272,20 +271,15 @@ public class UnixResolverDnsServerAddressStreamProviderTest {
assertHostNameEquals("127.0.0.3", stream.next());
}
private File buildFile(String contents) throws IOException {
File f = folder.newFile();
OutputStream out = new FileOutputStream(f);
try {
out.write(contents.getBytes(CharsetUtil.UTF_8));
} finally {
out.close();
}
return f;
private File buildFile(Path tempDir, String contents) throws IOException {
Path path = tempDir.resolve("netty-dns-" + UUID.randomUUID().toString().substring(24) + ".txt");
Files.write(path, contents.getBytes(CharsetUtil.UTF_8));
return path.toFile();
}
@Test
public void ignoreComments() throws Exception {
File f = buildFile("domain linecorp.local\n" +
public void ignoreComments(@TempDir Path tempDir) throws Exception {
File f = buildFile(tempDir, "domain linecorp.local\n" +
"nameserver 127.0.0.2 #somecomment\n");
UnixResolverDnsServerAddressStreamProvider p =
new UnixResolverDnsServerAddressStreamProvider(f, null);
@ -295,6 +289,6 @@ public class UnixResolverDnsServerAddressStreamProviderTest {
}
private static void assertHostNameEquals(String expectedHostname, InetSocketAddress next) {
assertEquals("unexpected hostname: " + next, expectedHostname, next.getHostString());
assertEquals(expectedHostname, next.getHostString(), "unexpected hostname: " + next);
}
}