Search in sources :

Example 66 with PublicKey

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();
}
Also used : PublicKey(com.quorum.tessera.encryption.PublicKey) Test(org.junit.Test)

Example 67 with PublicKey

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());
}
Also used : PublicKey(com.quorum.tessera.encryption.PublicKey) Test(org.junit.Test)

Example 68 with PublicKey

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));
}
Also used : PublicKey(com.quorum.tessera.encryption.PublicKey) Test(org.junit.Test)

Example 69 with PublicKey

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));
    }
}
Also used : PublicKey(com.quorum.tessera.encryption.PublicKey) RecipientKeyNotFoundException(com.quorum.tessera.transaction.exception.RecipientKeyNotFoundException) Test(org.junit.Test)

Example 70 with PublicKey

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();
}
Also used : PublicKey(com.quorum.tessera.encryption.PublicKey) Callable(java.util.concurrent.Callable) Test(org.junit.Test)

Aggregations

PublicKey (com.quorum.tessera.encryption.PublicKey)281 Test (org.junit.Test)213 Response (jakarta.ws.rs.core.Response)59 MessageHash (com.quorum.tessera.data.MessageHash)57 EncodedPayload (com.quorum.tessera.enclave.EncodedPayload)48 Collectors (java.util.stream.Collectors)32 PrivacyGroup (com.quorum.tessera.enclave.PrivacyGroup)28 NodeInfo (com.quorum.tessera.partyinfo.node.NodeInfo)25 java.util (java.util)23 SendResponse (com.quorum.tessera.api.SendResponse)21 Nonce (com.quorum.tessera.encryption.Nonce)20 Recipient (com.quorum.tessera.partyinfo.node.Recipient)20 Operation (io.swagger.v3.oas.annotations.Operation)20 ApiResponse (io.swagger.v3.oas.annotations.responses.ApiResponse)20 Stream (java.util.stream.Stream)19 ReceiveResponse (com.quorum.tessera.transaction.ReceiveResponse)18 EncryptedTransaction (com.quorum.tessera.data.EncryptedTransaction)17 PrivacyMode (com.quorum.tessera.enclave.PrivacyMode)17 URI (java.net.URI)17 SendRequest (com.quorum.tessera.api.SendRequest)15