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 Norman Maurer
parent e9a8eaeb18
commit 187c6015b4
4 changed files with 220 additions and 100 deletions

View File

@ -27,13 +27,14 @@ import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.local.LocalAddress;
import io.netty.channel.local.LocalChannel;
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,25 @@ 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 org.junit.jupiter.api.function.Executable;
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,70 +109,121 @@ 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";
final String header = "PROXY UDP4 192.168.0.1 192.168.0.11 56324 443\r\n";
assertThrows(HAProxyProtocolException.class, new Executable() {
@Override
public void execute() {
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";
final String header = "PROXY TCP4 192.168.0.1 192.168.0.11 80000 443\r\n";
assertThrows(HAProxyProtocolException.class, new Executable() {
@Override
public void execute() {
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";
final String header = "PROXY TCP4 299.168.0.1 192.168.0.11 56324 443\r\n";
assertThrows(HAProxyProtocolException.class, new Executable() {
@Override
public void execute() {
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";
final String header =
"PROXY TCP6 r001:0db8:85a3:0000:0000:8a2e:0370:7334 1050:0:0:0:5:600:300c:326b 56324 443\r\n";
assertThrows(HAProxyProtocolException.class, new Executable() {
@Override
public void execute() {
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";
final String header = "PROXY TCP7 192.168.0.1 192.168.0.11 56324 443\r\n";
assertThrows(HAProxyProtocolException.class, new Executable() {
@Override
public void execute() {
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";
final String header = "PROXY TCP4 192.168.0.1 192.168.0.11 56324\r\n";
assertThrows(HAProxyProtocolException.class, new Executable() {
@Override
public void execute() {
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";
final String header = "PROXY TCP4 192.168.0.1 192.168.0.11 56324 443 123\r\n";
assertThrows(HAProxyProtocolException.class, new Executable() {
@Override
public void execute() {
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";
final String header = "PING TCP4 192.168.0.1 192.168.0.11 56324 443\r\n";
assertThrows(HAProxyProtocolException.class, new Executable() {
@Override
public void execute() {
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";
final String header = "PROXY TCP4 192.168.0.1 192.168.0.11 56324 443\nGET / HTTP/1.1\r\n";
assertThrows(HAProxyProtocolException.class, new Executable() {
@Override
public void execute() {
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 " +
final String header = "PROXY TCP4 192.168.0.1 192.168.0.11 56324 " +
"00000000000000000000000000000000000000000000000000000000000000000443\r\n";
assertThrows(HAProxyProtocolException.class, new Executable() {
@Override
public void execute() {
ch.writeInbound(copiedBuffer(header, CharsetUtil.US_ASCII));
}
});
}
@Test
public void testFailSlowHeaderTooLong() {
EmbeddedChannel slowFailCh = new EmbeddedChannel(new HAProxyMessageDecoder(false));
final EmbeddedChannel slowFailCh = new EmbeddedChannel(new HAProxyMessageDecoder(false));
try {
String headerPart1 = "PROXY TCP4 192.168.0.1 192.168.0.11 56324 " +
"000000000000000000000000000000000000000000000000000000000000000000000443";
@ -179,13 +232,15 @@ public class HAProxyMessageDecoderTest {
String headerPart2 = "more header data";
// Should not throw exception
assertFalse(slowFailCh.writeInbound(copiedBuffer(headerPart2, CharsetUtil.US_ASCII)));
String headerPart3 = "end of header\r\n";
final 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, new Executable() {
@Override
public void execute() {
slowFailCh.writeInbound(copiedBuffer(headerPart3, CharsetUtil.US_ASCII));
}
}, "over " + discarded);
} finally {
assertFalse(slowFailCh.finishAndReleaseAll());
}
@ -193,13 +248,16 @@ public class HAProxyMessageDecoderTest {
@Test
public void testFailFastHeaderTooLong() {
EmbeddedChannel fastFailCh = new EmbeddedChannel(new HAProxyMessageDecoder(true));
final EmbeddedChannel fastFailCh = new EmbeddedChannel(new HAProxyMessageDecoder(true));
try {
String headerPart1 = "PROXY TCP4 192.168.0.1 192.168.0.11 56324 " +
final 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, new Executable() {
@Override
public void execute() {
fastFailCh.writeInbound(copiedBuffer(headerPart1, CharsetUtil.US_ASCII));
}
}, "over " + headerPart1.length());
} finally {
assertFalse(fastFailCh.finishAndReleaseAll());
}
@ -639,7 +697,7 @@ public class HAProxyMessageDecoderTest {
}
@Test
public void testV2WithSslTLVs() throws Exception {
public void testV2WithSslTLVs() {
ch = new EmbeddedChannel(new HAProxyMessageDecoder());
final byte[] bytes = {
@ -834,9 +892,9 @@ public class HAProxyMessageDecoderTest {
assertTrue(msg.release());
}
@Test(expected = HAProxyProtocolException.class)
@Test
public void testV2InvalidProtocol() {
byte[] header = new byte[28];
final byte[] header = new byte[28];
header[0] = 0x0D; // Binary Prefix
header[1] = 0x0A; // -----
header[2] = 0x0D; // -----
@ -872,12 +930,17 @@ public class HAProxyMessageDecoderTest {
header[26] = 0x01; // Destination Port
header[27] = (byte) 0xbb; // -----
assertThrows(HAProxyProtocolException.class, new Executable() {
@Override
public void execute() {
ch.writeInbound(copiedBuffer(header));
}
});
}
@Test(expected = HAProxyProtocolException.class)
@Test
public void testV2MissingParams() {
byte[] header = new byte[26];
final byte[] header = new byte[26];
header[0] = 0x0D; // Binary Prefix
header[1] = 0x0A; // -----
header[2] = 0x0D; // -----
@ -910,12 +973,17 @@ public class HAProxyMessageDecoderTest {
header[24] = (byte) 0xdc; // Source Port
header[25] = 0x04; // -----
assertThrows(HAProxyProtocolException.class, new Executable() {
@Override
public void execute() {
ch.writeInbound(copiedBuffer(header));
}
});
}
@Test(expected = HAProxyProtocolException.class)
@Test
public void testV2InvalidCommand() {
byte[] header = new byte[28];
final byte[] header = new byte[28];
header[0] = 0x0D; // Binary Prefix
header[1] = 0x0A; // -----
header[2] = 0x0D; // -----
@ -951,12 +1019,17 @@ public class HAProxyMessageDecoderTest {
header[26] = 0x01; // Destination Port
header[27] = (byte) 0xbb; // -----
assertThrows(HAProxyProtocolException.class, new Executable() {
@Override
public void execute() {
ch.writeInbound(copiedBuffer(header));
}
});
}
@Test(expected = HAProxyProtocolException.class)
@Test
public void testV2InvalidVersion() {
byte[] header = new byte[28];
final byte[] header = new byte[28];
header[0] = 0x0D; // Binary Prefix
header[1] = 0x0A; // -----
header[2] = 0x0D; // -----
@ -992,14 +1065,19 @@ public class HAProxyMessageDecoderTest {
header[26] = 0x01; // Destination Port
header[27] = (byte) 0xbb; // -----
assertThrows(HAProxyProtocolException.class, new Executable() {
@Override
public void execute() {
ch.writeInbound(copiedBuffer(header));
}
});
}
@Test(expected = HAProxyProtocolException.class)
@Test
public void testV2HeaderTooLong() {
ch = new EmbeddedChannel(new HAProxyMessageDecoder(0));
byte[] header = new byte[248];
final byte[] header = new byte[248];
header[0] = 0x0D; // Binary Prefix
header[1] = 0x0A; // -----
header[2] = 0x0D; // -----
@ -1035,8 +1113,13 @@ public class HAProxyMessageDecoderTest {
header[26] = 0x01; // Destination Port
header[27] = (byte) 0xbb; // -----
assertThrows(HAProxyProtocolException.class, new Executable() {
@Override
public void execute() {
ch.writeInbound(copiedBuffer(header));
}
});
}
@Test
public void testV2IncompleteHeader() {

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,8 @@ 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 org.junit.jupiter.api.function.Executable;
import java.util.ArrayList;
import java.util.Collections;
@ -31,7 +32,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 +361,79 @@ public class HaProxyMessageEncoderTest {
assertFalse(ch.finish());
}
@Test(expected = IllegalArgumentException.class)
@Test
public void testInvalidIpV4Address() {
String invalidIpv4Address = "192.168.0.1234";
final String invalidIpv4Address = "192.168.0.1234";
assertThrows(IllegalArgumentException.class, new Executable() {
@Override
public void execute() {
new HAProxyMessage(
HAProxyProtocolVersion.V1, HAProxyCommand.PROXY, HAProxyProxiedProtocol.TCP4,
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";
final String invalidIpv6Address = "2001:0db8:85a3:0000:0000:8a2e:0370:73345";
assertThrows(IllegalArgumentException.class, new Executable() {
@Override
public void execute() {
new HAProxyMessage(
HAProxyProtocolVersion.V1, HAProxyCommand.PROXY, HAProxyProxiedProtocol.TCP6,
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]);
final String invalidUnixAddress = new String(new byte[UNIX_ADDRESS_BYTES_LENGTH + 1]);
assertThrows(IllegalArgumentException.class, new Executable() {
@Override
public void execute() {
new HAProxyMessage(
HAProxyProtocolVersion.V2, HAProxyCommand.PROXY, HAProxyProxiedProtocol.UNIX_STREAM,
invalidUnixAddress, "/var/run/dst.sock", 0, 0);
}
});
}
@Test(expected = NullPointerException.class)
@Test
public void testNullUnixAddress() {
assertThrows(NullPointerException.class, new Executable() {
@Override
public void execute() {
new HAProxyMessage(
HAProxyProtocolVersion.V2, HAProxyCommand.PROXY, HAProxyProxiedProtocol.UNIX_STREAM,
null, null, 0, 0);
}
});
}
@Test(expected = IllegalArgumentException.class)
@Test
public void testLongUnixAddress() {
String longUnixAddress = new String(new char[109]).replace("\0", "a");
final String longUnixAddress = new String(new char[109]).replace("\0", "a");
assertThrows(IllegalArgumentException.class, new Executable() {
@Override
public void execute() {
new HAProxyMessage(
HAProxyProtocolVersion.V2, HAProxyCommand.PROXY, HAProxyProxiedProtocol.UNIX_STREAM,
"source", longUnixAddress, 0, 0);
}
});
}
@Test(expected = IllegalArgumentException.class)
@Test
public void testInvalidUnixPort() {
assertThrows(IllegalArgumentException.class, new Executable() {
@Override
public void execute() {
new HAProxyMessage(
HAProxyProtocolVersion.V2, HAProxyCommand.PROXY, HAProxyProxiedProtocol.UNIX_STREAM,
"/var/run/src.sock", "/var/run/dst.sock", 80, 443);
}
});
}
}