Search in sources :

Example 21 with EncryptedTransaction

use of com.quorum.tessera.data.EncryptedTransaction in project tessera by ConsenSys.

the class LegacyResendManagerImplTest method performResendAll.

@Test
public void performResendAll() {
    final PublicKey targetResendKey = PublicKey.from("target".getBytes());
    final ResendRequest request = ResendRequest.Builder.create().withType(ResendRequest.ResendRequestType.ALL).withRecipient(targetResendKey).build();
    // Not bothered about going through the process, just make sure they are all loaded from the
    // database
    // We are not testing the workflow itself, only that the workflow gets the right amount of
    // transactions
    when(dao.transactionCount()).thenReturn(2L);
    when(dao.retrieveTransactions(0, 1)).thenReturn(List.of(new EncryptedTransaction()));
    when(dao.retrieveTransactions(1, 1)).thenReturn(List.of(new EncryptedTransaction()));
    final ResendResponse response = resendManager.resend(request);
    assertThat(response).isNotNull();
    assertThat(response.getPayload()).isNull();
    verify(enclave, times(2)).status();
    verify(dao).transactionCount();
    verify(dao).retrieveTransactions(0, 1);
    verify(dao).retrieveTransactions(1, 1);
}
Also used : ResendResponse(com.quorum.tessera.recovery.resend.ResendResponse) PublicKey(com.quorum.tessera.encryption.PublicKey) ResendRequest(com.quorum.tessera.recovery.resend.ResendRequest) EncryptedTransaction(com.quorum.tessera.data.EncryptedTransaction) Test(org.junit.Test)

Example 22 with EncryptedTransaction

use of com.quorum.tessera.data.EncryptedTransaction in project tessera by ConsenSys.

the class LegacyResendManagerImplTest method individualNonStandardPrivateTxFails.

@Test
public void individualNonStandardPrivateTxFails() {
    final EncodedPayload nonSPPayload = EncodedPayload.Builder.create().withPrivacyMode(PrivacyMode.PARTY_PROTECTION).build();
    final EncryptedTransaction databaseTx = new EncryptedTransaction();
    databaseTx.setPayload(nonSPPayload);
    when(dao.retrieveByHash(any(MessageHash.class))).thenReturn(Optional.of(databaseTx));
    final MessageHash txHash = new MessageHash("sample-hash".getBytes());
    final PublicKey targetResendKey = PublicKey.from("target".getBytes());
    final ResendRequest request = ResendRequest.Builder.create().withType(ResendRequest.ResendRequestType.INDIVIDUAL).withHash(txHash).withRecipient(targetResendKey).build();
    final Throwable throwable = catchThrowable(() -> resendManager.resend(request));
    assertThat(throwable).isInstanceOf(EnhancedPrivacyNotSupportedException.class).hasMessage("Cannot resend enhanced privacy transaction in legacy resend");
    verify(dao).retrieveByHash(txHash);
}
Also used : PublicKey(com.quorum.tessera.encryption.PublicKey) Assertions.catchThrowable(org.assertj.core.api.Assertions.catchThrowable) EncodedPayload(com.quorum.tessera.enclave.EncodedPayload) MessageHash(com.quorum.tessera.data.MessageHash) ResendRequest(com.quorum.tessera.recovery.resend.ResendRequest) EncryptedTransaction(com.quorum.tessera.data.EncryptedTransaction) EnhancedPrivacyNotSupportedException(com.quorum.tessera.transaction.exception.EnhancedPrivacyNotSupportedException) Test(org.junit.Test)

Example 23 with EncryptedTransaction

use of com.quorum.tessera.data.EncryptedTransaction in project tessera by ConsenSys.

the class LegacyResendManagerImplTest method targetKeyIsNotSenderOfTransaction.

@Test
public void targetKeyIsNotSenderOfTransaction() {
    final MessageHash txHash = new MessageHash("sample-hash".getBytes());
    final PublicKey targetResendKey = PublicKey.from("target".getBytes());
    final EncodedPayload nonSPPayload = EncodedPayload.Builder.create().withPrivacyMode(PrivacyMode.STANDARD_PRIVATE).build();
    final EncryptedTransaction databaseTx = new EncryptedTransaction();
    databaseTx.setPayload(nonSPPayload);
    final ResendRequest request = ResendRequest.Builder.create().withType(ResendRequest.ResendRequestType.INDIVIDUAL).withHash(txHash).withRecipient(targetResendKey).build();
    when(dao.retrieveByHash(any(MessageHash.class))).thenReturn(Optional.of(databaseTx));
    final ResendResponse response;
    try (var mockStatic = mockStatic(EncodedPayload.Builder.class)) {
        EncodedPayload.Builder builder = mock(EncodedPayload.Builder.class);
        mockStatic.when(() -> EncodedPayload.Builder.forRecipient(nonSPPayload, targetResendKey)).thenReturn(builder);
        when(builder.build()).thenReturn(nonSPPayload);
        response = resendManager.resend(request);
        mockStatic.verify(() -> EncodedPayload.Builder.forRecipient(nonSPPayload, targetResendKey));
    }
    assertThat(response).isNotNull();
    assertThat(response.getPayload()).isEqualTo(nonSPPayload);
    verify(dao).retrieveByHash(txHash);
}
Also used : ResendResponse(com.quorum.tessera.recovery.resend.ResendResponse) PublicKey(com.quorum.tessera.encryption.PublicKey) EncodedPayload(com.quorum.tessera.enclave.EncodedPayload) MessageHash(com.quorum.tessera.data.MessageHash) ResendRequest(com.quorum.tessera.recovery.resend.ResendRequest) EncryptedTransaction(com.quorum.tessera.data.EncryptedTransaction) Test(org.junit.Test)

Example 24 with EncryptedTransaction

use of com.quorum.tessera.data.EncryptedTransaction in project tessera by ConsenSys.

the class LegacyResendWorkflowFactoryTest method successForAllStagesReturnsTrue.

@Test
public void successForAllStagesReturnsTrue() {
    final PublicKey targetResendKey = PublicKey.from("target".getBytes());
    final PublicKey localRecipient = PublicKey.from("local-recipient".getBytes());
    final EncodedPayload payload = EncodedPayload.Builder.create().withSenderKey(targetResendKey).withRecipientBox("testbox".getBytes()).build();
    final NodeInfo nodeInfo = mock(NodeInfo.class);
    when(nodeInfo.getRecipients()).thenReturn(Set.of(Recipient.of(targetResendKey, "url")));
    when(discovery.getCurrent()).thenReturn(nodeInfo);
    final EncryptedTransaction encryptedTx = mock(EncryptedTransaction.class);
    when(enclave.getPublicKeys()).thenReturn(Set.of(localRecipient));
    when(enclave.unencryptTransaction(any(EncodedPayload.class), eq(localRecipient))).thenReturn(new byte[0]);
    final BatchWorkflow batchWorkflow = wfFactory.create();
    final BatchWorkflowContext context = new BatchWorkflowContext();
    context.setEncryptedTransaction(encryptedTx);
    context.setEncodedPayload(payload);
    context.setRecipientKey(targetResendKey);
    context.setBatchSize(1);
    final boolean success = batchWorkflow.execute(context);
    assertThat(success).isTrue();
    verify(discovery).getCurrent();
    verify(payloadPublisher).publishPayload(any(EncodedPayload.class), eq(targetResendKey));
    verify(enclave).status();
    verify(enclave, times(2)).getPublicKeys();
    verify(enclave).unencryptTransaction(any(EncodedPayload.class), eq(localRecipient));
}
Also used : PublicKey(com.quorum.tessera.encryption.PublicKey) NodeInfo(com.quorum.tessera.partyinfo.node.NodeInfo) EncodedPayload(com.quorum.tessera.enclave.EncodedPayload) EncryptedTransaction(com.quorum.tessera.data.EncryptedTransaction) Test(org.junit.Test)

Example 25 with EncryptedTransaction

use of com.quorum.tessera.data.EncryptedTransaction 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)

Aggregations

EncryptedTransaction (com.quorum.tessera.data.EncryptedTransaction)32 Test (org.junit.Test)24 PublicKey (com.quorum.tessera.encryption.PublicKey)21 MessageHash (com.quorum.tessera.data.MessageHash)19 EncodedPayload (com.quorum.tessera.enclave.EncodedPayload)13 Collectors (java.util.stream.Collectors)6 IntStream (java.util.stream.IntStream)6 ResendRequest (com.quorum.tessera.recovery.resend.ResendRequest)5 BatchWorkflow (com.quorum.tessera.recovery.workflow.BatchWorkflow)5 BatchWorkflowContext (com.quorum.tessera.recovery.workflow.BatchWorkflowContext)5 EncryptedTransactionDAO (com.quorum.tessera.data.EncryptedTransactionDAO)4 ResendResponse (com.quorum.tessera.recovery.resend.ResendResponse)4 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)4 After (org.junit.After)4 Before (org.junit.Before)4 Base64Codec (com.quorum.tessera.base64.Base64Codec)3 StagingEntityDAO (com.quorum.tessera.data.staging.StagingEntityDAO)3 StagingTransaction (com.quorum.tessera.data.staging.StagingTransaction)3 com.quorum.tessera.enclave (com.quorum.tessera.enclave)3 PrivacyMode (com.quorum.tessera.enclave.PrivacyMode)3