Convert more tests to kotlin.

Resolves #13829
This commit is contained in:
Jameson Williams
2024-12-06 23:45:13 -06:00
committed by Greyson Parrelli
parent 574d6c51ab
commit c2aceb2bd1
12 changed files with 965 additions and 1038 deletions

View File

@@ -1,48 +0,0 @@
package org.thoughtcrime.securesms.testutil;
import org.mockito.ArgumentCaptor;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import java.security.SecureRandom;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;
public final class SecureRandomTestUtil {
private SecureRandomTestUtil() {
}
/**
* Creates a {@link SecureRandom} that returns exactly the {@param returnValue} the first time
* its {@link SecureRandom#nextBytes(byte[])}} method is called.
* <p>
* Any attempt to call with the incorrect length, or a second time will fail.
*/
public static SecureRandom mockRandom(byte[] returnValue) {
SecureRandom mock = mock(SecureRandom.class);
ArgumentCaptor<byte[]> argument = ArgumentCaptor.forClass(byte[].class);
doAnswer(new Answer<Void>() {
private int count;
@Override
public Void answer(InvocationOnMock invocation) {
assertEquals("SecureRandom Mock: nextBytes only expected to be called once", 1, ++count);
byte[] output = argument.getValue();
assertEquals("SecureRandom Mock: nextBytes byte[] length requested does not match byte[] setup", returnValue.length, output.length);
System.arraycopy(returnValue, 0, output, 0, returnValue.length);
return null;
}
}).when(mock).nextBytes(argument.capture());
return mock;
}
}

View File

@@ -0,0 +1,31 @@
package org.thoughtcrime.securesms.testutil
import io.mockk.every
import io.mockk.mockk
import io.mockk.slot
import org.junit.Assert.assertEquals
import java.security.SecureRandom
object SecureRandomTestUtil {
/**
* Creates a [SecureRandom] that returns exactly the {@param returnValue} the first time
* its [SecureRandom.nextBytes]} method is called.
*
* Any attempt to call with the incorrect length, or a second time will fail.
*/
@JvmStatic
fun mockRandom(returnValue: ByteArray): SecureRandom {
return mockk<SecureRandom> {
var count = 0
val slot = slot<ByteArray>()
every {
nextBytes(capture(slot))
} answers {
assertEquals("SecureRandom Mock: nextBytes only expected to be called once", 1, ++count)
val output = slot.captured
assertEquals("SecureRandom Mock: nextBytes byte[] length requested does not match byte[] setup", returnValue.size, output.size)
System.arraycopy(returnValue, 0, output, 0, returnValue.size)
}
}
}
}