Avoid generating invalid deviceId in unit test

This commit is contained in:
Ravi Khadiwala 2025-06-03 11:15:45 -05:00 committed by ravi-signal
parent 1767586797
commit aef7f3fef8
1 changed files with 12 additions and 8 deletions

View File

@ -47,7 +47,7 @@ class NoiseAuthenticatedHandlerTest extends AbstractNoiseHandlerTest {
@Override @Override
protected CipherStatePair doHandshake() throws Throwable { protected CipherStatePair doHandshake() throws Throwable {
final UUID accountIdentifier = UUID.randomUUID(); final UUID accountIdentifier = UUID.randomUUID();
final byte deviceId = (byte) ThreadLocalRandom.current().nextInt(1, Device.MAXIMUM_DEVICE_ID + 1); final byte deviceId = randomDeviceId();
when(clientPublicKeysManager.findPublicKey(accountIdentifier, deviceId)) when(clientPublicKeysManager.findPublicKey(accountIdentifier, deviceId))
.thenReturn(CompletableFuture.completedFuture(Optional.of(clientKeyPair.getPublicKey()))); .thenReturn(CompletableFuture.completedFuture(Optional.of(clientKeyPair.getPublicKey())));
return doHandshake(identityPayload(accountIdentifier, deviceId)); return doHandshake(identityPayload(accountIdentifier, deviceId));
@ -60,7 +60,7 @@ class NoiseAuthenticatedHandlerTest extends AbstractNoiseHandlerTest {
assertNotNull(embeddedChannel.pipeline().get(NoiseHandshakeHandler.class)); assertNotNull(embeddedChannel.pipeline().get(NoiseHandshakeHandler.class));
final UUID accountIdentifier = UUID.randomUUID(); final UUID accountIdentifier = UUID.randomUUID();
final byte deviceId = (byte) ThreadLocalRandom.current().nextInt(Device.MAXIMUM_DEVICE_ID); final byte deviceId = randomDeviceId();
when(clientPublicKeysManager.findPublicKey(accountIdentifier, deviceId)) when(clientPublicKeysManager.findPublicKey(accountIdentifier, deviceId))
.thenReturn(CompletableFuture.completedFuture(Optional.of(clientKeyPair.getPublicKey()))); .thenReturn(CompletableFuture.completedFuture(Optional.of(clientKeyPair.getPublicKey())));
@ -81,7 +81,7 @@ class NoiseAuthenticatedHandlerTest extends AbstractNoiseHandlerTest {
assertNotNull(embeddedChannel.pipeline().get(NoiseHandshakeHandler.class)); assertNotNull(embeddedChannel.pipeline().get(NoiseHandshakeHandler.class));
final UUID accountIdentifier = UUID.randomUUID(); final UUID accountIdentifier = UUID.randomUUID();
final byte deviceId = (byte) ThreadLocalRandom.current().nextInt(Device.MAXIMUM_DEVICE_ID); final byte deviceId = randomDeviceId();
when(clientPublicKeysManager.findPublicKey(accountIdentifier, deviceId)) when(clientPublicKeysManager.findPublicKey(accountIdentifier, deviceId))
.thenReturn(CompletableFuture.completedFuture(Optional.of(clientKeyPair.getPublicKey()))); .thenReturn(CompletableFuture.completedFuture(Optional.of(clientKeyPair.getPublicKey())));
@ -149,7 +149,7 @@ class NoiseAuthenticatedHandlerTest extends AbstractNoiseHandlerTest {
assertNotNull(embeddedChannel.pipeline().get(NoiseHandshakeHandler.class)); assertNotNull(embeddedChannel.pipeline().get(NoiseHandshakeHandler.class));
final UUID accountIdentifier = UUID.randomUUID(); final UUID accountIdentifier = UUID.randomUUID();
final byte deviceId = (byte) ThreadLocalRandom.current().nextInt(Device.MAXIMUM_DEVICE_ID); final byte deviceId = randomDeviceId();
when(clientPublicKeysManager.findPublicKey(accountIdentifier, deviceId)) when(clientPublicKeysManager.findPublicKey(accountIdentifier, deviceId))
.thenReturn(CompletableFuture.completedFuture(Optional.empty())); .thenReturn(CompletableFuture.completedFuture(Optional.empty()));
@ -174,7 +174,7 @@ class NoiseAuthenticatedHandlerTest extends AbstractNoiseHandlerTest {
assertNotNull(embeddedChannel.pipeline().get(NoiseHandshakeHandler.class)); assertNotNull(embeddedChannel.pipeline().get(NoiseHandshakeHandler.class));
final UUID accountIdentifier = UUID.randomUUID(); final UUID accountIdentifier = UUID.randomUUID();
final byte deviceId = (byte) ThreadLocalRandom.current().nextInt(Device.MAXIMUM_DEVICE_ID); final byte deviceId = randomDeviceId();
when(clientPublicKeysManager.findPublicKey(accountIdentifier, deviceId)) when(clientPublicKeysManager.findPublicKey(accountIdentifier, deviceId))
.thenReturn(CompletableFuture.completedFuture(Optional.of(Curve.generateKeyPair().getPublicKey()))); .thenReturn(CompletableFuture.completedFuture(Optional.of(Curve.generateKeyPair().getPublicKey())));
@ -196,7 +196,7 @@ class NoiseAuthenticatedHandlerTest extends AbstractNoiseHandlerTest {
assertNotNull(embeddedChannel.pipeline().get(NoiseHandshakeHandler.class)); assertNotNull(embeddedChannel.pipeline().get(NoiseHandshakeHandler.class));
final UUID accountIdentifier = UUID.randomUUID(); final UUID accountIdentifier = UUID.randomUUID();
final byte deviceId = (byte) ThreadLocalRandom.current().nextInt(Device.MAXIMUM_DEVICE_ID); final byte deviceId = randomDeviceId();
final HandshakeState clientHandshakeState = clientHandshakeState(); final HandshakeState clientHandshakeState = clientHandshakeState();
@ -228,9 +228,9 @@ class NoiseAuthenticatedHandlerTest extends AbstractNoiseHandlerTest {
} }
@Test @Test
public void handleKeyLookupError() throws Throwable { public void handleKeyLookupError() {
final UUID accountIdentifier = UUID.randomUUID(); final UUID accountIdentifier = UUID.randomUUID();
final byte deviceId = (byte) ThreadLocalRandom.current().nextInt(Device.MAXIMUM_DEVICE_ID); final byte deviceId = randomDeviceId();
when(clientPublicKeysManager.findPublicKey(accountIdentifier, deviceId)) when(clientPublicKeysManager.findPublicKey(accountIdentifier, deviceId))
.thenReturn(CompletableFuture.failedFuture(new IOException())); .thenReturn(CompletableFuture.failedFuture(new IOException()));
assertThrows(IOException.class, () -> doHandshake(identityPayload(accountIdentifier, deviceId))); assertThrows(IOException.class, () -> doHandshake(identityPayload(accountIdentifier, deviceId)));
@ -332,4 +332,8 @@ class NoiseAuthenticatedHandlerTest extends AbstractNoiseHandlerTest {
.build() .build()
.toByteArray(); .toByteArray();
} }
private static byte randomDeviceId() {
return (byte) ThreadLocalRandom.current().nextInt(1, Device.MAXIMUM_DEVICE_ID + 1);
}
} }