Add numberless case to AccountsManagerConcurrentModificationIntegrationTest

This commit is contained in:
Chris Eager authored and Jon Chambers committed 2026-08-12 09:08:51 -04:00
1 parent 9d601047c8
commit 7ec1f78406
1 file changed
+46 -50
@@ -25,9 +25,6 @@ import java.nio.charset.StandardCharsets;
import java.time.Clock;
import java.time.Duration;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Optional;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
@@ -38,8 +35,9 @@ import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
import java.util.stream.Stream;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.mockito.ArgumentCaptor;
import org.mockito.stubbing.Answer;
import org.signal.libsignal.protocol.IdentityKey;
@@ -47,15 +45,13 @@ import org.signal.libsignal.protocol.ecc.ECKeyPair;
import org.whispersystems.textsecuregcm.auth.DisconnectionRequestManager;
import org.whispersystems.textsecuregcm.auth.SaltedTokenHash;
import org.whispersystems.textsecuregcm.auth.UnidentifiedAccessUtil;
import org.whispersystems.textsecuregcm.entities.AccountAttributes;
import org.whispersystems.textsecuregcm.identity.IdentityType;
import org.whispersystems.textsecuregcm.redis.FaultTolerantRedisClient;
import org.whispersystems.textsecuregcm.securestorage.SecureStorageClient;
import org.whispersystems.textsecuregcm.securevaluerecovery.SecureValueRecoveryClient;
import org.whispersystems.textsecuregcm.storage.DynamoDbExtensionSchema.Tables;
import org.whispersystems.textsecuregcm.tests.util.AccountsHelper;
import org.whispersystems.textsecuregcm.tests.util.DevicesHelper;
import org.whispersystems.textsecuregcm.tests.util.JsonHelpers;
import org.whispersystems.textsecuregcm.tests.util.KeysHelper;
import org.whispersystems.textsecuregcm.tests.util.RedisClusterHelper;
import org.whispersystems.textsecuregcm.util.Pair;
import org.whispersystems.textsecuregcm.util.ThrowingSupplier;
@@ -71,6 +67,7 @@ class AccountsManagerConcurrentModificationIntegrationTest {
Tables.DELETED_ACCOUNTS,
Tables.EC_KEYS,
Tables.PAGED_PQ_KEYS,
Tables.REDEEMED_RECEIPTS,
Tables.REPEATED_USE_EC_SIGNED_PRE_KEYS,
Tables.REPEATED_USE_KEM_SIGNED_PRE_KEYS);
@@ -136,91 +133,89 @@ class AccountsManagerConcurrentModificationIntegrationTest {
}
}
@Test
void testConcurrentUpdate() throws IOException, InterruptedException {
final UUID uuid;
@ParameterizedTest
@ValueSource(booleans = {true, false})
void testConcurrentUpdate(final boolean numberless) throws IOException {
final UUID aci;
{
final ECKeyPair aciKeyPair = ECKeyPair.generate();
final ECKeyPair pniKeyPair = ECKeyPair.generate();
final Account account = accountsManager.update(
accountsManager.create("+14155551212",
new AccountAttributes(),
new ArrayList<>(),
new IdentityKey(aciKeyPair.getPublicKey()),
new IdentityKey(pniKeyPair.getPublicKey()),
new DeviceSpec(
null,
"password",
null,
Set.of(),
new DeviceIdentityInfo(1, KeysHelper.signedECPreKey(1, aciKeyPair), KeysHelper.signedKEMPreKey(3, aciKeyPair)),
Optional.of(new DeviceIdentityInfo(2, KeysHelper.signedECPreKey(2, pniKeyPair), KeysHelper.signedKEMPreKey(4, pniKeyPair))),
true,
Optional.empty(),
Optional.empty()),
null).getAccountIdentifier(),
final AccountsHelper.AccountBuilder accountBuilder = new AccountsHelper.AccountBuilder(accountsManager);
if (!numberless) {
accountBuilder.e164("+14155551212");
}
aci = accountBuilder.build().getAccountIdentifier();
// set some additional attributes
accountsManager.update(aci,
a -> {
a.setUnidentifiedAccessKey(new byte[UnidentifiedAccessUtil.UNIDENTIFIED_ACCESS_KEY_LENGTH]);
a.removeDevice(Device.PRIMARY_ID);
a.addDevice(DevicesHelper.createDevice(Device.PRIMARY_ID));
});
uuid = account.getAccountIdentifier();
}
final boolean discoverableByPhoneNumber = false;
final byte[] currentProfileVersion = new byte[32];
final IdentityKey identityKey = new IdentityKey(ECKeyPair.generate().getPublicKey());
final byte[] unidentifiedAccessKey = new byte[]{1};
final String pin = "1234";
final String registrationLock = "reglock";
final SaltedTokenHash credentials = SaltedTokenHash.generateFor(registrationLock);
final String accountRecovery = "account-recovery";
final SaltedTokenHash accountRecoveryPassword = SaltedTokenHash.generateFor(accountRecovery);
final boolean unrestrictedUnidentifiedAccess = true;
final long lastSeen = Instant.now().getEpochSecond();
CompletableFuture.allOf(
modifyAccount(uuid, account -> account.setDiscoverableByPhoneNumber(discoverableByPhoneNumber)),
modifyAccount(uuid, account -> account.setCurrentProfileVersion(currentProfileVersion)),
modifyAccount(uuid, account -> account.setIdentityKey(identityKey)),
modifyAccount(uuid, account -> account.setUnidentifiedAccessKey(unidentifiedAccessKey)),
modifyAccount(uuid, account -> account.setRegistrationLock(credentials.hash(), credentials.salt())),
modifyAccount(uuid, account -> account.setUnrestrictedUnidentifiedAccess(unrestrictedUnidentifiedAccess)),
modifyDevice(uuid, Device.PRIMARY_ID, device -> device.setLastSeen(lastSeen)),
modifyDevice(uuid, Device.PRIMARY_ID, device -> device.setName("deviceName".getBytes(StandardCharsets.UTF_8)))
modifyAccount(aci, account -> account.setDiscoverableByPhoneNumber(true)),
modifyAccount(aci, account -> account.setCurrentProfileVersion(currentProfileVersion)),
modifyAccount(aci, account -> account.setIdentityKey(identityKey)),
modifyAccount(aci, account -> account.setUnidentifiedAccessKey(unidentifiedAccessKey)),
modifyAccount(aci, account -> {
if (!numberless) {
account.setRegistrationLock(credentials.hash(), credentials.salt());
}
}),
modifyAccount(aci, account -> account.setAccountRecoveryPassword(accountRecoveryPassword)),
modifyAccount(aci, account -> account.setUnrestrictedUnidentifiedAccess(unrestrictedUnidentifiedAccess)),
modifyDevice(aci, Device.PRIMARY_ID, device -> device.setLastSeen(lastSeen)),
modifyDevice(aci, Device.PRIMARY_ID, device -> device.setName("deviceName".getBytes(StandardCharsets.UTF_8)))
).join();
final Account managerAccount = accountsManager.getByAccountIdentifier(uuid).orElseThrow();
final Account dynamoAccount = accounts.getByAccountIdentifier(uuid).orElseThrow();
final Account managerAccount = accountsManager.getByAccountIdentifier(aci).orElseThrow();
final Account dynamoAccount = accounts.getByAccountIdentifier(aci).orElseThrow();
final Account redisAccount = getLastAccountFromRedisMock(commands);
// accounts with numbers have twice as many setex calls, because they include a setex(pni, aci) call
final Account redisAccount = getLastAccountFromRedisMock(commands, numberless ? 10 : 20);
Stream.of(
new Pair<>("manager", managerAccount),
new Pair<>("dynamo", dynamoAccount),
new Pair<>("redis", redisAccount)
).forEach(pair ->
verifyAccount(pair.first(), pair.second(), discoverableByPhoneNumber,
currentProfileVersion, identityKey, unidentifiedAccessKey, pin, registrationLock,
verifyAccount(pair.first(), pair.second(), !numberless,
currentProfileVersion, identityKey, unidentifiedAccessKey, pin, registrationLock, accountRecovery,
unrestrictedUnidentifiedAccess, lastSeen));
}
private Account getLastAccountFromRedisMock(RedisAdvancedClusterCommands<String, String> commands) throws IOException {
private Account getLastAccountFromRedisMock(RedisAdvancedClusterCommands<String, String> commands, final int minimumSetExCalls) throws IOException {
ArgumentCaptor<String> redisSetArgumentCapture = ArgumentCaptor.forClass(String.class);
verify(commands, atLeast(20)).setex(anyString(), anyLong(), redisSetArgumentCapture.capture());
verify(commands, atLeast(minimumSetExCalls)).setex(anyString(), anyLong(), redisSetArgumentCapture.capture());
return JsonHelpers.fromJson(redisSetArgumentCapture.getValue(), Account.class);
}
private void verifyAccount(final String name, final Account account, final boolean discoverableByPhoneNumber, final byte[] currentProfileVersion, final IdentityKey identityKey, final byte[] unidentifiedAccessKey, final String pin, final String clientRegistrationLock, final boolean unrestrictedUnidentifiedAccess, final long lastSeen) {
private void verifyAccount(final String name, final Account account, final boolean discoverableByPhoneNumber, final byte[] currentProfileVersion, final IdentityKey identityKey, final byte[] unidentifiedAccessKey, final String pin, final String clientRegistrationLock, final String accountRecoveryPassword, final boolean unrestrictedUnidentifiedAccess, final long lastSeen) {
assertAll(name,
() -> assertEquals(discoverableByPhoneNumber, account.isDiscoverableByPhoneNumber()),
() -> assertArrayEquals(currentProfileVersion, account.getCurrentProfileVersion().orElseThrow()),
() -> assertEquals(identityKey, account.getIdentityKey(IdentityType.ACI)),
() -> assertEquals(identityKey, account.getAccountIdentityKey()),
() -> assertArrayEquals(unidentifiedAccessKey, account.getUnidentifiedAccessKey().orElseThrow()),
() -> assertTrue(account.getRegistrationLock().verify(clientRegistrationLock)),
() -> assertTrue(account.getPhoneNumberIdentifierOptional().isEmpty() || account.getRegistrationLock().verify(clientRegistrationLock)),
() -> assertTrue(account.getAccountRecoveryPassword().orElseThrow().verify(accountRecoveryPassword)),
() -> assertEquals(unrestrictedUnidentifiedAccess, account.isUnrestrictedUnidentifiedAccess())
);
}
@@ -232,4 +227,5 @@ class AccountsManagerConcurrentModificationIntegrationTest {
private CompletableFuture<?> modifyDevice(final UUID uuid, final byte deviceId, final Consumer<Device> deviceMutation) {
return CompletableFuture.runAsync(() -> accountsManager.updateDevice(uuid, deviceId, deviceMutation), mutationExecutor);
}
}