Search in sources :

Example 1 with OutgoingMessageEntity

use of org.whispersystems.textsecuregcm.entities.OutgoingMessageEntity in project Signal-Server by signalapp.

the class MessageControllerTest method testGetMessages.

@Test
void testGetMessages() {
    final long timestampOne = 313377;
    final long timestampTwo = 313388;
    final UUID messageGuidOne = UUID.randomUUID();
    final UUID sourceUuid = UUID.randomUUID();
    List<OutgoingMessageEntity> messages = new LinkedList<>() {

        {
            add(new OutgoingMessageEntity(messageGuidOne, Envelope.Type.CIPHERTEXT_VALUE, timestampOne, "+14152222222", sourceUuid, 2, AuthHelper.VALID_UUID, "hi there".getBytes(), 0));
            add(new OutgoingMessageEntity(null, Envelope.Type.SERVER_DELIVERY_RECEIPT_VALUE, timestampTwo, "+14152222222", sourceUuid, 2, AuthHelper.VALID_UUID, null, 0));
        }
    };
    OutgoingMessageEntityList messagesList = new OutgoingMessageEntityList(messages, false);
    when(messagesManager.getMessagesForDevice(eq(AuthHelper.VALID_UUID), eq(1L), anyString(), anyBoolean())).thenReturn(messagesList);
    OutgoingMessageEntityList response = resources.getJerseyTest().target("/v1/messages/").request().header("Authorization", AuthHelper.getAuthHeader(AuthHelper.VALID_UUID, AuthHelper.VALID_PASSWORD)).accept(MediaType.APPLICATION_JSON_TYPE).get(OutgoingMessageEntityList.class);
    assertEquals(response.getMessages().size(), 2);
    assertEquals(response.getMessages().get(0).getTimestamp(), timestampOne);
    assertEquals(response.getMessages().get(1).getTimestamp(), timestampTwo);
    assertEquals(response.getMessages().get(0).getGuid(), messageGuidOne);
    assertNull(response.getMessages().get(1).getGuid());
    assertEquals(response.getMessages().get(0).getSourceUuid(), sourceUuid);
    assertEquals(response.getMessages().get(1).getSourceUuid(), sourceUuid);
}
Also used : OutgoingMessageEntity(org.whispersystems.textsecuregcm.entities.OutgoingMessageEntity) OutgoingMessageEntityList(org.whispersystems.textsecuregcm.entities.OutgoingMessageEntityList) UUID(java.util.UUID) LinkedList(java.util.LinkedList) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 2 with OutgoingMessageEntity

use of org.whispersystems.textsecuregcm.entities.OutgoingMessageEntity in project Signal-Server by signalapp.

the class MessageControllerTest method testDeleteMessages.

@Test
void testDeleteMessages() throws Exception {
    long timestamp = System.currentTimeMillis();
    UUID sourceUuid = UUID.randomUUID();
    UUID uuid1 = UUID.randomUUID();
    when(messagesManager.delete(AuthHelper.VALID_UUID, 1, uuid1)).thenReturn(Optional.of(new OutgoingMessageEntity(uuid1, Envelope.Type.CIPHERTEXT_VALUE, timestamp, "+14152222222", sourceUuid, 1, AuthHelper.VALID_UUID, "hi".getBytes(), 0)));
    UUID uuid2 = UUID.randomUUID();
    when(messagesManager.delete(AuthHelper.VALID_UUID, 1, uuid2)).thenReturn(Optional.of(new OutgoingMessageEntity(uuid2, Envelope.Type.SERVER_DELIVERY_RECEIPT_VALUE, System.currentTimeMillis(), "+14152222222", sourceUuid, 1, AuthHelper.VALID_UUID, null, 0)));
    UUID uuid3 = UUID.randomUUID();
    when(messagesManager.delete(AuthHelper.VALID_UUID, 1, uuid3)).thenReturn(Optional.empty());
    Response response = resources.getJerseyTest().target(String.format("/v1/messages/uuid/%s", uuid1)).request().header("Authorization", AuthHelper.getAuthHeader(AuthHelper.VALID_UUID, AuthHelper.VALID_PASSWORD)).delete();
    assertThat("Good Response Code", response.getStatus(), is(equalTo(204)));
    verify(receiptSender).sendReceipt(any(AuthenticatedAccount.class), eq(sourceUuid), eq(timestamp));
    response = resources.getJerseyTest().target(String.format("/v1/messages/uuid/%s", uuid2)).request().header("Authorization", AuthHelper.getAuthHeader(AuthHelper.VALID_UUID, AuthHelper.VALID_PASSWORD)).delete();
    assertThat("Good Response Code", response.getStatus(), is(equalTo(204)));
    verifyNoMoreInteractions(receiptSender);
    response = resources.getJerseyTest().target(String.format("/v1/messages/uuid/%s", uuid3)).request().header("Authorization", AuthHelper.getAuthHeader(AuthHelper.VALID_UUID, AuthHelper.VALID_PASSWORD)).delete();
    assertThat("Good Response Code", response.getStatus(), is(equalTo(204)));
    verifyNoMoreInteractions(receiptSender);
}
Also used : Response(javax.ws.rs.core.Response) OutgoingMessageEntity(org.whispersystems.textsecuregcm.entities.OutgoingMessageEntity) DisabledPermittedAuthenticatedAccount(org.whispersystems.textsecuregcm.auth.DisabledPermittedAuthenticatedAccount) AuthenticatedAccount(org.whispersystems.textsecuregcm.auth.AuthenticatedAccount) UUID(java.util.UUID) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 3 with OutgoingMessageEntity

use of org.whispersystems.textsecuregcm.entities.OutgoingMessageEntity in project Signal-Server by signalapp.

the class MessageControllerTest method testGetMessagesBadAuth.

@Test
void testGetMessagesBadAuth() throws Exception {
    final long timestampOne = 313377;
    final long timestampTwo = 313388;
    List<OutgoingMessageEntity> messages = new LinkedList<OutgoingMessageEntity>() {

        {
            add(new OutgoingMessageEntity(UUID.randomUUID(), Envelope.Type.CIPHERTEXT_VALUE, timestampOne, "+14152222222", UUID.randomUUID(), 2, AuthHelper.VALID_UUID, "hi there".getBytes(), 0));
            add(new OutgoingMessageEntity(UUID.randomUUID(), Envelope.Type.SERVER_DELIVERY_RECEIPT_VALUE, timestampTwo, "+14152222222", UUID.randomUUID(), 2, AuthHelper.VALID_UUID, null, 0));
        }
    };
    OutgoingMessageEntityList messagesList = new OutgoingMessageEntityList(messages, false);
    when(messagesManager.getMessagesForDevice(eq(AuthHelper.VALID_UUID), eq(1L), anyString(), anyBoolean())).thenReturn(messagesList);
    Response response = resources.getJerseyTest().target("/v1/messages/").request().header("Authorization", AuthHelper.getAuthHeader(AuthHelper.VALID_UUID, AuthHelper.INVALID_PASSWORD)).accept(MediaType.APPLICATION_JSON_TYPE).get();
    assertThat("Unauthorized response", response.getStatus(), is(equalTo(401)));
}
Also used : Response(javax.ws.rs.core.Response) OutgoingMessageEntity(org.whispersystems.textsecuregcm.entities.OutgoingMessageEntity) OutgoingMessageEntityList(org.whispersystems.textsecuregcm.entities.OutgoingMessageEntityList) LinkedList(java.util.LinkedList) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 4 with OutgoingMessageEntity

use of org.whispersystems.textsecuregcm.entities.OutgoingMessageEntity in project Signal-Server by signalapp.

the class WebSocketConnectionTest method testOpen.

@Test
void testOpen() throws Exception {
    MessagesManager storedMessages = mock(MessagesManager.class);
    UUID accountUuid = UUID.randomUUID();
    UUID senderOneUuid = UUID.randomUUID();
    UUID senderTwoUuid = UUID.randomUUID();
    List<OutgoingMessageEntity> outgoingMessages = new LinkedList<OutgoingMessageEntity>() {

        {
            add(createMessage("sender1", senderOneUuid, UUID.randomUUID(), 1111, false, "first"));
            add(createMessage("sender1", senderOneUuid, UUID.randomUUID(), 2222, false, "second"));
            add(createMessage("sender2", senderTwoUuid, UUID.randomUUID(), 3333, false, "third"));
        }
    };
    OutgoingMessageEntityList outgoingMessagesList = new OutgoingMessageEntityList(outgoingMessages, false);
    when(device.getId()).thenReturn(2L);
    when(account.getNumber()).thenReturn("+14152222222");
    when(account.getUuid()).thenReturn(accountUuid);
    final Device sender1device = mock(Device.class);
    Set<Device> sender1devices = new HashSet<>() {

        {
            add(sender1device);
        }
    };
    Account sender1 = mock(Account.class);
    when(sender1.getDevices()).thenReturn(sender1devices);
    when(accountsManager.getByE164("sender1")).thenReturn(Optional.of(sender1));
    when(accountsManager.getByE164("sender2")).thenReturn(Optional.empty());
    String userAgent = "user-agent";
    when(storedMessages.getMessagesForDevice(account.getUuid(), device.getId(), userAgent, false)).thenReturn(outgoingMessagesList);
    final List<CompletableFuture<WebSocketResponseMessage>> futures = new LinkedList<>();
    final WebSocketClient client = mock(WebSocketClient.class);
    when(client.getUserAgent()).thenReturn(userAgent);
    when(client.sendRequest(eq("PUT"), eq("/api/v1/message"), ArgumentMatchers.nullable(List.class), ArgumentMatchers.<Optional<byte[]>>any())).thenAnswer(new Answer<CompletableFuture<WebSocketResponseMessage>>() {

        @Override
        public CompletableFuture<WebSocketResponseMessage> answer(InvocationOnMock invocationOnMock) {
            CompletableFuture<WebSocketResponseMessage> future = new CompletableFuture<>();
            futures.add(future);
            return future;
        }
    });
    WebSocketConnection connection = new WebSocketConnection(receiptSender, storedMessages, auth, device, client, retrySchedulingExecutor);
    connection.start();
    verify(client, times(3)).sendRequest(eq("PUT"), eq("/api/v1/message"), any(List.class), ArgumentMatchers.<Optional<byte[]>>any());
    assertEquals(3, futures.size());
    WebSocketResponseMessage response = mock(WebSocketResponseMessage.class);
    when(response.getStatus()).thenReturn(200);
    futures.get(1).complete(response);
    futures.get(0).completeExceptionally(new IOException());
    futures.get(2).completeExceptionally(new IOException());
    verify(storedMessages, times(1)).delete(eq(accountUuid), eq(2L), eq(outgoingMessages.get(1).getGuid()));
    verify(receiptSender, times(1)).sendReceipt(eq(auth), eq(senderOneUuid), eq(2222L));
    connection.stop();
    verify(client).close(anyInt(), anyString());
}
Also used : Account(org.whispersystems.textsecuregcm.storage.Account) AuthenticatedAccount(org.whispersystems.textsecuregcm.auth.AuthenticatedAccount) MessagesManager(org.whispersystems.textsecuregcm.storage.MessagesManager) OutgoingMessageEntityList(org.whispersystems.textsecuregcm.entities.OutgoingMessageEntityList) Device(org.whispersystems.textsecuregcm.storage.Device) WebSocketResponseMessage(org.whispersystems.websocket.messages.WebSocketResponseMessage) ByteString(com.google.protobuf.ByteString) Mockito.anyString(org.mockito.Mockito.anyString) IOException(java.io.IOException) WebSocketClient(org.whispersystems.websocket.WebSocketClient) LinkedList(java.util.LinkedList) CompletableFuture(java.util.concurrent.CompletableFuture) OutgoingMessageEntity(org.whispersystems.textsecuregcm.entities.OutgoingMessageEntity) InvocationOnMock(org.mockito.invocation.InvocationOnMock) OutgoingMessageEntityList(org.whispersystems.textsecuregcm.entities.OutgoingMessageEntityList) List(java.util.List) LinkedList(java.util.LinkedList) UUID(java.util.UUID) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 5 with OutgoingMessageEntity

use of org.whispersystems.textsecuregcm.entities.OutgoingMessageEntity in project Signal-Server by signalapp.

the class WebSocketConnectionTest method testRequeryOnStateMismatch.

@Test
public void testRequeryOnStateMismatch() {
    final MessagesManager messagesManager = mock(MessagesManager.class);
    final WebSocketClient client = mock(WebSocketClient.class);
    final WebSocketConnection connection = new WebSocketConnection(receiptSender, messagesManager, auth, device, client, retrySchedulingExecutor);
    final UUID accountUuid = UUID.randomUUID();
    when(account.getNumber()).thenReturn("+18005551234");
    when(account.getUuid()).thenReturn(accountUuid);
    when(device.getId()).thenReturn(1L);
    when(client.getUserAgent()).thenReturn("Test-UA");
    final List<OutgoingMessageEntity> firstPageMessages = List.of(createMessage("sender1", UUID.randomUUID(), UUID.randomUUID(), 1111, false, "first"), createMessage("sender1", UUID.randomUUID(), UUID.randomUUID(), 2222, false, "second"));
    final List<OutgoingMessageEntity> secondPageMessages = List.of(createMessage("sender1", UUID.randomUUID(), UUID.randomUUID(), 3333, false, "third"));
    final OutgoingMessageEntityList firstPage = new OutgoingMessageEntityList(firstPageMessages, false);
    final OutgoingMessageEntityList secondPage = new OutgoingMessageEntityList(secondPageMessages, false);
    when(messagesManager.getMessagesForDevice(eq(accountUuid), eq(1L), eq("Test-UA"), anyBoolean())).thenReturn(firstPage).thenReturn(secondPage).thenReturn(new OutgoingMessageEntityList(Collections.emptyList(), false));
    final WebSocketResponseMessage successResponse = mock(WebSocketResponseMessage.class);
    when(successResponse.getStatus()).thenReturn(200);
    final CountDownLatch sendLatch = new CountDownLatch(firstPageMessages.size() + secondPageMessages.size());
    when(client.sendRequest(eq("PUT"), eq("/api/v1/message"), any(List.class), any(Optional.class))).thenAnswer((Answer<CompletableFuture<WebSocketResponseMessage>>) invocation -> {
        connection.handleNewMessagesAvailable();
        sendLatch.countDown();
        return CompletableFuture.completedFuture(successResponse);
    });
    assertTimeoutPreemptively(Duration.ofSeconds(5), () -> {
        connection.processStoredMessages();
        sendLatch.await();
    });
    verify(client, times(firstPageMessages.size() + secondPageMessages.size())).sendRequest(eq("PUT"), eq("/api/v1/message"), any(List.class), any(Optional.class));
    verify(client).sendRequest(eq("PUT"), eq("/api/v1/queue/empty"), any(List.class), eq(Optional.empty()));
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) ScheduledFuture(java.util.concurrent.ScheduledFuture) ArgumentMatchers(org.mockito.ArgumentMatchers) BasicCredentials(io.dropwizard.auth.basic.BasicCredentials) ArgumentMatchers.argThat(org.mockito.ArgumentMatchers.argThat) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) WebSocketResponseMessage(org.whispersystems.websocket.messages.WebSocketResponseMessage) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) OutgoingMessageEntity(org.whispersystems.textsecuregcm.entities.OutgoingMessageEntity) Pair(org.whispersystems.textsecuregcm.util.Pair) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) ClientPresenceManager(org.whispersystems.textsecuregcm.push.ClientPresenceManager) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) Envelope(org.whispersystems.textsecuregcm.entities.MessageProtos.Envelope) WebSocketClient(org.whispersystems.websocket.WebSocketClient) RedisException(io.lettuce.core.RedisException) AccountAuthenticator(org.whispersystems.textsecuregcm.auth.AccountAuthenticator) Device(org.whispersystems.textsecuregcm.storage.Device) OutgoingMessageEntityList(org.whispersystems.textsecuregcm.entities.OutgoingMessageEntityList) Set(java.util.Set) UUID(java.util.UUID) MessagesManager(org.whispersystems.textsecuregcm.storage.MessagesManager) ByteString(com.google.protobuf.ByteString) Test(org.junit.jupiter.api.Test) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) AuthenticationResult(org.whispersystems.websocket.auth.WebSocketAuthenticator.AuthenticationResult) Optional(java.util.Optional) RandomStringUtils(org.apache.commons.lang3.RandomStringUtils) AccountsManager(org.whispersystems.textsecuregcm.storage.AccountsManager) Mockito.any(org.mockito.Mockito.any) Mockito.mock(org.mockito.Mockito.mock) Account(org.whispersystems.textsecuregcm.storage.Account) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) ArgumentMatchers.anyBoolean(org.mockito.ArgumentMatchers.anyBoolean) HashSet(java.util.HashSet) Answer(org.mockito.stubbing.Answer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ReceiptSender(org.whispersystems.textsecuregcm.push.ReceiptSender) WebSocketSessionContext(org.whispersystems.websocket.session.WebSocketSessionContext) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Assertions.assertTimeoutPreemptively(org.junit.jupiter.api.Assertions.assertTimeoutPreemptively) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Mockito.anyLong(org.mockito.Mockito.anyLong) LinkedList(java.util.LinkedList) Mockito.anyString(org.mockito.Mockito.anyString) ApnFallbackManager(org.whispersystems.textsecuregcm.push.ApnFallbackManager) AuthenticatedAccount(org.whispersystems.textsecuregcm.auth.AuthenticatedAccount) Mockito.times(org.mockito.Mockito.times) IOException(java.io.IOException) Mockito.when(org.mockito.Mockito.when) MessageSender(org.whispersystems.textsecuregcm.push.MessageSender) Mockito.verify(org.mockito.Mockito.verify) Mockito.never(org.mockito.Mockito.never) UpgradeRequest(org.eclipse.jetty.websocket.api.UpgradeRequest) Mockito.anyInt(org.mockito.Mockito.anyInt) Collections(java.util.Collections) MessagesManager(org.whispersystems.textsecuregcm.storage.MessagesManager) Optional(java.util.Optional) OutgoingMessageEntityList(org.whispersystems.textsecuregcm.entities.OutgoingMessageEntityList) WebSocketResponseMessage(org.whispersystems.websocket.messages.WebSocketResponseMessage) WebSocketClient(org.whispersystems.websocket.WebSocketClient) CountDownLatch(java.util.concurrent.CountDownLatch) CompletableFuture(java.util.concurrent.CompletableFuture) OutgoingMessageEntity(org.whispersystems.textsecuregcm.entities.OutgoingMessageEntity) OutgoingMessageEntityList(org.whispersystems.textsecuregcm.entities.OutgoingMessageEntityList) List(java.util.List) LinkedList(java.util.LinkedList) UUID(java.util.UUID) Test(org.junit.jupiter.api.Test)

Aggregations

OutgoingMessageEntity (org.whispersystems.textsecuregcm.entities.OutgoingMessageEntity)18 UUID (java.util.UUID)13 Test (org.junit.jupiter.api.Test)11 OutgoingMessageEntityList (org.whispersystems.textsecuregcm.entities.OutgoingMessageEntityList)10 LinkedList (java.util.LinkedList)9 CompletableFuture (java.util.concurrent.CompletableFuture)8 AuthenticatedAccount (org.whispersystems.textsecuregcm.auth.AuthenticatedAccount)8 ByteString (com.google.protobuf.ByteString)7 HashSet (java.util.HashSet)7 List (java.util.List)7 Mockito.anyString (org.mockito.Mockito.anyString)7 InvocationOnMock (org.mockito.invocation.InvocationOnMock)7 Account (org.whispersystems.textsecuregcm.storage.Account)7 Device (org.whispersystems.textsecuregcm.storage.Device)7 MessagesManager (org.whispersystems.textsecuregcm.storage.MessagesManager)7 WebSocketClient (org.whispersystems.websocket.WebSocketClient)7 WebSocketResponseMessage (org.whispersystems.websocket.messages.WebSocketResponseMessage)7 IOException (java.io.IOException)5 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)5 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)3