diff --git a/service/pom.xml b/service/pom.xml index af243986e..ff14a20aa 100644 --- a/service/pom.xml +++ b/service/pom.xml @@ -492,6 +492,13 @@ linux-x86_64 + + io.netty + netty-testsuite-common + test + ${netty.version} + + org.glassfish.jersey.test-framework jersey-test-framework-core diff --git a/service/src/test/java/org/whispersystems/textsecuregcm/grpc/net/AbstractLeakDetectionTest.java b/service/src/test/java/org/whispersystems/textsecuregcm/grpc/net/AbstractLeakDetectionTest.java deleted file mode 100644 index e219e4f0f..000000000 --- a/service/src/test/java/org/whispersystems/textsecuregcm/grpc/net/AbstractLeakDetectionTest.java +++ /dev/null @@ -1,25 +0,0 @@ -/* - * Copyright 2026 Signal Messenger, LLC - * SPDX License Identifier: AGPL 3.0 only - */ -package org.whispersystems.textsecuregcm.grpc.net; - -import io.netty.util.ResourceLeakDetector; -import org.junit.jupiter.api.AfterAll; -import org.junit.jupiter.api.BeforeAll; - -public abstract class AbstractLeakDetectionTest { - - private static ResourceLeakDetector.Level originalResourceLeakDetectorLevel; - - @BeforeAll - static void setLeakDetectionLevel() { - originalResourceLeakDetectorLevel = ResourceLeakDetector.getLevel(); - ResourceLeakDetector.setLevel(ResourceLeakDetector.Level.PARANOID); - } - - @AfterAll - static void restoreLeakDetectionLevel() { - ResourceLeakDetector.setLevel(originalResourceLeakDetectorLevel); - } -} diff --git a/service/src/test/java/org/whispersystems/textsecuregcm/grpc/net/H2FrameProxyHandlerTest.java b/service/src/test/java/org/whispersystems/textsecuregcm/grpc/net/H2FrameProxyHandlerTest.java index 93ba8f82f..217100053 100644 --- a/service/src/test/java/org/whispersystems/textsecuregcm/grpc/net/H2FrameProxyHandlerTest.java +++ b/service/src/test/java/org/whispersystems/textsecuregcm/grpc/net/H2FrameProxyHandlerTest.java @@ -11,9 +11,12 @@ import static org.junit.jupiter.api.Assertions.assertTrue; import io.netty.channel.WriteBufferWaterMark; import io.netty.channel.embedded.EmbeddedChannel; import java.nio.charset.StandardCharsets; +import io.netty.util.test.LeakPresenceExtension; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; -class H2FrameProxyHandlerTest extends AbstractLeakDetectionTest { +@ExtendWith(LeakPresenceExtension.class) +class H2FrameProxyHandlerTest { @Test void proxyWritabilityChanged() { final EmbeddedChannel target = new EmbeddedChannel(); diff --git a/service/src/test/java/org/whispersystems/textsecuregcm/grpc/net/OmnibusH2ServerTest.java b/service/src/test/java/org/whispersystems/textsecuregcm/grpc/net/OmnibusH2ServerTest.java index 172b0df8f..885d65592 100644 --- a/service/src/test/java/org/whispersystems/textsecuregcm/grpc/net/OmnibusH2ServerTest.java +++ b/service/src/test/java/org/whispersystems/textsecuregcm/grpc/net/OmnibusH2ServerTest.java @@ -49,6 +49,7 @@ import io.netty.handler.ssl.SslContext; import io.netty.handler.ssl.SslContextBuilder; import io.netty.handler.ssl.util.InsecureTrustManagerFactory; import io.netty.util.ReferenceCountUtil; +import io.netty.util.test.LeakPresenceExtension; import java.io.InputStream; import java.net.InetSocketAddress; import java.nio.charset.StandardCharsets; @@ -68,11 +69,13 @@ import javax.annotation.Nullable; import javax.net.ssl.SSLException; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; import org.junitpioneer.jupiter.cartesian.CartesianTest; -class OmnibusH2ServerTest extends AbstractLeakDetectionTest { +@ExtendWith(LeakPresenceExtension.class) +class OmnibusH2ServerTest { private static final String KEYSTORE_PASSWORD = "password"; // Paths that start with PREFIX should go to the prefix backend, everything else to default. diff --git a/service/src/test/java/org/whispersystems/textsecuregcm/grpc/net/ProxyProtocolHandlerTest.java b/service/src/test/java/org/whispersystems/textsecuregcm/grpc/net/ProxyProtocolHandlerTest.java index 0e3b2cb9b..499cce7b4 100644 --- a/service/src/test/java/org/whispersystems/textsecuregcm/grpc/net/ProxyProtocolHandlerTest.java +++ b/service/src/test/java/org/whispersystems/textsecuregcm/grpc/net/ProxyProtocolHandlerTest.java @@ -14,40 +14,65 @@ import io.netty.handler.codec.haproxy.HAProxyMessage; import io.netty.handler.codec.haproxy.HAProxyMessageEncoder; import io.netty.handler.codec.haproxy.HAProxyProtocolVersion; import io.netty.handler.codec.haproxy.HAProxyProxiedProtocol; +import io.netty.util.ReferenceCountUtil; +import io.netty.util.test.LeakPresenceExtension; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; -class ProxyProtocolHandlerTest extends AbstractLeakDetectionTest { - private static final HAProxyMessage PROXY_MESSAGE = new HAProxyMessage( - HAProxyProtocolVersion.V2, HAProxyCommand.PROXY, - HAProxyProxiedProtocol.TCP4, "10.0.0.1", "10.0.0.2", 1234, 5678); +@ExtendWith(LeakPresenceExtension.class) +class ProxyProtocolHandlerTest { + + private static final HAProxyProtocolVersion PROTOCOL_VERSION = HAProxyProtocolVersion.V2; + private static final String SOURCE_ADDRESS = "10.0.0.1"; + private static final String DESTINATION_ADDRESS = "10.0.0.2"; + + private static HAProxyMessage proxyMessage() { + return new HAProxyMessage( + PROTOCOL_VERSION, HAProxyCommand.PROXY, + HAProxyProxiedProtocol.TCP4, SOURCE_ADDRESS, DESTINATION_ADDRESS, 1234, 5678); + } @Test void sendHeader() { + final HAProxyMessage proxyMessage = proxyMessage(); final EmbeddedChannel encoder = new EmbeddedChannel(HAProxyMessageEncoder.INSTANCE); - encoder.writeOutbound(PROXY_MESSAGE.retain()); + encoder.writeOutbound(proxyMessage); final ByteBuf ppv2Bytes = encoder.readOutbound(); final EmbeddedChannel ch = new EmbeddedChannel(new ProxyProtocolHandler()); ch.writeInbound(ppv2Bytes); final HAProxyMessage actual = ch.readInbound(); - assertEquals(PROXY_MESSAGE.protocolVersion(), actual.protocolVersion()); - assertEquals(PROXY_MESSAGE.sourceAddress(), actual.sourceAddress()); + try { + assertEquals(PROTOCOL_VERSION, actual.protocolVersion()); + assertEquals(SOURCE_ADDRESS, actual.sourceAddress()); + } finally { + ReferenceCountUtil.release(actual); + } } @Test void sendHeaderSlowly() { + final HAProxyMessage proxyMessage = proxyMessage(); final EmbeddedChannel encoder = new EmbeddedChannel(HAProxyMessageEncoder.INSTANCE); - encoder.writeOutbound(PROXY_MESSAGE.retain()); - final ByteBuf ppv2Bytes = encoder.readOutbound(); + encoder.writeOutbound(proxyMessage); final EmbeddedChannel ch = new EmbeddedChannel(new ProxyProtocolHandler()); - while (ppv2Bytes.isReadable()) { - assertNull(ch.readInbound()); - ch.writeInbound(ppv2Bytes.readBytes(1)); + final ByteBuf ppv2Bytes = encoder.readOutbound(); + try { + while (ppv2Bytes.isReadable()) { + assertNull(ch.readInbound()); + ch.writeInbound(ppv2Bytes.readBytes(1)); + } + } finally { + ReferenceCountUtil.release(ppv2Bytes); } final HAProxyMessage actual = ch.readInbound(); - assertEquals(PROXY_MESSAGE.protocolVersion(), actual.protocolVersion()); - assertEquals(PROXY_MESSAGE.sourceAddress(), actual.sourceAddress()); + try { + assertEquals(PROTOCOL_VERSION, actual.protocolVersion()); + assertEquals(SOURCE_ADDRESS, actual.sourceAddress()); + } finally { + ReferenceCountUtil.release(actual); + } } }