mirror of
https://github.com/signalapp/Signal-Server.git
synced 2026-12-02 11:46:53 +00:00
Mirror message writes/deletions to FoundationDB
This commit is contained in:
1 parent
691a4162e1
commit
3cfe8b9f6c
29 files changed
+941
-117
No files matched your search
-40
@@ -1,40 +0,0 @@
|
||||
/*
|
||||
* Copyright 2026 Signal Messenger, LLC
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
|
||||
package org.whispersystems.textsecuregcm.configuration;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import java.util.List;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.Arguments;
|
||||
import org.junit.jupiter.params.provider.MethodSource;
|
||||
|
||||
class FoundationDbClusterConfigurationTest {
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource
|
||||
void isSingleClusterFileSourceSpecified(final FoundationDbClusterConfiguration clusterConfiguration,
|
||||
final boolean expectSingleClusterFileSourceSpecified) {
|
||||
|
||||
assertEquals(expectSingleClusterFileSourceSpecified, clusterConfiguration.isSingleClusterFileSourceSpecified());
|
||||
}
|
||||
|
||||
private static List<Arguments> isSingleClusterFileSourceSpecified() {
|
||||
return List.of(
|
||||
Arguments.argumentSet("Cluster file URL only",
|
||||
new FoundationDbClusterConfiguration("test-url", null), true),
|
||||
|
||||
Arguments.argumentSet("Cluster file contents only",
|
||||
new FoundationDbClusterConfiguration(null, "test-contents"), true),
|
||||
|
||||
Arguments.argumentSet("Both",
|
||||
new FoundationDbClusterConfiguration("test-url", "test-contents"), false),
|
||||
|
||||
Arguments.argumentSet("Neither",
|
||||
new FoundationDbClusterConfiguration(null, null), false)
|
||||
);
|
||||
}
|
||||
}
|
||||
+53
-12
@@ -6,6 +6,8 @@
|
||||
package org.whispersystems.textsecuregcm.configuration;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.whispersystems.textsecuregcm.configuration.secrets.SecretBytes;
|
||||
import org.whispersystems.textsecuregcm.util.TestRandomUtil;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -17,45 +19,84 @@ class FoundationDbMessagesConfigurationTest {
|
||||
@Test
|
||||
void isEveryEpochClusterConfigured() {
|
||||
assertTrue(new FoundationDbMessagesConfiguration(
|
||||
Map.of("messages-0", new FoundationDbClusterConfiguration("test-url", null)),
|
||||
Map.of("messages-0", new FoundationDbClusterConfiguration("test-url")),
|
||||
Map.of(0, List.of("messages-0")),
|
||||
0
|
||||
0,
|
||||
Map.of(0, new SecretBytes(TestRandomUtil.nextBytes(16))),
|
||||
0,
|
||||
FoundationDbMessagesConfiguration.DEFAULT_MAX_WATCHES_PER_CLIENT
|
||||
).isEveryEpochClusterConfigured());
|
||||
|
||||
assertFalse(new FoundationDbMessagesConfiguration(
|
||||
Map.of("messages-0", new FoundationDbClusterConfiguration("test-url", null)),
|
||||
Map.of("messages-0", new FoundationDbClusterConfiguration("test-url")),
|
||||
Map.of(0, List.of("messages-0", "unconfigured-cluster")),
|
||||
0
|
||||
0,
|
||||
Map.of(0, new SecretBytes(TestRandomUtil.nextBytes(16))),
|
||||
0,
|
||||
FoundationDbMessagesConfiguration.DEFAULT_MAX_WATCHES_PER_CLIENT
|
||||
).isEveryEpochClusterConfigured());
|
||||
}
|
||||
|
||||
@Test
|
||||
void isEveryEpochFreeOfDuplicates() {
|
||||
assertTrue(new FoundationDbMessagesConfiguration(
|
||||
Map.of("messages-0", new FoundationDbClusterConfiguration("test-url", null)),
|
||||
Map.of("messages-0", new FoundationDbClusterConfiguration("test-url")),
|
||||
Map.of(0, List.of("messages-0")),
|
||||
0
|
||||
0,
|
||||
Map.of(0, new SecretBytes(TestRandomUtil.nextBytes(16))),
|
||||
0,
|
||||
FoundationDbMessagesConfiguration.DEFAULT_MAX_WATCHES_PER_CLIENT
|
||||
).isEveryEpochFreeOfDuplicates());
|
||||
|
||||
assertFalse(new FoundationDbMessagesConfiguration(
|
||||
Map.of("messages-0", new FoundationDbClusterConfiguration("test-url", null)),
|
||||
Map.of("messages-0", new FoundationDbClusterConfiguration("test-url")),
|
||||
Map.of(0, List.of("messages-0", "messages-0")),
|
||||
0
|
||||
0,
|
||||
Map.of(0, new SecretBytes(TestRandomUtil.nextBytes(16))),
|
||||
0,
|
||||
FoundationDbMessagesConfiguration.DEFAULT_MAX_WATCHES_PER_CLIENT
|
||||
).isEveryEpochFreeOfDuplicates());
|
||||
}
|
||||
|
||||
@Test
|
||||
void isActiveEpochConfigured() {
|
||||
assertTrue(new FoundationDbMessagesConfiguration(
|
||||
Map.of("messages-0", new FoundationDbClusterConfiguration("test-url", null)),
|
||||
Map.of("messages-0", new FoundationDbClusterConfiguration("test-url")),
|
||||
Map.of(0, List.of("messages-0")),
|
||||
0
|
||||
0,
|
||||
Map.of(0, new SecretBytes(TestRandomUtil.nextBytes(16))),
|
||||
0,
|
||||
FoundationDbMessagesConfiguration.DEFAULT_MAX_WATCHES_PER_CLIENT
|
||||
).isActiveEpochConfigured());
|
||||
|
||||
assertFalse(new FoundationDbMessagesConfiguration(
|
||||
Map.of("messages-0", new FoundationDbClusterConfiguration("test-url", null)),
|
||||
Map.of("messages-0", new FoundationDbClusterConfiguration("test-url")),
|
||||
Map.of(0, List.of("messages-0")),
|
||||
1
|
||||
1,
|
||||
Map.of(0, new SecretBytes(TestRandomUtil.nextBytes(16))),
|
||||
0,
|
||||
FoundationDbMessagesConfiguration.DEFAULT_MAX_WATCHES_PER_CLIENT
|
||||
).isActiveEpochConfigured());
|
||||
}
|
||||
|
||||
@Test
|
||||
void isCurrentVersionstampCipherKeyConfigured() {
|
||||
assertTrue(new FoundationDbMessagesConfiguration(
|
||||
Map.of("messages-0", new FoundationDbClusterConfiguration("test-url")),
|
||||
Map.of(0, List.of("messages-0")),
|
||||
0,
|
||||
Map.of(0, new SecretBytes(TestRandomUtil.nextBytes(16))),
|
||||
0,
|
||||
FoundationDbMessagesConfiguration.DEFAULT_MAX_WATCHES_PER_CLIENT
|
||||
).isCurrentVersionstampCipherKeyConfigured());
|
||||
|
||||
assertFalse(new FoundationDbMessagesConfiguration(
|
||||
Map.of("messages-0", new FoundationDbClusterConfiguration("test-url")),
|
||||
Map.of(0, List.of("messages-0")),
|
||||
0,
|
||||
Map.of(0, new SecretBytes(TestRandomUtil.nextBytes(16))),
|
||||
1,
|
||||
FoundationDbMessagesConfiguration.DEFAULT_MAX_WATCHES_PER_CLIENT
|
||||
).isCurrentVersionstampCipherKeyConfigured());
|
||||
}
|
||||
}
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
* Copyright 2026 Signal Messenger, LLC
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
|
||||
package org.whispersystems.textsecuregcm.configuration;
|
||||
|
||||
import com.apple.foundationdb.Database;
|
||||
import com.apple.foundationdb.FDB;
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
import com.fasterxml.jackson.annotation.JsonTypeName;
|
||||
import java.io.IOException;
|
||||
import org.whispersystems.textsecuregcm.storage.TestcontainersFoundationDbDatabaseLifecycleManager;
|
||||
|
||||
@JsonTypeName("local")
|
||||
public class LocalFoundationDbDatabaseFactory implements FoundationDbDatabaseFactory {
|
||||
|
||||
@JsonIgnore
|
||||
private final TestcontainersFoundationDbDatabaseLifecycleManager testcontainersFoundationDbDatabaseLifecycleManager;
|
||||
|
||||
@JsonIgnore
|
||||
private Database database;
|
||||
|
||||
private LocalFoundationDbDatabaseFactory() {
|
||||
this.testcontainersFoundationDbDatabaseLifecycleManager = new TestcontainersFoundationDbDatabaseLifecycleManager();
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized Database build(final FDB fdb) throws IOException {
|
||||
if (database == null) {
|
||||
Runtime.getRuntime().addShutdownHook(new Thread(testcontainersFoundationDbDatabaseLifecycleManager::closeDatabase));
|
||||
testcontainersFoundationDbDatabaseLifecycleManager.initializeDatabase(fdb);
|
||||
database = testcontainersFoundationDbDatabaseLifecycleManager.getDatabase();
|
||||
}
|
||||
|
||||
return database;
|
||||
}
|
||||
}
|
||||
+3
-3
@@ -38,7 +38,6 @@ import org.junit.jupiter.api.Timeout;
|
||||
import org.junit.jupiter.api.extension.RegisterExtension;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.CsvSource;
|
||||
import org.signal.chat.messages.GetMessagesRequest;
|
||||
import org.signal.chat.messages.GetMessagesResponse;
|
||||
import org.whispersystems.textsecuregcm.auth.DisconnectionRequestManager;
|
||||
import org.whispersystems.textsecuregcm.configuration.dynamic.DynamicConfiguration;
|
||||
@@ -63,6 +62,7 @@ import org.whispersystems.textsecuregcm.storage.MessagesCache;
|
||||
import org.whispersystems.textsecuregcm.storage.MessagesDynamoDb;
|
||||
import org.whispersystems.textsecuregcm.storage.MessagesManager;
|
||||
import org.whispersystems.textsecuregcm.storage.ReportMessageManager;
|
||||
import org.whispersystems.textsecuregcm.storage.foundationdb.FoundationDbMessageStore;
|
||||
import org.whispersystems.textsecuregcm.util.UUIDUtil;
|
||||
import reactor.core.publisher.Mono;
|
||||
import reactor.core.publisher.Sinks;
|
||||
@@ -127,8 +127,8 @@ class MessageDispatcherIntegrationTest {
|
||||
|
||||
messageDispatcher = new MessageDispatcher(
|
||||
mock(ReceiptSender.class),
|
||||
new MessagesManager(messagesDynamoDb, messagesCache, redisMessageAvailabilityManager, reportMessageManager,
|
||||
sharedExecutorService, Clock.systemUTC()),
|
||||
new MessagesManager(messagesDynamoDb, messagesCache, mock(FoundationDbMessageStore.class), redisMessageAvailabilityManager, reportMessageManager,
|
||||
sharedExecutorService, Clock.systemUTC(), mock(ExperimentEnrollmentManager.class)),
|
||||
new MessageMetrics(),
|
||||
mock(PushNotificationManager.class),
|
||||
mock(PushNotificationScheduler.class),
|
||||
|
||||
+69
@@ -0,0 +1,69 @@
|
||||
/*
|
||||
* Copyright 2026 Signal Messenger, LLC
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
|
||||
package org.whispersystems.textsecuregcm.storage;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.Arguments;
|
||||
import org.junit.jupiter.params.provider.MethodSource;
|
||||
import org.whispersystems.textsecuregcm.experiment.ExperimentEnrollmentManager;
|
||||
import org.whispersystems.textsecuregcm.identity.AciServiceIdentifier;
|
||||
import org.whispersystems.textsecuregcm.storage.foundationdb.FoundationDbMessageStore;
|
||||
|
||||
class DeletionMirroringRedisDynamoDbMessageStreamTest {
|
||||
|
||||
private FoundationDbMessageStore foundationDbMessageStore;
|
||||
private ExperimentEnrollmentManager experimentEnrollmentManager;
|
||||
|
||||
private DeletionMirroringRedisDynamoDbMessageStream deletionMirroringRedisDynamoDbMessageStream;
|
||||
|
||||
private static final AciServiceIdentifier ACCOUNT_IDENTIFIER = new AciServiceIdentifier(UUID.randomUUID());
|
||||
private static final byte DEVICE_ID = Device.PRIMARY_ID;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
foundationDbMessageStore = mock(FoundationDbMessageStore.class);
|
||||
experimentEnrollmentManager = mock(ExperimentEnrollmentManager.class);
|
||||
|
||||
deletionMirroringRedisDynamoDbMessageStream = new DeletionMirroringRedisDynamoDbMessageStream(
|
||||
mock(RedisDynamoDbMessageStream.class),
|
||||
foundationDbMessageStore,
|
||||
experimentEnrollmentManager,
|
||||
Runnable::run,
|
||||
ACCOUNT_IDENTIFIER.uuid(),
|
||||
DEVICE_ID);
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource
|
||||
void acknowledgeMessage(final boolean enrolled, final UUID messageGuid, final boolean expectFoundationDbDeletion) {
|
||||
when(experimentEnrollmentManager.isEnrolled(any(UUID.class), eq(MessagesManager.MIRROR_DELETIONS_EXPERIMENT_NAME)))
|
||||
.thenReturn(enrolled);
|
||||
|
||||
deletionMirroringRedisDynamoDbMessageStream.acknowledgeMessage(messageGuid, System.currentTimeMillis());
|
||||
|
||||
verify(foundationDbMessageStore, times(expectFoundationDbDeletion ? 1 : 0))
|
||||
.delete(ACCOUNT_IDENTIFIER, DEVICE_ID, messageGuid);
|
||||
}
|
||||
|
||||
private static List<Arguments> acknowledgeMessage() {
|
||||
return List.of(
|
||||
Arguments.argumentSet("Not enrolled, v4 UUID", false, UUID.randomUUID(), false),
|
||||
Arguments.argumentSet("Not enrolled, v8 UUID", false, MessageGuidUtil.generateRandomV8UUID(), false),
|
||||
Arguments.argumentSet("Enrolled, v4 UUID", true, UUID.randomUUID(), false),
|
||||
Arguments.argumentSet("Enrolled, v8 UUID", true, MessageGuidUtil.generateRandomV8UUID(), true)
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
/*
|
||||
* Copyright 2026 Signal Messenger, LLC
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
|
||||
package org.whispersystems.textsecuregcm.storage;
|
||||
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
|
||||
public class MessageGuidUtil {
|
||||
private static final long VERSION_MASK = 0x0000_0000_0000_f000L;
|
||||
private static final long VERSION_8_BITS = 0x0000_0000_0000_8000L;
|
||||
|
||||
public static UUID generateRandomV8UUID() {
|
||||
long mostSignificantBits = ThreadLocalRandom.current().nextLong();
|
||||
|
||||
// Clear any bits in the version field
|
||||
mostSignificantBits &= ~VERSION_MASK;
|
||||
|
||||
// Set the version to 8
|
||||
mostSignificantBits |= VERSION_8_BITS;
|
||||
|
||||
return new UUID(mostSignificantBits, ThreadLocalRandom.current().nextLong());
|
||||
}
|
||||
}
|
||||
+5
-1
@@ -36,11 +36,13 @@ import org.junit.jupiter.api.extension.RegisterExtension;
|
||||
import org.whispersystems.textsecuregcm.configuration.dynamic.DynamicConfiguration;
|
||||
import org.whispersystems.textsecuregcm.configuration.dynamic.DynamicMessagePersisterConfiguration;
|
||||
import org.whispersystems.textsecuregcm.entities.MessageProtos;
|
||||
import org.whispersystems.textsecuregcm.experiment.ExperimentEnrollmentManager;
|
||||
import org.whispersystems.textsecuregcm.identity.AciServiceIdentifier;
|
||||
import org.whispersystems.textsecuregcm.push.MessageAvailabilityListener;
|
||||
import org.whispersystems.textsecuregcm.push.RedisMessageAvailabilityManager;
|
||||
import org.whispersystems.textsecuregcm.redis.RedisClusterExtension;
|
||||
import org.whispersystems.textsecuregcm.storage.DynamoDbExtensionSchema.Tables;
|
||||
import org.whispersystems.textsecuregcm.storage.foundationdb.FoundationDbMessageStore;
|
||||
import org.whispersystems.textsecuregcm.tests.util.DevicesHelper;
|
||||
import org.whispersystems.textsecuregcm.util.UUIDUtil;
|
||||
import reactor.core.scheduler.Scheduler;
|
||||
@@ -95,10 +97,12 @@ class MessagePersisterIntegrationTest {
|
||||
|
||||
final MessagesManager messagesManager = new MessagesManager(messagesDynamoDb,
|
||||
messagesCache,
|
||||
mock(FoundationDbMessageStore.class),
|
||||
mock(RedisMessageAvailabilityManager.class),
|
||||
mock(ReportMessageManager.class),
|
||||
messageDeletionExecutorService,
|
||||
Clock.systemUTC());
|
||||
Clock.systemUTC(),
|
||||
mock(ExperimentEnrollmentManager.class));
|
||||
|
||||
websocketConnectionEventExecutor = Executors.newVirtualThreadPerTaskExecutor();
|
||||
asyncOperationQueueingExecutor = Executors.newSingleThreadExecutor();
|
||||
|
||||
+144
-13
@@ -8,14 +8,17 @@ package org.whispersystems.textsecuregcm.storage;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyByte;
|
||||
import static org.mockito.ArgumentMatchers.argThat;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.verifyNoInteractions;
|
||||
import static org.mockito.Mockito.verifyNoMoreInteractions;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import com.apple.foundationdb.tuple.Versionstamp;
|
||||
import com.google.protobuf.ByteString;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.time.Instant;
|
||||
@@ -28,19 +31,23 @@ import java.util.UUID;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
import java.util.stream.Collectors;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.CsvSource;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
import org.signal.libsignal.protocol.InvalidMessageException;
|
||||
import org.signal.libsignal.protocol.InvalidVersionException;
|
||||
import org.signal.libsignal.protocol.SealedSenderMultiRecipientMessage;
|
||||
import org.signal.libsignal.protocol.ServiceId;
|
||||
import org.whispersystems.textsecuregcm.entities.MessageProtos.Envelope;
|
||||
import org.whispersystems.textsecuregcm.experiment.ExperimentEnrollmentManager;
|
||||
import org.whispersystems.textsecuregcm.identity.AciServiceIdentifier;
|
||||
import org.whispersystems.textsecuregcm.identity.IdentityType;
|
||||
import org.whispersystems.textsecuregcm.identity.PniServiceIdentifier;
|
||||
import org.whispersystems.textsecuregcm.identity.ServiceIdentifier;
|
||||
import org.whispersystems.textsecuregcm.push.RedisMessageAvailabilityManager;
|
||||
import org.whispersystems.textsecuregcm.storage.foundationdb.FoundationDbMessageStore;
|
||||
import org.whispersystems.textsecuregcm.tests.util.MultiRecipientMessageHelper;
|
||||
import org.whispersystems.textsecuregcm.tests.util.TestRecipient;
|
||||
import org.whispersystems.textsecuregcm.util.TestClock;
|
||||
@@ -48,27 +55,55 @@ import reactor.core.publisher.Mono;
|
||||
|
||||
class MessagesManagerTest {
|
||||
|
||||
private final MessagesDynamoDb messagesDynamoDb = mock(MessagesDynamoDb.class);
|
||||
private final MessagesCache messagesCache = mock(MessagesCache.class);
|
||||
private final ReportMessageManager reportMessageManager = mock(ReportMessageManager.class);
|
||||
private MessagesDynamoDb messagesDynamoDb;
|
||||
private MessagesCache messagesCache;
|
||||
private FoundationDbMessageStore foundationDbMessageStore;
|
||||
private ReportMessageManager reportMessageManager;
|
||||
private ExperimentEnrollmentManager experimentEnrollmentManager;
|
||||
|
||||
private static final TestClock CLOCK = TestClock.pinned(Instant.now());
|
||||
|
||||
private final MessagesManager messagesManager = new MessagesManager(messagesDynamoDb, messagesCache,
|
||||
mock(RedisMessageAvailabilityManager.class), reportMessageManager, Executors.newSingleThreadExecutor(), CLOCK);
|
||||
private MessagesManager messagesManager;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
messagesDynamoDb = mock(MessagesDynamoDb.class);
|
||||
messagesCache = mock(MessagesCache.class);
|
||||
foundationDbMessageStore = mock(FoundationDbMessageStore.class);
|
||||
reportMessageManager = mock(ReportMessageManager.class);
|
||||
experimentEnrollmentManager = mock(ExperimentEnrollmentManager.class);
|
||||
|
||||
when(messagesCache.insert(any(), any(), anyByte(), any())).thenReturn(CompletableFuture.completedFuture(true));
|
||||
|
||||
messagesManager = new MessagesManager(messagesDynamoDb, messagesCache, foundationDbMessageStore,
|
||||
mock(RedisMessageAvailabilityManager.class), reportMessageManager, Executors.newSingleThreadExecutor(), CLOCK,
|
||||
experimentEnrollmentManager);
|
||||
}
|
||||
|
||||
@Test
|
||||
void insert() {
|
||||
@ParameterizedTest
|
||||
@ValueSource(booleans = {true, false})
|
||||
void insert(final boolean mirrorInsert) {
|
||||
final UUID sourceAci = UUID.randomUUID();
|
||||
final Envelope message = Envelope.newBuilder()
|
||||
.setSourceServiceId(new AciServiceIdentifier(sourceAci).toCompactByteString())
|
||||
.build();
|
||||
|
||||
if (mirrorInsert) {
|
||||
when(experimentEnrollmentManager.isEnrolled(any(UUID.class), eq(MessagesManager.MIRROR_INSERTS_EXPERIMENT_NAME)))
|
||||
.thenReturn(true);
|
||||
|
||||
when(foundationDbMessageStore.insert(any(), any()))
|
||||
.thenAnswer(invocation -> {
|
||||
final Map<Byte, Envelope> messagesByDeviceId = invocation.getArgument(1);
|
||||
|
||||
return CompletableFuture.completedFuture(messagesByDeviceId.keySet().stream()
|
||||
.collect(Collectors.toMap(deviceId -> deviceId,
|
||||
_ -> new FoundationDbMessageStore.InsertResult(Optional.of(Versionstamp.fromBytes(new byte[12])),
|
||||
Optional.of(MessageGuidUtil.generateRandomV8UUID()),
|
||||
true))));
|
||||
});
|
||||
}
|
||||
|
||||
final UUID destinationUuid = UUID.randomUUID();
|
||||
|
||||
messagesManager.insert(destinationUuid, Map.of(Device.PRIMARY_ID, message));
|
||||
@@ -82,14 +117,78 @@ class MessagesManagerTest {
|
||||
messagesManager.insert(destinationUuid, Map.of(Device.PRIMARY_ID, syncMessage));
|
||||
|
||||
verifyNoMoreInteractions(reportMessageManager);
|
||||
|
||||
verify(foundationDbMessageStore, times(mirrorInsert ? 2 : 0)).insert(any(), any());
|
||||
verify(messagesCache, times(2))
|
||||
.insert(argThat(messageGuid -> messageGuid.version() == (mirrorInsert ? 8 : 4)),
|
||||
eq(destinationUuid),
|
||||
eq(Device.PRIMARY_ID),
|
||||
any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void insertMultiRecipientMessage() throws InvalidMessageException, InvalidVersionException {
|
||||
final ServiceIdentifier singleDeviceAccountAciServiceIdentifier = new AciServiceIdentifier(UUID.randomUUID());
|
||||
final ServiceIdentifier singleDeviceAccountPniServiceIdentifier = new PniServiceIdentifier(UUID.randomUUID());
|
||||
final ServiceIdentifier multiDeviceAccountAciServiceIdentifier = new AciServiceIdentifier(UUID.randomUUID());
|
||||
final ServiceIdentifier unresolvedAccountAciServiceIdentifier = new AciServiceIdentifier(UUID.randomUUID());
|
||||
void insertFoundationDbException() {
|
||||
final UUID sourceAci = UUID.randomUUID();
|
||||
final Envelope message = Envelope.newBuilder()
|
||||
.setSourceServiceId(new AciServiceIdentifier(sourceAci).toCompactByteString())
|
||||
.build();
|
||||
|
||||
when(experimentEnrollmentManager.isEnrolled(any(UUID.class), eq(MessagesManager.MIRROR_INSERTS_EXPERIMENT_NAME)))
|
||||
.thenReturn(true);
|
||||
|
||||
when(foundationDbMessageStore.insert(any(), any()))
|
||||
.thenReturn(CompletableFuture.failedFuture(new RuntimeException()));
|
||||
|
||||
final UUID destinationUuid = UUID.randomUUID();
|
||||
|
||||
messagesManager.insert(destinationUuid, Map.of(Device.PRIMARY_ID, message));
|
||||
|
||||
verify(foundationDbMessageStore).insert(any(), any());
|
||||
verify(messagesCache)
|
||||
.insert(argThat(messageGuid -> messageGuid.version() == 4),
|
||||
eq(destinationUuid),
|
||||
eq(Device.PRIMARY_ID),
|
||||
any());
|
||||
}
|
||||
|
||||
private static UUID generateRandomV8UUID() {
|
||||
final long versionMask = 0x0000_0000_0000_f000L;
|
||||
final long v8Version = 0x0000_0000_0000_8000L;
|
||||
|
||||
long mostSignificantBits = ThreadLocalRandom.current().nextLong();
|
||||
|
||||
// Clear any bits in the version field
|
||||
mostSignificantBits &= ~versionMask;
|
||||
|
||||
// Set the version to 8
|
||||
mostSignificantBits |= v8Version;
|
||||
|
||||
return new UUID(mostSignificantBits, ThreadLocalRandom.current().nextLong());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(booleans = {true, false})
|
||||
void insertMultiRecipientMessage(final boolean mirrorInsert) throws InvalidMessageException, InvalidVersionException {
|
||||
if (mirrorInsert) {
|
||||
when(experimentEnrollmentManager.isEnrolled(any(UUID.class), eq(MessagesManager.MIRROR_INSERTS_EXPERIMENT_NAME)))
|
||||
.thenReturn(true);
|
||||
|
||||
when(foundationDbMessageStore.insert(any(), any()))
|
||||
.thenAnswer(invocation -> {
|
||||
final Map<Byte, Envelope> messagesByDeviceId = invocation.getArgument(1);
|
||||
|
||||
return CompletableFuture.completedFuture(messagesByDeviceId.keySet().stream()
|
||||
.collect(Collectors.toMap(deviceId -> deviceId,
|
||||
_ -> new FoundationDbMessageStore.InsertResult(Optional.of(Versionstamp.fromBytes(new byte[12])),
|
||||
Optional.of(generateRandomV8UUID()),
|
||||
true))));
|
||||
});
|
||||
}
|
||||
|
||||
final AciServiceIdentifier singleDeviceAccountAciServiceIdentifier = new AciServiceIdentifier(UUID.randomUUID());
|
||||
final PniServiceIdentifier singleDeviceAccountPniServiceIdentifier = new PniServiceIdentifier(UUID.randomUUID());
|
||||
final AciServiceIdentifier multiDeviceAccountAciServiceIdentifier = new AciServiceIdentifier(UUID.randomUUID());
|
||||
final AciServiceIdentifier unresolvedAccountAciServiceIdentifier = new AciServiceIdentifier(UUID.randomUUID());
|
||||
|
||||
final Account singleDeviceAccount = mock(Account.class);
|
||||
final Account multiDeviceAccount = mock(Account.class);
|
||||
@@ -186,6 +285,38 @@ class MessagesManagerTest {
|
||||
eq(unresolvedAccountAciServiceIdentifier.uuid()),
|
||||
anyByte(),
|
||||
any());
|
||||
|
||||
if (mirrorInsert) {
|
||||
final Envelope prototypeExpectedFoundationDbMessage = prototypeExpectedMessage.toBuilder().clearSharedMrmKey().build();
|
||||
|
||||
verify(foundationDbMessageStore).insert(singleDeviceAccountAciServiceIdentifier,
|
||||
Map.of(Device.PRIMARY_ID, prototypeExpectedFoundationDbMessage.toBuilder()
|
||||
.setDestinationServiceId(singleDeviceAccountAciServiceIdentifier.toCompactByteString())
|
||||
.setContent(ByteString.copyFrom(multiRecipientMessage.messageForRecipient(multiRecipientMessage.getRecipients().get(new ServiceId.Aci(singleDeviceAccountAciServiceIdentifier.uuid())))))
|
||||
.build()));
|
||||
|
||||
verify(foundationDbMessageStore).insert(singleDeviceAccountAciServiceIdentifier,
|
||||
Map.of(Device.PRIMARY_ID, prototypeExpectedFoundationDbMessage.toBuilder()
|
||||
.setDestinationServiceId(singleDeviceAccountPniServiceIdentifier.toCompactByteString())
|
||||
.setContent(ByteString.copyFrom(multiRecipientMessage.messageForRecipient(multiRecipientMessage.getRecipients().get(new ServiceId.Pni(singleDeviceAccountPniServiceIdentifier.uuid())))))
|
||||
.build()));
|
||||
|
||||
verify(foundationDbMessageStore).insert(multiDeviceAccountAciServiceIdentifier,
|
||||
Map.of(
|
||||
Device.PRIMARY_ID, prototypeExpectedFoundationDbMessage.toBuilder()
|
||||
.setDestinationServiceId(multiDeviceAccountAciServiceIdentifier.toCompactByteString())
|
||||
.setContent(ByteString.copyFrom(multiRecipientMessage.messageForRecipient(multiRecipientMessage.getRecipients().get(new ServiceId.Aci(multiDeviceAccountAciServiceIdentifier.uuid())))))
|
||||
.build(),
|
||||
(byte) (Device.PRIMARY_ID + 1), prototypeExpectedFoundationDbMessage.toBuilder()
|
||||
.setDestinationServiceId(multiDeviceAccountAciServiceIdentifier.toCompactByteString())
|
||||
.setContent(ByteString.copyFrom(multiRecipientMessage.messageForRecipient(multiRecipientMessage.getRecipients().get(new ServiceId.Aci(multiDeviceAccountAciServiceIdentifier.uuid())))))
|
||||
.build())
|
||||
);
|
||||
|
||||
verify(foundationDbMessageStore, never()).insert(eq(unresolvedAccountAciServiceIdentifier), any());
|
||||
} else {
|
||||
verifyNoInteractions(foundationDbMessageStore);
|
||||
}
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
|
||||
+1
-1
@@ -12,7 +12,7 @@ import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.testcontainers.utility.DockerImageName;
|
||||
|
||||
class TestcontainersFoundationDbDatabaseLifecycleManager implements FoundationDbDatabaseLifecycleManager {
|
||||
public class TestcontainersFoundationDbDatabaseLifecycleManager implements FoundationDbDatabaseLifecycleManager {
|
||||
|
||||
private FoundationDBContainer foundationDBContainer;
|
||||
private Database database;
|
||||
|
||||
+88
@@ -452,6 +452,94 @@ class FoundationDbMessageStoreTest {
|
||||
Map.of(generateRandomAciForShard(0), Collections.emptyMap())));
|
||||
}
|
||||
|
||||
@Test
|
||||
void delete() {
|
||||
final AciServiceIdentifier deletedMessageAci = new AciServiceIdentifier(UUID.randomUUID());
|
||||
final byte deletedMessageDeviceId = Device.PRIMARY_ID;
|
||||
final MessageProtos.Envelope deletedMessage = generateRandomMessage(false);
|
||||
|
||||
final AciServiceIdentifier retainedMessageAci = new AciServiceIdentifier(UUID.randomUUID());
|
||||
final byte retainedMessageDeviceId = Device.PRIMARY_ID;
|
||||
final MessageProtos.Envelope retainedMessage = generateRandomMessage(false);
|
||||
|
||||
final UUID deletedMessageGuid =
|
||||
foundationDbMessageStore.insert(deletedMessageAci, Map.of(deletedMessageDeviceId, deletedMessage)).join()
|
||||
.get(deletedMessageDeviceId).messageGuid().orElseThrow();
|
||||
|
||||
foundationDbMessageStore.insert(retainedMessageAci, Map.of(retainedMessageDeviceId, retainedMessage)).join();
|
||||
|
||||
assertArrayEquals(deletedMessage.toByteArray(),
|
||||
getItemsInDeviceQueue(deletedMessageAci, deletedMessageDeviceId).getFirst().getValue());
|
||||
|
||||
assertArrayEquals(retainedMessage.toByteArray(),
|
||||
getItemsInDeviceQueue(retainedMessageAci, retainedMessageDeviceId).getFirst().getValue());
|
||||
|
||||
foundationDbMessageStore.delete(deletedMessageAci, deletedMessageDeviceId, deletedMessageGuid);
|
||||
|
||||
assertTrue(getItemsInDeviceQueue(deletedMessageAci, deletedMessageDeviceId).isEmpty());
|
||||
|
||||
assertArrayEquals(retainedMessage.toByteArray(),
|
||||
getItemsInDeviceQueue(retainedMessageAci, retainedMessageDeviceId).getFirst().getValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
void clearAllForAccount() {
|
||||
final AciServiceIdentifier deletedAccountIdentifier = new AciServiceIdentifier(UUID.randomUUID());
|
||||
final byte deletedPrimaryDeviceId = Device.PRIMARY_ID;
|
||||
final byte deletedLinkedDeviceId = deletedPrimaryDeviceId + 1;
|
||||
|
||||
final AciServiceIdentifier retainedAccountIdentifier = new AciServiceIdentifier(UUID.randomUUID());
|
||||
final byte retainedPrimaryDeviceId = Device.PRIMARY_ID;
|
||||
|
||||
foundationDbMessageStore.insert(deletedAccountIdentifier, Map.of(
|
||||
deletedPrimaryDeviceId, generateRandomMessage(false),
|
||||
deletedLinkedDeviceId, generateRandomMessage(false)
|
||||
)).join();
|
||||
|
||||
foundationDbMessageStore.insert(retainedAccountIdentifier, Map.of(
|
||||
retainedPrimaryDeviceId, generateRandomMessage(false)
|
||||
)).join();
|
||||
|
||||
assertEquals(1, getItemsInDeviceQueue(deletedAccountIdentifier, deletedPrimaryDeviceId).size());
|
||||
assertEquals(1, getItemsInDeviceQueue(deletedAccountIdentifier, deletedLinkedDeviceId).size());
|
||||
assertEquals(1, getItemsInDeviceQueue(retainedAccountIdentifier, retainedPrimaryDeviceId).size());
|
||||
|
||||
foundationDbMessageStore.clearAll(deletedAccountIdentifier);
|
||||
|
||||
assertEquals(0, getItemsInDeviceQueue(deletedAccountIdentifier, deletedPrimaryDeviceId).size());
|
||||
assertEquals(0, getItemsInDeviceQueue(deletedAccountIdentifier, deletedLinkedDeviceId).size());
|
||||
assertEquals(1, getItemsInDeviceQueue(retainedAccountIdentifier, retainedPrimaryDeviceId).size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void clearAllForDevice() {
|
||||
final AciServiceIdentifier targetedAccountIdentifier = new AciServiceIdentifier(UUID.randomUUID());
|
||||
final byte targetedAccountRetainedDeviceId = Device.PRIMARY_ID;
|
||||
final byte targetedAccountDeletedDeviceId = targetedAccountRetainedDeviceId + 1;
|
||||
|
||||
final AciServiceIdentifier untargetedAccountIdentifier = new AciServiceIdentifier(UUID.randomUUID());
|
||||
final byte untargetedAccountPrimaryDeviceId = Device.PRIMARY_ID;
|
||||
|
||||
foundationDbMessageStore.insert(targetedAccountIdentifier, Map.of(
|
||||
targetedAccountRetainedDeviceId, generateRandomMessage(false),
|
||||
targetedAccountDeletedDeviceId, generateRandomMessage(false)
|
||||
)).join();
|
||||
|
||||
foundationDbMessageStore.insert(untargetedAccountIdentifier, Map.of(
|
||||
untargetedAccountPrimaryDeviceId, generateRandomMessage(false)
|
||||
)).join();
|
||||
|
||||
assertEquals(1, getItemsInDeviceQueue(targetedAccountIdentifier, targetedAccountRetainedDeviceId).size());
|
||||
assertEquals(1, getItemsInDeviceQueue(targetedAccountIdentifier, targetedAccountDeletedDeviceId).size());
|
||||
assertEquals(1, getItemsInDeviceQueue(untargetedAccountIdentifier, untargetedAccountPrimaryDeviceId).size());
|
||||
|
||||
foundationDbMessageStore.clearAll(targetedAccountIdentifier, targetedAccountDeletedDeviceId);
|
||||
|
||||
assertEquals(1, getItemsInDeviceQueue(targetedAccountIdentifier, targetedAccountRetainedDeviceId).size());
|
||||
assertEquals(0, getItemsInDeviceQueue(targetedAccountIdentifier, targetedAccountDeletedDeviceId).size());
|
||||
assertEquals(1, getItemsInDeviceQueue(untargetedAccountIdentifier, untargetedAccountPrimaryDeviceId).size());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource
|
||||
void getMessages(final int numMessages, final int batchSize) {
|
||||
|
||||
+4
-3
@@ -68,6 +68,7 @@ import org.whispersystems.textsecuregcm.storage.MessagesCache;
|
||||
import org.whispersystems.textsecuregcm.storage.MessagesDynamoDb;
|
||||
import org.whispersystems.textsecuregcm.storage.MessagesManager;
|
||||
import org.whispersystems.textsecuregcm.storage.ReportMessageManager;
|
||||
import org.whispersystems.textsecuregcm.storage.foundationdb.FoundationDbMessageStore;
|
||||
import org.whispersystems.textsecuregcm.util.UUIDUtil;
|
||||
import org.whispersystems.websocket.WebSocketClient;
|
||||
import org.whispersystems.websocket.messages.WebSocketResponseMessage;
|
||||
@@ -149,7 +150,7 @@ class WebSocketConnectionIntegrationTest {
|
||||
void testProcessStoredMessages(final int persistedMessageCount, final int cachedMessageCount) {
|
||||
final WebSocketConnection webSocketConnection = new WebSocketConnection(
|
||||
mock(ReceiptSender.class),
|
||||
new MessagesManager(messagesDynamoDb, messagesCache, redisMessageAvailabilityManager, reportMessageManager, sharedExecutorService, Clock.systemUTC()),
|
||||
new MessagesManager(messagesDynamoDb, messagesCache, mock(FoundationDbMessageStore.class), redisMessageAvailabilityManager, reportMessageManager, sharedExecutorService, Clock.systemUTC(), mock(ExperimentEnrollmentManager.class)),
|
||||
new MessageMetrics(),
|
||||
mock(PushNotificationManager.class),
|
||||
mock(PushNotificationScheduler.class),
|
||||
@@ -224,7 +225,7 @@ class WebSocketConnectionIntegrationTest {
|
||||
void testProcessStoredMessagesMultipleSegments() {
|
||||
final WebSocketConnection webSocketConnection = new WebSocketConnection(
|
||||
mock(ReceiptSender.class),
|
||||
new MessagesManager(messagesDynamoDb, messagesCache, redisMessageAvailabilityManager, reportMessageManager, sharedExecutorService, Clock.systemUTC()),
|
||||
new MessagesManager(messagesDynamoDb, messagesCache, mock(FoundationDbMessageStore.class), redisMessageAvailabilityManager, reportMessageManager, sharedExecutorService, Clock.systemUTC(), mock(ExperimentEnrollmentManager.class)),
|
||||
new MessageMetrics(),
|
||||
mock(PushNotificationManager.class),
|
||||
mock(PushNotificationScheduler.class),
|
||||
@@ -319,7 +320,7 @@ class WebSocketConnectionIntegrationTest {
|
||||
void testProcessStoredMessagesClientClosed() {
|
||||
final WebSocketConnection webSocketConnection = new WebSocketConnection(
|
||||
mock(ReceiptSender.class),
|
||||
new MessagesManager(messagesDynamoDb, messagesCache, redisMessageAvailabilityManager, reportMessageManager, sharedExecutorService, Clock.systemUTC()),
|
||||
new MessagesManager(messagesDynamoDb, messagesCache, mock(FoundationDbMessageStore.class), redisMessageAvailabilityManager, reportMessageManager, sharedExecutorService, Clock.systemUTC(), mock(ExperimentEnrollmentManager.class)),
|
||||
new MessageMetrics(),
|
||||
mock(PushNotificationManager.class),
|
||||
mock(PushNotificationScheduler.class),
|
||||
|
||||
Reference in new issue
Block a user