Add a method to build an `OutgoingMessageEntity` from an `Envelope`
This commit is contained in:
parent
d385838dc1
commit
3e0919106d
|
@ -7,13 +7,14 @@ package org.whispersystems.textsecuregcm.entities;
|
||||||
|
|
||||||
import com.google.protobuf.ByteString;
|
import com.google.protobuf.ByteString;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
import javax.annotation.Nullable;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
public record OutgoingMessageEntity(UUID guid, int type, long timestamp, String source, UUID sourceUuid,
|
public record OutgoingMessageEntity(UUID guid, int type, long timestamp, @Nullable String source,
|
||||||
int sourceDevice, UUID destinationUuid, UUID updatedPni, byte[] content,
|
@Nullable UUID sourceUuid, int sourceDevice, UUID destinationUuid,
|
||||||
long serverTimestamp) {
|
@Nullable UUID updatedPni, byte[] content, long serverTimestamp) {
|
||||||
|
|
||||||
public MessageProtos.Envelope toEnvelope() {
|
public MessageProtos.Envelope toEnvelope() {
|
||||||
final MessageProtos.Envelope.Builder builder = MessageProtos.Envelope.newBuilder()
|
final MessageProtos.Envelope.Builder builder = MessageProtos.Envelope.newBuilder()
|
||||||
|
@ -43,6 +44,20 @@ public record OutgoingMessageEntity(UUID guid, int type, long timestamp, String
|
||||||
return builder.build();
|
return builder.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static OutgoingMessageEntity fromEnvelope(final MessageProtos.Envelope envelope) {
|
||||||
|
return new OutgoingMessageEntity(
|
||||||
|
UUID.fromString(envelope.getServerGuid()),
|
||||||
|
envelope.getType().getNumber(),
|
||||||
|
envelope.getTimestamp(),
|
||||||
|
envelope.getSource(),
|
||||||
|
envelope.hasSourceUuid() ? UUID.fromString(envelope.getSourceUuid()) : null,
|
||||||
|
envelope.getSourceDevice(),
|
||||||
|
envelope.hasDestinationUuid() ? UUID.fromString(envelope.getDestinationUuid()) : null,
|
||||||
|
envelope.hasUpdatedPni() ? UUID.fromString(envelope.getUpdatedPni()) : null,
|
||||||
|
envelope.getContent().toByteArray(),
|
||||||
|
envelope.getServerTimestamp());
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(final Object o) {
|
public boolean equals(final Object o) {
|
||||||
if (this == o) {
|
if (this == o) {
|
||||||
|
|
|
@ -0,0 +1,50 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2013-2022 Signal Messenger, LLC
|
||||||
|
* SPDX-License-Identifier: AGPL-3.0-only
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.whispersystems.textsecuregcm.entities;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
|
import java.util.Random;
|
||||||
|
import java.util.UUID;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
import javax.annotation.Nullable;
|
||||||
|
import org.junit.jupiter.params.ParameterizedTest;
|
||||||
|
import org.junit.jupiter.params.provider.Arguments;
|
||||||
|
import org.junit.jupiter.params.provider.MethodSource;
|
||||||
|
import org.whispersystems.textsecuregcm.storage.Device;
|
||||||
|
|
||||||
|
class OutgoingMessageEntityTest {
|
||||||
|
|
||||||
|
@ParameterizedTest
|
||||||
|
@MethodSource
|
||||||
|
void toFromEnvelope(@Nullable final String source, @Nullable final UUID sourceUuid, @Nullable final UUID updatedPni) {
|
||||||
|
final byte[] messageContent = new byte[16];
|
||||||
|
new Random().nextBytes(messageContent);
|
||||||
|
|
||||||
|
final long messageTimestamp = System.currentTimeMillis();
|
||||||
|
final long serverTimestamp = messageTimestamp + 17;
|
||||||
|
|
||||||
|
final OutgoingMessageEntity outgoingMessageEntity = new OutgoingMessageEntity(UUID.randomUUID(),
|
||||||
|
MessageProtos.Envelope.Type.CIPHERTEXT_VALUE,
|
||||||
|
messageTimestamp,
|
||||||
|
"+18005551234",
|
||||||
|
UUID.randomUUID(),
|
||||||
|
source != null ? (int) Device.MASTER_ID : 0,
|
||||||
|
UUID.randomUUID(),
|
||||||
|
UUID.randomUUID(),
|
||||||
|
messageContent,
|
||||||
|
serverTimestamp);
|
||||||
|
|
||||||
|
assertEquals(outgoingMessageEntity, OutgoingMessageEntity.fromEnvelope(outgoingMessageEntity.toEnvelope()));
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Stream<Arguments> toFromEnvelope() {
|
||||||
|
return Stream.of(
|
||||||
|
Arguments.of("+18005551234", UUID.randomUUID(), UUID.randomUUID()),
|
||||||
|
Arguments.of("+18005551234", UUID.randomUUID(), null),
|
||||||
|
Arguments.of(null, null, UUID.randomUUID()));
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue