Replace MultiRecipientMessage parsing with libsignal's implementation

Co-authored-by: Jonathan Klabunde Tomer <jkt@signal.org>
This commit is contained in:
Jordan Rose
2023-12-08 08:52:47 -08:00
committed by GitHub
parent f20d3043d6
commit 2ab3c97ee8
8 changed files with 64 additions and 337 deletions

View File

@@ -8,8 +8,8 @@ package org.whispersystems.textsecuregcm.controllers;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.collection.IsEmptyCollection.empty;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.collection.IsEmptyCollection.empty;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
@@ -19,7 +19,6 @@ import static org.mockito.ArgumentMatchers.argThat;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.anyBoolean;
import static org.mockito.Mockito.anyString;
import static org.mockito.Mockito.atLeastOnce;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
@@ -55,7 +54,6 @@ import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.Callable;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;
@@ -78,7 +76,6 @@ import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.ArgumentsSources;
import org.junit.jupiter.params.provider.MethodSource;
import org.junit.jupiter.params.provider.ValueSource;
import org.junitpioneer.jupiter.cartesian.ArgumentSets;
@@ -981,9 +978,7 @@ class MessageControllerTest {
bb.order(ByteOrder.BIG_ENDIAN);
// first write the header
bb.put(explicitIdentifiers
? MultiRecipientMessageProvider.EXPLICIT_ID_VERSION_IDENTIFIER
: MultiRecipientMessageProvider.AMBIGUOUS_ID_VERSION_IDENTIFIER); // version byte
bb.put(explicitIdentifiers ? (byte) 0x23 : (byte) 0x22); // version byte
// count varint
int nRecip = recipients.size();
@@ -1258,7 +1253,7 @@ class MessageControllerTest {
.header(OptionalAccess.UNIDENTIFIED, Base64.getEncoder().encodeToString(UNIDENTIFIED_ACCESS_BYTES))
.put(Entity.entity(initializeMultiPayload(recipients, new byte[2048], useExplicitIdentifier), MultiRecipientMessageProvider.MEDIA_TYPE));
checkBadMultiRecipientResponse(response, 422);
checkBadMultiRecipientResponse(response, 400);
}
@Test

View File

@@ -1,41 +0,0 @@
/*
* Copyright 2021 Signal Messenger, LLC
* SPDX-License-Identifier: AGPL-3.0-only
*/
package org.whispersystems.textsecuregcm.providers;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.params.provider.Arguments.arguments;
import java.io.ByteArrayInputStream;
import java.util.stream.Stream;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
public class MultiRecipientMessageProviderTest {
static byte[] createTwoByteArray(int b1, int b2) {
return new byte[]{(byte) b1, (byte) b2};
}
static Stream<Arguments> readU16TestCases() {
return Stream.of(
arguments(0xFFFE, createTwoByteArray(0xFF, 0xFE)),
arguments(0x0001, createTwoByteArray(0x00, 0x01)),
arguments(0xBEEF, createTwoByteArray(0xBE, 0xEF)),
arguments(0xFFFF, createTwoByteArray(0xFF, 0xFF)),
arguments(0x0000, createTwoByteArray(0x00, 0x00)),
arguments(0xF080, createTwoByteArray(0xF0, 0x80))
);
}
@ParameterizedTest
@MethodSource("readU16TestCases")
void testReadU16(int expectedValue, byte[] input) throws Exception {
try (final ByteArrayInputStream stream = new ByteArrayInputStream(input)) {
assertThat(MultiRecipientMessageProvider.readU16(stream)).isEqualTo(expectedValue);
}
}
}