mirror of
https://github.com/signalapp/Signal-Server
synced 2026-04-21 04:18:04 +01:00
Add annotation-based validations to a number of services
This commit is contained in:
committed by
ravi-signal
parent
aae1541e88
commit
610782b8c3
@@ -211,7 +211,7 @@ class AccountsGrpcServiceTest extends SimpleBaseGrpcTest<AccountsGrpcService, Ac
|
||||
//noinspection ResultOfMethodCallIgnored
|
||||
GrpcTestUtils.assertStatusException(Status.INVALID_ARGUMENT, "BAD_AUTHENTICATION",
|
||||
() -> authenticatedServiceStub().setRegistrationLock(SetRegistrationLockRequest.newBuilder()
|
||||
.setRegistrationLock(ByteString.copyFrom(TestRandomUtil.nextBytes(16)))
|
||||
.setRegistrationLock(ByteString.copyFrom(TestRandomUtil.nextBytes(32)))
|
||||
.build()));
|
||||
|
||||
verify(accountsManager, never()).update(any(), any());
|
||||
|
||||
@@ -11,6 +11,7 @@ import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import com.google.common.collect.Iterators;
|
||||
import com.google.protobuf.ByteString;
|
||||
import io.grpc.Status;
|
||||
import io.grpc.StatusRuntimeException;
|
||||
@@ -26,13 +27,16 @@ import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.IntStream;
|
||||
import java.util.stream.Stream;
|
||||
import org.apache.commons.collections4.IteratorUtils;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.Arguments;
|
||||
import org.junit.jupiter.params.provider.CsvSource;
|
||||
import org.junit.jupiter.params.provider.EnumSource;
|
||||
import org.junit.jupiter.params.provider.MethodSource;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
import org.junitpioneer.jupiter.cartesian.CartesianTest;
|
||||
import org.mockito.Mock;
|
||||
import org.signal.chat.backup.BackupsAnonymousGrpc;
|
||||
@@ -380,6 +384,60 @@ class BackupsAnonymousGrpcServiceTest extends
|
||||
assertThat(response.getCdnCredentials().getHeadersMap()).containsExactlyEntriesOf(Map.of("key", "value"));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(ints = {0, 1001})
|
||||
void copyMediaInvalidRequest(final int count) {
|
||||
final SignedPresentation sp = SignedPresentation.newBuilder()
|
||||
.setPresentation(ByteString.copyFrom(TestRandomUtil.nextBytes(10)))
|
||||
.setPresentationSignature(ByteString.copyFromUtf8("aaa")).build();
|
||||
|
||||
final CopyMediaItem validItem = CopyMediaItem.newBuilder()
|
||||
.setSourceAttachmentCdn(3)
|
||||
.setSourceKey("abc")
|
||||
.setObjectLength(100)
|
||||
.setMediaId(ByteString.copyFrom(TestRandomUtil.nextBytes(15)))
|
||||
.setHmacKey(ByteString.copyFrom(TestRandomUtil.nextBytes(32)))
|
||||
.setEncryptionKey(ByteString.copyFrom(TestRandomUtil.nextBytes(32)))
|
||||
.build();
|
||||
|
||||
GrpcTestUtils.assertStatusInvalidArgument(() -> IteratorUtils.toList(unauthenticatedServiceStub().copyMedia(
|
||||
CopyMediaRequest.newBuilder()
|
||||
.setSignedPresentation(sp)
|
||||
.addAllItems(IntStream.range(0, count).mapToObj(_ -> validItem).toList())
|
||||
.build())));
|
||||
}
|
||||
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(ints = {0, 1001})
|
||||
void deleteMediaInvalidRequest(final int count) {
|
||||
final SignedPresentation sp = SignedPresentation.newBuilder()
|
||||
.setPresentation(ByteString.copyFrom(TestRandomUtil.nextBytes(10)))
|
||||
.setPresentationSignature(ByteString.copyFromUtf8("aaa")).build();
|
||||
|
||||
final DeleteMediaItem validItem = DeleteMediaItem.newBuilder()
|
||||
.setCdn(3)
|
||||
.setMediaId(ByteString.copyFrom(TestRandomUtil.nextBytes(15)))
|
||||
.build();
|
||||
|
||||
GrpcTestUtils.assertStatusInvalidArgument(() -> IteratorUtils.toList(unauthenticatedServiceStub().deleteMedia(
|
||||
DeleteMediaRequest.newBuilder()
|
||||
.setSignedPresentation(sp)
|
||||
.addAllItems(IntStream.range(0, count).mapToObj(_ -> validItem).toList())
|
||||
.build())));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(ints = {0, 10001})
|
||||
void listMediaInvalidLimit(int count) {
|
||||
GrpcTestUtils.assertStatusInvalidArgument(() -> unauthenticatedServiceStub().listMedia(
|
||||
ListMediaRequest.newBuilder()
|
||||
.setSignedPresentation(signedPresentation(presentation))
|
||||
.setLimit(count)
|
||||
.build()));
|
||||
}
|
||||
|
||||
|
||||
private static AuthenticatedBackupUser backupUser(final byte[] backupId, final BackupCredentialType credentialType,
|
||||
final BackupLevel backupLevel) {
|
||||
return new AuthenticatedBackupUser(backupId, credentialType, backupLevel, "myBackupDir", "myMediaDir", null);
|
||||
|
||||
@@ -18,6 +18,8 @@ import java.util.UUID;
|
||||
import java.util.stream.IntStream;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.Mockito;
|
||||
import org.signal.chat.credentials.AuthCheckResult;
|
||||
@@ -127,11 +129,12 @@ class ExternalServiceCredentialsAnonymousGrpcServiceTest extends
|
||||
), day(25));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTooManyPasswords() {
|
||||
@ParameterizedTest
|
||||
@ValueSource(ints = {0, 11})
|
||||
public void testInvalidPasswordCount(int count) {
|
||||
final CheckSvrCredentialsRequest request = CheckSvrCredentialsRequest.newBuilder()
|
||||
.setNumber(USER_E164)
|
||||
.addAllPasswords(IntStream.range(0, 12).mapToObj(i -> token(UUID.randomUUID(), day(10))).toList())
|
||||
.addAllPasswords(IntStream.range(0, count).mapToObj(i -> token(UUID.randomUUID(), day(10))).toList())
|
||||
.build();
|
||||
final StatusRuntimeException status = assertThrows(StatusRuntimeException.class,
|
||||
() -> unauthenticatedServiceStub().checkSvrCredentials(request));
|
||||
|
||||
@@ -149,7 +149,7 @@ class KeysGrpcServiceTest extends SimpleBaseGrpcTest<KeysGrpcService, KeysGrpc.K
|
||||
void setOneTimeEcPreKeys(final org.signal.chat.common.IdentityType identityType) {
|
||||
final List<ECPreKey> preKeys = new ArrayList<>();
|
||||
|
||||
for (int keyId = 0; keyId < 100; keyId++) {
|
||||
for (int keyId = 1; keyId <= 100; keyId++) {
|
||||
preKeys.add(new ECPreKey(keyId, ECKeyPair.generate().getPublicKey()));
|
||||
}
|
||||
|
||||
@@ -220,7 +220,7 @@ class KeysGrpcServiceTest extends SimpleBaseGrpcTest<KeysGrpcService, KeysGrpc.K
|
||||
|
||||
final List<KEMSignedPreKey> preKeys = new ArrayList<>();
|
||||
|
||||
for (int keyId = 0; keyId < 100; keyId++) {
|
||||
for (int keyId = 1; keyId <= 100; keyId++) {
|
||||
preKeys.add(KeysHelper.signedKEMPreKey(keyId, identityKeyPair));
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user