Search in sources :

Example 71 with PublicKey

use of com.quorum.tessera.encryption.PublicKey in project tessera by ConsenSys.

the class TransactionManagerTest method receiveNoTransactionInDatabase.

@Test
public void receiveNoTransactionInDatabase() {
    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);
    when(encryptedTransactionDAO.retrieveByHash(any(MessageHash.class))).thenReturn(Optional.empty());
    try {
        transactionManager.receive(receiveRequest);
        failBecauseExceptionWasNotThrown(TransactionNotFoundException.class);
    } catch (TransactionNotFoundException ex) {
        verify(encryptedTransactionDAO).retrieveByHash(any(MessageHash.class));
    }
}
Also used : PublicKey(com.quorum.tessera.encryption.PublicKey) TransactionNotFoundException(com.quorum.tessera.transaction.exception.TransactionNotFoundException) Test(org.junit.Test)

Example 72 with PublicKey

use of com.quorum.tessera.encryption.PublicKey in project tessera by ConsenSys.

the class TransactionManagerTest method sendSignedTransactionWithMandatoryRecipients.

@Test
public void sendSignedTransactionWithMandatoryRecipients() {
    EncodedPayload payload = mock(EncodedPayload.class);
    MessageHash hash = new MessageHash("HASH".getBytes());
    EncryptedRawTransaction encryptedRawTransaction = mock(EncryptedRawTransaction.class);
    when(encryptedRawTransaction.getHash()).thenReturn(hash);
    when(encryptedRawTransaction.toRawTransaction()).thenReturn(mock((RawTransaction.class)));
    when(encryptedRawTransactionDAO.retrieveByHash(eq(hash))).thenReturn(Optional.of(encryptedRawTransaction));
    when(payload.getCipherText()).thenReturn("ENCRYPTED_PAYLOAD".getBytes());
    when(enclave.encryptPayload(any(RawTransaction.class), any(), any())).thenReturn(payload);
    PublicKey receiver = PublicKey.from("RECEIVER".getBytes());
    when(enclave.getPublicKeys()).thenReturn(Set.of(receiver));
    SendSignedRequest sendSignedRequest = mock(SendSignedRequest.class);
    when(sendSignedRequest.getRecipients()).thenReturn(List.of(receiver));
    when(sendSignedRequest.getSignedData()).thenReturn("HASH".getBytes());
    when(sendSignedRequest.getPrivacyMode()).thenReturn(PrivacyMode.MANDATORY_RECIPIENTS);
    when(sendSignedRequest.getMandatoryRecipients()).thenReturn(Set.of(receiver));
    SendResponse result = transactionManager.sendSignedTransaction(sendSignedRequest);
    assertThat(result).isNotNull();
    assertThat(result.getTransactionHash()).isEqualTo(new MessageHash("HASH".getBytes()));
    assertThat(result.getManagedParties()).containsExactly(receiver);
    ArgumentCaptor<PrivacyMetadata> data = ArgumentCaptor.forClass(PrivacyMetadata.class);
    verify(enclave).encryptPayload(any(RawTransaction.class), any(), data.capture());
    verify(encryptedTransactionDAO).save(any(EncryptedTransaction.class), any(Callable.class));
    verify(encryptedRawTransactionDAO).retrieveByHash(any(MessageHash.class));
    verify(enclave).getForwardingKeys();
    verify(enclave).getPublicKeys();
    final PrivacyMetadata passingData = data.getValue();
    assertThat(passingData.getPrivacyMode()).isEqualTo(PrivacyMode.MANDATORY_RECIPIENTS);
    assertThat(passingData.getPrivacyGroupId()).isNotPresent();
    assertThat(passingData.getMandatoryRecipients()).containsExactly(receiver);
}
Also used : PublicKey(com.quorum.tessera.encryption.PublicKey) Callable(java.util.concurrent.Callable) Test(org.junit.Test)

Example 73 with PublicKey

use of com.quorum.tessera.encryption.PublicKey in project tessera by ConsenSys.

the class TransactionManagerTest method receiveWithRecipientsPresent.

@Test
public void receiveWithRecipientsPresent() {
    final PublicKey sender = PublicKey.from("sender".getBytes());
    final PublicKey recipient1 = PublicKey.from("recipient1".getBytes());
    final PublicKey recipient2 = PublicKey.from("recipient2".getBytes());
    MessageHash messageHash = mock(MessageHash.class);
    ReceiveRequest receiveRequest = mock(ReceiveRequest.class);
    when(receiveRequest.getRecipient()).thenReturn(Optional.of(mock(PublicKey.class)));
    when(receiveRequest.getTransactionHash()).thenReturn(messageHash);
    EncodedPayload payload = mock(EncodedPayload.class);
    when(payload.getExecHash()).thenReturn("execHash".getBytes());
    when(payload.getPrivacyMode()).thenReturn(PrivacyMode.STANDARD_PRIVATE);
    when(payload.getRecipientKeys()).thenReturn(List.of(recipient1, recipient2));
    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(eq(payload), any(PublicKey.class))).thenReturn(expectedOutcome);
    when(enclave.getPublicKeys()).thenReturn(Set.of(recipient1, recipient2));
    ReceiveResponse receiveResponse = transactionManager.receive(receiveRequest);
    assertThat(receiveResponse).isNotNull();
    assertThat(receiveResponse.getUnencryptedTransactionData()).isEqualTo(expectedOutcome);
    assertThat(receiveResponse.getManagedParties()).containsExactlyInAnyOrder(recipient1, recipient2);
    assertThat(receiveResponse.sender()).isEqualTo(sender);
    verify(encryptedTransactionDAO).retrieveByHash(any(MessageHash.class));
    verify(enclave, times(2)).unencryptTransaction(any(EncodedPayload.class), any(PublicKey.class));
    verify(enclave, times(2)).getPublicKeys();
}
Also used : PublicKey(com.quorum.tessera.encryption.PublicKey) Test(org.junit.Test)

Example 74 with PublicKey

use of com.quorum.tessera.encryption.PublicKey in project tessera by ConsenSys.

the class TransactionManagerTest method receiveWithMandatoryRecipients.

@Test
public void receiveWithMandatoryRecipients() {
    PublicKey sender = PublicKey.from("sender".getBytes());
    byte[] randomData = Base64.getEncoder().encode("odd-data".getBytes());
    MessageHash messageHash = mock(MessageHash.class);
    ReceiveRequest receiveRequest = mock(ReceiveRequest.class);
    when(receiveRequest.getRecipient()).thenReturn(Optional.of(mock(PublicKey.class)));
    when(receiveRequest.getTransactionHash()).thenReturn(messageHash);
    PublicKey mandReceiver1 = PublicKey.from("mandatory1".getBytes());
    PublicKey mandReceiver2 = PublicKey.from("mandatory2".getBytes());
    EncodedPayload payload = mock(EncodedPayload.class);
    when(payload.getMandatoryRecipients()).thenReturn(Set.of(mandReceiver1, mandReceiver2));
    when(payload.getPrivacyMode()).thenReturn(PrivacyMode.MANDATORY_RECIPIENTS);
    when(payload.getSenderKey()).thenReturn(sender);
    when(payload.getPrivacyGroupId()).thenReturn(Optional.of(PrivacyGroup.Id.fromBytes("group".getBytes())));
    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(eq(payload), 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()).isPresent();
    assertThat(receiveResponse.getPrivacyGroupId().get()).isEqualTo(PrivacyGroup.Id.fromBytes("group".getBytes()));
    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 75 with PublicKey

use of com.quorum.tessera.encryption.PublicKey in project tessera by ConsenSys.

the class TransactionManagerTest method receiveWithNoRecipientsPresent.

@Test
public void receiveWithNoRecipientsPresent() {
    final PublicKey sender = PublicKey.from("sender".getBytes());
    final PublicKey recipient1 = PublicKey.from("recipient1".getBytes());
    MessageHash messageHash = mock(MessageHash.class);
    ReceiveRequest receiveRequest = mock(ReceiveRequest.class);
    when(receiveRequest.getRecipient()).thenReturn(Optional.of(mock(PublicKey.class)));
    when(receiveRequest.getTransactionHash()).thenReturn(messageHash);
    EncodedPayload payload = mock(EncodedPayload.class);
    when(payload.getSenderKey()).thenReturn(sender);
    when(payload.getPrivacyMode()).thenReturn(PrivacyMode.STANDARD_PRIVATE);
    when(payload.getRecipientBoxes()).thenReturn(List.of(RecipientBox.from("box1".getBytes())));
    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(eq(payload), any(PublicKey.class))).thenReturn(expectedOutcome);
    when(enclave.getPublicKeys()).thenReturn(Set.of(recipient1));
    ReceiveResponse receiveResponse = transactionManager.receive(receiveRequest);
    assertThat(receiveResponse).isNotNull();
    assertThat(receiveResponse.getUnencryptedTransactionData()).isEqualTo(expectedOutcome);
    assertThat(receiveResponse.getManagedParties()).containsExactly(recipient1);
    assertThat(receiveResponse.sender()).isEqualTo(sender);
    verify(encryptedTransactionDAO).retrieveByHash(any(MessageHash.class));
    verify(enclave, times(3)).unencryptTransaction(any(EncodedPayload.class), any(PublicKey.class));
    verify(enclave, times(2)).getPublicKeys();
}
Also used : PublicKey(com.quorum.tessera.encryption.PublicKey) 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