Search in sources :

Example 66 with EncodedPayload

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

the class EncryptedTransactionDAOTest method validEncryptedTransactionCanBePersisted.

@Test
public void validEncryptedTransactionCanBePersisted() {
    PayloadEncoder payloadEncoder = mock(PayloadEncoder.class);
    final byte[] payloadData = "PAYLOADATA".getBytes();
    EncodedPayload encodedPayload = mock(EncodedPayload.class);
    when(payloadEncoder.encode(encodedPayload)).thenReturn(payloadData);
    when(payloadEncoder.decode(payloadData)).thenReturn(encodedPayload);
    try (var createEncoderFunction = mockStatic(PayloadEncoder.class)) {
        createEncoderFunction.when(() -> PayloadEncoder.create(EncodedPayloadCodec.current())).thenReturn(payloadEncoder);
        final EncryptedTransaction encryptedTransaction = new EncryptedTransaction();
        encryptedTransaction.setPayload(encodedPayload);
        encryptedTransaction.setHash(new MessageHash(new byte[] { 1 }));
        encryptedTransactionDAO.save(encryptedTransaction);
        EntityManager entityManager = entityManagerFactory.createEntityManager();
        entityManager.getTransaction().begin();
        final EncryptedTransaction retrieved = entityManager.find(EncryptedTransaction.class, encryptedTransaction.getHash());
        assertThat(retrieved).usingRecursiveComparison().isEqualTo(encryptedTransaction);
        entityManager.getTransaction().rollback();
    }
}
Also used : PayloadEncoder(com.quorum.tessera.enclave.PayloadEncoder) EncodedPayload(com.quorum.tessera.enclave.EncodedPayload)

Example 67 with EncodedPayload

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

the class EncryptedTransactionDAOTest method updateTransaction.

@Test
public void updateTransaction() {
    PayloadEncoder payloadEncoder = mock(PayloadEncoder.class);
    final byte[] initialData = "DATA1".getBytes();
    EncodedPayload encodedPayload = mock(EncodedPayload.class);
    when(payloadEncoder.encode(encodedPayload)).thenReturn(initialData);
    when(payloadEncoder.decode(initialData)).thenReturn(encodedPayload);
    final byte[] updatedData = "DATA2".getBytes();
    EncodedPayload updatedPayload = mock(EncodedPayload.class);
    when(payloadEncoder.encode(updatedPayload)).thenReturn(updatedData);
    when(payloadEncoder.decode(updatedData)).thenReturn(updatedPayload);
    try (var createEncoderFunction = mockStatic(PayloadEncoder.class)) {
        createEncoderFunction.when(() -> PayloadEncoder.create(EncodedPayloadCodec.current())).thenReturn(payloadEncoder);
        final EncryptedTransaction encryptedTransaction = new EncryptedTransaction();
        encryptedTransaction.setPayload(encodedPayload);
        encryptedTransaction.setHash(new MessageHash(new byte[] { 1 }));
        encryptedTransactionDAO.save(encryptedTransaction);
        EntityManager entityManager = entityManagerFactory.createEntityManager();
        entityManager.getTransaction().begin();
        final EncryptedTransaction retrieved = entityManager.find(EncryptedTransaction.class, encryptedTransaction.getHash());
        assertThat(retrieved).isNotNull().usingRecursiveComparison().isEqualTo(encryptedTransaction);
        encryptedTransaction.setPayload(updatedPayload);
        encryptedTransaction.setEncodedPayload(new byte[] { 0 });
        encryptedTransactionDAO.update(encryptedTransaction);
        entityManager.getTransaction().rollback();
        entityManager.getTransaction().begin();
        final EncryptedTransaction after = entityManager.find(EncryptedTransaction.class, encryptedTransaction.getHash());
        assertThat(after.getPayload()).isEqualTo(updatedPayload);
        assertThat(after.getEncodedPayload()).isEqualTo(updatedData);
        entityManager.getTransaction().rollback();
    }
    verify(payloadEncoder, times(2)).encode(any());
    verify(payloadEncoder, times(3)).decode(any());
    verifyNoMoreInteractions(payloadEncoder);
}
Also used : PayloadEncoder(com.quorum.tessera.enclave.PayloadEncoder) EncodedPayload(com.quorum.tessera.enclave.EncodedPayload)

Example 68 with EncodedPayload

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

the class EncryptedTransactionDAOTest method deleteTransactionRemovesFromDatabaseAndReturnsTrue.

@Test
public void deleteTransactionRemovesFromDatabaseAndReturnsTrue() {
    PayloadEncoder payloadEncoder = mock(PayloadEncoder.class);
    final byte[] payloadData = "PAYLOADATA".getBytes();
    EncodedPayload encodedPayload = mock(EncodedPayload.class);
    when(payloadEncoder.encode(encodedPayload)).thenReturn(payloadData);
    try (var createEncoderFunction = mockStatic(PayloadEncoder.class)) {
        createEncoderFunction.when(() -> PayloadEncoder.create(EncodedPayloadCodec.current())).thenReturn(payloadEncoder);
        final MessageHash messageHash = new MessageHash(UUID.randomUUID().toString().getBytes());
        EntityManager entityManager = entityManagerFactory.createEntityManager();
        // put a transaction in the database
        final EncryptedTransaction encryptedTransaction = new EncryptedTransaction();
        encryptedTransaction.setPayload(encodedPayload);
        encryptedTransaction.setHash(messageHash);
        entityManager.getTransaction().begin();
        entityManager.persist(encryptedTransaction);
        entityManager.getTransaction().commit();
        Query countQuery = entityManager.createQuery("select count(t) from EncryptedTransaction t where t.hash = :hash");
        Long result = (Long) countQuery.setParameter("hash", messageHash).getSingleResult();
        assertThat(result).isEqualTo(1L);
        encryptedTransactionDAO.delete(messageHash);
        // check it is not longer in the database
        Long result2 = (Long) countQuery.setParameter("hash", messageHash).getSingleResult();
        assertThat(result2).isZero();
    }
    verify(payloadEncoder).decode(payloadData);
    verify(payloadEncoder).encode(encodedPayload);
    verifyNoMoreInteractions(payloadEncoder);
}
Also used : PayloadEncoder(com.quorum.tessera.enclave.PayloadEncoder) EncodedPayload(com.quorum.tessera.enclave.EncodedPayload)

Example 69 with EncodedPayload

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

the class StagingTransactionListener method onLoad.

@PostLoad
public void onLoad(StagingTransaction stagingTransaction) {
    LOGGER.debug("onLoad[{}]", stagingTransaction);
    final EncodedPayloadCodec encodedPayloadCodec = stagingTransaction.getEncodedPayloadCodec();
    final byte[] encodedPayloadData = stagingTransaction.getPayload();
    final PayloadEncoder payloadEncoder = PayloadEncoder.create(encodedPayloadCodec);
    final EncodedPayload encodedPayload = payloadEncoder.decode(encodedPayloadData);
    stagingTransaction.setEncodedPayload(encodedPayload);
}
Also used : PayloadEncoder(com.quorum.tessera.enclave.PayloadEncoder) EncodedPayload(com.quorum.tessera.enclave.EncodedPayload) EncodedPayloadCodec(com.quorum.tessera.enclave.EncodedPayloadCodec) PostLoad(jakarta.persistence.PostLoad)

Example 70 with EncodedPayload

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

the class EncryptedTransactionListenerTest method onSave.

@Test
public void onSave() {
    EncodedPayload encodedPayload = mock(EncodedPayload.class);
    EncryptedTransaction encryptedTransaction = new EncryptedTransaction();
    encryptedTransaction.setPayload(encodedPayload);
    byte[] payloadData = "PayloadData".getBytes();
    when(payloadEncoder.encode(encodedPayload)).thenReturn(payloadData);
    encryptedTransactionListener.onSave(encryptedTransaction);
    verify(payloadEncoder).encode(encodedPayload);
    payloadEncoderFactoryFunction.verify(() -> PayloadEncoder.create(EncodedPayloadCodec.current()));
    assertThat(encryptedTransaction.getEncodedPayload()).isEqualTo(payloadData);
}
Also used : EncodedPayload(com.quorum.tessera.enclave.EncodedPayload) 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