mirror of
https://github.com/signalapp/Signal-Server
synced 2026-04-21 20:18:05 +01:00
Add command to remove expired linked devices
This commit is contained in:
@@ -6,14 +6,14 @@
|
||||
package org.whispersystems.textsecuregcm.storage;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.util.stream.Stream;
|
||||
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.MethodSource;
|
||||
import org.whispersystems.textsecuregcm.entities.ECSignedPreKey;
|
||||
|
||||
class DeviceTest {
|
||||
|
||||
@@ -56,4 +56,33 @@ class DeviceTest {
|
||||
Arguments.of(false, true, null, null, Duration.ofDays(1), true)
|
||||
);
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource({
|
||||
"true, P1D, false",
|
||||
"true, P30D, false",
|
||||
"true, P31D, false",
|
||||
"true, P180D, false",
|
||||
"true, P181D, true",
|
||||
"false, P1D, false",
|
||||
"false, P30D, false",
|
||||
"false, P31D, true",
|
||||
"false, P180D, true",
|
||||
})
|
||||
public void testIsExpired(final boolean primary, final Duration timeSinceLastSeen, final boolean expectExpired) {
|
||||
|
||||
final long lastSeen = Instant.now()
|
||||
.minus(timeSinceLastSeen)
|
||||
// buffer for test runtime
|
||||
.plusSeconds(1)
|
||||
.toEpochMilli();
|
||||
|
||||
final Device device = new Device();
|
||||
device.setId(primary ? Device.PRIMARY_ID : Device.PRIMARY_ID + 1);
|
||||
device.setCreated(lastSeen);
|
||||
device.setLastSeen(lastSeen);
|
||||
|
||||
assertEquals(expectExpired, device.isExpired());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
* Copyright 2023 Signal Messenger, LLC
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
|
||||
package org.whispersystems.textsecuregcm.workers;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Stream;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.Arguments;
|
||||
import org.junit.jupiter.params.provider.MethodSource;
|
||||
import org.whispersystems.textsecuregcm.storage.Device;
|
||||
|
||||
class RemoveExpiredLinkedDevicesCommandTest {
|
||||
|
||||
public static Stream<Arguments> getDeviceIdsToRemove() {
|
||||
final Device primary = device(Device.PRIMARY_ID, false);
|
||||
|
||||
final byte expiredDevice2Id = 2;
|
||||
final Device expiredDevice2 = device(expiredDevice2Id, true);
|
||||
|
||||
final byte deviceId3 = 3;
|
||||
final Device device3 = device(deviceId3, false);
|
||||
|
||||
final Device expiredPrimary = device(Device.PRIMARY_ID, true);
|
||||
|
||||
return Stream.of(
|
||||
Arguments.of(List.of(primary), Set.of()),
|
||||
Arguments.of(List.of(primary, expiredDevice2), Set.of(expiredDevice2Id)),
|
||||
Arguments.of(List.of(primary, expiredDevice2, device3), Set.of(expiredDevice2Id)),
|
||||
Arguments.of(List.of(expiredPrimary, expiredDevice2, device3), Set.of(expiredDevice2Id))
|
||||
);
|
||||
}
|
||||
|
||||
private static Device device(byte id, boolean expired) {
|
||||
final Device device = mock(Device.class);
|
||||
when(device.getId()).thenReturn(id);
|
||||
when(device.isExpired()).thenReturn(expired);
|
||||
when(device.isPrimary()).thenCallRealMethod();
|
||||
return device;
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource
|
||||
void getDeviceIdsToRemove(final List<Device> devices, final Set<Byte> expectedIds) {
|
||||
assertEquals(expectedIds, RemoveExpiredLinkedDevicesCommand.getExpiredLinkedDeviceIds(devices));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user