Modernize RemoteDeprecationFilterTest

This commit is contained in:
Jon Chambers authored and Jon Chambers committed 2026-03-24 14:10:51 -04:00
1 parent 04c102e51f
commit 5d59538924
1 file changed
+59 -46
@@ -19,6 +19,7 @@ import com.google.common.net.HttpHeaders;
import com.google.common.net.InetAddresses;
import com.google.protobuf.ByteString;
import com.google.rpc.ErrorInfo;
import com.google.rpc.Status;
import com.vdurmont.semver4j.Semver;
import io.grpc.ManagedChannel;
import io.grpc.Server;
@@ -32,8 +33,10 @@ import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.EnumMap;
import java.util.Map;
import java.util.Set;
import java.util.stream.Stream;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
@@ -51,75 +54,79 @@ import org.whispersystems.textsecuregcm.util.ua.ClientPlatform;
class RemoteDeprecationFilterTest {
@Test
void testEmptyMap() throws IOException, ServletException {
// We're happy as long as there's no exception
final DynamicConfigurationManager dynamicConfigurationManager = mock(DynamicConfigurationManager.class);
final DynamicConfiguration dynamicConfiguration = mock(DynamicConfiguration.class);
final DynamicRemoteDeprecationConfiguration emptyConfiguration = DynamicRemoteDeprecationConfiguration.DEFAULT;
private DynamicConfiguration dynamicConfiguration;
private RemoteDeprecationFilter remoteDeprecationFilter;
@BeforeEach
void setUp() {
@SuppressWarnings("unchecked") final DynamicConfigurationManager<DynamicConfiguration> dynamicConfigurationManager =
mock(DynamicConfigurationManager.class);
dynamicConfiguration = mock(DynamicConfiguration.class);
when(dynamicConfigurationManager.getConfiguration()).thenReturn(dynamicConfiguration);
when(dynamicConfiguration.getRemoteDeprecationConfiguration()).thenReturn(emptyConfiguration);
when(dynamicConfiguration.getRemoteDeprecationConfiguration())
.thenReturn(DynamicRemoteDeprecationConfiguration.DEFAULT);
final RemoteDeprecationFilter filter = new RemoteDeprecationFilter(dynamicConfigurationManager);
remoteDeprecationFilter = new RemoteDeprecationFilter(dynamicConfigurationManager);
}
@Test
void testEmptyMap() throws IOException, ServletException {
final HttpServletRequest servletRequest = mock(HttpServletRequest.class);
final HttpServletResponse servletResponse = mock(HttpServletResponse.class);
final FilterChain filterChain = mock(FilterChain.class);
when(servletRequest.getHeader("UserAgent")).thenReturn("Signal-Android/4.68.3");
filter.doFilter(servletRequest, servletResponse, filterChain);
assertDoesNotThrow(() -> remoteDeprecationFilter.doFilter(servletRequest, servletResponse, filterChain));
verify(filterChain).doFilter(servletRequest, servletResponse);
verify(servletResponse, never()).sendError(anyInt());
}
private RemoteDeprecationFilter filterConfiguredForTest() {
final EnumMap<ClientPlatform, Semver> minimumVersionsByPlatform = new EnumMap<>(ClientPlatform.class);
minimumVersionsByPlatform.put(ClientPlatform.ANDROID, new Semver("1.0.0"));
minimumVersionsByPlatform.put(ClientPlatform.IOS, new Semver("1.0.0"));
minimumVersionsByPlatform.put(ClientPlatform.DESKTOP, new Semver("1.0.0"));
private static DynamicRemoteDeprecationConfiguration configuration() {
final EnumMap<ClientPlatform, Semver> minimumVersionsByPlatform = new EnumMap<>(Map.of(
ClientPlatform.ANDROID, new Semver("1.0.0"),
ClientPlatform.IOS, new Semver("1.0.0"),
ClientPlatform.DESKTOP, new Semver("1.0.0")
));
final EnumMap<ClientPlatform, Semver> versionsPendingDeprecationByPlatform = new EnumMap<>(ClientPlatform.class);
minimumVersionsByPlatform.put(ClientPlatform.ANDROID, new Semver("1.1.0"));
minimumVersionsByPlatform.put(ClientPlatform.IOS, new Semver("1.1.0"));
minimumVersionsByPlatform.put(ClientPlatform.DESKTOP, new Semver("1.1.0"));
final EnumMap<ClientPlatform, Semver> versionsPendingDeprecationByPlatform = new EnumMap<>(Map.of(
ClientPlatform.ANDROID, new Semver("1.1.0"),
ClientPlatform.IOS, new Semver("1.1.0"),
ClientPlatform.DESKTOP, new Semver("1.1.0")
));
final EnumMap<ClientPlatform, Set<Semver>> blockedVersionsByPlatform = new EnumMap<>(ClientPlatform.class);
blockedVersionsByPlatform.put(ClientPlatform.DESKTOP, Set.of(new Semver("8.0.0-beta.2")));
final EnumMap<ClientPlatform, Set<Semver>> blockedVersionsByPlatform = new EnumMap<>(Map.of(
ClientPlatform.DESKTOP, Set.of(new Semver("8.0.0-beta.2"))
));
final EnumMap<ClientPlatform, Set<Semver>> versionsPendingBlockByPlatform = new EnumMap<>(ClientPlatform.class);
versionsPendingBlockByPlatform.put(ClientPlatform.DESKTOP, Set.of(new Semver("8.0.0-beta.3")));
final EnumMap<ClientPlatform, Set<Semver>> versionsPendingBlockByPlatform = new EnumMap<>(Map.of(
ClientPlatform.DESKTOP, Set.of(new Semver("8.0.0-beta.3"))
));
final DynamicRemoteDeprecationConfiguration remoteDeprecationConfiguration =
new DynamicRemoteDeprecationConfiguration(
minimumVersionsByPlatform,
versionsPendingDeprecationByPlatform,
blockedVersionsByPlatform,
versionsPendingBlockByPlatform,
true);
final DynamicConfiguration dynamicConfiguration = mock(DynamicConfiguration.class);
final DynamicConfigurationManager dynamicConfigurationManager = mock(DynamicConfigurationManager.class);
when(dynamicConfigurationManager.getConfiguration()).thenReturn(dynamicConfiguration);
when(dynamicConfiguration.getRemoteDeprecationConfiguration()).thenReturn(remoteDeprecationConfiguration);
return new RemoteDeprecationFilter(dynamicConfigurationManager);
return new DynamicRemoteDeprecationConfiguration(
minimumVersionsByPlatform,
versionsPendingDeprecationByPlatform,
blockedVersionsByPlatform,
versionsPendingBlockByPlatform,
true);
}
@ParameterizedTest
@MethodSource
void testFilter(final String userAgent, final boolean expectDeprecation) throws IOException, ServletException {
when(dynamicConfiguration.getRemoteDeprecationConfiguration()).thenReturn(configuration());
final HttpServletRequest servletRequest = mock(HttpServletRequest.class);
final HttpServletResponse servletResponse = mock(HttpServletResponse.class);
final FilterChain filterChain = mock(FilterChain.class);
when(servletRequest.getHeader(HttpHeaders.USER_AGENT)).thenReturn(userAgent);
final RemoteDeprecationFilter filter = filterConfiguredForTest();
filter.doFilter(servletRequest, servletResponse, filterChain);
remoteDeprecationFilter.doFilter(servletRequest, servletResponse, filterChain);
if (expectDeprecation) {
verify(filterChain, never()).doFilter(any(), any());
@@ -130,16 +137,19 @@ class RemoteDeprecationFilterTest {
}
}
@SuppressWarnings("DataFlowIssue")
@ParameterizedTest
@MethodSource(value="testFilter")
void testGrpcFilter(final String userAgentString, final boolean expectDeprecation) throws IOException, InterruptedException {
when(dynamicConfiguration.getRemoteDeprecationConfiguration()).thenReturn(configuration());
final MockRequestAttributesInterceptor mockRequestAttributesInterceptor = new MockRequestAttributesInterceptor();
mockRequestAttributesInterceptor.setRequestAttributes(new RequestAttributes(InetAddresses.forString("127.0.0.1"), userAgentString, null));
final Server testServer = InProcessServerBuilder.forName("RemoteDeprecationFilterTest")
.directExecutor()
.addService(new EchoServiceImpl())
.intercept(filterConfiguredForTest())
.intercept(remoteDeprecationFilter)
.intercept(mockRequestAttributesInterceptor)
.build()
.start();
@@ -150,23 +160,26 @@ class RemoteDeprecationFilterTest {
.build();
try {
final String echoString = "Test";
final EchoServiceGrpc.EchoServiceBlockingStub client = EchoServiceGrpc.newBlockingStub(channel);
final EchoRequest req = EchoRequest.newBuilder()
.setPayload(ByteString.copyFromUtf8(echoString))
.build();
final EchoRequest req = EchoRequest.newBuilder().setPayload(ByteString.copyFromUtf8("cluck cluck, i'm a parrot")).build();
if (expectDeprecation) {
final StatusRuntimeException e = assertThrows(
StatusRuntimeException.class,
() -> client.echo(req));
final com.google.rpc.Status status = StatusProto.fromThrowable(e);
final StatusRuntimeException e = assertThrows(StatusRuntimeException.class, () -> client.echo(req));
final Status status = StatusProto.fromThrowable(e);
final ErrorInfo errorInfo = assertDoesNotThrow(() -> status.getDetailsList().stream()
.filter(any -> any.is(ErrorInfo.class)).findFirst()
.orElseThrow(() -> new AssertionError("No error info found"))
.unpack(ErrorInfo.class));
assertEquals(GrpcExceptions.DOMAIN, errorInfo.getDomain());
assertEquals(io.grpc.Status.Code.INVALID_ARGUMENT.value(), status.getCode());
assertEquals("UPGRADE_REQUIRED", errorInfo.getReason());
} else {
assertEquals("cluck cluck, i'm a parrot", client.echo(req).getPayload().toStringUtf8());
assertEquals(echoString, client.echo(req).getPayload().toStringUtf8());
}
} finally {
testServer.shutdownNow();