Add metrics for registration lock flow

This commit is contained in:
Katherine Yen
2023-03-09 09:07:21 -08:00
committed by GitHub
parent c06313dd2e
commit 46fef4082c
10 changed files with 157 additions and 39 deletions

View File

@@ -26,6 +26,7 @@ import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.EnumSource;
import org.junit.jupiter.params.provider.MethodSource;
import org.whispersystems.textsecuregcm.controllers.RateLimitExceededException;
import org.whispersystems.textsecuregcm.entities.PhoneVerificationRequest;
import org.whispersystems.textsecuregcm.limits.RateLimiter;
import org.whispersystems.textsecuregcm.limits.RateLimiters;
import org.whispersystems.textsecuregcm.push.ClientPresenceManager;
@@ -65,7 +66,7 @@ class RegistrationLockVerificationManagerTest {
@EnumSource
void testErrors(RegistrationLockError error) throws Exception {
when(existingRegistrationLock.requiresClientRegistrationLock()).thenReturn(true);
when(existingRegistrationLock.getStatus()).thenReturn(StoredRegistrationLock.Status.REQUIRED);
final String submittedRegistrationLock = "reglock";
@@ -89,29 +90,35 @@ class RegistrationLockVerificationManagerTest {
};
final Exception e = assertThrows(exceptionType.first(), () ->
registrationLockVerificationManager.verifyRegistrationLock(account, submittedRegistrationLock));
registrationLockVerificationManager.verifyRegistrationLock(account, submittedRegistrationLock,
"Signal-Android/4.68.3", RegistrationLockVerificationManager.Flow.REGISTRATION,
PhoneVerificationRequest.VerificationType.SESSION));
exceptionType.second().accept(e);
}
@ParameterizedTest
@MethodSource
void testSuccess(final boolean requiresClientRegistrationLock, @Nullable final String submittedRegistrationLock) {
void testSuccess(final StoredRegistrationLock.Status status, @Nullable final String submittedRegistrationLock) {
when(existingRegistrationLock.requiresClientRegistrationLock())
.thenReturn(requiresClientRegistrationLock);
when(existingRegistrationLock.getStatus())
.thenReturn(status);
when(existingRegistrationLock.verify(submittedRegistrationLock)).thenReturn(true);
assertDoesNotThrow(
() -> registrationLockVerificationManager.verifyRegistrationLock(account, submittedRegistrationLock));
() -> registrationLockVerificationManager.verifyRegistrationLock(account, submittedRegistrationLock,
"Signal-Android/4.68.3", RegistrationLockVerificationManager.Flow.REGISTRATION,
PhoneVerificationRequest.VerificationType.SESSION));
}
static Stream<Arguments> testSuccess() {
return Stream.of(
Arguments.of(false, null),
Arguments.of(true, null),
Arguments.of(false, "reglock"),
Arguments.of(true, "reglock")
Arguments.of(StoredRegistrationLock.Status.ABSENT, null),
Arguments.of(StoredRegistrationLock.Status.EXPIRED, null),
Arguments.of(StoredRegistrationLock.Status.REQUIRED, null),
Arguments.of(StoredRegistrationLock.Status.ABSENT, "reglock"),
Arguments.of(StoredRegistrationLock.Status.EXPIRED, "reglock"),
Arguments.of(StoredRegistrationLock.Status.REQUIRED, "reglock")
);
}

View File

@@ -0,0 +1,35 @@
package org.whispersystems.textsecuregcm.auth;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import javax.swing.text.html.Option;
import java.time.Duration;
import java.util.Optional;
import java.util.stream.Stream;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;
import static org.whispersystems.textsecuregcm.auth.StoredRegistrationLock.REGISTRATION_LOCK_EXPIRATION_DAYS;
public class StoredRegistrationLockTest {
@ParameterizedTest
@MethodSource
void getStatus(final Optional<String> registrationLock, final Optional<String> salt, final long lastSeen,
final StoredRegistrationLock.Status expectedStatus) {
final StoredRegistrationLock storedLock = new StoredRegistrationLock(registrationLock, salt, lastSeen);
assertEquals(expectedStatus, storedLock.getStatus());
}
private static Stream<Arguments> getStatus() {
return Stream.of(
Arguments.of(Optional.of("registrationLock"), Optional.of("salt"), System.currentTimeMillis() - Duration.ofDays(1).toMillis(), StoredRegistrationLock.Status.REQUIRED),
Arguments.of(Optional.empty(), Optional.empty(), 0L, StoredRegistrationLock.Status.ABSENT),
Arguments.of(Optional.of("registrationLock"), Optional.of("salt"), System.currentTimeMillis() - REGISTRATION_LOCK_EXPIRATION_DAYS.toMillis(), StoredRegistrationLock.Status.EXPIRED)
);
}
}

View File

@@ -1448,7 +1448,7 @@ class AccountControllerTest {
.thenReturn(CompletableFuture.completedFuture(true));
final StoredRegistrationLock existingRegistrationLock = mock(StoredRegistrationLock.class);
when(existingRegistrationLock.requiresClientRegistrationLock()).thenReturn(false);
when(existingRegistrationLock.getStatus()).thenReturn(StoredRegistrationLock.Status.ABSENT);
final Account existingAccount = mock(Account.class);
when(existingAccount.getNumber()).thenReturn(number);
@@ -1482,7 +1482,7 @@ class AccountControllerTest {
.thenReturn(CompletableFuture.completedFuture(true));
final StoredRegistrationLock existingRegistrationLock = mock(StoredRegistrationLock.class);
when(existingRegistrationLock.requiresClientRegistrationLock()).thenReturn(true);
when(existingRegistrationLock.getStatus()).thenReturn(StoredRegistrationLock.Status.REQUIRED);
final UUID existingUuid = UUID.randomUUID();
final Account existingAccount = mock(Account.class);
@@ -1521,7 +1521,7 @@ class AccountControllerTest {
.thenReturn(CompletableFuture.completedFuture(true));
final StoredRegistrationLock existingRegistrationLock = mock(StoredRegistrationLock.class);
when(existingRegistrationLock.requiresClientRegistrationLock()).thenReturn(true);
when(existingRegistrationLock.getStatus()).thenReturn(StoredRegistrationLock.Status.REQUIRED);
when(existingRegistrationLock.verify(anyString())).thenReturn(false);
UUID existingUuid = UUID.randomUUID();
@@ -1561,7 +1561,7 @@ class AccountControllerTest {
.thenReturn(CompletableFuture.completedFuture(true));
final StoredRegistrationLock existingRegistrationLock = mock(StoredRegistrationLock.class);
when(existingRegistrationLock.requiresClientRegistrationLock()).thenReturn(true);
when(existingRegistrationLock.getStatus()).thenReturn(StoredRegistrationLock.Status.REQUIRED);
when(existingRegistrationLock.verify(reglock)).thenReturn(true);
final Account existingAccount = mock(Account.class);

View File

@@ -295,7 +295,7 @@ class AccountControllerV2Test {
case RATE_LIMITED -> new RateLimitExceededException(null, true);
};
doThrow(e)
.when(registrationLockVerificationManager).verifyRegistrationLock(any(), any());
.when(registrationLockVerificationManager).verifyRegistrationLock(any(), any(), any(), any(), any());
final Invocation.Builder request = resources.getJerseyTest()
.target("/v2/accounts/number")

View File

@@ -265,7 +265,7 @@ class RegistrationControllerTest {
case RATE_LIMITED -> new RateLimitExceededException(null, true);
};
doThrow(e)
.when(registrationLockVerificationManager).verifyRegistrationLock(any(), any());
.when(registrationLockVerificationManager).verifyRegistrationLock(any(), any(), any(), any(), any());
final Invocation.Builder request = resources.getJerseyTest()
.target("/v1/registration")