Update `Account#getNextDeviceId` to not reuse disable device’s IDs
This commit is contained in:
parent
016141a05d
commit
23a076a204
|
@ -212,17 +212,13 @@ public class Account {
|
|||
public long getNextDeviceId() {
|
||||
requireNotStale();
|
||||
|
||||
long highestDevice = Device.MASTER_ID;
|
||||
long candidateId = Device.MASTER_ID + 1;
|
||||
|
||||
for (Device device : devices) {
|
||||
if (!device.isEnabled()) {
|
||||
return device.getId();
|
||||
} else if (device.getId() > highestDevice) {
|
||||
highestDevice = device.getId();
|
||||
}
|
||||
while (getDevice(candidateId).isPresent()) {
|
||||
candidateId++;
|
||||
}
|
||||
|
||||
return highestDevice + 1;
|
||||
return candidateId;
|
||||
}
|
||||
|
||||
public int getEnabledDeviceCount() {
|
||||
|
|
|
@ -12,6 +12,8 @@ import static org.junit.jupiter.api.Assertions.assertThrows;
|
|||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.whispersystems.textsecuregcm.tests.util.DevicesHelper.createDevice;
|
||||
import static org.whispersystems.textsecuregcm.tests.util.DevicesHelper.setEnabled;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Collections;
|
||||
|
@ -315,4 +317,29 @@ class AccountTest {
|
|||
assertThrows(AssertionError.class, account::getNumber);
|
||||
assertDoesNotThrow(account::getUuid);
|
||||
}
|
||||
|
||||
@Test
|
||||
void getNextDeviceId() {
|
||||
|
||||
final Set<Device> devices = new HashSet<>();
|
||||
devices.add(createDevice(Device.MASTER_ID));
|
||||
|
||||
final Account account = new Account("+14151234567", UUID.randomUUID(), devices, new byte[0]);
|
||||
|
||||
assertThat(account.getNextDeviceId()).isEqualTo(2L);
|
||||
|
||||
account.addDevice(createDevice(2L));
|
||||
|
||||
assertThat(account.getNextDeviceId()).isEqualTo(3L);
|
||||
|
||||
account.addDevice(createDevice(3L));
|
||||
|
||||
setEnabled(account.getDevice(2L).orElseThrow(), false);
|
||||
|
||||
assertThat(account.getNextDeviceId()).isEqualTo(4L);
|
||||
|
||||
account.removeDevice(2L);
|
||||
|
||||
assertThat(account.getNextDeviceId()).isEqualTo(2L);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue