use of com.quorum.tessera.encryption.PublicKey in project tessera by ConsenSys.
the class TransactionManagerTest method receive.
@Test
public void receive() {
PublicKey sender = PublicKey.from("sender".getBytes());
PublicKey recipient = PublicKey.from("recipient".getBytes());
MessageHash messageHash = new MessageHash("hash".getBytes());
ReceiveRequest receiveRequest = mock(ReceiveRequest.class);
when(receiveRequest.getRecipient()).thenReturn(Optional.of(recipient));
when(receiveRequest.getTransactionHash()).thenReturn(messageHash);
EncodedPayload payload = mock(EncodedPayload.class);
when(payload.getExecHash()).thenReturn("execHash".getBytes());
when(payload.getPrivacyMode()).thenReturn(PrivacyMode.PRIVATE_STATE_VALIDATION);
when(payload.getSenderKey()).thenReturn(sender);
EncryptedTransaction encryptedTransaction = mock(EncryptedTransaction.class);
when(encryptedTransaction.getHash()).thenReturn(messageHash);
when(encryptedTransaction.getPayload()).thenReturn(payload);
when(encryptedTransactionDAO.retrieveByHash(eq(messageHash))).thenReturn(Optional.of(encryptedTransaction));
byte[] expectedOutcome = "Encrypted payload".getBytes();
when(enclave.unencryptTransaction(any(EncodedPayload.class), any(PublicKey.class))).thenReturn(expectedOutcome);
PublicKey publicKey = mock(PublicKey.class);
when(enclave.getPublicKeys()).thenReturn(Set.of(publicKey));
ReceiveResponse receiveResponse = transactionManager.receive(receiveRequest);
assertThat(receiveResponse).isNotNull();
assertThat(receiveResponse.sender()).isEqualTo(sender);
assertThat(receiveResponse.getUnencryptedTransactionData()).isEqualTo(expectedOutcome);
assertThat(receiveResponse.getPrivacyGroupId()).isNotPresent();
verify(encryptedTransactionDAO).retrieveByHash(any(MessageHash.class));
verify(enclave, times(2)).unencryptTransaction(any(EncodedPayload.class), any(PublicKey.class));
verify(enclave).getPublicKeys();
}
use of com.quorum.tessera.encryption.PublicKey in project tessera by ConsenSys.
the class TransactionManagerTest method storePayloadWhenWeAreSender.
@Test
public void storePayloadWhenWeAreSender() {
final PublicKey senderKey = PublicKey.from("SENDER".getBytes());
final EncodedPayload encodedPayload = mock(EncodedPayload.class);
when(encodedPayload.getSenderKey()).thenReturn(senderKey);
when(encodedPayload.getCipherText()).thenReturn("CIPHERTEXT".getBytes());
when(encodedPayload.getRecipientBoxes()).thenReturn(new ArrayList<>());
when(encodedPayload.getRecipientKeys()).thenReturn(new ArrayList<>());
when(enclave.getPublicKeys()).thenReturn(singleton(senderKey));
transactionManager.storePayload(encodedPayload);
verify(resendManager).acceptOwnMessage(encodedPayload);
verify(enclave).getPublicKeys();
verify(enclave).findInvalidSecurityHashes(any(EncodedPayload.class), anyList());
}
use of com.quorum.tessera.encryption.PublicKey in project tessera by ConsenSys.
the class TransactionManagerTest method receiveRawTransaction.
@Test
public void receiveRawTransaction() {
byte[] keyData = Base64.getEncoder().encode("KEY".getBytes());
PublicKey recipient = PublicKey.from("recipient".getBytes());
MessageHash messageHash = new MessageHash(Base64.getDecoder().decode(keyData));
ReceiveRequest receiveRequest = mock(ReceiveRequest.class);
when(receiveRequest.getRecipient()).thenReturn(Optional.of(recipient));
when(receiveRequest.getTransactionHash()).thenReturn(messageHash);
when(receiveRequest.isRaw()).thenReturn(true);
EncryptedRawTransaction encryptedTransaction = mock(EncryptedRawTransaction.class);
when(encryptedTransaction.getEncryptedPayload()).thenReturn("payload".getBytes());
when(encryptedTransaction.getEncryptedKey()).thenReturn("key".getBytes());
when(encryptedTransaction.getNonce()).thenReturn("nonce".getBytes());
when(encryptedTransaction.getSender()).thenReturn("sender".getBytes());
when(encryptedRawTransactionDAO.retrieveByHash(messageHash)).thenReturn(Optional.of(encryptedTransaction));
when(enclave.unencryptRawPayload(any(RawTransaction.class))).thenReturn("response".getBytes());
ReceiveResponse response = transactionManager.receive(receiveRequest);
assertThat(response.getUnencryptedTransactionData()).isEqualTo("response".getBytes());
verify(enclave).unencryptRawPayload(any(RawTransaction.class));
}
use of com.quorum.tessera.encryption.PublicKey in project tessera by ConsenSys.
the class TransactionManagerTest method receiveNoRecipientKeyFound.
@Test
public void receiveNoRecipientKeyFound() {
PublicKey recipient = PublicKey.from("recipient".getBytes());
MessageHash messageHash = mock(MessageHash.class);
when(messageHash.getHashBytes()).thenReturn("KEY".getBytes());
ReceiveRequest receiveRequest = mock(ReceiveRequest.class);
when(receiveRequest.getTransactionHash()).thenReturn(messageHash);
when(receiveRequest.getRecipient()).thenReturn(Optional.of(recipient));
EncodedPayload payload = mock(EncodedPayload.class);
EncryptedTransaction encryptedTransaction = mock(EncryptedTransaction.class);
when(encryptedTransaction.getPayload()).thenReturn(payload);
when(encryptedTransaction.getHash()).thenReturn(messageHash);
when(encryptedTransactionDAO.retrieveByHash(eq(messageHash))).thenReturn(Optional.of(encryptedTransaction));
PublicKey publicKey = mock(PublicKey.class);
when(enclave.getPublicKeys()).thenReturn(Collections.singleton(publicKey));
when(enclave.unencryptTransaction(eq(payload), any(PublicKey.class))).thenThrow(EncryptorException.class);
try {
transactionManager.receive(receiveRequest);
failBecauseExceptionWasNotThrown(RecipientKeyNotFoundException.class);
} catch (RecipientKeyNotFoundException ex) {
verify(encryptedTransactionDAO).retrieveByHash(any(MessageHash.class));
verify(enclave).getPublicKeys();
verify(enclave).unencryptTransaction(any(EncodedPayload.class), any(PublicKey.class));
}
}
use of com.quorum.tessera.encryption.PublicKey in project tessera by ConsenSys.
the class TransactionManagerTest method sendWithDuplicateRecipients.
@Test
public void sendWithDuplicateRecipients() {
EncodedPayload encodedPayload = mock(EncodedPayload.class);
when(encodedPayload.getCipherText()).thenReturn("CIPHERTEXT".getBytes());
when(enclave.encryptPayload(any(), any(), any(), any())).thenReturn(encodedPayload);
when(enclave.getForwardingKeys()).thenReturn(Set.of(PublicKey.from("RECEIVER".getBytes())));
PublicKey sender = PublicKey.from("SENDER".getBytes());
PublicKey receiver = PublicKey.from("RECEIVER".getBytes());
byte[] payload = Base64.getEncoder().encode("PAYLOAD".getBytes());
SendRequest sendRequest = mock(SendRequest.class);
when(sendRequest.getPayload()).thenReturn(payload);
when(sendRequest.getSender()).thenReturn(sender);
when(sendRequest.getRecipients()).thenReturn(List.of(receiver));
when(sendRequest.getPrivacyMode()).thenReturn(PrivacyMode.STANDARD_PRIVATE);
SendResponse result = transactionManager.send(sendRequest);
assertThat(result).isNotNull();
assertThat(result.getTransactionHash().toString()).isEqualTo("Q0lQSEVSVEVYVA==");
assertThat(result.getManagedParties()).isEmpty();
verify(enclave).encryptPayload(any(), any(), any(), any());
verify(encryptedTransactionDAO).save(any(EncryptedTransaction.class), any(Callable.class));
verify(enclave).getForwardingKeys();
verify(enclave).getPublicKeys();
}
Aggregations