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
This commit is contained in:
Riley Park 2021-05-26 00:50:54 -07:00 committed by GitHub
parent 343b5df922
commit ab566ee357
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 85 additions and 70 deletions

View File

@ -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 {

View File

@ -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

View File

@ -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 {

View File

@ -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));
}
}