From b2c638a635b54c8405ac468a5101e20d16e1b715 Mon Sep 17 00:00:00 2001 From: Chris Eager Date: Mon, 5 Oct 2026 12:15:44 -0400 Subject: [PATCH] Check max device limit inside lock scope --- .../controllers/DeviceController.java | 13 +--- .../DeviceLimitExceededExceptionMapper.java | 2 +- .../storage/AccountsManager.java | 30 +++++++-- .../DeviceLimitExceededException.java | 6 +- .../controllers/DeviceControllerTest.java | 63 +++++++++++++++---- .../storage/AccountsManagerTest.java | 62 ++++++++++++++++-- .../AddRemoveDeviceIntegrationTest.java | 38 +++++------ 7 files changed, 156 insertions(+), 58 deletions(-) rename service/src/main/java/org/whispersystems/textsecuregcm/{controllers => storage}/DeviceLimitExceededException.java (76%) diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/controllers/DeviceController.java b/service/src/main/java/org/whispersystems/textsecuregcm/controllers/DeviceController.java index 8fba504d1..8a3488956 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/controllers/DeviceController.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/controllers/DeviceController.java @@ -72,6 +72,7 @@ import org.whispersystems.textsecuregcm.storage.AccountsManager; import org.whispersystems.textsecuregcm.storage.Device; import org.whispersystems.textsecuregcm.storage.DeviceCapability; import org.whispersystems.textsecuregcm.storage.DeviceIdentityInfo; +import org.whispersystems.textsecuregcm.storage.DeviceLimitExceededException; import org.whispersystems.textsecuregcm.storage.DeviceSpec; import org.whispersystems.textsecuregcm.storage.LinkDeviceTokenAlreadyUsedException; import org.whispersystems.textsecuregcm.storage.PersistentTimer; @@ -88,8 +89,6 @@ import org.whispersystems.textsecuregcm.util.ua.UserAgentUtil; @Tag(name = "Devices") public class DeviceController { - static final int MAX_DEVICES = 6; - private final AccountsManager accounts; private final RateLimiters rateLimiters; private final PersistentTimer persistentTimer; @@ -198,15 +197,11 @@ public class DeviceController { rateLimiters.getAllocateDeviceLimiter().validate(account.getAccountIdentifier()); - if (account.getDevices().size() >= MAX_DEVICES) { - throw new DeviceLimitExceededException(account.getDevices().size(), MAX_DEVICES); - } - if (auth.deviceId() != Device.PRIMARY_ID) { throw new WebApplicationException(Response.Status.UNAUTHORIZED); } - final String token = accounts.generateLinkDeviceToken(account.getAccountIdentifier()); + final String token = accounts.generateLinkDeviceToken(account); return new LinkDeviceToken(token, AccountsManager.getLinkDeviceTokenIdentifier(token)); } @@ -271,10 +266,6 @@ public class DeviceController { throw new WebApplicationException(Response.status(422).build()); } - if (account.getDevices().size() >= MAX_DEVICES) { - throw new DeviceLimitExceededException(account.getDevices().size(), MAX_DEVICES); - } - final Set capabilities = deviceAttributes.capabilities(); if (capabilities == null) { diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/mappers/DeviceLimitExceededExceptionMapper.java b/service/src/main/java/org/whispersystems/textsecuregcm/mappers/DeviceLimitExceededExceptionMapper.java index b9f5e0b8e..b9a0945cd 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/mappers/DeviceLimitExceededExceptionMapper.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/mappers/DeviceLimitExceededExceptionMapper.java @@ -10,7 +10,7 @@ import com.fasterxml.jackson.annotation.JsonProperty; import jakarta.ws.rs.core.Response; import jakarta.ws.rs.ext.ExceptionMapper; import jakarta.ws.rs.ext.Provider; -import org.whispersystems.textsecuregcm.controllers.DeviceLimitExceededException; +import org.whispersystems.textsecuregcm.storage.DeviceLimitExceededException; @Provider public class DeviceLimitExceededExceptionMapper implements ExceptionMapper { diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/storage/AccountsManager.java b/service/src/main/java/org/whispersystems/textsecuregcm/storage/AccountsManager.java index d6320c40c..e49274715 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/storage/AccountsManager.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/storage/AccountsManager.java @@ -197,6 +197,8 @@ public class AccountsManager extends RedisPubSubAdapter implemen private static final int MAX_UPDATE_ATTEMPTS = 10; + public static final int MAX_DEVICES = 6; + private static final SecureRandom SECURE_RANDOM = new SecureRandom(); private static final int AUTH_CREDENTIAL_SALT_SIZE = 16; @@ -683,20 +685,31 @@ public class AccountsManager extends RedisPubSubAdapter implemen } public Pair addDevice(final UUID accountIdentifier, final DeviceSpec deviceSpec, final String linkDeviceToken) - throws LinkDeviceTokenAlreadyUsedException { + throws LinkDeviceTokenAlreadyUsedException, DeviceLimitExceededException { final Account account = accounts.getByAccountIdentifier(accountIdentifier) .orElseThrow(() -> new IllegalArgumentException("Account not found: " + accountIdentifier)); - return accountLockManager.withSingleAccountLock(account, - () -> addDevice(accountIdentifier, deviceSpec, linkDeviceToken, MAX_UPDATE_ATTEMPTS)); + try { + return accountLockManager.withSingleAccountLock(account, + () -> addDevice(accountIdentifier, deviceSpec, linkDeviceToken, MAX_UPDATE_ATTEMPTS)); + } catch (final LinkDeviceTokenAlreadyUsedException | DeviceLimitExceededException | RuntimeException e) { + throw e; + } catch (final Exception e) { + throw new AssertionError("Unexpected exception adding device", e); + } } private Pair addDevice(final UUID accountIdentifier, final DeviceSpec deviceSpec, final String linkDeviceToken, final int retries) - throws LinkDeviceTokenAlreadyUsedException { + throws LinkDeviceTokenAlreadyUsedException, DeviceLimitExceededException { + final Account account = accounts.getByAccountIdentifier(accountIdentifier) .orElseThrow(ContestedOptimisticLockException::new); + if (account.getDevices().size() >= MAX_DEVICES) { + throw new DeviceLimitExceededException(account.getDevices().size(), MAX_DEVICES); + } + final byte nextDeviceId = account.getNextDeviceId(); CompletableFuture @@ -789,8 +802,13 @@ public class AccountsManager extends RedisPubSubAdapter implemen } } - public String generateLinkDeviceToken(final UUID aci) { - final String claims = aci + "." + clock.instant().toEpochMilli(); + public String generateLinkDeviceToken(final Account account) throws DeviceLimitExceededException { + + if (account.getDevices().size() >= MAX_DEVICES) { + throw new DeviceLimitExceededException(account.getDevices().size(), MAX_DEVICES); + } + + final String claims = account.getAccountIdentifier() + "." + clock.instant().toEpochMilli(); final byte[] signature = getInitializedMac().doFinal(claims.getBytes(StandardCharsets.UTF_8)); return claims + ":" + Base64.getUrlEncoder().encodeToString(signature); diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/controllers/DeviceLimitExceededException.java b/service/src/main/java/org/whispersystems/textsecuregcm/storage/DeviceLimitExceededException.java similarity index 76% rename from service/src/main/java/org/whispersystems/textsecuregcm/controllers/DeviceLimitExceededException.java rename to service/src/main/java/org/whispersystems/textsecuregcm/storage/DeviceLimitExceededException.java index 457c5d2cf..e06082d3e 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/controllers/DeviceLimitExceededException.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/storage/DeviceLimitExceededException.java @@ -1,9 +1,9 @@ /* - * Copyright 2013-2020 Signal Messenger, LLC + * Copyright 2013 Signal Messenger, LLC * SPDX-License-Identifier: AGPL-3.0-only */ -package org.whispersystems.textsecuregcm.controllers; +package org.whispersystems.textsecuregcm.storage; public class DeviceLimitExceededException extends Exception { @@ -13,7 +13,7 @@ public class DeviceLimitExceededException extends Exception { public DeviceLimitExceededException(int currentDevices, int maxDevices) { this.currentDevices = currentDevices; - this.maxDevices = maxDevices; + this.maxDevices = maxDevices; } public int getCurrentDevices() { diff --git a/service/src/test/java/org/whispersystems/textsecuregcm/controllers/DeviceControllerTest.java b/service/src/test/java/org/whispersystems/textsecuregcm/controllers/DeviceControllerTest.java index 0867e045c..bca19cbd9 100644 --- a/service/src/test/java/org/whispersystems/textsecuregcm/controllers/DeviceControllerTest.java +++ b/service/src/test/java/org/whispersystems/textsecuregcm/controllers/DeviceControllerTest.java @@ -34,11 +34,11 @@ import java.util.Arrays; import java.util.Base64; import java.util.EnumSet; import java.util.List; +import java.util.Map; import java.util.Optional; import java.util.Set; import java.util.concurrent.CompletableFuture; import java.util.stream.Collectors; -import java.util.stream.IntStream; import java.util.stream.Stream; import org.apache.commons.lang3.RandomStringUtils; import org.glassfish.jersey.server.ServerProperties; @@ -80,6 +80,7 @@ import org.whispersystems.textsecuregcm.storage.Account; import org.whispersystems.textsecuregcm.storage.AccountsManager; import org.whispersystems.textsecuregcm.storage.Device; import org.whispersystems.textsecuregcm.storage.DeviceCapability; +import org.whispersystems.textsecuregcm.storage.DeviceLimitExceededException; import org.whispersystems.textsecuregcm.storage.DeviceSpec; import org.whispersystems.textsecuregcm.storage.LinkDeviceTokenAlreadyUsedException; import org.whispersystems.textsecuregcm.storage.PersistentTimer; @@ -216,7 +217,7 @@ class DeviceControllerTest { final Optional apnRegistrationId, final Optional gcmRegistrationId, final Optional expectedApnsToken, - final Optional expectedGcmToken) throws LinkDeviceTokenAlreadyUsedException { + final Optional expectedGcmToken) throws LinkDeviceTokenAlreadyUsedException, DeviceLimitExceededException { final Device existingDevice = mock(Device.class); when(existingDevice.getId()).thenReturn(Device.PRIMARY_ID); @@ -304,7 +305,7 @@ class DeviceControllerTest { void deviceDowngrade(@CartesianTest.Enum(mode = CartesianTest.Enum.Mode.EXCLUDE, names = "SPARSE_POST_QUANTUM_RATCHET") final DeviceCapability capability, @CartesianTest.Values(booleans = {true, false}) final boolean accountHasCapability, @CartesianTest.Values(booleans = {true, false}) final boolean requestHasCapability) - throws LinkDeviceTokenAlreadyUsedException { + throws LinkDeviceTokenAlreadyUsedException, DeviceLimitExceededException { when(accountsManager.getByAccountIdentifier(AuthHelper.VALID_UUID)).thenReturn(Optional.of(account)); when(accountsManager.addDevice(any(), any(), any())) @@ -360,7 +361,7 @@ class DeviceControllerTest { } @Test - void missingRequiredCapability() throws LinkDeviceTokenAlreadyUsedException { + void missingRequiredCapability() throws LinkDeviceTokenAlreadyUsedException, DeviceLimitExceededException { when(accountsManager.getByAccountIdentifier(AuthHelper.VALID_UUID)).thenReturn(Optional.of(account)); when(accountsManager.addDevice(any(), any(), any())) @@ -447,7 +448,7 @@ class DeviceControllerTest { } @Test - void linkDeviceAtomicReusedToken() throws LinkDeviceTokenAlreadyUsedException { + void linkDeviceAtomicReusedToken() throws LinkDeviceTokenAlreadyUsedException, DeviceLimitExceededException { final Device existingDevice = mock(Device.class); when(existingDevice.getId()).thenReturn(Device.PRIMARY_ID); when(account.getDevices()).thenReturn(List.of(existingDevice)); @@ -492,6 +493,47 @@ class DeviceControllerTest { } } + @Test + void linkDeviceMaxDevices() throws LinkDeviceTokenAlreadyUsedException, DeviceLimitExceededException { + final Device existingDevice = mock(Device.class); + when(existingDevice.getId()).thenReturn(Device.PRIMARY_ID); + when(account.getDevices()).thenReturn(List.of(existingDevice)); + + final ECKeyPair aciIdentityKeyPair = ECKeyPair.generate(); + + final ECSignedPreKey aciSignedPreKey = KeysHelper.signedECPreKey(1, aciIdentityKeyPair); + final KEMSignedPreKey aciPqLastResortPreKey = KeysHelper.signedKEMPreKey(3, aciIdentityKeyPair); + + when(account.getAccountIdentityKey()).thenReturn(new IdentityKey(aciIdentityKeyPair.getPublicKey())); + when(account.getPhoneNumberIdentityKey()).thenReturn(Optional.empty()); + + when(accountsManager.checkDeviceLinkingToken(anyString())).thenReturn(Optional.of(AuthHelper.VALID_UUID)); + + when(accountsManager.addDevice(any(), any(), any())) + .thenThrow(new DeviceLimitExceededException(AccountsManager.MAX_DEVICES, AccountsManager.MAX_DEVICES)); + + final DeviceAttributes deviceAttributes = new DeviceAttributes(true, 1234, null, null, + DeviceCapability.CAPABILITIES_REQUIRED_FOR_NEW_DEVICES); + + final LinkDeviceRequest request = new LinkDeviceRequest("link-device-token", + deviceAttributes, + new DeviceActivationRequest(aciSignedPreKey, Optional.empty(), aciPqLastResortPreKey, Optional.empty(), Optional.empty(), Optional.empty())); + + try (final Response response = resources.getJerseyTest() + .target("/v1/devices/link") + .request() + .header("Authorization", AuthHelper.getProvisioningAuthHeader(AuthHelper.VALID_NUMBER, "password1")) + .put(Entity.entity(request, MediaType.APPLICATION_JSON_TYPE))) { + + assertEquals(411, response.getStatus()); + + final Map details = response.readEntity(Map.class); + assertEquals(Map.of("current", AccountsManager.MAX_DEVICES, "max", AccountsManager.MAX_DEVICES), details); + } + + verify(accountsManager).addDevice(eq(AuthHelper.VALID_UUID), any(), eq("link-device-token")); + } + @Test void linkDeviceAtomicWithVerificationTokenUsed() { @@ -538,7 +580,7 @@ class DeviceControllerTest { @SuppressWarnings("OptionalUsedAsFieldOrParameterType") void linkDeviceAtomicConflictingChannel(final boolean fetchesMessages, final Optional apnRegistrationId, - final Optional gcmRegistrationId) { + final Optional gcmRegistrationId) throws Exception { when(accountsManager.getByAccountIdentifier(AuthHelper.VALID_UUID)).thenReturn(Optional.of(account)); when(accountsManager.generateLinkDeviceToken(any())).thenReturn("test"); @@ -849,7 +891,7 @@ class DeviceControllerTest { @ParameterizedTest @MethodSource void linkDeviceRegistrationId(final int registrationId, final int pniRegistrationId, final int expectedStatusCode) - throws LinkDeviceTokenAlreadyUsedException { + throws LinkDeviceTokenAlreadyUsedException, DeviceLimitExceededException { final Device existingDevice = mock(Device.class); when(existingDevice.getId()).thenReturn(Device.PRIMARY_ID); when(account.getDevices()).thenReturn(List.of(existingDevice)); @@ -955,12 +997,9 @@ class DeviceControllerTest { } @Test - void maxDevicesTest() throws LinkDeviceTokenAlreadyUsedException { - final List devices = IntStream.range(0, DeviceController.MAX_DEVICES + 1) - .mapToObj(_ -> mock(Device.class)) - .toList(); + void generateTokenMaxDevicesTest() throws LinkDeviceTokenAlreadyUsedException, DeviceLimitExceededException { - when(account.getDevices()).thenReturn(devices); + when(accountsManager.generateLinkDeviceToken(any())).thenThrow(new DeviceLimitExceededException(5, 5)); Response response = resources.getJerseyTest() .target("/v1/devices/provisioning/code") diff --git a/service/src/test/java/org/whispersystems/textsecuregcm/storage/AccountsManagerTest.java b/service/src/test/java/org/whispersystems/textsecuregcm/storage/AccountsManagerTest.java index 15c61f974..5422d8a24 100644 --- a/service/src/test/java/org/whispersystems/textsecuregcm/storage/AccountsManagerTest.java +++ b/service/src/test/java/org/whispersystems/textsecuregcm/storage/AccountsManagerTest.java @@ -1223,7 +1223,7 @@ class AccountsManagerTest { @ParameterizedTest @ValueSource(booleans = {true, false}) - void testAddDevice(boolean accountHasPhoneNumber) throws LinkDeviceTokenAlreadyUsedException { + void testAddDevice(boolean accountHasPhoneNumber) throws LinkDeviceTokenAlreadyUsedException, DeviceLimitExceededException { final String phoneNumber = PhoneNumberUtil.getInstance().format(PhoneNumberUtil.getInstance().getExampleNumber("US"), PhoneNumberUtil.PhoneNumberFormat.E164); @@ -1269,7 +1269,7 @@ class AccountsManagerTest { true, Optional.empty(), Optional.empty()), - accountsManager.generateLinkDeviceToken(aci)); + accountsManager.generateLinkDeviceToken(account)); verify(keysManager).deleteSingleUsePreKeys(aci, nextDeviceId); maybePni.ifPresent(pni -> verify(keysManager).deleteSingleUsePreKeys(pni, nextDeviceId)); @@ -1298,6 +1298,55 @@ class AccountsManagerTest { assertNull(device.getGcmId()); } + @Test + void testAddDeviceExceedsMaxDevices() { + final List devices = IntStream.range(0, AccountsManager.MAX_DEVICES - 1) + .mapToObj(i -> { + final Device device = generateTestDevice(CLOCK.millis()); + device.setId((byte) (Device.PRIMARY_ID + i)); + return device; + }) + .toList(); + + final Account account = AccountsHelper.generateTestAccount("+14152222222", devices); + final UUID aci = account.getAccountIdentifier(); + account.setIdentityKey(new IdentityKey(ECKeyPair.generate().getPublicKey())); + + final String linkDeviceToken = assertDoesNotThrow(() -> accountsManager.generateLinkDeviceToken(account), + "the account should still have a free device slot"); + + // Simulate a concurrent link request + final Device concurrentlyLinkedDevice = generateTestDevice(CLOCK.millis()); + concurrentlyLinkedDevice.setId(account.getNextDeviceId()); + account.addDevice(concurrentlyLinkedDevice); + + when(accounts.getByAccountIdentifier(aci)).thenReturn(Optional.of(account)); + + final ECKeyPair aciKeyPair = ECKeyPair.generate(); + final ECKeyPair pniKeyPair = ECKeyPair.generate(); + + final DeviceLimitExceededException exception = assertThrows(DeviceLimitExceededException.class, + () -> accountsManager.addDevice(aci, + new DeviceSpec( + "device-name".getBytes(StandardCharsets.UTF_8), + "password", + "OWT", + Set.of(), + new DeviceIdentityInfo(17, KeysHelper.signedECPreKey(1, aciKeyPair), KeysHelper.signedKEMPreKey(3, aciKeyPair)), + Optional.of(new DeviceIdentityInfo(19, KeysHelper.signedECPreKey(2, pniKeyPair), KeysHelper.signedKEMPreKey(4, pniKeyPair))), + true, + Optional.empty(), + Optional.empty()), + linkDeviceToken)); + + assertEquals(AccountsManager.MAX_DEVICES, exception.getCurrentDevices()); + assertEquals(AccountsManager.MAX_DEVICES, exception.getMaxDevices()); + + verify(accounts, never()).updateTransactionally(any(), any()); + verifyNoInteractions(keysManager); + verifyNoInteractions(messagesManager); + } + @ParameterizedTest @MethodSource void testUpdateDeviceLastSeen(final boolean expectUpdate, final long initialLastSeen, final long updatedLastSeen) { @@ -1765,11 +1814,12 @@ class AccountsManagerTest { } @Test - void checkDeviceLinkingToken() { - final UUID aci = UUID.randomUUID(); + void checkDeviceLinkingToken() throws Exception { + final Account account = createAccount(new AccountAttributes(false, 1, 2, null, null, false, null, + TestRandomUtil.nextBytes(16))); - assertEquals(Optional.of(aci), - accountsManager.checkDeviceLinkingToken(accountsManager.generateLinkDeviceToken(aci))); + assertEquals(Optional.of(account.getAccountIdentifier()), + accountsManager.checkDeviceLinkingToken(accountsManager.generateLinkDeviceToken(account))); } @ParameterizedTest diff --git a/service/src/test/java/org/whispersystems/textsecuregcm/storage/AddRemoveDeviceIntegrationTest.java b/service/src/test/java/org/whispersystems/textsecuregcm/storage/AddRemoveDeviceIntegrationTest.java index 3a80fcddf..85bb29b00 100644 --- a/service/src/test/java/org/whispersystems/textsecuregcm/storage/AddRemoveDeviceIntegrationTest.java +++ b/service/src/test/java/org/whispersystems/textsecuregcm/storage/AddRemoveDeviceIntegrationTest.java @@ -163,12 +163,12 @@ public class AddRemoveDeviceIntegrationTest { } @AfterEach - void tearDown() throws InterruptedException { + void tearDown() { accountsManager.stop(); } @Test - void addDevice() throws LinkDeviceTokenAlreadyUsedException { + void addDevice() throws LinkDeviceTokenAlreadyUsedException, DeviceLimitExceededException { final String number = PhoneNumberUtil.getInstance().format( PhoneNumberUtil.getInstance().getExampleNumber("US"), PhoneNumberUtil.PhoneNumberFormat.E164); @@ -190,7 +190,7 @@ public class AddRemoveDeviceIntegrationTest { true, Optional.empty(), Optional.empty()), - accountsManager.generateLinkDeviceToken(account.getAccountIdentifier())); + accountsManager.generateLinkDeviceToken(account)); assertEquals(2, updatedAccountAndDevice.first().getDevices().size()); @@ -212,7 +212,7 @@ public class AddRemoveDeviceIntegrationTest { } @Test - void addDeviceReusedToken() throws LinkDeviceTokenAlreadyUsedException { + void addDeviceReusedToken() throws LinkDeviceTokenAlreadyUsedException, DeviceLimitExceededException { final String number = PhoneNumberUtil.getInstance().format( PhoneNumberUtil.getInstance().getExampleNumber("US"), PhoneNumberUtil.PhoneNumberFormat.E164); @@ -223,7 +223,7 @@ public class AddRemoveDeviceIntegrationTest { final Account account = AccountsHelper.createAccount(accountsManager, number); assertEquals(1, accountsManager.getByAccountIdentifier(account.getAccountIdentifier()).orElseThrow().getDevices().size()); - final String linkDeviceToken = accountsManager.generateLinkDeviceToken(account.getAccountIdentifier()); + final String linkDeviceToken = accountsManager.generateLinkDeviceToken(account); final Pair updatedAccountAndDevice = accountsManager.addDevice(account.getAccountIdentifier(), new DeviceSpec( @@ -261,7 +261,7 @@ public class AddRemoveDeviceIntegrationTest { } @Test - void removeDevice() throws LinkDeviceTokenAlreadyUsedException { + void removeDevice()throws LinkDeviceTokenAlreadyUsedException, DeviceLimitExceededException { final String number = PhoneNumberUtil.getInstance().format( PhoneNumberUtil.getInstance().getExampleNumber("US"), PhoneNumberUtil.PhoneNumberFormat.E164); @@ -283,7 +283,7 @@ public class AddRemoveDeviceIntegrationTest { true, Optional.empty(), Optional.empty()), - accountsManager.generateLinkDeviceToken(account.getAccountIdentifier())); + accountsManager.generateLinkDeviceToken(account)); final byte addedDeviceId = updatedAccountAndDevice.second().getId(); @@ -306,7 +306,7 @@ public class AddRemoveDeviceIntegrationTest { } @Test - void removeDevicePartialFailure() throws LinkDeviceTokenAlreadyUsedException { + void removeDevicePartialFailure() throws LinkDeviceTokenAlreadyUsedException, DeviceLimitExceededException { final String number = PhoneNumberUtil.getInstance().format( PhoneNumberUtil.getInstance().getExampleNumber("US"), PhoneNumberUtil.PhoneNumberFormat.E164); @@ -330,7 +330,7 @@ public class AddRemoveDeviceIntegrationTest { true, Optional.empty(), Optional.empty()), - accountsManager.generateLinkDeviceToken(account.getAccountIdentifier())); + accountsManager.generateLinkDeviceToken(account)); final byte addedDeviceId = updatedAccountAndDevice.second().getId(); @@ -360,7 +360,7 @@ public class AddRemoveDeviceIntegrationTest { } @Test - void waitForNewLinkedDevice() throws LinkDeviceTokenAlreadyUsedException { + void waitForNewLinkedDevice() throws LinkDeviceTokenAlreadyUsedException, DeviceLimitExceededException { final String number = PhoneNumberUtil.getInstance().format( PhoneNumberUtil.getInstance().getExampleNumber("US"), PhoneNumberUtil.PhoneNumberFormat.E164); @@ -370,7 +370,7 @@ public class AddRemoveDeviceIntegrationTest { final Account account = AccountsHelper.createAccount(accountsManager, number); - final String linkDeviceToken = accountsManager.generateLinkDeviceToken(account.getAccountIdentifier()); + final String linkDeviceToken = accountsManager.generateLinkDeviceToken(account); final String linkDeviceTokenIdentifier = AccountsManager.getLinkDeviceTokenIdentifier(linkDeviceToken); final CompletableFuture> displacedFuture = accountsManager.waitForNewLinkedDevice( @@ -409,7 +409,7 @@ public class AddRemoveDeviceIntegrationTest { } @Test - void waitForNewLinkedDeviceAlreadyAdded() throws LinkDeviceTokenAlreadyUsedException { + void waitForNewLinkedDeviceAlreadyAdded() throws LinkDeviceTokenAlreadyUsedException, DeviceLimitExceededException { final String number = PhoneNumberUtil.getInstance().format( PhoneNumberUtil.getInstance().getExampleNumber("US"), PhoneNumberUtil.PhoneNumberFormat.E164); @@ -419,7 +419,7 @@ public class AddRemoveDeviceIntegrationTest { final Account account = AccountsHelper.createAccount(accountsManager, number); - final String linkDeviceToken = accountsManager.generateLinkDeviceToken(account.getAccountIdentifier()); + final String linkDeviceToken = accountsManager.generateLinkDeviceToken(account); final String linkDeviceTokenIdentifier = AccountsManager.getLinkDeviceTokenIdentifier(linkDeviceToken); final Pair updatedAccountAndDevice = @@ -452,13 +452,13 @@ public class AddRemoveDeviceIntegrationTest { } @Test - void waitForNewLinkedDeviceTimeout() { + void waitForNewLinkedDeviceTimeout() throws Exception { final String number = PhoneNumberUtil.getInstance().format( PhoneNumberUtil.getInstance().getExampleNumber("US"), PhoneNumberUtil.PhoneNumberFormat.E164); final Account account = AccountsHelper.createAccount(accountsManager, number); - final String linkDeviceToken = accountsManager.generateLinkDeviceToken(UUID.randomUUID()); + final String linkDeviceToken = accountsManager.generateLinkDeviceToken(account); final String linkDeviceTokenIdentifier = AccountsManager.getLinkDeviceTokenIdentifier(linkDeviceToken); final CompletableFuture> linkedDeviceFuture = accountsManager.waitForNewLinkedDevice( @@ -477,7 +477,7 @@ public class AddRemoveDeviceIntegrationTest { "10_000,10_001,false", // pending message after now }) void waitForMessageFetch(long currentTime, Long oldestMessage, boolean shouldWait) - throws LinkDeviceTokenAlreadyUsedException { + throws LinkDeviceTokenAlreadyUsedException, DeviceLimitExceededException { final String number = PhoneNumberUtil.getInstance().format( PhoneNumberUtil.getInstance().getExampleNumber("US"), PhoneNumberUtil.PhoneNumberFormat.E164); @@ -485,7 +485,7 @@ public class AddRemoveDeviceIntegrationTest { final ECKeyPair pniKeyPair = ECKeyPair.generate(); final Account account = AccountsHelper.createAccount(accountsManager, number); - final String linkDeviceToken = accountsManager.generateLinkDeviceToken(UUID.randomUUID()); + final String linkDeviceToken = accountsManager.generateLinkDeviceToken(account); final String linkDeviceTokenIdentifier = AccountsManager.getLinkDeviceTokenIdentifier(linkDeviceToken); accountsManager.addDevice(account.getAccountIdentifier(), new DeviceSpec( @@ -514,7 +514,7 @@ public class AddRemoveDeviceIntegrationTest { // preempted by the timeout check @Timeout(value = 10, threadMode = Timeout.ThreadMode.SEPARATE_THREAD) @Test - void waitForMessageFetchRetries() throws LinkDeviceTokenAlreadyUsedException { + void waitForMessageFetchRetries() throws LinkDeviceTokenAlreadyUsedException, DeviceLimitExceededException { final String number = PhoneNumberUtil.getInstance().format( PhoneNumberUtil.getInstance().getExampleNumber("US"), PhoneNumberUtil.PhoneNumberFormat.E164); @@ -522,7 +522,7 @@ public class AddRemoveDeviceIntegrationTest { final ECKeyPair pniKeyPair = ECKeyPair.generate(); final Account account = AccountsHelper.createAccount(accountsManager, number); - final String linkDeviceToken = accountsManager.generateLinkDeviceToken(UUID.randomUUID()); + final String linkDeviceToken = accountsManager.generateLinkDeviceToken(account); final String linkDeviceTokenIdentifier = AccountsManager.getLinkDeviceTokenIdentifier(linkDeviceToken); clock.pin(Instant.ofEpochMilli(0));