mirror of
https://github.com/signalapp/Signal-Server
synced 2026-04-19 15:38:05 +01:00
Extract configuration for WebSocket max message sizes
This commit is contained in:
@@ -1,12 +1,12 @@
|
||||
/*
|
||||
* Copyright 2013-2020 Signal Messenger, LLC
|
||||
* Copyright 2013-2021 Signal Messenger, LLC
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
package org.whispersystems.websocket;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
@@ -25,27 +25,43 @@ import org.eclipse.jetty.websocket.servlet.ServletUpgradeRequest;
|
||||
import org.eclipse.jetty.websocket.servlet.ServletUpgradeResponse;
|
||||
import org.eclipse.jetty.websocket.servlet.WebSocketServletFactory;
|
||||
import org.glassfish.jersey.server.ResourceConfig;
|
||||
import org.junit.Test;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.whispersystems.websocket.auth.AuthenticationException;
|
||||
import org.whispersystems.websocket.auth.WebSocketAuthenticator;
|
||||
import org.whispersystems.websocket.configuration.WebSocketConfiguration;
|
||||
import org.whispersystems.websocket.setup.WebSocketEnvironment;
|
||||
|
||||
public class WebSocketResourceProviderFactoryTest {
|
||||
|
||||
@Test
|
||||
public void testUnauthorized() throws AuthenticationException, IOException {
|
||||
ResourceConfig jerseyEnvironment = new DropwizardResourceConfig();
|
||||
WebSocketEnvironment environment = mock(WebSocketEnvironment.class );
|
||||
WebSocketAuthenticator authenticator = mock(WebSocketAuthenticator.class);
|
||||
ServletUpgradeRequest request = mock(ServletUpgradeRequest.class );
|
||||
ServletUpgradeResponse response = mock(ServletUpgradeResponse.class);
|
||||
private ResourceConfig jerseyEnvironment;
|
||||
private WebSocketEnvironment<Account> environment;
|
||||
private WebSocketAuthenticator<Account> authenticator;
|
||||
private ServletUpgradeRequest request;
|
||||
private ServletUpgradeResponse response;
|
||||
|
||||
@BeforeEach
|
||||
void setup() {
|
||||
jerseyEnvironment = new DropwizardResourceConfig();
|
||||
//noinspection unchecked
|
||||
environment = mock(WebSocketEnvironment.class);
|
||||
//noinspection unchecked
|
||||
authenticator = mock(WebSocketAuthenticator.class);
|
||||
request = mock(ServletUpgradeRequest.class);
|
||||
response = mock(ServletUpgradeResponse.class);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
void testUnauthorized() throws AuthenticationException, IOException {
|
||||
when(environment.getAuthenticator()).thenReturn(authenticator);
|
||||
when(authenticator.authenticate(eq(request))).thenReturn(new WebSocketAuthenticator.AuthenticationResult<>(Optional.empty(), true));
|
||||
when(authenticator.authenticate(eq(request))).thenReturn(
|
||||
new WebSocketAuthenticator.AuthenticationResult<>(Optional.empty(), true));
|
||||
when(environment.jersey()).thenReturn(jerseyEnvironment);
|
||||
|
||||
WebSocketResourceProviderFactory factory = new WebSocketResourceProviderFactory(environment, Account.class);
|
||||
Object connection = factory.createWebSocket(request, response);
|
||||
WebSocketResourceProviderFactory<?> factory = new WebSocketResourceProviderFactory<>(environment, Account.class,
|
||||
mock(WebSocketConfiguration.class));
|
||||
Object connection = factory.createWebSocket(request, response);
|
||||
|
||||
assertNull(connection);
|
||||
verify(response).sendForbidden(eq("Unauthorized"));
|
||||
@@ -53,47 +69,40 @@ public class WebSocketResourceProviderFactoryTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testValidAuthorization() throws AuthenticationException {
|
||||
ResourceConfig jerseyEnvironment = new DropwizardResourceConfig();
|
||||
WebSocketEnvironment environment = mock(WebSocketEnvironment.class );
|
||||
WebSocketAuthenticator authenticator = mock(WebSocketAuthenticator.class );
|
||||
ServletUpgradeRequest request = mock(ServletUpgradeRequest.class );
|
||||
ServletUpgradeResponse response = mock(ServletUpgradeResponse.class );
|
||||
Session session = mock(Session.class );
|
||||
Account account = new Account();
|
||||
void testValidAuthorization() throws AuthenticationException {
|
||||
Session session = mock(Session.class);
|
||||
Account account = new Account();
|
||||
|
||||
when(environment.getAuthenticator()).thenReturn(authenticator);
|
||||
when(authenticator.authenticate(eq(request))).thenReturn(new WebSocketAuthenticator.AuthenticationResult<>(Optional.of(account), true));
|
||||
when(authenticator.authenticate(eq(request))).thenReturn(
|
||||
new WebSocketAuthenticator.AuthenticationResult<>(Optional.of(account), true));
|
||||
when(environment.jersey()).thenReturn(jerseyEnvironment);
|
||||
when(session.getUpgradeRequest()).thenReturn(mock(UpgradeRequest.class));
|
||||
|
||||
WebSocketResourceProviderFactory factory = new WebSocketResourceProviderFactory(environment, Account.class);
|
||||
Object connection = factory.createWebSocket(request, response);
|
||||
WebSocketResourceProviderFactory<?> factory = new WebSocketResourceProviderFactory<>(environment, Account.class,
|
||||
mock(WebSocketConfiguration.class));
|
||||
Object connection = factory.createWebSocket(request, response);
|
||||
|
||||
assertNotNull(connection);
|
||||
verifyNoMoreInteractions(response);
|
||||
verify(authenticator).authenticate(eq(request));
|
||||
|
||||
((WebSocketResourceProvider)connection).onWebSocketConnect(session);
|
||||
((WebSocketResourceProvider<?>) connection).onWebSocketConnect(session);
|
||||
|
||||
assertNotNull(((WebSocketResourceProvider) connection).getContext().getAuthenticated());
|
||||
assertEquals(((WebSocketResourceProvider)connection).getContext().getAuthenticated(), account);
|
||||
assertNotNull(((WebSocketResourceProvider<?>) connection).getContext().getAuthenticated());
|
||||
assertEquals(((WebSocketResourceProvider<?>) connection).getContext().getAuthenticated(), account);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testErrorAuthorization() throws AuthenticationException, IOException {
|
||||
ResourceConfig jerseyEnvironment = new DropwizardResourceConfig();
|
||||
WebSocketEnvironment environment = mock(WebSocketEnvironment.class );
|
||||
WebSocketAuthenticator authenticator = mock(WebSocketAuthenticator.class );
|
||||
ServletUpgradeRequest request = mock(ServletUpgradeRequest.class );
|
||||
ServletUpgradeResponse response = mock(ServletUpgradeResponse.class );
|
||||
|
||||
void testErrorAuthorization() throws AuthenticationException, IOException {
|
||||
when(environment.getAuthenticator()).thenReturn(authenticator);
|
||||
when(authenticator.authenticate(eq(request))).thenThrow(new AuthenticationException("database failure"));
|
||||
when(environment.jersey()).thenReturn(jerseyEnvironment);
|
||||
|
||||
WebSocketResourceProviderFactory factory = new WebSocketResourceProviderFactory(environment, Account.class);
|
||||
Object connection = factory.createWebSocket(request, response);
|
||||
WebSocketResourceProviderFactory<Account> factory = new WebSocketResourceProviderFactory<>(environment,
|
||||
Account.class,
|
||||
mock(WebSocketConfiguration.class));
|
||||
Object connection = factory.createWebSocket(request, response);
|
||||
|
||||
assertNull(connection);
|
||||
verify(response).sendError(eq(500), eq("Failure"));
|
||||
@@ -101,14 +110,14 @@ public class WebSocketResourceProviderFactoryTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConfigure() {
|
||||
ResourceConfig jerseyEnvironment = new DropwizardResourceConfig();
|
||||
WebSocketEnvironment environment = mock(WebSocketEnvironment.class );
|
||||
WebSocketServletFactory servletFactory = mock(WebSocketServletFactory.class );
|
||||
void testConfigure() {
|
||||
WebSocketServletFactory servletFactory = mock(WebSocketServletFactory.class);
|
||||
when(environment.jersey()).thenReturn(jerseyEnvironment);
|
||||
when(servletFactory.getPolicy()).thenReturn(mock(WebSocketPolicy.class));
|
||||
|
||||
WebSocketResourceProviderFactory factory = new WebSocketResourceProviderFactory(environment, Account.class);
|
||||
WebSocketResourceProviderFactory<Account> factory = new WebSocketResourceProviderFactory<>(environment,
|
||||
Account.class,
|
||||
mock(WebSocketConfiguration.class));
|
||||
factory.configure(servletFactory);
|
||||
|
||||
verify(servletFactory).setCreator(eq(factory));
|
||||
|
||||
Reference in New Issue
Block a user