From ab566ee357ab87f7c1d4864d94673fd7546c0734 Mon Sep 17 00:00:00 2001 From: Riley Park Date: Wed, 26 May 2021 00:50:54 -0700 Subject: [PATCH] Migrate codec-haproxy tests to JUnit 5 (#11308) 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 codec-smtp tests Result: Related to https://github.com/netty/netty/issues/10757 --- .../codec/haproxy/HAProxyIntegrationTest.java | 5 +- .../haproxy/HAProxyMessageDecoderTest.java | 101 ++++++++++-------- .../codec/haproxy/HAProxySSLTLVTest.java | 5 +- .../haproxy/HaProxyMessageEncoderTest.java | 44 ++++---- 4 files changed, 85 insertions(+), 70 deletions(-) diff --git a/codec-haproxy/src/test/java/io/netty/handler/codec/haproxy/HAProxyIntegrationTest.java b/codec-haproxy/src/test/java/io/netty/handler/codec/haproxy/HAProxyIntegrationTest.java index e3e2efa940..d72ebe53dd 100644 --- a/codec-haproxy/src/test/java/io/netty/handler/codec/haproxy/HAProxyIntegrationTest.java +++ b/codec-haproxy/src/test/java/io/netty/handler/codec/haproxy/HAProxyIntegrationTest.java @@ -28,13 +28,14 @@ import io.netty.channel.local.LocalAddress; import io.netty.channel.local.LocalChannel; import io.netty.channel.local.LocalHandler; import io.netty.channel.local.LocalServerChannel; -import org.junit.Test; +import org.junit.jupiter.api.Test; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicReference; -import static org.junit.Assert.*; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; public class HAProxyIntegrationTest { diff --git a/codec-haproxy/src/test/java/io/netty/handler/codec/haproxy/HAProxyMessageDecoderTest.java b/codec-haproxy/src/test/java/io/netty/handler/codec/haproxy/HAProxyMessageDecoderTest.java index ef56606218..1e8ce7c743 100644 --- a/codec-haproxy/src/test/java/io/netty/handler/codec/haproxy/HAProxyMessageDecoderTest.java +++ b/codec-haproxy/src/test/java/io/netty/handler/codec/haproxy/HAProxyMessageDecoderTest.java @@ -23,23 +23,24 @@ import io.netty.handler.codec.ProtocolDetectionState; import io.netty.handler.codec.haproxy.HAProxyProxiedProtocol.AddressFamily; import io.netty.handler.codec.haproxy.HAProxyProxiedProtocol.TransportProtocol; import io.netty.util.CharsetUtil; -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import java.util.List; import static io.netty.buffer.Unpooled.*; -import static org.junit.Assert.*; +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNull; +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 HAProxyMessageDecoderTest { - @Rule - public ExpectedException exceptionRule = ExpectedException.none(); - private EmbeddedChannel ch; - @Before + @BeforeEach public void setUp() { ch = new EmbeddedChannel(new HAProxyMessageDecoder()); } @@ -107,65 +108,75 @@ public class HAProxyMessageDecoderTest { assertTrue(msg.release()); } - @Test(expected = HAProxyProtocolException.class) + @Test public void testV1NoUDP() { String header = "PROXY UDP4 192.168.0.1 192.168.0.11 56324 443\r\n"; - ch.writeInbound(copiedBuffer(header, CharsetUtil.US_ASCII)); + assertThrows(HAProxyProtocolException.class, + () -> ch.writeInbound(copiedBuffer(header, CharsetUtil.US_ASCII))); } - @Test(expected = HAProxyProtocolException.class) + @Test public void testInvalidPort() { String header = "PROXY TCP4 192.168.0.1 192.168.0.11 80000 443\r\n"; - ch.writeInbound(copiedBuffer(header, CharsetUtil.US_ASCII)); + assertThrows(HAProxyProtocolException.class, + () -> ch.writeInbound(copiedBuffer(header, CharsetUtil.US_ASCII))); } - @Test(expected = HAProxyProtocolException.class) + @Test public void testInvalidIPV4Address() { String header = "PROXY TCP4 299.168.0.1 192.168.0.11 56324 443\r\n"; - ch.writeInbound(copiedBuffer(header, CharsetUtil.US_ASCII)); + assertThrows(HAProxyProtocolException.class, + () -> ch.writeInbound(copiedBuffer(header, CharsetUtil.US_ASCII))); } - @Test(expected = HAProxyProtocolException.class) + @Test public void testInvalidIPV6Address() { String header = "PROXY TCP6 r001:0db8:85a3:0000:0000:8a2e:0370:7334 1050:0:0:0:5:600:300c:326b 56324 443\r\n"; - ch.writeInbound(copiedBuffer(header, CharsetUtil.US_ASCII)); + assertThrows(HAProxyProtocolException.class, + () -> ch.writeInbound(copiedBuffer(header, CharsetUtil.US_ASCII))); } - @Test(expected = HAProxyProtocolException.class) + @Test public void testInvalidProtocol() { String header = "PROXY TCP7 192.168.0.1 192.168.0.11 56324 443\r\n"; - ch.writeInbound(copiedBuffer(header, CharsetUtil.US_ASCII)); + assertThrows(HAProxyProtocolException.class, + () -> ch.writeInbound(copiedBuffer(header, CharsetUtil.US_ASCII))); } - @Test(expected = HAProxyProtocolException.class) + @Test public void testMissingParams() { String header = "PROXY TCP4 192.168.0.1 192.168.0.11 56324\r\n"; - ch.writeInbound(copiedBuffer(header, CharsetUtil.US_ASCII)); + assertThrows(HAProxyProtocolException.class, + () -> ch.writeInbound(copiedBuffer(header, CharsetUtil.US_ASCII))); } - @Test(expected = HAProxyProtocolException.class) + @Test public void testTooManyParams() { String header = "PROXY TCP4 192.168.0.1 192.168.0.11 56324 443 123\r\n"; - ch.writeInbound(copiedBuffer(header, CharsetUtil.US_ASCII)); + assertThrows(HAProxyProtocolException.class, + () -> ch.writeInbound(copiedBuffer(header, CharsetUtil.US_ASCII))); } - @Test(expected = HAProxyProtocolException.class) + @Test public void testInvalidCommand() { String header = "PING TCP4 192.168.0.1 192.168.0.11 56324 443\r\n"; - ch.writeInbound(copiedBuffer(header, CharsetUtil.US_ASCII)); + assertThrows(HAProxyProtocolException.class, + () -> ch.writeInbound(copiedBuffer(header, CharsetUtil.US_ASCII))); } - @Test(expected = HAProxyProtocolException.class) + @Test public void testInvalidEOL() { String header = "PROXY TCP4 192.168.0.1 192.168.0.11 56324 443\nGET / HTTP/1.1\r\n"; - ch.writeInbound(copiedBuffer(header, CharsetUtil.US_ASCII)); + assertThrows(HAProxyProtocolException.class, + () -> ch.writeInbound(copiedBuffer(header, CharsetUtil.US_ASCII))); } - @Test(expected = HAProxyProtocolException.class) + @Test public void testHeaderTooLong() { String header = "PROXY TCP4 192.168.0.1 192.168.0.11 56324 " + "00000000000000000000000000000000000000000000000000000000000000000443\r\n"; - ch.writeInbound(copiedBuffer(header, CharsetUtil.US_ASCII)); + assertThrows(HAProxyProtocolException.class, + () -> ch.writeInbound(copiedBuffer(header, CharsetUtil.US_ASCII))); } @Test @@ -182,10 +193,8 @@ public class HAProxyMessageDecoderTest { String headerPart3 = "end of header\r\n"; int discarded = headerPart1.length() + headerPart2.length() + headerPart3.length() - 2; - // Should throw exception - exceptionRule.expect(HAProxyProtocolException.class); - exceptionRule.expectMessage("over " + discarded); - assertFalse(slowFailCh.writeInbound(copiedBuffer(headerPart3, CharsetUtil.US_ASCII))); + assertThrows(HAProxyProtocolException.class, + () -> slowFailCh.writeInbound(copiedBuffer(headerPart3, CharsetUtil.US_ASCII)), "over " + discarded); } finally { assertFalse(slowFailCh.finishAndReleaseAll()); } @@ -197,9 +206,9 @@ public class HAProxyMessageDecoderTest { try { String headerPart1 = "PROXY TCP4 192.168.0.1 192.168.0.11 56324 " + "000000000000000000000000000000000000000000000000000000000000000000000443"; - exceptionRule.expect(HAProxyProtocolException.class); // Should throw exception, fail fast - exceptionRule.expectMessage("over " + headerPart1.length()); - assertFalse(fastFailCh.writeInbound(copiedBuffer(headerPart1, CharsetUtil.US_ASCII))); + assertThrows(HAProxyProtocolException.class, + () -> fastFailCh.writeInbound(copiedBuffer(headerPart1, CharsetUtil.US_ASCII)), + "over " + headerPart1.length()); } finally { assertFalse(fastFailCh.finishAndReleaseAll()); } @@ -834,7 +843,7 @@ public class HAProxyMessageDecoderTest { assertTrue(msg.release()); } - @Test(expected = HAProxyProtocolException.class) + @Test public void testV2InvalidProtocol() { byte[] header = new byte[28]; header[0] = 0x0D; // Binary Prefix @@ -872,10 +881,10 @@ public class HAProxyMessageDecoderTest { header[26] = 0x01; // Destination Port header[27] = (byte) 0xbb; // ----- - ch.writeInbound(copiedBuffer(header)); + assertThrows(HAProxyProtocolException.class, () -> ch.writeInbound(copiedBuffer(header))); } - @Test(expected = HAProxyProtocolException.class) + @Test public void testV2MissingParams() { byte[] header = new byte[26]; header[0] = 0x0D; // Binary Prefix @@ -910,10 +919,10 @@ public class HAProxyMessageDecoderTest { header[24] = (byte) 0xdc; // Source Port header[25] = 0x04; // ----- - ch.writeInbound(copiedBuffer(header)); + assertThrows(HAProxyProtocolException.class, () -> ch.writeInbound(copiedBuffer(header))); } - @Test(expected = HAProxyProtocolException.class) + @Test public void testV2InvalidCommand() { byte[] header = new byte[28]; header[0] = 0x0D; // Binary Prefix @@ -951,10 +960,10 @@ public class HAProxyMessageDecoderTest { header[26] = 0x01; // Destination Port header[27] = (byte) 0xbb; // ----- - ch.writeInbound(copiedBuffer(header)); + assertThrows(HAProxyProtocolException.class, () -> ch.writeInbound(copiedBuffer(header))); } - @Test(expected = HAProxyProtocolException.class) + @Test public void testV2InvalidVersion() { byte[] header = new byte[28]; header[0] = 0x0D; // Binary Prefix @@ -992,10 +1001,10 @@ public class HAProxyMessageDecoderTest { header[26] = 0x01; // Destination Port header[27] = (byte) 0xbb; // ----- - ch.writeInbound(copiedBuffer(header)); + assertThrows(HAProxyProtocolException.class, () -> ch.writeInbound(copiedBuffer(header))); } - @Test(expected = HAProxyProtocolException.class) + @Test public void testV2HeaderTooLong() { ch = new EmbeddedChannel(new HAProxyMessageDecoder(0)); @@ -1035,7 +1044,7 @@ public class HAProxyMessageDecoderTest { header[26] = 0x01; // Destination Port header[27] = (byte) 0xbb; // ----- - ch.writeInbound(copiedBuffer(header)); + assertThrows(HAProxyProtocolException.class, () -> ch.writeInbound(copiedBuffer(header))); } @Test diff --git a/codec-haproxy/src/test/java/io/netty/handler/codec/haproxy/HAProxySSLTLVTest.java b/codec-haproxy/src/test/java/io/netty/handler/codec/haproxy/HAProxySSLTLVTest.java index 1e0582c628..a2065b41d5 100644 --- a/codec-haproxy/src/test/java/io/netty/handler/codec/haproxy/HAProxySSLTLVTest.java +++ b/codec-haproxy/src/test/java/io/netty/handler/codec/haproxy/HAProxySSLTLVTest.java @@ -17,11 +17,12 @@ package io.netty.handler.codec.haproxy; import io.netty.buffer.Unpooled; -import org.junit.Test; +import org.junit.jupiter.api.Test; import java.util.Collections; -import static org.junit.Assert.*; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; public class HAProxySSLTLVTest { diff --git a/codec-haproxy/src/test/java/io/netty/handler/codec/haproxy/HaProxyMessageEncoderTest.java b/codec-haproxy/src/test/java/io/netty/handler/codec/haproxy/HaProxyMessageEncoderTest.java index 388dc0ddc5..efc201e7dd 100644 --- a/codec-haproxy/src/test/java/io/netty/handler/codec/haproxy/HaProxyMessageEncoderTest.java +++ b/codec-haproxy/src/test/java/io/netty/handler/codec/haproxy/HaProxyMessageEncoderTest.java @@ -23,7 +23,7 @@ import io.netty.channel.embedded.EmbeddedChannel; import io.netty.handler.codec.haproxy.HAProxyTLV.Type; import io.netty.util.ByteProcessor; import io.netty.util.CharsetUtil; -import org.junit.Test; +import org.junit.jupiter.api.Test; import java.util.ArrayList; import java.util.Collections; @@ -31,7 +31,11 @@ import java.util.List; import static io.netty.handler.codec.haproxy.HAProxyConstants.*; import static io.netty.handler.codec.haproxy.HAProxyMessageEncoder.*; -import static org.junit.Assert.*; +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; public class HaProxyMessageEncoderTest { @@ -356,49 +360,49 @@ public class HaProxyMessageEncoderTest { assertFalse(ch.finish()); } - @Test(expected = IllegalArgumentException.class) + @Test public void testInvalidIpV4Address() { String invalidIpv4Address = "192.168.0.1234"; - new HAProxyMessage( + assertThrows(IllegalArgumentException.class, () -> new HAProxyMessage( HAProxyProtocolVersion.V1, HAProxyCommand.PROXY, HAProxyProxiedProtocol.TCP4, - invalidIpv4Address, "192.168.0.11", 56324, 443); + invalidIpv4Address, "192.168.0.11", 56324, 443)); } - @Test(expected = IllegalArgumentException.class) + @Test public void testInvalidIpV6Address() { String invalidIpv6Address = "2001:0db8:85a3:0000:0000:8a2e:0370:73345"; - new HAProxyMessage( + assertThrows(IllegalArgumentException.class, () -> new HAProxyMessage( HAProxyProtocolVersion.V1, HAProxyCommand.PROXY, HAProxyProxiedProtocol.TCP6, - invalidIpv6Address, "1050:0:0:0:5:600:300c:326b", 56324, 443); + invalidIpv6Address, "1050:0:0:0:5:600:300c:326b", 56324, 443)); } - @Test(expected = IllegalArgumentException.class) + @Test public void testInvalidUnixAddress() { String invalidUnixAddress = new String(new byte[UNIX_ADDRESS_BYTES_LENGTH + 1]); - new HAProxyMessage( + assertThrows(IllegalArgumentException.class, () -> new HAProxyMessage( HAProxyProtocolVersion.V2, HAProxyCommand.PROXY, HAProxyProxiedProtocol.UNIX_STREAM, - invalidUnixAddress, "/var/run/dst.sock", 0, 0); + invalidUnixAddress, "/var/run/dst.sock", 0, 0)); } - @Test(expected = NullPointerException.class) + @Test public void testNullUnixAddress() { - new HAProxyMessage( + assertThrows(NullPointerException.class, () -> new HAProxyMessage( HAProxyProtocolVersion.V2, HAProxyCommand.PROXY, HAProxyProxiedProtocol.UNIX_STREAM, - null, null, 0, 0); + null, null, 0, 0)); } - @Test(expected = IllegalArgumentException.class) + @Test public void testLongUnixAddress() { String longUnixAddress = new String(new char[109]).replace("\0", "a"); - new HAProxyMessage( + assertThrows(IllegalArgumentException.class, () -> new HAProxyMessage( HAProxyProtocolVersion.V2, HAProxyCommand.PROXY, HAProxyProxiedProtocol.UNIX_STREAM, - "source", longUnixAddress, 0, 0); + "source", longUnixAddress, 0, 0)); } - @Test(expected = IllegalArgumentException.class) + @Test public void testInvalidUnixPort() { - new HAProxyMessage( + assertThrows(IllegalArgumentException.class, () -> new HAProxyMessage( HAProxyProtocolVersion.V2, HAProxyCommand.PROXY, HAProxyProxiedProtocol.UNIX_STREAM, - "/var/run/src.sock", "/var/run/dst.sock", 80, 443); + "/var/run/src.sock", "/var/run/dst.sock", 80, 443)); } }