Search in sources :

Example 6 with EncodedPayload

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

the class EncryptedTransactionDAOTest method saveTransactionWithCallbackRuntimeException.

@Test
public void saveTransactionWithCallbackRuntimeException() throws Exception {
    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);
        MessageHash transactionHash = new MessageHash(UUID.randomUUID().toString().getBytes());
        EncryptedTransaction transaction = new EncryptedTransaction();
        transaction.setHash(transactionHash);
        transaction.setPayload(encodedPayload);
        Callable<Void> callback = mock(Callable.class);
        when(callback.call()).thenThrow(new RuntimeException("OUCH"));
        try {
            encryptedTransactionDAO.save(transaction, callback);
            failBecauseExceptionWasNotThrown(RuntimeException.class);
        } catch (RuntimeException ex) {
            assertThat(ex).isNotNull().hasMessageContaining("OUCH");
        }
        EntityManager entityManager = entityManagerFactory.createEntityManager();
        final EncryptedTransaction result = entityManager.find(EncryptedTransaction.class, transactionHash);
        assertThat(result).isNull();
        verify(callback).call();
    }
    verify(payloadEncoder).encode(encodedPayload);
    verifyNoMoreInteractions(payloadEncoder);
}
Also used : PayloadEncoder(com.quorum.tessera.enclave.PayloadEncoder) EncodedPayload(com.quorum.tessera.enclave.EncodedPayload)

Example 7 with EncodedPayload

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

the class EncryptedTransactionDAOTest method cannotPersistMultipleOfSameHash.

@Test
public void cannotPersistMultipleOfSameHash() {
    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(new byte[] { 1 });
        final EncryptedTransaction encryptedTransaction = new EncryptedTransaction();
        encryptedTransaction.setPayload(encodedPayload);
        encryptedTransaction.setHash(messageHash);
        EntityManager entityManager = entityManagerFactory.createEntityManager();
        entityManager.getTransaction().begin();
        entityManager.persist(encryptedTransaction);
        entityManager.getTransaction().commit();
        final EncryptedTransaction duplicateTransaction = new EncryptedTransaction();
        duplicateTransaction.setPayload(encodedPayload);
        duplicateTransaction.setHash(messageHash);
        try {
            encryptedTransactionDAO.save(encryptedTransaction);
            failBecauseExceptionWasNotThrown(PersistenceException.class);
        } catch (PersistenceException ex) {
            assertThat(ex).isInstanceOf(PersistenceException.class).hasMessageContaining(testConfig.getUniqueConstraintViolationMessage());
        }
        verify(payloadEncoder, times(2)).encode(encodedPayload);
        verifyNoMoreInteractions(payloadEncoder);
    }
}
Also used : PayloadEncoder(com.quorum.tessera.enclave.PayloadEncoder) EncodedPayload(com.quorum.tessera.enclave.EncodedPayload)

Example 8 with EncodedPayload

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

the class EncryptedTransactionListenerTest method onLoadLegacyEncodedData.

@Test
public void onLoadLegacyEncodedData() {
    byte[] payloadData = "PayloadData".getBytes();
    EncodedPayload payload = mock(EncodedPayload.class);
    when(payloadEncoder.decode(payloadData)).thenReturn(payload);
    EncryptedTransaction encryptedTransaction = new EncryptedTransaction();
    encryptedTransaction.setEncodedPayload(payloadData);
    encryptedTransactionListener.onLoad(encryptedTransaction);
    verify(payloadEncoder).decode(payloadData);
    payloadEncoderFactoryFunction.verify(() -> PayloadEncoder.create(eq(EncodedPayloadCodec.LEGACY)));
    assertThat(encryptedTransaction.getPayload()).isEqualTo(payload);
    assertThat(encryptedTransaction.getEncodedPayloadCodec()).isEqualTo(EncodedPayloadCodec.LEGACY);
}
Also used : EncodedPayload(com.quorum.tessera.enclave.EncodedPayload) Test(org.junit.Test)

Example 9 with EncodedPayload

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

the class EncryptedTransactionListenerTest method onLoad.

@Test
public void onLoad() {
    byte[] payloadData = "PayloadData".getBytes();
    EncodedPayload payload = mock(EncodedPayload.class);
    when(payloadEncoder.decode(payloadData)).thenReturn(payload);
    EncryptedTransaction encryptedTransaction = new EncryptedTransaction();
    encryptedTransaction.setEncodedPayloadCodec(EncodedPayloadCodec.CBOR);
    encryptedTransaction.setEncodedPayload(payloadData);
    encryptedTransactionListener.onLoad(encryptedTransaction);
    verify(payloadEncoder).decode(payloadData);
    payloadEncoderFactoryFunction.verify(() -> PayloadEncoder.create(any(EncodedPayloadCodec.class)));
    assertThat(encryptedTransaction.getPayload()).isEqualTo(payload);
}
Also used : EncodedPayload(com.quorum.tessera.enclave.EncodedPayload) Test(org.junit.Test)

Example 10 with EncodedPayload

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

the class ResendAllIT method transactionsAreReconstructedFromMultipleParties.

@Test
public void transactionsAreReconstructedFromMultipleParties() throws UnsupportedEncodingException, SQLException {
    // setup (sending in a tx)
    Response sendRawResponse = partyOne.getRestClient().target(partyOne.getQ2TUri()).path("/sendraw").request().header("c11n-from", partyOne.getPublicKey()).header("c11n-to", partyTwo.getPublicKey() + "," + partyThree.getPublicKey()).post(Entity.entity(transactionData, MediaType.APPLICATION_OCTET_STREAM));
    URI location = sendRawResponse.getLocation();
    String hash = sendRawResponse.readEntity(String.class);
    final String encodedHash = URLEncoder.encode(hash, UTF_8.toString());
    // delete it from the sender node
    final Response deleteReq = partyOne.getRestClient().target(location).request().delete();
    assertThat(deleteReq).isNotNull();
    assertThat(deleteReq.getStatus()).isEqualTo(204);
    // check it is deleted
    final Response deleteCheck = partyOne.getRestClient().target(partyOne.getQ2TUri()).path("transaction").path(encodedHash).request().get();
    assertThat(deleteCheck).isNotNull();
    assertThat(deleteCheck.getStatus()).isEqualTo(404);
    // request resend from recipients
    final ResendRequest req = new ResendRequest();
    req.setType(RESEND_ALL_VALUE);
    req.setPublicKey(partyOne.getPublicKey());
    final Response resendRequest = vanillaHttpOnlyClient.target(partyTwo.getP2PUri()).path(RESEND_PATH).request().buildPost(Entity.entity(req, MediaType.APPLICATION_JSON_TYPE)).invoke();
    assertThat(resendRequest).isNotNull();
    assertThat(resendRequest.getStatus()).isEqualTo(200);
    final Response resendRequestNode3 = vanillaHttpOnlyClient.target(partyThree.getP2PUri()).path(RESEND_PATH).request().buildPost(Entity.entity(req, MediaType.APPLICATION_JSON_TYPE)).invoke();
    assertThat(resendRequestNode3).isNotNull();
    assertThat(resendRequestNode3.getStatus()).isEqualTo(200);
    final String fetch = "SELECT ENCODED_PAYLOAD, PAYLOAD_CODEC FROM ENCRYPTED_TRANSACTION WHERE HASH = ?";
    final Connection databaseConnection = PartyHelper.create().findByPublicKey(partyOne.getPublicKey()).getDatabaseConnection();
    try (PreparedStatement statement = databaseConnection.prepareStatement(fetch)) {
        statement.setBytes(1, Base64.getDecoder().decode(hash));
        try (ResultSet rs = statement.executeQuery()) {
            assertThat(rs.next()).isTrue();
            final byte[] output = rs.getBytes(1);
            final EncodedPayloadCodec codec = EncodedPayloadCodec.valueOf(rs.getString(2));
            final PayloadEncoder encoderToUse = PayloadEncoder.create(codec);
            final EncodedPayload payload = encoderToUse.decode(output);
            assertThat(payload.getRecipientKeys()).hasSize(3);
            assertThat(payload.getSenderKey().encodeToBase64()).isEqualTo(partyOne.getPublicKey());
            assertThat(payload.getRecipientBoxes()).hasSize(3);
        }
    }
}
Also used : Response(jakarta.ws.rs.core.Response) PayloadEncoder(com.quorum.tessera.enclave.PayloadEncoder) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) EncodedPayload(com.quorum.tessera.enclave.EncodedPayload) URI(java.net.URI) ResendRequest(com.quorum.tessera.p2p.resend.ResendRequest) EncodedPayloadCodec(com.quorum.tessera.enclave.EncodedPayloadCodec) 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