mirror of
https://github.com/signalapp/Signal-Android.git
synced 2026-04-19 08:09:12 +01:00
Hmac-SIV encryption/decryption.
This commit is contained in:
committed by
Greyson Parrelli
parent
3907ec8b51
commit
7d70ea78cd
@@ -0,0 +1,20 @@
|
||||
package org.thoughtcrime.securesms.testutil;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonParser;
|
||||
import com.fasterxml.jackson.databind.DeserializationContext;
|
||||
import com.fasterxml.jackson.databind.JsonDeserializer;
|
||||
|
||||
import org.thoughtcrime.securesms.util.Hex;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* Jackson deserializes Json Strings to byte[] using Base64 by default, this allows Base16.
|
||||
*/
|
||||
public final class HexDeserializer extends JsonDeserializer<byte[]> {
|
||||
|
||||
@Override
|
||||
public byte[] deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
|
||||
return Hex.fromStringCondensed(p.getText());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user