Search in sources :

Example 41 with EncodedPayload

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

the class FilterPayloadTest method testCurrentNodeIsSender.

@Test
public void testCurrentNodeIsSender() {
    BatchWorkflowContext context = new BatchWorkflowContext();
    PublicKey sender = mock(PublicKey.class);
    PublicKey recipient = mock(PublicKey.class);
    EncodedPayload encodedPayload = mock(EncodedPayload.class);
    when(encodedPayload.getSenderKey()).thenReturn(sender);
    when(encodedPayload.getRecipientKeys()).thenReturn(List.of(sender, recipient));
    context.setEncodedPayload(encodedPayload);
    context.setRecipientKey(recipient);
    when(enclave.getPublicKeys()).thenReturn(Set.of(sender));
    boolean result = filterPayload.execute(context);
    assertThat(result).isTrue();
    verify(enclave).getPublicKeys();
}
Also used : PublicKey(com.quorum.tessera.encryption.PublicKey) EncodedPayload(com.quorum.tessera.enclave.EncodedPayload) Test(org.junit.Test)

Example 42 with EncodedPayload

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

the class FilterPayloadTest method testRequestedNodeIsSender.

@Test
public void testRequestedNodeIsSender() {
    BatchWorkflowContext context = new BatchWorkflowContext();
    PublicKey sender = mock(PublicKey.class);
    PublicKey recipient = mock(PublicKey.class);
    EncodedPayload encodedPayload = mock(EncodedPayload.class);
    when(encodedPayload.getSenderKey()).thenReturn(recipient);
    when(encodedPayload.getRecipientKeys()).thenReturn(List.of(sender, recipient));
    context.setEncodedPayload(encodedPayload);
    context.setRecipientKey(recipient);
    when(enclave.getPublicKeys()).thenReturn(Set.of(sender));
    boolean result = filterPayload.filter(context);
    assertThat(result).isTrue();
    verify(enclave).getPublicKeys();
}
Also used : PublicKey(com.quorum.tessera.encryption.PublicKey) EncodedPayload(com.quorum.tessera.enclave.EncodedPayload) Test(org.junit.Test)

Example 43 with EncodedPayload

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

the class PartyInfoResourceTest method partyInfoValidateThrowsException.

@Test
public void partyInfoValidateThrowsException() {
    String url = "http://www.bogus.com";
    PublicKey myKey = PublicKey.from("myKey".getBytes());
    PublicKey recipientKey = PublicKey.from("recipientKey".getBytes());
    String message = "I love sparrows";
    byte[] payload = message.getBytes();
    Recipient recipient = Recipient.of(recipientKey, url);
    Set<Recipient> recipientList = Collections.singleton(recipient);
    PartyInfo partyInfo = new PartyInfo(url, recipientList, Collections.emptySet());
    when(partyInfoParser.from(payload)).thenReturn(partyInfo);
    when(enclave.defaultPublicKey()).thenReturn(myKey);
    when(partyInfoParser.to(partyInfo)).thenReturn(payload);
    EncodedPayload encodedPayload = mock(EncodedPayload.class);
    when(enclave.encryptPayload(any(byte[].class), any(PublicKey.class), anyList(), any(PrivacyMetadata.class))).thenReturn(encodedPayload);
    when(payloadEncoder.encode(encodedPayload)).thenReturn(payload);
    WebTarget webTarget = mock(WebTarget.class);
    when(restClient.target(url)).thenReturn(webTarget);
    when(webTarget.path(anyString())).thenReturn(webTarget);
    Invocation.Builder invocationBuilder = mock(Invocation.Builder.class);
    when(webTarget.request()).thenReturn(invocationBuilder);
    when(invocationBuilder.post(any(Entity.class))).thenThrow(new UncheckedIOException(new IOException("GURU meditation")));
    try {
        partyInfoResource.partyInfo(payload, null);
        failBecauseExceptionWasNotThrown(SecurityException.class);
    } catch (SecurityException ex) {
        verify(partyInfoParser).from(payload);
        verify(enclave).defaultPublicKey();
        verify(enclave).encryptPayload(any(byte[].class), any(PublicKey.class), anyList(), any(PrivacyMetadata.class));
        verify(payloadEncoder).encode(encodedPayload);
        verify(restClient).target(url);
    }
}
Also used : Entity(jakarta.ws.rs.client.Entity) Invocation(jakarta.ws.rs.client.Invocation) PublicKey(com.quorum.tessera.encryption.PublicKey) Recipient(com.quorum.tessera.partyinfo.model.Recipient) EncodedPayload(com.quorum.tessera.enclave.EncodedPayload) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) PartyInfo(com.quorum.tessera.partyinfo.model.PartyInfo) PrivacyMetadata(com.quorum.tessera.enclave.PrivacyMetadata) WebTarget(jakarta.ws.rs.client.WebTarget) Test(org.junit.Test)

Example 44 with EncodedPayload

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

the class PartyInfoResourceTest method partyInfoValidationEncryptsUniqueDataForEachKey.

@Test
public void partyInfoValidationEncryptsUniqueDataForEachKey() {
    String url = "http://bogus";
    Set<Party> parties = Collections.emptySet();
    Set<Recipient> recipients = new HashSet<>();
    recipients.add(Recipient.of(mock(PublicKey.class), url));
    recipients.add(Recipient.of(mock(PublicKey.class), url));
    PartyInfo partyInfo = new PartyInfo(url, recipients, parties);
    byte[] payload = new byte[] {};
    when(partyInfoParser.from(payload)).thenReturn(partyInfo);
    when(enclave.defaultPublicKey()).thenReturn(PublicKey.from("defaultKey".getBytes()));
    EncodedPayload encodedPayload = mock(EncodedPayload.class);
    List<String> uuidList = new ArrayList<>();
    doAnswer((invocation) -> {
        byte[] d = invocation.getArgument(0);
        uuidList.add(new String(d));
        return encodedPayload;
    }).when(enclave).encryptPayload(any(byte[].class), any(PublicKey.class), anyList(), any(PrivacyMetadata.class));
    when(payloadEncoder.encode(any(EncodedPayload.class))).thenReturn("somedata".getBytes());
    WebTarget webTarget = mock(WebTarget.class);
    when(restClient.target(url)).thenReturn(webTarget);
    when(webTarget.path(anyString())).thenReturn(webTarget);
    Invocation.Builder invocationBuilder = mock(Invocation.Builder.class);
    when(webTarget.request()).thenReturn(invocationBuilder);
    Response response = mock(Response.class);
    when(invocationBuilder.post(any(Entity.class))).thenReturn(response);
    when(response.getStatus()).thenReturn(200);
    when(response.getEntity()).thenReturn("");
    doAnswer(new Answer() {

        private int i = 0;

        public Object answer(InvocationOnMock invocation) {
            String result = uuidList.get(i);
            i++;
            return result;
        }
    }).when(response).readEntity(String.class);
    // the test
    partyInfoResource.partyInfo(payload, null);
    ArgumentCaptor<byte[]> uuidCaptor = ArgumentCaptor.forClass(byte[].class);
    verify(enclave, times(2)).encryptPayload(uuidCaptor.capture(), any(PublicKey.class), anyList(), any(PrivacyMetadata.class));
    List<byte[]> capturedUUIDs = uuidCaptor.getAllValues();
    assertThat(capturedUUIDs).hasSize(2);
    assertThat(capturedUUIDs.get(0)).isNotEqualTo(capturedUUIDs.get(1));
    // other verifications
    verify(discovery).onUpdate(any(NodeInfo.class));
    verify(partyInfoParser).from(payload);
    verify(enclave).defaultPublicKey();
    verify(payloadEncoder, times(2)).encode(encodedPayload);
    verify(restClient, times(2)).target(url);
}
Also used : Entity(jakarta.ws.rs.client.Entity) Invocation(jakarta.ws.rs.client.Invocation) Party(com.quorum.tessera.partyinfo.model.Party) PrivacyMetadata(com.quorum.tessera.enclave.PrivacyMetadata) PublicKey(com.quorum.tessera.encryption.PublicKey) Recipient(com.quorum.tessera.partyinfo.model.Recipient) EncodedPayload(com.quorum.tessera.enclave.EncodedPayload) PartyInfo(com.quorum.tessera.partyinfo.model.PartyInfo) Response(jakarta.ws.rs.core.Response) Answer(org.mockito.stubbing.Answer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) NodeInfo(com.quorum.tessera.partyinfo.node.NodeInfo) JsonObject(jakarta.json.JsonObject) WebTarget(jakarta.ws.rs.client.WebTarget) Test(org.junit.Test)

Example 45 with EncodedPayload

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

the class PartyInfoResourceTest method validate.

@Test
public void validate() {
    String message = UUID.randomUUID().toString();
    byte[] payload = message.getBytes();
    PublicKey myKey = PublicKey.from("myKey".getBytes());
    EncodedPayload encodedPayload = mock(EncodedPayload.class);
    when(encodedPayload.getRecipientKeys()).thenReturn(Collections.singletonList(myKey));
    when(payloadEncoder.decode(payload)).thenReturn(encodedPayload);
    when(enclave.unencryptTransaction(encodedPayload, myKey)).thenReturn(message.getBytes());
    Response result = partyInfoResource.validate(payload);
    assertThat(result.getStatus()).isEqualTo(200);
    assertThat(result.getEntity()).isEqualTo(message);
    verify(payloadEncoder).decode(payload);
    verify(enclave).unencryptTransaction(encodedPayload, myKey);
}
Also used : Response(jakarta.ws.rs.core.Response) PublicKey(com.quorum.tessera.encryption.PublicKey) 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