mirror of
https://github.com/signalapp/Signal-Android.git
synced 2026-04-19 08:09:12 +01:00
committed by
Greyson Parrelli
parent
574d6c51ab
commit
c2aceb2bd1
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user