Search in sources :

Example 51 with EncodedPayload

use of com.quorum.tessera.enclave.EncodedPayload in project tessera by ConsenSys.

the class PreparePayloadForRecipientTest method psvTransactionOnlyUsesKeysWithBoxes.

@Test
public void psvTransactionOnlyUsesKeysWithBoxes() {
    final PublicKey targetResendKey = PublicKey.from("target".getBytes());
    final PublicKey recipient1 = PublicKey.from("recipient1".getBytes());
    final PublicKey recipient2 = PublicKey.from("recipient2".getBytes());
    final EncodedPayload payload = mock(EncodedPayload.class);
    when(payload.getSenderKey()).thenReturn(targetResendKey);
    when(payload.getPrivacyMode()).thenReturn(PrivacyMode.PRIVATE_STATE_VALIDATION);
    when(payload.getRecipientKeys()).thenReturn(List.of(recipient1, recipient2));
    when(payload.getRecipientBoxes()).thenReturn(List.of(RecipientBox.from("box1".getBytes())));
    final BatchWorkflowContext workflowEvent = new BatchWorkflowContext();
    workflowEvent.setEncodedPayload(payload);
    workflowEvent.setRecipientKey(targetResendKey);
    try (var mockStatic = mockStatic(EncodedPayload.Builder.class)) {
        EncodedPayload.Builder builder1 = mock(EncodedPayload.Builder.class);
        when(builder1.build()).thenReturn(mock(EncodedPayload.class));
        EncodedPayload.Builder builder2 = mock(EncodedPayload.Builder.class);
        when(builder2.build()).thenReturn(mock(EncodedPayload.class));
        mockStatic.when(() -> EncodedPayload.Builder.forRecipient(payload, recipient1)).thenReturn(builder1);
        mockStatic.when(() -> EncodedPayload.Builder.forRecipient(payload, recipient2)).thenReturn(builder2);
        preparePayloadForRecipient.execute(workflowEvent);
        mockStatic.verify(() -> EncodedPayload.Builder.forRecipient(payload, recipient1));
        verify(builder1).build();
        verifyNoMoreInteractions(builder1, builder2);
    }
    final Set<EncodedPayload> payloadsToPublish = workflowEvent.getPayloadsToPublish();
    assertThat(payloadsToPublish).hasSize(1);
}
Also used : PublicKey(com.quorum.tessera.encryption.PublicKey) EncodedPayload(com.quorum.tessera.enclave.EncodedPayload) Test(org.junit.Test)

Example 52 with EncodedPayload

use of com.quorum.tessera.enclave.EncodedPayload in project tessera by ConsenSys.

the class PreparePayloadForRecipientTest method targetKeyIsSenderOfTransactionWithRecipientsPresent.

@Test
public void targetKeyIsSenderOfTransactionWithRecipientsPresent() {
    final PublicKey targetResendKey = PublicKey.from("target".getBytes());
    final PublicKey recipient1 = PublicKey.from("recipient1".getBytes());
    final PublicKey recipient2 = PublicKey.from("recipient2".getBytes());
    final EncodedPayload payload = mock(EncodedPayload.class);
    when(payload.getSenderKey()).thenReturn(targetResendKey);
    when(payload.getRecipientKeys()).thenReturn(List.of(recipient1, recipient2));
    when(payload.getRecipientBoxes()).thenReturn(List.of(mock(RecipientBox.class), mock(RecipientBox.class)));
    final BatchWorkflowContext workflowEvent = new BatchWorkflowContext();
    workflowEvent.setEncodedPayload(payload);
    workflowEvent.setRecipientKey(targetResendKey);
    try (var mockStatic = mockStatic(EncodedPayload.Builder.class)) {
        EncodedPayload.Builder builder1 = mock(EncodedPayload.Builder.class);
        when(builder1.build()).thenReturn(mock(EncodedPayload.class));
        EncodedPayload.Builder builder2 = mock(EncodedPayload.Builder.class);
        when(builder2.build()).thenReturn(mock(EncodedPayload.class));
        mockStatic.when(() -> EncodedPayload.Builder.forRecipient(payload, recipient1)).thenReturn(builder1);
        mockStatic.when(() -> EncodedPayload.Builder.forRecipient(payload, recipient2)).thenReturn(builder2);
        preparePayloadForRecipient.execute(workflowEvent);
        mockStatic.verify(() -> EncodedPayload.Builder.forRecipient(payload, recipient1));
        mockStatic.verify(() -> EncodedPayload.Builder.forRecipient(payload, recipient2));
        verify(builder1).build();
        verify(builder2).build();
    }
    final Set<EncodedPayload> payloadsToPublish = workflowEvent.getPayloadsToPublish();
    assertThat(payloadsToPublish).hasSize(2);
}
Also used : PublicKey(com.quorum.tessera.encryption.PublicKey) EncodedPayload(com.quorum.tessera.enclave.EncodedPayload) Test(org.junit.Test)

Example 53 with EncodedPayload

use of com.quorum.tessera.enclave.EncodedPayload in project tessera by ConsenSys.

the class SearchRecipientKeyForPayloadTest method execute.

@Test
public void execute() {
    final BatchWorkflowContext workflowEvent = new BatchWorkflowContext();
    final EncryptedTransaction encryptedTransaction = new EncryptedTransaction();
    encryptedTransaction.setHash(new MessageHash("sampleHash".getBytes()));
    workflowEvent.setEncryptedTransaction(encryptedTransaction);
    final EncodedPayload encodedPayloadForRecipient1 = EncodedPayload.Builder.create().withRecipientBox("sample-box-1".getBytes()).build();
    final EncodedPayload encodedPayloadForRecipient2 = EncodedPayload.Builder.create().withRecipientBox("sample-box-2".getBytes()).build();
    workflowEvent.setPayloadsToPublish(Set.of(encodedPayloadForRecipient1, encodedPayloadForRecipient2));
    final PublicKey recipient1 = PublicKey.from("sample-public-key-1".getBytes());
    final PublicKey recipient2 = PublicKey.from("sample-public-key-2".getBytes());
    // Using this LinkedHashSet instead of Set.of(...) to provide a defined iteration order.
    final LinkedHashSet<PublicKey> enclaveKeys = new LinkedHashSet<>();
    enclaveKeys.add(recipient1);
    enclaveKeys.add(recipient2);
    when(enclave.getPublicKeys()).thenReturn(enclaveKeys);
    when(enclave.unencryptTransaction(encodedPayloadForRecipient1, recipient1)).thenReturn(new byte[0]);
    when(enclave.unencryptTransaction(encodedPayloadForRecipient2, recipient2)).thenReturn(new byte[0]);
    when(enclave.unencryptTransaction(encodedPayloadForRecipient2, recipient1)).thenThrow(EncryptorException.class);
    searchRecipientKeyForPayload.execute(workflowEvent);
    final Set<EncodedPayload> updatedPayloads = workflowEvent.getPayloadsToPublish();
    assertThat(updatedPayloads).containsExactlyInAnyOrder(EncodedPayload.Builder.from(encodedPayloadForRecipient1).withRecipientKey(recipient1).build(), EncodedPayload.Builder.from(encodedPayloadForRecipient2).withRecipientKey(recipient2).build());
    verify(enclave).unencryptTransaction(encodedPayloadForRecipient1, recipient1);
    verify(enclave).unencryptTransaction(encodedPayloadForRecipient2, recipient1);
    verify(enclave).unencryptTransaction(encodedPayloadForRecipient2, recipient2);
    verify(enclave, times(2)).getPublicKeys();
    verifyNoMoreInteractions(enclave);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) PublicKey(com.quorum.tessera.encryption.PublicKey) EncodedPayload(com.quorum.tessera.enclave.EncodedPayload) MessageHash(com.quorum.tessera.data.MessageHash) EncryptedTransaction(com.quorum.tessera.data.EncryptedTransaction) Test(org.junit.Test)

Example 54 with EncodedPayload

use of com.quorum.tessera.enclave.EncodedPayload in project tessera by ConsenSys.

the class SingleEncodedPayloadPublisherTest method unsuccessfulPublishReturnsFalse.

@Test
public void unsuccessfulPublishReturnsFalse() {
    final PublicKey sampleKey = PublicKey.from("testkey".getBytes());
    final EncodedPayload samplePayload1 = EncodedPayload.Builder.create().withSenderKey(sampleKey).build();
    final EncodedPayload samplePayload2 = EncodedPayload.Builder.create().build();
    final BatchWorkflowContext context = new BatchWorkflowContext();
    context.setRecipientKey(sampleKey);
    context.setPayloadsToPublish(Set.of(samplePayload1, samplePayload2));
    doThrow(PublishPayloadException.class).when(payloadPublisher).publishPayload(samplePayload1, sampleKey);
    final boolean success = workflowPublisher.execute(context);
    assertThat(success).isFalse();
    verify(payloadPublisher).publishPayload(samplePayload1, sampleKey);
}
Also used : PublicKey(com.quorum.tessera.encryption.PublicKey) EncodedPayload(com.quorum.tessera.enclave.EncodedPayload) Test(org.junit.Test)

Example 55 with EncodedPayload

use of com.quorum.tessera.enclave.EncodedPayload in project tessera by ConsenSys.

the class RecoveryImplTest method testSyncPartialSuccess.

@Test
public void testSyncPartialSuccess() {
    StagingTransaction version1 = mock(StagingTransaction.class);
    StagingTransaction version2 = mock(StagingTransaction.class);
    when(version1.getHash()).thenReturn("TXN1");
    when(version2.getHash()).thenReturn("TXN1");
    when(stagingEntityDAO.retrieveTransactionBatchOrderByStageAndHash(anyInt(), anyInt())).thenReturn(List.of(version1, version2));
    when(stagingEntityDAO.countAll()).thenReturn(2L);
    EncodedPayload encodedPayload = mock(EncodedPayload.class);
    EncodedPayload encodedPayload2 = mock(EncodedPayload.class);
    when(version1.getEncodedPayload()).thenReturn(encodedPayload);
    when(version2.getEncodedPayload()).thenReturn(encodedPayload2);
    when(transactionManager.storePayload(encodedPayload)).thenThrow(PrivacyViolationException.class);
    RecoveryResult result = recovery.sync();
    assertThat(result).isEqualTo(RecoveryResult.PARTIAL_SUCCESS);
    verify(stagingEntityDAO).retrieveTransactionBatchOrderByStageAndHash(anyInt(), anyInt());
    verify(stagingEntityDAO, times(2)).countAll();
    verify(transactionManager).storePayload(encodedPayload);
    verify(transactionManager).storePayload(encodedPayload2);
}
Also used : StagingTransaction(com.quorum.tessera.data.staging.StagingTransaction) EncodedPayload(com.quorum.tessera.enclave.EncodedPayload) RecoveryResult(com.quorum.tessera.recovery.RecoveryResult) Test(org.junit.Test)

Aggregations

EncodedPayload (com.quorum.tessera.enclave.EncodedPayload)91 Test (org.junit.Test)60 PublicKey (com.quorum.tessera.encryption.PublicKey)50 PayloadEncoder (com.quorum.tessera.enclave.PayloadEncoder)23 Response (jakarta.ws.rs.core.Response)20 MessageHash (com.quorum.tessera.data.MessageHash)13 Collectors (java.util.stream.Collectors)12 EncryptedTransaction (com.quorum.tessera.data.EncryptedTransaction)11 PrivacyMode (com.quorum.tessera.enclave.PrivacyMode)11 EncodedPayloadCodec (com.quorum.tessera.enclave.EncodedPayloadCodec)9 NodeInfo (com.quorum.tessera.partyinfo.node.NodeInfo)8 Recipient (com.quorum.tessera.partyinfo.node.Recipient)8 java.util (java.util)8 Invocation (jakarta.ws.rs.client.Invocation)6 WebTarget (jakarta.ws.rs.client.WebTarget)6 StagingTransaction (com.quorum.tessera.data.staging.StagingTransaction)5 PrivacyMetadata (com.quorum.tessera.enclave.PrivacyMetadata)5 RecipientBox (com.quorum.tessera.enclave.RecipientBox)5 TxHash (com.quorum.tessera.enclave.TxHash)5 ResendRequest (com.quorum.tessera.p2p.resend.ResendRequest)5